ap/internal/window: make cached JNI method descriptors global

We're about to remove all references to window from runOnMain callbacks.
The JNI methods are constant and can be moved out of window.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2021-01-10 11:14:45 +01:00
parent 759b796283
commit beb046ac42
+27 -21
View File
@@ -74,14 +74,17 @@ type window struct {
mu sync.Mutex mu sync.Mutex
win *C.ANativeWindow win *C.ANativeWindow
animating bool animating bool
}
// Cached Java methods. // gioView hold cached JNI methods for GioView.
mgetDensity C.jmethodID var gioView struct {
mgetFontScale C.jmethodID once sync.Once
mshowTextInput C.jmethodID getDensity C.jmethodID
mhideTextInput C.jmethodID getFontScale C.jmethodID
mpostFrameCallback C.jmethodID showTextInput C.jmethodID
msetCursor C.jmethodID hideTextInput C.jmethodID
postFrameCallback C.jmethodID
setCursor C.jmethodID
} }
// ViewEvent is sent whenever the Window's underlying Android view // ViewEvent is sent whenever the Window's underlying Android view
@@ -196,15 +199,18 @@ func GetDataDir() string {
//export Java_org_gioui_GioView_onCreateView //export Java_org_gioui_GioView_onCreateView
func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.jobject) C.jlong { func Java_org_gioui_GioView_onCreateView(env *C.JNIEnv, class C.jclass, view C.jobject) C.jlong {
gioView.once.Do(func() {
m := &gioView
m.getDensity = getMethodID(env, class, "getDensity", "()I")
m.getFontScale = getMethodID(env, class, "getFontScale", "()F")
m.showTextInput = getMethodID(env, class, "showTextInput", "()V")
m.hideTextInput = getMethodID(env, class, "hideTextInput", "()V")
m.postFrameCallback = getMethodID(env, class, "postFrameCallback", "()V")
m.setCursor = getMethodID(env, class, "setCursor", "(Landroid/content/Context;I)V")
})
view = C.gio_jni_NewGlobalRef(env, view) view = C.gio_jni_NewGlobalRef(env, view)
w := &window{ w := &window{
view: view, view: view,
mgetDensity: getMethodID(env, class, "getDensity", "()I"),
mgetFontScale: getMethodID(env, class, "getFontScale", "()F"),
mshowTextInput: getMethodID(env, class, "showTextInput", "()V"),
mhideTextInput: getMethodID(env, class, "hideTextInput", "()V"),
mpostFrameCallback: getMethodID(env, class, "postFrameCallback", "()V"),
msetCursor: getMethodID(env, class, "setCursor", "(Landroid/content/Context;I)V"),
} }
wopts := <-mainWindow.out wopts := <-mainWindow.out
w.callbacks = wopts.window w.callbacks = wopts.window
@@ -292,7 +298,7 @@ func Java_org_gioui_GioView_onFrameCallback(env *C.JNIEnv, class C.jclass, view
w.mu.Unlock() w.mu.Unlock()
if anim { if anim {
runInJVM(javaVM(), func(env *C.JNIEnv) { runInJVM(javaVM(), func(env *C.JNIEnv) {
callVoidMethod(env, w.view, w.mpostFrameCallback) callVoidMethod(env, w.view, gioView.postFrameCallback)
}) })
w.draw(false) w.draw(false)
} }
@@ -367,8 +373,8 @@ func (w *window) aNativeWindow() *C.ANativeWindow {
} }
func (w *window) loadConfig(env *C.JNIEnv, class C.jclass) { func (w *window) loadConfig(env *C.JNIEnv, class C.jclass) {
dpi := int(C.gio_jni_CallIntMethod(env, w.view, w.mgetDensity)) dpi := int(C.gio_jni_CallIntMethod(env, w.view, gioView.getDensity))
w.fontScale = float32(C.gio_jni_CallFloatMethod(env, w.view, w.mgetFontScale)) w.fontScale = float32(C.gio_jni_CallFloatMethod(env, w.view, gioView.getFontScale))
switch dpi { switch dpi {
case C.ACONFIGURATION_DENSITY_NONE, case C.ACONFIGURATION_DENSITY_NONE,
C.ACONFIGURATION_DENSITY_DEFAULT, C.ACONFIGURATION_DENSITY_DEFAULT,
@@ -390,7 +396,7 @@ func (w *window) SetAnimating(anim bool) {
// View was destroyed while switching to main thread. // View was destroyed while switching to main thread.
return return
} }
callVoidMethod(env, w.view, w.mpostFrameCallback) callVoidMethod(env, w.view, gioView.postFrameCallback)
}) })
} }
} }
@@ -536,9 +542,9 @@ func (w *window) ShowTextInput(show bool) {
} }
runInJVM(javaVM(), func(env *C.JNIEnv) { runInJVM(javaVM(), func(env *C.JNIEnv) {
if show { if show {
callVoidMethod(env, w.view, w.mshowTextInput) callVoidMethod(env, w.view, gioView.showTextInput)
} else { } else {
callVoidMethod(env, w.view, w.mhideTextInput) callVoidMethod(env, w.view, gioView.hideTextInput)
} }
}) })
} }
@@ -673,7 +679,7 @@ func (w *window) SetCursor(name pointer.CursorName) {
curID = 0 // TYPE_NULL curID = 0 // TYPE_NULL
} }
runOnMain(func(env *C.JNIEnv) { runOnMain(func(env *C.JNIEnv) {
callVoidMethod(env, w.view, w.msetCursor, callVoidMethod(env, w.view, gioView.setCursor,
jvalue(android.appCtx), jvalue(curID)) jvalue(android.appCtx), jvalue(curID))
}) })
} }