buffers: fill 0xFF for alpha bits if XRGB8888
[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 static void
199 fill_smpte_yuv_planar(const struct yuv_info *yuv,
200                                           unsigned char *y_mem, unsigned char *u_mem,
201                                           unsigned char *v_mem, unsigned int width,
202                                           unsigned int height, unsigned int stride)
203 {
204         const struct color_yuv colors_top[] = {
205                 MAKE_YUV_601(191, 192, 192),    /* grey */
206                 MAKE_YUV_601(192, 192, 0),      /* yellow */
207                 MAKE_YUV_601(0, 192, 192),      /* cyan */
208                 MAKE_YUV_601(0, 192, 0),        /* green */
209                 MAKE_YUV_601(192, 0, 192),      /* magenta */
210                 MAKE_YUV_601(192, 0, 0),        /* red */
211                 MAKE_YUV_601(0, 0, 192),        /* blue */
212         };
213         const struct color_yuv colors_middle[] = {
214                 MAKE_YUV_601(0, 0, 192),        /* blue */
215                 MAKE_YUV_601(19, 19, 19),       /* black */
216                 MAKE_YUV_601(192, 0, 192),      /* magenta */
217                 MAKE_YUV_601(19, 19, 19),       /* black */
218                 MAKE_YUV_601(0, 192, 192),      /* cyan */
219                 MAKE_YUV_601(19, 19, 19),       /* black */
220                 MAKE_YUV_601(192, 192, 192),    /* grey */
221         };
222         const struct color_yuv colors_bottom[] = {
223                 MAKE_YUV_601(0, 33, 76),        /* in-phase */
224                 MAKE_YUV_601(255, 255, 255),    /* super white */
225                 MAKE_YUV_601(50, 0, 106),       /* quadrature */
226                 MAKE_YUV_601(19, 19, 19),       /* black */
227                 MAKE_YUV_601(9, 9, 9),          /* 3.5% */
228                 MAKE_YUV_601(19, 19, 19),       /* 7.5% */
229                 MAKE_YUV_601(29, 29, 29),       /* 11.5% */
230                 MAKE_YUV_601(19, 19, 19),       /* black */
231         };
232         unsigned int cs = yuv->chroma_stride;
233         unsigned int xsub = yuv->xsub;
234         unsigned int ysub = yuv->ysub;
235         unsigned int x;
236         unsigned int y;
237
238         /* Luma */
239         for (y = 0; y < height * 6 / 9; ++y) {
240                 for (x = 0; x < width; ++x)
241                         y_mem[x] = colors_top[x * 7 / width].y;
242                 y_mem += stride;
243         }
244
245         for (; y < height * 7 / 9; ++y) {
246                 for (x = 0; x < width; ++x)
247                         y_mem[x] = colors_middle[x * 7 / width].y;
248                 y_mem += stride;
249         }
250
251         for (; y < height; ++y) {
252                 for (x = 0; x < width * 5 / 7; ++x)
253                         y_mem[x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
254                 for (; x < width * 6 / 7; ++x)
255                         y_mem[x] = colors_bottom[(x - width * 5 / 7) * 3
256                                                                          / (width / 7) + 4].y;
257                 for (; x < width; ++x)
258                         y_mem[x] = colors_bottom[7].y;
259                 y_mem += stride;
260         }
261
262         /* Chroma */
263         for (y = 0; y < height / ysub * 6 / 9; ++y) {
264                 for (x = 0; x < width; x += xsub) {
265                         u_mem[x * cs / xsub] = colors_top[x * 7 / width].u;
266                         v_mem[x * cs / xsub] = colors_top[x * 7 / width].v;
267                 }
268                 u_mem += stride * cs / xsub;
269                 v_mem += stride * cs / xsub;
270         }
271
272         for (; y < height / ysub * 7 / 9; ++y) {
273                 for (x = 0; x < width; x += xsub) {
274                         u_mem[x * cs / xsub] = colors_middle[x * 7 / width].u;
275                         v_mem[x * cs / xsub] = colors_middle[x * 7 / width].v;
276                 }
277                 u_mem += stride * cs / xsub;
278                 v_mem += stride * cs / xsub;
279         }
280
281         for (; y < height / ysub; ++y) {
282                 for (x = 0; x < width * 5 / 7; x += xsub) {
283                         u_mem[x * cs / xsub] =
284                                 colors_bottom[x * 4 / (width * 5 / 7)].u;
285                         v_mem[x * cs / xsub] =
286                                 colors_bottom[x * 4 / (width * 5 / 7)].v;
287                 }
288                 for (; x < width * 6 / 7; x += xsub) {
289                         u_mem[x * cs / xsub] = colors_bottom[(x - width * 5 / 7) *
290                                                                                                  3 / (width / 7) + 4].u;
291                         v_mem[x * cs / xsub] = colors_bottom[(x - width * 5 / 7) *
292                                                                                                  3 / (width / 7) + 4].v;
293                 }
294                 for (; x < width; x += xsub) {
295                         u_mem[x * cs / xsub] = colors_bottom[7].u;
296                         v_mem[x * cs / xsub] = colors_bottom[7].v;
297                 }
298                 u_mem += stride * cs / xsub;
299                 v_mem += stride * cs / xsub;
300         }
301 }
302
303 static void
304 fill_smpte_yuv_packed(const struct yuv_info *yuv, unsigned char *mem,
305                                           unsigned int width, unsigned int height,
306                                           unsigned int stride)
307 {
308         const struct color_yuv colors_top[] = {
309                 MAKE_YUV_601(191, 192, 192),    /* grey */
310                 MAKE_YUV_601(192, 192, 0),      /* yellow */
311                 MAKE_YUV_601(0, 192, 192),      /* cyan */
312                 MAKE_YUV_601(0, 192, 0),        /* green */
313                 MAKE_YUV_601(192, 0, 192),      /* magenta */
314                 MAKE_YUV_601(192, 0, 0),        /* red */
315                 MAKE_YUV_601(0, 0, 192),        /* blue */
316         };
317         const struct color_yuv colors_middle[] = {
318                 MAKE_YUV_601(0, 0, 192),        /* blue */
319                 MAKE_YUV_601(19, 19, 19),       /* black */
320                 MAKE_YUV_601(192, 0, 192),      /* magenta */
321                 MAKE_YUV_601(19, 19, 19),       /* black */
322                 MAKE_YUV_601(0, 192, 192),      /* cyan */
323                 MAKE_YUV_601(19, 19, 19),       /* black */
324                 MAKE_YUV_601(192, 192, 192),    /* grey */
325         };
326         const struct color_yuv colors_bottom[] = {
327                 MAKE_YUV_601(0, 33, 76),        /* in-phase */
328                 MAKE_YUV_601(255, 255, 255),    /* super white */
329                 MAKE_YUV_601(50, 0, 106),       /* quadrature */
330                 MAKE_YUV_601(19, 19, 19),       /* black */
331                 MAKE_YUV_601(9, 9, 9),          /* 3.5% */
332                 MAKE_YUV_601(19, 19, 19),       /* 7.5% */
333                 MAKE_YUV_601(29, 29, 29),       /* 11.5% */
334                 MAKE_YUV_601(19, 19, 19),       /* black */
335         };
336         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
337         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
338         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
339         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
340         unsigned int x;
341         unsigned int y;
342
343         if (width < 8)
344                 return;
345
346         /* Luma */
347         for (y = 0; y < height * 6 / 9; ++y) {
348                 for (x = 0; x < width; ++x)
349                         y_mem[2 * x] = colors_top[x * 7 / width].y;
350                 y_mem += stride;
351         }
352
353         for (; y < height * 7 / 9; ++y) {
354                 for (x = 0; x < width; ++x)
355                         y_mem[2 * x] = colors_middle[x * 7 / width].y;
356                 y_mem += stride;
357         }
358
359         for (; y < height; ++y) {
360                 for (x = 0; x < width * 5 / 7; ++x)
361                         y_mem[2 * x] = colors_bottom[x * 4 / (width * 5 / 7)].y;
362                 for (; x < width * 6 / 7; ++x)
363                         y_mem[2 * x] = colors_bottom[(x - width * 5 / 7) * 3
364                                                                                  / (width / 7) + 4].y;
365                 for (; x < width; ++x)
366                         y_mem[2 * x] = colors_bottom[7].y;
367                 y_mem += stride;
368         }
369
370         /* Chroma */
371         for (y = 0; y < height * 6 / 9; ++y) {
372                 for (x = 0; x < width; x += 2) {
373                         c_mem[2 * x + u] = colors_top[x * 7 / width].u;
374                         c_mem[2 * x + v] = colors_top[x * 7 / width].v;
375                 }
376                 c_mem += stride;
377         }
378
379         for (; y < height * 7 / 9; ++y) {
380                 for (x = 0; x < width; x += 2) {
381                         c_mem[2 * x + u] = colors_middle[x * 7 / width].u;
382                         c_mem[2 * x + v] = colors_middle[x * 7 / width].v;
383                 }
384                 c_mem += stride;
385         }
386
387         for (; y < height; ++y) {
388                 for (x = 0; x < width * 5 / 7; x += 2) {
389                         c_mem[2 * x + u] = colors_bottom[x * 4 / (width * 5 / 7)].u;
390                         c_mem[2 * x + v] = colors_bottom[x * 4 / (width * 5 / 7)].v;
391                 }
392                 for (; x < width * 6 / 7; x += 2) {
393                         c_mem[2 * x + u] = colors_bottom[(x - width * 5 / 7) *
394                                                                                          3 / (width / 7) + 4].u;
395                         c_mem[2 * x + v] = colors_bottom[(x - width * 5 / 7) *
396                                                                                          3 / (width / 7) + 4].v;
397                 }
398                 for (; x < width; x += 2) {
399                         c_mem[2 * x + u] = colors_bottom[7].u;
400                         c_mem[2 * x + v] = colors_bottom[7].v;
401                 }
402                 c_mem += stride;
403         }
404 }
405
406 static void
407 fill_smpte_rgb16(const struct rgb_info *rgb, unsigned char *mem,
408                                  unsigned int width, unsigned int height, unsigned int stride)
409 {
410         const uint16_t colors_top[] = {
411                 MAKE_RGBA(rgb, 192, 192, 192, ALPHA_VALUE),     /* grey */
412                 MAKE_RGBA(rgb, 192, 192, 0, ALPHA_VALUE),       /* yellow */
413                 MAKE_RGBA(rgb, 0, 192, 192, ALPHA_VALUE),       /* cyan */
414                 MAKE_RGBA(rgb, 0, 192, 0, ALPHA_VALUE),         /* green */
415                 MAKE_RGBA(rgb, 192, 0, 192, ALPHA_VALUE),       /* magenta */
416                 MAKE_RGBA(rgb, 192, 0, 0, ALPHA_VALUE),         /* red */
417                 MAKE_RGBA(rgb, 0, 0, 192, ALPHA_VALUE),         /* blue */
418         };
419         const uint16_t colors_middle[] = {
420                 MAKE_RGBA(rgb, 0, 0, 192, ALPHA_VALUE),         /* blue */
421                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
422                 MAKE_RGBA(rgb, 192, 0, 192, ALPHA_VALUE),       /* magenta */
423                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
424                 MAKE_RGBA(rgb, 0, 192, 192, ALPHA_VALUE),       /* cyan */
425                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
426                 MAKE_RGBA(rgb, 192, 192, 192, ALPHA_VALUE),     /* grey */
427         };
428         const uint16_t colors_bottom[] = {
429                 MAKE_RGBA(rgb, 0, 33, 76, ALPHA_VALUE),         /* in-phase */
430                 MAKE_RGBA(rgb, 255, 255, 255, ALPHA_VALUE),     /* super white */
431                 MAKE_RGBA(rgb, 50, 0, 106, ALPHA_VALUE),        /* quadrature */
432                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
433                 MAKE_RGBA(rgb, 9, 9, 9, ALPHA_VALUE),           /* 3.5% */
434                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* 7.5% */
435                 MAKE_RGBA(rgb, 29, 29, 29, ALPHA_VALUE),        /* 11.5% */
436                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
437         };
438         unsigned int x;
439         unsigned int y;
440
441         if (width < 8)
442                 return;
443
444         for (y = 0; y < height * 6 / 9; ++y) {
445                 for (x = 0; x < width; ++x)
446                         ((uint16_t *)mem)[x] = colors_top[x * 7 / width];
447                 mem += stride;
448         }
449
450         for (; y < height * 7 / 9; ++y) {
451                 for (x = 0; x < width; ++x)
452                         ((uint16_t *)mem)[x] = colors_middle[x * 7 / width];
453                 mem += stride;
454         }
455
456         for (; y < height; ++y) {
457                 for (x = 0; x < width * 5 / 7; ++x)
458                         ((uint16_t *)mem)[x] =
459                                 colors_bottom[x * 4 / (width * 5 / 7)];
460                 for (; x < width * 6 / 7; ++x)
461                         ((uint16_t *)mem)[x] =
462                                 colors_bottom[(x - width * 5 / 7) * 3
463                                                           / (width / 7) + 4];
464                 for (; x < width; ++x)
465                         ((uint16_t *)mem)[x] = colors_bottom[7];
466                 mem += stride;
467         }
468 }
469
470 static void
471 fill_smpte_rgb24(const struct rgb_info *rgb, void *mem,
472                                  unsigned int width, unsigned int height, unsigned int stride)
473 {
474         const struct color_rgb24 colors_top[] = {
475                 MAKE_RGB24(rgb, 192, 192, 192), /* grey */
476                 MAKE_RGB24(rgb, 192, 192, 0),   /* yellow */
477                 MAKE_RGB24(rgb, 0, 192, 192),   /* cyan */
478                 MAKE_RGB24(rgb, 0, 192, 0),     /* green */
479                 MAKE_RGB24(rgb, 192, 0, 192),   /* magenta */
480                 MAKE_RGB24(rgb, 192, 0, 0),     /* red */
481                 MAKE_RGB24(rgb, 0, 0, 192),     /* blue */
482         };
483         const struct color_rgb24 colors_middle[] = {
484                 MAKE_RGB24(rgb, 0, 0, 192),     /* blue */
485                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
486                 MAKE_RGB24(rgb, 192, 0, 192),   /* magenta */
487                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
488                 MAKE_RGB24(rgb, 0, 192, 192),   /* cyan */
489                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
490                 MAKE_RGB24(rgb, 192, 192, 192), /* grey */
491         };
492         const struct color_rgb24 colors_bottom[] = {
493                 MAKE_RGB24(rgb, 0, 33, 76),     /* in-phase */
494                 MAKE_RGB24(rgb, 255, 255, 255), /* super white */
495                 MAKE_RGB24(rgb, 50, 0, 106),    /* quadrature */
496                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
497                 MAKE_RGB24(rgb, 9, 9, 9),       /* 3.5% */
498                 MAKE_RGB24(rgb, 19, 19, 19),    /* 7.5% */
499                 MAKE_RGB24(rgb, 29, 29, 29),    /* 11.5% */
500                 MAKE_RGB24(rgb, 19, 19, 19),    /* black */
501         };
502         unsigned int x;
503         unsigned int y;
504
505         if (width < 8)
506                 return;
507
508         for (y = 0; y < height * 6 / 9; ++y) {
509                 for (x = 0; x < width; ++x)
510                         ((struct color_rgb24 *)mem)[x] =
511                                 colors_top[x * 7 / width];
512                 mem += stride;
513         }
514
515         for (; y < height * 7 / 9; ++y) {
516                 for (x = 0; x < width; ++x)
517                         ((struct color_rgb24 *)mem)[x] =
518                                 colors_middle[x * 7 / width];
519                 mem += stride;
520         }
521
522         for (; y < height; ++y) {
523                 for (x = 0; x < width * 5 / 7; ++x)
524                         ((struct color_rgb24 *)mem)[x] =
525                                 colors_bottom[x * 4 / (width * 5 / 7)];
526                 for (; x < width * 6 / 7; ++x)
527                         ((struct color_rgb24 *)mem)[x] =
528                                 colors_bottom[(x - width * 5 / 7) * 3
529                                                           / (width / 7) + 4];
530                 for (; x < width; ++x)
531                         ((struct color_rgb24 *)mem)[x] = colors_bottom[7];
532                 mem += stride;
533         }
534 }
535
536 static void
537 fill_smpte_rgb32(const struct rgb_info *rgb, unsigned char *mem,
538                                  unsigned int width, unsigned int height, unsigned int stride)
539 {
540         const uint32_t colors_top[] = {
541                 MAKE_RGBA(rgb, 192, 192, 192, ALPHA_VALUE),     /* grey */
542                 MAKE_RGBA(rgb, 192, 192, 0, ALPHA_VALUE),       /* yellow */
543                 MAKE_RGBA(rgb, 0, 192, 192, ALPHA_VALUE),       /* cyan */
544                 MAKE_RGBA(rgb, 0, 192, 0, ALPHA_VALUE),         /* green */
545                 MAKE_RGBA(rgb, 192, 0, 192, ALPHA_VALUE),       /* magenta */
546                 MAKE_RGBA(rgb, 192, 0, 0, ALPHA_VALUE),         /* red */
547                 MAKE_RGBA(rgb, 0, 0, 192, ALPHA_VALUE),         /* blue */
548         };
549         const uint32_t colors_middle[] = {
550                 MAKE_RGBA(rgb, 0, 0, 192, ALPHA_VALUE),         /* blue */
551                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
552                 MAKE_RGBA(rgb, 192, 0, 192, ALPHA_VALUE),       /* magenta */
553                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
554                 MAKE_RGBA(rgb, 0, 192, 192, ALPHA_VALUE),       /* cyan */
555                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
556                 MAKE_RGBA(rgb, 192, 192, 192, ALPHA_VALUE),     /* grey */
557         };
558         const uint32_t colors_bottom[] = {
559                 MAKE_RGBA(rgb, 0, 33, 76, ALPHA_VALUE),         /* in-phase */
560                 MAKE_RGBA(rgb, 255, 255, 255, ALPHA_VALUE),     /* super white */
561                 MAKE_RGBA(rgb, 50, 0, 106, ALPHA_VALUE),        /* quadrature */
562                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
563                 MAKE_RGBA(rgb, 9, 9, 9, ALPHA_VALUE),           /* 3.5% */
564                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* 7.5% */
565                 MAKE_RGBA(rgb, 29, 29, 29, ALPHA_VALUE),        /* 11.5% */
566                 MAKE_RGBA(rgb, 19, 19, 19, ALPHA_VALUE),        /* black */
567         };
568         unsigned int x;
569         unsigned int y;
570
571         if (width < 8)
572                 return;
573
574         for (y = 0; y < height * 6 / 9; ++y) {
575                 for (x = 0; x < width; ++x)
576                         ((uint32_t *)mem)[x] = colors_top[x * 7 / width];
577                 mem += stride;
578         }
579
580         for (; y < height * 7 / 9; ++y) {
581                 for (x = 0; x < width; ++x)
582                         ((uint32_t *)mem)[x] = colors_middle[x * 7 / width];
583                 mem += stride;
584         }
585
586         for (; y < height; ++y) {
587                 for (x = 0; x < width * 5 / 7; ++x)
588                         ((uint32_t *)mem)[x] =
589                                 colors_bottom[x * 4 / (width * 5 / 7)];
590                 for (; x < width * 6 / 7; ++x)
591                         ((uint32_t *)mem)[x] =
592                                 colors_bottom[(x - width * 5 / 7) * 3
593                                                           / (width / 7) + 4];
594                 for (; x < width; ++x)
595                         ((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);
596                 mem += stride;
597         }
598 }
599
600 static void
601 fill_smpte_rgb32_dont_care_alpha(const struct rgb_info *rgb, unsigned char *mem,
602                                                                  unsigned int width, unsigned int height, unsigned int stride)
603 {
604         const uint32_t colors_top[] = {
605                 MAKE_RGBX(rgb, 192, 192, 192),  /* grey */
606                 MAKE_RGBX(rgb, 192, 192, 0),    /* yellow */
607                 MAKE_RGBX(rgb, 0, 192, 192),    /* cyan */
608                 MAKE_RGBX(rgb, 0, 192, 0),              /* green */
609                 MAKE_RGBX(rgb, 192, 0, 192),    /* magenta */
610                 MAKE_RGBX(rgb, 192, 0, 0),              /* red */
611                 MAKE_RGBX(rgb, 0, 0, 192),              /* blue */
612         };
613         const uint32_t colors_middle[] = {
614                 MAKE_RGBX(rgb, 0, 0, 192),              /* blue */
615                 MAKE_RGBX(rgb, 19, 19, 19),     /* black */
616                 MAKE_RGBX(rgb, 192, 0, 192),    /* magenta */
617                 MAKE_RGBX(rgb, 19, 19, 19),     /* black */
618                 MAKE_RGBX(rgb, 0, 192, 192),    /* cyan */
619                 MAKE_RGBX(rgb, 19, 19, 19),     /* black */
620                 MAKE_RGBX(rgb, 192, 192, 192),  /* grey */
621         };
622         const uint32_t colors_bottom[] = {
623                 MAKE_RGBX(rgb, 0, 33, 76),              /* in-phase */
624                 MAKE_RGBX(rgb, 255, 255, 255),  /* super white */
625                 MAKE_RGBX(rgb, 50, 0, 106),     /* quadrature */
626                 MAKE_RGBX(rgb, 19, 19, 19),     /* black */
627                 MAKE_RGBX(rgb, 9, 9, 9),                /* 3.5% */
628                 MAKE_RGBX(rgb, 19, 19, 19),     /* 7.5% */
629                 MAKE_RGBX(rgb, 29, 29, 29),     /* 11.5% */
630                 MAKE_RGBX(rgb, 19, 19, 19),     /* black */
631         };
632         unsigned int x;
633         unsigned int y;
634
635         if (width < 8)
636                 return;
637
638         for (y = 0; y < height * 6 / 9; ++y) {
639                 for (x = 0; x < width; ++x)
640                         ((uint32_t *)mem)[x] = colors_top[x * 7 / width];
641                 mem += stride;
642         }
643
644         for (; y < height * 7 / 9; ++y) {
645                 for (x = 0; x < width; ++x)
646                         ((uint32_t *)mem)[x] = colors_middle[x * 7 / width];
647                 mem += stride;
648         }
649
650         for (; y < height; ++y) {
651                 for (x = 0; x < width * 5 / 7; ++x)
652                         ((uint32_t *)mem)[x] =
653                                 colors_bottom[x * 4 / (width * 5 / 7)];
654                 for (; x < width * 6 / 7; ++x)
655                         ((uint32_t *)mem)[x] =
656                                 colors_bottom[(x - width * 5 / 7) * 3
657                                                           / (width / 7) + 4];
658                 for (; x < width; ++x)
659                         ((uint32_t *)mem)[x] = (rand_r(&rand_seed) % 2) ? MAKE_RGBX(rgb, 255, 255, 255) : MAKE_RGBX(rgb, 0, 0, 0);
660                 mem += stride;
661         }
662 }
663
664 static void
665 fill_smpte(const struct format_info *info, void *planes[3], unsigned int width,
666                    unsigned int height, unsigned int stride)
667 {
668         unsigned char *u, *v;
669
670         switch (info->format) {
671         case TBM_FORMAT_UYVY:
672         case TBM_FORMAT_VYUY:
673         case TBM_FORMAT_YUYV:
674         case TBM_FORMAT_YVYU:
675                 return fill_smpte_yuv_packed(&info->yuv, planes[0], width,
676                                                                          height, stride);
677
678         case TBM_FORMAT_NV12:
679         case TBM_FORMAT_NV21:
680         case TBM_FORMAT_NV16:
681         case TBM_FORMAT_NV61:
682                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
683                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
684                 return fill_smpte_yuv_planar(&info->yuv, planes[0], u, v,
685                                                                          width, height, stride);
686
687         case TBM_FORMAT_YUV420:
688                 return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[1],
689                                                                          planes[2], width, height, stride);
690
691         case TBM_FORMAT_YVU420:
692                 return fill_smpte_yuv_planar(&info->yuv, planes[0], planes[2],
693                                                                          planes[1], width, height, stride);
694
695         case TBM_FORMAT_ARGB4444:
696         case TBM_FORMAT_XRGB4444:
697         case TBM_FORMAT_ABGR4444:
698         case TBM_FORMAT_XBGR4444:
699         case TBM_FORMAT_RGBA4444:
700         case TBM_FORMAT_RGBX4444:
701         case TBM_FORMAT_BGRA4444:
702         case TBM_FORMAT_BGRX4444:
703         case TBM_FORMAT_RGB565:
704         case TBM_FORMAT_BGR565:
705         case TBM_FORMAT_ARGB1555:
706         case TBM_FORMAT_XRGB1555:
707         case TBM_FORMAT_ABGR1555:
708         case TBM_FORMAT_XBGR1555:
709         case TBM_FORMAT_RGBA5551:
710         case TBM_FORMAT_RGBX5551:
711         case TBM_FORMAT_BGRA5551:
712         case TBM_FORMAT_BGRX5551:
713                 return fill_smpte_rgb16(&info->rgb, planes[0],
714                                                                 width, height, stride);
715
716         case TBM_FORMAT_BGR888:
717         case TBM_FORMAT_RGB888:
718                 return fill_smpte_rgb24(&info->rgb, planes[0],
719                                                                 width, height, stride);
720         case TBM_FORMAT_ARGB8888:
721         case TBM_FORMAT_ABGR8888:
722         case TBM_FORMAT_XBGR8888:
723         case TBM_FORMAT_RGBA8888:
724         case TBM_FORMAT_RGBX8888:
725         case TBM_FORMAT_BGRA8888:
726         case TBM_FORMAT_BGRX8888:
727         case TBM_FORMAT_ARGB2101010:
728         case TBM_FORMAT_XRGB2101010:
729         case TBM_FORMAT_ABGR2101010:
730         case TBM_FORMAT_XBGR2101010:
731         case TBM_FORMAT_RGBA1010102:
732         case TBM_FORMAT_RGBX1010102:
733         case TBM_FORMAT_BGRA1010102:
734         case TBM_FORMAT_BGRX1010102:
735                 return fill_smpte_rgb32(&info->rgb, planes[0],
736                                                                 width, height, stride);
737         case TBM_FORMAT_XRGB8888:
738                 return fill_smpte_rgb32_dont_care_alpha(&info->rgb, planes[0],
739                                                                                                 width, height, stride);
740         }
741 }
742
743 static void
744 fill_tiles_yuv_planar(const struct format_info *info,
745                                           unsigned char *y_mem, unsigned char *u_mem,
746                                           unsigned char *v_mem, unsigned int width,
747                                           unsigned int height, unsigned int stride)
748 {
749         const struct yuv_info *yuv = &info->yuv;
750         unsigned int cs = yuv->chroma_stride;
751         unsigned int xsub = yuv->xsub;
752         unsigned int ysub = yuv->ysub;
753         unsigned int x;
754         unsigned int y;
755
756         for (y = 0; y < height; ++y) {
757                 for (x = 0; x < width; ++x) {
758                         div_t d = div(x + y, width);
759                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
760                                                          + 0x000a1120 * (d.rem >> 6);
761                         struct color_yuv color =
762                                 MAKE_YUV_601((rgb32 >> 16) & 0xff,
763                                                          (rgb32 >> 8) & 0xff, rgb32 & 0xff);
764
765                         y_mem[x] = color.y;
766                         u_mem[x / xsub * cs] = color.u;
767                         v_mem[x / xsub * cs] = color.v;
768                 }
769
770                 y_mem += stride;
771                 if ((y + 1) % ysub == 0) {
772                         u_mem += stride * cs / xsub;
773                         v_mem += stride * cs / xsub;
774                 }
775         }
776 }
777
778 static void
779 fill_tiles_yuv_packed(const struct format_info *info, unsigned char *mem,
780                                           unsigned int width, unsigned int height,
781                                           unsigned int stride)
782 {
783         const struct yuv_info *yuv = &info->yuv;
784         unsigned char *y_mem = (yuv->order & YUV_YC) ? mem : mem + 1;
785         unsigned char *c_mem = (yuv->order & YUV_CY) ? mem : mem + 1;
786         unsigned int u = (yuv->order & YUV_YCrCb) ? 2 : 0;
787         unsigned int v = (yuv->order & YUV_YCbCr) ? 2 : 0;
788         unsigned int x;
789         unsigned int y;
790
791         for (y = 0; y < height; ++y) {
792                 for (x = 0; x < width; x += 2) {
793                         div_t d = div(x + y, width);
794                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
795                                                          + 0x000a1120 * (d.rem >> 6);
796                         struct color_yuv color =
797                                 MAKE_YUV_601((rgb32 >> 16) & 0xff,
798                                                          (rgb32 >> 8) & 0xff, rgb32 & 0xff);
799
800                         y_mem[2 * x] = color.y;
801                         c_mem[2 * x + u] = color.u;
802                         y_mem[2 * x + 2] = color.y;
803                         c_mem[2 * x + v] = color.v;
804                 }
805
806                 y_mem += stride;
807                 c_mem += stride;
808         }
809 }
810
811 static void
812 fill_tiles_rgb16(const struct format_info *info, unsigned char *mem,
813                                  unsigned int width, unsigned int height, unsigned int stride)
814 {
815         const struct rgb_info *rgb = &info->rgb;
816         unsigned int x, y;
817
818         for (y = 0; y < height; ++y) {
819                 for (x = 0; x < width; ++x) {
820                         div_t d = div(x + y, width);
821                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
822                                                          + 0x000a1120 * (d.rem >> 6);
823                         uint16_t color =
824                                 MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
825                                                   (rgb32 >> 8) & 0xff, rgb32 & 0xff,
826                                                   ALPHA_VALUE);
827
828                         ((uint16_t *)mem)[x] = color;
829                 }
830                 mem += stride;
831         }
832 }
833
834 static void
835 fill_tiles_rgb24(const struct format_info *info, unsigned char *mem,
836                                  unsigned int width, unsigned int height, unsigned int stride)
837 {
838         const struct rgb_info *rgb = &info->rgb;
839         unsigned int x, y;
840
841         for (y = 0; y < height; ++y) {
842                 for (x = 0; x < width; ++x) {
843                         div_t d = div(x + y, width);
844                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
845                                                          + 0x000a1120 * (d.rem >> 6);
846                         struct color_rgb24 color =
847                                 MAKE_RGB24(rgb, (rgb32 >> 16) & 0xff,
848                                                    (rgb32 >> 8) & 0xff, rgb32 & 0xff);
849
850                         ((struct color_rgb24 *)mem)[x] = color;
851                 }
852                 mem += stride;
853         }
854 }
855
856 static void
857 fill_tiles_rgb32(const struct format_info *info, unsigned char *mem,
858                                  unsigned int width, unsigned int height, unsigned int stride)
859 {
860         const struct rgb_info *rgb = &info->rgb;
861         unsigned int x, y;
862
863         for (y = 0; y < height; ++y) {
864                 for (x = 0; x < width; ++x) {
865                         div_t d = div(x + y, width);
866                         uint32_t rgb32 = 0x00130502 * (d.quot >> 6)
867                                                          + 0x000a1120 * (d.rem >> 6);
868                         uint32_t color =
869                                 MAKE_RGBA(rgb, (rgb32 >> 16) & 0xff,
870                                                   (rgb32 >> 8) & 0xff, rgb32 & 0xff,
871                                                   ALPHA_VALUE);
872
873                         ((uint32_t *)mem)[x] = color;
874                 }
875                 mem += stride;
876         }
877 }
878
879 static void
880 fill_tiles(const struct format_info *info, void *planes[3], unsigned int width,
881                    unsigned int height, unsigned int stride)
882 {
883         unsigned char *u, *v;
884
885         switch (info->format) {
886         case TBM_FORMAT_UYVY:
887         case TBM_FORMAT_VYUY:
888         case TBM_FORMAT_YUYV:
889         case TBM_FORMAT_YVYU:
890                 return fill_tiles_yuv_packed(info, planes[0],
891                                                                          width, height, stride);
892
893         case TBM_FORMAT_NV12:
894         case TBM_FORMAT_NV21:
895         case TBM_FORMAT_NV16:
896         case TBM_FORMAT_NV61:
897                 u = info->yuv.order & YUV_YCbCr ? planes[1] : planes[1] + 1;
898                 v = info->yuv.order & YUV_YCrCb ? planes[1] : planes[1] + 1;
899                 return fill_tiles_yuv_planar(info, planes[0], u, v,
900                                                                          width, height, stride);
901
902         case TBM_FORMAT_YUV420:
903                 return fill_tiles_yuv_planar(info, planes[0], planes[1],
904                                                                          planes[2], width, height, stride);
905
906         case TBM_FORMAT_YVU420:
907                 return fill_tiles_yuv_planar(info, planes[0], planes[2],
908                                                                          planes[1], width, height, stride);
909
910         case TBM_FORMAT_ARGB4444:
911         case TBM_FORMAT_XRGB4444:
912         case TBM_FORMAT_ABGR4444:
913         case TBM_FORMAT_XBGR4444:
914         case TBM_FORMAT_RGBA4444:
915         case TBM_FORMAT_RGBX4444:
916         case TBM_FORMAT_BGRA4444:
917         case TBM_FORMAT_BGRX4444:
918         case TBM_FORMAT_RGB565:
919         case TBM_FORMAT_BGR565:
920         case TBM_FORMAT_ARGB1555:
921         case TBM_FORMAT_XRGB1555:
922         case TBM_FORMAT_ABGR1555:
923         case TBM_FORMAT_XBGR1555:
924         case TBM_FORMAT_RGBA5551:
925         case TBM_FORMAT_RGBX5551:
926         case TBM_FORMAT_BGRA5551:
927         case TBM_FORMAT_BGRX5551:
928                 return fill_tiles_rgb16(info, planes[0],
929                                                                 width, height, stride);
930
931         case TBM_FORMAT_BGR888:
932         case TBM_FORMAT_RGB888:
933                 return fill_tiles_rgb24(info, planes[0],
934                                                                 width, height, stride);
935         case TBM_FORMAT_ARGB8888:
936         case TBM_FORMAT_XRGB8888:
937         case TBM_FORMAT_ABGR8888:
938         case TBM_FORMAT_XBGR8888:
939         case TBM_FORMAT_RGBA8888:
940         case TBM_FORMAT_RGBX8888:
941         case TBM_FORMAT_BGRA8888:
942         case TBM_FORMAT_BGRX8888:
943         case TBM_FORMAT_ARGB2101010:
944         case TBM_FORMAT_XRGB2101010:
945         case TBM_FORMAT_ABGR2101010:
946         case TBM_FORMAT_XBGR2101010:
947         case TBM_FORMAT_RGBA1010102:
948         case TBM_FORMAT_RGBX1010102:
949         case TBM_FORMAT_BGRA1010102:
950         case TBM_FORMAT_BGRX1010102:
951                 return fill_tiles_rgb32(info, planes[0],
952                                                                 width, height, stride);
953         }
954 }
955
956 static void
957 fill_plain(const struct format_info *info, void *planes[3], unsigned int width,
958                    unsigned int height, unsigned int stride)
959 {
960         memset(planes[0], 0x77, stride * height);
961 }
962
963 /*
964  * fill_pattern - Fill a buffer with a test pattern
965  * @format: Pixel format
966  * @pattern: Test pattern
967  * @buffer: Buffer memory
968  * @width: Width in pixels
969  * @height: Height in pixels
970  * @stride: Line stride (pitch) in bytes
971  *
972  * Fill the buffer with the test pattern specified by the pattern parameter.
973  * Supported formats vary depending on the selected pattern.
974  */
975 static void
976 fill_pattern(unsigned int format, enum fill_pattern pattern, void *planes[3],
977                          unsigned int width, unsigned int height, unsigned int stride)
978 {
979         const struct format_info *info = NULL;
980         unsigned int i;
981
982         for (i = 0; i < ARRAY_SIZE(format_info); ++i) {
983                 if (format_info[i].format == format) {
984                         info = &format_info[i];
985                         break;
986                 }
987         }
988
989         if (info == NULL)
990                 return;
991
992         switch (pattern) {
993         case PATTERN_TILES:
994                 return fill_tiles(info, planes, width, height, stride);
995
996         case PATTERN_SMPTE:
997                 return fill_smpte(info, planes, width, height, stride);
998
999         case PATTERN_PLAIN:
1000                 return fill_plain(info, planes, width, height, stride);
1001
1002         default:
1003                 printf("Error: unsupported test pattern %u.\n", pattern);
1004                 break;
1005         }
1006 }
1007
1008 void
1009 tdm_test_buffer_fill(tbm_surface_h buffer, int pattern)
1010 {
1011         tbm_surface_info_s info;
1012         void *plane[3];
1013         int ret;
1014
1015         if (rand_seed == 0)
1016                 rand_seed = time(NULL);
1017
1018         ret = tbm_surface_map(buffer, TBM_OPTION_WRITE, &info);
1019         assert(ret == 0);
1020
1021         plane[0] = info.planes[0].ptr;
1022         plane[1] = info.planes[1].ptr;
1023         plane[2] = info.planes[2].ptr;
1024         fill_pattern(info.format, pattern, plane, info.width, info.height, info.planes[0].stride);
1025         tbm_surface_unmap(buffer);
1026 }
1027
1028 /* LCOV_EXCL_END */