Fix invalid assignment with unsigned values
[platform/core/uifw/libtdm.git] / tools / buffers.c
1 /*
2  * DRM based mode setting test program
3  * Copyright 2008 Tungsten Graphics
4  *   Jakob Bornecrantz <jakob@tungstengraphics.com>
5  * Copyright 2008 Intel Corporation
6  *   Jesse Barnes <jesse.barnes@intel.com>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24  * IN THE SOFTWARE.
25  */
26
27 #include <assert.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <string.h>
34 #include <time.h>
35 #include <math.h>
36
37 #include <tbm_bufmgr.h>
38 #include <tbm_surface.h>
39
40 /* LCOV_EXCL_START */
41
42 #include "buffers.h"
43
44 #define ALPHA_VALUE  100
45 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
46
47 /* -----------------------------------------------------------------------------
48  * Formats
49  */
50
51 struct color_component {
52         unsigned int length;
53         unsigned int offset;
54 };
55
56 struct rgb_info {
57         struct color_component red;
58         struct color_component green;
59         struct color_component blue;
60         struct color_component alpha;
61 };
62
63 enum yuv_order {
64         YUV_YCbCr = 1,
65         YUV_YCrCb = 2,
66         YUV_YC = 4,
67         YUV_CY = 8,
68 };
69
70 struct yuv_info {
71         enum yuv_order order;
72         unsigned int xsub;
73         unsigned int ysub;
74         unsigned int chroma_stride;
75 };
76
77 struct format_info {
78         unsigned int format;
79         const char *name;
80         const struct rgb_info rgb;
81         const struct yuv_info yuv;
82 };
83
84 #define MAKE_RGB_INFO(rl, ro, bl, bo, gl, go, al, ao) \
85         .rgb = { { (rl), (ro) }, { (bl), (bo) }, { (gl), (go) }, { (al), (ao) } }
86
87 #define MAKE_YUV_INFO(order, xsub, ysub, chroma_stride) \
88         .yuv = { (order), (xsub), (ysub), (chroma_stride) }
89
90 static const struct format_info format_info[] = {
91         /* YUV packed */
92         { TBM_FORMAT_UYVY, "UYVY", MAKE_YUV_INFO(YUV_YCbCr | YUV_CY, 2, 2, 2) },
93         { TBM_FORMAT_VYUY, "VYUY", MAKE_YUV_INFO(YUV_YCrCb | YUV_CY, 2, 2, 2) },
94         { TBM_FORMAT_YUYV, "YUYV", MAKE_YUV_INFO(YUV_YCbCr | YUV_YC, 2, 2, 2) },
95         { TBM_FORMAT_YVYU, "YVYU", MAKE_YUV_INFO(YUV_YCrCb | YUV_YC, 2, 2, 2) },
96         /* YUV semi-planar */
97         { TBM_FORMAT_NV12, "NV12", MAKE_YUV_INFO(YUV_YCbCr, 2, 2, 2) },
98         { TBM_FORMAT_NV21, "NV21", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 2) },
99         { TBM_FORMAT_NV16, "NV16", MAKE_YUV_INFO(YUV_YCbCr, 2, 1, 2) },
100         { TBM_FORMAT_NV61, "NV61", MAKE_YUV_INFO(YUV_YCrCb, 2, 1, 2) },
101         /* YUV planar */
102         { TBM_FORMAT_YUV420, "YU12", MAKE_YUV_INFO(YUV_YCbCr, 2, 2, 1) },
103         { TBM_FORMAT_YVU420, "YV12", MAKE_YUV_INFO(YUV_YCrCb, 2, 2, 1) },
104         /* RGB16 */
105         { TBM_FORMAT_ARGB4444, "AR12", MAKE_RGB_INFO(4, 8, 4, 4, 4, 0, 4, 12) },
106         { TBM_FORMAT_XRGB4444, "XR12", MAKE_RGB_INFO(4, 8, 4, 4, 4, 0, 0, 0) },
107         { TBM_FORMAT_ABGR4444, "AB12", MAKE_RGB_INFO(4, 0, 4, 4, 4, 8, 4, 12) },
108         { TBM_FORMAT_XBGR4444, "XB12", MAKE_RGB_INFO(4, 0, 4, 4, 4, 8, 0, 0) },
109         { TBM_FORMAT_RGBA4444, "RA12", MAKE_RGB_INFO(4, 12, 4, 8, 4, 4, 4, 0) },
110         { TBM_FORMAT_RGBX4444, "RX12", MAKE_RGB_INFO(4, 12, 4, 8, 4, 4, 0, 0) },
111         { TBM_FORMAT_BGRA4444, "BA12", MAKE_RGB_INFO(4, 4, 4, 8, 4, 12, 4, 0) },
112         { TBM_FORMAT_BGRX4444, "BX12", MAKE_RGB_INFO(4, 4, 4, 8, 4, 12, 0, 0) },
113         { TBM_FORMAT_ARGB1555, "AR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 1, 15) },
114         { TBM_FORMAT_XRGB1555, "XR15", MAKE_RGB_INFO(5, 10, 5, 5, 5, 0, 0, 0) },
115         { TBM_FORMAT_ABGR1555, "AB15", MAKE_RGB_INFO(5, 0, 5, 5, 5, 10, 1, 15) },
116         { TBM_FORMAT_XBGR1555, "XB15", MAKE_RGB_INFO(5, 0, 5, 5, 5, 10, 0, 0) },
117         { TBM_FORMAT_RGBA5551, "RA15", MAKE_RGB_INFO(5, 11, 5, 6, 5, 1, 1, 0) },
118         { TBM_FORMAT_RGBX5551, "RX15", MAKE_RGB_INFO(5, 11, 5, 6, 5, 1, 0, 0) },
119         { TBM_FORMAT_BGRA5551, "BA15", MAKE_RGB_INFO(5, 1, 5, 6, 5, 11, 1, 0) },
120         { TBM_FORMAT_BGRX5551, "BX15", MAKE_RGB_INFO(5, 1, 5, 6, 5, 11, 0, 0) },
121         { TBM_FORMAT_RGB565, "RG16", MAKE_RGB_INFO(5, 11, 6, 5, 5, 0, 0, 0) },
122         { TBM_FORMAT_BGR565, "BG16", MAKE_RGB_INFO(5, 0, 6, 5, 5, 11, 0, 0) },
123         /* RGB24 */
124         { TBM_FORMAT_BGR888, "BG24", MAKE_RGB_INFO(8, 0, 8, 8, 8, 16, 0, 0) },
125         { TBM_FORMAT_RGB888, "RG24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 0) },
126         /* RGB32 */
127         { TBM_FORMAT_ARGB8888, "AR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 8, 24) },
128         { TBM_FORMAT_XRGB8888, "XR24", MAKE_RGB_INFO(8, 16, 8, 8, 8, 0, 0, 24) },
129         { TBM_FORMAT_ABGR8888, "AB24", MAKE_RGB_INFO(8, 0, 8, 8, 8, 16, 8, 24) },
130         { TBM_FORMAT_XBGR8888, "XB24", MAKE_RGB_INFO(8, 0, 8, 8, 8, 16, 0, 0) },
131         { TBM_FORMAT_RGBA8888, "RA24", MAKE_RGB_INFO(8, 24, 8, 16, 8, 8, 8, 0) },
132         { TBM_FORMAT_RGBX8888, "RX24", MAKE_RGB_INFO(8, 24, 8, 16, 8, 8, 0, 0) },
133         { TBM_FORMAT_BGRA8888, "BA24", MAKE_RGB_INFO(8, 8, 8, 16, 8, 24, 8, 0) },
134         { TBM_FORMAT_BGRX8888, "BX24", MAKE_RGB_INFO(8, 8, 8, 16, 8, 24, 0, 0) },
135         { TBM_FORMAT_ARGB2101010, "AR30", MAKE_RGB_INFO(10, 20, 10, 10, 10, 0, 2, 30) },
136         { TBM_FORMAT_XRGB2101010, "XR30", MAKE_RGB_INFO(10, 20, 10, 10, 10, 0, 0, 0) },
137         { TBM_FORMAT_ABGR2101010, "AB30", MAKE_RGB_INFO(10, 0, 10, 10, 10, 20, 2, 30) },
138         { TBM_FORMAT_XBGR2101010, "XB30", MAKE_RGB_INFO(10, 0, 10, 10, 10, 20, 0, 0) },
139         { TBM_FORMAT_RGBA1010102, "RA30", MAKE_RGB_INFO(10, 22, 10, 12, 10, 2, 2, 0) },
140         { TBM_FORMAT_RGBX1010102, "RX30", MAKE_RGB_INFO(10, 22, 10, 12, 10, 2, 0, 0) },
141         { TBM_FORMAT_BGRA1010102, "BA30", MAKE_RGB_INFO(10, 2, 10, 12, 10, 22, 2, 0) },
142         { TBM_FORMAT_BGRX1010102, "BX30", MAKE_RGB_INFO(10, 2, 10, 12, 10, 22, 0, 0) },
143 };
144
145 static unsigned int rand_seed;
146
147 unsigned int format_fourcc(const char *name)
148 {
149         unsigned int i;
150         for (i = 0; i < ARRAY_SIZE(format_info); i++) {
151                 if (!strcmp(format_info[i].name, name))
152                         return format_info[i].format;
153         }
154         return 0;
155 }
156
157 /* -----------------------------------------------------------------------------
158  * Test patterns
159  */
160
161 struct color_rgb24 {
162         unsigned int value:24;
163 } __attribute__((__packed__));
164
165 struct color_yuv {
166         unsigned char y;
167         unsigned char u;
168         unsigned char v;
169 };
170
171 #define MAKE_YUV_601_Y(r, g, b) \
172         (((66 * (r) + 129 * (g) +  25 * (b) + 128) >> 8) + 16)
173 #define MAKE_YUV_601_U(r, g, b) \
174         (((-38 * (r) -  74 * (g) + 112 * (b) + 128) >> 8) + 128)
175 #define MAKE_YUV_601_V(r, g, b) \
176         (((112 * (r) -  94 * (g) -  18 * (b) + 128) >> 8) + 128)
177
178 #define MAKE_YUV_601(r, g, b) \
179         { .y = MAKE_YUV_601_Y(r, g, b), \
180           .u = MAKE_YUV_601_U(r, g, b), \
181           .v = MAKE_YUV_601_V(r, g, b) }
182
183 #define MAKE_RGBA(rgb, r, g, b, a) \
184         ((((r) >> (8 - (rgb)->red.length)) << (rgb)->red.offset) | \
185          (((g) >> (8 - (rgb)->green.length)) << (rgb)->green.offset) | \
186          (((b) >> (8 - (rgb)->blue.length)) << (rgb)->blue.offset) | \
187          (((a) >> (8 - (rgb)->alpha.length)) << (rgb)->alpha.offset))
188
189 #define MAKE_RGBX(rgb, r, g, b) \
190                 ((((r) >> (8 - (rgb)->red.length)) << (rgb)->red.offset) | \
191                  (((g) >> (8 - (rgb)->green.length)) << (rgb)->green.offset) | \
192                  (((b) >> (8 - (rgb)->blue.length)) << (rgb)->blue.offset) | \
193                  (255 << (rgb)->alpha.offset))
194
195 #define MAKE_RGB24(rgb, r, g, b) \
196         { .value = MAKE_RGBA(rgb, r, g, b, 0) }
197
198 const struct color_yuv colors_yuv_top[] = {
199         MAKE_YUV_601(191, 192, 192),    /* grey */
200         MAKE_YUV_601(192, 192, 0),      /* yellow */
201         MAKE_YUV_601(0, 192, 192),      /* cyan */
202         MAKE_YUV_601(0, 192, 0),        /* green */
203         MAKE_YUV_601(192, 0, 192),      /* magenta */
204         MAKE_YUV_601(192, 0, 0),        /* red */
205         MAKE_YUV_601(0, 0, 192),        /* blue */
206 };
207 const struct color_yuv colors_yuv_middle[] = {
208         MAKE_YUV_601(0, 0, 192),        /* blue */
209         MAKE_YUV_601(19, 19, 19),       /* black */
210         MAKE_YUV_601(192, 0, 192),      /* magenta */
211         MAKE_YUV_601(19, 19, 19),       /* black */
212         MAKE_YUV_601(0, 192, 192),      /* cyan */
213         MAKE_YUV_601(19, 19, 19),       /* black */
214         MAKE_YUV_601(192, 192, 192),    /* grey */
215 };
216 const struct color_yuv colors_yuv_bottom[] = {
217         MAKE_YUV_601(0, 33, 76),        /* in-phase */
218         MAKE_YUV_601(255, 255, 255),    /* super white */
219         MAKE_YUV_601(50, 0, 106),       /* quadrature */
220         MAKE_YUV_601(19, 19, 19),       /* black */
221         MAKE_YUV_601(9, 9, 9),          /* 3.5% */
222         MAKE_YUV_601(19, 19, 19),       /* 7.5% */
223         MAKE_YUV_601(29, 29, 29),       /* 11.5% */
224         MAKE_YUV_601(19, 19, 19),       /* black */
225 };
226
227 static void
228 fill_smpte_yuv_planar(const struct yuv_info *yuv,
229                                           unsigned char *y_mem, unsigned char *u_mem,
230                                           unsigned char *v_mem, unsigned int width,
231                                           unsigned int height, unsigned int stride)
232 {
233         unsigned int cs = yuv->chroma_stride;
234         unsigned int xsub = yuv->xsub;
235         unsigned int ysub = yuv->ysub;
236         unsigned int x;
237         unsigned int y;
238
239         /* Luma */
240         for (y = 0; y < height * 6 / 9; ++y) {
241                 for (x = 0; x < width; ++x)
242                         y_mem[x] = colors_yuv_top[x * 7 / width].y;
243                 y_mem += stride;
244         }
245
246         for (; y < height * 7 / 9; ++y) {
247                 for (x = 0; x < width; ++x)
248                         y_mem[x] = colors_yuv_middle[x * 7 / width].y;
249                 y_mem += stride;
250         }
251
252         for (; y < height; ++y) {
253                 for (x = 0; x < width * 5 / 7; ++x)
254                         y_mem[x] = colors_yuv_bottom[x * 4 / (width * 5 / 7)].y;
255                 for (; x < width * 6 / 7; ++x)
256                         y_mem[x] = colors_yuv_bottom[(x - width * 5 / 7) * 3
257                                                                          / (width / 7) + 4].y;
258                 for (; x < width; ++x) {
259                         if (rand_r(&rand_seed) % 2)
260                                 y_mem[x] = colors_yuv_bottom[1].y;
261                         else
262                                 y_mem[x] = colors_yuv_bottom[7].y;
263                 }
264                 y_mem += stride;
265         }
266
267         /* Chroma */
268         for (y = 0; y < height / ysub * 6 / 9; ++y) {
269                 for (x = 0; x < width; x += xsub) {
270                         u_mem[x * cs / xsub] = colors_yuv_top[x * 7 / width].u;
271                         v_mem[x * cs / xsub] = colors_yuv_top[x * 7 / width].v;
272                 }
273                 u_mem += stride * cs / xsub;
274                 v_mem += stride * cs / xsub;
275         }
276
277         for (; y < height / ysub * 7 / 9; ++y) {
278                 for (x = 0; x < width; x += xsub) {
279                         u_mem[x * cs / xsub] = colors_yuv_middle[x * 7 / width].u;
280                         v_mem[x * cs / xsub] = colors_yuv_middle[x * 7 / width].v;
281                 }
282                 u_mem += stride * cs / xsub;
283                 v_mem += stride * cs / xsub;
284         }
285
286         for (; y < height / ysub; ++y) {
287                 for (x = 0; x < width * 5 / 7; x += xsub) {
288                         u_mem[x * cs / xsub] =
289                                 colors_yuv_bottom[x * 4 / (width * 5 / 7)].u;
290                         v_mem[x * cs / xsub] =
291                                 colors_yuv_bottom[x * 4 / (width * 5 / 7)].v;
292                 }
293                 for (; x < width * 6 / 7; x += xsub) {
294                         u_mem[x * cs / xsub] = colors_yuv_bottom[(x - width * 5 / 7) *
295                                                                                                  3 / (width / 7) + 4].u;
296                         v_mem[x * cs / xsub] = colors_yuv_bottom[(x - width * 5 / 7) *
297                                                                                                  3 / (width / 7) + 4].v;
298                 }
299                 for (; x < width; x += xsub) {
300                         if (rand_r(&rand_seed) % 2) {
301                                 u_mem[x * cs / xsub] = colors_yuv_bottom[1].u;
302                                 v_mem[x * cs / xsub] = colors_yuv_bottom[1].v;
303                         } else {
304                                 u_mem[x * cs / xsub] = colors_yuv_bottom[7].u;
305                                 v_mem[x * cs / xsub] = colors_yuv_bottom[7].v;
306                         }
307                 }
308                 u_mem += stride * cs / xsub;
309                 v_mem += stride * cs / xsub;
310         }
311 }
312
313 static void
314 fill_smpte_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
315                                           unsigned int width, unsigned int height,
316                                           unsigned int stride)
317 {
318         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
319         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
320         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
321         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
322         unsigned int x;
323         unsigned int y;
324
325         if (width < 8)
326                 return;
327
328         /* Luma */
329         for (y = 0; y < height * 6 / 9; ++y) {
330                 for (x = 0; x < width; ++x)
331                         y_mem[2 * x] = colors_yuv_top[x * 7 / width].y;
332                 y_mem += stride;
333         }
334
335         for (; y < height * 7 / 9; ++y) {
336                 for (x = 0; x < width; ++x)
337                         y_mem[2 * x] = colors_yuv_middle[x * 7 / width].y;
338                 y_mem += stride;
339         }
340
341         for (; y < height; ++y) {
342                 for (x = 0; x < width * 5 / 7; ++x)
343                         y_mem[2 * x] = colors_yuv_bottom[x * 4 / (width * 5 / 7)].y;
344                 for (; x < width * 6 / 7; ++x)
345                         y_mem[2 * x] = colors_yuv_bottom[(x - width * 5 / 7) * 3
346                                                                                  / (width / 7) + 4].y;
347                 for (; x < width; ++x)
348                         y_mem[2 * x] = colors_yuv_bottom[7].y;
349                 y_mem += stride;
350         }
351
352         /* Chroma */
353         for (y = 0; y < height * 6 / 9; ++y) {
354                 for (x = 0; x < width; x += 2) {
355                         c_mem[2 * x + u] = colors_yuv_top[x * 7 / width].u;
356                         c_mem[2 * x + v] = colors_yuv_top[x * 7 / width].v;
357                 }
358                 c_mem += stride;
359         }
360
361         for (; y < height * 7 / 9; ++y) {
362                 for (x = 0; x < width; x += 2) {
363                         c_mem[2 * x + u] = colors_yuv_middle[x * 7 / width].u;
364                         c_mem[2 * x + v] = colors_yuv_middle[x * 7 / width].v;
365                 }
366                 c_mem += stride;
367         }
368
369         for (; y < height; ++y) {
370                 for (x = 0; x < width * 5 / 7; x += 2) {
371                         c_mem[2 * x + u] = colors_yuv_bottom[x * 4 / (width * 5 / 7)].u;
372                         c_mem[2 * x + v] = colors_yuv_bottom[x * 4 / (width * 5 / 7)].v;
373                 }
374                 for (; x < width * 6 / 7; x += 2) {
375                         c_mem[2 * x + u] = colors_yuv_bottom[(x - width * 5 / 7) *
376                                                                                          3 / (width / 7) + 4].u;
377                         c_mem[2 * x + v] = colors_yuv_bottom[(x - width * 5 / 7) *
378                                                                                          3 / (width / 7) + 4].v;
379                 }
380                 for (; x < width; x += 2) {
381                         c_mem[2 * x + u] = colors_yuv_bottom[7].u;
382                         c_mem[2 * x + v] = colors_yuv_bottom[7].v;
383                 }
384                 c_mem += stride;
385         }
386 }
387
388 static void
389 fill_smpte_rgb16(const struct rgb_info *rgb, unsigned char *mem,
390                                  unsigned int width, unsigned int height, unsigned int stride)
391 {
392         const uint16_t colors_rgb16_top[] = {
393                 MAKE_RGBA(rgb, 192, 192, 192, ALPHA_VALUE),     /* grey */
394                 MAKE_RGBA(rgb, 192, 192, 0, ALPHA_VALUE),       /* yellow */
395                 MAKE_RGBA(rgb, 0, 192, 192, ALPHA_VALUE),       /* cyan */
396                 MAKE_RGBA(rgb, 0, 192, 0, ALPHA_VALUE),         /* green */
397                 MAKE_RGBA(rgb, 192, 0, 192, ALPHA_VALUE),       /* magenta */
398                 MAKE_RGBA(rgb, 192, 0, 0, ALPHA_VALUE),         /* red */
399                 MAKE_RGBA(rgb, 0, 0, 192, ALPHA_VALUE),         /* blue */
400         };
401         const uint16_t colors_rgb16_middle[] = {
402                 MAKE_RGBA(rgb, 0, 0, 192, ALPHA_VALUE),         /* blue */
403                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
404                 MAKE_RGBA(rgb, 192, 0, 192, ALPHA_VALUE),       /* magenta */
405                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
406                 MAKE_RGBA(rgb, 0, 192, 192, ALPHA_VALUE),       /* cyan */
407                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
408                 MAKE_RGBA(rgb, 192, 192, 192, ALPHA_VALUE),     /* grey */
409         };
410         const uint16_t colors_rgb16_bottom[] = {
411                 MAKE_RGBA(rgb, 0, 33, 76, ALPHA_VALUE),         /* in-phase */
412                 MAKE_RGBA(rgb, 255, 255, 255, ALPHA_VALUE),     /* super white */
413                 MAKE_RGBA(rgb, 50, 0, 106, ALPHA_VALUE),        /* quadrature */
414                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
415                 MAKE_RGBA(rgb, 9, 9, 9, ALPHA_VALUE),           /* 3.5% */
416                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* 7.5% */
417                 MAKE_RGBA(rgb, 29, 29, 29, ALPHA_VALUE),        /* 11.5% */
418                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
419         };
420
421         unsigned int x;
422         unsigned int y;
423
424         if (width < 8)
425                 return;
426
427         for (y = 0; y < height * 6 / 9; ++y) {
428                 for (x = 0; x < width; ++x)
429                         ((uint16_t *)mem)[x] = colors_rgb16_top[x * 7 / width];
430                 mem += stride;
431         }
432
433         for (; y < height * 7 / 9; ++y) {
434                 for (x = 0; x < width; ++x)
435                         ((uint16_t *)mem)[x] = colors_rgb16_middle[x * 7 / width];
436                 mem += stride;
437         }
438
439         for (; y < height; ++y) {
440                 for (x = 0; x < width * 5 / 7; ++x)
441                         ((uint16_t *)mem)[x] =
442                                 colors_rgb16_bottom[x * 4 / (width * 5 / 7)];
443                 for (; x < width * 6 / 7; ++x)
444                         ((uint16_t *)mem)[x] =
445                                 colors_rgb16_bottom[(x - width * 5 / 7) * 3
446                                                           / (width / 7) + 4];
447                 for (; x < width; ++x)
448                         ((uint16_t *)mem)[x] = colors_rgb16_bottom[7];
449                 mem += stride;
450         }
451 }
452
453 static void
454 fill_smpte_rgb24(const struct rgb_info *rgb, void *mem,
455                                  unsigned int width, unsigned int height, unsigned int stride)
456 {
457         const struct color_rgb24 colors_rgb24_top[] = {
458                 MAKE_RGB24(rgb, 192, 192, 192), /* grey */
459                 MAKE_RGB24(rgb, 192, 192, 0),   /* yellow */
460                 MAKE_RGB24(rgb, 0, 192, 192),   /* cyan */
461                 MAKE_RGB24(rgb, 0, 192, 0),     /* green */
462                 MAKE_RGB24(rgb, 192, 0, 192),   /* magenta */
463                 MAKE_RGB24(rgb, 192, 0, 0),     /* red */
464                 MAKE_RGB24(rgb, 0, 0, 192),     /* blue */
465         };
466         const struct color_rgb24 colors_rgb24_middle[] = {
467                 MAKE_RGB24(rgb, 0, 0, 192),     /* blue */
468                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
469                 MAKE_RGB24(rgb, 192, 0, 192),   /* magenta */
470                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
471                 MAKE_RGB24(rgb, 0, 192, 192),   /* cyan */
472                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
473                 MAKE_RGB24(rgb, 192, 192, 192), /* grey */
474         };
475         const struct color_rgb24 colors_rgb24_bottom[] = {
476                 MAKE_RGB24(rgb, 0, 33, 76),     /* in-phase */
477                 MAKE_RGB24(rgb, 255, 255, 255), /* super white */
478                 MAKE_RGB24(rgb, 50, 0, 106),    /* quadrature */
479                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
480                 MAKE_RGB24(rgb, 9, 9, 9),       /* 3.5% */
481                 MAKE_RGB24(rgb, 19, 19, 19),    /* 7.5% */
482                 MAKE_RGB24(rgb, 29, 29, 29),    /* 11.5% */
483                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
484         };
485
486         unsigned int x;
487         unsigned int y;
488
489         if (width < 8)
490                 return;
491
492         for (y = 0; y < height * 6 / 9; ++y) {
493                 for (x = 0; x < width; ++x)
494                         ((struct color_rgb24 *)mem)[x] =
495                                 colors_rgb24_top[x * 7 / width];
496                 mem += stride;
497         }
498
499         for (; y < height * 7 / 9; ++y) {
500                 for (x = 0; x < width; ++x)
501                         ((struct color_rgb24 *)mem)[x] =
502                                 colors_rgb24_middle[x * 7 / width];
503                 mem += stride;
504         }
505
506         for (; y < height; ++y) {
507                 for (x = 0; x < width * 5 / 7; ++x)
508                         ((struct color_rgb24 *)mem)[x] =
509                                 colors_rgb24_bottom[x * 4 / (width * 5 / 7)];
510                 for (; x < width * 6 / 7; ++x)
511                         ((struct color_rgb24 *)mem)[x] =
512                                 colors_rgb24_bottom[(x - width * 5 / 7) * 3
513                                                           / (width / 7) + 4];
514                 for (; x < width; ++x)
515                         ((struct color_rgb24 *)mem)[x] = colors_rgb24_bottom[7];
516                 mem += stride;
517         }
518 }
519
520 static void
521 fill_smpte_rgb32(const struct rgb_info *rgb, unsigned char *mem,
522                                  unsigned int width, unsigned int height, unsigned int stride, bool alpha)
523 {
524         const uint32_t colors_rgba32_top[] = {
525                 MAKE_RGBA(rgb, 192, 192, 192, ALPHA_VALUE),     /* grey */
526                 MAKE_RGBA(rgb, 192, 192, 0, ALPHA_VALUE),       /* yellow */
527                 MAKE_RGBA(rgb, 0, 192, 192, ALPHA_VALUE),       /* cyan */
528                 MAKE_RGBA(rgb, 0, 192, 0, ALPHA_VALUE),         /* green */
529                 MAKE_RGBA(rgb, 192, 0, 192, ALPHA_VALUE),       /* magenta */
530                 MAKE_RGBA(rgb, 192, 0, 0, ALPHA_VALUE),         /* red */
531                 MAKE_RGBA(rgb, 0, 0, 192, ALPHA_VALUE),         /* blue */
532         };
533         const uint32_t colors_rgba32_middle[] = {
534                 MAKE_RGBA(rgb, 0, 0, 192, ALPHA_VALUE),         /* blue */
535                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
536                 MAKE_RGBA(rgb, 192, 0, 192, ALPHA_VALUE),       /* magenta */
537                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
538                 MAKE_RGBA(rgb, 0, 192, 192, ALPHA_VALUE),       /* cyan */
539                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
540                 MAKE_RGBA(rgb, 192, 192, 192, ALPHA_VALUE),     /* grey */
541         };
542         const uint32_t colors_rgba32_bottom[] = {
543                 MAKE_RGBA(rgb, 0, 33, 76, ALPHA_VALUE),         /* in-phase */
544                 MAKE_RGBA(rgb, 255, 255, 255, ALPHA_VALUE),     /* super white */
545                 MAKE_RGBA(rgb, 50, 0, 106, ALPHA_VALUE),        /* quadrature */
546                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
547                 MAKE_RGBA(rgb, 9, 9, 9, ALPHA_VALUE),           /* 3.5% */
548                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* 7.5% */
549                 MAKE_RGBA(rgb, 29, 29, 29, ALPHA_VALUE),        /* 11.5% */
550                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
551         };
552
553         const uint32_t colors_rgbx32_top[] = {
554                 MAKE_RGBX(rgb, 192, 192, 192),  /* grey */
555                 MAKE_RGBX(rgb, 192, 192, 0),    /* yellow */
556                 MAKE_RGBX(rgb, 0, 192, 192),    /* cyan */
557                 MAKE_RGBX(rgb, 0, 192, 0),              /* green */
558                 MAKE_RGBX(rgb, 192, 0, 192),    /* magenta */
559                 MAKE_RGBX(rgb, 192, 0, 0),              /* red */
560                 MAKE_RGBX(rgb, 0, 0, 192),              /* blue */
561         };
562         const uint32_t colors_rgbx32_middle[] = {
563                 MAKE_RGBX(rgb, 0, 0, 192),              /* blue */
564                 MAKE_RGBX(rgb, 19, 19, 19),     /* black */
565                 MAKE_RGBX(rgb, 192, 0, 192),    /* magenta */
566                 MAKE_RGBX(rgb, 19, 19, 19),     /* black */
567                 MAKE_RGBX(rgb, 0, 192, 192),    /* cyan */
568                 MAKE_RGBX(rgb, 19, 19, 19),     /* black */
569                 MAKE_RGBX(rgb, 192, 192, 192),  /* grey */
570         };
571         const uint32_t colors_rgbx32_bottom[] = {
572                 MAKE_RGBX(rgb, 0, 33, 76),              /* in-phase */
573                 MAKE_RGBX(rgb, 255, 255, 255),  /* super white */
574                 MAKE_RGBX(rgb, 50, 0, 106),     /* quadrature */
575                 MAKE_RGBX(rgb, 19, 19, 19),     /* black */
576                 MAKE_RGBX(rgb, 9, 9, 9),                /* 3.5% */
577                 MAKE_RGBX(rgb, 19, 19, 19),     /* 7.5% */
578                 MAKE_RGBX(rgb, 29, 29, 29),     /* 11.5% */
579                 MAKE_RGBX(rgb, 19, 19, 19),     /* black */
580         };
581
582         const uint32_t *colors_top;
583         const uint32_t *colors_middle;
584         const uint32_t *colors_bottom;
585
586         unsigned int x;
587         unsigned int y;
588
589         if (alpha) {
590                 colors_top = colors_rgba32_top;
591                 colors_middle = colors_rgba32_middle;
592                 colors_bottom = colors_rgba32_bottom;
593         } else {
594                 colors_top = colors_rgbx32_top;
595                 colors_middle = colors_rgbx32_middle;
596                 colors_bottom = colors_rgbx32_bottom;
597         }
598
599         if (width < 8)
600                 return;
601
602         for (y = 0; y < height * 6 / 9; ++y) {
603                 for (x = 0; x < width; ++x)
604                         ((uint32_t *)mem)[x] = colors_top[x * 7 / width];
605                 mem += stride;
606         }
607
608         for (; y < height * 7 / 9; ++y) {
609                 for (x = 0; x < width; ++x)
610                         ((uint32_t *)mem)[x] = colors_middle[x * 7 / width];
611                 mem += stride;
612         }
613
614         for (; y < height; ++y) {
615                 for (x = 0; x < width * 5 / 7; ++x)
616                         ((uint32_t *)mem)[x] =
617                                 colors_bottom[x * 4 / (width * 5 / 7)];
618                 for (; x < width * 6 / 7; ++x)
619                         ((uint32_t *)mem)[x] =
620                                 colors_bottom[(x - width * 5 / 7) * 3
621                                                           / (width / 7) + 4];
622                 for (; x < width; ++x) {
623                         if (alpha)
624                                 ((uint32_t *)mem)[x] = (rand_r(&rand_seed) % 2) ? MAKE_RGBA(rgb, 255, 255, 255, ALPHA_VALUE) : MAKE_RGBA(rgb, 0, 0, 0, ALPHA_VALUE);
625                         else
626                                 ((uint32_t *)mem)[x] = (rand_r(&rand_seed) % 2) ? MAKE_RGBX(rgb, 255, 255, 255) : MAKE_RGBX(rgb, 0, 0, 0);
627                 }
628                 mem += stride;
629         }
630 }
631
632 static void
633 fill_smpte(const struct format_info *info, void *planes[3], unsigned int width,
634                    unsigned int height, unsigned int stride)
635 {
636         unsigned char *u, *v;
637
638         switch (info->format) {
639         case TBM_FORMAT_UYVY:
640         case TBM_FORMAT_VYUY:
641         case TBM_FORMAT_YUYV:
642         case TBM_FORMAT_YVYU:
643                 return fill_smpte_yuv_packed(&info->yuv, planes[0], width,
644                                                                          height, stride);
645
646         case TBM_FORMAT_NV12:
647         case TBM_FORMAT_NV21:
648         case TBM_FORMAT_NV16:
649         case TBM_FORMAT_NV61:
650                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
651                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
652                 return fill_smpte_yuv_planar(&info->yuv, planes[0], u, v,
653                                                                          width, height, stride);
654
655         case TBM_FORMAT_YUV420:
656                 return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[1],
657                                                                          planes[2], width, height, stride);
658
659         case TBM_FORMAT_YVU420:
660                 return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[2],
661                                                                          planes[1], width, height, stride);
662
663         case TBM_FORMAT_ARGB4444:
664         case TBM_FORMAT_XRGB4444:
665         case TBM_FORMAT_ABGR4444:
666         case TBM_FORMAT_XBGR4444:
667         case TBM_FORMAT_RGBA4444:
668         case TBM_FORMAT_RGBX4444:
669         case TBM_FORMAT_BGRA4444:
670         case TBM_FORMAT_BGRX4444:
671         case TBM_FORMAT_RGB565:
672         case TBM_FORMAT_BGR565:
673         case TBM_FORMAT_ARGB1555:
674         case TBM_FORMAT_XRGB1555:
675         case TBM_FORMAT_ABGR1555:
676         case TBM_FORMAT_XBGR1555:
677         case TBM_FORMAT_RGBA5551:
678         case TBM_FORMAT_RGBX5551:
679         case TBM_FORMAT_BGRA5551:
680         case TBM_FORMAT_BGRX5551:
681                 return fill_smpte_rgb16(&info->rgb, planes[0],
682                                                                 width, height, stride);
683
684         case TBM_FORMAT_BGR888:
685         case TBM_FORMAT_RGB888:
686                 return fill_smpte_rgb24(&info->rgb, planes[0],
687                                                                 width, height, stride);
688         case TBM_FORMAT_ARGB8888:
689         case TBM_FORMAT_ABGR8888:
690         case TBM_FORMAT_XBGR8888:
691         case TBM_FORMAT_RGBA8888:
692         case TBM_FORMAT_RGBX8888:
693         case TBM_FORMAT_BGRA8888:
694         case TBM_FORMAT_BGRX8888:
695         case TBM_FORMAT_ARGB2101010:
696         case TBM_FORMAT_XRGB2101010:
697         case TBM_FORMAT_ABGR2101010:
698         case TBM_FORMAT_XBGR2101010:
699         case TBM_FORMAT_RGBA1010102:
700         case TBM_FORMAT_RGBX1010102:
701         case TBM_FORMAT_BGRA1010102:
702         case TBM_FORMAT_BGRX1010102:
703                 return fill_smpte_rgb32(&info->rgb, planes[0],
704                                                                 width, height, stride, true);
705         case TBM_FORMAT_XRGB8888:
706                 return fill_smpte_rgb32(&info->rgb, planes[0],
707                                                                 width, height, stride, false);
708         }
709 }
710
711 static void
712 fill_tiles_yuv_planar(const struct format_info *info,
713                                           unsigned char *y_mem, unsigned char *u_mem,
714                                           unsigned char *v_mem, unsigned int width,
715                                           unsigned int height, unsigned int stride)
716 {
717         const struct yuv_info *yuv = &info->yuv;
718         unsigned int cs = yuv->chroma_stride;
719         unsigned int xsub = yuv->xsub;
720         unsigned int ysub = yuv->ysub;
721         unsigned int x;
722         unsigned int y;
723
724         for (y = 0; y < height; ++y) {
725                 for (x = 0; x < width; ++x) {
726                         div_t d = div(x + y, width);
727                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
728                                                          + 0x000a1120 * (d.rem >> 6);
729                         struct color_yuv color =
730                                 MAKE_YUV_601((rgb32 >> 16) & 0xff,
731                                                          (rgb32 >> 8) & 0xff, rgb32 & 0xff);
732
733                         y_mem[x] = color.y;
734                         u_mem[x / xsub * cs] = color.u;
735                         v_mem[x / xsub * cs] = color.v;
736                 }
737
738                 y_mem += stride;
739                 if ((y + 1) % ysub == 0) {
740                         u_mem += stride * cs / xsub;
741                         v_mem += stride * cs / xsub;
742                 }
743         }
744 }
745
746 static void
747 fill_tiles_yuv_packed(const struct format_info *info, unsigned char *mem,
748                                           unsigned int width, unsigned int height,
749                                           unsigned int stride)
750 {
751         const struct yuv_info *yuv = &info->yuv;
752         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
753         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
754         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
755         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
756         unsigned int x;
757         unsigned int y;
758
759         for (y = 0; y < height; ++y) {
760                 for (x = 0; x < width; x += 2) {
761                         div_t d = div(x + y, width);
762                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
763                                                          + 0x000a1120 * (d.rem >> 6);
764                         struct color_yuv color =
765                                 MAKE_YUV_601((rgb32 >> 16) & 0xff,
766                                                          (rgb32 >> 8) & 0xff, rgb32 & 0xff);
767
768                         y_mem[2 * x] = color.y;
769                         c_mem[2 * x + u] = color.u;
770                         y_mem[2 * x + 2] = color.y;
771                         c_mem[2 * x + v] = color.v;
772                 }
773
774                 y_mem += stride;
775                 c_mem += stride;
776         }
777 }
778
779 static void
780 fill_tiles_rgb16(const struct format_info *info, unsigned char *mem,
781                                  unsigned int width, unsigned int height, unsigned int stride)
782 {
783         const struct rgb_info *rgb = &info->rgb;
784         unsigned int x, y;
785
786         for (y = 0; y < height; ++y) {
787                 for (x = 0; x < width; ++x) {
788                         div_t d = div(x + y, width);
789                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
790                                                          + 0x000a1120 * (d.rem >> 6);
791                         uint16_t color =
792                                 MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
793                                                   (rgb32 >> 8) & 0xff, rgb32 & 0xff,
794                                                   ALPHA_VALUE);
795
796                         ((uint16_t *)mem)[x] = color;
797                 }
798                 mem += stride;
799         }
800 }
801
802 static void
803 fill_tiles_rgb24(const struct format_info *info, unsigned char *mem,
804                                  unsigned int width, unsigned int height, unsigned int stride)
805 {
806         const struct rgb_info *rgb = &info->rgb;
807         unsigned int x, y;
808
809         for (y = 0; y < height; ++y) {
810                 for (x = 0; x < width; ++x) {
811                         div_t d = div(x + y, width);
812                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
813                                                          + 0x000a1120 * (d.rem >> 6);
814                         struct color_rgb24 color =
815                                 MAKE_RGB24(rgb, (rgb32 >> 16) & 0xff,
816                                                    (rgb32 >> 8) & 0xff, rgb32 & 0xff);
817
818                         ((struct color_rgb24 *)mem)[x] = color;
819                 }
820                 mem += stride;
821         }
822 }
823
824 static void
825 fill_tiles_rgb32(const struct format_info *info, unsigned char *mem,
826                                  unsigned int width, unsigned int height, unsigned int stride)
827 {
828         const struct rgb_info *rgb = &info->rgb;
829         unsigned int x, y;
830
831         for (y = 0; y < height; ++y) {
832                 for (x = 0; x < width; ++x) {
833                         div_t d = div(x + y, width);
834                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
835                                                          + 0x000a1120 * (d.rem >> 6);
836                         uint32_t color =
837                                 MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
838                                                   (rgb32 >> 8) & 0xff, rgb32 & 0xff,
839                                                   ALPHA_VALUE);
840
841                         ((uint32_t *)mem)[x] = color;
842                 }
843                 mem += stride;
844         }
845 }
846
847 static void
848 fill_tiles(const struct format_info *info, void *planes[3], unsigned int width,
849                    unsigned int height, unsigned int stride)
850 {
851         unsigned char *u, *v;
852
853         switch (info->format) {
854         case TBM_FORMAT_UYVY:
855         case TBM_FORMAT_VYUY:
856         case TBM_FORMAT_YUYV:
857         case TBM_FORMAT_YVYU:
858                 return fill_tiles_yuv_packed(info, planes[0],
859                                                                          width, height, stride);
860
861         case TBM_FORMAT_NV12:
862         case TBM_FORMAT_NV21:
863         case TBM_FORMAT_NV16:
864         case TBM_FORMAT_NV61:
865                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
866                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
867                 return fill_tiles_yuv_planar(info, planes[0], u, v,
868                                                                          width, height, stride);
869
870         case TBM_FORMAT_YUV420:
871                 return fill_tiles_yuv_planar(info, planes[0], planes[1],
872                                                                          planes[2], width, height, stride);
873
874         case TBM_FORMAT_YVU420:
875                 return fill_tiles_yuv_planar(info, planes[0], planes[2],
876                                                                          planes[1], width, height, stride);
877
878         case TBM_FORMAT_ARGB4444:
879         case TBM_FORMAT_XRGB4444:
880         case TBM_FORMAT_ABGR4444:
881         case TBM_FORMAT_XBGR4444:
882         case TBM_FORMAT_RGBA4444:
883         case TBM_FORMAT_RGBX4444:
884         case TBM_FORMAT_BGRA4444:
885         case TBM_FORMAT_BGRX4444:
886         case TBM_FORMAT_RGB565:
887         case TBM_FORMAT_BGR565:
888         case TBM_FORMAT_ARGB1555:
889         case TBM_FORMAT_XRGB1555:
890         case TBM_FORMAT_ABGR1555:
891         case TBM_FORMAT_XBGR1555:
892         case TBM_FORMAT_RGBA5551:
893         case TBM_FORMAT_RGBX5551:
894         case TBM_FORMAT_BGRA5551:
895         case TBM_FORMAT_BGRX5551:
896                 return fill_tiles_rgb16(info, planes[0],
897                                                                 width, height, stride);
898
899         case TBM_FORMAT_BGR888:
900         case TBM_FORMAT_RGB888:
901                 return fill_tiles_rgb24(info, planes[0],
902                                                                 width, height, stride);
903         case TBM_FORMAT_ARGB8888:
904         case TBM_FORMAT_XRGB8888:
905         case TBM_FORMAT_ABGR8888:
906         case TBM_FORMAT_XBGR8888:
907         case TBM_FORMAT_RGBA8888:
908         case TBM_FORMAT_RGBX8888:
909         case TBM_FORMAT_BGRA8888:
910         case TBM_FORMAT_BGRX8888:
911         case TBM_FORMAT_ARGB2101010:
912         case TBM_FORMAT_XRGB2101010:
913         case TBM_FORMAT_ABGR2101010:
914         case TBM_FORMAT_XBGR2101010:
915         case TBM_FORMAT_RGBA1010102:
916         case TBM_FORMAT_RGBX1010102:
917         case TBM_FORMAT_BGRA1010102:
918         case TBM_FORMAT_BGRX1010102:
919                 return fill_tiles_rgb32(info, planes[0],
920                                                                 width, height, stride);
921         }
922 }
923
924 static void
925 fill_plain(const struct format_info *info, void *planes[3], unsigned int width,
926                    unsigned int height, unsigned int stride)
927 {
928         memset(planes[0], 0x77, stride * height);
929 }
930
931 /*
932  * fill_pattern - Fill a buffer with a test pattern
933  * @format: Pixel format
934  * @pattern: Test pattern
935  * @buffer: Buffer memory
936  * @width: Width in pixels
937  * @height: Height in pixels
938  * @stride: Line stride (pitch) in bytes
939  *
940  * Fill the buffer with the test pattern specified by the pattern parameter.
941  * Supported formats vary depending on the selected pattern.
942  */
943 static void
944 fill_pattern(unsigned int format, enum fill_pattern pattern, void *planes[3],
945                          unsigned int width, unsigned int height, unsigned int stride)
946 {
947         const struct format_info *info = NULL;
948         unsigned int i;
949
950         for (i = 0; i < ARRAY_SIZE(format_info); ++i) {
951                 if (format_info[i].format == format) {
952                         info = &format_info[i];
953                         break;
954                 }
955         }
956
957         if (info == NULL)
958                 return;
959
960         switch (pattern) {
961         case PATTERN_TILES:
962                 return fill_tiles(info, planes, width, height, stride);
963
964         case PATTERN_SMPTE:
965                 return fill_smpte(info, planes, width, height, stride);
966
967         case PATTERN_PLAIN:
968                 return fill_plain(info, planes, width, height, stride);
969
970         default:
971                 printf("Error: unsupported test pattern %u.\n", pattern);
972                 break;
973         }
974 }
975
976 void
977 tdm_test_buffer_fill(tbm_surface_h buffer, int pattern)
978 {
979         tbm_surface_info_s info;
980         void *plane[3];
981         int ret;
982
983         if (rand_seed == 0)
984                 rand_seed = time(NULL);
985
986         ret = tbm_surface_map(buffer, TBM_OPTION_WRITE, &info);
987         assert(ret == 0);
988
989         plane[0] = info.planes[0].ptr;
990         plane[1] = info.planes[1].ptr;
991         plane[2] = info.planes[2].ptr;
992         fill_pattern(info.format, pattern, plane, info.width, info.height, info.planes[0].stride);
993         tbm_surface_unmap(buffer);
994 }
995
996 /* LCOV_EXCL_END */