jdk/src/share/classes/java/awt/image/BufferStrategy.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The <code>BufferStrategy</code> class represents the mechanism with which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * to organize complex memory on a particular <code>Canvas</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <code>Window</code>.  Hardware and software limitations determine whether and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * how a particular buffer strategy can be implemented.  These limitations
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * are detectible through the capabilities of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <code>GraphicsConfiguration</code> used when creating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * <code>Canvas</code> or <code>Window</code>.
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * Double buffering:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *                    ***********         ***********
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *                    *         * ------> *         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * [To display] <---- * Front B *   Show  * Back B. * <---- Rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *                    *         * <------ *         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *                    ***********         ***********
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * Triple buffering:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * [To      ***********         ***********        ***********
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * display] *         * --------+---------+------> *         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *    <---- * Front B *   Show  * Mid. B. *        * Back B. * <---- Rendering
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *          *         * <------ *         * <----- *         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *          ***********         ***********        ***********
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * Here is an example of how buffer strategies can be created and used:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <pre><code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * // Check the capabilities of the GraphicsConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * // Create our component
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Window w = new Window(gc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * // Show our window
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * w.setVisible(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * // Create a general double-buffering strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * w.createBufferStrategy(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * BufferStrategy strategy = w.getBufferStrategy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * // Main loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * while (!done) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *     // Prepare for rendering the next frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *     // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *     // Render single frame
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *     do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *         // The following loop ensures that the contents of the drawing buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *         // are consistent in case the underlying surface was recreated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *         do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *             // Get a new graphics context every time through the loop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *             // to make sure the strategy is validated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *             Graphics graphics = strategy.getDrawGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *             // Render to graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *             // ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *             // Dispose the graphics
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *             graphics.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *             // Repeat the rendering if the drawing buffer contents
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *             // were restored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *         } while (strategy.contentsRestored());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *         // Display the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *         strategy.show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *         // Repeat the rendering if the drawing buffer was lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *     } while (strategy.contentsLost());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * // Dispose the window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * w.setVisible(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * w.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @see java.awt.Window
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @see java.awt.Canvas
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @see java.awt.GraphicsConfiguration
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * @see VolatileImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 * @author Michael Martak
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
public abstract class BufferStrategy {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Returns the <code>BufferCapabilities</code> for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * <code>BufferStrategy</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * @return the buffering capabilities of this strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public abstract BufferCapabilities getCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * Creates a graphics context for the drawing buffer.  This method may not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * be synchronized for performance reasons; use of this method by multiple
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * threads should be handled at the application level.  Disposal of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * graphics object obtained must be handled by the application.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @return a graphics context for the drawing buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public abstract Graphics getDrawGraphics();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Returns whether the drawing buffer was lost since the last call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <code>getDrawGraphics</code>.  Since the buffers in a buffer strategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * are usually type <code>VolatileImage</code>, they may become lost.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * For a discussion on lost buffers, see <code>VolatileImage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * @return Whether or not the drawing buffer was lost since the last call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * to <code>getDrawGraphics</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see java.awt.image.VolatileImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public abstract boolean contentsLost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Returns whether the drawing buffer was recently restored from a lost
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * state and reinitialized to the default background color (white).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * Since the buffers in a buffer strategy are usually type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * <code>VolatileImage</code>, they may become lost.  If a surface has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * been recently restored from a lost state since the last call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * <code>getDrawGraphics</code>, it may require repainting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * For a discussion on lost buffers, see <code>VolatileImage</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return Whether or not the drawing buffer was restored since the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *         call to <code>getDrawGraphics</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @see java.awt.image.VolatileImage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    public abstract boolean contentsRestored();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * Makes the next available buffer visible by either copying the memory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * (blitting) or changing the display pointer (flipping).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public abstract void show();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * Releases system resources currently consumed by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <code>BufferStrategy</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * removes it from the associated Component.  After invoking this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * method, <code>getBufferStrategy</code> will return null.  Trying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * to use a <code>BufferStrategy</code> after it has been disposed will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * result in undefined behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @see java.awt.Window#createBufferStrategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @see java.awt.Canvas#createBufferStrategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @see java.awt.Window#getBufferStrategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @see java.awt.Canvas#getBufferStrategy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}