package io.flutter.view; import android.hardware.display.DisplayManager; import android.view.Choreographer; import io.flutter.embedding.engine.FlutterJNI; import java.util.Objects; /* loaded from: classes.dex */ public class VsyncWaiter { private static VsyncWaiter instance; private static DisplayListener listener; private FlutterJNI flutterJNI; private long refreshPeriodNanos = -1; private FrameCallback frameCallback = new FrameCallback(this, 0); private final FlutterJNI.AsyncWaitForVsyncDelegate asyncWaitForVsyncDelegate = new FlutterJNI.AsyncWaitForVsyncDelegate(this) { // from class: io.flutter.view.VsyncWaiter.1 final VsyncWaiter this$0; { this.this$0 = this; } private Choreographer.FrameCallback obtainFrameCallback(long j) { if (this.this$0.frameCallback != null) { this.this$0.frameCallback.cookie = j; FrameCallback frameCallback = this.this$0.frameCallback; this.this$0.frameCallback = null; return frameCallback; } return new FrameCallback(this.this$0, j); } @Override // io.flutter.embedding.engine.FlutterJNI.AsyncWaitForVsyncDelegate public void asyncWaitForVsync(long j) { Choreographer.getInstance().postFrameCallback(obtainFrameCallback(j)); } }; public static void reset() { instance = null; listener = null; } /* loaded from: classes.dex */ class DisplayListener implements DisplayManager.DisplayListener { private DisplayManager displayManager; final VsyncWaiter this$0; @Override // android.hardware.display.DisplayManager.DisplayListener public void onDisplayAdded(int i) { } @Override // android.hardware.display.DisplayManager.DisplayListener public void onDisplayRemoved(int i) { } DisplayListener(VsyncWaiter vsyncWaiter, DisplayManager displayManager) { this.this$0 = vsyncWaiter; this.displayManager = displayManager; } void register() { this.displayManager.registerDisplayListener(this, null); } @Override // android.hardware.display.DisplayManager.DisplayListener public void onDisplayChanged(int i) { if (i == 0) { float refreshRate = this.displayManager.getDisplay(0).getRefreshRate(); this.this$0.refreshPeriodNanos = (long) (1.0E9d / refreshRate); this.this$0.flutterJNI.setRefreshRateFPS(refreshRate); } } } public static VsyncWaiter getInstance(float f, FlutterJNI flutterJNI) { if (instance == null) { instance = new VsyncWaiter(flutterJNI); } flutterJNI.setRefreshRateFPS(f); VsyncWaiter vsyncWaiter = instance; vsyncWaiter.refreshPeriodNanos = (long) (1.0E9d / f); return vsyncWaiter; } public static VsyncWaiter getInstance(DisplayManager displayManager, FlutterJNI flutterJNI) { if (instance == null) { instance = new VsyncWaiter(flutterJNI); } if (listener == null) { VsyncWaiter vsyncWaiter = instance; Objects.requireNonNull(vsyncWaiter); DisplayListener displayListener = new DisplayListener(vsyncWaiter, displayManager); listener = displayListener; displayListener.register(); } if (instance.refreshPeriodNanos == -1) { float refreshRate = displayManager.getDisplay(0).getRefreshRate(); instance.refreshPeriodNanos = (long) (1.0E9d / refreshRate); flutterJNI.setRefreshRateFPS(refreshRate); } return instance; } /* JADX INFO: Access modifiers changed from: package-private */ /* loaded from: classes.dex */ public class FrameCallback implements Choreographer.FrameCallback { private long cookie; final VsyncWaiter this$0; FrameCallback(VsyncWaiter vsyncWaiter, long j) { this.this$0 = vsyncWaiter; this.cookie = j; } @Override // android.view.Choreographer.FrameCallback public void doFrame(long j) { long nanoTime = System.nanoTime() - j; this.this$0.flutterJNI.onVsync(nanoTime < 0 ? 0L : nanoTime, this.this$0.refreshPeriodNanos, this.cookie); this.this$0.frameCallback = this; } } private VsyncWaiter(FlutterJNI flutterJNI) { this.flutterJNI = flutterJNI; } public void init() { this.flutterJNI.setAsyncWaitForVsyncDelegate(this.asyncWaitForVsyncDelegate); } }