2 //#define DRAW_PERF_PRINT
4 #include <chrono>
5 #include <cstring>
6 #include <cassert>
7 #include <thread>
9 #include <kms++.h>
10 #include <kms++util.h>
11 #include <cpuframebuffer.h>
13 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
15 using namespace std;
17 namespace kms
18 {
19 static void draw_rgb_pixel(IMappedFramebuffer& buf, unsigned x, unsigned y, RGB color)
20 {
21 switch (buf.format()) {
22 case PixelFormat::XRGB8888:
23 case PixelFormat::ARGB8888:
24 {
25 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
26 *p = color.argb8888();
27 break;
28 }
29 case PixelFormat::XBGR8888:
30 case PixelFormat::ABGR8888:
31 {
32 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
33 *p = color.abgr8888();
34 break;
35 }
36 case PixelFormat::RGB565:
37 {
38 uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
39 *p = color.rgb565();
40 break;
41 }
42 default:
43 throw std::invalid_argument("invalid pixelformat");
44 }
45 }
47 static void draw_yuv422_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2)
48 {
49 ASSERT((x & 1) == 0);
51 uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
53 uint8_t y0 = yuv1.y;
54 uint8_t y1 = yuv2.y;
55 uint8_t u = (yuv1.u + yuv2.u) / 2;
56 uint8_t v = (yuv1.v + yuv2.v) / 2;
58 switch (buf.format()) {
59 case PixelFormat::UYVY:
60 p[0] = u;
61 p[1] = y0;
62 p[2] = v;
63 p[3] = y1;
64 break;
66 case PixelFormat::YUYV:
67 p[0] = y0;
68 p[1] = u;
69 p[2] = y1;
70 p[3] = v;
71 break;
73 case PixelFormat::YVYU:
74 p[0] = y0;
75 p[1] = v;
76 p[2] = y1;
77 p[3] = u;
78 break;
80 case PixelFormat::VYUY:
81 p[0] = v;
82 p[1] = y0;
83 p[2] = u;
84 p[3] = y1;
85 break;
87 default:
88 throw std::invalid_argument("invalid pixelformat");
89 }
90 }
92 static void draw_yuv420_macropixel(IMappedFramebuffer& buf, unsigned x, unsigned y,
93 YUV yuv1, YUV yuv2, YUV yuv3, YUV yuv4)
94 {
95 ASSERT((x & 1) == 0);
96 ASSERT((y & 1) == 0);
98 uint8_t *py1 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 0) + x);
99 uint8_t *py2 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 1) + x);
101 uint8_t *puv = (uint8_t*)(buf.map(1) + buf.stride(1) * (y / 2) + x);
103 uint8_t y0 = yuv1.y;
104 uint8_t y1 = yuv2.y;
105 uint8_t y2 = yuv3.y;
106 uint8_t y3 = yuv4.y;
107 uint8_t u = (yuv1.u + yuv2.u + yuv3.u + yuv4.u) / 4;
108 uint8_t v = (yuv1.v + yuv2.v + yuv3.v + yuv4.v) / 4;
110 switch (buf.format()) {
111 case PixelFormat::NV12:
112 py1[0] = y0;
113 py1[1] = y1;
114 py2[0] = y2;
115 py2[1] = y3;
116 puv[0] = u;
117 puv[1] = v;
118 break;
120 case PixelFormat::NV21:
121 py1[0] = y0;
122 py1[1] = y1;
123 py2[0] = y2;
124 py2[1] = y3;
125 puv[0] = v;
126 puv[1] = u;
127 break;
129 default:
130 throw std::invalid_argument("invalid pixelformat");
131 }
132 }
134 static RGB get_test_pattern_pixel(IMappedFramebuffer& fb, unsigned x, unsigned y)
135 {
136 const unsigned w = fb.width();
137 const unsigned h = fb.height();
139 const unsigned mw = 20;
141 const unsigned xm1 = mw;
142 const unsigned xm2 = w - mw - 1;
143 const unsigned ym1 = mw;
144 const unsigned ym2 = h - mw - 1;
146 // white margin lines
147 if (x == xm1 || x == xm2 || y == ym1 || y == ym2)
148 return RGB(255, 255, 255);
149 // white box outlines to corners
150 else if ((x == 0 || x == w - 1) && (y < ym1 || y > ym2))
151 return RGB(255, 255, 255);
152 // white box outlines to corners
153 else if ((y == 0 || y == h - 1) && (x < xm1 || x > xm2))
154 return RGB(255, 255, 255);
155 // blue bar on the left
156 else if (x < xm1 && (y > ym1 && y < ym2))
157 return RGB(0, 0, 255);
158 // blue bar on the top
159 else if (y < ym1 && (x > xm1 && x < xm2))
160 return RGB(0, 0, 255);
161 // red bar on the right
162 else if (x > xm2 && (y > ym1 && y < ym2))
163 return RGB(255, 0, 0);
164 // red bar on the bottom
165 else if (y > ym2 && (x > xm1 && x < xm2))
166 return RGB(255, 0, 0);
167 // inside the margins
168 else if (x > xm1 && x < xm2 && y > ym1 && y < ym2) {
169 // diagonal line
170 if (x == y || w - x == h - y)
171 return RGB(255, 255, 255);
172 // diagonal line
173 else if (w - x == y || x == h - y)
174 return RGB(255, 255, 255);
175 else {
176 int t = (x - xm1 - 1) * 8 / (xm2 - xm1 - 1);
177 unsigned r = 0, g = 0, b = 0;
179 unsigned c = (y - ym1 - 1) % 256;
181 switch (t) {
182 case 0:
183 r = c;
184 break;
185 case 1:
186 g = c;
187 break;
188 case 2:
189 b = c;
190 break;
191 case 3:
192 g = b = c;
193 break;
194 case 4:
195 r = b = c;
196 break;
197 case 5:
198 r = g = c;
199 break;
200 case 6:
201 r = g = b = c;
202 break;
203 case 7:
204 break;
205 }
207 return RGB(r, g, b);
208 }
209 } else {
210 // black corners
211 return RGB(0, 0, 0);
212 }
213 }
215 static void draw_test_pattern_part(IMappedFramebuffer& fb, unsigned start_y, unsigned end_y)
216 {
217 unsigned x, y;
218 unsigned w = fb.width();
220 switch (fb.format()) {
221 case PixelFormat::XRGB8888:
222 case PixelFormat::XBGR8888:
223 case PixelFormat::ARGB8888:
224 case PixelFormat::ABGR8888:
225 case PixelFormat::RGB565:
226 for (y = start_y; y < end_y; y++) {
227 for (x = 0; x < w; x++) {
228 RGB pixel = get_test_pattern_pixel(fb, x, y);
229 draw_rgb_pixel(fb, x, y, pixel);
230 }
231 }
232 break;
234 case PixelFormat::UYVY:
235 case PixelFormat::YUYV:
236 case PixelFormat::YVYU:
237 case PixelFormat::VYUY:
238 for (y = start_y; y < end_y; y++) {
239 for (x = 0; x < w; x += 2) {
240 RGB pixel1 = get_test_pattern_pixel(fb, x, y);
241 RGB pixel2 = get_test_pattern_pixel(fb, x + 1, y);
242 draw_yuv422_macropixel(fb, x, y, pixel1.yuv(), pixel2.yuv());
243 }
244 }
245 break;
247 case PixelFormat::NV12:
248 case PixelFormat::NV21:
249 for (y = start_y; y < end_y; y += 2) {
250 for (x = 0; x < w; x += 2) {
251 RGB pixel00 = get_test_pattern_pixel(fb, x, y);
252 RGB pixel10 = get_test_pattern_pixel(fb, x + 1, y);
253 RGB pixel01 = get_test_pattern_pixel(fb, x, y + 1);
254 RGB pixel11 = get_test_pattern_pixel(fb, x + 1, y + 1);
255 draw_yuv420_macropixel(fb, x, y,
256 pixel00.yuv(), pixel10.yuv(),
257 pixel01.yuv(), pixel11.yuv());
258 }
259 }
260 break;
261 default:
262 throw std::invalid_argument("unknown pixelformat");
263 }
264 }
266 static void draw_test_pattern_impl(IMappedFramebuffer& fb)
267 {
268 if (fb.height() < 20) {
269 draw_test_pattern_part(fb, 0, fb.height());
270 return;
271 }
273 unsigned num_threads = thread::hardware_concurrency();
274 vector<thread> workers;
276 unsigned part = (fb.height() / num_threads) & ~1;
278 for (unsigned n = 0; n < num_threads; ++n) {
279 unsigned start = n * part;
280 unsigned end = start + part;
282 if (n == num_threads - 1)
283 end = fb.height();
285 workers.push_back(thread([&fb, start, end]() { draw_test_pattern_part(fb, start, end); }));
286 }
288 for (thread& t : workers)
289 t.join();
290 }
292 void draw_test_pattern(IMappedFramebuffer &fb)
293 {
294 #ifdef DRAW_PERF_PRINT
295 using namespace std::chrono;
297 auto t1 = high_resolution_clock::now();
298 #endif
300 draw_test_pattern_impl(fb);
302 #ifdef DRAW_PERF_PRINT
303 auto t2 = high_resolution_clock::now();
304 auto time_span = duration_cast<microseconds>(t2 - t1);
306 printf("draw took %u us\n", (unsigned)time_span.count());
307 #endif
308 }
310 void draw_rect(IMappedFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color)
311 {
312 for (unsigned i = x; i < x + w; ++i) {
313 for (unsigned j = y; j < y + h; ++j) {
314 draw_rgb_pixel(fb, i, j, color);
315 }
316 }
317 }
319 }