The attached patch fixes the Swing painting to make better use of the
optimizedDrawingEnabled flag for finding the paint root. This fixes a
bug that has been seen with JViewports (#25968) and should increase
performance in some cases.

2006-01-27  Roman Kennke  <[EMAIL PROTECTED]>

        PR classpath/25968
        * javax/swing/JComponent.java
        (findOverlapFreeParent): Improved the algorithm to make better use
        of the optimizedDrawingEnabled flag.
        * javax/swing/JLayeredPane.java
        (isOptimizedDrawingEnabled): Reimplemented to match the specs.
        * javax/swing/JViewport.java
        (computeBlit): Fixed check to decide if blitting is possible or
not,
        so that it doesn't blit if nothing was scrolled (in order to
        update the buffer when the view updates itself).

/Roman
Index: javax/swing/JComponent.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JComponent.java,v
retrieving revision 1.96
diff -u -r1.96 JComponent.java
--- javax/swing/JComponent.java	27 Jan 2006 10:16:28 -0000	1.96
+++ javax/swing/JComponent.java	27 Jan 2006 12:33:06 -0000
@@ -3245,62 +3245,25 @@
     while (parent != null && !(parent instanceof Window))
       {
         Container newParent = parent.getParent();
-        if (newParent == null)
+        if (newParent == null || newParent instanceof Window)
           break;
         // If the parent is optimizedDrawingEnabled, then its children are
         // tiled and cannot have an overlapping child. Go directly to next
         // parent.
-        if (newParent instanceof JComponent
-            && ((JComponent) newParent).isOptimizedDrawingEnabled())
+        if ((newParent instanceof JComponent
+            && ((JComponent) newParent).isOptimizedDrawingEnabled()))
+          
           {
             parent = newParent;
             continue;
           }
-
-        // First we must check if the new parent itself somehow clips the
-        // target rectangle. This can happen in JViewports.
-        Rectangle parRect = new Rectangle(0, 0, newParent.getWidth(),
-                                          newParent.getHeight());
+        // If the parent is not optimizedDrawingEnabled, we must paint the
+        // parent.
         Rectangle target = SwingUtilities.convertRectangle(found,
                                                            currentClip,
                                                            newParent);
-        if (! target.intersection(parRect).equals(target))
-          {
-            found = newParent;
-            currentClip = target;
-            parent = newParent;
-            continue;
-          }
-
-        // Otherwise we must check if one of the children of this parent
-        // overlaps with the current component.
-        Component[] children = newParent.getComponents();
-        // This flag is used to skip components that are 'below' the component
-        // in question.
-        boolean skip = true;
-        for (int i = children.length - 1; i >= 0; i--)
-          {
-            boolean nextSkip = skip;
-            if (children[i] == parent)
-              nextSkip = false;
-            if (skip)
-              continue;
-            skip = nextSkip;
-            Component c = children[i];
-            Rectangle compBounds = c.getBounds();
-            // If the component completely overlaps the clip in question, we
-            // don't need to repaint. Return null.
-            if (compBounds.contains(target))
-              return null;
-            if (compBounds.intersects(target))
-              {
-                // We found a parent whose children overlap with our current
-                // component. Make this the current component.
-                found = newParent;
-                currentClip = target;
-                break;
-              }
-          }
+        found = newParent;
+        currentClip = target;
         parent = newParent;
       }
     return found;
Index: javax/swing/JLayeredPane.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JLayeredPane.java,v
retrieving revision 1.35
diff -u -r1.35 JLayeredPane.java
--- javax/swing/JLayeredPane.java	27 Jan 2006 10:10:00 -0000	1.35
+++ javax/swing/JLayeredPane.java	27 Jan 2006 12:33:06 -0000
@@ -711,13 +711,39 @@
   }
 
   /**
-   * Overridden to return <code>false</code>, since <code>JLayeredPane</code>
-   * cannot guarantee that its children don't overlap.
+   * Returns <code>false</code> if components in this layered pane can overlap,
+   * otherwise <code>true</code>.
    *
-   * @return <code>false</code>
+   * @return <code>false</code> if components in this layered pane can overlap,
+   *         otherwise <code>true</code>
    */
   public boolean isOptimizedDrawingEnabled()
   {
-    return false;
+    int numChildren = getComponentCount();
+    boolean result = false;
+    // We implement a heuristic here in order to return a quick result:
+    // - For 0 or 1 children it is clear that they do not overlap, return true.
+    // - For 2 children we check their bounding rectangles and if they don't
+    //   interect, return true.
+    // - For more than 2 children we return false. Comparing all the bounding
+    //   rectangles costs too much time and in most cases this will return
+    //   false anyway, since JLayeredPane are mostly used in JRootPane and then
+    //   have at least one child (the contentPane) that takes up the whole
+    //   area of the JLayeredPane.
+    switch (numChildren)
+    {
+      case 0:
+      case 1:
+        result = true;
+        break;
+      case 2:
+        Rectangle r1 = getComponent(0).getBounds();
+        Rectangle r2 = getComponent(1).getBounds();
+        result = !r1.intersects(r2);
+        break;
+      default:
+        result = false;
+    }
+    return result;
   }
 }
Index: javax/swing/JViewport.java
===================================================================
RCS file: /cvsroot/classpath/classpath/javax/swing/JViewport.java,v
retrieving revision 1.40
diff -u -r1.40 JViewport.java
--- javax/swing/JViewport.java	27 Jan 2006 10:10:00 -0000	1.40
+++ javax/swing/JViewport.java	27 Jan 2006 12:33:06 -0000
@@ -710,9 +710,11 @@
   protected boolean computeBlit(int dx, int dy, Point blitFrom, Point blitTo,
                                 Dimension blitSize, Rectangle blitPaint)
   {
-    if ((dx != 0 && dy != 0) || damaged)
+    if ((dx != 0 && dy != 0) || (dy == 0 && dy == 0) || damaged)
       // We cannot blit if the viewport is scrolled in both directions at
-      // once.
+      // once. Also, we do not want to blit if the viewport is not scrolled at
+      // all, because that probably means the view component repaints itself
+      // and the buffer needs updating.
       return false;
 
     Rectangle portBounds = SwingUtilities.calculateInnerArea(this, getBounds());

Reply via email to