Implemented reference counted loading of gl library in java
This commit is contained in:
parent
b07143f400
commit
1827f0efd4
|
@ -508,19 +508,29 @@ public final class Display {
|
||||||
throw new IllegalStateException("Only one LWJGL context may be instantiated at any one time.");
|
throw new IllegalStateException("Only one LWJGL context may be instantiated at any one time.");
|
||||||
if (fullscreen)
|
if (fullscreen)
|
||||||
switchDisplayMode(current_mode);
|
switchDisplayMode(current_mode);
|
||||||
createContext(pixel_format);
|
|
||||||
context = new Display();
|
|
||||||
try {
|
try {
|
||||||
createWindow();
|
GLContext.loadOpenGLLibrary();
|
||||||
|
try {
|
||||||
|
createContext(pixel_format);
|
||||||
|
try {
|
||||||
|
context = new Display();
|
||||||
|
createWindow();
|
||||||
|
makeCurrent();
|
||||||
|
initContext();
|
||||||
|
} catch (LWJGLException e) {
|
||||||
|
destroyContext();
|
||||||
|
context = null;
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
} catch (LWJGLException e) {
|
||||||
|
GLContext.unloadOpenGLLibrary();
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
} catch (LWJGLException e) {
|
} catch (LWJGLException e) {
|
||||||
destroyContext();
|
|
||||||
context = null;
|
|
||||||
if (fullscreen)
|
if (fullscreen)
|
||||||
resetDisplayMode();
|
resetDisplayMode();
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
makeCurrent();
|
|
||||||
initContext();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -595,6 +605,7 @@ public final class Display {
|
||||||
|
|
||||||
destroyWindow();
|
destroyWindow();
|
||||||
destroyContext();
|
destroyContext();
|
||||||
|
GLContext.unloadOpenGLLibrary();
|
||||||
context = null;
|
context = null;
|
||||||
try {
|
try {
|
||||||
GLContext.useContext(null);
|
GLContext.useContext(null);
|
||||||
|
|
|
@ -183,6 +183,8 @@ public final class GLContext {
|
||||||
|
|
||||||
/** Map of classes that have native stubs loaded */
|
/** Map of classes that have native stubs loaded */
|
||||||
private static Map exts;
|
private static Map exts;
|
||||||
|
private static int gl_ref_count = 0;
|
||||||
|
private static boolean did_auto_load = false;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Sys.initialize();
|
Sys.initialize();
|
||||||
|
@ -228,6 +230,8 @@ public final class GLContext {
|
||||||
public static synchronized void useContext(Object context) throws LWJGLException {
|
public static synchronized void useContext(Object context) throws LWJGLException {
|
||||||
if (context == null) {
|
if (context == null) {
|
||||||
unloadStubs();
|
unloadStubs();
|
||||||
|
if (did_auto_load)
|
||||||
|
unloadOpenGLLibrary();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Is this the same as last time?
|
// Is this the same as last time?
|
||||||
|
@ -238,11 +242,19 @@ public final class GLContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ok, now it's the current context.
|
// Ok, now it's the current context.
|
||||||
loadOpenGLLibrary();
|
if (gl_ref_count == 0) {
|
||||||
GL11.initNativeStubs();
|
loadOpenGLLibrary();
|
||||||
loadStubs();
|
did_auto_load = true;
|
||||||
currentContext = new WeakReference(context);
|
}
|
||||||
VBOTracker.setCurrent(currentContext);
|
try {
|
||||||
|
GL11.initNativeStubs();
|
||||||
|
loadStubs();
|
||||||
|
currentContext = new WeakReference(context);
|
||||||
|
VBOTracker.setCurrent(currentContext);
|
||||||
|
} catch (LWJGLException e) {
|
||||||
|
if (did_auto_load)
|
||||||
|
unloadOpenGLLibrary();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void getExtensionClassesAndNames(Map exts, Set exts_names) {
|
private static void getExtensionClassesAndNames(Map exts, Set exts_names) {
|
||||||
|
@ -338,9 +350,28 @@ public final class GLContext {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Native method to load the OpenGL library
|
* If the OpenGL reference count is 0, the library is loaded. The
|
||||||
|
* reference count is then incremented.
|
||||||
*/
|
*/
|
||||||
private static native void loadOpenGLLibrary();
|
public static void loadOpenGLLibrary() throws LWJGLException {
|
||||||
|
if (gl_ref_count == 0)
|
||||||
|
nLoadOpenGLLibrary();
|
||||||
|
gl_ref_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static native void nLoadOpenGLLibrary() throws LWJGLException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The OpenGL library reference count is decremented, and if it
|
||||||
|
* reaches 0, the library is unloaded.
|
||||||
|
*/
|
||||||
|
public static void unloadOpenGLLibrary() {
|
||||||
|
gl_ref_count--;
|
||||||
|
if (gl_ref_count == 0)
|
||||||
|
nUnloadOpenGLLibrary();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static native void nUnloadOpenGLLibrary();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Native method to clear native stub bindings
|
* Native method to clear native stub bindings
|
||||||
|
|
|
@ -97,6 +97,7 @@ public class PbufferTest {
|
||||||
private void initialize() {
|
private void initialize() {
|
||||||
try {
|
try {
|
||||||
//find displaymode
|
//find displaymode
|
||||||
|
pbuffer = new Pbuffer(512, 512, new PixelFormat(), null);
|
||||||
mode = findDisplayMode(800, 600, 16);
|
mode = findDisplayMode(800, 600, 16);
|
||||||
Display.setDisplayMode(mode);
|
Display.setDisplayMode(mode);
|
||||||
// start of in windowed mode
|
// start of in windowed mode
|
||||||
|
@ -175,7 +176,12 @@ public class PbufferTest {
|
||||||
if (pbuffer.isBufferLost()) {
|
if (pbuffer.isBufferLost()) {
|
||||||
System.out.println("Buffer contents lost - will recreate the buffer");
|
System.out.println("Buffer contents lost - will recreate the buffer");
|
||||||
pbuffer.destroy();
|
pbuffer.destroy();
|
||||||
initPbuffer();
|
try {
|
||||||
|
pbuffer = new Pbuffer(512, 512, new PixelFormat(), null);
|
||||||
|
initPbuffer();
|
||||||
|
} catch (LWJGLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
pbuffer.makeCurrent();
|
pbuffer.makeCurrent();
|
||||||
|
@ -235,7 +241,6 @@ public class PbufferTest {
|
||||||
|
|
||||||
private void initPbuffer() {
|
private void initPbuffer() {
|
||||||
try {
|
try {
|
||||||
pbuffer = new Pbuffer(512, 512, new PixelFormat(), null);
|
|
||||||
pbuffer.makeCurrent();
|
pbuffer.makeCurrent();
|
||||||
initGLState(256, 256, 0.5f);
|
initGLState(256, 256, 0.5f);
|
||||||
GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex_handle);
|
GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex_handle);
|
||||||
|
|
|
@ -34,13 +34,17 @@
|
||||||
#include "extgl.h"
|
#include "extgl.h"
|
||||||
#include "common_tools.h"
|
#include "common_tools.h"
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLContext_loadOpenGLLibrary(JNIEnv * env, jclass clazz) {
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLContext_nLoadOpenGLLibrary(JNIEnv * env, jclass clazz) {
|
||||||
if (!extgl_Open()) {
|
if (!extgl_Open()) {
|
||||||
throwException(env, "Failed to load OpenGL library");
|
throwException(env, "Failed to load OpenGL library");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLContext_nUnloadOpenGLLibrary(JNIEnv * env, jclass clazz) {
|
||||||
|
extgl_Close();
|
||||||
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLContext_resetNativeStubs(JNIEnv *env, jclass clazz, jclass gl_class) {
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLContext_resetNativeStubs(JNIEnv *env, jclass clazz, jclass gl_class) {
|
||||||
env->UnregisterNatives(gl_class);
|
env->UnregisterNatives(gl_class);
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,15 +120,25 @@ extern "C" {
|
||||||
/* Inaccessible static: OpenGL13 */
|
/* Inaccessible static: OpenGL13 */
|
||||||
/* Inaccessible static: OpenGL14 */
|
/* Inaccessible static: OpenGL14 */
|
||||||
/* Inaccessible static: OpenGL15 */
|
/* Inaccessible static: OpenGL15 */
|
||||||
/* Inaccessible static: extension_classes */
|
/* Inaccessible static: exts */
|
||||||
/* Inaccessible static: class_00024org_00024lwjgl_00024opengl_00024GL12 */
|
/* Inaccessible static: gl_ref_count */
|
||||||
|
/* Inaccessible static: did_auto_load */
|
||||||
/* Inaccessible static: class_00024org_00024lwjgl_00024opengl_00024GLContext */
|
/* Inaccessible static: class_00024org_00024lwjgl_00024opengl_00024GLContext */
|
||||||
|
/* Inaccessible static: class_00024org_00024lwjgl_00024opengl_00024GL11 */
|
||||||
/*
|
/*
|
||||||
* Class: org_lwjgl_opengl_GLContext
|
* Class: org_lwjgl_opengl_GLContext
|
||||||
* Method: loadOpenGLLibrary
|
* Method: nLoadOpenGLLibrary
|
||||||
* Signature: ()V
|
* Signature: ()V
|
||||||
*/
|
*/
|
||||||
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLContext_loadOpenGLLibrary
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLContext_nLoadOpenGLLibrary
|
||||||
|
(JNIEnv *, jclass);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: org_lwjgl_opengl_GLContext
|
||||||
|
* Method: nUnloadOpenGLLibrary
|
||||||
|
* Signature: ()V
|
||||||
|
*/
|
||||||
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GLContext_nUnloadOpenGLLibrary
|
||||||
(JNIEnv *, jclass);
|
(JNIEnv *, jclass);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -520,7 +520,6 @@ static void destroyContext(void) {
|
||||||
context = NULL;
|
context = NULL;
|
||||||
setRepeatMode(AutoRepeatModeDefault);
|
setRepeatMode(AutoRepeatModeDefault);
|
||||||
decDisplay();
|
decDisplay();
|
||||||
extgl_Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool initWindowGLX13(JNIEnv *env, jobject pixel_format) {
|
static bool initWindowGLX13(JNIEnv *env, jobject pixel_format) {
|
||||||
|
@ -618,17 +617,12 @@ JNIEXPORT jstring JNICALL Java_org_lwjgl_opengl_Display_getVersion(JNIEnv *env,
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_createContext(JNIEnv *env, jclass clazz, jobject pixel_format) {
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_createContext(JNIEnv *env, jclass clazz, jobject pixel_format) {
|
||||||
if (!extgl_Open()) {
|
|
||||||
throwException(env, "Could not load gl libs");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
Display *disp = incDisplay(env);
|
Display *disp = incDisplay(env);
|
||||||
if (disp == NULL)
|
if (disp == NULL)
|
||||||
return;
|
return;
|
||||||
current_screen = XDefaultScreen(disp);
|
current_screen = XDefaultScreen(disp);
|
||||||
if (!extgl_InitGLX(env, disp, current_screen)) {
|
if (!extgl_InitGLX(env, disp, current_screen)) {
|
||||||
decDisplay();
|
decDisplay();
|
||||||
extgl_Close();
|
|
||||||
throwException(env, "Could not init GLX");
|
throwException(env, "Could not init GLX");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -640,7 +634,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_createContext(JNIEnv *env,
|
||||||
}
|
}
|
||||||
if (!create_success) {
|
if (!create_success) {
|
||||||
decDisplay();
|
decDisplay();
|
||||||
extgl_Close();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,6 @@ static void destroy(JNIEnv *env, jclass clazz) {
|
||||||
CGLSetCurrentContext(NULL);
|
CGLSetCurrentContext(NULL);
|
||||||
CGLDestroyContext(context);
|
CGLDestroyContext(context);
|
||||||
destroyMode(env, clazz);
|
destroyMode(env, clazz);
|
||||||
extgl_Close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool createFullscreenContext(JNIEnv *env, jint bpp, jint alpha, jint depth, jint stencil) {
|
static bool createFullscreenContext(JNIEnv *env, jint bpp, jint alpha, jint depth, jint stencil) {
|
||||||
|
@ -114,10 +113,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nMakeCurrent
|
||||||
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nCreate(JNIEnv *env, jclass clazz, jstring title, jint x, jint y, jint width, jint height, jboolean fullscreen, jint bpp, jint alpha, jint depth, jint stencil, jint samples) {
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nCreate(JNIEnv *env, jclass clazz, jstring title, jint x, jint y, jint width, jint height, jboolean fullscreen, jint bpp, jint alpha, jint depth, jint stencil, jint samples) {
|
||||||
vsync_enabled = false;
|
vsync_enabled = false;
|
||||||
current_fullscreen = fullscreen == JNI_TRUE;
|
current_fullscreen = fullscreen == JNI_TRUE;
|
||||||
if (!extgl_Open()) {
|
|
||||||
throwException(env, "Could not load gl library");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!extgl_InitAGL(env)) {
|
if (!extgl_InitAGL(env)) {
|
||||||
throwException(env, "Could not load agl symbols");
|
throwException(env, "Could not load agl symbols");
|
||||||
return;
|
return;
|
||||||
|
@ -125,14 +120,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nCreate(JNIEnv *env, jclass
|
||||||
if (!current_fullscreen) {
|
if (!current_fullscreen) {
|
||||||
if (!switchToNearestMode(env, width, height, bpp, 60)) {
|
if (!switchToNearestMode(env, width, height, bpp, 60)) {
|
||||||
destroyMode(env, clazz);
|
destroyMode(env, clazz);
|
||||||
extgl_Close();
|
|
||||||
throwException(env, "Could not switch mode.");
|
throwException(env, "Could not switch mode.");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!createFullscreenContext(env, bpp, alpha, depth, stencil)) {
|
if (!createFullscreenContext(env, bpp, alpha, depth, stencil)) {
|
||||||
destroyMode(env, clazz);
|
destroyMode(env, clazz);
|
||||||
extgl_Close();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
FlushEventQueue(GetMainEventQueue());
|
FlushEventQueue(GetMainEventQueue());
|
||||||
|
|
|
@ -674,11 +674,6 @@ JNIEXPORT jobject JNICALL Java_org_lwjgl_opengl_Display_init(JNIEnv *env, jclass
|
||||||
}
|
}
|
||||||
|
|
||||||
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_createContext(JNIEnv *env, jclass clazz, jobject pixel_format) {
|
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_createContext(JNIEnv *env, jclass clazz, jobject pixel_format) {
|
||||||
if (!extgl_Open()) {
|
|
||||||
throwException(env, "Failed to open extgl");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 1. Register window class if necessary
|
// 1. Register window class if necessary
|
||||||
if (!registerWindow()) {
|
if (!registerWindow()) {
|
||||||
throwException(env, "Could not register window class");
|
throwException(env, "Could not register window class");
|
||||||
|
@ -686,25 +681,21 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_createContext(JNIEnv *env,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!createWindow(env, 1, 1, false, false)) {
|
if (!createWindow(env, 1, 1, false, false)) {
|
||||||
extgl_Close();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
pixel_format_index = findPixelFormat(env, pixel_format);
|
pixel_format_index = findPixelFormat(env, pixel_format);
|
||||||
if (pixel_format_index == -1) {
|
if (pixel_format_index == -1) {
|
||||||
extgl_Close();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Special option for allowing software opengl
|
// Special option for allowing software opengl
|
||||||
if (!applyPixelFormat(env, hdc, pixel_format_index)) {
|
if (!applyPixelFormat(env, hdc, pixel_format_index)) {
|
||||||
closeWindow();
|
closeWindow();
|
||||||
extgl_Close();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
hglrc = wglCreateContext(hdc);
|
hglrc = wglCreateContext(hdc);
|
||||||
if (hglrc == NULL) {
|
if (hglrc == NULL) {
|
||||||
throwException(env, "Failed to create OpenGL rendering context");
|
throwException(env, "Failed to create OpenGL rendering context");
|
||||||
extgl_Close();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
BOOL result = wglMakeCurrent(hdc, hglrc);
|
BOOL result = wglMakeCurrent(hdc, hglrc);
|
||||||
|
@ -724,23 +715,19 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_createContext(JNIEnv *env,
|
||||||
wglDeleteContext(hglrc);
|
wglDeleteContext(hglrc);
|
||||||
closeWindow();
|
closeWindow();
|
||||||
if (pixel_format_index == -1) {
|
if (pixel_format_index == -1) {
|
||||||
extgl_Close();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!createWindow(env, 1, 1, false, false)) {
|
if (!createWindow(env, 1, 1, false, false)) {
|
||||||
extgl_Close();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!applyPixelFormat(env, hdc, pixel_format_index)) {
|
if (!applyPixelFormat(env, hdc, pixel_format_index)) {
|
||||||
closeWindow();
|
closeWindow();
|
||||||
extgl_Close();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
hglrc = wglCreateContext(hdc);
|
hglrc = wglCreateContext(hdc);
|
||||||
closeWindow();
|
closeWindow();
|
||||||
if (hglrc == NULL) {
|
if (hglrc == NULL) {
|
||||||
throwException(env, "Failed to create OpenGL rendering context (ARB)");
|
throwException(env, "Failed to create OpenGL rendering context (ARB)");
|
||||||
extgl_Close();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
|
@ -756,5 +743,4 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Display_destroyContext(JNIEnv *env,
|
||||||
wglDeleteContext(hglrc);
|
wglDeleteContext(hglrc);
|
||||||
hglrc = NULL;
|
hglrc = NULL;
|
||||||
}
|
}
|
||||||
extgl_Close();
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue