Skip to content

Commit d7a1a76

Browse files
authored
Re-Enable ExternalTexturesTest (flutter#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.
1 parent 33a5136 commit d7a1a76

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

testing/scenario_app/android/app/src/androidTest/java/dev/flutter/scenariosui/ExternalTextureTests.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@
1414
import androidx.test.runner.AndroidJUnit4;
1515
import dev.flutter.scenarios.ExternalTextureFlutterActivity;
1616
import org.junit.Before;
17-
import org.junit.Ignore;
1817
import org.junit.Rule;
1918
import org.junit.Test;
2019
import org.junit.runner.RunWith;
2120

2221
@RunWith(AndroidJUnit4.class)
2322
@LargeTest
24-
@Ignore("Test fails with java.lang.UnsupportedOperationException")
2523
public class ExternalTextureTests {
2624
private static final int SURFACE_WIDTH = 192;
2725
private static final int SURFACE_HEIGHT = 256;
@@ -42,7 +40,7 @@ public void setUp() {
4240

4341
@Test
4442
public void testCanvasSurface() throws Exception {
45-
intent.putExtra("scenario", "display_texture");
43+
intent.putExtra("scenario_name", "display_texture");
4644
intent.putExtra("surface_renderer", "canvas");
4745
ScreenshotUtil.capture(
4846
activityRule.launchActivity(intent), "ExternalTextureTests_testCanvasSurface");
@@ -51,7 +49,7 @@ public void testCanvasSurface() throws Exception {
5149
@Test
5250
@SdkSuppress(minSdkVersion = VERSION_CODES.LOLLIPOP)
5351
public void testMediaSurface() throws Exception {
54-
intent.putExtra("scenario", "display_texture");
52+
intent.putExtra("scenario_name", "display_texture");
5553
intent.putExtra("surface_renderer", "media");
5654
ScreenshotUtil.capture(
5755
activityRule.launchActivity(intent), "ExternalTextureTests_testMediaSurface");
@@ -60,7 +58,7 @@ public void testMediaSurface() throws Exception {
6058
@Test
6159
@SdkSuppress(minSdkVersion = VERSION_CODES.LOLLIPOP)
6260
public void testRotatedMediaSurface_90() throws Exception {
63-
intent.putExtra("scenario", "display_texture");
61+
intent.putExtra("scenario_name", "display_texture");
6462
intent.putExtra("surface_renderer", "media");
6563
intent.putExtra("rotation", 90);
6664
ScreenshotUtil.capture(
@@ -70,7 +68,7 @@ public void testRotatedMediaSurface_90() throws Exception {
7068
@Test
7169
@SdkSuppress(minSdkVersion = VERSION_CODES.LOLLIPOP)
7270
public void testRotatedMediaSurface_180() throws Exception {
73-
intent.putExtra("scenario", "display_texture");
71+
intent.putExtra("scenario_name", "display_texture");
7472
intent.putExtra("surface_renderer", "media");
7573
intent.putExtra("rotation", 180);
7674
ScreenshotUtil.capture(
@@ -80,7 +78,7 @@ public void testRotatedMediaSurface_180() throws Exception {
8078
@Test
8179
@SdkSuppress(minSdkVersion = VERSION_CODES.LOLLIPOP)
8280
public void testRotatedMediaSurface_270() throws Exception {
83-
intent.putExtra("scenario", "display_texture");
81+
intent.putExtra("scenario_name", "display_texture");
8482
intent.putExtra("surface_renderer", "media");
8583
intent.putExtra("rotation", 270);
8684
ScreenshotUtil.capture(
@@ -90,7 +88,7 @@ public void testRotatedMediaSurface_270() throws Exception {
9088
@Test
9189
@SdkSuppress(minSdkVersion = VERSION_CODES.M)
9290
public void testCroppedMediaSurface_bottomLeft() throws Exception {
93-
intent.putExtra("scenario", "display_texture");
91+
intent.putExtra("scenario_name", "display_texture");
9492
intent.putExtra("surface_renderer", "image");
9593
intent.putExtra("crop", new Rect(0, 0, SURFACE_WIDTH / 2, SURFACE_HEIGHT / 2));
9694
ScreenshotUtil.capture(
@@ -101,7 +99,7 @@ public void testCroppedMediaSurface_bottomLeft() throws Exception {
10199
@Test
102100
@SdkSuppress(minSdkVersion = VERSION_CODES.M)
103101
public void testCroppedMediaSurface_topRight() throws Exception {
104-
intent.putExtra("scenario", "display_texture");
102+
intent.putExtra("scenario_name", "display_texture");
105103
intent.putExtra("surface_renderer", "image");
106104
intent.putExtra(
107105
"crop", new Rect(SURFACE_WIDTH / 2, SURFACE_HEIGHT / 2, SURFACE_WIDTH, SURFACE_HEIGHT));
@@ -113,7 +111,7 @@ public void testCroppedMediaSurface_topRight() throws Exception {
113111
@Test
114112
@SdkSuppress(minSdkVersion = VERSION_CODES.M)
115113
public void testCroppedRotatedMediaSurface_bottomLeft_90() throws Exception {
116-
intent.putExtra("scenario", "display_texture");
114+
intent.putExtra("scenario_name", "display_texture");
117115
intent.putExtra("surface_renderer", "image");
118116
intent.putExtra("crop", new Rect(0, 0, SURFACE_WIDTH / 2, SURFACE_HEIGHT / 2));
119117
intent.putExtra("rotation", 90);

testing/scenario_app/android/app/src/main/java/dev/flutter/scenarios/ExternalTextureFlutterActivity.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
import android.content.res.AssetFileDescriptor;
88
import android.graphics.Canvas;
9+
import android.graphics.ImageFormat;
910
import android.graphics.LinearGradient;
1011
import android.graphics.Paint;
1112
import android.graphics.Rect;
1213
import android.graphics.Shader.TileMode;
1314
import android.graphics.SurfaceTexture;
15+
import android.hardware.HardwareBuffer;
1416
import android.media.Image;
1517
import android.media.ImageReader;
1618
import android.media.ImageWriter;
@@ -91,7 +93,7 @@ public void waitUntilFlutterRendered() {
9193
super.waitUntilFlutterRendered();
9294

9395
try {
94-
firstFrameLatch.await();
96+
firstFrameLatch.await(10, java.util.concurrent.TimeUnit.SECONDS);
9597
} catch (InterruptedException e) {
9698
throw new RuntimeException(e);
9799
}
@@ -368,9 +370,23 @@ protected ImageSurfaceRenderer(SurfaceRenderer inner, Rect crop) {
368370
@Override
369371
public void attach(Surface surface, CountDownLatch onFirstFrame) {
370372
this.onFirstFrame = onFirstFrame;
371-
writer = ImageWriter.newInstance(surface, 3);
372-
reader = ImageReader.newInstance(SURFACE_WIDTH, SURFACE_HEIGHT, writer.getFormat(), 2);
373-
373+
if (VERSION.SDK_INT >= VERSION_CODES.Q) {
374+
// On Android Q+, use PRIVATE image format.
375+
// Also let the frame producer know the images will
376+
// be sampled from by the GPU.
377+
writer = ImageWriter.newInstance(surface, 3, ImageFormat.PRIVATE);
378+
reader =
379+
ImageReader.newInstance(
380+
SURFACE_WIDTH,
381+
SURFACE_HEIGHT,
382+
ImageFormat.PRIVATE,
383+
2,
384+
HardwareBuffer.USAGE_GPU_SAMPLED_IMAGE);
385+
} else {
386+
// Before Android Q, this will change the format of the surface to match the images.
387+
writer = ImageWriter.newInstance(surface, 3);
388+
reader = ImageReader.newInstance(SURFACE_WIDTH, SURFACE_HEIGHT, writer.getFormat(), 2);
389+
}
374390
inner.attach(reader.getSurface(), null);
375391

376392
handlerThread = new HandlerThread("image reader/writer thread");
@@ -409,7 +425,8 @@ private void onImageAvailable(ImageReader reader) {
409425
// If the output surface disconnects, this method will be interrupted with an
410426
// IllegalStateException.
411427
// Simply log and return.
412-
Log.i(TAG, "Surface disconnected from ImageWriter");
428+
Log.i(TAG, "Surface disconnected from ImageWriter", e);
429+
image.close();
413430
return;
414431
}
415432

0 commit comments

Comments
 (0)