1 /* |
|
2 * Copyright 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 |
|
26 package sun.java2d.windows; |
|
27 |
|
28 import java.awt.Color; |
|
29 import java.awt.Transparency; |
|
30 import java.awt.Rectangle; |
|
31 import java.awt.image.ColorModel; |
|
32 import java.awt.image.IndexColorModel; |
|
33 import java.awt.image.DirectColorModel; |
|
34 import java.awt.image.BufferedImage; |
|
35 import java.awt.image.DataBuffer; |
|
36 import java.awt.image.DataBufferInt; |
|
37 |
|
38 import sun.awt.Win32GraphicsConfig; |
|
39 import sun.awt.Win32GraphicsDevice; |
|
40 import sun.awt.image.BufImgSurfaceData; |
|
41 import sun.awt.image.SunWritableRaster; |
|
42 import sun.java2d.SurfaceData; |
|
43 import sun.java2d.SurfaceDataProxy; |
|
44 import sun.java2d.SunGraphics2D; |
|
45 import sun.java2d.StateTracker; |
|
46 import sun.java2d.InvalidPipeException; |
|
47 import sun.java2d.loops.CompositeType; |
|
48 |
|
49 /** |
|
50 * The proxy class contains the logic for when to replace a |
|
51 * SurfaceData with a cached X11 Pixmap and the code to create |
|
52 * the accelerated surfaces. |
|
53 */ |
|
54 public abstract class Win32SurfaceDataProxy extends SurfaceDataProxy { |
|
55 /** |
|
56 * Represents the maximum size (width * height) of an image that we should |
|
57 * scan for an unused color. Any image larger than this would probably |
|
58 * require too much computation time. |
|
59 */ |
|
60 private static final int MAX_SIZE = 65536; |
|
61 |
|
62 public static SurfaceDataProxy createProxy(SurfaceData srcData, |
|
63 Win32GraphicsConfig dstConfig) |
|
64 { |
|
65 Win32GraphicsDevice wgd = |
|
66 (Win32GraphicsDevice) dstConfig.getDevice(); |
|
67 if (!wgd.isDDEnabledOnDevice() || |
|
68 srcData instanceof Win32SurfaceData || |
|
69 srcData instanceof Win32OffScreenSurfaceData) |
|
70 { |
|
71 // If they are not on the same screen then we could cache the |
|
72 // blit by returning an instance of Opaque below, but this |
|
73 // only happens for VolatileImage blits to the wrong screen |
|
74 // which we make no promises on so we just punt to UNCACHED... |
|
75 return UNCACHED; |
|
76 } |
|
77 |
|
78 ColorModel srcCM = srcData.getColorModel(); |
|
79 int srcTransparency = srcCM.getTransparency(); |
|
80 |
|
81 if (srcTransparency == Transparency.OPAQUE) { |
|
82 return new Opaque(dstConfig); |
|
83 } else if (srcTransparency == Transparency.BITMASK) { |
|
84 if (Bitmask.isCompatible(srcCM, srcData)) { |
|
85 return new Bitmask(dstConfig); |
|
86 } |
|
87 } |
|
88 |
|
89 return UNCACHED; |
|
90 } |
|
91 |
|
92 int srcTransparency; |
|
93 Win32GraphicsConfig wgc; |
|
94 |
|
95 public Win32SurfaceDataProxy(Win32GraphicsConfig wgc, |
|
96 int srcTransparency) |
|
97 { |
|
98 this.wgc = wgc; |
|
99 this.srcTransparency = srcTransparency; |
|
100 activateDisplayListener(); |
|
101 } |
|
102 |
|
103 @Override |
|
104 public SurfaceData validateSurfaceData(SurfaceData srcData, |
|
105 SurfaceData cachedData, |
|
106 int w, int h) |
|
107 { |
|
108 if (cachedData == null || |
|
109 !cachedData.isValid() || |
|
110 cachedData.isSurfaceLost()) |
|
111 { |
|
112 // use the device's color model for ddraw surfaces |
|
113 ColorModel dstScreenCM = wgc.getDeviceColorModel(); |
|
114 try { |
|
115 cachedData = |
|
116 Win32OffScreenSurfaceData.createData(w, h, |
|
117 dstScreenCM, |
|
118 wgc, null, |
|
119 srcTransparency); |
|
120 } catch (InvalidPipeException e) { |
|
121 Win32GraphicsDevice wgd = (Win32GraphicsDevice) wgc.getDevice(); |
|
122 if (!wgd.isDDEnabledOnDevice()) { |
|
123 invalidate(); |
|
124 flush(); |
|
125 return null; |
|
126 } |
|
127 } |
|
128 } |
|
129 return cachedData; |
|
130 } |
|
131 |
|
132 /** |
|
133 * Proxy for opaque source images. |
|
134 */ |
|
135 public static class Opaque extends Win32SurfaceDataProxy { |
|
136 static int TXMAX = |
|
137 (WindowsFlags.isDDScaleEnabled() |
|
138 ? SunGraphics2D.TRANSFORM_TRANSLATESCALE |
|
139 : SunGraphics2D.TRANSFORM_ANY_TRANSLATE); |
|
140 |
|
141 public Opaque(Win32GraphicsConfig wgc) { |
|
142 super(wgc, Transparency.OPAQUE); |
|
143 } |
|
144 |
|
145 @Override |
|
146 public boolean isSupportedOperation(SurfaceData srcData, |
|
147 int txtype, |
|
148 CompositeType comp, |
|
149 Color bgColor) |
|
150 { |
|
151 // we save a read from video memory for compositing |
|
152 // operations by copying from the buffered image sd |
|
153 return (txtype <= TXMAX && |
|
154 (CompositeType.SrcOverNoEa.equals(comp) || |
|
155 CompositeType.SrcNoEa.equals(comp))); |
|
156 } |
|
157 } |
|
158 |
|
159 /** |
|
160 * Proxy for bitmask transparent source images. |
|
161 * This proxy can accelerate unscaled SrcOver copies with no bgColor. |
|
162 * |
|
163 * Note that this proxy plays some games with returning the srcData |
|
164 * from the validate method. It needs to do this since the conditions |
|
165 * for caching an accelerated copy depend on many factors that can |
|
166 * change over time, including: |
|
167 * |
|
168 * - the depth of the display |
|
169 * - the availability of a transparent pixel |
|
170 */ |
|
171 public static class Bitmask extends Win32SurfaceDataProxy { |
|
172 /** |
|
173 * Tests a source image ColorModel and SurfaceData to |
|
174 * see if they are of an appropriate size and type to |
|
175 * perform our transparent pixel searches. |
|
176 * |
|
177 * Note that some dynamic factors may occur which prevent |
|
178 * us from finding or using a transparent pixel. These |
|
179 * are detailed above in the class comments. We do not |
|
180 * test those conditions here, but rely on the Bitmask |
|
181 * proxy to verify those conditions on the fly. |
|
182 */ |
|
183 public static boolean isCompatible(ColorModel srcCM, |
|
184 SurfaceData srcData) |
|
185 { |
|
186 if (srcCM instanceof IndexColorModel) { |
|
187 return true; |
|
188 } else if (srcCM instanceof DirectColorModel) { |
|
189 return isCompatibleDCM((DirectColorModel) srcCM, srcData); |
|
190 } |
|
191 |
|
192 return false; |
|
193 } |
|
194 |
|
195 /** |
|
196 * Tests a given DirectColorModel to make sure it is |
|
197 * compatible with the assumptions we make when scanning |
|
198 * a DCM image for a transparent pixel. |
|
199 */ |
|
200 public static boolean isCompatibleDCM(DirectColorModel dcm, |
|
201 SurfaceData srcData) |
|
202 { |
|
203 // The BISD restriction is because we need to |
|
204 // examine the pixels to find a tranparent color |
|
205 if (!(srcData instanceof BufImgSurfaceData)) { |
|
206 return false; |
|
207 } |
|
208 |
|
209 // The size restriction prevents us from wasting too |
|
210 // much time scanning large images for unused pixel values. |
|
211 Rectangle bounds = srcData.getBounds(); |
|
212 // Using division instead of multiplication avoids overflow |
|
213 if (bounds.width <= 0 || |
|
214 MAX_SIZE / bounds.width < bounds.height) |
|
215 { |
|
216 return false; |
|
217 } |
|
218 |
|
219 // Below we use the pixels from the data buffer to map |
|
220 // directly to pixel values using the dstData.pixelFor() |
|
221 // method so the pixel format must be compatible with |
|
222 // ARGB or we could end up with bad results. We assume |
|
223 // here that the destination is opaque and so only the |
|
224 // red, green, and blue masks matter. |
|
225 // These new checks for RGB masks are more correct, |
|
226 // but potentially reject the acceleration of some images |
|
227 // that we used to allow just because we cannot prove |
|
228 // that they will work OK. If we ever had an INT_BGR |
|
229 // image for instance, would that have really failed here? |
|
230 // 565 and 555 screens will both keep equal numbers of |
|
231 // bits of red and blue, but will differ in the amount of |
|
232 // green they keep so INT_BGR might be safe, but if anyone |
|
233 // ever created an INT_RBG image then 555 and 565 might |
|
234 // differ in whether they thought a transparent pixel |
|
235 // was available. Also, are there any other strange |
|
236 // screen formats where bizarre orderings of the RGB |
|
237 // would cause the tests below to make mistakes? |
|
238 return ((dcm.getPixelSize() == 25) && |
|
239 (dcm.getTransferType() == DataBuffer.TYPE_INT) && |
|
240 (dcm.getRedMask() == 0x00ff0000) && |
|
241 (dcm.getGreenMask() == 0x0000ff00) && |
|
242 (dcm.getBlueMask() == 0x000000ff)); |
|
243 } |
|
244 |
|
245 int transPixel; |
|
246 Color transColor; |
|
247 |
|
248 // The real accelerated surface - only used when we can find |
|
249 // a transparent color. |
|
250 SurfaceData accelData; |
|
251 |
|
252 public Bitmask(Win32GraphicsConfig wgc) { |
|
253 super(wgc, Transparency.BITMASK); |
|
254 } |
|
255 |
|
256 @Override |
|
257 public boolean isSupportedOperation(SurfaceData srcData, |
|
258 int txtype, |
|
259 CompositeType comp, |
|
260 Color bgColor) |
|
261 { |
|
262 // We have accelerated loops only for blits with SrcOverNoEa |
|
263 // (no blit bg loops or blit loops with SrcNoEa) |
|
264 return (CompositeType.SrcOverNoEa.equals(comp) && |
|
265 bgColor == null && |
|
266 txtype < SunGraphics2D.TRANSFORM_TRANSLATESCALE); |
|
267 } |
|
268 |
|
269 /** |
|
270 * Note that every time we update the surface we may or may |
|
271 * not find a transparent pixel depending on what was modified |
|
272 * in the source image since the last time we looked. |
|
273 * Our validation method saves the accelerated surface aside |
|
274 * in a different field so we can switch back and forth between |
|
275 * the accelerated version and null depending on whether we |
|
276 * find a transparent pixel. |
|
277 * Note that we also override getRetryTracker() and return a |
|
278 * tracker that tracks the source pixels so that we do not |
|
279 * try to revalidate until there are new pixels to be scanned. |
|
280 */ |
|
281 @Override |
|
282 public SurfaceData validateSurfaceData(SurfaceData srcData, |
|
283 SurfaceData cachedData, |
|
284 int w, int h) |
|
285 { |
|
286 // Evaluate the dest screen pixel size every time |
|
287 ColorModel dstScreenCM = wgc.getDeviceColorModel(); |
|
288 if (dstScreenCM.getPixelSize() <= 8) { |
|
289 return null; |
|
290 } |
|
291 accelData = super.validateSurfaceData(srcData, accelData, w, h); |
|
292 return (accelData != null && |
|
293 findTransparentPixel(srcData, accelData)) |
|
294 ? accelData |
|
295 : null; |
|
296 } |
|
297 |
|
298 @Override |
|
299 public StateTracker getRetryTracker(SurfaceData srcData) { |
|
300 // If we failed to validate, it is permanent until the |
|
301 // next change to srcData... |
|
302 return srcData.getStateTracker(); |
|
303 } |
|
304 |
|
305 @Override |
|
306 public void updateSurfaceData(SurfaceData srcData, |
|
307 SurfaceData dstData, |
|
308 int w, int h) |
|
309 { |
|
310 updateSurfaceDataBg(srcData, dstData, w, h, transColor); |
|
311 } |
|
312 |
|
313 /** |
|
314 * Invoked when the cached surface should be dropped. |
|
315 * Overrides the base class implementation so we can invalidate |
|
316 * the accelData field instead of the cachedSD field. |
|
317 */ |
|
318 @Override |
|
319 public synchronized void flush() { |
|
320 SurfaceData accelData = this.accelData; |
|
321 if (accelData != null) { |
|
322 this.accelData = null; |
|
323 accelData.flush(); |
|
324 } |
|
325 super.flush(); |
|
326 } |
|
327 |
|
328 /** |
|
329 * The following constants determine the size of the histograms |
|
330 * used when searching for an unused color |
|
331 */ |
|
332 private static final int ICM_HISTOGRAM_SIZE = 256; |
|
333 private static final int ICM_HISTOGRAM_MASK = ICM_HISTOGRAM_SIZE - 1; |
|
334 private static final int DCM_HISTOGRAM_SIZE = 1024; |
|
335 private static final int DCM_HISTOGRAM_MASK = DCM_HISTOGRAM_SIZE - 1; |
|
336 |
|
337 /** |
|
338 * Attempts to find an unused pixel value in the image and if |
|
339 * successful, sets up the DirectDraw surface so that it uses |
|
340 * this value as its color key. |
|
341 */ |
|
342 public boolean findTransparentPixel(SurfaceData srcData, |
|
343 SurfaceData accelData) |
|
344 { |
|
345 ColorModel srcCM = srcData.getColorModel(); |
|
346 boolean success = false; |
|
347 |
|
348 if (srcCM instanceof IndexColorModel) { |
|
349 success = findUnusedPixelICM((IndexColorModel) srcCM, |
|
350 accelData); |
|
351 } else if (srcCM instanceof DirectColorModel) { |
|
352 success = findUnusedPixelDCM((BufImgSurfaceData) srcData, |
|
353 accelData); |
|
354 } |
|
355 |
|
356 if (success) { |
|
357 int rgb = accelData.rgbFor(transPixel); |
|
358 transColor = new Color(rgb); |
|
359 Win32OffScreenSurfaceData wossd = |
|
360 (Win32OffScreenSurfaceData) accelData; |
|
361 wossd.setTransparentPixel(transPixel); |
|
362 } else { |
|
363 transColor = null; |
|
364 } |
|
365 return success; |
|
366 } |
|
367 |
|
368 /** |
|
369 * Attempts to find an unused pixel value in the color map of an |
|
370 * IndexColorModel. If successful, it returns that value (in the |
|
371 * ColorModel of the destination surface) or null otherwise. |
|
372 */ |
|
373 private boolean findUnusedPixelICM(IndexColorModel icm, |
|
374 SurfaceData accelData) { |
|
375 int mapsize = icm.getMapSize(); |
|
376 int[] histogram = new int[ICM_HISTOGRAM_SIZE]; |
|
377 int[] cmap = new int[mapsize]; |
|
378 icm.getRGBs(cmap); |
|
379 |
|
380 // load up the histogram |
|
381 for (int i = 0; i < mapsize; i++) { |
|
382 int pixel = accelData.pixelFor(cmap[i]); |
|
383 histogram[pixel & ICM_HISTOGRAM_MASK]++; |
|
384 } |
|
385 |
|
386 // find an empty histo-bucket |
|
387 for (int j = 0; j < histogram.length; j++) { |
|
388 if (histogram[j] == 0) { |
|
389 transPixel = j; |
|
390 return true; |
|
391 } |
|
392 } |
|
393 |
|
394 return false; |
|
395 } |
|
396 |
|
397 /** |
|
398 * Attempts to find an unused pixel value in an image with a |
|
399 * 25-bit DirectColorModel and a DataBuffer of TYPE_INT. |
|
400 * If successful, it returns that value (in the ColorModel |
|
401 * of the destination surface) or null otherwise. |
|
402 */ |
|
403 private boolean findUnusedPixelDCM(BufImgSurfaceData bisd, |
|
404 SurfaceData accelData) |
|
405 { |
|
406 BufferedImage bimg = (BufferedImage) bisd.getDestination(); |
|
407 DataBufferInt db = |
|
408 (DataBufferInt) bimg.getRaster().getDataBuffer(); |
|
409 int[] pixels = SunWritableRaster.stealData(db, 0); |
|
410 int[] histogram = new int[DCM_HISTOGRAM_SIZE]; |
|
411 |
|
412 // load up the histogram |
|
413 // REMIND: we could possibly make this faster by keeping track |
|
414 // of the unique colors found, and only doing a pixelFor() |
|
415 // when we come across a new unique color |
|
416 // REMIND: We are assuming pixels are in ARGB format. Is that |
|
417 // a safe assumption here? |
|
418 for (int i = 0; i < pixels.length; i++) { |
|
419 int pixel = accelData.pixelFor(pixels[i]); |
|
420 histogram[pixel & DCM_HISTOGRAM_MASK]++; |
|
421 } |
|
422 |
|
423 // find an empty histo-bucket |
|
424 for (int j = 0; j < histogram.length; j++) { |
|
425 if (histogram[j] == 0) { |
|
426 transPixel = j; |
|
427 return true; |
|
428 } |
|
429 } |
|
430 |
|
431 return false; |
|
432 } |
|
433 } |
|
434 } |
|