From 60c7fee48d29d776f5d0d40539c111c665fef4fe Mon Sep 17 00:00:00 2001 From: Leandro Ribeiro Date: Wed, 6 Oct 2021 12:05:40 -0300 Subject: [PATCH] drm-formats: add weston_drm_format_array_count_pairs() It simply returns the number of format/modifier pairs in the array. This will be useful for the next commits, in which we add support for dma-buf feedback. Signed-off-by: Leandro Ribeiro Reviewed-by: Daniel Stone --- libweston/drm-formats.c | 18 ++++++++++++++++++ libweston/libweston-internal.h | 3 +++ tests/drm-formats-test.c | 5 +++++ 3 files changed, 26 insertions(+) diff --git a/libweston/drm-formats.c b/libweston/drm-formats.c index 491ecd9f..6bc08b5d 100644 --- a/libweston/drm-formats.c +++ b/libweston/drm-formats.c @@ -184,6 +184,24 @@ weston_drm_format_array_find_format(const struct weston_drm_format_array *format return NULL; } +/** + * Counts the number of format/modifier pairs in a weston_drm_format_array + * + * @param formats The weston_drm_format_array + * @return The number of format/modifier pairs in the array + */ +WL_EXPORT unsigned int +weston_drm_format_array_count_pairs(const struct weston_drm_format_array *formats) +{ + struct weston_drm_format *fmt; + unsigned int num_pairs = 0; + + wl_array_for_each(fmt, &formats->arr) + num_pairs += fmt->modifiers.size / sizeof(uint64_t); + + return num_pairs; +} + /** * Compare the content of two weston_drm_format_array * diff --git a/libweston/libweston-internal.h b/libweston/libweston-internal.h index ec6a5952..1603db38 100644 --- a/libweston/libweston-internal.h +++ b/libweston/libweston-internal.h @@ -358,6 +358,9 @@ struct weston_drm_format * weston_drm_format_array_find_format(const struct weston_drm_format_array *formats, uint32_t format); +unsigned int +weston_drm_format_array_count_pairs(const struct weston_drm_format_array *formats); + bool weston_drm_format_array_equal(const struct weston_drm_format_array *formats_A, const struct weston_drm_format_array *formats_B); diff --git a/tests/drm-formats-test.c b/tests/drm-formats-test.c index cfbf3cd8..b4e4a17a 100644 --- a/tests/drm-formats-test.c +++ b/tests/drm-formats-test.c @@ -81,6 +81,8 @@ TEST(basic_operations) weston_drm_format_array_init(&format_array); + assert(weston_drm_format_array_count_pairs(&format_array) == 0); + ADD_FORMATS_AND_MODS(&format_array, formats, modifiers); for (i = 0; i < ARRAY_LENGTH(formats); i++) { @@ -90,6 +92,9 @@ TEST(basic_operations) assert(weston_drm_format_has_modifier(fmt, modifiers[j])); } + assert(weston_drm_format_array_count_pairs(&format_array) == + ARRAY_LENGTH(formats) * ARRAY_LENGTH(modifiers)); + weston_drm_format_array_fini(&format_array); }