From c134ca5d76f9a72f945f2ed8c21565beec876e46 Mon Sep 17 00:00:00 2001 From: reuk Date: Wed, 2 Jul 2025 20:32:56 +0100 Subject: [PATCH] OpenGL (Android): Use a simpler approach to register SurfaceHolder callbacks --- .../app/com/rmsl/juce/JuceOpenGLView.java | 18 -- .../juce_opengl/native/juce_OpenGL_android.h | 182 ++++++++---------- .../juce_opengl/opengl/juce_OpenGLContext.cpp | 10 +- 3 files changed, 91 insertions(+), 119 deletions(-) diff --git a/modules/juce_opengl/native/java/app/com/rmsl/juce/JuceOpenGLView.java b/modules/juce_opengl/native/java/app/com/rmsl/juce/JuceOpenGLView.java index 16d8ed3ce6..477339cd5a 100644 --- a/modules/juce_opengl/native/java/app/com/rmsl/juce/JuceOpenGLView.java +++ b/modules/juce_opengl/native/java/app/com/rmsl/juce/JuceOpenGLView.java @@ -63,24 +63,6 @@ public class JuceOpenGLView extends SurfaceView return true; } - @Override - protected void onAttachedToWindow () - { - super.onAttachedToWindow (); - - if (host != 0) - onAttchedWindowNative (host); - } - - @Override - protected void onDetachedFromWindow () - { - if (host != 0) - onDetachedFromWindowNative (host); - - super.onDetachedFromWindow (); - } - @Override protected void dispatchDraw (Canvas canvas) { diff --git a/modules/juce_opengl/native/juce_OpenGL_android.h b/modules/juce_opengl/native/juce_OpenGL_android.h index 62a00d3d7f..fae45f56da 100644 --- a/modules/juce_opengl/native/juce_OpenGL_android.h +++ b/modules/juce_opengl/native/juce_OpenGL_android.h @@ -40,75 +40,74 @@ namespace juce // See juce_core/native/java/README.txt on how to generate this byte-code. static const uint8 javaJuceOpenGLView[] = { - 0x1f, 0x8b, 0x08, 0x08, 0xac, 0xdb, 0x8b, 0x62, 0x00, 0x03, 0x4a, 0x61, - 0x76, 0x61, 0x44, 0x65, 0x78, 0x42, 0x79, 0x74, 0x65, 0x43, 0x6f, 0x64, - 0x65, 0x2e, 0x64, 0x65, 0x78, 0x00, 0x6d, 0x54, 0x4f, 0x48, 0x14, 0x51, - 0x18, 0xff, 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0xcb, 0xba, 0x38, 0x86, 0x90, 0xc5, 0x32, 0x0b, - 0xf9, 0x24, 0x13, 0x0d, 0x82, 0x37, 0x3f, 0x91, 0xb7, 0xdb, 0x75, 0x12, - 0xbc, 0xee, 0xae, 0x75, 0x7a, 0x6a, 0xf0, 0x45, 0xe9, 0x72, 0xaf, 0x7c, - 0xcd, 0x07, 0xb9, 0x66, 0xf7, 0xec, 0x3a, 0x64, 0x7b, 0x09, 0x0d, 0xd5, - 0x74, 0x79, 0x16, 0xd5, 0x73, 0x50, 0x85, 0xad, 0x48, 0xfb, 0x7f, 0x5d, - 0xa3, 0x68, 0xbf, 0xe3, 0x3a, 0xc1, 0x69, 0x52, 0x32, 0xd4, 0x94, 0x29, - 0x5b, 0xf6, 0x68, 0xd1, 0x76, 0xcf, 0x1b, 0x59, 0xc7, 0x9e, 0x39, 0x34, - 0x69, 0x56, 0x4c, 0xea, 0x30, 0x4c, 0x37, 0xef, 0x7b, 0x4e, 0x3e, 0x6d, - 0x79, 0x6e, 0x60, 0xbb, 0x41, 0x7a, 0x88, 0xcb, 0xd9, 0xa0, 0x6f, 0x47, - 0x68, 0xc2, 0x37, 0x8b, 0x05, 0xc7, 0x2a, 0xa5, 0x87, 0x4c, 0xb7, 0x62, - 0x96, 0x76, 0x0d, 0x5d, 0xb1, 0x27, 0x1c, 0xcf, 0xed, 0xa3, 0xce, 0xbf, - 0xa1, 0x0a, 0x9a, 0xa4, 0xc7, 0xca, 0xfe, 0x1d, 0xd3, 0xb2, 0x79, 0xc3, - 0x3e, 0xda, 0x6b, 0x58, 0xde, 0x74, 0xda, 0x9f, 0x2e, 0x4d, 0xa5, 0x27, - 0xc1, 0x25, 0xfd, 0x2f, 0xa1, 0x3e, 0x52, 0xb2, 0xa4, 0x66, 0x33, 0xa4, - 0x65, 0x33, 0x06, 0x14, 0x03, 0x8a, 0x91, 0x21, 0x25, 0x47, 0x6a, 0xce, - 0xa0, 0xa8, 0x65, 0xba, 0x96, 0x3d, 0x25, 0x24, 0x38, 0x50, 0xcc, 0x0a, - 0x79, 0x52, 0x3c, 0xef, 0x94, 0x8a, 0x66, 0x60, 0x15, 0x86, 0x7d, 0x73, - 0x86, 0xda, 0x26, 0xcc, 0xa0, 0x60, 0xfb, 0xe3, 0xbe, 0xe9, 0xc2, 0xeb, - 0x63, 0x43, 0x21, 0x31, 0x62, 0x05, 0xaf, 0x14, 0x50, 0xad, 0x6b, 0x06, - 0x4e, 0xc5, 0x1e, 0x2f, 0x38, 0x25, 0x4a, 0x7a, 0xee, 0x60, 0x10, 0x98, - 0x56, 0xc1, 0xce, 0x8f, 0x7b, 0xd7, 0x1c, 0x37, 0xef, 0xcd, 0x50, 0x8b, - 0xf0, 0x71, 0x57, 0xe8, 0x18, 0x11, 0xe9, 0xd4, 0xec, 0xb9, 0xc3, 0x76, - 0x98, 0x7a, 0xce, 0xf7, 0xa6, 0x65, 0x72, 0xe7, 0x6e, 0x5e, 0xb9, 0x22, - 0x8e, 0x18, 0xf8, 0x48, 0x8b, 0x05, 0xbc, 0x61, 0xb4, 0xec, 0x96, 0x4b, - 0x76, 0x9e, 0x0e, 0xaa, 0xc9, 0xd6, 0x98, 0x7e, 0x62, 0xb4, 0x9b, 0xba, - 0x29, 0xa6, 0x5f, 0xa2, 0x11, 0xa5, 0x31, 0xa6, 0x9f, 0x5a, 0xc8, 0xd1, - 0x71, 0xa5, 0x2b, 0xa6, 0x53, 0x3f, 0x85, 0xd6, 0x59, 0xc8, 0x85, 0x1b, - 0xfd, 0xf8, 0x27, 0x19, 0xee, 0x02, 0x9b, 0x9f, 0x67, 0x1b, 0x5a, 0xe4, - 0xbe, 0x4a, 0x2a, 0xa0, 0x00, 0x11, 0x65, 0x91, 0x29, 0xec, 0x19, 0x53, - 0x94, 0x6f, 0x90, 0xbf, 0x98, 0xca, 0x96, 0x22, 0xf2, 0xde, 0xd3, 0x8e, - 0xef, 0x84, 0xcb, 0xed, 0x99, 0xa6, 0x52, 0x75, 0xae, 0x69, 0x54, 0x9d, - 0x6d, 0x8c, 0xaa, 0xf3, 0x6d, 0xbb, 0x06, 0x9f, 0x71, 0x51, 0xaa, 0xce, - 0x39, 0x25, 0x25, 0xe7, 0x04, 0xd7, 0x13, 0xd5, 0x59, 0xa2, 0xa6, 0xc2, - 0xfa, 0x7c, 0xfe, 0x69, 0x32, 0x87, 0xdf, 0x45, 0x4a, 0x85, 0x6b, 0xc5, - 0x3d, 0x4d, 0x84, 0x3a, 0x9f, 0xaf, 0x7f, 0x00, 0x34, 0xf2, 0xd3, 0x47, - 0x98, 0x05, 0x00, 0x00 + 0x1f, 0x8b, 0x08, 0x08, 0x7e, 0xb3, 0x66, 0x68, 0x00, 0x03, 0x63, 0x6c, + 0x61, 0x73, 0x73, 0x65, 0x73, 0x2e, 0x64, 0x65, 0x78, 0x00, 0x6d, 0x94, + 0xbd, 0x6f, 0xd3, 0x50, 0x10, 0xc0, 0xef, 0x3d, 0xbb, 0xa1, 0x5f, 0xb4, + 0x69, 0x4b, 0xa9, 0xe8, 0x50, 0x05, 0x33, 0x20, 0xa1, 0xa4, 0x4e, 0xda, + 0xb4, 0x49, 0x9a, 0x82, 0x8a, 0x1a, 0xbe, 0x42, 0x04, 0x82, 0x56, 0x01, + 0x45, 0x0c, 0xbc, 0xd8, 0x2f, 0x8d, 0xdb, 0xc4, 0xb6, 0x6c, 0x27, 0x8d, + 0x84, 0x68, 0x2b, 0x84, 0x04, 0x62, 0x42, 0x62, 0x64, 0x60, 0x62, 0x60, + 0xeb, 0x9f, 0xd0, 0x81, 0x09, 0x21, 0x31, 0x31, 0xb1, 0xb0, 0xb0, 0x21, + 0x56, 0x24, 0x40, 0x88, 0xb3, 0xfd, 0x42, 0x53, 0x84, 0xa5, 0x9f, 0xef, + 0xde, 0xdd, 0xbd, 0xbb, 0x7b, 0xb2, 0xdf, 0xe9, 0xbc, 0x33, 0x98, 0x9c, + 0xcf, 0xc0, 0xdd, 0x0f, 0x3f, 0xde, 0x7d, 0x23, 0xab, 0x3f, 0xb7, 0x9e, + 0x7e, 0x19, 0x1a, 0x39, 0xd8, 0xfd, 0xfd, 0xe2, 0xe3, 0x7e, 0xf5, 0x71, + 0xf9, 0xe3, 0xe9, 0x68, 0x1f, 0x80, 0x0d, 0x00, 0x9d, 0x72, 0x7a, 0x1c, + 0xc4, 0x63, 0xcb, 0x00, 0x53, 0x10, 0xda, 0xfb, 0x91, 0xf7, 0xc8, 0x31, + 0xe4, 0x2b, 0x42, 0x90, 0x15, 0x7c, 0x0d, 0xa0, 0xbc, 0x4a, 0xc2, 0xf5, + 0x73, 0x7c, 0xdd, 0x97, 0x00, 0xf6, 0x51, 0x6e, 0x52, 0x00, 0x07, 0x69, + 0x21, 0xcf, 0x90, 0xd7, 0xc8, 0x01, 0xf2, 0x09, 0xf9, 0x8e, 0x4c, 0x61, + 0xdc, 0x34, 0x32, 0x83, 0x9c, 0x41, 0xce, 0x22, 0x71, 0x44, 0x45, 0xe6, + 0x91, 0x3c, 0x72, 0x0d, 0xa9, 0x22, 0x75, 0x64, 0x17, 0x79, 0x89, 0xbc, + 0x91, 0xc2, 0x5a, 0x28, 0x00, 0xdb, 0x03, 0x6c, 0x1b, 0x22, 0xa2, 0x2f, + 0xbf, 0xc7, 0xe3, 0x42, 0x46, 0xc4, 0x19, 0x06, 0x84, 0xbe, 0x8c, 0x35, + 0x07, 0x85, 0x5e, 0x40, 0x7d, 0x48, 0xe8, 0x25, 0xd4, 0x87, 0x85, 0xbe, + 0xde, 0x63, 0xbf, 0x87, 0xfa, 0x88, 0xc8, 0xab, 0xd3, 0xb0, 0xce, 0x78, + 0x50, 0x53, 0x0a, 0xf2, 0xca, 0x68, 0x89, 0x06, 0xf5, 0xe5, 0x60, 0xed, + 0xfb, 0x47, 0x03, 0xd9, 0xb5, 0x47, 0x60, 0x2c, 0x90, 0x04, 0x26, 0x84, + 0x3c, 0x11, 0x48, 0x0a, 0x93, 0x22, 0x9e, 0x88, 0x33, 0xf8, 0x0f, 0x15, + 0xf2, 0xba, 0xdc, 0x5d, 0x77, 0x2d, 0xa1, 0x6f, 0x7c, 0x6c, 0x94, 0xc8, + 0x18, 0xed, 0xeb, 0xe7, 0x68, 0xd8, 0xa3, 0x1d, 0x03, 0xac, 0x55, 0x09, + 0x3a, 0x95, 0x82, 0x6c, 0x00, 0x29, 0xd1, 0xeb, 0x49, 0xa4, 0x12, 0x0b, + 0xcf, 0x10, 0xc1, 0x5d, 0x7e, 0xda, 0x05, 0x1a, 0xf6, 0x68, 0xc5, 0x08, + 0xac, 0xc3, 0xda, 0x0a, 0x46, 0xd1, 0x60, 0x07, 0xcd, 0xd2, 0x3e, 0xb0, + 0x57, 0xfa, 0x41, 0xbe, 0x35, 0x02, 0x49, 0x3f, 0xdf, 0x32, 0x2c, 0x42, + 0x1c, 0x2e, 0x11, 0x18, 0x39, 0xff, 0x44, 0x64, 0xee, 0xf6, 0x48, 0x83, + 0x35, 0x11, 0xeb, 0x7f, 0x75, 0x09, 0x22, 0xcb, 0x86, 0x69, 0x78, 0x17, + 0x80, 0x14, 0x61, 0xa2, 0xd8, 0xd2, 0xf8, 0x4d, 0x9b, 0x9b, 0x57, 0x4a, + 0x65, 0x83, 0x6f, 0xcf, 0x6e, 0xb2, 0x36, 0x83, 0x53, 0x25, 0x66, 0xea, + 0x8e, 0x65, 0xe8, 0xaa, 0x66, 0x99, 0x1e, 0x37, 0x3d, 0x75, 0xd5, 0x97, + 0x1d, 0x2f, 0xdf, 0xe3, 0xda, 0x70, 0x98, 0x5d, 0x37, 0x34, 0x57, 0x5d, + 0x65, 0x66, 0x9b, 0xb9, 0xff, 0x75, 0xdd, 0xe6, 0x1b, 0x86, 0x65, 0xe6, + 0x61, 0xfa, 0xaf, 0xab, 0x8d, 0x45, 0xd4, 0xb5, 0x96, 0x53, 0x63, 0x1a, + 0xf7, 0x0b, 0xe6, 0x61, 0xa6, 0xa4, 0x59, 0x4d, 0xd5, 0x69, 0xba, 0x0d, + 0x75, 0x13, 0x7b, 0x51, 0x8f, 0x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x8e, 0xa7, 0x73, 0x19, 0x2d, + 0x95, 0xcc, 0xb1, 0xcc, 0x42, 0xb5, 0x96, 0xa9, 0xe5, 0x52, 0xba, 0x9f, + 0xb5, 0xcd, 0x1d, 0x17, 0xcb, 0xe1, 0xa6, 0xec, 0x6c, 0x2a, 0x39, 0x9b, + 0x4b, 0xe8, 0xbc, 0xad, 0x3c, 0xc4, 0xef, 0x2c, 0x4b, 0x40, 0xe9, 0xde, + 0x9e, 0xfc, 0x56, 0x92, 0x1f, 0xe1, 0xef, 0x20, 0x90, 0xc8, 0x67, 0x89, + 0xc8, 0xbf, 0x24, 0x42, 0xf6, 0xa5, 0xe0, 0x1e, 0x82, 0xf8, 0x27, 0xba, + 0xb2, 0x3b, 0x4b, 0x68, 0xcf, 0x3c, 0x91, 0x7a, 0x66, 0x8a, 0xdc, 0x33, + 0x57, 0xfa, 0xe0, 0x70, 0xb6, 0x44, 0xe0, 0x70, 0xbe, 0x90, 0x58, 0x18, + 0xe7, 0xcf, 0x18, 0x29, 0x16, 0xee, 0xf7, 0xef, 0x03, 0x89, 0x1e, 0xde, + 0x73, 0x1a, 0x0b, 0x6b, 0xf9, 0x33, 0x08, 0x62, 0xe1, 0xde, 0xe0, 0x5e, + 0x45, 0x43, 0xdd, 0x9f, 0x6b, 0x7f, 0x00, 0xa1, 0xe3, 0x13, 0x23, 0x10, + 0x05, 0x00, 0x00 }; //============================================================================== @@ -141,10 +140,19 @@ public: if (surfaceView.get() == nullptr) return; + surfaceHolderCallback = GlobalRef (CreateJavaInterface (this, "android/view/SurfaceHolder$Callback")); + + if (surfaceHolderCallback == nullptr) + return; + + if (LocalRef holder { env->CallObjectMethod (surfaceView, AndroidSurfaceView.getHolder) }) + env->CallVoidMethod (holder, AndroidSurfaceHolder.addCallback, surfaceHolderCallback.get()); + // add the view to the view hierarchy // after this the nativecontext can receive callbacks env->CallVoidMethod ((jobject) component.getPeer()->getNativeHandle(), - AndroidViewGroup.addView, surfaceView.get()); + AndroidViewGroup.addView, + surfaceView.get()); // initialise the geometry of the view updateWindowPosition (component.localAreaToGlobal (component.getLocalBounds())); @@ -155,6 +163,10 @@ public: { auto env = getEnv(); + if (surfaceView != nullptr && surfaceHolderCallback != nullptr) + if (LocalRef holder { env->CallObjectMethod (surfaceView, AndroidSurfaceView.getHolder) }) + env->CallVoidMethod (holder, AndroidSurfaceHolder.removeCallback, surfaceHolderCallback.get()); + if (jobject viewParent = env->CallObjectMethod (surfaceView.get(), JuceOpenGLViewSurface.getParent)) env->CallVoidMethod (viewParent, AndroidViewGroup.removeView, surfaceView.get()); } @@ -276,37 +288,13 @@ private: #define JNI_CLASS_MEMBERS(METHOD, STATICMETHOD, FIELD, STATICFIELD, CALLBACK) \ METHOD (constructor, "", "(Landroid/content/Context;J)V") \ METHOD (getParent, "getParent", "()Landroid/view/ViewParent;") \ - METHOD (getHolder, "getHolder", "()Landroid/view/SurfaceHolder;") \ METHOD (layout, "layout", "(IIII)V" ) \ - CALLBACK (generatedCallback<&NativeContext::attachedToWindow>, "onAttchedWindowNative", "(J)V") \ - CALLBACK (generatedCallback<&NativeContext::detachedFromWindow>, "onDetachedFromWindowNative", "(J)V") \ CALLBACK (generatedCallback<&NativeContext::dispatchDraw>, "onDrawNative", "(JLandroid/graphics/Canvas;)V") DECLARE_JNI_CLASS_WITH_BYTECODE (JuceOpenGLViewSurface, "com/rmsl/juce/JuceOpenGLView", 16, javaJuceOpenGLView) #undef JNI_CLASS_MEMBERS //============================================================================== - static void attachedToWindow (JNIEnv* env, NativeContext& t) - { - LocalRef holder (env->CallObjectMethod (t.surfaceView.get(), JuceOpenGLViewSurface.getHolder)); - - if (t.surfaceHolderCallback == nullptr) - t.surfaceHolderCallback = GlobalRef (CreateJavaInterface (&t, "android/view/SurfaceHolder$Callback")); - - env->CallVoidMethod (holder, AndroidSurfaceHolder.addCallback, t.surfaceHolderCallback.get()); - } - - static void detachedFromWindow (JNIEnv* env, NativeContext& t) - { - if (t.surfaceHolderCallback != nullptr) - { - LocalRef holder (env->CallObjectMethod (t.surfaceView.get(), JuceOpenGLViewSurface.getHolder)); - - env->CallVoidMethod (holder.get(), AndroidSurfaceHolder.removeCallback, t.surfaceHolderCallback.get()); - t.surfaceHolderCallback.clear(); - } - } - static void dispatchDraw (JNIEnv*, NativeContext& t, jobject /*canvas*/) { const std::lock_guard lock { t.nativeHandleMutex }; @@ -371,16 +359,14 @@ private: void operator() (ANativeWindow* ptr) const { if (ptr != nullptr) ANativeWindow_release (ptr); } }; - std::unique_ptr getNativeWindow() const + static std::unique_ptr getNativeWindowFromSurfaceHolder (jobject holder) { auto* env = getEnv(); - const LocalRef holder (env->CallObjectMethod (surfaceView.get(), JuceOpenGLViewSurface.getHolder)); - if (holder == nullptr) return nullptr; - const LocalRef jSurface (env->CallObjectMethod (holder.get(), AndroidSurfaceHolder.getSurface)); + const LocalRef jSurface (env->CallObjectMethod (holder, AndroidSurfaceHolder.getSurface)); if (jSurface == nullptr) return nullptr; diff --git a/modules/juce_opengl/opengl/juce_OpenGLContext.cpp b/modules/juce_opengl/opengl/juce_OpenGLContext.cpp index 482004ea14..f1acbf6c4c 100644 --- a/modules/juce_opengl/opengl/juce_OpenGLContext.cpp +++ b/modules/juce_opengl/opengl/juce_OpenGLContext.cpp @@ -1691,7 +1691,7 @@ void OpenGLContext::NativeContextListener::removeListener (OpenGLContext& ctx, N #if JUCE_ANDROID -void OpenGLContext::NativeContext::surfaceCreated (LocalRef) +void OpenGLContext::NativeContext::surfaceCreated (LocalRef holder) { { const std::lock_guard lock { nativeHandleMutex }; @@ -1701,7 +1701,7 @@ void OpenGLContext::NativeContext::surfaceCreated (LocalRef) // has the context already attached? jassert (surface.get() == EGL_NO_SURFACE && context.get() == EGL_NO_CONTEXT); - const auto window = getNativeWindow(); + const auto window = getNativeWindowFromSurfaceHolder (holder); if (window == nullptr) { @@ -1710,7 +1710,11 @@ void OpenGLContext::NativeContext::surfaceCreated (LocalRef) return; } - // create the surface + // Reset the surface (only one window surface may be alive at a time) + context.reset(); + surface.reset(); + + // Create the surface surface.reset (eglCreateWindowSurface (display, config, window.get(), nullptr)); jassert (surface.get() != EGL_NO_SURFACE);