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 29443aeb63..5853689fc2 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 @@ -1,7 +1,6 @@ package io.flutter.plugin.platform; import android.annotation.TargetApi; -import android.graphics.Canvas; import android.graphics.ImageFormat; import android.hardware.HardwareBuffer; import android.media.Image; @@ -122,14 +121,6 @@ public class ImageReaderPlatformViewRenderTarget implements PlatformViewRenderTa return this.bufferHeight; } - public Canvas lockHardwareCanvas() { - return getSurface().lockHardwareCanvas(); - } - - public void unlockCanvasAndPost(Canvas canvas) { - getSurface().unlockCanvasAndPost(canvas); - } - public long getId() { return this.textureEntry.id(); } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewRenderTarget.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewRenderTarget.java index da4d2b54bf..d04240374c 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewRenderTarget.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewRenderTarget.java @@ -4,7 +4,6 @@ package io.flutter.plugin.platform; -import android.graphics.Canvas; import android.view.Surface; /** @@ -22,15 +21,6 @@ public interface PlatformViewRenderTarget { // Returns the currently specified height. public int getHeight(); - // Forwards call to Surface returned by getSurface. - // NOTE: If this returns null the RenderTarget is "full" and has no room for a - // new frame. - Canvas lockHardwareCanvas(); - - // Forwards call to Surface returned by getSurface. - // NOTE: Must be called if lockHardwareCanvas returns a non-null Canvas. - void unlockCanvasAndPost(Canvas canvas); - // The id of this render target. public long getId(); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java index aca60f4ca0..9ffb59877e 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java @@ -13,6 +13,7 @@ import android.graphics.Matrix; import android.graphics.PorterDuff; import android.graphics.Rect; import android.view.MotionEvent; +import android.view.Surface; import android.view.View; import android.view.ViewParent; import android.view.ViewTreeObserver; @@ -151,7 +152,8 @@ public class PlatformViewWrapper extends FrameLayout { Log.e(TAG, "Platform view cannot be composed without a RenderTarget."); return; } - final Canvas targetCanvas = renderTarget.lockHardwareCanvas(); + final Surface targetSurface = renderTarget.getSurface(); + final Canvas targetCanvas = targetSurface.lockHardwareCanvas(); if (targetCanvas == null) { // Cannot render right now. invalidate(); @@ -165,7 +167,7 @@ public class PlatformViewWrapper extends FrameLayout { // Override the canvas that this subtree of views will use to draw. super.draw(targetCanvas); } finally { - renderTarget.unlockCanvasAndPost(targetCanvas); + targetSurface.unlockCanvasAndPost(targetCanvas); } } 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 412b2889e5..0ce64f0c9c 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 @@ -968,12 +968,12 @@ public class PlatformViewsController implements PlatformViewsAccessibilityDelega private static PlatformViewRenderTarget makePlatformViewRenderTarget( TextureRegistry textureRegistry) { - if (enableSurfaceProducerRenderTarget && Build.VERSION.SDK_INT >= 33) { + if (enableSurfaceProducerRenderTarget && Build.VERSION.SDK_INT >= 29) { final TextureRegistry.SurfaceProducer textureEntry = textureRegistry.createSurfaceProducer(); Log.i(TAG, "PlatformView is using SurfaceProducer backend"); return new SurfaceProducerPlatformViewRenderTarget(textureEntry); } - if (enableImageRenderTarget && Build.VERSION.SDK_INT >= 33) { + if (enableImageRenderTarget && Build.VERSION.SDK_INT >= 29) { final TextureRegistry.ImageTextureEntry textureEntry = textureRegistry.createImageTexture(); Log.i(TAG, "PlatformView is using ImageReader backend"); return new ImageReaderPlatformViewRenderTarget(textureEntry); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SurfaceProducerPlatformViewRenderTarget.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SurfaceProducerPlatformViewRenderTarget.java index daf125f64a..430bad3c89 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SurfaceProducerPlatformViewRenderTarget.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SurfaceProducerPlatformViewRenderTarget.java @@ -1,7 +1,6 @@ package io.flutter.plugin.platform; import android.annotation.TargetApi; -import android.graphics.Canvas; import android.view.Surface; import io.flutter.view.TextureRegistry.SurfaceProducer; @@ -29,21 +28,6 @@ public class SurfaceProducerPlatformViewRenderTarget implements PlatformViewRend return this.producer.getHeight(); } - // Forwards call to Surface returned by getSurface. - // NOTE: If this returns null the RenderTarget is "full" and has no room for a - // new frame. - public Canvas lockHardwareCanvas() { - Surface surface = this.producer.getSurface(); - return surface.lockHardwareCanvas(); - } - - // Forwards call to Surface returned by getSurface. - // NOTE: Must be called if lockHardwareCanvas returns a non-null Canvas. - public void unlockCanvasAndPost(Canvas canvas) { - Surface surface = this.producer.getSurface(); - surface.unlockCanvasAndPost(canvas); - } - // The id of this render target. public long getId() { return this.producer.id(); diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SurfaceTexturePlatformViewRenderTarget.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SurfaceTexturePlatformViewRenderTarget.java index 1c36102611..4b474b0b01 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SurfaceTexturePlatformViewRenderTarget.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/SurfaceTexturePlatformViewRenderTarget.java @@ -3,27 +3,16 @@ package io.flutter.plugin.platform; import static android.content.ComponentCallbacks2.TRIM_MEMORY_COMPLETE; import android.annotation.TargetApi; -import android.graphics.Canvas; import android.graphics.SurfaceTexture; import android.os.Build; import android.view.Surface; -import io.flutter.Log; import io.flutter.view.TextureRegistry; import io.flutter.view.TextureRegistry.SurfaceTextureEntry; -import java.util.concurrent.atomic.AtomicLong; @TargetApi(26) public class SurfaceTexturePlatformViewRenderTarget implements PlatformViewRenderTarget { private static final String TAG = "SurfaceTexturePlatformViewRenderTarget"; - private final AtomicLong pendingFramesCount = new AtomicLong(0L); - - private void onFrameProduced() { - if (Build.VERSION.SDK_INT == 29) { - pendingFramesCount.incrementAndGet(); - } - } - private final SurfaceTextureEntry surfaceTextureEntry; private SurfaceTexture surfaceTexture; @@ -31,16 +20,6 @@ public class SurfaceTexturePlatformViewRenderTarget implements PlatformViewRende private int bufferWidth = 0; private int bufferHeight = 0; - private final TextureRegistry.OnFrameConsumedListener frameConsumedListener = - new TextureRegistry.OnFrameConsumedListener() { - @Override - public void onFrameConsumed() { - if (Build.VERSION.SDK_INT == 29) { - pendingFramesCount.decrementAndGet(); - } - } - }; - private boolean shouldRecreateSurfaceForLowMemory = false; private final TextureRegistry.OnTrimMemoryListener trimMemoryListener = new TextureRegistry.OnTrimMemoryListener() { @@ -63,6 +42,7 @@ public class SurfaceTexturePlatformViewRenderTarget implements PlatformViewRende private void recreateSurfaceIfNeeded() { if (surface != null && !shouldRecreateSurfaceForLowMemory) { + // No need to recreate the surface. return; } if (surface != null) { @@ -86,35 +66,9 @@ public class SurfaceTexturePlatformViewRenderTarget implements PlatformViewRende } this.surfaceTextureEntry = surfaceTextureEntry; this.surfaceTexture = surfaceTextureEntry.surfaceTexture(); - surfaceTextureEntry.setOnFrameConsumedListener(frameConsumedListener); surfaceTextureEntry.setOnTrimMemoryListener(trimMemoryListener); } - public Canvas lockHardwareCanvas() { - recreateSurfaceIfNeeded(); - - // We've observed on Android Q that we have to wait for the consumer of {@link - // SurfaceTexture} - // to consume the last image before continuing to draw, otherwise subsequent - // calls to - // {@code dequeueBuffer} to request a free buffer from the {@link BufferQueue} - // will fail. - // See https://github.com/flutter/flutter/issues/98722 - if (Build.VERSION.SDK_INT == 29 && pendingFramesCount.get() > 0L) { - return null; - } - if (surfaceTexture == null || surfaceTexture.isReleased()) { - Log.e(TAG, "Invalid RenderTarget: null or already released SurfaceTexture"); - return null; - } - onFrameProduced(); - return surface.lockHardwareCanvas(); - } - - public void unlockCanvasAndPost(Canvas canvas) { - surface.unlockCanvasAndPost(canvas); - } - public void resize(int width, int height) { bufferWidth = width; bufferHeight = height; @@ -150,6 +104,9 @@ public class SurfaceTexturePlatformViewRenderTarget implements PlatformViewRende public Surface getSurface() { recreateSurfaceIfNeeded(); + if (surfaceTexture == null || surfaceTexture.isReleased()) { + return null; + } return surface; } } diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/ImageReaderPlatformViewRenderTargetTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/ImageReaderPlatformViewRenderTargetTest.java index 64a0fd1e13..5f73328969 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/ImageReaderPlatformViewRenderTargetTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/ImageReaderPlatformViewRenderTargetTest.java @@ -16,6 +16,7 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.PorterDuff; import android.media.Image; +import android.view.Surface; import android.view.View; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; @@ -78,7 +79,9 @@ public class ImageReaderPlatformViewRenderTargetTest { assertNull(textureEntry.acquireLatestImage()); // Start rendering a frame. - final Canvas targetCanvas = renderTarget.lockHardwareCanvas(); + final Surface s = renderTarget.getSurface(); + assertNotNull(s); + final Canvas targetCanvas = s.lockHardwareCanvas(); assertNotNull(targetCanvas); try { @@ -89,7 +92,7 @@ public class ImageReaderPlatformViewRenderTargetTest { platformView.draw(targetCanvas); } finally { // Finish rendering a frame. - renderTarget.unlockCanvasAndPost(targetCanvas); + s.unlockCanvasAndPost(targetCanvas); } // Pump the UI thread task loop. This is needed so that the OnImageAvailable callback diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/SurfaceTexturePlatformViewRenderTargetTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/SurfaceTexturePlatformViewRenderTargetTest.java index f77302a602..2f086f6f73 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/SurfaceTexturePlatformViewRenderTargetTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/platform/SurfaceTexturePlatformViewRenderTargetTest.java @@ -59,9 +59,10 @@ public class SurfaceTexturePlatformViewRenderTargetTest { platformView.layout(0, 0, size, size); // Test. - final Canvas c = renderTarget.lockHardwareCanvas(); + final Surface s = renderTarget.getSurface(); + final Canvas c = s.lockHardwareCanvas(); platformView.draw(c); - renderTarget.unlockCanvasAndPost(c); + s.unlockCanvasAndPost(c); // Verify. verify(canvas, times(1)).drawColor(Color.RED); @@ -88,8 +89,9 @@ public class SurfaceTexturePlatformViewRenderTargetTest { } }; - final Canvas c = renderTarget.lockHardwareCanvas(); - renderTarget.unlockCanvasAndPost(c); + final Surface s = renderTarget.getSurface(); + final Canvas c = s.lockHardwareCanvas(); + s.unlockCanvasAndPost(c); reset(surface); reset(surfaceTexture);