1. /*
  2. * @(#)BufferStrategy.java 1.6 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.image;
  8. import java.awt.BufferCapabilities;
  9. import java.awt.Graphics;
  10. import java.awt.Image;
  11. /**
  12. * The <code>BufferStrategy</code> class represents the mechanism with which
  13. * to organize complex memory on a particular <code>Canvas</code> or
  14. * <code>Window</code>. Hardware and software limitations determine whether and
  15. * how a particular buffer strategy can be implemented. These limitations
  16. * are detectible through the capabilities of the
  17. * <code>GraphicsConfiguration</code> used when creating the
  18. * <code>Canvas</code> or <code>Window</code>.
  19. * <p>
  20. * It is worth noting that the terms <i>buffer</i> and <i>surface</i> are meant
  21. * to be synonymous: an area of contiguous memory, either in video device
  22. * memory or in system memory.
  23. * <p>
  24. * There are several types of complex buffer strategies;
  25. * sequential ring buffering, blit buffering, and stereo buffering are
  26. * common types. Sequential ring buffering (i.e., double or triple
  27. * buffering) is the most common; an application draws to a single <i>back
  28. * buffer</i> and then moves the contents to the front (display) in a single
  29. * step, either by copying the data or moving the video pointer.
  30. * Moving the video pointer exchanges the buffers so that the first buffer
  31. * drawn becomes the <i>front buffer</i>, or what is currently displayed on the
  32. * device; this is called <i>page flipping</i>.
  33. * <p>
  34. * Alternatively, the contents of the back buffer can be copied, or
  35. * <i>blitted</i> forward in a chain instead of moving the video pointer.
  36. * <p>
  37. * <pre>
  38. * Double buffering:
  39. *
  40. * *********** ***********
  41. * * * ------> * *
  42. * [To display] <---- * Front B * Show * Back B. * <---- Rendering
  43. * * * <------ * *
  44. * *********** ***********
  45. *
  46. * Triple buffering:
  47. *
  48. * [To *********** *********** ***********
  49. * display] * * --------+---------+------> * *
  50. * <---- * Front B * Show * Mid. B. * * Back B. * <---- Rendering
  51. * * * <------ * * <----- * *
  52. * *********** *********** ***********
  53. *
  54. * </pre>
  55. * <p>
  56. * Stereo buffering is for hardware that supports rendering separate images for
  57. * a left and right eye. It is similar to sequential ring buffering, but
  58. * there are two buffer chains, one for each eye. Both buffer chains flip
  59. * simultaneously:
  60. *
  61. * <pre>
  62. * Stereo buffering:
  63. *
  64. * *********** ***********
  65. * * * ------> * *
  66. * [To left eye] <---- * Front B * * Back B. * <---- Rendering
  67. * * * <------ * *
  68. * *********** ***********
  69. * Show
  70. * *********** ***********
  71. * * * ------> * *
  72. * [To right eye] <--- * Front B * * Back B. * <---- Rendering
  73. * * * <------ * *
  74. * *********** ***********
  75. * </pre>
  76. * <p>
  77. * Here is an example of how buffer strategies can be created and used:
  78. * <pre><code>
  79. *
  80. * // Check the capabilities of the GraphicsConfiguration
  81. * ...
  82. *
  83. * // Create our component
  84. * Window w = new Window(gc);
  85. *
  86. * // Show our window
  87. * w.setVisible(true);
  88. *
  89. * // Create a general double-buffering strategy
  90. * w.createBufferStrategy(2);
  91. * BufferStrategy strategy = w.getBufferStrategy();
  92. *
  93. * // Render loop
  94. * while (!done) {
  95. * Graphics g = strategy.getDrawGraphics();
  96. * // Draw to graphics
  97. * ...
  98. * strategy.show();
  99. * }
  100. *
  101. * // Dispose the window
  102. * w.setVisible(false);
  103. * w.dispose();
  104. * </code></pre>
  105. *
  106. * @see java.awt.Component
  107. * @see java.awt.GraphicsConfiguration
  108. * @author Michael Martak
  109. * @since 1.4
  110. */
  111. public abstract class BufferStrategy {
  112. /**
  113. * @return the buffering capabilities of this strategy
  114. */
  115. public abstract BufferCapabilities getCapabilities();
  116. /**
  117. * @return the graphics on the drawing buffer. This method may not
  118. * be synchronized for performance reasons; use of this method by multiple
  119. * threads should be handled at the application level. Disposal of the
  120. * graphics object obtained must be handled by the application.
  121. */
  122. public abstract Graphics getDrawGraphics();
  123. /**
  124. * Returns whether the drawing buffer was lost since the last call to
  125. * <code>getDrawGraphics</code>. Since the buffers in a buffer strategy
  126. * are usually type <code>VolatileImage</code>, they may become lost.
  127. * For a discussion on lost buffers, see <code>VolatileImage</code>.
  128. * @see java.awt.image.VolatileImage
  129. */
  130. public abstract boolean contentsLost();
  131. /**
  132. * Returns whether the drawing buffer was recently restored from a lost
  133. * state and reinitialized to the default background color (white).
  134. * Since the buffers in a buffer strategy are usually type
  135. * <code>VolatileImage</code>, they may become lost. If a surface has
  136. * been recently restored from a lost state since the last call to
  137. * <code>getDrawGraphics</code>, it may require repainting.
  138. * For a discussion on lost buffers, see <code>VolatileImage</code>.
  139. * @see java.awt.image.VolatileImage
  140. */
  141. public abstract boolean contentsRestored();
  142. /**
  143. * Makes the next available buffer visible by either copying the memory
  144. * (blitting) or changing the display pointer (flipping).
  145. */
  146. public abstract void show();
  147. }