2020-04-14 16:44:40 +00:00
|
|
|
#include <assert.h>
|
2019-04-01 16:15:56 +00: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 17:02:49 +00:00
|
|
|
#include "render/drm_format_set.h"
|
2019-04-01 16:15:56 +00:00
|
|
|
|
|
|
|
void wlr_drm_format_set_finish(struct wlr_drm_format_set *set) {
|
|
|
|
for (size_t i = 0; i < set->len; ++i) {
|
|
|
|
free(set->formats[i]);
|
|
|
|
}
|
|
|
|
free(set->formats);
|
|
|
|
|
|
|
|
set->len = 0;
|
2021-11-15 14:44:12 +00:00
|
|
|
set->capacity = 0;
|
2019-04-01 16:15:56 +00:00
|
|
|
set->formats = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct wlr_drm_format **format_set_get_ref(struct wlr_drm_format_set *set,
|
|
|
|
uint32_t format) {
|
|
|
|
for (size_t i = 0; i < set->len; ++i) {
|
|
|
|
if (set->formats[i]->format == format) {
|
|
|
|
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) {
|
|
|
|
struct wlr_drm_format **ptr =
|
|
|
|
format_set_get_ref((struct wlr_drm_format_set *)set, format);
|
|
|
|
return ptr ? *ptr : NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-09 20:36:35 +00: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 14:50:17 +00:00
|
|
|
return wlr_drm_format_has(fmt, modifier);
|
2019-04-09 20:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-04-01 16:15:56 +00:00
|
|
|
bool wlr_drm_format_set_add(struct wlr_drm_format_set *set, uint32_t format,
|
|
|
|
uint64_t modifier) {
|
2020-04-14 16:44:40 +00:00
|
|
|
assert(format != DRM_FORMAT_INVALID);
|
2019-04-01 16:15:56 +00:00
|
|
|
|
2020-12-12 21:26:14 +00:00
|
|
|
struct wlr_drm_format **ptr = format_set_get_ref(set, format);
|
2019-04-01 16:15:56 +00:00
|
|
|
if (ptr) {
|
2020-12-12 21:26:14 +00:00
|
|
|
return wlr_drm_format_add(ptr, modifier);
|
2019-04-01 16:15:56 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 21:26:14 +00:00
|
|
|
struct wlr_drm_format *fmt = wlr_drm_format_create(format);
|
2019-04-01 16:15:56 +00:00
|
|
|
if (!fmt) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-12 21:26:14 +00:00
|
|
|
if (!wlr_drm_format_add(&fmt, modifier)) {
|
|
|
|
return false;
|
2019-04-01 16:15:56 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 14:44:12 +00:00
|
|
|
if (set->len == set->capacity) {
|
|
|
|
size_t new = set->capacity ? set->capacity * 2 : 4;
|
2019-04-01 16:15:56 +00:00
|
|
|
|
|
|
|
struct wlr_drm_format **tmp = realloc(set->formats,
|
2023-03-17 01:13:11 +00:00
|
|
|
sizeof(set->formats[0]) * new);
|
2019-04-01 16:15:56 +00:00
|
|
|
if (!tmp) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
free(fmt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-15 14:44:12 +00:00
|
|
|
set->capacity = new;
|
2019-04-01 16:15:56 +00:00
|
|
|
set->formats = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
set->formats[set->len++] = fmt;
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-07 17:02:49 +00:00
|
|
|
|
2020-12-12 21:26:14 +00:00
|
|
|
struct wlr_drm_format *wlr_drm_format_create(uint32_t format) {
|
2021-11-15 14:44:12 +00:00
|
|
|
size_t capacity = 4;
|
2020-12-12 21:26:14 +00:00
|
|
|
struct wlr_drm_format *fmt =
|
2021-11-15 14:44:12 +00:00
|
|
|
calloc(1, sizeof(*fmt) + sizeof(fmt->modifiers[0]) * capacity);
|
2020-12-12 21:26:14 +00:00
|
|
|
if (!fmt) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fmt->format = format;
|
2021-11-15 14:44:12 +00:00
|
|
|
fmt->capacity = capacity;
|
2020-12-12 21:26:14 +00:00
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
|
2021-03-31 14:50:17 +00: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;
|
|
|
|
}
|
|
|
|
|
2020-12-12 21:26:14 +00:00
|
|
|
bool wlr_drm_format_add(struct wlr_drm_format **fmt_ptr, uint64_t modifier) {
|
|
|
|
struct wlr_drm_format *fmt = *fmt_ptr;
|
|
|
|
|
2021-03-31 14:50:17 +00:00
|
|
|
if (wlr_drm_format_has(fmt, modifier)) {
|
|
|
|
return true;
|
2020-12-12 21:26:14 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 14:44:12 +00:00
|
|
|
if (fmt->len == fmt->capacity) {
|
|
|
|
size_t capacity = fmt->capacity ? fmt->capacity * 2 : 4;
|
2020-12-12 21:26:14 +00:00
|
|
|
|
2021-11-15 14:44:12 +00:00
|
|
|
fmt = realloc(fmt, sizeof(*fmt) + sizeof(fmt->modifiers[0]) * capacity);
|
2020-12-12 21:26:14 +00:00
|
|
|
if (!fmt) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-15 14:44:12 +00:00
|
|
|
fmt->capacity = capacity;
|
2020-12-12 21:26:14 +00:00
|
|
|
*fmt_ptr = fmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt->modifiers[fmt->len++] = modifier;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-07 17:02:49 +00:00
|
|
|
struct wlr_drm_format *wlr_drm_format_dup(const struct wlr_drm_format *format) {
|
2021-11-15 14:44:12 +00:00
|
|
|
assert(format->len <= format->capacity);
|
2020-08-07 17:02:49 +00:00
|
|
|
size_t format_size = sizeof(struct wlr_drm_format) +
|
2021-11-15 14:44:12 +00:00
|
|
|
format->capacity * sizeof(format->modifiers[0]);
|
2020-08-07 17:02:49 +00:00
|
|
|
struct wlr_drm_format *duped_format = malloc(format_size);
|
|
|
|
if (duped_format == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(duped_format, format, format_size);
|
|
|
|
return duped_format;
|
|
|
|
}
|
2020-11-25 14:58:37 +00:00
|
|
|
|
2022-12-02 13:32:50 +00:00
|
|
|
bool wlr_drm_format_set_copy(struct wlr_drm_format_set *dst, const struct wlr_drm_format_set *src) {
|
|
|
|
struct wlr_drm_format **formats = malloc(src->len * sizeof(formats[0]));
|
|
|
|
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++) {
|
|
|
|
out.formats[out.len] = wlr_drm_format_dup(src->formats[i]);
|
|
|
|
if (out.formats[out.len] == NULL) {
|
|
|
|
wlr_drm_format_set_finish(&out);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
out.len++;
|
|
|
|
}
|
|
|
|
|
2022-12-02 16:39:10 +00:00
|
|
|
*dst = out;
|
|
|
|
|
2022-12-02 13:32:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-11-25 14:58:37 +00:00
|
|
|
struct wlr_drm_format *wlr_drm_format_intersect(
|
|
|
|
const struct wlr_drm_format *a, const struct wlr_drm_format *b) {
|
|
|
|
assert(a->format == b->format);
|
|
|
|
|
|
|
|
size_t format_cap = a->len < b->len ? a->len : b->len;
|
|
|
|
size_t format_size = sizeof(struct wlr_drm_format) +
|
|
|
|
format_cap * sizeof(a->modifiers[0]);
|
|
|
|
struct wlr_drm_format *format = calloc(1, format_size);
|
|
|
|
if (format == NULL) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
format->format = a->format;
|
2021-11-15 14:44:12 +00:00
|
|
|
format->capacity = format_cap;
|
2020-11-25 14:58:37 +00: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]) {
|
2021-11-15 14:44:12 +00:00
|
|
|
assert(format->len < format->capacity);
|
2020-11-25 14:58:37 +00:00
|
|
|
format->modifiers[format->len] = a->modifiers[i];
|
|
|
|
format->len++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 15:07:55 +00:00
|
|
|
// If the intersection is empty, then the formats aren't compatible with
|
|
|
|
// each other.
|
|
|
|
if (format->len == 0) {
|
2020-11-25 14:58:37 +00:00
|
|
|
free(format);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return format;
|
|
|
|
}
|
2021-12-01 12:35:50 +00: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) {
|
|
|
|
assert(dst != a && dst != b);
|
|
|
|
|
|
|
|
struct wlr_drm_format_set out = {0};
|
|
|
|
out.capacity = a->len < b->len ? a->len : b->len;
|
|
|
|
out.formats = calloc(out.capacity, sizeof(struct wlr_drm_format *));
|
|
|
|
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++) {
|
|
|
|
if (a->formats[i]->format == b->formats[j]->format) {
|
|
|
|
// When the two formats have no common modifier, keep
|
|
|
|
// intersecting the rest of the formats: they may be compatible
|
|
|
|
// with each other
|
|
|
|
struct wlr_drm_format *format =
|
|
|
|
wlr_drm_format_intersect(a->formats[i], b->formats[j]);
|
|
|
|
if (format != NULL) {
|
|
|
|
out.formats[out.len] = format;
|
|
|
|
out.len++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (out.len == 0) {
|
|
|
|
wlr_drm_format_set_finish(&out);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = out;
|
|
|
|
return true;
|
|
|
|
}
|
2022-07-22 21:16:41 +00: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++) {
|
|
|
|
struct wlr_drm_format *format = src->formats[i];
|
|
|
|
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) {
|
|
|
|
assert(dst != a && dst != b);
|
|
|
|
|
|
|
|
struct wlr_drm_format_set out = {0};
|
|
|
|
out.capacity = a->len + b->len;
|
|
|
|
out.formats = calloc(out.capacity, sizeof(struct wlr_drm_format *));
|
|
|
|
if (out.formats == NULL) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add both a and b sets into out
|
|
|
|
if (!drm_format_set_extend(&out, a)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!drm_format_set_extend(&out, b)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = out;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|