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