From 23f6dcb868eb3ba48956eac9cc1688ca20f34b38 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Fri, 3 Sep 2021 13:35:54 +0200 Subject: [PATCH] app: [Android] simplify invalidate in onFrameCallback Since onFrameCallback runs on the app UI thread, using plain invalidate() can be used instead of postInvalidate(). The latter just schedules a call to invalidate() on the UI thread. Also, since onFrameCallback is directly called by JNI, there is no need to set up a new JNI environment to call back into Java. Signed-off-by: Felix Lange --- app/os_android.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/app/os_android.go b/app/os_android.go index b80ae749..3063abaa 100644 --- a/app/os_android.go +++ b/app/os_android.go @@ -155,7 +155,8 @@ var gioView struct { showTextInput C.jmethodID hideTextInput C.jmethodID setInputHint C.jmethodID - postInvalidate C.jmethodID + postInvalidate C.jmethodID // requests draw, called from non-UI thread + invalidate C.jmethodID // requests draw, called from UI thread setCursor C.jmethodID setOrientation C.jmethodID setNavigationColor C.jmethodID @@ -311,6 +312,7 @@ func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.j m.hideTextInput = getMethodID(env, class, "hideTextInput", "()V") m.setInputHint = getMethodID(env, class, "setInputHint", "(I)V") m.postInvalidate = getMethodID(env, class, "postInvalidate", "()V") + m.invalidate = getMethodID(env, class, "invalidate", "()V") m.setCursor = getMethodID(env, class, "setCursor", "(I)V") m.setOrientation = getMethodID(env, class, "setOrientation", "(II)V") m.setNavigationColor = getMethodID(env, class, "setNavigationColor", "(II)V") @@ -403,12 +405,11 @@ func Java_org_gioui_GioView_onFrameCallback(env *C.JNIEnv, class C.jclass, view if w.stage < system.StageRunning { return } - anim := w.animating - if anim { - runInJVM(javaVM(), func(env *C.JNIEnv) { - callVoidMethod(env, w.view, gioView.postInvalidate) - }) + if w.animating { w.draw(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) } }