Skip to content

Commit

Permalink
Fix[egl_bridge]: fix broken gbuffer pointer and clean up sources
Browse files Browse the repository at this point in the history
  • Loading branch information
artdeell committed Sep 16, 2023
1 parent 566f0ca commit 61a4e56
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 117 deletions.
149 changes: 32 additions & 117 deletions app_pojavlauncher/src/main/jni/egl_bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@
#define GLFW_NO_API 0
#define GLFW_OPENGL_API 0x30001

// This means that the function is an external API and that it will be used
#define EXTERNAL_API __attribute__((used))
// This means that you are forced to have this function/variable for ABI compatibility
#define ABI_COMPAT __attribute__((unused))


struct PotatoBridge {

/* EGLContext */ void* eglContextOld;
/* EGLContext */ void* eglContext;
/* EGLDisplay */ void* eglDisplay;
/* EGLSurface */ void* eglSurface;
Expand All @@ -54,9 +58,7 @@ struct PotatoBridge potatoBridge;
#define RENDERER_VK_ZINK 2
#define RENDERER_VULKAN 4

void* gbuffer;

void pojavTerminate() {
EXTERNAL_API void pojavTerminate() {
printf("EGLBridge: Terminating\n");

switch (pojav_environ->config_renderer) {
Expand All @@ -79,7 +81,7 @@ void pojavTerminate() {
}
}

JNIEXPORT void JNICALL Java_net_kdt_pojavlaunch_utils_JREUtils_setupBridgeWindow(JNIEnv* env, jclass clazz, jobject surface) {
JNIEXPORT void JNICALL Java_net_kdt_pojavlaunch_utils_JREUtils_setupBridgeWindow(JNIEnv* env, ABI_COMPAT jclass clazz, jobject surface) {
pojav_environ->pojavWindow = ANativeWindow_fromSurface(env, surface);
if(pojav_environ->config_renderer == RENDERER_GL4ES) {
gl_setup_window();
Expand All @@ -88,11 +90,11 @@ JNIEXPORT void JNICALL Java_net_kdt_pojavlaunch_utils_JREUtils_setupBridgeWindow


JNIEXPORT void JNICALL
Java_net_kdt_pojavlaunch_utils_JREUtils_releaseBridgeWindow(JNIEnv *env, jclass clazz) {
Java_net_kdt_pojavlaunch_utils_JREUtils_releaseBridgeWindow(ABI_COMPAT JNIEnv *env, ABI_COMPAT jclass clazz) {
ANativeWindow_release(pojav_environ->pojavWindow);
}

void* pojavGetCurrentContext() {
EXTERNAL_API void* pojavGetCurrentContext() {
switch (pojav_environ->config_renderer) {
case RENDERER_GL4ES:
return (void *)eglGetCurrentContext_p();
Expand All @@ -103,41 +105,7 @@ void* pojavGetCurrentContext() {
}
}

/*void dlsym_EGL(void* dl_handle) {
eglBindAPI_p = dlsym(dl_handle,"eglBindAPI");
eglChooseConfig_p = dlsym(dl_handle, "eglChooseConfig");
eglCreateContext_p = dlsym(dl_handle, "eglCreateContext");
eglCreatePbufferSurface_p = dlsym(dl_handle, "eglCreatePbufferSurface");
eglCreateWindowSurface_p = dlsym(dl_handle, "eglCreateWindowSurface");
eglDestroyContext_p = dlsym(dl_handle, "eglDestroyContext");
eglDestroySurface_p = dlsym(dl_handle, "eglDestroySurface");
eglGetConfigAttrib_p = dlsym(dl_handle, "eglGetConfigAttrib");
eglGetCurrentContext_p = dlsym(dl_handle, "eglGetCurrentContext");
eglGetDisplay_p = dlsym(dl_handle, "eglGetDisplay");
eglGetError_p = dlsym(dl_handle, "eglGetError");
eglInitialize_p = dlsym(dl_handle, "eglInitialize");
eglMakeCurrent_p = dlsym(dl_handle, "eglMakeCurrent");
eglSwapBuffers_p = dlsym(dl_handle, "eglSwapBuffers");
eglReleaseThread_p = dlsym(dl_handle, "eglReleaseThread");
eglSwapInterval_p = dlsym(dl_handle, "eglSwapInterval");
eglTerminate_p = dlsym(dl_handle, "eglTerminate");
eglGetCurrentSurface_p = dlsym(dl_handle,"eglGetCurrentSurface");
}
void dlsym_OSMesa(void* dl_handle) {
OSMesaMakeCurrent_p = dlsym(dl_handle,"OSMesaMakeCurrent");
OSMesaGetCurrentContext_p = dlsym(dl_handle,"OSMesaGetCurrentContext");
OSMesaCreateContext_p = dlsym(dl_handle, "OSMesaCreateContext");
OSMesaDestroyContext_p = dlsym(dl_handle, "OSMesaDestroyContext");
OSMesaPixelStore_p = dlsym(dl_handle,"OSMesaPixelStore");
glGetString_p = dlsym(dl_handle,"glGetString");
glClearColor_p = dlsym(dl_handle, "glClearColor");
glClear_p = dlsym(dl_handle,"glClear");
glFinish_p = dlsym(dl_handle,"glFinish");
glReadPixels_p = dlsym(dl_handle,"glReadPixels");
}*/

bool loadSymbols() {
void loadSymbols() {
switch (pojav_environ->config_renderer) {
case RENDERER_VK_ZINK:
dlsym_OSMesa();
Expand Down Expand Up @@ -190,7 +158,7 @@ void* load_turnip_vulkan() {
if(!linker_ns_load(native_dir)) return NULL;
void* linkerhook = linker_ns_dlopen("liblinkerhook.so", RTLD_LOCAL | RTLD_NOW);
if(linkerhook == NULL) return NULL;
void* turnip_driver_handle = linker_ns_dlopen("libvulkan.adr.so", RTLD_LOCAL | RTLD_NOW);
void* turnip_driver_handle = linker_ns_dlopen("libvulkan_freedreno.so", RTLD_LOCAL | RTLD_NOW);
if(turnip_driver_handle == NULL) {
printf("AdrenoSupp: Failed to load Turnip!\n%s\n", dlerror());
dlclose(linkerhook);
Expand All @@ -212,11 +180,11 @@ static void set_vulkan_ptr(void* ptr) {
char envval[64];
sprintf(envval, "%"PRIxPTR, (uintptr_t)ptr);
setenv("VULKAN_PTR", envval, 1);
return;
}

void load_vulkan() {
if(getenv("POJAV_ZINK_PREFER_SYSTEM_DRIVER") == NULL) {
if(getenv("POJAV_ZINK_PREFER_SYSTEM_DRIVER") == NULL &&
android_get_device_api_level() >= 28) { // the loader does not support below that
#ifdef ADRENO_POSSIBLE
void* result = load_turnip_vulkan();
if(result != NULL) {
Expand All @@ -232,7 +200,7 @@ void load_vulkan() {
set_vulkan_ptr(vulkan_ptr);
}

int pojavInit() {
EXTERNAL_API int pojavInit() {
ANativeWindow_acquire(pojav_environ->pojavWindow);
pojav_environ->savedWidth = ANativeWindow_getWidth(pojav_environ->pojavWindow);
pojav_environ->savedHeight = ANativeWindow_getHeight(pojav_environ->pojavWindow);
Expand All @@ -250,7 +218,7 @@ int pojavInitOpenGL() {
const char *renderer = getenv("POJAV_RENDERER");
if (strncmp("opengles", renderer, 8) == 0) {
pojav_environ->config_renderer = RENDERER_GL4ES;
//loadSymbols();
// Symbols are loaded inside gl_bridge
} else if (strcmp(renderer, "vulkan_zink") == 0) {
pojav_environ->config_renderer = RENDERER_VK_ZINK;
load_vulkan();
Expand All @@ -259,7 +227,7 @@ int pojavInitOpenGL() {
}
if(pojav_environ->config_renderer == RENDERER_GL4ES) {
if(gl_init()) {
gl_setup_window(pojav_environ->pojavWindow);
gl_setup_window();
return 1;
}
return 0;
Expand All @@ -269,23 +237,12 @@ int pojavInitOpenGL() {
printf("OSMDroid: %s\n",dlerror());
return 0;
}

printf("OSMDroid: width=%i;height=%i, reserving %i bytes for frame buffer\n", pojav_environ->savedWidth, pojav_environ->savedHeight,
pojav_environ->savedWidth * 4 * pojav_environ->savedHeight);
gbuffer = malloc(pojav_environ->savedWidth * 4 * pojav_environ->savedHeight+1);
if (gbuffer) {
printf("OSMDroid: created frame buffer\n");
return 1;
} else {
printf("OSMDroid: can't generate frame buffer\n");
return 0;
}
}

return 0;
}

void pojavSetWindowHint(int hint, int value) {
EXTERNAL_API void pojavSetWindowHint(int hint, int value) {
if (hint != GLFW_CLIENT_API) return;
switch (value) {
case GLFW_NO_API:
Expand Down Expand Up @@ -321,79 +278,44 @@ void pojavSwapBuffers() {
printf("Zink: attempted to swap buffers without context!");
break;
}
OSMesaMakeCurrent_p(ctx,buf.bits,GL_UNSIGNED_BYTE,pojav_environ->savedWidth,pojav_environ->savedHeight);
OSMesaMakeCurrent_p(ctx,buf.bits,GL_UNSIGNED_BYTE,pojav_environ->savedWidth, pojav_environ->savedHeight);
glFinish_p();
ANativeWindow_unlockAndPost(pojav_environ->pojavWindow);
//OSMesaMakeCurrent_p(ctx,gbuffer,GL_UNSIGNED_BYTE,savedWidth,savedHeight);
ANativeWindow_lock(pojav_environ->pojavWindow,&buf,NULL);
} break;
}
}


bool locked = false;
void pojavMakeCurrent(void* window) {
//if(OSMesaGetCurrentContext_p() != NULL) {
// printf("OSMDroid: skipped context reset\n");
// return JNI_TRUE;
//}
EXTERNAL_API void pojavMakeCurrent(void* window) {
if(pojav_environ->config_renderer == RENDERER_GL4ES) {
gl_make_current((render_window_t*)window);
}
if (pojav_environ->config_renderer == RENDERER_VK_ZINK) {
printf("OSMDroid: making current\n");
OSMesaMakeCurrent_p((OSMesaContext)window,gbuffer,GL_UNSIGNED_BYTE,pojav_environ->savedWidth,pojav_environ->savedHeight);
printf("OSMDroid: making current %p\n", pojav_environ->pojavWindow);
ANativeWindow_lock(pojav_environ->pojavWindow,&buf,NULL);
OSMesaMakeCurrent_p((OSMesaContext)window,buf.bits,GL_UNSIGNED_BYTE,pojav_environ->savedWidth,pojav_environ->savedHeight);
OSMesaPixelStore_p(OSMESA_ROW_LENGTH,buf.stride);
stride = buf.stride;
//ANativeWindow_unlockAndPost(pojav_environ->pojavWindow);
OSMesaPixelStore_p(OSMESA_Y_UP,0);


printf("OSMDroid: vendor: %s\n",glGetString_p(GL_VENDOR));
printf("OSMDroid: renderer: %s\n",glGetString_p(GL_RENDERER));
glClear_p(GL_COLOR_BUFFER_BIT);
glClearColor_p(0.4f, 0.4f, 0.4f, 1.0f);
glClear_p(GL_COLOR_BUFFER_BIT);

pojavSwapBuffers();
return;
}
}

/*
JNIEXPORT void JNICALL
Java_org_lwjgl_glfw_GLFW_nativeEglDetachOnCurrentThread(JNIEnv *env, jclass clazz) {
//Obstruct the context on the current thread
switch (pojav_environ->config_renderer) {
case RENDERER_GL4ES: {
eglMakeCurrent_p(potatoBridge.eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
} break;
case RENDERER_VIRGL:
case RENDERER_VK_ZINK: {
// Nothing to do here
} break;
}
}
*/

void* pojavCreateContext(void* contextSrc) {
EXTERNAL_API void* pojavCreateContext(void* contextSrc) {
if (pojav_environ->config_renderer == RENDERER_VULKAN) {
return (void *)pojav_environ->pojavWindow;
}

pojavInitOpenGL();

if (pojav_environ->config_renderer == RENDERER_GL4ES) {
/*const EGLint ctx_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, atoi(getenv("LIBGL_ES")),
EGL_NONE
};
EGLContext* ctx = eglCreateContext_p(potatoBridge.eglDisplay, config, (void*)contextSrc, ctx_attribs);
potatoBridge.eglContext = ctx;
printf("EGLBridge: Created CTX pointer = %p\n",ctx);
//(*env)->ThrowNew(env,(*env)->FindClass(env,"java/lang/Exception"),"Trace exception");
return (long)ctx;*/
return gl_init_context(contextSrc);
}

Expand All @@ -403,29 +325,22 @@ void* pojavCreateContext(void* contextSrc) {
printf("OSMDroid: context=%p\n",ctx);
return ctx;
}
}

JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL_nativeRegalMakeCurrent(JNIEnv *env, jclass clazz) {
/*printf("Regal: making current");
RegalMakeCurrent_func *RegalMakeCurrent = (RegalMakeCurrent_func *) dlsym(RTLD_DEFAULT, "RegalMakeCurrent");
RegalMakeCurrent(potatoBridge.eglContext);*/

printf("regal removed\n");
printf("Unknown config_renderer value: %i\n", pojav_environ->config_renderer);
abort();
}
JNIEXPORT jlong JNICALL
Java_org_lwjgl_opengl_GL_getGraphicsBufferAddr(JNIEnv *env, jobject thiz) {
return (jlong) &gbuffer;

EXTERNAL_API JNIEXPORT jlong JNICALL
Java_org_lwjgl_opengl_GL_getGraphicsBufferAddr(ABI_COMPAT JNIEnv *env, ABI_COMPAT jobject thiz) {
return (jlong) buf.bits;
}
JNIEXPORT jintArray JNICALL
Java_org_lwjgl_opengl_GL_getNativeWidthHeight(JNIEnv *env, jobject thiz) {
EXTERNAL_API JNIEXPORT jintArray JNICALL
Java_org_lwjgl_opengl_GL_getNativeWidthHeight(JNIEnv *env, ABI_COMPAT jobject thiz) {
jintArray ret = (*env)->NewIntArray(env,2);
jint arr[] = {pojav_environ->savedWidth, pojav_environ->savedHeight};
(*env)->SetIntArrayRegion(env,ret,0,2,arr);
return ret;
}
void pojavSwapInterval(int interval) {
EXTERNAL_API void pojavSwapInterval(int interval) {
switch (pojav_environ->config_renderer) {
case RENDERER_GL4ES: {
gl_swap_interval(interval);
Expand Down
Binary file not shown.

0 comments on commit 61a4e56

Please sign in to comment.