Debian Codegraph @

src/renderer/damage.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 #include <picom/backend.h>
5
6 #include "backend/backend.h"
7 #include "common.h"
8 #include "layout.h"
9 #include "region.h"
10 #include "utils/dynarr.h"
11 #include "wm/win.h"
12
13 #include "damage.h"
14
15 /// Compare two layers that contain the same window, return if they are the "same". Same
16 /// means these two layers are render in the same way at the same position, with the only
17 /// possible differences being the contents inside the window.
18 static bool
19 layer_compare(const struct layer *past_layer, const struct backend_command *past_layer_cmd,
20 const struct layer *curr_layer, const struct backend_command *curr_layer_cmd) {
21 if (!ibox_eq(past_layer->window, curr_layer->window)) {
22 // Window moved or size changed
23 return false;
24 }
25
26 // TODO(yshui) consider window body and shadow separately.
27 if (!vec2_eq(past_layer->scale, curr_layer->scale) ||
28 !vec2_eq(past_layer->shadow_scale, curr_layer->shadow_scale)) {
29 // Window or shadow scale changed
30 return false;
31 }
32
33 if (!ibox_eq(past_layer->shadow, curr_layer->shadow)) {
34 // Shadow moved or size changed
35 return false;
36 }
37 if (past_layer->saved_image_blend != curr_layer->saved_image_blend) {
38 // The amount of blending with the saved image changed
39 return false;
40 }
41 if (past_layer->number_of_commands != curr_layer->number_of_commands) {
42 // Number of render commands changed. We are being conservative
43 // here, because even though the number of commands changed, we can still
44 // try to match them up. For example, maybe this window just has shadow
45 // disabled, but other commands are still the same. We are not do that
46 // here, this could be a TODO
47 // TODO(yshui) match render commands here
48 return false;
49 }
50
51 for (unsigned i = 0; i < past_layer->number_of_commands; i++) {
52 auto cmd1 = &past_layer_cmd[i];
53 auto cmd2 = &curr_layer_cmd[i];
54 if (cmd1->op != cmd2->op || !ivec2_eq(cmd1->origin, cmd2->origin) ||
55 cmd1->source != cmd2->source) {
56 return false;
57 }
58 }
59 return true;
60 }
61
62 /// Add all regions of `layer`'s commands to `region`
63 static inline void region_union_render_layer(region_t *region, const struct layer *layer,
64 const struct backend_command *cmds) {
65 for (auto i = cmds; i < &cmds[layer->number_of_commands]; i++) {
66 pixman_region32_union(region, region, &i->target_mask);
67 }
68 }
69
70 static inline void
71 command_blit_damage(region_t *damage, region_t *scratch_region, struct backend_command *cmd1,
72 struct backend_command *cmd2, const struct layout_manager *lm,
73 unsigned layer_index, unsigned buffer_age) {
74 // clang-format off
75 // First part, if any blit argument that would affect the whole image changed
76 if (!color_eq(cmd1->blit.tint, cmd2->blit.tint) ||
77 cmd1->blit.shader != cmd2->blit.shader ||
78 cmd1->blit.corner_radius != cmd2->blit.corner_radius ||
79 cmd1->blit.max_brightness != cmd2->blit.max_brightness ||
80 cmd1->blit.color_inverted != cmd2->blit.color_inverted ||
81
82 // (we already checked cmd1 and cmd2 have the same shader)
83 // If the custom shader is animated, we consider the whole window
84 // damaged.
85 (cmd1->shader_info != NULL &&
86 cmd1->shader_info->attributes & SHADER_ATTRIBUTE_ANIMATED) ||
87
88 // Second part, if round corner is enabled, then border width and effective size
89 // affect the whole image too.
90 (cmd1->blit.corner_radius > 0 &&
91 (cmd1->blit.border_width != cmd2->blit.border_width ||
92 !ivec2_eq(cmd1->blit.effective_size, cmd2->blit.effective_size)))
93 )
94 {
95 pixman_region32_union(damage, damage, &cmd1->target_mask);
96 pixman_region32_union(damage, damage, &cmd2->target_mask);
97 return;
98 }
99 // clang-format on
100
101 if (cmd1->blit.tint.alpha == 0) {
102 // invisible
103 return;
104 }
105
106 // Damage from layers below that is covered up by the current layer, won't be
107 // visible. So remove them.
108 pixman_region32_subtract(damage, damage, &cmd2->opaque_region);
109 region_symmetric_difference_local(damage, scratch_region, &cmd1->target_mask,
110 &cmd2->target_mask);
111 if (cmd1->source == BACKEND_COMMAND_SOURCE_WINDOW) {
112 layout_manager_collect_window_damage(lm, layer_index, buffer_age,
113 scratch_region);
114 region_scale(scratch_region, cmd2->origin, cmd2->blit.scale);
115 pixman_region32_intersect(scratch_region, scratch_region, &cmd1->target_mask);
116 pixman_region32_intersect(scratch_region, scratch_region, &cmd2->target_mask);
117 pixman_region32_union(damage, damage, scratch_region);
118 }
119 }
120
121 static inline void command_blur_damage(region_t *damage, region_t *scratch_region,
122 struct backend_command *cmd1,
123 struct backend_command *cmd2, ivec2 blur_size) {
124 if (cmd1->blur.opacity != cmd2->blur.opacity) {
125 pixman_region32_union(damage, damage, &cmd1->target_mask);
126 pixman_region32_union(damage, damage, &cmd2->target_mask);
127 return;
128 }
129 if (cmd1->blur.opacity == 0) {
130 return;
131 }
132 region_symmetric_difference_local(damage, scratch_region, &cmd1->target_mask,
133 &cmd2->target_mask);
134
135 // We need to expand the damage region underneath the blur. Because blur
136 // "diffuses" the changes from below.
137 pixman_region32_copy(scratch_region, damage);
138 resize_region_in_place(scratch_region, blur_size.width, blur_size.height);
139 pixman_region32_intersect(scratch_region, scratch_region, &cmd2->target_mask);
140 pixman_region32_union(damage, damage, scratch_region);
141 }
142
143 /// Do the first step of render planning, collecting damages and calculating which
144 /// parts of the final screen will be affected by the damages.
145 void layout_manager_damage(struct layout_manager *lm, unsigned buffer_age,
146 ivec2 blur_size, region_t *damage) {
147 // TODO(yshui): take occulusion into consideration when calculating damage.
148 log_trace("Damage for buffer age %d", buffer_age);
149 unsigned past_layer_rank = 0, curr_layer_rank = 0;
150 auto past_layout = layout_manager_layout(lm, buffer_age);
151 auto curr_layout = layout_manager_layout(lm, 0);
152 auto past_layer = &past_layout->layers[past_layer_rank];
153 auto curr_layer = &curr_layout->layers[curr_layer_rank];
154 auto past_layer_cmd = &past_layout->commands[past_layout->first_layer_start];
155 auto curr_layer_cmd = &curr_layout->commands[curr_layout->first_layer_start];
156 region_t scratch_region;
157 pixman_region32_init(&scratch_region);
158 pixman_region32_clear(damage);
159 if (past_layout->size.width != curr_layout->size.width ||
160 past_layout->size.height != curr_layout->size.height ||
161 past_layout->root_image_generation != curr_layout->root_image_generation) {
162 pixman_region32_union_rect(damage, damage, 0, 0,
163 (unsigned)curr_layout->size.width,
164 (unsigned)curr_layout->size.height);
165 return;
166 }
167
168 // First, if the existence of background image changed between curr_layout and
169 // past_layout, mark it as damaged.
170 auto is_background_changed =
171 curr_layout->first_layer_start != past_layout->first_layer_start;
172 if (!is_background_changed && curr_layout->first_layer_start > 1) {
173 auto background = &curr_layout->commands[1];
174 assert(curr_layout->first_layer_start == 2);
175
176 // Otherwise, consider background changed if it has a shader that might be
177 // animated.
178 is_background_changed =
179 background->op == BACKEND_COMMAND_BLIT &&
180 background->shader_info != NULL &&
181 (background->shader_info->attributes & SHADER_ATTRIBUTE_ANIMATED) != 0;
182 }
183 if (is_background_changed) {
184 // TODO(yshui) this is crude, even though the background has changed, that
185 // change might be obscured by other windows on top, and not actually be
186 // visible.
187 pixman_region32_union_rect(damage, damage, 0, 0,
188 (unsigned)curr_layout->size.width,
189 (unsigned)curr_layout->size.height);
190 return;
191 }
192 if (log_get_level_tls() <= LOG_LEVEL_TRACE) {
193 log_trace("Comparing across %d layouts:", buffer_age);
194 for (unsigned l = 0; l <= buffer_age; l++) {
195 log_trace("Layout[%d]: ", -l);
196 auto layout = layout_manager_layout(lm, l);
197 dynarr_foreach(layout->layers, layer) {
198 log_trace("\t%#010x %dx%d+%dx%d (prev %d, next %d)",
199 layer->key.x, layer->window.size.width,
200 layer->window.size.height,
201 layer->window.origin.x, layer->window.origin.y,
202 layer->prev_rank, layer->next_rank);
203 }
204 }
205 }
206
207 // Explanation of what's happening here. We want to get damage by comparing
208 // `past_layout` and `curr_layout` But windows in them could be different. And
209 // comparing different windows doesn't really make sense. So we want to "align"
210 // the layouts so we compare matching windows and skip over non-matching ones. For
211 // example, say past layout has window "ABCDE"; and in current layout, window C is
212 // closed, and F is opened: "ABDFE", we want to align them like this:
213 // ABCD E
214 // AB DFE
215 // Note there can be multiple ways of aligning windows, some of them are not
216 // optimal. For example, in layout "ABCDEFG", if we move B to after F: "ACDEFBG",
217 // we want to align them like this:
218 // ABCDEF G
219 // A CDEFBG
220 // not like this:
221 // A BCDEFG
222 // ACDEFB G
223 //
224 // This is the classic Longest Common Sequence (LCS) problem, but we are not doing
225 // a full LCS algorithm here. Since damage is calculated every frame, there is
226 // likely not a lot of changes between the two layouts. We use a simple linear
227 // time greedy approximation that should work well enough in those cases.
228
229 for (;; past_layer_rank += 1, curr_layer_rank += 1,
230 past_layer_cmd += past_layer->number_of_commands,
231 curr_layer_cmd += curr_layer->number_of_commands, past_layer += 1,
232 curr_layer += 1) {
233 int past_layer_curr_rank = -1, curr_layer_past_rank = -1;
234 unsigned past_layer_rank_target = past_layer_rank,
235 curr_layer_rank_target = curr_layer_rank;
236 log_region(TRACE, damage);
237
238 // Skip layers in the past layout doesn't contain a window that has a
239 // match in the remaining layers of the current layout; and vice versa.
240 while (past_layer_rank_target < dynarr_len(past_layout->layers)) {
241 past_layer_curr_rank =
242 layer_next_rank(lm, buffer_age, past_layer_rank_target);
243 if (past_layer_curr_rank >= (int)curr_layer_rank) {
244 break;
245 }
246 past_layer_rank_target++;
247 };
248 while (curr_layer_rank_target < dynarr_len(curr_layout->layers)) {
249 curr_layer_past_rank =
250 layer_prev_rank(lm, buffer_age, curr_layer_rank_target);
251 if (curr_layer_past_rank >= (int)past_layer_rank) {
252 break;
253 }
254 curr_layer_rank_target++;
255 };
256
257 // past_layer_curr_rank/curr_layer_past_rank can be -1
258 if (past_layer_curr_rank >= (int)curr_layer_rank ||
259 curr_layer_past_rank >= (int)past_layer_rank) {
260 // Now past_layer_current_rank and current_layer_past_rank both
261 // have a matching layer in the other layout. We check which side
262 // has less layers to skip.
263 assert((unsigned)curr_layer_past_rank >= past_layer_rank_target);
264 assert((unsigned)past_layer_curr_rank >= curr_layer_rank_target);
265 // How many layers will be skipped on either side if we move
266 // past_layer_rank to past_layer_rank_target. And vice versa.
267 auto skipped_using_past_target =
268 past_layer_rank_target - past_layer_rank +
269 ((unsigned)past_layer_curr_rank - curr_layer_rank);
270 auto skipped_using_curr_target =
271 curr_layer_rank_target - curr_layer_rank +
272 ((unsigned)curr_layer_past_rank - past_layer_rank);
273 if (skipped_using_curr_target < skipped_using_past_target) {
274 past_layer_rank_target = (unsigned)curr_layer_past_rank;
275 } else {
276 curr_layer_rank_target = (unsigned)past_layer_curr_rank;
277 }
278 }
279
280 // For the skipped layers, we need to add them to the damage region.
281 for (; past_layer_rank < past_layer_rank_target; past_layer_rank++) {
282 region_union_render_layer(damage, past_layer, past_layer_cmd);
283 past_layer_cmd += past_layer->number_of_commands;
284 past_layer += 1;
285 }
286 for (; curr_layer_rank < curr_layer_rank_target; curr_layer_rank++) {
287 region_union_render_layer(damage, curr_layer, curr_layer_cmd);
288 curr_layer_cmd += curr_layer->number_of_commands;
289 curr_layer += 1;
290 }
291
292 if (past_layer_rank >= dynarr_len(past_layout->layers) ||
293 curr_layer_rank >= dynarr_len(curr_layout->layers)) {
294 // No more matching layers left.
295 assert(past_layer_rank >= dynarr_len(past_layout->layers) &&
296 curr_layer_rank >= dynarr_len(curr_layout->layers));
297 break;
298 }
299
300 assert(wm_treeid_eq(past_layer->key, curr_layer->key));
301 log_trace("%#010x == %#010x %s", past_layer->key.x, curr_layer->key.x,
302 curr_layer->win->name);
303
304 if (!layer_compare(past_layer, past_layer_cmd, curr_layer, curr_layer_cmd)) {
305 region_union_render_layer(damage, curr_layer, curr_layer_cmd);
306 region_union_render_layer(damage, past_layer, past_layer_cmd);
307 continue;
308 }
309
310 // Layers are otherwise identical besides the window content. We will
311 // process their render command and add appropriate damage.
312 log_trace("Adding window damage");
313 for (struct backend_command *cmd1 = past_layer_cmd, *cmd2 = curr_layer_cmd;
314 cmd1 < past_layer_cmd + past_layer->number_of_commands; cmd1++, cmd2++) {
315 switch (cmd1->op) {
316 case BACKEND_COMMAND_BLIT:
317 command_blit_damage(damage, &scratch_region, cmd1, cmd2,
318 lm, curr_layer_rank, buffer_age);
319 break;
320 case BACKEND_COMMAND_BLUR:
321 command_blur_damage(damage, &scratch_region, cmd1, cmd2,
322 blur_size);
323 break;
324 default: assert(false);
325 }
326 }
327 }
328 pixman_region32_fini(&scratch_region);
329 }
330
331 void commands_cull_with_damage(struct layout *layout, const region_t *damage,
332 ivec2 blur_size, region_t *culled_mask) {
333 // This may sound silly, and probably actually is. Why do GPU's job on the CPU?
334 // Isn't the GPU supposed to be the one that does culling, depth testing etc.?
335 //
336 // Well, the things is the compositor is a bit special which makes this a bit
337 // hard. First of all, each window is its own texture. If we bundle them in one
338 // draw call, we might run into texture unit limits. If we don't bundle them,
339 // then because we draw things bottom up, depth testing is pointless. Maybe we
340 // can draw consecutive opaque windows top down with depth test, which will work
341 // on OpenGL. But xrender won't like it. So that would be backend specific.
342 //
343 // Which is to say, there might be better ways of utilizing the GPU for culling,
344 // but that will be complicated. And being a compositor makes doing this on CPU
345 // easier, we only need to handle a dozen axis aligned rectangles, not hundreds of
346 // thousands of triangles. So this is what we are stuck with for now.
347 region_t scratch_region, tmp;
348 pixman_region32_init(&scratch_region);
349 pixman_region32_init(&tmp);
350 // scratch_region stores the visible damage region of the screen at the current
351 // layer. at the top most layer, all of damage is visible
352 pixman_region32_copy(&scratch_region, damage);
353 for (int i = to_int_checked(layout->number_of_commands - 1); i >= 0; i--) {
354 auto cmd = &layout->commands[i];
355 pixman_region32_copy(&culled_mask[i], &cmd->target_mask);
356 pixman_region32_intersect(&culled_mask[i], &culled_mask[i], &scratch_region);
357 switch (cmd->op) {
358 case BACKEND_COMMAND_BLIT:
359 pixman_region32_subtract(&scratch_region, &scratch_region,
360 &cmd->opaque_region);
361 cmd->blit.target_mask = &culled_mask[i];
362 break;
363 case BACKEND_COMMAND_COPY_AREA:
364 pixman_region32_subtract(&scratch_region, &scratch_region,
365 &cmd->target_mask);
366 cmd->copy_area.region = &culled_mask[i];
367 break;
368 case BACKEND_COMMAND_BLUR:
369 // To render blur, the layers below must render pixels surrounding
370 // the blurred area in this layer.
371 pixman_region32_copy(&tmp, &scratch_region);
372 pixman_region32_intersect(&tmp, &tmp, &cmd->target_mask);
373 resize_region_in_place(&tmp, blur_size.width, blur_size.height);
374 pixman_region32_union(&scratch_region, &scratch_region, &tmp);
375 cmd->blur.target_mask = &culled_mask[i];
376 break;
377 case BACKEND_COMMAND_INVALID: assert(false);
378 }
379 }
380 pixman_region32_fini(&tmp);
381 pixman_region32_fini(&scratch_region);
382 }
383
384 void commands_uncull(struct layout *layout) {
385 for (auto i = layout->commands;
386 i != &layout->commands[layout->number_of_commands]; i++) {
387 switch (i->op) {
388 case BACKEND_COMMAND_BLIT: i->blit.target_mask = &i->target_mask; break;
389 case BACKEND_COMMAND_BLUR: i->blur.target_mask = &i->target_mask; break;
390 case BACKEND_COMMAND_COPY_AREA:
391 i->copy_area.region = &i->target_mask;
392 break;
393 case BACKEND_COMMAND_INVALID: assert(false);
394 }
395 }
396 }