Skip to content

RediWM is a Wayland compositor written in Zig (OpenGL ES) that includes everything in one process

Optimize Files progress-only repaint

Mel90887d7ecf65
diff --git a/src/files/app.zig b/src/files/app.zig
index 8b38695..6009e21 100644
--- a/src/files/app.zig
+++ b/src/files/app.zig
@@ -64,6 +64,7 @@ pub const setSource = app_draw.setSource;
pub const roundedRect = app_draw.roundedRect;
pub const drawSpinner = app_draw.drawSpinner;
pub const clipDamage = app_draw.clipDamage;
+pub const clipDamageRegions = app_draw.clipDamageRegions;
pub const drawEllipsis = app_draw.drawEllipsis;
pub const drawText = app_draw.drawText;
pub const FontMetrics = app_draw.FontMetrics;
@@ -105,6 +106,8 @@ pub const App = struct {
h: i32 = 540,
dirty: bool = true,
paint_damage: ?header.Rect = null,
+ progress_damage_regions: [128]header.Rect = undefined,
+ progress_damage_count: usize = 0,
scale: i32 = 1,
// Items and state
@@ -438,6 +441,7 @@ pub const App = struct {
pub fn invalidate(self: *App) void {
self.dirty = true;
self.paint_damage = null;
+ self.progress_damage_count = 0;
}
pub const scan_notice_delay_ms: i64 = 500;
@@ -466,6 +470,7 @@ pub const App = struct {
// app_hover.zig
pub const hoverBounds = app_hover.hoverBounds;
pub const addDamage = app_hover.addDamage;
+ pub const addDamageRegion = app_hover.addDamageRegion;
pub const invalidateHover = app_hover.invalidateHover;
pub const clipToViewport = app_hover.clipToViewport;
pub const hoveredPlace = app_hover.hoveredPlace;
diff --git a/src/files/app_draw.zig b/src/files/app_draw.zig
index 5401b1b..ebeb824 100644
--- a/src/files/app_draw.zig
+++ b/src/files/app_draw.zig
@@ -134,6 +134,39 @@ pub fn clipDamage(cr: *c.cairo_t, rect: header.Rect) void {
c.cairo_set_matrix(cr, &matrix);
}
+/// Clips to independent damage rectangles without expanding them into one
+/// large bounding box. Edges are rounded to whole device pixels.
+pub fn clipDamageRegions(cr: *c.cairo_t, rects: []const header.Rect) void {
+ c.cairo_new_path(cr);
+
+ var matrix: c.cairo_matrix_t = undefined;
+ c.cairo_get_matrix(cr, &matrix);
+
+ for (rects) |rect| {
+ if (rect.w <= 0 or rect.h <= 0) continue;
+
+ var x1 = rect.x;
+ var y1 = rect.y;
+ var x2 = rect.x + rect.w;
+ var y2 = rect.y + rect.h;
+ c.cairo_user_to_device(cr, &x1, &y1);
+ c.cairo_user_to_device(cr, &x2, &y2);
+
+ c.cairo_identity_matrix(cr);
+ c.cairo_rectangle(
+ cr,
+ @floor(x1),
+ @floor(y1),
+ @ceil(x2) - @floor(x1),
+ @ceil(y2) - @floor(y1),
+ );
+ c.cairo_set_matrix(cr, &matrix);
+ }
+
+ // An empty path intentionally produces an empty clip.
+ c.cairo_clip(cr);
+}
+
/// Like `drawEllipsis`, but keeps the end of `text`: "…/dir/file.zig".
pub fn drawHeadEllipsis(cr: *c.cairo_t, text: []const u8, x: f64, y: f64, width: f64, size: f64, bold: bool) void {
if (width <= 0) return;
diff --git a/src/files/app_hover.zig b/src/files/app_hover.zig
index 9bf4e55..2e266c0 100644
--- a/src/files/app_hover.zig
+++ b/src/files/app_hover.zig
@@ -74,8 +74,58 @@ pub fn hoverBounds(self: *App, key: usize) ?header.Rect {
/// absorbs it; damage left over from the last paint is not pending, or
/// every hover would grow the next repaint by all the ones before it.
pub fn addDamage(self: *App, rect: header.Rect) void {
- if (self.dirty and self.paint_damage == null) return;
- self.paint_damage = if (self.dirty) unionRect(self.paint_damage.?, rect) else rect;
+ if (rect.w <= 0 or rect.h <= 0) return;
+ if (self.dirty and self.paint_damage == null and self.progress_damage_count == 0) return;
+
+ if (!self.dirty) {
+ self.paint_damage = rect;
+ self.progress_damage_count = 0;
+ self.dirty = true;
+ return;
+ }
+
+ if (self.paint_damage) |previous| {
+ self.paint_damage = unionRect(previous, rect);
+ } else {
+ var combined = rect;
+ for (self.progress_damage_regions[0..self.progress_damage_count]) |region| {
+ combined = unionRect(combined, region);
+ }
+ self.paint_damage = combined;
+ self.progress_damage_count = 0;
+ }
+ self.dirty = true;
+}
+
+/// Queues an independent region for progress-only repaints. If the bounded
+/// queue fills, safely fall back to the existing single-rectangle path.
+pub fn addDamageRegion(self: *App, rect: header.Rect) void {
+ if (rect.w <= 0 or rect.h <= 0) return;
+ if (self.dirty and self.paint_damage == null and self.progress_damage_count == 0) return;
+
+ if (!self.dirty) {
+ self.paint_damage = null;
+ self.progress_damage_count = 0;
+ self.dirty = true;
+ }
+
+ if (self.paint_damage) |previous| {
+ self.progress_damage_regions[0] = previous;
+ self.progress_damage_count = 1;
+ self.paint_damage = null;
+ }
+
+ if (self.progress_damage_count == self.progress_damage_regions.len) {
+ var combined = self.progress_damage_regions[0];
+ for (self.progress_damage_regions[1..self.progress_damage_count]) |region| {
+ combined = unionRect(combined, region);
+ }
+ self.paint_damage = unionRect(combined, rect);
+ self.progress_damage_count = 0;
+ } else {
+ self.progress_damage_regions[self.progress_damage_count] = rect;
+ self.progress_damage_count += 1;
+ }
self.dirty = true;
}
diff --git a/src/files/app_hover_tests.zig b/src/files/app_hover_tests.zig
index 5999976..86f3221 100644
--- a/src/files/app_hover_tests.zig
+++ b/src/files/app_hover_tests.zig
@@ -7,6 +7,7 @@ const c = @import("c.zig").api;
const header = @import("header.zig");
const ui_theme = @import("ui").theme;
const clipDamage = @import("app_draw.zig").clipDamage;
+const clipDamageRegions = @import("app_draw.zig").clipDamageRegions;
const app_tests = @import("app_tests.zig");
const testApp = app_tests.testApp;
const testItems = app_tests.testItems;
@@ -390,6 +391,85 @@ test "hover damage covers this repaint only" {
try std.testing.expect(app.dirty and app.paint_damage == null);
}
+test "progress damage regions stay independent until ordinary damage arrives" {
+ var app = try testApp();
+ defer app.deinit();
+
+ app.dirty = false;
+ app.paint_damage = null;
+ app.progress_damage_count = 0;
+
+ const first: header.Rect = .{ .x = 10, .y = 20, .w = 30, .h = 12 };
+ const second: header.Rect = .{ .x = 300, .y = 240, .w = 40, .h = 14 };
+
+ app.addDamageRegion(first);
+ app.addDamageRegion(second);
+
+ try std.testing.expect(app.dirty);
+ try std.testing.expect(app.paint_damage == null);
+ try std.testing.expectEqual(@as(usize, 2), app.progress_damage_count);
+ try std.testing.expectEqual(first, app.progress_damage_regions[0]);
+ try std.testing.expectEqual(second, app.progress_damage_regions[1]);
+
+ app.addDamage(.{ .x = 500, .y = 400, .w = 10, .h = 10 });
+ try std.testing.expectEqual(@as(usize, 0), app.progress_damage_count);
+ try std.testing.expect(app.paint_damage != null);
+
+ app.invalidate();
+ try std.testing.expectEqual(@as(usize, 0), app.progress_damage_count);
+ try std.testing.expect(app.paint_damage == null);
+}
+
+test "multi-region Cairo clipping matches painting the regions individually" {
+ const width: i32 = 120;
+ const height: i32 = 80;
+ const retained = c.cairo_image_surface_create(c.CAIRO_FORMAT_ARGB32, width, height).?;
+ defer c.cairo_surface_destroy(retained);
+ const reference = c.cairo_image_surface_create(c.CAIRO_FORMAT_ARGB32, width, height).?;
+ defer c.cairo_surface_destroy(reference);
+
+ const regions = [_]header.Rect{
+ .{ .x = 8, .y = 8, .w = 24, .h = 18 },
+ .{ .x = 82, .y = 54, .w = 26, .h = 16 },
+ };
+
+ const retained_cr = c.cairo_create(retained).?;
+ defer c.cairo_destroy(retained_cr);
+ const reference_cr = c.cairo_create(reference).?;
+ defer c.cairo_destroy(reference_cr);
+
+ for ([_]*c.cairo_t{ retained_cr, reference_cr }) |cr| {
+ c.cairo_set_source_rgb(cr, 1, 1, 1);
+ c.cairo_paint(cr);
+ }
+
+ c.cairo_save(retained_cr);
+ clipDamageRegions(retained_cr, &regions);
+ c.cairo_set_source_rgb(retained_cr, 0, 0, 0);
+ c.cairo_paint(retained_cr);
+ c.cairo_restore(retained_cr);
+
+ c.cairo_set_source_rgb(reference_cr, 0, 0, 0);
+ for (regions) |region| {
+ c.cairo_rectangle(
+ reference_cr,
+ region.x,
+ region.y,
+ region.w,
+ region.h,
+ );
+ }
+ c.cairo_fill(reference_cr);
+
+ c.cairo_surface_flush(retained);
+ c.cairo_surface_flush(reference);
+
+ const len: usize = @intCast(width * height * 4);
+ const actual = c.cairo_image_surface_get_data(retained)[0..len];
+ const expected = c.cairo_image_surface_get_data(reference)[0..len];
+ try std.testing.expectEqualSlices(u8, expected, actual);
+}
+
test "column surface owns list header hover and clears modal and departed targets" {
var app = try testApp();
defer app.deinit();
diff --git a/src/files/app_listing.zig b/src/files/app_listing.zig
index 711cb58..4f30333 100644
--- a/src/files/app_listing.zig
+++ b/src/files/app_listing.zig
@@ -128,103 +128,79 @@ pub fn isStaleSnapshot(snapshot_request_id: u64, active_request_id: u64) bool {
return snapshot_request_id < active_request_id;
}
-fn includeProgressDamage(damage: *?header.Rect, rect: header.Rect) void {
- damage.* = if (damage.*) |previous| App.unionRect(previous, rect) else rect;
-}
-
/// Damage only the surfaces that display file-operation byte progress.
/// Structural changes still use invalidate(), because they can change layout.
-fn damageJobProgress(self: *App) void {
- var damage: ?header.Rect = null;
-
- if (self.transfer_view.active) {
- // The file operation is the final row in the Transfers hub.
- const row_index = self.torrents.items().len;
- const y = self.viewTop() + self.itemInset() +
- @as(i32, @intCast(row_index)) * self.rowHeight() - self.scroll_y;
- const row: header.Rect = .{
- .x = @floatFromInt(self.sidebarWidth() + 20),
- .y = @floatFromInt(y),
- .w = @floatFromInt(@max(0, self.cellWidth() - 20)),
- .h = @floatFromInt(self.cardHeight()),
- };
- if (row.y + row.h > @as(f64, @floatFromInt(self.viewTop())) and
- row.y < @as(f64, @floatFromInt(self.contentBottom())))
- {
- includeProgressDamage(&damage, self.clipToViewport(row));
- }
- } else if (self.job_runner) |runner| {
+fn damageJobProgress(
+ self: *App,
+ damage_surfaces: bool,
+ damage_tooltip: bool,
+) void {
+ if (damage_surfaces) {
+ if (self.transfer_view.active) {
+ // The file operation is the final row in the Transfers hub.
+ const row = @import("app_torrents.zig").fileOperationRowRect(
+ self,
+ self.torrents.items().len,
+ );
+ self.addDamageRegion(self.clipToViewport(row));
+ } else if (self.job_runner) |runner| {
// Parent-folder progress rings are drawn inside visible item icons.
- const columns = self.columns();
- const first = @divTrunc(self.scroll_y, self.rowHeight()) * columns;
- const last = @min(
- @as(i32, @intCast(self.items.items.len)),
- (@divTrunc(self.scroll_y + @max(0, self.contentBottom() - self.viewTop()), self.rowHeight()) + 1) * columns,
- );
-
- var index = first;
- while (index < last) : (index += 1) {
- const item = self.items.items[@intCast(index)];
- if (!item.is_dir or item.is_symlink) continue;
- if (runner.progressForPath(item.path) == null) continue;
-
- const icon = self.itemIconRect(index);
- const ring_size = @min(26.0, icon.w);
- const ring: header.Rect = .{
- .x = icon.x + (icon.w - ring_size) / 2 - 2,
- .y = icon.y + (icon.h - ring_size) / 2 - 2,
- .w = ring_size + 4,
- .h = ring_size + 4,
- };
- includeProgressDamage(&damage, self.clipToViewport(ring));
+ const columns = self.columns();
+ const first = @divTrunc(self.scroll_y, self.rowHeight()) * columns;
+ const last = @min(
+ @as(i32, @intCast(self.items.items.len)),
+ (@divTrunc(
+ self.scroll_y + @max(0, self.contentBottom() - self.viewTop()),
+ self.rowHeight(),
+ ) + 1) * columns,
+ );
+
+ var index = first;
+ while (index < last) : (index += 1) {
+ const item = self.items.items[@intCast(index)];
+ if (!item.is_dir or item.is_symlink) continue;
+ if (runner.progressForPath(item.path) == null) continue;
+
+ const icon = self.itemIconRect(index);
+ const ring_size = @min(26.0, icon.w);
+ self.addDamageRegion(self.clipToViewport(.{
+ .x = icon.x + (icon.w - ring_size) / 2 - 2,
+ .y = icon.y + (icon.w - ring_size) / 2 - 2,
+ .w = ring_size + 4,
+ .h = ring_size + 4,
+ }));
+ }
}
- }
- // The sidebar progress row remains visible alongside either main view.
- const transfers = self.sidebarLayout().cards[
- @intFromEnum(@import("app_model.zig").SidebarSection.transfers)
- ];
- if (transfers.visible and transfers.expanded and self.sidebarWidth() > 0) {
- const row: header.Rect = .{
- .x = 12,
- .y = @floatFromInt(
- transfers.y + transfers.header_height +
- self.torrentSidebarHeight() + @import("app_model.zig").SidebarLayout.gap,
- ),
- .w = @floatFromInt(@max(0, self.sidebarWidth() - 24 - self.scrollbarGutter())),
- .h = 52,
- };
- if (row.w >= 120) includeProgressDamage(&damage, row);
+ // The sidebar progress row remains visible alongside either main view.
+ const transfers = self.sidebarLayout().cards[
+ @intFromEnum(@import("app_model.zig").SidebarSection.transfers)
+ ];
+ if (transfers.visible and transfers.expanded and self.sidebarWidth() > 0) {
+ const area = @import("app_sidebar_paint.zig").fileOpsBox(self);
+ if (area.w >= 120) {
+ self.addDamageRegion(.{
+ .x = @floatCast(area.x),
+ .y = @floatCast(area.y),
+ .w = @floatCast(area.w),
+ .h = @floatCast(area.h),
+ });
+ }
+ }
}
// The tooltip is the only other consumer of the live byte/rate counters.
- // Include it only while it is actually visible.
- if (self.hoverKey() == std.math.maxInt(usize) - 2002) {
- const width = @min(320.0, @as(f64, @floatFromInt(self.w)) - 16);
- const height: f64 = 84;
- if (width > 0 and @as(f64, @floatFromInt(self.h)) >= height + 16) {
- const max_x = @max(8.0, @as(f64, @floatFromInt(self.w)) - width - 8);
- const x = std.math.clamp(self.mouse_x + 14, 8, max_x);
- const below = self.mouse_y + 14;
- const window_h: f64 = @floatFromInt(self.h);
- const y = std.math.clamp(
- if (below + height <= window_h - 8)
- below
- else
- self.mouse_y - height - 14,
- 8,
- window_h - height - 8,
- );
- includeProgressDamage(&damage, .{
- .x = x,
- .y = y,
- .w = width,
- .h = height,
- });
+ if (damage_tooltip) {
+ if (@import("app_transfer_tip.zig").bounds(
+ self.w,
+ self.h,
+ self.mouse_x,
+ self.mouse_y,
+ 2,
+ )) |rect| {
+ self.addDamageRegion(rect);
}
}
-
- if (damage) |rect| self.addDamage(rect);
}
pub fn sync(self: *App) void {
@@ -322,12 +298,13 @@ pub fn sync(self: *App) void {
const structure_changed = self.job_progress != progress.processed_items or
self.job_state != progress.state;
const bytes_changed = self.job_transferred_bytes != progress.transferred_bytes;
+ const tooltip_hovered = self.hoverKey() == std.math.maxInt(usize) - 2002;
if (structure_changed) {
self.invalidate();
} else if (bytes_changed or
- (rate_changed and self.hoverKey() == std.math.maxInt(usize) - 2002))
+ (rate_changed and tooltip_hovered))
{
- damageJobProgress(self);
+ damageJobProgress(self, bytes_changed, tooltip_hovered);
}
}
self.job_progress = progress.processed_items;
diff --git a/src/files/app_sidebar_paint.zig b/src/files/app_sidebar_paint.zig
index 0cd5906..0389d41 100644
--- a/src/files/app_sidebar_paint.zig
+++ b/src/files/app_sidebar_paint.zig
@@ -454,7 +454,7 @@ pub fn sidebarExtent(self: *App) i32 {
return bottom + self.sidebar_scroll + app_model.SidebarLayout.gap;
}
-fn fileOpsBox(self: *App) shell_ui.Rect {
+pub fn fileOpsBox(self: *App) shell_ui.Rect {
const card = self.sidebarLayout().cards[@intFromEnum(app_model.SidebarSection.transfers)];
return .{
.x = 12,
diff --git a/src/files/app_torrents.zig b/src/files/app_torrents.zig
index 49bb5b2..92f4f55 100644
--- a/src/files/app_torrents.zig
+++ b/src/files/app_torrents.zig
@@ -587,22 +587,27 @@ fn fileOperationFraction(progress: ops_mod.ProgressInfo) f64 {
);
}
-fn renderFileOperationRow(
- self: *App,
- cr: *c.cairo_t,
- index: usize,
- progress: ops_mod.ProgressInfo,
-) void {
+pub fn fileOperationRowRect(self: *App, index: usize) header.Rect {
const y: f64 = @floatFromInt(
self.viewTop() + self.itemInset() +
@as(i32, @intCast(index)) * self.rowHeight() - self.scroll_y,
);
- const row: header.Rect = .{
+ return .{
.x = @floatFromInt(self.sidebarWidth() + 20),
.y = y,
.w = @floatFromInt(@max(0, self.cellWidth() - 20)),
.h = @floatFromInt(self.cardHeight()),
};
+}
+
+fn renderFileOperationRow(
+ self: *App,
+ cr: *c.cairo_t,
+ index: usize,
+ progress: ops_mod.ProgressInfo,
+) void {
+ const row = fileOperationRowRect(self, index);
+ const y = row.y;
const hover = if (self.dialog == null) transferAt(self, self.mouse_x, self.mouse_y) else null;
if (self.transfer_view.row == index or hover == index) {
ui.drawItemHighlight(cr, .{
diff --git a/src/files/app_transfer_tip.zig b/src/files/app_transfer_tip.zig
index b3adc81..7e42be2 100644
--- a/src/files/app_transfer_tip.zig
+++ b/src/files/app_transfer_tip.zig
@@ -4,19 +4,19 @@ const ui = @import("ui").cairo;
const theme = @import("ui").theme;
const frame = @import("ui").widgets.dialog;
const c = @import("c.zig").api;
+const header = @import("header.zig");
-pub fn render(
- cr: *c.cairo_t,
+/// Exact window-coordinate bounds shared by tooltip painting and damage.
+pub fn bounds(
window_w: i32,
window_h: i32,
mouse_x: f64,
mouse_y: f64,
- title: []const u8,
- details: []const []const u8,
-) void {
+ detail_count: usize,
+) ?header.Rect {
const width: f32 = @min(320, @as(f32, @floatFromInt(window_w)) - 16);
- const height: f32 = 18 + @as(f32, @floatFromInt(details.len + 1)) * 22;
- if (width <= 0 or @as(f32, @floatFromInt(window_h)) < height + 16) return;
+ const height: f32 = 18 + @as(f32, @floatFromInt(detail_count + 1)) * 22;
+ if (width <= 0 or @as(f32, @floatFromInt(window_h)) < height + 16) return null;
const max_x = @max(8, @as(f32, @floatFromInt(window_w)) - width - 8);
const x = std.math.clamp(@as(f32, @floatCast(mouse_x + 14)), 8, max_x);
@@ -30,19 +30,42 @@ pub fn render(
@as(f32, @floatFromInt(window_h)) - height - 8,
);
- var layer = ui.Layer.begin(cr, .{ .x = x, .y = y, .w = width, .h = height }) orelse return;
+ return .{
+ .x = x,
+ .y = y,
+ .w = width,
+ .h = height,
+ };
+}
+
+pub fn render(
+ cr: *c.cairo_t,
+ window_w: i32,
+ window_h: i32,
+ mouse_x: f64,
+ mouse_y: f64,
+ title: []const u8,
+ details: []const []const u8,
+) void {
+ const area = bounds(window_w, window_h, mouse_x, mouse_y, details.len) orelse return;
+ var layer = ui.Layer.begin(cr, .{
+ .x = @floatCast(area.x),
+ .y = @floatCast(area.y),
+ .w = @floatCast(area.w),
+ .h = @floatCast(area.h),
+ }) orelse return;
defer layer.finish();
var palette = theme.shellPalette();
palette.fg = theme.global.window_fg;
layer.renderer.palette = palette;
frame.paintFrame(&layer.renderer, layer.local());
- layer.renderer.drawText(12, 9, width - 24, 22, .{
+ layer.renderer.drawText(12, 9, @as(f32, @floatCast(area.w)) - 24, 22, .{
.content = title,
.font_size = ui.textSize(),
.color = palette.fg,
});
for (details, 0..) |detail, i| {
- layer.renderer.drawText(12, 31 + @as(f32, @floatFromInt(i)) * 22, width - 24, 22, .{
+ layer.renderer.drawText(12, 31 + @as(f32, @floatFromInt(i)) * 22, @as(f32, @floatCast(area.w)) - 24, 22, .{
.content = detail,
.font_size = ui.statusSize(),
.color = palette.fg,
diff --git a/src/files/client_render.zig b/src/files/client_render.zig
index d79eb05..370c5ef 100644
--- a/src/files/client_render.zig
+++ b/src/files/client_render.zig
@@ -3,6 +3,7 @@
const std = @import("std");
const c = @import("c.zig").api;
const app_mod = @import("app.zig");
+const header = @import("header.zig");
const shell_ui = @import("ui").cairo;
const ui_theme = @import("ui").theme;
const a = std.heap.c_allocator;
@@ -32,6 +33,13 @@ pub fn acquireBuffer(self: *Client, w: i32, h: i32) !*Buffer {
/// Device rows [y1, y2) one commit changed.
pub const RowBand = struct { y1: i32 = 0, y2: i32 = 0 };
+const PixelRect = struct {
+ x1: i32 = 0,
+ y1: i32 = 0,
+ x2: i32 = 0,
+ y2: i32 = 0,
+};
+
/// Brings a recycled `buffer` up to the last published image (`retained`):
/// only the rows committed since it was last painted, when they are still
/// in `self.bands`, else all of it.
@@ -65,7 +73,8 @@ pub fn render(self: *Client) !void {
if (!self.configured or !self.app.dirty or self.outstanding >= 2) return;
// Partial repaints (hover, proximity glow, fades) follow the display,
// not the pointer rate: wait for the last commit's frame callback.
- if (self.app.paint_damage != null and self.frame_callback != null and !self.ack_uncommitted) return;
+ if ((self.app.paint_damage != null or self.app.progress_damage_count > 0) and
+ self.frame_callback != null and !self.ack_uncommitted) return;
const tbh = self.titlebarHeight();
const w = self.app.w;
@@ -74,14 +83,50 @@ pub fn render(self: *Client) !void {
const bh = h * self.scale;
const buffer = try self.acquireBuffer(bw, bh);
- const partial = self.retained.items.len == buffer.len and self.retained_w == bw and self.applied_scale == self.scale and self.app.paint_damage != null;
+ const retained_valid = self.retained.items.len == buffer.len and
+ self.retained_w == bw and self.applied_scale == self.scale;
+ const multi_partial = retained_valid and self.app.progress_damage_count > 0;
+ const partial = retained_valid and self.app.paint_damage != null and !multi_partial;
+ const max_progress_regions = 128;
+ var progress_pixels: [max_progress_regions]PixelRect = undefined;
+ var progress_pixel_count: usize = 0;
+ var damage_regions: [max_progress_regions]header.Rect = undefined;
+ var damage_region_count: usize = 0;
// The rows this paint can change: the clip of a partial paint (rounded
// out to device pixels like `clipDamage`), else everything. Outside
// them the buffer already equals `retained`, so only these are compared
// and copied back.
var band: RowBand = .{ .y1 = 0, .y2 = bh };
var cols: RowBand = .{ .y1 = 0, .y2 = bw }; // the same, for columns
- if (partial) {
+ if (multi_partial) {
+ const s: f64 = @floatFromInt(self.scale);
+ band = .{ .y1 = bh, .y2 = 0 };
+
+ for (self.app.progress_damage_regions[0..self.app.progress_damage_count]) |rect| {
+ const top = (rect.y + @as(f64, @floatFromInt(tbh))) * s;
+ const left = rect.x * s;
+ const bottom = top + rect.h * s;
+ const right = left + rect.w * s;
+ const pixel = PixelRect{
+ .x1 = @intFromFloat(std.math.clamp(@floor(left), 0, @as(f64, @floatFromInt(bw)))),
+ .y1 = @intFromFloat(std.math.clamp(@floor(top), 0, @as(f64, @floatFromInt(bh)))),
+ .x2 = @intFromFloat(std.math.clamp(@ceil(right), 0, @as(f64, @floatFromInt(bw)))),
+ .y2 = @intFromFloat(std.math.clamp(@ceil(bottom), 0, @as(f64, @floatFromInt(bh)))),
+ };
+ if (pixel.x2 <= pixel.x1 or pixel.y2 <= pixel.y1) continue;
+
+ progress_pixels[progress_pixel_count] = pixel;
+ damage_regions[damage_region_count] = rect;
+ progress_pixel_count += 1;
+ damage_region_count += 1;
+ band.y1 = @min(band.y1, pixel.y1);
+ band.y2 = @max(band.y2, pixel.y2);
+ }
+
+ if (band.y2 <= band.y1) band = .{ .y1 = 0, .y2 = 0 };
+ refreshBuffer(self, buffer, bw, bh);
+ c.cairo_surface_mark_dirty(buffer.image);
+ } else if (partial) {
const damage = self.app.paint_damage.?;
const s: f64 = @floatFromInt(self.scale);
const top = (damage.y + @as(f64, @floatFromInt(tbh))) * s;
@@ -101,7 +146,14 @@ pub fn render(self: *Client) !void {
defer c.cairo_destroy(cr);
c.cairo_scale(cr, @floatFromInt(self.scale), @floatFromInt(self.scale));
- if (partial) {
+ if (multi_partial) {
+ var clipped: [max_progress_regions]header.Rect = undefined;
+ for (damage_regions[0..damage_region_count], 0..) |rect, i| {
+ clipped[i] = rect;
+ clipped[i].y += @floatFromInt(tbh);
+ }
+ app_mod.clipDamageRegions(cr, clipped[0..damage_region_count]);
+ } else if (partial) {
var rect = self.app.paint_damage.?;
rect.y += @floatFromInt(tbh);
app_mod.clipDamage(cr, rect);
@@ -121,7 +173,53 @@ pub fn render(self: *Client) !void {
var y1 = bh;
var x2: i32 = 0;
var y2: i32 = 0;
- if (self.retained.items.len != bytes.len or self.retained_w != bw or self.applied_scale != self.scale) {
+ var changed_regions: [max_progress_regions]PixelRect = undefined;
+ var changed_region_count: usize = 0;
+ if (multi_partial) {
+ const stride: usize = @intCast(bw * 4);
+ for (progress_pixels[0..progress_pixel_count]) |region| {
+ const col0: usize = @intCast(region.x1);
+ const col1: usize = @intCast(region.x2);
+ var rx1 = region.x2;
+ var ry1 = region.y2;
+ var rx2: i32 = 0;
+ var ry2: i32 = 0;
+
+ for (@intCast(region.y1)..@intCast(region.y2)) |row| {
+ const offset = row * stride;
+ const now: []const u32 = @alignCast(std.mem.bytesAsSlice(
+ u32,
+ bytes[offset + col0 * 4 .. offset + col1 * 4],
+ ));
+ const was: []const u32 = @alignCast(std.mem.bytesAsSlice(
+ u32,
+ self.retained.items[offset + col0 * 4 .. offset + col1 * 4],
+ ));
+ if (std.mem.eql(u32, now, was)) continue;
+
+ ry1 = @min(ry1, @as(i32, @intCast(row)));
+ ry2 = @as(i32, @intCast(row + 1));
+ const left = std.mem.indexOfDiff(u32, now, was).?;
+ var right = now.len;
+ while (right > left and now[right - 1] == was[right - 1]) right -= 1;
+ rx1 = @min(rx1, @as(i32, @intCast(col0 + left)));
+ rx2 = @max(rx2, @as(i32, @intCast(col0 + right)));
+ }
+
+ if (rx2 <= rx1 or ry2 <= ry1) continue;
+ changed_regions[changed_region_count] = .{
+ .x1 = rx1,
+ .y1 = ry1,
+ .x2 = rx2,
+ .y2 = ry2,
+ };
+ changed_region_count += 1;
+ x1 = @min(x1, rx1);
+ y1 = @min(y1, ry1);
+ x2 = @max(x2, rx2);
+ y2 = @max(y2, ry2);
+ }
+ } else if (self.retained.items.len != bytes.len or self.retained_w != bw or self.applied_scale != self.scale) {
x1 = 0;
y1 = 0;
x2 = bw;
@@ -145,7 +243,7 @@ pub fn render(self: *Client) !void {
x2 = @max(x2, @as(i32, @intCast(col0 + right)));
}
}
- if (partial) {
+ if (partial or multi_partial) {
const stride: usize = @intCast(bw * 4);
if (y2 > y1) {
const from = @as(usize, @intCast(y1)) * stride;
@@ -172,7 +270,18 @@ pub fn render(self: *Client) !void {
}
self.xdg_surface.setWindowGeometry(0, 0, w, h);
self.surface.attach(buffer.wl_buffer, 0, 0);
- self.surface.damageBuffer(x1, y1, x2 - x1, y2 - y1);
+ if (multi_partial) {
+ for (changed_regions[0..changed_region_count]) |region| {
+ self.surface.damageBuffer(
+ region.x1,
+ region.y1,
+ region.x2 - region.x1,
+ region.y2 - region.y1,
+ );
+ }
+ } else {
+ self.surface.damageBuffer(x1, y1, x2 - x1, y2 - y1);
+ }
self.surface.commit();
buffer.busy = true;
self.outstanding += 1;
@@ -185,6 +294,7 @@ pub fn render(self: *Client) !void {
buffer.seq = self.commit_seq;
self.ack_uncommitted = false;
self.app.dirty = false;
+ self.app.progress_damage_count = 0;
}
pub fn renderTitlebar(self: *Client, cr: *c.cairo_t, w: i32, h: i32) void {