Debian Codegraph @

src/renderer/layout.c

raw symbols SCIP
1 // SPDX-License-Identifier: MPL-2.0
2 // Copyright (c) Yuxuan Shui <[email protected]>
3
4 #include <stddef.h>
5 #include <uthash.h>
6
7 #include <picom/types.h>
8
9 #include "command_builder.h"
10 #include "common.h"
11 #include "region.h"
12 #include "utils/dynarr.h"
13 #include "utils/list.h"
14 #include "utils/misc.h"
15 #include "wm/defs.h"
16 #include "wm/win.h"
17 #include "wm/wm.h"
18
19 #include "layout.h"
20 struct layer_index {
21 UT_hash_handle hh;
22 wm_treeid key;
23 unsigned index;
24 struct list_node free_list;
25 };
26 struct layout_manager {
27 unsigned max_buffer_age;
28 /// Index of the most recent layout in `layouts`.
29 unsigned current;
30 /// Mapping from window to its index in the current layout.
31 struct layer_index *layer_indices;
32 struct list_node free_indices;
33
34 // internal
35 /// Scratch region used for calculations, to avoid repeated allocations.
36 region_t scratch_region;
37 /// Current and past layouts, at most `max_buffer_age` layouts are stored.
38 struct layout layouts[];
39 };
40
41 /// Compute layout of a layer from a window. Returns false if the window is not
42 /// visible / should not be rendered. `out_layer` is modified either way.
43 static bool layer_from_window(struct layer *out_layer, struct win *w, ivec2 size) {
44 bool to_paint = false;
45 auto w_opts = win_options(w);
46 if (!w->ever_damaged || !w_opts.paint) {
47 goto out;
48 }
49 if (w->win_image == NULL) {
50 goto out;
51 }
52
53 out_layer->options = w_opts;
54 out_layer->scale = (vec2){
55 .x = win_animatable_get(w, WIN_SCRIPT_SCALE_X),
56 .y = win_animatable_get(w, WIN_SCRIPT_SCALE_Y),
57 };
58 out_layer->window.origin =
59 vec2_as((vec2){.x = w->g.x + win_animatable_get(w, WIN_SCRIPT_OFFSET_X),
60 .y = w->g.y + win_animatable_get(w, WIN_SCRIPT_OFFSET_Y)});
61 out_layer->window.size = vec2_as((vec2){.width = w->widthb, .height = w->heightb});
62 out_layer->crop.origin = vec2_as((vec2){
63 .x = win_animatable_get(w, WIN_SCRIPT_CROP_X),
64 .y = win_animatable_get(w, WIN_SCRIPT_CROP_Y),
65 });
66 out_layer->crop.size = vec2_as((vec2){
67 .x = win_animatable_get(w, WIN_SCRIPT_CROP_WIDTH),
68 .y = win_animatable_get(w, WIN_SCRIPT_CROP_HEIGHT),
69 });
70 if (w_opts.shadow) {
71 vec2 scale = {
72 .x = win_animatable_get(w, WIN_SCRIPT_SHADOW_SCALE_X),
73 .y = win_animatable_get(w, WIN_SCRIPT_SHADOW_SCALE_Y),
74 };
75 out_layer->shadow_scale = scale;
76 // If shadow_offset is not zero, the shadow and the window will have
77 // different scale origins. This manifests as shadow and window having an
78 // apparent movement relative to each other during a scale animation.
79 // Although this can be fixed in animation scripts, it's reasonable to
80 // expect shadows and windows are scaled at the same origin.
81 out_layer->shadow.origin =
82 vec2_as((vec2){.x = w->g.x + w->shadow_dx * scale.x +
83 win_animatable_get(w, WIN_SCRIPT_SHADOW_OFFSET_X),
84 .y = w->g.y + w->shadow_dy * scale.y +
85 win_animatable_get(w, WIN_SCRIPT_SHADOW_OFFSET_Y)});
86 out_layer->shadow.size =
87 vec2_as((vec2){.width = w->shadow_width, .height = w->shadow_height});
88 } else {
89 out_layer->shadow.origin = (ivec2){};
90 out_layer->shadow.size = (ivec2){};
91 out_layer->shadow_scale = SCALE_IDENTITY;
92 }
93
94 struct ibox window_scaled = {
95 .origin = out_layer->window.origin,
96 .size = ivec2_scale_floor(out_layer->window.size, out_layer->scale),
97 };
98 struct ibox screen = {.origin = {0, 0}, .size = size};
99 if (!ibox_overlap(window_scaled, screen) || !ibox_overlap(out_layer->crop, screen)) {
100 goto out;
101 }
102
103 out_layer->opacity = (float)win_animatable_get(w, WIN_SCRIPT_OPACITY);
104 out_layer->blur_opacity =
105 (float)clamp(win_animatable_get(w, WIN_SCRIPT_BLUR_OPACITY), 0., 1.);
106 out_layer->shadow_color.red = (float)win_animatable_get(w, WIN_SCRIPT_SHADOW_RED);
107 out_layer->shadow_color.green = (float)win_animatable_get(w, WIN_SCRIPT_SHADOW_GREEN);
108 out_layer->shadow_color.blue = (float)win_animatable_get(w, WIN_SCRIPT_SHADOW_BLUE);
109 out_layer->shadow_color.alpha = 1.0;
110 out_layer->shadow_color = color_mult_alpha(
111 out_layer->shadow_color, win_animatable_get(w, WIN_SCRIPT_SHADOW_OPACITY) *
112 w->shadow_opacity * w->frame_opacity);
113 if (out_layer->opacity == 0 && out_layer->blur_opacity == 0) {
114 goto out;
115 }
116
117 out_layer->saved_image_blend =
118 (float)win_animatable_get(w, WIN_SCRIPT_SAVED_IMAGE_BLEND);
119 if (w->saved_win_image == NULL) {
120 out_layer->saved_image_blend = 0;
121 }
122
123 pixman_region32_copy(&out_layer->damaged, &w->damaged);
124 pixman_region32_translate(&out_layer->damaged, out_layer->window.origin.x,
125 out_layer->window.origin.y);
126 out_layer->next_rank = -1;
127 out_layer->prev_rank = -1;
128 out_layer->key = wm_ref_treeid(w->tree_ref);
129 out_layer->win = w;
130 to_paint = true;
131
132 out:
133 pixman_region32_clear(&w->damaged);
134 return to_paint;
135 }
136
137 static void layer_deinit(struct layer *layer) {
138 pixman_region32_fini(&layer->damaged);
139 }
140
141 static void layer_init(struct layer *layer) {
142 pixman_region32_init(&layer->damaged);
143 }
144
145 static void layout_deinit(struct layout *layout) {
146 dynarr_clear(layout->layers, layer_deinit);
147 command_builder_command_list_free(layout->commands);
148 layout->number_of_commands = 0;
149 layout->root_image_generation = 0;
150 layout->size = (ivec2){};
151 layout->first_layer_start = 0;
152 layout->commands = NULL;
153 }
154
155 struct layout_manager *layout_manager_new(unsigned max_buffer_age) {
156 struct layout_manager *lm = malloc(sizeof(struct layout_manager) +
157 (max_buffer_age + 1) * sizeof(struct layout));
158 lm->max_buffer_age = max_buffer_age + 1;
159 lm->current = 0;
160 lm->layer_indices = NULL;
161 list_init_head(&lm->free_indices);
162 pixman_region32_init(&lm->scratch_region);
163 for (unsigned i = 0; i <= max_buffer_age; i++) {
164 lm->layouts[i] = (struct layout){};
165 lm->layouts[i].layers = dynarr_new(struct layer, 5);
166 }
167 return lm;
168 }
169
170 void layout_manager_clear(struct layout_manager *lm) {
171 for (unsigned i = 0; i < lm->max_buffer_age; i++) {
172 layout_deinit(&lm->layouts[i]);
173 }
174 struct layer_index *index, *tmp;
175 HASH_ITER(hh, lm->layer_indices, index, tmp) {
176 HASH_DEL(lm->layer_indices, index);
177 list_insert_after(&lm->free_indices, &index->free_list);
178 }
179 lm->current = 0;
180 }
181
182 void layout_manager_free(struct layout_manager *lm) {
183 for (unsigned i = 0; i < lm->max_buffer_age; i++) {
184 layout_deinit(&lm->layouts[i]);
185 dynarr_free(lm->layouts[i].layers, layer_deinit);
186 }
187 struct layer_index *index, *tmp;
188 HASH_ITER(hh, lm->layer_indices, index, tmp) {
189 HASH_DEL(lm->layer_indices, index);
190 free(index);
191 }
192 list_foreach_safe(struct layer_index, i, &lm->free_indices, free_list) {
193 list_remove(&i->free_list);
194 free(i);
195 }
196 pixman_region32_fini(&lm->scratch_region);
197 free(lm);
198 }
199
200 // ## Layout manager Concepts
201 //
202 // - "layer", because windows form a stack, it's easy to think of the final screen as
203 // a series of layers stacked on top of each other. Each layer is the same size as
204 // the screen, and contains a single window positioned somewhere in the layer. Other
205 // parts of the layer are transparent.
206 // When talking about "screen at a certain layer", we mean the result you would get
207 // if you stack all layers from the bottom up to that certain layer, ignoring any layers
208 // above.
209
210 void layout_manager_append_layout(struct layout_manager *lm, struct wm *wm,
211 uint64_t root_pixmap_generation, ivec2 size) {
212 auto prev_layout = &lm->layouts[lm->current];
213 lm->current = (lm->current + 1) % lm->max_buffer_age;
214 auto layout = &lm->layouts[lm->current];
215 command_builder_command_list_free(layout->commands);
216 layout->root_image_generation = root_pixmap_generation;
217 layout->size = size;
218
219 unsigned rank = 0;
220 struct layer_index *index, *next_index;
221 wm_stack_foreach_rev(wm, cursor) {
222 auto w = wm_ref_deref(cursor);
223 if (w == NULL) {
224 continue;
225 }
226 dynarr_resize(layout->layers, rank + 1, layer_init, layer_deinit);
227 if (!layer_from_window(&layout->layers[rank], (struct win *)w, size)) {
228 continue;
229 }
230
231 HASH_FIND(hh, lm->layer_indices, &layout->layers[rank].key,
232 sizeof(layout->layers[rank].key), index);
233 if (index) {
234 prev_layout->layers[index->index].next_rank = (int)rank;
235 layout->layers[rank].prev_rank = (int)index->index;
236 }
237 rank++;
238 }
239 dynarr_truncate(layout->layers, rank, layer_deinit);
240
241 // Update indices. If a layer exist in both prev_layout and current layout,
242 // we could update the index using next_rank; if a layer no longer exist in
243 // current layout, we remove it from the indices.
244 HASH_ITER(hh, lm->layer_indices, index, next_index) {
245 if (prev_layout->layers[index->index].next_rank == -1) {
246 HASH_DEL(lm->layer_indices, index);
247 list_insert_after(&lm->free_indices, &index->free_list);
248 } else {
249 index->index = (unsigned)prev_layout->layers[index->index].next_rank;
250 }
251 }
252 // And finally, if a layer in current layout didn't exist in prev_layout, add a
253 // new index for it.
254 dynarr_foreach(layout->layers, layer) {
255 if (layer->prev_rank != -1) {
256 continue;
257 }
258 if (!list_is_empty(&lm->free_indices)) {
259 index =
260 list_entry(lm->free_indices.next, struct layer_index, free_list);
261 list_remove(&index->free_list);
262 } else {
263 index = cmalloc(struct layer_index);
264 }
265 index->key = layer->key;
266 index->index = to_u32_checked(layer - layout->layers);
267 HASH_ADD(hh, lm->layer_indices, key, sizeof(index->key), index);
268 }
269 }
270
271 struct layout *layout_manager_layout(struct layout_manager *lm, unsigned age) {
272 if (age >= lm->max_buffer_age) {
273 assert(false);
274 return NULL;
275 }
276 return &lm->layouts[(lm->current + lm->max_buffer_age - age) % lm->max_buffer_age];
277 }
278
279 void layout_manager_collect_window_damage(const struct layout_manager *lm, unsigned index,
280 unsigned buffer_age, region_t *damage) {
281 auto curr = lm->current;
282 auto layer = &lm->layouts[curr].layers[index];
283 for (unsigned i = 0; i < buffer_age; i++) {
284 pixman_region32_union(damage, damage, &layer->damaged);
285 curr = (curr + lm->max_buffer_age - 1) % lm->max_buffer_age;
286 assert(layer->prev_rank >= 0);
287 layer = &lm->layouts[curr].layers[layer->prev_rank];
288 }
289 }
290
291 unsigned layout_manager_max_buffer_age(const struct layout_manager *lm) {
292 return lm->max_buffer_age - 1;
293 }
294
295 int layer_prev_rank(struct layout_manager *lm, unsigned buffer_age, unsigned index_) {
296 int index = to_int_checked(index_);
297 unsigned layout = lm->current;
298 while (buffer_age--) {
299 index = lm->layouts[layout].layers[index].prev_rank;
300 if (index < 0) {
301 break;
302 }
303 layout = (layout + lm->max_buffer_age - 1) % lm->max_buffer_age;
304 }
305 return index;
306 }
307
308 int layer_next_rank(struct layout_manager *lm, unsigned buffer_age, unsigned index_) {
309 int index = to_int_checked(index_);
310 unsigned layout = (lm->current + lm->max_buffer_age - buffer_age) % lm->max_buffer_age;
311 while (buffer_age--) {
312 index = lm->layouts[layout].layers[index].next_rank;
313 if (index < 0) {
314 break;
315 }
316 layout = (layout + 1) % lm->max_buffer_age;
317 }
318 return index;
319 }