testpat: add NV12/NV21 support
[android/external-libkmsxx.git] / libkmstest / testpat.cpp
2 #include <chrono>
3 #include <cstring>
4 #include <cassert>
6 #include <xf86drm.h>
7 #include <xf86drmMode.h>
8 #include <drm_fourcc.h>
9 #include <drm.h>
10 #include <drm_mode.h>
12 #include "kms++.h"
13 #include "test.h"
15 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
17 namespace kms
18 {
19 static void draw_rgb_pixel(DumbFramebuffer& buf, unsigned x, unsigned y, RGB color)
20 {
21         switch (buf.format()) {
22         case PixelFormat::XRGB8888:
23         {
24                 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
25                 *p = color.rgb888();
26                 break;
27         }
28         case PixelFormat::XBGR8888:
29         {
30                 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
31                 *p = color.bgr888();
32                 break;
33         }
34         case PixelFormat::RGB565:
35         {
36                 uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
37                 *p = color.rgb565();
38                 break;
39         }
40         default:
41                 throw std::invalid_argument("invalid pixelformat");
42         }
43 }
45 static void draw_yuv422_macropixel(DumbFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2)
46 {
47         ASSERT((x & 1) == 0);
49         uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
51         uint8_t y0 = yuv1.y;
52         uint8_t y1 = yuv2.y;
53         uint8_t u = (yuv1.u + yuv2.u) / 2;
54         uint8_t v = (yuv1.v + yuv2.v) / 2;
56         switch (buf.format()) {
57         case PixelFormat::UYVY:
58                 p[0] = u;
59                 p[1] = y0;
60                 p[2] = v;
61                 p[3] = y1;
62                 break;
64         case PixelFormat::YUYV:
65                 p[0] = y0;
66                 p[1] = u;
67                 p[2] = y1;
68                 p[3] = v;
69                 break;
71         case PixelFormat::YVYU:
72                 p[0] = y0;
73                 p[1] = v;
74                 p[2] = y1;
75                 p[3] = u;
76                 break;
78         case PixelFormat::VYUY:
79                 p[0] = v;
80                 p[1] = y0;
81                 p[2] = u;
82                 p[3] = y1;
83                 break;
85         default:
86                 throw std::invalid_argument("invalid pixelformat");
87         }
88 }
90 static void draw_yuv420_macropixel(DumbFramebuffer& buf, unsigned x, unsigned y,
91                                    YUV yuv1, YUV yuv2, YUV yuv3, YUV yuv4)
92 {
93         ASSERT((x & 1) == 0);
94         ASSERT((y & 1) == 0);
96         uint8_t *py1 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 0) + x);
97         uint8_t *py2 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 1) + x);
99         uint8_t *puv = (uint8_t*)(buf.map(1) + buf.stride(1) * (y / 2) + x);
101         uint8_t y0 = yuv1.y;
102         uint8_t y1 = yuv2.y;
103         uint8_t y2 = yuv3.y;
104         uint8_t y3 = yuv4.y;
105         uint8_t u = (yuv1.u + yuv2.u + yuv3.u + yuv4.u) / 4;
106         uint8_t v = (yuv1.v + yuv2.v + yuv3.v + yuv4.v) / 4;
108         switch (buf.format()) {
109         case PixelFormat::NV12:
110                 py1[0] = y0;
111                 py1[1] = y1;
112                 py2[0] = y2;
113                 py2[1] = y3;
114                 puv[0] = u;
115                 puv[1] = v;
116                 break;
118         case PixelFormat::NV21:
119                 py1[0] = y0;
120                 py1[1] = y1;
121                 py2[0] = y2;
122                 py2[1] = y3;
123                 puv[0] = v;
124                 puv[1] = u;
125                 break;
127         default:
128                 throw std::invalid_argument("invalid pixelformat");
129         }
132 static RGB get_test_pattern_pixel(DumbFramebuffer& fb, unsigned x, unsigned y)
134         unsigned w = fb.width();
135         unsigned h = fb.height();
137         const unsigned mw = 20;
139         const unsigned xm1 = mw;
140         const unsigned xm2 = w - mw - 1;
141         const unsigned ym1 = mw;
142         const unsigned ym2 = h - mw - 1;
144         // white margin lines
145         if (x == xm1 || x == xm2 || y == ym1 || y == ym2)
146                 return RGB(255, 255, 255);
147         // white box outlines to corners
148         else if ((x == 0 || x == w - 1) && (y < ym1 || y > ym2))
149                 return RGB(255, 255, 255);
150         // white box outlines to corners
151         else if ((y == 0 || y == h - 1) && (x < xm1 || x > xm2))
152                 return RGB(255, 255, 255);
153         // blue bar on the left
154         else if (x < xm1 && (y > ym1 && y < ym2))
155                 return RGB(0, 0, 255);
156         // blue bar on the top
157         else if (y < ym1 && (x > xm1 && x < xm2))
158                 return RGB(0, 0, 255);
159         // red bar on the right
160         else if (x > xm2 && (y > ym1 && y < ym2))
161                 return RGB(255, 0, 0);
162         // red bar on the bottom
163         else if (y > ym2 && (x > xm1 && x < xm2))
164                 return RGB(255, 0, 0);
165         // inside the margins
166         else if (x > xm1 && x < xm2 && y > ym1 && y < ym2) {
167                 // diagonal line
168                 if (x == y || w - x == h - y)
169                         return RGB(255, 255, 255);
170                 // diagonal line
171                 else if (w - x == y || x == h - y)
172                         return RGB(255, 255, 255);
173                 else {
174                         int t = (x - xm1 - 1) * 3 / (xm2 - xm1 - 1);
175                         unsigned r = 0, g = 0, b = 0;
177                         unsigned c = (y - ym1 - 1) % 256;
179                         switch (t) {
180                         case 0:
181                                 r = c;
182                                 break;
183                         case 1:
184                                 g = c;
185                                 break;
186                         case 2:
187                                 b = c;
188                                 break;
189                         }
191                         return RGB(r, g, b);
192                 }
193         } else {
194                 // black corners
195                 return RGB(0, 0, 0);
196         }
199 static void draw_test_pattern_impl(DumbFramebuffer& fb)
201         unsigned x, y;
202         unsigned w = fb.width();
203         unsigned h = fb.height();
205         switch (fb.format()) {
206         case PixelFormat::XRGB8888:
207         case PixelFormat::XBGR8888:
208         case PixelFormat::RGB565:
209                 for (y = 0; y < h; y++) {
210                         for (x = 0; x < w; x++) {
211                                 RGB pixel = get_test_pattern_pixel(fb, x, y);
212                                 draw_rgb_pixel(fb, x, y, pixel);
213                         }
214                 }
215                 break;
217         case PixelFormat::UYVY:
218         case PixelFormat::YUYV:
219         case PixelFormat::YVYU:
220         case PixelFormat::VYUY:
221                 for (y = 0; y < h; y++) {
222                         for (x = 0; x < w; x += 2) {
223                                 RGB pixel1 = get_test_pattern_pixel(fb, x, y);
224                                 RGB pixel2 = get_test_pattern_pixel(fb, x + 1, y);
225                                 draw_yuv422_macropixel(fb, x, y, pixel1.yuv(), pixel2.yuv());
226                         }
227                 }
228                 break;
230         case PixelFormat::NV12:
231         case PixelFormat::NV21:
232                 for (y = 0; y < h; y += 2) {
233                         for (x = 0; x < w; x += 2) {
234                                 RGB pixel00 = get_test_pattern_pixel(fb, x, y);
235                                 RGB pixel10 = get_test_pattern_pixel(fb, x + 1, y);
236                                 RGB pixel01 = get_test_pattern_pixel(fb, x, y + 1);
237                                 RGB pixel11 = get_test_pattern_pixel(fb, x + 1, y + 1);
238                                 draw_yuv420_macropixel(fb, x, y,
239                                                        pixel00.yuv(), pixel10.yuv(),
240                                                        pixel01.yuv(), pixel11.yuv());
241                         }
242                 }
243                 break;
244         default:
245                 throw std::invalid_argument("unknown pixelformat");
246         }
249 void draw_test_pattern(DumbFramebuffer& fb)
251         using namespace std::chrono;
253         auto t1 = high_resolution_clock::now();
255         draw_test_pattern_impl(fb);
257         auto t2 = high_resolution_clock::now();
258         auto time_span = duration_cast<microseconds>(t2 - t1);
260         printf("draw took %u us\n", (unsigned)time_span.count());