1 /*
   2  * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package javax.swing;
  26 
  27 
  28 import java.awt.*;
  29 import java.awt.event.*;
  30 import java.awt.peer.ComponentPeer;
  31 import java.awt.peer.ContainerPeer;
  32 import java.awt.image.VolatileImage;
  33 import java.security.AccessController;
  34 import java.util.*;
  35 import java.applet.*;
  36 
  37 import sun.awt.AWTAccessor;
  38 import sun.awt.AppContext;
  39 import sun.awt.DisplayChangedListener;
  40 import sun.awt.SunToolkit;
  41 import sun.java2d.SunGraphicsEnvironment;
  42 import sun.security.action.GetPropertyAction;
  43 
  44 import com.sun.java.swing.SwingUtilities3;
  45 
  46 /**
  47  * This class manages repaint requests, allowing the number
  48  * of repaints to be minimized, for example by collapsing multiple
  49  * requests into a single repaint for members of a component tree.
  50  * <p>
  51  * As of 1.6 <code>RepaintManager</code> handles repaint requests
  52  * for Swing's top level components (<code>JApplet</code>,
  53  * <code>JWindow</code>, <code>JFrame</code> and <code>JDialog</code>).
  54  * Any calls to <code>repaint</code> on one of these will call into the
  55  * appropriate <code>addDirtyRegion</code> method.
  56  *
  57  * @author Arnaud Weber
  58  */
  59 public class RepaintManager
  60 {
  61     /**
  62      * Whether or not the RepaintManager should handle paint requests
  63      * for top levels.
  64      */
  65     static final boolean HANDLE_TOP_LEVEL_PAINT;
  66 
  67     private static final short BUFFER_STRATEGY_NOT_SPECIFIED = 0;
  68     private static final short BUFFER_STRATEGY_SPECIFIED_ON = 1;
  69     private static final short BUFFER_STRATEGY_SPECIFIED_OFF = 2;
  70 
  71     private static final short BUFFER_STRATEGY_TYPE;
  72 
  73     /**
  74      * Maps from GraphicsConfiguration to VolatileImage.
  75      */
  76     private Map<GraphicsConfiguration,VolatileImage> volatileMap = new
  77                         HashMap<GraphicsConfiguration,VolatileImage>(1);
  78 
  79     //
  80     // As of 1.6 Swing handles scheduling of paint events from native code.
  81     // That is, SwingPaintEventDispatcher is invoked on the toolkit thread,
  82     // which in turn invokes nativeAddDirtyRegion.  Because this is invoked
  83     // from the native thread we can not invoke any public methods and so
  84     // we introduce these added maps.  So, any time nativeAddDirtyRegion is
  85     // invoked the region is added to hwDirtyComponents and a work request
  86     // is scheduled.  When the work request is processed all entries in
  87     // this map are pushed to the real map (dirtyComponents) and then
  88     // painted with the rest of the components.
  89     //
  90     private Map<Container,Rectangle> hwDirtyComponents;
  91 
  92     private Map<Component,Rectangle> dirtyComponents;
  93     private Map<Component,Rectangle> tmpDirtyComponents;
  94     private java.util.List<Component> invalidComponents;
  95 
  96     // List of Runnables that need to be processed before painting from AWT.
  97     private java.util.List<Runnable> runnableList;
  98 
  99     boolean   doubleBufferingEnabled = true;
 100 
 101     private Dimension doubleBufferMaxSize;
 102 
 103     // Support for both the standard and volatile offscreen buffers exists to
 104     // provide backwards compatibility for the [rare] programs which may be
 105     // calling getOffScreenBuffer() and not expecting to get a VolatileImage.
 106     // Swing internally is migrating to use *only* the volatile image buffer.
 107 
 108     // Support for standard offscreen buffer
 109     //
 110     DoubleBufferInfo standardDoubleBuffer;
 111 
 112     /**
 113      * Object responsible for hanlding core paint functionality.
 114      */
 115     private PaintManager paintManager;
 116 
 117     private static final Object repaintManagerKey = RepaintManager.class;
 118 
 119     // Whether or not a VolatileImage should be used for double-buffered painting
 120     static boolean volatileImageBufferEnabled = true;
 121     /**
 122      * Value of the system property awt.nativeDoubleBuffering.
 123      */
 124     private static boolean nativeDoubleBuffering;
 125 
 126     // The maximum number of times Swing will attempt to use the VolatileImage
 127     // buffer during a paint operation.
 128     private static final int VOLATILE_LOOP_MAX = 2;
 129 
 130     /**
 131      * Number of <code>beginPaint</code> that have been invoked.
 132      */
 133     private int paintDepth = 0;
 134 
 135     /**
 136      * Type of buffer strategy to use.  Will be one of the BUFFER_STRATEGY_
 137      * constants.
 138      */
 139     private short bufferStrategyType;
 140 
 141     //
 142     // BufferStrategyPaintManager has the unique characteristic that it
 143     // must deal with the buffer being lost while painting to it.  For
 144     // example, if we paint a component and show it and the buffer has
 145     // become lost we must repaint the whole window.  To deal with that
 146     // the PaintManager calls into repaintRoot, and if we're still in
 147     // the process of painting the repaintRoot field is set to the JRootPane
 148     // and after the current JComponent.paintImmediately call finishes
 149     // paintImmediately will be invoked on the repaintRoot.  In this
 150     // way we don't try to show garbage to the screen.
 151     //
 152     /**
 153      * True if we're in the process of painting the dirty regions.  This is
 154      * set to true in <code>paintDirtyRegions</code>.
 155      */
 156     private boolean painting;
 157     /**
 158      * If the PaintManager calls into repaintRoot during painting this field
 159      * will be set to the root.
 160      */
 161     private JComponent repaintRoot;
 162 
 163     /**
 164      * The Thread that has initiated painting.  If null it
 165      * indicates painting is not currently in progress.
 166      */
 167     private Thread paintThread;
 168 
 169     /**
 170      * Runnable used to process all repaint/revalidate requests.
 171      */
 172     private final ProcessingRunnable processingRunnable;
 173 
 174 
 175     static {
 176         volatileImageBufferEnabled = "true".equals(AccessController.
 177                 doPrivileged(new GetPropertyAction(
 178                 "swing.volatileImageBufferEnabled", "true")));
 179         boolean headless = GraphicsEnvironment.isHeadless();
 180         if (volatileImageBufferEnabled && headless) {
 181             volatileImageBufferEnabled = false;
 182         }
 183         nativeDoubleBuffering = "true".equals(AccessController.doPrivileged(
 184                     new GetPropertyAction("awt.nativeDoubleBuffering")));
 185         String bs = AccessController.doPrivileged(
 186                           new GetPropertyAction("swing.bufferPerWindow"));
 187         if (headless) {
 188             BUFFER_STRATEGY_TYPE = BUFFER_STRATEGY_SPECIFIED_OFF;
 189         }
 190         else if (bs == null) {
 191             BUFFER_STRATEGY_TYPE = BUFFER_STRATEGY_NOT_SPECIFIED;
 192         }
 193         else if ("true".equals(bs)) {
 194             BUFFER_STRATEGY_TYPE = BUFFER_STRATEGY_SPECIFIED_ON;
 195         }
 196         else {
 197             BUFFER_STRATEGY_TYPE = BUFFER_STRATEGY_SPECIFIED_OFF;
 198         }
 199         HANDLE_TOP_LEVEL_PAINT = "true".equals(AccessController.doPrivileged(
 200                new GetPropertyAction("swing.handleTopLevelPaint", "true")));
 201         GraphicsEnvironment ge = GraphicsEnvironment.
 202                 getLocalGraphicsEnvironment();
 203         if (ge instanceof SunGraphicsEnvironment) {
 204             ((SunGraphicsEnvironment)ge).addDisplayChangedListener(
 205                     new DisplayChangedHandler());
 206         }
 207     }
 208 
 209     /**
 210      * Return the RepaintManager for the calling thread given a Component.
 211      *
 212      * @param c a Component -- unused in the default implementation, but could
 213      *          be used by an overridden version to return a different RepaintManager
 214      *          depending on the Component
 215      * @return the RepaintManager object
 216      */
 217     public static RepaintManager currentManager(Component c) {
 218         // Note: DisplayChangedRunnable passes in null as the component, so if
 219         // component is ever used to determine the current
 220         // RepaintManager, DisplayChangedRunnable will need to be modified
 221         // accordingly.
 222         return currentManager(AppContext.getAppContext());
 223     }
 224 
 225     /**
 226      * Returns the RepaintManager for the specified AppContext.  If
 227      * a RepaintManager has not been created for the specified
 228      * AppContext this will return null.
 229      */
 230     static RepaintManager currentManager(AppContext appContext) {
 231         RepaintManager rm = (RepaintManager)appContext.get(repaintManagerKey);
 232         if (rm == null) {
 233             rm = new RepaintManager(BUFFER_STRATEGY_TYPE);
 234             appContext.put(repaintManagerKey, rm);
 235         }
 236         return rm;
 237     }
 238 
 239     /**
 240      * Return the RepaintManager for the calling thread given a JComponent.
 241      * <p>
 242     * Note: This method exists for backward binary compatibility with earlier
 243      * versions of the Swing library. It simply returns the result returned by
 244      * {@link #currentManager(Component)}.
 245      *
 246      * @param c a JComponent -- unused
 247      * @return the RepaintManager object
 248      */
 249     public static RepaintManager currentManager(JComponent c) {
 250         return currentManager((Component)c);
 251     }
 252 
 253 
 254     /**
 255      * Set the RepaintManager that should be used for the calling
 256      * thread. <b>aRepaintManager</b> will become the current RepaintManager
 257      * for the calling thread's thread group.
 258      * @param aRepaintManager  the RepaintManager object to use
 259      */
 260     public static void setCurrentManager(RepaintManager aRepaintManager) {
 261         if (aRepaintManager != null) {
 262             SwingUtilities.appContextPut(repaintManagerKey, aRepaintManager);
 263         } else {
 264             SwingUtilities.appContextRemove(repaintManagerKey);
 265         }
 266     }
 267 
 268     /**
 269      * Create a new RepaintManager instance. You rarely call this constructor.
 270      * directly. To get the default RepaintManager, use
 271      * RepaintManager.currentManager(JComponent) (normally "this").
 272      */
 273     public RepaintManager() {
 274         // Because we can't know what a subclass is doing with the
 275         // volatile image we immediately punt in subclasses.  If this
 276         // poses a problem we'll need a more sophisticated detection algorithm,
 277         // or API.
 278         this(BUFFER_STRATEGY_SPECIFIED_OFF);
 279     }
 280 
 281     private RepaintManager(short bufferStrategyType) {
 282         // If native doublebuffering is being used, do NOT use
 283         // Swing doublebuffering.
 284         doubleBufferingEnabled = !nativeDoubleBuffering;
 285         synchronized(this) {
 286             dirtyComponents = new IdentityHashMap<Component,Rectangle>();
 287             tmpDirtyComponents = new IdentityHashMap<Component,Rectangle>();
 288             this.bufferStrategyType = bufferStrategyType;
 289             hwDirtyComponents = new IdentityHashMap<Container,Rectangle>();
 290         }
 291         processingRunnable = new ProcessingRunnable();
 292     }
 293 
 294     private void displayChanged() {
 295         clearImages();
 296     }
 297 
 298     /**
 299      * Mark the component as in need of layout and queue a runnable
 300      * for the event dispatching thread that will validate the components
 301      * first isValidateRoot() ancestor.
 302      *
 303      * @see JComponent#isValidateRoot
 304      * @see #removeInvalidComponent
 305      */
 306     public synchronized void addInvalidComponent(JComponent invalidComponent)
 307     {
 308         RepaintManager delegate = getDelegate(invalidComponent);
 309         if (delegate != null) {
 310             delegate.addInvalidComponent(invalidComponent);
 311             return;
 312         }
 313         Component validateRoot =
 314             SwingUtilities.getValidateRoot(invalidComponent, true);
 315 
 316         if (validateRoot == null) {
 317             return;
 318         }
 319 
 320         /* Lazily create the invalidateComponents vector and add the
 321          * validateRoot if it's not there already.  If this validateRoot
 322          * is already in the vector, we're done.
 323          */
 324         if (invalidComponents == null) {
 325             invalidComponents = new ArrayList<Component>();
 326         }
 327         else {
 328             int n = invalidComponents.size();
 329             for(int i = 0; i < n; i++) {
 330                 if(validateRoot == invalidComponents.get(i)) {
 331                     return;
 332                 }
 333             }
 334         }
 335         invalidComponents.add(validateRoot);
 336 
 337         // Queue a Runnable to invoke paintDirtyRegions and
 338         // validateInvalidComponents.
 339         scheduleProcessingRunnable();
 340     }
 341 
 342 
 343     /**
 344      * Remove a component from the list of invalid components.
 345      *
 346      * @see #addInvalidComponent
 347      */
 348     public synchronized void removeInvalidComponent(JComponent component) {
 349         RepaintManager delegate = getDelegate(component);
 350         if (delegate != null) {
 351             delegate.removeInvalidComponent(component);
 352             return;
 353         }
 354         if(invalidComponents != null) {
 355             int index = invalidComponents.indexOf(component);
 356             if(index != -1) {
 357                 invalidComponents.remove(index);
 358             }
 359         }
 360     }
 361 
 362 
 363     /**
 364      * Add a component in the list of components that should be refreshed.
 365      * If <i>c</i> already has a dirty region, the rectangle <i>(x,y,w,h)</i>
 366      * will be unioned with the region that should be redrawn.
 367      *
 368      * @see JComponent#repaint
 369      */
 370     private void addDirtyRegion0(Container c, int x, int y, int w, int h) {
 371         /* Special cases we don't have to bother with.
 372          */
 373         if ((w <= 0) || (h <= 0) || (c == null)) {
 374             return;
 375         }
 376 
 377         if ((c.getWidth() <= 0) || (c.getHeight() <= 0)) {
 378             return;
 379         }
 380 
 381         if (extendDirtyRegion(c, x, y, w, h)) {
 382             // Component was already marked as dirty, region has been
 383             // extended, no need to continue.
 384             return;
 385         }
 386 
 387         /* Make sure that c and all it ancestors (up to an Applet or
 388          * Window) are visible.  This loop has the same effect as
 389          * checking c.isShowing() (and note that it's still possible
 390          * that c is completely obscured by an opaque ancestor in
 391          * the specified rectangle).
 392          */
 393         Component root = null;
 394 
 395         // Note: We can't synchronize around this, Frame.getExtendedState
 396         // is synchronized so that if we were to synchronize around this
 397         // it could lead to the possibility of getting locks out
 398         // of order and deadlocking.
 399         for (Container p = c; p != null; p = p.getParent()) {
 400             if (!p.isVisible() || (p.getPeer() == null)) {
 401                 return;
 402             }
 403             if ((p instanceof Window) || (p instanceof Applet)) {
 404                 // Iconified frames are still visible!
 405                 if (p instanceof Frame &&
 406                         (((Frame)p).getExtendedState() & Frame.ICONIFIED) ==
 407                                     Frame.ICONIFIED) {
 408                     return;
 409                 }
 410                 root = p;
 411                 break;
 412             }
 413         }
 414 
 415         if (root == null) return;
 416 
 417         synchronized(this) {
 418             if (extendDirtyRegion(c, x, y, w, h)) {
 419                 // In between last check and this check another thread
 420                 // queued up runnable, can bail here.
 421                 return;
 422             }
 423             dirtyComponents.put(c, new Rectangle(x, y, w, h));
 424         }
 425 
 426         // Queue a Runnable to invoke paintDirtyRegions and
 427         // validateInvalidComponents.
 428         scheduleProcessingRunnable();
 429     }
 430 
 431     /**
 432      * Add a component in the list of components that should be refreshed.
 433      * If <i>c</i> already has a dirty region, the rectangle <i>(x,y,w,h)</i>
 434      * will be unioned with the region that should be redrawn.
 435      *
 436      * @param c Component to repaint, null results in nothing happening.
 437      * @param x X coordinate of the region to repaint
 438      * @param y Y coordinate of the region to repaint
 439      * @param w Width of the region to repaint
 440      * @param h Height of the region to repaint
 441      * @see JComponent#repaint
 442      */
 443     public void addDirtyRegion(JComponent c, int x, int y, int w, int h)
 444     {
 445         RepaintManager delegate = getDelegate(c);
 446         if (delegate != null) {
 447             delegate.addDirtyRegion(c, x, y, w, h);
 448             return;
 449         }
 450         addDirtyRegion0(c, x, y, w, h);
 451     }
 452 
 453     /**
 454      * Adds <code>window</code> to the list of <code>Component</code>s that
 455      * need to be repainted.
 456      *
 457      * @param window Window to repaint, null results in nothing happening.
 458      * @param x X coordinate of the region to repaint
 459      * @param y Y coordinate of the region to repaint
 460      * @param w Width of the region to repaint
 461      * @param h Height of the region to repaint
 462      * @see JFrame#repaint
 463      * @see JWindow#repaint
 464      * @see JDialog#repaint
 465      * @since 1.6
 466      */
 467     public void addDirtyRegion(Window window, int x, int y, int w, int h) {
 468         addDirtyRegion0(window, x, y, w, h);
 469     }
 470 
 471     /**
 472      * Adds <code>applet</code> to the list of <code>Component</code>s that
 473      * need to be repainted.
 474      *
 475      * @param applet Applet to repaint, null results in nothing happening.
 476      * @param x X coordinate of the region to repaint
 477      * @param y Y coordinate of the region to repaint
 478      * @param w Width of the region to repaint
 479      * @param h Height of the region to repaint
 480      * @see JApplet#repaint
 481      * @since 1.6
 482      */
 483     public void addDirtyRegion(Applet applet, int x, int y, int w, int h) {
 484         addDirtyRegion0(applet, x, y, w, h);
 485     }
 486 
 487     void scheduleHeavyWeightPaints() {
 488         Map<Container,Rectangle> hws;
 489 
 490         synchronized(this) {
 491             if (hwDirtyComponents.size() == 0) {
 492                 return;
 493             }
 494             hws = hwDirtyComponents;
 495             hwDirtyComponents =  new IdentityHashMap<Container,Rectangle>();
 496         }
 497         for (Container hw : hws.keySet()) {
 498             Rectangle dirty = hws.get(hw);
 499             if (hw instanceof Window) {
 500                 addDirtyRegion((Window)hw, dirty.x, dirty.y,
 501                                dirty.width, dirty.height);
 502             }
 503             else if (hw instanceof Applet) {
 504                 addDirtyRegion((Applet)hw, dirty.x, dirty.y,
 505                                dirty.width, dirty.height);
 506             }
 507             else { // SwingHeavyWeight
 508                 addDirtyRegion0(hw, dirty.x, dirty.y,
 509                                 dirty.width, dirty.height);
 510             }
 511         }
 512     }
 513 
 514     //
 515     // This is called from the toolkit thread when a native expose is
 516     // received.
 517     //
 518     void nativeAddDirtyRegion(AppContext appContext, Container c,
 519                               int x, int y, int w, int h) {
 520         if (w > 0 && h > 0) {
 521             synchronized(this) {
 522                 Rectangle dirty = hwDirtyComponents.get(c);
 523                 if (dirty == null) {
 524                     hwDirtyComponents.put(c, new Rectangle(x, y, w, h));
 525                 }
 526                 else {
 527                     hwDirtyComponents.put(c, SwingUtilities.computeUnion(
 528                                               x, y, w, h, dirty));
 529                 }
 530             }
 531             scheduleProcessingRunnable(appContext);
 532         }
 533     }
 534 
 535     //
 536     // This is called from the toolkit thread when awt needs to run a
 537     // Runnable before we paint.
 538     //
 539     void nativeQueueSurfaceDataRunnable(AppContext appContext, Component c,
 540                                         Runnable r) {
 541         synchronized(this) {
 542             if (runnableList == null) {
 543                 runnableList = new LinkedList<Runnable>();
 544             }
 545             runnableList.add(r);
 546         }
 547         scheduleProcessingRunnable(appContext);
 548     }
 549 
 550     /**
 551      * Extends the dirty region for the specified component to include
 552      * the new region.
 553      *
 554      * @return false if <code>c</code> is not yet marked dirty.
 555      */
 556     private synchronized boolean extendDirtyRegion(
 557         Component c, int x, int y, int w, int h) {
 558         Rectangle r = dirtyComponents.get(c);
 559         if (r != null) {
 560             // A non-null r implies c is already marked as dirty,
 561             // and that the parent is valid. Therefore we can
 562             // just union the rect and bail.
 563             SwingUtilities.computeUnion(x, y, w, h, r);
 564             return true;
 565         }
 566         return false;
 567     }
 568 
 569     /** Return the current dirty region for a component.
 570      *  Return an empty rectangle if the component is not
 571      *  dirty.
 572      */
 573     public Rectangle getDirtyRegion(JComponent aComponent) {
 574         RepaintManager delegate = getDelegate(aComponent);
 575         if (delegate != null) {
 576             return delegate.getDirtyRegion(aComponent);
 577         }
 578         Rectangle r;
 579         synchronized(this) {
 580             r = dirtyComponents.get(aComponent);
 581         }
 582         if(r == null)
 583             return new Rectangle(0,0,0,0);
 584         else
 585             return new Rectangle(r);
 586     }
 587 
 588     /**
 589      * Mark a component completely dirty. <b>aComponent</b> will be
 590      * completely painted during the next paintDirtyRegions() call.
 591      */
 592     public void markCompletelyDirty(JComponent aComponent) {
 593         RepaintManager delegate = getDelegate(aComponent);
 594         if (delegate != null) {
 595             delegate.markCompletelyDirty(aComponent);
 596             return;
 597         }
 598         addDirtyRegion(aComponent,0,0,Integer.MAX_VALUE,Integer.MAX_VALUE);
 599     }
 600 
 601     /**
 602      * Mark a component completely clean. <b>aComponent</b> will not
 603      * get painted during the next paintDirtyRegions() call.
 604      */
 605     public void markCompletelyClean(JComponent aComponent) {
 606         RepaintManager delegate = getDelegate(aComponent);
 607         if (delegate != null) {
 608             delegate.markCompletelyClean(aComponent);
 609             return;
 610         }
 611         synchronized(this) {
 612                 dirtyComponents.remove(aComponent);
 613         }
 614     }
 615 
 616     /**
 617      * Convenience method that returns true if <b>aComponent</b> will be completely
 618      * painted during the next paintDirtyRegions(). If computing dirty regions is
 619      * expensive for your component, use this method and avoid computing dirty region
 620      * if it return true.
 621      */
 622     public boolean isCompletelyDirty(JComponent aComponent) {
 623         RepaintManager delegate = getDelegate(aComponent);
 624         if (delegate != null) {
 625             return delegate.isCompletelyDirty(aComponent);
 626         }
 627         Rectangle r;
 628 
 629         r = getDirtyRegion(aComponent);
 630         if(r.width == Integer.MAX_VALUE &&
 631            r.height == Integer.MAX_VALUE)
 632             return true;
 633         else
 634             return false;
 635     }
 636 
 637 
 638     /**
 639      * Validate all of the components that have been marked invalid.
 640      * @see #addInvalidComponent
 641      */
 642     public void validateInvalidComponents() {
 643         java.util.List<Component> ic;
 644         synchronized(this) {
 645             if(invalidComponents == null) {
 646                 return;
 647             }
 648             ic = invalidComponents;
 649             invalidComponents = null;
 650         }
 651         int n = ic.size();
 652         for(int i = 0; i < n; i++) {
 653             ic.get(i).validate();
 654         }
 655     }
 656 
 657 
 658     /**
 659      * This is invoked to process paint requests.  It's needed
 660      * for backward compatability in so far as RepaintManager would previously
 661      * not see paint requests for top levels, so, we have to make sure
 662      * a subclass correctly paints any dirty top levels.
 663      */
 664     private void prePaintDirtyRegions() {
 665         Map<Component,Rectangle> dirtyComponents;
 666         java.util.List<Runnable> runnableList;
 667         synchronized(this) {
 668             dirtyComponents = this.dirtyComponents;
 669             runnableList = this.runnableList;
 670             this.runnableList = null;
 671         }
 672         if (runnableList != null) {
 673             for (Runnable runnable : runnableList) {
 674                 runnable.run();
 675             }
 676         }
 677         paintDirtyRegions();
 678         if (dirtyComponents.size() > 0) {
 679             // This'll only happen if a subclass isn't correctly dealing
 680             // with toplevels.
 681             paintDirtyRegions(dirtyComponents);
 682         }
 683     }
 684 
 685     private void updateWindows(Map<Component,Rectangle> dirtyComponents) {
 686         Toolkit toolkit = Toolkit.getDefaultToolkit();
 687         if (!(toolkit instanceof SunToolkit &&
 688               ((SunToolkit)toolkit).needUpdateWindow()))
 689         {
 690             return;
 691         }
 692 
 693         Set<Window> windows = new HashSet<Window>();
 694         Set<Component> dirtyComps = dirtyComponents.keySet();
 695         for (Iterator<Component> it = dirtyComps.iterator(); it.hasNext();) {
 696             Component dirty = it.next();
 697             Window window = dirty instanceof Window ?
 698                 (Window)dirty :
 699                 SwingUtilities.getWindowAncestor(dirty);
 700             if (window != null &&
 701                 !window.isOpaque())
 702             {
 703                 windows.add(window);
 704             }
 705         }
 706 
 707         for (Window window : windows) {
 708             AWTAccessor.getWindowAccessor().updateWindow(window);
 709         }
 710     }
 711 
 712     boolean isPainting() {
 713         return painting;
 714     }
 715 
 716     /**
 717      * Paint all of the components that have been marked dirty.
 718      *
 719      * @see #addDirtyRegion
 720      */
 721     public void paintDirtyRegions() {
 722         synchronized(this) {  // swap for thread safety
 723             Map<Component,Rectangle> tmp = tmpDirtyComponents;
 724             tmpDirtyComponents = dirtyComponents;
 725             dirtyComponents = tmp;
 726             dirtyComponents.clear();
 727         }
 728         paintDirtyRegions(tmpDirtyComponents);
 729     }
 730 
 731     private void paintDirtyRegions(Map<Component,Rectangle>
 732                                    tmpDirtyComponents){
 733         int i, count;
 734         java.util.List<Component> roots;
 735         Component dirtyComponent;
 736 
 737         count = tmpDirtyComponents.size();
 738         if (count == 0) {
 739             return;
 740         }
 741 
 742         Rectangle rect;
 743         int localBoundsX = 0;
 744         int localBoundsY = 0;
 745         int localBoundsH;
 746         int localBoundsW;
 747 
 748         roots = new ArrayList<Component>(count);
 749 
 750         for (Component dirty : tmpDirtyComponents.keySet()) {
 751             collectDirtyComponents(tmpDirtyComponents, dirty, roots);
 752         }
 753 
 754         count = roots.size();
 755         painting = true;
 756         try {
 757             for(i=0 ; i < count ; i++) {
 758                 dirtyComponent = roots.get(i);
 759                 rect = tmpDirtyComponents.get(dirtyComponent);
 760                 // Sometimes when RepaintManager is changed during the painting
 761                 // we may get null here, see #6995769 for details
 762                 if (rect == null) {
 763                     continue;
 764                 }
 765                 localBoundsH = dirtyComponent.getHeight();
 766                 localBoundsW = dirtyComponent.getWidth();
 767 
 768                 SwingUtilities.computeIntersection(localBoundsX,
 769                                                    localBoundsY,
 770                                                    localBoundsW,
 771                                                    localBoundsH,
 772                                                    rect);
 773                 if (dirtyComponent instanceof JComponent) {
 774                     ((JComponent)dirtyComponent).paintImmediately(
 775                         rect.x,rect.y,rect.width, rect.height);
 776                 }
 777                 else if (dirtyComponent.isShowing()) {
 778                     Graphics g = JComponent.safelyGetGraphics(
 779                             dirtyComponent, dirtyComponent);
 780                     // If the Graphics goes away, it means someone disposed of
 781                     // the window, don't do anything.
 782                     if (g != null) {
 783                         g.setClip(rect.x, rect.y, rect.width, rect.height);
 784                         try {
 785                             dirtyComponent.paint(g);
 786                         } finally {
 787                             g.dispose();
 788                         }
 789                     }
 790                 }
 791                 // If the repaintRoot has been set, service it now and
 792                 // remove any components that are children of repaintRoot.
 793                 if (repaintRoot != null) {
 794                     adjustRoots(repaintRoot, roots, i + 1);
 795                     count = roots.size();
 796                     paintManager.isRepaintingRoot = true;
 797                     repaintRoot.paintImmediately(0, 0, repaintRoot.getWidth(),
 798                                                  repaintRoot.getHeight());
 799                     paintManager.isRepaintingRoot = false;
 800                     // Only service repaintRoot once.
 801                     repaintRoot = null;
 802                 }
 803             }
 804         } finally {
 805             painting = false;
 806         }
 807 
 808         updateWindows(tmpDirtyComponents);
 809 
 810         tmpDirtyComponents.clear();
 811     }
 812 
 813 
 814     /**
 815      * Removes any components from roots that are children of
 816      * root.
 817      */
 818     private void adjustRoots(JComponent root,
 819                              java.util.List<Component> roots, int index) {
 820         for (int i = roots.size() - 1; i >= index; i--) {
 821             Component c = roots.get(i);
 822             for(;;) {
 823                 if (c == root || c == null || !(c instanceof JComponent)) {
 824                     break;
 825                 }
 826                 c = c.getParent();
 827             }
 828             if (c == root) {
 829                 roots.remove(i);
 830             }
 831         }
 832     }
 833 
 834     Rectangle tmp = new Rectangle();
 835 
 836     void collectDirtyComponents(Map<Component,Rectangle> dirtyComponents,
 837                                 Component dirtyComponent,
 838                                 java.util.List<Component> roots) {
 839         int dx, dy, rootDx, rootDy;
 840         Component component, rootDirtyComponent,parent;
 841         Rectangle cBounds;
 842 
 843         // Find the highest parent which is dirty.  When we get out of this
 844         // rootDx and rootDy will contain the translation from the
 845         // rootDirtyComponent's coordinate system to the coordinates of the
 846         // original dirty component.  The tmp Rect is also used to compute the
 847         // visible portion of the dirtyRect.
 848 
 849         component = rootDirtyComponent = dirtyComponent;
 850 
 851         int x = dirtyComponent.getX();
 852         int y = dirtyComponent.getY();
 853         int w = dirtyComponent.getWidth();
 854         int h = dirtyComponent.getHeight();
 855 
 856         dx = rootDx = 0;
 857         dy = rootDy = 0;
 858         tmp.setBounds(dirtyComponents.get(dirtyComponent));
 859 
 860         // System.out.println("Collect dirty component for bound " + tmp +
 861         //                                   "component bounds is " + cBounds);;
 862         SwingUtilities.computeIntersection(0,0,w,h,tmp);
 863 
 864         if (tmp.isEmpty()) {
 865             // System.out.println("Empty 1");
 866             return;
 867         }
 868 
 869         for(;;) {
 870             if(!(component instanceof JComponent))
 871                 break;
 872 
 873             parent = component.getParent();
 874             if(parent == null)
 875                 break;
 876 
 877             component = parent;
 878 
 879             dx += x;
 880             dy += y;
 881             tmp.setLocation(tmp.x + x, tmp.y + y);
 882 
 883             x = component.getX();
 884             y = component.getY();
 885             w = component.getWidth();
 886             h = component.getHeight();
 887             tmp = SwingUtilities.computeIntersection(0,0,w,h,tmp);
 888 
 889             if (tmp.isEmpty()) {
 890                 // System.out.println("Empty 2");
 891                 return;
 892             }
 893 
 894             if (dirtyComponents.get(component) != null) {
 895                 rootDirtyComponent = component;
 896                 rootDx = dx;
 897                 rootDy = dy;
 898             }
 899         }
 900 
 901         if (dirtyComponent != rootDirtyComponent) {
 902             Rectangle r;
 903             tmp.setLocation(tmp.x + rootDx - dx,
 904                             tmp.y + rootDy - dy);
 905             r = dirtyComponents.get(rootDirtyComponent);
 906             SwingUtilities.computeUnion(tmp.x,tmp.y,tmp.width,tmp.height,r);
 907         }
 908 
 909         // If we haven't seen this root before, then we need to add it to the
 910         // list of root dirty Views.
 911 
 912         if (!roots.contains(rootDirtyComponent))
 913             roots.add(rootDirtyComponent);
 914     }
 915 
 916 
 917     /**
 918      * Returns a string that displays and identifies this
 919      * object's properties.
 920      *
 921      * @return a String representation of this object
 922      */
 923     public synchronized String toString() {
 924         StringBuffer sb = new StringBuffer();
 925         if(dirtyComponents != null)
 926             sb.append("" + dirtyComponents);
 927         return sb.toString();
 928     }
 929 
 930 
 931    /**
 932      * Return the offscreen buffer that should be used as a double buffer with
 933      * the component <code>c</code>.
 934      * By default there is a double buffer per RepaintManager.
 935      * The buffer might be smaller than <code>(proposedWidth,proposedHeight)</code>
 936      * This happens when the maximum double buffer size as been set for the receiving
 937      * repaint manager.
 938      */
 939     public Image getOffscreenBuffer(Component c,int proposedWidth,int proposedHeight) {
 940         RepaintManager delegate = getDelegate(c);
 941         if (delegate != null) {
 942             return delegate.getOffscreenBuffer(c, proposedWidth, proposedHeight);
 943         }
 944         return _getOffscreenBuffer(c, proposedWidth, proposedHeight);
 945     }
 946 
 947   /**
 948    * Return a volatile offscreen buffer that should be used as a
 949    * double buffer with the specified component <code>c</code>.
 950    * The image returned will be an instance of VolatileImage, or null
 951    * if a VolatileImage object could not be instantiated.
 952    * This buffer might be smaller than <code>(proposedWidth,proposedHeight)</code>.
 953    * This happens when the maximum double buffer size has been set for this
 954    * repaint manager.
 955    *
 956    * @see java.awt.image.VolatileImage
 957    * @since 1.4
 958    */
 959     public Image getVolatileOffscreenBuffer(Component c,
 960                                             int proposedWidth,int proposedHeight) {
 961         RepaintManager delegate = getDelegate(c);
 962         if (delegate != null) {
 963             return delegate.getVolatileOffscreenBuffer(c, proposedWidth,
 964                                                         proposedHeight);
 965         }
 966 
 967         // If the window is non-opaque, it's double-buffered at peer's level
 968         Window w = (c instanceof Window) ? (Window)c : SwingUtilities.getWindowAncestor(c);
 969         if (!w.isOpaque()) {
 970             Toolkit tk = Toolkit.getDefaultToolkit();
 971             if ((tk instanceof SunToolkit) && (((SunToolkit)tk).needUpdateWindow())) {
 972                 return null;
 973             }
 974         }
 975 
 976         GraphicsConfiguration config = c.getGraphicsConfiguration();
 977         if (config == null) {
 978             config = GraphicsEnvironment.getLocalGraphicsEnvironment().
 979                             getDefaultScreenDevice().getDefaultConfiguration();
 980         }
 981         Dimension maxSize = getDoubleBufferMaximumSize();
 982         int width = proposedWidth < 1 ? 1 :
 983             (proposedWidth > maxSize.width? maxSize.width : proposedWidth);
 984         int height = proposedHeight < 1 ? 1 :
 985             (proposedHeight > maxSize.height? maxSize.height : proposedHeight);
 986         VolatileImage image = volatileMap.get(config);
 987         if (image == null || image.getWidth() < width ||
 988                              image.getHeight() < height) {
 989             if (image != null) {
 990                 image.flush();
 991             }
 992             image = config.createCompatibleVolatileImage(width, height);
 993             volatileMap.put(config, image);
 994         }
 995         return image;
 996     }
 997 
 998     private Image _getOffscreenBuffer(Component c, int proposedWidth, int proposedHeight) {
 999         Dimension maxSize = getDoubleBufferMaximumSize();
1000         DoubleBufferInfo doubleBuffer;
1001         int width, height;
1002 
1003         // If the window is non-opaque, it's double-buffered at peer's level
1004         Window w = (c instanceof Window) ? (Window)c : SwingUtilities.getWindowAncestor(c);
1005         if (!w.isOpaque()) {
1006             Toolkit tk = Toolkit.getDefaultToolkit();
1007             if ((tk instanceof SunToolkit) && (((SunToolkit)tk).needUpdateWindow())) {
1008                 return null;
1009             }
1010         }
1011 
1012         if (standardDoubleBuffer == null) {
1013             standardDoubleBuffer = new DoubleBufferInfo();
1014         }
1015         doubleBuffer = standardDoubleBuffer;
1016 
1017         width = proposedWidth < 1? 1 :
1018                   (proposedWidth > maxSize.width? maxSize.width : proposedWidth);
1019         height = proposedHeight < 1? 1 :
1020                   (proposedHeight > maxSize.height? maxSize.height : proposedHeight);
1021 
1022         if (doubleBuffer.needsReset || (doubleBuffer.image != null &&
1023                                         (doubleBuffer.size.width < width ||
1024                                          doubleBuffer.size.height < height))) {
1025             doubleBuffer.needsReset = false;
1026             if (doubleBuffer.image != null) {
1027                 doubleBuffer.image.flush();
1028                 doubleBuffer.image = null;
1029             }
1030             width = Math.max(doubleBuffer.size.width, width);
1031             height = Math.max(doubleBuffer.size.height, height);
1032         }
1033 
1034         Image result = doubleBuffer.image;
1035 
1036         if (doubleBuffer.image == null) {
1037             result = c.createImage(width , height);
1038             doubleBuffer.size = new Dimension(width, height);
1039             if (c instanceof JComponent) {
1040                 ((JComponent)c).setCreatedDoubleBuffer(true);
1041                 doubleBuffer.image = result;
1042             }
1043             // JComponent will inform us when it is no longer valid
1044             // (via removeNotify) we have no such hook to other components,
1045             // therefore we don't keep a ref to the Component
1046             // (indirectly through the Image) by stashing the image.
1047         }
1048         return result;
1049     }
1050 
1051 
1052     /** Set the maximum double buffer size. **/
1053     public void setDoubleBufferMaximumSize(Dimension d) {
1054         doubleBufferMaxSize = d;
1055         if (doubleBufferMaxSize == null) {
1056             clearImages();
1057         } else {
1058             clearImages(d.width, d.height);
1059         }
1060     }
1061 
1062     private void clearImages() {
1063         clearImages(0, 0);
1064     }
1065 
1066     private void clearImages(int width, int height) {
1067         if (standardDoubleBuffer != null && standardDoubleBuffer.image != null) {
1068             if (standardDoubleBuffer.image.getWidth(null) > width ||
1069                 standardDoubleBuffer.image.getHeight(null) > height) {
1070                 standardDoubleBuffer.image.flush();
1071                 standardDoubleBuffer.image = null;
1072             }
1073         }
1074         // Clear out the VolatileImages
1075         Iterator<GraphicsConfiguration> gcs = volatileMap.keySet().iterator();
1076         while (gcs.hasNext()) {
1077             GraphicsConfiguration gc = gcs.next();
1078             VolatileImage image = volatileMap.get(gc);
1079             if (image.getWidth() > width || image.getHeight() > height) {
1080                 image.flush();
1081                 gcs.remove();
1082             }
1083         }
1084     }
1085 
1086     /**
1087      * Returns the maximum double buffer size.
1088      *
1089      * @return a Dimension object representing the maximum size
1090      */
1091     public Dimension getDoubleBufferMaximumSize() {
1092         if (doubleBufferMaxSize == null) {
1093             try {
1094                 Rectangle virtualBounds = new Rectangle();
1095                 GraphicsEnvironment ge = GraphicsEnvironment.
1096                                                  getLocalGraphicsEnvironment();
1097                 for (GraphicsDevice gd : ge.getScreenDevices()) {
1098                     GraphicsConfiguration gc = gd.getDefaultConfiguration();
1099                     virtualBounds = virtualBounds.union(gc.getBounds());
1100                 }
1101                 doubleBufferMaxSize = new Dimension(virtualBounds.width,
1102                                                     virtualBounds.height);
1103             } catch (HeadlessException e) {
1104                 doubleBufferMaxSize = new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
1105             }
1106         }
1107         return doubleBufferMaxSize;
1108     }
1109 
1110     /**
1111      * Enables or disables double buffering in this RepaintManager.
1112      * CAUTION: The default value for this property is set for optimal
1113      * paint performance on the given platform and it is not recommended
1114      * that programs modify this property directly.
1115      *
1116      * @param aFlag  true to activate double buffering
1117      * @see #isDoubleBufferingEnabled
1118      */
1119     public void setDoubleBufferingEnabled(boolean aFlag) {
1120         doubleBufferingEnabled = aFlag;
1121         PaintManager paintManager = getPaintManager();
1122         if (!aFlag && paintManager.getClass() != PaintManager.class) {
1123             setPaintManager(new PaintManager());
1124         }
1125     }
1126 
1127     /**
1128      * Returns true if this RepaintManager is double buffered.
1129      * The default value for this property may vary from platform
1130      * to platform.  On platforms where native double buffering
1131      * is supported in the AWT, the default value will be <code>false</code>
1132      * to avoid unnecessary buffering in Swing.
1133      * On platforms where native double buffering is not supported,
1134      * the default value will be <code>true</code>.
1135      *
1136      * @return true if this object is double buffered
1137      */
1138     public boolean isDoubleBufferingEnabled() {
1139         return doubleBufferingEnabled;
1140     }
1141 
1142     /**
1143      * This resets the double buffer. Actually, it marks the double buffer
1144      * as invalid, the double buffer will then be recreated on the next
1145      * invocation of getOffscreenBuffer.
1146      */
1147     void resetDoubleBuffer() {
1148         if (standardDoubleBuffer != null) {
1149             standardDoubleBuffer.needsReset = true;
1150         }
1151     }
1152 
1153     /**
1154      * This resets the volatile double buffer.
1155      */
1156     void resetVolatileDoubleBuffer(GraphicsConfiguration gc) {
1157         Image image = volatileMap.remove(gc);
1158         if (image != null) {
1159             image.flush();
1160         }
1161     }
1162 
1163     /**
1164      * Returns true if we should use the <code>Image</code> returned
1165      * from <code>getVolatileOffscreenBuffer</code> to do double buffering.
1166      */
1167     boolean useVolatileDoubleBuffer() {
1168         return volatileImageBufferEnabled;
1169     }
1170 
1171     /**
1172      * Returns true if the current thread is the thread painting.  This
1173      * will return false if no threads are painting.
1174      */
1175     private synchronized boolean isPaintingThread() {
1176         return (Thread.currentThread() == paintThread);
1177     }
1178     //
1179     // Paint methods.  You very, VERY rarely need to invoke these.
1180     // They are invoked directly from JComponent's painting code and
1181     // when painting happens outside the normal flow: DefaultDesktopManager
1182     // and JViewport.  If you end up needing these methods in other places be
1183     // careful that you don't get stuck in a paint loop.
1184     //
1185 
1186     /**
1187      * Paints a region of a component
1188      *
1189      * @param paintingComponent Component to paint
1190      * @param bufferComponent Component to obtain buffer for
1191      * @param g Graphics to paint to
1192      * @param x X-coordinate
1193      * @param y Y-coordinate
1194      * @param w Width
1195      * @param h Height
1196      */
1197     void paint(JComponent paintingComponent,
1198                JComponent bufferComponent, Graphics g,
1199                int x, int y, int w, int h) {
1200         PaintManager paintManager = getPaintManager();
1201         if (!isPaintingThread()) {
1202             // We're painting to two threads at once.  PaintManager deals
1203             // with this a bit better than BufferStrategyPaintManager, use
1204             // it to avoid possible exceptions/corruption.
1205             if (paintManager.getClass() != PaintManager.class) {
1206                 paintManager = new PaintManager();
1207                 paintManager.repaintManager = this;
1208             }
1209         }
1210         if (!paintManager.paint(paintingComponent, bufferComponent, g,
1211                                 x, y, w, h)) {
1212             g.setClip(x, y, w, h);
1213             paintingComponent.paintToOffscreen(g, x, y, w, h, x + w, y + h);
1214         }
1215     }
1216 
1217     /**
1218      * Does a copy area on the specified region.
1219      *
1220      * @param clip Whether or not the copyArea needs to be clipped to the
1221      *             Component's bounds.
1222      */
1223     void copyArea(JComponent c, Graphics g, int x, int y, int w, int h,
1224                   int deltaX, int deltaY, boolean clip) {
1225         getPaintManager().copyArea(c, g, x, y, w, h, deltaX, deltaY, clip);
1226     }
1227 
1228     /**
1229      * Invoked prior to any paint/copyArea method calls.  This will
1230      * be followed by an invocation of <code>endPaint</code>.
1231      * <b>WARNING</b>: Callers of this method need to wrap the call
1232      * in a <code>try/finally</code>, otherwise if an exception is thrown
1233      * during the course of painting the RepaintManager may
1234      * be left in a state in which the screen is not updated, eg:
1235      * <pre>
1236      * repaintManager.beginPaint();
1237      * try {
1238      *   repaintManager.paint(...);
1239      * } finally {
1240      *   repaintManager.endPaint();
1241      * }
1242      * </pre>
1243      */
1244     void beginPaint() {
1245         boolean multiThreadedPaint = false;
1246         int paintDepth;
1247         Thread currentThread = Thread.currentThread();
1248         synchronized(this) {
1249             paintDepth = this.paintDepth;
1250             if (paintThread == null || currentThread == paintThread) {
1251                 paintThread = currentThread;
1252                 this.paintDepth++;
1253             } else {
1254                 multiThreadedPaint = true;
1255             }
1256         }
1257         if (!multiThreadedPaint && paintDepth == 0) {
1258             getPaintManager().beginPaint();
1259         }
1260     }
1261 
1262     /**
1263      * Invoked after <code>beginPaint</code> has been invoked.
1264      */
1265     void endPaint() {
1266         if (isPaintingThread()) {
1267             PaintManager paintManager = null;
1268             synchronized(this) {
1269                 if (--paintDepth == 0) {
1270                     paintManager = getPaintManager();
1271                 }
1272             }
1273             if (paintManager != null) {
1274                 paintManager.endPaint();
1275                 synchronized(this) {
1276                     paintThread = null;
1277                 }
1278             }
1279         }
1280     }
1281 
1282     /**
1283      * If possible this will show a previously rendered portion of
1284      * a Component.  If successful, this will return true, otherwise false.
1285      * <p>
1286      * WARNING: This method is invoked from the native toolkit thread, be
1287      * very careful as to what methods this invokes!
1288      */
1289     boolean show(Container c, int x, int y, int w, int h) {
1290         return getPaintManager().show(c, x, y, w, h);
1291     }
1292 
1293     /**
1294      * Invoked when the doubleBuffered or useTrueDoubleBuffering
1295      * properties of a JRootPane change.  This may come in on any thread.
1296      */
1297     void doubleBufferingChanged(JRootPane rootPane) {
1298         getPaintManager().doubleBufferingChanged(rootPane);
1299     }
1300 
1301     /**
1302      * Sets the <code>PaintManager</code> that is used to handle all
1303      * double buffered painting.
1304      *
1305      * @param paintManager The PaintManager to use.  Passing in null indicates
1306      *        the fallback PaintManager should be used.
1307      */
1308     void setPaintManager(PaintManager paintManager) {
1309         if (paintManager == null) {
1310             paintManager = new PaintManager();
1311         }
1312         PaintManager oldPaintManager;
1313         synchronized(this) {
1314             oldPaintManager = this.paintManager;
1315             this.paintManager = paintManager;
1316             paintManager.repaintManager = this;
1317         }
1318         if (oldPaintManager != null) {
1319             oldPaintManager.dispose();
1320         }
1321     }
1322 
1323     private synchronized PaintManager getPaintManager() {
1324         if (paintManager == null) {
1325             PaintManager paintManager = null;
1326             if (doubleBufferingEnabled && !nativeDoubleBuffering) {
1327                 switch (bufferStrategyType) {
1328                 case BUFFER_STRATEGY_NOT_SPECIFIED:
1329                     Toolkit tk = Toolkit.getDefaultToolkit();
1330                     if (tk instanceof SunToolkit) {
1331                         SunToolkit stk = (SunToolkit) tk;
1332                         if (stk.useBufferPerWindow()) {
1333                             paintManager = new BufferStrategyPaintManager();
1334                         }
1335                     }
1336                     break;
1337                 case BUFFER_STRATEGY_SPECIFIED_ON:
1338                     paintManager = new BufferStrategyPaintManager();
1339                     break;
1340                 default:
1341                     break;
1342                 }
1343             }
1344             // null case handled in setPaintManager
1345             setPaintManager(paintManager);
1346         }
1347         return paintManager;
1348     }
1349 
1350     private void scheduleProcessingRunnable() {
1351         scheduleProcessingRunnable(AppContext.getAppContext());
1352     }
1353 
1354     private void scheduleProcessingRunnable(AppContext context) {
1355         if (processingRunnable.markPending()) {
1356             Toolkit tk = Toolkit.getDefaultToolkit();
1357             if (tk instanceof SunToolkit) {
1358                 SunToolkit.getSystemEventQueueImplPP(context).
1359                   postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(),
1360                                                 processingRunnable));
1361             } else {
1362                 Toolkit.getDefaultToolkit().getSystemEventQueue().
1363                       postEvent(new InvocationEvent(Toolkit.getDefaultToolkit(),
1364                                                     processingRunnable));
1365             }
1366         }
1367     }
1368 
1369 
1370     /**
1371      * PaintManager is used to handle all double buffered painting for
1372      * Swing.  Subclasses should call back into the JComponent method
1373      * <code>paintToOffscreen</code> to handle the actual painting.
1374      */
1375     static class PaintManager {
1376         /**
1377          * RepaintManager the PaintManager has been installed on.
1378          */
1379         protected RepaintManager repaintManager;
1380         boolean isRepaintingRoot;
1381 
1382         /**
1383          * Paints a region of a component
1384          *
1385          * @param paintingComponent Component to paint
1386          * @param bufferComponent Component to obtain buffer for
1387          * @param g Graphics to paint to
1388          * @param x X-coordinate
1389          * @param y Y-coordinate
1390          * @param w Width
1391          * @param h Height
1392          * @return true if painting was successful.
1393          */
1394         public boolean paint(JComponent paintingComponent,
1395                              JComponent bufferComponent, Graphics g,
1396                              int x, int y, int w, int h) {
1397             // First attempt to use VolatileImage buffer for performance.
1398             // If this fails (which should rarely occur), fallback to a
1399             // standard Image buffer.
1400             boolean paintCompleted = false;
1401             Image offscreen;
1402             if (repaintManager.useVolatileDoubleBuffer() &&
1403                 (offscreen = getValidImage(repaintManager.
1404                 getVolatileOffscreenBuffer(bufferComponent, w, h))) != null) {
1405                 VolatileImage vImage = (java.awt.image.VolatileImage)offscreen;
1406                 GraphicsConfiguration gc = bufferComponent.
1407                                             getGraphicsConfiguration();
1408                 for (int i = 0; !paintCompleted &&
1409                          i < RepaintManager.VOLATILE_LOOP_MAX; i++) {
1410                     if (vImage.validate(gc) ==
1411                                    VolatileImage.IMAGE_INCOMPATIBLE) {
1412                         repaintManager.resetVolatileDoubleBuffer(gc);
1413                         offscreen = repaintManager.getVolatileOffscreenBuffer(
1414                             bufferComponent,w, h);
1415                         vImage = (java.awt.image.VolatileImage)offscreen;
1416                     }
1417                     paintDoubleBuffered(paintingComponent, vImage, g, x, y,
1418                                         w, h);
1419                     paintCompleted = !vImage.contentsLost();
1420                 }
1421             }
1422             // VolatileImage painting loop failed, fallback to regular
1423             // offscreen buffer
1424             if (!paintCompleted && (offscreen = getValidImage(
1425                       repaintManager.getOffscreenBuffer(
1426                       bufferComponent, w, h))) != null) {
1427                 paintDoubleBuffered(paintingComponent, offscreen, g, x, y, w,
1428                                     h);
1429                 paintCompleted = true;
1430             }
1431             return paintCompleted;
1432         }
1433 
1434         /**
1435          * Does a copy area on the specified region.
1436          */
1437         public void copyArea(JComponent c, Graphics g, int x, int y, int w,
1438                              int h, int deltaX, int deltaY, boolean clip) {
1439             g.copyArea(x, y, w, h, deltaX, deltaY);
1440         }
1441 
1442         /**
1443          * Invoked prior to any calls to paint or copyArea.
1444          */
1445         public void beginPaint() {
1446         }
1447 
1448         /**
1449          * Invoked to indicate painting has been completed.
1450          */
1451         public void endPaint() {
1452         }
1453 
1454         /**
1455          * Shows a region of a previously rendered component.  This
1456          * will return true if successful, false otherwise.  The default
1457          * implementation returns false.
1458          */
1459         public boolean show(Container c, int x, int y, int w, int h) {
1460             return false;
1461         }
1462 
1463         /**
1464          * Invoked when the doubleBuffered or useTrueDoubleBuffering
1465          * properties of a JRootPane change.  This may come in on any thread.
1466          */
1467         public void doubleBufferingChanged(JRootPane rootPane) {
1468         }
1469 
1470         /**
1471          * Paints a portion of a component to an offscreen buffer.
1472          */
1473         protected void paintDoubleBuffered(JComponent c, Image image,
1474                             Graphics g, int clipX, int clipY,
1475                             int clipW, int clipH) {
1476             Graphics osg = image.getGraphics();
1477             int bw = Math.min(clipW, image.getWidth(null));
1478             int bh = Math.min(clipH, image.getHeight(null));
1479             int x,y,maxx,maxy;
1480 
1481             try {
1482                 for(x = clipX, maxx = clipX+clipW; x < maxx ;  x += bw ) {
1483                     for(y=clipY, maxy = clipY + clipH; y < maxy ; y += bh) {
1484                         osg.translate(-x, -y);
1485                         osg.setClip(x,y,bw,bh);
1486                         c.paintToOffscreen(osg, x, y, bw, bh, maxx, maxy);
1487                         g.setClip(x, y, bw, bh);
1488                         g.drawImage(image, x, y, c);
1489                         osg.translate(x, y);
1490                     }
1491                 }
1492             } finally {
1493                 osg.dispose();
1494             }
1495         }
1496 
1497         /**
1498          * If <code>image</code> is non-null with a positive size it
1499          * is returned, otherwise null is returned.
1500          */
1501         private Image getValidImage(Image image) {
1502             if (image != null && image.getWidth(null) > 0 &&
1503                                  image.getHeight(null) > 0) {
1504                 return image;
1505             }
1506             return null;
1507         }
1508 
1509         /**
1510          * Schedules a repaint for the specified component.  This differs
1511          * from <code>root.repaint</code> in that if the RepaintManager is
1512          * currently processing paint requests it'll process this request
1513          * with the current set of requests.
1514          */
1515         protected void repaintRoot(JComponent root) {
1516             assert (repaintManager.repaintRoot == null);
1517             if (repaintManager.painting) {
1518                 repaintManager.repaintRoot = root;
1519             }
1520             else {
1521                 root.repaint();
1522             }
1523         }
1524 
1525         /**
1526          * Returns true if the component being painted is the root component
1527          * that was previously passed to <code>repaintRoot</code>.
1528          */
1529         protected boolean isRepaintingRoot() {
1530             return isRepaintingRoot;
1531         }
1532 
1533         /**
1534          * Cleans up any state.  After invoked the PaintManager will no
1535          * longer be used anymore.
1536          */
1537         protected void dispose() {
1538         }
1539     }
1540 
1541 
1542     private class DoubleBufferInfo {
1543         public Image image;
1544         public Dimension size;
1545         public boolean needsReset = false;
1546     }
1547 
1548 
1549     /**
1550      * Listener installed to detect display changes. When display changes,
1551      * schedules a callback to notify all RepaintManagers of the display
1552      * changes. Only one DisplayChangedHandler is ever installed. The
1553      * singleton instance will schedule notification for all AppContexts.
1554      */
1555     private static final class DisplayChangedHandler implements
1556                                              DisplayChangedListener {
1557         public void displayChanged() {
1558             scheduleDisplayChanges();
1559         }
1560 
1561         public void paletteChanged() {
1562         }
1563 
1564         private void scheduleDisplayChanges() {
1565             // To avoid threading problems, we notify each RepaintManager
1566             // on the thread it was created on.
1567             for (Object c : AppContext.getAppContexts()) {
1568                 AppContext context = (AppContext) c;
1569                 synchronized(context) {
1570                     if (!context.isDisposed()) {
1571                         EventQueue eventQueue = (EventQueue)context.get(
1572                             AppContext.EVENT_QUEUE_KEY);
1573                         if (eventQueue != null) {
1574                             eventQueue.postEvent(new InvocationEvent(
1575                                 Toolkit.getDefaultToolkit(),
1576                                 new DisplayChangedRunnable()));
1577                         }
1578                     }
1579                 }
1580             }
1581         }
1582     }
1583 
1584 
1585     private static final class DisplayChangedRunnable implements Runnable {
1586         public void run() {
1587             RepaintManager.currentManager((JComponent)null).displayChanged();
1588         }
1589     }
1590 
1591 
1592     /**
1593      * Runnable used to process all repaint/revalidate requests.
1594      */
1595     private final class ProcessingRunnable implements Runnable {
1596         // If true, we're wainting on the EventQueue.
1597         private boolean pending;
1598 
1599         /**
1600          * Marks this processing runnable as pending. If this was not
1601          * already marked as pending, true is returned.
1602          */
1603         public synchronized boolean markPending() {
1604             if (!pending) {
1605                 pending = true;
1606                 return true;
1607             }
1608             return false;
1609         }
1610 
1611         public void run() {
1612             synchronized (this) {
1613                 pending = false;
1614             }
1615             // First pass, flush any heavy paint events into real paint
1616             // events.  If there are pending heavy weight requests this will
1617             // result in q'ing this request up one more time.  As
1618             // long as no other requests come in between now and the time
1619             // the second one is processed nothing will happen.  This is not
1620             // ideal, but the logic needed to suppress the second request is
1621             // more headache than it's worth.
1622             scheduleHeavyWeightPaints();
1623             // Do the actual validation and painting.
1624             validateInvalidComponents();
1625             prePaintDirtyRegions();
1626         }
1627     }
1628     private RepaintManager getDelegate(Component c) {
1629         RepaintManager delegate = SwingUtilities3.getDelegateRepaintManager(c);
1630         if (this == delegate) {
1631             delegate = null;
1632         }
1633         return delegate;
1634     }
1635 }