author | trims |
Thu, 11 Nov 2010 23:30:49 -0800 | |
changeset 7128 | a683e80b9ca8 |
parent 5506 | 202f599c92aa |
child 7748 | 26e6f5fc1bdb |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2007, 2008, Oracle and/or its affiliates. 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.java2d.d3d; |
|
27 |
||
28 |
import java.awt.AlphaComposite; |
|
887 | 29 |
import java.awt.BufferCapabilities; |
30 |
import java.awt.Component; |
|
2 | 31 |
import java.awt.GraphicsConfiguration; |
887 | 32 |
import java.awt.GraphicsDevice; |
33 |
import java.awt.GraphicsEnvironment; |
|
2 | 34 |
import java.awt.Image; |
887 | 35 |
import java.awt.Rectangle; |
2 | 36 |
import java.awt.Transparency; |
37 |
import java.awt.image.ColorModel; |
|
887 | 38 |
import java.awt.image.DataBuffer; |
2 | 39 |
import java.awt.image.DirectColorModel; |
887 | 40 |
import java.awt.image.Raster; |
41 |
import java.awt.image.SampleModel; |
|
42 |
import java.awt.image.SinglePixelPackedSampleModel; |
|
2 | 43 |
import sun.awt.SunHints; |
887 | 44 |
import sun.awt.image.DataBufferNative; |
45 |
import sun.awt.image.PixelConverter; |
|
2 | 46 |
import sun.awt.image.SurfaceManager; |
887 | 47 |
import sun.awt.image.WritableRasterNative; |
48 |
import sun.awt.windows.WComponentPeer; |
|
49 |
import sun.java2d.pipe.hw.AccelSurface; |
|
2 | 50 |
import sun.java2d.InvalidPipeException; |
51 |
import sun.java2d.SunGraphics2D; |
|
52 |
import sun.java2d.SurfaceData; |
|
53 |
import sun.java2d.loops.GraphicsPrimitive; |
|
887 | 54 |
import sun.java2d.loops.MaskFill; |
2 | 55 |
import sun.java2d.loops.SurfaceType; |
887 | 56 |
import sun.java2d.loops.CompositeType; |
57 |
import sun.java2d.pipe.ParallelogramPipe; |
|
58 |
import sun.java2d.pipe.PixelToParallelogramConverter; |
|
59 |
import sun.java2d.pipe.RenderBuffer; |
|
2 | 60 |
import sun.java2d.pipe.TextPipe; |
887 | 61 |
import static sun.java2d.pipe.BufferedOpCodes.*; |
62 |
import static sun.java2d.d3d.D3DContext.D3DContextCaps.*; |
|
63 |
import static sun.java2d.pipe.hw.ExtendedBufferCapabilities.VSyncType.*; |
|
64 |
import sun.java2d.pipe.hw.ExtendedBufferCapabilities.VSyncType; |
|
65 |
import java.awt.BufferCapabilities.FlipContents; |
|
66 |
import java.awt.Window; |
|
67 |
import sun.awt.SunToolkit; |
|
68 |
import sun.awt.image.SunVolatileImage; |
|
69 |
import sun.java2d.ScreenUpdateManager; |
|
70 |
import sun.java2d.StateTracker; |
|
71 |
import sun.java2d.SurfaceDataProxy; |
|
72 |
import sun.java2d.pipe.hw.ExtendedBufferCapabilities; |
|
2 | 73 |
|
887 | 74 |
/** |
75 |
* This class describes a D3D "surface", that is, a region of pixels |
|
76 |
* managed via D3D. An D3DSurfaceData can be tagged with one of three |
|
77 |
* different SurfaceType objects for the purpose of registering loops, etc. |
|
78 |
* This diagram shows the hierarchy of D3D SurfaceTypes: |
|
79 |
* |
|
80 |
* Any |
|
81 |
* / \ |
|
82 |
* D3DSurface D3DTexture |
|
83 |
* | |
|
84 |
* D3DSurfaceRTT |
|
85 |
* |
|
86 |
* D3DSurface |
|
87 |
* This kind of surface can be rendered to using D3D APIs. It is also |
|
88 |
* possible to copy a D3DSurface to another D3DSurface (or to itself). |
|
89 |
* |
|
90 |
* D3DTexture |
|
91 |
* This kind of surface cannot be rendered to using D3D (in the same sense |
|
92 |
* as in D3DSurface). However, it is possible to upload a region of pixels |
|
93 |
* to a D3DTexture object via Lock/UnlockRect(). One can also copy a |
|
94 |
* surface of type D3DTexture to a D3DSurface by binding the texture |
|
95 |
* to a quad and then rendering it to the destination surface (this process |
|
96 |
* is known as "texture mapping"). |
|
97 |
* |
|
98 |
* D3DSurfaceRTT |
|
99 |
* This kind of surface can be thought of as a sort of hybrid between |
|
100 |
* D3DSurface and D3DTexture, in that one can render to this kind of |
|
101 |
* surface as if it were of type D3DSurface, but the process of copying |
|
102 |
* this kind of surface to another is more like a D3DTexture. (Note that |
|
103 |
* "RTT" stands for "render-to-texture".) |
|
104 |
* |
|
105 |
* In addition to these SurfaceType variants, we have also defined some |
|
106 |
* constants that describe in more detail the type of underlying D3D |
|
107 |
* surface. This table helps explain the relationships between those |
|
108 |
* "type" constants and their corresponding SurfaceType: |
|
109 |
* |
|
110 |
* D3D Type Corresponding SurfaceType |
|
111 |
* -------- ------------------------- |
|
112 |
* RT_PLAIN D3DSurface |
|
113 |
* TEXTURE D3DTexture |
|
114 |
* FLIP_BACKBUFFER D3DSurface |
|
115 |
* RT_TEXTURE D3DSurfaceRTT |
|
116 |
*/ |
|
117 |
public class D3DSurfaceData extends SurfaceData implements AccelSurface { |
|
2 | 118 |
|
119 |
/** |
|
887 | 120 |
* To be used with getNativeResource() only. |
121 |
* @see #getNativeResource() |
|
2 | 122 |
*/ |
887 | 123 |
public static final int D3D_DEVICE_RESOURCE= 100; |
124 |
/* |
|
125 |
* Surface types. |
|
126 |
* We use these surface types when copying from a sw surface |
|
127 |
* to a surface or texture. |
|
2 | 128 |
*/ |
887 | 129 |
public static final int ST_INT_ARGB = 0; |
130 |
public static final int ST_INT_ARGB_PRE = 1; |
|
131 |
public static final int ST_INT_ARGB_BM = 2; |
|
132 |
public static final int ST_INT_RGB = 3; |
|
133 |
public static final int ST_INT_BGR = 4; |
|
134 |
public static final int ST_USHORT_565_RGB = 5; |
|
135 |
public static final int ST_USHORT_555_RGB = 6; |
|
136 |
public static final int ST_BYTE_INDEXED = 7; |
|
137 |
public static final int ST_BYTE_INDEXED_BM = 8; |
|
1723
4a616c1eac0b
6739267: D3D/OGL: add missing ThreeByteBgr to texture upload blit loop
tdv
parents:
1717
diff
changeset
|
138 |
public static final int ST_3BYTE_BGR = 9; |
2 | 139 |
|
887 | 140 |
/** Equals to D3DSWAPEFFECT_DISCARD */ |
141 |
public static final int SWAP_DISCARD = 1; |
|
142 |
/** Equals to D3DSWAPEFFECT_FLIP */ |
|
143 |
public static final int SWAP_FLIP = 2; |
|
144 |
/** Equals to D3DSWAPEFFECT_COPY */ |
|
145 |
public static final int SWAP_COPY = 3; |
|
146 |
/* |
|
147 |
* SurfaceTypes |
|
148 |
*/ |
|
149 |
private static final String DESC_D3D_SURFACE = "D3D Surface"; |
|
150 |
private static final String DESC_D3D_SURFACE_RTT = |
|
151 |
"D3D Surface (render-to-texture)"; |
|
152 |
private static final String DESC_D3D_TEXTURE = "D3D Texture"; |
|
2 | 153 |
|
887 | 154 |
// REMIND: regarding ArgbPre?? |
155 |
static final SurfaceType D3DSurface = |
|
156 |
SurfaceType.Any.deriveSubType(DESC_D3D_SURFACE, |
|
157 |
PixelConverter.ArgbPre.instance); |
|
158 |
static final SurfaceType D3DSurfaceRTT = |
|
159 |
D3DSurface.deriveSubType(DESC_D3D_SURFACE_RTT); |
|
160 |
static final SurfaceType D3DTexture = |
|
161 |
SurfaceType.Any.deriveSubType(DESC_D3D_TEXTURE); |
|
2 | 162 |
|
887 | 163 |
private int type; |
164 |
private int width, height; |
|
165 |
// these fields are set from the native code when the surface is |
|
166 |
// initialized |
|
167 |
private int nativeWidth, nativeHeight; |
|
168 |
protected WComponentPeer peer; |
|
169 |
private Image offscreenImage; |
|
170 |
protected D3DGraphicsDevice graphicsDevice; |
|
2 | 171 |
|
887 | 172 |
private int swapEffect; |
173 |
private VSyncType syncType; |
|
174 |
private int backBuffersNum; |
|
2 | 175 |
|
887 | 176 |
private WritableRasterNative wrn; |
2 | 177 |
|
887 | 178 |
protected static D3DRenderer d3dRenderPipe; |
179 |
protected static PixelToParallelogramConverter d3dTxRenderPipe; |
|
180 |
protected static ParallelogramPipe d3dAAPgramPipe; |
|
181 |
protected static D3DTextRenderer d3dTextPipe; |
|
182 |
protected static D3DDrawImage d3dImagePipe; |
|
2 | 183 |
|
887 | 184 |
private native boolean initTexture(long pData, boolean isRTT, |
185 |
boolean isOpaque); |
|
186 |
private native boolean initFlipBackbuffer(long pData, long pPeerData, |
|
187 |
int numbuffers, |
|
188 |
int swapEffect, int syncType); |
|
189 |
private native boolean initRTSurface(long pData, boolean isOpaque); |
|
190 |
private native void initOps(int screen, int width, int height); |
|
2 | 191 |
|
192 |
static { |
|
887 | 193 |
D3DRenderQueue rq = D3DRenderQueue.getInstance(); |
194 |
d3dImagePipe = new D3DDrawImage(); |
|
195 |
d3dTextPipe = new D3DTextRenderer(rq); |
|
196 |
d3dRenderPipe = new D3DRenderer(rq); |
|
197 |
if (GraphicsPrimitive.tracingEnabled()) { |
|
198 |
d3dTextPipe = d3dTextPipe.traceWrap(); |
|
199 |
d3dRenderPipe = d3dRenderPipe.traceWrap(); |
|
200 |
//The wrapped d3dRenderPipe will wrap the AA pipe as well... |
|
201 |
//d3dAAPgramPipe = d3dRenderPipe.traceWrap(); |
|
2 | 202 |
} |
887 | 203 |
d3dAAPgramPipe = d3dRenderPipe.getAAParallelogramPipe(); |
204 |
d3dTxRenderPipe = |
|
205 |
new PixelToParallelogramConverter(d3dRenderPipe, d3dRenderPipe, |
|
206 |
1.0, 0.25, true); |
|
207 |
||
208 |
D3DBlitLoops.register(); |
|
209 |
D3DMaskFill.register(); |
|
210 |
D3DMaskBlit.register(); |
|
211 |
} |
|
2 | 212 |
|
887 | 213 |
protected D3DSurfaceData(WComponentPeer peer, D3DGraphicsConfig gc, |
214 |
int width, int height, Image image, |
|
215 |
ColorModel cm, int numBackBuffers, |
|
216 |
int swapEffect, VSyncType vSyncType, |
|
217 |
int type) |
|
218 |
{ |
|
219 |
super(getCustomSurfaceType(type), cm); |
|
220 |
this.graphicsDevice = gc.getD3DDevice(); |
|
221 |
this.peer = peer; |
|
222 |
this.type = type; |
|
223 |
this.width = width; |
|
224 |
this.height = height; |
|
225 |
this.offscreenImage = image; |
|
226 |
this.backBuffersNum = numBackBuffers; |
|
227 |
this.swapEffect = swapEffect; |
|
228 |
this.syncType = vSyncType; |
|
2 | 229 |
|
887 | 230 |
initOps(graphicsDevice.getScreen(), width, height); |
231 |
if (type == WINDOW) { |
|
232 |
// we put the surface into the "lost" |
|
233 |
// state; it will be restored by the D3DScreenUpdateManager |
|
234 |
// prior to rendering to it for the first time. This is done |
|
235 |
// so that vram is not wasted for surfaces never rendered to |
|
236 |
setSurfaceLost(true); |
|
237 |
} else { |
|
238 |
initSurface(); |
|
2 | 239 |
} |
887 | 240 |
setBlitProxyKey(gc.getProxyKey()); |
2 | 241 |
} |
242 |
||
243 |
@Override |
|
244 |
public SurfaceDataProxy makeProxyFor(SurfaceData srcData) { |
|
887 | 245 |
return D3DSurfaceDataProxy. |
246 |
createProxy(srcData, |
|
247 |
(D3DGraphicsConfig)graphicsDevice.getDefaultConfiguration()); |
|
248 |
} |
|
249 |
||
250 |
/** |
|
251 |
* Creates a SurfaceData object representing the back buffer of a |
|
252 |
* double-buffered on-screen Window. |
|
253 |
*/ |
|
254 |
public static D3DSurfaceData createData(WComponentPeer peer, Image image) { |
|
255 |
D3DGraphicsConfig gc = getGC(peer); |
|
256 |
if (gc == null || !peer.isAccelCapable()) { |
|
257 |
return null; |
|
258 |
} |
|
259 |
BufferCapabilities caps = peer.getBackBufferCaps(); |
|
260 |
VSyncType vSyncType = VSYNC_DEFAULT; |
|
261 |
if (caps instanceof ExtendedBufferCapabilities) { |
|
262 |
vSyncType = ((ExtendedBufferCapabilities)caps).getVSync(); |
|
263 |
} |
|
264 |
Rectangle r = peer.getBounds(); |
|
265 |
BufferCapabilities.FlipContents flip = caps.getFlipContents(); |
|
266 |
int swapEffect; |
|
267 |
if (flip == FlipContents.COPIED) { |
|
268 |
swapEffect = SWAP_COPY; |
|
269 |
} else if (flip == FlipContents.PRIOR) { |
|
270 |
swapEffect = SWAP_FLIP; |
|
271 |
} else { // flip == FlipContents.UNDEFINED || .BACKGROUND |
|
272 |
swapEffect = SWAP_DISCARD; |
|
273 |
} |
|
274 |
return new D3DSurfaceData(peer, gc, r.width, r.height, |
|
275 |
image, peer.getColorModel(), |
|
276 |
peer.getBackBuffersNum(), |
|
277 |
swapEffect, vSyncType, FLIP_BACKBUFFER); |
|
2 | 278 |
} |
279 |
||
280 |
/** |
|
887 | 281 |
* Returns a WINDOW type of surface - a |
282 |
* swap chain which serves as an on-screen surface, |
|
283 |
* handled by the D3DScreenUpdateManager. |
|
284 |
* |
|
285 |
* Note that the native surface is not initialized |
|
286 |
* when the surface is created to avoid using excessive |
|
287 |
* resources, and the surface is placed into the lost |
|
288 |
* state. It will be restored prior to any rendering |
|
289 |
* to it. |
|
2 | 290 |
* |
887 | 291 |
* @param peer peer for which the onscreen surface is to be created |
292 |
* @return a D3DWindowSurfaceData (flip chain) surface |
|
293 |
*/ |
|
294 |
public static D3DSurfaceData createData(WComponentPeer peer) { |
|
295 |
D3DGraphicsConfig gc = getGC(peer); |
|
296 |
if (gc == null || !peer.isAccelCapable()) { |
|
297 |
return null; |
|
298 |
} |
|
299 |
return new D3DWindowSurfaceData(peer, gc); |
|
300 |
} |
|
301 |
||
302 |
/** |
|
303 |
* Creates a SurfaceData object representing an off-screen buffer (either |
|
304 |
* a plain surface or Texture). |
|
2 | 305 |
*/ |
887 | 306 |
public static D3DSurfaceData createData(D3DGraphicsConfig gc, |
307 |
int width, int height, |
|
308 |
ColorModel cm, |
|
309 |
Image image, int type) |
|
2 | 310 |
{ |
887 | 311 |
if (type == RT_TEXTURE) { |
312 |
boolean isOpaque = cm.getTransparency() == Transparency.OPAQUE; |
|
313 |
int cap = isOpaque ? CAPS_RT_TEXTURE_OPAQUE : CAPS_RT_TEXTURE_ALPHA; |
|
314 |
if (!gc.getD3DDevice().isCapPresent(cap)) { |
|
315 |
type = RT_PLAIN; |
|
316 |
} |
|
317 |
} |
|
318 |
D3DSurfaceData ret = null; |
|
319 |
try { |
|
320 |
ret = new D3DSurfaceData(null, gc, width, height, |
|
321 |
image, cm, 0, SWAP_DISCARD, VSYNC_DEFAULT, |
|
322 |
type); |
|
323 |
} catch (InvalidPipeException ipe) { |
|
324 |
// try again - we might have ran out of vram, and rt textures |
|
325 |
// could take up more than a plain surface, so it might succeed |
|
326 |
if (type == RT_TEXTURE) { |
|
327 |
// If a RT_TEXTURE was requested do not attempt to create a |
|
328 |
// plain surface. (note that RT_TEXTURE can only be requested |
|
329 |
// from a VI so the cast is safe) |
|
330 |
if (((SunVolatileImage)image).getForcedAccelSurfaceType() != |
|
331 |
RT_TEXTURE) |
|
332 |
{ |
|
333 |
type = RT_PLAIN; |
|
334 |
ret = new D3DSurfaceData(null, gc, width, height, |
|
335 |
image, cm, 0, SWAP_DISCARD, |
|
336 |
VSYNC_DEFAULT, type); |
|
337 |
} |
|
338 |
} |
|
339 |
} |
|
340 |
return ret; |
|
2 | 341 |
} |
342 |
||
343 |
/** |
|
887 | 344 |
* Returns the appropriate SurfaceType corresponding to the given D3D |
345 |
* surface type constant (e.g. TEXTURE -> D3DTexture). |
|
2 | 346 |
*/ |
887 | 347 |
private static SurfaceType getCustomSurfaceType(int d3dType) { |
348 |
switch (d3dType) { |
|
349 |
case TEXTURE: |
|
350 |
return D3DTexture; |
|
351 |
case RT_TEXTURE: |
|
352 |
return D3DSurfaceRTT; |
|
353 |
default: |
|
354 |
return D3DSurface; |
|
355 |
} |
|
2 | 356 |
} |
357 |
||
887 | 358 |
private boolean initSurfaceNow() { |
359 |
boolean isOpaque = (getTransparency() == Transparency.OPAQUE); |
|
360 |
switch (type) { |
|
361 |
case RT_PLAIN: |
|
362 |
return initRTSurface(getNativeOps(), isOpaque); |
|
363 |
case TEXTURE: |
|
364 |
return initTexture(getNativeOps(), false/*isRTT*/, isOpaque); |
|
365 |
case RT_TEXTURE: |
|
366 |
return initTexture(getNativeOps(), true/*isRTT*/, isOpaque); |
|
367 |
// REMIND: we may want to pass the exact type to the native |
|
368 |
// level here so that we could choose the right presentation |
|
369 |
// interval for the frontbuffer (immediate vs v-synced) |
|
370 |
case WINDOW: |
|
371 |
case FLIP_BACKBUFFER: |
|
372 |
return initFlipBackbuffer(getNativeOps(), peer.getData(), |
|
373 |
backBuffersNum, swapEffect, |
|
374 |
syncType.id()); |
|
375 |
default: |
|
376 |
return false; |
|
2 | 377 |
} |
378 |
} |
|
379 |
||
887 | 380 |
/** |
381 |
* Initializes the appropriate D3D offscreen surface based on the value |
|
382 |
* of the type parameter. If the surface creation fails for any reason, |
|
383 |
* an OutOfMemoryError will be thrown. |
|
384 |
*/ |
|
385 |
protected void initSurface() { |
|
386 |
// any time we create or restore the surface, recreate the raster |
|
387 |
synchronized (this) { |
|
388 |
wrn = null; |
|
389 |
} |
|
390 |
// REMIND: somewhere a puppy died |
|
391 |
class Status { |
|
392 |
boolean success = false; |
|
393 |
}; |
|
394 |
final Status status = new Status(); |
|
395 |
D3DRenderQueue rq = D3DRenderQueue.getInstance(); |
|
396 |
rq.lock(); |
|
397 |
try { |
|
398 |
rq.flushAndInvokeNow(new Runnable() { |
|
399 |
public void run() { |
|
400 |
status.success = initSurfaceNow(); |
|
2 | 401 |
} |
887 | 402 |
}); |
403 |
if (!status.success) { |
|
404 |
throw new InvalidPipeException("Error creating D3DSurface"); |
|
405 |
} |
|
406 |
} finally { |
|
407 |
rq.unlock(); |
|
408 |
} |
|
409 |
} |
|
410 |
||
411 |
/** |
|
412 |
* Returns the D3DContext for the GraphicsConfig associated with this |
|
413 |
* surface. |
|
414 |
*/ |
|
415 |
public final D3DContext getContext() { |
|
416 |
return graphicsDevice.getContext(); |
|
417 |
} |
|
418 |
||
419 |
/** |
|
420 |
* Returns one of the surface type constants defined above. |
|
421 |
*/ |
|
422 |
public final int getType() { |
|
423 |
return type; |
|
424 |
} |
|
425 |
||
426 |
private static native int dbGetPixelNative(long pData, int x, int y); |
|
427 |
private static native void dbSetPixelNative(long pData, int x, int y, |
|
428 |
int pixel); |
|
429 |
static class D3DDataBufferNative extends DataBufferNative { |
|
430 |
int pixel; |
|
431 |
protected D3DDataBufferNative(SurfaceData sData, |
|
432 |
int type, int w, int h) |
|
433 |
{ |
|
434 |
super(sData, type, w, h); |
|
435 |
} |
|
436 |
||
437 |
protected int getElem(final int x, final int y, |
|
438 |
final SurfaceData sData) |
|
439 |
{ |
|
440 |
int retPixel; |
|
441 |
D3DRenderQueue rq = D3DRenderQueue.getInstance(); |
|
442 |
rq.lock(); |
|
443 |
try { |
|
444 |
rq.flushAndInvokeNow(new Runnable() { |
|
445 |
public void run() { |
|
446 |
pixel = dbGetPixelNative(sData.getNativeOps(), x, y); |
|
2 | 447 |
} |
887 | 448 |
}); |
449 |
} finally { |
|
450 |
retPixel = pixel; |
|
451 |
rq.unlock(); |
|
452 |
} |
|
453 |
return retPixel; |
|
454 |
} |
|
455 |
||
456 |
protected void setElem(final int x, final int y, final int pixel, |
|
457 |
final SurfaceData sData) |
|
458 |
{ |
|
459 |
D3DRenderQueue rq = D3DRenderQueue.getInstance(); |
|
460 |
rq.lock(); |
|
461 |
try { |
|
462 |
rq.flushAndInvokeNow(new Runnable() { |
|
463 |
public void run() { |
|
464 |
dbSetPixelNative(sData.getNativeOps(), x, y, pixel); |
|
2 | 465 |
} |
887 | 466 |
}); |
467 |
sData.markDirty(); |
|
468 |
} finally { |
|
469 |
rq.unlock(); |
|
2 | 470 |
} |
471 |
} |
|
472 |
} |
|
473 |
||
887 | 474 |
public synchronized Raster getRaster(int x, int y, int w, int h) { |
475 |
if (wrn == null) { |
|
476 |
DirectColorModel dcm = (DirectColorModel)getColorModel(); |
|
477 |
SampleModel smHw; |
|
478 |
int dataType = 0; |
|
479 |
int scanStride = width; |
|
2 | 480 |
|
887 | 481 |
if (dcm.getPixelSize() == 24 || dcm.getPixelSize() == 32) { |
482 |
dataType = DataBuffer.TYPE_INT; |
|
483 |
} else { |
|
484 |
// 15, 16 |
|
485 |
dataType = DataBuffer.TYPE_USHORT; |
|
2 | 486 |
} |
887 | 487 |
|
488 |
// note that we have to use the surface width and height here, |
|
489 |
// not the passed w,h |
|
490 |
smHw = new SinglePixelPackedSampleModel(dataType, width, height, |
|
491 |
scanStride, dcm.getMasks()); |
|
492 |
DataBuffer dbn = new D3DDataBufferNative(this, dataType, |
|
493 |
width, height); |
|
494 |
wrn = WritableRasterNative.createNativeRaster(smHw, dbn); |
|
495 |
} |
|
496 |
||
497 |
return wrn; |
|
498 |
} |
|
499 |
||
500 |
/** |
|
501 |
* For now, we can only render LCD text if: |
|
502 |
* - the pixel shaders are available, and |
|
503 |
* - blending is disabled, and |
|
504 |
* - the source color is opaque |
|
1717
5d7c1102107a
6728834: D3D/OGL: LCD AA text becomes bold and blurred when rendering to a non-opaque destination
tdv
parents:
887
diff
changeset
|
505 |
* - and the destination is opaque |
887 | 506 |
*/ |
507 |
public boolean canRenderLCDText(SunGraphics2D sg2d) { |
|
508 |
return |
|
509 |
graphicsDevice.isCapPresent(CAPS_LCD_SHADER) && |
|
510 |
sg2d.compositeState <= SunGraphics2D.COMP_ISCOPY && |
|
1717
5d7c1102107a
6728834: D3D/OGL: LCD AA text becomes bold and blurred when rendering to a non-opaque destination
tdv
parents:
887
diff
changeset
|
511 |
sg2d.paintState <= SunGraphics2D.PAINT_OPAQUECOLOR && |
5d7c1102107a
6728834: D3D/OGL: LCD AA text becomes bold and blurred when rendering to a non-opaque destination
tdv
parents:
887
diff
changeset
|
512 |
sg2d.surfaceData.getTransparency() == Transparency.OPAQUE; |
887 | 513 |
} |
514 |
||
515 |
public void validatePipe(SunGraphics2D sg2d) { |
|
516 |
TextPipe textpipe; |
|
517 |
boolean validated = false; |
|
518 |
||
519 |
// REMIND: the D3D pipeline doesn't support XOR!, more |
|
520 |
// fixes will be needed below |
|
521 |
if (sg2d.compositeState >= sg2d.COMP_XOR) { |
|
522 |
super.validatePipe(sg2d); |
|
523 |
sg2d.imagepipe = d3dImagePipe; |
|
524 |
return; |
|
525 |
} |
|
526 |
||
527 |
// D3DTextRenderer handles both AA and non-AA text, but |
|
528 |
// only works with the following modes: |
|
529 |
// (Note: For LCD text we only enter this code path if |
|
530 |
// canRenderLCDText() has already validated that the mode is |
|
531 |
// CompositeType.SrcNoEa (opaque color), which will be subsumed |
|
532 |
// by the CompositeType.SrcNoEa (any color) test below.) |
|
533 |
||
534 |
if (/* CompositeType.SrcNoEa (any color) */ |
|
535 |
(sg2d.compositeState <= sg2d.COMP_ISCOPY && |
|
536 |
sg2d.paintState <= sg2d.PAINT_ALPHACOLOR) || |
|
537 |
||
538 |
/* CompositeType.SrcOver (any color) */ |
|
539 |
(sg2d.compositeState == sg2d.COMP_ALPHA && |
|
540 |
sg2d.paintState <= sg2d.PAINT_ALPHACOLOR && |
|
541 |
(((AlphaComposite)sg2d.composite).getRule() == |
|
542 |
AlphaComposite.SRC_OVER)) || |
|
543 |
||
544 |
/* CompositeType.Xor (any color) */ |
|
545 |
(sg2d.compositeState == sg2d.COMP_XOR && |
|
546 |
sg2d.paintState <= sg2d.PAINT_ALPHACOLOR)) |
|
547 |
{ |
|
548 |
textpipe = d3dTextPipe; |
|
549 |
} else { |
|
550 |
// do this to initialize textpipe correctly; we will attempt |
|
551 |
// to override the non-text pipes below |
|
552 |
super.validatePipe(sg2d); |
|
553 |
textpipe = sg2d.textpipe; |
|
554 |
validated = true; |
|
555 |
} |
|
556 |
||
557 |
PixelToParallelogramConverter txPipe = null; |
|
558 |
D3DRenderer nonTxPipe = null; |
|
559 |
||
560 |
if (sg2d.antialiasHint != SunHints.INTVAL_ANTIALIAS_ON) { |
|
561 |
if (sg2d.paintState <= sg2d.PAINT_ALPHACOLOR) { |
|
562 |
if (sg2d.compositeState <= sg2d.COMP_XOR) { |
|
563 |
txPipe = d3dTxRenderPipe; |
|
564 |
nonTxPipe = d3dRenderPipe; |
|
565 |
} |
|
566 |
} else if (sg2d.compositeState <= sg2d.COMP_ALPHA) { |
|
567 |
if (D3DPaints.isValid(sg2d)) { |
|
568 |
txPipe = d3dTxRenderPipe; |
|
569 |
nonTxPipe = d3dRenderPipe; |
|
570 |
} |
|
571 |
// custom paints handled by super.validatePipe() below |
|
2 | 572 |
} |
887 | 573 |
} else { |
574 |
if (sg2d.paintState <= sg2d.PAINT_ALPHACOLOR) { |
|
575 |
if (graphicsDevice.isCapPresent(CAPS_AA_SHADER) && |
|
576 |
(sg2d.imageComp == CompositeType.SrcOverNoEa || |
|
577 |
sg2d.imageComp == CompositeType.SrcOver)) |
|
578 |
{ |
|
579 |
if (!validated) { |
|
580 |
super.validatePipe(sg2d); |
|
581 |
validated = true; |
|
582 |
} |
|
583 |
PixelToParallelogramConverter aaConverter = |
|
584 |
new PixelToParallelogramConverter(sg2d.shapepipe, |
|
585 |
d3dAAPgramPipe, |
|
586 |
1.0/8.0, 0.499, |
|
587 |
false); |
|
588 |
sg2d.drawpipe = aaConverter; |
|
589 |
sg2d.fillpipe = aaConverter; |
|
590 |
sg2d.shapepipe = aaConverter; |
|
591 |
} else if (sg2d.compositeState == sg2d.COMP_XOR) { |
|
592 |
// install the solid pipes when AA and XOR are both enabled |
|
593 |
txPipe = d3dTxRenderPipe; |
|
594 |
nonTxPipe = d3dRenderPipe; |
|
595 |
} |
|
596 |
} |
|
597 |
// other cases handled by super.validatePipe() below |
|
598 |
} |
|
599 |
||
600 |
if (txPipe != null) { |
|
601 |
if (sg2d.transformState >= sg2d.TRANSFORM_TRANSLATESCALE) { |
|
602 |
sg2d.drawpipe = txPipe; |
|
603 |
sg2d.fillpipe = txPipe; |
|
604 |
} else if (sg2d.strokeState != sg2d.STROKE_THIN) { |
|
605 |
sg2d.drawpipe = txPipe; |
|
606 |
sg2d.fillpipe = nonTxPipe; |
|
2 | 607 |
} else { |
887 | 608 |
sg2d.drawpipe = nonTxPipe; |
609 |
sg2d.fillpipe = nonTxPipe; |
|
610 |
} |
|
611 |
// Note that we use the transforming pipe here because it |
|
612 |
// will examine the shape and possibly perform an optimized |
|
613 |
// operation if it can be simplified. The simplifications |
|
614 |
// will be valid for all STROKE and TRANSFORM types. |
|
615 |
sg2d.shapepipe = txPipe; |
|
616 |
} else { |
|
617 |
if (!validated) { |
|
618 |
super.validatePipe(sg2d); |
|
2 | 619 |
} |
620 |
} |
|
887 | 621 |
|
622 |
// install the text pipe based on our earlier decision |
|
623 |
sg2d.textpipe = textpipe; |
|
624 |
||
625 |
// always override the image pipe with the specialized D3D pipe |
|
626 |
sg2d.imagepipe = d3dImagePipe; |
|
627 |
} |
|
628 |
||
629 |
@Override |
|
630 |
protected MaskFill getMaskFill(SunGraphics2D sg2d) { |
|
631 |
if (sg2d.paintState > sg2d.PAINT_ALPHACOLOR) { |
|
632 |
/* |
|
633 |
* We can only accelerate non-Color MaskFill operations if |
|
634 |
* all of the following conditions hold true: |
|
635 |
* - there is an implementation for the given paintState |
|
636 |
* - the current Paint can be accelerated for this destination |
|
637 |
* - multitexturing is available (since we need to modulate |
|
638 |
* the alpha mask texture with the paint texture) |
|
639 |
* |
|
640 |
* In all other cases, we return null, in which case the |
|
641 |
* validation code will choose a more general software-based loop. |
|
642 |
*/ |
|
643 |
if (!D3DPaints.isValid(sg2d) || |
|
644 |
!graphicsDevice.isCapPresent(CAPS_MULTITEXTURE)) |
|
645 |
{ |
|
646 |
return null; |
|
647 |
} |
|
648 |
} |
|
649 |
return super.getMaskFill(sg2d); |
|
650 |
} |
|
651 |
||
652 |
@Override |
|
653 |
public boolean copyArea(SunGraphics2D sg2d, |
|
654 |
int x, int y, int w, int h, int dx, int dy) |
|
655 |
{ |
|
656 |
if (sg2d.transformState < sg2d.TRANSFORM_TRANSLATESCALE && |
|
657 |
sg2d.compositeState < sg2d.COMP_XOR) |
|
658 |
{ |
|
659 |
x += sg2d.transX; |
|
660 |
y += sg2d.transY; |
|
661 |
||
662 |
d3dRenderPipe.copyArea(sg2d, x, y, w, h, dx, dy); |
|
663 |
||
664 |
return true; |
|
665 |
} |
|
666 |
return false; |
|
2 | 667 |
} |
668 |
||
669 |
@Override |
|
887 | 670 |
public void flush() { |
671 |
D3DRenderQueue rq = D3DRenderQueue.getInstance(); |
|
672 |
rq.lock(); |
|
673 |
try { |
|
674 |
RenderBuffer buf = rq.getBuffer(); |
|
675 |
rq.ensureCapacityAndAlignment(12, 4); |
|
676 |
buf.putInt(FLUSH_SURFACE); |
|
677 |
buf.putLong(getNativeOps()); |
|
678 |
||
679 |
// this call is expected to complete synchronously, so flush now |
|
680 |
rq.flushNow(); |
|
681 |
} finally { |
|
682 |
rq.unlock(); |
|
683 |
} |
|
684 |
} |
|
685 |
||
686 |
/** |
|
687 |
* Disposes the native resources associated with the given D3DSurfaceData |
|
688 |
* (referenced by the pData parameter). This method is invoked from |
|
689 |
* the native Dispose() method from the Disposer thread when the |
|
690 |
* Java-level D3DSurfaceData object is about to go away. |
|
691 |
*/ |
|
692 |
static void dispose(long pData) { |
|
693 |
D3DRenderQueue rq = D3DRenderQueue.getInstance(); |
|
694 |
rq.lock(); |
|
695 |
try { |
|
696 |
RenderBuffer buf = rq.getBuffer(); |
|
697 |
rq.ensureCapacityAndAlignment(12, 4); |
|
698 |
buf.putInt(DISPOSE_SURFACE); |
|
699 |
buf.putLong(pData); |
|
700 |
||
701 |
// this call is expected to complete synchronously, so flush now |
|
702 |
rq.flushNow(); |
|
703 |
} finally { |
|
704 |
rq.unlock(); |
|
705 |
} |
|
706 |
} |
|
2 | 707 |
|
887 | 708 |
static void swapBuffers(D3DSurfaceData sd, |
709 |
final int x1, final int y1, |
|
710 |
final int x2, final int y2) |
|
711 |
{ |
|
712 |
long pData = sd.getNativeOps(); |
|
713 |
D3DRenderQueue rq = D3DRenderQueue.getInstance(); |
|
714 |
// swapBuffers can be called from the toolkit thread by swing, we |
|
715 |
// should detect this and prevent the deadlocks |
|
716 |
if (rq.isRenderQueueThread()) { |
|
717 |
if (!rq.tryLock()) { |
|
718 |
// if we could not obtain the lock, repaint the area |
|
719 |
// that was supposed to be swapped, and no-op this swap |
|
720 |
final Component target = (Component)sd.getPeer().getTarget(); |
|
721 |
SunToolkit.executeOnEventHandlerThread(target, new Runnable() { |
|
722 |
public void run() { |
|
723 |
target.repaint(x1, y1, x2, y2); |
|
724 |
} |
|
725 |
}); |
|
726 |
return; |
|
2 | 727 |
} |
887 | 728 |
} else { |
729 |
rq.lock(); |
|
730 |
} |
|
731 |
try { |
|
732 |
RenderBuffer buf = rq.getBuffer(); |
|
733 |
rq.ensureCapacityAndAlignment(28, 4); |
|
734 |
buf.putInt(SWAP_BUFFERS); |
|
735 |
buf.putLong(pData); |
|
736 |
buf.putInt(x1); |
|
737 |
buf.putInt(y1); |
|
738 |
buf.putInt(x2); |
|
739 |
buf.putInt(y2); |
|
740 |
rq.flushNow(); |
|
741 |
} finally { |
|
742 |
rq.unlock(); |
|
743 |
} |
|
744 |
} |
|
745 |
||
746 |
/** |
|
747 |
* Returns destination Image associated with this SurfaceData. |
|
748 |
*/ |
|
749 |
public Object getDestination() { |
|
750 |
return offscreenImage; |
|
751 |
} |
|
2 | 752 |
|
887 | 753 |
public Rectangle getBounds() { |
754 |
if (type == FLIP_BACKBUFFER || type == WINDOW) { |
|
755 |
Rectangle r = peer.getBounds(); |
|
756 |
r.x = r.y = 0; |
|
757 |
return r; |
|
2 | 758 |
} else { |
887 | 759 |
return new Rectangle(width, height); |
2 | 760 |
} |
887 | 761 |
} |
762 |
||
763 |
public Rectangle getNativeBounds() { |
|
764 |
D3DRenderQueue rq = D3DRenderQueue.getInstance(); |
|
765 |
// need to lock to make sure nativeWidth and Height are consistent |
|
766 |
// since they are set from the render thread from the native |
|
767 |
// level |
|
768 |
rq.lock(); |
|
769 |
try { |
|
770 |
// REMIND: use xyoffsets? |
|
771 |
return new Rectangle(nativeWidth, nativeHeight); |
|
772 |
} finally { |
|
773 |
rq.unlock(); |
|
774 |
} |
|
775 |
} |
|
776 |
||
2 | 777 |
|
887 | 778 |
public GraphicsConfiguration getDeviceConfiguration() { |
779 |
return graphicsDevice.getDefaultConfiguration(); |
|
780 |
} |
|
781 |
||
782 |
public SurfaceData getReplacement() { |
|
783 |
return restoreContents(offscreenImage); |
|
784 |
} |
|
785 |
||
786 |
private static D3DGraphicsConfig getGC(WComponentPeer peer) { |
|
787 |
GraphicsConfiguration gc; |
|
788 |
if (peer != null) { |
|
789 |
gc = peer.getGraphicsConfiguration(); |
|
790 |
} else { |
|
791 |
GraphicsEnvironment env = |
|
792 |
GraphicsEnvironment.getLocalGraphicsEnvironment(); |
|
793 |
GraphicsDevice gd = env.getDefaultScreenDevice(); |
|
794 |
gc = gd.getDefaultConfiguration(); |
|
795 |
} |
|
796 |
return (gc instanceof D3DGraphicsConfig) ? (D3DGraphicsConfig)gc : null; |
|
2 | 797 |
} |
798 |
||
799 |
/** |
|
887 | 800 |
* Attempts to restore the surface by initializing the native data |
2 | 801 |
*/ |
887 | 802 |
void restoreSurface() { |
803 |
initSurface(); |
|
804 |
} |
|
805 |
||
806 |
WComponentPeer getPeer() { |
|
807 |
return peer; |
|
808 |
} |
|
809 |
||
810 |
/** |
|
811 |
* We need to let the surface manager know that the surface is lost so |
|
812 |
* that for example BufferStrategy.contentsLost() returns correct result. |
|
813 |
* Normally the status of contentsLost is set in validate(), but in some |
|
814 |
* cases (like Swing's buffer per window) we intentionally don't call |
|
815 |
* validate from the toolkit thread but only check for the BS status. |
|
816 |
*/ |
|
817 |
@Override |
|
818 |
public void setSurfaceLost(boolean lost) { |
|
819 |
super.setSurfaceLost(lost); |
|
820 |
if (lost && offscreenImage != null) { |
|
821 |
SurfaceManager sm = SurfaceManager.getManager(offscreenImage); |
|
822 |
sm.acceleratedSurfaceLost(); |
|
2 | 823 |
} |
824 |
} |
|
825 |
||
887 | 826 |
private static native long getNativeResourceNative(long sdops, int resType); |
827 |
/** |
|
828 |
* Returns a pointer to the native resource of specified {@code resType} |
|
829 |
* associated with this surface. |
|
830 |
* |
|
831 |
* Specifically, for {@code D3DSurfaceData} this method returns pointers of |
|
832 |
* the following: |
|
833 |
* <pre> |
|
834 |
* TEXTURE - (IDirect3DTexture9*) |
|
835 |
* RT_TEXTURE, RT_PLAIN - (IDirect3DSurface9*) |
|
836 |
* FLIP_BACKBUFFER - (IDirect3DSwapChain9*) |
|
837 |
* D3D_DEVICE_RESOURCE - (IDirect3DDevice9*) |
|
838 |
* </pre> |
|
839 |
* |
|
840 |
* Multiple resources may be available for some types (i.e. for render to |
|
841 |
* texture one could retrieve both a destination surface by specifying |
|
842 |
* RT_TEXTURE, and a texture by using TEXTURE). |
|
843 |
* |
|
844 |
* Note: the pointer returned by this method is only valid on the rendering |
|
845 |
* thread. |
|
846 |
* |
|
847 |
* @return pointer to the native resource of specified type or 0L if |
|
848 |
* such resource doesn't exist or can not be retrieved. |
|
849 |
* @see sun.java2d.pipe.hw.AccelSurface#getNativeResource |
|
850 |
*/ |
|
851 |
public long getNativeResource(int resType) { |
|
852 |
return getNativeResourceNative(getNativeOps(), resType); |
|
2 | 853 |
} |
854 |
||
887 | 855 |
/** |
856 |
* Class representing an on-screen d3d surface. Since d3d can't |
|
857 |
* render to the screen directly, it is implemented as a swap chain, |
|
858 |
* controlled by D3DScreenUpdateManager. |
|
859 |
* |
|
860 |
* @see D3DScreenUpdateManager |
|
861 |
*/ |
|
862 |
public static class D3DWindowSurfaceData extends D3DSurfaceData { |
|
863 |
StateTracker dirtyTracker; |
|
864 |
||
865 |
public D3DWindowSurfaceData(WComponentPeer peer, |
|
866 |
D3DGraphicsConfig gc) |
|
867 |
{ |
|
868 |
super(peer, gc, |
|
869 |
peer.getBounds().width, peer.getBounds().height, |
|
870 |
null, peer.getColorModel(), 1, SWAP_COPY, VSYNC_DEFAULT, |
|
871 |
WINDOW); |
|
872 |
dirtyTracker = getStateTracker(); |
|
873 |
} |
|
874 |
||
875 |
/** |
|
876 |
* {@inheritDoc} |
|
877 |
* |
|
878 |
* Overridden to use ScreenUpdateManager to obtain the replacement |
|
879 |
* surface. |
|
880 |
* |
|
881 |
* @see sun.java2d.ScreenUpdateManager#getReplacementScreenSurface |
|
882 |
*/ |
|
883 |
@Override |
|
884 |
public SurfaceData getReplacement() { |
|
885 |
ScreenUpdateManager mgr = ScreenUpdateManager.getInstance(); |
|
886 |
return mgr.getReplacementScreenSurface(peer, this); |
|
887 |
} |
|
888 |
||
889 |
/** |
|
890 |
* Returns destination Component associated with this SurfaceData. |
|
891 |
*/ |
|
892 |
@Override |
|
893 |
public Object getDestination() { |
|
894 |
return peer.getTarget(); |
|
895 |
} |
|
896 |
||
897 |
@Override |
|
898 |
void restoreSurface() { |
|
899 |
Window fsw = graphicsDevice.getFullScreenWindow(); |
|
900 |
if (fsw != null && fsw != peer.getTarget()) { |
|
901 |
throw new InvalidPipeException("Can't restore onscreen surface"+ |
|
902 |
" when in full-screen mode"); |
|
903 |
} |
|
904 |
super.restoreSurface(); |
|
905 |
// if initialization was unsuccessful, an IPE will be thrown |
|
906 |
// and the surface will remain lost |
|
907 |
setSurfaceLost(false); |
|
908 |
||
909 |
// This is to make sure the render target is reset after this |
|
910 |
// surface is restored. The reason for this is that sometimes this |
|
911 |
// surface can be restored from multiple threads (the screen update |
|
912 |
// manager's thread and app's rendering thread) at the same time, |
|
913 |
// and when that happens the second restoration will create the |
|
914 |
// native resource which will not be set as render target because |
|
915 |
// the BufferedContext's validate method will think that since the |
|
916 |
// surface data object didn't change then the current render target |
|
917 |
// is correct and no rendering will appear on the screen. |
|
918 |
D3DRenderQueue rq = D3DRenderQueue.getInstance(); |
|
919 |
rq.lock(); |
|
920 |
try { |
|
921 |
getContext().invalidateContext(); |
|
922 |
} finally { |
|
923 |
rq.unlock(); |
|
924 |
} |
|
925 |
} |
|
926 |
||
927 |
public boolean isDirty() { |
|
928 |
return !dirtyTracker.isCurrent(); |
|
929 |
} |
|
930 |
||
931 |
public void markClean() { |
|
932 |
dirtyTracker = getStateTracker(); |
|
933 |
} |
|
2 | 934 |
} |
887 | 935 |
|
936 |
/** |
|
937 |
* Updates the layered window with the contents of the surface. |
|
938 |
* |
|
939 |
* @param pd3dsd pointer to the D3DSDOps structure |
|
940 |
* @param pData pointer to the AwtWindow peer data |
|
941 |
* @param w width of the window |
|
942 |
* @param h height of the window |
|
943 |
* @see sun.awt.windows.TranslucentWindowPainter |
|
944 |
*/ |
|
945 |
public static native boolean updateWindowAccelImpl(long pd3dsd, long pData, |
|
946 |
int w, int h); |
|
2 | 947 |
} |