diff --git a/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java b/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java index e08a1629dd917..4c647142e9e3a 100644 --- a/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java +++ b/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java @@ -20,9 +20,11 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; +import io.flutter.Log; import io.flutter.embedding.engine.renderer.FlutterRenderer; import io.flutter.embedding.engine.renderer.RenderSurface; import java.nio.ByteBuffer; +import java.util.Locale; /** * Paints a Flutter UI provided by an {@link android.media.ImageReader} onto a {@link @@ -38,11 +40,17 @@ */ @TargetApi(19) public class FlutterImageView extends View implements RenderSurface { + private static final String TAG = "FlutterImageView"; + @NonNull private ImageReader imageReader; @Nullable private Image currentImage; @Nullable private Bitmap currentBitmap; @Nullable private FlutterRenderer flutterRenderer; + public ImageReader getImageReader() { + return imageReader; + } + public enum SurfaceKind { /** Displays the background canvas. */ background, @@ -86,9 +94,21 @@ private void init() { setAlpha(0.0f); } + private static void logW(String format, Object... args) { + Log.w(TAG, String.format(Locale.US, format, args)); + } + @TargetApi(19) @NonNull private static ImageReader createImageReader(int width, int height) { + if (width <= 0) { + logW("ImageReader width must be greater than 0, but given width=%d, set width=1", width); + width = 1; + } + if (height <= 0) { + logW("ImageReader height must be greater than 0, but given height=%d, set height=1", height); + height = 1; + } if (android.os.Build.VERSION.SDK_INT >= 29) { return ImageReader.newInstance( width, diff --git a/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java b/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java index b34d9431f2d58..a8b17284a367f 100644 --- a/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java +++ b/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java @@ -734,6 +734,31 @@ public void flutterImageView_detachFromRendererClosesPreviousImage() { verify(mockImage, times(3)).close(); } + @Test + public void flutterImageView_workaroundWithOnePixelWhenResizeWithZero() { + final ImageReader mockReader = mock(ImageReader.class); + when(mockReader.getMaxImages()).thenReturn(2); + + final FlutterImageView imageView = + spy( + new FlutterImageView( + RuntimeEnvironment.application, + mockReader, + FlutterImageView.SurfaceKind.background)); + + final FlutterJNI jni = mock(FlutterJNI.class); + imageView.attachToRenderer(new FlutterRenderer(jni)); + + final Image mockImage = mock(Image.class); + when(mockReader.acquireLatestImage()).thenReturn(mockImage); + + final int incorrectWidth = 0; + final int incorrectHeight = -100; + imageView.resizeIfNeeded(incorrectWidth, incorrectHeight); + assertEquals(1, imageView.getImageReader().getWidth()); + assertEquals(1, imageView.getImageReader().getHeight()); + } + @Test public void flutterSurfaceView_GathersTransparentRegion() { final Region mockRegion = mock(Region.class);