You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-commits@xmlgraphics.apache.org by dv...@apache.org on 2006/11/09 15:33:40 UTC
svn commit: r472915 -
/xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/CompositeGraphicsNode.java
Author: dvholten
Date: Thu Nov 9 06:33:38 2006
New Revision: 472915
URL: http://svn.apache.org/viewvc?view=rev&rev=472915
Log:
make some members volatile, comments
Modified:
xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/CompositeGraphicsNode.java
Modified: xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/CompositeGraphicsNode.java
URL: http://svn.apache.org/viewvc/xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/CompositeGraphicsNode.java?view=diff&rev=472915&r1=472914&r2=472915
==============================================================================
--- xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/CompositeGraphicsNode.java (original)
+++ xmlgraphics/batik/trunk/sources/org/apache/batik/gvt/CompositeGraphicsNode.java Thu Nov 9 06:33:38 2006
@@ -1,6 +1,6 @@
/*
- Copyright 2000-2003 The Apache Software Foundation
+ Copyright 2000-2003, 2006 The Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -36,10 +36,12 @@
/**
* A CompositeGraphicsNode is a graphics node that can contain graphics nodes.
*
+ * <br>Note: this class is a 'little bit aware of' other threads, but not really threadsafe.
+ *
* @author <a href="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
* @version $Id$
*/
-public class CompositeGraphicsNode extends AbstractGraphicsNode
+public class CompositeGraphicsNode extends AbstractGraphicsNode
implements List {
public static final Rectangle2D VIEWPORT = new Rectangle();
@@ -53,12 +55,12 @@
/**
* The number of children of this composite graphics node.
*/
- protected int count;
+ protected volatile int count;
/**
* The number of times the children list has been structurally modified.
*/
- protected int modCount;
+ protected volatile int modCount;
/**
* This flag indicates if this node has BackgroundEnable = 'new'.
@@ -70,17 +72,17 @@
* Internal Cache: Geometry bounds for this node, not taking into
* account any of its children rendering attributes into account
*/
- private Rectangle2D geometryBounds;
+ private volatile Rectangle2D geometryBounds;
/**
* Internal Cache: Primitive bounds.
*/
- private Rectangle2D primitiveBounds;
+ private volatile Rectangle2D primitiveBounds;
/**
* Internal Cache: Sensitive bounds.
*/
- private Rectangle2D sensitiveBounds;
+ private volatile Rectangle2D sensitiveBounds;
/**
* Internal Cache: the outline.
@@ -147,9 +149,12 @@
return;
}
+ // Thread.currentThread() is potentially expensive, so reuse my instance in hasBeenHalted()
+ Thread currentThread = Thread.currentThread();
+
// Paint children
for (int i=0; i < count; ++i) {
- if (HaltingThread.hasBeenHalted())
+ if (HaltingThread.hasBeenHalted( currentThread ))
return;
GraphicsNode node = children[i];
@@ -192,14 +197,17 @@
return primitiveBounds;
}
+ // Thread.currentThread() is potentially expensive, so reuse my instance in hasBeenHalted()
+ Thread currentThread = Thread.currentThread();
+
int i=0;
Rectangle2D bounds = null;
while ((bounds == null) && i < count) {
bounds = children[i++].getTransformedBounds(IDENTITY);
- if (((i & 0x0F) == 0) && HaltingThread.hasBeenHalted())
+ if (((i & 0x0F) == 0) && HaltingThread.hasBeenHalted( currentThread ))
break; // check every 16 children if we have been interrupted.
}
- if (HaltingThread.hasBeenHalted()) {
+ if (HaltingThread.hasBeenHalted( currentThread )) {
invalidateGeometryCache();
return null;
}
@@ -210,9 +218,9 @@
}
primitiveBounds = bounds;
- Rectangle2D ctb = null;
+
while (i < count) {
- ctb = children[i++].getTransformedBounds(IDENTITY);
+ Rectangle2D ctb = children[i++].getTransformedBounds(IDENTITY);
if (ctb != null) {
if (primitiveBounds == null) {
// another thread has set the primitive bounds to null,
@@ -223,12 +231,12 @@
}
}
- if (((i & 0x0F) == 0) && HaltingThread.hasBeenHalted())
+ if (((i & 0x0F) == 0) && HaltingThread.hasBeenHalted( currentThread ))
break; // check every 16 children if we have been interrupted.
}
-
+
// Check If we should halt early.
- if (HaltingThread.hasBeenHalted()) {
+ if (HaltingThread.hasBeenHalted( currentThread )) {
// The Thread has been halted.
// Invalidate any cached values and proceed.
invalidateGeometryCache();
@@ -261,7 +269,7 @@
}
return new Rectangle2D.Float
- ((float)(x*sx+t.getTranslateX()),
+ ((float)(x*sx+t.getTranslateX()),
(float)(y*sy+t.getTranslateY()),
(float)(w*sx), (float)(h*sy));
}
@@ -272,7 +280,8 @@
* transform (if any).
*
* @param txf the affine transform with which this node's transform should
- * be concatenated. Should not be null. */
+ * be concatenated. Should not be null.
+ */
public Rectangle2D getTransformedPrimitiveBounds(AffineTransform txf) {
AffineTransform t = txf;
if (transform != null) {
@@ -284,16 +293,15 @@
// No rotation it's safe to simply transform our bounding box.
return getTransformedBBox(getPrimitiveBounds(), t);
}
-
+
int i = 0;
Rectangle2D tpb = null;
while (tpb == null && i < count) {
tpb = children[i++].getTransformedBounds(t);
}
- Rectangle2D ctb = null;
while (i < count) {
- ctb = children[i++].getTransformedBounds(t);
+ Rectangle2D ctb = children[i++].getTransformedBounds(t);
if(ctb != null){
tpb.add(ctb);
}
@@ -304,7 +312,7 @@
/**
* Returns the bounds of the area covered by this node, without
- * taking any of its rendering attribute into account. That is,
+ * taking any of its rendering attributes into account. That is,
* exclusive of any clipping, masking, filtering or stroking, for
* example.
*/
@@ -313,13 +321,12 @@
// System.err.println("geometryBounds are null");
int i=0;
while(geometryBounds == null && i < count){
- geometryBounds =
- children[i++].getTransformedGeometryBounds (IDENTITY);
+ geometryBounds =
+ children[i++].getTransformedGeometryBounds (IDENTITY);
}
- Rectangle2D cgb = null;
while (i<count) {
- cgb = children[i++].getTransformedGeometryBounds(IDENTITY);
+ Rectangle2D cgb = children[i++].getTransformedGeometryBounds(IDENTITY);
if (cgb != null) {
if (geometryBounds == null) {
// another thread has set the geometry bounds to null,
@@ -356,13 +363,13 @@
// No rotation it's safe to simply transform our bounding box.
return getTransformedBBox(getGeometryBounds(), t);
}
-
+
Rectangle2D gb = null;
int i=0;
while (gb == null && i < count) {
gb = children[i++].getTransformedGeometryBounds(t);
}
-
+
Rectangle2D cgb = null;
while (i < count) {
cgb = children[i++].getTransformedGeometryBounds(t);
@@ -386,19 +393,18 @@
// System.out.println("sensitiveBoundsBounds are null");
int i=0;
while(sensitiveBounds == null && i < count){
- sensitiveBounds =
+ sensitiveBounds =
children[i++].getTransformedSensitiveBounds(IDENTITY);
}
- Rectangle2D cgb = null;
while (i<count) {
- cgb = children[i++].getTransformedSensitiveBounds(IDENTITY);
+ Rectangle2D cgb = children[i++].getTransformedSensitiveBounds(IDENTITY);
if (cgb != null) {
if (sensitiveBounds == null)
// another thread has set the geometry bounds to null,
// need to recall this function
return getSensitiveBounds();
-
+
sensitiveBounds.add(cgb);
}
}
@@ -422,21 +428,20 @@
t = new AffineTransform(txf);
t.concatenate(transform);
}
-
+
if ((t == null) || ((t.getShearX() == 0) && (t.getShearY() == 0))) {
// No rotation it's safe to simply transform our bounding box.
return getTransformedBBox(getSensitiveBounds(), t);
}
-
+
Rectangle2D sb = null;
int i=0;
while (sb == null && i < count) {
sb = children[i++].getTransformedSensitiveBounds(t);
}
-
- Rectangle2D csb = null;
+
while (i < count) {
- csb = children[i++].getTransformedSensitiveBounds(t);
+ Rectangle2D csb = children[i++].getTransformedSensitiveBounds(t);
if (csb != null) {
sb.add(csb);
}
@@ -508,7 +513,7 @@
* Returns the outline of this node.
*/
public Shape getOutline() {
- if (outline != null)
+ if (outline != null)
return outline;
outline = new GeneralPath();
@@ -585,9 +590,9 @@
*/
public Object [] toArray() {
GraphicsNode [] result = new GraphicsNode[count];
- for (int i=0; i < count; ++i) {
- result[i] = children[i];
- }
+
+ System.arraycopy( children, 0, result, 0, count );
+
return result;
}
@@ -595,7 +600,7 @@
* Returns an array containing all of the graphics node in the children list
* of this composite graphics node in the correct order.
*
- * @param a the array to fit if possible
+ * @param a the array to fit if possible
*/
public Object[] toArray(Object [] a) {
if (a.length < count) {
@@ -630,7 +635,7 @@
* @return the graphics node previously at the specified position
* @exception IndexOutOfBoundsException if the index is out of range
* @exception IllegalArgumentException if the node is not an
- * instance of GraphicsNode
+ * instance of GraphicsNode
*/
public Object set(int index, Object o) {
// Check for correct arguments
@@ -714,7 +719,7 @@
* @param o the graphics node to be inserted.
* @exception IndexOutOfBoundsException if the index is out of range
* @exception IllegalArgumentException if the node is not an
- * instance of GraphicsNode
+ * instance of GraphicsNode
*/
public void add(int index, Object o) {
// Check for correct arguments
@@ -767,12 +772,13 @@
}
/**
- * Removes the specified graphics node from the children list.
+ * Removes the first instance of the specified graphics node from the children list.
*
* @param o the node the remove
* @return true if the children list contains the specified graphics node
* @exception IllegalArgumentException if the node is not an
* instance of GraphicsNode
+ * @exception IndexOutOfBoundsException when o is not in children list
*/
public boolean remove(Object o) {
// Check for correct argument
@@ -785,7 +791,7 @@
}
// Remove the node
int index = 0;
- for (; node != children[index]; index++);
+ for (; node != children[index]; index++); // fires exception when node not found!
remove(index);
return true;
}
@@ -797,7 +803,7 @@
*
* @param index the position of the graphics node to remove
* @return the graphics node that was removed
- * @exception IndexOutOfBoundsException if index out of range <tt>
+ * @exception IndexOutOfBoundsException if index out of range <tt>
*/
public Object remove(int index) {
// Check for correct argument
@@ -857,7 +863,7 @@
* Returns true if this composite graphics node contains all the graphics
* node in the specified collection, false otherwise.
*
- * @param c the collection to be checked for containment
+ * @param c the collection to be checked for containment
*/
public boolean containsAll(Collection c) {
Iterator i = c.iterator();
@@ -875,15 +881,17 @@
* Returns the index in the children list of the specified graphics node or
* -1 if the children list does not contain this graphics node.
*
- * @param node the graphics node to search for
+ * @param node the graphics node to search for
*/
public int indexOf(Object node) {
if (node == null || !(node instanceof GraphicsNode)) {
return -1;
}
if (((GraphicsNode) node).getParent() == this) {
- for (int i = 0; i < count; i++) {
- if (node == children[i]) {
+ int iCount = count; // local is cheaper
+ GraphicsNode[] workList = children; // local is cheaper
+ for (int i = 0; i < iCount; i++) {
+ if (node == workList[ i ]) {
return i;
}
}
@@ -896,7 +904,7 @@
* specified graphics node, or -1 if the list does not contain this graphics
* node.
*
- * @param node the graphics node to search for
+ * @param node the graphics node to search for
*/
public int lastIndexOf(Object node) {
if (node == null || !(node instanceof GraphicsNode)) {
@@ -926,7 +934,7 @@
* the specified position in the children list.
*
* @param index the index of the first graphics node to return
- * from the children list
+ * from the children list
*/
public ListIterator listIterator(int index) {
if (index < 0 || index > count) {