2
|
1 |
/*
|
|
2 |
* Copyright 2005-2006 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 |
#include "sun_java2d_d3d_D3DContext.h"
|
|
27 |
#include "jlong.h"
|
|
28 |
#include "jni_util.h"
|
|
29 |
#include "Trace.h"
|
|
30 |
|
|
31 |
#include "ddrawUtils.h"
|
|
32 |
#include "awt_Win32GraphicsDevice.h"
|
|
33 |
#include "sun_java2d_SunGraphics2D.h"
|
|
34 |
|
|
35 |
#include "GraphicsPrimitiveMgr.h"
|
|
36 |
|
|
37 |
#include "RegistryKey.h"
|
|
38 |
#include "WindowsFlags.h"
|
|
39 |
|
|
40 |
#include "Win32SurfaceData.h"
|
|
41 |
#include "D3DSurfaceData.h"
|
|
42 |
#include "D3DUtils.h"
|
|
43 |
#include "D3DContext.h"
|
|
44 |
#include "D3DRuntimeTest.h"
|
|
45 |
|
|
46 |
#include "IntDcm.h"
|
|
47 |
#include "IntArgb.h"
|
|
48 |
#include "Region.h"
|
|
49 |
|
|
50 |
typedef struct {
|
|
51 |
D3DBLEND src;
|
|
52 |
D3DBLEND dst;
|
|
53 |
} D3DBlendRule;
|
|
54 |
|
|
55 |
/**
|
|
56 |
* This table contains the standard blending rules (or Porter-Duff compositing
|
|
57 |
* factors) used in SetRenderState(), indexed by the rule constants from the
|
|
58 |
* AlphaComposite class.
|
|
59 |
*/
|
|
60 |
D3DBlendRule StdBlendRules[] = {
|
|
61 |
{ D3DBLEND_ZERO, D3DBLEND_ZERO }, /* 0 - Nothing */
|
|
62 |
{ D3DBLEND_ZERO, D3DBLEND_ZERO }, /* 1 - RULE_Clear */
|
|
63 |
{ D3DBLEND_ONE, D3DBLEND_ZERO }, /* 2 - RULE_Src */
|
|
64 |
{ D3DBLEND_ONE, D3DBLEND_INVSRCALPHA }, /* 3 - RULE_SrcOver */
|
|
65 |
{ D3DBLEND_INVDESTALPHA, D3DBLEND_ONE }, /* 4 - RULE_DstOver */
|
|
66 |
{ D3DBLEND_DESTALPHA, D3DBLEND_ZERO }, /* 5 - RULE_SrcIn */
|
|
67 |
{ D3DBLEND_ZERO, D3DBLEND_SRCALPHA }, /* 6 - RULE_DstIn */
|
|
68 |
{ D3DBLEND_INVDESTALPHA, D3DBLEND_ZERO }, /* 7 - RULE_SrcOut */
|
|
69 |
{ D3DBLEND_ZERO, D3DBLEND_INVSRCALPHA }, /* 8 - RULE_DstOut */
|
|
70 |
{ D3DBLEND_ZERO, D3DBLEND_ONE }, /* 9 - RULE_Dst */
|
|
71 |
{ D3DBLEND_DESTALPHA, D3DBLEND_INVSRCALPHA }, /*10 - RULE_SrcAtop */
|
|
72 |
{ D3DBLEND_INVDESTALPHA, D3DBLEND_SRCALPHA }, /*11 - RULE_DstAtop */
|
|
73 |
{ D3DBLEND_INVDESTALPHA, D3DBLEND_INVSRCALPHA }, /*12 - RULE_AlphaXor*/
|
|
74 |
};
|
|
75 |
|
|
76 |
/**
|
|
77 |
* D3DContext
|
|
78 |
*/
|
|
79 |
D3DContext* D3DContext::CreateD3DContext(DDraw *ddObject, DXObject* dxObject)
|
|
80 |
{
|
|
81 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::CreateD3DContext");
|
|
82 |
// create and test the d3d context
|
|
83 |
D3DContext *d3dContext = new D3DContext(ddObject, dxObject);
|
|
84 |
// if there was a failure while creating or testing the device,
|
|
85 |
// dispose of it and return NULL
|
|
86 |
if (!(d3dContext->GetDeviceCaps() & J2D_D3D_ENABLED_OK)) {
|
|
87 |
delete d3dContext;
|
|
88 |
d3dContext = NULL;
|
|
89 |
}
|
|
90 |
return d3dContext;
|
|
91 |
}
|
|
92 |
|
|
93 |
D3DContext::D3DContext(DDraw *ddObject, DXObject* dxObject)
|
|
94 |
{
|
|
95 |
GetExclusiveAccess();
|
|
96 |
J2dRlsTraceLn(J2D_TRACE_INFO, "D3DContext::D3DContext");
|
|
97 |
J2dTraceLn2(J2D_TRACE_VERBOSE, " ddObject=0x%d dxObject=0x%x",
|
|
98 |
ddObject, dxObject);
|
|
99 |
d3dDevice = NULL;
|
|
100 |
d3dObject = NULL;
|
|
101 |
ddTargetSurface = NULL;
|
|
102 |
lpMaskTexture = NULL;
|
|
103 |
lpGlyphCacheTexture = NULL;
|
|
104 |
glyphCache = NULL;
|
|
105 |
glyphCacheAvailable = TRUE;
|
|
106 |
deviceCaps = J2D_D3D_FAILURE;
|
|
107 |
bBeginScenePending = FALSE;
|
|
108 |
jD3DContext = NULL;
|
|
109 |
|
|
110 |
this->dxObject = dxObject;
|
|
111 |
this->ddObject = ddObject;
|
|
112 |
|
|
113 |
if (SUCCEEDED(dxObject->CreateD3DObject(&d3dObject))) {
|
|
114 |
|
|
115 |
// The device type we choose to use doesn't change over time
|
|
116 |
pDeviceGUID = D3DUtils_SelectDeviceGUID(d3dObject);
|
|
117 |
if (pDeviceGUID) {
|
|
118 |
bIsHWRasterizer = (*pDeviceGUID == IID_IDirect3DHALDevice ||
|
|
119 |
*pDeviceGUID == IID_IDirect3DTnLHalDevice);
|
|
120 |
CreateD3DDevice();
|
|
121 |
} else {
|
|
122 |
J2dRlsTraceLn(J2D_TRACE_ERROR,
|
|
123 |
"D3CCoD3DContext::D3DContext: Can't find "\
|
|
124 |
"suitable D3D device");
|
|
125 |
}
|
|
126 |
} else {
|
|
127 |
J2dRlsTraceLn(J2D_TRACE_ERROR,
|
|
128 |
"D3DContext::D3DContext: Can't "\
|
|
129 |
"create IDirect3D7 interface");
|
|
130 |
}
|
|
131 |
|
|
132 |
compState = sun_java2d_SunGraphics2D_COMP_ISCOPY;
|
|
133 |
extraAlpha = 1.0f;
|
|
134 |
colorPixel = 0xffffffff;
|
|
135 |
|
|
136 |
ReleaseExclusiveAccess();
|
|
137 |
}
|
|
138 |
|
|
139 |
void D3DContext::SetJavaContext(JNIEnv *env, jobject newD3Dc) {
|
|
140 |
GetExclusiveAccess();
|
|
141 |
|
|
142 |
// Only bother if the new D3DContext object is different
|
|
143 |
// from the one we already have reference to.
|
|
144 |
if (env->IsSameObject(newD3Dc, jD3DContext) == FALSE) {
|
|
145 |
J2dTraceLn(J2D_TRACE_VERBOSE, "D3DContext:SetJavaContext: "\
|
|
146 |
"setting new java context object");
|
|
147 |
// invalidate the old context, since we've got a new one
|
|
148 |
InvalidateIfTarget(env, ddTargetSurface);
|
|
149 |
|
|
150 |
if (jD3DContext != NULL) {
|
|
151 |
env->DeleteWeakGlobalRef(jD3DContext);
|
|
152 |
}
|
|
153 |
// set the new java-level context object
|
|
154 |
jD3DContext = env->NewWeakGlobalRef(newD3Dc);
|
|
155 |
}
|
|
156 |
ReleaseExclusiveAccess();
|
|
157 |
}
|
|
158 |
|
|
159 |
void D3DContext::Release3DDevice() {
|
|
160 |
GetExclusiveAccess();
|
|
161 |
J2dTraceLn1(J2D_TRACE_INFO,
|
|
162 |
"D3DContext::Release3DDevice: d3dDevice = 0x%x",
|
|
163 |
d3dDevice);
|
|
164 |
|
|
165 |
// make sure we do EndScene if one is pending
|
|
166 |
FlushD3DQueueForTarget(ddTargetSurface);
|
|
167 |
|
|
168 |
// Let the java-level object know that the context
|
|
169 |
// state is no longer valid, forcing it to be reinitialized
|
|
170 |
// later.
|
|
171 |
JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
|
|
172 |
InvalidateIfTarget(env, ddTargetSurface);
|
|
173 |
|
|
174 |
// We don't need to release it since we didn't create it
|
|
175 |
ddTargetSurface = NULL;
|
|
176 |
|
|
177 |
// disable the use of this context until we ensure the capabilities
|
|
178 |
// of the new device and run the tests
|
|
179 |
deviceCaps = J2D_D3D_FAILURE;
|
|
180 |
|
|
181 |
if (lpMaskTexture != NULL) {
|
|
182 |
lpMaskTexture->Release();
|
|
183 |
delete lpMaskTexture;
|
|
184 |
lpMaskTexture = NULL;
|
|
185 |
}
|
|
186 |
|
|
187 |
// reset the depth buffer format
|
|
188 |
memset(&depthBufferFormat, 0, sizeof(depthBufferFormat));
|
|
189 |
|
|
190 |
if (d3dDevice) {
|
|
191 |
// setting the texture increases its reference number, so
|
|
192 |
// we should reset the textures for all stages to make sure
|
|
193 |
// they're released
|
|
194 |
for (int stage = 0; stage <= MAX_USED_TEXTURE_STAGE; stage++) {
|
|
195 |
d3dDevice->SetTexture(stage, NULL);
|
|
196 |
lastTexture[stage] = NULL;
|
|
197 |
}
|
|
198 |
d3dDevice->Release();
|
|
199 |
d3dDevice = NULL;
|
|
200 |
}
|
|
201 |
ReleaseExclusiveAccess();
|
|
202 |
}
|
|
203 |
|
|
204 |
D3DContext::~D3DContext() {
|
|
205 |
J2dTraceLn2(J2D_TRACE_INFO,
|
|
206 |
"~D3DContext: d3dDevice=0x%x, d3dObject =0x%x",
|
|
207 |
d3dDevice, d3dObject);
|
|
208 |
GetExclusiveAccess();
|
|
209 |
if (lpGlyphCacheTexture != NULL) {
|
|
210 |
lpGlyphCacheTexture->Release();
|
|
211 |
delete lpGlyphCacheTexture;
|
|
212 |
lpGlyphCacheTexture = NULL;
|
|
213 |
}
|
|
214 |
Release3DDevice();
|
|
215 |
if (d3dObject != NULL) {
|
|
216 |
d3dObject->Release();
|
|
217 |
d3dObject = NULL;
|
|
218 |
}
|
|
219 |
ReleaseExclusiveAccess();
|
|
220 |
}
|
|
221 |
|
|
222 |
HRESULT
|
|
223 |
D3DContext::InitD3DDevice(IDirect3DDevice7 *d3dDevice)
|
|
224 |
{
|
|
225 |
HRESULT res = D3D_OK;
|
|
226 |
J2dRlsTraceLn1(J2D_TRACE_INFO,
|
|
227 |
"D3DContext::InitD3DDevice: d3dDevice=Ox%x", d3dDevice);
|
|
228 |
|
|
229 |
d3dDevice->GetCaps(&d3dDevDesc);
|
|
230 |
// disable some of the unneeded and costly d3d functionality
|
|
231 |
d3dDevice->SetRenderState(D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
|
|
232 |
d3dDevice->SetRenderState(D3DRENDERSTATE_TEXTUREPERSPECTIVE, FALSE);
|
|
233 |
d3dDevice->SetRenderState(D3DRENDERSTATE_SPECULARENABLE, FALSE);
|
|
234 |
d3dDevice->SetRenderState(D3DRENDERSTATE_LIGHTING, FALSE);
|
|
235 |
d3dDevice->SetRenderState(D3DRENDERSTATE_CLIPPING, FALSE);
|
|
236 |
d3dDevice->SetRenderState(D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
|
|
237 |
d3dDevice->SetRenderState(D3DRENDERSTATE_COLORVERTEX, FALSE);
|
|
238 |
d3dDevice->SetRenderState(D3DRENDERSTATE_STENCILENABLE, FALSE);
|
|
239 |
|
|
240 |
d3dDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTFG_POINT);
|
|
241 |
d3dDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTFG_POINT);
|
|
242 |
|
|
243 |
// these states never change
|
|
244 |
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
|
|
245 |
d3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
|
|
246 |
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
|
|
247 |
d3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
|
|
248 |
// init the array of latest textures
|
|
249 |
memset(&lastTexture, 0, sizeof(lastTexture));
|
|
250 |
// this will force the state initialization on first UpdateState
|
|
251 |
opState = STATE_UNDEFINED;
|
|
252 |
|
|
253 |
D3DMATRIX tx;
|
|
254 |
D3DUtils_SetIdentityMatrix(&tx);
|
|
255 |
d3dDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &tx);
|
|
256 |
|
|
257 |
bBeginScenePending = FALSE;
|
|
258 |
|
|
259 |
D3DUtils_SetupTextureFormats(d3dDevice, textureTable);
|
|
260 |
|
|
261 |
// REMIND: debugging: allows testing the argb path in
|
|
262 |
// UploadImageToTexture on devices with alpha texture support
|
|
263 |
if ((getenv("J2D_D3D_NOALPHATEXTURE") != NULL) ||
|
|
264 |
FAILED(res = D3DUtils_FindMaskTileTextureFormat(d3dDevice,
|
|
265 |
&maskTileTexFormat)))
|
|
266 |
{
|
|
267 |
// use ARGB if can't find alpha texture (or in case argb
|
|
268 |
// was specifically requested)
|
|
269 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
270 |
"D3DContext::InitD3DDevice: "\
|
|
271 |
"Using IntARBG instead of Alpha texture");
|
|
272 |
if (textureTable[TR_TRANSLUCENT_IDX][DEPTH32_IDX].pfType != PF_INVALID)
|
|
273 |
{
|
|
274 |
memcpy(&maskTileTexFormat,
|
|
275 |
&textureTable[TR_TRANSLUCENT_IDX][DEPTH32_IDX].pddpf,
|
|
276 |
sizeof(maskTileTexFormat));
|
|
277 |
res = D3D_OK;
|
|
278 |
}
|
|
279 |
} else {
|
|
280 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
281 |
"D3DContext::InitD3DDevice: Found Alpha-texture format");
|
|
282 |
}
|
|
283 |
return res;
|
|
284 |
}
|
|
285 |
|
|
286 |
HRESULT
|
|
287 |
D3DContext::CreateAndTestD3DDevice(DxCapabilities *dxCaps)
|
|
288 |
{
|
|
289 |
J2dRlsTraceLn(J2D_TRACE_INFO, "D3DContext::CreateAndTestD3DDevice");
|
|
290 |
HRESULT res;
|
|
291 |
if (pDeviceGUID == NULL) {
|
|
292 |
J2dRlsTraceLn(J2D_TRACE_ERROR,
|
|
293 |
"D3DContext::CreateAndTestD3DDevice: "\
|
|
294 |
"No usable d3d device");
|
|
295 |
deviceCaps = J2D_D3D_FAILURE;
|
|
296 |
return DDERR_GENERIC;
|
|
297 |
}
|
|
298 |
|
|
299 |
Release3DDevice();
|
|
300 |
|
|
301 |
// Create a temp surface so we can use it when creating a device
|
|
302 |
DXSurface *target = NULL;
|
|
303 |
if (FAILED(res = CreateSurface(NULL, 10, 10, 32, TR_OPAQUE,
|
|
304 |
D3D_PLAIN_SURFACE|D3D_RENDER_TARGET,
|
|
305 |
&target, NULL)))
|
|
306 |
{
|
|
307 |
DebugPrintDirectDrawError(res,
|
|
308 |
"D3DContext::CreateAndTestD3DDevice: "\
|
|
309 |
"can't create scratch surface");
|
|
310 |
return res;
|
|
311 |
}
|
|
312 |
|
|
313 |
if (FAILED(res = d3dObject->CreateDevice(*pDeviceGUID,
|
|
314 |
target->GetDDSurface(),
|
|
315 |
&d3dDevice)))
|
|
316 |
{
|
|
317 |
DebugPrintDirectDrawError(res,
|
|
318 |
"D3DContext::CreateAndTestD3DDevice: "\
|
|
319 |
"error creating d3d device");
|
|
320 |
} else if (FAILED(res = InitD3DDevice(d3dDevice))) {
|
|
321 |
DebugPrintDirectDrawError(res,
|
|
322 |
"D3DContext::CreateAndTestD3DDevice: "\
|
|
323 |
"error initializing D3D device");
|
|
324 |
} else {
|
|
325 |
J2dRlsTraceLn(J2D_TRACE_VERBOSE,
|
|
326 |
"D3DContext::CreateAndTestD3DDevice: "\
|
|
327 |
"D3D device creation/initialization successful");
|
|
328 |
// the device is successfully created and initialized,
|
|
329 |
// now run some tests on it
|
|
330 |
deviceCaps = TestD3DDevice(ddObject, this, dxCaps);
|
|
331 |
}
|
|
332 |
|
|
333 |
// We can safely dispose the scratch surface here
|
|
334 |
if (target != NULL) {
|
|
335 |
target->Release();
|
|
336 |
delete target;
|
|
337 |
}
|
|
338 |
|
|
339 |
return res;
|
|
340 |
}
|
|
341 |
|
|
342 |
void
|
|
343 |
D3DContext::CreateD3DDevice()
|
|
344 |
{
|
|
345 |
GetExclusiveAccess();
|
|
346 |
J2dRlsTraceLn(J2D_TRACE_INFO, "D3DContext::CreateD3DDevice");
|
|
347 |
// this is a weird way of getting a handle on the ddInstance
|
|
348 |
HMONITOR hMonitor = dxObject->GetHMonitor();
|
|
349 |
DxCapabilities *dxCaps =
|
|
350 |
AwtWin32GraphicsDevice::GetDxCapsForDevice(hMonitor);
|
|
351 |
|
|
352 |
int d3dCapsValidity = dxCaps->GetD3dCapsValidity();
|
|
353 |
// Always run the test unless we crashed doing so the last time.
|
|
354 |
// The reasons:
|
|
355 |
// - the user may have disabled d3d acceleration in the display panel
|
|
356 |
// since the last run
|
|
357 |
// - the user may have installed the new drivers, which may cause BSODs
|
|
358 |
// - if the test had failed previously because of quality issues, the
|
|
359 |
// new driver may have fixed the problem, but we'd never know since we
|
|
360 |
// never try again
|
|
361 |
// - user (or developer, rather) may have specified a
|
|
362 |
// different rasterizer via env. variable
|
|
363 |
if (d3dCapsValidity != J2D_ACCEL_TESTING) {
|
|
364 |
dxCaps->SetD3dCapsValidity(J2D_ACCEL_TESTING);
|
|
365 |
|
|
366 |
// this will create the device, test it and set the
|
|
367 |
// deviceCaps
|
|
368 |
CreateAndTestD3DDevice(dxCaps);
|
|
369 |
|
|
370 |
dxCaps->SetD3dDeviceCaps(deviceCaps);
|
|
371 |
dxCaps->SetD3dCapsValidity(J2D_ACCEL_SUCCESS);
|
|
372 |
}
|
|
373 |
int requiredResults = forceD3DUsage ?
|
|
374 |
J2D_D3D_REQUIRED_RESULTS : J2D_D3D_DESIRED_RESULTS;
|
|
375 |
|
|
376 |
#ifdef DEBUG
|
|
377 |
J2dTraceLn(J2D_TRACE_VERBOSE, "CreateD3DDevice: requested caps:");
|
|
378 |
PrintD3DCaps(requiredResults);
|
|
379 |
J2dTraceLn(J2D_TRACE_VERBOSE, " caps supported by the device:");
|
|
380 |
PrintD3DCaps(deviceCaps);
|
|
381 |
J2dTraceLn(J2D_TRACE_VERBOSE, " missing caps:");
|
|
382 |
PrintD3DCaps(requiredResults & ~deviceCaps);
|
|
383 |
#endif // DEBUG
|
|
384 |
|
|
385 |
if ((deviceCaps & requiredResults) != requiredResults) {
|
|
386 |
if (!(deviceCaps & J2D_D3D_HW_OK)) {
|
|
387 |
// disable d3d for all devices, because we've encountered
|
|
388 |
// known bad hardware. See comment in TestForBadHardware().
|
|
389 |
J2dRlsTraceLn(J2D_TRACE_ERROR,
|
|
390 |
"CreateD3DDevice: bad hardware found,"\
|
|
391 |
" disabling d3d for all devices.");
|
|
392 |
SetD3DEnabledFlag(NULL, FALSE, FALSE);
|
|
393 |
} else {
|
|
394 |
J2dRlsTraceLn(J2D_TRACE_ERROR,
|
|
395 |
"CreateD3DDevice: tests FAILED, d3d disabled.");
|
|
396 |
}
|
|
397 |
// REMIND: the first time the context initialization fails,
|
|
398 |
// deviceUseD3D is set to FALSE in DDrawObjectStruct, and because of
|
|
399 |
// this we never attempt to initialize it again later.
|
|
400 |
// For example, if the app switches to a display mode where
|
|
401 |
// d3d is not supported, we disable d3d, but it stays disabled
|
|
402 |
// even when the display mode is switched back to a supported one.
|
|
403 |
// May be we should disable it only in case of a hard error.
|
|
404 |
ddObject->DisableD3D();
|
|
405 |
Release3DDevice();
|
|
406 |
} else {
|
|
407 |
deviceCaps |= J2D_D3D_ENABLED_OK;
|
|
408 |
J2dRlsTraceLn1(J2D_TRACE_INFO,
|
|
409 |
"CreateD3DDevice: tests PASSED, "\
|
|
410 |
"d3d enabled (forced: %s).",
|
|
411 |
forceD3DUsage ? "yes" : "no");
|
|
412 |
}
|
|
413 |
|
|
414 |
ReleaseExclusiveAccess();
|
|
415 |
}
|
|
416 |
|
|
417 |
HRESULT
|
|
418 |
D3DContext::SetRenderTarget(DDrawSurface *ddSurface)
|
|
419 |
{
|
|
420 |
static D3DVIEWPORT7 vp = { 0, 0, 0, 0, 0.0f, 1.0f };
|
|
421 |
static D3DMATRIX tx;
|
|
422 |
BOOL bSetProjectionMatrix = FALSE;
|
|
423 |
HRESULT res = DDERR_GENERIC;
|
|
424 |
GetExclusiveAccess();
|
|
425 |
|
|
426 |
J2dTraceLn2(J2D_TRACE_INFO,
|
|
427 |
"D3DContext::SetRenderTarget: old=0x%x new=0x%x",
|
|
428 |
ddTargetSurface, ddSurface);
|
|
429 |
|
|
430 |
ddTargetSurface = NULL;
|
|
431 |
|
|
432 |
DXSurface *dxSurface = NULL;
|
|
433 |
if (d3dDevice == NULL || ddSurface == NULL ||
|
|
434 |
(dxSurface = ddSurface->GetDXSurface()) == NULL)
|
|
435 |
{
|
|
436 |
ReleaseExclusiveAccess();
|
|
437 |
J2dTraceLn3(J2D_TRACE_WARNING,
|
|
438 |
"D3DContext::SetRenderTarget invalid state:"\
|
|
439 |
"d3dDevice=0x%x ddSurface=0x%x dxSurface=0x%x",
|
|
440 |
d3dDevice, ddSurface, dxSurface);
|
|
441 |
return res;
|
|
442 |
}
|
|
443 |
|
|
444 |
if (FAILED(res = ddSurface->IsLost())) {
|
|
445 |
ReleaseExclusiveAccess();
|
|
446 |
DebugPrintDirectDrawError(res, "D3DContext::SetRenderTarget: "\
|
|
447 |
"target surface (and/or depth buffer) lost");
|
|
448 |
return res;
|
|
449 |
}
|
|
450 |
|
|
451 |
ForceEndScene();
|
|
452 |
|
|
453 |
if (FAILED(res = d3dDevice->SetRenderTarget(dxSurface->GetDDSurface(), 0)))
|
|
454 |
{
|
|
455 |
ReleaseExclusiveAccess();
|
|
456 |
DebugPrintDirectDrawError(res, "D3DContext::SetRenderTarget: "\
|
|
457 |
"error setting render target");
|
|
458 |
return res;
|
|
459 |
}
|
|
460 |
|
|
461 |
int width = dxSurface->GetWidth();
|
|
462 |
int height = dxSurface->GetHeight();
|
|
463 |
// set the projection matrix if the the dimensions of the new
|
|
464 |
// rendertarget are different from the old one.
|
|
465 |
if (FAILED(d3dDevice->GetViewport(&vp)) ||
|
|
466 |
(int)vp.dwWidth != width || (int)vp.dwHeight != height)
|
|
467 |
{
|
|
468 |
bSetProjectionMatrix = TRUE;
|
|
469 |
}
|
|
470 |
|
|
471 |
vp.dwX = vp.dwY = 0;
|
|
472 |
vp.dwWidth = width;
|
|
473 |
vp.dwHeight = height;
|
|
474 |
vp.dvMinZ = 0.0f;
|
|
475 |
vp.dvMaxZ = 1.0f;
|
|
476 |
|
|
477 |
if (FAILED(res = d3dDevice->SetViewport(&vp))) {
|
|
478 |
DebugPrintDirectDrawError(res, "D3DContext::SetRenderTarget: "\
|
|
479 |
"error setting viewport");
|
|
480 |
ReleaseExclusiveAccess();
|
|
481 |
return res;
|
|
482 |
}
|
|
483 |
|
|
484 |
if (bSetProjectionMatrix) {
|
|
485 |
D3DUtils_SetOrthoMatrixOffCenterLH(&tx, (float)width, (float)height);
|
|
486 |
res = d3dDevice->SetTransform(D3DTRANSFORMSTATE_PROJECTION, &tx);
|
|
487 |
}
|
|
488 |
|
|
489 |
if (SUCCEEDED(res)) {
|
|
490 |
ddTargetSurface = ddSurface;
|
|
491 |
J2dTraceLn1(J2D_TRACE_VERBOSE,
|
|
492 |
"D3DContext::SetRenderTarget: succeeded, "\
|
|
493 |
"new target=0x%x", ddTargetSurface);
|
|
494 |
} else {
|
|
495 |
DebugPrintDirectDrawError(res, "D3DContext::SetRenderTarget: failed");
|
|
496 |
}
|
|
497 |
|
|
498 |
ReleaseExclusiveAccess();
|
|
499 |
return res;
|
|
500 |
}
|
|
501 |
|
|
502 |
HRESULT
|
|
503 |
D3DContext::SetTransform(jobject xform,
|
|
504 |
jdouble m00, jdouble m10,
|
|
505 |
jdouble m01, jdouble m11,
|
|
506 |
jdouble m02, jdouble m12)
|
|
507 |
{
|
|
508 |
GetExclusiveAccess();
|
|
509 |
|
|
510 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::SetTransform");
|
|
511 |
if (d3dDevice == NULL) {
|
|
512 |
ReleaseExclusiveAccess();
|
|
513 |
return DDERR_GENERIC;
|
|
514 |
}
|
|
515 |
HRESULT res = D3D_OK;
|
|
516 |
D3DMATRIX tx;
|
|
517 |
|
|
518 |
if (xform == NULL) {
|
|
519 |
J2dTraceLn(J2D_TRACE_VERBOSE, " disabling transform");
|
|
520 |
D3DUtils_SetIdentityMatrix(&tx);
|
|
521 |
} else {
|
|
522 |
J2dTraceLn(J2D_TRACE_VERBOSE, " enabling transform");
|
|
523 |
|
|
524 |
// copy values from AffineTransform object into native matrix array
|
|
525 |
memset(&tx, 0, sizeof(D3DMATRIX));
|
|
526 |
tx._11 = (float)m00;
|
|
527 |
tx._12 = (float)m10;
|
|
528 |
tx._21 = (float)m01;
|
|
529 |
tx._22 = (float)m11;
|
|
530 |
// The -0.5 adjustment is needed to correctly align texels to
|
|
531 |
// pixels with orgthogonal projection matrix.
|
|
532 |
// Note that we readjust vertex coordinates for cases
|
|
533 |
// when we don't do texture mapping or use D3DPT_LINESTRIP.
|
|
534 |
tx._41 = (float)m02-0.5f;
|
|
535 |
tx._42 = (float)m12-0.5f;
|
|
536 |
|
|
537 |
tx._33 = 1.0f;
|
|
538 |
tx._44 = 1.0f;
|
|
539 |
}
|
|
540 |
|
|
541 |
J2dTraceLn(J2D_TRACE_VERBOSE, " setting new tx matrix");
|
|
542 |
J2dTraceLn4(J2D_TRACE_VERBOSE,
|
|
543 |
" %5f %5f %5f %5f", tx._11, tx._12, tx._13, tx._14);
|
|
544 |
J2dTraceLn4(J2D_TRACE_VERBOSE,
|
|
545 |
" %5f %5f %5f %5f", tx._21, tx._22, tx._23, tx._24);
|
|
546 |
J2dTraceLn4(J2D_TRACE_VERBOSE,
|
|
547 |
" %5f %5f %5f %5f", tx._31, tx._32, tx._33, tx._34);
|
|
548 |
J2dTraceLn4(J2D_TRACE_VERBOSE,
|
|
549 |
" %5f %5f %5f %5f", tx._41, tx._42, tx._43, tx._44);
|
|
550 |
if (FAILED(res = d3dDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &tx))) {
|
|
551 |
DebugPrintDirectDrawError(res, "D3DContext::SetTransform failed");
|
|
552 |
}
|
|
553 |
|
|
554 |
ReleaseExclusiveAccess();
|
|
555 |
return res;
|
|
556 |
}
|
|
557 |
|
|
558 |
/**
|
|
559 |
* This method assumes that ::SetRenderTarget has already
|
|
560 |
* been called. SetRenderTarget creates and attaches a
|
|
561 |
* depth buffer to the target surface prior to setting it
|
|
562 |
* as target surface to the device.
|
|
563 |
*/
|
|
564 |
HRESULT
|
|
565 |
D3DContext::SetClip(JNIEnv *env, jobject clip,
|
|
566 |
jboolean isRect,
|
|
567 |
int x1, int y1,
|
|
568 |
int x2, int y2)
|
|
569 |
{
|
|
570 |
HRESULT res;
|
|
571 |
static J2D_XY_VERTEX clipRect[] = {
|
|
572 |
#ifdef USE_SINGLE_VERTEX_FORMAT
|
|
573 |
{ 0.0f, 0.0f, 1.0f, 0xffffffff, 0.0f, 0.0f },
|
|
574 |
{ 0.0f, 0.0f, 1.0f, 0xffffffff, 0.0f, 0.0f },
|
|
575 |
{ 0.0f, 0.0f, 1.0f, 0xffffffff, 0.0f, 0.0f },
|
|
576 |
{ 0.0f, 0.0f, 1.0f, 0xffffffff, 0.0f, 0.0f }
|
|
577 |
#else
|
|
578 |
// Note that we use D3DFVF_XYZ vertex format
|
|
579 |
// implies 0xffffffff diffuse color, so we don't
|
|
580 |
// have to specify it.
|
|
581 |
{ 0.0f, 0.0f, 1.0f },
|
|
582 |
{ 0.0f, 0.0f, 1.0f },
|
|
583 |
{ 0.0f, 0.0f, 1.0f },
|
|
584 |
{ 0.0f, 0.0f, 1.0f },
|
|
585 |
#endif // USE_SINGLE_VERTEX_FORMAT
|
|
586 |
};
|
|
587 |
static J2DXY_HEXA spanVx[MAX_CACHED_SPAN_VX_NUM];
|
|
588 |
|
|
589 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::SetClip");
|
|
590 |
J2dTraceLn5(J2D_TRACE_VERBOSE,
|
|
591 |
" x1=%-4d y1=%-4d x2=%-4d y2=%-4d isRect=%-2d",
|
|
592 |
x1, y1, x2, y2, isRect);
|
|
593 |
GetExclusiveAccess();
|
|
594 |
// the target surface must already be set
|
|
595 |
if (d3dDevice == NULL || ddTargetSurface == NULL) {
|
|
596 |
ReleaseExclusiveAccess();
|
|
597 |
return DDERR_GENERIC;
|
|
598 |
}
|
|
599 |
|
|
600 |
|
|
601 |
// Must do EndScene prior to setting a new clip, otherwise the
|
|
602 |
// primitives which are already in the pipeline will be rendered with
|
|
603 |
// the new clip when we do EndScene.
|
|
604 |
ForceEndScene();
|
|
605 |
|
|
606 |
if (clip == NULL) {
|
|
607 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
608 |
"D3DContext::SetClip: disabling clip (== NULL)");
|
|
609 |
res = d3dDevice->SetRenderState(D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
|
|
610 |
ReleaseExclusiveAccess();
|
|
611 |
return res;
|
|
612 |
} else if (isRect) {
|
|
613 |
// optimization: disable depth buffer if the clip is equal to
|
|
614 |
// the size of the viewport
|
|
615 |
int w = ddTargetSurface->GetDXSurface()->GetWidth();
|
|
616 |
int h = ddTargetSurface->GetDXSurface()->GetHeight();
|
|
617 |
if (x1 == 0 && y1 == 0 && x2 == w && y2 == h) {
|
|
618 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
619 |
"D3DContext::SetClip: disabling clip (== viewport)");
|
|
620 |
res = d3dDevice->SetRenderState(D3DRENDERSTATE_ZENABLE, D3DZB_FALSE);
|
|
621 |
ReleaseExclusiveAccess();
|
|
622 |
return res;
|
|
623 |
}
|
|
624 |
}
|
|
625 |
|
|
626 |
// save the old settings
|
|
627 |
DWORD dwAlphaSt, dwSrcBlendSt, dwDestBlendSt;
|
|
628 |
d3dDevice->GetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, &dwAlphaSt);
|
|
629 |
d3dDevice->GetRenderState(D3DRENDERSTATE_SRCBLEND, &dwSrcBlendSt);
|
|
630 |
d3dDevice->GetRenderState(D3DRENDERSTATE_DESTBLEND, &dwDestBlendSt);
|
|
631 |
|
|
632 |
d3dDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
|
|
633 |
d3dDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, D3DBLEND_ZERO);
|
|
634 |
d3dDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, D3DBLEND_ONE);
|
|
635 |
|
|
636 |
// disable texturing
|
|
637 |
if (lastTexture[0] != NULL) {
|
|
638 |
// note that we do not restore the texture after we set the clip,
|
|
639 |
// it will be reset the next time a texturing operation is performed
|
|
640 |
SetTexture(NULL);
|
|
641 |
}
|
|
642 |
|
|
643 |
D3DMATRIX tx, idTx;
|
|
644 |
d3dDevice->GetTransform(D3DTRANSFORMSTATE_WORLD, &tx);
|
|
645 |
D3DUtils_SetIdentityMatrix(&idTx);
|
|
646 |
d3dDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &idTx);
|
|
647 |
|
|
648 |
// The depth buffer is first cleared with zeroes, which is the farthest
|
|
649 |
// plane from the viewer (our projection matrix is an inversed orthogonal
|
|
650 |
// transform).
|
|
651 |
// To set the clip we'll render the clip spans with Z coordinates of 1.0f
|
|
652 |
// (the closest to the viewer). Since all rendering primitives
|
|
653 |
// have their vertices' Z coordinate set to 0.0, they will effectively be
|
|
654 |
// clipped because the Z depth test for them will fail (vertex with 1.0
|
|
655 |
// depth is closer than the one with 0.0f)
|
|
656 |
d3dDevice->SetRenderState(D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
|
|
657 |
d3dDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, TRUE);
|
|
658 |
d3dDevice->SetRenderState(D3DRENDERSTATE_ZFUNC, D3DCMP_ALWAYS);
|
|
659 |
d3dDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, 0L, 0.0f, 0x0L);
|
|
660 |
|
|
661 |
float fx1, fy1, fx2, fy2;
|
|
662 |
if (SUCCEEDED(d3dDevice->BeginScene())) {
|
|
663 |
if (isRect) {
|
|
664 |
fx1 = (float)x1; fy1 = (float)y1;
|
|
665 |
fx2 = (float)x2; fy2 = (float)y2;
|
|
666 |
D3DU_INIT_VERTEX_QUAD_XY(clipRect, fx1, fy1, fx2, fy2);
|
|
667 |
res = d3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, D3DFVF_XY_VERTEX,
|
|
668 |
clipRect, 4, NULL);
|
|
669 |
} else {
|
|
670 |
RegionData clipInfo;
|
|
671 |
Region_GetInfo(env, clip, &clipInfo);
|
|
672 |
SurfaceDataBounds span;
|
|
673 |
J2DXY_HEXA *pHexa = (J2DXY_HEXA*)spanVx;
|
|
674 |
jint numOfCachedSpans = 0;
|
|
675 |
|
|
676 |
Region_StartIteration(env, &clipInfo);
|
|
677 |
while (Region_NextIteration(&clipInfo, &span)) {
|
|
678 |
fx1 = (float)(span.x1); fy1 = (float)(span.y1);
|
|
679 |
fx2 = (float)(span.x2); fy2 = (float)(span.y2);
|
|
680 |
D3DU_INIT_VERTEX_XYZ_6(*pHexa, fx1, fy1, fx2, fy2, 1.0f);
|
|
681 |
numOfCachedSpans++;
|
|
682 |
pHexa = (J2DXY_HEXA*)PtrAddBytes(pHexa, sizeof(J2DXY_HEXA));
|
|
683 |
if (numOfCachedSpans >= MAX_CACHED_SPAN_VX_NUM) {
|
|
684 |
res = d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,
|
|
685 |
D3DFVF_XY_VERTEX,
|
|
686 |
(void*)spanVx,
|
|
687 |
6*numOfCachedSpans, NULL);
|
|
688 |
numOfCachedSpans = 0;
|
|
689 |
pHexa = (J2DXY_HEXA*)spanVx;
|
|
690 |
if (FAILED(res)) {
|
|
691 |
break;
|
|
692 |
}
|
|
693 |
}
|
|
694 |
}
|
|
695 |
if (numOfCachedSpans > 0) {
|
|
696 |
res = d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,
|
|
697 |
D3DFVF_XY_VERTEX,
|
|
698 |
(void*)spanVx,
|
|
699 |
6*numOfCachedSpans, NULL);
|
|
700 |
}
|
|
701 |
Region_EndIteration(env, &clipInfo);
|
|
702 |
}
|
|
703 |
res = d3dDevice->EndScene();
|
|
704 |
}
|
|
705 |
|
|
706 |
// reset the transform
|
|
707 |
d3dDevice->SetTransform(D3DTRANSFORMSTATE_WORLD, &tx);
|
|
708 |
|
|
709 |
// reset the alpha compositing
|
|
710 |
d3dDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, dwAlphaSt);
|
|
711 |
d3dDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND, dwSrcBlendSt);
|
|
712 |
d3dDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND, dwDestBlendSt);
|
|
713 |
|
|
714 |
// Setup the depth buffer.
|
|
715 |
// We disable further updates to the depth buffer: it should only
|
|
716 |
// be updated in SetClip method.
|
|
717 |
d3dDevice->SetRenderState(D3DRENDERSTATE_ZWRITEENABLE, FALSE);
|
|
718 |
d3dDevice->SetRenderState(D3DRENDERSTATE_ZFUNC, D3DCMP_LESS);
|
|
719 |
|
|
720 |
ReleaseExclusiveAccess();
|
|
721 |
return res;
|
|
722 |
}
|
|
723 |
|
|
724 |
DXSurface *
|
|
725 |
D3DContext::GetMaskTexture()
|
|
726 |
{
|
|
727 |
if (lpMaskTexture != NULL) {
|
|
728 |
// This in theory should never happen since
|
|
729 |
// we're using managed textures, but in case
|
|
730 |
// we switch to using something else.
|
|
731 |
if (FAILED(lpMaskTexture->IsLost())) {
|
|
732 |
lpMaskTexture->Restore();
|
|
733 |
}
|
|
734 |
return lpMaskTexture;
|
|
735 |
}
|
|
736 |
InitMaskTileTexture();
|
|
737 |
return lpMaskTexture;
|
|
738 |
}
|
|
739 |
|
|
740 |
|
|
741 |
HRESULT
|
|
742 |
D3DContext::InitMaskTileTexture()
|
|
743 |
{
|
|
744 |
HRESULT res;
|
|
745 |
|
|
746 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::InitMaskTileTexture");
|
|
747 |
if (lpMaskTexture != NULL) {
|
|
748 |
lpMaskTexture->Release();
|
|
749 |
}
|
|
750 |
lpMaskTexture = NULL;
|
|
751 |
|
|
752 |
DWORD caps2 = 0, caps = DDSCAPS_TEXTURE;
|
|
753 |
if (bIsHWRasterizer) {
|
|
754 |
caps2 = DDSCAPS2_TEXTUREMANAGE;
|
|
755 |
} else {
|
|
756 |
caps |= DDSCAPS_SYSTEMMEMORY;
|
|
757 |
}
|
|
758 |
|
|
759 |
if (FAILED(res =
|
|
760 |
dxObject->CreateSurface(DDSD_WIDTH|DDSD_HEIGHT|DDSD_CAPS|
|
|
761 |
DDSD_PIXELFORMAT|DDSD_TEXTURESTAGE,
|
|
762 |
caps,
|
|
763 |
caps2,
|
|
764 |
&maskTileTexFormat,
|
|
765 |
D3DSD_MASK_TILE_SIZE, D3DSD_MASK_TILE_SIZE,
|
|
766 |
(DXSurface **)&lpMaskTexture, 0)))
|
|
767 |
{
|
|
768 |
// in case we want to do something here later..
|
|
769 |
DebugPrintDirectDrawError(res,
|
|
770 |
"D3DContext::InitMaskTileTexture: "\
|
|
771 |
"failed to create mask tile texture");
|
|
772 |
}
|
|
773 |
return res;
|
|
774 |
}
|
|
775 |
|
|
776 |
HRESULT
|
|
777 |
D3DContext::UploadImageToTexture(DXSurface *texture, jubyte *pixels,
|
|
778 |
jint dstx, jint dsty,
|
|
779 |
jint srcx, jint srcy,
|
|
780 |
jint srcWidth, jint srcHeight,
|
|
781 |
jint srcStride)
|
|
782 |
{
|
|
783 |
HRESULT res = D3D_OK;
|
|
784 |
SurfaceDataRasInfo rasInfo;
|
|
785 |
|
|
786 |
|
|
787 |
RECT r = { dstx, dsty, dstx+srcWidth, dsty+srcHeight };
|
|
788 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::UploadImageToTexture");
|
|
789 |
J2dTraceLn4(J2D_TRACE_VERBOSE,
|
|
790 |
" rect={%-4d, %-4d, %-4d, %-4d}",
|
|
791 |
r.left, r.top, r.right, r.bottom);
|
|
792 |
// REMIND: it may be faster to lock for NULL instead of
|
|
793 |
// rect, need to test later.
|
|
794 |
if (FAILED(res = texture->Lock(&r, &rasInfo,
|
|
795 |
DDLOCK_WAIT|DDLOCK_NOSYSLOCK, NULL)))
|
|
796 |
{
|
|
797 |
DebugPrintDirectDrawError(res,
|
|
798 |
"D3DContext::UploadImageToTexture: could "\
|
|
799 |
"not lock texture");
|
|
800 |
return res;
|
|
801 |
}
|
|
802 |
|
|
803 |
if (rasInfo.pixelStride == 1) {
|
|
804 |
// 8bpp alpha texture
|
|
805 |
void *pSrcPixels = PtrCoord(pixels, srcx, 1, srcy, srcStride);
|
|
806 |
void *pDstPixels = rasInfo.rasBase;
|
|
807 |
do {
|
|
808 |
memcpy(pDstPixels, pSrcPixels, srcWidth);
|
|
809 |
pSrcPixels = PtrAddBytes(pSrcPixels, srcStride);
|
|
810 |
pDstPixels = PtrAddBytes(pDstPixels, rasInfo.scanStride);
|
|
811 |
} while (--srcHeight > 0);
|
|
812 |
} else {
|
|
813 |
// ARGB texture
|
|
814 |
jubyte *pSrcPixels = (jubyte*)PtrCoord(pixels, srcx, 1, srcy, srcStride);
|
|
815 |
jint *pDstPixels = (jint*)rasInfo.rasBase;
|
|
816 |
for (int yy = 0; yy < srcHeight; yy++) {
|
|
817 |
for (int xx = 0; xx < srcWidth; xx++) {
|
|
818 |
jubyte pix = pSrcPixels[xx];
|
|
819 |
StoreIntArgbFrom4ByteArgb(pDstPixels, 0, xx,
|
|
820 |
pix, pix, pix, pix);
|
|
821 |
}
|
|
822 |
pSrcPixels = (jubyte*)PtrAddBytes(pSrcPixels, srcStride);
|
|
823 |
pDstPixels = (jint*)PtrAddBytes(pDstPixels, rasInfo.scanStride);
|
|
824 |
}
|
|
825 |
}
|
|
826 |
return texture->Unlock(&r);
|
|
827 |
}
|
|
828 |
|
|
829 |
HRESULT
|
|
830 |
D3DContext::InitGlyphCache()
|
|
831 |
{
|
|
832 |
HRESULT res = D3D_OK;
|
|
833 |
|
|
834 |
if (glyphCache != NULL) {
|
|
835 |
return D3D_OK;
|
|
836 |
}
|
|
837 |
|
|
838 |
if (!glyphCacheAvailable) {
|
|
839 |
return DDERR_GENERIC;
|
|
840 |
}
|
|
841 |
|
|
842 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::InitGlyphCache");
|
|
843 |
|
|
844 |
// init glyph cache data structure
|
|
845 |
glyphCache = AccelGlyphCache_Init(D3D_GCACHE_WIDTH,
|
|
846 |
D3D_GCACHE_HEIGHT,
|
|
847 |
D3D_GCACHE_CELL_WIDTH,
|
|
848 |
D3D_GCACHE_CELL_HEIGHT,
|
|
849 |
NULL);
|
|
850 |
if (glyphCache == NULL) {
|
|
851 |
J2dRlsTraceLn(J2D_TRACE_ERROR,
|
|
852 |
"D3DContext::InitGlyphCache: "\
|
|
853 |
"could not init D3D glyph cache");
|
|
854 |
glyphCacheAvailable = FALSE;
|
|
855 |
return DDERR_GENERIC;
|
|
856 |
}
|
|
857 |
|
|
858 |
DWORD caps2 = 0, caps = DDSCAPS_TEXTURE;
|
|
859 |
if (bIsHWRasterizer) {
|
|
860 |
caps2 = DDSCAPS2_TEXTUREMANAGE;
|
|
861 |
} else {
|
|
862 |
caps |= DDSCAPS_SYSTEMMEMORY;
|
|
863 |
}
|
|
864 |
if (FAILED(res =
|
|
865 |
dxObject->CreateSurface(DDSD_WIDTH|DDSD_HEIGHT|DDSD_CAPS|
|
|
866 |
DDSD_PIXELFORMAT|DDSD_TEXTURESTAGE,
|
|
867 |
caps,
|
|
868 |
caps2,
|
|
869 |
&maskTileTexFormat,
|
|
870 |
D3D_GCACHE_WIDTH, D3D_GCACHE_HEIGHT,
|
|
871 |
(DXSurface **)&lpGlyphCacheTexture, 0)))
|
|
872 |
{
|
|
873 |
DebugPrintDirectDrawError(res,
|
|
874 |
"D3DContext::InitGlyphCache: glyph cache "\
|
|
875 |
"texture creation failed");
|
|
876 |
glyphCacheAvailable = FALSE;
|
|
877 |
return res;
|
|
878 |
}
|
|
879 |
return res;
|
|
880 |
}
|
|
881 |
|
|
882 |
HRESULT
|
|
883 |
D3DContext::GlyphCacheAdd(JNIEnv *env, GlyphInfo *glyph)
|
|
884 |
{
|
|
885 |
HRESULT res = D3D_OK;
|
|
886 |
if (!glyphCacheAvailable || glyph->image == NULL) {
|
|
887 |
return DDERR_GENERIC;
|
|
888 |
}
|
|
889 |
|
|
890 |
AccelGlyphCache_AddGlyph(glyphCache, glyph);
|
|
891 |
|
|
892 |
if (glyph->cellInfo != NULL) {
|
|
893 |
// store glyph image in texture cell
|
|
894 |
res = UploadImageToTexture(lpGlyphCacheTexture, (jubyte*)glyph->image,
|
|
895 |
glyph->cellInfo->x, glyph->cellInfo->y,
|
|
896 |
0, 0,
|
|
897 |
glyph->width, glyph->height,
|
|
898 |
glyph->width);
|
|
899 |
}
|
|
900 |
|
|
901 |
return res;
|
|
902 |
}
|
|
903 |
|
|
904 |
void
|
|
905 |
D3DContext::SetColor(jint eargb, jint flags)
|
|
906 |
{
|
|
907 |
J2dTraceLn2(J2D_TRACE_INFO,
|
|
908 |
"D3DContext::SetColor: eargb=%08x flags=%d", eargb, flags);
|
|
909 |
|
|
910 |
/*
|
|
911 |
* The colorPixel field is a 32-bit ARGB premultiplied color
|
|
912 |
* value. The incoming eargb field is a 32-bit ARGB value
|
|
913 |
* that is not premultiplied. If the alpha is not 1.0 (255)
|
|
914 |
* then we need to premultiply the color components before
|
|
915 |
* storing it in the colorPixel field.
|
|
916 |
*/
|
|
917 |
jint a = (eargb >> 24) & 0xff;
|
|
918 |
|
|
919 |
if (a == 0xff) {
|
|
920 |
colorPixel = eargb;
|
|
921 |
} else {
|
|
922 |
jint a2 = a + (a >> 7);
|
|
923 |
jint r = (((eargb >> 16) & 0xff) * a2) >> 8;
|
|
924 |
jint g = (((eargb >> 8) & 0xff) * a2) >> 8;
|
|
925 |
jint b = (((eargb ) & 0xff) * a2) >> 8;
|
|
926 |
colorPixel = (a << 24) | (r << 16) | (g << 8) | (b << 0);
|
|
927 |
}
|
|
928 |
J2dTraceLn1(J2D_TRACE_VERBOSE, " updated color: colorPixel=%08x",
|
|
929 |
colorPixel);
|
|
930 |
}
|
|
931 |
|
|
932 |
void
|
|
933 |
D3DContext::ResetComposite()
|
|
934 |
{
|
|
935 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::ResetComposite");
|
|
936 |
GetExclusiveAccess();
|
|
937 |
if (d3dDevice == NULL) {
|
|
938 |
ReleaseExclusiveAccess();
|
|
939 |
return;
|
|
940 |
}
|
|
941 |
d3dDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
|
|
942 |
compState = sun_java2d_SunGraphics2D_COMP_ISCOPY;
|
|
943 |
extraAlpha = 1.0f;
|
|
944 |
ReleaseExclusiveAccess();
|
|
945 |
}
|
|
946 |
|
|
947 |
void
|
|
948 |
D3DContext::SetAlphaComposite(jint rule, jfloat ea, jint flags)
|
|
949 |
{
|
|
950 |
J2dTraceLn3(J2D_TRACE_INFO,
|
|
951 |
"D3DContext::SetAlphaComposite: rule=%-1d ea=%f flags=%d",
|
|
952 |
rule, ea, flags);
|
|
953 |
GetExclusiveAccess();
|
|
954 |
|
|
955 |
if (d3dDevice == NULL) {
|
|
956 |
ReleaseExclusiveAccess();
|
|
957 |
return;
|
|
958 |
}
|
|
959 |
|
|
960 |
// we can safely disable blending when:
|
|
961 |
// - comp is SrcNoEa or SrcOverNoEa, and
|
|
962 |
// - the source is opaque
|
|
963 |
// (turning off blending can have a large positive impact on
|
|
964 |
// performance);
|
|
965 |
if ((rule == RULE_Src || rule == RULE_SrcOver) &&
|
|
966 |
(ea == 1.0f) &&
|
|
967 |
(flags & D3DC_SRC_IS_OPAQUE))
|
|
968 |
{
|
|
969 |
J2dTraceLn1(J2D_TRACE_VERBOSE,
|
|
970 |
" disabling alpha comp rule=%-1d ea=1.0 src=opq)", rule);
|
|
971 |
d3dDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, FALSE);
|
|
972 |
} else {
|
|
973 |
J2dTraceLn2(J2D_TRACE_VERBOSE,
|
|
974 |
" enabling alpha comp (rule=%-1d ea=%f)", rule, ea);
|
|
975 |
d3dDevice->SetRenderState(D3DRENDERSTATE_ALPHABLENDENABLE, TRUE);
|
|
976 |
|
|
977 |
d3dDevice->SetRenderState(D3DRENDERSTATE_SRCBLEND,
|
|
978 |
StdBlendRules[rule].src);
|
|
979 |
d3dDevice->SetRenderState(D3DRENDERSTATE_DESTBLEND,
|
|
980 |
StdBlendRules[rule].dst);
|
|
981 |
}
|
|
982 |
|
|
983 |
// update state
|
|
984 |
compState = sun_java2d_SunGraphics2D_COMP_ALPHA;
|
|
985 |
extraAlpha = ea;
|
|
986 |
|
|
987 |
if (extraAlpha == 1.0f) {
|
|
988 |
blitPolygonPixel = 0xffffffff;
|
|
989 |
} else {
|
|
990 |
// the 0xffffffff pixel needs to be premultiplied by extraAlpha
|
|
991 |
jint ea = (jint)(extraAlpha * 255.0f + 0.5f) & 0xff;
|
|
992 |
blitPolygonPixel = (ea << 24) | (ea << 16) | (ea << 8) | (ea << 0);
|
|
993 |
}
|
|
994 |
|
|
995 |
ReleaseExclusiveAccess();
|
|
996 |
}
|
|
997 |
|
|
998 |
HRESULT D3DContext::CreateSurface(JNIEnv *env, jint width, jint height,
|
|
999 |
jint depth, jint transparency,
|
|
1000 |
jint d3dSurfaceType,
|
|
1001 |
DXSurface **dxSurface, jint* pType)
|
|
1002 |
{
|
|
1003 |
DWORD dwFlags = 0, ddsCaps = 0, ddsCaps2 = 0;
|
|
1004 |
D3DTextureTableCell *cell = NULL;
|
|
1005 |
DXSurface *lpRetSurface = NULL;
|
|
1006 |
HRESULT res;
|
|
1007 |
|
|
1008 |
GetExclusiveAccess();
|
|
1009 |
|
|
1010 |
dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
|
|
1011 |
|
|
1012 |
if (d3dSurfaceType & D3D_TEXTURE_SURFACE) {
|
|
1013 |
ddsCaps |= DDSCAPS_TEXTURE;
|
|
1014 |
dwFlags |= DDSD_PIXELFORMAT | DDSD_TEXTURESTAGE;
|
|
1015 |
|
|
1016 |
jint trIdx = D3D_TR_IDX(transparency);
|
|
1017 |
jint depthIdx = D3D_DEPTH_IDX(depth);
|
|
1018 |
cell = &textureTable[trIdx][depthIdx];
|
|
1019 |
if (cell->pfType == PF_INVALID) {
|
|
1020 |
ReleaseExclusiveAccess();
|
|
1021 |
J2dTraceLn2(J2D_TRACE_ERROR,
|
|
1022 |
"D3DContext::CreateSurface: no texture "\
|
|
1023 |
"pixel format for depth: %d transparency=%d",
|
|
1024 |
depth, transparency);
|
|
1025 |
return DDERR_NOTFOUND;
|
|
1026 |
}
|
|
1027 |
if (pType != NULL) *pType = cell->pfType;
|
|
1028 |
if (d3dSurfaceType & D3D_RENDER_TARGET) {
|
|
1029 |
// RTT is requested => must be allocated non-managed and
|
|
1030 |
// non-systemmemory pool.
|
|
1031 |
// REMIND: must check if this is supported by
|
|
1032 |
// the device, as it may not have a local video memory, only AGP
|
|
1033 |
// may be we should just use VIDEOMEMORY
|
|
1034 |
// NOTE: this will likely fail if the device is not accelerated
|
|
1035 |
ddsCaps |= DDSCAPS_LOCALVIDMEM;
|
|
1036 |
} else {
|
|
1037 |
// This is a normal texture, allocate in managed pool if the device
|
|
1038 |
// is accelerated, otherwise must use system memory.
|
|
1039 |
if (bIsHWRasterizer) {
|
|
1040 |
ddsCaps2 |= DDSCAPS2_TEXTUREMANAGE;
|
|
1041 |
} else {
|
|
1042 |
ddsCaps |= DDSCAPS_SYSTEMMEMORY;
|
|
1043 |
}
|
|
1044 |
}
|
|
1045 |
|
|
1046 |
if (IsPow2TexturesOnly()) {
|
|
1047 |
jint w, h;
|
|
1048 |
for (w = 1; width > w; w <<= 1);
|
|
1049 |
for (h = 1; height > h; h <<= 1);
|
|
1050 |
width = w;
|
|
1051 |
height = h;
|
|
1052 |
}
|
|
1053 |
if (IsSquareTexturesOnly()) {
|
|
1054 |
if (width > height) {
|
|
1055 |
height = width;
|
|
1056 |
} else {
|
|
1057 |
width = height;
|
|
1058 |
}
|
|
1059 |
}
|
|
1060 |
|
|
1061 |
DWORD dwRatio = GetMaxTextureAspectRatio();
|
|
1062 |
// Note: Reference rasterizer returns ratio '0',
|
|
1063 |
// which presumably means 'any'.
|
|
1064 |
if ((DWORD)width > GetMaxTextureWidth() ||
|
|
1065 |
(DWORD)height > GetMaxTextureHeight() ||
|
|
1066 |
(DWORD)width < GetMinTextureWidth() ||
|
|
1067 |
(DWORD)height < GetMinTextureHeight() ||
|
|
1068 |
((dwRatio > 0) && ((DWORD)(width/height) > dwRatio ||
|
|
1069 |
(DWORD)(height/width) > dwRatio)))
|
|
1070 |
{
|
|
1071 |
ReleaseExclusiveAccess();
|
|
1072 |
J2dRlsTraceLn2(J2D_TRACE_ERROR,
|
|
1073 |
"D3DContext::CreateSurface: failed to create"\
|
|
1074 |
" texture: dimensions %dx%d not supported.",
|
|
1075 |
width, height);
|
|
1076 |
J2dRlsTraceLn5(J2D_TRACE_ERROR,
|
|
1077 |
" Supported texture dimensions: %dx%d-%dxd% "\
|
|
1078 |
" with max ratio %f.",
|
|
1079 |
GetMinTextureWidth(), GetMinTextureHeight(),
|
|
1080 |
GetMaxTextureWidth(), GetMaxTextureHeight(),
|
|
1081 |
GetMaxTextureAspectRatio());
|
|
1082 |
return D3DERR_TEXTURE_BADSIZE;
|
|
1083 |
}
|
|
1084 |
} else if (d3dSurfaceType & D3D_PLAIN_SURFACE) {
|
|
1085 |
ddsCaps |= DDSCAPS_OFFSCREENPLAIN |
|
|
1086 |
(bIsHWRasterizer ? DDSCAPS_VIDEOMEMORY : DDSCAPS_SYSTEMMEMORY);
|
|
1087 |
} else if (d3dSurfaceType & D3D_ATTACHED_SURFACE) {
|
|
1088 |
// can't handle this for now
|
|
1089 |
J2dRlsTraceLn(J2D_TRACE_ERROR,
|
|
1090 |
"D3DContext::CreateSurface: Can't create attached"\
|
|
1091 |
" surfaces using this code path yet");
|
|
1092 |
ReleaseExclusiveAccess();
|
|
1093 |
return DDERR_GENERIC;
|
|
1094 |
}
|
|
1095 |
if (d3dSurfaceType & D3D_RENDER_TARGET) {
|
|
1096 |
ddsCaps |= DDSCAPS_3DDEVICE;
|
|
1097 |
}
|
|
1098 |
|
|
1099 |
if (SUCCEEDED(res = dxObject->CreateSurface(dwFlags, ddsCaps, ddsCaps2,
|
|
1100 |
(cell != NULL) ?
|
|
1101 |
&cell->pddpf : NULL,
|
|
1102 |
width, height,
|
|
1103 |
&lpRetSurface,
|
|
1104 |
0/*backbuffers*/)))
|
|
1105 |
{
|
|
1106 |
if (d3dSurfaceType & D3D_RENDER_TARGET) {
|
|
1107 |
if (FAILED(res = AttachDepthBuffer(lpRetSurface))) {
|
|
1108 |
lpRetSurface->Release();
|
|
1109 |
delete lpRetSurface;
|
|
1110 |
ReleaseExclusiveAccess();
|
|
1111 |
return res;
|
|
1112 |
}
|
|
1113 |
// Attempt to set the new surface as a temporary render target;
|
|
1114 |
// in some cases this may fail. For example, if undocumented maximum
|
|
1115 |
// Direct3D target surface dimensions were exceeded (2048 in some
|
|
1116 |
// cases).
|
|
1117 |
if (d3dDevice != NULL) {
|
|
1118 |
FlushD3DQueueForTarget(NULL);
|
|
1119 |
|
|
1120 |
IDirectDrawSurface7 *lpDDSurface = NULL;
|
|
1121 |
HRESULT res1 = d3dDevice->GetRenderTarget(&lpDDSurface);
|
|
1122 |
|
|
1123 |
// we are holding a lock for the context, so we can
|
|
1124 |
// change/restore the current render target safely
|
|
1125 |
res = d3dDevice->SetRenderTarget(lpRetSurface->GetDDSurface(), 0);
|
|
1126 |
if (SUCCEEDED(res1) && lpDDSurface != NULL) {
|
|
1127 |
d3dDevice->SetRenderTarget(lpDDSurface, 0);
|
|
1128 |
}
|
|
1129 |
if (FAILED(res)) {
|
|
1130 |
DebugPrintDirectDrawError(res,
|
|
1131 |
"D3DContext::CreateSurface: cannot set new surface as "\
|
|
1132 |
"temp. render target");
|
|
1133 |
lpRetSurface->Release();
|
|
1134 |
delete lpRetSurface;
|
|
1135 |
ReleaseExclusiveAccess();
|
|
1136 |
return res;
|
|
1137 |
}
|
|
1138 |
}
|
|
1139 |
}
|
|
1140 |
*dxSurface = lpRetSurface;
|
|
1141 |
} else {
|
|
1142 |
DebugPrintDirectDrawError(res,
|
|
1143 |
"D3DContext::CreateSurface: error"\
|
|
1144 |
" creating surface");
|
|
1145 |
}
|
|
1146 |
|
|
1147 |
ReleaseExclusiveAccess();
|
|
1148 |
return res;
|
|
1149 |
}
|
|
1150 |
|
|
1151 |
HRESULT
|
|
1152 |
D3DContext::AttachDepthBuffer(DXSurface *dxSurface)
|
|
1153 |
{
|
|
1154 |
HRESULT res;
|
|
1155 |
|
|
1156 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::AttachDepthBuffer");
|
|
1157 |
|
|
1158 |
if (dxSurface == NULL) {
|
|
1159 |
return DDERR_GENERIC;
|
|
1160 |
}
|
|
1161 |
|
|
1162 |
GetExclusiveAccess();
|
|
1163 |
|
|
1164 |
// initialize the depth buffer format it needed
|
|
1165 |
if (depthBufferFormat.dwSize == 0) {
|
|
1166 |
// Some hardware has a restriction that the target surface and the
|
|
1167 |
// attached depth buffer must have the same bit depth, so we should
|
|
1168 |
// attempt to find a depth pixel format with the same depth as
|
|
1169 |
// the target.
|
|
1170 |
DWORD prefDepth = dxSurface->ddsd.ddpfPixelFormat.dwRGBBitCount;
|
|
1171 |
if (FAILED(res = D3DUtils_FindDepthBufferFormat(d3dObject,
|
|
1172 |
prefDepth,
|
|
1173 |
&depthBufferFormat,
|
|
1174 |
pDeviceGUID)))
|
|
1175 |
{
|
|
1176 |
DebugPrintDirectDrawError(res,
|
|
1177 |
"D3DContext::AttachDepthBuffer: "\
|
|
1178 |
"can't find depth buffer format");
|
|
1179 |
ReleaseExclusiveAccess();
|
|
1180 |
return res;
|
|
1181 |
}
|
|
1182 |
}
|
|
1183 |
if (FAILED(res = dxSurface->AttachDepthBuffer(dxObject,
|
|
1184 |
bIsHWRasterizer,
|
|
1185 |
&depthBufferFormat)))
|
|
1186 |
{
|
|
1187 |
DebugPrintDirectDrawError(res,
|
|
1188 |
"D3DContext::AttachDepthBuffer: "\
|
|
1189 |
"can't attach depth buffer or it is lost");
|
|
1190 |
}
|
|
1191 |
|
|
1192 |
ReleaseExclusiveAccess();
|
|
1193 |
return res;
|
|
1194 |
}
|
|
1195 |
|
|
1196 |
/**
|
|
1197 |
* We go into the pains of maintaining the list of set textures
|
|
1198 |
* instead of just calling GetTexture() and comparing the old one
|
|
1199 |
* with the new one because it's actually noticeably slower to call
|
|
1200 |
* GetTexture() (note that we'd have to then call Release() on the
|
|
1201 |
* texture since GetTexture() increases texture's ref. count).
|
|
1202 |
*/
|
|
1203 |
HRESULT /*NOLOCK*/
|
|
1204 |
D3DContext::SetTexture(DXSurface *dxSurface, DWORD dwStage)
|
|
1205 |
{
|
|
1206 |
HRESULT res = D3D_OK;
|
|
1207 |
IDirectDrawSurface7 *newTexture =
|
|
1208 |
dxSurface == NULL ? NULL : dxSurface->GetDDSurface();
|
|
1209 |
|
|
1210 |
if (dwStage < 0 || dwStage > MAX_USED_TEXTURE_STAGE) {
|
|
1211 |
J2dTraceLn1(J2D_TRACE_ERROR,
|
|
1212 |
"D3DContext::SetTexture: incorrect stage: %d", dwStage);
|
|
1213 |
return DDERR_GENERIC;
|
|
1214 |
}
|
|
1215 |
if (lastTexture[dwStage] != newTexture) {
|
|
1216 |
J2dTraceLn1(J2D_TRACE_VERBOSE,
|
|
1217 |
"D3DContext::SetTexture: new texture=0x%x", newTexture);
|
|
1218 |
res = d3dDevice->SetTexture(dwStage, newTexture);
|
|
1219 |
lastTexture[dwStage] = SUCCEEDED(res) ? newTexture : NULL;
|
|
1220 |
}
|
|
1221 |
return res;
|
|
1222 |
}
|
|
1223 |
|
|
1224 |
void
|
|
1225 |
D3DContext::FlushD3DQueueForTarget(DDrawSurface *ddSurface)
|
|
1226 |
{
|
|
1227 |
GetExclusiveAccess();
|
|
1228 |
J2dTraceLn2(J2D_TRACE_VERBOSE,
|
|
1229 |
"D3DContext::FlushD3DQueueForTarget surface=0x%x target=0x%x",
|
|
1230 |
ddSurface, ddTargetSurface);
|
|
1231 |
|
|
1232 |
if ((ddSurface == ddTargetSurface || ddSurface == NULL) &&
|
|
1233 |
d3dDevice != NULL)
|
|
1234 |
{
|
|
1235 |
ForceEndScene();
|
|
1236 |
}
|
|
1237 |
ReleaseExclusiveAccess();
|
|
1238 |
}
|
|
1239 |
|
|
1240 |
void
|
|
1241 |
D3DContext::InvalidateIfTarget(JNIEnv *env, DDrawSurface *ddSurface)
|
|
1242 |
{
|
|
1243 |
GetExclusiveAccess();
|
|
1244 |
if ((ddSurface == ddTargetSurface) && d3dDevice != NULL &&
|
|
1245 |
jD3DContext != NULL)
|
|
1246 |
{
|
|
1247 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
1248 |
"D3DContext:InvalidateIfTarget: invalidating java context");
|
|
1249 |
|
|
1250 |
jobject jD3DContext_tmp = env->NewLocalRef(jD3DContext);
|
|
1251 |
if (jD3DContext_tmp != NULL) {
|
|
1252 |
JNU_CallMethodByName(env, NULL, jD3DContext_tmp,
|
|
1253 |
"invalidateContext", "()V");
|
|
1254 |
env->DeleteLocalRef(jD3DContext_tmp);
|
|
1255 |
}
|
|
1256 |
}
|
|
1257 |
ReleaseExclusiveAccess();
|
|
1258 |
}
|
|
1259 |
|
|
1260 |
void /*NOLOCK*/
|
|
1261 |
D3DContext::UpdateState(jbyte newState)
|
|
1262 |
{
|
|
1263 |
// Try to minimize context switching by only changing
|
|
1264 |
// attributes when necessary.
|
|
1265 |
if (newState != opState) {
|
|
1266 |
// if the new context is texture rendering
|
|
1267 |
if (newState & STATE_TEXTURE) {
|
|
1268 |
// we can be here because of two reasons:
|
|
1269 |
// old context wasn't STATE_TEXTURE or
|
|
1270 |
// the new STATE_TEXTURE_STAGE is different
|
|
1271 |
|
|
1272 |
// do the appropriate texture stage setup if needed
|
|
1273 |
DWORD dwAA1, dwCA1;
|
|
1274 |
BOOL bUpdateStateNeeded = FALSE;
|
|
1275 |
if ((newState & STATE_TEXTURE_STAGE_MASK) &&
|
|
1276 |
!(opState & STATE_TEXTURE_STAGE_MASK))
|
|
1277 |
{
|
|
1278 |
// setup mask rendering
|
|
1279 |
dwAA1 = (D3DTA_TEXTURE|D3DTA_ALPHAREPLICATE);
|
|
1280 |
dwCA1 = (D3DTA_TEXTURE|D3DTA_ALPHAREPLICATE);
|
|
1281 |
bUpdateStateNeeded = TRUE;
|
|
1282 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
1283 |
"UpdateState: STATE_TEXTURE_STAGE_MASK");
|
|
1284 |
} else if ((newState & STATE_TEXTURE_STAGE_BLIT) &&
|
|
1285 |
!(opState & STATE_TEXTURE_STAGE_BLIT))
|
|
1286 |
{
|
|
1287 |
// setup blit rendering
|
|
1288 |
dwAA1 = D3DTA_TEXTURE;
|
|
1289 |
dwCA1 = D3DTA_TEXTURE;
|
|
1290 |
bUpdateStateNeeded = TRUE;
|
|
1291 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
1292 |
"UpdateState: STATE_TEXTURE_STAGE_BLIT");
|
|
1293 |
}
|
|
1294 |
|
|
1295 |
// this optimization makes sense because if the state
|
|
1296 |
// is changing from non-texture to texture, we don't necessarily
|
|
1297 |
// need to update the texture stage state
|
|
1298 |
if (bUpdateStateNeeded) {
|
|
1299 |
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, dwAA1);
|
|
1300 |
d3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, dwCA1);
|
|
1301 |
} else {
|
|
1302 |
J2dTraceLn2(J2D_TRACE_WARNING,
|
|
1303 |
"UpdateState: no context changes were made! "\
|
|
1304 |
"current=0x%x new=0x%x", opState, newState);
|
|
1305 |
}
|
|
1306 |
} else {
|
|
1307 |
J2dTraceLn(J2D_TRACE_VERBOSE,
|
|
1308 |
"UpdateState: STATE_RENDEROP");
|
|
1309 |
// if switching from a texture rendering state
|
|
1310 |
if (opState & STATE_TEXTURE) {
|
|
1311 |
// disable texture rendering
|
|
1312 |
// we don't need to change texture stage states
|
|
1313 |
// because they're irrelevant if the texture
|
|
1314 |
// is not set
|
|
1315 |
// REMIND: another possible optimiziation: instead of
|
|
1316 |
// setting texture to NULL, change the texture stage state
|
|
1317 |
SetTexture(NULL);
|
|
1318 |
}
|
|
1319 |
}
|
|
1320 |
opState = newState;
|
|
1321 |
}
|
|
1322 |
}
|
|
1323 |
|
|
1324 |
HRESULT D3DContext::BeginScene(jbyte newState)
|
|
1325 |
{
|
|
1326 |
if (!d3dDevice) {
|
|
1327 |
return DDERR_GENERIC;
|
|
1328 |
} else {
|
|
1329 |
UpdateState(newState);
|
|
1330 |
if (!bBeginScenePending) {
|
|
1331 |
bBeginScenePending = TRUE;
|
|
1332 |
#ifdef DEBUG
|
|
1333 |
endSceneQueueDepth = 0;
|
|
1334 |
#endif /* DEBUG */
|
|
1335 |
HRESULT res = d3dDevice->BeginScene();
|
|
1336 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::BeginScene");
|
|
1337 |
if (FAILED(res)) {
|
|
1338 |
// this will cause context reinitialization
|
|
1339 |
opState = STATE_UNDEFINED;
|
|
1340 |
}
|
|
1341 |
return res;
|
|
1342 |
}
|
|
1343 |
return D3D_OK;
|
|
1344 |
}
|
|
1345 |
}
|
|
1346 |
|
|
1347 |
HRESULT D3DContext::EndScene(HRESULT ddResult) {
|
|
1348 |
if (FAILED(ddResult)) {
|
|
1349 |
return ForceEndScene();
|
|
1350 |
}
|
|
1351 |
#ifdef DEBUG
|
|
1352 |
endSceneQueueDepth++;
|
|
1353 |
#endif /* DEBUG */
|
|
1354 |
return D3D_OK;
|
|
1355 |
}
|
|
1356 |
|
|
1357 |
HRESULT D3DContext::ForceEndScene() {
|
|
1358 |
if (bBeginScenePending) {
|
|
1359 |
bBeginScenePending = FALSE;
|
|
1360 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext::ForceEndScene");
|
|
1361 |
#ifdef DEBUG
|
|
1362 |
J2dTraceLn1(J2D_TRACE_VERBOSE, " queue depth=%d",
|
|
1363 |
endSceneQueueDepth);
|
|
1364 |
endSceneQueueDepth = 0;
|
|
1365 |
#endif /* DEBUG */
|
|
1366 |
return d3dDevice->EndScene();
|
|
1367 |
}
|
|
1368 |
return D3D_OK;
|
|
1369 |
}
|
|
1370 |
|
|
1371 |
/**
|
|
1372 |
* Utility function: checks the result, calls RestoreSurface
|
|
1373 |
* on the destination surface, and throws InvalidPipeException.
|
|
1374 |
*/
|
|
1375 |
static void
|
|
1376 |
D3DContext_CheckResult(JNIEnv *env, HRESULT res, jlong pDest) {
|
|
1377 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext_CheckResult");
|
|
1378 |
if (FAILED(res)) {
|
|
1379 |
J2dTraceLn(J2D_TRACE_ERROR,
|
|
1380 |
"D3DContext_CheckResult: failed, restoring dest surface");
|
|
1381 |
Win32SDOps *dstOps = (Win32SDOps *)jlong_to_ptr(pDest);
|
|
1382 |
if (dstOps != NULL) {
|
|
1383 |
// RestoreSurface for surfaces associated
|
|
1384 |
// with VolatileImages only marks them lost, not
|
|
1385 |
// attempting to restore. This is done later
|
|
1386 |
// when VolatileImage.validate() is called.
|
|
1387 |
dstOps->RestoreSurface(env, dstOps);
|
|
1388 |
|
|
1389 |
// if this is an "unexpected" error, disable acceleration
|
|
1390 |
// of this image to avoid an infinite recreate/render/error loop
|
|
1391 |
if (res != DDERR_SURFACELOST && res != DDERR_INVALIDMODE &&
|
|
1392 |
res != DDERR_GENERIC && res != DDERR_WASSTILLDRAWING &&
|
|
1393 |
res != DDERR_SURFACEBUSY)
|
|
1394 |
{
|
|
1395 |
jobject sdObject = env->NewLocalRef(dstOps->sdOps.sdObject);
|
|
1396 |
if (sdObject != NULL) {
|
|
1397 |
JNU_CallMethodByName(env, NULL, sdObject,
|
|
1398 |
"disableD3D", "()V");
|
|
1399 |
env->DeleteLocalRef(sdObject);
|
|
1400 |
}
|
|
1401 |
}
|
|
1402 |
|
|
1403 |
}
|
|
1404 |
SurfaceData_ThrowInvalidPipeException(env, "Surface Lost");
|
|
1405 |
}
|
|
1406 |
}
|
|
1407 |
|
|
1408 |
/*
|
|
1409 |
* Class: sun_java2d_d3d_D3DContext
|
|
1410 |
* Method: setTransform
|
|
1411 |
* Signature: (JLLjava/awt/geom/AffineTransform;DDDDDD)V
|
|
1412 |
*/
|
|
1413 |
JNIEXPORT void JNICALL Java_sun_java2d_d3d_D3DContext_setTransform
|
|
1414 |
(JNIEnv *env, jobject d3dc, jlong pCtx, jlong pDest, jobject xform,
|
|
1415 |
jdouble m00, jdouble m10,
|
|
1416 |
jdouble m01, jdouble m11,
|
|
1417 |
jdouble m02, jdouble m12)
|
|
1418 |
{
|
|
1419 |
D3DContext *pd3dc = (D3DContext *)jlong_to_ptr(pCtx);
|
|
1420 |
|
|
1421 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext_setTransform");
|
|
1422 |
if (pd3dc != NULL) {
|
|
1423 |
HRESULT res = pd3dc->SetTransform(xform,
|
|
1424 |
m00, m10,
|
|
1425 |
m01, m11,
|
|
1426 |
m02, m12);
|
|
1427 |
D3DContext_CheckResult(env, res, pDest);
|
|
1428 |
}
|
|
1429 |
}
|
|
1430 |
|
|
1431 |
/*
|
|
1432 |
* Class: sun_java2d_d3d_D3DContext
|
|
1433 |
* Method: resetTransform
|
|
1434 |
* Signature: (JLL)V
|
|
1435 |
*/
|
|
1436 |
JNIEXPORT void JNICALL Java_sun_java2d_d3d_D3DContext_resetTransform
|
|
1437 |
(JNIEnv *env, jobject d3dc, jlong pCtx, jlong pDest)
|
|
1438 |
{
|
|
1439 |
D3DContext *pd3dc = (D3DContext *)jlong_to_ptr(pCtx);
|
|
1440 |
|
|
1441 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext_resetTransform");
|
|
1442 |
if (pd3dc != NULL) {
|
|
1443 |
HRESULT res = pd3dc->SetTransform(NULL, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
|
|
1444 |
D3DContext_CheckResult(env, res, pDest);
|
|
1445 |
}
|
|
1446 |
}
|
|
1447 |
|
|
1448 |
/*
|
|
1449 |
* Class: sun_java2d_d3d_D3DContext
|
|
1450 |
* Method: setClip
|
|
1451 |
* Signature: (JLLsun/java2d/pipe/Region;ZIIII)V
|
|
1452 |
*/
|
|
1453 |
JNIEXPORT void JNICALL Java_sun_java2d_d3d_D3DContext_setClip
|
|
1454 |
(JNIEnv *env, jobject d3dc, jlong pCtx, jlong pDest,
|
|
1455 |
jobject clip, jboolean isRect,
|
|
1456 |
jint x1, jint y1, jint x2, jint y2)
|
|
1457 |
{
|
|
1458 |
D3DContext *pd3dc = (D3DContext *)jlong_to_ptr(pCtx);
|
|
1459 |
|
|
1460 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext_setClip");
|
|
1461 |
if (pd3dc != NULL) {
|
|
1462 |
HRESULT res = pd3dc->SetClip(env, clip, isRect, x1, y1, x2, y2);
|
|
1463 |
D3DContext_CheckResult(env, res, pDest);
|
|
1464 |
}
|
|
1465 |
}
|
|
1466 |
|
|
1467 |
/*
|
|
1468 |
* Class: sun_java2d_d3d_D3DContext
|
|
1469 |
* Method: resetClip
|
|
1470 |
* Signature: (JLLsun/java2d/pipe/Region;Z)V
|
|
1471 |
*/
|
|
1472 |
JNIEXPORT void JNICALL Java_sun_java2d_d3d_D3DContext_resetClip
|
|
1473 |
(JNIEnv *env, jobject d3dc, jlong pCtx, jlong pDest)
|
|
1474 |
{
|
|
1475 |
D3DContext *pd3dc = (D3DContext *)jlong_to_ptr(pCtx);
|
|
1476 |
|
|
1477 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext_resetClip");
|
|
1478 |
if (pd3dc != NULL) {
|
|
1479 |
HRESULT res = pd3dc->SetClip(env, NULL, JNI_FALSE, 0, 0, 0, 0);
|
|
1480 |
D3DContext_CheckResult(env, res, pDest);
|
|
1481 |
}
|
|
1482 |
}
|
|
1483 |
|
|
1484 |
/*
|
|
1485 |
* Class: sun_java2d_d3d_D3DContext
|
|
1486 |
* Method: setRenderTarget
|
|
1487 |
* Signature: (JJ)V
|
|
1488 |
*/
|
|
1489 |
JNIEXPORT void JNICALL Java_sun_java2d_d3d_D3DContext_setRenderTarget
|
|
1490 |
(JNIEnv *env, jobject d3dc, jlong pCtx, jlong pDest)
|
|
1491 |
{
|
|
1492 |
D3DContext *pd3dc = (D3DContext *)jlong_to_ptr(pCtx);
|
|
1493 |
Win32SDOps *dstOps = (Win32SDOps *)jlong_to_ptr(pDest);
|
|
1494 |
|
|
1495 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext_setRenderTarget");
|
|
1496 |
if (pd3dc != NULL && dstOps != NULL) {
|
|
1497 |
HRESULT res = pd3dc->SetRenderTarget(dstOps->lpSurface);
|
|
1498 |
D3DContext_CheckResult(env, res, pDest);
|
|
1499 |
}
|
|
1500 |
}
|
|
1501 |
|
|
1502 |
/*
|
|
1503 |
* Class: sun_java2d_d3d_D3DContext
|
|
1504 |
* Method: setColor
|
|
1505 |
* Signature: (JII)V
|
|
1506 |
*/
|
|
1507 |
JNIEXPORT void JNICALL
|
|
1508 |
Java_sun_java2d_d3d_D3DContext_setColor(JNIEnv *env, jobject oc,
|
|
1509 |
jlong pCtx,
|
|
1510 |
jint pixel, jint flags)
|
|
1511 |
{
|
|
1512 |
D3DContext *d3dc = (D3DContext *)jlong_to_ptr(pCtx);
|
|
1513 |
|
|
1514 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext_setColor");
|
|
1515 |
if (d3dc != NULL) {
|
|
1516 |
d3dc->SetColor(pixel, flags);
|
|
1517 |
}
|
|
1518 |
}
|
|
1519 |
|
|
1520 |
/*
|
|
1521 |
* Class: sun_java2d_d3d_D3DContext
|
|
1522 |
* Method: setAlphaComposite
|
|
1523 |
* Signature: (JIFI)V
|
|
1524 |
*/
|
|
1525 |
JNIEXPORT void JNICALL
|
|
1526 |
Java_sun_java2d_d3d_D3DContext_setAlphaComposite(JNIEnv *env, jobject oc,
|
|
1527 |
jlong pCtx,
|
|
1528 |
jint rule,
|
|
1529 |
jfloat extraAlpha,
|
|
1530 |
jint flags)
|
|
1531 |
{
|
|
1532 |
D3DContext *d3dc = (D3DContext *)jlong_to_ptr(pCtx);
|
|
1533 |
|
|
1534 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext_setAlphaComposite");
|
|
1535 |
if (d3dc != NULL) {
|
|
1536 |
d3dc->SetAlphaComposite(rule, extraAlpha, flags);
|
|
1537 |
}
|
|
1538 |
|
|
1539 |
}
|
|
1540 |
|
|
1541 |
JNIEXPORT void JNICALL
|
|
1542 |
Java_sun_java2d_d3d_D3DContext_resetComposite(JNIEnv *env, jobject oc,
|
|
1543 |
jlong pCtx)
|
|
1544 |
{
|
|
1545 |
D3DContext *d3dc = (D3DContext *)jlong_to_ptr(pCtx);
|
|
1546 |
|
|
1547 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext_resetComposite");
|
|
1548 |
if (d3dc != NULL) {
|
|
1549 |
d3dc->ResetComposite();
|
|
1550 |
}
|
|
1551 |
}
|
|
1552 |
|
|
1553 |
/*
|
|
1554 |
* Class: sun_java2d_d3d_D3DContext
|
|
1555 |
* Method: initNativeContext
|
|
1556 |
* Signature: (I)J
|
|
1557 |
*/
|
|
1558 |
JNIEXPORT jlong JNICALL
|
|
1559 |
Java_sun_java2d_d3d_D3DContext_initNativeContext
|
|
1560 |
(JNIEnv *env, jobject d3dc, jint screen)
|
|
1561 |
{
|
|
1562 |
J2dTraceLn1(J2D_TRACE_INFO, "D3DContext_initNativeContext screen=%d",
|
|
1563 |
screen);
|
|
1564 |
|
|
1565 |
HMONITOR hMon = (HMONITOR)AwtWin32GraphicsDevice::GetMonitor(screen);
|
|
1566 |
DDrawObjectStruct *tmpDdInstance = GetDDInstanceForDevice(hMon);
|
|
1567 |
D3DContext *d3dContext = NULL;
|
|
1568 |
|
|
1569 |
if (tmpDdInstance != NULL && tmpDdInstance->ddObject != NULL) {
|
|
1570 |
AwtToolkit::GetInstance().SendMessage(WM_AWT_D3D_CREATE_DEVICE,
|
|
1571 |
(WPARAM)tmpDdInstance->ddObject,
|
|
1572 |
NULL);
|
|
1573 |
d3dContext = tmpDdInstance->ddObject->GetD3dContext();
|
|
1574 |
}
|
|
1575 |
J2dTraceLn1(J2D_TRACE_VERBOSE,
|
|
1576 |
"D3DContext_initNativeContext created d3dContext=0x%x",
|
|
1577 |
d3dContext);
|
|
1578 |
|
|
1579 |
return ptr_to_jlong(d3dContext);
|
|
1580 |
}
|
|
1581 |
|
|
1582 |
/*
|
|
1583 |
* Class: sun_java2d_d3d_D3DContext
|
|
1584 |
* Method: getNativeDeviceCaps
|
|
1585 |
* Signature: (J)I
|
|
1586 |
*/
|
|
1587 |
JNIEXPORT jint JNICALL Java_sun_java2d_d3d_D3DContext_getNativeDeviceCaps
|
|
1588 |
(JNIEnv *env, jobject d3dc, jlong pCtx)
|
|
1589 |
{
|
|
1590 |
D3DContext *d3dContext = (D3DContext *)jlong_to_ptr(pCtx);
|
|
1591 |
|
|
1592 |
J2dTraceLn(J2D_TRACE_INFO, "D3DContext_getNativeDeviceCaps");
|
|
1593 |
if (d3dContext != NULL) {
|
|
1594 |
d3dContext->SetJavaContext(env, d3dc);
|
|
1595 |
return (jint)d3dContext->GetDeviceCaps();
|
|
1596 |
}
|
|
1597 |
return J2D_D3D_FAILURE;
|
|
1598 |
}
|