@@ -42,6 +42,9 @@ static fml::jni::ScopedJavaGlobalRef<jclass>* g_flutter_callback_info_class =
4242
4343static fml::jni::ScopedJavaGlobalRef<jclass>* g_flutter_jni_class = nullptr ;
4444
45+ static fml::jni::ScopedJavaGlobalRef<jclass>* g_java_weak_reference_class =
46+ nullptr ;
47+
4548static fml::jni::ScopedJavaGlobalRef<jclass>* g_texture_wrapper_class = nullptr ;
4649
4750static fml::jni::ScopedJavaGlobalRef<jclass>* g_java_long_class = nullptr ;
@@ -87,6 +90,8 @@ static jmethodID g_on_begin_frame_method = nullptr;
8790
8891static jmethodID g_on_end_frame_method = nullptr ;
8992
93+ static jmethodID g_java_weak_reference_get_method = nullptr ;
94+
9095static jmethodID g_attach_to_gl_context_method = nullptr ;
9196
9297static jmethodID g_update_tex_image_method = nullptr ;
@@ -452,8 +457,8 @@ static void RegisterTexture(JNIEnv* env,
452457 jlong texture_id,
453458 jobject surface_texture) {
454459 ANDROID_SHELL_HOLDER->GetPlatformView ()->RegisterExternalTexture (
455- static_cast <int64_t >(texture_id), //
456- fml::jni::JavaObjectWeakGlobalRef (env, surface_texture) //
460+ static_cast <int64_t >(texture_id), //
461+ fml::jni::ScopedJavaGlobalRef<jobject> (env, surface_texture) //
457462 );
458463}
459464
@@ -714,8 +719,8 @@ bool RegisterApi(JNIEnv* env) {
714719 },
715720 {
716721 .name = " nativeRegisterTexture" ,
717- .signature = " (JJLio/flutter/embedding/engine/renderer /"
718- " SurfaceTextureWrapper ;)V" ,
722+ .signature = " (JJLjava/lang/ref /"
723+ " WeakReference ;)V" ,
719724 .fnPtr = reinterpret_cast <void *>(&RegisterTexture),
720725 },
721726 {
@@ -1007,6 +1012,20 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
10071012 return false ;
10081013 }
10091014
1015+ g_java_weak_reference_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
1016+ env, env->FindClass (" java/lang/ref/WeakReference" ));
1017+ if (g_java_weak_reference_class->is_null ()) {
1018+ FML_LOG (ERROR) << " Could not locate WeakReference class" ;
1019+ return false ;
1020+ }
1021+
1022+ g_java_weak_reference_get_method = env->GetMethodID (
1023+ g_java_weak_reference_class->obj (), " get" , " ()Ljava/lang/Object;" );
1024+ if (g_java_weak_reference_get_method == nullptr ) {
1025+ FML_LOG (ERROR) << " Could not locate WeakReference.get method" ;
1026+ return false ;
1027+ }
1028+
10101029 g_texture_wrapper_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
10111030 env, env->FindClass (
10121031 " io/flutter/embedding/engine/renderer/SurfaceTextureWrapper" ));
@@ -1215,12 +1234,18 @@ void PlatformViewAndroidJNIImpl::FlutterViewOnPreEngineRestart() {
12151234}
12161235
12171236void PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext (
1218- JavaWeakGlobalRef surface_texture,
1237+ JavaLocalRef surface_texture,
12191238 int textureId) {
12201239 JNIEnv* env = fml::jni::AttachCurrentThread ();
12211240
1222- fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
1223- surface_texture.get (env);
1241+ if (surface_texture.is_null ()) {
1242+ return ;
1243+ }
1244+
1245+ fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref (
1246+ env, env->CallObjectMethod (surface_texture.obj (),
1247+ g_java_weak_reference_get_method));
1248+
12241249 if (surface_texture_local_ref.is_null ()) {
12251250 return ;
12261251 }
@@ -1232,11 +1257,16 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext(
12321257}
12331258
12341259void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage (
1235- JavaWeakGlobalRef surface_texture) {
1260+ JavaLocalRef surface_texture) {
12361261 JNIEnv* env = fml::jni::AttachCurrentThread ();
12371262
1238- fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
1239- surface_texture.get (env);
1263+ if (surface_texture.is_null ()) {
1264+ return ;
1265+ }
1266+
1267+ fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref (
1268+ env, env->CallObjectMethod (surface_texture.obj (),
1269+ g_java_weak_reference_get_method));
12401270 if (surface_texture_local_ref.is_null ()) {
12411271 return ;
12421272 }
@@ -1260,12 +1290,17 @@ SkSize ScaleToFill(float scaleX, float scaleY) {
12601290}
12611291
12621292void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix (
1263- JavaWeakGlobalRef surface_texture,
1293+ JavaLocalRef surface_texture,
12641294 SkMatrix& transform) {
12651295 JNIEnv* env = fml::jni::AttachCurrentThread ();
12661296
1267- fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
1268- surface_texture.get (env);
1297+ if (surface_texture.is_null ()) {
1298+ return ;
1299+ }
1300+
1301+ fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref (
1302+ env, env->CallObjectMethod (surface_texture.obj (),
1303+ g_java_weak_reference_get_method));
12691304 if (surface_texture_local_ref.is_null ()) {
12701305 return ;
12711306 }
@@ -1290,11 +1325,16 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(
12901325}
12911326
12921327void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext (
1293- JavaWeakGlobalRef surface_texture) {
1328+ JavaLocalRef surface_texture) {
12941329 JNIEnv* env = fml::jni::AttachCurrentThread ();
12951330
1296- fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
1297- surface_texture.get (env);
1331+ if (surface_texture.is_null ()) {
1332+ return ;
1333+ }
1334+
1335+ fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref (
1336+ env, env->CallObjectMethod (surface_texture.obj (),
1337+ g_java_weak_reference_get_method));
12981338 if (surface_texture_local_ref.is_null ()) {
12991339 return ;
13001340 }
0 commit comments