From b48b1270a3f476a960e891761b2fd7406e315a89 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Thu, 10 Mar 2022 18:46:14 +0100 Subject: [PATCH] app: [Android] re-introduce Choreographer for frame pacing According to #375, change b86928ceecf6800069cfd5a92e5b6f2216367fe5 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 --- app/GioView.java | 10 ++++++++-- app/os_android.go | 10 ++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/GioView.java b/app/GioView.java index d4e8ca0d..45045edd 100644 --- a/app/GioView.java +++ b/app/GioView.java @@ -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); } diff --git a/app/os_android.go b/app/os_android.go index b79fdf39..468ca751 100644 --- a/app/os_android.go +++ b/app/os_android.go @@ -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) }) } }