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 6a0f3686ec..1ab9699c3f 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 @@ -8,6 +8,7 @@ import android.annotation.TargetApi; import android.graphics.Bitmap; import android.graphics.Rect; import android.graphics.SurfaceTexture; +import android.hardware.SyncFence; import android.media.Image; import android.os.Build; import android.os.Handler; @@ -21,6 +22,7 @@ import io.flutter.embedding.engine.FlutterJNI; import io.flutter.embedding.engine.renderer.FlutterRenderer.ImageTextureRegistryEntry; import io.flutter.view.TextureRegistry; import io.flutter.view.TextureRegistry.ImageTextureEntry; +import java.io.IOException; import java.lang.ref.WeakReference; import java.nio.ByteBuffer; import java.util.ArrayList; @@ -223,15 +225,18 @@ public class FlutterRenderer implements TextureRegistry { this.textureWrapper = new SurfaceTextureWrapper(surfaceTexture, onFrameConsumed); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - // The callback relies on being executed on the UI thread (unsynchronised read of + // The callback relies on being executed on the UI thread (unsynchronised read + // of // mNativeView // 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()); } 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 + // 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); } @@ -371,24 +376,36 @@ public class FlutterRenderer implements TextureRegistry { synchronized (this) { toClose = this.image; this.image = image; + if (image != null) { + // Mark that we have a new frame available. + markTextureFrameAvailable(id); + } } // Close the previously pushed buffer. if (toClose != null) { toClose.close(); } - if (image != null) { - // Mark that we have a new frame available. - markTextureFrameAvailable(id); - } } @Override + @TargetApi(33) public Image acquireLatestImage() { Image r; synchronized (this) { r = this.image; this.image = null; } + if (r != null) { + try { + SyncFence fence = r.getFence(); + boolean signaled = fence.awaitForever(); + if (!signaled) { + Log.e(TAG, "acquireLatestImage failed to wait on image fence."); + } + } catch (IOException e) { + Log.e(TAG, "acquireLatestImage failed calling Image.getFence: " + e); + } + } return r; } @@ -479,9 +496,12 @@ public class FlutterRenderer implements TextureRegistry { if (surface != null) { flutterJNI.onSurfaceDestroyed(); - // TODO(mattcarroll): the source of truth for this call should be FlutterJNI, which is where - // the call to onFlutterUiDisplayed() comes from. However, no such native callback exists yet, - // so until the engine and FlutterJNI are configured to call us back when rendering stops, + // TODO(mattcarroll): the source of truth for this call should be FlutterJNI, + // which is where + // the call to onFlutterUiDisplayed() comes from. However, no such native + // callback exists yet, + // so until the engine and FlutterJNI are configured to call us back when + // rendering stops, // we will manually monitor that change here. if (isDisplayingFlutterUi) { flutterUiDisplayListener.onFlutterUiNoLongerDisplayed(); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/ImageReaderPlatformViewRenderTarget.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/ImageReaderPlatformViewRenderTarget.java index 4ba58fce13..60b2d73e70 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/ImageReaderPlatformViewRenderTarget.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/ImageReaderPlatformViewRenderTarget.java @@ -12,7 +12,7 @@ import android.view.Surface; import io.flutter.Log; import io.flutter.view.TextureRegistry.ImageTextureEntry; -@TargetApi(29) +@TargetApi(33) public class ImageReaderPlatformViewRenderTarget implements PlatformViewRenderTarget { private ImageTextureEntry textureEntry; private ImageReader reader; @@ -72,33 +72,18 @@ public class ImageReaderPlatformViewRenderTarget implements PlatformViewRenderTa return reader; } - @TargetApi(29) - protected ImageReader createImageReader29() { - final ImageReader reader = - ImageReader.newInstance( - bufferWidth, - bufferHeight, - ImageFormat.PRIVATE, - MAX_IMAGES, - HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE); - reader.setOnImageAvailableListener(this.onImageAvailableListener, onImageAvailableHandler); - return reader; - } - protected ImageReader createImageReader() { if (Build.VERSION.SDK_INT >= 33) { return createImageReader33(); - } else if (Build.VERSION.SDK_INT >= 29) { - return createImageReader29(); } throw new UnsupportedOperationException( - "ImageReaderPlatformViewRenderTarget requires API version 29+"); + "ImageReaderPlatformViewRenderTarget requires API version 33+"); } public ImageReaderPlatformViewRenderTarget(ImageTextureEntry textureEntry) { - if (Build.VERSION.SDK_INT < 29) { + if (Build.VERSION.SDK_INT < 33) { throw new UnsupportedOperationException( - "ImageReaderPlatformViewRenderTarget requires API version 29+"); + "ImageReaderPlatformViewRenderTarget requires API version 33+"); } this.textureEntry = textureEntry; } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java index e2e87b57c1..d5a1621d1e 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java @@ -974,7 +974,7 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega private static PlatformViewRenderTarget makePlatformViewRenderTarget( TextureRegistry textureRegistry) { - if (enableHardwareBufferRenderingTarget && Build.VERSION.SDK_INT >= 29) { + if (enableHardwareBufferRenderingTarget && Build.VERSION.SDK_INT >= 33) { final TextureRegistry.ImageTextureEntry textureEntry = textureRegistry.createImageTexture(); Log.i(TAG, "PlatformView is using ImageReader backend"); return new ImageReaderPlatformViewRenderTarget(textureEntry);