1 /*
2  * This an unstable interface of wlroots. No guarantees are made regarding the
3  * future consistency of this API.
4  */
5 #ifndef WLR_USE_UNSTABLE
6 #error "Add -DWLR_USE_UNSTABLE to enable unstable wlroots features"
7 #endif
8 
9 #ifndef WLR_TYPES_WLR_OUTPUT_DAMAGE_H
10 #define WLR_TYPES_WLR_OUTPUT_DAMAGE_H
11 
12 #include <pixman.h>
13 #include <time.h>
14 #include <wlr/types/wlr_box.h>
15 #include <wlr/types/wlr_output.h>
16 
17 /**
18  * Damage tracking requires to keep track of previous frames' damage. To allow
19  * damage tracking to work with triple buffering, a history of two frames is
20  * required.
21  */
22 #define WLR_OUTPUT_DAMAGE_PREVIOUS_LEN 2
23 
24 /**
25  * Tracks damage for an output.
26  *
27  * The `frame` event will be emitted when it is a good time for the compositor
28  * to submit a new frame.
29  *
30  * To render a new frame, compositors should call
31  * `wlr_output_damage_attach_render`, render and call `wlr_output_commit`. No
32  * rendering should happen outside a `frame` event handler or before
33  * `wlr_output_damage_attach_render`.
34  */
35 struct wlr_output_damage {
36 	struct wlr_output *output;
37 	int max_rects; // max number of damaged rectangles
38 
39 	pixman_region32_t current; // in output-local coordinates
40 
41 	// circular queue for previous damage
42 	pixman_region32_t previous[WLR_OUTPUT_DAMAGE_PREVIOUS_LEN];
43 	size_t previous_idx;
44 
45 	struct {
46 		struct wl_signal frame;
47 		struct wl_signal destroy;
48 	} events;
49 
50 	struct wl_listener output_destroy;
51 	struct wl_listener output_mode;
52 	struct wl_listener output_transform;
53 	struct wl_listener output_scale;
54 	struct wl_listener output_needs_frame;
55 	struct wl_listener output_damage;
56 	struct wl_listener output_frame;
57 	struct wl_listener output_commit;
58 };
59 
60 struct wlr_output_damage *wlr_output_damage_create(struct wlr_output *output);
61 void wlr_output_damage_destroy(struct wlr_output_damage *output_damage);
62 /**
63  * Attach the renderer's buffer to the output. Compositors must call this
64  * function before rendering. After they are done rendering, they should call
65  * `wlr_output_set_damage` and `wlr_output_commit` to submit the new frame.
66  *
67  * `needs_frame` will be set to true if a frame should be submitted. `damage`
68  * will be set to the region of the output that needs to be repainted, in
69  * output-buffer-local coordinates.
70  *
71  * The buffer damage region accumulates all damage since the buffer has last
72  * been swapped. This is not to be confused with the output surface damage,
73  * which only contains the changes between two frames.
74  */
75 bool wlr_output_damage_attach_render(struct wlr_output_damage *output_damage,
76 	bool *needs_frame, pixman_region32_t *buffer_damage);
77 /**
78  * Accumulates damage and schedules a `frame` event.
79  */
80 void wlr_output_damage_add(struct wlr_output_damage *output_damage,
81 	pixman_region32_t *damage);
82 /**
83  * Damages the whole output and schedules a `frame` event.
84  */
85 void wlr_output_damage_add_whole(struct wlr_output_damage *output_damage);
86 /**
87  * Accumulates damage from a box and schedules a `frame` event.
88  */
89 void wlr_output_damage_add_box(struct wlr_output_damage *output_damage,
90 	struct wlr_box *box);
91 
92 #endif
93