From 300bd80772b7fc916275a04dd9c65a44a3c73741 Mon Sep 17 00:00:00 2001 From: Alexander Orzechowski Date: Thu, 11 May 2023 03:53:11 -0400 Subject: [PATCH] wlr_drm_format_set: Store formats on array --- backend/drm/backend.c | 2 +- backend/drm/libliftoff.c | 2 +- include/wlr/render/drm_format_set.h | 2 +- render/drm_format_set.c | 72 +++++++++++------------------ types/wlr_drm.c | 2 +- types/wlr_linux_dmabuf_v1.c | 10 ++-- 6 files changed, 37 insertions(+), 53 deletions(-) diff --git a/backend/drm/backend.c b/backend/drm/backend.c index d061272d..89240352 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -271,7 +271,7 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, // Forbid implicit modifiers, because their meaning changes from one // GPU to another. 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++) { uint64_t mod = fmt->modifiers[j]; if (mod == DRM_FORMAT_MOD_INVALID) { diff --git a/backend/drm/libliftoff.c b/backend/drm/libliftoff.c index 550d4533..a6afb1f9 100644 --- a/backend/drm/libliftoff.c +++ b/backend/drm/libliftoff.c @@ -265,7 +265,7 @@ static void update_layer_feedback(struct wlr_drm_backend *drm, } 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++) { wlr_drm_format_set_add(&formats, format->format, format->modifiers[k]); diff --git a/include/wlr/render/drm_format_set.h b/include/wlr/render/drm_format_set.h index 8ae930be..3d54c6f4 100644 --- a/include/wlr/render/drm_format_set.h +++ b/include/wlr/render/drm_format_set.h @@ -54,7 +54,7 @@ struct wlr_drm_format_set { // The capacity of the array; private to wlroots size_t capacity; // A pointer to an array of `struct wlr_drm_format *` of length `len`. - struct wlr_drm_format **formats; + struct wlr_drm_format *formats; }; /** diff --git a/render/drm_format_set.c b/render/drm_format_set.c index b669074d..5028c1ce 100644 --- a/render/drm_format_set.c +++ b/render/drm_format_set.c @@ -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) { for (size_t i = 0; i < set->len; ++i) { - wlr_drm_format_finish(set->formats[i]); - free(set->formats[i]); + wlr_drm_format_finish(&set->formats[i]); } free(set->formats); @@ -28,10 +27,10 @@ void wlr_drm_format_set_finish(struct wlr_drm_format_set *set) { 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) { 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]; } } @@ -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_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; + return format_set_get(set, format); } 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) { assert(format != DRM_FORMAT_INVALID); - struct wlr_drm_format **ptr = format_set_get_ref(set, format); - if (ptr) { - return wlr_drm_format_add(*ptr, modifier); + struct wlr_drm_format *existing = format_set_get(set, format); + if (existing) { + return wlr_drm_format_add(existing, modifier); } - struct wlr_drm_format *fmt = calloc(1, sizeof(*fmt)); - if (!fmt) { - return false; - } - - wlr_drm_format_init(fmt, format); - if (!wlr_drm_format_add(fmt, modifier)) { - wlr_drm_format_finish(fmt); + struct wlr_drm_format fmt; + wlr_drm_format_init(&fmt, format); + if (!wlr_drm_format_add(&fmt, modifier)) { + wlr_drm_format_finish(&fmt); return false; } 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, - sizeof(set->formats[0]) * new); - if (!tmp) { + struct wlr_drm_format *fmts = realloc(set->formats, sizeof(*fmts) * capacity); + if (!fmts) { wlr_log_errno(WLR_ERROR, "Allocation failed"); - free(fmt); return false; } - set->capacity = new; - set->formats = tmp; + set->capacity = capacity; + set->formats = fmts; } 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) { - 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) { return false; } @@ -168,13 +159,12 @@ bool wlr_drm_format_set_copy(struct wlr_drm_format_set *dst, const struct wlr_dr return false; } - wlr_drm_format_copy(fmt, src->formats[i]); - - out.formats[out.len] = fmt; - if (out.formats[out.len] == NULL) { + out.formats[out.len] = (struct wlr_drm_format){0}; + if (!wlr_drm_format_copy(&out.formats[out.len], &src->formats[i])) { wlr_drm_format_set_finish(&out); return false; } + 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) { 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 *)); + out.formats = malloc(sizeof(*out.formats) * out.capacity); if (out.formats == NULL) { wlr_log_errno(WLR_ERROR, "Allocation failed"); 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 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 // intersecting the rest of the formats: they may be compatible // with each other - struct wlr_drm_format *format = calloc(1, sizeof(*format)); - if (!format) { + if (!wlr_drm_format_intersect(&out.formats[out.len], + &a->formats[i], &b->formats[j])) { wlr_drm_format_set_finish(&out); return false; } - if (!wlr_drm_format_intersect(format, a->formats[i], b->formats[j])) { - wlr_drm_format_set_finish(&out); - return false; - } - - if (format->len == 0) { - wlr_drm_format_finish(format); + if (out.formats[out.len].len == 0) { + wlr_drm_format_finish(&out.formats[out.len]); } else { - out.formats[out.len] = format; 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, const struct wlr_drm_format_set *src) { 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++) { if (!wlr_drm_format_set_add(dst, format->format, format->modifiers[j])) { 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) { struct wlr_drm_format_set out = {0}; 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) { wlr_log_errno(WLR_ERROR, "Allocation failed"); return false; diff --git a/types/wlr_drm.c b/types/wlr_drm.c index 34cb52ad..2be70631 100644 --- a/types/wlr_drm.c +++ b/types/wlr_drm.c @@ -161,7 +161,7 @@ static void drm_bind(struct wl_client *client, void *data, wl_drm_send_capabilities(resource, WL_DRM_CAPABILITY_PRIME); 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)) { wl_drm_send_format(resource, fmt->format); } diff --git a/types/wlr_linux_dmabuf_v1.c b/types/wlr_linux_dmabuf_v1.c index 7967d609..cc7011f3 100644 --- a/types/wlr_linux_dmabuf_v1.c +++ b/types/wlr_linux_dmabuf_v1.c @@ -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; size_t idx = 0; for (size_t i = 0; i < set->len; i++) { - fmt = set->formats[i]; + fmt = &set->formats[i]; if (fmt->format == format) { format_found = true; break; @@ -508,7 +508,7 @@ static struct wlr_linux_dmabuf_feedback_v1_compiled *feedback_compile( size_t table_len = 0; 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; } assert(table_len > 0); @@ -534,7 +534,7 @@ static struct wlr_linux_dmabuf_feedback_v1_compiled *feedback_compile( size_t n = 0; 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++) { 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; uint16_t *indices = compiled_tranche->indices.data; 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++) { ssize_t index = get_drm_format_set_index( &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, struct wl_resource *resource) { 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); } }