Simplify PlatformViewRenderTarget interface (flutter/engine#49639)

- Enable ImageReader/SurfaceProducer backends for Android >= 29.
- This removes the need for a weird Android 29-specific fix in
SurfaceTexturePlatformViewRenderTarget.
- Now that we don't need the weird fix we can simplify the
PlatformViewRenderTarget interface.
This commit is contained in:
John McCutchan
2024-01-09 10:30:39 -08:00
committed by GitHub
parent 7e5be21b65
commit 53769525bd
8 changed files with 21 additions and 92 deletions

View File

@@ -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();
}

View File

@@ -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();

View File

@@ -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);
}
}

View File

@@ -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);

View File

@@ -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();

View File

@@ -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;
}
}

View File

@@ -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

View File

@@ -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);