From 0f9cdc2b54a8c42f0a16632ca8c0a12aa6b9f880 Mon Sep 17 00:00:00 2001 From: ivorne <40567249+ivorne@users.noreply.github.com> Date: Tue, 31 Jul 2018 17:20:42 +0200 Subject: [PATCH] Fixed selection of GLES version on android. (#6088) Fixes #6086 Fixes #6047 --- .../src/cc/openframeworks/OFActivity.java | 4 ++ .../src/cc/openframeworks/OFAndroid.java | 12 +++- .../cc/openframeworks/OFAndroidLifeCycle.java | 56 +++++++++++-------- .../cc/openframeworks/OFAndroidWindow.java | 13 +++-- addons/ofxAndroid/src/ofAppAndroidWindow.cpp | 23 +++++--- addons/ofxAndroid/src/ofAppAndroidWindow.h | 2 + .../project/android/common-functions.gradle | 2 +- 7 files changed, 74 insertions(+), 38 deletions(-) diff --git a/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFActivity.java b/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFActivity.java index ba6e2a6b9e7..6d127b5689e 100644 --- a/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFActivity.java +++ b/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFActivity.java @@ -20,6 +20,10 @@ public void onUnpackingResourcesDone(){} //gesture handler member private ViewGroup mOFGlSurfaceContainer; + public ViewGroup getSurfaceContainer(){ + return mOFGlSurfaceContainer; + } + public void initView(){ String packageName = this.getPackageName(); try { diff --git a/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroid.java b/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroid.java index f3f9355dfcf..5578d0773e0 100644 --- a/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroid.java +++ b/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroid.java @@ -37,6 +37,7 @@ import android.widget.ListAdapter; import android.widget.ListView; import android.widget.Toast; +import java.util.concurrent.Semaphore; public class OFAndroid { @@ -822,13 +823,22 @@ public static void disableOrientationChangeEvents(){ public static void setupGL(int version){ final int finalversion = version; + final Semaphore mutex = new Semaphore( 0 ); + runOnMainThread(new Runnable() { - @Override public void run() { OFEGLConfigChooser.setGLESVersion(finalversion); + OFAndroidLifeCycle.glCreateSurface(); + mutex.release(); } }); + + try{ + mutex.acquire(); + } catch( Exception ex ){ + Log.w( "OF", "setupGL mutex acquire failed" ); + } } /** diff --git a/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroidLifeCycle.java b/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroidLifeCycle.java index 14296f6763d..d8df36bee50 100644 --- a/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroidLifeCycle.java +++ b/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroidLifeCycle.java @@ -262,18 +262,25 @@ public static void init() } static String TAG = "OF"; - + public static void glCreate() - { + { Log.d(TAG, "glCreate"); + + if(m_countActivities == 0) + pushState(State.create); + m_countActivities++; + } + + public static void glCreateSurface() + { if(mGLView == null) { Log.d(TAG, "Create surface"); mGLView = new OFGLSurfaceView(m_activity); + + glResume( getActivity().getSurfaceContainer() ); } - if(m_countActivities == 0) - pushState(State.create); - m_countActivities++; } public static void glResume(ViewGroup glContainer) @@ -281,14 +288,17 @@ public static void glResume(ViewGroup glContainer) Log.d(TAG, "glResume"); OFGLSurfaceView glView = getGLView(); - glView.setVisibility(View.INVISIBLE); + if( glView != null ) + { + glView.setVisibility(View.INVISIBLE); + + glContainer.addView(glView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); + Log.d(TAG, "addView surface"); - glContainer.addView(glView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); - Log.d(TAG, "addView surface"); - - glView.onResume(); - - pushState(State.resume); + glView.onResume(); + + pushState(State.resume); + } } public static void glPause() @@ -296,17 +306,19 @@ public static void glPause() Log.d(TAG, "glPause"); OFGLSurfaceView glView = getGLView(); - - glView.onPause(); - - ViewGroup parent = (ViewGroup)glView.getParent(); - - if(parent != null){ - Log.d(TAG, "remove surface"); - parent.removeView(glView); + if( glView != null ) + { + glView.onPause(); + + ViewGroup parent = (ViewGroup)glView.getParent(); + + if(parent != null){ + Log.d(TAG, "remove surface"); + parent.removeView(glView); + } + + pushState(State.pause); } - - pushState(State.pause); } public static void glDestroy() diff --git a/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroidWindow.java b/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroidWindow.java index 7af2657ea78..396d84de3c7 100644 --- a/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroidWindow.java +++ b/addons/ofxAndroid/ofAndroidLib/src/cc/openframeworks/OFAndroidWindow.java @@ -25,12 +25,14 @@ public OFEGLConfigChooser(int r, int g, int b, int a, int depth, int stencil) { } public static void setGLESVersion(int version){ + GLES_VERSION = version; + if(version==1) EGL_OPENGL_ES_BIT=1; else EGL_OPENGL_ES_BIT=4; } public static int getGLESVersion(){ - return EGL_OPENGL_ES_BIT==1?1:2; + return GLES_VERSION; } /* This EGL config specification is used to specify 1.x rendering. @@ -39,6 +41,7 @@ public static int getGLESVersion(){ */ private static boolean DEBUG = false; private static int EGL_OPENGL_ES_BIT = 1; + private static int GLES_VERSION = 1; private static int[] s_configAttribs2 = { EGL10.EGL_RED_SIZE, 4, @@ -220,8 +223,8 @@ class OFGLSurfaceView extends GLSurfaceView{ public OFGLSurfaceView(Context context) { super(context); mRenderer = new OFAndroidWindow(getWidth(),getHeight()); - if(OFEGLConfigChooser.getGLESVersion()==2){ - setEGLContextClientVersion(2); + if(OFEGLConfigChooser.getGLESVersion()>=2){ + setEGLContextClientVersion(OFEGLConfigChooser.getGLESVersion()); } getHolder().setFormat( PixelFormat.OPAQUE ); OFEGLConfigChooser configChooser = new OFEGLConfigChooser(8,8,8,0,16,0); @@ -233,8 +236,8 @@ public OFGLSurfaceView(Context context) { public OFGLSurfaceView(Context context,AttributeSet attributes) { super(context,attributes); mRenderer = new OFAndroidWindow(getWidth(),getHeight()); - if(OFEGLConfigChooser.getGLESVersion()==2){ - setEGLContextClientVersion(2); + if(OFEGLConfigChooser.getGLESVersion()>=2){ + setEGLContextClientVersion(OFEGLConfigChooser.getGLESVersion()); } getHolder().setFormat( PixelFormat.OPAQUE ); OFEGLConfigChooser configChooser = new OFEGLConfigChooser(8,8,8,0,16,0); diff --git a/addons/ofxAndroid/src/ofAppAndroidWindow.cpp b/addons/ofxAndroid/src/ofAppAndroidWindow.cpp index d7f03810493..e91ddd01c43 100644 --- a/addons/ofxAndroid/src/ofAppAndroidWindow.cpp +++ b/addons/ofxAndroid/src/ofAppAndroidWindow.cpp @@ -236,6 +236,11 @@ shared_ptr & ofAppAndroidWindow::renderer(){ return currentRenderer; } +int ofAppAndroidWindow::getGlesVersion() +{ + return glesVersion; +} + extern "C"{ jint JNI_OnLoad(JavaVM* vm, void* reserved) @@ -323,15 +328,15 @@ Java_cc_openframeworks_OFAndroid_onSurfaceCreated( JNIEnv* env, jclass thiz ){ window->renderer()->popStyle(); }else{ - - if(window->renderer()->getType()==ofGLProgrammableRenderer::TYPE) - { - static_cast(window->renderer().get())->setup(2,0); - } - else - { - static_cast(window->renderer().get())->setup(); - } + int glesVersion = window->getGlesVersion(); + if( glesVersion < 2 ) + { + static_cast(window->renderer().get())->setup(); + } + else + { + static_cast(window->renderer().get())->setup(glesVersion,0); + } } surfaceDestroyed = false; diff --git a/addons/ofxAndroid/src/ofAppAndroidWindow.h b/addons/ofxAndroid/src/ofAppAndroidWindow.h index 8bc86c82908..48ad3a1cd21 100644 --- a/addons/ofxAndroid/src/ofAppAndroidWindow.h +++ b/addons/ofxAndroid/src/ofAppAndroidWindow.h @@ -63,6 +63,8 @@ class ofAppAndroidWindow: public ofAppBaseGLESWindow { void setThreadedEvents(bool threadedEvents); void setAccumulateTouchEvents(bool accumEvents); + int getGlesVersion(); + private: ofCoreEvents coreEvents; std::shared_ptr currentRenderer; diff --git a/libs/openFrameworksCompiled/project/android/common-functions.gradle b/libs/openFrameworksCompiled/project/android/common-functions.gradle index 46786db7990..8b92e87687e 100755 --- a/libs/openFrameworksCompiled/project/android/common-functions.gradle +++ b/libs/openFrameworksCompiled/project/android/common-functions.gradle @@ -431,4 +431,4 @@ ext { javaDependencies = this.&javaDependencies -} \ No newline at end of file +}