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