internal/ui: bug fix: GetVideoMode must not be called from othere threads

Now setInitMonitor can be called from any other threads than the main
thread. Do not call this.

This change fixes the issue by using *Monitor instead of *glfw.Monitor.
This commit is contained in:
Hajime Hoshi 2023-09-23 17:27:57 +09:00
parent 4a0201e3b2
commit 7118057e4f
2 changed files with 15 additions and 15 deletions

View File

@ -79,7 +79,7 @@ type userInterfaceImpl struct {
// These values are not changed after initialized. // These values are not changed after initialized.
// TODO: the fullscreen size should be updated when the initial window position is changed? // TODO: the fullscreen size should be updated when the initial window position is changed?
initMonitor *glfw.Monitor initMonitor *Monitor
initDeviceScaleFactor float64 initDeviceScaleFactor float64
initFullscreenWidthInDIP int initFullscreenWidthInDIP int
initFullscreenHeightInDIP int initFullscreenHeightInDIP int
@ -187,7 +187,7 @@ func initialize() error {
return errors.New("ui: no monitor was found at initialize") return errors.New("ui: no monitor was found at initialize")
} }
theUI.setInitMonitor(m) theUI.setInitMonitor(theMonitors.monitorFromGLFWMonitor(m))
// Create system cursors. These cursors are destroyed at glfw.Terminate(). // Create system cursors. These cursors are destroyed at glfw.Terminate().
glfwSystemCursors[CursorShapeDefault] = nil glfwSystemCursors[CursorShapeDefault] = nil
@ -204,17 +204,17 @@ func initialize() error {
return nil return nil
} }
func (u *userInterfaceImpl) setInitMonitor(m *glfw.Monitor) { func (u *userInterfaceImpl) setInitMonitor(m *Monitor) {
u.m.Lock() u.m.Lock()
defer u.m.Unlock() defer u.m.Unlock()
u.initMonitor = m u.initMonitor = m
u.initDeviceScaleFactor = u.deviceScaleFactor(m)
// GetVideoMode must be called from the main thread, then call this here and record // TODO: Remove these members. These can be calculated anytime from initMonitor.
// initFullscreen{Width,Height}InDIP. u.initDeviceScaleFactor = u.deviceScaleFactor(m.m)
v := m.GetVideoMode() v := m.vm
u.initFullscreenWidthInDIP = int(u.dipFromGLFWMonitorPixel(float64(v.Width), m)) u.initFullscreenWidthInDIP = int(u.dipFromGLFWMonitorPixel(float64(v.Width), m.m))
u.initFullscreenHeightInDIP = int(u.dipFromGLFWMonitorPixel(float64(v.Height), m)) u.initFullscreenHeightInDIP = int(u.dipFromGLFWMonitorPixel(float64(v.Height), m.m))
} }
// AppendMonitors appends the current monitors to the passed in mons slice and returns it. // AppendMonitors appends the current monitors to the passed in mons slice and returns it.
@ -990,9 +990,9 @@ func (u *userInterfaceImpl) initOnMainThread(options *RunOptions) error {
} }
ww, wh := u.getInitWindowSizeInDIP() ww, wh := u.getInitWindowSizeInDIP()
initW := int(u.dipToGLFWPixel(float64(ww), u.initMonitor)) initW := int(u.dipToGLFWPixel(float64(ww), u.initMonitor.m))
initH := int(u.dipToGLFWPixel(float64(wh), u.initMonitor)) initH := int(u.dipToGLFWPixel(float64(wh), u.initMonitor.m))
if err := u.createWindow(initW, initH, u.initMonitor); err != nil { if err := u.createWindow(initW, initH, u.initMonitor.m); err != nil {
return err return err
} }
@ -1013,7 +1013,7 @@ func (u *userInterfaceImpl) initOnMainThread(options *RunOptions) error {
if max := u.initFullscreenHeightInDIP - wh; wy >= max { if max := u.initFullscreenHeightInDIP - wh; wy >= max {
wy = max wy = max
} }
u.setWindowPositionInDIP(wx, wy, u.initMonitor) u.setWindowPositionInDIP(wx, wy, u.initMonitor.m)
u.setWindowSizeInDIP(ww, wh, true) u.setWindowSizeInDIP(ww, wh, true)
// Maximizing a window requires a proper size and position. Call Maximize here (#1117). // Maximizing a window requires a proper size and position. Call Maximize here (#1117).
@ -1508,7 +1508,7 @@ func (u *userInterfaceImpl) updateVsyncOnRenderThread() {
// currentMonitor must be called on the main thread. // currentMonitor must be called on the main thread.
func (u *userInterfaceImpl) currentMonitor() *glfw.Monitor { func (u *userInterfaceImpl) currentMonitor() *glfw.Monitor {
if u.window == nil { if u.window == nil {
return u.initMonitor return u.initMonitor.m
} }
if m := monitorFromWindow(u.window); m != nil { if m := monitorFromWindow(u.window); m != nil {
return m return m

View File

@ -234,7 +234,7 @@ func (w *glfwWindow) SetMonitor(monitor *Monitor) {
return return
} }
if !w.ui.isRunning() { if !w.ui.isRunning() {
w.ui.setInitMonitor(monitor.m) w.ui.setInitMonitor(monitor)
return return
} }
w.ui.mainThread.Call(func() { w.ui.mainThread.Call(func() {