Skip to content

Commit

Permalink
Fix[renderer]: move GL4ES initialization code (#6447)
Browse files Browse the repository at this point in the history
  • Loading branch information
artdeell authored Jan 3, 2025
1 parent 80e0a6a commit 2e596cd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 23 deletions.
40 changes: 17 additions & 23 deletions app_pojavlauncher/src/main/jni/ctxbridges/gl_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,6 @@ static void gl4esi_get_display_dimensions(int* width, int* height) {
*height = 0;
}

static bool already_initialized = false;
static void gl_init_gl4es_internals() {
if(already_initialized) return;
already_initialized = true;
void* gl4es = dlopen("libgl4es_114.so", RTLD_NOLOAD);
if(gl4es == NULL) return;
void (*set_getmainfbsize)(void (*new_getMainFBSize)(int* width, int* height));
set_getmainfbsize = dlsym(gl4es, "set_getmainfbsize");
if(set_getmainfbsize == NULL) goto warn;
set_getmainfbsize(gl4esi_get_display_dimensions);
goto cleanup;

warn:
printf("gl4esinternals warning: gl4es was found but internals not initialized. expect rendering issues.\n");
cleanup:
// dlclose just decreases a ref counter, so this is fine
dlclose(gl4es);
}

gl_render_window_t* gl_init_context(gl_render_window_t *share) {
gl_render_window_t* bundle = malloc(sizeof(gl_render_window_t));
memset(bundle, 0, sizeof(gl_render_window_t));
Expand Down Expand Up @@ -145,10 +126,6 @@ void gl_swap_surface(gl_render_window_t* bundle) {
}

void gl_make_current(gl_render_window_t* bundle) {
// Perform initialization here as the renderer may not be loaded when gl_init or gl_init_context is called.
// Yes, even though it is dlopened on MC startup by Pojav, due to linker namespacing weirdness
// on API 29/MIUI it may not be loaded at the point of the gl_init call in the current namespace.
gl_init_gl4es_internals();

if(bundle == NULL) {
if(eglMakeCurrent_p(g_EglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) {
Expand Down Expand Up @@ -211,3 +188,20 @@ void gl_swap_interval(int swapInterval) {

eglSwapInterval_p(g_EglDisplay, swapInterval);
}

JNIEXPORT void JNICALL
Java_org_lwjgl_opengl_PojavRendererInit_nativeInitGl4esInternals(JNIEnv *env, jclass clazz,
jobject function_provider) {
__android_log_print(ANDROID_LOG_INFO, g_LogTag, "GL4ES internals initializing...");
jclass funcProviderClass = (*env)->GetObjectClass(env, function_provider);
jmethodID method_getFunctionAddress = (*env)->GetMethodID(env, funcProviderClass, "getFunctionAddress", "(Ljava/lang/CharSequence;)J");
#define GETSYM(N) ((*env)->CallLongMethod(env, function_provider, method_getFunctionAddress, (*env)->NewStringUTF(env, N)));

void (*set_getmainfbsize)(void (*new_getMainFBSize)(int* width, int* height)) = (void*)GETSYM("set_getmainfbsize");
if(set_getmainfbsize != NULL) {
__android_log_print(ANDROID_LOG_INFO, g_LogTag, "GL4ES internals initialized dimension callback");
set_getmainfbsize(gl4esi_get_display_dimensions);
}

#undef GETSYM
}
Original file line number Diff line number Diff line change
Expand Up @@ -4792,6 +4792,8 @@ public final class GLCapabilities {
GLCapabilities(FunctionProvider provider, Set<String> ext, boolean fc, IntFunction<PointerBuffer> bufferFactory) {
forwardCompatible = fc;

PojavRendererInit.onCreateCapabilities(provider);

PointerBuffer caps = bufferFactory.apply(ADDRESS_BUFFER_SIZE);

OpenGL11 = check_GL11(provider, caps, ext, fc);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.lwjgl.opengl;

import org.lwjgl.system.FunctionProvider;
import org.lwjgl.system.SharedLibrary;

import javax.annotation.Nullable;

/**
* Class for initializing renderer-specific callbacks. Allows to reliably initialize
* any callbacks needed for renderers by using the same FunctionProvider as used for loading
* GL symbols.
* */
public class PojavRendererInit {

public static void onCreateCapabilities(FunctionProvider functionProvider) {
String rendererName = null;
if(functionProvider instanceof SharedLibrary) {
SharedLibrary rendererLibrary = (SharedLibrary) functionProvider;
rendererName = rendererLibrary.getName();
}
if(!isValidString(rendererName)) {
rendererName = System.getProperty("org.lwjgl.opengl.libname");
}
if(!isValidString(rendererName)) {
System.out.println("PojavRendererInit: Failed to find Pojav renderer name! " +
"Renderer-specific initialization may not work properly");
}
// NOTE: hardcoded gl4es libname
if(rendererName.endsWith("libgl4es_114.so")) {
nativeInitGl4esInternals(functionProvider);
}
}

private static boolean isValidString(@Nullable String s) {
return s != null && !s.isEmpty();
}

public static native void nativeInitGl4esInternals(FunctionProvider functionProvider);
}

0 comments on commit 2e596cd

Please sign in to comment.