summaryrefslogtreecommitdiff
path: root/kms++util/src/drawing.cpp
blob: 194daf87658624bb14c1761bbfb1eca9996edb6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205

    /** first thing is always the DRM locking structure */
    drm_hw_lock_t		lock;
    /** \todo Use readers/writer lock for drm_sarea::drawable_lock */
    drm_hw_lock_t		drawable_lock;
    drm_sarea_drawable_t	drawableTable[SAREA_MAX_DRAWABLES];	/**< drawables */
    drm_sarea_frame_t		frame;	/**< frame */
    drm_context_t		dummy_context;
} drm_sarea_t;

#endif	/* _DRM_SAREA_H_ */
='#n238'>238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

#include <cmath>

#include <kms++/kms++.h>
#include <kms++util/kms++util.h>

using namespace std;

namespace kms
{
void draw_rgb_pixel(IFramebuffer& buf, unsigned x, unsigned y, RGB color)
{
	if (x >= buf.width() || y >= buf.height())
		throw runtime_error("attempt to draw outside the buffer");

	switch (buf.format()) {
	case PixelFormat::XRGB8888:
	case PixelFormat::ARGB8888:
	{
		uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
		*p = color.argb8888();
		break;
	}
	case PixelFormat::XBGR8888:
	case PixelFormat::ABGR8888:
	{
		uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
		*p = color.abgr8888();
		break;
	}
	case PixelFormat::RGBX8888:
	case PixelFormat::RGBA8888:
	{
		uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
		*p = color.rgba8888();
		break;
	}
	case PixelFormat::BGRX8888:
	case PixelFormat::BGRA8888:
	{
		uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
		*p = color.bgra8888();
		break;
	}
	case PixelFormat::XRGB2101010:
	case PixelFormat::ARGB2101010:
	{
		uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
		*p = color.argb2101010();
		break;
	}
	case PixelFormat::XBGR2101010:
	case PixelFormat::ABGR2101010:
	{
		uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
		*p = color.abgr2101010();
		break;
	}
	case PixelFormat::RGBX1010102:
	case PixelFormat::RGBA1010102:
	{
		uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
		*p = color.rgba1010102();
		break;
	}
	case PixelFormat::BGRX1010102:
	case PixelFormat::BGRA1010102:
	{
		uint32_t *p = (uint32_t*)(buf.map(0) + buf.stride(0) * y + x * 4);
		*p = color.bgra1010102();
		break;
	}
	case PixelFormat::RGB888:
	{
		uint8_t *p = buf.map(0) + buf.stride(0) * y + x * 3;
		p[0] = color.b;
		p[1] = color.g;
		p[2] = color.r;
		break;
	}
	case PixelFormat::BGR888:
	{
		uint8_t *p = buf.map(0) + buf.stride(0) * y + x * 3;
		p[0] = color.r;
		p[1] = color.g;
		p[2] = color.b;
		break;
	}
	case PixelFormat::RGB565:
	{
		uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
		*p = color.rgb565();
		break;
	}
	case PixelFormat::BGR565:
	{
		uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
		*p = color.bgr565();
		break;
	}
	case PixelFormat::ARGB4444:
	{
		uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
		*p = color.argb4444();
		break;
	}
	case PixelFormat::ARGB1555:
	{
		uint16_t *p = (uint16_t*)(buf.map(0) + buf.stride(0) * y + x * 2);
		*p = color.argb1555();
		break;
	}
	default:
		throw std::invalid_argument("invalid pixelformat");
	}
}

void draw_yuv422_macropixel(IFramebuffer& buf, unsigned x, unsigned y, YUV yuv1, YUV yuv2)
{
	if ((x + 1) >= buf.width() || y >= buf.height())
		throw runtime_error("attempt to draw outside the buffer");

	ASSERT((x & 1) == 0);

	uint8_t *p = (uint8_t*)(buf.map(0) + buf.stride(0) * y + x * 2);

	uint8_t y0 = yuv1.y;
	uint8_t y1 = yuv2.y;
	uint8_t u = (yuv1.u + yuv2.u) / 2;
	uint8_t v = (yuv1.v + yuv2.v) / 2;

	switch (buf.format()) {
	case PixelFormat::UYVY:
		p[0] = u;
		p[1] = y0;
		p[2] = v;
		p[3] = y1;
		break;

	case PixelFormat::YUYV:
		p[0] = y0;
		p[1] = u;
		p[2] = y1;
		p[3] = v;
		break;

	case PixelFormat::YVYU:
		p[0] = y0;
		p[1] = v;
		p[2] = y1;
		p[3] = u;
		break;

	case PixelFormat::VYUY:
		p[0] = v;
		p[1] = y0;
		p[2] = u;
		p[3] = y1;
		break;

	default:
		throw std::invalid_argument("invalid pixelformat");
	}
}

void draw_yuv420_macropixel(IFramebuffer& buf, unsigned x, unsigned y,
			    YUV yuv1, YUV yuv2, YUV yuv3, YUV yuv4)
{
	if ((x + 1) >= buf.width() || (y + 1) >= buf.height())
		throw runtime_error("attempt to draw outside the buffer");

	ASSERT((x & 1) == 0);
	ASSERT((y & 1) == 0);

	uint8_t *py1 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 0) + x);
	uint8_t *py2 = (uint8_t*)(buf.map(0) + buf.stride(0) * (y + 1) + x);

	uint8_t *puv = (uint8_t*)(buf.map(1) + buf.stride(1) * (y / 2) + x);

	uint8_t y0 = yuv1.y;
	uint8_t y1 = yuv2.y;
	uint8_t y2 = yuv3.y;
	uint8_t y3 = yuv4.y;
	uint8_t u = (yuv1.u + yuv2.u + yuv3.u + yuv4.u) / 4;
	uint8_t v = (yuv1.v + yuv2.v + yuv3.v + yuv4.v) / 4;

	switch (buf.format()) {
	case PixelFormat::NV12:
		py1[0] = y0;
		py1[1] = y1;
		py2[0] = y2;
		py2[1] = y3;
		puv[0] = u;
		puv[1] = v;
		break;

	case PixelFormat::NV21:
		py1[0] = y0;
		py1[1] = y1;
		py2[0] = y2;
		py2[1] = y3;
		puv[0] = v;
		puv[1] = u;
		break;

	default:
		throw std::invalid_argument("invalid pixelformat");
	}
}

void draw_rect(IFramebuffer &fb, uint32_t x, uint32_t y, uint32_t w, uint32_t h, RGB color)
{
	unsigned i, j;
	YUV yuvcolor = color.yuv();

	switch (fb.format()) {
	case PixelFormat::XRGB8888:
	case PixelFormat::XBGR8888:
	case PixelFormat::ARGB8888:
	case PixelFormat::ABGR8888:
	case PixelFormat::RGB888:
	case PixelFormat::BGR888:
	case PixelFormat::RGB565:
	case PixelFormat::BGR565:
	case PixelFormat::ARGB4444:
	case PixelFormat::ARGB1555:
		for (j = 0; j < h; j++) {
			for (i = 0; i < w; i++) {
				draw_rgb_pixel(fb, x + i, y + j, color);
			}
		}
		break;

	case PixelFormat::UYVY:
	case PixelFormat::YUYV:
	case PixelFormat::YVYU:
	case PixelFormat::VYUY:
		for (j = 0; j < h; j++) {
			for (i = 0; i < w; i += 2) {
				draw_yuv422_macropixel(fb, x + i, y + j, yuvcolor, yuvcolor);
			}
		}
		break;

	case PixelFormat::NV12:
	case PixelFormat::NV21:
		for (j = 0; j < h; j += 2) {
			for (i = 0; i < w; i += 2) {
				draw_yuv420_macropixel(fb, x + i, y + j,
						       yuvcolor, yuvcolor, yuvcolor, yuvcolor);
			}
		}
		break;
	default:
		throw std::invalid_argument("draw_rect: unknown pixelformat");
	}
}

void draw_horiz_line(IFramebuffer& fb, uint32_t x1, uint32_t x2, uint32_t y, RGB color)
{
	for (uint32_t x = x1; x <= x2; ++x)
		draw_rgb_pixel(fb, x, y, color);
}

void draw_circle(IFramebuffer& fb, int32_t xCenter, int32_t yCenter, int32_t radius, RGB color)
{
	int32_t r2 = radius * radius;

	for (int y = -radius; y <= radius; y++) {
		int32_t x = (int)(sqrt(r2 - y * y) + 0.5);
		draw_horiz_line(fb, xCenter - x, xCenter + x, yCenter - y, color);
	}
}

static bool get_char_pixel(char c, uint32_t x, uint32_t y)
{
#include "font_8x8.h"

	uint8_t bits = fontdata_8x8[8 * c + y];
	bool bit = (bits >> (7 - x)) & 1;

	return bit;
}

static void draw_char(IFramebuffer& buf, uint32_t xpos, uint32_t ypos, char c, RGB color)
{
	unsigned x, y;
	YUV yuvcolor = color.yuv();

	switch (buf.format()) {
	case PixelFormat::XRGB8888:
	case PixelFormat::XBGR8888:
	case PixelFormat::ARGB8888:
	case PixelFormat::ABGR8888:
	case PixelFormat::RGB888:
	case PixelFormat::BGR888:
	case PixelFormat::RGB565:
	case PixelFormat::BGR565:
	case PixelFormat::ARGB4444:
	case PixelFormat::ARGB1555:
		for (y = 0; y < 8; y++) {
			for (x = 0; x < 8; x++) {
				bool b = get_char_pixel(c, x, y);

				draw_rgb_pixel(buf, xpos + x, ypos + y, b ? color : RGB());
			}
		}
		break;

	case PixelFormat::UYVY:
	case PixelFormat::YUYV:
	case PixelFormat::YVYU:
	case PixelFormat::VYUY:
		for (y = 0; y < 8; y++) {
			for (x = 0; x < 8; x += 2) {
				bool b0 = get_char_pixel(c, x, y);
				bool b1 = get_char_pixel(c, x + 1, y);

				draw_yuv422_macropixel(buf, xpos + x, ypos + y,
						       b0 ? yuvcolor : YUV(RGB()), b1 ? yuvcolor : YUV(RGB()));
			}
		}
		break;

	case PixelFormat::NV12:
	case PixelFormat::NV21:
		for (y = 0; y < 8; y += 2) {
			for (x = 0; x < 8; x += 2) {
				bool b00 = get_char_pixel(c, x, y);
				bool b10 = get_char_pixel(c, x + 1, y);
				bool b01 = get_char_pixel(c, x, y + 1);
				bool b11 = get_char_pixel(c, x + 1, y + 1);

				draw_yuv420_macropixel(buf, xpos + x, ypos + y,
						       b00 ? yuvcolor : YUV(RGB()), b10 ? yuvcolor : YUV(RGB()),
						       b01 ? yuvcolor : YUV(RGB()), b11 ? yuvcolor : YUV(RGB()));
			}
		}
		break;
	default:
		throw std::invalid_argument("draw_char: unknown pixelformat");
	}
}

void draw_text(IFramebuffer& buf, uint32_t x, uint32_t y, const string& str, RGB color)
{
	for(unsigned i = 0; i < str.size(); i++)
		draw_char(buf, (x + 8 * i), y, str[i], color);
}

}