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
|
|
|
|
|
|
|
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;
|
|
|
|
set->cap = 0;
|
|
|
|
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 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-10-01 17:22:04 +02:00
|
|
|
|
|
|
|
if (modifier == DRM_FORMAT_MOD_INVALID) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:22:04 +02:00
|
|
|
for (size_t i = 0; i < fmt->len; ++i) {
|
|
|
|
if (fmt->modifiers[i] == modifier) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
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
|
|
|
|
2020-12-12 22:26:14 +01:00
|
|
|
struct wlr_drm_format **ptr = format_set_get_ref(set, format);
|
2019-04-01 18:15:56 +02:00
|
|
|
if (ptr) {
|
2020-12-12 22:26:14 +01:00
|
|
|
return wlr_drm_format_add(ptr, modifier);
|
2019-04-01 18:15:56 +02:00
|
|
|
}
|
|
|
|
|
2020-12-12 22:26:14 +01:00
|
|
|
struct wlr_drm_format *fmt = wlr_drm_format_create(format);
|
2019-04-01 18:15:56 +02:00
|
|
|
if (!fmt) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-12 22:26:14 +01:00
|
|
|
if (!wlr_drm_format_add(&fmt, modifier)) {
|
|
|
|
return false;
|
2019-04-01 18:15:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (set->len == set->cap) {
|
|
|
|
size_t new = set->cap ? set->cap * 2 : 4;
|
|
|
|
|
|
|
|
struct wlr_drm_format **tmp = realloc(set->formats,
|
|
|
|
sizeof(*fmt) + sizeof(fmt->modifiers[0]) * new);
|
|
|
|
if (!tmp) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
free(fmt);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
set->cap = new;
|
|
|
|
set->formats = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
set->formats[set->len++] = fmt;
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-07 19:02:49 +02:00
|
|
|
|
2020-12-12 22:26:14 +01:00
|
|
|
struct wlr_drm_format *wlr_drm_format_create(uint32_t format) {
|
|
|
|
size_t cap = 4;
|
|
|
|
struct wlr_drm_format *fmt =
|
|
|
|
calloc(1, sizeof(*fmt) + sizeof(fmt->modifiers[0]) * cap);
|
|
|
|
if (!fmt) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
fmt->format = format;
|
|
|
|
fmt->cap = cap;
|
|
|
|
return fmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wlr_drm_format_add(struct wlr_drm_format **fmt_ptr, uint64_t modifier) {
|
|
|
|
struct wlr_drm_format *fmt = *fmt_ptr;
|
|
|
|
|
2021-10-01 17:22:04 +02:00
|
|
|
if (modifier == DRM_FORMAT_MOD_INVALID) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:22:04 +02:00
|
|
|
for (size_t i = 0; i < fmt->len; ++i) {
|
|
|
|
if (fmt->modifiers[i] == modifier) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-12-12 22:26:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fmt->len == fmt->cap) {
|
|
|
|
size_t cap = fmt->cap ? fmt->cap * 2 : 4;
|
|
|
|
|
|
|
|
fmt = realloc(fmt, sizeof(*fmt) + sizeof(fmt->modifiers[0]) * cap);
|
|
|
|
if (!fmt) {
|
|
|
|
wlr_log_errno(WLR_ERROR, "Allocation failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt->cap = cap;
|
|
|
|
*fmt_ptr = fmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt->modifiers[fmt->len++] = modifier;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-07 19:02:49 +02:00
|
|
|
struct wlr_drm_format *wlr_drm_format_dup(const struct wlr_drm_format *format) {
|
2020-12-08 23:35:05 +01:00
|
|
|
assert(format->len <= format->cap);
|
2020-08-07 19:02:49 +02:00
|
|
|
size_t format_size = sizeof(struct wlr_drm_format) +
|
2020-12-08 16:02:44 +01:00
|
|
|
format->cap * sizeof(format->modifiers[0]);
|
2020-08-07 19:02:49 +02: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 15:58:37 +01: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);
|
|
|
|
|
2021-10-01 17:22:04 +02:00
|
|
|
// Special case: if a format only supports LINEAR and the other doesn't
|
|
|
|
// support any modifier, force LINEAR. This will force the allocator to
|
|
|
|
// create a buffer with a LINEAR layout instead of an implicit modifier.
|
|
|
|
if (a->len == 0 && b->len == 1 && b->modifiers[0] == DRM_FORMAT_MOD_LINEAR) {
|
2021-10-01 17:22:04 +02:00
|
|
|
return wlr_drm_format_dup(b);
|
|
|
|
}
|
2021-10-01 17:22:04 +02:00
|
|
|
if (b->len == 0 && a->len == 1 && a->modifiers[0] == DRM_FORMAT_MOD_LINEAR) {
|
|
|
|
return wlr_drm_format_dup(a);
|
|
|
|
}
|
2021-10-01 17:22:04 +02:00
|
|
|
|
2020-11-25 15:58:37 +01:00
|
|
|
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;
|
|
|
|
format->cap = format_cap;
|
|
|
|
|
|
|
|
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]) {
|
|
|
|
assert(format->len < format->cap);
|
|
|
|
format->modifiers[format->len] = a->modifiers[i];
|
|
|
|
format->len++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-01 17:22:04 +02:00
|
|
|
// If both formats support modifiers, but the intersection is empty, then
|
|
|
|
// the formats aren't compatible with each other
|
|
|
|
if (format->len == 0 && a->len > 0 && b->len > 0) {
|
2020-11-25 15:58:37 +01:00
|
|
|
free(format);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return format;
|
|
|
|
}
|