1516369d0f533f3edb037481b096d813bc8951b9
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 DRM_FORMAT_XRGB8888:
25 {
26 uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
27 *p = color.raw;
28 break;
29 }
30 case DRM_FORMAT_RGB565:
31 {
32 uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
33 *p = color.rgb565();
34 break;
35 }
36 case DRM_FORMAT_UYVY:
37 {
38 if ((x & 1) == 0) {
39 c1 = color;
40 return;
41 }
43 uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
45 YUV yuv1 = c1.yuv();
46 YUV yuv2 = color.yuv();
48 p[0] = (yuv1.u + yuv2.u) / 2;
49 p[1] = yuv1.y;
50 p[2] = (yuv1.v + yuv2.v) / 2;
51 p[3] = yuv2.y;
52 break;
53 }
54 case DRM_FORMAT_YUYV:
55 {
56 if ((x & 1) == 0) {
57 c1 = color;
58 return;
59 }
61 uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
63 YUV yuv1 = c1.yuv();
64 YUV yuv2 = color.yuv();
66 p[0] = yuv1.y;
67 p[1] = (yuv1.u + yuv2.u) / 2;
68 p[2] = yuv2.y;
69 p[3] = (yuv1.v + yuv2.v) / 2;
70 break;
71 }
72 }
73 }
75 static void draw_rgb_test_pattern(DumbFramebuffer& fb)
76 {
77 unsigned x, y;
78 unsigned w = fb.width();
79 unsigned h = fb.height();
81 const unsigned mw = 20;
83 const unsigned xm1 = mw;
84 const unsigned xm2 = w - mw - 1;
85 const unsigned ym1 = mw;
86 const unsigned ym2 = h - mw - 1;
88 for (y = 0; y < h; y++) {
89 for (x = 0; x < w; x++) {
90 // white margin lines
91 if (x == xm1 || x == xm2 || y == ym1 || y == ym2)
92 draw_pixel(fb, x, y, RGB(255, 255, 255));
93 // white box outlines to corners
94 else if ((x == 0 || x == w - 1) && (y < ym1 || y > ym2))
95 draw_pixel(fb, x, y, RGB(255, 255, 255));
96 // white box outlines to corners
97 else if ((y == 0 || y == h - 1) && (x < xm1 || x > xm2))
98 draw_pixel(fb, x, y, RGB(255, 255, 255));
99 // blue bar on the left
100 else if (x < xm1 && (y > ym1 && y < ym2))
101 draw_pixel(fb, x, y, RGB(0, 0, 255));
102 // blue bar on the top
103 else if (y < ym1 && (x > xm1 && x < xm2))
104 draw_pixel(fb, x, y, RGB(0, 0, 255));
105 // red bar on the right
106 else if (x > xm2 && (y > ym1 && y < ym2))
107 draw_pixel(fb, x, y, RGB(255, 0, 0));
108 // red bar on the bottom
109 else if (y > ym2 && (x > xm1 && x < xm2))
110 draw_pixel(fb, x, y, RGB(255, 0, 0));
111 // inside the margins
112 else if (x > xm1 && x < xm2 && y > ym1 && y < ym2) {
113 // diagonal line
114 if (x == y || w - x == h - y)
115 draw_pixel(fb, x, y, RGB(255, 255, 255));
116 // diagonal line
117 else if (w - x == y || x == h - y)
118 draw_pixel(fb, x, y, RGB(255, 255, 255));
119 else {
120 int t = (x - xm1 - 1) * 3 / (xm2 - xm1 - 1);
121 unsigned r = 0, g = 0, b = 0;
123 unsigned c = (y - ym1 - 1) % 256;
125 switch (t) {
126 case 0:
127 r = c;
128 break;
129 case 1:
130 g = c;
131 break;
132 case 2:
133 b = c;
134 break;
135 }
137 draw_pixel(fb, x, y, RGB(r, g, b));
138 }
139 // black corners
140 } else {
141 draw_pixel(fb, x, y, RGB(0, 0, 0));
142 }
143 }
144 }
145 }
147 void draw_test_pattern(DumbFramebuffer& fb)
148 {
149 using namespace std::chrono;
151 auto t1 = high_resolution_clock::now();
153 draw_rgb_test_pattern(fb);
155 auto t2 = high_resolution_clock::now();
156 auto time_span = duration_cast<microseconds>(t2 - t1);
158 printf("draw took %u us\n", (unsigned)time_span.count());
159 }
160 }