summaryrefslogtreecommitdiffstats
path: root/commands/fbtest.c
blob: e5dd8ba7fabf47489147e35e0f5095c5af438009 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
#include <common.h>
#include <command.h>
#include <errno.h>
#include <malloc.h>
#include <getopt.h>
#include <fb.h>
#include <gui/graphic_utils.h>
#include <gui/2d-primitives.h>
#include <linux/gcd.h>
#include <int_sqrt.h>

static void fbtest_pattern_solid(struct screen *sc, u32 color)
{
	const u8 r = (color >> 16) & 0xff;
	const u8 g = (color >>  8) & 0xff;
	const u8 b = (color >>  0) & 0xff;

	gu_fill_rectangle(sc, 0, 0, -1, -1, r, g, b, 0xff);
}

static void fbtest_pattern_bars(struct screen *sc, u32 unused)
{
	int i;

	const u32 xres = sc->info->xres;

	const u32 colors[] =  {
		0xFFFFFF, 	/* white */
		0xFFFF00,	/* yellow */
		0x00FFFF,	/* cyan */
		0x00FF00,	/* green */
		0xFF00FF,	/* magenta */
		0xFF0000,	/* red */
		0x0000FF,	/* blue */
		0x000000,	/* black */
	};

	for (i = 0; i < ARRAY_SIZE(colors); i++) {
		const u8 r = (colors[i] >> 16) & 0xff;
		const u8 g = (colors[i] >>  8) & 0xff;
		const u8 b = (colors[i] >>  0) & 0xff;
		const int dx = xres / ARRAY_SIZE(colors);

		gu_fill_rectangle(sc,
				  i * dx, -1, (i + 1) * dx - 1, -1,
				  r, g, b, 0xff);
	}
}

static void fbtest_pattern_geometry(struct screen *sc, u32 color)
{
	int i;

	const u8 r = (color >> 16) & 0xff;
	const u8 g = (color >>  8) & 0xff;
	const u8 b = (color >>  0) & 0xff;

	const u32 xres = sc->info->xres;
	const u32 yres = sc->info->yres;

	const u8 xcount = xres / gcd(xres, yres);
	const u8 ycount = yres / gcd(xres, yres);

	const struct {
		int x1, y1, x2, y2;
	} borders[] = {
		{ 0,        0,        xres - 1, 0        },
		{ xres - 1, 0,        xres - 1, yres - 1 },
		{ 0,        yres - 1, xres - 1, yres - 1 },
		{ 0,        0,        0,        yres - 1 },
	};

	const int R1 = min(xres, yres) / 2;
	const int h  = xres * xres + yres * yres;
	const int R2 = (int_sqrt(h) / 2 - R1) * 5 / 12;

	const  struct {
		int x0, y0, radius;
	} circles[] = {
		{ xres / 2,  yres / 2,  R1 - 1 },
		{ R2,        R2,        R2 - 1 },
		{ xres - R2, R2,        R2 - 1 },
		{ xres - R2, yres - R2, R2 - 1 },
		{ R2,        yres - R2, R2 - 1 }
	};

	void *buf = gui_screen_render_buffer(sc);

	gu_memset_pixel(sc->info, buf, ~color,
			sc->s.width * sc->s.height);

	for (i = 0; i < ARRAY_SIZE(borders); i++)
		gu_draw_line(sc,
			     borders[i].x1, borders[i].y1,
			     borders[i].x2, borders[i].y2,
			     r, g, b, 0xff, 10);

	for (i = 0; i < ARRAY_SIZE(circles); i++)
		gu_draw_circle(sc,
			       circles[i].x0, circles[i].y0,
			       circles[i].radius,
			       r, g, b, 0xff);

	for (i = 1; i < ycount; i++) {
		const int y = (yres - 1) * i / ycount;
		gu_draw_line(sc,
			     0, y, xres - 1, y,
			     r, g, b, 0xff, 0);
	}


	for (i = 1; i < xcount; i++) {
		const int x = (xres - 1) * i / xcount;
		gu_draw_line(sc,
			     x, 0, x, yres - 1,
			     r, g, b, 0xff, 0);
	}
}

static void draw_line_r(struct screen *sc, bool rotate_90_ccw,
			int x1, int y1, int x2, int y2,
			uint8_t r, uint8_t g, uint8_t b)
{
	if (rotate_90_ccw)
		gu_draw_line(sc,
			     y1, sc->info->yres - x1,
			     y2, sc->info->yres - x2,
			     r, g, b, 0xff, 0);
	else
		gu_draw_line(sc, x1, y1, x2, y2, r, g, b, 0xff, 0);
}

static void solid_rect_r(struct screen *sc, bool rotate_90_ccw,
			 int x1, int y1, int x2, int y2,
			 uint8_t r, uint8_t g, uint8_t b)
{
	if (rotate_90_ccw)
		gu_fill_rectangle(sc,
				  y1, sc->info->yres - x1,
				  y2, sc->info->yres - x2,
				  r, g, b, 0xff);
	else
		gu_fill_rectangle(sc, x1, y1, x2, y2, r, g, b, 0xff);
}

static void grad_rect_r(struct screen *sc, bool rotate_90_ccw,
			int x1, int y1, int x2, int y2,
			uint8_t r, uint8_t g, uint8_t b)
{
	int x;

	for (x = x1; x <= x2; x++)
		draw_line_r(sc, rotate_90_ccw, x, y1, x, y2,
			    r * (x - x1 + 1) / (x2 - x1 + 1),
			    g * (x - x1 + 1) / (x2 - x1 + 1),
			    b * (x - x1 + 1) / (x2 - x1 + 1));
}

static void anchor_rect_r(struct screen *sc, bool rotate_90_ccw,
			  int x1, int y1, int x2, int y2)
{
	int dx = (x2 - x1 + 1) / 6, dy = (y2 - y1 + 1) / 6;

	solid_rect_r(sc, rotate_90_ccw,
		     x1, y1, x2, y2,
		     0xff, 0xff, 0xff);

	solid_rect_r(sc, rotate_90_ccw,
		     x1 + dx, y1 + dy, x2 - dx, y2 - dy,
		     0, 0, 0);

	solid_rect_r(sc, rotate_90_ccw,
		     x1 + 2 * dx, y1 + 2 * dy, x2 - 2 * dx, y2 - 2 * dy,
		     0xff, 0xff, 0xff);
}


static void fbtest_pattern_gradient(struct screen *sc, u32 unused)
{
	bool rotate_90_ccw;
	int w, h, border;

	if (sc->info->xres > sc->info->yres) {
		w = sc->info->xres;
		h = sc->info->yres;
		rotate_90_ccw = false;
	} else {
		w = sc->info->yres;
		h = sc->info->xres;
		rotate_90_ccw = true;
	}

	solid_rect_r(sc, rotate_90_ccw, 0, 0, w - 1, h - 1, 0, 0, 0);

	border = h / 5;

	anchor_rect_r(sc, rotate_90_ccw,
		      border * 3 / 10, border * 3 / 10,
		      border * 7 / 10, border * 7 / 10);
	anchor_rect_r(sc, rotate_90_ccw,
		      w - border * 7 / 10, border * 3 / 10,
		      w - border * 3 / 10, border * 7 / 10);
	anchor_rect_r(sc, rotate_90_ccw,
		      w - border * 7 / 10, h - border * 7 / 10,
		      w - border * 3 / 10, h - border * 3 / 10);

	grad_rect_r(sc, rotate_90_ccw,
		    border, border,
		    w - border, border + (h - 2 * border) / 4 - 1,
		    0xff, 0, 0);
	grad_rect_r(sc, rotate_90_ccw,
		    border, border + (h - 2 * border) / 4,
		    w - border, border + (h - 2 * border) / 2 - 1,
		    0, 0xff, 0);
	grad_rect_r(sc, rotate_90_ccw,
		    border, border + (h - 2 * border) / 2,
		    w - border, border + (h - 2 * border) * 3 / 4 - 1,
		    0, 0, 0xff);
	grad_rect_r(sc, rotate_90_ccw,
		    border, border + (h - 2 * border) * 3 / 4,
		    w - border, h - border,
		    0xff, 0xff, 0xff);
}

static int do_fbtest(int argc, char *argv[])
{
	struct screen *sc;
	int opt;
	unsigned int i;
	const char *pattern_name = NULL;
	char *fbdev = "/dev/fb0";
	void (*pattern) (struct screen *sc, u32 color) = NULL;
	u32 color = 0xffffff;

	struct {
		const char *name;
		void (*func) (struct screen *sc, u32 color);
	} patterns[] = {
		{ "solid",    fbtest_pattern_solid    },
		{ "geometry", fbtest_pattern_geometry },
		{ "bars",     fbtest_pattern_bars     },
		{ "gradient", fbtest_pattern_gradient },
	};

	while((opt = getopt(argc, argv, "d:p:c:")) > 0) {
		switch(opt) {
		case 'd':
			fbdev = optarg;
			break;
		case 'p':
			pattern_name = optarg;
			break;
		case 'c':
			color = simple_strtoul(optarg, NULL, 16);
			break;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (pattern_name) {
		for (i = 0; i < ARRAY_SIZE(patterns); i++)
			if (!strcmp(pattern_name, patterns[i].name))
				pattern = patterns[i].func;

		if (!pattern) {
			printf("Unknown pattern: %s\n", pattern_name);
			return -EINVAL;
		}
	}

	sc = fb_open(fbdev);
	if (IS_ERR(sc)) {
		perror("fd_open");
		return PTR_ERR(sc);
	}

	if (!pattern_name) {
		printf("No pattern selected. Cycling through all of them.\n");
		printf("Press Ctrl-C to stop\n");

		i = 0;
		for (;;) {
			uint64_t start;
			pattern = patterns[i++ % ARRAY_SIZE(patterns)].func;
			pattern(sc, color);
			gu_screen_blit(sc);
			fb_flush(sc->info);

			start = get_time_ns();
			while (!is_timeout(start, 2 * SECOND))
				if (ctrlc())
					goto done;
		}
	} else {
		pattern(sc, color);
		gu_screen_blit(sc);
	}
done:
	fb_close(sc);

	return 0;
}

BAREBOX_CMD_HELP_START(fbtest)
BAREBOX_CMD_HELP_TEXT("This command displays a test pattern on a screen")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT ("-d <fbdev>\t",    "framebuffer device (default /dev/fb0)")
BAREBOX_CMD_HELP_OPT ("-c color\t", "color, in hex RRGGBB format")
BAREBOX_CMD_HELP_OPT ("-p pattern\t", "pattern name (solid, geometry, bars, gradient)")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(fbtest)
	.cmd		= do_fbtest,
	BAREBOX_CMD_DESC("display a test pattern")
	BAREBOX_CMD_OPTS("[-dcp]")
	BAREBOX_CMD_GROUP(CMD_GRP_CONSOLE)
	BAREBOX_CMD_HELP(cmd_fbtest_help)
BAREBOX_CMD_END