From d7a1a768a431b9c3afeafaa92d3c7b02d2ae57e5 Mon Sep 17 00:00:00 2001 From: Dwayne Slater Date: Tue, 21 Jun 2022 10:11:06 -0700 Subject: [PATCH] Re-Enable ExternalTexturesTest (#34118) Use ImageFormat.PRIVATE since produced images are not read. Hint to downstream surface producers that the image will be sampled from on the GPU. --- .../scenariosui/ExternalTextureTests.java | 18 ++++++------- .../ExternalTextureFlutterActivity.java | 27 +++++++++++++++---- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/ExternalTextureTests.java b/testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/ExternalTextureTests.java index 68d5f37f36d09..37d96fa3e261f 100644 --- a/testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/ExternalTextureTests.java +++ b/testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/ExternalTextureTests.java @@ -14,14 +14,12 @@ import androidx.test.runner.AndroidJUnit4; import dev.flutter.scenarios.ExternalTextureFlutterActivity; import org.junit.Before; -import org.junit.Ignore; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(AndroidJUnit4.class) @LargeTest -@Ignore("Test fails with java.lang.UnsupportedOperationException") public class ExternalTextureTests { private static final int SURFACE_WIDTH = 192; private static final int SURFACE_HEIGHT = 256; @@ -42,7 +40,7 @@ public void setUp() { @Test public void testCanvasSurface() throws Exception { - intent.putExtra("scenario", "display_texture"); + intent.putExtra("scenario_name", "display_texture"); intent.putExtra("surface_renderer", "canvas"); ScreenshotUtil.capture( activityRule.launchActivity(intent), "ExternalTextureTests_testCanvasSurface"); @@ -51,7 +49,7 @@ public void testCanvasSurface() throws Exception { @Test @SdkSuppress(minSdkVersion = VERSION_CODES.LOLLIPOP) public void testMediaSurface() throws Exception { - intent.putExtra("scenario", "display_texture"); + intent.putExtra("scenario_name", "display_texture"); intent.putExtra("surface_renderer", "media"); ScreenshotUtil.capture( activityRule.launchActivity(intent), "ExternalTextureTests_testMediaSurface"); @@ -60,7 +58,7 @@ public void testMediaSurface() throws Exception { @Test @SdkSuppress(minSdkVersion = VERSION_CODES.LOLLIPOP) public void testRotatedMediaSurface_90() throws Exception { - intent.putExtra("scenario", "display_texture"); + intent.putExtra("scenario_name", "display_texture"); intent.putExtra("surface_renderer", "media"); intent.putExtra("rotation", 90); ScreenshotUtil.capture( @@ -70,7 +68,7 @@ public void testRotatedMediaSurface_90() throws Exception { @Test @SdkSuppress(minSdkVersion = VERSION_CODES.LOLLIPOP) public void testRotatedMediaSurface_180() throws Exception { - intent.putExtra("scenario", "display_texture"); + intent.putExtra("scenario_name", "display_texture"); intent.putExtra("surface_renderer", "media"); intent.putExtra("rotation", 180); ScreenshotUtil.capture( @@ -80,7 +78,7 @@ public void testRotatedMediaSurface_180() throws Exception { @Test @SdkSuppress(minSdkVersion = VERSION_CODES.LOLLIPOP) public void testRotatedMediaSurface_270() throws Exception { - intent.putExtra("scenario", "display_texture"); + intent.putExtra("scenario_name", "display_texture"); intent.putExtra("surface_renderer", "media"); intent.putExtra("rotation", 270); ScreenshotUtil.capture( @@ -90,7 +88,7 @@ public void testRotatedMediaSurface_270() throws Exception { @Test @SdkSuppress(minSdkVersion = VERSION_CODES.M) public void testCroppedMediaSurface_bottomLeft() throws Exception { - intent.putExtra("scenario", "display_texture"); + intent.putExtra("scenario_name", "display_texture"); intent.putExtra("surface_renderer", "image"); intent.putExtra("crop", new Rect(0, 0, SURFACE_WIDTH / 2, SURFACE_HEIGHT / 2)); ScreenshotUtil.capture( @@ -101,7 +99,7 @@ public void testCroppedMediaSurface_bottomLeft() throws Exception { @Test @SdkSuppress(minSdkVersion = VERSION_CODES.M) public void testCroppedMediaSurface_topRight() throws Exception { - intent.putExtra("scenario", "display_texture"); + intent.putExtra("scenario_name", "display_texture"); intent.putExtra("surface_renderer", "image"); intent.putExtra( "crop", new Rect(SURFACE_WIDTH / 2, SURFACE_HEIGHT / 2, SURFACE_WIDTH, SURFACE_HEIGHT)); @@ -113,7 +111,7 @@ public void testCroppedMediaSurface_topRight() throws Exception { @Test @SdkSuppress(minSdkVersion = VERSION_CODES.M) public void testCroppedRotatedMediaSurface_bottomLeft_90() throws Exception { - intent.putExtra("scenario", "display_texture"); + intent.putExtra("scenario_name", "display_texture"); intent.putExtra("surface_renderer", "image"); intent.putExtra("crop", new Rect(0, 0, SURFACE_WIDTH / 2, SURFACE_HEIGHT / 2)); intent.putExtra("rotation", 90); diff --git a/testing/scenario_app/android/app/src/main/java/dev/flutter/scenarios/ExternalTextureFlutterActivity.java b/testing/scenario_app/android/app/src/main/java/dev/flutter/scenarios/ExternalTextureFlutterActivity.java index 66cd2be09dc04..a99936daa07e7 100644 --- a/testing/scenario_app/android/app/src/main/java/dev/flutter/scenarios/ExternalTextureFlutterActivity.java +++ b/testing/scenario_app/android/app/src/main/java/dev/flutter/scenarios/ExternalTextureFlutterActivity.java @@ -6,11 +6,13 @@ import android.content.res.AssetFileDescriptor; import android.graphics.Canvas; +import android.graphics.ImageFormat; import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Shader.TileMode; import android.graphics.SurfaceTexture; +import android.hardware.HardwareBuffer; import android.media.Image; import android.media.ImageReader; import android.media.ImageWriter; @@ -91,7 +93,7 @@ public void waitUntilFlutterRendered() { super.waitUntilFlutterRendered(); try { - firstFrameLatch.await(); + firstFrameLatch.await(10, java.util.concurrent.TimeUnit.SECONDS); } catch (InterruptedException e) { throw new RuntimeException(e); } @@ -368,9 +370,23 @@ protected ImageSurfaceRenderer(SurfaceRenderer inner, Rect crop) { @Override public void attach(Surface surface, CountDownLatch onFirstFrame) { this.onFirstFrame = onFirstFrame; - writer = ImageWriter.newInstance(surface, 3); - reader = ImageReader.newInstance(SURFACE_WIDTH, SURFACE_HEIGHT, writer.getFormat(), 2); - + if (VERSION.SDK_INT >= VERSION_CODES.Q) { + // On Android Q+, use PRIVATE image format. + // Also let the frame producer know the images will + // be sampled from by the GPU. + writer = ImageWriter.newInstance(surface, 3, ImageFormat.PRIVATE); + reader = + ImageReader.newInstance( + SURFACE_WIDTH, + SURFACE_HEIGHT, + ImageFormat.PRIVATE, + 2, + HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE); + } else { + // Before Android Q, this will change the format of the surface to match the images. + writer = ImageWriter.newInstance(surface, 3); + reader = ImageReader.newInstance(SURFACE_WIDTH, SURFACE_HEIGHT, writer.getFormat(), 2); + } inner.attach(reader.getSurface(), null); handlerThread = new HandlerThread("image reader/writer thread"); @@ -409,7 +425,8 @@ private void onImageAvailable(ImageReader reader) { // If the output surface disconnects, this method will be interrupted with an // IllegalStateException. // Simply log and return. - Log.i(TAG, "Surface disconnected from ImageWriter"); + Log.i(TAG, "Surface disconnected from ImageWriter", e); + image.close(); return; }