aboutsummaryrefslogtreecommitdiffstats
path: root/src/native/ogl/OglCheck.c
blob: b4447803e2e294bb628bc05d5e726e47a8235cda (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
 * $RCSfile$
 *
 * Copyright 2007-2008 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.
 *
 * $Revision$
 * $Date$
 * $State$
 */

#ifdef DEBUG
/* #define VERBOSE */
#endif /* DEBUG */

/* This entire file is Windows-only */
#ifdef WIN32

/* j3dsys.h needs to be included before any other include files to suppres VC warning */
#include "j3dsys.h"

#include <jni.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>

#include <GL/gl.h>
#include "wglext.h"
#include "javax_media_j3d_NativePipeline.h"


static void
printErrorMessage(char *message) 
{
    DWORD err;
    char * errString;

    err = GetLastError();
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
		  FORMAT_MESSAGE_FROM_SYSTEM,
		  NULL, err, 0, (LPTSTR)&errString, 0, NULL);    
    fprintf(stderr, "Java 3D ERROR : %s - %s\n", message, errString);
    LocalFree(errString);
}


/*
 * A dummy WndProc for dummy window
 */
static LONG WINAPI
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    /* This function handles any messages that we didn't. */
    /* (Which is most messages) It belongs to the OS. */
    return (LONG) DefWindowProc( hWnd, msg, wParam, lParam );
}


static HWND
createDummyWindow(const char* szAppName)
{
    static const char *szTitle = "Dummy Window";
    WNDCLASS wc;   /* windows class sruct */
   
    HWND hWnd; 

    /* Fill in window class structure with parameters that */
    /*  describe the main window. */

    wc.style         =
	CS_HREDRAW | CS_VREDRAW;/* Class style(s). */
    wc.lpfnWndProc   = 
	(WNDPROC)WndProc;      /* Window Procedure */
    wc.cbClsExtra    = 0;     /* No per-class extra data. */
    wc.cbWndExtra    = 0;     /* No per-window extra data. */
    wc.hInstance     =
	NULL;            /* Owner of this class */
    wc.hIcon         = NULL;  /* Icon name */
    wc.hCursor       =
	NULL;/* Cursor */
    wc.hbrBackground = 
	(HBRUSH)(COLOR_WINDOW+1);/* Default color */
    wc.lpszMenuName  = NULL;  /* Menu from .RC */
    wc.lpszClassName =
	szAppName;            /* Name to register as

				 /* Register the window class */
    
    if(RegisterClass( &wc )==0) {
	printErrorMessage("createDummyWindow: couldn't register class");
	return NULL;
    }
  
    /* Create a main window for this application instance. */

    hWnd = CreateWindow(
			szAppName, /* app name */
			szTitle,   /* Text for window title bar */
			WS_OVERLAPPEDWINDOW/* Window style */
			/* NEED THESE for OpenGL calls to work!*/
			| WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
			CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
			NULL,     /* no parent window */
			NULL,     /* Use the window class menu.*/
			NULL,     /* This instance owns this window */
			NULL      /* We don't use any extra data */
			);

    /* If window could not be created, return zero */
    if ( !hWnd ){
	printErrorMessage("createDummyWindow: couldn't create window");
	UnregisterClass(szAppName, (HINSTANCE)NULL);
	return NULL;
    }
    return hWnd; 
}


static PIXELFORMATDESCRIPTOR
getDummyPFD()
{
    
    /*  Dummy pixel format.  -- Chien */
    static PIXELFORMATDESCRIPTOR dummy_pfd = {
	sizeof(PIXELFORMATDESCRIPTOR),
	1,                      /* Version number */
	PFD_DRAW_TO_WINDOW |
	PFD_SUPPORT_OPENGL,
	PFD_TYPE_RGBA,
	16,                     /* 16 bit color depth */
	0, 0, 0,                /* RGB bits and pixel sizes */
	0, 0, 0,                /* Do not care about them */
	0, 0,                   /* no alpha buffer info */
	0, 0, 0, 0, 0,          /* no accumulation buffer */
	8,                      /* 8 bit depth buffer */
	0,                      /* no stencil buffer */
	0,                      /* no auxiliary buffers */
	PFD_MAIN_PLANE,         /* layer type */
	0,                      /* reserved, must be 0 */
	0,                      /* no layer mask */
	0,                      /* no visible mask */
	0                       /* no damage mask */
    };

    return dummy_pfd;
}


static BOOL
isSupportedWGL(const char *extensions, const char *extension_string)
{    
    /* get the list of supported extensions */
    const char *p = extensions; 

    /* search for extension_string in the list */
    while(p = strstr(p, extension_string)){
	const char *q = p + strlen(extension_string);

	/* must be terminated by <space> or <nul> */
	if(*q == ' ' || *q == '\0') {
	    return TRUE;
	}

	/* try to find another match */
	p = q;
    }
    return FALSE;
}


/*
static HDC
getMonitorDC(int screen)
{
    return CreateDC("DISPLAY", NULL, NULL, NULL);     
}
*/


/*
 * Extract the version numbers from a copy of the version string.
 * Upon return, numbers[0] contains major version number
 * numbers[1] contains minor version number
 * Note that the passed in version string is modified.
 */
static void
extractVersionInfo(char *versionStr, int* numbers)
{
    char *majorNumStr;
    char *minorNumStr;

    numbers[0] = numbers[1] = -1;
    majorNumStr = strtok(versionStr, (char *)".");
    minorNumStr = strtok(0, (char *)".");
    if (majorNumStr != NULL)
	numbers[0] = atoi(majorNumStr);
    if (minorNumStr != NULL)
	numbers[1] = atoi(minorNumStr);
}


/*
 * get properties from current context
 */
static char*
queryVendorString(HDC hdc, HGLRC hrc)
{
    char *glVersion;
    char *tmpVersionStr;
    int  versionNumbers[2];
    char *glVendor;
    char *supportedExtensions;
    PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB;

    if (!wglMakeCurrent(hdc, hrc)) {
#ifdef DEBUG
	printErrorMessage("getSupportedOglVendorNative : Failed in wglMakeCurrent");
#endif /* DEBUG */
	return NULL;
    }

    wglGetExtensionsStringARB = (PFNWGLGETEXTENSIONSSTRINGARBPROC)
            wglGetProcAddress("wglGetExtensionsStringARB");
    if (wglGetExtensionsStringARB == NULL) {
#ifdef DEBUG
	printErrorMessage("getSupportedOglVendorNative : wglGetExtensionsStringARB not supported\n");
#endif /* DEBUG */
	return NULL;
    }

    /* get the list of supported extensions */
    supportedExtensions = (char *)wglGetExtensionsStringARB(hdc);

#ifdef VERBOSE
    fprintf(stderr, "WGL Supported extensions: %s\n",
       supportedExtensions);
#endif /* VERBOSE */

    if (supportedExtensions == NULL ||
            !isSupportedWGL(supportedExtensions, "WGL_ARB_pixel_format") ||
            wglGetProcAddress("wglChoosePixelFormatARB") == NULL ||
            wglGetProcAddress("wglGetPixelFormatAttribivARB") == NULL) {
#ifdef DEBUG
	printErrorMessage("getSupportedOglVendorNative : wglChoosePixelFormatARB/GetPixelFormatAttribivARB not supported\n");
#endif /* DEBUG */
	return NULL;
    }

    /* Get the OpenGL version */
    glVersion = (char *)glGetString(GL_VERSION);
    if (glVersion == NULL) {
#ifdef DEBUG
	fprintf(stderr, "JAVA 3D ERROR : glVersion == null\n");
#endif /* DEBUG */
	return NULL;
    }

    /* find out the version, major and minor version number */
    tmpVersionStr = strdup(glVersion);
    extractVersionInfo(tmpVersionStr, versionNumbers);
    free(tmpVersionStr);

#ifdef VERBOSE
    fprintf(stderr, "GL_VERSION string = %s\n", glVersion);
    fprintf(stderr, "GL_VERSION (major.minor) = %d.%d\n",
            versionNumbers[0], versionNumbers[1]);
#endif /* VERBOSE */

    /*
     * Check for OpenGL 1.2 or later.
     */
    if (versionNumbers[0] < 1 ||
            (versionNumbers[0] == 1 && versionNumbers[1] < 2)) {
#ifdef DEBUG
	fprintf(stderr,
		"Java 3D ERROR : OpenGL 1.2 or better is required (GL_VERSION=%d.%d)\n",
		versionNumbers[0], versionNumbers[1]);
#endif /* DEBUG */
	return NULL;
    }

    /* Get the OpenGL vendor */
    glVendor = (char *)glGetString(GL_VENDOR);
    if (glVendor == NULL) {
#ifdef DEBUG
	fprintf(stderr, "JAVA 3D ERROR : glVendor == null\n");
#endif /* DEBUG */
	return NULL;
    }

#ifdef VERBOSE
    fprintf(stderr, "GL_VENDOR = %s\n", glVendor);
#endif /* VERBOSE */

    return glVendor;
}


/*
 * Class:     javax_media_j3d_NativePipeline
 * Method:    getSupportedOglVendorNative
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL
Java_javax_media_j3d_NativePipeline_getSupportedOglVendorNative(
    JNIEnv *env,
    jclass clazz)
{
    static char szAppName[] = "OglCheck";

    static int wglAttrs[] = {
        WGL_SUPPORT_OPENGL_ARB,
        TRUE,
        WGL_ACCELERATION_ARB, 
        WGL_FULL_ACCELERATION_ARB,
        WGL_DRAW_TO_WINDOW_ARB, 
        TRUE,
        WGL_RED_BITS_ARB,
        4,
        WGL_GREEN_BITS_ARB,
        4,
        WGL_BLUE_BITS_ARB,
        4,
        WGL_DEPTH_BITS_ARB,
        16,
    };

    HWND  hwnd;
    HGLRC hrc;
    HDC   hdc;
    int pixelFormat;
    PIXELFORMATDESCRIPTOR dummy_pfd = getDummyPFD();
    char *glVendor = NULL;
    jstring glVendorString = NULL;

    JNIEnv table = *env; 

#ifdef VERBOSE
    fprintf(stderr, "NativePipeline.getSupportedOglVendorNative()\n");
#endif /* VERBOSE */

    /*
     * Select any pixel format and bound current context to
     * it so that we can get the wglChoosePixelFormatARB entry point.
     * Otherwise wglxxx entry point will always return null.
     * That's why we need to create a dummy window also.
     */
    hwnd = createDummyWindow((const char *)szAppName);

    if (!hwnd) {
	return NULL;
    }
    hdc = GetDC(hwnd);

    pixelFormat = ChoosePixelFormat(hdc, &dummy_pfd);

    if (pixelFormat<1) {
#ifdef DEBUG
	printErrorMessage("getSupportedOglVendorNative : Failed in ChoosePixelFormat");
#endif /* DEBUG */
	DestroyWindow(hwnd);
	UnregisterClass(szAppName, (HINSTANCE)NULL);
	return NULL;
    }

    if (!SetPixelFormat(hdc, pixelFormat, NULL)) {
#ifdef DEBUG
 	printErrorMessage("getSupportedOglVendorNative : Failed in SetPixelFormat");
#endif /* DEBUG */
	DestroyWindow(hwnd);
	UnregisterClass(szAppName, (HINSTANCE)NULL);
	return NULL;    
    }

    hrc = wglCreateContext(hdc);
    if (!hrc) {
#ifdef DEBUG
	printErrorMessage("getSupportedOglVendorNative : Failed in wglCreateContext");
#endif /* DEBUG */
	DestroyWindow(hwnd);
	UnregisterClass(szAppName, (HINSTANCE)NULL);
	return NULL;
    }

    /* Check OpenGL extensions & version, and return vendor string */
    glVendor = queryVendorString(hdc, hrc);
    if (glVendor != NULL) {
	glVendorString = table->NewStringUTF(env, glVendor);
    }

    /* Destroy all dummy objects */
    wglDeleteContext(hrc);
    ReleaseDC(hwnd, hdc);
    DestroyWindow(hwnd);
    UnregisterClass(szAppName, (HINSTANCE)NULL);

    return glVendorString;
}

#endif /* WIN32 */