wlr_drm_format_set: Store formats on array

This commit is contained in:
Alexander Orzechowski 2023-05-11 03:53:11 -04:00 committed by Simon Ser
parent 90d08f8f1c
commit 300bd80772
6 changed files with 37 additions and 53 deletions

View File

@ -271,7 +271,7 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display,
// Forbid implicit modifiers, because their meaning changes from one // Forbid implicit modifiers, because their meaning changes from one
// GPU to another. // GPU to another.
for (size_t i = 0; i < texture_formats->len; i++) { for (size_t i = 0; i < texture_formats->len; i++) {
const struct wlr_drm_format *fmt = texture_formats->formats[i]; const struct wlr_drm_format *fmt = &texture_formats->formats[i];
for (size_t j = 0; j < fmt->len; j++) { for (size_t j = 0; j < fmt->len; j++) {
uint64_t mod = fmt->modifiers[j]; uint64_t mod = fmt->modifiers[j];
if (mod == DRM_FORMAT_MOD_INVALID) { if (mod == DRM_FORMAT_MOD_INVALID) {

View File

@ -265,7 +265,7 @@ static void update_layer_feedback(struct wlr_drm_backend *drm,
} }
for (size_t j = 0; j < plane->formats.len; j++) { for (size_t j = 0; j < plane->formats.len; j++) {
const struct wlr_drm_format *format = plane->formats.formats[j]; const struct wlr_drm_format *format = &plane->formats.formats[j];
for (size_t k = 0; k < format->len; k++) { for (size_t k = 0; k < format->len; k++) {
wlr_drm_format_set_add(&formats, format->format, wlr_drm_format_set_add(&formats, format->format,
format->modifiers[k]); format->modifiers[k]);

View File

@ -54,7 +54,7 @@ struct wlr_drm_format_set {
// The capacity of the array; private to wlroots // The capacity of the array; private to wlroots
size_t capacity; size_t capacity;
// A pointer to an array of `struct wlr_drm_format *` of length `len`. // A pointer to an array of `struct wlr_drm_format *` of length `len`.
struct wlr_drm_format **formats; struct wlr_drm_format *formats;
}; };
/** /**

View File

@ -18,8 +18,7 @@ void wlr_drm_format_finish(struct wlr_drm_format *format) {
void wlr_drm_format_set_finish(struct wlr_drm_format_set *set) { void wlr_drm_format_set_finish(struct wlr_drm_format_set *set) {
for (size_t i = 0; i < set->len; ++i) { for (size_t i = 0; i < set->len; ++i) {
wlr_drm_format_finish(set->formats[i]); wlr_drm_format_finish(&set->formats[i]);
free(set->formats[i]);
} }
free(set->formats); free(set->formats);
@ -28,10 +27,10 @@ void wlr_drm_format_set_finish(struct wlr_drm_format_set *set) {
set->formats = NULL; set->formats = NULL;
} }
static struct wlr_drm_format **format_set_get_ref(struct wlr_drm_format_set *set, static struct wlr_drm_format *format_set_get(const struct wlr_drm_format_set *set,
uint32_t format) { uint32_t format) {
for (size_t i = 0; i < set->len; ++i) { for (size_t i = 0; i < set->len; ++i) {
if (set->formats[i]->format == format) { if (set->formats[i].format == format) {
return &set->formats[i]; return &set->formats[i];
} }
} }
@ -41,9 +40,7 @@ static struct wlr_drm_format **format_set_get_ref(struct wlr_drm_format_set *set
const struct wlr_drm_format *wlr_drm_format_set_get( const struct wlr_drm_format *wlr_drm_format_set_get(
const struct wlr_drm_format_set *set, uint32_t format) { const struct wlr_drm_format_set *set, uint32_t format) {
struct wlr_drm_format **ptr = return format_set_get(set, format);
format_set_get_ref((struct wlr_drm_format_set *)set, format);
return ptr ? *ptr : NULL;
} }
bool wlr_drm_format_set_has(const struct wlr_drm_format_set *set, bool wlr_drm_format_set_has(const struct wlr_drm_format_set *set,
@ -59,35 +56,29 @@ bool wlr_drm_format_set_add(struct wlr_drm_format_set *set, uint32_t format,
uint64_t modifier) { uint64_t modifier) {
assert(format != DRM_FORMAT_INVALID); assert(format != DRM_FORMAT_INVALID);
struct wlr_drm_format **ptr = format_set_get_ref(set, format); struct wlr_drm_format *existing = format_set_get(set, format);
if (ptr) { if (existing) {
return wlr_drm_format_add(*ptr, modifier); return wlr_drm_format_add(existing, modifier);
} }
struct wlr_drm_format *fmt = calloc(1, sizeof(*fmt)); struct wlr_drm_format fmt;
if (!fmt) { wlr_drm_format_init(&fmt, format);
return false; if (!wlr_drm_format_add(&fmt, modifier)) {
} wlr_drm_format_finish(&fmt);
wlr_drm_format_init(fmt, format);
if (!wlr_drm_format_add(fmt, modifier)) {
wlr_drm_format_finish(fmt);
return false; return false;
} }
if (set->len == set->capacity) { if (set->len == set->capacity) {
size_t new = set->capacity ? set->capacity * 2 : 4; size_t capacity = set->capacity ? set->capacity * 2 : 4;
struct wlr_drm_format **tmp = realloc(set->formats, struct wlr_drm_format *fmts = realloc(set->formats, sizeof(*fmts) * capacity);
sizeof(set->formats[0]) * new); if (!fmts) {
if (!tmp) {
wlr_log_errno(WLR_ERROR, "Allocation failed"); wlr_log_errno(WLR_ERROR, "Allocation failed");
free(fmt);
return false; return false;
} }
set->capacity = new; set->capacity = capacity;
set->formats = tmp; set->formats = fmts;
} }
set->formats[set->len++] = fmt; set->formats[set->len++] = fmt;
@ -149,7 +140,7 @@ bool wlr_drm_format_copy(struct wlr_drm_format *dst, const struct wlr_drm_format
} }
bool wlr_drm_format_set_copy(struct wlr_drm_format_set *dst, const struct wlr_drm_format_set *src) { 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])); struct wlr_drm_format *formats = malloc(src->len * sizeof(formats[0]));
if (formats == NULL) { if (formats == NULL) {
return false; return false;
} }
@ -168,13 +159,12 @@ bool wlr_drm_format_set_copy(struct wlr_drm_format_set *dst, const struct wlr_dr
return false; return false;
} }
wlr_drm_format_copy(fmt, src->formats[i]); out.formats[out.len] = (struct wlr_drm_format){0};
if (!wlr_drm_format_copy(&out.formats[out.len], &src->formats[i])) {
out.formats[out.len] = fmt;
if (out.formats[out.len] == NULL) {
wlr_drm_format_set_finish(&out); wlr_drm_format_set_finish(&out);
return false; return false;
} }
out.len++; out.len++;
} }
@ -219,7 +209,7 @@ 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) { const struct wlr_drm_format_set *a, const struct wlr_drm_format_set *b) {
struct wlr_drm_format_set out = {0}; struct wlr_drm_format_set out = {0};
out.capacity = a->len < b->len ? a->len : b->len; out.capacity = a->len < b->len ? a->len : b->len;
out.formats = calloc(out.capacity, sizeof(struct wlr_drm_format *)); out.formats = malloc(sizeof(*out.formats) * out.capacity);
if (out.formats == NULL) { if (out.formats == NULL) {
wlr_log_errno(WLR_ERROR, "Allocation failed"); wlr_log_errno(WLR_ERROR, "Allocation failed");
return false; return false;
@ -227,25 +217,19 @@ bool wlr_drm_format_set_intersect(struct wlr_drm_format_set *dst,
for (size_t i = 0; i < a->len; i++) { for (size_t i = 0; i < a->len; i++) {
for (size_t j = 0; j < b->len; j++) { for (size_t j = 0; j < b->len; j++) {
if (a->formats[i]->format == b->formats[j]->format) { if (a->formats[i].format == b->formats[j].format) {
// When the two formats have no common modifier, keep // When the two formats have no common modifier, keep
// intersecting the rest of the formats: they may be compatible // intersecting the rest of the formats: they may be compatible
// with each other // with each other
struct wlr_drm_format *format = calloc(1, sizeof(*format)); if (!wlr_drm_format_intersect(&out.formats[out.len],
if (!format) { &a->formats[i], &b->formats[j])) {
wlr_drm_format_set_finish(&out); wlr_drm_format_set_finish(&out);
return false; return false;
} }
if (!wlr_drm_format_intersect(format, a->formats[i], b->formats[j])) { if (out.formats[out.len].len == 0) {
wlr_drm_format_set_finish(&out); wlr_drm_format_finish(&out.formats[out.len]);
return false;
}
if (format->len == 0) {
wlr_drm_format_finish(format);
} else { } else {
out.formats[out.len] = format;
out.len++; out.len++;
} }
@ -267,7 +251,7 @@ bool wlr_drm_format_set_intersect(struct wlr_drm_format_set *dst,
static bool drm_format_set_extend(struct wlr_drm_format_set *dst, static bool drm_format_set_extend(struct wlr_drm_format_set *dst,
const struct wlr_drm_format_set *src) { const struct wlr_drm_format_set *src) {
for (size_t i = 0; i < src->len; i++) { for (size_t i = 0; i < src->len; i++) {
struct wlr_drm_format *format = src->formats[i]; struct wlr_drm_format *format = &src->formats[i];
for (size_t j = 0; j < format->len; j++) { for (size_t j = 0; j < format->len; j++) {
if (!wlr_drm_format_set_add(dst, format->format, format->modifiers[j])) { if (!wlr_drm_format_set_add(dst, format->format, format->modifiers[j])) {
wlr_log_errno(WLR_ERROR, "Adding format/modifier to set failed"); wlr_log_errno(WLR_ERROR, "Adding format/modifier to set failed");
@ -283,7 +267,7 @@ 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) { const struct wlr_drm_format_set *a, const struct wlr_drm_format_set *b) {
struct wlr_drm_format_set out = {0}; struct wlr_drm_format_set out = {0};
out.capacity = a->len + b->len; out.capacity = a->len + b->len;
out.formats = calloc(out.capacity, sizeof(struct wlr_drm_format *)); out.formats = malloc(sizeof(*out.formats) * out.capacity);
if (out.formats == NULL) { if (out.formats == NULL) {
wlr_log_errno(WLR_ERROR, "Allocation failed"); wlr_log_errno(WLR_ERROR, "Allocation failed");
return false; return false;

View File

@ -161,7 +161,7 @@ static void drm_bind(struct wl_client *client, void *data,
wl_drm_send_capabilities(resource, WL_DRM_CAPABILITY_PRIME); wl_drm_send_capabilities(resource, WL_DRM_CAPABILITY_PRIME);
for (size_t i = 0; i < drm->formats.len; i++) { for (size_t i = 0; i < drm->formats.len; i++) {
const struct wlr_drm_format *fmt = drm->formats.formats[i]; const struct wlr_drm_format *fmt = &drm->formats.formats[i];
if (wlr_drm_format_has(fmt, DRM_FORMAT_MOD_INVALID)) { if (wlr_drm_format_has(fmt, DRM_FORMAT_MOD_INVALID)) {
wl_drm_send_format(resource, fmt->format); wl_drm_send_format(resource, fmt->format);
} }

View File

@ -476,7 +476,7 @@ static ssize_t get_drm_format_set_index(const struct wlr_drm_format_set *set,
const struct wlr_drm_format *fmt; const struct wlr_drm_format *fmt;
size_t idx = 0; size_t idx = 0;
for (size_t i = 0; i < set->len; i++) { for (size_t i = 0; i < set->len; i++) {
fmt = set->formats[i]; fmt = &set->formats[i];
if (fmt->format == format) { if (fmt->format == format) {
format_found = true; format_found = true;
break; break;
@ -508,7 +508,7 @@ static struct wlr_linux_dmabuf_feedback_v1_compiled *feedback_compile(
size_t table_len = 0; size_t table_len = 0;
for (size_t i = 0; i < fallback_tranche->formats.len; i++) { for (size_t i = 0; i < fallback_tranche->formats.len; i++) {
const struct wlr_drm_format *fmt = fallback_tranche->formats.formats[i]; const struct wlr_drm_format *fmt = &fallback_tranche->formats.formats[i];
table_len += fmt->len; table_len += fmt->len;
} }
assert(table_len > 0); assert(table_len > 0);
@ -534,7 +534,7 @@ static struct wlr_linux_dmabuf_feedback_v1_compiled *feedback_compile(
size_t n = 0; size_t n = 0;
for (size_t i = 0; i < fallback_tranche->formats.len; i++) { for (size_t i = 0; i < fallback_tranche->formats.len; i++) {
const struct wlr_drm_format *fmt = fallback_tranche->formats.formats[i]; const struct wlr_drm_format *fmt = &fallback_tranche->formats.formats[i];
for (size_t k = 0; k < fmt->len; k++) { for (size_t k = 0; k < fmt->len; k++) {
table[n] = (struct wlr_linux_dmabuf_feedback_v1_table_entry){ table[n] = (struct wlr_linux_dmabuf_feedback_v1_table_entry){
@ -579,7 +579,7 @@ static struct wlr_linux_dmabuf_feedback_v1_compiled *feedback_compile(
n = 0; n = 0;
uint16_t *indices = compiled_tranche->indices.data; uint16_t *indices = compiled_tranche->indices.data;
for (size_t j = 0; j < tranche->formats.len; j++) { for (size_t j = 0; j < tranche->formats.len; j++) {
const struct wlr_drm_format *fmt = tranche->formats.formats[j]; const struct wlr_drm_format *fmt = &tranche->formats.formats[j];
for (size_t k = 0; k < fmt->len; k++) { for (size_t k = 0; k < fmt->len; k++) {
ssize_t index = get_drm_format_set_index( ssize_t index = get_drm_format_set_index(
&fallback_tranche->formats, fmt->format, fmt->modifiers[k]); &fallback_tranche->formats, fmt->format, fmt->modifiers[k]);
@ -820,7 +820,7 @@ static void linux_dmabuf_send_modifiers(struct wl_resource *resource,
static void linux_dmabuf_send_formats(struct wlr_linux_dmabuf_v1 *linux_dmabuf, static void linux_dmabuf_send_formats(struct wlr_linux_dmabuf_v1 *linux_dmabuf,
struct wl_resource *resource) { struct wl_resource *resource) {
for (size_t i = 0; i < linux_dmabuf->default_formats.len; i++) { for (size_t i = 0; i < linux_dmabuf->default_formats.len; i++) {
const struct wlr_drm_format *fmt = linux_dmabuf->default_formats.formats[i]; const struct wlr_drm_format *fmt = &linux_dmabuf->default_formats.formats[i];
linux_dmabuf_send_modifiers(resource, fmt); linux_dmabuf_send_modifiers(resource, fmt);
} }
} }