author | xdono |
Wed, 02 Jul 2008 12:55:45 -0700 | |
changeset 715 | f16baef3a20e |
parent 557 | 800259d3792b |
child 889 | 6549643c008c |
permissions | -rw-r--r-- |
2 | 1 |
/* |
715 | 2 |
* Copyright 2000-2008 Sun Microsystems, Inc. All Rights Reserved. |
2 | 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 |
||
26 |
package sun.awt.image; |
|
27 |
||
28 |
import java.awt.AlphaComposite; |
|
29 |
import java.awt.Color; |
|
30 |
import java.awt.Component; |
|
31 |
import java.awt.Font; |
|
32 |
import java.awt.Graphics; |
|
33 |
import java.awt.Graphics2D; |
|
34 |
import java.awt.GraphicsConfiguration; |
|
35 |
import java.awt.ImageCapabilities; |
|
36 |
import java.awt.Transparency; |
|
37 |
import java.awt.image.BufferedImage; |
|
38 |
import java.awt.image.ColorModel; |
|
39 |
import java.awt.image.ImageObserver; |
|
40 |
import java.awt.image.VolatileImage; |
|
41 |
import java.awt.image.WritableRaster; |
|
42 |
import sun.java2d.SunGraphics2D; |
|
43 |
import sun.java2d.SurfaceData; |
|
44 |
import 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 |
*/ |
|
55 |
public 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 |
} |
|
557
800259d3792b
6675596: SurfaceManagerFactory should allow plugging in different implementations
tdv
parents:
2
diff
changeset
|
168 |
SurfaceManagerFactory smf = SurfaceManagerFactory.getInstance(); |
800259d3792b
6675596: SurfaceManagerFactory should allow plugging in different implementations
tdv
parents:
2
diff
changeset
|
169 |
return smf.createVolatileManager(this, context); |
2 | 170 |
} |
171 |
||
172 |
private Color getForeground() { |
|
173 |
if (comp != null) { |
|
174 |
return comp.getForeground(); |
|
175 |
} else { |
|
176 |
return Color.black; |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
private Color getBackground() { |
|
181 |
if (comp != null) { |
|
182 |
return comp.getBackground(); |
|
183 |
} else { |
|
184 |
return Color.white; |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
private Font getFont() { |
|
189 |
if (comp != null) { |
|
190 |
return comp.getFont(); |
|
191 |
} else { |
|
192 |
if (defaultFont == null) { |
|
193 |
defaultFont = new Font("Dialog", Font.PLAIN, 12); |
|
194 |
} |
|
195 |
return defaultFont; |
|
196 |
} |
|
197 |
} |
|
198 |
||
199 |
public Graphics2D createGraphics() { |
|
200 |
return new SunGraphics2D(volSurfaceManager.getPrimarySurfaceData(), |
|
201 |
getForeground(), |
|
202 |
getBackground(), |
|
203 |
getFont()); |
|
204 |
} |
|
205 |
||
206 |
// Image method implementations |
|
207 |
public Object getProperty(String name, ImageObserver observer) { |
|
208 |
if (name == null) { |
|
209 |
throw new NullPointerException("null property name is not allowed"); |
|
210 |
} |
|
211 |
return java.awt.Image.UndefinedProperty; |
|
212 |
} |
|
213 |
||
214 |
public int getWidth(ImageObserver observer) { |
|
215 |
return getWidth(); |
|
216 |
} |
|
217 |
||
218 |
public int getHeight(ImageObserver observer) { |
|
219 |
return getHeight(); |
|
220 |
} |
|
221 |
||
222 |
/** |
|
223 |
* This method creates a BufferedImage intended for use as a "snapshot" |
|
224 |
* or a backup surface. |
|
225 |
*/ |
|
226 |
public BufferedImage getBackupImage() { |
|
227 |
return graphicsConfig.createCompatibleImage(getWidth(), getHeight(), |
|
228 |
getTransparency()); |
|
229 |
} |
|
230 |
||
231 |
public BufferedImage getSnapshot() { |
|
232 |
BufferedImage bi = getBackupImage(); |
|
233 |
Graphics2D g = bi.createGraphics(); |
|
234 |
g.setComposite(AlphaComposite.Src); |
|
235 |
g.drawImage(this, 0, 0, null); |
|
236 |
g.dispose(); |
|
237 |
return bi; |
|
238 |
} |
|
239 |
||
240 |
public int validate(GraphicsConfiguration gc) { |
|
241 |
return volSurfaceManager.validate(gc); |
|
242 |
} |
|
243 |
||
244 |
public boolean contentsLost() { |
|
245 |
return volSurfaceManager.contentsLost(); |
|
246 |
} |
|
247 |
||
248 |
public ImageCapabilities getCapabilities() { |
|
249 |
return volSurfaceManager.getCapabilities(graphicsConfig); |
|
250 |
} |
|
251 |
} |