From 517f61119f5946fb2ae4e093a9f4729756a8db22 Mon Sep 17 00:00:00 2001 From: eggfly Date: Tue, 10 Aug 2021 10:25:01 +0800 Subject: [PATCH] Avoid crashing when FlutterImageView is resized to zero dimension (flutter/engine#27946) --- .../embedding/android/FlutterImageView.java | 20 +++++++++++++++ .../embedding/android/FlutterViewTest.java | 25 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java index e08a1629dd..4c647142e9 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/embedding/android/FlutterImageView.java @@ -20,9 +20,11 @@ import android.view.View; 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 @@ import java.nio.ByteBuffer; */ @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 @@ public class FlutterImageView extends View implements RenderSurface { 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/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java index b34d9431f2..a8b17284a3 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/android/FlutterViewTest.java @@ -734,6 +734,31 @@ public class FlutterViewTest { 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);