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