mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
Move VideoModeScaleAt into internal/uidriver/glfw. (#1812)
It's only used by GLFW, and has been in devicescale only to ease migration to separate video mode vs content scaling. Updates #1774
This commit is contained in:
parent
923c84a3d6
commit
5bc7b93e6f
@ -25,7 +25,6 @@ type pos struct {
|
|||||||
var (
|
var (
|
||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
cache = map[pos]float64{}
|
cache = map[pos]float64{}
|
||||||
videoModeScaleCache = map[pos]float64{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetAt returns the device scale at (x, y), i.e. the number of device-dependent pixels per device-independent pixel.
|
// GetAt returns the device scale at (x, y), i.e. the number of device-dependent pixels per device-independent pixel.
|
||||||
@ -46,16 +45,3 @@ func GetAt(x, y int) float64 {
|
|||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// VideoModeScaleAt returns the video mode scale scale at (x, y), i.e. the number of video mode pixels per device-dependent 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 VideoModeScaleAt(x, y int) float64 {
|
|
||||||
m.Lock()
|
|
||||||
defer m.Unlock()
|
|
||||||
if s, ok := videoModeScaleCache[pos{x, y}]; ok {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
s := videoModeScaleImpl(x, y)
|
|
||||||
videoModeScaleCache[pos{x, y}] = s
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
@ -103,7 +103,3 @@ func impl(x, y int) float64 {
|
|||||||
}
|
}
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func videoModeScaleImpl(x, y int) float64 {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
@ -30,7 +30,3 @@ import "C"
|
|||||||
func impl(x, y int) float64 {
|
func impl(x, y int) float64 {
|
||||||
return float64(C.devicePixelRatio())
|
return float64(C.devicePixelRatio())
|
||||||
}
|
}
|
||||||
|
|
||||||
func videoModeScaleImpl(x, y int) float64 {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
@ -33,7 +33,3 @@ func impl(x, y int) float64 {
|
|||||||
}
|
}
|
||||||
return ratio
|
return ratio
|
||||||
}
|
}
|
||||||
|
|
||||||
func videoModeScaleImpl(x, y int) float64 {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
@ -1,22 +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 (darwin && !ios) || windows
|
|
||||||
// +build darwin,!ios windows
|
|
||||||
|
|
||||||
package devicescale
|
|
||||||
|
|
||||||
func videoModeScaleImpl(x, y int) float64 {
|
|
||||||
return 1
|
|
||||||
}
|
|
@ -1,82 +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 && !darwin && !js && !windows
|
|
||||||
// +build !android,!darwin,!js,!windows
|
|
||||||
|
|
||||||
package devicescale
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math"
|
|
||||||
|
|
||||||
"github.com/jezek/xgb"
|
|
||||||
"github.com/jezek/xgb/randr"
|
|
||||||
"github.com/jezek/xgb/xproto"
|
|
||||||
)
|
|
||||||
|
|
||||||
func videoModeScaleImpl(x, y int) float64 {
|
|
||||||
// TODO: if https://github.com/glfw/glfw/issues/1961 gets fixed, this function may need revising.
|
|
||||||
// In case GLFW decides to switch to returning logical pixels, we can just return 1.
|
|
||||||
|
|
||||||
// Note: GLFW currently returns physical pixel sizes,
|
|
||||||
// but we need to predict the window system-side size of the fullscreen window
|
|
||||||
// for our `ScreenSizeInFullscreen` public API.
|
|
||||||
// Also at the moment we need this prior to switching to fullscreen, but that might be replacable.
|
|
||||||
// So this function computes the ratio of physical per logical pixels.
|
|
||||||
m := monitorAt(x, y)
|
|
||||||
monitorX, monitorY := m.GetPos()
|
|
||||||
xconn, err := xgb.NewConn()
|
|
||||||
defer xconn.Close()
|
|
||||||
if err != nil {
|
|
||||||
// No X11 connection?
|
|
||||||
// Assume we're on pure Wayland then.
|
|
||||||
// GLFW/Wayland shouldn't be having this issue.
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
if err = randr.Init(xconn); err != nil {
|
|
||||||
// No RANDR extension?
|
|
||||||
// No problem.
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
root := xproto.Setup(xconn).DefaultScreen(xconn).Root
|
|
||||||
res, err := randr.GetScreenResourcesCurrent(xconn, root).Reply()
|
|
||||||
if err != nil {
|
|
||||||
// Likely means RANDR is not working.
|
|
||||||
// No problem.
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
for _, crtc := range res.Crtcs[:res.NumCrtcs] {
|
|
||||||
info, err := randr.GetCrtcInfo(xconn, crtc, res.ConfigTimestamp).Reply()
|
|
||||||
if err != nil {
|
|
||||||
// This Crtc is bad. Maybe just got disconnected?
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if info.NumOutputs == 0 {
|
|
||||||
// This Crtc is not connected to any output.
|
|
||||||
// In other words, a disabled monitor.
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if int(info.X) == monitorX && int(info.Y) == monitorY {
|
|
||||||
xWidth, xHeight := info.Width, info.Height
|
|
||||||
vm := m.GetVideoMode()
|
|
||||||
physWidth, physHeight := vm.Width, vm.Height
|
|
||||||
// Return one scale, even though there may be separate X and Y scales.
|
|
||||||
// Return the _larger_ scale, as this would yield a letterboxed display on mismatch, rather than a cut-off one.
|
|
||||||
scale := math.Max(float64(physWidth)/float64(xWidth), float64(physHeight)/float64(xHeight))
|
|
||||||
return scale
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Monitor not known to XRandR. Weird.
|
|
||||||
return 1
|
|
||||||
}
|
|
@ -189,8 +189,7 @@ func initialize() error {
|
|||||||
m := currentMonitor(w)
|
m := currentMonitor(w)
|
||||||
theUI.initMonitor = m
|
theUI.initMonitor = m
|
||||||
v := m.GetVideoMode()
|
v := m.GetVideoMode()
|
||||||
mx, my := currentMonitor(w).GetPos()
|
scale := videoModeScale(currentMonitor(w))
|
||||||
scale := devicescale.VideoModeScaleAt(mx, my)
|
|
||||||
theUI.initFullscreenWidthInDP = int(theUI.fromGLFWMonitorPixel(float64(v.Width), scale))
|
theUI.initFullscreenWidthInDP = int(theUI.fromGLFWMonitorPixel(float64(v.Width), scale))
|
||||||
theUI.initFullscreenHeightInDP = int(theUI.fromGLFWMonitorPixel(float64(v.Height), scale))
|
theUI.initFullscreenHeightInDP = int(theUI.fromGLFWMonitorPixel(float64(v.Height), scale))
|
||||||
|
|
||||||
@ -232,6 +231,7 @@ func updateMonitors() {
|
|||||||
y: y,
|
y: y,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
clearVideoModeScaleCache()
|
||||||
}
|
}
|
||||||
|
|
||||||
func ensureMonitors() []*monitor {
|
func ensureMonitors() []*monitor {
|
||||||
@ -531,8 +531,7 @@ func (u *UserInterface) ScreenSizeInFullscreen() (int, int) {
|
|||||||
_ = u.t.Call(func() error {
|
_ = u.t.Call(func() error {
|
||||||
m := currentMonitor(u.window)
|
m := currentMonitor(u.window)
|
||||||
v := m.GetVideoMode()
|
v := m.GetVideoMode()
|
||||||
mx, my := m.GetPos()
|
s := videoModeScale(m)
|
||||||
s := devicescale.VideoModeScaleAt(mx, my)
|
|
||||||
w = int(u.fromGLFWMonitorPixel(float64(v.Width), s))
|
w = int(u.fromGLFWMonitorPixel(float64(v.Width), s))
|
||||||
h = int(u.fromGLFWMonitorPixel(float64(v.Height), s))
|
h = int(u.fromGLFWMonitorPixel(float64(v.Height), s))
|
||||||
return nil
|
return nil
|
||||||
@ -940,8 +939,7 @@ func (u *UserInterface) updateSize() (float64, float64, bool) {
|
|||||||
m := currentMonitor(u.window)
|
m := currentMonitor(u.window)
|
||||||
v := m.GetVideoMode()
|
v := m.GetVideoMode()
|
||||||
ww, wh := v.Width, v.Height
|
ww, wh := v.Width, v.Height
|
||||||
mx, my := m.GetPos()
|
s := videoModeScale(m)
|
||||||
s := devicescale.VideoModeScaleAt(mx, my)
|
|
||||||
w = u.fromGLFWMonitorPixel(float64(ww), s)
|
w = u.fromGLFWMonitorPixel(float64(ww), s)
|
||||||
h = u.fromGLFWMonitorPixel(float64(wh), s)
|
h = u.fromGLFWMonitorPixel(float64(wh), s)
|
||||||
} else {
|
} else {
|
||||||
|
@ -81,6 +81,14 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// clearVideoModeScaleCache must be called from the main thread.
|
||||||
|
func clearVideoModeScaleCache() {}
|
||||||
|
|
||||||
|
// videoModeScale must be called from the main thread.
|
||||||
|
func videoModeScale(m *glfw.Monitor) float64 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
// fromGLFWMonitorPixel must be called from the main thread.
|
// fromGLFWMonitorPixel must be called from the main thread.
|
||||||
func (u *UserInterface) fromGLFWMonitorPixel(x float64, videoModeScale float64) float64 {
|
func (u *UserInterface) fromGLFWMonitorPixel(x float64, videoModeScale float64) float64 {
|
||||||
// videoModeScale is always 1 on macOS,
|
// videoModeScale is always 1 on macOS,
|
||||||
|
@ -19,10 +19,96 @@
|
|||||||
package glfw
|
package glfw
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/driver"
|
"github.com/hajimehoshi/ebiten/v2/internal/driver"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
"github.com/hajimehoshi/ebiten/v2/internal/glfw"
|
||||||
|
"github.com/jezek/xgb"
|
||||||
|
"github.com/jezek/xgb/randr"
|
||||||
|
"github.com/jezek/xgb/xproto"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type videoModeScaleCacheKey struct{ X, Y int }
|
||||||
|
|
||||||
|
var videoModeScaleCache = map[videoModeScaleCacheKey]float64{}
|
||||||
|
|
||||||
|
// clearVideoModeScaleCache must be called from the main thread.
|
||||||
|
func clearVideoModeScaleCache() {
|
||||||
|
for k := range videoModeScaleCache {
|
||||||
|
delete(videoModeScaleCache, k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// videoModeScale must be called from the main thread.
|
||||||
|
func videoModeScale(m *glfw.Monitor) float64 {
|
||||||
|
// Caching wrapper for videoModeScaleUncached as
|
||||||
|
// videoModeScaleUncached may be expensive (uses blocking calls on X connection)
|
||||||
|
// and public ScreenSizeInFullscreen API needs the videoModeScale.
|
||||||
|
monitorX, monitorY := m.GetPos()
|
||||||
|
cacheKey := videoModeScaleCacheKey{X: monitorX, Y: monitorY}
|
||||||
|
if cached, ok := videoModeScaleCache[cacheKey]; ok {
|
||||||
|
return cached
|
||||||
|
}
|
||||||
|
scale := videoModeScaleUncached(m, monitorX, monitorY)
|
||||||
|
videoModeScaleCache[cacheKey] = scale
|
||||||
|
return scale
|
||||||
|
}
|
||||||
|
|
||||||
|
// videoModeScaleUncached must be called from the main thread.
|
||||||
|
func videoModeScaleUncached(m *glfw.Monitor, monitorX, monitorY int) float64 {
|
||||||
|
// TODO: if https://github.com/glfw/glfw/issues/1961 gets fixed, this function may need revising.
|
||||||
|
// In case GLFW decides to switch to returning logical pixels, we can just return 1.
|
||||||
|
|
||||||
|
// Note: GLFW currently returns physical pixel sizes,
|
||||||
|
// but we need to predict the window system-side size of the fullscreen window
|
||||||
|
// for our `ScreenSizeInFullscreen` public API.
|
||||||
|
// Also at the moment we need this prior to switching to fullscreen, but that might be replacable.
|
||||||
|
// So this function computes the ratio of physical per logical pixels.
|
||||||
|
xconn, err := xgb.NewConn()
|
||||||
|
defer xconn.Close()
|
||||||
|
if err != nil {
|
||||||
|
// No X11 connection?
|
||||||
|
// Assume we're on pure Wayland then.
|
||||||
|
// GLFW/Wayland shouldn't be having this issue.
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
if err = randr.Init(xconn); err != nil {
|
||||||
|
// No RANDR extension?
|
||||||
|
// No problem.
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
root := xproto.Setup(xconn).DefaultScreen(xconn).Root
|
||||||
|
res, err := randr.GetScreenResourcesCurrent(xconn, root).Reply()
|
||||||
|
if err != nil {
|
||||||
|
// Likely means RANDR is not working.
|
||||||
|
// No problem.
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
for _, crtc := range res.Crtcs[:res.NumCrtcs] {
|
||||||
|
info, err := randr.GetCrtcInfo(xconn, crtc, res.ConfigTimestamp).Reply()
|
||||||
|
if err != nil {
|
||||||
|
// This Crtc is bad. Maybe just got disconnected?
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if info.NumOutputs == 0 {
|
||||||
|
// This Crtc is not connected to any output.
|
||||||
|
// In other words, a disabled monitor.
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if int(info.X) == monitorX && int(info.Y) == monitorY {
|
||||||
|
xWidth, xHeight := info.Width, info.Height
|
||||||
|
vm := m.GetVideoMode()
|
||||||
|
physWidth, physHeight := vm.Width, vm.Height
|
||||||
|
// Return one scale, even though there may be separate X and Y scales.
|
||||||
|
// Return the _larger_ scale, as this would yield a letterboxed display on mismatch, rather than a cut-off one.
|
||||||
|
scale := math.Max(float64(physWidth)/float64(xWidth), float64(physHeight)/float64(xHeight))
|
||||||
|
return scale
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Monitor not known to XRandR. Weird.
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
// fromGLFWMonitorPixel must be called from the main thread.
|
// fromGLFWMonitorPixel must be called from the main thread.
|
||||||
func (u *UserInterface) fromGLFWMonitorPixel(x float64, videoModeScale float64) float64 {
|
func (u *UserInterface) fromGLFWMonitorPixel(x float64, videoModeScale float64) float64 {
|
||||||
return x / (videoModeScale * u.deviceScaleFactor())
|
return x / (videoModeScale * u.deviceScaleFactor())
|
||||||
|
@ -98,6 +98,14 @@ func getMonitorInfoW(hMonitor uintptr, lpmi *monitorInfo) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// clearVideoModeScaleCache must be called from the main thread.
|
||||||
|
func clearVideoModeScaleCache() {}
|
||||||
|
|
||||||
|
// videoModeScale must be called from the main thread.
|
||||||
|
func videoModeScale(m *glfw.Monitor) float64 {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
// fromGLFWMonitorPixel must be called from the main thread.
|
// fromGLFWMonitorPixel must be called from the main thread.
|
||||||
func (u *UserInterface) fromGLFWMonitorPixel(x float64, videoModeScale float64) float64 {
|
func (u *UserInterface) fromGLFWMonitorPixel(x float64, videoModeScale float64) float64 {
|
||||||
return x / (videoModeScale * u.deviceScaleFactor())
|
return x / (videoModeScale * u.deviceScaleFactor())
|
||||||
|
Loading…
Reference in New Issue
Block a user