Skip to content

Commit

Permalink
Avoid crashing when FlutterImageView is resized to zero dimension (fl…
Browse files Browse the repository at this point in the history
  • Loading branch information
eggfly authored Aug 10, 2021
1 parent c2f2291 commit a1fd1e1
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit a1fd1e1

Please sign in to comment.