From 4c6216e8a33118ae5afabc34b9bb793375409dae Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Mon, 13 Nov 2023 14:36:50 +0100 Subject: [PATCH] tests/wayland: Add basic YCbCr ref-tests The test makes sure the YCbCr formats create the expected image and we don't accidentally break it. Like all wayland tests, this is now part of mutter/wayland, mutter/tty, and mutter/kvm and will use either shm or dma-buf depending on which suite is chosen. Part-of: --- src/tests/meson.build | 1 + .../wayland_buffer_ycbcr-basic_0.ref.png | Bin 0 -> 1478 bytes .../wayland_buffer_ycbcr-basic_1.ref.png | Bin 0 -> 1516 bytes .../wayland_buffer_ycbcr-basic_2.ref.png | Bin 0 -> 1478 bytes .../wayland_buffer_ycbcr-basic_3.ref.png | Bin 0 -> 1392 bytes src/tests/wayland-test-clients/meson.build | 3 + src/tests/wayland-test-clients/ycbcr.c | 231 ++++++++++++++++++ src/tests/wayland-unit-tests.c | 12 + 8 files changed, 247 insertions(+) create mode 100644 src/tests/ref-tests/wayland_buffer_ycbcr-basic_0.ref.png create mode 100644 src/tests/ref-tests/wayland_buffer_ycbcr-basic_1.ref.png create mode 100644 src/tests/ref-tests/wayland_buffer_ycbcr-basic_2.ref.png create mode 100644 src/tests/ref-tests/wayland_buffer_ycbcr-basic_3.ref.png create mode 100644 src/tests/wayland-test-clients/ycbcr.c diff --git a/src/tests/meson.build b/src/tests/meson.build index fff12137d..8eae7c64b 100644 --- a/src/tests/meson.build +++ b/src/tests/meson.build @@ -584,6 +584,7 @@ if have_native_tests test_client_executables.get('xdg-apply-limits'), test_client_executables.get('xdg-foreign'), test_client_executables.get('xdg-toplevel-bounds'), + test_client_executables.get('ycbcr'), ], }, ] diff --git a/src/tests/ref-tests/wayland_buffer_ycbcr-basic_0.ref.png b/src/tests/ref-tests/wayland_buffer_ycbcr-basic_0.ref.png new file mode 100644 index 0000000000000000000000000000000000000000..2d24f850e78a8838eddc8c178da30533848faffa GIT binary patch literal 1478 zcmeAS@N?(olHy`uVBq!ia0y~yU}|7sV0^&A1Qgk|*?TjPVoUONcVYMsf(!O8pUl9( zTJ7oL7*a9k?JY+?X+{y2z#oTqPL{D%FE9&il)4wa(P2~SOaGI+`T0Q2qhMe|VD`QJ zzjzPyfB9GTz3%&+;`4X^|M}2=|3}5V|23WW_y4Q;s?K`N;`)WjR(^sSFh)k3S#1!pR{3LdQ;mZ=H`28wdYxQ zRoU4#-uz{!mZ8CEFlE~7Y9PbXfW!CtbrxPtcD98tf6ZE;sCqzP*1GpI7c6{oK*1z7 zUf5)bj6}<_*V#>NQy82xc0tX%P*UrAVM0*?P%XqNpyqj;QeJ#KPM5!$Iy#?fbOfqZ z%kbbd0BU|MZ2&Rl_s56Q1}!WSKt;B~CQ~4c|EwT~0S&PG$HoI;(a0T?c^DFaAa{ZT z5a=(;%)=dkw&&SF5wlYNLzi^(gkMbSEsVh&ZwSW>%&CTOEWw=B5RMIyGqCA+)OKJ9 ce7O6E^;c3sFVdQ&MBb@09d&*OaK4? literal 0 HcmV?d00001 diff --git a/src/tests/ref-tests/wayland_buffer_ycbcr-basic_1.ref.png b/src/tests/ref-tests/wayland_buffer_ycbcr-basic_1.ref.png new file mode 100644 index 0000000000000000000000000000000000000000..5afbb27e8a13e8807eee929ce488cbd7f3743d34 GIT binary patch literal 1516 zcmeAS@N?(olHy`uVBq!ia0y~yU}|7sV0^&A1Qgk|*?TjPVoUONcVYMsf(!O8pUl9( zy3o_bF{EP7+dGbV!j1xM7yDWoR22%f&YgbCk@)be!s<1JbN25}`*ZJ5_mQr&ss5o) zQ+R;pjDi6TfrjL&$iEB)Y5VWh^%)-fv90oN-PftFzJ59ER?|UYtt{rhHWeoO+E)4TpJWU8XCJ8oF|By1PCT@7%?)7GPWrQ8z6BQ z*|eB=9K$Yv6rW(>2?6S4kyycDz!K?*sTiVDC}Dxp0f7`&UXWslHBg=V*3~gA&;?qd zodHs!0(Qa)Ua)zx6-;%pK;M)Bt!wA`@cNl(2hd4YH@K>1-?P`2XPMW?;Cvxb?Llsd z?*W5WhQr0X`_?fW7GdH!|2(%R&^^2%k&Ce{eUts(yaNJi2R_JrTXLOOLQN>aV)yNr zrOa)vEE4Bdd%b`Ag3G{1@j!vw^_O?$W6cw0I5j+s+4izF9%^djoBu7Fr439J4iwaU zUj{TmN+7{v)8FlJ<_Q^Y4G(90yHjq^hN@rF*cfZcU6U<@orMUW`&GX0MOgxE9 zJacXAPS?GWS7S6};<0^l?L^*z3zHZQXYDlI1ayYlfeq*8fLL2p4s39;+69!H$8b32 z^L3~)9SCa%gY&&xP-PRgXhD=Y2eq4iQvS#WbkHQZ^Q)u{=1gG#i9t-8@Pv&=1Sn&* zW+Ko-s*G(a<_QZF4otX`*!zZ!N0-w;C0BX-xA|UN1_PSJM=b}1zz4;DjGVTth1prN Pia~r&S3j3^P6N;`)WjR(^sSFh)k3S#1!pR{3LdQ;mZ=H`28wdYxQ zRoU4#-uz{!mZ8CEFlE~7Y9PbXfW!CtbrxPtcD98tf6ZE;sCqzP*1GpI7c6{oK*1z7 zUf5)bj6}<_*V#>NQy82xc0tX%P*UrAVM0*?P%XqNpyqj;QeJ#KPM5!$Iy#?fbOfqZ z%kbbd0BU|MZ2&Rl_s56Q1}!WSKt;B~CQ~4c|EwT~0S&PG$HoI;(a0T?c^DFaAa{ZT z5a=(;%)=dkw&&SF5wlYNLzi^(gkMbSEsVh&ZwSW>%&CTOEWw=B5RMIyGqCA+)OKJ9 ce7O6E^;c3sFVdQ&MBb@09d&*OaK4? literal 0 HcmV?d00001 diff --git a/src/tests/ref-tests/wayland_buffer_ycbcr-basic_3.ref.png b/src/tests/ref-tests/wayland_buffer_ycbcr-basic_3.ref.png new file mode 100644 index 0000000000000000000000000000000000000000..ed8d6f3243cf056aee895441ad1aeaa97746bee3 GIT binary patch literal 1392 zcmeAS@N?(olHy`uVBq!ia0y~yU}|7sV0^&A1Qgk|*?TjPVoUONcVYMsf(!O8pUl9( zs_ND%>=$X9{5L#xt)rsbAxTbo z>y;q$M!|rFz>Q_Azc6k{_m{6$tDDImp76vcC5TT8VL*>0S7K*OrGCVw|>>C z?H@!pR717h@?q3p{ry8t?_DNwC#VJHFPZlh$Lu}*Z7ZX*F~okolNqmNOARi#L6q#Z z4tc+#B=>;8q6S6<2gbv4YggVgUC)#Y@x+bpucz1XN~kS>NKTs@wAU=5BM9u*#B8go z>ufyf%OP@i>{h+nb9HwOOzVvEg. + */ + +#include "config.h" + +#include +#include +#include +#include + +#include "wayland-test-client-utils.h" + +static WaylandDisplay *display; + +static struct wl_surface *surface; +static struct xdg_surface *xdg_surface; +static struct xdg_toplevel *xdg_toplevel; + +static gboolean waiting_for_configure = FALSE; + +static void +shader_color_gradient (float x, + float y, + float *out_luma, + float *out_cb, + float *out_cr) +{ + *out_luma = 1.0; + *out_cb = x; + *out_cr = y; +} + +static void +shader_luma_gradient (float x, + float y, + float *out_luma, + float *out_cb, + float *out_cr) +{ + *out_luma = (x + y) / 2; + *out_cb = 0.5; + *out_cr = 0.5; +} + +typedef void (*ShaderFunc) (float x, + float y, + float *out_luma, + float *out_cb, + float *out_cr); + +static void +draw (uint32_t drm_format, + ShaderFunc shader) +{ + WaylandBuffer *buffer; + uint8_t *planes[4]; + size_t strides[4]; + int x, y; + +#define BUFFER_WIDTH 64 +#define BUFFER_HEIGHT 64 + + buffer = wayland_buffer_create (display, NULL, + BUFFER_WIDTH, BUFFER_HEIGHT, + drm_format, + NULL, 0, + GBM_BO_USE_LINEAR); + if (!buffer) + g_error ("Failed to create buffer"); + + switch (drm_format) + { + case DRM_FORMAT_YUYV: + planes[0] = wayland_buffer_mmap_plane (buffer, 0, &strides[0]); + break; + case DRM_FORMAT_YUV420: + planes[0] = wayland_buffer_mmap_plane (buffer, 0, &strides[0]); + planes[1] = wayland_buffer_mmap_plane (buffer, 1, &strides[1]); + planes[2] = wayland_buffer_mmap_plane (buffer, 2, &strides[2]); + break; + } + + for (y = 0; y < BUFFER_WIDTH; y++) + { + for (x = 0; x < BUFFER_WIDTH; x++) + { + uint8_t *pixel; + float luma; + float cb; + float cr; + + shader (x / (BUFFER_WIDTH - 1.0), + y / (BUFFER_HEIGHT - 1.0), + &luma, + &cb, + &cr); + + switch (drm_format) + { + case DRM_FORMAT_YUYV: + /* packed [31:0] Cr0:Y1:Cb0:Y0 8:8:8:8 little endian */ + pixel = planes[0] + (y * strides[0]) + (x * 2); + pixel[0] = luma * 255; + if (x % 2 == 0) + pixel[1] = cb * 255; + else + pixel[1] = cr * 255; + break; + case DRM_FORMAT_YUV420: + /* + 3 plane YCbCr + index 0: Y plane, [7:0] Y + index 1: Cb plane, [7:0] Cb + index 2: Cr plane, [7:0] Cr + 2x2 subsampled Cb (1) and Cr (2) planes */ + pixel = planes[0] + (y * strides[0]) + x; + pixel[0] = luma * 255; + pixel = planes[1] + (y / 2 * strides[1]) + x / 2; + pixel[0] = cb * 255; + pixel = planes[2] + (y / 2 * strides[2]) + x / 2; + pixel[0] = cr * 255; + break; + default: + g_assert_not_reached (); + } + } + } + + wl_surface_damage_buffer (surface, 0, 0, BUFFER_WIDTH, BUFFER_HEIGHT); + wl_surface_attach (surface, wayland_buffer_get_wl_buffer (buffer), 0, 0); +} + +static void +handle_xdg_toplevel_configure (void *data, + struct xdg_toplevel *xdg_toplevel, + int32_t width, + int32_t height, + struct wl_array *state) +{ +} + +static void +handle_xdg_toplevel_close (void *data, + struct xdg_toplevel *xdg_toplevel) +{ + g_assert_not_reached (); +} + +static const struct xdg_toplevel_listener xdg_toplevel_listener = { + handle_xdg_toplevel_configure, + handle_xdg_toplevel_close, +}; + +static void +handle_xdg_surface_configure (void *data, + struct xdg_surface *xdg_surface, + uint32_t serial) +{ + xdg_surface_ack_configure (xdg_surface, serial); + + waiting_for_configure = FALSE; +} + +static const struct xdg_surface_listener xdg_surface_listener = { + handle_xdg_surface_configure, +}; + +static void +wait_for_configure (void) +{ + waiting_for_configure = TRUE; + while (waiting_for_configure) + { + if (wl_display_dispatch (display->display) == -1) + exit (EXIT_FAILURE); + } +} + +int +main (int argc, + char **argv) +{ + display = wayland_display_new (WAYLAND_DISPLAY_CAPABILITY_TEST_DRIVER); + + surface = wl_compositor_create_surface (display->compositor); + xdg_surface = xdg_wm_base_get_xdg_surface (display->xdg_wm_base, surface); + xdg_surface_add_listener (xdg_surface, &xdg_surface_listener, NULL); + xdg_toplevel = xdg_surface_get_toplevel (xdg_surface); + xdg_toplevel_add_listener (xdg_toplevel, &xdg_toplevel_listener, NULL); + xdg_toplevel_set_title (xdg_toplevel, "ycbcr"); + xdg_toplevel_set_fullscreen (xdg_toplevel, NULL); + wl_surface_commit (surface); + + wait_for_configure (); + + draw (DRM_FORMAT_YUYV, shader_luma_gradient); + wl_surface_commit (surface); + wait_for_effects_completed (display, surface); + wait_for_view_verified (display, 0); + + draw (DRM_FORMAT_YUYV, shader_color_gradient); + wl_surface_commit (surface); + wait_for_view_verified (display, 1); + + draw (DRM_FORMAT_YUV420, shader_luma_gradient); + wl_surface_commit (surface); + wait_for_view_verified (display, 2); + + draw (DRM_FORMAT_YUV420, shader_color_gradient); + wl_surface_commit (surface); + wait_for_view_verified (display, 3); + + g_clear_pointer (&xdg_toplevel, xdg_toplevel_destroy); + g_clear_pointer (&xdg_surface, xdg_surface_destroy); + + g_clear_object (&display); +} diff --git a/src/tests/wayland-unit-tests.c b/src/tests/wayland-unit-tests.c index aff7515a8..c9f3f32e3 100644 --- a/src/tests/wayland-unit-tests.c +++ b/src/tests/wayland-unit-tests.c @@ -81,6 +81,16 @@ buffer_single_pixel_buffer (void) meta_wayland_test_client_finish (wayland_test_client); } +static void +buffer_ycbcr_basic (void) +{ + MetaWaylandTestClient *wayland_test_client; + + wayland_test_client = + meta_wayland_test_client_new (test_context, "ycbcr"); + meta_wayland_test_client_finish (wayland_test_client); +} + static gboolean set_true (gpointer user_data) { @@ -878,6 +888,8 @@ init_tests (void) buffer_transform); g_test_add_func ("/wayland/buffer/single-pixel-buffer", buffer_single_pixel_buffer); + g_test_add_func ("/wayland/buffer/ycbcr-basic", + buffer_ycbcr_basic); g_test_add_func ("/wayland/idle-inhibit/instant-destroy", idle_inhibit_instant_destroy); g_test_add_func ("/wayland/registry/filter",