radv, libdrm-mlg: windowed frames follow the window, clipped, off when it leaves

- drmMlgPlaceWindow (libdrm-mlg) places one frame: the window's content
  and the monitor's screen in global points (CoreGraphics' space), the
  output's mode and the image. A screen point is crtc/screen pixels, which
  is the backing scale and the mode in one factor. A window that fills the
  screen (or is in native full screen there) gets the primary plane;
  otherwise an overlay plane at its rectangle in CRTC pixels, clipped to
  the CRTC with the matching crop of the image on every edge. A window off
  the monitor, or with less of it there than DC's 12-pixel smallest
  viewport (which DC would grow without saying), gets nothing, with the
  reason. No window or no screen keeps the old rules.
- LX_SCANOUT HIDE: the client's frames off the screen (the desktop back,
  the overlay off) while it stays attached and keeps its overlay plane.
- The WSI (radv-macos-display-wsi.patch) reads the window at every
  present, in global points, and uses drmMlgPlaceWindow. Off the monitor
  it hides once, logs it, keeps the game running paced at the refresh
  rate, and shows the frames again when the window comes back; going full
  screen on the monitor when the window is elsewhere is gone. A place that
  changed is checked with TEST before it is presented; a refusal names the
  rectangles and the scale. A layer at another size than the images makes
  presents VK_SUBOPTIMAL_KHR, so a resized window or a full-screen toggle
  gets new images.
- Tests: test-mlg-place (1x and 2x, screens at any global position, full
  screen, clipping on each edge and a corner, a scaled window clipped, off
  the screen, the viewport minimum, modes, refusals); test-scanout (HIDE,
  a cropped present after it, a crop past the framebuffer refused);
  test-radv-scanout (suboptimal presents for a layer of another size).
This commit is contained in:
geramyloveless committed 2026-10-04 09:32:48 -07:00
1 parent cf6ca2a3e0
commit b004eb754d
10 files changed
+560 -114

No files matched your search

+11
View File
@@ -1155,6 +1155,17 @@ test-libdrm-mlg: lib
test: test-libdrm-mlg
CRASH_PATH_TESTS += test-libdrm-mlg
# drmMlgPlaceWindow: a window's place on the output (no device needed).
test-mlg-place: libdrm-mlg
clang -std=c11 -Wall -Wextra -Werror -g -fsanitize=address,undefined \
-I$(LIBDRM_MLG)/include -I$(LIBDRM_MLG)/include/libdrm \
libmlg_drm/libdrm/tests/test_mlg_place.c -L$(LIBDRM_MLG)/lib -ldrm_mlg \
-Wl,-rpath,$(abspath $(LIBDRM_MLG)/lib) -o $(BUILD)/test-mlg-place
$(BUILD)/test-mlg-place
test: test-mlg-place
.PHONY: test-mlg-place
# Client framebuffers on the display output (LX_SCANOUT): a libdrm-mlg
# client on the primary node of the CS fixture with its DCN 4.0.1 display.
test-scanout: lib
+40
View File
@@ -245,6 +245,46 @@ extern int drmMlgConnectorDisplay(int fd, uint32_t connector_id, uint32_t *displ
extern int drmMlgEdidIdentity(const void *edid, size_t length, uint32_t *vendor,
uint32_t *product, uint32_t *serial);
/* Where a window's frames go on the output (drmMlgPlaceWindow). Rectangles
* of macOS are in global points, origin top left (CoreGraphics' space);
* the monitor's screen covers the output's whole mode, so a point maps to
* crtc_w / screen width pixels (the screen's backing scale and the mode in
* one factor). */
#define DRM_MLG_PLACE_AUTO 0 /* full screen when the window fills its screen */
#define DRM_MLG_PLACE_FULLSCREEN 1
#define DRM_MLG_PLACE_WINDOWED 2
/* DC's smallest viewport (MIN_VIEWPORT_SIZE): a smaller visible part of a
* window is not shown rather than grown by DC. */
#define DRM_MLG_MIN_VIEWPORT 12
struct drm_mlg_place_in {
int mode; /* DRM_MLG_PLACE_* */
int have_screen; /* the monitor has a macOS screen */
double screen_x, screen_y, screen_w, screen_h;
int have_window; /* the layer is in a window */
int window_native_fullscreen; /* AppKit's full screen, on the monitor's screen */
double win_x, win_y, win_w, win_h; /* the window's content */
uint32_t crtc_w, crtc_h; /* the output's mode */
uint32_t image_w, image_h; /* the swapchain's images */
};
struct drm_mlg_placement {
uint32_t layer; /* MLG_LX_LAYER_*, or 0: nothing shown */
uint32_t src_x, src_y, src_w, src_h; /* in image pixels */
int32_t dst_x, dst_y; /* on the CRTC */
uint32_t dst_w, dst_h;
char why[128]; /* for layer 0 or an error: the reason */
};
/* The layer and rectangles for one frame: the primary plane, whole, for a
* window that fills the monitor's screen (or MODE_FULLSCREEN, or no screen
* to place it on); else an overlay plane at the window's content mapped to
* CRTC pixels, clipped to the CRTC with the matching crop of the image;
* nothing (layer 0, with why) when no part of the window is on the
* monitor, or the part is under DRM_MLG_MIN_VIEWPORT pixels. Returns 0, or
* -EINVAL with why for inputs that make no placement. */
extern int drmMlgPlaceWindow(const struct drm_mlg_place_in *in, struct drm_mlg_placement *out);
#if defined(__cplusplus)
}
#endif
+99
View File
@@ -2,6 +2,8 @@
* "the driver's display output"): LX_SCANOUT for primary-node proxies, and
* the macOS display that stands for a connector's monitor. */
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -139,3 +141,100 @@ int drmMlgConnectorDisplay(int fd, uint32_t connector_id, uint32_t *display_id)
*display_id = found;
return 0;
}
static void place_whole(const struct drm_mlg_place_in *in, struct drm_mlg_placement *out)
{
out->layer = MLG_LX_LAYER_PRIMARY;
out->src_x = out->src_y = 0;
out->src_w = in->image_w;
out->src_h = in->image_h;
out->dst_x = out->dst_y = 0;
out->dst_w = in->crtc_w;
out->dst_h = in->crtc_h;
}
int drmMlgPlaceWindow(const struct drm_mlg_place_in *in, struct drm_mlg_placement *out)
{
memset(out, 0, sizeof(*out));
if (!in->crtc_w || !in->crtc_h || !in->image_w || !in->image_h ||
in->mode < DRM_MLG_PLACE_AUTO || in->mode > DRM_MLG_PLACE_WINDOWED ||
(in->have_screen && (!(in->screen_w > 0) || !(in->screen_h > 0)))) {
snprintf(out->why, sizeof(out->why), "no mode, image or screen size to place with");
return -EINVAL;
}
if (in->mode == DRM_MLG_PLACE_FULLSCREEN) {
place_whole(in, out);
return 0;
}
if (!in->have_screen || !in->have_window) {
/* Nothing to relate the window to: full screen, or the image's
* size centred when windowed was asked for. */
if (in->mode == DRM_MLG_PLACE_AUTO) {
place_whole(in, out);
return 0;
}
out->layer = MLG_LX_LAYER_OVERLAY;
out->src_w = in->image_w < in->crtc_w ? in->image_w : in->crtc_w;
out->src_h = in->image_h < in->crtc_h ? in->image_h : in->crtc_h;
out->dst_w = out->src_w;
out->dst_h = out->src_h;
out->dst_x = (int32_t)(in->crtc_w - out->dst_w) / 2;
out->dst_y = (int32_t)(in->crtc_h - out->dst_h) / 2;
return 0;
}
if (!(in->win_w > 0) || !(in->win_h > 0)) {
snprintf(out->why, sizeof(out->why), "the window has no size");
out->layer = 0;
return 0;
}
/* The window in CRTC pixels: a screen point is crtc/screen pixels. */
const double kx = in->crtc_w / in->screen_w, ky = in->crtc_h / in->screen_h;
const double x0 = (in->win_x - in->screen_x) * kx, y0 = (in->win_y - in->screen_y) * ky;
const double x1 = x0 + in->win_w * kx, y1 = y0 + in->win_h * ky;
if (in->mode == DRM_MLG_PLACE_AUTO &&
(in->window_native_fullscreen ||
(x0 <= 0.5 && y0 <= 0.5 && x1 >= in->crtc_w - 0.5 && y1 >= in->crtc_h - 0.5))) {
place_whole(in, out);
return 0;
}
/* The part on the CRTC, and the part of the image that maps there. */
const double vx0 = fmax(x0, 0), vy0 = fmax(y0, 0);
const double vx1 = fmin(x1, in->crtc_w), vy1 = fmin(y1, in->crtc_h);
if (vx1 <= vx0 || vy1 <= vy0) {
snprintf(out->why, sizeof(out->why), "the window is off the monitor's screen");
return 0;
}
const int32_t dx0 = (int32_t)lround(vx0), dy0 = (int32_t)lround(vy0);
const int32_t dx1 = (int32_t)lround(vx1), dy1 = (int32_t)lround(vy1);
if (dx1 - dx0 < DRM_MLG_MIN_VIEWPORT || dy1 - dy0 < DRM_MLG_MIN_VIEWPORT) {
snprintf(out->why, sizeof(out->why),
"%dx%d pixels of the window are on the monitor, under DC's %d-pixel viewport",
dx1 - dx0, dy1 - dy0, DRM_MLG_MIN_VIEWPORT);
return 0;
}
const double sx = in->image_w / (x1 - x0), sy = in->image_h / (y1 - y0);
double sx0 = floor((dx0 - x0) * sx + 1e-6), sy0 = floor((dy0 - y0) * sy + 1e-6);
double sx1 = ceil((dx1 - x0) * sx - 1e-6), sy1 = ceil((dy1 - y0) * sy - 1e-6);
sx0 = fmax(sx0, 0);
sy0 = fmax(sy0, 0);
sx1 = fmin(sx1, in->image_w);
sy1 = fmin(sy1, in->image_h);
if (sx1 - sx0 < 1 || sy1 - sy0 < 1) {
snprintf(out->why, sizeof(out->why), "no pixel of the image falls on the monitor");
return 0;
}
out->layer = MLG_LX_LAYER_OVERLAY;
out->dst_x = dx0;
out->dst_y = dy0;
out->dst_w = (uint32_t)(dx1 - dx0);
out->dst_h = (uint32_t)(dy1 - dy0);
out->src_x = (uint32_t)sx0;
out->src_y = (uint32_t)sy0;
out->src_w = (uint32_t)(sx1 - sx0);
out->src_h = (uint32_t)(sy1 - sy0);
return 0;
}
+152
View File
@@ -0,0 +1,152 @@
/* drmMlgPlaceWindow (libdrm-mlg): where a window's frames go on the
* output. macOS global points (top left origin) to CRTC pixels for screens
* at different global positions and backing scales, the whole-screen and
* native full screen cases, clipping with the matching image crop on every
* edge, a window off the screen, a sliver under DC's minimum viewport, the
* modes, no screen, and refusals. */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xf86drm.h>
#define CHECK(c) do { if (!(c)) { fprintf(stderr, "%s:%d: CHECK failed: %s\n", \
__FILE__, __LINE__, #c); exit(1); } } while (0)
static struct drm_mlg_place_in dell(double sx, double sy, double sw, double sh)
{
return (struct drm_mlg_place_in){
.mode = DRM_MLG_PLACE_AUTO, .have_screen = 1,
.screen_x = sx, .screen_y = sy, .screen_w = sw, .screen_h = sh,
.have_window = 1, .crtc_w = 2560, .crtc_h = 1440, .image_w = 1920, .image_h = 1080,
};
}
static void window(struct drm_mlg_place_in *in, double x, double y, double w, double h)
{
in->win_x = x;
in->win_y = y;
in->win_w = w;
in->win_h = h;
}
static int is(const struct drm_mlg_placement *p, uint32_t layer, int32_t dx, int32_t dy, uint32_t dw,
uint32_t dh, uint32_t sx, uint32_t sy, uint32_t sw, uint32_t sh)
{
if (p->layer == layer && p->dst_x == dx && p->dst_y == dy && p->dst_w == dw && p->dst_h == dh &&
p->src_x == sx && p->src_y == sy && p->src_w == sw && p->src_h == sh)
return 1;
fprintf(stderr, "got layer %u dst %d,%d %ux%u src %u,%u %ux%u (%s)\n", p->layer, p->dst_x,
p->dst_y, p->dst_w, p->dst_h, p->src_x, p->src_y, p->src_w, p->src_h, p->why);
return 0;
}
int main(void)
{
struct drm_mlg_placement p;
struct drm_mlg_place_in in;
/* The Dell right of the built-in screen, at 1x: a 1920x1080 window
* at 320,180 on it is an overlay there, the whole image. */
in = dell(2336, 0, 2560, 1440);
window(&in, 2336 + 320, 180, 1920, 1080);
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 320, 180, 1920, 1080, 0, 0, 1920, 1080));
/* The same screen at 2x (1280x720 points on the 2560x1440 mode): a
* point is two pixels. */
in = dell(2336, 0, 1280, 720);
in.image_w = 1280;
in.image_h = 720;
window(&in, 2336 + 100, 50, 640, 360);
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 200, 100, 1280, 720, 0, 0, 1280, 720));
/* The Dell left of and above the main screen (negative coordinates). */
in = dell(-2560, -300, 2560, 1440);
window(&in, -2560 + 10, -300 + 20, 1920, 1080);
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 10, 20, 1920, 1080, 0, 0, 1920, 1080));
/* Hanging off the right edge by half: the left half of the image. */
in = dell(2336, 0, 2560, 1440);
window(&in, 2336 + 2560 - 960, 100, 1920, 1080);
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 1600, 100, 960, 1080, 0, 0, 960, 1080));
/* ... off the left edge (onto the built-in screen): the right part. */
window(&in, 2336 - 480, 100, 1920, 1080);
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 0, 100, 1440, 1080, 480, 0, 1440, 1080));
/* ... off the top and the bottom. */
window(&in, 2336 + 100, -540, 1920, 1080);
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 100, 0, 1920, 540, 0, 540, 1920, 540));
window(&in, 2336 + 100, 1440 - 300, 1920, 1080);
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 100, 1140, 1920, 300, 0, 0, 1920, 300));
/* A corner off both edges. */
window(&in, 2336 - 960, -540, 1920, 1080);
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 0, 0, 960, 540, 960, 540, 960, 540));
/* A window drawn at half the image's size (the image scaled down 2:1),
* half off the right edge: the crop is in image pixels. */
window(&in, 2336 + 2560 - 480, 100, 960, 540);
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 2080, 100, 480, 540, 0, 0, 960, 1080));
/* Entirely off: nothing, with the reason. */
window(&in, 100, 100, 1920, 1080);
CHECK(drmMlgPlaceWindow(&in, &p) == 0 && p.layer == 0 && strstr(p.why, "off"));
window(&in, 2336 + 2560, 0, 640, 480);
CHECK(drmMlgPlaceWindow(&in, &p) == 0 && p.layer == 0);
/* A sliver of 5 pixels: under DC's 12-pixel viewport, not grown. */
window(&in, 2336 - 1920 + 5, 100, 1920, 1080);
CHECK(drmMlgPlaceWindow(&in, &p) == 0 && p.layer == 0 && strstr(p.why, "12-pixel"));
window(&in, 2336 - 1920 + 12, 100, 1920, 1080);
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 0, 100, 12, 1080, 1908, 0, 12, 1080));
/* Filling the screen: the primary plane, whole; native full screen
* too; MODE_WINDOWED keeps such a window on the overlay. */
window(&in, 2336, 0, 2560, 1440);
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_PRIMARY, 0, 0, 2560, 1440, 0, 0, 1920, 1080));
in.mode = DRM_MLG_PLACE_WINDOWED;
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 0, 0, 2560, 1440, 0, 0, 1920, 1080));
in.mode = DRM_MLG_PLACE_AUTO;
window(&in, 2336 + 100, 100, 800, 600);
in.window_native_fullscreen = 1;
CHECK(drmMlgPlaceWindow(&in, &p) == 0 && p.layer == MLG_LX_LAYER_PRIMARY);
in.window_native_fullscreen = 0;
/* MODE_FULLSCREEN: the primary plane wherever the window is. */
in.mode = DRM_MLG_PLACE_FULLSCREEN;
window(&in, 100, 100, 640, 480);
CHECK(drmMlgPlaceWindow(&in, &p) == 0 && p.layer == MLG_LX_LAYER_PRIMARY);
/* No macOS screen for the monitor: full screen, or the image centred
* when windowed. */
in = dell(0, 0, 0, 0);
in.have_screen = 0;
CHECK(drmMlgPlaceWindow(&in, &p) == 0 && p.layer == MLG_LX_LAYER_PRIMARY);
in.mode = DRM_MLG_PLACE_WINDOWED;
CHECK(drmMlgPlaceWindow(&in, &p) == 0);
CHECK(is(&p, MLG_LX_LAYER_OVERLAY, 320, 180, 1920, 1080, 0, 0, 1920, 1080));
/* Refusals. */
in = dell(2336, 0, 2560, 1440);
in.crtc_w = 0;
CHECK(drmMlgPlaceWindow(&in, &p) == -EINVAL && p.why[0]);
in = dell(2336, 0, 0, 1440);
CHECK(drmMlgPlaceWindow(&in, &p) == -EINVAL);
in = dell(2336, 0, 2560, 1440);
in.mode = 7;
CHECK(drmMlgPlaceWindow(&in, &p) == -EINVAL);
printf("PASS mlg place: points to CRTC pixels at 1x and 2x and any global position, "
"full screen, clipping with the image crop on every edge, off the screen, the "
"12-pixel viewport, modes, no screen, refusals\n");
return 0;
}
+38 -1
View File
@@ -12,7 +12,8 @@
* from a client that is not attached, framebuffers of no file); presents on
* the primary plane (the desktop's frames wait and are counted), a present
* while the previous one is not committed (EBUSY), the overlay plane over
* the desktop, the syncobj signalling at the flip, TEST; detach; the output
* the desktop, the syncobj signalling at the flip, TEST; hide (a window off
* the monitor) and a cropped present after it; detach; the output
* relit under an attached client.
*
* This file is a library of its own linked against libdrm-mlg (as
@@ -388,6 +389,42 @@ int test_scanout(void)
CHECK(flip_wait(card, sync, 2000) == 0);
CHECK(EVENTUALLY(scanout_fx_pipes_showing(0x00aa0004u)));
/* A window that leaves the monitor: HIDE takes the overlay off
* and leaves the client attached; its next present (cropped:
* the window back half over the left edge) shows it again. */
{
int32_t x, y;
uint32_t w, h;
req = request(MLG_LX_SCANOUT_HIDE);
CHECK(drmMlgScanout(&req, &st) == 0 && st.attached && st.overlay_plane_id);
CHECK(EVENTUALLY(!scanout_fx_pipes_showing(0x00aa0004u) &&
one_pipe(scanout_fx_pipes_showing(0x00405060u))));
CHECK(!scanout_fx_overlay(&x, &y, &w, &h));
CHECK(drmMlgScanout(&req, &st) == 0); /* again: nothing to do */
req = request(MLG_LX_SCANOUT_PRESENT);
req.fd = card;
req.layer = MLG_LX_LAYER_OVERLAY;
req.fb_id = small.fb;
req.syncobj = sync;
req.src_x = 320;
req.src_y = 0;
req.src_w = 320;
req.src_h = 360;
req.dst_x = 0;
req.dst_y = 200;
req.dst_w = 320;
req.dst_h = 360;
CHECK(drmMlgScanout(&req, &st) == 0);
CHECK(flip_wait(card, sync, 2000) == 0);
CHECK(EVENTUALLY(scanout_fx_overlay(&x, &y, &w, &h) && x == 0 && y == 200 &&
w == 320 && h == 360));
/* A crop past the framebuffer is refused, not trimmed. */
req.op = MLG_LX_SCANOUT_TEST;
req.src_x = 400;
CHECK(drmMlgScanout(&req, &st) == -ERANGE);
}
/* Detach: the overlay goes; presents are refused. */
req = request(MLG_LX_SCANOUT_DETACH);
CHECK(drmMlgScanout(&req, &st) == 0 && !st.attached);
+4
View File
@@ -142,6 +142,9 @@ extern "C" {
* is not committed yet; the output's worker error otherwise.
* DETACH the desktop back on the primary plane, the overlay off; returns
* once that flip happened. Closing the client does the same.
* HIDE as DETACH, but the client stays attached (its overlay plane stays
* reserved): nothing of it is on screen until its next PRESENT (a
* window that left the monitor's screen).
* STATE the output and this client's attachment.
*
* src is in framebuffer pixels, dst in CRTC pixels; a zero width or height
@@ -151,6 +154,7 @@ extern "C" {
#define MLG_LX_SCANOUT_PRESENT 3u
#define MLG_LX_SCANOUT_DETACH 4u
#define MLG_LX_SCANOUT_STATE 5u
#define MLG_LX_SCANOUT_HIDE 6u
#define MLG_LX_LAYER_PRIMARY 1u
#define MLG_LX_LAYER_OVERLAY 2u
+9
View File
@@ -2313,6 +2313,15 @@ static int lx_scanout(struct pci_dev *pdev, void *owner, struct file *file,
ret = -EACCES;
}
break;
case MLG_LX_SCANOUT_HIDE:
/* Off the screen, still attached. */
if (!o)
ret = -ENOENT;
else if (o->client.owner != owner)
ret = -EACCES;
else
ret = scanout_detach(o);
break;
default:
ret = -EINVAL;
break;
+1 -1
View File
@@ -146,7 +146,7 @@
},
{
"patch": "patches/mesa/radv-macos-display-wsi.patch",
"sha256": "649c016d6c668f3d85f9010bb3c990ddb4ec3e3e00cc1f64ad03d1f6e7026f9b",
"sha256": "14eb3ad9366d4728c21f305de447c58a8ba584ffca949917fdd8b21f7ec2c488",
"files": [
"src/amd/vulkan/radv_instance.c",
"src/amd/vulkan/radv_wsi.h",
+201 -110
View File
@@ -20,10 +20,19 @@ from.
"auto"); without it the surface reports no presentation support. The
swapchain attaches to the output, makes its images framebuffers of the
primary node with modifiers both of the output's planes scan out, and
checks the first one with the driver. Each present goes on the primary
plane when the window fills its screen or is not on the monitor's
screen, else on an overlay plane at the window's rectangle
(MLG_WSI_MODE=fullscreen or windowed decides instead). FIFO: every frame
checks the first one with the driver. Each present is placed by
libdrm-mlg's drmMlgPlaceWindow from the window's content and the
monitor's screen in global points: the primary plane when the window
fills that screen, else an overlay plane at the window's rectangle in
CRTC pixels, clipped to the CRTC with the matching crop of the image,
following the window frame by frame. A window off the monitor (or with
less of it there than DC's smallest viewport) takes the frames off
(LX_SCANOUT HIDE): the desktop shows, the game goes on paced at the
refresh rate, and the frames come back with the window. A new place is
checked by the driver before it is used; a refusal names the place and
the scale. MLG_WSI_MODE=fullscreen or windowed decides instead of the
window, and a layer at another size than the images makes presents
return VK_SUBOPTIMAL_KHR. FIFO: every frame
is shown, the next one handed over once the previous reached the screen;
a per-image syncobj signals at the vblank that shows it, which acquire
and VK_KHR_present_wait wait on. The last swapchain of the process gives
@@ -90,7 +99,7 @@ index 80925d1..df9f1f2 100644
)
endif
diff --git a/src/vulkan/wsi/wsi_common_metal.c b/src/vulkan/wsi/wsi_common_metal.c
index 4fae988..c3cb703 100644
index 4fae988..ca7659d 100644
--- a/src/vulkan/wsi/wsi_common_metal.c
+++ b/src/vulkan/wsi/wsi_common_metal.c
@@ -15,7 +15,49 @@
@@ -238,7 +247,7 @@ index 4fae988..c3cb703 100644
static VkResult
wsi_metal_create_mem(const struct wsi_swapchain *chain,
const struct wsi_image_info *info,
@@ -929,3 +1017,923 @@ wsi_metal_configure_image(const struct wsi_swapchain *chain,
@@ -929,3 +1017,989 @@ wsi_metal_configure_image(const struct wsi_swapchain *chain,
return VK_SUCCESS;
}
@@ -478,11 +487,10 @@ index 4fae988..c3cb703 100644
+ VkResult status;
+ uint64_t serial;
+ uint64_t shown_present_id; /* the newest present id on screen */
+ /* What the last present did, to report changes. */
+ uint32_t last_layer;
+ int32_t last_x, last_y;
+ uint32_t last_w, last_h;
+ bool warned_away;
+ /* The last frame's placement (checked by the driver when it changes),
+ * and whether the window is off the monitor (nothing of ours on it). */
+ struct drm_mlg_placement last;
+ bool hidden;
+ struct wsi_mlg_image images[0];
+};
+VK_DEFINE_NONDISP_HANDLE_CASTS(wsi_mlg_swapchain, base.base, VkSwapchainKHR,
@@ -570,73 +578,77 @@ index 4fae988..c3cb703 100644
+ return kept;
+}
+
+/* Where the next present goes: the layer and the destination on the CRTC. */
+static void
+mlg_placement(struct wsi_mlg_swapchain *chain, uint32_t *layer, int32_t *x, int32_t *y,
+ uint32_t *w, uint32_t *h)
+/* Where the next frame goes (drmMlgPlaceWindow): the window's content and
+ * the monitor's screen in global points, the output's mode, the image. 0, or
+ * -EINVAL with the reason in out->why. */
+static int
+mlg_placement(struct wsi_mlg_swapchain *chain, struct drm_mlg_placement *out)
+{
+ struct drm_mlg_place_in in = {
+ .mode = chain->mode == MLG_MODE_FULLSCREEN ? DRM_MLG_PLACE_FULLSCREEN :
+ chain->mode == MLG_MODE_WINDOWED ? DRM_MLG_PLACE_WINDOWED : DRM_MLG_PLACE_AUTO,
+ .crtc_w = chain->output.width,
+ .crtc_h = chain->output.height,
+ .image_w = chain->extent.width,
+ .image_h = chain->extent.height,
+ };
+ struct wsi_mlg_window win;
+ const uint32_t mw = chain->output.width, mh = chain->output.height;
+
+ wsi_mlg_window_get(chain->window, &win);
+ const bool on_monitor = win.has_window && chain->display_id &&
+ win.display_id == chain->display_id;
+ in.have_screen = chain->display_id &&
+ wsi_mlg_display_bounds(chain->display_id, &in.screen_x, &in.screen_y,
+ &in.screen_w, &in.screen_h);
+ in.have_window = win.has_window;
+ /* Native full screen counts on the monitor's screen only. */
+ in.window_native_fullscreen = win.native_fullscreen && win.display_id == chain->display_id;
+ in.win_x = win.x;
+ in.win_y = win.y;
+ in.win_w = win.width;
+ in.win_h = win.height;
+ return drmMlgPlaceWindow(&in, out);
+}
+
+ if (chain->mode == MLG_MODE_FULLSCREEN ||
+ (chain->mode == MLG_MODE_AUTO && (!on_monitor || win.fullscreen))) {
+ if (chain->mode == MLG_MODE_AUTO && !on_monitor && win.has_window && chain->display_id &&
+ !chain->warned_away) {
+ mlg_error("the window left %s's screen; the frames stay full screen on %s",
+ chain->output.connector, chain->output.connector);
+ chain->warned_away = true;
+ }
+ *layer = MLG_LX_LAYER_PRIMARY;
+ *x = *y = 0;
+ *w = mw;
+ *h = mh;
+ return;
+ }
+ *layer = MLG_LX_LAYER_OVERLAY;
+ if (on_monitor && win.screen_width && win.screen_height) {
+ /* Screen pixels to CRTC pixels (the same with the virtual display's
+ * mode at 1x). */
+ *x = (int32_t)((int64_t)win.x * mw / win.screen_width);
+ *y = (int32_t)((int64_t)win.y * mh / win.screen_height);
+ *w = (uint32_t)((uint64_t)win.width * mw / win.screen_width);
+ *h = (uint32_t)((uint64_t)win.height * mh / win.screen_height);
+ } else {
+ /* A window elsewhere, or none: the image's size, centred. */
+ *w = MIN2(chain->extent.width, mw);
+ *h = MIN2(chain->extent.height, mh);
+ *x = (int32_t)(mw - *w) / 2;
+ *y = (int32_t)(mh - *h) / 2;
+ }
+ if (!*w || !*h) {
+ *w = MIN2(chain->extent.width, mw);
+ *h = MIN2(chain->extent.height, mh);
+ }
+static bool
+mlg_same_placement(const struct drm_mlg_placement *a, const struct drm_mlg_placement *b)
+{
+ return a->layer == b->layer && a->src_x == b->src_x && a->src_y == b->src_y &&
+ a->src_w == b->src_w && a->src_h == b->src_h && a->dst_x == b->dst_x &&
+ a->dst_y == b->dst_y && a->dst_w == b->dst_w && a->dst_h == b->dst_h;
+}
+
+static int
+mlg_scanout_image(struct wsi_mlg_swapchain *chain, uint32_t op, struct wsi_mlg_image *image,
+ uint32_t layer, int32_t x, int32_t y, uint32_t w, uint32_t h,
+ struct mlg_lx_scanout_state *st)
+ const struct drm_mlg_placement *p, struct mlg_lx_scanout_state *st)
+{
+ struct mlg_lx_scanout req = mlg_request(op);
+
+ req.fd = chain->card;
+ req.layer = layer;
+ req.layer = p->layer;
+ req.fb_id = image->fb_id;
+ req.syncobj = image->syncobj;
+ req.src_w = chain->extent.width;
+ req.src_h = chain->extent.height;
+ req.dst_x = x;
+ req.dst_y = y;
+ req.dst_w = w;
+ req.dst_h = h;
+ req.src_x = p->src_x;
+ req.src_y = p->src_y;
+ req.src_w = p->src_w;
+ req.src_h = p->src_h;
+ req.dst_x = p->dst_x;
+ req.dst_y = p->dst_y;
+ req.dst_w = p->dst_w;
+ req.dst_h = p->dst_h;
+ return drmMlgScanout(&req, st);
+}
+
+/* A driver check that failed, with what it was asked. */
+static void
+mlg_report_refusal(struct wsi_mlg_swapchain *chain, const char *what, const struct drm_mlg_placement *p,
+ int r)
+{
+ mlg_error("%s on %s refused (%s): %s, image %u,%u %ux%u to %d,%d %ux%u "
+ "(scale %.3f x %.3f)", what, chain->output.connector, layer_name(p->layer),
+ strerror(-r), p->src_x, p->src_y, p->src_w, p->src_h, p->dst_x, p->dst_y, p->dst_w,
+ p->dst_h, p->src_w ? (double)p->dst_w / p->src_w : 0.0,
+ p->src_h ? (double)p->dst_h / p->src_h : 0.0);
+}
+
+static VkResult
+mlg_surface_lost(struct wsi_mlg_swapchain *chain)
+{
@@ -794,8 +806,7 @@ index 4fae988..c3cb703 100644
+ struct wsi_mlg_swapchain *chain = (struct wsi_mlg_swapchain *)wsi_chain;
+ struct wsi_mlg_image *image = &chain->images[image_index];
+ struct mlg_lx_scanout_state st;
+ uint32_t layer, w, h;
+ int32_t x, y;
+ struct drm_mlg_placement place;
+ VkResult result;
+ int r;
+
@@ -819,27 +830,78 @@ index 4fae988..c3cb703 100644
+ }
+ }
+
+ mlg_placement(chain, &layer, &x, &y, &w, &h);
+ r = mlg_scanout_image(chain, MLG_LX_SCANOUT_PRESENT, image, layer, x, y, w, h, &st);
+ r = mlg_placement(chain, &place);
+ if (r) {
+ mlg_error("present on %s (%s, %dx%d at %d,%d) failed: %s%s%s", chain->output.connector,
+ layer_name(layer), w, h, x, y, strerror(-r),
+ st.error ? "; the display output stopped with " : "",
+ mlg_error("placing a frame on %s: %s", chain->output.connector, place.why);
+ result = mlg_surface_lost(chain);
+ goto out;
+ }
+
+ if (!place.layer) {
+ /* The window is off the monitor: the desktop shows there, the game
+ * goes on, and its frames wait one refresh each (FIFO pacing). */
+ if (!chain->hidden) {
+ struct mlg_lx_scanout req = mlg_request(MLG_LX_SCANOUT_HIDE);
+
+ r = drmMlgScanout(&req, &st);
+ if (r) {
+ mlg_error("taking the frames off %s failed: %s", chain->output.connector,
+ strerror(-r));
+ result = mlg_surface_lost(chain);
+ goto out;
+ }
+ for (uint32_t i = 0; i < chain->base.image_count; i++)
+ if (chain->images[i].state == MLG_IMAGE_SHOWN)
+ chain->images[i].state = MLG_IMAGE_IDLE;
+ fprintf(stderr, "mlg-wsi: %s: %s; the desktop shows there until the window "
+ "comes back\n", chain->output.connector, place.why);
+ chain->hidden = true;
+ }
+ image->state = MLG_IMAGE_IDLE;
+ if (present_id > chain->shown_present_id)
+ chain->shown_present_id = present_id;
+ chain->last = place;
+ mtx_unlock(&chain->lock);
+ if (chain->output.refresh_mhz)
+ usleep((useconds_t)(1000000000ull / chain->output.refresh_mhz));
+ return result;
+ }
+ if (chain->hidden) {
+ fprintf(stderr, "mlg-wsi: %s: the window is back\n", chain->output.connector);
+ chain->hidden = false;
+ }
+ /* A new place is checked by the driver first: a refusal names it. */
+ if (!mlg_same_placement(&place, &chain->last)) {
+ r = mlg_scanout_image(chain, MLG_LX_SCANOUT_TEST, image, &place, &st);
+ if (r) {
+ mlg_report_refusal(chain, "a frame's place", &place, r);
+ result = mlg_surface_lost(chain);
+ goto out;
+ }
+ }
+ r = mlg_scanout_image(chain, MLG_LX_SCANOUT_PRESENT, image, &place, &st);
+ if (r) {
+ mlg_error("present on %s (%s at %d,%d %ux%u) failed: %s%s%s", chain->output.connector,
+ layer_name(place.layer), place.dst_x, place.dst_y, place.dst_w, place.dst_h,
+ strerror(-r), st.error ? "; the display output stopped with " : "",
+ st.error ? strerror(-st.error) : "");
+ result = mlg_surface_lost(chain);
+ goto out;
+ }
+ if (layer != chain->last_layer || x != chain->last_x || y != chain->last_y ||
+ w != chain->last_w || h != chain->last_h) {
+ if (layer != chain->last_layer || getenv("MLG_WSI_DEBUG"))
+ fprintf(stderr, "mlg-wsi: %s: %ux%u frames on %s at %d,%d %ux%u\n",
+ chain->output.connector, chain->extent.width, chain->extent.height,
+ layer_name(layer), x, y, w, h);
+ chain->last_layer = layer;
+ chain->last_x = x;
+ chain->last_y = y;
+ chain->last_w = w;
+ chain->last_h = h;
+ if (place.layer != chain->last.layer || getenv("MLG_WSI_DEBUG"))
+ fprintf(stderr, "mlg-wsi: %s: %ux%u frames on %s at %d,%d %ux%u (image %u,%u %ux%u)\n",
+ chain->output.connector, chain->extent.width, chain->extent.height,
+ layer_name(place.layer), place.dst_x, place.dst_y, place.dst_w, place.dst_h,
+ place.src_x, place.src_y, place.src_w, place.src_h);
+ chain->last = place;
+ {
+ /* The layer at another size than the images: the application
+ * makes new ones (a window resized, full screen toggled). */
+ uint32_t lw = 0, lh = 0;
+
+ wsi_metal_layer_size(chain->surface->pLayer, &lw, &lh);
+ if (lw && lh && (lw != chain->extent.width || lh != chain->extent.height))
+ result = VK_SUBOPTIMAL_KHR;
+ }
+ image->state = MLG_IMAGE_QUEUED;
+ image->serial = ++chain->serial;
@@ -1120,21 +1182,34 @@ index 4fae988..c3cb703 100644
+ }
+ }
+
+ /* The driver's check of the first frame where it will go. */
+ /* The driver's check of the first frame where it will go (and of
+ * the whole screen: a window may go full screen later). */
+ {
+ uint32_t layer, w, h;
+ int32_t x, y;
+ struct drm_mlg_placement place, whole = {
+ .layer = MLG_LX_LAYER_PRIMARY, .src_w = chain->extent.width,
+ .src_h = chain->extent.height, .dst_w = chain->output.width,
+ .dst_h = chain->output.height,
+ };
+
+ mlg_placement(chain, &layer, &x, &y, &w, &h);
+ r = mlg_scanout_image(chain, MLG_LX_SCANOUT_TEST, &chain->images[0], layer, x, y, w, h, &st);
+ r = mlg_placement(chain, &place);
+ if (r) {
+ mlg_error("%s refuses a %ux%u image (modifier 0x%llx) on %s at %d,%d %ux%u: %s",
+ chain->output.connector, chain->extent.width, chain->extent.height,
+ (unsigned long long)chain->images[0].base.drm_modifier, layer_name(layer),
+ x, y, w, h, strerror(-r));
+ mlg_error("placing frames on %s: %s", chain->output.connector, place.why);
+ result = VK_ERROR_INITIALIZATION_FAILED;
+ goto fail_images;
+ }
+ for (int pass = 0; pass < 2; pass++) {
+ const struct drm_mlg_placement *p = pass ? &whole : &place;
+
+ if (!p->layer || (pass && mlg_same_placement(&place, &whole)))
+ continue;
+ r = mlg_scanout_image(chain, MLG_LX_SCANOUT_TEST, &chain->images[0], p, &st);
+ if (r) {
+ mlg_report_refusal(chain, "a swapchain image (modifier check)", p, r);
+ result = VK_ERROR_INITIALIZATION_FAILED;
+ goto fail_images;
+ }
+ }
+ chain->last = place;
+ }
+ fprintf(stderr, "mlg-wsi: %u %ux%u images on %s (%ux%u, modifier 0x%llx)\n", num_images,
+ chain->extent.width, chain->extent.height, chain->output.connector,
@@ -1163,7 +1238,7 @@ index 4fae988..c3cb703 100644
+
+#endif /* LIBDRM_MLG_SCANOUT */
diff --git a/src/vulkan/wsi/wsi_common_metal_layer.h b/src/vulkan/wsi/wsi_common_metal_layer.h
index e9473b1..eb88a90 100644
index e9473b1..571a7a6 100644
--- a/src/vulkan/wsi/wsi_common_metal_layer.h
+++ b/src/vulkan/wsi/wsi_common_metal_layer.h
@@ -33,6 +33,11 @@ void
@@ -1178,23 +1253,27 @@ index e9473b1..eb88a90 100644
CAMetalDrawable *
wsi_metal_layer_acquire_drawable(const CAMetalLayer *metal_layer);
@@ -52,4 +57,31 @@ wsi_metal_layer_blit_and_present(struct wsi_metal_layer_blit_context *context,
@@ -52,4 +57,35 @@ wsi_metal_layer_blit_and_present(struct wsi_metal_layer_blit_context *context,
CAMetalDrawable **drawable_ptr, void *buffer,
uint32_t width, uint32_t height, uint32_t row_pitch);
+/* ---- the layer's window (mac_linuxgpu; wsi_common_metal.c's display
+ * engine presents need it) ---- */
+
+/* Where a layer's window is, in pixels of its screen, origin top left. */
+/* Where a layer's window is: its content rectangle in global points,
+ * origin top left (CoreGraphics' space, as CGDisplayBounds gives screens). */
+struct wsi_mlg_window {
+ bool has_window; /* the layer is in a window on a screen */
+ bool fullscreen; /* the content covers the whole screen */
+ uint32_t display_id; /* the screen's CGDirectDisplayID */
+ int32_t x, y; /* the content rectangle */
+ uint32_t width, height;
+ uint32_t screen_width, screen_height;
+ bool native_fullscreen; /* AppKit's full screen */
+ uint32_t display_id; /* the window's screen (CGDirectDisplayID) */
+ double x, y, width, height;
+ double scale; /* the screen's backing scale (points to pixels) */
+};
+
+/* The bounds of display @display_id in global points (CGDisplayBounds). */
+bool wsi_mlg_display_bounds(uint32_t display_id, double *x, double *y, double *width,
+ double *height);
+
+struct wsi_mlg_window_cache;
+
+/* Follows the window of @layer. Reads it once on the main thread. */
@@ -1211,7 +1290,7 @@ index e9473b1..eb88a90 100644
+
#endif // WSI_COMMON_METAL_LAYER_H
diff --git a/src/vulkan/wsi/wsi_common_metal_layer.m b/src/vulkan/wsi/wsi_common_metal_layer.m
index 67c3e7f..0f9e10d 100644
index 67c3e7f..d6fd48e 100644
--- a/src/vulkan/wsi/wsi_common_metal_layer.m
+++ b/src/vulkan/wsi/wsi_common_metal_layer.m
@@ -8,6 +8,12 @@
@@ -1249,7 +1328,7 @@ index 67c3e7f..0f9e10d 100644
static VkResult
get_mtl_pixel_format(VkFormat format, MTLPixelFormat *metal_format)
{
@@ -343,3 +364,225 @@ wsi_metal_layer_blit_and_present(struct wsi_metal_layer_blit_context *context,
@@ -343,3 +364,237 @@ wsi_metal_layer_blit_and_present(struct wsi_metal_layer_blit_context *context,
*drawable_ptr = nil;
}
}
@@ -1306,29 +1385,41 @@ index 67c3e7f..0f9e10d 100644
+ NSView *view = layer_view(layer);
+ NSWindow *window = view.window;
+ NSScreen *screen = window.screen;
+ if (!view || !window || !screen)
+ NSArray<NSScreen *> *screens = [NSScreen screens];
+ if (!view || !window || !screen || !screens.count)
+ return;
+
+ NSNumber *number = screen.deviceDescription[@"NSScreenNumber"];
+ CGFloat scale = screen.backingScaleFactor;
+ NSRect content = [window convertRectToScreen:[view convertRect:view.bounds toView:nil]];
+ NSRect frame = screen.frame;
+ /* AppKit's global space has its origin at the bottom left of the
+ * first screen; CoreGraphics' at its top left, y downwards. */
+ const CGFloat top = NSMaxY(screens[0].frame);
+
+ out->has_window = number != nil;
+ out->display_id = number.unsignedIntValue;
+ out->x = (int32_t)lround((content.origin.x - frame.origin.x) * scale);
+ out->y = (int32_t)lround((NSMaxY(frame) - NSMaxY(content)) * scale);
+ out->width = (uint32_t)lround(content.size.width * scale);
+ out->height = (uint32_t)lround(content.size.height * scale);
+ out->screen_width = (uint32_t)lround(frame.size.width * scale);
+ out->screen_height = (uint32_t)lround(frame.size.height * scale);
+ out->fullscreen = (window.styleMask & NSWindowStyleMaskFullScreen) ||
+ (out->x <= 0 && out->y <= 0 &&
+ out->x + (int64_t)out->width >= out->screen_width &&
+ out->y + (int64_t)out->height >= out->screen_height);
+ out->x = content.origin.x;
+ out->y = top - NSMaxY(content);
+ out->width = content.size.width;
+ out->height = content.size.height;
+ out->scale = screen.backingScaleFactor;
+ out->native_fullscreen = (window.styleMask & NSWindowStyleMaskFullScreen) != 0;
+ }
+}
+
+bool
+wsi_mlg_display_bounds(uint32_t display_id, double *x, double *y, double *width, double *height)
+{
+ CGRect bounds = CGDisplayBounds(display_id);
+
+ if (CGRectIsEmpty(bounds))
+ return false;
+ *x = bounds.origin.x;
+ *y = bounds.origin.y;
+ *width = bounds.size.width;
+ *height = bounds.size.height;
+ return true;
+}
+
+static void
+cache_refresh(WsiMlgWindowCache *cache)
+{
+5 -2
View File
@@ -14,7 +14,8 @@
* images are tiled framebuffers on the output's primary plane in place of
* the desktop, acquired and presented frame after frame (each present's
* vblank before the next); the desktop back when the swapchain goes; a
* windowed swapchain on an overlay plane, centred. The software GPU runs
* windowed swapchain on an overlay plane, centred, its presents
* suboptimal for a layer of another size. The software GPU runs
* no shaders, so what the images contain is not checked: which
* framebuffers the planes scan out is. */
#include <dlfcn.h>
@@ -355,7 +356,9 @@ int main(int argc, char **argv)
VkPresentInfoKHR pi = { .sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR,
.waitSemaphoreCount = 1, .pWaitSemaphores = &r, .swapchainCount = 1,
.pSwapchains = &swapchain, .pImageIndices = &index };
VK(vkQueuePresentKHR(queue, &pi));
/* Shown, and suboptimal: the layer is 1920x1080, the images
* 640x360 (the application would make new ones). */
CHECK(vkQueuePresentKHR(queue, &pi) == VK_SUBOPTIMAL_KHR);
}
{
int32_t x, y;