jdk/src/share/classes/javax/swing/BufferStrategyPaintManager.java
author lana
Thu, 26 Dec 2013 12:04:16 -0800
changeset 23010 6dadb192ad81
parent 18178 ee71c923891d
child 23328 4c53a6ebc779
permissions -rw-r--r--
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013 Summary: updated files with 2011, 2012 and 2013 years according to the file's last updated date Reviewed-by: tbell, lancea, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 18178
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3938
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.image.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.lang.ref.WeakReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.*;
3105
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
    33
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
    34
import com.sun.java.swing.SwingUtilities3;
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
    35
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.awt.SubRegionShowable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.java2d.SunGraphics2D;
3105
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
    38
import sun.java2d.pipe.hw.ExtendedBufferCapabilities;
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 888
diff changeset
    39
import sun.awt.SunToolkit;
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3109
diff changeset
    40
import sun.util.logging.PlatformLogger;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * A PaintManager implementation that uses a BufferStrategy for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * rendering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Scott Violet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
class BufferStrategyPaintManager extends RepaintManager.PaintManager {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // All drawing is done to a BufferStrategy.  At the end of painting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // (endPaint) the region that was painted is flushed to the screen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    // (using BufferStrategy.show).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // PaintManager.show is overriden to show directly from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // BufferStrategy (when using blit), if successful true is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // returned and a paint event will not be generated.  To avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // showing from the buffer while painting a locking scheme is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // implemented.  When beginPaint is invoked the field painting is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // set to true.  If painting is true and show is invoked we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // immediately return false.  This is done to avoid blocking the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    // toolkit thread while painting happens.  In a similar way when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // show is invoked the field showing is set to true, beginPaint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // will then block until showing is true.  This scheme ensures we
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // only ever have one thread using the BufferStrategy and it also
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // ensures the toolkit thread remains as responsive as possible.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // If we're using a flip strategy the contents of the backbuffer may
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // have changed and so show only attempts to show from the backbuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // if we get a blit strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Methods used to create BufferStrategy for Applets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static Method COMPONENT_CREATE_BUFFER_STRATEGY_METHOD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static Method COMPONENT_GET_BUFFER_STRATEGY_METHOD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3109
diff changeset
    78
    private static final PlatformLogger LOGGER = PlatformLogger.getLogger(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                           "javax.swing.BufferStrategyPaintManager");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * List of BufferInfos.  We don't use a Map primarily because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * there are typically only a handful of top level components making
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * a Map overkill.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private ArrayList<BufferInfo> bufferInfos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Indicates <code>beginPaint</code> has been invoked.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * set to true for the life of beginPaint/endPaint pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private boolean painting;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Indicates we're in the process of showing.  All painting, on the EDT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * is blocked while this is true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private boolean showing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    // Region that we need to flush.  When beginPaint is called these are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // reset and any subsequent calls to paint/copyArea then update these
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    // fields accordingly.  When endPaint is called we then try and show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    // the accumulated region.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // These fields are in the coordinate system of the root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private int accumulatedX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    private int accumulatedY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private int accumulatedMaxX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private int accumulatedMaxY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    // The following fields are set by prepare
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * Farthest JComponent ancestor for the current paint/copyArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private JComponent rootJ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * Location of component being painted relative to root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private int xOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Location of component being painted relative to root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private int yOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * Graphics from the BufferStrategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private Graphics bsg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * BufferStrategy currently being used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private BufferStrategy bufferStrategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * BufferInfo corresponding to root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    private BufferInfo bufferInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Set to true if the bufferInfo needs to be disposed when current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * paint loop is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private boolean disposeBufferOnEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    private static Method getGetBufferStrategyMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (COMPONENT_GET_BUFFER_STRATEGY_METHOD == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return COMPONENT_GET_BUFFER_STRATEGY_METHOD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private static Method getCreateBufferStrategyMethod() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        if (COMPONENT_CREATE_BUFFER_STRATEGY_METHOD == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            getMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        return COMPONENT_CREATE_BUFFER_STRATEGY_METHOD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private static void getMethods() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        java.security.AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            new java.security.PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    COMPONENT_CREATE_BUFFER_STRATEGY_METHOD = Component.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                              getDeclaredMethod("createBufferStrategy",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                                new Class[] { int.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                                BufferCapabilities.class });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    COMPONENT_CREATE_BUFFER_STRATEGY_METHOD.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                                            setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                    COMPONENT_GET_BUFFER_STRATEGY_METHOD = Component.class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                              getDeclaredMethod("getBufferStrategy");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    COMPONENT_GET_BUFFER_STRATEGY_METHOD.setAccessible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                } catch (NoSuchMethodException nsme) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                    assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    BufferStrategyPaintManager() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        bufferInfos = new ArrayList<BufferInfo>(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    // PaintManager methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Cleans up any created BufferStrategies.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    protected void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // dipose can be invoked at any random time. To avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // threading dependancies we do the actual diposing via an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // invokeLater.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                java.util.List<BufferInfo> bufferInfos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                synchronized(BufferStrategyPaintManager.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    while (showing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                        try {
8145
6714ca3d6773 7013453: BufferStrategyPaintManager.dispose will cause IllegalMonitorStateException in event thread
rupashka
parents: 5506
diff changeset
   205
                            BufferStrategyPaintManager.this.wait();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    bufferInfos = BufferStrategyPaintManager.this.bufferInfos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                    BufferStrategyPaintManager.this.bufferInfos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                dispose(bufferInfos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private void dispose(java.util.List<BufferInfo> bufferInfos) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   218
        if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3109
diff changeset
   219
            LOGGER.finer("BufferStrategyPaintManager disposed",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3109
diff changeset
   220
                         new RuntimeException());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (bufferInfos != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            for (BufferInfo bufferInfo : bufferInfos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                bufferInfo.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Shows the specified region of the back buffer.  This will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * true if successful, false otherwise.  This is invoked on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * toolkit thread in response to an expose event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public boolean show(Container c, int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (painting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                // Don't show from backbuffer while in the process of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                // painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            showing = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            BufferInfo info = getBufferInfo(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            BufferStrategy bufferStrategy;
3105
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   246
            if (info != null && info.isInSync() &&
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                (bufferStrategy = info.getBufferStrategy(false)) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                SubRegionShowable bsSubRegion =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                        (SubRegionShowable)bufferStrategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                boolean paintAllOnExpose = info.getPaintAllOnExpose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                info.setPaintAllOnExpose(false);
887
0aab8d3fa11a 6725214: D3D: forward-port the new pipeline from 6u10
tdv
parents: 2
diff changeset
   252
                if (bsSubRegion.showIfNotLost(x, y, (x + w), (y + h))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    return !paintAllOnExpose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                // Mark the buffer as needing to be repainted.  We don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                // immediately do a repaint as this method will return false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                // indicating a PaintEvent should be generated which will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                // trigger a complete repaint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                bufferInfo.setContentsLostDuringExpose(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                showing = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    public boolean paint(JComponent paintingComponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                         JComponent bufferComponent, Graphics g,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                         int x, int y, int w, int h) {
10690
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   274
        Container root = fetchRoot(paintingComponent);
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   275
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   276
        if (prepare(paintingComponent, root, true, x, y, w, h)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            if ((g instanceof SunGraphics2D) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                    ((SunGraphics2D)g).getDestination() == root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                // BufferStrategy may have already constrained the Graphics. To
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                // account for that we revert the constrain, then apply a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                // constrain for Swing on top of that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                int cx = ((SunGraphics2D)bsg).constrainX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                int cy = ((SunGraphics2D)bsg).constrainY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                if (cx != 0 || cy != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    bsg.translate(-cx, -cy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                ((SunGraphics2D)bsg).constrain(xOffset + cx, yOffset + cy,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                                               x + w, y + h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                bsg.setClip(x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                paintingComponent.paintToOffscreen(bsg, x, y, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                                                   x + w, y + h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                accumulate(xOffset + x, yOffset + y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                // Assume they are going to eventually render to the screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                // This disables showing from backbuffer until a complete
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                // repaint occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                bufferInfo.setInSync(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                // Fall through to old rendering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        // Invalid root, do what Swing has always done.
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   303
        if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            LOGGER.finer("prepare failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        return super.paint(paintingComponent, bufferComponent, g, x, y, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    public void copyArea(JComponent c, Graphics g, int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                         int deltaX, int deltaY, boolean clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        // Note: this method is only called internally and we know that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        // g is from a heavyweight Component, so no check is necessary as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        // it is in paint() above.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        // If the buffer isn't in sync there is no point in doing a copyArea,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // it has garbage.
10690
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   317
        Container root = fetchRoot(c);
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   318
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   319
        if (prepare(c, root, false, 0, 0, 0, 0) && bufferInfo.isInSync()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if (clip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                Rectangle cBounds = c.getVisibleRect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                int relX = xOffset + x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                int relY = yOffset + y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                bsg.clipRect(xOffset + cBounds.x,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                             yOffset + cBounds.y,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                             cBounds.width, cBounds.height);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                bsg.copyArea(relX, relY, w, h, deltaX, deltaY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                bsg.copyArea(xOffset + x, yOffset + y, w, h, deltaX,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                             deltaY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            accumulate(x + xOffset + deltaX, y + yOffset + deltaY, w, h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        } else {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   335
            if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                LOGGER.finer("copyArea: prepare failed or not in sync");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            // Prepare failed, or not in sync. By calling super.copyArea
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            // we'll copy on screen. We need to flush any pending paint to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            // the screen otherwise we'll do a copyArea on the wrong thing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (!flushAccumulatedRegion()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                // Flush failed, copyArea will be copying garbage,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                // force repaint of all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                rootJ.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                super.copyArea(c, g, x, y, w, h, deltaX, deltaY, clip);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    public void beginPaint() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            painting = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            // Make sure another thread isn't attempting to show from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            // the back buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            while(showing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   363
        if (LOGGER.isLoggable(PlatformLogger.Level.FINEST)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            LOGGER.finest("beginPaint");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // Reset the area that needs to be painted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        resetAccumulated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    public void endPaint() {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   371
        if (LOGGER.isLoggable(PlatformLogger.Level.FINEST)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            LOGGER.finest("endPaint: region " + accumulatedX + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                       accumulatedY + " " +  accumulatedMaxX + " " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                       accumulatedMaxY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        if (painting) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (!flushAccumulatedRegion()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                if (!isRepaintingRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    repaintRoot(rootJ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    // Contents lost twice in a row, punt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    resetDoubleBufferPerWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    // In case we've left junk on the screen, force a repaint.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    rootJ.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        BufferInfo toDispose = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            painting = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            if (disposeBufferOnEnd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                disposeBufferOnEnd = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                toDispose = bufferInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                bufferInfos.remove(toDispose);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (toDispose != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            toDispose.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Renders the BufferStrategy to the screen.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @return true if successful, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    private boolean flushAccumulatedRegion() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        boolean success = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (accumulatedX != Integer.MAX_VALUE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            SubRegionShowable bsSubRegion = (SubRegionShowable)bufferStrategy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            boolean contentsLost = bufferStrategy.contentsLost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            if (!contentsLost) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                bsSubRegion.show(accumulatedX, accumulatedY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                 accumulatedMaxX, accumulatedMaxY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                contentsLost = bufferStrategy.contentsLost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if (contentsLost) {
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   420
                if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    LOGGER.finer("endPaint: contents lost");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                // Shown region was bogus, mark buffer as out of sync.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                bufferInfo.setInSync(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                success = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        resetAccumulated();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        return success;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    private void resetAccumulated() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        accumulatedX = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        accumulatedY = Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        accumulatedMaxX = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        accumulatedMaxY = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Invoked when the double buffering or useTrueDoubleBuffering
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * changes for a JRootPane.  If the rootpane is not double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * buffered, or true double buffering changes we throw out any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * cache we may have.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public void doubleBufferingChanged(final JRootPane rootPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if ((!rootPane.isDoubleBuffered() ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                !rootPane.getUseTrueDoubleBuffering()) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                rootPane.getParent() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            if (!SwingUtilities.isEventDispatchThread()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                Runnable updater = new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        doubleBufferingChanged0(rootPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                SwingUtilities.invokeLater(updater);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                doubleBufferingChanged0(rootPane);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Does the work for doubleBufferingChanged.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    private void doubleBufferingChanged0(JRootPane rootPane) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        // This will only happen on the EDT.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        BufferInfo info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        synchronized(this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            // Make sure another thread isn't attempting to show from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            // the back buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            while(showing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            info = getBufferInfo(rootPane.getParent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            if (painting && bufferInfo == info) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                // We're in the process of painting and the user grabbed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                // the Graphics. If we dispose now, endPaint will attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                // to show a bogus BufferStrategy. Set a flag so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                // endPaint knows it needs to dispose this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                disposeBufferOnEnd = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                info = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            } else if (info != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                bufferInfos.remove(info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if (info != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            info.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Calculates information common to paint/copyArea.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @return true if should use buffering per window in painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     */
10690
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   500
    private boolean prepare(JComponent c, Container root, boolean isPaint, int x, int y,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                            int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        if (bsg != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            bsg.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            bsg = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        bufferStrategy = null;
10690
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   507
        if (root != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            boolean contentsLost = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            BufferInfo bufferInfo = getBufferInfo(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            if (bufferInfo == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
                contentsLost = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                bufferInfo = new BufferInfo(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                bufferInfos.add(bufferInfo);
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   514
                if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    LOGGER.finer("prepare: new BufferInfo: " + root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            this.bufferInfo = bufferInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            if (!bufferInfo.hasBufferStrategyChanged()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                bufferStrategy = bufferInfo.getBufferStrategy(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                if (bufferStrategy != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                    bsg = bufferStrategy.getDrawGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                    if (bufferStrategy.contentsRestored()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                        contentsLost = true;
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   525
                        if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
16839
d0f2e97b7359 8010297: Missing isLoggable() checks in logging code
anthony
parents: 10690
diff changeset
   526
                            LOGGER.finer("prepare: contents restored in prepare");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                    // Couldn't create BufferStrategy, fallback to normal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                    // painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                if (bufferInfo.getContentsLostDuringExpose()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                    contentsLost = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                    bufferInfo.setContentsLostDuringExpose(false);
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   538
                    if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                        LOGGER.finer("prepare: contents lost on expose");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                if (isPaint && c == rootJ && x == 0 && y == 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                      c.getWidth() == w && c.getHeight() == h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                    bufferInfo.setInSync(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                else if (contentsLost) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                    // We either recreated the BufferStrategy, or the contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    // of the buffer strategy were restored.  We need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                    // repaint the root pane so that the back buffer is in sync
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    // again.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                    bufferInfo.setInSync(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    if (!isRepaintingRoot()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        repaintRoot(rootJ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                        // Contents lost twice in a row, punt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                        resetDoubleBufferPerWindow();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                return (bufferInfos != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
10690
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   566
    private Container fetchRoot(JComponent c) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        boolean encounteredHW = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        rootJ = c;
10690
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   569
        Container root = c;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        xOffset = yOffset = 0;
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 888
diff changeset
   571
        while (root != null &&
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 888
diff changeset
   572
               (!(root instanceof Window) &&
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 888
diff changeset
   573
                !SunToolkit.isInstanceOf(root, "java.applet.Applet"))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            xOffset += root.getX();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            yOffset += root.getY();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            root = root.getParent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                if (root instanceof JComponent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    rootJ = (JComponent)root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                else if (!root.isLightweight()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    if (!encounteredHW) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                        encounteredHW = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        // We've encountered two hws now and may have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                        // a containment hierarchy with lightweights containing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        // heavyweights containing other lightweights.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        // Heavyweights poke holes in lightweight
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        // rendering so that if we call show on the BS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                        // (which is associated with the Window) you will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        // not see the contents over any child
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                        // heavyweights.  If we didn't do this when we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        // went to show the descendants of the nested hw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                        // you would see nothing, so, we bail out here.
10690
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   596
                        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if ((root instanceof RootPaneContainer) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            (rootJ instanceof JRootPane)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            // We're in a Swing heavyeight (JFrame/JWindow...), use double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            // buffering if double buffering enabled on the JRootPane and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            // the JRootPane wants true double buffering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if (rootJ.isDoubleBuffered() &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                    ((JRootPane)rootJ).getUseTrueDoubleBuffering()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                // Whether or not a component is double buffered is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                // bit tricky with Swing. This gives a good approximation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                // of the various ways to turn on double buffering for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                // components.
10690
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   612
                return root;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        // Don't do true double buffering.
10690
4e232ff8b640 7072167: The "root" field in BufferStrategyPaintManager leaks memory
rupashka
parents: 9035
diff changeset
   616
        return null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * Turns off double buffering per window.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    private void resetDoubleBufferPerWindow() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (bufferInfos != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            dispose(bufferInfos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            bufferInfos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            repaintManager.setPaintManager(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * Returns the BufferInfo for the specified root or null if one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * hasn't been created yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    private BufferInfo getBufferInfo(Container root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        for (int counter = bufferInfos.size() - 1; counter >= 0; counter--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            BufferInfo bufferInfo = bufferInfos.get(counter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            Container biRoot = bufferInfo.getRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            if (biRoot == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                // Window gc'ed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                bufferInfos.remove(counter);
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   641
                if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    LOGGER.finer("BufferInfo pruned, root null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            else if (biRoot == root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                return bufferInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    private void accumulate(int x, int y, int w, int h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        accumulatedX = Math.min(x, accumulatedX);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        accumulatedY = Math.min(y, accumulatedY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        accumulatedMaxX = Math.max(accumulatedMaxX, x + w);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        accumulatedMaxY = Math.max(accumulatedMaxY, y + h);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * BufferInfo is used to track the BufferStrategy being used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * a particular Component.  In addition to tracking the BufferStrategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * it will install a WindowListener and ComponentListener.  When the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * component is hidden/iconified the buffer is marked as needing to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * completely repainted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    private class BufferInfo extends ComponentAdapter implements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                               WindowListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        // NOTE: This class does NOT hold a direct reference to the root, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        // did there would be a cycle between the BufferPerWindowPaintManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        // and the Window so that it could never be GC'ed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        // Reference to BufferStrategy is referenced via WeakReference for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        // same reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        private WeakReference<BufferStrategy> weakBS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
        private WeakReference<Container> root;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        // Indicates whether or not the backbuffer and display are in sync.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        // This is set to true when a full repaint on the rootpane is done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        private boolean inSync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        // Indicates the contents were lost during and expose event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        private boolean contentsLostDuringExpose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        // Indicates we need to generate a paint event on expose.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        private boolean paintAllOnExpose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        public BufferInfo(Container root) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            this.root = new WeakReference<Container>(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            root.addComponentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            if (root instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                ((Window)root).addWindowListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        public void setPaintAllOnExpose(boolean paintAllOnExpose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            this.paintAllOnExpose = paintAllOnExpose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        public boolean getPaintAllOnExpose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            return paintAllOnExpose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        public void setContentsLostDuringExpose(boolean value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            contentsLostDuringExpose = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        public boolean getContentsLostDuringExpose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            return contentsLostDuringExpose;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        public void setInSync(boolean inSync) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            this.inSync = inSync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
         * Whether or not the contents of the buffer strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
         * is in sync with the window.  This is set to true when the root
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
         * pane paints all, and false when contents are lost/restored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        public boolean isInSync() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            return inSync;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         * Returns the Root (Window or Applet) that this BufferInfo references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        public Container getRoot() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
            return (root == null) ? null : root.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
         * Returns the BufferStartegy.  This will return null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
         * the BufferStartegy hasn't been created and <code>create</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         * false, or if there is a problem in creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
         * <code>BufferStartegy</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
         * @param create If true, and the BufferStartegy is currently null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
         *               one will be created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        public BufferStrategy getBufferStrategy(boolean create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            BufferStrategy bs = (weakBS == null) ? null : weakBS.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            if (bs == null && create) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                bs = createBufferStrategy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                if (bs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                    weakBS = new WeakReference<BufferStrategy>(bs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                }
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   747
                if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                    LOGGER.finer("getBufferStrategy: created bs: " + bs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            return bs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         * Returns true if the buffer strategy of the component differs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
         * from current buffer strategy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        public boolean hasBufferStrategyChanged() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            Container root = getRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
            if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                BufferStrategy ourBS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                BufferStrategy componentBS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                ourBS = getBufferStrategy(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                if (root instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    componentBS = ((Window)root).getBufferStrategy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                        componentBS = (BufferStrategy)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                                 getGetBufferStrategyMethod().invoke(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                    } catch (InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                        assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                        assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    } catch (IllegalAccessException iae2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                        assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                if (componentBS != ourBS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                    // Component has a different BS, dispose ours.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                    if (ourBS != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        ourBS.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                    weakBS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
         * Creates the BufferStrategy.  If the appropriate system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
         * has been set we'll try for flip first and then we'll try for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
         * blit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        private BufferStrategy createBufferStrategy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            Container root = getRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            if (root == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            BufferStrategy bs = null;
3105
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   803
            if (SwingUtilities3.isVsyncRequested(root)) {
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   804
                bs = createBufferStrategy(root, true);
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   805
                if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
3105
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   806
                    LOGGER.finer("createBufferStrategy: using vsynced strategy");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            if (bs == null) {
3105
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   810
                bs = createBufferStrategy(root, false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if (!(bs instanceof SubRegionShowable)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                // We do this for two reasons:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                // 1. So that we know we can cast to SubRegionShowable and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                //    invoke show with the minimal region to update
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                // 2. To avoid the possibility of invoking client code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                //    on the toolkit thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                bs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            return bs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
3105
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   823
        // Creates and returns a buffer strategy.  If
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        // there is a problem creating the buffer strategy this will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        // eat the exception and return null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        private BufferStrategy createBufferStrategy(Container root,
3105
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   827
                boolean isVsynced) {
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   828
            BufferCapabilities caps;
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   829
            if (isVsynced) {
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   830
                caps = new ExtendedBufferCapabilities(
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   831
                    new ImageCapabilities(true), new ImageCapabilities(true),
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   832
                    BufferCapabilities.FlipContents.COPIED,
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   833
                    ExtendedBufferCapabilities.VSyncType.VSYNC_ON);
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   834
            } else {
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   835
                caps = new BufferCapabilities(
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   836
                    new ImageCapabilities(true), new ImageCapabilities(true),
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   837
                    null);
b5931be76fbc 6679840: provide a way to choose v-synced BufferStrategy
malenkov
parents: 888
diff changeset
   838
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            BufferStrategy bs = null;
3084
67ca55732362 6824169: Need to remove some AWT class dependencies
dcherepanov
parents: 888
diff changeset
   840
            if (SunToolkit.isInstanceOf(root, "java.applet.Applet")) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                    getCreateBufferStrategyMethod().invoke(root, 2, caps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                    bs = (BufferStrategy)getGetBufferStrategyMethod().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
                                            invoke(root);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                } catch (InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                    // Type is not supported
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   847
                    if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3109
diff changeset
   848
                        LOGGER.finer("createBufferStratety failed",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3109
diff changeset
   849
                                     ite);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                } catch (IllegalArgumentException iae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                    assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                } catch (IllegalAccessException iae2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                    assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    ((Window)root).createBufferStrategy(2, caps);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    bs = ((Window)root).getBufferStrategy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                } catch (AWTException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    // Type not supported
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   863
                    if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3109
diff changeset
   864
                        LOGGER.finer("createBufferStratety failed",
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3109
diff changeset
   865
                                     e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            return bs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
         * Cleans up and removes any references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            Container root = getRoot();
18178
ee71c923891d 8016747: Replace deprecated PlatformLogger isLoggable(int) with isLoggable(Level)
chegar
parents: 16839
diff changeset
   877
            if (LOGGER.isLoggable(PlatformLogger.Level.FINER)) {
3938
ef327bd847c0 6879044: Eliminate the dependency on logging from the AWT/2D/Swing classes
mchung
parents: 3109
diff changeset
   878
                LOGGER.finer("disposed BufferInfo for: " + root);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
            if (root != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                root.removeComponentListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                if (root instanceof Window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    ((Window)root).removeWindowListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                BufferStrategy bs = getBufferStrategy(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                if (bs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    bs.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
            this.root = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            weakBS = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        // We mark the buffer as needing to be painted on a hide/iconify
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        // because the developer may have conditionalized painting based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        // visibility.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        // Ideally we would also move to having the BufferStrategy being
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        // a SoftReference in Component here, but that requires changes to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        // Component and the like.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        public void componentHidden(ComponentEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            Container root = getRoot();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            if (root != null && root.isVisible()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                // This case will only happen if a developer calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                // hide immediately followed by show.  In this case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                // the event is delivered after show and the window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                // will still be visible.  If a developer altered the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                // contents of the window between the hide/show
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
                // invocations we won't recognize we need to paint and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
                // the contents would be bogus.  Calling repaint here
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
                // fixs everything up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
                root.repaint();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
                setPaintAllOnExpose(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        public void windowIconified(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            setPaintAllOnExpose(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        // On a dispose we chuck everything.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        public void windowClosed(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            // Make sure we're not showing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            synchronized(BufferStrategyPaintManager.this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
                while (showing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                        BufferStrategyPaintManager.this.wait();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                    } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
                bufferInfos.remove(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        public void windowOpened(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        public void windowClosing(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        public void windowDeiconified(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        public void windowActivated(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        public void windowDeactivated(WindowEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
}