uidriver/glfw: Refactoring: Limit the calls of devicescale.GetAt

This commit is contained in:
Hajime Hoshi 2019-12-08 20:24:20 +09:00
parent 8e9f5b9535
commit a0cf8bac21
3 changed files with 21 additions and 28 deletions

View File

@ -53,7 +53,7 @@ type UserInterface struct {
runnableInBackground bool runnableInBackground bool
vsync bool vsync bool
lastMonitorScale float64 lastDeviceScaleFactor float64
initMonitor *glfw.Monitor initMonitor *glfw.Monitor
initFullscreenWidth int initFullscreenWidth int
@ -450,13 +450,12 @@ func (u *UserInterface) ScreenPadding() (x0, y0, x1, y1 float64) {
vw := 0.0 vw := 0.0
vh := 0.0 vh := 0.0
_ = u.t.Call(func() error { _ = u.t.Call(func() error {
m := u.window.GetMonitor() d = u.deviceScaleFactor()
d = devicescale.GetAt(m.GetPos())
sx = float64(u.width) * u.actualScreenScale() sx = float64(u.width) * u.actualScreenScale()
sy = float64(u.height) * u.actualScreenScale() sy = float64(u.height) * u.actualScreenScale()
gs = u.glfwScale() gs = u.glfwScale()
v := m.GetVideoMode() v := u.window.GetMonitor().GetVideoMode()
vw, vh = float64(v.Width), float64(v.Height) vw, vh = float64(v.Width), float64(v.Height)
return nil return nil
}) })
@ -565,19 +564,28 @@ func (u *UserInterface) SetWindowResizable(resizable bool) {
} }
func (u *UserInterface) DeviceScaleFactor() float64 { func (u *UserInterface) DeviceScaleFactor() float64 {
f := 0.0
if !u.isRunning() { if !u.isRunning() {
return devicescale.GetAt(u.initMonitor.GetPos()) return devicescale.GetAt(u.initMonitor.GetPos())
} }
f := 0.0
_ = u.t.Call(func() error { _ = u.t.Call(func() error {
m := u.currentMonitor() f = u.deviceScaleFactor()
f = devicescale.GetAt(m.GetPos())
return nil return nil
}) })
return f 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() { func init() {
// Lock the main thread. // Lock the main thread.
runtime.LockOSThread() runtime.LockOSThread()
@ -793,16 +801,7 @@ func (u *UserInterface) getScale() float64 {
// actualScreenScale must be called from the main thread. // actualScreenScale must be called from the main thread.
func (u *UserInterface) actualScreenScale() float64 { func (u *UserInterface) actualScreenScale() float64 {
return u.getScale() * u.monitorScale() return u.getScale() * u.deviceScaleFactor()
}
// 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())
} }
func (u *UserInterface) updateSize(context driver.UIContext) { func (u *UserInterface) updateSize(context driver.UIContext) {
@ -945,7 +944,7 @@ func (u *UserInterface) setScreenSize(width, height int, scale float64, fullscre
windowRecreated := false windowRecreated := false
_ = u.t.Call(func() error { _ = 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 return nil
} }
@ -966,7 +965,7 @@ func (u *UserInterface) setScreenSize(width, height int, scale float64, fullscre
u.width = width u.width = width
u.windowWidth = width u.windowWidth = width
s := scale * devicescale.GetAt(u.currentMonitor().GetPos()) s := scale * u.deviceScaleFactor()
if int(float64(width)*s) < minWindowWidth { if int(float64(width)*s) < minWindowWidth {
u.windowWidth = int(math.Ceil(float64(minWindowWidth) / s)) 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.scale = scale
u.fullscreenScale = 0 u.fullscreenScale = 0
u.vsync = vsync u.vsync = vsync
u.lastMonitorScale = u.monitorScale() u.lastDeviceScaleFactor = u.deviceScaleFactor()
// To make sure the current existing framebuffers are rendered, // To make sure the current existing framebuffers are rendered,
// swap buffers here before SetSize is called. // swap buffers here before SetSize is called.

View File

@ -26,12 +26,7 @@ import (
) )
func (u *UserInterface) glfwScale() float64 { func (u *UserInterface) glfwScale() float64 {
// This function must be called on the main thread. return u.deviceScaleFactor()
cm, ok := getCachedMonitor(u.window.GetPos())
if !ok {
return devicescale.GetAt(u.currentMonitor().GetPos())
}
return devicescale.GetAt(cm.x, cm.y)
} }
func adjustWindowPosition(x, y int) (int, int) { func adjustWindowPosition(x, y int) (int, int) {

View File

@ -101,8 +101,7 @@ func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error {
} }
func (u *UserInterface) glfwScale() float64 { func (u *UserInterface) glfwScale() float64 {
// This function must be called on the main thread. return u.deviceScaleFactor()
return devicescale.GetAt(u.currentMonitor().GetPos())
} }
func adjustWindowPosition(x, y int) (int, int) { func adjustWindowPosition(x, y int) (int, int) {