aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/d3d/GraphicsContext3D.cpp
blob: db89b3dc80871ce3f3423b026a14819d29e3f83b (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
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * $RCSfile$
 *
 * Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved.
 *
 * Use is subject to license terms.
 *
 * $Revision$
 * $Date$
 * $State$
 */

#include "StdAfx.h"

extern "C" JNIEXPORT
void JNICALL Java_javax_media_j3d_GraphicsContext3D_readRasterNative(
    JNIEnv *env, jobject obj, jlong ctx,
    jint type, jint xOffset, jint yOffset, 
    jint wRaster, jint hRaster, jint hCanvas, jint format,
    jobject image, jobject depth, jobject gc)
{

    GetDevice();

    jclass gc_class = env->GetObjectClass(gc);
    jfieldID id;

    if ((type & javax_media_j3d_Raster_RASTER_COLOR) != 0) {
        jclass image_class = env->GetObjectClass(image);
	
	if (image_class == NULL) {
	    return;
	}

        id = env->GetFieldID(gc_class, "byteBuffer","[B");
        jarray byteData_array = (jarray) env->GetObjectField(gc, id);
	jbyte *byteData;

	if ((d3dCtx->d3dPresent.SwapEffect == D3DSWAPEFFECT_DISCARD) 
	    // For offScreen rendering, swapBuffer never invoked
	    // so it is safe to use backBuffer
	    && (!d3dCtx->offScreen)
	    // If fail to createFrontBuffer, fallback to use
	    // backSurface. There is no gaurantee this fallback
	    // will work, but at least in non-debug DirectX library
	    // it works.
	    && ((d3dCtx->frontSurface != NULL) ||
		(d3dCtx->frontSurface == NULL) &&
		d3dCtx->createFrontBuffer())) {

	    
	    HRESULT hr = device->GetFrontBuffer(d3dCtx->frontSurface);
	    if (FAILED(hr)) {
		printf("GetFrontBuffer fail %s\n", DXGetErrorString8(hr));
		return;
	    }

	    byteData = (jbyte *)
		env->GetPrimitiveArrayCritical(byteData_array, NULL);

	    if (!d3dCtx->bFullScreen) {
		// We need to invoke GetWindowRect() everytime
		// since message resize() will not receive
		// when Canvas3D inside browers.
		d3dCtx->getScreenRect(d3dCtx->hwnd, &d3dCtx->windowRect);
		copyDataFromSurface(format, 
				    xOffset + d3dCtx->windowRect.left, 
				    yOffset + d3dCtx->windowRect.top, 
				    wRaster,
				    hRaster, 
				    byteData, 
				    d3dCtx->frontSurface);
	    } else {
		copyDataFromSurface(format, xOffset, yOffset,
				    wRaster, hRaster,  byteData,
				    d3dCtx->frontSurface);
	    }
	} else {
	    if (d3dCtx->backSurface == NULL) {
		HRESULT hr = device->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO,
						   &d3dCtx->backSurface);
		if (FAILED(hr)) {
		    printf("GetBackBuffer fail %s\n", DXGetErrorString8(hr));
		    return;
		}
	    }
	    byteData = (jbyte *)
		env->GetPrimitiveArrayCritical(byteData_array, NULL);

	    copyDataFromSurface(format, xOffset, yOffset, wRaster,
				hRaster, byteData, d3dCtx->backSurface);
        }



        env->ReleasePrimitiveArrayCritical(byteData_array, byteData, 0);
    }

    if ((type & javax_media_j3d_Raster_RASTER_DEPTH) != 0) {
	jclass depth_class = env->GetObjectClass(depth);

	if (depth_class == NULL) {
	    return;
	}

        id = env->GetFieldID(depth_class, "width", "I");
        int wDepth = env->GetIntField(depth, id);
        id = env->GetFieldID(depth_class, "type", "I"); 
        int depth_type = env->GetIntField(depth, id);

	if (d3dCtx->depthStencilSurface == NULL) {
	    HRESULT hr =
		device->GetDepthStencilSurface(&d3dCtx->depthStencilSurface);
	    if (FAILED(hr)) {
		if (debug) {
		    printf("[Java3D] Fail to get depth stencil surface %s\n",
			   DXGetErrorString8(hr));
		}
		return;
	    }
	}

        if (depth_type == javax_media_j3d_DepthComponentRetained_DEPTH_COMPONENT_TYPE_INT) { 
            id  = env->GetFieldID(gc_class, "intBuffer","[I");
            jarray intData_array = (jarray) env->GetObjectField(gc, id);
            jint *intData = (jint *) 
		env->GetPrimitiveArrayCritical(intData_array, NULL);

            // yOffset is adjusted for OpenGL - Y upward

	    copyDepthFromSurface(xOffset, yOffset, wRaster,
				 hRaster, intData, d3dCtx->depthStencilSurface);

            env->ReleasePrimitiveArrayCritical(intData_array, intData, 0);

        } else { // javax_media_j3d_DepthComponentRetained_DEPTH_COMPONENT_TYPE_FLOAT
            id = env->GetFieldID(gc_class, "floatBuffer","[F");
            jarray floatData_array = (jarray) env->GetObjectField(gc, id);
            jfloat *floatData = (jfloat *)
		env->GetPrimitiveArrayCritical(floatData_array, NULL);

            // yOffset is adjusted for OpenGL - Y upward
	    copyDepthFromSurface(xOffset, yOffset, wRaster,
				 hRaster, floatData, d3dCtx->depthStencilSurface);

            env->ReleasePrimitiveArrayCritical(floatData_array,
					       floatData, 0);
        }
    }
}