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