From e426bc80586c70c3987bbb112fced4cfbd8c0b8e Mon Sep 17 00:00:00 2001 From: Flyinghead Date: Mon, 18 Feb 2019 13:14:12 +0100 Subject: [PATCH] android: call eglMakeCurrent() before rendering or deleting context Flush texture cache and terminate gui before releasing the EGL context --- core/hw/pvr/Renderer_if.cpp | 4 +- core/rend/gles/gles.cpp | 39 ++++++++++++------- .../emulator/BaseNativeGLActivity.java | 4 +- .../reicast/src/main/jni/src/Android.cpp | 5 ++- 4 files changed, 35 insertions(+), 17 deletions(-) diff --git a/core/hw/pvr/Renderer_if.cpp b/core/hw/pvr/Renderer_if.cpp index 0b430b220..2117f6248 100644 --- a/core/hw/pvr/Renderer_if.cpp +++ b/core/hw/pvr/Renderer_if.cpp @@ -364,6 +364,8 @@ void rend_init_renderer() void rend_term_renderer() { + killtex(); + gui_term(); renderer->Term(); delete renderer; renderer = NULL; @@ -372,8 +374,6 @@ void rend_term_renderer() delete fallback_renderer; fallback_renderer = NULL; } - killtex(); - gui_term(); } void* rend_thread(void* p) diff --git a/core/rend/gles/gles.cpp b/core/rend/gles/gles.cpp index 00fc0bf6c..4159b1127 100644 --- a/core/rend/gles/gles.cpp +++ b/core/rend/gles/gles.cpp @@ -407,6 +407,14 @@ GLuint fogTextureId; #if (HOST_OS != OS_DARWIN) && !defined(TARGET_NACL32) #if defined(GLES) && !defined(USE_SDL) + + bool egl_makecurrent() + { + if (gl.setup.surface == EGL_NO_SURFACE || gl.setup.context == EGL_NO_CONTEXT) + return false; + return eglMakeCurrent(gl.setup.display, gl.setup.surface, gl.setup.surface, gl.setup.context); + } + // Create a basic GLES context bool gl_init(void* wind, void* disp) { @@ -465,34 +473,39 @@ GLuint fogTextureId; EGLint format; if (!eglGetConfigAttrib(gl.setup.display, config, EGL_NATIVE_VISUAL_ID, &format)) { - printf("eglGetConfigAttrib() returned error %d", eglGetError()); + printf("eglGetConfigAttrib() returned error %x\n", eglGetError()); return false; } ANativeWindow_setBuffersGeometry((ANativeWindow *)wind, 0, 0, format); #endif - gl.setup.surface = eglCreateWindowSurface(gl.setup.display, config, (EGLNativeWindowType)wind, NULL); - if (eglCheck() || gl.setup.surface == EGL_NO_SURFACE) + if (gl.setup.surface == EGL_NO_SURFACE) { - printf("EGL Error: eglCreateWindowSurface failed\n"); + printf("EGL Error: eglCreateWindowSurface failed: %x\n", eglGetError()); return false; } - eglBindAPI(EGL_OPENGL_ES_API); - if (eglCheck()) + if (!eglBindAPI(EGL_OPENGL_ES_API)) + { + printf("eglBindAPI() failed: %x\n", eglGetError()); return false; + } gl.setup.context = eglCreateContext(gl.setup.display, config, NULL, pi32ContextAttribs); - if (eglCheck()) + if (gl.setup.context == EGL_NO_CONTEXT) + { + printf("eglCreateContext() failed: %x\n", eglGetError()); return false; + } } - eglMakeCurrent(gl.setup.display, gl.setup.surface, gl.setup.surface, gl.setup.context); - - if (eglCheck()) + if (!egl_makecurrent()) + { + printf("eglMakeCurrent() failed: %x\n", eglGetError()); return false; + } EGLint w,h; eglQuerySurface(gl.setup.display, gl.setup.surface, EGL_WIDTH, &w); @@ -753,9 +766,9 @@ void gl_term() close( fbdev ); fbdev=-1; #endif - gl.setup.context = NULL; - gl.setup.surface = NULL; - gl.setup.display = NULL; + gl.setup.context = EGL_NO_CONTEXT; + gl.setup.surface = EGL_NO_SURFACE; + gl.setup.display = EGL_NO_DISPLAY; #endif // TARGET_PANDORA || _ANDROID } diff --git a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/BaseNativeGLActivity.java b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/BaseNativeGLActivity.java index f9139894b..33f4621b1 100644 --- a/shell/android-studio/reicast/src/main/java/com/reicast/emulator/BaseNativeGLActivity.java +++ b/shell/android-studio/reicast/src/main/java/com/reicast/emulator/BaseNativeGLActivity.java @@ -4,9 +4,9 @@ import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.support.annotation.Nullable; +import android.util.Log; import android.view.SurfaceHolder; -import com.reicast.emulator.config.EditVJoyActivity; import com.reicast.emulator.emu.JNIdc; import com.reicast.emulator.emu.NativeGLView; @@ -59,11 +59,13 @@ public class BaseNativeGLActivity extends Activity implements SurfaceHolder.Call @Override public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { + //Log.i("BaseNativeGLActivity", "surfaceChanged: " + w + "x" + h); JNIdc.rendinit(holder.getSurface(), w, h); } @Override public void surfaceDestroyed(SurfaceHolder holder) { + //Log.i("BaseNativeGLActivity", "surfaceDestroyed"); JNIdc.rendinit(null, 0, 0); } } diff --git a/shell/android-studio/reicast/src/main/jni/src/Android.cpp b/shell/android-studio/reicast/src/main/jni/src/Android.cpp index 62f49f6a2..7cb547648 100644 --- a/shell/android-studio/reicast/src/main/jni/src/Android.cpp +++ b/shell/android-studio/reicast/src/main/jni/src/Android.cpp @@ -111,7 +111,6 @@ JNIEXPORT void JNICALL Java_com_reicast_emulator_emu_JNIdc_screenDpi(JNIEnv *env screen_dpi = screenDpi; } -void egl_stealcntx(); void SetApplicationPath(wchar *path); int dc_init(int argc, wchar* argv[]); void dc_run(); @@ -126,6 +125,7 @@ bool VramLockedWrite(u8* address); bool rend_single_frame(); void rend_init_renderer(); void rend_term_renderer(); +bool egl_makecurrent(); //extern cResetEvent rs,re; extern int screen_width,screen_height; @@ -536,6 +536,8 @@ JNIEXPORT jboolean JNICALL Java_com_reicast_emulator_emu_JNIdc_rendframe(JNIEnv { if (g_window == NULL) return false; + if (!egl_makecurrent()) + return false; jboolean ret = (jboolean)rend_single_frame(); if (ret) gl_swap(); @@ -553,6 +555,7 @@ JNIEXPORT void JNICALL Java_com_reicast_emulator_emu_JNIdc_rendinit(JNIEnv * env } else { + egl_makecurrent(); rend_term_renderer(); ANativeWindow_release(g_window); g_window = NULL;