[vlc-devel] [PATCH 04/14] android: utils: load SurfaceTexture constructors

Thomas Guillem thomas at gllm.fr
Mon Jun 15 13:16:52 CEST 2020



On Fri, Jun 12, 2020, at 11:40, Alexandre Janniaux wrote:
> init_z needs API 26 and is the most suitable since it allows creating
> SurfaceTexture in a detached state, while init_iz needs API 19 and
> cannot be created without an EGL context currently bounded.
> 
> init_i is the most compatible version (API 11) but won't allow
> single-buffering.
> ---
>  modules/video_output/android/utils.c | 65 ++++++++++++++++++----------
>  1 file changed, 42 insertions(+), 23 deletions(-)
> 
> diff --git a/modules/video_output/android/utils.c 
> b/modules/video_output/android/utils.c
> index e4d407f560f..7818f1e5e7b 100644
> --- a/modules/video_output/android/utils.c
> +++ b/modules/video_output/android/utils.c
> @@ -129,7 +129,9 @@ static struct
>      } AWindow;
>      struct {
>            jclass clazz;
> -          jmethodID constructor;
> +          jmethodID init_i;
> +          jmethodID init_iz;
> +          jmethodID init_z;
>      } SurfaceTexture;
>  } jfields;
>  
> @@ -558,7 +560,7 @@ static const struct vlc_asurfacetexture_operations 
> JNISurfaceAPI =
>  
>  
>  static int
> -LoadNDKSurfaceTextureAPI(AWindowHandler *p_awh, void *p_library, 
> JNIEnv *p_env)
> +LoadNDKSurfaceTextureAPI(AWindowHandler *p_awh, void *p_library)
>  {
>      p_awh->ndk_ast_api.pf_astFromst = 
> (ptr_ASurfaceTexture_fromSurfaceTexture)
>          dlsym(p_library, "ASurfaceTexture_fromSurfaceTexture");
> @@ -592,25 +594,6 @@ LoadNDKSurfaceTextureAPI(AWindowHandler *p_awh, 
> void *p_library, JNIEnv *p_env)
>          dlsym(p_library, "ANativeWindow_toSurface");
>      if (p_awh->ndk_ast_api.pf_anwToSurface == NULL) return 
> VLC_EGENERIC;
>  
> -    jclass surfacetexture_class = (*p_env)->FindClass(p_env,
> -                                            "android/graphics/SurfaceTexture");
> -    if (!surfacetexture_class)
> -        return VLC_EGENERIC;
> -
> -    jfields.SurfaceTexture.clazz = (*p_env)->NewGlobalRef(p_env,
> -                                                         surfacetexture_class);
> -    (*p_env)->DeleteLocalRef(p_env, surfacetexture_class);
> -    if (!jfields.SurfaceTexture.clazz)
> -        return VLC_EGENERIC;
> -
> -    jfields.SurfaceTexture.constructor = (*p_env)->GetMethodID(p_env,
> -                               jfields.SurfaceTexture.clazz, "<init>", "(Z)V");
> -    if (!jfields.SurfaceTexture.constructor)
> -    {
> -        (*p_env)->DeleteGlobalRef(p_env, jfields.SurfaceTexture.clazz);
> -        return VLC_EGENERIC;
> -    }
> -
>      return VLC_SUCCESS;
>  }
>  
> @@ -638,7 +621,7 @@ LoadNativeWindowAPI(AWindowHandler *p_awh, JNIEnv 
> *p_env)
>       && p_awh->anw_api.winLock && p_awh->anw_api.unlockAndPost
>       && p_awh->anw_api.setBuffersGeometry)
>      {
> -        p_awh->b_has_ndk_ast_api = !LoadNDKSurfaceTextureAPI(p_awh, 
> p_library, p_env);
> +        p_awh->b_has_ndk_ast_api = !LoadNDKSurfaceTextureAPI(p_awh, 
> p_library);
>          p_awh->p_anw_dl = p_library;
>      }
>      else
> @@ -646,6 +629,38 @@ LoadNativeWindowAPI(AWindowHandler *p_awh, JNIEnv 
> *p_env)
>          dlclose(p_library);
>          LoadNativeSurfaceAPI(p_awh);
>      }
> +    jfields.SurfaceTexture.clazz = NULL;
> +
> +    jclass surfacetexture_class = (*p_env)->FindClass(p_env,
> +                                            
> "android/graphics/SurfaceTexture");
> +    if (surfacetexture_class == NULL)
> +        goto error;
> +
> +    jfields.SurfaceTexture.clazz = (*p_env)->NewGlobalRef(p_env,
> +                                                         
> surfacetexture_class);
> +    (*p_env)->DeleteLocalRef(p_env, surfacetexture_class);
> +    if (jfields.SurfaceTexture.clazz == NULL)
> +        goto error;
> +
> +    jfields.SurfaceTexture.init_i = (*p_env)->GetMethodID(p_env,
> +                               jfields.SurfaceTexture.clazz, "<init>", 
> "(I)V");
> +    jfields.SurfaceTexture.init_iz = (*p_env)->GetMethodID(p_env,
> +                               jfields.SurfaceTexture.clazz, "<init>", 
> "(IZ)V");
> +    jfields.SurfaceTexture.init_z = (*p_env)->GetMethodID(p_env,
> +                               jfields.SurfaceTexture.clazz, "<init>", 
> "(Z)V");

You need to check and clear the exception in case of error
cf. the GET_METHOD macro.

Otherwise, the app will abort on the next Java call.

> +
> +    /* We cannot create any SurfaceTexture if we cannot load the 
> SurfaceTexture
> +     * methods. */
> +    if (!jfields.SurfaceTexture.init_i &&
> +        !jfields.SurfaceTexture.init_iz &&
> +        !jfields.SurfaceTexture.init_z)
> +        goto error;
> +
> +    return;
> +
> +error:
> +    if (jfields.SurfaceTexture.clazz != NULL)
> +        (*p_env)->DeleteGlobalRef(p_env, jfields.SurfaceTexture.clazz);
>  }
>  
>  static void
> @@ -878,8 +893,12 @@ AWindowHandler_getANativeWindowAPI(AWindowHandler *p_awh)
>  static jobject InitNDKSurfaceTexture(AWindowHandler *p_awh, JNIEnv *p_env,
>          enum AWindow_ID id)
>  {
> +    /* This API should be available if using NDK SurfaceTexture API */
> +    if (!jfields.SurfaceTexture.init_z)
> +        return NULL;
> +
>      jobject surfacetexture = (*p_env)->NewObject(p_env,
> -      jfields.SurfaceTexture.clazz, jfields.SurfaceTexture.constructor, false);
> +      jfields.SurfaceTexture.clazz, jfields.SurfaceTexture.init_z, false);
>  
>      if (surfacetexture == NULL)
>          goto error;
> -- 
> 2.27.0
> 
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel


More information about the vlc-devel mailing list