-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwlr_scene.c
2742 lines (2288 loc) · 84.3 KB
/
wlr_scene.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <assert.h>
#include <pixman.h>
#include <stdio.h>
#include <stdlib.h>
#include <scenefx/types/wlr_scene.h>
#include <string.h>
#include <wlr/backend.h>
#include <wlr/render/gles2.h>
#include <wlr/types/wlr_compositor.h>
#include <wlr/types/wlr_damage_ring.h>
#include <wlr/types/wlr_presentation_time.h>
#include <wlr/types/wlr_subcompositor.h>
#include <wlr/types/wlr_xdg_shell.h>
#include <wlr/util/log.h>
#include <wlr/util/region.h>
#include <wlr/util/transform.h>
#include <wlr/render/swapchain.h>
#include "scenefx/render/fx_renderer/fx_effect_framebuffers.h"
#include "scenefx/render/fx_renderer/fx_renderer.h"
#include "scenefx/render/pass.h"
#include "scenefx/types/fx/blur_data.h"
#include "scenefx/types/fx/clipped_region.h"
#include "scenefx/types/fx/corner_location.h"
#include "types/wlr_buffer.h"
#include "types/wlr_output.h"
#include "types/wlr_scene.h"
#include "util/array.h"
#include "util/env.h"
#include "util/time.h"
#include "wlr/util/box.h"
#define HIGHLIGHT_DAMAGE_FADEOUT_TIME 250
struct wlr_scene_tree *wlr_scene_tree_from_node(struct wlr_scene_node *node) {
assert(node->type == WLR_SCENE_NODE_TREE);
struct wlr_scene_tree *tree = wl_container_of(node, tree, node);
return tree;
}
struct wlr_scene_rect *wlr_scene_rect_from_node(struct wlr_scene_node *node) {
assert(node->type == WLR_SCENE_NODE_RECT);
struct wlr_scene_rect *rect = wl_container_of(node, rect, node);
return rect;
}
struct wlr_scene_optimized_blur *wlr_scene_optimized_blur_from_node(
struct wlr_scene_node *node) {
assert(node->type == WLR_SCENE_NODE_OPTIMIZED_BLUR);
struct wlr_scene_optimized_blur *blur_node =
wl_container_of(node, blur_node, node);
return blur_node;
}
struct wlr_scene_buffer *wlr_scene_buffer_from_node(
struct wlr_scene_node *node) {
assert(node->type == WLR_SCENE_NODE_BUFFER);
struct wlr_scene_buffer *buffer = wl_container_of(node, buffer, node);
return buffer;
}
struct wlr_scene_shadow *wlr_scene_shadow_from_node(struct wlr_scene_node *node) {
assert(node->type == WLR_SCENE_NODE_SHADOW);
struct wlr_scene_shadow *shadow = wl_container_of(node, shadow, node);
return shadow;
}
struct wlr_scene *scene_node_get_root(struct wlr_scene_node *node) {
struct wlr_scene_tree *tree;
if (node->type == WLR_SCENE_NODE_TREE) {
tree = wlr_scene_tree_from_node(node);
} else {
tree = node->parent;
}
while (tree->node.parent != NULL) {
tree = tree->node.parent;
}
struct wlr_scene *scene = wl_container_of(tree, scene, tree);
return scene;
}
static void scene_node_init(struct wlr_scene_node *node,
enum wlr_scene_node_type type, struct wlr_scene_tree *parent) {
*node = (struct wlr_scene_node){
.type = type,
.parent = parent,
.enabled = true,
};
wl_list_init(&node->link);
wl_signal_init(&node->events.destroy);
pixman_region32_init(&node->visible);
if (parent != NULL) {
wl_list_insert(parent->children.prev, &node->link);
}
wlr_addon_set_init(&node->addons);
}
struct highlight_region {
pixman_region32_t region;
struct timespec when;
struct wl_list link;
};
static void scene_buffer_set_buffer(struct wlr_scene_buffer *scene_buffer,
struct wlr_buffer *buffer);
static void scene_buffer_set_texture(struct wlr_scene_buffer *scene_buffer,
struct wlr_texture *texture);
void wlr_scene_node_destroy(struct wlr_scene_node *node) {
if (node == NULL) {
return;
}
// We want to call the destroy listeners before we do anything else
// in case the destroy signal would like to remove children before they
// are recursively destroyed.
wl_signal_emit_mutable(&node->events.destroy, NULL);
wlr_addon_set_finish(&node->addons);
wlr_scene_node_set_enabled(node, false);
struct wlr_scene *scene = scene_node_get_root(node);
if (node->type == WLR_SCENE_NODE_BUFFER) {
struct wlr_scene_buffer *scene_buffer = wlr_scene_buffer_from_node(node);
uint64_t active = scene_buffer->active_outputs;
if (active) {
struct wlr_scene_output *scene_output;
wl_list_for_each(scene_output, &scene->outputs, link) {
if (active & (1ull << scene_output->index)) {
wl_signal_emit_mutable(&scene_buffer->events.output_leave,
scene_output);
}
}
}
scene_buffer_set_buffer(scene_buffer, NULL);
scene_buffer_set_texture(scene_buffer, NULL);
pixman_region32_fini(&scene_buffer->opaque_region);
} else if (node->type == WLR_SCENE_NODE_TREE) {
struct wlr_scene_tree *scene_tree = wlr_scene_tree_from_node(node);
if (scene_tree == &scene->tree) {
assert(!node->parent);
struct wlr_scene_output *scene_output, *scene_output_tmp;
wl_list_for_each_safe(scene_output, scene_output_tmp, &scene->outputs, link) {
wlr_scene_output_destroy(scene_output);
}
wl_list_remove(&scene->linux_dmabuf_v1_destroy.link);
} else {
assert(node->parent);
}
struct wlr_scene_node *child, *child_tmp;
wl_list_for_each_safe(child, child_tmp,
&scene_tree->children, link) {
wlr_scene_node_destroy(child);
}
}
wl_list_remove(&node->link);
pixman_region32_fini(&node->visible);
free(node);
}
static void scene_tree_init(struct wlr_scene_tree *tree,
struct wlr_scene_tree *parent) {
*tree = (struct wlr_scene_tree){0};
scene_node_init(&tree->node, WLR_SCENE_NODE_TREE, parent);
wl_list_init(&tree->children);
}
struct wlr_scene *wlr_scene_create(void) {
struct wlr_scene *scene = calloc(1, sizeof(*scene));
if (scene == NULL) {
return NULL;
}
scene_tree_init(&scene->tree, NULL);
wl_list_init(&scene->outputs);
wl_list_init(&scene->linux_dmabuf_v1_destroy.link);
const char *debug_damage_options[] = {
"none",
"rerender",
"highlight",
NULL
};
scene->debug_damage_option = env_parse_switch("WLR_SCENE_DEBUG_DAMAGE", debug_damage_options);
scene->direct_scanout = !env_parse_bool("WLR_SCENE_DISABLE_DIRECT_SCANOUT");
scene->calculate_visibility = !env_parse_bool("WLR_SCENE_DISABLE_VISIBILITY");
scene->highlight_transparent_region = env_parse_bool("WLR_SCENE_HIGHLIGHT_TRANSPARENT_REGION");
scene->blur_data = blur_data_get_default();
return scene;
}
struct wlr_scene_tree *wlr_scene_tree_create(struct wlr_scene_tree *parent) {
assert(parent);
struct wlr_scene_tree *tree = calloc(1, sizeof(*tree));
if (tree == NULL) {
return NULL;
}
scene_tree_init(tree, parent);
return tree;
}
static void scene_node_get_size(struct wlr_scene_node *node, int *lx, int *ly);
typedef bool (*scene_node_box_iterator_func_t)(struct wlr_scene_node *node,
int sx, int sy, void *data);
static bool _scene_nodes_in_box(struct wlr_scene_node *node, struct wlr_box *box,
scene_node_box_iterator_func_t iterator, void *user_data, int lx, int ly) {
if (!node->enabled) {
return false;
}
switch (node->type) {
case WLR_SCENE_NODE_TREE:;
struct wlr_scene_tree *scene_tree = wlr_scene_tree_from_node(node);
struct wlr_scene_node *child;
wl_list_for_each_reverse(child, &scene_tree->children, link) {
if (_scene_nodes_in_box(child, box, iterator, user_data, lx + child->x, ly + child->y)) {
return true;
}
}
break;
case WLR_SCENE_NODE_OPTIMIZED_BLUR:;
case WLR_SCENE_NODE_RECT:
case WLR_SCENE_NODE_SHADOW:
case WLR_SCENE_NODE_BUFFER:;
struct wlr_box node_box = { .x = lx, .y = ly };
scene_node_get_size(node, &node_box.width, &node_box.height);
if (wlr_box_intersection(&node_box, &node_box, box) &&
iterator(node, lx, ly, user_data)) {
return true;
}
break;
}
return false;
}
static bool scene_nodes_in_box(struct wlr_scene_node *node, struct wlr_box *box,
scene_node_box_iterator_func_t iterator, void *user_data) {
int x, y;
wlr_scene_node_coords(node, &x, &y);
return _scene_nodes_in_box(node, box, iterator, user_data, x, y);
}
static void scene_node_opaque_region(struct wlr_scene_node *node, int x, int y,
pixman_region32_t *opaque) {
int width, height;
scene_node_get_size(node, &width, &height);
if (node->type == WLR_SCENE_NODE_RECT) {
struct wlr_scene_rect *scene_rect = wlr_scene_rect_from_node(node);
if (scene_rect->corner_radius > 0) {
// TODO: this is incorrect
return;
}
if (scene_rect->color[3] != 1) {
return;
}
} else if (node->type == WLR_SCENE_NODE_SHADOW) {
// TODO: test & handle case of blur sigma = 0 and color[3] = 1?
return;
} else if (node->type == WLR_SCENE_NODE_BUFFER) {
struct wlr_scene_buffer *scene_buffer = wlr_scene_buffer_from_node(node);
if (!scene_buffer->buffer) {
return;
}
if (scene_buffer->opacity != 1) {
return;
}
if (scene_buffer->corner_radius > 0) {
// TODO: this is incorrect
return;
}
if (!scene_buffer->buffer_is_opaque) {
pixman_region32_copy(opaque, &scene_buffer->opaque_region);
pixman_region32_intersect_rect(opaque, opaque, 0, 0, width, height);
pixman_region32_translate(opaque, x, y);
return;
}
} else if (node->type == WLR_SCENE_NODE_OPTIMIZED_BLUR) {
// Always transparent
return;
}
pixman_region32_fini(opaque);
pixman_region32_init_rect(opaque, x, y, width, height);
}
struct scene_update_data {
pixman_region32_t *visible;
pixman_region32_t *update_region;
struct wl_list *outputs;
bool calculate_visibility;
};
static uint32_t region_area(pixman_region32_t *region) {
uint32_t area = 0;
int nrects;
pixman_box32_t *rects = pixman_region32_rectangles(region, &nrects);
for (int i = 0; i < nrects; ++i) {
area += (rects[i].x2 - rects[i].x1) * (rects[i].y2 - rects[i].y1);
}
return area;
}
static void scale_output_damage(pixman_region32_t *damage, float scale) {
wlr_region_scale(damage, damage, scale);
if (floor(scale) != scale) {
wlr_region_expand(damage, damage, 1);
}
}
struct blur_info {
bool has_blur;
bool has_optimized;
};
struct render_data {
enum wl_output_transform transform;
float scale;
struct wlr_box logical;
int trans_width, trans_height;
struct wlr_scene_output *output;
struct fx_gles_render_pass *render_pass;
pixman_region32_t damage;
struct blur_info blur_info;
};
static void transform_output_damage(pixman_region32_t *damage, const struct render_data *data) {
enum wl_output_transform transform = wlr_output_transform_invert(data->transform);
wlr_region_transform(damage, damage, transform, data->trans_width, data->trans_height);
}
static void scene_output_damage(struct wlr_scene_output *scene_output,
const pixman_region32_t *region) {
if (wlr_damage_ring_add(&scene_output->damage_ring, region)) {
wlr_output_schedule_frame(scene_output->output);
struct wlr_output *output = scene_output->output;
enum wl_output_transform transform =
wlr_output_transform_invert(scene_output->output->transform);
int width = output->width;
int height = output->height;
if (transform & WL_OUTPUT_TRANSFORM_90) {
width = output->height;
height = output->width;
}
pixman_region32_t frame_damage;
pixman_region32_init(&frame_damage);
wlr_region_transform(&frame_damage, region, transform, width, height);
pixman_region32_union(&scene_output->pending_commit_damage,
&scene_output->pending_commit_damage, &frame_damage);
pixman_region32_intersect_rect(&scene_output->pending_commit_damage,
&scene_output->pending_commit_damage, 0, 0, output->width, output->height);
pixman_region32_fini(&frame_damage);
}
}
static void scene_output_damage_whole(struct wlr_scene_output *scene_output) {
struct wlr_damage_ring *ring = &scene_output->damage_ring;
pixman_region32_t damage;
pixman_region32_init_rect(&damage, 0, 0, ring->width, ring->height);
scene_output_damage(scene_output, &damage);
pixman_region32_fini(&damage);
}
static void transform_output_box(struct wlr_box *box, const struct render_data *data) {
enum wl_output_transform transform = wlr_output_transform_invert(data->transform);
wlr_box_transform(box, box, transform, data->trans_width, data->trans_height);
}
static void scene_damage_outputs(struct wlr_scene *scene, pixman_region32_t *damage) {
if (!pixman_region32_not_empty(damage)) {
return;
}
struct wlr_scene_output *scene_output;
wl_list_for_each(scene_output, &scene->outputs, link) {
pixman_region32_t output_damage;
pixman_region32_init(&output_damage);
pixman_region32_copy(&output_damage, damage);
pixman_region32_translate(&output_damage,
-scene_output->x, -scene_output->y);
scale_output_damage(&output_damage, scene_output->output->scale);
scene_output_damage(scene_output, &output_damage);
pixman_region32_fini(&output_damage);
}
}
static void update_node_update_outputs(struct wlr_scene_node *node,
struct wl_list *outputs, struct wlr_scene_output *ignore,
struct wlr_scene_output *force) {
if (node->type != WLR_SCENE_NODE_BUFFER) {
return;
}
struct wlr_scene_buffer *scene_buffer = wlr_scene_buffer_from_node(node);
uint32_t largest_overlap = 0;
struct wlr_scene_output *old_primary_output = scene_buffer->primary_output;
scene_buffer->primary_output = NULL;
size_t count = 0;
uint64_t active_outputs = 0;
// let's update the outputs in two steps:
// - the primary outputs
// - the enter/leave signals
// This ensures that the enter/leave signals can rely on the primary output
// to have a reasonable value. Otherwise, they may get a value that's in
// the middle of a calculation.
struct wlr_scene_output *scene_output;
wl_list_for_each(scene_output, outputs, link) {
if (scene_output == ignore) {
continue;
}
if (!scene_output->output->enabled) {
continue;
}
struct wlr_box output_box = {
.x = scene_output->x,
.y = scene_output->y,
};
wlr_output_effective_resolution(scene_output->output,
&output_box.width, &output_box.height);
pixman_region32_t intersection;
pixman_region32_init(&intersection);
pixman_region32_intersect_rect(&intersection, &node->visible,
output_box.x, output_box.y, output_box.width, output_box.height);
if (pixman_region32_not_empty(&intersection)) {
uint32_t overlap = region_area(&intersection);
if (overlap >= largest_overlap) {
largest_overlap = overlap;
scene_buffer->primary_output = scene_output;
}
active_outputs |= 1ull << scene_output->index;
count++;
}
pixman_region32_fini(&intersection);
}
if (old_primary_output != scene_buffer->primary_output) {
scene_buffer->prev_feedback_options =
(struct wlr_linux_dmabuf_feedback_v1_init_options){0};
}
uint64_t old_active = scene_buffer->active_outputs;
scene_buffer->active_outputs = active_outputs;
wl_list_for_each(scene_output, outputs, link) {
uint64_t mask = 1ull << scene_output->index;
bool intersects = active_outputs & mask;
bool intersects_before = old_active & mask;
if (intersects && !intersects_before) {
wl_signal_emit_mutable(&scene_buffer->events.output_enter, scene_output);
} else if (!intersects && intersects_before) {
wl_signal_emit_mutable(&scene_buffer->events.output_leave, scene_output);
}
}
// if there are active outputs on this node, we should always have a primary
// output
assert(!scene_buffer->active_outputs || scene_buffer->primary_output);
// Skip output update event if nothing was updated
if (old_active == active_outputs &&
(!force || ((1ull << force->index) & ~active_outputs)) &&
old_primary_output == scene_buffer->primary_output) {
return;
}
struct wlr_scene_output *outputs_array[64];
struct wlr_scene_outputs_update_event event = {
.active = outputs_array,
.size = count,
};
size_t i = 0;
wl_list_for_each(scene_output, outputs, link) {
if (~active_outputs & (1ull << scene_output->index)) {
continue;
}
assert(i < count);
outputs_array[i++] = scene_output;
}
wl_signal_emit_mutable(&scene_buffer->events.outputs_update, &event);
}
static bool scene_node_update_iterator(struct wlr_scene_node *node,
int lx, int ly, void *_data) {
struct scene_update_data *data = _data;
struct wlr_box box = { .x = lx, .y = ly };
scene_node_get_size(node, &box.width, &box.height);
pixman_region32_subtract(&node->visible, &node->visible, data->update_region);
pixman_region32_union(&node->visible, &node->visible, data->visible);
pixman_region32_intersect_rect(&node->visible, &node->visible,
lx, ly, box.width, box.height);
if (data->calculate_visibility) {
pixman_region32_t opaque;
pixman_region32_init(&opaque);
scene_node_opaque_region(node, lx, ly, &opaque);
pixman_region32_subtract(data->visible, data->visible, &opaque);
pixman_region32_fini(&opaque);
}
update_node_update_outputs(node, data->outputs, NULL, NULL);
return false;
}
static void scene_node_visibility(struct wlr_scene_node *node,
pixman_region32_t *visible) {
if (!node->enabled) {
return;
}
if (node->type == WLR_SCENE_NODE_TREE) {
struct wlr_scene_tree *scene_tree = wlr_scene_tree_from_node(node);
struct wlr_scene_node *child;
wl_list_for_each(child, &scene_tree->children, link) {
scene_node_visibility(child, visible);
}
return;
}
pixman_region32_union(visible, visible, &node->visible);
}
static void scene_node_bounds(struct wlr_scene_node *node,
int x, int y, pixman_region32_t *visible) {
if (!node->enabled) {
return;
}
if (node->type == WLR_SCENE_NODE_TREE) {
struct wlr_scene_tree *scene_tree = wlr_scene_tree_from_node(node);
struct wlr_scene_node *child;
wl_list_for_each(child, &scene_tree->children, link) {
scene_node_bounds(child, x + child->x, y + child->y, visible);
}
return;
}
int width, height;
scene_node_get_size(node, &width, &height);
pixman_region32_union_rect(visible, visible, x, y, width, height);
}
static void scene_update_region(struct wlr_scene *scene,
pixman_region32_t *update_region) {
pixman_region32_t visible;
pixman_region32_init(&visible);
pixman_region32_copy(&visible, update_region);
struct scene_update_data data = {
.visible = &visible,
.update_region = update_region,
.outputs = &scene->outputs,
.calculate_visibility = scene->calculate_visibility,
};
struct pixman_box32 *region_box = pixman_region32_extents(update_region);
struct wlr_box box = {
.x = region_box->x1,
.y = region_box->y1,
.width = region_box->x2 - region_box->x1,
.height = region_box->y2 - region_box->y1,
};
// update node visibility and output enter/leave events
scene_nodes_in_box(&scene->tree.node, &box, scene_node_update_iterator, &data);
pixman_region32_fini(&visible);
}
static void scene_node_update(struct wlr_scene_node *node,
pixman_region32_t *damage) {
struct wlr_scene *scene = scene_node_get_root(node);
int x, y;
if (!wlr_scene_node_coords(node, &x, &y)) {
if (damage) {
scene_update_region(scene, damage);
scene_damage_outputs(scene, damage);
pixman_region32_fini(damage);
}
return;
}
pixman_region32_t visible;
if (!damage) {
pixman_region32_init(&visible);
scene_node_visibility(node, &visible);
damage = &visible;
}
pixman_region32_t update_region;
pixman_region32_init(&update_region);
pixman_region32_copy(&update_region, damage);
scene_node_bounds(node, x, y, &update_region);
scene_update_region(scene, &update_region);
pixman_region32_fini(&update_region);
scene_node_visibility(node, damage);
scene_damage_outputs(scene, damage);
pixman_region32_fini(damage);
}
struct wlr_scene_rect *wlr_scene_rect_create(struct wlr_scene_tree *parent,
int width, int height, const float color[static 4]) {
struct wlr_scene_rect *scene_rect = calloc(1, sizeof(*scene_rect));
if (scene_rect == NULL) {
return NULL;
}
assert(parent);
scene_node_init(&scene_rect->node, WLR_SCENE_NODE_RECT, parent);
scene_rect->width = width;
scene_rect->height = height;
memcpy(scene_rect->color, color, sizeof(scene_rect->color));
scene_rect->corner_radius = 0;
scene_rect->corners = CORNER_LOCATION_NONE;
scene_rect->accepts_input = true;
scene_rect->clipped_region = clipped_region_get_default();
scene_rect->backdrop_blur = false;
scene_rect->backdrop_blur_optimized = false;
scene_node_update(&scene_rect->node, NULL);
return scene_rect;
}
void wlr_scene_rect_set_size(struct wlr_scene_rect *rect, int width, int height) {
if (rect->width == width && rect->height == height) {
return;
}
rect->width = width;
rect->height = height;
scene_node_update(&rect->node, NULL);
}
void wlr_scene_rect_set_color(struct wlr_scene_rect *rect, const float color[static 4]) {
if (memcmp(rect->color, color, sizeof(rect->color)) == 0) {
return;
}
memcpy(rect->color, color, sizeof(rect->color));
scene_node_update(&rect->node, NULL);
}
void wlr_scene_rect_set_backdrop_blur(struct wlr_scene_rect *rect,
bool enabled) {
if (rect->backdrop_blur == enabled) {
return;
}
rect->backdrop_blur = enabled;
scene_node_update(&rect->node, NULL);
}
void wlr_scene_rect_set_backdrop_blur_optimized(struct wlr_scene_rect *rect,
bool enabled) {
if (rect->backdrop_blur_optimized == enabled) {
return;
}
rect->backdrop_blur_optimized = enabled;
scene_node_update(&rect->node, NULL);
}
static void scene_buffer_handle_buffer_release(struct wl_listener *listener,
void *data) {
struct wlr_scene_buffer *scene_buffer =
wl_container_of(listener, scene_buffer, buffer_release);
scene_buffer->buffer = NULL;
wl_list_remove(&scene_buffer->buffer_release.link);
wl_list_init(&scene_buffer->buffer_release.link);
}
static void scene_buffer_set_buffer(struct wlr_scene_buffer *scene_buffer,
struct wlr_buffer *buffer) {
wl_list_remove(&scene_buffer->buffer_release.link);
wl_list_init(&scene_buffer->buffer_release.link);
if (scene_buffer->own_buffer) {
wlr_buffer_unlock(scene_buffer->buffer);
}
scene_buffer->buffer = NULL;
scene_buffer->own_buffer = false;
scene_buffer->buffer_width = scene_buffer->buffer_height = 0;
scene_buffer->buffer_is_opaque = false;
if (!buffer) {
return;
}
scene_buffer->own_buffer = true;
scene_buffer->buffer = wlr_buffer_lock(buffer);
scene_buffer->buffer_width = buffer->width;
scene_buffer->buffer_height = buffer->height;
scene_buffer->buffer_is_opaque = buffer_is_opaque(buffer);
scene_buffer->buffer_release.notify = scene_buffer_handle_buffer_release;
wl_signal_add(&buffer->events.release, &scene_buffer->buffer_release);
}
static void scene_buffer_handle_renderer_destroy(struct wl_listener *listener,
void *data) {
struct wlr_scene_buffer *scene_buffer = wl_container_of(listener, scene_buffer, renderer_destroy);
scene_buffer_set_texture(scene_buffer, NULL);
}
static void scene_buffer_set_texture(struct wlr_scene_buffer *scene_buffer,
struct wlr_texture *texture) {
wl_list_remove(&scene_buffer->renderer_destroy.link);
wlr_texture_destroy(scene_buffer->texture);
scene_buffer->texture = texture;
if (texture != NULL) {
scene_buffer->renderer_destroy.notify = scene_buffer_handle_renderer_destroy;
wl_signal_add(&texture->renderer->events.destroy, &scene_buffer->renderer_destroy);
} else {
wl_list_init(&scene_buffer->renderer_destroy.link);
}
}
void wlr_scene_rect_set_corner_radius(struct wlr_scene_rect *rect, int corner_radius,
enum corner_location corners) {
if (rect->corner_radius == corner_radius && rect->corners == corners) {
return;
}
rect->corner_radius = corner_radius;
rect->corners = corners;
scene_node_update(&rect->node, NULL);
}
void wlr_scene_rect_set_clipped_region(struct wlr_scene_rect *rect,
struct clipped_region clipped_region) {
if (rect->clipped_region.corner_radius == clipped_region.corner_radius &&
rect->clipped_region.corners == clipped_region.corners &&
wlr_box_equal(&rect->clipped_region.area, &clipped_region.area)) {
return;
}
if (clipped_region.corner_radius && clipped_region.corners == CORNER_LOCATION_NONE) {
wlr_log(WLR_ERROR, "Applying corner radius without specifying which"
" corners to round for rect: %p", rect);
}
rect->clipped_region = clipped_region;
scene_node_update(&rect->node, NULL);
}
struct wlr_scene_shadow *wlr_scene_shadow_create(struct wlr_scene_tree *parent,
int width, int height, int corner_radius, float blur_sigma,
const float color [static 4]) {
struct wlr_scene_shadow *scene_shadow = calloc(1, sizeof(*scene_shadow));
if (scene_shadow == NULL) {
return NULL;
}
assert(parent);
scene_node_init(&scene_shadow->node, WLR_SCENE_NODE_SHADOW, parent);
scene_shadow->width = width;
scene_shadow->height = height;
scene_shadow->corner_radius = corner_radius;
scene_shadow->blur_sigma = blur_sigma;
memcpy(scene_shadow->color, color, sizeof(scene_shadow->color));
scene_shadow->clipped_region = clipped_region_get_default();
scene_node_update(&scene_shadow->node, NULL);
return scene_shadow;
}
void wlr_scene_shadow_set_size(struct wlr_scene_shadow *shadow, int width, int height) {
if (shadow->width == width && shadow->height == height) {
return;
}
shadow->width = width;
shadow->height = height;
scene_node_update(&shadow->node, NULL);
}
void wlr_scene_shadow_set_corner_radius(struct wlr_scene_shadow *shadow, int corner_radius) {
if (shadow->corner_radius == corner_radius) {
return;
}
shadow->corner_radius = corner_radius;
scene_node_update(&shadow->node, NULL);
}
void wlr_scene_shadow_set_blur_sigma(struct wlr_scene_shadow *shadow, float blur_sigma) {
if (shadow->blur_sigma == blur_sigma) {
return;
}
shadow->blur_sigma = blur_sigma;
scene_node_update(&shadow->node, NULL);
}
void wlr_scene_shadow_set_color(struct wlr_scene_shadow *shadow, const float color[static 4]) {
if (memcmp(shadow->color, color, sizeof(shadow->color)) == 0) {
return;
}
memcpy(shadow->color, color, sizeof(shadow->color));
scene_node_update(&shadow->node, NULL);
}
void wlr_scene_shadow_set_clipped_region(struct wlr_scene_shadow *shadow,
struct clipped_region clipped_region) {
if (shadow->clipped_region.corner_radius == clipped_region.corner_radius &&
shadow->clipped_region.corners == clipped_region.corners &&
wlr_box_equal(&shadow->clipped_region.area, &clipped_region.area)) {
return;
}
if (clipped_region.corner_radius && clipped_region.corners == CORNER_LOCATION_NONE) {
wlr_log(WLR_ERROR, "Applying corner radius without specifying which"
" corners to round for shadow: %p", shadow);
}
shadow->clipped_region = clipped_region;
scene_node_update(&shadow->node, NULL);
}
void wlr_scene_set_blur_data(struct wlr_scene *scene, struct blur_data blur_data) {
struct blur_data *buff_data = &scene->blur_data;
if (blur_data_cmp(buff_data, &blur_data)) {
return;
}
memcpy(&scene->blur_data, &blur_data,
sizeof(struct blur_data));
wlr_scene_optimized_blur_mark_dirty(scene, NULL, NULL);
}
struct wlr_scene_optimized_blur *wlr_scene_optimized_blur_create(
struct wlr_scene_tree *parent, int width, int height) {
struct wlr_scene_optimized_blur *scene_blur = calloc(1, sizeof(*scene_blur));
if (scene_blur == NULL) {
return NULL;
}
assert(parent);
scene_node_init(&scene_blur->node, WLR_SCENE_NODE_OPTIMIZED_BLUR, parent);
scene_blur->width = width;
scene_blur->height = height;
scene_node_update(&scene_blur->node, NULL);
return scene_blur;
}
void wlr_scene_optimized_blur_set_size(struct wlr_scene_optimized_blur *blur_node,
int width, int height) {
assert(blur_node);
if (blur_node->width == width && blur_node->height == height) {
return;
}
blur_node->width = width;
blur_node->height = height;
wlr_scene_optimized_blur_mark_dirty(scene_node_get_root(&blur_node->node), blur_node, NULL);
}
struct wlr_scene_buffer *wlr_scene_buffer_create(struct wlr_scene_tree *parent,
struct wlr_buffer *buffer) {
struct wlr_scene_buffer *scene_buffer = calloc(1, sizeof(*scene_buffer));
if (scene_buffer == NULL) {
return NULL;
}
assert(parent);
scene_node_init(&scene_buffer->node, WLR_SCENE_NODE_BUFFER, parent);
wl_signal_init(&scene_buffer->events.outputs_update);
wl_signal_init(&scene_buffer->events.output_enter);
wl_signal_init(&scene_buffer->events.output_leave);
wl_signal_init(&scene_buffer->events.output_sample);
wl_signal_init(&scene_buffer->events.frame_done);
pixman_region32_init(&scene_buffer->opaque_region);
wl_list_init(&scene_buffer->buffer_release.link);
wl_list_init(&scene_buffer->renderer_destroy.link);
scene_buffer->opacity = 1;
scene_buffer->corner_radius = 0;
scene_buffer->backdrop_blur = false;
scene_buffer->backdrop_blur_optimized = false;
scene_buffer->backdrop_blur_ignore_transparent = true;
scene_buffer->corners = CORNER_LOCATION_NONE;
scene_buffer_set_buffer(scene_buffer, buffer);
scene_node_update(&scene_buffer->node, NULL);
return scene_buffer;
}
void wlr_scene_buffer_set_buffer_with_damage(struct wlr_scene_buffer *scene_buffer,
struct wlr_buffer *buffer, const pixman_region32_t *damage) {
// specifying a region for a NULL buffer doesn't make sense. We need to know
// about the buffer to scale the buffer local coordinates down to scene
// coordinates.
assert(buffer || !damage);
bool mapped = buffer != NULL;
bool prev_mapped = scene_buffer->buffer != NULL || scene_buffer->texture != NULL;
if (!mapped && !prev_mapped) {
// unmapping already unmapped buffer - noop
return;
}
// if this node used to not be mapped or its previous displayed
// buffer region will be different from what the new buffer would
// produce we need to update the node.
bool update = mapped != prev_mapped;
if (buffer != NULL && scene_buffer->dst_width == 0 && scene_buffer->dst_height == 0) {
update = update || scene_buffer->buffer_width != buffer->width ||
scene_buffer->buffer_height != buffer->height;
}
scene_buffer_set_buffer(scene_buffer, buffer);
scene_buffer_set_texture(scene_buffer, NULL);
if (update) {
scene_node_update(&scene_buffer->node, NULL);
// updating the node will already damage the whole node for us. Return
// early to not damage again
return;
}
int lx, ly;
if (!wlr_scene_node_coords(&scene_buffer->node, &lx, &ly)) {
return;
}
pixman_region32_t fallback_damage;
pixman_region32_init_rect(&fallback_damage, 0, 0, buffer->width, buffer->height);
if (!damage) {
damage = &fallback_damage;
}
struct wlr_fbox box = scene_buffer->src_box;
if (wlr_fbox_empty(&box)) {
box.x = 0;
box.y = 0;
box.width = buffer->width;
box.height = buffer->height;
}