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