libkmstest: disable draw perf prints
[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"
17 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
19 namespace kms
20 {
21 static void draw_rgb_pixel(DumbFramebuffer& buf, unsigned x, unsigned y, RGB color)
22 {
23         switch (buf.format()) {
24         case PixelFormat::XRGB8888:
25         case PixelFormat::ARGB8888:
26         {
27                 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
28                 *p = color.rgb888();
29                 break;
30         }
31         case PixelFormat::XBGR8888:
32         case PixelFormat::ABGR8888:
33         {
34                 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
35                 *p = color.bgr888();
36                 break;
37         }
38         case PixelFormat::RGB565:
39         {
40                 uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
41                 *p = color.rgb565();
42                 break;
43         }
44         default:
45                 throw std::invalid_argument("invalid pixelformat");
46         }
47 }
49 static void draw_yuv422_macropixel(DumbFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2)
50 {
51         ASSERT((x & 1) == 0);
53         uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
55         uint8_t y0 = yuv1.y;
56         uint8_t y1 = yuv2.y;
57         uint8_t u = (yuv1.u + yuv2.u) / 2;
58         uint8_t v = (yuv1.v + yuv2.v) / 2;
60         switch (buf.format()) {
61         case PixelFormat::UYVY:
62                 p[0] = u;
63                 p[1] = y0;
64                 p[2] = v;
65                 p[3] = y1;
66                 break;
68         case PixelFormat::YUYV:
69                 p[0] = y0;
70                 p[1] = u;
71                 p[2] = y1;
72                 p[3] = v;
73                 break;
75         case PixelFormat::YVYU:
76                 p[0] = y0;
77                 p[1] = v;
78                 p[2] = y1;
79                 p[3] = u;
80                 break;
82         case PixelFormat::VYUY:
83                 p[0] = v;
84                 p[1] = y0;
85                 p[2] = u;
86                 p[3] = y1;
87                 break;
89         default:
90                 throw std::invalid_argument("invalid pixelformat");
91         }
92 }
94 static void draw_yuv420_macropixel(DumbFramebuffer& buf, unsigned x, unsigned y,
95                                    YUV yuv1, YUV yuv2, YUV yuv3, YUV yuv4)
96 {
97         ASSERT((x & 1) == 0);
98         ASSERT((y & 1) == 0);
100         uint8_t *py1 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 0) + x);
101         uint8_t *py2 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 1) + x);
103         uint8_t *puv = (uint8_t*)(buf.map(1) + buf.stride(1) * (y / 2) + x);
105         uint8_t y0 = yuv1.y;
106         uint8_t y1 = yuv2.y;
107         uint8_t y2 = yuv3.y;
108         uint8_t y3 = yuv4.y;
109         uint8_t u = (yuv1.u + yuv2.u + yuv3.u + yuv4.u) / 4;
110         uint8_t v = (yuv1.v + yuv2.v + yuv3.v + yuv4.v) / 4;
112         switch (buf.format()) {
113         case PixelFormat::NV12:
114                 py1[0] = y0;
115                 py1[1] = y1;
116                 py2[0] = y2;
117                 py2[1] = y3;
118                 puv[0] = u;
119                 puv[1] = v;
120                 break;
122         case PixelFormat::NV21:
123                 py1[0] = y0;
124                 py1[1] = y1;
125                 py2[0] = y2;
126                 py2[1] = y3;
127                 puv[0] = v;
128                 puv[1] = u;
129                 break;
131         default:
132                 throw std::invalid_argument("invalid pixelformat");
133         }
136 static RGB get_test_pattern_pixel(DumbFramebuffer& fb, unsigned x, unsigned y)
138         const unsigned w = fb.width();
139         const unsigned h = fb.height();
141         const unsigned mw = 20;
143         const unsigned xm1 = mw;
144         const unsigned xm2 = w - mw - 1;
145         const unsigned ym1 = mw;
146         const unsigned ym2 = h - mw - 1;
148         // white margin lines
149         if (x == xm1 || x == xm2 || y == ym1 || y == ym2)
150                 return RGB(255, 255, 255);
151         // white box outlines to corners
152         else if ((x == 0 || x == w - 1) && (y < ym1 || y > ym2))
153                 return RGB(255, 255, 255);
154         // white box outlines to corners
155         else if ((y == 0 || y == h - 1) && (x < xm1 || x > xm2))
156                 return RGB(255, 255, 255);
157         // blue bar on the left
158         else if (x < xm1 && (y > ym1 && y < ym2))
159                 return RGB(0, 0, 255);
160         // blue bar on the top
161         else if (y < ym1 && (x > xm1 && x < xm2))
162                 return RGB(0, 0, 255);
163         // red bar on the right
164         else if (x > xm2 && (y > ym1 && y < ym2))
165                 return RGB(255, 0, 0);
166         // red bar on the bottom
167         else if (y > ym2 && (x > xm1 && x < xm2))
168                 return RGB(255, 0, 0);
169         // inside the margins
170         else if (x > xm1 && x < xm2 && y > ym1 && y < ym2) {
171                 // diagonal line
172                 if (x == y || w - x == h - y)
173                         return RGB(255, 255, 255);
174                 // diagonal line
175                 else if (w - x == y || x == h - y)
176                         return RGB(255, 255, 255);
177                 else {
178                         int t = (x - xm1 - 1) * 8 / (xm2 - xm1 - 1);
179                         unsigned r = 0, g = 0, b = 0;
181                         unsigned c = (y - ym1 - 1) % 256;
183                         switch (t) {
184                         case 0:
185                                 r = c;
186                                 break;
187                         case 1:
188                                 g = c;
189                                 break;
190                         case 2:
191                                 b = c;
192                                 break;
193                         case 3:
194                                 g = b = c;
195                                 break;
196                         case 4:
197                                 r = b = c;
198                                 break;
199                         case 5:
200                                 r = g = c;
201                                 break;
202                         case 6:
203                                 r = g = b = c;
204                                 break;
205                         case 7:
206                                 break;
207                         }
209                         return RGB(r, g, b);
210                 }
211         } else {
212                 // black corners
213                 return RGB(0, 0, 0);
214         }
217 static void draw_test_pattern_impl(DumbFramebuffer& fb)
219         unsigned x, y;
220         unsigned w = fb.width();
221         unsigned h = fb.height();
223         switch (fb.format()) {
224         case PixelFormat::XRGB8888:
225         case PixelFormat::XBGR8888:
226         case PixelFormat::ARGB8888:
227         case PixelFormat::ABGR8888:
228         case PixelFormat::RGB565:
229                 for (y = 0; y < h; y++) {
230                         for (x = 0; x < w; x++) {
231                                 RGB pixel = get_test_pattern_pixel(fb, x, y);
232                                 draw_rgb_pixel(fb, x, y, pixel);
233                         }
234                 }
235                 break;
237         case PixelFormat::UYVY:
238         case PixelFormat::YUYV:
239         case PixelFormat::YVYU:
240         case PixelFormat::VYUY:
241                 for (y = 0; y < h; y++) {
242                         for (x = 0; x < w; x += 2) {
243                                 RGB pixel1 = get_test_pattern_pixel(fb, x, y);
244                                 RGB pixel2 = get_test_pattern_pixel(fb, x + 1, y);
245                                 draw_yuv422_macropixel(fb, x, y, pixel1.yuv(), pixel2.yuv());
246                         }
247                 }
248                 break;
250         case PixelFormat::NV12:
251         case PixelFormat::NV21:
252                 for (y = 0; y < h; y += 2) {
253                         for (x = 0; x < w; x += 2) {
254                                 RGB pixel00 = get_test_pattern_pixel(fb, x, y);
255                                 RGB pixel10 = get_test_pattern_pixel(fb, x + 1, y);
256                                 RGB pixel01 = get_test_pattern_pixel(fb, x, y + 1);
257                                 RGB pixel11 = get_test_pattern_pixel(fb, x + 1, y + 1);
258                                 draw_yuv420_macropixel(fb, x, y,
259                                                        pixel00.yuv(), pixel10.yuv(),
260                                                        pixel01.yuv(), pixel11.yuv());
261                         }
262                 }
263                 break;
264         default:
265                 throw std::invalid_argument("unknown pixelformat");
266         }
269 void draw_test_pattern(DumbFramebuffer& fb)
271 #ifdef DRAW_PERF_PRINT
272         using namespace std::chrono;
274         auto t1 = high_resolution_clock::now();
275 #endif
277         draw_test_pattern_impl(fb);
279 #ifdef DRAW_PERF_PRINT
280         auto t2 = high_resolution_clock::now();
281         auto time_span = duration_cast<microseconds>(t2 - t1);
283         printf("draw took %u us\n", (unsigned)time_span.count());
284 #endif