internal/ui: bug fix: DeviceScaleFactor should be initialized asap on Android

Closes #2913
This commit is contained in:
Hajime Hoshi 2024-02-24 19:48:17 +09:00
parent 5f08bbf01c
commit 40f3cbe1e3

View File

@ -176,9 +176,6 @@ func (u *UserInterface) update() error {
renderEndCh <- struct{}{} renderEndCh <- struct{}{}
}() }()
// The device scale factor can be obtained after the main function starts, especially on Android.
theMonitor.initDeviceScaleFactorIfNeeded()
w, h := u.outsideSize() w, h := u.outsideSize()
if err := u.context.updateFrame(u.graphicsDriver, w, h, theMonitor.DeviceScaleFactor(), u); err != nil { if err := u.context.updateFrame(u.graphicsDriver, w, h, theMonitor.DeviceScaleFactor(), u); err != nil {
return err return err
@ -197,11 +194,11 @@ func (u *UserInterface) ScreenSizeInFullscreen() (int, int) {
// SetOutsideSize is concurrent safe. // SetOutsideSize is concurrent safe.
func (u *UserInterface) SetOutsideSize(outsideWidth, outsideHeight float64) { func (u *UserInterface) SetOutsideSize(outsideWidth, outsideHeight float64) {
u.m.Lock() u.m.Lock()
defer u.m.Unlock()
if u.outsideWidth != outsideWidth || u.outsideHeight != outsideHeight { if u.outsideWidth != outsideWidth || u.outsideHeight != outsideHeight {
u.outsideWidth = outsideWidth u.outsideWidth = outsideWidth
u.outsideHeight = outsideHeight u.outsideHeight = outsideHeight
} }
u.m.Unlock()
} }
func (u *UserInterface) CursorMode() CursorMode { func (u *UserInterface) CursorMode() CursorMode {
@ -267,7 +264,8 @@ func (u *UserInterface) Window() Window {
} }
type Monitor struct { type Monitor struct {
deviceScaleFactor float64 deviceScaleFactor float64
deviceScaleFactorOnce sync.Once
m sync.Mutex m sync.Mutex
} }
@ -278,19 +276,16 @@ func (m *Monitor) Name() string {
return "" return ""
} }
func (m *Monitor) initDeviceScaleFactorIfNeeded() {
// Assume that the device scale factor never changes on mobiles.
m.m.Lock()
defer m.m.Unlock()
if m.deviceScaleFactor != 0 {
return
}
m.deviceScaleFactor = deviceScaleFactorImpl()
}
func (m *Monitor) DeviceScaleFactor() float64 { func (m *Monitor) DeviceScaleFactor() float64 {
m.m.Lock() m.m.Lock()
defer m.m.Unlock() defer m.m.Unlock()
// The device scale factor can be obtained after the main function starts, especially on Android.
// Initialize this lazily.
m.deviceScaleFactorOnce.Do(func() {
// Assume that the device scale factor never changes on mobiles.
m.deviceScaleFactor = deviceScaleFactorImpl()
})
return m.deviceScaleFactor return m.deviceScaleFactor
} }