5package io.flutter.view;
7import android.hardware.display.DisplayManager;
8import android.view.Choreographer;
10import androidx.annotation.NonNull;
11import androidx.annotation.VisibleForTesting;
12import io.flutter.embedding.engine.FlutterJNI;
18 this.displayManager = displayManager;
21 private DisplayManager displayManager;
24 displayManager.registerDisplayListener(
this,
null);
35 if (displayId == Display.DEFAULT_DISPLAY) {
36 final Display primaryDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
37 float fps = primaryDisplay.getRefreshRate();
38 VsyncWaiter.this.refreshPeriodNanos = (long) (1000000000.0 / fps);
45 private static DisplayListener listener;
46 private long refreshPeriodNanos = -1;
48 private FrameCallback frameCallback =
new FrameCallback(0);
55 flutterJNI.setRefreshRateFPS(fps);
56 instance.refreshPeriodNanos = (long) (1000000000.0 / fps);
62 @NonNull DisplayManager displayManager, @NonNull
FlutterJNI flutterJNI) {
66 if (listener ==
null) {
70 if (
instance.refreshPeriodNanos == -1) {
71 final Display primaryDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
72 float fps = primaryDisplay.getRefreshRate();
73 instance.refreshPeriodNanos = (long) (1000000000.0 / fps);
74 flutterJNI.setRefreshRateFPS(fps);
86 private class FrameCallback
implements Choreographer.FrameCallback {
90 FrameCallback(
long cookie) {
95 public void doFrame(
long frameTimeNanos) {
96 long delay = System.nanoTime() - frameTimeNanos;
100 flutterJNI.
onVsync(delay, refreshPeriodNanos, cookie);
101 frameCallback =
this;
105 private final FlutterJNI.AsyncWaitForVsyncDelegate asyncWaitForVsyncDelegate =
106 new FlutterJNI.AsyncWaitForVsyncDelegate() {
108 private Choreographer.FrameCallback obtainFrameCallback(
final long cookie) {
109 if (frameCallback !=
null) {
110 frameCallback.cookie = cookie;
111 FrameCallback ret = frameCallback;
112 frameCallback =
null;
115 return new FrameCallback(cookie);
119 public void asyncWaitForVsync(
long cookie) {
120 Choreographer.getInstance().postFrameCallback(obtainFrameCallback(cookie));
124 private VsyncWaiter(@NonNull FlutterJNI flutterJNI) {
125 this.flutterJNI = flutterJNI;
void setRefreshRateFPS(float refreshRateFPS)
void setAsyncWaitForVsyncDelegate(@Nullable AsyncWaitForVsyncDelegate delegate)
void onVsync(long frameDelayNanos, long refreshPeriodNanos, long cookie)
void onDisplayAdded(int displayId)
void onDisplayRemoved(int displayId)
DisplayListener(DisplayManager displayManager)
void onDisplayChanged(int displayId)
static VsyncWaiter getInstance(float fps, @NonNull FlutterJNI flutterJNI)
static VsyncWaiter getInstance( @NonNull DisplayManager displayManager, @NonNull FlutterJNI flutterJNI)