1. /*
  2. * @(#)VolatileImage.java 1.15 03/01/23
  3. *
  4. * Copyright 2003 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.Color;
  9. import java.awt.Graphics;
  10. import java.awt.Graphics2D;
  11. import java.awt.GraphicsConfiguration;
  12. import java.awt.GraphicsDevice;
  13. import java.awt.Image;
  14. import java.awt.ImageCapabilities;
  15. import java.awt.Toolkit;
  16. /**
  17. * VolatileImage is an image which can lose its
  18. * contents at any time due to circumstances beyond the control of the
  19. * application (e.g., situations caused by the operating system or by
  20. * other applications). Because of the potential for hardware acceleration,
  21. * a VolatileImage object can have significant performance benefits on
  22. * some platforms.
  23. * <p>
  24. * The drawing surface of an image (the memory where the image contents
  25. * actually reside) can be lost or invalidated, causing the contents of that
  26. * memory to go away. The drawing surface thus needs to be restored
  27. * or recreated and the contents of that surface need to be
  28. * re-rendered. VolatileImage provides an interface for
  29. * allowing the user to detect these problems and fix them
  30. * when they occur.
  31. * <p>
  32. * This image should not be subclassed directly but should be created
  33. * by using the {@link java.awt.Component#createVolatileImage(int, int)
  34. * Component.createVolatileImage} or
  35. * {@link java.awt.GraphicsConfiguration#createCompatibleVolatileImage(int, int)
  36. * GraphicsConfiguration.createCompatibleVolatileImage(int, int)} methods.
  37. * <P>
  38. * An example of using a VolatileImage object follows:
  39. * <pre>
  40. * // image creation
  41. * VolatileImage vImg = createVolatileImage(w, h);
  42. *
  43. *
  44. * // rendering to the image
  45. * void renderOffscreen() {
  46. * do {
  47. * if (vImg.validate(getGraphicsConfiguration()) ==
  48. * VolatileImage.IMAGE_INCOMPATIBLE)
  49. * {
  50. * // old vImg doesn't work with new GraphicsConfig; re-create it
  51. * vImg = createVolatileImage(w, h);
  52. * }
  53. * Graphics2D g = vImg.createGraphics();
  54. * //
  55. * // miscellaneous rendering commands...
  56. * //
  57. * g.dispose();
  58. * } while (vImg.contentsLost());
  59. * }
  60. *
  61. *
  62. * // copying from the image (here, gScreen is the Graphics
  63. * // object for the onscreen window)
  64. * do {
  65. * int returnCode = vImg.validate(getGraphicsConfiguration());
  66. * if (returnCode == VolatileImage.IMAGE_RESTORED) {
  67. * // Contents need to be restored
  68. * renderOffscreen(); // restore contents
  69. * } else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {
  70. * // old vImg doesn't work with new GraphicsConfig; re-create it
  71. * vImg = createVolatileImage(w, h);
  72. * renderOffscreen();
  73. * }
  74. * gScreen.drawImage(vImg, 0, 0, this);
  75. * } while (vImg.contentsLost());
  76. * </pre>
  77. * <P>
  78. * Note that this class subclasses from the {@link Image} class, which
  79. * includes methods that take an {@link ImageObserver} parameter for
  80. * asynchronous notifications as information is received from
  81. * a potential {@link ImageProducer}. Since this <code>VolatileImage</code>
  82. * is not loaded from an asynchronous source, the various methods that take
  83. * an <code>ImageObserver</code> parameter will behave as if the data has
  84. * already been obtained from the <code>ImageProducer</code>.
  85. * Specifically, this means that the return values from such methods
  86. * will never indicate that the information is not yet available and
  87. * the <code>ImageObserver</code> used in such methods will never
  88. * need to be recorded for an asynchronous callback notification.
  89. */
  90. public abstract class VolatileImage extends Image
  91. {
  92. // Return codes for validate() method
  93. /**
  94. * Validated image is ready to use as-is.
  95. */
  96. public static final int IMAGE_OK = 0;
  97. /**
  98. * Validated image has been restored and is now ready to use.
  99. * Note that restoration causes contents of the image to be lost.
  100. */
  101. public static final int IMAGE_RESTORED = 1;
  102. /**
  103. * Validated image is incompatible with supplied
  104. * <code>GraphicsConfiguration</code> object and should be
  105. * re-created as appropriate. Usage of the image as-is
  106. * after receiving this return code from <code>validate</code>
  107. * is undefined.
  108. */
  109. public static final int IMAGE_INCOMPATIBLE = 2;
  110. /**
  111. * Returns a static snapshot image of this object. The
  112. * <code>BufferedImage</code> returned is only current with
  113. * the <code>VolatileImage</code> at the time of the request
  114. * and will not be updated with any future changes to the
  115. * <code>VolatileImage</code>.
  116. * @return a {@link BufferedImage} representation of this
  117. * <code>VolatileImage</code>
  118. * @see BufferedImage
  119. */
  120. public abstract BufferedImage getSnapshot();
  121. /**
  122. * Returns the width of the <code>VolatileImage</code>.
  123. * @return the width of this <code>VolatileImage</code>.
  124. */
  125. public abstract int getWidth();
  126. /**
  127. * Returns the height of the <code>VolatileImage</code>.
  128. * @return the height of this <code>VolatileImage</code>.
  129. */
  130. public abstract int getHeight();
  131. // Image overrides
  132. /**
  133. * This returns an ImageProducer for this VolatileImage.
  134. * Note that the VolatileImage object is optimized for
  135. * rendering operations and blitting to the screen or other
  136. * VolatileImage objects, as opposed to reading back the
  137. * pixels of the image. Therefore, operations such as
  138. * <code>getSource</code> may not perform as fast as
  139. * operations that do not rely on reading the pixels.
  140. * Note also that the pixel values read from the image are current
  141. * with those in the image only at the time that they are
  142. * retrieved. This method takes a snapshot
  143. * of the image at the time the request is made and the
  144. * ImageProducer object returned works with
  145. * that static snapshot image, not the original VolatileImage.
  146. * Calling getSource()
  147. * is equivalent to calling getSnapshot().getSource().
  148. * @return an {@link ImageProducer} that can be used to produce the
  149. * pixels for a <code>BufferedImage</code> representation of
  150. * this Image.
  151. * @see ImageProducer
  152. * @see #getSnapshot()
  153. */
  154. public ImageProducer getSource() {
  155. // REMIND: Make sure this functionality is in line with the
  156. // spec. In particular, we are returning the Source for a
  157. // static image (the snapshot), not a changing image (the
  158. // VolatileImage). So if the user expects the Source to be
  159. // up-to-date with the current contents of the VolatileImage,
  160. // they will be disappointed...
  161. // REMIND: This assumes that getSnapshot() returns something
  162. // valid and not the default null object returned by this class
  163. // (so it assumes that the actual VolatileImage object is
  164. // subclassed off something that does the right thing
  165. // (e.g., SunVolatileImage).
  166. return getSnapshot().getSource();
  167. }
  168. // REMIND: if we want any decent performance for getScaledInstance(),
  169. // we should override the Image implementation of it...
  170. /**
  171. * Releases system resources currently consumed by this image.
  172. * <p>
  173. * When a VolatileImage object is created, limited system resources
  174. * such as video memory (VRAM) may be allocated in order to
  175. * support the image. When a VolatileImage object is no longer
  176. * used, it may be garbage-collected and those system resources
  177. * will be returned, but this process does
  178. * not happen at guaranteed times. Applications that create
  179. * many VolatileImage objects (for example, a resizing window
  180. * may force recreation of its back buffer as the size
  181. * changes) may run out of optimal system
  182. * resources for new VolatileImage objects simply because the
  183. * old objects have not yet been removed from the system.
  184. * (New VolatileImage objects may still be created, but they
  185. * may not perform as well as those created in accelerated
  186. * memory).
  187. * <p>
  188. * By calling this flush method, applications can have more control over
  189. * the state of the resources taken up by obsolete VolatileImage objects.
  190. * <p>
  191. * This method will cause the contents of the image to be lost, so
  192. * calls to {@link #contentsLost} will return <code>true</code>
  193. * and the image must be validated before it can be used again.
  194. * @see #contentsLost
  195. * @see #validate
  196. */
  197. public void flush() {
  198. }
  199. /**
  200. * This method returns a {@link Graphics2D}, but is here
  201. * for backwards compatibility. {@link #createGraphics() createGraphics} is more
  202. * convenient, since it is declared to return a
  203. * <code>Graphics2D</code>.
  204. * @return a <code>Graphics2D</code>, which can be used to draw into
  205. * this image.
  206. */
  207. public Graphics getGraphics() {
  208. return createGraphics();
  209. }
  210. /**
  211. * Creates a <code>Graphics2D</code>, which can be used to draw into
  212. * this <code>VolatileImage</code>.
  213. * @return a <code>Graphics2D</code>, used for drawing into this
  214. * image.
  215. */
  216. public abstract Graphics2D createGraphics();
  217. // Volatile management methods
  218. /**
  219. * Attempts to restore the drawing surface of the image if the surface
  220. * had been lost since the last <code>validate</code> call. Also
  221. * validates this image against the given GraphicsConfiguration
  222. * parameter to see whether operations from this image to the
  223. * GraphicsConfiguration are compatible. An example of an
  224. * incompatible combination might be a situation where a VolatileImage
  225. * object was created on one graphics device and then was used
  226. * to render to a different graphics device. Since VolatileImage
  227. * objects tend to be very device-specific, this operation might
  228. * not work as intended, so the return code from this validate
  229. * call would note that incompatibility. A null or incorrect
  230. * value for gc may cause incorrect values to be returned from
  231. * <code>validate</code> and may cause later problems with rendering.
  232. *
  233. * @param gc a <code>GraphicsConfiguration</code> object for this
  234. * image to be validated against. A null gc implies that the
  235. * validate method should skip the compatibility test.
  236. * @return <code>IMAGE_OK</code> if the image did not need validation<BR>
  237. * <code>IMAGE_RESTORED</code> if the image needed restoration.
  238. * Restoration implies that the contents of the image may have
  239. * been affected and the image may need to be re-rendered.<BR>
  240. * <code>IMAGE_INCOMPATIBLE</code> if the image is incompatible
  241. * with the <code>GraphicsConfiguration</code> object passed
  242. * into the <code>validate</code> method. Incompatibility
  243. * implies that the image may need to be recreated with a
  244. * new <code>Component</code> or
  245. * <code>GraphicsConfiguration</code> in order to get an image
  246. * that can be used successfully with this
  247. * <code>GraphicsConfiguration</code>.
  248. * An incompatible image is not checked for whether restoration
  249. * was necessary, so the state of the image is unchanged
  250. * after a return value of <code>IMAGE_INCOMPATIBLE</code>
  251. * and this return value implies nothing about whether the
  252. * image needs to be restored.
  253. * @see java.awt.GraphicsConfiguration
  254. * @see java.awt.Component
  255. * @see #IMAGE_OK
  256. * @see #IMAGE_RESTORED
  257. * @see #IMAGE_INCOMPATIBLE
  258. */
  259. public abstract int validate(GraphicsConfiguration gc);
  260. /**
  261. * Returns <code>true</code> if rendering data was lost since last
  262. * <code>validate</code> call. This method should be called by the
  263. * application at the end of any series of rendering operations to
  264. * or from the image to see whether
  265. * the image needs to be validated and the rendering redone.
  266. * @return <code>true</code> if the drawing surface needs to be restored;
  267. * <code>false</code> otherwise.
  268. */
  269. public abstract boolean contentsLost();
  270. /**
  271. * Returns an ImageCapabilities object which can be
  272. * inquired as to the specific capabilities of this
  273. * VolatileImage. This would allow programmers to find
  274. * out more runtime information on the specific VolatileImage
  275. * object that they have created. For example, the user
  276. * might create a VolatileImage but the system may have
  277. * no video memory left for creating an image of that
  278. * size, so although the object is a VolatileImage, it is
  279. * not as accelerated as other VolatileImage objects on
  280. * this platform might be. The user might want that
  281. * information to find other solutions to their problem.
  282. * @return an <code>ImageCapabilities</code> object that contains
  283. * the capabilities of this <code>VolatileImage</code>.
  284. * @since 1.4
  285. */
  286. public abstract ImageCapabilities getCapabilities();
  287. }