int findPixelFormatOnDC(JNIEnv *env, HDC hdc, int origin_x, int origin_y, jobject pixel_format, jobject pixelFormatCaps, bool use_hdc_bpp, bool window, bool pbuffer, bool double_buffer) { HGLRC dummy_hglrc; HDC saved_current_hdc; HGLRC saved_current_hglrc; WGLExtensions extensions; HWND dummy_hwnd; HDC dummy_hdc; int pixel_format_id; jclass cls_pixel_format = (*env)->GetObjectClass(env, pixel_format); int samples = (int)(*env)->GetIntField(env, pixel_format, (*env)->GetFieldID(env, cls_pixel_format, "samples", "I")); int colorSamples = (int)(*env)->GetIntField(env, pixel_format, (*env)->GetFieldID(env, cls_pixel_format, "colorSamples", "I")); bool floating_point = (bool)(*env)->GetBooleanField(env, pixel_format, (*env)->GetFieldID(env, cls_pixel_format, "floating_point", "Z")); bool floating_point_packed = (bool)(*env)->GetBooleanField(env, pixel_format, (*env)->GetFieldID(env, cls_pixel_format, "floating_point_packed", "Z")); bool sRGB = (bool)(*env)->GetBooleanField(env, pixel_format, (*env)->GetFieldID(env, cls_pixel_format, "sRGB", "Z")); bool use_arb_selection = samples > 0 || floating_point || floating_point_packed || sRGB || pbuffer || pixelFormatCaps != NULL; pixel_format_id = findPixelFormatDefault(env, hdc, pixel_format, use_hdc_bpp, double_buffer); if (!(*env)->ExceptionOccurred(env) && use_arb_selection) { dummy_hwnd = createDummyWindow(origin_x, origin_y); if (dummy_hwnd == NULL) { throwException(env, "Could not create dummy window"); return -1; } dummy_hdc = GetDC(dummy_hwnd); if (!applyPixelFormat(env, dummy_hdc, pixel_format_id)) { closeWindow(&dummy_hwnd, &dummy_hdc); return -1; } dummy_hglrc = wglCreateContext(dummy_hdc); if (dummy_hglrc == NULL) { closeWindow(&dummy_hwnd, &dummy_hdc); throwException(env, "Failed to create OpenGL rendering context"); return -1; } // Save the current HDC and HGLRC to avoid disruption saved_current_hdc = wglGetCurrentDC(); saved_current_hglrc = wglGetCurrentContext(); if (validateAndGetExtensions(env, &extensions, dummy_hdc, dummy_hglrc, samples, colorSamples, floating_point, floating_point_packed, sRGB, pixelFormatCaps)) { pixel_format_id = findPixelFormatARB(env, hdc, &extensions, pixel_format, pixelFormatCaps, use_hdc_bpp, window, pbuffer, double_buffer); } wglMakeCurrent(saved_current_hdc, saved_current_hglrc); wglDeleteContext(dummy_hglrc); closeWindow(&dummy_hwnd, &dummy_hdc); } return pixel_format_id; }
static bool getExtensions(JNIEnv *env, WGLExtensions *extensions, jobject pixel_format, jobject pixelFormatCaps) { int origin_x = 0; int origin_y = 0; HWND dummy_hwnd; HDC dummy_hdc; HGLRC dummy_context; HDC saved_hdc; HGLRC saved_context; int pixel_format_id; dummy_hwnd = createDummyWindow(origin_x, origin_y); if (dummy_hwnd == NULL) { throwException(env, "Could not create dummy window"); return false; } dummy_hdc = GetDC(dummy_hwnd); pixel_format_id = findPixelFormatOnDC(env, dummy_hdc, origin_x, origin_y, pixel_format, pixelFormatCaps, false, true, false, false); if (pixel_format_id == -1) { closeWindow(&dummy_hwnd, &dummy_hdc); return false; } if (!applyPixelFormat(env, dummy_hdc, pixel_format_id)) { closeWindow(&dummy_hwnd, &dummy_hdc); return false; } dummy_context = wglCreateContext(dummy_hdc); if (dummy_context == NULL) { closeWindow(&dummy_hwnd, &dummy_hdc); throwException(env, "Could not create dummy context"); return false; } saved_hdc = wglGetCurrentDC(); saved_context = wglGetCurrentContext(); if (!wglMakeCurrent(dummy_hdc, dummy_context)) { wglMakeCurrent(saved_hdc, saved_context); closeWindow(&dummy_hwnd, &dummy_hdc); wglDeleteContext(dummy_context); throwException(env, "Could not make dummy context current"); return false; } extgl_InitWGL(extensions); if (!wglMakeCurrent(saved_hdc, saved_context)) printfDebugJava(env, "ERROR: Could not restore current context"); closeWindow(&dummy_hwnd, &dummy_hdc); wglDeleteContext(dummy_context); return true; }
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_WindowsPeerInfo_setPixelFormat (JNIEnv *env, jclass clazz, jlong hdc_ptr, jint pixel_format) { HDC hdc = (HDC)(INT_PTR)hdc_ptr; applyPixelFormat(env, hdc, pixel_format); }