-
Notifications
You must be signed in to change notification settings - Fork 0
/
plane.c
411 lines (362 loc) · 9.8 KB
/
plane.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
#include <errno.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include "private.h"
static int
guess_plane_zpos_from_type(struct liftoff_device *device, uint32_t plane_id,
uint32_t type)
{
struct liftoff_plane *primary;
/* From far to close to the eye: primary, overlay, cursor. Unless
* the overlay ID < primary ID. */
switch (type) {
case DRM_PLANE_TYPE_PRIMARY:
return 0;
case DRM_PLANE_TYPE_CURSOR:
return 2;
case DRM_PLANE_TYPE_OVERLAY:
if (liftoff_list_empty(&device->planes)) {
return 0; /* No primary plane, shouldn't happen */
}
primary = liftoff_container_of(device->planes.next,
primary, link);
if (plane_id < primary->id) {
return -1;
} else {
return 1;
}
}
return 0;
}
struct liftoff_plane *
liftoff_plane_create(struct liftoff_device *device, uint32_t id)
{
struct liftoff_plane *plane, *cur;
drmModePlane *drm_plane;
drmModeObjectProperties *drm_props;
uint32_t i;
drmModePropertyRes *prop;
uint64_t value;
bool has_type = false, has_zpos = false;
liftoff_list_for_each(plane, &device->planes, link) {
if (plane->id == id) {
liftoff_log(LIFTOFF_ERROR, "tried to register plane "
"%"PRIu32" twice\n", id);
errno = EEXIST;
return NULL;
}
}
plane = calloc(1, sizeof(*plane));
if (plane == NULL) {
liftoff_log_errno(LIFTOFF_ERROR, "calloc");
return NULL;
}
drm_plane = drmModeGetPlane(device->drm_fd, id);
if (drm_plane == NULL) {
liftoff_log_errno(LIFTOFF_ERROR, "drmModeGetPlane");
return NULL;
}
plane->id = drm_plane->plane_id;
plane->possible_crtcs = drm_plane->possible_crtcs;
drmModeFreePlane(drm_plane);
drm_props = drmModeObjectGetProperties(device->drm_fd, id,
DRM_MODE_OBJECT_PLANE);
if (drm_props == NULL) {
liftoff_log_errno(LIFTOFF_ERROR, "drmModeObjectGetProperties");
return NULL;
}
plane->props = calloc(drm_props->count_props, sizeof(plane->props[0]));
if (plane->props == NULL) {
liftoff_log_errno(LIFTOFF_ERROR, "calloc");
drmModeFreeObjectProperties(drm_props);
return NULL;
}
for (i = 0; i < drm_props->count_props; i++) {
prop = drmModeGetProperty(device->drm_fd, drm_props->props[i]);
if (prop == NULL) {
liftoff_log_errno(LIFTOFF_ERROR, "drmModeGetProperty");
drmModeFreeObjectProperties(drm_props);
return NULL;
}
plane->props[i] = prop;
plane->props_len++;
value = drm_props->prop_values[i];
if (strcmp(prop->name, "type") == 0) {
plane->type = value;
has_type = true;
} else if (strcmp(prop->name, "zpos") == 0) {
plane->zpos = value;
has_zpos = true;
} else if (strcmp(prop->name, "IN_FORMATS") == 0) {
plane->in_formats_blob = drmModeGetPropertyBlob(device->drm_fd,
value);
if (plane->in_formats_blob == NULL) {
liftoff_log_errno(LIFTOFF_ERROR, "drmModeGetPropertyBlob");
return NULL;
}
}
}
drmModeFreeObjectProperties(drm_props);
if (!has_type) {
liftoff_log(LIFTOFF_ERROR,
"plane %"PRIu32" is missing the 'type' property",
plane->id);
free(plane);
errno = EINVAL;
return NULL;
} else if (!has_zpos) {
plane->zpos = guess_plane_zpos_from_type(device, plane->id,
plane->type);
}
/* During plane allocation, we will use the plane list order to fill
* planes with FBs. Primary planes need to be filled first, then planes
* far from the primary planes, then planes closer and closer to the
* primary plane. */
if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
liftoff_list_insert(&device->planes, &plane->link);
} else {
liftoff_list_for_each(cur, &device->planes, link) {
if (cur->type != DRM_PLANE_TYPE_PRIMARY &&
plane->zpos >= cur->zpos) {
liftoff_list_insert(cur->link.prev, &plane->link);
break;
}
}
if (plane->link.next == NULL) { /* not inserted */
liftoff_list_insert(device->planes.prev, &plane->link);
}
}
return plane;
}
void
liftoff_plane_destroy(struct liftoff_plane *plane)
{
size_t i;
if (plane == NULL) {
return;
}
if (plane->layer != NULL) {
plane->layer->plane = NULL;
}
for (i = 0; i < plane->props_len; i++) {
drmModeFreeProperty(plane->props[i]);
}
liftoff_list_remove(&plane->link);
free(plane->props);
drmModeFreePropertyBlob(plane->in_formats_blob);
free(plane);
}
uint32_t
liftoff_plane_get_id(struct liftoff_plane *plane)
{
return plane->id;
}
static const drmModePropertyRes *
plane_get_property(struct liftoff_plane *plane, const char *name)
{
size_t i;
for (i = 0; i < plane->props_len; i++) {
if (strcmp(plane->props[i]->name, name) == 0) {
return plane->props[i];
}
}
return NULL;
}
static int
check_range_prop(const drmModePropertyRes *prop, uint64_t value)
{
if (value < prop->values[0] || value > prop->values[1]) {
return -EINVAL;
}
return 0;
}
static int
check_enum_prop(const drmModePropertyRes *prop, uint64_t value)
{
int i;
for (i = 0; i < prop->count_enums; i++) {
if (prop->enums[i].value == value) {
return 0;
}
}
return -EINVAL;
}
static int
check_bitmask_prop(const drmModePropertyRes *prop, uint64_t value)
{
int i;
uint64_t mask;
mask = 0;
for (i = 0; i < prop->count_enums; i++) {
mask |= (uint64_t)1 << prop->enums[i].value;
}
if ((value & ~mask) != 0) {
return -EINVAL;
}
return 0;
}
static int
check_signed_range_prop(const drmModePropertyRes *prop, uint64_t value)
{
if ((int64_t) value < (int64_t) prop->values[0] ||
(int64_t) value > (int64_t) prop->values[1]) {
return -EINVAL;
}
return 0;
}
static int
plane_set_prop(struct liftoff_plane *plane, drmModeAtomicReq *req,
const drmModePropertyRes *prop, uint64_t value)
{
int ret;
if (prop->flags & DRM_MODE_PROP_IMMUTABLE) {
return -EINVAL;
}
/* Manually check the property value if we can: this may avoid
* unnecessary test commits */
ret = 0;
switch (drmModeGetPropertyType(prop)) {
case DRM_MODE_PROP_RANGE:
ret = check_range_prop(prop, value);
break;
case DRM_MODE_PROP_ENUM:
ret = check_enum_prop(prop, value);
break;
case DRM_MODE_PROP_BITMASK:
ret = check_bitmask_prop(prop, value);
break;
case DRM_MODE_PROP_SIGNED_RANGE:
ret = check_signed_range_prop(prop, value);
break;
}
if (ret != 0) {
return ret;
}
ret = drmModeAtomicAddProperty(req, plane->id, prop->prop_id, value);
if (ret < 0) {
liftoff_log(LIFTOFF_ERROR, "drmModeAtomicAddProperty: %s",
strerror(-ret));
return ret;
}
return 0;
}
static int
set_plane_prop_str(struct liftoff_plane *plane, drmModeAtomicReq *req,
const char *name, uint64_t value)
{
const drmModePropertyRes *prop;
prop = plane_get_property(plane, name);
if (prop == NULL) {
liftoff_log(LIFTOFF_DEBUG,
"plane %"PRIu32" is missing the %s property",
plane->id, name);
return -EINVAL;
}
return plane_set_prop(plane, req, prop, value);
}
bool
plane_check_layer_fb(struct liftoff_plane *plane, struct liftoff_layer *layer)
{
const struct drm_format_modifier_blob *set;
const uint32_t *formats;
const struct drm_format_modifier *modifiers;
size_t i;
ssize_t format_index, modifier_index;
int format_shift;
/* TODO: add support for legacy format list with implicit modifier */
if (layer->fb_info.fb_id == 0 ||
!(layer->fb_info.flags & DRM_MODE_FB_MODIFIERS) ||
plane->in_formats_blob == NULL) {
return true; /* not enough information to reject */
}
set = plane->in_formats_blob->data;
formats = (void *)((char *)set + set->formats_offset);
format_index = -1;
for (i = 0; i < set->count_formats; ++i) {
if (formats[i] == layer->fb_info.pixel_format) {
format_index = (ssize_t)i;
break;
}
}
if (format_index < 0) {
return false;
}
modifiers = (void *)((char *)set + set->modifiers_offset);
modifier_index = -1;
for (i = 0; i < set->count_modifiers; i++) {
if (modifiers[i].modifier == layer->fb_info.modifier) {
modifier_index = (ssize_t)i;
break;
}
}
if (modifier_index < 0) {
return false;
}
if ((size_t)format_index < modifiers[modifier_index].offset ||
(size_t)format_index >= modifiers[modifier_index].offset + 64) {
return false;
}
format_shift = format_index - (int)modifiers[modifier_index].offset;
return (modifiers[modifier_index].formats & ((uint64_t)1 << format_shift)) != 0;
}
int
plane_apply(struct liftoff_plane *plane, struct liftoff_layer *layer,
drmModeAtomicReq *req)
{
int cursor, ret;
size_t i;
struct liftoff_layer_property *layer_prop;
const drmModePropertyRes *plane_prop;
cursor = drmModeAtomicGetCursor(req);
if (layer == NULL) {
ret = set_plane_prop_str(plane, req, "FB_ID", 0);
if (ret != 0) {
return ret;
}
return set_plane_prop_str(plane, req, "CRTC_ID", 0);
}
ret = set_plane_prop_str(plane, req, "CRTC_ID", layer->output->crtc_id);
if (ret != 0) {
return ret;
}
for (i = 0; i < layer->props_len; i++) {
layer_prop = &layer->props[i];
if (strcmp(layer_prop->name, "zpos") == 0) {
/* We don't yet support setting the zpos property. We
* only use it (read-only) during plane allocation. */
continue;
}
plane_prop = plane_get_property(plane, layer_prop->name);
if (plane_prop == NULL) {
if (strcmp(layer_prop->name, "alpha") == 0 &&
layer_prop->value == 0xFFFF) {
continue; /* Layer is completely opaque */
}
if (strcmp(layer_prop->name, "rotation") == 0 &&
layer_prop->value == DRM_MODE_ROTATE_0) {
continue; /* Layer isn't rotated */
}
if (strcmp(layer_prop->name, "SCALING_FILTER") == 0 &&
layer_prop->value == 0) {
continue; /* Layer uses default scaling filter */
}
if (strcmp(layer_prop->name, "pixel blend mode") == 0 &&
layer_prop->value == 0) {
continue; /* Layer uses pre-multiplied alpha */
}
if (strcmp(layer_prop->name, "FB_DAMAGE_CLIPS") == 0) {
continue; /* Damage can be omitted */
}
drmModeAtomicSetCursor(req, cursor);
return -EINVAL;
}
ret = plane_set_prop(plane, req, plane_prop, layer_prop->value);
if (ret != 0) {
drmModeAtomicSetCursor(req, cursor);
return ret;
}
}
return 0;
}