Avoid object creation during each frame (flutter/engine#35343)

This commit is contained in:
simonla 2022-08-19 02:45:47 +08:00 committed by GitHub
parent 0c596afd9a
commit 1711a00c0d

View File

@ -47,6 +47,7 @@ public class VsyncWaiter {
private static DisplayListener listener;
private long refreshPeriodNanos = -1;
private FlutterJNI flutterJNI;
private FrameCallback frameCallback = new FrameCallback(0);
@NonNull
public static VsyncWaiter getInstance(float fps, @NonNull FlutterJNI flutterJNI) {
@ -85,22 +86,41 @@ public class VsyncWaiter {
listener = null;
}
private class FrameCallback implements Choreographer.FrameCallback {
private long cookie;
FrameCallback(long cookie) {
this.cookie = cookie;
}
@Override
public void doFrame(long frameTimeNanos) {
long delay = System.nanoTime() - frameTimeNanos;
if (delay < 0) {
delay = 0;
}
flutterJNI.onVsync(delay, refreshPeriodNanos, cookie);
frameCallback = this;
}
}
private final FlutterJNI.AsyncWaitForVsyncDelegate asyncWaitForVsyncDelegate =
new FlutterJNI.AsyncWaitForVsyncDelegate() {
private Choreographer.FrameCallback obtainFrameCallback(final long cookie) {
if (frameCallback != null) {
frameCallback.cookie = cookie;
FrameCallback ret = frameCallback;
frameCallback = null;
return ret;
}
return new FrameCallback(cookie);
}
@Override
public void asyncWaitForVsync(long cookie) {
Choreographer.getInstance()
.postFrameCallback(
new Choreographer.FrameCallback() {
@Override
public void doFrame(long frameTimeNanos) {
long delay = System.nanoTime() - frameTimeNanos;
if (delay < 0) {
delay = 0;
}
flutterJNI.onVsync(delay, refreshPeriodNanos, cookie);
}
});
Choreographer.getInstance().postFrameCallback(obtainFrameCallback(cookie));
}
};