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