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 "color.h"
15 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
17 namespace kms
18 {
19 static void draw_pixel(DumbFramebuffer& buf, unsigned x, unsigned y, RGB color)
20 {
21 static RGB c1;
23 switch (buf.format()) {
24 case PixelFormat::XRGB8888:
25 {
26 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
27 *p = color.rgb888();
28 break;
29 }
30 case PixelFormat::XBGR8888:
31 {
32 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
33 *p = color.bgr888();
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 case PixelFormat::UYVY:
43 {
44 if ((x & 1) == 0) {
45 c1 = color;
46 return;
47 }
49 uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
51 YUV yuv1 = c1.yuv();
52 YUV yuv2 = color.yuv();
54 p[0] = (yuv1.u + yuv2.u) / 2;
55 p[1] = yuv1.y;
56 p[2] = (yuv1.v + yuv2.v) / 2;
57 p[3] = yuv2.y;
58 break;
59 }
60 case PixelFormat::YUYV:
61 {
62 if ((x & 1) == 0) {
63 c1 = color;
64 return;
65 }
67 uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
69 YUV yuv1 = c1.yuv();
70 YUV yuv2 = color.yuv();
72 p[0] = yuv1.y;
73 p[1] = (yuv1.u + yuv2.u) / 2;
74 p[2] = yuv2.y;
75 p[3] = (yuv1.v + yuv2.v) / 2;
76 break;
77 }
78 default:
79 throw std::invalid_argument("unknown pixelformat");
80 }
81 }
83 static void draw_rgb_test_pattern(DumbFramebuffer& fb)
84 {
85 unsigned x, y;
86 unsigned w = fb.width();
87 unsigned h = fb.height();
89 const unsigned mw = 20;
91 const unsigned xm1 = mw;
92 const unsigned xm2 = w - mw - 1;
93 const unsigned ym1 = mw;
94 const unsigned ym2 = h - mw - 1;
96 for (y = 0; y < h; y++) {
97 for (x = 0; x < w; x++) {
98 // white margin lines
99 if (x == xm1 || x == xm2 || y == ym1 || y == ym2)
100 draw_pixel(fb, x, y, RGB(255, 255, 255));
101 // white box outlines to corners
102 else if ((x == 0 || x == w - 1) && (y < ym1 || y > ym2))
103 draw_pixel(fb, x, y, RGB(255, 255, 255));
104 // white box outlines to corners
105 else if ((y == 0 || y == h - 1) && (x < xm1 || x > xm2))
106 draw_pixel(fb, x, y, RGB(255, 255, 255));
107 // blue bar on the left
108 else if (x < xm1 && (y > ym1 && y < ym2))
109 draw_pixel(fb, x, y, RGB(0, 0, 255));
110 // blue bar on the top
111 else if (y < ym1 && (x > xm1 && x < xm2))
112 draw_pixel(fb, x, y, RGB(0, 0, 255));
113 // red bar on the right
114 else if (x > xm2 && (y > ym1 && y < ym2))
115 draw_pixel(fb, x, y, RGB(255, 0, 0));
116 // red bar on the bottom
117 else if (y > ym2 && (x > xm1 && x < xm2))
118 draw_pixel(fb, x, y, RGB(255, 0, 0));
119 // inside the margins
120 else if (x > xm1 && x < xm2 && y > ym1 && y < ym2) {
121 // diagonal line
122 if (x == y || w - x == h - y)
123 draw_pixel(fb, x, y, RGB(255, 255, 255));
124 // diagonal line
125 else if (w - x == y || x == h - y)
126 draw_pixel(fb, x, y, RGB(255, 255, 255));
127 else {
128 int t = (x - xm1 - 1) * 3 / (xm2 - xm1 - 1);
129 unsigned r = 0, g = 0, b = 0;
131 unsigned c = (y - ym1 - 1) % 256;
133 switch (t) {
134 case 0:
135 r = c;
136 break;
137 case 1:
138 g = c;
139 break;
140 case 2:
141 b = c;
142 break;
143 }
145 draw_pixel(fb, x, y, RGB(r, g, b));
146 }
147 // black corners
148 } else {
149 draw_pixel(fb, x, y, RGB(0, 0, 0));
150 }
151 }
152 }
153 }
155 void draw_test_pattern(DumbFramebuffer& fb)
156 {
157 using namespace std::chrono;
159 auto t1 = high_resolution_clock::now();
161 draw_rgb_test_pattern(fb);
163 auto t2 = high_resolution_clock::now();
164 auto time_span = duration_cast<microseconds>(t2 - t1);
166 printf("draw took %u us\n", (unsigned)time_span.count());
167 }
168 }