Skip to content

Commit

Permalink
Re-Enable ExternalTexturesTest (flutter#34118)
Browse files Browse the repository at this point in the history
Use ImageFormat.PRIVATE since produced images are not read. Hint to downstream surface producers that the image will be sampled from on the GPU.
  • Loading branch information
ds84182 authored Jun 21, 2022
1 parent 33a5136 commit d7a1a76
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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));
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit d7a1a76

Please sign in to comment.