forked from andy-shev/fb-test-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rect.c
281 lines (220 loc) · 5.17 KB
/
rect.c
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
/*
* rect.c
*
* Author: Tomi Valkeinen <[email protected]>
* Copyright (C) 2009-2012 Tomi Valkeinen
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
#include <time.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <linux/fb.h>
#define ERROR(x) printf("fbtest error in line %s:%d: %s\n", __func__, \
__LINE__, strerror(errno));
#define FBCTL(cmd, arg) \
if (ioctl(fd, cmd, arg) == -1) { \
ERROR("ioctl failed"); \
exit(1); }
#define FBCTL0(cmd) \
if (ioctl(fd, cmd) == -1) { \
ERROR("ioctl failed"); \
exit(1); }
static struct fb_var_screeninfo var;
static struct fb_fix_screeninfo fix;
int open_fb(const char *dev)
{
int fd = -1;
fd = open(dev, O_RDWR);
if (fd == -1) {
printf("Error opening device %s : %s\n", dev, strerror(errno));
exit(-1);
}
return fd;
}
struct rect {
short x;
short y;
short w;
short h;
};
static struct rect *get_rand_rect(struct rect *r,
short max_x, short max_y,
short min_w, short min_h,
short max_w, short max_h)
{
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
const short max_width = MIN(max_w, max_x)+1;
const short max_height = MIN(max_h, max_y)+1;
if (min_w == max_x)
r->x = 0;
else
r->x = rand() % (max_x - min_w);
if (min_h == max_y)
r->y = 0;
else
r->y = rand() % (max_y - min_h);
r->w = min_w + rand() % (MIN(max_width, 1+max_x - r->x - min_w));
r->h = min_h + rand() % (MIN(max_height, 1+max_y - r->y - min_h));
#if 0
printf("Returning x = %d, y = %d, w = %d, h = %d\n",
r->x, r->y, r->w, r->h);
#endif
return r;
}
static void draw_pixel(void *fbmem, int x, int y, unsigned int color)
{
if (var.bits_per_pixel == 8) {
unsigned char *p;
fbmem += fix.line_length * y;
p = fbmem;
p += x;
*p = color;
} else if (var.bits_per_pixel == 16) {
unsigned short c;
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff;
int b = (color >> 0) & 0xff;
unsigned short *p;
r = r * 8 / 5;
g = g * 8 / 6;
b = b * 8 / 5;
c = (r << 11) | (g << 6) | (b << 0);
fbmem += fix.line_length * y;
p = fbmem;
p += x;
*p = c;
} else if (var.bits_per_pixel == 24) {
unsigned int *p;
unsigned c;
fbmem += fix.line_length * y;
fbmem += 3 * x;
p = fbmem;
c = *p;
c = (c & 0xFF000000) | (color & 0x00FFFFFF);
*p = c;
} else {
unsigned int *p;
fbmem += fix.line_length * y;
p = fbmem;
p += x;
*p = color;
}
}
static unsigned fill_rect(int *fb, const struct rect *r)
{
short x, y;
int color = rand() % (0xffffff+1);
const int max_w = r->x + r->w;
const int max_h = r->y + r->h;
#if 0
printf("fill_rect %d,%d %dx%d\n", r->x, r->y, r->w, r->h);
#endif
for (y = r->y; y < max_h; y++) {
for (x = r->x; x < max_w; x++) {
if (y - r->y == x - r->x)
draw_pixel(fb, x, y, ~color & 0xffffff);
else
draw_pixel(fb, x, y, color);
}
}
return color;
}
void fill_screen(void *fbmem)
{
int x, y;
int color;
color = rand() % 0xffffff;
for (y = 0; y < var.yres_virtual; y++) {
for (x = 0; x < var.xres_virtual; x++) {
int c;
if (y == 0 || x == 0 ||
y == var.yres_virtual - 1 ||
x == var.xres_virtual - 1)
c = 0xff0000;
else
c = color;
draw_pixel(fbmem, x, y, c);
}
}
}
void show_help(void)
{
printf("Usage: fb-test-rect -f fbnum -s seed\n");
printf("Where -f fbnum = framebuffer device number\n");
printf(" -s seed = seed for srand()\n");
}
int main(int argc, char **argv)
{
int fd;
struct rect r;
int i;
void *readbuf;
unsigned readbuf_size;
int opt;
int fb = 0;
int seed = 0;
char str[256];
printf("rect %d.%d.%d (%s)\n", VERSION, PATCHLEVEL, SUBLEVEL,
VERSION_NAME);
while ((opt = getopt(argc, argv, "hf:s:")) != -1) {
switch (opt) {
case 'f':
fb = atoi(optarg);
break;
case 's':
seed = atoi(optarg);
break;
case 'h':
show_help();
return 0;
default:
exit(EXIT_FAILURE);
}
}
sprintf(str, "/dev/fb%d", fb);
fd = open_fb(str);
FBCTL(FBIOGET_VSCREENINFO, &var);
FBCTL(FBIOGET_FSCREENINFO, &fix);
readbuf_size = var.xres_virtual * var.yres_virtual * 3;
readbuf = malloc(readbuf_size);
void *ptr = mmap(0, var.yres_virtual * fix.line_length,
PROT_WRITE | PROT_READ,
MAP_SHARED, fd, 0);
if (ptr == MAP_FAILED) {
perror("mmap failed");
exit(1);
}
if (seed) {
srand(seed);
} else {
srand((unsigned int)time(NULL) + getpid());
}
fill_screen(ptr);
for (i = 0; 1 || i < 10000; i++) {
get_rand_rect(&r,
var.xres_virtual, var.yres_virtual,
2, 0,
var.xres_virtual, var.yres_virtual);
fill_rect(ptr, &r);
}
free(readbuf);
return 0;
}