diff --git a/engine/src/flutter/ci/licenses_golden/licenses_flutter b/engine/src/flutter/ci/licenses_golden/licenses_flutter index db2650aba0..b1018822b0 100755 --- a/engine/src/flutter/ci/licenses_golden/licenses_flutter +++ b/engine/src/flutter/ci/licenses_golden/licenses_flutter @@ -766,6 +766,7 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/plugin FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterUiDisplayListener.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/renderer/RenderSurface.java +FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/renderer/SurfaceTextureWrapper.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/AccessibilityChannel.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/KeyEventChannel.java FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/systemchannels/LifecycleChannel.java diff --git a/engine/src/flutter/shell/platform/android/BUILD.gn b/engine/src/flutter/shell/platform/android/BUILD.gn index 0c22c91301..5ccfd56d91 100644 --- a/engine/src/flutter/shell/platform/android/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/BUILD.gn @@ -182,6 +182,7 @@ android_java_sources = [ "io/flutter/embedding/engine/renderer/FlutterRenderer.java", "io/flutter/embedding/engine/renderer/FlutterUiDisplayListener.java", "io/flutter/embedding/engine/renderer/RenderSurface.java", + "io/flutter/embedding/engine/renderer/SurfaceTextureWrapper.java", "io/flutter/embedding/engine/systemchannels/AccessibilityChannel.java", "io/flutter/embedding/engine/systemchannels/KeyEventChannel.java", "io/flutter/embedding/engine/systemchannels/LifecycleChannel.java", diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java index 59fa7132e3..5e9864c345 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java @@ -23,6 +23,7 @@ import io.flutter.embedding.engine.dart.PlatformMessageHandler; import io.flutter.embedding.engine.mutatorsstack.FlutterMutatorsStack; import io.flutter.embedding.engine.renderer.FlutterUiDisplayListener; import io.flutter.embedding.engine.renderer.RenderSurface; +import io.flutter.embedding.engine.renderer.SurfaceTextureWrapper; import io.flutter.plugin.common.StandardMessageCodec; import io.flutter.plugin.localization.LocalizationPlugin; import io.flutter.plugin.platform.PlatformViewsController; @@ -581,14 +582,14 @@ public class FlutterJNI { * within Flutter's UI. */ @UiThread - public void registerTexture(long textureId, @NonNull SurfaceTexture surfaceTexture) { + public void registerTexture(long textureId, @NonNull SurfaceTextureWrapper textureWrapper) { ensureRunningOnMainThread(); ensureAttachedToNative(); - nativeRegisterTexture(nativePlatformViewId, textureId, surfaceTexture); + nativeRegisterTexture(nativePlatformViewId, textureId, textureWrapper); } private native void nativeRegisterTexture( - long nativePlatformViewId, long textureId, @NonNull SurfaceTexture surfaceTexture); + long nativePlatformViewId, long textureId, @NonNull SurfaceTextureWrapper textureWrapper); /** * Call this method to inform Flutter that a texture previously registered with {@link diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java index 0aa11c4f2a..20a72e81fa 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java @@ -99,18 +99,18 @@ public class FlutterRenderer implements TextureRegistry { final SurfaceTextureRegistryEntry entry = new SurfaceTextureRegistryEntry(nextTextureId.getAndIncrement(), surfaceTexture); Log.v(TAG, "New SurfaceTexture ID: " + entry.id()); - registerTexture(entry.id(), surfaceTexture); + registerTexture(entry.id(), entry.textureWrapper()); return entry; } final class SurfaceTextureRegistryEntry implements TextureRegistry.SurfaceTextureEntry { private final long id; - @NonNull private final SurfaceTexture surfaceTexture; + @NonNull private final SurfaceTextureWrapper textureWrapper; private boolean released; SurfaceTextureRegistryEntry(long id, @NonNull SurfaceTexture surfaceTexture) { this.id = id; - this.surfaceTexture = surfaceTexture; + this.textureWrapper = new SurfaceTextureWrapper(surfaceTexture); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // The callback relies on being executed on the UI thread (unsynchronised read of @@ -118,12 +118,12 @@ public class FlutterRenderer implements TextureRegistry { // and also the engine code check for platform thread in // Shell::OnPlatformViewMarkTextureFrameAvailable), // so we explicitly pass a Handler for the current thread. - this.surfaceTexture.setOnFrameAvailableListener(onFrameListener, new Handler()); + this.surfaceTexture().setOnFrameAvailableListener(onFrameListener, new Handler()); } else { // Android documentation states that the listener can be called on an arbitrary thread. // But in practice, versions of Android that predate the newer API will call the listener // on the thread where the SurfaceTexture was constructed. - this.surfaceTexture.setOnFrameAvailableListener(onFrameListener); + this.surfaceTexture().setOnFrameAvailableListener(onFrameListener); } } @@ -142,10 +142,15 @@ public class FlutterRenderer implements TextureRegistry { } }; + @NonNull + public SurfaceTextureWrapper textureWrapper() { + return textureWrapper; + } + @Override @NonNull public SurfaceTexture surfaceTexture() { - return surfaceTexture; + return textureWrapper.surfaceTexture(); } @Override @@ -159,7 +164,7 @@ public class FlutterRenderer implements TextureRegistry { return; } Log.v(TAG, "Releasing a SurfaceTexture (" + id + ")."); - surfaceTexture.release(); + textureWrapper.release(); unregisterTexture(id); released = true; } @@ -298,8 +303,8 @@ public class FlutterRenderer implements TextureRegistry { } // TODO(mattcarroll): describe the native behavior that this invokes - private void registerTexture(long textureId, @NonNull SurfaceTexture surfaceTexture) { - flutterJNI.registerTexture(textureId, surfaceTexture); + private void registerTexture(long textureId, @NonNull SurfaceTextureWrapper textureWrapper) { + flutterJNI.registerTexture(textureId, textureWrapper); } // TODO(mattcarroll): describe the native behavior that this invokes diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/renderer/SurfaceTextureWrapper.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/renderer/SurfaceTextureWrapper.java new file mode 100644 index 0000000000..478d997a5e --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/engine/renderer/SurfaceTextureWrapper.java @@ -0,0 +1,69 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package io.flutter.embedding.engine.renderer; + +import android.graphics.SurfaceTexture; +import androidx.annotation.Keep; +import androidx.annotation.NonNull; + +/** + * A wrapper for a SurfaceTexture that tracks whether the texture has been released. + * + *

The engine calls {@code SurfaceTexture.release} on the platform thread, but {@code + * updateTexImage} is called on the raster thread. This wrapper will prevent {@code updateTexImage} + * calls on an abandoned texture. + */ +@Keep +public class SurfaceTextureWrapper { + private SurfaceTexture surfaceTexture; + private boolean released; + + public SurfaceTextureWrapper(@NonNull SurfaceTexture surfaceTexture) { + this.surfaceTexture = surfaceTexture; + this.released = false; + } + + @NonNull + public SurfaceTexture surfaceTexture() { + return surfaceTexture; + } + + // Called by native. + @SuppressWarnings("unused") + public void updateTexImage() { + synchronized (this) { + if (!released) { + surfaceTexture.updateTexImage(); + } + } + } + + public void release() { + synchronized (this) { + if (!released) { + surfaceTexture.release(); + released = true; + } + } + } + + // Called by native. + @SuppressWarnings("unused") + public void attachToGLContext(int texName) { + surfaceTexture.attachToGLContext(texName); + } + + // Called by native. + @SuppressWarnings("unused") + public void detachFromGLContext() { + surfaceTexture.detachFromGLContext(); + } + + // Called by native. + @SuppressWarnings("unused") + public void getTransformMatrix(float[] mtx) { + surfaceTexture.getTransformMatrix(mtx); + } +} diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java index 50598cd734..b630ad242a 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java @@ -46,6 +46,7 @@ import io.flutter.embedding.android.AndroidKeyProcessor; import io.flutter.embedding.android.AndroidTouchProcessor; import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.embedding.engine.renderer.FlutterRenderer; +import io.flutter.embedding.engine.renderer.SurfaceTextureWrapper; import io.flutter.embedding.engine.systemchannels.AccessibilityChannel; import io.flutter.embedding.engine.systemchannels.KeyEventChannel; import io.flutter.embedding.engine.systemchannels.LifecycleChannel; @@ -876,18 +877,18 @@ public class FlutterView extends SurfaceView surfaceTexture.detachFromGLContext(); final SurfaceTextureRegistryEntry entry = new SurfaceTextureRegistryEntry(nextTextureId.getAndIncrement(), surfaceTexture); - mNativeView.getFlutterJNI().registerTexture(entry.id(), surfaceTexture); + mNativeView.getFlutterJNI().registerTexture(entry.id(), entry.textureWrapper()); return entry; } final class SurfaceTextureRegistryEntry implements TextureRegistry.SurfaceTextureEntry { private final long id; - private final SurfaceTexture surfaceTexture; + private final SurfaceTextureWrapper textureWrapper; private boolean released; SurfaceTextureRegistryEntry(long id, SurfaceTexture surfaceTexture) { this.id = id; - this.surfaceTexture = surfaceTexture; + this.textureWrapper = new SurfaceTextureWrapper(surfaceTexture); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // The callback relies on being executed on the UI thread (unsynchronised read of @@ -895,12 +896,12 @@ public class FlutterView extends SurfaceView // and also the engine code check for platform thread in // Shell::OnPlatformViewMarkTextureFrameAvailable), // so we explicitly pass a Handler for the current thread. - this.surfaceTexture.setOnFrameAvailableListener(onFrameListener, new Handler()); + this.surfaceTexture().setOnFrameAvailableListener(onFrameListener, new Handler()); } else { // Android documentation states that the listener can be called on an arbitrary thread. // But in practice, versions of Android that predate the newer API will call the listener // on the thread where the SurfaceTexture was constructed. - this.surfaceTexture.setOnFrameAvailableListener(onFrameListener); + this.surfaceTexture().setOnFrameAvailableListener(onFrameListener); } } @@ -921,9 +922,13 @@ public class FlutterView extends SurfaceView } }; + public SurfaceTextureWrapper textureWrapper() { + return textureWrapper; + } + @Override public SurfaceTexture surfaceTexture() { - return surfaceTexture; + return textureWrapper.surfaceTexture(); } @Override @@ -945,8 +950,8 @@ public class FlutterView extends SurfaceView // Otherwise onFrameAvailableListener might be called after mNativeView==null // (https://github.com/flutter/flutter/issues/20951). See also the check in onFrameAvailable. - surfaceTexture.setOnFrameAvailableListener(null); - surfaceTexture.release(); + surfaceTexture().setOnFrameAvailableListener(null); + textureWrapper.release(); mNativeView.getFlutterJNI().unregisterTexture(id); } } diff --git a/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc b/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc index 3b8a61bf39..b3b11dbb8e 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc @@ -53,7 +53,7 @@ static fml::jni::ScopedJavaGlobalRef* g_flutter_callback_info_class = static fml::jni::ScopedJavaGlobalRef* g_flutter_jni_class = nullptr; -static fml::jni::ScopedJavaGlobalRef* g_surface_texture_class = nullptr; +static fml::jni::ScopedJavaGlobalRef* g_texture_wrapper_class = nullptr; // Called By Native @@ -613,7 +613,8 @@ bool RegisterApi(JNIEnv* env) { }, { .name = "nativeRegisterTexture", - .signature = "(JJLandroid/graphics/SurfaceTexture;)V", + .signature = "(JJLio/flutter/embedding/engine/renderer/" + "SurfaceTextureWrapper;)V", .fnPtr = reinterpret_cast(&RegisterTexture), }, { @@ -857,15 +858,16 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { return false; } - g_surface_texture_class = new fml::jni::ScopedJavaGlobalRef( - env, env->FindClass("android/graphics/SurfaceTexture")); - if (g_surface_texture_class->is_null()) { - FML_LOG(ERROR) << "Could not locate SurfaceTexture class"; + g_texture_wrapper_class = new fml::jni::ScopedJavaGlobalRef( + env, env->FindClass( + "io/flutter/embedding/engine/renderer/SurfaceTextureWrapper")); + if (g_texture_wrapper_class->is_null()) { + FML_LOG(ERROR) << "Could not locate SurfaceTextureWrapper class"; return false; } g_attach_to_gl_context_method = env->GetMethodID( - g_surface_texture_class->obj(), "attachToGLContext", "(I)V"); + g_texture_wrapper_class->obj(), "attachToGLContext", "(I)V"); if (g_attach_to_gl_context_method == nullptr) { FML_LOG(ERROR) << "Could not locate attachToGlContext method"; @@ -873,7 +875,7 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { } g_update_tex_image_method = - env->GetMethodID(g_surface_texture_class->obj(), "updateTexImage", "()V"); + env->GetMethodID(g_texture_wrapper_class->obj(), "updateTexImage", "()V"); if (g_update_tex_image_method == nullptr) { FML_LOG(ERROR) << "Could not locate updateTexImage method"; @@ -881,7 +883,7 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { } g_get_transform_matrix_method = env->GetMethodID( - g_surface_texture_class->obj(), "getTransformMatrix", "([F)V"); + g_texture_wrapper_class->obj(), "getTransformMatrix", "([F)V"); if (g_get_transform_matrix_method == nullptr) { FML_LOG(ERROR) << "Could not locate getTransformMatrix method"; @@ -889,7 +891,7 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { } g_detach_from_gl_context_method = env->GetMethodID( - g_surface_texture_class->obj(), "detachFromGLContext", "()V"); + g_texture_wrapper_class->obj(), "detachFromGLContext", "()V"); if (g_detach_from_gl_context_method == nullptr) { FML_LOG(ERROR) << "Could not locate detachFromGlContext method"; diff --git a/engine/src/flutter/tools/android_lint/baseline.xml b/engine/src/flutter/tools/android_lint/baseline.xml index 39bdc950e5..0e47fcf9f8 100644 --- a/engine/src/flutter/tools/android_lint/baseline.xml +++ b/engine/src/flutter/tools/android_lint/baseline.xml @@ -188,4 +188,26 @@ column="24"/> + + + + + + + +