Prevent a race between SurfaceTexture.release and updateTexImage (flutter/engine#21777)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
* <p>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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ static fml::jni::ScopedJavaGlobalRef<jclass>* g_flutter_callback_info_class =
|
||||
|
||||
static fml::jni::ScopedJavaGlobalRef<jclass>* g_flutter_jni_class = nullptr;
|
||||
|
||||
static fml::jni::ScopedJavaGlobalRef<jclass>* g_surface_texture_class = nullptr;
|
||||
static fml::jni::ScopedJavaGlobalRef<jclass>* 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<void*>(&RegisterTexture),
|
||||
},
|
||||
{
|
||||
@@ -857,15 +858,16 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
|
||||
return false;
|
||||
}
|
||||
|
||||
g_surface_texture_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
|
||||
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<jclass>(
|
||||
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";
|
||||
|
||||
@@ -188,4 +188,26 @@
|
||||
column="24"/>
|
||||
</issue>
|
||||
|
||||
<issue
|
||||
id="Recycle"
|
||||
message="This `SurfaceTexture` should be freed up after use with `#release()`"
|
||||
errorLine1=" final SurfaceTexture surfaceTexture = new SurfaceTexture(0);"
|
||||
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~">
|
||||
<location
|
||||
file="../../../flutter/shell/platform/android/io/flutter/embedding/engine/renderer/FlutterRenderer.java"
|
||||
line="97"
|
||||
column="43"/>
|
||||
</issue>
|
||||
|
||||
<issue
|
||||
id="Recycle"
|
||||
message="This `SurfaceTexture` should be freed up after use with `#release()`"
|
||||
errorLine1=" final SurfaceTexture surfaceTexture = new SurfaceTexture(0);"
|
||||
errorLine2=" ~~~~~~~~~~~~~~~~~~~~~">
|
||||
<location
|
||||
file="../../../flutter/shell/platform/android/io/flutter/view/FlutterView.java"
|
||||
line="876"
|
||||
column="43"/>
|
||||
</issue>
|
||||
|
||||
</issues>
|
||||
|
||||
Reference in New Issue
Block a user