Avoid crashing when FlutterImageView is resized to zero dimension (flutter/engine#27946)

This commit is contained in:
eggfly
2021-08-10 10:25:01 +08:00
committed by GitHub
parent 684ad825ce
commit 517f61119f
2 changed files with 45 additions and 0 deletions

View File

@@ -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,

View File

@@ -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);