libkmstest: remove dependency to libdrm
[android/external-libkmsxx.git] / libkmstest / testpat.cpp
2 //#define DRAW_PERF_PRINT
4 #include <chrono>
5 #include <cstring>
6 #include <cassert>
8 #include "kms++.h"
9 #include "test.h"
10 #include "cpuframebuffer.h"
12 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
14 namespace kms
15 {
16 static void draw_rgb_pixel(IMappedFramebuffer& buf, unsigned x, unsigned y, RGB color)
17 {
18         switch (buf.format()) {
19         case PixelFormat::XRGB8888:
20         case PixelFormat::ARGB8888:
21         {
22                 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
23                 *p = color.argb8888();
24                 break;
25         }
26         case PixelFormat::XBGR8888:
27         case PixelFormat::ABGR8888:
28         {
29                 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
30                 *p = color.abgr8888();
31                 break;
32         }
33         case PixelFormat::RGB565:
34         {
35                 uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
36                 *p = color.rgb565();
37                 break;
38         }
39         default:
40                 throw std::invalid_argument("invalid pixelformat");
41         }
42 }
44 static void draw_yuv422_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2)
45 {
46         ASSERT((x & 1) == 0);
48         uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
50         uint8_t y0 = yuv1.y;
51         uint8_t y1 = yuv2.y;
52         uint8_t u = (yuv1.u + yuv2.u) / 2;
53         uint8_t v = (yuv1.v + yuv2.v) / 2;
55         switch (buf.format()) {
56         case PixelFormat::UYVY:
57                 p[0] = u;
58                 p[1] = y0;
59                 p[2] = v;
60                 p[3] = y1;
61                 break;
63         case PixelFormat::YUYV:
64                 p[0] = y0;
65                 p[1] = u;
66                 p[2] = y1;
67                 p[3] = v;
68                 break;
70         case PixelFormat::YVYU:
71                 p[0] = y0;
72                 p[1] = v;
73                 p[2] = y1;
74                 p[3] = u;
75                 break;
77         case PixelFormat::VYUY:
78                 p[0] = v;
79                 p[1] = y0;
80                 p[2] = u;
81                 p[3] = y1;
82                 break;
84         default:
85                 throw std::invalid_argument("invalid pixelformat");
86         }
87 }
89 static void draw_yuv420_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y,
90                                    YUV yuv1, YUV yuv2, YUV yuv3, YUV yuv4)
91 {
92         ASSERT((x & 1) == 0);
93         ASSERT((y & 1) == 0);
95         uint8_t *py1 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 0) + x);
96         uint8_t *py2 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 1) + x);
98         uint8_t *puv = (uint8_t*)(buf.map(1) + buf.stride(1) * (y / 2) + x);
100         uint8_t y0 = yuv1.y;
101         uint8_t y1 = yuv2.y;
102         uint8_t y2 = yuv3.y;
103         uint8_t y3 = yuv4.y;
104         uint8_t u = (yuv1.u + yuv2.u + yuv3.u + yuv4.u) / 4;
105         uint8_t v = (yuv1.v + yuv2.v + yuv3.v + yuv4.v) / 4;
107         switch (buf.format()) {
108         case PixelFormat::NV12:
109                 py1[0] = y0;
110                 py1[1] = y1;
111                 py2[0] = y2;
112                 py2[1] = y3;
113                 puv[0] = u;
114                 puv[1] = v;
115                 break;
117         case PixelFormat::NV21:
118                 py1[0] = y0;
119                 py1[1] = y1;
120                 py2[0] = y2;
121                 py2[1] = y3;
122                 puv[0] = v;
123                 puv[1] = u;
124                 break;
126         default:
127                 throw std::invalid_argument("invalid pixelformat");
128         }
131 static RGB get_test_pattern_pixel(IMappedFramebuffer& fb, unsigned x, unsigned y)
133         const unsigned w = fb.width();
134         const unsigned h = fb.height();
136         const unsigned mw = 20;
138         const unsigned xm1 = mw;
139         const unsigned xm2 = w - mw - 1;
140         const unsigned ym1 = mw;
141         const unsigned ym2 = h - mw - 1;
143         // white margin lines
144         if (x == xm1 || x == xm2 || y == ym1 || y == ym2)
145                 return RGB(255, 255, 255);
146         // white box outlines to corners
147         else if ((x == 0 || x == w - 1) && (y < ym1 || y > ym2))
148                 return RGB(255, 255, 255);
149         // white box outlines to corners
150         else if ((y == 0 || y == h - 1) && (x < xm1 || x > xm2))
151                 return RGB(255, 255, 255);
152         // blue bar on the left
153         else if (x < xm1 && (y > ym1 && y < ym2))
154                 return RGB(0, 0, 255);
155         // blue bar on the top
156         else if (y < ym1 && (x > xm1 && x < xm2))
157                 return RGB(0, 0, 255);
158         // red bar on the right
159         else if (x > xm2 && (y > ym1 && y < ym2))
160                 return RGB(255, 0, 0);
161         // red bar on the bottom
162         else if (y > ym2 && (x > xm1 && x < xm2))
163                 return RGB(255, 0, 0);
164         // inside the margins
165         else if (x > xm1 && x < xm2 && y > ym1 && y < ym2) {
166                 // diagonal line
167                 if (x == y || w - x == h - y)
168                         return RGB(255, 255, 255);
169                 // diagonal line
170                 else if (w - x == y || x == h - y)
171                         return RGB(255, 255, 255);
172                 else {
173                         int t = (x - xm1 - 1) * 8 / (xm2 - xm1 - 1);
174                         unsigned r = 0, g = 0, b = 0;
176                         unsigned c = (y - ym1 - 1) % 256;
178                         switch (t) {
179                         case 0:
180                                 r = c;
181                                 break;
182                         case 1:
183                                 g = c;
184                                 break;
185                         case 2:
186                                 b = c;
187                                 break;
188                         case 3:
189                                 g = b = c;
190                                 break;
191                         case 4:
192                                 r = b = c;
193                                 break;
194                         case 5:
195                                 r = g = c;
196                                 break;
197                         case 6:
198                                 r = g = b = c;
199                                 break;
200                         case 7:
201                                 break;
202                         }
204                         return RGB(r, g, b);
205                 }
206         } else {
207                 // black corners
208                 return RGB(0, 0, 0);
209         }
212 static void draw_test_pattern_impl(IMappedFramebuffer& fb)
214         unsigned x, y;
215         unsigned w = fb.width();
216         unsigned h = fb.height();
218         switch (fb.format()) {
219         case PixelFormat::XRGB8888:
220         case PixelFormat::XBGR8888:
221         case PixelFormat::ARGB8888:
222         case PixelFormat::ABGR8888:
223         case PixelFormat::RGB565:
224                 for (y = 0; y < h; y++) {
225                         for (x = 0; x < w; x++) {
226                                 RGB pixel = get_test_pattern_pixel(fb, x, y);
227                                 draw_rgb_pixel(fb, x, y, pixel);
228                         }
229                 }
230                 break;
232         case PixelFormat::UYVY:
233         case PixelFormat::YUYV:
234         case PixelFormat::YVYU:
235         case PixelFormat::VYUY:
236                 for (y = 0; y < h; y++) {
237                         for (x = 0; x < w; x += 2) {
238                                 RGB pixel1 = get_test_pattern_pixel(fb, x, y);
239                                 RGB pixel2 = get_test_pattern_pixel(fb, x + 1, y);
240                                 draw_yuv422_macropixel(fb, x, y, pixel1.yuv(), pixel2.yuv());
241                         }
242                 }
243                 break;
245         case PixelFormat::NV12:
246         case PixelFormat::NV21:
247                 for (y = 0; y < h; y += 2) {
248                         for (x = 0; x < w; x += 2) {
249                                 RGB pixel00 = get_test_pattern_pixel(fb, x, y);
250                                 RGB pixel10 = get_test_pattern_pixel(fb, x + 1, y);
251                                 RGB pixel01 = get_test_pattern_pixel(fb, x, y + 1);
252                                 RGB pixel11 = get_test_pattern_pixel(fb, x + 1, y + 1);
253                                 draw_yuv420_macropixel(fb, x, y,
254                                                        pixel00.yuv(), pixel10.yuv(),
255                                                        pixel01.yuv(), pixel11.yuv());
256                         }
257                 }
258                 break;
259         default:
260                 throw std::invalid_argument("unknown pixelformat");
261         }
264 void draw_test_pattern(IMappedFramebuffer &fb)
266 #ifdef DRAW_PERF_PRINT
267         using namespace std::chrono;
269         auto t1 = high_resolution_clock::now();
270 #endif
272         draw_test_pattern_impl(fb);
274 #ifdef DRAW_PERF_PRINT
275         auto t2 = high_resolution_clock::now();
276         auto time_span = duration_cast<microseconds>(t2 - t1);
278         printf("draw took %u us\n", (unsigned)time_span.count());
279 #endif
282 void draw_rect(IMappedFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color)
284         for (unsigned i = x; i < x + w; ++i) {
285                 for (unsigned j = y; j < y + h; ++j) {
286                         draw_rgb_pixel(fb, i, j, color);
287                 }
288         }