[vlc-devel] [PATCH 5/5] android: util: use NDK ASurfaceTexture API if available

Louis Regnier louis.videolabs at gmail.com
Mon Apr 27 23:28:55 CEST 2020


If the NDK API requierements are met, native functions for ASurfaceTexture are
used instead of their JNI counterpart. The behavior change is made through the
SurfaceTextureHandler pointer structure.

Then, in all cases, wrap pointers of SurfaceTextureHandler in a common functions
used by OpenGL to attach / update / detach textures.

fixes #20344
---
 modules/video_output/android/utils.c          | 59 +++++++++++++++----
 modules/video_output/android/utils.h          |  5 +-
 modules/video_output/opengl/interop_android.c |  2 +-
 3 files changed, 49 insertions(+), 17 deletions(-)

diff --git a/modules/video_output/android/utils.c b/modules/video_output/android/utils.c
index cb190a3c53..570f8fe38c 100644
--- a/modules/video_output/android/utils.c
+++ b/modules/video_output/android/utils.c
@@ -332,6 +332,14 @@ ASurfaceTexture_updateTexImage(AWindowHandler *p_awh, const float **pp_transform
 static void
 ASurfaceTexture_detachFromGLContext(AWindowHandler *p_awh);
 
+static int
+JNISurfaceTexture_waitAndUpdateTexImage(AWindowHandler *p_awh,
+                                            const float **pp_transform_mtx);
+static void
+JNISurfaceTexture_detachFromGLContext(AWindowHandler *p_awh);
+static int
+JNISurfaceTexture_attachToGLContext(AWindowHandler *p_awh, uint32_t tex_name);
+
 static int
 LoadSurfaceTextureAPI(AWindowHandler *p_awh, void *p_library)
 {
@@ -394,12 +402,19 @@ LoadNativeWindowAPI(AWindowHandler *p_awh, JNIEnv *p_env)
      && p_awh->anw_api.winLock && p_awh->anw_api.unlockAndPost
      && p_awh->anw_api.setBuffersGeometry)
     {
-        if (!LoadSurfaceTextureAPI(p_awh, p_library))
-            JNI_getSurfaceTexture(p_env);
-        p_awh->b_has_ast_api = false;
-        p_awh->st.pf_attachToGL = ASurfaceTexture_attachToGLContext;
-        p_awh->st.pf_updateTexImage = ASurfaceTexture_updateTexImage;
-        p_awh->st.pf_detachFromGL = ASurfaceTexture_detachFromGLContext;
+        p_awh->b_has_ast_api = !LoadSurfaceTextureAPI(p_awh, p_library) && !JNI_getSurfaceTexture(p_env);
+        if (p_awh->b_has_ast_api)
+        {
+            p_awh->st.pf_attachToGL = ASurfaceTexture_attachToGLContext;
+            p_awh->st.pf_updateTexImage = ASurfaceTexture_updateTexImage;
+            p_awh->st.pf_detachFromGL = ASurfaceTexture_detachFromGLContext;
+        }
+        else
+        {
+            p_awh->st.pf_attachToGL = JNISurfaceTexture_attachToGLContext;
+            p_awh->st.pf_updateTexImage = JNISurfaceTexture_waitAndUpdateTexImage;
+            p_awh->st.pf_detachFromGL = JNISurfaceTexture_detachFromGLContext;
+        }
 
         p_awh->p_anw_dl = p_library;
     }
@@ -900,8 +915,8 @@ ASurfaceTexture_attachToGLContext(AWindowHandler *p_awh, uint32_t texName)
     return p_awh->ast_api.pf_attachToGL(p_awh->ast_api.p_ast, texName);
 }
 
-int
-SurfaceTexture_attachToGLContext(AWindowHandler *p_awh, int tex_name)
+static int
+JNISurfaceTexture_attachToGLContext(AWindowHandler *p_awh, uint32_t tex_name)
 {
     JNIEnv *p_env = android_getEnvCommon(NULL, p_awh->p_jvm, "SurfaceTexture");
     if (!p_env)
@@ -911,14 +926,20 @@ SurfaceTexture_attachToGLContext(AWindowHandler *p_awh, int tex_name)
            VLC_SUCCESS : VLC_EGENERIC;
 }
 
+int
+SurfaceTexture_attachToGLContext(AWindowHandler *p_awh, uint32_t tex_name)
+{
+    return p_awh->st.pf_attachToGL(p_awh, tex_name);
+}
+
 static void
 ASurfaceTexture_detachFromGLContext(AWindowHandler *p_awh)
 {
     p_awh->ast_api.pf_detachFromGL(p_awh->ast_api.p_ast);
 }
 
-void
-SurfaceTexture_detachFromGLContext(AWindowHandler *p_awh)
+static void
+JNISurfaceTexture_detachFromGLContext(AWindowHandler *p_awh)
 {
     JNIEnv *p_env = android_getEnvCommon(NULL, p_awh->p_jvm, "SurfaceTexture");
     if (!p_env)
@@ -937,6 +958,12 @@ SurfaceTexture_detachFromGLContext(AWindowHandler *p_awh)
     }
 }
 
+void
+SurfaceTexture_detachFromGLContext(AWindowHandler *p_awh)
+{
+    p_awh->st.pf_detachFromGL(p_awh);
+}
+
 static int
 ASurfaceTexture_updateTexImage(AWindowHandler *p_awh, const float **pp_transform_mtx)
 {
@@ -948,9 +975,9 @@ ASurfaceTexture_updateTexImage(AWindowHandler *p_awh, const float **pp_transform
     return VLC_SUCCESS;
 }
 
-int
-SurfaceTexture_waitAndUpdateTexImage(AWindowHandler *p_awh,
-                                     const float **pp_transform_mtx)
+static int
+JNISurfaceTexture_waitAndUpdateTexImage(AWindowHandler *p_awh,
+                                            const float **pp_transform_mtx)
 {
     JNIEnv *p_env = android_getEnvCommon(NULL, p_awh->p_jvm, "SurfaceTexture");
     if (!p_env)
@@ -976,3 +1003,9 @@ SurfaceTexture_waitAndUpdateTexImage(AWindowHandler *p_awh,
         return VLC_EGENERIC;
     }
 }
+
+int
+SurfaceTexture_updateTexImage(AWindowHandler *p_awh, const float **pp_transform_mtx)
+{
+    return p_awh->st.pf_updateTexImage(p_awh, pp_transform_mtx);
+}
diff --git a/modules/video_output/android/utils.h b/modules/video_output/android/utils.h
index 28aed6c6a1..086819dc1a 100644
--- a/modules/video_output/android/utils.h
+++ b/modules/video_output/android/utils.h
@@ -153,7 +153,7 @@ int AWindowHandler_setVideoLayout(AWindowHandler *p_awh,
  * \return 0 on success, -1 on error.
  */
 int
-SurfaceTexture_attachToGLContext(AWindowHandler *p_awh, int tex_name);
+SurfaceTexture_attachToGLContext(AWindowHandler *p_awh, uint32_t tex_name);
 
 /**
  * Detach a SurfaceTexture from the OpenGL ES context that owns the OpenGL ES
@@ -198,5 +198,4 @@ SurfaceTexture_getANativeWindow(AWindowHandler *p_awh)
  * \return VLC_SUCCESS or a VLC error
  */
 int
-SurfaceTexture_waitAndUpdateTexImage(AWindowHandler *p_awh,
-                                     const float **pp_transform_mtx);
+SurfaceTexture_updateTexImage(AWindowHandler *p_awh, const float **pp_transform_mtx);
diff --git a/modules/video_output/opengl/interop_android.c b/modules/video_output/opengl/interop_android.c
index f62043944a..29fef17fce 100644
--- a/modules/video_output/opengl/interop_android.c
+++ b/modules/video_output/opengl/interop_android.c
@@ -72,7 +72,7 @@ tc_anop_update(const struct vlc_gl_interop *interop, GLuint *textures,
     if (!priv->avctx->render(pic->context))
         return VLC_SUCCESS; /* already rendered */
 
-    if (SurfaceTexture_waitAndUpdateTexImage(priv->awh, &priv->transform_mtx)
+    if (SurfaceTexture_updateTexImage(priv->awh, &priv->transform_mtx)
         != VLC_SUCCESS)
     {
         priv->transform_mtx = NULL;
-- 
2.26.2



More information about the vlc-devel mailing list