From a0cf8bac21d3ab64aa288dd836b95803bf63abe0 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 8 Dec 2019 20:24:20 +0900 Subject: [PATCH] uidriver/glfw: Refactoring: Limit the calls of devicescale.GetAt --- internal/uidriver/glfw/ui.go | 39 ++++++++++++++-------------- internal/uidriver/glfw/ui_unix.go | 7 +---- internal/uidriver/glfw/ui_windows.go | 3 +-- 3 files changed, 21 insertions(+), 28 deletions(-) diff --git a/internal/uidriver/glfw/ui.go b/internal/uidriver/glfw/ui.go index 6bd4c3332..200f45692 100644 --- a/internal/uidriver/glfw/ui.go +++ b/internal/uidriver/glfw/ui.go @@ -53,7 +53,7 @@ type UserInterface struct { runnableInBackground bool vsync bool - lastMonitorScale float64 + lastDeviceScaleFactor float64 initMonitor *glfw.Monitor initFullscreenWidth int @@ -450,13 +450,12 @@ func (u *UserInterface) ScreenPadding() (x0, y0, x1, y1 float64) { vw := 0.0 vh := 0.0 _ = u.t.Call(func() error { - m := u.window.GetMonitor() - d = devicescale.GetAt(m.GetPos()) + d = u.deviceScaleFactor() sx = float64(u.width) * u.actualScreenScale() sy = float64(u.height) * u.actualScreenScale() gs = u.glfwScale() - v := m.GetVideoMode() + v := u.window.GetMonitor().GetVideoMode() vw, vh = float64(v.Width), float64(v.Height) return nil }) @@ -565,19 +564,28 @@ func (u *UserInterface) SetWindowResizable(resizable bool) { } func (u *UserInterface) DeviceScaleFactor() float64 { - f := 0.0 if !u.isRunning() { return devicescale.GetAt(u.initMonitor.GetPos()) } + f := 0.0 _ = u.t.Call(func() error { - m := u.currentMonitor() - f = devicescale.GetAt(m.GetPos()) + f = u.deviceScaleFactor() return nil }) return f } +// deviceScaleFactor must be called from the main thread. +func (u *UserInterface) deviceScaleFactor() float64 { + // Avoid calling monitor.GetPos if we have the monitor position cached already. + if cm, ok := getCachedMonitor(u.window.GetPos()); ok { + return devicescale.GetAt(cm.x, cm.y) + } + // TODO: When is this reached? + return devicescale.GetAt(u.currentMonitor().GetPos()) +} + func init() { // Lock the main thread. runtime.LockOSThread() @@ -793,16 +801,7 @@ func (u *UserInterface) getScale() float64 { // actualScreenScale must be called from the main thread. func (u *UserInterface) actualScreenScale() float64 { - return u.getScale() * u.monitorScale() -} - -// monitorScale must be called from the main thread. -func (u *UserInterface) monitorScale() float64 { - // Avoid calling monitor.GetPos if we have the monitor position cached already. - if cm, ok := getCachedMonitor(u.window.GetPos()); ok { - return devicescale.GetAt(cm.x, cm.y) - } - return devicescale.GetAt(u.currentMonitor().GetPos()) + return u.getScale() * u.deviceScaleFactor() } func (u *UserInterface) updateSize(context driver.UIContext) { @@ -945,7 +944,7 @@ func (u *UserInterface) setScreenSize(width, height int, scale float64, fullscre windowRecreated := false _ = u.t.Call(func() error { - if u.width == width && u.height == height && u.scale == scale && u.isFullscreen() == fullscreen && u.vsync == vsync && u.lastMonitorScale == u.monitorScale() { + if u.width == width && u.height == height && u.scale == scale && u.isFullscreen() == fullscreen && u.vsync == vsync && u.lastDeviceScaleFactor == u.deviceScaleFactor() { return nil } @@ -966,7 +965,7 @@ func (u *UserInterface) setScreenSize(width, height int, scale float64, fullscre u.width = width u.windowWidth = width - s := scale * devicescale.GetAt(u.currentMonitor().GetPos()) + s := scale * u.deviceScaleFactor() if int(float64(width)*s) < minWindowWidth { u.windowWidth = int(math.Ceil(float64(minWindowWidth) / s)) } @@ -974,7 +973,7 @@ func (u *UserInterface) setScreenSize(width, height int, scale float64, fullscre u.scale = scale u.fullscreenScale = 0 u.vsync = vsync - u.lastMonitorScale = u.monitorScale() + u.lastDeviceScaleFactor = u.deviceScaleFactor() // To make sure the current existing framebuffers are rendered, // swap buffers here before SetSize is called. diff --git a/internal/uidriver/glfw/ui_unix.go b/internal/uidriver/glfw/ui_unix.go index 141b7938e..b71d46398 100644 --- a/internal/uidriver/glfw/ui_unix.go +++ b/internal/uidriver/glfw/ui_unix.go @@ -26,12 +26,7 @@ import ( ) func (u *UserInterface) glfwScale() float64 { - // This function must be called on the main thread. - cm, ok := getCachedMonitor(u.window.GetPos()) - if !ok { - return devicescale.GetAt(u.currentMonitor().GetPos()) - } - return devicescale.GetAt(cm.x, cm.y) + return u.deviceScaleFactor() } func adjustWindowPosition(x, y int) (int, int) { diff --git a/internal/uidriver/glfw/ui_windows.go b/internal/uidriver/glfw/ui_windows.go index a561f07bc..6c1ebcf90 100644 --- a/internal/uidriver/glfw/ui_windows.go +++ b/internal/uidriver/glfw/ui_windows.go @@ -101,8 +101,7 @@ func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error { } func (u *UserInterface) glfwScale() float64 { - // This function must be called on the main thread. - return devicescale.GetAt(u.currentMonitor().GetPos()) + return u.deviceScaleFactor() } func adjustWindowPosition(x, y int) (int, int) {