6e9c2f85fafe6f33ceed4036dc0f4a9cb382b3a3
[openwrt/openwrt.git] / package / fbtest / src / fbtest.c
1 /******************************************************************************
2 * fbtest - fbtest.c
3 * test program for the tuxbox-framebuffer device
4 * tests all GTX/eNX supported modes
5 *
6 * (c) 2003 Carsten Juttner (carjay@gmx.net)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * The Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 *
22 ******************************************************************************
23 * $Id: fbtest.c,v 1.5 2005/01/14 23:14:41 carjay Exp $
24 ******************************************************************************/
25
26 // TODO: - should restore the colour map and mode to what it was before
27 // - is colour map handled correctly?
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37 #include <sys/mman.h>
38 #include <fcntl.h>
39
40 #include <linux/fb.h>
41
42 #include <error.h>
43
44 #define FBDEV "/dev/fb0"
45
46 struct vidsize{
47 int width;
48 int height;
49 };
50 static
51 const struct vidsize vidsizetable[]={ // all supported sizes
52 {720,576},{720,480},{720,288},{720,240},
53 {640,576},{640,480},{640,288},{640,240},
54 {360,576},{360,480},{360,288},{360,240},
55 {320,576},{320,480},{320,288},{320,240}
56 };
57 #define VIDSIZENUM (sizeof(vidsizetable)/sizeof(struct vidsize))
58
59 enum pixenum{ // keep in sync with pixname !
60 CLUT4=0,
61 CLUT8,
62 RGB565,
63 ARGB1555,
64 ARGB
65 };
66 const char *pixname[] = {
67 "CLUT4",
68 "CLUT8",
69 "RGB565",
70 "ARGB1555",
71 "ARGB"
72 };
73
74 struct pixelformat{
75 char *name;
76 struct fb_bitfield red;
77 struct fb_bitfield green;
78 struct fb_bitfield blue;
79 struct fb_bitfield transp;
80 char bpp;
81 char pixenum;
82 };
83
84 static // so far these are all modes supported by the eNX (only partially by GTX)
85 const struct pixelformat pixelformattable[] = {
86 { .name = "CLUT4 ARGB8888", // CLUT4 (ARGB8888)
87 .bpp = 4, .pixenum = CLUT4,
88 .red = { .offset = 0, .length=8, .msb_right =0 },
89 .green = { .offset = 0, .length=8, .msb_right =0 },
90 .blue = { .offset = 0, .length=8, .msb_right =0 },
91 .transp= { .offset = 0, .length=8, .msb_right =0 }
92 },
93 { .name = "CLUT4 ARGB1555", // CLUT4 (ARGB1555)
94 .bpp = 4, .pixenum = CLUT4,
95 .red = { .offset = 0, .length=5, .msb_right =0 },
96 .green = { .offset = 0, .length=5, .msb_right =0 },
97 .blue = { .offset = 0, .length=5, .msb_right =0 },
98 .transp= { .offset = 0, .length=1, .msb_right =0 }
99 },
100 { .name = "CLUT8 ARGB8888", // CLUT8 (ARGB8888)
101 .bpp = 8, .pixenum = CLUT8,
102 .red = { .offset = 0, .length=8, .msb_right =0 },
103 .green = { .offset = 0, .length=8, .msb_right =0 },
104 .blue = { .offset = 0, .length=8, .msb_right =0 },
105 .transp= { .offset = 0, .length=8, .msb_right =0 }
106 },
107 { .name = "CLUT8 ARGB1555", // CLUT8 (ARGB1555)
108 .bpp = 8, .pixenum = CLUT8,
109 .red = { .offset = 0, .length=5, .msb_right =0 },
110 .green = { .offset = 0, .length=5, .msb_right =0 },
111 .blue = { .offset = 0, .length=5, .msb_right =0 },
112 .transp= { .offset = 0, .length=1, .msb_right =0 }
113 },
114 { .name = "ARGB1555", // ARGB1555
115 .bpp = 16, .pixenum = ARGB1555,
116 .red = { .offset = 10, .length=5, .msb_right =0 },
117 .green = { .offset = 5, .length=5, .msb_right =0 },
118 .blue = { .offset = 0, .length=5, .msb_right =0 },
119 .transp= { .offset = 15, .length=1, .msb_right =0 }
120 },
121 { .name = "RGB565", // RGB565
122 .bpp = 16, .pixenum = RGB565,
123 .red = { .offset = 11, .length=5, .msb_right =0 },
124 .green = { .offset = 5, .length=6, .msb_right =0 },
125 .blue = { .offset = 0, .length=5, .msb_right =0 },
126 .transp= { .offset = 0, .length=0, .msb_right =0 }
127 },
128 { .name = "ARGB", // 32 f*cking bits, the real McCoy :)
129 .bpp = 32, .pixenum = ARGB,
130 .red = { .offset = 16, .length=8, .msb_right =0 },
131 .green = { .offset = 8, .length=8, .msb_right =0 },
132 .blue = { .offset = 0, .length=8, .msb_right =0 },
133 .transp= { .offset = 24, .length=8, .msb_right =0 }
134 }
135 };
136 #define PIXELFORMATNUM (sizeof(pixelformattable)/sizeof(struct pixelformat))
137
138 struct colour {
139 __u16 r;
140 __u16 g;
141 __u16 b;
142 __u16 a;
143 };
144 static
145 struct colour colourtable[] = {
146 {.r =0xffff, .g = 0xffff, .b=0xffff, .a=0xffff}, // fully transparent white
147 {.r =0xffff, .g = 0x0000, .b=0x0000, .a=0x0000}, // red
148 {.r =0x0000, .g = 0xffff, .b=0x0000, .a=0x0000}, // green
149 {.r =0x0000, .g = 0x0000, .b=0xffff, .a=0x0000}, // blue
150 {.r =0x0000, .g = 0x0000, .b=0x0000, .a=0x0000} // black
151 };
152 #define COLOURNUM (sizeof(colourtable)/sizeof(struct colour))
153
154 struct rect{
155 int x;
156 int y;
157 int width;
158 int height;
159 const struct colour *col;
160 };
161 struct pixel{ // up to 32 bits of pixel information
162 char byte[4];
163 };
164
165 void col2pixel (struct pixel *pix, const struct pixelformat *pixf, const struct colour *col){
166 switch (pixf->pixenum){
167 case RGB565:
168 pix->byte[0]=(col->r&0xf8)|(col->g&0xfc)>>5;
169 pix->byte[1]=(col->g&0xfc)<<3|(col->b&0xf8)>>3;
170 break;
171 case ARGB1555:
172 pix->byte[0]=(col->a&0x80)|(col->r&0xf8)>>1|(col->g&0xf8)>>6;
173 pix->byte[1]=(col->g&0xf8)<<2|(col->b&0xf8)>>3;
174 break;
175 case ARGB:
176 pix->byte[0]=col->a;
177 pix->byte[1]=col->r;
178 pix->byte[2]=col->g;
179 pix->byte[3]=col->b;
180 break;
181 default:
182 printf ("unknown pixelformat\n");
183 exit(1);
184 }
185 }
186
187 int setmode(int fbd, const struct pixelformat *pixf,const struct vidsize *vids){
188 struct fb_var_screeninfo var;
189 int stat;
190 stat = ioctl (fbd, FBIOGET_VSCREENINFO,&var);
191 if (stat<0) return -2;
192
193 var.xres= vids->width;
194 var.xres_virtual = vids->width;
195 var.yres= vids->height;
196 var.yres_virtual = vids->height;
197
198 var.bits_per_pixel = pixf->bpp;
199 var.red = pixf->red;
200 var.green = pixf->green;
201 var.blue = pixf->blue;
202 var.transp = pixf->transp;
203
204 stat = ioctl (fbd, FBIOPUT_VSCREENINFO,&var);
205 if (stat<0) return -1;
206 return 0;
207 }
208
209 // unefficient implementation, do NOT use it for your next ego shooter, please :)
210 // for 4-Bit only rectangles with even width are supported
211 // CLUT-modes use value of red component as index
212 void drawrect(void *videoram, struct rect *r, const struct pixelformat *pixf, const struct vidsize *vids){
213 int x,y,corwidth, bpp = 0, tocopy = 1;
214 struct pixel pix;
215 unsigned char *pmem = videoram;
216 corwidth = r->width; // actually only "corrected" for 4 Bit
217
218 if (pixf->pixenum!=CLUT4&&pixf->pixenum!=CLUT8){
219 switch (pixf->pixenum){
220 case ARGB1555:
221 case RGB565:
222 bpp = 16;
223 tocopy = 2;
224 break;
225 case ARGB:
226 bpp = 32;
227 tocopy = 4;
228 break;
229 default:
230 printf ("drawrect: unknown pixelformat(%d) bpp:%d\n",pixf->pixenum,pixf->bpp);
231 exit(1);
232 }
233 col2pixel(&pix,pixf,r->col);
234 } else {
235 switch (pixf->pixenum){ // CLUT = Colour LookUp Table (palette)
236 case CLUT4: // take red value as index in this case
237 pix.byte[0]=(r->col->r)<<4|(r->col->r&0xf); // slightly cryptic... "rect->colour->red"
238 corwidth>>=1; // we copy bytes
239 bpp=4;
240 tocopy=1;
241 break;
242 case CLUT8:
243 pix.byte[0]=(r->col->r&0xff);
244 bpp=8;
245 tocopy=1;
246 break;
247 }
248 }
249 pmem=videoram+((((r->y*vids->width)+r->x)*bpp)>>3);
250 for (y=0;y<r->height;y++){
251 int offset = 0;
252 for (x=0;x<corwidth;x++){
253 memcpy (pmem+offset,pix.byte,tocopy);
254 offset+=tocopy;
255 }
256 pmem +=((vids->width*bpp)>>3); // skip one whole line, actually should be taken from "fix-info"
257 }
258 }
259
260 // create quick little test image, 4 colours from table
261 void draw4field(void *videoram, const struct pixelformat *pixf, const struct vidsize *vids){
262 struct rect r;
263 struct colour c;
264 int height, width;
265 c.r = 1; // only used for the indexed modes, r is taken as index
266 height = vids->height;
267 width = vids->width;
268
269 r.height = height>>1;
270 r.width = width>>1;
271 r.x = 0; r.y = 0;
272 if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) r.col = &c;
273 else r.col = &colourtable[1];
274 drawrect (videoram, &r, pixf, vids);
275
276 r.x = width/2; r.y = 0;
277 if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) c.r = 2;
278 else r.col = &colourtable[2];
279 drawrect (videoram, &r, pixf, vids);
280
281 r.x = 0; r.y = height/2;
282 if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) c.r = 3;
283 else r.col = &colourtable[3];
284 drawrect (videoram, &r, pixf, vids);
285
286 r.x = width/2; r.y = height/2;
287 if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) c.r = 0;
288 else r.col = &colourtable[0];
289 drawrect (videoram, &r, pixf, vids);
290 }
291
292 void usage(char *name){
293 printf ("Usage: %s [options]\n"
294 "Options: -f<pixelformat>\n"
295 " where format is one of:\n"
296 " CLUT4,CLUT8,ARGB1555,RGB565,ARGB\n"
297 " -s<width>x<heigth>\n"
298 " where width is either 720,640,360,320\n"
299 " and height is either 288,240,480,576\n"
300 " -n\n"
301 " disables clearing the framebuffer after drawing\n"
302 " the testimage. This can be useful to keep the last\n"
303 " drawn image onscreen.\n"
304 "\nExample: %s -fRGB322\n",name,name);
305 exit(0);
306 }
307
308 int main (int argc,char **argv){
309 struct fb_fix_screeninfo fix;
310 struct fb_var_screeninfo var;
311 struct fb_cmap cmap;
312 struct rect r;
313 int fbd;
314 unsigned char *pfb;
315 int stat;
316 int optchar,fmode=-1,smode=-1,clear=1;
317 int i_cmap,i_size,i_pix;
318 extern char *optarg;
319
320 if (argc!=0&&argc>4) usage(argv[0]);
321 while ( (optchar = getopt (argc,argv,"f:s:n"))!= -1){
322 int i,height,width;
323 switch (optchar){
324 case 'f':
325 for (i=0;i<(sizeof(pixname)/sizeof(char*));i++){
326 if (!strncmp (optarg,pixname[i],strlen(pixname[i]))){
327 fmode=i;
328 printf ("displaying only %s-modes\n",pixname[i]);
329 break;
330 }
331 }
332 if (fmode==-1){
333 printf ("unknown pixelformat\n");
334 exit(0);
335 }
336 break;
337 case 's':
338 if (sscanf (optarg,"%dx%d",&width,&height)!=2){
339 printf ("parsing size failed\n");
340 exit(0);
341 } else {
342 printf ("requested size %dx%d\n",width,height);
343 for (i=0;i<VIDSIZENUM;i++){
344 if (vidsizetable[i].width == width &&
345 vidsizetable[i].height == height){
346 smode = i;
347 break;
348 }
349 }
350 if (smode==-1){
351 printf ("this size is not supported\n");
352 exit(0);
353 }
354 }
355 break;
356 case 'n':
357 clear = 0;
358 printf ("clearing framebuffer after drawing is disabled\n");
359 break;
360 case '?':
361 usage (argv[0]);
362 }
363 }
364
365 fbd = open (FBDEV, O_RDWR);
366 if (fbd<0){
367 perror ("Error opening framebuffer device");
368 return 1;
369 }
370 stat = ioctl (fbd, FBIOGET_FSCREENINFO,&fix);
371 if (stat<0){
372 perror ("Error getting fix screeninfo");
373 return 1;
374 }
375 stat = ioctl (fbd, FBIOGET_VSCREENINFO,&var);
376 if (stat<0){
377 perror ("Error getting var screeninfo");
378 return 1;
379 }
380 stat = ioctl (fbd, FBIOPUT_VSCREENINFO,&var);
381 if (stat<0){
382 perror ("Error setting mode");
383 return 1;
384 }
385 pfb = mmap (0, fix.smem_len, PROT_READ|PROT_WRITE, MAP_SHARED, fbd, 0);
386 if (pfb == MAP_FAILED){
387 perror ("Error mmap'ing framebuffer device");
388 return 1;
389 }
390
391 // iterate over all modes
392 for (i_pix=0;i_pix<PIXELFORMATNUM;i_pix++){
393 if (fmode!=-1 && pixelformattable[i_pix].pixenum != fmode) continue;
394 printf ("testing: %s",pixelformattable[i_pix].name);
395 printf (" for sizes: \n");
396 for (i_size=0;i_size<VIDSIZENUM;i_size++){
397 if (smode!=-1 && i_size!=smode) continue;
398 printf ("%dx%d ",vidsizetable[i_size].width,vidsizetable[i_size].height);
399 fflush(stdout);
400 if ((i_size%4)==3) printf ("\n");
401
402 // try to set mode
403 stat = setmode(fbd,&pixelformattable[i_pix],&vidsizetable[i_size]);
404 if (stat==-2) perror ("fbtest: could not get fb_var-screeninfo from fb-device");
405 else if (stat==-1){
406 printf ("\nCould not set mode %s (%dx%d), possible reasons:\n"
407 "- you have a GTX (soz m8)\n"
408 "- your configuration does not have enough graphics RAM\n"
409 "- you found a bug\n"
410 "choose your poison accordingly...\n",
411 pixelformattable[i_pix].name,vidsizetable[i_size].width,vidsizetable[i_size].height);
412 continue;
413 }
414 // fill cmap;
415 cmap.len = 1;
416 if ((pixelformattable[i_pix].bpp==4)||
417 ((pixelformattable[i_pix].bpp==8)&&(pixelformattable[i_pix].red.length!=3))){
418 for (i_cmap=0;i_cmap<COLOURNUM;i_cmap++){
419 cmap.start=i_cmap;
420 cmap.red=&colourtable[i_cmap].r;
421 cmap.green=&colourtable[i_cmap].g;
422 cmap.blue=&colourtable[i_cmap].b;
423 cmap.transp=&colourtable[i_cmap].a;
424 stat = ioctl (fbd, FBIOPUTCMAP, &cmap);
425 if (stat<0) printf ("setting colourmap failed\n");
426 }
427 }
428 // create the test image
429 draw4field(pfb,&pixelformattable[i_pix],&vidsizetable[i_size]);
430 usleep (500000);
431 // clear screen
432 if (clear){
433 r.x=r.y=0;r.width = vidsizetable[i_size].width; r.height = vidsizetable[i_size].height;
434 r.col = &colourtable[4];
435 drawrect(pfb,&r,&pixelformattable[i_pix],&vidsizetable[i_size]);
436 }
437 }
438 printf ("\n");
439 }
440
441 stat = munmap (pfb,fix.smem_len);
442 if (stat<0){
443 perror ("Error munmap'ing framebuffer device");
444 return 1;
445 }
446 close (fbd);
447 return 0;
448 }