package io.flutter.embedding.engine.renderer; import android.graphics.Bitmap; import android.graphics.Rect; import android.graphics.SurfaceTexture; import android.os.Handler; import android.view.Surface; import com.google.android.gms.maps.model.BitmapDescriptorFactory; import io.flutter.Log; import io.flutter.embedding.engine.FlutterJNI; import io.flutter.view.TextureRegistry; import java.lang.ref.WeakReference; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import java.util.concurrent.atomic.AtomicLong; /* loaded from: classes.dex */ public class FlutterRenderer implements TextureRegistry { private static final String TAG = "FlutterRenderer"; private final FlutterJNI flutterJNI; private final FlutterUiDisplayListener flutterUiDisplayListener; private Surface surface; private final AtomicLong nextTextureId = new AtomicLong(0); private boolean isDisplayingFlutterUi = false; private Handler handler = new Handler(); private final Set> onTrimMemoryListeners = new HashSet(); /* loaded from: classes6.dex */ public static final class ViewportMetrics { public static final int unsetValue = -1; public float devicePixelRatio = 1.0f; public int width = 0; public int height = 0; public int viewPaddingTop = 0; public int viewPaddingRight = 0; public int viewPaddingBottom = 0; public int viewPaddingLeft = 0; public int viewInsetTop = 0; public int viewInsetRight = 0; public int viewInsetBottom = 0; public int viewInsetLeft = 0; public int systemGestureInsetTop = 0; public int systemGestureInsetRight = 0; public int systemGestureInsetBottom = 0; public int systemGestureInsetLeft = 0; public int physicalTouchSlop = -1; public List displayFeatures = new ArrayList(); final boolean validate() { return this.width > 0 && this.height > 0 && this.devicePixelRatio > BitmapDescriptorFactory.HUE_RED; } } public boolean isDisplayingFlutterUi() { return this.isDisplayingFlutterUi; } public FlutterRenderer(FlutterJNI flutterJNI) { FlutterUiDisplayListener flutterUiDisplayListener = new FlutterUiDisplayListener(this) { // from class: io.flutter.embedding.engine.renderer.FlutterRenderer.1 final FlutterRenderer this$0; { this.this$0 = this; } @Override // io.flutter.embedding.engine.renderer.FlutterUiDisplayListener public void onFlutterUiDisplayed() { this.this$0.isDisplayingFlutterUi = true; } @Override // io.flutter.embedding.engine.renderer.FlutterUiDisplayListener public void onFlutterUiNoLongerDisplayed() { this.this$0.isDisplayingFlutterUi = false; } }; this.flutterUiDisplayListener = flutterUiDisplayListener; this.flutterJNI = flutterJNI; flutterJNI.addIsDisplayingFlutterUiListener(flutterUiDisplayListener); } public void addIsDisplayingFlutterUiListener(FlutterUiDisplayListener flutterUiDisplayListener) { this.flutterJNI.addIsDisplayingFlutterUiListener(flutterUiDisplayListener); if (this.isDisplayingFlutterUi) { flutterUiDisplayListener.onFlutterUiDisplayed(); } } public void removeIsDisplayingFlutterUiListener(FlutterUiDisplayListener flutterUiDisplayListener) { this.flutterJNI.removeIsDisplayingFlutterUiListener(flutterUiDisplayListener); } private void clearDeadListeners() { Iterator> it = this.onTrimMemoryListeners.iterator(); while (it.hasNext()) { if (it.next().get() == null) { it.remove(); } } } void addOnTrimMemoryListener(TextureRegistry.OnTrimMemoryListener onTrimMemoryListener) { clearDeadListeners(); this.onTrimMemoryListeners.add(new WeakReference<>(onTrimMemoryListener)); } void removeOnTrimMemoryListener(TextureRegistry.OnTrimMemoryListener onTrimMemoryListener) { for (WeakReference weakReference : this.onTrimMemoryListeners) { if (weakReference.get() == onTrimMemoryListener) { this.onTrimMemoryListeners.remove(weakReference); return; } } } @Override // io.flutter.view.TextureRegistry public TextureRegistry.SurfaceTextureEntry createSurfaceTexture() { Log.v(TAG, "Creating a SurfaceTexture."); return registerSurfaceTexture(new SurfaceTexture(0)); } @Override // io.flutter.view.TextureRegistry public TextureRegistry.SurfaceTextureEntry registerSurfaceTexture(SurfaceTexture surfaceTexture) { surfaceTexture.detachFromGLContext(); SurfaceTextureRegistryEntry surfaceTextureRegistryEntry = new SurfaceTextureRegistryEntry(this, this.nextTextureId.getAndIncrement(), surfaceTexture); Log.v(TAG, "New SurfaceTexture ID: " + surfaceTextureRegistryEntry.id()); registerTexture(surfaceTextureRegistryEntry.id(), surfaceTextureRegistryEntry.textureWrapper()); addOnTrimMemoryListener(surfaceTextureRegistryEntry); return surfaceTextureRegistryEntry; } @Override // io.flutter.view.TextureRegistry public void onTrimMemory(int i) { Iterator> it = this.onTrimMemoryListeners.iterator(); while (it.hasNext()) { TextureRegistry.OnTrimMemoryListener onTrimMemoryListener = it.next().get(); if (onTrimMemoryListener != null) { onTrimMemoryListener.onTrimMemory(i); } else { it.remove(); } } } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes6.dex */ public final class SurfaceTextureRegistryEntry implements TextureRegistry.SurfaceTextureEntry, TextureRegistry.OnTrimMemoryListener { private TextureRegistry.OnFrameConsumedListener frameConsumedListener; private final long id; private final Runnable onFrameConsumed; private SurfaceTexture.OnFrameAvailableListener onFrameListener; private boolean released; private final SurfaceTextureWrapper textureWrapper; final FlutterRenderer this$0; private TextureRegistry.OnTrimMemoryListener trimMemoryListener; @Override // io.flutter.view.TextureRegistry.SurfaceTextureEntry public final long id() { return this.id; } @Override // io.flutter.view.TextureRegistry.SurfaceTextureEntry public final void setOnFrameConsumedListener(TextureRegistry.OnFrameConsumedListener onFrameConsumedListener) { this.frameConsumedListener = onFrameConsumedListener; } @Override // io.flutter.view.TextureRegistry.SurfaceTextureEntry public final void setOnTrimMemoryListener(TextureRegistry.OnTrimMemoryListener onTrimMemoryListener) { this.trimMemoryListener = onTrimMemoryListener; } public final SurfaceTextureWrapper textureWrapper() { return this.textureWrapper; } SurfaceTextureRegistryEntry(FlutterRenderer flutterRenderer, long j, SurfaceTexture surfaceTexture) { this.this$0 = flutterRenderer; Runnable runnable = new Runnable(this) { // from class: io.flutter.embedding.engine.renderer.FlutterRenderer.SurfaceTextureRegistryEntry.1 final SurfaceTextureRegistryEntry this$1; { this.this$1 = this; } @Override // java.lang.Runnable public void run() { if (this.this$1.frameConsumedListener != null) { this.this$1.frameConsumedListener.onFrameConsumed(); } } }; this.onFrameConsumed = runnable; this.onFrameListener = new SurfaceTexture.OnFrameAvailableListener(this) { // from class: io.flutter.embedding.engine.renderer.FlutterRenderer.SurfaceTextureRegistryEntry.2 final SurfaceTextureRegistryEntry this$1; { this.this$1 = this; } @Override // android.graphics.SurfaceTexture.OnFrameAvailableListener public void onFrameAvailable(SurfaceTexture surfaceTexture2) { if (this.this$1.released || !this.this$1.this$0.flutterJNI.isAttached()) { return; } this.this$1.this$0.markTextureFrameAvailable(this.this$1.id); } }; this.id = j; this.textureWrapper = new SurfaceTextureWrapper(surfaceTexture, runnable); surfaceTexture().setOnFrameAvailableListener(this.onFrameListener, new Handler()); } @Override // io.flutter.view.TextureRegistry.OnTrimMemoryListener public final void onTrimMemory(int i) { TextureRegistry.OnTrimMemoryListener onTrimMemoryListener = this.trimMemoryListener; if (onTrimMemoryListener != null) { onTrimMemoryListener.onTrimMemory(i); } } private void removeListener() { this.this$0.removeOnTrimMemoryListener(this); } @Override // io.flutter.view.TextureRegistry.SurfaceTextureEntry public final SurfaceTexture surfaceTexture() { return this.textureWrapper.surfaceTexture(); } @Override // io.flutter.view.TextureRegistry.SurfaceTextureEntry public final void release() { if (this.released) { return; } Log.v(FlutterRenderer.TAG, "Releasing a SurfaceTexture (" + this.id + ")."); this.textureWrapper.release(); this.this$0.unregisterTexture(this.id); removeListener(); this.released = true; } protected final void finalize() throws Throwable { try { if (this.released) { return; } this.this$0.handler.post(new SurfaceTextureFinalizerRunnable(this.id, this.this$0.flutterJNI)); } finally { super.finalize(); } } } /* loaded from: classes6.dex */ static final class SurfaceTextureFinalizerRunnable implements Runnable { private final FlutterJNI flutterJNI; private final long id; SurfaceTextureFinalizerRunnable(long j, FlutterJNI flutterJNI) { this.id = j; this.flutterJNI = flutterJNI; } @Override // java.lang.Runnable public final void run() { if (this.flutterJNI.isAttached()) { Log.v(FlutterRenderer.TAG, "Releasing a SurfaceTexture (" + this.id + ")."); this.flutterJNI.unregisterTexture(this.id); } } } public void startRenderingToSurface(Surface surface, boolean z) { if (this.surface != null && !z) { stopRenderingToSurface(); } this.surface = surface; this.flutterJNI.onSurfaceCreated(surface); } public void swapSurface(Surface surface) { this.surface = surface; this.flutterJNI.onSurfaceWindowChanged(surface); } public void surfaceChanged(int i, int i2) { this.flutterJNI.onSurfaceChanged(i, i2); } public void stopRenderingToSurface() { this.flutterJNI.onSurfaceDestroyed(); this.surface = null; if (this.isDisplayingFlutterUi) { this.flutterUiDisplayListener.onFlutterUiNoLongerDisplayed(); } this.isDisplayingFlutterUi = false; } public void setViewportMetrics(ViewportMetrics viewportMetrics) { if (viewportMetrics.validate()) { Log.v(TAG, "Setting viewport metrics\nSize: " + viewportMetrics.width + " x " + viewportMetrics.height + "\nPadding - L: " + viewportMetrics.viewPaddingLeft + ", T: " + viewportMetrics.viewPaddingTop + ", R: " + viewportMetrics.viewPaddingRight + ", B: " + viewportMetrics.viewPaddingBottom + "\nInsets - L: " + viewportMetrics.viewInsetLeft + ", T: " + viewportMetrics.viewInsetTop + ", R: " + viewportMetrics.viewInsetRight + ", B: " + viewportMetrics.viewInsetBottom + "\nSystem Gesture Insets - L: " + viewportMetrics.systemGestureInsetLeft + ", T: " + viewportMetrics.systemGestureInsetTop + ", R: " + viewportMetrics.systemGestureInsetRight + ", B: " + viewportMetrics.systemGestureInsetRight + "\nDisplay Features: " + viewportMetrics.displayFeatures.size()); int[] iArr = new int[viewportMetrics.displayFeatures.size() * 4]; int[] iArr2 = new int[viewportMetrics.displayFeatures.size()]; int[] iArr3 = new int[viewportMetrics.displayFeatures.size()]; for (int i = 0; i < viewportMetrics.displayFeatures.size(); i++) { DisplayFeature displayFeature = viewportMetrics.displayFeatures.get(i); int i2 = i * 4; iArr[i2] = displayFeature.bounds.left; iArr[i2 + 1] = displayFeature.bounds.top; iArr[i2 + 2] = displayFeature.bounds.right; iArr[i2 + 3] = displayFeature.bounds.bottom; iArr2[i] = displayFeature.type.encodedValue; iArr3[i] = displayFeature.state.encodedValue; } this.flutterJNI.setViewportMetrics(viewportMetrics.devicePixelRatio, viewportMetrics.width, viewportMetrics.height, viewportMetrics.viewPaddingTop, viewportMetrics.viewPaddingRight, viewportMetrics.viewPaddingBottom, viewportMetrics.viewPaddingLeft, viewportMetrics.viewInsetTop, viewportMetrics.viewInsetRight, viewportMetrics.viewInsetBottom, viewportMetrics.viewInsetLeft, viewportMetrics.systemGestureInsetTop, viewportMetrics.systemGestureInsetRight, viewportMetrics.systemGestureInsetBottom, viewportMetrics.systemGestureInsetLeft, viewportMetrics.physicalTouchSlop, iArr, iArr2, iArr3); } } public Bitmap getBitmap() { return this.flutterJNI.getBitmap(); } public void dispatchPointerDataPacket(ByteBuffer byteBuffer, int i) { this.flutterJNI.dispatchPointerDataPacket(byteBuffer, i); } private void registerTexture(long j, SurfaceTextureWrapper surfaceTextureWrapper) { this.flutterJNI.registerTexture(j, surfaceTextureWrapper); } /* JADX INFO: Access modifiers changed from: private */ public void markTextureFrameAvailable(long j) { this.flutterJNI.markTextureFrameAvailable(j); } /* JADX INFO: Access modifiers changed from: private */ public void unregisterTexture(long j) { this.flutterJNI.unregisterTexture(j); } public boolean isSoftwareRenderingEnabled() { return this.flutterJNI.getIsSoftwareRenderingEnabled(); } public void setAccessibilityFeatures(int i) { this.flutterJNI.setAccessibilityFeatures(i); } public void setSemanticsEnabled(boolean z) { this.flutterJNI.setSemanticsEnabled(z); } public void dispatchSemanticsAction(int i, int i2, ByteBuffer byteBuffer, int i3) { this.flutterJNI.dispatchSemanticsAction(i, i2, byteBuffer, i3); } /* loaded from: classes6.dex */ public static final class DisplayFeature { public final Rect bounds; public final DisplayFeatureState state; public final DisplayFeatureType type; public DisplayFeature(Rect rect, DisplayFeatureType displayFeatureType, DisplayFeatureState displayFeatureState) { this.bounds = rect; this.type = displayFeatureType; this.state = displayFeatureState; } public DisplayFeature(Rect rect, DisplayFeatureType displayFeatureType) { this.bounds = rect; this.type = displayFeatureType; this.state = DisplayFeatureState.UNKNOWN; } } /* loaded from: classes6.dex */ public enum DisplayFeatureType { UNKNOWN(0), FOLD(1), HINGE(2), CUTOUT(3); public final int encodedValue; DisplayFeatureType(int i) { this.encodedValue = i; } } /* loaded from: classes6.dex */ public enum DisplayFeatureState { UNKNOWN(0), POSTURE_FLAT(1), POSTURE_HALF_OPENED(2); public final int encodedValue; DisplayFeatureState(int i) { this.encodedValue = i; } } }