drm-formats: save result of intersection in the first array

In the current API, we have some set operations: join, intersect and
subtract. Both join and subtract receives two DRM format arrays and save
the result in the first one.

For the intersection we have a slightly different approach, what makes
the API weird. We don't save the result in the arguments, instead we
return a new array with the result.

Modify weston_drm_format_array_intersect() in order to make it similar
to the other two set operations.

Signed-off-by: Leandro Ribeiro <leandro.ribeiro@collabora.com>
Reviewed-by: Alexandros Frantzis <alexandros.frantzis@collabora.com>
dev
Leandro Ribeiro 3 years ago committed by Pekka Paalanen
parent ec0b717d77
commit c51d4ad9c8
  1. 24
      libweston/drm-formats.c
  2. 4
      libweston/libweston-internal.h
  3. 65
      tests/drm-formats-test.c

@ -320,17 +320,14 @@ modifiers_intersect(const struct weston_drm_format *fmt_A,
} }
/** /**
* Compute the intersection between two DRM-format arrays * Compute the intersection between two DRM-format arrays, keeping the result in A
* *
* Callers are responsible for destroying the returned array. * @param formats_A The weston_drm_format_array that keeps the result
*
* @param formats_A One of the weston_drm_format_array
* @param formats_B The other weston_drm_format_array * @param formats_B The other weston_drm_format_array
* @return Array with formats and modifiers that are present * @return 0 on success, -1 on failure
* on both A and B, or NULL on failure
*/ */
WL_EXPORT struct weston_drm_format_array * WL_EXPORT int
weston_drm_format_array_intersect(const struct weston_drm_format_array *formats_A, weston_drm_format_array_intersect(struct weston_drm_format_array *formats_A,
const struct weston_drm_format_array *formats_B) const struct weston_drm_format_array *formats_B)
{ {
struct weston_drm_format_array *formats_result; struct weston_drm_format_array *formats_result;
@ -339,7 +336,7 @@ weston_drm_format_array_intersect(const struct weston_drm_format_array *formats_
formats_result = weston_drm_format_array_create(); formats_result = weston_drm_format_array_create();
if (!formats_result) if (!formats_result)
return NULL; return -1;
wl_array_for_each(fmt_A, &formats_A->arr) { wl_array_for_each(fmt_A, &formats_A->arr) {
fmt_B = weston_drm_format_array_find_format(formats_B, fmt_B = weston_drm_format_array_find_format(formats_B,
@ -360,11 +357,16 @@ weston_drm_format_array_intersect(const struct weston_drm_format_array *formats_
weston_drm_format_array_remove_latest_format(formats_result); weston_drm_format_array_remove_latest_format(formats_result);
} }
return formats_result; ret = weston_drm_format_array_replace(formats_A, formats_result);
if (ret < 0)
goto err;
weston_drm_format_array_destroy(formats_result);
return 0;
err: err:
weston_drm_format_array_destroy(formats_result); weston_drm_format_array_destroy(formats_result);
return NULL; return -1;
} }
static int static int

@ -372,8 +372,8 @@ int
weston_drm_format_array_join(struct weston_drm_format_array *formats_A, weston_drm_format_array_join(struct weston_drm_format_array *formats_A,
const struct weston_drm_format_array *formats_B); const struct weston_drm_format_array *formats_B);
struct weston_drm_format_array * int
weston_drm_format_array_intersect(const struct weston_drm_format_array *formats_A, weston_drm_format_array_intersect(struct weston_drm_format_array *formats_A,
const struct weston_drm_format_array *formats_B); const struct weston_drm_format_array *formats_B);
int int

@ -339,12 +339,13 @@ TEST(join_arrays_modifier_invalid)
TEST(intersect_arrays) TEST(intersect_arrays)
{ {
struct weston_drm_format_array *format_array_A, *format_array_B; struct weston_drm_format_array *format_array_A, *format_array_B;
struct weston_drm_format_array *format_array_C, *format_array_result; struct weston_drm_format_array *format_array_C;
uint32_t formats_A[] = {1, 2, 6, 9, 10}; uint32_t formats_A[] = {1, 2, 6, 9, 10};
uint32_t formats_B[] = {2, 5, 7, 9, 10}; uint32_t formats_B[] = {2, 5, 7, 9, 10};
uint64_t modifiers_A[] = {1, 2, 3, 4, 7}; uint64_t modifiers_A[] = {1, 2, 3, 4, 7};
uint64_t modifiers_B[] = {0, 2, 3, 5, 6}; uint64_t modifiers_B[] = {0, 2, 3, 5, 6};
uint64_t modifiers_intersect[] = {2, 3}; uint64_t modifiers_intersect[] = {2, 3};
int ret;
format_array_A = weston_drm_format_array_create(); format_array_A = weston_drm_format_array_create();
format_array_B = weston_drm_format_array_create(); format_array_B = weston_drm_format_array_create();
@ -355,26 +356,24 @@ TEST(intersect_arrays)
ADD_FORMATS_AND_MODS(format_array_A, formats_A, modifiers_A); ADD_FORMATS_AND_MODS(format_array_A, formats_A, modifiers_A);
ADD_FORMATS_AND_MODS(format_array_B, formats_B, modifiers_B); ADD_FORMATS_AND_MODS(format_array_B, formats_B, modifiers_B);
format_array_result = weston_drm_format_array_intersect(format_array_A, ret = weston_drm_format_array_intersect(format_array_A, format_array_B);
format_array_B); assert(ret == 0);
assert(format_array_result);
/* The result of the intersection should have the same content as C. */ /* The result of the intersection (stored in A) should have the same
* content as C. */
ADD_FORMATS_AND_MODS(format_array_C, (uint32_t[]){2}, modifiers_intersect); ADD_FORMATS_AND_MODS(format_array_C, (uint32_t[]){2}, modifiers_intersect);
ADD_FORMATS_AND_MODS(format_array_C, (uint32_t[]){9}, modifiers_intersect); ADD_FORMATS_AND_MODS(format_array_C, (uint32_t[]){9}, modifiers_intersect);
ADD_FORMATS_AND_MODS(format_array_C, (uint32_t[]){10}, modifiers_intersect); ADD_FORMATS_AND_MODS(format_array_C, (uint32_t[]){10}, modifiers_intersect);
assert(weston_drm_format_array_equal(format_array_result, format_array_C)); assert(weston_drm_format_array_equal(format_array_A, format_array_C));
weston_drm_format_array_destroy(format_array_A); weston_drm_format_array_destroy(format_array_A);
weston_drm_format_array_destroy(format_array_B); weston_drm_format_array_destroy(format_array_B);
weston_drm_format_array_destroy(format_array_C); weston_drm_format_array_destroy(format_array_C);
weston_drm_format_array_destroy(format_array_result);
} }
TEST(intersect_arrays_same_content) TEST(intersect_arrays_same_content)
{ {
struct weston_drm_format_array *format_array_A, *format_array_B; struct weston_drm_format_array *format_array_A, *format_array_B;
struct weston_drm_format_array *format_array_result;
uint32_t formats[] = {1, 2, 3, 4, 5}; uint32_t formats[] = {1, 2, 3, 4, 5};
uint64_t modifiers[] = {11, 12, 13, 14, 15}; uint64_t modifiers[] = {11, 12, 13, 14, 15};
int ret; int ret;
@ -386,36 +385,31 @@ TEST(intersect_arrays_same_content)
/* The intersection between two empty arrays must be an /* The intersection between two empty arrays must be an
* empty array. */ * empty array. */
format_array_result = weston_drm_format_array_intersect(format_array_A, ret = weston_drm_format_array_intersect(format_array_A, format_array_B);
format_array_B); assert(ret == 0);
assert(format_array_result); assert(format_array_A->arr.size == 0);
assert(format_array_result->arr.size == 0);
weston_drm_format_array_destroy(format_array_result);
/* DRM-format arrays A and B have the same content, so the intersection /* DRM-format arrays A and B have the same content, so the intersection
* should be equal to them. */ * should be equal to them. A keeps the result of the intersection, and B
* does not change. So we compare them. */
ADD_FORMATS_AND_MODS(format_array_A, formats, modifiers); ADD_FORMATS_AND_MODS(format_array_A, formats, modifiers);
ret = weston_drm_format_array_replace(format_array_B, format_array_A); ret = weston_drm_format_array_replace(format_array_B, format_array_A);
assert(ret == 0); assert(ret == 0);
format_array_result = weston_drm_format_array_intersect(format_array_A, ret = weston_drm_format_array_intersect(format_array_A, format_array_B);
format_array_B); assert(ret == 0);
assert(format_array_result); assert(weston_drm_format_array_equal(format_array_A, format_array_B));
assert(weston_drm_format_array_equal(format_array_result,
format_array_A));
weston_drm_format_array_destroy(format_array_A); weston_drm_format_array_destroy(format_array_A);
weston_drm_format_array_destroy(format_array_B); weston_drm_format_array_destroy(format_array_B);
weston_drm_format_array_destroy(format_array_result);
} }
TEST(intersect_arrays_exclusive_formats) TEST(intersect_arrays_exclusive_formats)
{ {
struct weston_drm_format_array *format_array_A, *format_array_B; struct weston_drm_format_array *format_array_A, *format_array_B;
struct weston_drm_format_array *format_array_result;
uint64_t formats_A[] = {1, 2, 3, 4, 5}; uint64_t formats_A[] = {1, 2, 3, 4, 5};
uint64_t formats_B[] = {6, 7, 8, 9, 10}; uint64_t formats_B[] = {6, 7, 8, 9, 10};
uint64_t modifiers[] = {11, 12, 13, 14, 15}; uint64_t modifiers[] = {11, 12, 13, 14, 15};
int ret;
format_array_A = weston_drm_format_array_create(); format_array_A = weston_drm_format_array_create();
format_array_B = weston_drm_format_array_create(); format_array_B = weston_drm_format_array_create();
@ -423,25 +417,23 @@ TEST(intersect_arrays_exclusive_formats)
assert(format_array_B); assert(format_array_B);
/* DRM-format arrays A and B have formats that are mutually exclusive, /* DRM-format arrays A and B have formats that are mutually exclusive,
* so the intersection must be empty. */ * so the intersection (which is stored in A) must be empty. */
ADD_FORMATS_AND_MODS(format_array_A, formats_A, modifiers); ADD_FORMATS_AND_MODS(format_array_A, formats_A, modifiers);
ADD_FORMATS_AND_MODS(format_array_B, formats_B, modifiers); ADD_FORMATS_AND_MODS(format_array_B, formats_B, modifiers);
format_array_result = weston_drm_format_array_intersect(format_array_A, ret = weston_drm_format_array_intersect(format_array_A, format_array_B);
format_array_B); assert(ret == 0);
assert(format_array_result); assert(format_array_A->arr.size == 0);
assert(format_array_result->arr.size == 0);
weston_drm_format_array_destroy(format_array_A); weston_drm_format_array_destroy(format_array_A);
weston_drm_format_array_destroy(format_array_B); weston_drm_format_array_destroy(format_array_B);
weston_drm_format_array_destroy(format_array_result);
} }
TEST(intersect_arrays_exclusive_modifiers) TEST(intersect_arrays_exclusive_modifiers)
{ {
struct weston_drm_format_array *format_array_A, *format_array_B; struct weston_drm_format_array *format_array_A, *format_array_B;
struct weston_drm_format_array *format_array_result;
uint64_t modifiers_A[] = {1, 2, 3, 4, 5}; uint64_t modifiers_A[] = {1, 2, 3, 4, 5};
uint64_t modifiers_B[] = {6, 7, 8, 9, 10}; uint64_t modifiers_B[] = {6, 7, 8, 9, 10};
int ret;
format_array_A = weston_drm_format_array_create(); format_array_A = weston_drm_format_array_create();
format_array_B = weston_drm_format_array_create(); format_array_B = weston_drm_format_array_create();
@ -449,19 +441,18 @@ TEST(intersect_arrays_exclusive_modifiers)
assert(format_array_B); assert(format_array_B);
/* Both DRM-format arrays A and B have the same format but with modifier /* Both DRM-format arrays A and B have the same format but with modifier
* sets that are mutually exclusive. The intersection between mutually * sets that are mutually exclusive. The intersection (which is stored
* exclusive modifier must be empty, and so the format should not be * in A) between mutually exclusive modifier must be empty, and so the
* added to the array. So the array must also be empty. */ * format should not be added to the array. So the array must also be
* empty. */
ADD_FORMATS_AND_MODS(format_array_A, (uint32_t[]){1}, modifiers_A); ADD_FORMATS_AND_MODS(format_array_A, (uint32_t[]){1}, modifiers_A);
ADD_FORMATS_AND_MODS(format_array_B, (uint32_t[]){1}, modifiers_B); ADD_FORMATS_AND_MODS(format_array_B, (uint32_t[]){1}, modifiers_B);
format_array_result = weston_drm_format_array_intersect(format_array_A, ret = weston_drm_format_array_intersect(format_array_A, format_array_B);
format_array_B); assert(ret == 0);
assert(format_array_result); assert(format_array_A->arr.size == 0);
assert(format_array_result->arr.size == 0);
weston_drm_format_array_destroy(format_array_A); weston_drm_format_array_destroy(format_array_A);
weston_drm_format_array_destroy(format_array_B); weston_drm_format_array_destroy(format_array_B);
weston_drm_format_array_destroy(format_array_result);
} }
TEST(subtract_arrays) TEST(subtract_arrays)

Loading…
Cancel
Save