This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
layer.c
217 lines (179 loc) · 4.64 KB
/
layer.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
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "private.h"
struct liftoff_layer *
liftoff_layer_create(struct liftoff_output *output)
{
struct liftoff_layer *layer;
layer = calloc(1, sizeof(*layer));
if (layer == NULL) {
liftoff_log_errno(LIFTOFF_ERROR, "calloc");
return NULL;
}
layer->output = output;
liftoff_list_insert(output->layers.prev, &layer->link);
output->layers_changed = true;
return layer;
}
void
liftoff_layer_destroy(struct liftoff_layer *layer)
{
if (layer == NULL) {
return;
}
layer->output->layers_changed = true;
if (layer->plane != NULL) {
layer->plane->layer = NULL;
}
if (layer->output->composition_layer == layer) {
layer->output->composition_layer = NULL;
}
free(layer->props);
liftoff_list_remove(&layer->link);
free(layer);
}
struct liftoff_layer_property *
layer_get_property(struct liftoff_layer *layer, const char *name)
{
size_t i;
for (i = 0; i < layer->props_len; i++) {
if (strcmp(layer->props[i].name, name) == 0) {
return &layer->props[i];
}
}
return NULL;
}
int
liftoff_layer_set_property(struct liftoff_layer *layer, const char *name,
uint64_t value)
{
struct liftoff_layer_property *props;
struct liftoff_layer_property *prop;
if (strcmp(name, "CRTC_ID") == 0) {
liftoff_log(LIFTOFF_ERROR,
"refusing to set a layer's CRTC_ID");
return -EINVAL;
}
prop = layer_get_property(layer, name);
if (prop == NULL) {
props = realloc(layer->props, (layer->props_len + 1)
* sizeof(struct liftoff_layer_property));
if (props == NULL) {
liftoff_log_errno(LIFTOFF_ERROR, "realloc");
return -ENOMEM;
}
layer->props = props;
layer->props_len++;
prop = &layer->props[layer->props_len - 1];
memset(prop, 0, sizeof(*prop));
strncpy(prop->name, name, sizeof(prop->name) - 1);
layer->changed = true;
}
prop->value = value;
if (strcmp(name, "FB_ID") == 0 && layer->force_composition) {
layer->force_composition = false;
layer->changed = true;
}
return 0;
}
void
liftoff_layer_set_fb_composited(struct liftoff_layer *layer)
{
if (layer->force_composition) {
return;
}
liftoff_layer_set_property(layer, "FB_ID", 0);
layer->force_composition = true;
layer->changed = true;
}
struct liftoff_plane *
liftoff_layer_get_plane(struct liftoff_layer *layer)
{
return layer->plane;
}
bool
liftoff_layer_needs_composition(struct liftoff_layer *layer)
{
if (!layer_is_visible(layer)) {
return false;
}
return layer->plane == NULL;
}
void
layer_get_rect(struct liftoff_layer *layer, struct liftoff_rect *rect)
{
struct liftoff_layer_property *x_prop, *y_prop, *w_prop, *h_prop;
x_prop = layer_get_property(layer, "CRTC_X");
y_prop = layer_get_property(layer, "CRTC_Y");
w_prop = layer_get_property(layer, "CRTC_W");
h_prop = layer_get_property(layer, "CRTC_H");
rect->x = x_prop != NULL ? x_prop->value : 0;
rect->y = y_prop != NULL ? y_prop->value : 0;
rect->width = w_prop != NULL ? w_prop->value : 0;
rect->height = h_prop != NULL ? h_prop->value : 0;
}
bool
layer_intersects(struct liftoff_layer *a, struct liftoff_layer *b)
{
struct liftoff_rect ra, rb;
layer_get_rect(a, &ra);
layer_get_rect(b, &rb);
return ra.x < rb.x + rb.width && ra.y < rb.y + rb.height &&
ra.x + ra.width > rb.x && ra.y + ra.height > rb.y;
}
void
layer_mark_clean(struct liftoff_layer *layer)
{
size_t i;
layer->changed = false;
for (i = 0; i < layer->props_len; i++) {
layer->props[i].prev_value = layer->props[i].value;
}
}
static void
log_priority(struct liftoff_layer *layer)
{
if (layer->current_priority == layer->pending_priority) {
return;
}
liftoff_log(LIFTOFF_DEBUG, "Layer %p priority change: %d -> %d",
(void *)layer, layer->current_priority,
layer->pending_priority);
}
void
layer_update_priority(struct liftoff_layer *layer, bool make_current)
{
struct liftoff_layer_property *prop;
/* TODO: also bump priority when updating other properties */
prop = layer_get_property(layer, "FB_ID");
if (prop != NULL && prop->prev_value != prop->value) {
layer->pending_priority++;
}
if (make_current) {
log_priority(layer);
layer->current_priority = layer->pending_priority;
layer->pending_priority = 0;
}
}
bool
layer_has_fb(struct liftoff_layer *layer)
{
struct liftoff_layer_property *fb_id_prop;
fb_id_prop = layer_get_property(layer, "FB_ID");
return fb_id_prop != NULL && fb_id_prop->value != 0;
}
bool
layer_is_visible(struct liftoff_layer *layer)
{
struct liftoff_layer_property *alpha_prop;
alpha_prop = layer_get_property(layer, "alpha");
if (alpha_prop != NULL && alpha_prop->value == 0) {
return false; /* fully transparent */
}
if (layer->force_composition) {
return true;
} else {
return layer_has_fb(layer);
}
}