mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-24 01:42:05 +01:00
internal/ui: move impls for device scale to internal/ui for desktops
This enables to remove restrictions for some functions to be called from the main thread. Updates #2423
This commit is contained in:
parent
458a415131
commit
c7d1d28582
@ -1,66 +0,0 @@
|
|||||||
// Copyright 2018 The Ebiten Authors
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
//go:build !android && !ios && !js
|
|
||||||
|
|
||||||
package devicescale
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
type posAndScale struct {
|
|
||||||
x, y int
|
|
||||||
scale float64
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
m sync.Mutex
|
|
||||||
cache []posAndScale
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetAt returns the device scale at (x, y), i.e. the number of device-dependent pixels per device-independent pixel.
|
|
||||||
// x and y are in device-dependent pixels and must be the top-left coordinate of a monitor, or 0,0 to request a "global scale".
|
|
||||||
func GetAt(x, y int) float64 {
|
|
||||||
m.Lock()
|
|
||||||
defer m.Unlock()
|
|
||||||
|
|
||||||
for _, p := range cache {
|
|
||||||
if p.x == x && p.y == y {
|
|
||||||
return p.scale
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
s := impl(x, y)
|
|
||||||
cache = append(cache, posAndScale{
|
|
||||||
x: x,
|
|
||||||
y: y,
|
|
||||||
scale: s,
|
|
||||||
})
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClearCache clears the cache.
|
|
||||||
// This should be called when monitors are changed by connecting or disconnecting.
|
|
||||||
func ClearCache() {
|
|
||||||
// TODO: This should be called not only when monitors are changed but also a monitor's scales are changed.
|
|
||||||
// The device scale can vary even for the same monitor.
|
|
||||||
// The only known case is when the application works on macOS, with OpenGL, with a wider screen mode,
|
|
||||||
// and in the fullscreen mode (#1573).
|
|
||||||
|
|
||||||
m.Lock()
|
|
||||||
defer m.Unlock()
|
|
||||||
|
|
||||||
cache = cache[:0]
|
|
||||||
}
|
|
@ -1,51 +0,0 @@
|
|||||||
// Copyright 2021 The Ebiten Authors
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
//go:build !android && !ios && !js
|
|
||||||
|
|
||||||
package devicescale
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
|
||||||
)
|
|
||||||
|
|
||||||
func monitorAt(x, y int) *glfw.Monitor {
|
|
||||||
// Note: this assumes that x, y are exact monitor origins.
|
|
||||||
// If they're not, this arbitrarily returns the first monitor.
|
|
||||||
monitors := glfw.GetMonitors()
|
|
||||||
for _, mon := range monitors {
|
|
||||||
mx, my := mon.GetPos()
|
|
||||||
if x == mx && y == my {
|
|
||||||
return mon
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return monitors[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
func impl(x, y int) float64 {
|
|
||||||
// Keep calling GetContentScale until the returned scale is 0 (#2051).
|
|
||||||
// Retry this at most 5 times to avoid an infinite loop.
|
|
||||||
for i := 0; i < 5; i++ {
|
|
||||||
// An error can happen e.g. when entering a screensaver on Windows (#2488).
|
|
||||||
sx, _, err := monitorAt(x, y).GetContentScale()
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if sx == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return float64(sx)
|
|
||||||
}
|
|
||||||
return 1
|
|
||||||
}
|
|
@ -20,7 +20,6 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/devicescale"
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,6 +32,7 @@ type Monitor struct {
|
|||||||
name string
|
name string
|
||||||
x int
|
x int
|
||||||
y int
|
y int
|
||||||
|
contentScale float64
|
||||||
videoModeScale float64
|
videoModeScale float64
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,6 +90,25 @@ func (m *monitors) update() {
|
|||||||
newMonitors := make([]*Monitor, 0, len(glfwMonitors))
|
newMonitors := make([]*Monitor, 0, len(glfwMonitors))
|
||||||
for i, m := range glfwMonitors {
|
for i, m := range glfwMonitors {
|
||||||
x, y := m.GetPos()
|
x, y := m.GetPos()
|
||||||
|
|
||||||
|
// TODO: Detect the update of the content scale by SetContentScaleCallback (#2343).
|
||||||
|
contentScale := 1.0
|
||||||
|
|
||||||
|
// Keep calling GetContentScale until the returned scale is 0 (#2051).
|
||||||
|
// Retry this at most 5 times to avoid an infinite loop.
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
// An error can happen e.g. when entering a screensaver on Windows (#2488).
|
||||||
|
sx, _, err := m.GetContentScale()
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if sx == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
contentScale = float64(sx)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
newMonitors = append(newMonitors, &Monitor{
|
newMonitors = append(newMonitors, &Monitor{
|
||||||
m: m,
|
m: m,
|
||||||
videoMode: m.GetVideoMode(),
|
videoMode: m.GetVideoMode(),
|
||||||
@ -97,6 +116,7 @@ func (m *monitors) update() {
|
|||||||
name: m.GetName(),
|
name: m.GetName(),
|
||||||
x: x,
|
x: x,
|
||||||
y: y,
|
y: y,
|
||||||
|
contentScale: contentScale,
|
||||||
videoModeScale: videoModeScale(m),
|
videoModeScale: videoModeScale(m),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -105,7 +125,5 @@ func (m *monitors) update() {
|
|||||||
m.monitors = newMonitors
|
m.monitors = newMonitors
|
||||||
m.m.Unlock()
|
m.m.Unlock()
|
||||||
|
|
||||||
devicescale.ClearCache()
|
|
||||||
|
|
||||||
atomic.StoreInt32(&m.updateCalled, 1)
|
atomic.StoreInt32(&m.updateCalled, 1)
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,6 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/devicescale"
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/file"
|
"github.com/hajimehoshi/ebiten/v2/internal/file"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
|
"github.com/hajimehoshi/ebiten/v2/internal/gamepad"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
||||||
@ -239,12 +238,9 @@ func (u *userInterfaceImpl) Monitor() *Monitor {
|
|||||||
|
|
||||||
// getMonitorFromPosition returns a monitor for the given window x/y,
|
// getMonitorFromPosition returns a monitor for the given window x/y,
|
||||||
// or returns nil if monitor is not found.
|
// or returns nil if monitor is not found.
|
||||||
//
|
|
||||||
// getMonitorFromPosition must be called on the main thread.
|
|
||||||
func getMonitorFromPosition(wx, wy int) *Monitor {
|
func getMonitorFromPosition(wx, wy int) *Monitor {
|
||||||
for _, m := range theMonitors.append(nil) {
|
for _, m := range theMonitors.append(nil) {
|
||||||
// TODO: Fix incorrectness in the cases of https://github.com/glfw/glfw/issues/1961.
|
// TODO: Fix incorrectness in the cases of https://github.com/glfw/glfw/issues/1961.
|
||||||
// See also internal/devicescale/impl_desktop.go for a maybe better way of doing this.
|
|
||||||
if m.x <= wx && wx < m.x+m.videoMode.Width && m.y <= wy && wy < m.y+m.videoMode.Height {
|
if m.x <= wx && wx < m.x+m.videoMode.Width && m.y <= wy && wy < m.y+m.videoMode.Height {
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
@ -759,7 +755,6 @@ func (u *userInterfaceImpl) DeviceScaleFactor() float64 {
|
|||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
// deviceScaleFactor must be called from the main thread.
|
|
||||||
func (u *userInterfaceImpl) deviceScaleFactor(monitor *Monitor) float64 {
|
func (u *userInterfaceImpl) deviceScaleFactor(monitor *Monitor) float64 {
|
||||||
// It is rare, but monitor can be nil when glfw.GetPrimaryMonitor returns nil.
|
// It is rare, but monitor can be nil when glfw.GetPrimaryMonitor returns nil.
|
||||||
// In this case, return 1 as a tentative scale (#1878).
|
// In this case, return 1 as a tentative scale (#1878).
|
||||||
@ -767,7 +762,7 @@ func (u *userInterfaceImpl) deviceScaleFactor(monitor *Monitor) float64 {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return devicescale.GetAt(monitor.x, monitor.y)
|
return monitor.contentScale
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -110,17 +110,14 @@ func videoModeScale(m *glfw.Monitor) float64 {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// dipFromGLFWMonitorPixel must be called from the main thread.
|
|
||||||
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 {
|
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 {
|
||||||
return x / (monitor.videoModeScale * u.deviceScaleFactor(monitor))
|
return x / (monitor.videoModeScale * u.deviceScaleFactor(monitor))
|
||||||
}
|
}
|
||||||
|
|
||||||
// dipFromGLFWPixel must be called from the main thread.
|
|
||||||
func (u *userInterfaceImpl) dipFromGLFWPixel(x float64, monitor *Monitor) float64 {
|
func (u *userInterfaceImpl) dipFromGLFWPixel(x float64, monitor *Monitor) float64 {
|
||||||
return x / u.deviceScaleFactor(monitor)
|
return x / u.deviceScaleFactor(monitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
// dipToGLFWPixel must be called from the main thread.
|
|
||||||
func (u *userInterfaceImpl) dipToGLFWPixel(x float64, monitor *Monitor) float64 {
|
func (u *userInterfaceImpl) dipToGLFWPixel(x float64, monitor *Monitor) float64 {
|
||||||
return x * u.deviceScaleFactor(monitor)
|
return x * u.deviceScaleFactor(monitor)
|
||||||
}
|
}
|
||||||
|
@ -91,17 +91,14 @@ func videoModeScale(monitor *glfw.Monitor) float64 {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// dipFromGLFWMonitorPixel must be called from the main thread.
|
|
||||||
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 {
|
func (u *userInterfaceImpl) dipFromGLFWMonitorPixel(x float64, monitor *Monitor) float64 {
|
||||||
return x / u.deviceScaleFactor(monitor)
|
return x / u.deviceScaleFactor(monitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
// dipFromGLFWPixel must be called from the main thread.
|
|
||||||
func (u *userInterfaceImpl) dipFromGLFWPixel(x float64, monitor *Monitor) float64 {
|
func (u *userInterfaceImpl) dipFromGLFWPixel(x float64, monitor *Monitor) float64 {
|
||||||
return x / u.deviceScaleFactor(monitor)
|
return x / u.deviceScaleFactor(monitor)
|
||||||
}
|
}
|
||||||
|
|
||||||
// dipToGLFWPixel must be called from the main thread.
|
|
||||||
func (u *userInterfaceImpl) dipToGLFWPixel(x float64, monitor *Monitor) float64 {
|
func (u *userInterfaceImpl) dipToGLFWPixel(x float64, monitor *Monitor) float64 {
|
||||||
return x * u.deviceScaleFactor(monitor)
|
return x * u.deviceScaleFactor(monitor)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user