src/java.desktop/share/classes/java/awt/image/BufferStrategy.java
author tvaleev
Thu, 04 Oct 2018 12:40:55 -0700
changeset 52248 2e330da7cbf4
parent 47216 71c04702a3d5
permissions -rw-r--r--
8211300: Convert C-style array declarations in JDK client code Reviewed-by: prr, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21957
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.awt.image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.BufferCapabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    33
 * The {@code BufferStrategy} class represents the mechanism with which
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    34
 * to organize complex memory on a particular {@code Canvas} or
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    35
 * {@code Window}.  Hardware and software limitations determine whether and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * how a particular buffer strategy can be implemented.  These limitations
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 19169
diff changeset
    37
 * are detectable through the capabilities of the
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    38
 * {@code GraphicsConfiguration} used when creating the
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
    39
 * {@code Canvas} or {@code Window}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * It is worth noting that the terms <i>buffer</i> and <i>surface</i> are meant
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * to be synonymous: an area of contiguous memory, either in video device
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * memory or in system memory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * There are several types of complex buffer strategies, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * sequential ring buffering and blit buffering.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Sequential ring buffering (i.e., double or triple
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * buffering) is the most common; an application draws to a single <i>back
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * buffer</i> and then moves the contents to the front (display) in a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * step, either by copying the data or moving the video pointer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * Moving the video pointer exchanges the buffers so that the first buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * drawn becomes the <i>front buffer</i>, or what is currently displayed on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * device; this is called <i>page flipping</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Alternatively, the contents of the back buffer can be copied, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <i>blitted</i> forward in a chain instead of moving the video pointer.
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 5506
diff changeset
    57
 * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Double buffering:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *                    ***********         ***********
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *                    *         * ------> *         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * [To display] <---- * Front B *   Show  * Back B. * <---- Rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *                    *         * <------ *         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *                    ***********         ***********
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * Triple buffering:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * [To      ***********         ***********        ***********
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * display] *         * --------+---------+------> *         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *    <---- * Front B *   Show  * Mid. B. *        * Back B. * <---- Rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *          *         * <------ *         * <----- *         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *          ***********         ***********        ***********
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *
19169
1807a84c3d63 8022447: Fix doclint warnings in java.awt.image
prr
parents: 5506
diff changeset
    74
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * Here is an example of how buffer strategies can be created and used:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * <pre><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * // Check the capabilities of the GraphicsConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * // Create our component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * Window w = new Window(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * // Show our window
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * w.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * // Create a general double-buffering strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * w.createBufferStrategy(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * BufferStrategy strategy = w.getBufferStrategy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * // Main loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *     // Prepare for rendering the next frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *     // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *     // Render single frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *     do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *         // The following loop ensures that the contents of the drawing buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *         // are consistent in case the underlying surface was recreated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *         do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *             // Get a new graphics context every time through the loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *             // to make sure the strategy is validated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *             Graphics graphics = strategy.getDrawGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *             // Render to graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *             // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *             // Dispose the graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *             graphics.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *             // Repeat the rendering if the drawing buffer contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *             // were restored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *         } while (strategy.contentsRestored());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *         // Display the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *         strategy.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *         // Repeat the rendering if the drawing buffer was lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *     } while (strategy.contentsLost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * // Dispose the window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * w.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * w.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * @see java.awt.Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @see java.awt.Canvas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @see java.awt.GraphicsConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @see VolatileImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @author Michael Martak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
public abstract class BufferStrategy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   138
     * Returns the {@code BufferCapabilities} for this
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   139
     * {@code BufferStrategy}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return the buffering capabilities of this strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public abstract BufferCapabilities getCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Creates a graphics context for the drawing buffer.  This method may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * be synchronized for performance reasons; use of this method by multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * threads should be handled at the application level.  Disposal of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * graphics object obtained must be handled by the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @return a graphics context for the drawing buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public abstract Graphics getDrawGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Returns whether the drawing buffer was lost since the last call to
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   157
     * {@code getDrawGraphics}.  Since the buffers in a buffer strategy
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   158
     * are usually type {@code VolatileImage}, they may become lost.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   159
     * For a discussion on lost buffers, see {@code VolatileImage}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @return Whether or not the drawing buffer was lost since the last call
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   162
     * to {@code getDrawGraphics}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @see java.awt.image.VolatileImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public abstract boolean contentsLost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns whether the drawing buffer was recently restored from a lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * state and reinitialized to the default background color (white).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Since the buffers in a buffer strategy are usually type
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   171
     * {@code VolatileImage}, they may become lost.  If a surface has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * been recently restored from a lost state since the last call to
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   173
     * {@code getDrawGraphics}, it may require repainting.
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   174
     * For a discussion on lost buffers, see {@code VolatileImage}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @return Whether or not the drawing buffer was restored since the last
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   177
     *         call to {@code getDrawGraphics}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @see java.awt.image.VolatileImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    public abstract boolean contentsRestored();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Makes the next available buffer visible by either copying the memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * (blitting) or changing the display pointer (flipping).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    public abstract void show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Releases system resources currently consumed by this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   190
     * {@code BufferStrategy} and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * removes it from the associated Component.  After invoking this
35667
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   192
     * method, {@code getBufferStrategy} will return null.  Trying
ed476aba94de 8138838: docs cleanup for java.desktop
avstepan
parents: 25859
diff changeset
   193
     * to use a {@code BufferStrategy} after it has been disposed will
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * result in undefined behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @see java.awt.Window#createBufferStrategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see java.awt.Canvas#createBufferStrategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @see java.awt.Window#getBufferStrategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @see java.awt.Canvas#getBufferStrategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}