diff options
Diffstat (limited to 'sway/desktop/render.c')
-rw-r--r-- | sway/desktop/render.c | 893 |
1 files changed, 893 insertions, 0 deletions
diff --git a/sway/desktop/render.c b/sway/desktop/render.c new file mode 100644 index 00000000..43948f29 --- /dev/null +++ b/sway/desktop/render.c | |||
@@ -0,0 +1,893 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
2 | #include <assert.h> | ||
3 | #include <stdlib.h> | ||
4 | #include <strings.h> | ||
5 | #include <time.h> | ||
6 | #include <wayland-server.h> | ||
7 | #include <wlr/render/wlr_renderer.h> | ||
8 | #include <wlr/types/wlr_box.h> | ||
9 | #include <wlr/types/wlr_buffer.h> | ||
10 | #include <wlr/types/wlr_matrix.h> | ||
11 | #include <wlr/types/wlr_output_damage.h> | ||
12 | #include <wlr/types/wlr_output_layout.h> | ||
13 | #include <wlr/types/wlr_output.h> | ||
14 | #include <wlr/types/wlr_surface.h> | ||
15 | #include <wlr/util/region.h> | ||
16 | #include "log.h" | ||
17 | #include "sway/config.h" | ||
18 | #include "sway/input/input-manager.h" | ||
19 | #include "sway/input/seat.h" | ||
20 | #include "sway/layers.h" | ||
21 | #include "sway/output.h" | ||
22 | #include "sway/server.h" | ||
23 | #include "sway/tree/arrange.h" | ||
24 | #include "sway/tree/container.h" | ||
25 | #include "sway/tree/layout.h" | ||
26 | #include "sway/tree/view.h" | ||
27 | #include "sway/tree/workspace.h" | ||
28 | |||
29 | struct render_data { | ||
30 | struct root_geometry root_geo; | ||
31 | struct sway_output *output; | ||
32 | pixman_region32_t *damage; | ||
33 | struct sway_view *view; | ||
34 | float alpha; | ||
35 | }; | ||
36 | |||
37 | static void scale_box(struct wlr_box *box, float scale) { | ||
38 | box->x *= scale; | ||
39 | box->y *= scale; | ||
40 | box->width *= scale; | ||
41 | box->height *= scale; | ||
42 | } | ||
43 | |||
44 | static void scissor_output(struct wlr_output *wlr_output, | ||
45 | pixman_box32_t *rect) { | ||
46 | struct wlr_renderer *renderer = wlr_backend_get_renderer(wlr_output->backend); | ||
47 | assert(renderer); | ||
48 | |||
49 | struct wlr_box box = { | ||
50 | .x = rect->x1, | ||
51 | .y = rect->y1, | ||
52 | .width = rect->x2 - rect->x1, | ||
53 | .height = rect->y2 - rect->y1, | ||
54 | }; | ||
55 | |||
56 | int ow, oh; | ||
57 | wlr_output_transformed_resolution(wlr_output, &ow, &oh); | ||
58 | |||
59 | enum wl_output_transform transform = | ||
60 | wlr_output_transform_invert(wlr_output->transform); | ||
61 | wlr_box_transform(&box, transform, ow, oh, &box); | ||
62 | |||
63 | wlr_renderer_scissor(renderer, &box); | ||
64 | } | ||
65 | |||
66 | static void render_texture(struct wlr_output *wlr_output, | ||
67 | pixman_region32_t *output_damage, struct wlr_texture *texture, | ||
68 | const struct wlr_box *box, const float matrix[static 9], float alpha) { | ||
69 | struct wlr_renderer *renderer = | ||
70 | wlr_backend_get_renderer(wlr_output->backend); | ||
71 | |||
72 | pixman_region32_t damage; | ||
73 | pixman_region32_init(&damage); | ||
74 | pixman_region32_union_rect(&damage, &damage, box->x, box->y, | ||
75 | box->width, box->height); | ||
76 | pixman_region32_intersect(&damage, &damage, output_damage); | ||
77 | bool damaged = pixman_region32_not_empty(&damage); | ||
78 | if (!damaged) { | ||
79 | goto damage_finish; | ||
80 | } | ||
81 | |||
82 | int nrects; | ||
83 | pixman_box32_t *rects = pixman_region32_rectangles(&damage, &nrects); | ||
84 | for (int i = 0; i < nrects; ++i) { | ||
85 | scissor_output(wlr_output, &rects[i]); | ||
86 | wlr_render_texture_with_matrix(renderer, texture, matrix, alpha); | ||
87 | } | ||
88 | |||
89 | damage_finish: | ||
90 | pixman_region32_fini(&damage); | ||
91 | } | ||
92 | |||
93 | static void render_surface_iterator(struct wlr_surface *surface, int sx, int sy, | ||
94 | void *_data) { | ||
95 | struct render_data *data = _data; | ||
96 | struct wlr_output *wlr_output = data->output->wlr_output; | ||
97 | float rotation = data->root_geo.rotation; | ||
98 | pixman_region32_t *output_damage = data->damage; | ||
99 | float alpha = data->alpha; | ||
100 | |||
101 | struct wlr_texture *texture = wlr_surface_get_texture(surface); | ||
102 | if (!texture) { | ||
103 | return; | ||
104 | } | ||
105 | |||
106 | struct wlr_box box; | ||
107 | bool intersects = output_get_surface_box(&data->root_geo, data->output, | ||
108 | surface, sx, sy, &box); | ||
109 | if (!intersects) { | ||
110 | return; | ||
111 | } | ||
112 | |||
113 | scale_box(&box, wlr_output->scale); | ||
114 | |||
115 | float matrix[9]; | ||
116 | enum wl_output_transform transform = | ||
117 | wlr_output_transform_invert(surface->current.transform); | ||
118 | wlr_matrix_project_box(matrix, &box, transform, rotation, | ||
119 | wlr_output->transform_matrix); | ||
120 | |||
121 | render_texture(wlr_output, output_damage, texture, &box, matrix, alpha); | ||
122 | } | ||
123 | |||
124 | static void render_layer(struct sway_output *output, | ||
125 | pixman_region32_t *damage, struct wl_list *layer_surfaces) { | ||
126 | struct render_data data = { | ||
127 | .output = output, | ||
128 | .damage = damage, | ||
129 | .alpha = 1.0f, | ||
130 | }; | ||
131 | output_layer_for_each_surface(layer_surfaces, &data.root_geo, | ||
132 | render_surface_iterator, &data); | ||
133 | } | ||
134 | |||
135 | static void render_unmanaged(struct sway_output *output, | ||
136 | pixman_region32_t *damage, struct wl_list *unmanaged) { | ||
137 | struct render_data data = { | ||
138 | .output = output, | ||
139 | .damage = damage, | ||
140 | .alpha = 1.0f, | ||
141 | }; | ||
142 | output_unmanaged_for_each_surface(unmanaged, output, &data.root_geo, | ||
143 | render_surface_iterator, &data); | ||
144 | } | ||
145 | |||
146 | static void render_drag_icons(struct sway_output *output, | ||
147 | pixman_region32_t *damage, struct wl_list *drag_icons) { | ||
148 | struct render_data data = { | ||
149 | .output = output, | ||
150 | .damage = damage, | ||
151 | .alpha = 1.0f, | ||
152 | }; | ||
153 | output_drag_icons_for_each_surface(drag_icons, output, &data.root_geo, | ||
154 | render_surface_iterator, &data); | ||
155 | } | ||
156 | |||
157 | static void render_rect(struct wlr_output *wlr_output, | ||
158 | pixman_region32_t *output_damage, const struct wlr_box *_box, | ||
159 | float color[static 4]) { | ||
160 | struct wlr_renderer *renderer = | ||
161 | wlr_backend_get_renderer(wlr_output->backend); | ||
162 | |||
163 | struct wlr_box box; | ||
164 | memcpy(&box, _box, sizeof(struct wlr_box)); | ||
165 | box.x -= wlr_output->lx * wlr_output->scale; | ||
166 | box.y -= wlr_output->ly * wlr_output->scale; | ||
167 | |||
168 | pixman_region32_t damage; | ||
169 | pixman_region32_init(&damage); | ||
170 | pixman_region32_union_rect(&damage, &damage, box.x, box.y, | ||
171 | box.width, box.height); | ||
172 | pixman_region32_intersect(&damage, &damage, output_damage); | ||
173 | bool damaged = pixman_region32_not_empty(&damage); | ||
174 | if (!damaged) { | ||
175 | goto damage_finish; | ||
176 | } | ||
177 | |||
178 | int nrects; | ||
179 | pixman_box32_t *rects = pixman_region32_rectangles(&damage, &nrects); | ||
180 | for (int i = 0; i < nrects; ++i) { | ||
181 | scissor_output(wlr_output, &rects[i]); | ||
182 | wlr_render_rect(renderer, &box, color, | ||
183 | wlr_output->transform_matrix); | ||
184 | } | ||
185 | |||
186 | damage_finish: | ||
187 | pixman_region32_fini(&damage); | ||
188 | } | ||
189 | |||
190 | static void premultiply_alpha(float color[4], float opacity) { | ||
191 | color[3] *= opacity; | ||
192 | color[0] *= color[3]; | ||
193 | color[1] *= color[3]; | ||
194 | color[2] *= color[3]; | ||
195 | } | ||
196 | |||
197 | static void render_view_surfaces(struct sway_view *view, | ||
198 | struct sway_output *output, pixman_region32_t *damage, float alpha) { | ||
199 | struct render_data data = { | ||
200 | .output = output, | ||
201 | .damage = damage, | ||
202 | .view = view, | ||
203 | .alpha = alpha, | ||
204 | }; | ||
205 | output_view_for_each_surface(view, output, &data.root_geo, | ||
206 | render_surface_iterator, &data); | ||
207 | } | ||
208 | |||
209 | static void render_saved_view(struct sway_view *view, | ||
210 | struct sway_output *output, pixman_region32_t *damage, float alpha) { | ||
211 | struct wlr_output *wlr_output = output->wlr_output; | ||
212 | |||
213 | int width, height; | ||
214 | struct wlr_texture *texture = | ||
215 | transaction_get_saved_texture(view, &width, &height); | ||
216 | if (!texture) { | ||
217 | return; | ||
218 | } | ||
219 | struct wlr_box box = { | ||
220 | .x = view->swayc->current.view_x - output->swayc->current.swayc_x, | ||
221 | .y = view->swayc->current.view_y - output->swayc->current.swayc_y, | ||
222 | .width = width, | ||
223 | .height = height, | ||
224 | }; | ||
225 | |||
226 | struct wlr_box output_box = { | ||
227 | .width = output->swayc->current.swayc_width, | ||
228 | .height = output->swayc->current.swayc_height, | ||
229 | }; | ||
230 | |||
231 | struct wlr_box intersection; | ||
232 | bool intersects = wlr_box_intersection(&output_box, &box, &intersection); | ||
233 | if (!intersects) { | ||
234 | return; | ||
235 | } | ||
236 | |||
237 | scale_box(&box, wlr_output->scale); | ||
238 | |||
239 | float matrix[9]; | ||
240 | wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0, | ||
241 | wlr_output->transform_matrix); | ||
242 | |||
243 | render_texture(wlr_output, damage, texture, &box, matrix, alpha); | ||
244 | } | ||
245 | |||
246 | /** | ||
247 | * Render a view's surface and left/bottom/right borders. | ||
248 | */ | ||
249 | static void render_view(struct sway_output *output, pixman_region32_t *damage, | ||
250 | struct sway_container *con, struct border_colors *colors) { | ||
251 | struct sway_view *view = con->sway_view; | ||
252 | if (view->swayc->instructions->length) { | ||
253 | render_saved_view(view, output, damage, view->swayc->alpha); | ||
254 | } else { | ||
255 | render_view_surfaces(view, output, damage, view->swayc->alpha); | ||
256 | } | ||
257 | |||
258 | struct wlr_box box; | ||
259 | float output_scale = output->wlr_output->scale; | ||
260 | float color[4]; | ||
261 | struct sway_container_state *state = &con->current; | ||
262 | |||
263 | if (state->border != B_NONE) { | ||
264 | if (state->border_left) { | ||
265 | memcpy(&color, colors->child_border, sizeof(float) * 4); | ||
266 | premultiply_alpha(color, con->alpha); | ||
267 | box.x = state->swayc_x; | ||
268 | box.y = state->view_y; | ||
269 | box.width = state->border_thickness; | ||
270 | box.height = state->view_height; | ||
271 | scale_box(&box, output_scale); | ||
272 | render_rect(output->wlr_output, damage, &box, color); | ||
273 | } | ||
274 | |||
275 | if (state->border_right) { | ||
276 | if (state->parent->current.children->length == 1 | ||
277 | && state->parent->current.layout == L_HORIZ) { | ||
278 | memcpy(&color, colors->indicator, sizeof(float) * 4); | ||
279 | } else { | ||
280 | memcpy(&color, colors->child_border, sizeof(float) * 4); | ||
281 | } | ||
282 | premultiply_alpha(color, con->alpha); | ||
283 | box.x = state->view_x + state->view_width; | ||
284 | box.y = state->view_y; | ||
285 | box.width = state->border_thickness; | ||
286 | box.height = state->view_height; | ||
287 | scale_box(&box, output_scale); | ||
288 | render_rect(output->wlr_output, damage, &box, color); | ||
289 | } | ||
290 | |||
291 | if (state->border_bottom) { | ||
292 | if (state->parent->current.children->length == 1 | ||
293 | && con->current.parent->current.layout == L_VERT) { | ||
294 | memcpy(&color, colors->indicator, sizeof(float) * 4); | ||
295 | } else { | ||
296 | memcpy(&color, colors->child_border, sizeof(float) * 4); | ||
297 | } | ||
298 | premultiply_alpha(color, con->alpha); | ||
299 | box.x = state->swayc_x; | ||
300 | box.y = state->view_y + state->view_height; | ||
301 | box.width = state->swayc_width; | ||
302 | box.height = state->border_thickness; | ||
303 | scale_box(&box, output_scale); | ||
304 | render_rect(output->wlr_output, damage, &box, color); | ||
305 | } | ||
306 | } | ||
307 | } | ||
308 | |||
309 | /** | ||
310 | * Render a titlebar. | ||
311 | * | ||
312 | * Care must be taken not to render over the same pixel multiple times, | ||
313 | * otherwise the colors will be incorrect when using opacity. | ||
314 | * | ||
315 | * The height is: 1px border, 3px padding, font height, 3px padding, 1px border | ||
316 | * The left side for L_TABBED is: 1px border, 2px padding, title | ||
317 | * The left side for other layouts is: 3px padding, title | ||
318 | */ | ||
319 | static void render_titlebar(struct sway_output *output, | ||
320 | pixman_region32_t *output_damage, struct sway_container *con, | ||
321 | int x, int y, int width, | ||
322 | struct border_colors *colors, struct wlr_texture *title_texture, | ||
323 | struct wlr_texture *marks_texture) { | ||
324 | struct wlr_box box; | ||
325 | float color[4]; | ||
326 | struct sway_container_state *state = &con->current; | ||
327 | float output_scale = output->wlr_output->scale; | ||
328 | enum sway_container_layout layout = state->parent->current.layout; | ||
329 | list_t *children = state->parent->current.children; | ||
330 | bool is_last_child = children->items[children->length - 1] == con; | ||
331 | double output_x = output->swayc->current.swayc_x; | ||
332 | double output_y = output->swayc->current.swayc_y; | ||
333 | |||
334 | // Single pixel bar above title | ||
335 | memcpy(&color, colors->border, sizeof(float) * 4); | ||
336 | premultiply_alpha(color, con->alpha); | ||
337 | box.x = x; | ||
338 | box.y = y; | ||
339 | box.width = width; | ||
340 | box.height = TITLEBAR_BORDER_THICKNESS; | ||
341 | scale_box(&box, output_scale); | ||
342 | render_rect(output->wlr_output, output_damage, &box, color); | ||
343 | |||
344 | // Single pixel bar below title | ||
345 | size_t left_offset = 0, right_offset = 0; | ||
346 | bool connects_sides = false; | ||
347 | if (layout == L_HORIZ || layout == L_VERT || | ||
348 | (layout == L_STACKED && is_last_child)) { | ||
349 | if (con->type == C_VIEW) { | ||
350 | left_offset = state->border_left * state->border_thickness; | ||
351 | right_offset = state->border_right * state->border_thickness; | ||
352 | connects_sides = true; | ||
353 | } | ||
354 | } | ||
355 | box.x = x + left_offset; | ||
356 | box.y = y + container_titlebar_height() - TITLEBAR_BORDER_THICKNESS; | ||
357 | box.width = width - left_offset - right_offset; | ||
358 | box.height = TITLEBAR_BORDER_THICKNESS; | ||
359 | scale_box(&box, output_scale); | ||
360 | render_rect(output->wlr_output, output_damage, &box, color); | ||
361 | |||
362 | if (layout == L_TABBED) { | ||
363 | // Single pixel left edge | ||
364 | box.x = x; | ||
365 | box.y = y + TITLEBAR_BORDER_THICKNESS; | ||
366 | box.width = TITLEBAR_BORDER_THICKNESS; | ||
367 | box.height = | ||
368 | container_titlebar_height() - TITLEBAR_BORDER_THICKNESS * 2; | ||
369 | scale_box(&box, output_scale); | ||
370 | render_rect(output->wlr_output, output_damage, &box, color); | ||
371 | |||
372 | // Single pixel right edge | ||
373 | box.x = (x + width - TITLEBAR_BORDER_THICKNESS) * output_scale; | ||
374 | render_rect(output->wlr_output, output_damage, &box, color); | ||
375 | } | ||
376 | |||
377 | size_t inner_width = width - TITLEBAR_H_PADDING * 2; | ||
378 | |||
379 | // Marks | ||
380 | size_t marks_ob_width = 0; // output-buffer-local | ||
381 | if (config->show_marks && marks_texture) { | ||
382 | struct wlr_box texture_box; | ||
383 | wlr_texture_get_size(marks_texture, | ||
384 | &texture_box.width, &texture_box.height); | ||
385 | texture_box.x = (x - output_x + width - TITLEBAR_H_PADDING) | ||
386 | * output_scale - texture_box.width; | ||
387 | texture_box.y = (y - output_y + TITLEBAR_V_PADDING) * output_scale; | ||
388 | |||
389 | float matrix[9]; | ||
390 | wlr_matrix_project_box(matrix, &texture_box, | ||
391 | WL_OUTPUT_TRANSFORM_NORMAL, | ||
392 | 0.0, output->wlr_output->transform_matrix); | ||
393 | |||
394 | if (inner_width * output_scale < texture_box.width) { | ||
395 | texture_box.width = inner_width * output_scale; | ||
396 | } | ||
397 | render_texture(output->wlr_output, output_damage, marks_texture, | ||
398 | &texture_box, matrix, con->alpha); | ||
399 | marks_ob_width = texture_box.width; | ||
400 | |||
401 | // Gap between the marks and bottom padding, for when the marks texture | ||
402 | // height is smaller than the config's font height | ||
403 | memcpy(&color, colors->background, sizeof(float) * 4); | ||
404 | premultiply_alpha(color, con->alpha); | ||
405 | box.x = texture_box.x; | ||
406 | box.y = texture_box.y + texture_box.height; | ||
407 | box.width = texture_box.width; | ||
408 | box.height = config->font_height * output_scale - texture_box.height; | ||
409 | if (box.height > 0) { | ||
410 | render_rect(output->wlr_output, output_damage, &box, color); | ||
411 | } | ||
412 | } | ||
413 | |||
414 | // Title text | ||
415 | size_t title_ob_width = 0; // output-buffer-local | ||
416 | if (title_texture) { | ||
417 | struct wlr_box texture_box; | ||
418 | wlr_texture_get_size(title_texture, | ||
419 | &texture_box.width, &texture_box.height); | ||
420 | texture_box.x = (x - output_x + TITLEBAR_H_PADDING) * output_scale; | ||
421 | texture_box.y = (y - output_y + TITLEBAR_V_PADDING) * output_scale; | ||
422 | |||
423 | float matrix[9]; | ||
424 | wlr_matrix_project_box(matrix, &texture_box, | ||
425 | WL_OUTPUT_TRANSFORM_NORMAL, | ||
426 | 0.0, output->wlr_output->transform_matrix); | ||
427 | |||
428 | if (inner_width * output_scale - marks_ob_width < texture_box.width) { | ||
429 | texture_box.width = inner_width * output_scale - marks_ob_width; | ||
430 | } | ||
431 | render_texture(output->wlr_output, output_damage, title_texture, | ||
432 | &texture_box, matrix, con->alpha); | ||
433 | title_ob_width = texture_box.width; | ||
434 | |||
435 | // Gap between the title and bottom padding, for when the title texture | ||
436 | // height is smaller than the config's font height | ||
437 | memcpy(&color, colors->background, sizeof(float) * 4); | ||
438 | premultiply_alpha(color, con->alpha); | ||
439 | box.x = texture_box.x; | ||
440 | box.y = texture_box.y + texture_box.height; | ||
441 | box.width = texture_box.width; | ||
442 | box.height = config->font_height * output_scale - texture_box.height; | ||
443 | if (box.height > 0) { | ||
444 | render_rect(output->wlr_output, output_damage, &box, color); | ||
445 | } | ||
446 | } | ||
447 | |||
448 | // Padding above title | ||
449 | memcpy(&color, colors->background, sizeof(float) * 4); | ||
450 | premultiply_alpha(color, con->alpha); | ||
451 | box.x = x + (layout == L_TABBED) * TITLEBAR_BORDER_THICKNESS; | ||
452 | box.y = y + TITLEBAR_BORDER_THICKNESS; | ||
453 | box.width = width - (layout == L_TABBED) * TITLEBAR_BORDER_THICKNESS * 2; | ||
454 | box.height = TITLEBAR_V_PADDING - TITLEBAR_BORDER_THICKNESS; | ||
455 | scale_box(&box, output_scale); | ||
456 | render_rect(output->wlr_output, output_damage, &box, color); | ||
457 | |||
458 | // Padding below title | ||
459 | box.y = (y + TITLEBAR_V_PADDING + config->font_height) * output_scale; | ||
460 | render_rect(output->wlr_output, output_damage, &box, color); | ||
461 | |||
462 | // Filler between title and marks | ||
463 | box.width = inner_width * output_scale - title_ob_width - marks_ob_width; | ||
464 | if (box.width > 0) { | ||
465 | box.x = (x + TITLEBAR_H_PADDING) * output_scale + title_ob_width; | ||
466 | box.y = (y + TITLEBAR_V_PADDING) * output_scale; | ||
467 | box.height = config->font_height * output_scale; | ||
468 | render_rect(output->wlr_output, output_damage, &box, color); | ||
469 | } | ||
470 | |||
471 | // Padding left of title | ||
472 | left_offset = (layout == L_TABBED) * TITLEBAR_BORDER_THICKNESS; | ||
473 | box.x = x + left_offset; | ||
474 | box.y = y + TITLEBAR_V_PADDING; | ||
475 | box.width = TITLEBAR_H_PADDING - left_offset; | ||
476 | box.height = config->font_height; | ||
477 | scale_box(&box, output_scale); | ||
478 | render_rect(output->wlr_output, output_damage, &box, color); | ||
479 | |||
480 | // Padding right of marks | ||
481 | right_offset = (layout == L_TABBED) * TITLEBAR_BORDER_THICKNESS; | ||
482 | box.x = x + width - TITLEBAR_H_PADDING; | ||
483 | box.y = y + TITLEBAR_V_PADDING; | ||
484 | box.width = TITLEBAR_H_PADDING - right_offset; | ||
485 | box.height = config->font_height; | ||
486 | scale_box(&box, output_scale); | ||
487 | render_rect(output->wlr_output, output_damage, &box, color); | ||
488 | |||
489 | if (connects_sides) { | ||
490 | // Left pixel in line with bottom bar | ||
491 | box.x = x; | ||
492 | box.y = y + container_titlebar_height() - TITLEBAR_BORDER_THICKNESS; | ||
493 | box.width = state->border_thickness * state->border_left; | ||
494 | box.height = TITLEBAR_BORDER_THICKNESS; | ||
495 | scale_box(&box, output_scale); | ||
496 | render_rect(output->wlr_output, output_damage, &box, color); | ||
497 | |||
498 | // Right pixel in line with bottom bar | ||
499 | box.x = x + width - state->border_thickness * state->border_right; | ||
500 | box.y = y + container_titlebar_height() - TITLEBAR_BORDER_THICKNESS; | ||
501 | box.width = state->border_thickness * state->border_right; | ||
502 | box.height = TITLEBAR_BORDER_THICKNESS; | ||
503 | scale_box(&box, output_scale); | ||
504 | render_rect(output->wlr_output, output_damage, &box, color); | ||
505 | } | ||
506 | } | ||
507 | |||
508 | /** | ||
509 | * Render the top border line for a view using "border pixel". | ||
510 | */ | ||
511 | static void render_top_border(struct sway_output *output, | ||
512 | pixman_region32_t *output_damage, struct sway_container *con, | ||
513 | struct border_colors *colors) { | ||
514 | struct sway_container_state *state = &con->current; | ||
515 | if (!state->border_top) { | ||
516 | return; | ||
517 | } | ||
518 | struct wlr_box box; | ||
519 | float color[4]; | ||
520 | float output_scale = output->wlr_output->scale; | ||
521 | |||
522 | // Child border - top edge | ||
523 | memcpy(&color, colors->child_border, sizeof(float) * 4); | ||
524 | premultiply_alpha(color, con->alpha); | ||
525 | box.x = state->swayc_x; | ||
526 | box.y = state->swayc_y; | ||
527 | box.width = state->swayc_width; | ||
528 | box.height = state->border_thickness; | ||
529 | scale_box(&box, output_scale); | ||
530 | render_rect(output->wlr_output, output_damage, &box, color); | ||
531 | } | ||
532 | |||
533 | static void render_container(struct sway_output *output, | ||
534 | pixman_region32_t *damage, struct sway_container *con, bool parent_focused); | ||
535 | |||
536 | /** | ||
537 | * Render a container's children using a L_HORIZ or L_VERT layout. | ||
538 | * | ||
539 | * Wrap child views in borders and leave child containers borderless because | ||
540 | * they'll apply their own borders to their children. | ||
541 | */ | ||
542 | static void render_container_simple(struct sway_output *output, | ||
543 | pixman_region32_t *damage, struct sway_container *con, | ||
544 | bool parent_focused) { | ||
545 | struct sway_seat *seat = input_manager_current_seat(input_manager); | ||
546 | struct sway_container *focus = seat_get_focus(seat); | ||
547 | |||
548 | for (int i = 0; i < con->current.children->length; ++i) { | ||
549 | struct sway_container *child = con->current.children->items[i]; | ||
550 | |||
551 | if (child->type == C_VIEW) { | ||
552 | struct sway_view *view = child->sway_view; | ||
553 | struct border_colors *colors; | ||
554 | struct wlr_texture *title_texture; | ||
555 | struct wlr_texture *marks_texture; | ||
556 | struct sway_container_state *state = &child->current; | ||
557 | |||
558 | if (focus == child || parent_focused) { | ||
559 | colors = &config->border_colors.focused; | ||
560 | title_texture = child->title_focused; | ||
561 | marks_texture = view->marks_focused; | ||
562 | } else if (seat_get_focus_inactive(seat, con) == child) { | ||
563 | colors = &config->border_colors.focused_inactive; | ||
564 | title_texture = child->title_focused_inactive; | ||
565 | marks_texture = view->marks_focused_inactive; | ||
566 | } else { | ||
567 | colors = &config->border_colors.unfocused; | ||
568 | title_texture = child->title_unfocused; | ||
569 | marks_texture = view->marks_unfocused; | ||
570 | } | ||
571 | |||
572 | if (state->border == B_NORMAL) { | ||
573 | render_titlebar(output, damage, child, state->swayc_x, | ||
574 | state->swayc_y, state->swayc_width, colors, | ||
575 | title_texture, marks_texture); | ||
576 | } else { | ||
577 | render_top_border(output, damage, child, colors); | ||
578 | } | ||
579 | render_view(output, damage, child, colors); | ||
580 | } else { | ||
581 | render_container(output, damage, child, | ||
582 | parent_focused || focus == child); | ||
583 | } | ||
584 | } | ||
585 | } | ||
586 | |||
587 | /** | ||
588 | * Render a container's children using the L_TABBED layout. | ||
589 | */ | ||
590 | static void render_container_tabbed(struct sway_output *output, | ||
591 | pixman_region32_t *damage, struct sway_container *con, | ||
592 | bool parent_focused) { | ||
593 | if (!con->current.children->length) { | ||
594 | return; | ||
595 | } | ||
596 | struct sway_seat *seat = input_manager_current_seat(input_manager); | ||
597 | struct sway_container *focus = seat_get_focus(seat); | ||
598 | struct sway_container *current = seat_get_active_current_child(seat, con); | ||
599 | struct border_colors *current_colors = &config->border_colors.unfocused; | ||
600 | struct sway_container_state *pstate = &con->current; | ||
601 | |||
602 | // Render tabs | ||
603 | for (int i = 0; i < con->current.children->length; ++i) { | ||
604 | struct sway_container *child = con->current.children->items[i]; | ||
605 | struct sway_view *view = child->type == C_VIEW ? child->sway_view : NULL; | ||
606 | struct sway_container_state *cstate = &child->current; | ||
607 | struct border_colors *colors; | ||
608 | struct wlr_texture *title_texture; | ||
609 | struct wlr_texture *marks_texture; | ||
610 | |||
611 | if (focus == child || parent_focused) { | ||
612 | colors = &config->border_colors.focused; | ||
613 | title_texture = child->title_focused; | ||
614 | marks_texture = view ? view->marks_focused : NULL; | ||
615 | } else if (child == current) { | ||
616 | colors = &config->border_colors.focused_inactive; | ||
617 | title_texture = child->title_focused_inactive; | ||
618 | marks_texture = view ? view->marks_focused_inactive : NULL; | ||
619 | } else { | ||
620 | colors = &config->border_colors.unfocused; | ||
621 | title_texture = child->title_unfocused; | ||
622 | marks_texture = view ? view->marks_unfocused : NULL; | ||
623 | } | ||
624 | |||
625 | int tab_width = pstate->swayc_width / pstate->children->length; | ||
626 | int x = pstate->swayc_x + tab_width * i; | ||
627 | // Make last tab use the remaining width of the parent | ||
628 | if (i == pstate->children->length - 1) { | ||
629 | tab_width = pstate->swayc_width - tab_width * i; | ||
630 | } | ||
631 | |||
632 | render_titlebar(output, damage, child, x, cstate->swayc_y, tab_width, | ||
633 | colors, title_texture, marks_texture); | ||
634 | |||
635 | if (child == current) { | ||
636 | current_colors = colors; | ||
637 | } | ||
638 | } | ||
639 | |||
640 | // Render surface and left/right/bottom borders | ||
641 | if (current) { | ||
642 | if (current->type == C_VIEW) { | ||
643 | render_view(output, damage, current, current_colors); | ||
644 | } else { | ||
645 | render_container(output, damage, current, | ||
646 | parent_focused || current == focus); | ||
647 | } | ||
648 | } | ||
649 | } | ||
650 | |||
651 | /** | ||
652 | * Render a container's children using the L_STACKED layout. | ||
653 | */ | ||
654 | static void render_container_stacked(struct sway_output *output, | ||
655 | pixman_region32_t *damage, struct sway_container *con, | ||
656 | bool parent_focused) { | ||
657 | if (!con->current.children->length) { | ||
658 | return; | ||
659 | } | ||
660 | struct sway_seat *seat = input_manager_current_seat(input_manager); | ||
661 | struct sway_container *focus = seat_get_focus(seat); | ||
662 | struct sway_container *current = seat_get_active_current_child(seat, con); | ||
663 | struct border_colors *current_colors = &config->border_colors.unfocused; | ||
664 | struct sway_container_state *pstate = &con->current; | ||
665 | |||
666 | // Render titles | ||
667 | for (int i = 0; i < con->current.children->length; ++i) { | ||
668 | struct sway_container *child = con->current.children->items[i]; | ||
669 | struct sway_view *view = child->type == C_VIEW ? child->sway_view : NULL; | ||
670 | struct sway_container_state *cstate = &child->current; | ||
671 | struct border_colors *colors; | ||
672 | struct wlr_texture *title_texture; | ||
673 | struct wlr_texture *marks_texture; | ||
674 | |||
675 | if (focus == child || parent_focused) { | ||
676 | colors = &config->border_colors.focused; | ||
677 | title_texture = child->title_focused; | ||
678 | marks_texture = view ? view->marks_focused : NULL; | ||
679 | } else if (child == current) { | ||
680 | colors = &config->border_colors.focused_inactive; | ||
681 | title_texture = child->title_focused_inactive; | ||
682 | marks_texture = view ? view->marks_focused_inactive : NULL; | ||
683 | } else { | ||
684 | colors = &config->border_colors.unfocused; | ||
685 | title_texture = child->title_unfocused; | ||
686 | marks_texture = view ? view->marks_unfocused : NULL; | ||
687 | } | ||
688 | |||
689 | int y = pstate->swayc_y + container_titlebar_height() * i; | ||
690 | render_titlebar(output, damage, child, cstate->swayc_x, y, | ||
691 | cstate->swayc_width, colors, title_texture, marks_texture); | ||
692 | |||
693 | if (child == current) { | ||
694 | current_colors = colors; | ||
695 | } | ||
696 | } | ||
697 | |||
698 | // Render surface and left/right/bottom borders | ||
699 | if (current) { | ||
700 | if (current->type == C_VIEW) { | ||
701 | render_view(output, damage, current, current_colors); | ||
702 | } else { | ||
703 | render_container(output, damage, current, | ||
704 | parent_focused || current == focus); | ||
705 | } | ||
706 | } | ||
707 | } | ||
708 | |||
709 | static void render_container(struct sway_output *output, | ||
710 | pixman_region32_t *damage, struct sway_container *con, | ||
711 | bool parent_focused) { | ||
712 | switch (con->current.layout) { | ||
713 | case L_NONE: | ||
714 | case L_HORIZ: | ||
715 | case L_VERT: | ||
716 | render_container_simple(output, damage, con, parent_focused); | ||
717 | break; | ||
718 | case L_STACKED: | ||
719 | render_container_stacked(output, damage, con, parent_focused); | ||
720 | break; | ||
721 | case L_TABBED: | ||
722 | render_container_tabbed(output, damage, con, parent_focused); | ||
723 | break; | ||
724 | case L_FLOATING: | ||
725 | sway_assert(false, "Didn't expect to see floating here"); | ||
726 | } | ||
727 | } | ||
728 | |||
729 | static void render_floating_container(struct sway_output *soutput, | ||
730 | pixman_region32_t *damage, struct sway_container *con) { | ||
731 | if (con->type == C_VIEW) { | ||
732 | struct sway_view *view = con->sway_view; | ||
733 | struct sway_seat *seat = input_manager_current_seat(input_manager); | ||
734 | struct sway_container *focus = seat_get_focus(seat); | ||
735 | struct border_colors *colors; | ||
736 | struct wlr_texture *title_texture; | ||
737 | struct wlr_texture *marks_texture; | ||
738 | |||
739 | if (focus == con) { | ||
740 | colors = &config->border_colors.focused; | ||
741 | title_texture = con->title_focused; | ||
742 | marks_texture = view->marks_focused; | ||
743 | } else { | ||
744 | colors = &config->border_colors.unfocused; | ||
745 | title_texture = con->title_unfocused; | ||
746 | marks_texture = view->marks_unfocused; | ||
747 | } | ||
748 | |||
749 | if (con->current.border == B_NORMAL) { | ||
750 | render_titlebar(soutput, damage, con, con->current.swayc_x, | ||
751 | con->current.swayc_y, con->current.swayc_width, colors, | ||
752 | title_texture, marks_texture); | ||
753 | } else if (con->current.border != B_NONE) { | ||
754 | render_top_border(soutput, damage, con, colors); | ||
755 | } | ||
756 | render_view(soutput, damage, con, colors); | ||
757 | } else { | ||
758 | render_container(soutput, damage, con, false); | ||
759 | } | ||
760 | } | ||
761 | |||
762 | static void render_floating(struct sway_output *soutput, | ||
763 | pixman_region32_t *damage) { | ||
764 | for (int i = 0; i < root_container.current.children->length; ++i) { | ||
765 | struct sway_container *output = | ||
766 | root_container.current.children->items[i]; | ||
767 | for (int j = 0; j < output->current.children->length; ++j) { | ||
768 | struct sway_container *ws = output->current.children->items[j]; | ||
769 | if (!workspace_is_visible(ws)) { | ||
770 | continue; | ||
771 | } | ||
772 | list_t *floating = | ||
773 | ws->current.ws_floating->current.children; | ||
774 | for (int k = 0; k < floating->length; ++k) { | ||
775 | struct sway_container *floater = floating->items[k]; | ||
776 | render_floating_container(soutput, damage, floater); | ||
777 | } | ||
778 | } | ||
779 | } | ||
780 | } | ||
781 | |||
782 | void output_render(struct sway_output *output, struct timespec *when, | ||
783 | pixman_region32_t *damage) { | ||
784 | struct wlr_output *wlr_output = output->wlr_output; | ||
785 | |||
786 | struct wlr_renderer *renderer = | ||
787 | wlr_backend_get_renderer(wlr_output->backend); | ||
788 | if (!sway_assert(renderer != NULL, | ||
789 | "expected the output backend to have a renderer")) { | ||
790 | return; | ||
791 | } | ||
792 | |||
793 | wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height); | ||
794 | |||
795 | bool damage_whole_before_swap = false; | ||
796 | if (!pixman_region32_not_empty(damage)) { | ||
797 | // Output isn't damaged but needs buffer swap | ||
798 | goto renderer_end; | ||
799 | } | ||
800 | |||
801 | const char *damage_debug = getenv("SWAY_DAMAGE_DEBUG"); | ||
802 | if (damage_debug != NULL) { | ||
803 | if (strcmp(damage_debug, "highlight") == 0) { | ||
804 | wlr_renderer_clear(renderer, (float[]){1, 1, 0, 1}); | ||
805 | damage_whole_before_swap = true; | ||
806 | } else if (strcmp(damage_debug, "rerender") == 0) { | ||
807 | int width, height; | ||
808 | wlr_output_transformed_resolution(wlr_output, &width, &height); | ||
809 | pixman_region32_union_rect(damage, damage, 0, 0, width, height); | ||
810 | } | ||
811 | } | ||
812 | |||
813 | struct sway_container *workspace = output_get_active_workspace(output); | ||
814 | struct sway_view *fullscreen_view = workspace->current.ws_fullscreen; | ||
815 | struct sway_seat *seat = input_manager_current_seat(input_manager); | ||
816 | |||
817 | if (output_has_opaque_lockscreen(output, seat)) { | ||
818 | struct wlr_layer_surface *wlr_layer_surface = seat->focused_layer; | ||
819 | struct sway_layer_surface *sway_layer_surface = | ||
820 | layer_from_wlr_layer_surface(seat->focused_layer); | ||
821 | struct render_data data = { | ||
822 | .output = output, | ||
823 | .damage = damage, | ||
824 | .alpha = 1.0f, | ||
825 | }; | ||
826 | output_surface_for_each_surface(wlr_layer_surface->surface, | ||
827 | sway_layer_surface->geo.x, sway_layer_surface->geo.y, | ||
828 | &data.root_geo, render_surface_iterator, &data); | ||
829 | } else if (fullscreen_view) { | ||
830 | float clear_color[] = {0.0f, 0.0f, 0.0f, 1.0f}; | ||
831 | |||
832 | int nrects; | ||
833 | pixman_box32_t *rects = pixman_region32_rectangles(damage, &nrects); | ||
834 | for (int i = 0; i < nrects; ++i) { | ||
835 | scissor_output(wlr_output, &rects[i]); | ||
836 | wlr_renderer_clear(renderer, clear_color); | ||
837 | } | ||
838 | |||
839 | // TODO: handle views smaller than the output | ||
840 | render_view_surfaces(fullscreen_view, output, damage, 1.0f); | ||
841 | |||
842 | if (fullscreen_view->type == SWAY_VIEW_XWAYLAND) { | ||
843 | render_unmanaged(output, damage, | ||
844 | &root_container.sway_root->xwayland_unmanaged); | ||
845 | } | ||
846 | } else { | ||
847 | float clear_color[] = {0.25f, 0.25f, 0.25f, 1.0f}; | ||
848 | |||
849 | int nrects; | ||
850 | pixman_box32_t *rects = pixman_region32_rectangles(damage, &nrects); | ||
851 | for (int i = 0; i < nrects; ++i) { | ||
852 | scissor_output(wlr_output, &rects[i]); | ||
853 | wlr_renderer_clear(renderer, clear_color); | ||
854 | } | ||
855 | |||
856 | render_layer(output, damage, | ||
857 | &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BACKGROUND]); | ||
858 | render_layer(output, damage, | ||
859 | &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_BOTTOM]); | ||
860 | |||
861 | struct sway_seat *seat = input_manager_current_seat(input_manager); | ||
862 | struct sway_container *focus = seat_get_focus(seat); | ||
863 | render_container(output, damage, workspace, focus == workspace); | ||
864 | render_floating(output, damage); | ||
865 | |||
866 | render_unmanaged(output, damage, | ||
867 | &root_container.sway_root->xwayland_unmanaged); | ||
868 | render_layer(output, damage, | ||
869 | &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_TOP]); | ||
870 | } | ||
871 | render_layer(output, damage, | ||
872 | &output->layers[ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY]); | ||
873 | render_drag_icons(output, damage, &root_container.sway_root->drag_icons); | ||
874 | |||
875 | renderer_end: | ||
876 | if (root_container.sway_root->debug_tree) { | ||
877 | wlr_render_texture(renderer, root_container.sway_root->debug_tree, | ||
878 | wlr_output->transform_matrix, 0, 0, 1); | ||
879 | } | ||
880 | |||
881 | if (damage_whole_before_swap || root_container.sway_root->debug_tree) { | ||
882 | int width, height; | ||
883 | wlr_output_transformed_resolution(wlr_output, &width, &height); | ||
884 | pixman_region32_union_rect(damage, damage, 0, 0, width, height); | ||
885 | } | ||
886 | |||
887 | wlr_renderer_scissor(renderer, NULL); | ||
888 | wlr_renderer_end(renderer); | ||
889 | if (!wlr_output_damage_swap_buffers(output->damage, when, damage)) { | ||
890 | return; | ||
891 | } | ||
892 | output->last_frame = *when; | ||
893 | } | ||