blob: 875201d1bec59563278cdec66de60906781e640b [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2000-2007 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.awt.image;
27
28import java.awt.AlphaComposite;
29import java.awt.Color;
30import java.awt.Component;
31import java.awt.Font;
32import java.awt.Graphics;
33import java.awt.Graphics2D;
34import java.awt.GraphicsConfiguration;
35import java.awt.ImageCapabilities;
36import java.awt.Transparency;
37import java.awt.image.BufferedImage;
38import java.awt.image.ColorModel;
39import java.awt.image.ImageObserver;
40import java.awt.image.VolatileImage;
41import java.awt.image.WritableRaster;
42import sun.java2d.SunGraphics2D;
43import sun.java2d.SurfaceData;
44import sun.java2d.SurfaceManagerFactory;
45
46/**
47 * This class is the base implementation of the VolatileImage
48 * abstract class. The class implements most of the standard Image
49 * methods (width, height, etc.) but delegates all surface management
50 * issues to a platform-specific VolatileSurfaceManager. When a new instance
51 * of SunVolatileImage is created, it automatically creates an
52 * appropriate VolatileSurfaceManager for the GraphicsConfiguration
53 * under which this SunVolatileImage was created.
54 */
55public class SunVolatileImage extends VolatileImage {
56
57 protected VolatileSurfaceManager volSurfaceManager;
58 protected Component comp;
59 private GraphicsConfiguration graphicsConfig;
60 private Font defaultFont;
61 private int width, height;
62
63 private SunVolatileImage(Component comp,
64 GraphicsConfiguration graphicsConfig,
65 int width, int height, Object context,
66 int transparency, ImageCapabilities caps)
67 {
68 this.comp = comp;
69 this.graphicsConfig = graphicsConfig;
70 this.width = width;
71 this.height = height;
72 if (!(transparency == Transparency.OPAQUE ||
73 transparency == Transparency.BITMASK ||
74 transparency == Transparency.TRANSLUCENT))
75 {
76 throw new IllegalArgumentException("Unknown transparency type:" +
77 transparency);
78 }
79 this.transparency = transparency;
80 this.volSurfaceManager = createSurfaceManager(context, caps);
81 SurfaceManager.setManager(this, volSurfaceManager);
82
83 // post-construction initialization of the surface manager
84 volSurfaceManager.initialize();
85 // clear the background
86 volSurfaceManager.initContents();
87 }
88
89 private SunVolatileImage(Component comp,
90 GraphicsConfiguration graphicsConfig,
91 int width, int height, Object context,
92 ImageCapabilities caps)
93 {
94 this(comp, graphicsConfig,
95 width, height, context, Transparency.OPAQUE, caps);
96 }
97
98 public SunVolatileImage(Component comp, int width, int height) {
99 this(comp, width, height, null);
100 }
101
102 public SunVolatileImage(Component comp,
103 int width, int height, Object context)
104 {
105 this(comp, comp.getGraphicsConfiguration(),
106 width, height, context, null);
107 }
108
109 public SunVolatileImage(GraphicsConfiguration graphicsConfig,
110 int width, int height, int transparency,
111 ImageCapabilities caps)
112 {
113 this(null, graphicsConfig, width, height, null, transparency, caps);
114 }
115
116 public int getWidth() {
117 return width;
118 }
119
120 public int getHeight() {
121 return height;
122 }
123
124 public GraphicsConfiguration getGraphicsConfig() {
125 return graphicsConfig;
126 }
127
128 public void updateGraphicsConfig() {
129 // If this VImage is associated with a Component, get an updated
130 // graphicsConfig from that component. Otherwise, keep the one
131 // that we were created with
132 if (comp != null) {
133 GraphicsConfiguration gc = comp.getGraphicsConfiguration();
134 if (gc != null) {
135 // Could potentially be null in some failure situations;
136 // better to keep the old non-null value around than to
137 // set graphicsConfig to null
138 graphicsConfig = gc;
139 }
140 }
141 }
142
143 public Component getComponent() {
144 return comp;
145 }
146
147 protected VolatileSurfaceManager createSurfaceManager(Object context,
148 ImageCapabilities caps)
149 {
150 /**
151 * Platform-specific SurfaceManagerFactories will return a
152 * manager suited to acceleration on each platform. But if
153 * the user is asking for a VolatileImage from a BufferedImageGC,
154 * then we need to return the appropriate unaccelerated manager.
155 * Note: this could change in the future; if some platform would
156 * like to accelerate BIGC volatile images, then this special-casing
157 * of the BIGC graphicsConfig should live in platform-specific
158 * code instead.
159 * We do the same for a Printer Device, and if user requested an
160 * unaccelerated VolatileImage by passing the capabilities object.
161 */
162 if (graphicsConfig instanceof BufferedImageGraphicsConfig ||
163 graphicsConfig instanceof sun.print.PrinterGraphicsConfig ||
164 (caps != null && !caps.isAccelerated()))
165 {
166 return new BufImgVolatileSurfaceManager(this, context);
167 }
168 return SurfaceManagerFactory.createVolatileManager(this, context);
169 }
170
171 private Color getForeground() {
172 if (comp != null) {
173 return comp.getForeground();
174 } else {
175 return Color.black;
176 }
177 }
178
179 private Color getBackground() {
180 if (comp != null) {
181 return comp.getBackground();
182 } else {
183 return Color.white;
184 }
185 }
186
187 private Font getFont() {
188 if (comp != null) {
189 return comp.getFont();
190 } else {
191 if (defaultFont == null) {
192 defaultFont = new Font("Dialog", Font.PLAIN, 12);
193 }
194 return defaultFont;
195 }
196 }
197
198 public Graphics2D createGraphics() {
199 return new SunGraphics2D(volSurfaceManager.getPrimarySurfaceData(),
200 getForeground(),
201 getBackground(),
202 getFont());
203 }
204
205 // Image method implementations
206 public Object getProperty(String name, ImageObserver observer) {
207 if (name == null) {
208 throw new NullPointerException("null property name is not allowed");
209 }
210 return java.awt.Image.UndefinedProperty;
211 }
212
213 public int getWidth(ImageObserver observer) {
214 return getWidth();
215 }
216
217 public int getHeight(ImageObserver observer) {
218 return getHeight();
219 }
220
221 /**
222 * This method creates a BufferedImage intended for use as a "snapshot"
223 * or a backup surface.
224 */
225 public BufferedImage getBackupImage() {
226 return graphicsConfig.createCompatibleImage(getWidth(), getHeight(),
227 getTransparency());
228 }
229
230 public BufferedImage getSnapshot() {
231 BufferedImage bi = getBackupImage();
232 Graphics2D g = bi.createGraphics();
233 g.setComposite(AlphaComposite.Src);
234 g.drawImage(this, 0, 0, null);
235 g.dispose();
236 return bi;
237 }
238
239 public int validate(GraphicsConfiguration gc) {
240 return volSurfaceManager.validate(gc);
241 }
242
243 public boolean contentsLost() {
244 return volSurfaceManager.contentsLost();
245 }
246
247 public ImageCapabilities getCapabilities() {
248 return volSurfaceManager.getCapabilities(graphicsConfig);
249 }
250}