225 lines
7.6 KiB
Java
225 lines
7.6 KiB
Java
package io.flutter.embedding.android;
|
|
|
|
import android.content.Context;
|
|
import android.graphics.Bitmap;
|
|
import android.graphics.Canvas;
|
|
import android.graphics.ColorSpace;
|
|
import android.graphics.Paint;
|
|
import android.hardware.HardwareBuffer;
|
|
import android.media.Image;
|
|
import android.media.ImageReader;
|
|
import android.os.Build;
|
|
import android.util.AttributeSet;
|
|
import android.view.Surface;
|
|
import android.view.View;
|
|
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
|
|
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;
|
|
|
|
/* loaded from: classes5.dex */
|
|
public class FlutterImageView extends View implements RenderSurface {
|
|
private static final String TAG = "FlutterImageView";
|
|
private Bitmap currentBitmap;
|
|
private Image currentImage;
|
|
private FlutterRenderer flutterRenderer;
|
|
private ImageReader imageReader;
|
|
private boolean isAttachedToFlutterRenderer;
|
|
private SurfaceKind kind;
|
|
|
|
/* loaded from: classes5.dex */
|
|
public enum SurfaceKind {
|
|
background,
|
|
overlay
|
|
}
|
|
|
|
@Override // io.flutter.embedding.engine.renderer.RenderSurface
|
|
public FlutterRenderer getAttachedRenderer() {
|
|
return this.flutterRenderer;
|
|
}
|
|
|
|
public ImageReader getImageReader() {
|
|
return this.imageReader;
|
|
}
|
|
|
|
@Override // io.flutter.embedding.engine.renderer.RenderSurface
|
|
public void pause() {
|
|
}
|
|
|
|
public FlutterImageView(Context context, int i, int i2, SurfaceKind surfaceKind) {
|
|
this(context, createImageReader(i, i2), surfaceKind);
|
|
}
|
|
|
|
public FlutterImageView(Context context) {
|
|
this(context, 1, 1, SurfaceKind.background);
|
|
}
|
|
|
|
public FlutterImageView(Context context, AttributeSet attributeSet) {
|
|
this(context, 1, 1, SurfaceKind.background);
|
|
}
|
|
|
|
FlutterImageView(Context context, ImageReader imageReader, SurfaceKind surfaceKind) {
|
|
super(context, null);
|
|
this.isAttachedToFlutterRenderer = false;
|
|
this.imageReader = imageReader;
|
|
this.kind = surfaceKind;
|
|
init();
|
|
}
|
|
|
|
private void init() {
|
|
setAlpha(BitmapDescriptorFactory.HUE_RED);
|
|
}
|
|
|
|
private static void logW(String str, Object... objArr) {
|
|
Log.w(TAG, String.format(Locale.US, str, objArr));
|
|
}
|
|
|
|
private static ImageReader createImageReader(int i, int i2) {
|
|
int i3;
|
|
int i4;
|
|
if (i <= 0) {
|
|
logW("ImageReader width must be greater than 0, but given width=%d, set width=1", Integer.valueOf(i));
|
|
i3 = 1;
|
|
} else {
|
|
i3 = i;
|
|
}
|
|
if (i2 <= 0) {
|
|
logW("ImageReader height must be greater than 0, but given height=%d, set height=1", Integer.valueOf(i2));
|
|
i4 = 1;
|
|
} else {
|
|
i4 = i2;
|
|
}
|
|
if (Build.VERSION.SDK_INT >= 29) {
|
|
return ImageReader.newInstance(i3, i4, 1, 3, 768L);
|
|
}
|
|
return ImageReader.newInstance(i3, i4, 1, 3);
|
|
}
|
|
|
|
public Surface getSurface() {
|
|
return this.imageReader.getSurface();
|
|
}
|
|
|
|
/* renamed from: io.flutter.embedding.android.FlutterImageView$1, reason: invalid class name */
|
|
/* loaded from: classes5.dex */
|
|
static /* synthetic */ class AnonymousClass1 {
|
|
static final int[] $SwitchMap$io$flutter$embedding$android$FlutterImageView$SurfaceKind;
|
|
|
|
static {
|
|
int[] iArr = new int[SurfaceKind.values().length];
|
|
$SwitchMap$io$flutter$embedding$android$FlutterImageView$SurfaceKind = iArr;
|
|
try {
|
|
iArr[SurfaceKind.background.ordinal()] = 1;
|
|
} catch (NoSuchFieldError unused) {
|
|
}
|
|
try {
|
|
$SwitchMap$io$flutter$embedding$android$FlutterImageView$SurfaceKind[SurfaceKind.overlay.ordinal()] = 2;
|
|
} catch (NoSuchFieldError unused2) {
|
|
}
|
|
}
|
|
}
|
|
|
|
@Override // io.flutter.embedding.engine.renderer.RenderSurface
|
|
public void attachToRenderer(FlutterRenderer flutterRenderer) {
|
|
if (AnonymousClass1.$SwitchMap$io$flutter$embedding$android$FlutterImageView$SurfaceKind[this.kind.ordinal()] == 1) {
|
|
flutterRenderer.swapSurface(this.imageReader.getSurface());
|
|
}
|
|
setAlpha(1.0f);
|
|
this.flutterRenderer = flutterRenderer;
|
|
this.isAttachedToFlutterRenderer = true;
|
|
}
|
|
|
|
@Override // io.flutter.embedding.engine.renderer.RenderSurface
|
|
public void detachFromRenderer() {
|
|
if (this.isAttachedToFlutterRenderer) {
|
|
setAlpha(BitmapDescriptorFactory.HUE_RED);
|
|
acquireLatestImage();
|
|
this.currentBitmap = null;
|
|
closeCurrentImage();
|
|
invalidate();
|
|
this.isAttachedToFlutterRenderer = false;
|
|
}
|
|
}
|
|
|
|
public boolean acquireLatestImage() {
|
|
if (!this.isAttachedToFlutterRenderer) {
|
|
return false;
|
|
}
|
|
Image acquireLatestImage = this.imageReader.acquireLatestImage();
|
|
if (acquireLatestImage != null) {
|
|
closeCurrentImage();
|
|
this.currentImage = acquireLatestImage;
|
|
invalidate();
|
|
}
|
|
return acquireLatestImage != null;
|
|
}
|
|
|
|
public void resizeIfNeeded(int i, int i2) {
|
|
if (this.flutterRenderer == null) {
|
|
return;
|
|
}
|
|
if (i == this.imageReader.getWidth() && i2 == this.imageReader.getHeight()) {
|
|
return;
|
|
}
|
|
closeCurrentImage();
|
|
closeImageReader();
|
|
this.imageReader = createImageReader(i, i2);
|
|
}
|
|
|
|
public void closeImageReader() {
|
|
this.imageReader.close();
|
|
}
|
|
|
|
@Override // android.view.View
|
|
protected void onDraw(Canvas canvas) {
|
|
super.onDraw(canvas);
|
|
if (this.currentImage != null) {
|
|
updateCurrentBitmap();
|
|
}
|
|
Bitmap bitmap = this.currentBitmap;
|
|
if (bitmap != null) {
|
|
canvas.drawBitmap(bitmap, BitmapDescriptorFactory.HUE_RED, BitmapDescriptorFactory.HUE_RED, (Paint) null);
|
|
}
|
|
}
|
|
|
|
private void closeCurrentImage() {
|
|
Image image = this.currentImage;
|
|
if (image != null) {
|
|
image.close();
|
|
this.currentImage = null;
|
|
}
|
|
}
|
|
|
|
private void updateCurrentBitmap() {
|
|
if (Build.VERSION.SDK_INT >= 29) {
|
|
HardwareBuffer hardwareBuffer = this.currentImage.getHardwareBuffer();
|
|
this.currentBitmap = Bitmap.wrapHardwareBuffer(hardwareBuffer, ColorSpace.get(ColorSpace.Named.SRGB));
|
|
hardwareBuffer.close();
|
|
return;
|
|
}
|
|
Image.Plane[] planes = this.currentImage.getPlanes();
|
|
if (planes.length != 1) {
|
|
return;
|
|
}
|
|
Image.Plane plane = planes[0];
|
|
int rowStride = plane.getRowStride() / plane.getPixelStride();
|
|
int height = this.currentImage.getHeight();
|
|
Bitmap bitmap = this.currentBitmap;
|
|
if (bitmap == null || bitmap.getWidth() != rowStride || this.currentBitmap.getHeight() != height) {
|
|
this.currentBitmap = Bitmap.createBitmap(rowStride, height, Bitmap.Config.ARGB_8888);
|
|
}
|
|
ByteBuffer buffer = plane.getBuffer();
|
|
buffer.rewind();
|
|
this.currentBitmap.copyPixelsFromBuffer(buffer);
|
|
}
|
|
|
|
@Override // android.view.View
|
|
protected void onSizeChanged(int i, int i2, int i3, int i4) {
|
|
if (!(i == this.imageReader.getWidth() && i2 == this.imageReader.getHeight()) && this.kind == SurfaceKind.background && this.isAttachedToFlutterRenderer) {
|
|
resizeIfNeeded(i, i2);
|
|
this.flutterRenderer.swapSurface(this.imageReader.getSurface());
|
|
}
|
|
}
|
|
}
|