b0b67a49a445ef8c28ee92b957d268088cb03783
[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 RGB get_test_pattern_pixel(DumbFramebuffer& fb, unsigned x, unsigned y)
91 {
92         unsigned w = fb.width();
93         unsigned h = fb.height();
95         const unsigned mw = 20;
97         const unsigned xm1 = mw;
98         const unsigned xm2 = w - mw - 1;
99         const unsigned ym1 = mw;
100         const unsigned ym2 = h - mw - 1;
102         // white margin lines
103         if (x == xm1 || x == xm2 || y == ym1 || y == ym2)
104                 return RGB(255, 255, 255);
105         // white box outlines to corners
106         else if ((x == 0 || x == w - 1) && (y < ym1 || y > ym2))
107                 return RGB(255, 255, 255);
108         // white box outlines to corners
109         else if ((y == 0 || y == h - 1) && (x < xm1 || x > xm2))
110                 return RGB(255, 255, 255);
111         // blue bar on the left
112         else if (x < xm1 && (y > ym1 && y < ym2))
113                 return RGB(0, 0, 255);
114         // blue bar on the top
115         else if (y < ym1 && (x > xm1 && x < xm2))
116                 return RGB(0, 0, 255);
117         // red bar on the right
118         else if (x > xm2 && (y > ym1 && y < ym2))
119                 return RGB(255, 0, 0);
120         // red bar on the bottom
121         else if (y > ym2 && (x > xm1 && x < xm2))
122                 return RGB(255, 0, 0);
123         // inside the margins
124         else if (x > xm1 && x < xm2 && y > ym1 && y < ym2) {
125                 // diagonal line
126                 if (x == y || w - x == h - y)
127                         return RGB(255, 255, 255);
128                 // diagonal line
129                 else if (w - x == y || x == h - y)
130                         return RGB(255, 255, 255);
131                 else {
132                         int t = (x - xm1 - 1) * 3 / (xm2 - xm1 - 1);
133                         unsigned r = 0, g = 0, b = 0;
135                         unsigned c = (y - ym1 - 1) % 256;
137                         switch (t) {
138                         case 0:
139                                 r = c;
140                                 break;
141                         case 1:
142                                 g = c;
143                                 break;
144                         case 2:
145                                 b = c;
146                                 break;
147                         }
149                         return RGB(r, g, b);
150                 }
151         } else {
152                 // black corners
153                 return RGB(0, 0, 0);
154         }
157 static void draw_test_pattern_impl(DumbFramebuffer& fb)
159         unsigned x, y;
160         unsigned w = fb.width();
161         unsigned h = fb.height();
163         switch (fb.format()) {
164         case PixelFormat::XRGB8888:
165         case PixelFormat::XBGR8888:
166         case PixelFormat::RGB565:
167                 for (y = 0; y < h; y++) {
168                         for (x = 0; x < w; x++) {
169                                 RGB pixel = get_test_pattern_pixel(fb, x, y);
170                                 draw_rgb_pixel(fb, x, y, pixel);
171                         }
172                 }
173                 break;
175         case PixelFormat::UYVY:
176         case PixelFormat::YUYV:
177         case PixelFormat::YVYU:
178         case PixelFormat::VYUY:
179                 for (y = 0; y < h; y++) {
180                         for (x = 0; x < w; x += 2) {
181                                 RGB pixel1 = get_test_pattern_pixel(fb, x, y);
182                                 RGB pixel2 = get_test_pattern_pixel(fb, x + 1, y);
183                                 draw_yuv422_macropixel(fb, x, y, pixel1.yuv(), pixel2.yuv());
184                         }
185                 }
186                 break;
187         default:
188                 throw std::invalid_argument("unknown pixelformat");
189         }
192 void draw_test_pattern(DumbFramebuffer& fb)
194         using namespace std::chrono;
196         auto t1 = high_resolution_clock::now();
198         draw_test_pattern_impl(fb);
200         auto t2 = high_resolution_clock::now();
201         auto time_span = duration_cast<microseconds>(t2 - t1);
203         printf("draw took %u us\n", (unsigned)time_span.count());