/** * Initializes the shared context and shared surface. */ static jint WGLGC_InitSharedContext(JNIEnv *env, WGLGraphicsConfigInfo *wglinfo) { HWND hwnd; HDC hdc; J2dTraceLn(J2D_TRACE_INFO, "in WGLGC_InitSharedContext"); sharedContext = WGLGC_InitOGLContext(env, wglinfo, JNI_FALSE); if (sharedContext == NULL) { J2dTraceLn(J2D_TRACE_ERROR, "could not create shared context"); return SD_FAILURE; } hwnd = WGLGC_CreateScratchWindow(wglinfo->screen); if (hwnd == 0) { J2dTraceLn(J2D_TRACE_ERROR, "could not create scratch window"); return SD_FAILURE; } // get the HDC for the scratch window hdc = GetDC(hwnd); if (hdc == 0) { J2dTraceLn(J2D_TRACE_ERROR, "could not get dc for scratch window"); DestroyWindow(hwnd); return SD_FAILURE; } sharedSurface = j2d_wglCreatePbufferARB(hdc, wglinfo->pixfmt, 1, 1, NULL); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); if (sharedSurface == 0) { J2dTraceLn(J2D_TRACE_ERROR, "could not create shared surface"); WGLGC_DestroyOGLContext(env, sharedContext); sharedContext = NULL; return SD_FAILURE; } sharedSurfaceDC = j2d_wglGetPbufferDCARB(sharedSurface); if (sharedSurfaceDC == 0) { J2dTraceLn(J2D_TRACE_ERROR, "could not get hdc for shared pbuffer"); WGLGC_DestroyOGLContext(env, sharedContext); sharedContext = NULL; j2d_wglDestroyPbufferARB(sharedSurface); sharedSurface = 0; return SD_FAILURE; } sharedConfigInfo = wglinfo; return SD_SUCCESS; }
/** * Creates a context that is compatible with the given pixel format * identifier. Returns 0 if the context could not be created properly. */ static HGLRC WGLGC_CreateContext(jint screennum, jint pixfmt) { PIXELFORMATDESCRIPTOR pfd; HWND hwnd; HDC hdc; HGLRC hglrc; J2dTraceLn(J2D_TRACE_INFO, "WGLGC_CreateContext"); hwnd = WGLGC_CreateScratchWindow(screennum); if (hwnd == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGC_CreateContext: could not create scratch window"); return 0; } // get the HDC for the scratch window hdc = GetDC(hwnd); if (hdc == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGC_CreateContext: could not get dc for scratch window"); DestroyWindow(hwnd); return 0; } // set the pixel format for the scratch window if (!SetPixelFormat(hdc, pixfmt, &pfd)) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGC_CreateContext: error setting pixel format"); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return 0; } // create a context based on the scratch window hglrc = j2d_wglCreateContext(hdc); // release the temporary resources ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return hglrc; }
/** * Determines whether the WGL pipeline can be used for a given GraphicsConfig * provided its screen number and visual ID. If the minimum requirements are * met, the native WGLGraphicsConfigInfo structure is initialized for this * GraphicsConfig with the necessary information (pixel format, etc.) * and a pointer to this structure is returned as a jlong. If * initialization fails at any point, zero is returned, indicating that WGL * cannot be used for this GraphicsConfig (we should fallback on the existing * DX pipeline). */ JNIEXPORT jlong JNICALL Java_sun_java2d_opengl_WGLGraphicsConfig_getWGLConfigInfo(JNIEnv *env, jclass wglgc, jint screennum, jint pixfmt) { OGLContext *oglc; PIXELFORMATDESCRIPTOR pfd; HWND hwnd; HDC hdc; HGLRC context; HPBUFFERARB scratch; HDC scratchDC; WGLGraphicsConfigInfo *wglinfo; const unsigned char *versionstr; const char *extstr; jint caps = CAPS_EMPTY; int attrKeys[] = { WGL_DOUBLE_BUFFER_ARB, WGL_ALPHA_BITS_ARB }; int attrVals[2]; J2dRlsTraceLn(J2D_TRACE_INFO, "WGLGraphicsConfig_getWGLConfigInfo"); // initialize GL/WGL extension functions if (!WGLGC_InitExtFuncs(screennum)) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: could not init ext funcs"); return 0L; } // create a scratch window hwnd = WGLGC_CreateScratchWindow(screennum); if (hwnd == 0) { return 0L; } // get the HDC for the scratch window hdc = GetDC(hwnd); if (hdc == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: could not get dc for scratch window"); DestroyWindow(hwnd); return 0L; } if (pixfmt == 0) { // find an appropriate pixel format pixfmt = WGLGC_GetPixelFormatForDC(hdc); if (pixfmt == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: could not find appropriate pixfmt"); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return 0L; } } if (sharedContext == 0) { // create the one shared context sharedContext = WGLGC_CreateContext(screennum, pixfmt); if (sharedContext == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: could not create shared context"); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return 0L; } } // set the pixel format for the scratch window if (!SetPixelFormat(hdc, pixfmt, &pfd)) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsconfig_getWGLConfigInfo: error setting pixel format"); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return 0L; } // create the HGLRC (context) for this WGLGraphicsConfig context = j2d_wglCreateContext(hdc); if (context == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: could not create WGL context"); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return 0L; } // REMIND: when using wglShareLists, the two contexts must use an // identical pixel format... if (!j2d_wglShareLists(sharedContext, context)) { J2dRlsTraceLn(J2D_TRACE_WARNING, "WGLGraphicsConfig_getWGLConfigInfo: unable to share lists"); } // make the context current so that we can query the OpenGL version // and extension strings if (!j2d_wglMakeCurrent(hdc, context)) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: could not make temp context current"); j2d_wglDeleteContext(context); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return 0L; } // get version and extension strings versionstr = j2d_glGetString(GL_VERSION); extstr = j2d_wglGetExtensionsStringARB(hdc); OGLContext_GetExtensionInfo(env, &caps); J2dRlsTraceLn1(J2D_TRACE_INFO, "WGLGraphicsConfig_getWGLConfigInfo: OpenGL version=%s", (versionstr == NULL) ? "null" : (char *)versionstr); if (!OGLContext_IsVersionSupported(versionstr)) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: OpenGL 1.2 is required"); j2d_wglMakeCurrent(NULL, NULL); j2d_wglDeleteContext(context); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return 0L; } // check for required WGL extensions if (!OGLContext_IsExtensionAvailable(extstr, "WGL_ARB_pbuffer") || !OGLContext_IsExtensionAvailable(extstr, "WGL_ARB_make_current_read")|| !OGLContext_IsExtensionAvailable(extstr, "WGL_ARB_pixel_format")) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: required ext(s) unavailable"); j2d_wglMakeCurrent(NULL, NULL); j2d_wglDeleteContext(context); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return 0L; } // get config-specific capabilities j2d_wglGetPixelFormatAttribivARB(hdc, pixfmt, 0, 2, attrKeys, attrVals); if (attrVals[0]) { caps |= CAPS_DOUBLEBUFFERED; } if (attrVals[1] > 0) { caps |= CAPS_STORED_ALPHA; } // create the scratch pbuffer scratch = j2d_wglCreatePbufferARB(hdc, pixfmt, 1, 1, NULL); // destroy the temporary resources j2d_wglMakeCurrent(NULL, NULL); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); if (scratch == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: could not create scratch surface"); j2d_wglDeleteContext(context); return 0L; } // get the HDC for the scratch pbuffer scratchDC = j2d_wglGetPbufferDCARB(scratch); if (scratchDC == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: could not get hdc for scratch surface"); j2d_wglDeleteContext(context); j2d_wglDestroyPbufferARB(scratch); return 0L; } // initialize the OGLContext, which wraps the pixfmt and HGLRC (context) oglc = WGLGC_InitOGLContext(pixfmt, context, scratch, scratchDC, caps); if (oglc == NULL) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: could not create oglc"); j2d_wglDeleteContext(context); j2d_wglReleasePbufferDCARB(scratch, scratchDC); j2d_wglDestroyPbufferARB(scratch); return 0L; } J2dTraceLn(J2D_TRACE_VERBOSE, "WGLGraphicsConfig_getWGLConfigInfo: finished checking dependencies"); // create the WGLGraphicsConfigInfo record for this config wglinfo = (WGLGraphicsConfigInfo *)malloc(sizeof(WGLGraphicsConfigInfo)); if (wglinfo == NULL) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGraphicsConfig_getWGLConfigInfo: could not allocate memory for wglinfo"); WGLGC_DestroyOGLContext(oglc); return 0L; } wglinfo->screen = screennum; wglinfo->pixfmt = pixfmt; wglinfo->context = oglc; return ptr_to_jlong(wglinfo); }
/** * Initializes the extension function pointers for the given device. Note * that under WGL, extension functions have different entrypoints depending * on the device, so we must first make a context current for the given * device before attempting to load the function pointers via * wglGetProcAddress. * * REMIND: ideally the extension function pointers would not be global, but * rather would be stored in a structure associated with the * WGLGraphicsConfig, so that we use the correct function entrypoint * depending on the destination device... */ static jboolean WGLGC_InitExtFuncs(jint screennum) { HWND hwnd; HDC hdc; HGLRC context; J2dTraceLn(J2D_TRACE_INFO, "WGLGC_InitExtFuncs"); // create a scratch window hwnd = WGLGC_CreateScratchWindow(screennum); if (hwnd == 0) { return JNI_FALSE; } // get the HDC for the scratch window hdc = GetDC(hwnd); if (hdc == 0) { DestroyWindow(hwnd); return JNI_FALSE; } // find and set a basic pixel format for the scratch window if (!WGLGC_SetBasicPixelFormatForDC(hdc)) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGC_InitExtFuncs: could not find appropriate pixfmt"); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return JNI_FALSE; } // create a temporary context context = j2d_wglCreateContext(hdc); if (context == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGC_InitExtFuncs: could not create temp WGL context"); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return JNI_FALSE; } // make the context current so that we can load the function pointers // using wglGetProcAddress if (!j2d_wglMakeCurrent(hdc, context)) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGC_InitExtFuncs: could not make temp context current"); j2d_wglDeleteContext(context); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return JNI_FALSE; } if (!OGLFuncs_InitExtFuncs()) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLGC_InitExtFuncs: could not initialize extension funcs"); j2d_wglMakeCurrent(NULL, NULL); j2d_wglDeleteContext(context); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return JNI_FALSE; } // destroy the temporary resources j2d_wglMakeCurrent(NULL, NULL); j2d_wglDeleteContext(context); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return JNI_TRUE; }
/** * Determines whether the WGL pipeline can be used for a given GraphicsConfig * provided its screen number and visual ID. If the minimum requirements are * met, the native WGLGraphicsConfigInfo structure is initialized for this * GraphicsConfig with the necessary information (pixel format, etc.) * and a pointer to this structure is returned as a jlong. If * initialization fails at any point, zero is returned, indicating that WGL * cannot be used for this GraphicsConfig (we should fallback on the existing * DX pipeline). */ static jlong WGLGC_GetWGLConfigInfo(JNIEnv *env, jint screennum, jint pixfmt) { PIXELFORMATDESCRIPTOR pfd; HWND hwnd; HDC hdc; HGLRC context; WGLGraphicsConfigInfo *wglinfo; const unsigned char *versionstr; const char *extstr; int attr[] = { WGL_DOUBLE_BUFFER_ARB, 0 }; int db; J2dTraceLn(J2D_TRACE_INFO, "in WGLGC_GetWGLConfigInfo"); // initialize GL/WGL extension functions if (!WGLGC_InitExtFuncs(screennum)) { J2dTraceLn(J2D_TRACE_ERROR, "could not initialize extension funcs"); return 0L; } // create the WGLGraphicsConfigInfo record for this config wglinfo = (WGLGraphicsConfigInfo *)malloc(sizeof(WGLGraphicsConfigInfo)); if (wglinfo == NULL) { J2dTraceLn(J2D_TRACE_ERROR, "could not allocate memory for wglinfo"); return 0L; } // create a scratch window hwnd = WGLGC_CreateScratchWindow(screennum); if (hwnd == 0) { free(wglinfo); return 0L; } // get the HDC for the scratch window hdc = GetDC(hwnd); if (hdc == 0) { J2dTraceLn(J2D_TRACE_ERROR, "could not get dc for scratch window"); DestroyWindow(hwnd); free(wglinfo); return 0L; } if (pixfmt == 0) { // find an appropriate pixel format pixfmt = WGLGC_GetPixelFormatForDC(hdc); if (pixfmt == 0) { J2dTraceLn(J2D_TRACE_ERROR, "could not find appropriate pixfmt"); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); free(wglinfo); return 0L; } } // set the pixel format for the scratch window if (!SetPixelFormat(hdc, pixfmt, &pfd)) { J2dTraceLn(J2D_TRACE_ERROR, "error setting pixel format"); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); free(wglinfo); return 0L; } // create a temporary context context = j2d_wglCreateContext(hdc); if (context == 0) { J2dTraceLn(J2D_TRACE_ERROR, "could not create temp WGL context"); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); free(wglinfo); return 0L; } // make the context current so that we can query the OpenGL version // and extension strings if (!j2d_wglMakeCurrent(hdc, context)) { J2dTraceLn(J2D_TRACE_ERROR, "could not make temp context current"); j2d_wglDeleteContext(context); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); free(wglinfo); return 0L; } // invalidate the current context OGLContext_InvalidateCurrentContext(env); // get version and extension strings versionstr = j2d_glGetString(GL_VERSION); extstr = j2d_wglGetExtensionsStringARB(hdc); OGLContext_GetExtensionInfo(&wglinfo->extInfo); J2dTraceLn1(J2D_TRACE_INFO, "OpenGL version: %s", versionstr); if (!OGLContext_IsVersionSupported(versionstr)) { J2dTraceLn(J2D_TRACE_ERROR, "invalid OpenGL version; 1.2 is required"); j2d_wglMakeCurrent(NULL, NULL); j2d_wglDeleteContext(context); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); free(wglinfo); return 0L; } // check for required WGL extensions if (!OGLContext_IsExtensionAvailable(extstr, "WGL_ARB_pbuffer") || !OGLContext_IsExtensionAvailable(extstr, "WGL_ARB_render_texture") || !OGLContext_IsExtensionAvailable(extstr, "WGL_ARB_pixel_format")) { J2dTraceLn(J2D_TRACE_ERROR, "required extension(s) not available"); j2d_wglMakeCurrent(NULL, NULL); j2d_wglDeleteContext(context); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); free(wglinfo); return 0L; } // check whether pixel format is double buffered j2d_wglGetPixelFormatAttribivARB(hdc, pixfmt, 0, 1, attr, &db); // destroy the temporary resources j2d_wglMakeCurrent(NULL, NULL); j2d_wglDeleteContext(context); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); J2dTraceLn(J2D_TRACE_VERBOSE, "successfully finished checking dependencies"); wglinfo->screen = screennum; wglinfo->pixfmt = pixfmt; wglinfo->isDoubleBuffered = db; // create the single shared context (if it hasn't been created already) if (sharedContext == NULL) { if (WGLGC_InitSharedContext(env, wglinfo) == SD_FAILURE) { J2dTraceLn(J2D_TRACE_ERROR, "could not init shared context"); free(wglinfo); return 0L; } } return ptr_to_jlong(wglinfo); }
JNIEXPORT jboolean JNICALL Java_sun_java2d_opengl_WGLSurfaceData_initPbuffer (JNIEnv *env, jobject wglsd, jlong pData, jlong pConfigInfo, jboolean isOpaque, jint width, jint height) { int attrKeys[] = { WGL_MAX_PBUFFER_WIDTH_ARB, WGL_MAX_PBUFFER_HEIGHT_ARB, }; int attrVals[2]; int pbAttrList[] = { 0 }; OGLSDOps *oglsdo = (OGLSDOps *)jlong_to_ptr(pData); WGLGraphicsConfigInfo *wglInfo = (WGLGraphicsConfigInfo *)jlong_to_ptr(pConfigInfo); WGLSDOps *wglsdo; HWND hwnd; HDC hdc, pbufferDC; HPBUFFERARB pbuffer; int maxWidth, maxHeight; int actualWidth, actualHeight; J2dTraceLn3(J2D_TRACE_INFO, "WGLSurfaceData_initPbuffer: w=%d h=%d opq=%d", width, height, isOpaque); if (oglsdo == NULL) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLSurfaceData_initPbuffer: ops are null"); return JNI_FALSE; } wglsdo = (WGLSDOps *)oglsdo->privOps; if (wglsdo == NULL) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLSurfaceData_initPbuffer: wgl ops are null"); return JNI_FALSE; } if (wglInfo == NULL) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLSurfaceData_initPbuffer: wgl config info is null"); return JNI_FALSE; } // create a scratch window hwnd = WGLGC_CreateScratchWindow(wglInfo->screen); if (hwnd == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLSurfaceData_initPbuffer: could not create scratch window"); return JNI_FALSE; } // get the HDC for the scratch window hdc = GetDC(hwnd); if (hdc == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLSurfaceData_initPbuffer: could not get dc for scratch window"); DestroyWindow(hwnd); return JNI_FALSE; } // get the maximum allowable pbuffer dimensions j2d_wglGetPixelFormatAttribivARB(hdc, wglInfo->pixfmt, 0, 2, attrKeys, attrVals); maxWidth = attrVals[0]; maxHeight = attrVals[1]; J2dTraceLn4(J2D_TRACE_VERBOSE, " desired pbuffer dimensions: w=%d h=%d maxw=%d maxh=%d", width, height, maxWidth, maxHeight); // if either dimension is 0 or larger than the maximum, we cannot // allocate a pbuffer with the requested dimensions if (width == 0 || width > maxWidth || height == 0 || height > maxHeight) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLSurfaceData_initPbuffer: invalid dimensions"); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); return JNI_FALSE; } pbuffer = j2d_wglCreatePbufferARB(hdc, wglInfo->pixfmt, width, height, pbAttrList); ReleaseDC(hwnd, hdc); DestroyWindow(hwnd); if (pbuffer == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLSurfaceData_initPbuffer: could not create wgl pbuffer"); return JNI_FALSE; } // note that we get the DC for the pbuffer at creation time, and then // release the DC when the pbuffer is disposed; the WGL_ARB_pbuffer // spec is vague about such things, but from past experience we know // this approach to be more robust than, for example, doing a // Get/ReleasePbufferDC() everytime we make a context current pbufferDC = j2d_wglGetPbufferDCARB(pbuffer); if (pbufferDC == 0) { J2dRlsTraceLn(J2D_TRACE_ERROR, "WGLSurfaceData_initPbuffer: could not get dc for pbuffer"); j2d_wglDestroyPbufferARB(pbuffer); return JNI_FALSE; } // make sure the actual dimensions match those that we requested j2d_wglQueryPbufferARB(pbuffer, WGL_PBUFFER_WIDTH_ARB, &actualWidth); j2d_wglQueryPbufferARB(pbuffer, WGL_PBUFFER_HEIGHT_ARB, &actualHeight); if (width != actualWidth || height != actualHeight) { J2dRlsTraceLn2(J2D_TRACE_ERROR, "WGLSurfaceData_initPbuffer: actual (w=%d h=%d) != requested", actualWidth, actualHeight); j2d_wglReleasePbufferDCARB(pbuffer, pbufferDC); j2d_wglDestroyPbufferARB(pbuffer); return JNI_FALSE; } oglsdo->drawableType = OGLSD_PBUFFER; oglsdo->isOpaque = isOpaque; oglsdo->width = width; oglsdo->height = height; wglsdo->pbuffer = pbuffer; wglsdo->pbufferDC = pbufferDC; OGLSD_SetNativeDimensions(env, oglsdo, width, height); return JNI_TRUE; }