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