2020-04-14 18:44:40 +02:00
|
|
|
#include <assert.h>
|
2019-04-01 18:15:56 +02:00
|
|
|
#include <drm_fourcc.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <wlr/render/drm_format_set.h>
|
|
|
|
#include <wlr/util/log.h>
|
2020-08-07 19:02:49 +02:00
|
|
|
#include "render/drm_format_set.h"
|
2019-04-01 18:15:56 +02:00
|
|
|
|
2023-05-10 22:02:07 +02:00
|
|
|
void wlr_drm_format_finish(struct wlr_drm_format *format) {
|
2023-05-10 22:18:44 +02:00
|
|
|
if (!format) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(format->modifiers);
|
2023-05-10 22:02:07 +02:00
|
|
|
}
|
|
|
|
|
2019-04-01 18:15:56 +02:00
|
|
|
void wlr_drm_format_set_finish(struct wlr_drm_format_set *set) {
|
|
|
|
for (size_t i = 0; i < set->len; ++i) {
|
2023-05-11 09:53:11 +02:00
|
|
|
wlr_drm_format_finish(&set->formats[i]);
|
2019-04-01 18:15:56 +02:00
|
|
|
}
|
|
|
|
free(set->formats);
|
|
|
|
|
|
|
|
set->len = 0;
|
2021-11-15 15:44:12 +01:00
|
|
|
set->capacity = 0;
|
2019-04-01 18:15:56 +02:00
|
|
|
set->formats = NULL;
|
|
|
|
}
|
|
|
|
|
2023-05-11 09:53:11 +02:00
|
|
|
static struct wlr_drm_format *format_set_get(const struct wlr_drm_format_set *set,
|
2019-04-01 18:15:56 +02:00
|
|
|
uint32_t format) {
|
|
|
|
for (size_t i = 0; i < set->len; ++i) {
|
2023-05-11 09:53:11 +02:00
|
|
|
if (set->formats[i].format == format) {
|
2019-04-01 18:15:56 +02:00
|
|
|
return &set->formats[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct wlr_drm_format *wlr_drm_format_set_get(
|
|
|
|
const struct wlr_drm_format_set *set, uint32_t format) {
|
2023-05-11 09:53:11 +02:00
|
|
|
return format_set_get(set, format);
|
2019-04-01 18:15:56 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 22:36:35 +02:00
|
|
|
bool wlr_drm_format_set_has(const struct wlr_drm_format_set *set,
|
|
|
|
uint32_t format, uint64_t modifier) {
|
|
|
|
const struct wlr_drm_format *fmt = wlr_drm_format_set_get(set, format);
|
|
|
|
if (!fmt) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-03-31 16:50:17 +02:00
|
|
|
return wlr_drm_format_has(fmt, modifier);
|
2019-04-09 22:36:35 +02:00
|
|
|
}
|
|
|
|
|
2019-04-01 18:15:56 +02:00
|
|
|
bool wlr_drm_format_set_add(struct wlr_drm_format_set *set, uint32_t format,
|
|
|
|
uint64_t modifier) {
|
2020-04-14 18:44:40 +02:00
|
|
|
assert(format != DRM_FORMAT_INVALID);
|
2019-04-01 18:15:56 +02:00
|
|
|
|
2023-05-11 09:53:11 +02:00
|
|
|
struct wlr_drm_format *existing = format_set_get(set, format);
|
|
|
|
if (existing) {
|
|
|
|
return wlr_drm_format_add(existing, modifier);
|
2019-04-01 18:15:56 +02:00
|
|
|
}
|
|
|
|
|
2023-05-11 09:53:11 +02:00
|
|
|
struct wlr_drm_format fmt;
|
|
|
|
wlr_drm_format_init(&fmt, format);
|
|
|
|
if (!wlr_drm_format_add(&fmt, modifier)) {
|
|
|
|
wlr_drm_format_finish(&fmt);
|
2020-12-12 22:26:14 +01:00
|
|
|
return false;
|
2019-04-01 18:15:56 +02:00
|
|
|
}
|
|
|
|
|
2021-11-15 15:44:12 +01:00
|
|
|
if (set->len == set->capacity) {
|
2023-05-11 09:53:11 +02:00
|
|
|
size_t capacity = set->capacity ? set->capacity * 2 : 4;
|
2019-04-01 18:15:56 +02:00
|
|
|
|
2023-05-11 09:53:11 +02:00
|
|
|
struct wlr_drm_format *fmts = realloc(set->formats, sizeof(*fmts) * capacity);
|
|
|
|
if (!fmts) {
|
2019-04-01 18:15:56 +02:00
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
2024-01-16 23:55:01 +01:00
|
|
|
wlr_drm_format_finish(&fmt);
|
2019-04-01 18:15:56 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-11 09:53:11 +02:00
|
|
|
set->capacity = capacity;
|
|
|
|
set->formats = fmts;
|
2019-04-01 18:15:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
set->formats[set->len++] = fmt;
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-07 19:02:49 +02:00
|
|
|
|
2023-05-05 00:26:45 +02:00
|
|
|
void wlr_drm_format_init(struct wlr_drm_format *fmt, uint32_t format) {
|
2023-07-07 14:34:56 +02:00
|
|
|
*fmt = (struct wlr_drm_format){
|
|
|
|
.format = format,
|
|
|
|
};
|
2020-12-12 22:26:14 +01:00
|
|
|
}
|
|
|
|
|
2021-03-31 16:50:17 +02:00
|
|
|
bool wlr_drm_format_has(const struct wlr_drm_format *fmt, uint64_t modifier) {
|
|
|
|
for (size_t i = 0; i < fmt->len; ++i) {
|
|
|
|
if (fmt->modifiers[i] == modifier) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-05 00:17:55 +02:00
|
|
|
bool wlr_drm_format_add(struct wlr_drm_format *fmt, uint64_t modifier) {
|
2021-03-31 16:50:17 +02:00
|
|
|
if (wlr_drm_format_has(fmt, modifier)) {
|
|
|
|
return true;
|
2020-12-12 22:26:14 +01:00
|
|
|
}
|
|
|
|
|
2021-11-15 15:44:12 +01:00
|
|
|
if (fmt->len == fmt->capacity) {
|
|
|
|
size_t capacity = fmt->capacity ? fmt->capacity * 2 : 4;
|
2020-12-12 22:26:14 +01:00
|
|
|
|
2023-05-10 22:18:44 +02:00
|
|
|
uint64_t *new_modifiers = realloc(fmt->modifiers, sizeof(*fmt->modifiers) * capacity);
|
|
|
|
if (!new_modifiers) {
|
2020-12-12 22:26:14 +01:00
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-15 15:44:12 +01:00
|
|
|
fmt->capacity = capacity;
|
2023-05-10 22:18:44 +02:00
|
|
|
fmt->modifiers = new_modifiers;
|
2020-12-12 22:26:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt->modifiers[fmt->len++] = modifier;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:00:22 +02:00
|
|
|
bool wlr_drm_format_copy(struct wlr_drm_format *dst, const struct wlr_drm_format *src) {
|
|
|
|
assert(src->len <= src->capacity);
|
2023-05-10 22:18:44 +02:00
|
|
|
|
2023-05-10 22:00:22 +02:00
|
|
|
uint64_t *modifiers = malloc(sizeof(*modifiers) * src->len);
|
2023-05-10 22:18:44 +02:00
|
|
|
if (!modifiers) {
|
2023-05-10 22:00:22 +02:00
|
|
|
return false;
|
2020-08-07 19:02:49 +02:00
|
|
|
}
|
2023-05-10 22:18:44 +02:00
|
|
|
|
2023-05-10 22:00:22 +02:00
|
|
|
memcpy(modifiers, src->modifiers, sizeof(*modifiers) * src->len);
|
2023-05-10 22:18:44 +02:00
|
|
|
|
2023-05-10 22:00:22 +02:00
|
|
|
wlr_drm_format_finish(dst);
|
|
|
|
dst->capacity = src->len;
|
|
|
|
dst->len = src->len;
|
|
|
|
dst->format = src->format;
|
2023-05-10 22:18:44 +02:00
|
|
|
dst->modifiers = modifiers;
|
2023-05-10 22:00:22 +02:00
|
|
|
return true;
|
2020-08-07 19:02:49 +02:00
|
|
|
}
|
2020-11-25 15:58:37 +01:00
|
|
|
|
2022-12-02 14:32:50 +01:00
|
|
|
bool wlr_drm_format_set_copy(struct wlr_drm_format_set *dst, const struct wlr_drm_format_set *src) {
|
2023-05-11 09:53:11 +02:00
|
|
|
struct wlr_drm_format *formats = malloc(src->len * sizeof(formats[0]));
|
2022-12-02 14:32:50 +01:00
|
|
|
if (formats == NULL) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct wlr_drm_format_set out = {
|
|
|
|
.len = 0,
|
|
|
|
.capacity = src->len,
|
|
|
|
.formats = formats,
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < src->len; i++) {
|
2023-05-11 09:53:11 +02:00
|
|
|
out.formats[out.len] = (struct wlr_drm_format){0};
|
|
|
|
if (!wlr_drm_format_copy(&out.formats[out.len], &src->formats[i])) {
|
2022-12-02 14:32:50 +01:00
|
|
|
wlr_drm_format_set_finish(&out);
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-11 09:53:11 +02:00
|
|
|
|
2022-12-02 14:32:50 +01:00
|
|
|
out.len++;
|
|
|
|
}
|
|
|
|
|
2022-12-02 17:39:10 +01:00
|
|
|
*dst = out;
|
|
|
|
|
2022-12-02 14:32:50 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-05-05 01:24:44 +02:00
|
|
|
bool wlr_drm_format_intersect(struct wlr_drm_format *dst,
|
2020-11-25 15:58:37 +01:00
|
|
|
const struct wlr_drm_format *a, const struct wlr_drm_format *b) {
|
|
|
|
assert(a->format == b->format);
|
|
|
|
|
2023-05-10 22:18:44 +02:00
|
|
|
size_t capacity = a->len < b->len ? a->len : b->len;
|
|
|
|
uint64_t *modifiers = malloc(sizeof(*modifiers) * capacity);
|
|
|
|
if (!modifiers) {
|
|
|
|
return false;
|
2020-11-25 15:58:37 +01:00
|
|
|
}
|
2023-05-10 22:18:44 +02:00
|
|
|
|
|
|
|
struct wlr_drm_format fmt = {
|
|
|
|
.capacity = capacity,
|
|
|
|
.len = 0,
|
|
|
|
.modifiers = modifiers,
|
|
|
|
.format = a->format,
|
|
|
|
};
|
2020-11-25 15:58:37 +01:00
|
|
|
|
|
|
|
for (size_t i = 0; i < a->len; i++) {
|
|
|
|
for (size_t j = 0; j < b->len; j++) {
|
|
|
|
if (a->modifiers[i] == b->modifiers[j]) {
|
2023-05-10 22:18:44 +02:00
|
|
|
assert(fmt.len < fmt.capacity);
|
|
|
|
fmt.modifiers[fmt.len++] = a->modifiers[i];
|
2020-11-25 15:58:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 01:24:44 +02:00
|
|
|
wlr_drm_format_finish(dst);
|
|
|
|
*dst = fmt;
|
|
|
|
return true;
|
2020-11-25 15:58:37 +01:00
|
|
|
}
|
2021-12-01 13:35:50 +01:00
|
|
|
|
|
|
|
bool wlr_drm_format_set_intersect(struct wlr_drm_format_set *dst,
|
|
|
|
const struct wlr_drm_format_set *a, const struct wlr_drm_format_set *b) {
|
|
|
|
struct wlr_drm_format_set out = {0};
|
|
|
|
out.capacity = a->len < b->len ? a->len : b->len;
|
2023-05-11 09:53:11 +02:00
|
|
|
out.formats = malloc(sizeof(*out.formats) * out.capacity);
|
2021-12-01 13:35:50 +01:00
|
|
|
if (out.formats == NULL) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < a->len; i++) {
|
|
|
|
for (size_t j = 0; j < b->len; j++) {
|
2023-05-11 09:53:11 +02:00
|
|
|
if (a->formats[i].format == b->formats[j].format) {
|
2021-12-01 13:35:50 +01:00
|
|
|
// When the two formats have no common modifier, keep
|
|
|
|
// intersecting the rest of the formats: they may be compatible
|
|
|
|
// with each other
|
2023-05-11 20:43:22 +02:00
|
|
|
out.formats[out.len] = (struct wlr_drm_format){0};
|
2023-05-11 09:53:11 +02:00
|
|
|
if (!wlr_drm_format_intersect(&out.formats[out.len],
|
|
|
|
&a->formats[i], &b->formats[j])) {
|
2023-05-05 01:24:44 +02:00
|
|
|
wlr_drm_format_set_finish(&out);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-11 09:53:11 +02:00
|
|
|
if (out.formats[out.len].len == 0) {
|
|
|
|
wlr_drm_format_finish(&out.formats[out.len]);
|
2023-05-05 01:24:44 +02:00
|
|
|
} else {
|
2021-12-01 13:35:50 +01:00
|
|
|
out.len++;
|
|
|
|
}
|
2023-05-05 01:24:44 +02:00
|
|
|
|
2021-12-01 13:35:50 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out.len == 0) {
|
|
|
|
wlr_drm_format_set_finish(&out);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-04 20:16:55 +02:00
|
|
|
wlr_drm_format_set_finish(dst);
|
2021-12-01 13:35:50 +01:00
|
|
|
*dst = out;
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-22 23:16:41 +02:00
|
|
|
|
|
|
|
static bool drm_format_set_extend(struct wlr_drm_format_set *dst,
|
|
|
|
const struct wlr_drm_format_set *src) {
|
|
|
|
for (size_t i = 0; i < src->len; i++) {
|
2023-05-11 09:53:11 +02:00
|
|
|
struct wlr_drm_format *format = &src->formats[i];
|
2022-07-22 23:16:41 +02:00
|
|
|
for (size_t j = 0; j < format->len; j++) {
|
|
|
|
if (!wlr_drm_format_set_add(dst, format->format, format->modifiers[j])) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Adding format/modifier to set failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wlr_drm_format_set_union(struct wlr_drm_format_set *dst,
|
|
|
|
const struct wlr_drm_format_set *a, const struct wlr_drm_format_set *b) {
|
|
|
|
struct wlr_drm_format_set out = {0};
|
|
|
|
out.capacity = a->len + b->len;
|
2023-05-11 09:53:11 +02:00
|
|
|
out.formats = malloc(sizeof(*out.formats) * out.capacity);
|
2022-07-22 23:16:41 +02:00
|
|
|
if (out.formats == NULL) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add both a and b sets into out
|
2024-02-22 23:03:50 +01:00
|
|
|
if (!drm_format_set_extend(&out, a) ||
|
|
|
|
!drm_format_set_extend(&out, b)) {
|
|
|
|
wlr_drm_format_set_finish(&out);
|
2022-07-22 23:16:41 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-05-04 20:16:34 +02:00
|
|
|
wlr_drm_format_set_finish(dst);
|
2022-07-22 23:16:41 +02:00
|
|
|
*dst = out;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|