Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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