app: [Android] re-introduce Choreographer for frame pacing

According to #375, change b86928ceec
increased frame pacing jitter. This change effectively reverts it
by re-introducing Choreographer for pacing.

References: https://todo.sr.ht/~eliasnaur/gio/375
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2022-03-10 18:46:14 +01:00
parent f19b16fecc
commit b48b1270a3
2 changed files with 12 additions and 8 deletions
+8 -2
View File
@@ -25,6 +25,7 @@ import android.text.Selection;
import android.text.SpannableStringBuilder;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Choreographer;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.MotionEvent;
@@ -55,7 +56,7 @@ import android.view.accessibility.AccessibilityManager;
import java.io.UnsupportedEncodingException;
public final class GioView extends SurfaceView {
public final class GioView extends SurfaceView implements Choreographer.FrameCallback {
private static boolean jniLoaded;
private final SurfaceHolder.Callback surfCallbacks;
@@ -380,7 +381,12 @@ public final class GioView extends SurfaceView {
return true;
}
@Override protected void onDraw(Canvas canvas) {
void postFrameCallback() {
Choreographer.getInstance().removeFrameCallback(this);
Choreographer.getInstance().postFrameCallback(this);
}
@Override public void doFrame(long nanos) {
if (nhandle != 0) {
onFrameCallback(nhandle);
}
+4 -6
View File
@@ -179,7 +179,7 @@ var gioView struct {
showTextInput C.jmethodID
hideTextInput C.jmethodID
setInputHint C.jmethodID
postInvalidate C.jmethodID // requests draw, called from non-UI thread
postFrameCallback C.jmethodID
invalidate C.jmethodID // requests draw, called from UI thread
setCursor C.jmethodID
setOrientation C.jmethodID
@@ -462,7 +462,7 @@ func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.j
m.showTextInput = getMethodID(env, class, "showTextInput", "()V")
m.hideTextInput = getMethodID(env, class, "hideTextInput", "()V")
m.setInputHint = getMethodID(env, class, "setInputHint", "(I)V")
m.postInvalidate = getMethodID(env, class, "postInvalidate", "()V")
m.postFrameCallback = getMethodID(env, class, "postFrameCallback", "()V")
m.invalidate = getMethodID(env, class, "invalidate", "()V")
m.setCursor = getMethodID(env, class, "setCursor", "(I)V")
m.setOrientation = getMethodID(env, class, "setOrientation", "(II)V")
@@ -564,9 +564,7 @@ func Java_org_gioui_GioView_onFrameCallback(env *C.JNIEnv, class C.jclass, view
}
if w.animating {
w.draw(env, false)
// Schedule the next draw immediately after this one. Since onFrameCallback runs
// on the UI thread, View.invalidate can be used here instead of postInvalidate.
callVoidMethod(env, w.view, gioView.invalidate)
callVoidMethod(env, w.view, gioView.postFrameCallback)
}
}
@@ -818,7 +816,7 @@ func (w *window) SetAnimating(anim bool) {
w.animating = anim
if anim {
runInJVM(javaVM(), func(env *C.JNIEnv) {
callVoidMethod(env, w.view, gioView.postInvalidate)
callVoidMethod(env, w.view, gioView.postFrameCallback)
})
}
}