Pekka Paalanen | ae12b95 | 2020-12-09 15:14:11 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2020 Collabora, Ltd. |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <sys/mman.h> |
| 31 | #include <math.h> |
| 32 | #include <unistd.h> |
| 33 | |
| 34 | #include <drm_fourcc.h> |
| 35 | |
| 36 | #include "weston-test-client-helper.h" |
| 37 | #include "weston-test-fixture-compositor.h" |
| 38 | #include "shared/os-compatibility.h" |
| 39 | #include "shared/xalloc.h" |
| 40 | |
| 41 | static enum test_result_code |
| 42 | fixture_setup(struct weston_test_harness *harness) |
| 43 | { |
| 44 | struct compositor_setup setup; |
| 45 | |
| 46 | compositor_setup_defaults(&setup); |
| 47 | setup.renderer = RENDERER_GL; |
| 48 | setup.width = 324; |
| 49 | setup.height = 264; |
| 50 | setup.shell = SHELL_TEST_DESKTOP; |
Harish Krupo | 7ef2688 | 2019-04-18 21:45:48 +0530 | [diff] [blame] | 51 | setup.logging_scopes = "log,gl-shader-generator"; |
Pekka Paalanen | ae12b95 | 2020-12-09 15:14:11 +0200 | [diff] [blame] | 52 | |
| 53 | return weston_test_harness_execute_as_client(harness, &setup); |
| 54 | } |
| 55 | DECLARE_FIXTURE_SETUP(fixture_setup); |
| 56 | |
| 57 | struct yuv_buffer { |
| 58 | void *data; |
| 59 | size_t bytes; |
| 60 | struct wl_buffer *proxy; |
| 61 | int width; |
| 62 | int height; |
| 63 | }; |
| 64 | |
| 65 | struct yuv_case { |
| 66 | uint32_t drm_format; |
| 67 | const char *drm_format_name; |
| 68 | struct yuv_buffer *(*create_buffer)(struct client *client, |
| 69 | uint32_t drm_format, |
| 70 | pixman_image_t *rgb_image); |
| 71 | }; |
| 72 | |
| 73 | static struct yuv_buffer * |
| 74 | yuv_buffer_create(struct client *client, |
| 75 | size_t bytes, |
| 76 | int width, |
| 77 | int height, |
| 78 | int stride_bytes, |
| 79 | uint32_t drm_format) |
| 80 | { |
| 81 | struct wl_shm_pool *pool; |
| 82 | struct yuv_buffer *buf; |
| 83 | int fd; |
| 84 | |
| 85 | buf = xzalloc(sizeof *buf); |
| 86 | buf->bytes = bytes; |
| 87 | buf->width = width; |
| 88 | buf->height = height; |
| 89 | |
| 90 | fd = os_create_anonymous_file(buf->bytes); |
| 91 | assert(fd >= 0); |
| 92 | |
| 93 | buf->data = mmap(NULL, buf->bytes, |
| 94 | PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| 95 | if (buf->data == MAP_FAILED) { |
| 96 | close(fd); |
| 97 | assert(buf->data != MAP_FAILED); |
| 98 | } |
| 99 | |
| 100 | pool = wl_shm_create_pool(client->wl_shm, fd, buf->bytes); |
| 101 | buf->proxy = wl_shm_pool_create_buffer(pool, 0, buf->width, buf->height, |
| 102 | stride_bytes, drm_format); |
| 103 | wl_shm_pool_destroy(pool); |
| 104 | close(fd); |
| 105 | |
| 106 | return buf; |
| 107 | } |
| 108 | |
| 109 | static void |
| 110 | yuv_buffer_destroy(struct yuv_buffer *buf) |
| 111 | { |
| 112 | wl_buffer_destroy(buf->proxy); |
| 113 | assert(munmap(buf->data, buf->bytes) == 0); |
| 114 | free(buf); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Based on Rec. ITU-R BT.601-7 |
| 119 | * |
| 120 | * This is intended to be obvious and accurate, not fast. |
| 121 | */ |
| 122 | static void |
| 123 | x8r8g8b8_to_ycbcr8_bt601(uint32_t xrgb, |
| 124 | uint8_t *y_out, uint8_t *cb_out, uint8_t *cr_out) |
| 125 | { |
| 126 | double y, cb, cr; |
| 127 | double r = (xrgb >> 16) & 0xff; |
| 128 | double g = (xrgb >> 8) & 0xff; |
| 129 | double b = (xrgb >> 0) & 0xff; |
| 130 | |
| 131 | /* normalize to [0.0, 1.0] */ |
| 132 | r /= 255.0; |
| 133 | g /= 255.0; |
| 134 | b /= 255.0; |
| 135 | |
| 136 | /* Y normalized to [0.0, 1.0], Cb and Cr [-0.5, 0.5] */ |
| 137 | y = 0.299 * r + 0.587 * g + 0.114 * b; |
| 138 | cr = (r - y) / 1.402; |
| 139 | cb = (b - y) / 1.772; |
| 140 | |
| 141 | /* limited range quantization to 8 bit */ |
| 142 | *y_out = round(219.0 * y + 16.0); |
| 143 | if (cr_out) |
| 144 | *cr_out = round(224.0 * cr + 128.0); |
| 145 | if (cb_out) |
| 146 | *cb_out = round(224.0 * cb + 128.0); |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * 3 plane YCbCr |
| 151 | * plane 0: Y plane, [7:0] Y |
| 152 | * plane 1: Cb plane, [7:0] Cb |
| 153 | * plane 2: Cr plane, [7:0] Cr |
| 154 | * 2x2 subsampled Cb (1) and Cr (2) planes |
| 155 | */ |
| 156 | static struct yuv_buffer * |
| 157 | yuv420_create_buffer(struct client *client, |
| 158 | uint32_t drm_format, |
| 159 | pixman_image_t *rgb_image) |
| 160 | { |
| 161 | struct yuv_buffer *buf; |
| 162 | size_t bytes; |
| 163 | int width; |
| 164 | int height; |
| 165 | int x, y; |
| 166 | void *rgb_pixels; |
| 167 | int rgb_stride_bytes; |
| 168 | uint32_t *rgb_row; |
| 169 | uint8_t *y_base; |
| 170 | uint8_t *u_base; |
| 171 | uint8_t *v_base; |
| 172 | uint8_t *y_row; |
| 173 | uint8_t *u_row; |
| 174 | uint8_t *v_row; |
| 175 | uint32_t argb; |
| 176 | |
| 177 | assert(drm_format == DRM_FORMAT_YUV420); |
| 178 | |
| 179 | width = pixman_image_get_width(rgb_image); |
| 180 | height = pixman_image_get_height(rgb_image); |
| 181 | rgb_pixels = pixman_image_get_data(rgb_image); |
| 182 | rgb_stride_bytes = pixman_image_get_stride(rgb_image); |
| 183 | |
| 184 | /* Full size Y, quarter U and V */ |
| 185 | bytes = width * height + (width / 2) * (height / 2) * 2; |
| 186 | buf = yuv_buffer_create(client, bytes, width, height, width, drm_format); |
| 187 | |
| 188 | y_base = buf->data; |
| 189 | u_base = y_base + width * height; |
| 190 | v_base = u_base + (width / 2) * (height / 2); |
| 191 | |
| 192 | for (y = 0; y < height; y++) { |
| 193 | rgb_row = rgb_pixels + (y / 2 * 2) * rgb_stride_bytes; |
| 194 | y_row = y_base + y * width; |
| 195 | u_row = u_base + (y / 2) * (width / 2); |
| 196 | v_row = v_base + (y / 2) * (width / 2); |
| 197 | |
| 198 | for (x = 0; x < width; x++) { |
| 199 | /* |
| 200 | * Sub-sample the source image instead, so that U and V |
| 201 | * sub-sampling does not require proper |
| 202 | * filtering/averaging/siting. |
| 203 | */ |
| 204 | argb = *(rgb_row + x / 2 * 2); |
| 205 | |
| 206 | /* |
| 207 | * A stupid way of "sub-sampling" chroma. This does not |
| 208 | * do the necessary filtering/averaging/siting or |
| 209 | * alternate Cb/Cr rows. |
| 210 | */ |
| 211 | if ((y & 1) == 0 && (x & 1) == 0) { |
| 212 | x8r8g8b8_to_ycbcr8_bt601(argb, y_row + x, |
| 213 | u_row + x / 2, |
| 214 | v_row + x / 2); |
| 215 | } else { |
| 216 | x8r8g8b8_to_ycbcr8_bt601(argb, y_row + x, |
| 217 | NULL, NULL); |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return buf; |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * 2 plane YCbCr |
| 227 | * plane 0 = Y plane, [7:0] Y |
| 228 | * plane 1 = Cr:Cb plane, [15:0] Cr:Cb little endian |
| 229 | * 2x2 subsampled Cr:Cb plane |
| 230 | */ |
| 231 | static struct yuv_buffer * |
| 232 | nv12_create_buffer(struct client *client, |
| 233 | uint32_t drm_format, |
| 234 | pixman_image_t *rgb_image) |
| 235 | { |
| 236 | struct yuv_buffer *buf; |
| 237 | size_t bytes; |
| 238 | int width; |
| 239 | int height; |
| 240 | int x, y; |
| 241 | void *rgb_pixels; |
| 242 | int rgb_stride_bytes; |
| 243 | uint32_t *rgb_row; |
| 244 | uint8_t *y_base; |
| 245 | uint16_t *uv_base; |
| 246 | uint8_t *y_row; |
| 247 | uint16_t *uv_row; |
| 248 | uint32_t argb; |
| 249 | uint8_t cr; |
| 250 | uint8_t cb; |
| 251 | |
| 252 | assert(drm_format == DRM_FORMAT_NV12); |
| 253 | |
| 254 | width = pixman_image_get_width(rgb_image); |
| 255 | height = pixman_image_get_height(rgb_image); |
| 256 | rgb_pixels = pixman_image_get_data(rgb_image); |
| 257 | rgb_stride_bytes = pixman_image_get_stride(rgb_image); |
| 258 | |
| 259 | /* Full size Y, quarter UV */ |
| 260 | bytes = width * height + (width / 2) * (height / 2) * sizeof(uint16_t); |
| 261 | buf = yuv_buffer_create(client, bytes, width, height, width, drm_format); |
| 262 | |
| 263 | y_base = buf->data; |
| 264 | uv_base = (uint16_t *)(y_base + width * height); |
| 265 | |
| 266 | for (y = 0; y < height; y++) { |
| 267 | rgb_row = rgb_pixels + (y / 2 * 2) * rgb_stride_bytes; |
| 268 | y_row = y_base + y * width; |
| 269 | uv_row = uv_base + (y / 2) * (width / 2); |
| 270 | |
| 271 | for (x = 0; x < width; x++) { |
| 272 | /* |
| 273 | * Sub-sample the source image instead, so that U and V |
| 274 | * sub-sampling does not require proper |
| 275 | * filtering/averaging/siting. |
| 276 | */ |
| 277 | argb = *(rgb_row + x / 2 * 2); |
| 278 | |
| 279 | /* |
| 280 | * A stupid way of "sub-sampling" chroma. This does not |
| 281 | * do the necessary filtering/averaging/siting. |
| 282 | */ |
| 283 | if ((y & 1) == 0 && (x & 1) == 0) { |
| 284 | x8r8g8b8_to_ycbcr8_bt601(argb, y_row + x, |
| 285 | &cb, &cr); |
| 286 | *(uv_row + x / 2) = ((uint16_t)cr << 8) | cb; |
| 287 | } else { |
| 288 | x8r8g8b8_to_ycbcr8_bt601(argb, y_row + x, |
| 289 | NULL, NULL); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | return buf; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Packed YCbCr |
| 299 | * |
| 300 | * [31:0] Cr0:Y1:Cb0:Y0 8:8:8:8 little endian |
| 301 | * 2x1 subsampled Cr:Cb plane |
| 302 | */ |
| 303 | static struct yuv_buffer * |
| 304 | yuyv_create_buffer(struct client *client, |
| 305 | uint32_t drm_format, |
| 306 | pixman_image_t *rgb_image) |
| 307 | { |
| 308 | struct yuv_buffer *buf; |
| 309 | size_t bytes; |
| 310 | int width; |
| 311 | int height; |
| 312 | int x, y; |
| 313 | void *rgb_pixels; |
| 314 | int rgb_stride_bytes; |
| 315 | uint32_t *rgb_row; |
| 316 | uint32_t *yuv_base; |
| 317 | uint32_t *yuv_row; |
| 318 | uint8_t cr; |
| 319 | uint8_t cb; |
| 320 | uint8_t y0; |
| 321 | |
| 322 | assert(drm_format == DRM_FORMAT_YUYV); |
| 323 | |
| 324 | width = pixman_image_get_width(rgb_image); |
| 325 | height = pixman_image_get_height(rgb_image); |
| 326 | rgb_pixels = pixman_image_get_data(rgb_image); |
| 327 | rgb_stride_bytes = pixman_image_get_stride(rgb_image); |
| 328 | |
| 329 | /* Full size Y, horizontally subsampled UV, 2 pixels in 32 bits */ |
| 330 | bytes = width / 2 * height * sizeof(uint32_t); |
| 331 | buf = yuv_buffer_create(client, bytes, width, height, width / 2 * sizeof(uint32_t), drm_format); |
| 332 | |
| 333 | yuv_base = buf->data; |
| 334 | |
| 335 | for (y = 0; y < height; y++) { |
| 336 | rgb_row = rgb_pixels + (y / 2 * 2) * rgb_stride_bytes; |
| 337 | yuv_row = yuv_base + y * (width / 2); |
| 338 | |
| 339 | for (x = 0; x < width; x += 2) { |
| 340 | /* |
| 341 | * Sub-sample the source image instead, so that U and V |
| 342 | * sub-sampling does not require proper |
| 343 | * filtering/averaging/siting. |
| 344 | */ |
| 345 | x8r8g8b8_to_ycbcr8_bt601(*(rgb_row + x), &y0, &cb, &cr); |
| 346 | *(yuv_row + x / 2) = |
| 347 | ((uint32_t)cr << 24) | |
| 348 | ((uint32_t)y0 << 16) | |
| 349 | ((uint32_t)cb << 8) | |
| 350 | ((uint32_t)y0 << 0); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | return buf; |
| 355 | } |
| 356 | |
| 357 | static void |
| 358 | show_window_with_yuv(struct client *client, struct yuv_buffer *buf) |
| 359 | { |
| 360 | struct surface *surface = client->surface; |
| 361 | int done; |
| 362 | |
| 363 | weston_test_move_surface(client->test->weston_test, surface->wl_surface, |
| 364 | 4, 4); |
| 365 | wl_surface_attach(surface->wl_surface, buf->proxy, 0, 0); |
| 366 | wl_surface_damage(surface->wl_surface, 0, 0, buf->width, |
| 367 | buf->height); |
| 368 | frame_callback_set(surface->wl_surface, &done); |
| 369 | wl_surface_commit(surface->wl_surface); |
| 370 | frame_callback_wait(client, &done); |
| 371 | } |
| 372 | |
| 373 | static const struct yuv_case yuv_cases[] = { |
| 374 | #define FMT(x) DRM_FORMAT_ ##x, #x |
| 375 | { FMT(YUV420), yuv420_create_buffer }, |
| 376 | { FMT(NV12), nv12_create_buffer }, |
| 377 | { FMT(YUYV), yuyv_create_buffer }, |
| 378 | #undef FMT |
| 379 | }; |
| 380 | |
| 381 | /* |
| 382 | * Test that various YUV pixel formats result in correct coloring on screen. |
| 383 | */ |
| 384 | TEST_P(yuv_buffer_shm, yuv_cases) |
| 385 | { |
| 386 | const struct yuv_case *my_case = data; |
| 387 | char *fname; |
| 388 | pixman_image_t *img; |
| 389 | struct client *client; |
| 390 | struct yuv_buffer *buf; |
| 391 | bool match; |
| 392 | |
| 393 | testlog("%s: format %s\n", get_test_name(), my_case->drm_format_name); |
| 394 | |
| 395 | /* |
| 396 | * This test image is 256 x 256 pixels. |
| 397 | * |
| 398 | * Therefore this test does NOT exercise: |
| 399 | * - odd image dimensions |
| 400 | * - non-square image |
| 401 | * - row padding |
| 402 | * - unaligned row stride |
| 403 | * - different alignments or padding in sub-sampled planes |
| 404 | * |
| 405 | * The reason to not test these is that GL-renderer seems to be more |
| 406 | * or less broken. |
| 407 | * |
| 408 | * The source image is effectively further downscaled to 128 x 128 |
| 409 | * before sampled and converted to 256 x 256 YUV, so that |
| 410 | * sub-sampling for U and V does not require proper algorithms. |
| 411 | * Therefore, this test also does not test: |
| 412 | * - chroma siting (chroma sample positioning) |
| 413 | */ |
| 414 | fname = image_filename("chocolate-cake"); |
| 415 | img = load_image_from_png(fname); |
| 416 | free(fname); |
| 417 | assert(img); |
| 418 | |
| 419 | client = create_client(); |
| 420 | client->surface = create_test_surface(client); |
| 421 | buf = my_case->create_buffer(client, my_case->drm_format, img); |
| 422 | show_window_with_yuv(client, buf); |
| 423 | |
| 424 | match = verify_screen_content(client, "yuv-buffer", 0, NULL, 0); |
| 425 | assert(match); |
| 426 | |
| 427 | yuv_buffer_destroy(buf); |
| 428 | client_destroy(client); |
| 429 | } |