Files
Reims-Go/internal/host/display.go
T
NwflowerandClaude Opus 5.5 181ad64618 fix(display): mark HiDPI presets by reims-vgpu's own rule
Presets were flagged HiDPI by screen width alone, so a 2880x1800 preset
read "HiDPI" on a 100% screen where macOS runs it at 1x, and a VM left at
the 1920x1080 default ran 1x on a 200% screen.

- GuestStartsHiDPI follows upstream reims-vgpu 3731afd
  (RgfxDisplayIdentity.hpp, reims-vgpu-window features.md): HiDPI from a
  2048x1536 window above 160 ppi (150% scaling or more), or from
  3840x2160 at any scale.
- Presets are the 16:9 or 16:10 sizes that fit the screen's work area
  below a title bar, largest four; the largest HiDPI one is recommended.
  The display probe now reports the work area.
- When no preset is HiDPI, the resolution row says how to get there:
  borderless full screen, 150% Windows scaling, or not on this screen.

On this host (3072x1920 at 200%) the presets are 2560x1600 (HiDPI,
recommended), 1920x1200, 1680x1050 and 1280x800.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 23:48:52 -07:00

191 lines
6.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package host
import (
"fmt"
"math"
)
// DisplayInfo describes a connected physical monitor on the host.
type DisplayInfo struct {
Name string `json:"name,omitempty"`
Width int `json:"width"`
Height int `json:"height"`
ScalePct int `json:"scale_pct"` // Windows DPI scaling percentage, e.g. 100, 125, 150, 200
RefreshHz int `json:"refresh_hz,omitempty"`
Primary bool `json:"primary"`
// WorkWidth and WorkHeight are the work area (the screen less the taskbar) in
// pixels; 0 where unknown.
WorkWidth int `json:"work_width,omitempty"`
WorkHeight int `json:"work_height,omitempty"`
}
// DisplayPreset is a suggested macOS VM resolution option tailored to the host monitor.
type DisplayPreset struct {
Resolution string `json:"resolution"`
Label string `json:"label"`
// HiDPI: macOS starts this window size HiDPI on the primary screen (GuestStartsHiDPI).
HiDPI bool `json:"hidpi"`
Recommended bool `json:"recommended,omitempty"`
}
// PrimaryDisplayOf picks the primary monitor (or the first detected monitor).
func PrimaryDisplayOf(displays []DisplayInfo) *DisplayInfo {
for i := range displays {
if displays[i].Primary {
cp := displays[i]
return &cp
}
}
if len(displays) > 0 {
cp := displays[0]
return &cp
}
return nil
}
// When an x86-64 macOS guest on reims-vgpu starts its desktop HiDPI. Upstream
// steelbrain/reims-vgpu 3731afd, guest/kext/src/RgfxDisplayIdentity.hpp:33-41
// and 68-79, crates/reims-vgpu-window/features.md:138-150:
// - the display has 110 pixels per inch a point at the host screen's scale;
// - it starts HiDPI from a window of 2048 x 1536 pixels above 160 pixels per
// inch, which is a screen at 150% or more (145% gives exactly 160);
// - and from 3840 x 2160 pixels at any scale.
//
// On Windows the window's points are the screen's pixels
// (crates/reims-vgpu-window/src/geometry.rs:68-73), so --resolution is the
// window's size in pixels and a borderless window is the screen's.
const (
guestPointsPerInch = 110
guestHiDPIMinPPI = 160
guestHiDPIMinWidth = 2048
guestHiDPIMinHeight = 1536
guestHiDPIAnyWidth = 3840
guestHiDPIAnyHeight = 2160
windowTitleBarPoints = 32 // a window's title bar and frame, in Windows points
defaultTaskbarPoints = 48 // used where the work area is unknown
maxDisplayPresetCount = 4
)
// guestPixelsPerInch mirrors rgfxDisplayPixelsPerInch for a screen at scalePct.
func guestPixelsPerInch(scalePct int) int {
if scalePct <= 0 {
return guestPointsPerInch
}
scale120ths := (scalePct*120 + 50) / 100
return (guestPointsPerInch*scale120ths + 60) / 120
}
// GuestStartsHiDPI reports whether macOS starts HiDPI in a reims-vgpu window of
// w x h pixels on a host screen at scalePct.
func GuestStartsHiDPI(w, h, scalePct int) bool {
if w >= guestHiDPIAnyWidth && h >= guestHiDPIAnyHeight {
return true
}
return guestPixelsPerInch(scalePct) > guestHiDPIMinPPI && w >= guestHiDPIMinWidth && h >= guestHiDPIMinHeight
}
// FullscreenStartsHiDPI reports whether a borderless window, which fills the
// screen, starts macOS HiDPI on d.
func FullscreenStartsHiDPI(d *DisplayInfo) bool {
if d == nil || d.Width <= 0 || d.Height <= 0 {
return false
}
return GuestStartsHiDPI(d.Width, d.Height, d.ScalePct)
}
// What HiDPIAdvice returns.
const (
HiDPIAdviceFullscreen = "fullscreen" // a borderless window is HiDPI
HiDPIAdviceScale = "scale" // the screen would be at 150% Windows scaling
HiDPIAdviceUnavailable = "unavailable" // too few pixels at any scale
)
// HiDPIAdvice says how to get a HiDPI desktop on d when none of presets is
// one, and "" when one is or nothing is known about d.
func HiDPIAdvice(d *DisplayInfo, presets []DisplayPreset) string {
if d == nil || d.Width <= 0 || d.Height <= 0 {
return ""
}
for _, p := range presets {
if p.HiDPI {
return ""
}
}
switch {
case FullscreenStartsHiDPI(d):
return HiDPIAdviceFullscreen
case d.ScalePct < 150 && GuestStartsHiDPI(d.Width, d.Height, 150):
return HiDPIAdviceScale
default:
return HiDPIAdviceUnavailable
}
}
var (
presetSizes16x9 = [][2]int{{1280, 720}, {1600, 900}, {1920, 1080}, {2560, 1440}, {2880, 1620}, {3200, 1800}, {3840, 2160}}
presetSizes16x10 = [][2]int{{1280, 800}, {1680, 1050}, {1920, 1200}, {2560, 1600}, {2880, 1800}, {3840, 2400}}
)
// RecommendDisplayPresets lists window sizes that fit the primary screen's
// work area below a title bar, largest first, each marked HiDPI where macOS
// starts it so there. The recommended one is the largest HiDPI size, else the
// largest size: text on a 1x macOS desktop is visibly rough, as macOS has no
// subpixel antialiasing.
func RecommendDisplayPresets(primary *DisplayInfo) ([]DisplayPreset, string) {
if primary == nil || primary.Width <= 0 || primary.Height <= 0 {
// Nothing known about the screen: no size can be called HiDPI.
return []DisplayPreset{
{Resolution: "1920x1080", Label: "1920 × 1080", Recommended: true},
{Resolution: "2560x1440", Label: "2560 × 1440"},
{Resolution: "2880x1800", Label: "2880 × 1800"},
}, "1920x1080"
}
scale := primary.ScalePct
if scale <= 0 {
scale = 100
}
usableW, usableH := primary.WorkWidth, primary.WorkHeight
if usableW <= 0 || usableH <= 0 {
usableW = primary.Width
usableH = primary.Height - defaultTaskbarPoints*scale/100
}
usableH -= windowTitleBarPoints * scale / 100
sizes := presetSizes16x9
if math.Abs(float64(primary.Width)/float64(primary.Height)-1.6) < 0.08 {
sizes = presetSizes16x10
}
var fit [][2]int
for i := len(sizes) - 1; i >= 0; i-- {
if sizes[i][0] <= usableW && sizes[i][1] <= usableH {
fit = append(fit, sizes[i])
}
}
if len(fit) == 0 {
fit = [][2]int{sizes[0]} // a screen smaller than every size: the window shrinks to fit
}
if len(fit) > maxDisplayPresetCount {
fit = fit[:maxDisplayPresetCount]
}
out := make([]DisplayPreset, 0, len(fit))
rec := -1
for i, sz := range fit {
hidpi := GuestStartsHiDPI(sz[0], sz[1], scale)
if hidpi && rec < 0 {
rec = i
}
out = append(out, DisplayPreset{
Resolution: fmt.Sprintf("%dx%d", sz[0], sz[1]),
Label: fmt.Sprintf("%d × %d", sz[0], sz[1]),
HiDPI: hidpi,
})
}
if rec < 0 {
rec = 0
}
out[rec].Recommended = true
return out, out[rec].Resolution
}