Call eglGetDisplay() before make current to no context

This commit is contained in:
khanhduytran0 2020-09-16 19:35:28 +07:00
parent 696c415564
commit 8331aeaf73

View File

@ -49,87 +49,88 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_glfw_GLFW_nativeEglInit(JNIEnv* env, j
} }
JNIEXPORT jboolean JNICALL Java_org_lwjgl_glfw_GLFW_nativeEglMakeCurrent(JNIEnv* env, jclass clazz) { JNIEXPORT jboolean JNICALL Java_org_lwjgl_glfw_GLFW_nativeEglMakeCurrent(JNIEnv* env, jclass clazz) {
if (potatoBridge.eglDisplay == NULL || potatoBridge.eglDisplay == EGL_NO_DISPLAY) {
potatoBridge.eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (potatoBridge.eglDisplay == EGL_NO_DISPLAY) {
printf("Error: eglGetDefaultDisplay() failed: %p\n", eglGetError());
return JNI_FALSE;
}
}
eglMakeCurrent(potatoBridge.eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); eglMakeCurrent(potatoBridge.eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
if (potatoBridge.eglContext == NULL || potatoBridge.eglContext == EGL_NO_CONTEXT) { if (potatoBridge.eglContext == NULL || potatoBridge.eglContext == EGL_NO_CONTEXT) {
printf("EGLBridge: Initializing\n"); printf("EGLBridge: Initializing\n");
printf("ANativeWindow pointer = %p\n", potatoBridge.androidWindow); printf("ANativeWindow pointer = %p\n", potatoBridge.androidWindow);
potatoBridge.eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); if (!eglInitialize(potatoBridge.eglDisplay, NULL, NULL)) {
if (potatoBridge.eglDisplay == EGL_NO_DISPLAY) { printf("Error: eglInitialize() failed\n");
printf("Error: eglGetDefaultDisplay() failed: %p\n", eglGetError()); return JNI_FALSE;
return JNI_FALSE; }
}
if (!eglInitialize(potatoBridge.eglDisplay, NULL, NULL)) {
printf("Error: eglInitialize() failed\n");
return JNI_FALSE;
}
static const EGLint attribs[] = {
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
static const EGLint ctx_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLConfig config; static const EGLint attribs[] = {
EGLint num_configs; EGL_RED_SIZE, 8,
EGLint vid; EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_NONE
};
static const EGLint ctx_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
EGLConfig config;
EGLint num_configs;
EGLint vid;
if (!eglChooseConfig(potatoBridge.eglDisplay, attribs, &config, 1, &num_configs)) { if (!eglChooseConfig(potatoBridge.eglDisplay, attribs, &config, 1, &num_configs)) {
printf("Error: couldn't get an EGL visual config\n"); printf("Error: couldn't get an EGL visual config\n");
return JNI_FALSE; return JNI_FALSE;
} }
assert(config); assert(config);
assert(num_configs > 0); assert(num_configs > 0);
if (!eglGetConfigAttrib(potatoBridge.eglDisplay, config, EGL_NATIVE_VISUAL_ID, &vid)) { if (!eglGetConfigAttrib(potatoBridge.eglDisplay, config, EGL_NATIVE_VISUAL_ID, &vid)) {
printf("Error: eglGetConfigAttrib() failed\n"); printf("Error: eglGetConfigAttrib() failed\n");
return JNI_FALSE; return JNI_FALSE;
} }
ANativeWindow_setBuffersGeometry(potatoBridge.androidWindow, 0, 0, vid); ANativeWindow_setBuffersGeometry(potatoBridge.androidWindow, 0, 0, vid);
eglBindAPI(EGL_OPENGL_ES_API); eglBindAPI(EGL_OPENGL_ES_API);
potatoBridge.eglContext = eglCreateContext(potatoBridge.eglDisplay, config, EGL_NO_CONTEXT, ctx_attribs); potatoBridge.eglContext = eglCreateContext(potatoBridge.eglDisplay, config, EGL_NO_CONTEXT, ctx_attribs);
if (!potatoBridge.eglContext) { if (!potatoBridge.eglContext) {
printf("Error: eglCreateContext failed\n"); printf("Error: eglCreateContext failed\n");
return JNI_FALSE; return JNI_FALSE;
} }
// test eglQueryContext() // test eglQueryContext()
{ {
EGLint val; EGLint val;
eglQueryContext(potatoBridge.eglDisplay, potatoBridge.eglContext, EGL_CONTEXT_CLIENT_VERSION, &val); eglQueryContext(potatoBridge.eglDisplay, potatoBridge.eglContext, EGL_CONTEXT_CLIENT_VERSION, &val);
printf("OpenGL ES from eglQueryContext: %i\n", val); printf("OpenGL ES from eglQueryContext: %i\n", val);
// assert(val >= 2); // assert(val >= 2);
} }
potatoBridge.eglSurface = eglCreateWindowSurface(potatoBridge.eglDisplay, config, potatoBridge.androidWindow, NULL); potatoBridge.eglSurface = eglCreateWindowSurface(potatoBridge.eglDisplay, config, potatoBridge.androidWindow, NULL);
if (!potatoBridge.eglSurface) { if (!potatoBridge.eglSurface) {
printf("Error: eglCreateWindowSurface failed: %p\n", eglGetError()); printf("Error: eglCreateWindowSurface failed: %p\n", eglGetError());
return JNI_FALSE; return JNI_FALSE;
} }
// sanity checks // sanity checks
{ {
EGLint val; EGLint val;
assert(eglGetConfigAttrib(potatoBridge.eglDisplay, config, EGL_SURFACE_TYPE, &val)); assert(eglGetConfigAttrib(potatoBridge.eglDisplay, config, EGL_SURFACE_TYPE, &val));
assert(val & EGL_WINDOW_BIT); assert(val & EGL_WINDOW_BIT);
} }
// return JNI_TRUE; // return JNI_TRUE;
} }
printf("EGLBridge: Making current\n"); printf("EGLBridge: Making current\n");