mirror of
https://github.com/lemonade-sdk/mac_linuxgpu.git
synced 2026-11-22 23:57:31 -10:00
display: copy a region redrawn in place once per flip
Measured on the R9700 (build 235, the Dell at 2560x1440): a window redrawing a 1732x1140 region every frame made every flip copy about 24.8 MB from the capture surface, three times the region: the frame's damage plus the same region again for each scanout buffer that had missed it, kept as separate rectangles. At 0.73 ms per MiB over Thunderbolt that is 17.3 ms of copy per 16.7 ms frame, so frames were replaced before they could flip (55 flips/s) and the picture lagged; a full-screen workload stopped the output when its copies backed up past the flip timeout. Damage now folds a rectangle into any it overlaps closely (contained, equal, or overlapping so the box is no larger than the two), so the region stays one rectangle and is copied once; damage covering nine tenths of the frame becomes one whole-frame copy. The flip wait first waits for its own copies (bounded at 2 s, as GPU work), then for the flip (200 ms), and says which did not complete. printk_once and the DRM *_ONCE messages are now once per call site: DM's MST/DSC check logged "dsc precompute is not needed" on every commit, sixty times a second, filling the kernel log. The display agent also finishes launching as a background agent only after its virtual display has a mode: made an NSApplication earlier, the process never saw the new display's modes. test_display_pipeline: a window redrawn in place with an item inside it copies the window's bytes once per flip.
This commit is contained in:
1 parent
c073a9b4bd
commit
3266899b74
7 files changed
+143
-26
No files matched your search
@@ -376,15 +376,14 @@ private final class MirroredDisplay: NSObject, SCStreamOutput, SCStreamDelegate
|
||||
}
|
||||
self.display = display
|
||||
// macOS brings the display online, then gives it a mode,
|
||||
// asynchronously; the run loop must turn for the WindowServer's
|
||||
// notifications to arrive (the agent is an NSApplication).
|
||||
// asynchronously.
|
||||
for _ in 0..<50 {
|
||||
var count: UInt32 = 0
|
||||
CGGetOnlineDisplayList(0, nil, &count)
|
||||
var ids = [CGDirectDisplayID](repeating: 0, count: Int(count))
|
||||
CGGetOnlineDisplayList(count, &ids, &count)
|
||||
if ids.contains(display.displayID) && currentMode() != nil { return true }
|
||||
RunLoop.main.run(until: Date().addingTimeInterval(0.1))
|
||||
Thread.sleep(forTimeInterval: 0.1)
|
||||
}
|
||||
failure = "the virtual display \(display.displayID) did not come online in a mode the monitor listed" +
|
||||
(CGDisplayCopyDisplayMode(display.displayID).map { " (macOS chose \($0.pixelWidth)x\($0.pixelHeight) @ \($0.refreshRate) Hz)" } ?? "")
|
||||
@@ -699,6 +698,13 @@ func runDisplayAgentCreate(_ options: [String]) -> Int32 {
|
||||
guard mirror.startOutput(mode) else { return teardown() }
|
||||
outputOn = true
|
||||
guard mirror.startCapture(mode) else { return teardown() }
|
||||
// The agent talks to the WindowServer from the app's own executable:
|
||||
// as a background agent that has finished launching, or the Dock shows
|
||||
// the app bouncing for as long as it runs. Only once the virtual
|
||||
// display has its mode: made an NSApplication before, the process does
|
||||
// not see the new display's modes (CGDisplayCopyDisplayMode is nil).
|
||||
NSApplication.shared.setActivationPolicy(.accessory)
|
||||
NSApplication.shared.finishLaunching()
|
||||
if let error = workload.start(displayID: mirror.display!.displayID, refreshHz: mode.refreshRate) {
|
||||
mirror.failure = "workload: \(error)"
|
||||
return teardown()
|
||||
|
||||
@@ -1359,16 +1359,7 @@ func runDisplayAgent(_ options: [String]) -> Int32 {
|
||||
guard let at = options.firstIndex(of: flag), at + 1 < options.count else { return nil }
|
||||
return options[at + 1]
|
||||
}
|
||||
if options.contains("--create") {
|
||||
// The agent talks to the WindowServer (CGVirtualDisplay,
|
||||
// ScreenCaptureKit, the workload window) from the app's executable:
|
||||
// as a background agent that has finished launching, or the Dock
|
||||
// shows the app bouncing for as long as it runs.
|
||||
let app = NSApplication.shared
|
||||
app.setActivationPolicy(.accessory)
|
||||
app.finishLaunching()
|
||||
return runDisplayAgentCreate(options)
|
||||
}
|
||||
if options.contains("--create") { return runDisplayAgentCreate(options) }
|
||||
guard options.contains("--dry-run") else {
|
||||
print("display-agent: say --dry-run (print what it would create) or --create (make a virtual display)")
|
||||
return 2
|
||||
|
||||
@@ -375,7 +375,7 @@ void __drm_err(const char *format, ...);
|
||||
/* Macros to make printk easier */
|
||||
|
||||
#define _DRM_PRINTK(once, level, fmt, ...) \
|
||||
pr_info("[" DRM_NAME "] " fmt, ##__VA_ARGS__)
|
||||
printk##once(KERN_##level "[" DRM_NAME "] " fmt, ##__VA_ARGS__)
|
||||
#define DRM_NAME "drm"
|
||||
|
||||
/* NOTE: this is deprecated in favor of pr_info(). */
|
||||
|
||||
@@ -28,10 +28,10 @@
|
||||
#define pr_warn_once(fmt, ...) printk_once(KERN_WARNING fmt, ##__VA_ARGS__)
|
||||
#define pr_notice(fmt, ...) printk(KERN_NOTICE fmt, ##__VA_ARGS__)
|
||||
#define pr_info(fmt, ...) printk(KERN_INFO fmt, ##__VA_ARGS__)
|
||||
#define pr_info_once(fmt, ...) printk(KERN_INFO fmt, ##__VA_ARGS__)
|
||||
#define pr_warning_once(fmt, ...) printk(KERN_WARNING fmt, ##__VA_ARGS__)
|
||||
#define pr_notice_once(fmt, ...) printk(KERN_NOTICE fmt, ##__VA_ARGS__)
|
||||
#define pr_err_once(fmt, ...) printk(KERN_ERR fmt, ##__VA_ARGS__)
|
||||
#define pr_info_once(fmt, ...) printk_once(KERN_INFO fmt, ##__VA_ARGS__)
|
||||
#define pr_warning_once(fmt, ...) printk_once(KERN_WARNING fmt, ##__VA_ARGS__)
|
||||
#define pr_notice_once(fmt, ...) printk_once(KERN_NOTICE fmt, ##__VA_ARGS__)
|
||||
#define pr_err_once(fmt, ...) printk_once(KERN_ERR fmt, ##__VA_ARGS__)
|
||||
#define pr_cont(fmt, ...) printk(KERN_CONT fmt, ##__VA_ARGS__)
|
||||
#define pr_fatal(fmt, ...) printk(KERN_CRIT fmt, ##__VA_ARGS__)
|
||||
#define pr_emerg_ratelimited(fmt, ...) printk(KERN_EMERG fmt, ##__VA_ARGS__)
|
||||
@@ -56,7 +56,18 @@ extern asmlinkage int vprintk_emit(int facility, int level,
|
||||
extern int printk(const char *fmt, ...) __printf(1, 2);
|
||||
extern int dynamic_pr_debug(const char *fmt, ...) __printf(1, 2);
|
||||
extern int printk_ratelimited(const char *fmt, ...) __printf(1, 2);
|
||||
extern int printk_once(const char *fmt, ...) __printf(1, 2);
|
||||
extern int (printk_once)(const char *fmt, ...) __printf(1, 2);
|
||||
/* Once per call site, as Linux's DO_ONCE_LITE: a per-commit message
|
||||
* (DRM_INFO_ONCE in DM's MST/DSC check) must not log every frame. */
|
||||
#define printk_once(fmt, ...) \
|
||||
({ \
|
||||
static bool __linuxu_printed_once; \
|
||||
bool __linuxu_first = !__atomic_exchange_n(&__linuxu_printed_once, \
|
||||
true, __ATOMIC_RELAXED); \
|
||||
if (__linuxu_first) \
|
||||
printk(fmt, ##__VA_ARGS__); \
|
||||
__linuxu_first; \
|
||||
})
|
||||
extern int printk_deferred(const char *fmt, ...) __printf(1, 2);
|
||||
extern int vprintk_deferred(const char *fmt, va_list args);
|
||||
extern int vprintk_sprint(const char *fmt, va_list args,
|
||||
|
||||
@@ -80,6 +80,7 @@ struct display_snapshot {
|
||||
#define OUTPUT_BUFFERS 3
|
||||
#define OUTPUT_DAMAGE_MAX 128u
|
||||
#define OUTPUT_FLIP_TIMEOUT_MS 200u
|
||||
#define OUTPUT_COPY_TIMEOUT_MS 2000u
|
||||
|
||||
extern struct dma_fence *drm_crtc_create_fence(struct drm_crtc *crtc);
|
||||
|
||||
@@ -1051,17 +1052,83 @@ static int modeset_use_mode(struct drm_client_dev *client, uint32_t width, uint3
|
||||
* flip's fence callback, a new frame) and never polls; its waits are
|
||||
* bounded as a backstop. Each buffer remembers the damage it missed while
|
||||
* others were drawn, so a copy is that plus the frame's own. */
|
||||
static void damage_add(struct output_damage *d, const struct rt_surface_rect *rects, uint32_t count)
|
||||
static uint64_t rect_area(const struct rt_surface_rect *r)
|
||||
{
|
||||
if (d->full)
|
||||
return (uint64_t)r->width * r->height;
|
||||
}
|
||||
|
||||
/* @a becomes the bounding box of @a and @b when that box is no larger than
|
||||
* the two apart: a rectangle inside another, the same rectangle twice, or
|
||||
* two that overlap closely. Copying the box then copies no pixel twice and
|
||||
* no more pixels than the two would. */
|
||||
static bool rect_absorb(struct rt_surface_rect *a, const struct rt_surface_rect *b)
|
||||
{
|
||||
const uint64_t ax1 = (uint64_t)a->x + a->width, ay1 = (uint64_t)a->y + a->height;
|
||||
const uint64_t bx1 = (uint64_t)b->x + b->width, by1 = (uint64_t)b->y + b->height;
|
||||
const uint32_t x0 = min(a->x, b->x), y0 = min(a->y, b->y);
|
||||
const uint64_t x1 = max(ax1, bx1), y1 = max(ay1, by1);
|
||||
|
||||
if ((x1 - x0) * (y1 - y0) > rect_area(a) + rect_area(b) || x1 - x0 > UINT32_MAX ||
|
||||
y1 - y0 > UINT32_MAX)
|
||||
return false;
|
||||
a->x = x0;
|
||||
a->y = y0;
|
||||
a->width = (uint32_t)(x1 - x0);
|
||||
a->height = (uint32_t)(y1 - y0);
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Add one rectangle, folding it into any it overlaps closely (repeatedly,
|
||||
* as a fold can make a rectangle that absorbs others). The same region
|
||||
* damaged in successive frames (a window redrawing in place) stays one
|
||||
* rectangle, so a buffer that missed it and the frame that redraws it
|
||||
* copy it once, not once per frame. */
|
||||
static void damage_add_rect(struct output_damage *d, const struct rt_surface_rect *r)
|
||||
{
|
||||
struct rt_surface_rect cur = *r;
|
||||
|
||||
if (d->full || !cur.width || !cur.height)
|
||||
return;
|
||||
if (count > OUTPUT_DAMAGE_MAX - d->count) {
|
||||
for (bool again = true; again;) {
|
||||
again = false;
|
||||
for (uint32_t i = 0; i < d->count; i++) {
|
||||
struct rt_surface_rect box = d->rect[i];
|
||||
|
||||
if (rect_absorb(&box, &cur)) {
|
||||
cur = box;
|
||||
d->rect[i] = d->rect[--d->count];
|
||||
again = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (d->count == OUTPUT_DAMAGE_MAX) {
|
||||
d->full = true;
|
||||
d->count = 0;
|
||||
return;
|
||||
}
|
||||
memcpy(&d->rect[d->count], rects, count * sizeof(*rects));
|
||||
d->count += count;
|
||||
d->rect[d->count++] = cur;
|
||||
}
|
||||
|
||||
static void damage_add(struct output_damage *d, const struct rt_surface_rect *rects, uint32_t count)
|
||||
{
|
||||
for (uint32_t i = 0; i < count && !d->full; i++)
|
||||
damage_add_rect(d, &rects[i]);
|
||||
}
|
||||
|
||||
/* Most of the frame damaged: one copy of the whole frame. */
|
||||
static void damage_settle(struct output_damage *d, uint32_t width, uint32_t height)
|
||||
{
|
||||
uint64_t area = 0;
|
||||
|
||||
if (d->full)
|
||||
return;
|
||||
for (uint32_t i = 0; i < d->count; i++)
|
||||
area += rect_area(&d->rect[i]);
|
||||
if (area * 10 >= (uint64_t)width * height * 9) {
|
||||
d->full = true;
|
||||
d->count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void damage_merge(struct output_damage *d, const struct output_damage *add)
|
||||
@@ -1178,9 +1245,26 @@ static int output_flip_wait(struct display_output *o)
|
||||
|
||||
if (!o->pending_flip)
|
||||
return 0;
|
||||
/* The flip waits for its copies (the implicit in-fence): those first,
|
||||
* under the bound of GPU work, then the flip, under the vblank's. */
|
||||
for (unsigned int i = 0; i < RT_SURFACE_ENGINES_MAX; i++) {
|
||||
struct dma_fence *copy = o->pending_frame.copy[i];
|
||||
|
||||
if (!copy)
|
||||
continue;
|
||||
left = dma_fence_wait_timeout(copy, false, msecs_to_jiffies(OUTPUT_COPY_TIMEOUT_MS));
|
||||
if (left <= 0) {
|
||||
pr_err("display: the copy for the pending flip did not complete in %u ms (%ld)\n",
|
||||
OUTPUT_COPY_TIMEOUT_MS, left);
|
||||
return left < 0 ? (int)left : -ETIME;
|
||||
}
|
||||
}
|
||||
left = dma_fence_wait_timeout(o->pending_flip, false, msecs_to_jiffies(OUTPUT_FLIP_TIMEOUT_MS));
|
||||
if (left <= 0)
|
||||
if (left <= 0) {
|
||||
pr_err("display: the flip did not complete within %u ms of its copy (%ld)\n",
|
||||
OUTPUT_FLIP_TIMEOUT_MS, left);
|
||||
return left < 0 ? (int)left : -ETIME;
|
||||
}
|
||||
output_flip_account(o);
|
||||
return 0;
|
||||
}
|
||||
@@ -1274,6 +1358,7 @@ static int output_frame(struct display_output *o, struct rt_surface *surface,
|
||||
b++;
|
||||
copy = o->missed[b];
|
||||
damage_merge(©, damage);
|
||||
damage_settle(©, o->width, o->height);
|
||||
r = rt_surface_copy_submit(surface, o->fb[b]->fb->obj[0], o->fb_address[b], o->fb[b]->fb->pitches[0],
|
||||
copy.full ? &full : copy.rect, copy.full ? 1 : copy.count, &o->engines,
|
||||
frame.copy, &cs);
|
||||
|
||||
@@ -243,7 +243,7 @@ int printk_ratelimited(const char *fmt, ...)
|
||||
return n;
|
||||
}
|
||||
|
||||
int printk_once(const char *fmt, ...)
|
||||
int (printk_once)(const char *fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
int n;
|
||||
|
||||
@@ -304,6 +304,30 @@ int main(void)
|
||||
CHECK(screen_pixel(55, 55) == ((0x00c00003u) ^ (55u << 12) ^ 55u));
|
||||
}
|
||||
|
||||
/* A window redrawing in place (the same rectangle every frame, with
|
||||
* a menu-bar item inside it): each flip copies the region once, not
|
||||
* once for the frame and again for each buffer that missed it. */
|
||||
{
|
||||
const struct rt_surface_rect win[2] = { { 300, 200, 400, 300 }, { 310, 210, 20, 20 } };
|
||||
const uint64_t once = 400ull * 4 * 300;
|
||||
|
||||
for (int f = 0; f < 6; f++) {
|
||||
uint64_t flipped;
|
||||
|
||||
CHECK(rt_display_stats(pdev, &st) == 0);
|
||||
flipped = st.frames_flipped;
|
||||
fake_fill(&surf, 0x00d00000u + (uint32_t)f, &win[0]);
|
||||
CHECK(rt_display_present(pdev, rt_surface_get_hold(1, handle), win, 2,
|
||||
ktime_get_ns(), &st) == 0);
|
||||
st = wait_flipped(pdev, flipped + 1);
|
||||
if (f >= 2)
|
||||
CHECK(st.last_bytes == once);
|
||||
}
|
||||
printf("pipeline: a window redrawn in place copies %llu bytes per flip\n",
|
||||
(unsigned long long)st.last_bytes);
|
||||
CHECK(screen_pixel(500, 350) == ((0x00d00005u) ^ (500u << 12) ^ 350u));
|
||||
}
|
||||
|
||||
/* A frame of another size is refused; nothing breaks. */
|
||||
CHECK(rt_display_stats(pdev, &st) == 0 && !st.error);
|
||||
printf("pipeline: average copy %llu us, latency %llu us (max %llu), worker submit %llu us per frame\n",
|
||||
|
||||
Reference in new issue
Block a user