aboutsummaryrefslogtreecommitdiff
path: root/src/windows/native/sun/java2d/d3d/D3DBlitLoops.cpp
blob: e3e208a77df65fbdf9575d9cccfc9c5c379ec88b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

#include <stdlib.h>
#include <jni.h>
#include "jlong.h"
#include <sun_java2d_d3d_D3DBlitLoops.h>
#include "ddrawUtils.h"
#include "GraphicsPrimitiveMgr.h"
#include "Region.h"

#include "D3DUtils.h"
#include "D3DContext.h"
#include "D3DSurfaceData.h"

extern CriticalSection windowMoveLock;

extern "C" {

JNIEXPORT void JNICALL
Java_sun_java2d_d3d_D3DBlitLoops_doTransform
    (JNIEnv *env, jclass d3dbl,
     jlong pSrcData, jlong pDstData,
     jlong pCtx,
     jint hint,
     jint sx1, jint sy1, jint sx2, jint sy2,
     jfloat dx1, jfloat dy1, jfloat dx2, jfloat dy2)
{
    static J2DLVERTEX quadVerts[4] = {
        { 0.0f, 0.0f, 0.0f, 0x0, 0.0f, 0.0f },
        { 0.0f, 0.0f, 0.0f, 0x0, 0.0f, 0.0f },
        { 0.0f, 0.0f, 0.0f, 0x0, 0.0f, 0.0f },
        { 0.0f, 0.0f, 0.0f, 0x0, 0.0f, 0.0f }
    };

    J2dTraceLn(J2D_TRACE_INFO, "D3DBlitLoops_doTransform");
    J2dTraceLn4(J2D_TRACE_VERBOSE, "  sx1=%-4d sy1=%-4d sx2=%-4d sy2=%-4d ",
                sx1, sy1, sx2, sy2);
    J2dTraceLn4(J2D_TRACE_VERBOSE,
                "  dx1=%4f dy1=%4f dx2=%4f dy2=%4f", dx1, dy1, dx2, dy2);

    if (sx2 <= sx1 || sy2 <= sy1 || dx2 <= dx1 || dy2 <= dy1) {
        J2dTraceLn(J2D_TRACE_WARNING,
                   "D3DBlitLoops_doTransform: invalid dimensions");
        return;
    }

    D3DContext *d3dc = (D3DContext *)jlong_to_ptr(pCtx);
    if (d3dc == NULL) {
        J2dTraceLn(J2D_TRACE_WARNING,
                   "D3DBlitLoops_doTransform: null device context");
        return;
    }
    Win32SDOps *srcOps = (Win32SDOps *)jlong_to_ptr(pSrcData);
    Win32SDOps *dstOps = (Win32SDOps *)jlong_to_ptr(pDstData);

    if (!srcOps->ddInstance || !dstOps->ddInstance) {
        // Some situations can cause us to fail on primary
        // creation, resulting in null lpSurface and null ddInstance
        // for a Win32Surface object.. Just noop this call in that case.
        return;
    }

    DDrawSurface *ddTargetSurface = d3dc->GetTargetSurface();
    DDrawSurface *ddSrcSurface = srcOps->lpSurface;
    if (ddTargetSurface == NULL || ddSrcSurface == NULL) {
        return;
    }
    ddTargetSurface->GetExclusiveAccess();
    d3dc->GetExclusiveAccess();

    IDirect3DDevice7 *d3dDevice = d3dc->Get3DDevice();
    if (d3dDevice == NULL) {
        d3dc->ReleaseExclusiveAccess();
        ddTargetSurface->ReleaseExclusiveAccess();
        return;
    }

    float tw = (float)ddSrcSurface->GetDXSurface()->GetWidth();
    float th = (float)ddSrcSurface->GetDXSurface()->GetHeight();
    float tx1 = ((float)sx1) / tw;
    float ty1 = ((float)sy1) / th;
    float tx2 = ((float)sx2) / tw;
    float ty2 = ((float)sy2) / th;

    D3DU_INIT_VERTEX_QUAD(quadVerts, dx1, dy1, dx2, dy2,
                          d3dc->blitPolygonPixel,
                          tx1, ty1, tx2, ty2);

    if (hint == D3DSD_XFORM_BILINEAR) {
        d3dDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTFG_LINEAR);
        d3dDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTFG_LINEAR);
    } else if (hint == D3DSD_XFORM_NEAREST_NEIGHBOR) {
        d3dDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTFG_POINT);
        d3dDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTFG_POINT);
    }

    HRESULT res;
    D3DU_PRIM2_LOOP_BEGIN(res, srcOps, dstOps);
    if (SUCCEEDED(res = d3dc->BeginScene(STATE_BLITOP))) {
        DXSurface *dxSurface = ddSrcSurface->GetDXSurface();
        if (SUCCEEDED(res = d3dc->SetTexture(dxSurface)))
        {
            res = d3dDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, D3DFVF_J2DLVERTEX,
                                           quadVerts, 4, 0);
        }
        d3dc->EndScene(res);
    }
    D3DU_PRIM2_LOOP_END(env, res, srcOps, dstOps,
                        "DrawPrimitive(D3DPT_TRIANGLEFAN)");

    d3dc->ReleaseExclusiveAccess();
    ddTargetSurface->ReleaseExclusiveAccess();
}

}