1 /******************************************************************************
3 * test program for the tuxbox-framebuffer device
4 * tests all GTX/eNX supported modes
6 * (c) 2003 Carsten Juttner (carjay@gmx.net)
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.
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.
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.
22 ******************************************************************************
23 * $Id: fbtest.c,v 1.5 2005/01/14 23:14:41 carjay Exp $
24 ******************************************************************************/
26 // TODO: - should restore the colour map and mode to what it was before
27 // - is colour map handled correctly?
34 #include <sys/types.h>
36 #include <sys/ioctl.h>
42 #define FBDEV "/dev/fb0"
49 const struct vidsize vidsizetable[]={ // all supported sizes
50 {720,576},{720,480},{720,288},{720,240},
51 {640,576},{640,480},{640,288},{640,240},
52 {360,576},{360,480},{360,288},{360,240},
53 {320,576},{320,480},{320,288},{320,240}
55 #define VIDSIZENUM (sizeof(vidsizetable)/sizeof(struct vidsize))
57 enum pixenum{ // keep in sync with pixname !
64 const char *pixname[] = {
74 struct fb_bitfield red;
75 struct fb_bitfield green;
76 struct fb_bitfield blue;
77 struct fb_bitfield transp;
82 static // so far these are all modes supported by the eNX (only partially by GTX)
83 const struct pixelformat pixelformattable[] = {
84 { .name = "CLUT4 ARGB8888", // CLUT4 (ARGB8888)
85 .bpp = 4, .pixenum = CLUT4,
86 .red = { .offset = 0, .length=8, .msb_right =0 },
87 .green = { .offset = 0, .length=8, .msb_right =0 },
88 .blue = { .offset = 0, .length=8, .msb_right =0 },
89 .transp= { .offset = 0, .length=8, .msb_right =0 }
91 { .name = "CLUT4 ARGB1555", // CLUT4 (ARGB1555)
92 .bpp = 4, .pixenum = CLUT4,
93 .red = { .offset = 0, .length=5, .msb_right =0 },
94 .green = { .offset = 0, .length=5, .msb_right =0 },
95 .blue = { .offset = 0, .length=5, .msb_right =0 },
96 .transp= { .offset = 0, .length=1, .msb_right =0 }
98 { .name = "CLUT8 ARGB8888", // CLUT8 (ARGB8888)
99 .bpp = 8, .pixenum = CLUT8,
100 .red = { .offset = 0, .length=8, .msb_right =0 },
101 .green = { .offset = 0, .length=8, .msb_right =0 },
102 .blue = { .offset = 0, .length=8, .msb_right =0 },
103 .transp= { .offset = 0, .length=8, .msb_right =0 }
105 { .name = "CLUT8 ARGB1555", // CLUT8 (ARGB1555)
106 .bpp = 8, .pixenum = CLUT8,
107 .red = { .offset = 0, .length=5, .msb_right =0 },
108 .green = { .offset = 0, .length=5, .msb_right =0 },
109 .blue = { .offset = 0, .length=5, .msb_right =0 },
110 .transp= { .offset = 0, .length=1, .msb_right =0 }
112 { .name = "ARGB1555", // ARGB1555
113 .bpp = 16, .pixenum = ARGB1555,
114 .red = { .offset = 10, .length=5, .msb_right =0 },
115 .green = { .offset = 5, .length=5, .msb_right =0 },
116 .blue = { .offset = 0, .length=5, .msb_right =0 },
117 .transp= { .offset = 15, .length=1, .msb_right =0 }
119 { .name = "RGB565", // RGB565
120 .bpp = 16, .pixenum = RGB565,
121 .red = { .offset = 11, .length=5, .msb_right =0 },
122 .green = { .offset = 5, .length=6, .msb_right =0 },
123 .blue = { .offset = 0, .length=5, .msb_right =0 },
124 .transp= { .offset = 0, .length=0, .msb_right =0 }
126 { .name = "ARGB", // 32 f*cking bits, the real McCoy :)
127 .bpp = 32, .pixenum = ARGB,
128 .red = { .offset = 16, .length=8, .msb_right =0 },
129 .green = { .offset = 8, .length=8, .msb_right =0 },
130 .blue = { .offset = 0, .length=8, .msb_right =0 },
131 .transp= { .offset = 24, .length=8, .msb_right =0 }
134 #define PIXELFORMATNUM (sizeof(pixelformattable)/sizeof(struct pixelformat))
143 struct colour colourtable[] = {
144 {.r =0xffff, .g = 0xffff, .b=0xffff, .a=0xffff}, // fully transparent white
145 {.r =0xffff, .g = 0x0000, .b=0x0000, .a=0x0000}, // red
146 {.r =0x0000, .g = 0xffff, .b=0x0000, .a=0x0000}, // green
147 {.r =0x0000, .g = 0x0000, .b=0xffff, .a=0x0000}, // blue
148 {.r =0x0000, .g = 0x0000, .b=0x0000, .a=0x0000} // black
150 #define COLOURNUM (sizeof(colourtable)/sizeof(struct colour))
157 const struct colour *col;
159 struct pixel{ // up to 32 bits of pixel information
163 void col2pixel (struct pixel *pix, const struct pixelformat *pixf, const struct colour *col){
164 switch (pixf->pixenum){
166 pix->byte[0]=(col->r&0xf8)|(col->g&0xfc)>>5;
167 pix->byte[1]=(col->g&0xfc)<<3|(col->b&0xf8)>>3;
170 pix->byte[0]=(col->a&0x80)|(col->r&0xf8)>>1|(col->g&0xf8)>>6;
171 pix->byte[1]=(col->g&0xf8)<<2|(col->b&0xf8)>>3;
180 printf ("unknown pixelformat\n");
185 int setmode(int fbd, const struct pixelformat *pixf,const struct vidsize *vids){
186 struct fb_var_screeninfo var;
188 stat = ioctl (fbd, FBIOGET_VSCREENINFO,&var);
189 if (stat<0) return -2;
191 var.xres= vids->width;
192 var.xres_virtual = vids->width;
193 var.yres= vids->height;
194 var.yres_virtual = vids->height;
196 var.bits_per_pixel = pixf->bpp;
198 var.green = pixf->green;
199 var.blue = pixf->blue;
200 var.transp = pixf->transp;
202 stat = ioctl (fbd, FBIOPUT_VSCREENINFO,&var);
203 if (stat<0) return -1;
207 // unefficient implementation, do NOT use it for your next ego shooter, please :)
208 // for 4-Bit only rectangles with even width are supported
209 // CLUT-modes use value of red component as index
210 void drawrect(void *videoram, struct rect *r, const struct pixelformat *pixf, const struct vidsize *vids){
211 int x,y,corwidth, bpp = 0, tocopy = 1;
213 unsigned char *pmem = videoram;
214 corwidth = r->width; // actually only "corrected" for 4 Bit
216 if (pixf->pixenum!=CLUT4&&pixf->pixenum!=CLUT8){
217 switch (pixf->pixenum){
228 printf ("drawrect: unknown pixelformat(%d) bpp:%d\n",pixf->pixenum,pixf->bpp);
231 col2pixel(&pix,pixf,r->col);
233 switch (pixf->pixenum){ // CLUT = Colour LookUp Table (palette)
234 case CLUT4: // take red value as index in this case
235 pix.byte[0]=(r->col->r)<<4|(r->col->r&0xf); // slightly cryptic... "rect->colour->red"
236 corwidth>>=1; // we copy bytes
241 pix.byte[0]=(r->col->r&0xff);
247 pmem=videoram+((((r->y*vids->width)+r->x)*bpp)>>3);
248 for (y=0;y<r->height;y++){
250 for (x=0;x<corwidth;x++){
251 memcpy (pmem+offset,pix.byte,tocopy);
254 pmem +=((vids->width*bpp)>>3); // skip one whole line, actually should be taken from "fix-info"
258 // create quick little test image, 4 colours from table
259 void draw4field(void *videoram, const struct pixelformat *pixf, const struct vidsize *vids){
263 c.r = 1; // only used for the indexed modes, r is taken as index
264 height = vids->height;
267 r.height = height>>1;
270 if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) r.col = &c;
271 else r.col = &colourtable[1];
272 drawrect (videoram, &r, pixf, vids);
274 r.x = width/2; r.y = 0;
275 if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) c.r = 2;
276 else r.col = &colourtable[2];
277 drawrect (videoram, &r, pixf, vids);
279 r.x = 0; r.y = height/2;
280 if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) c.r = 3;
281 else r.col = &colourtable[3];
282 drawrect (videoram, &r, pixf, vids);
284 r.x = width/2; r.y = height/2;
285 if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) c.r = 0;
286 else r.col = &colourtable[0];
287 drawrect (videoram, &r, pixf, vids);
290 void usage(char *name){
291 printf ("Usage: %s [options]\n"
292 "Options: -f<pixelformat>\n"
293 " where format is one of:\n"
294 " CLUT4,CLUT8,ARGB1555,RGB565,ARGB\n"
295 " -s<width>x<heigth>\n"
296 " where width is either 720,640,360,320\n"
297 " and height is either 288,240,480,576\n"
299 " disables clearing the framebuffer after drawing\n"
300 " the testimage. This can be useful to keep the last\n"
301 " drawn image onscreen.\n"
302 "\nExample: %s -fRGB322\n",name,name);
306 int main (int argc,char **argv){
307 struct fb_fix_screeninfo fix;
308 struct fb_var_screeninfo var;
314 int optchar,fmode=-1,smode=-1,clear=1;
315 int i_cmap,i_size,i_pix;
318 if (argc!=0&&argc>4) usage(argv[0]);
319 while ( (optchar = getopt (argc,argv,"f:s:n"))!= -1){
323 for (i=0;i<(sizeof(pixname)/sizeof(char*));i++){
324 if (!strncmp (optarg,pixname[i],strlen(pixname[i]))){
326 printf ("displaying only %s-modes\n",pixname[i]);
331 printf ("unknown pixelformat\n");
336 if (sscanf (optarg,"%dx%d",&width,&height)!=2){
337 printf ("parsing size failed\n");
340 printf ("requested size %dx%d\n",width,height);
341 for (i=0;i<VIDSIZENUM;i++){
342 if (vidsizetable[i].width == width &&
343 vidsizetable[i].height == height){
349 printf ("this size is not supported\n");
356 printf ("clearing framebuffer after drawing is disabled\n");
363 fbd = open (FBDEV, O_RDWR);
365 perror ("Error opening framebuffer device");
368 stat = ioctl (fbd, FBIOGET_FSCREENINFO,&fix);
370 perror ("Error getting fix screeninfo");
373 stat = ioctl (fbd, FBIOGET_VSCREENINFO,&var);
375 perror ("Error getting var screeninfo");
378 stat = ioctl (fbd, FBIOPUT_VSCREENINFO,&var);
380 perror ("Error setting mode");
383 pfb = mmap (0, fix.smem_len, PROT_READ|PROT_WRITE, MAP_SHARED, fbd, 0);
384 if (pfb == MAP_FAILED){
385 perror ("Error mmap'ing framebuffer device");
389 // iterate over all modes
390 for (i_pix=0;i_pix<PIXELFORMATNUM;i_pix++){
391 if (fmode!=-1 && pixelformattable[i_pix].pixenum != fmode) continue;
392 printf ("testing: %s",pixelformattable[i_pix].name);
393 printf (" for sizes: \n");
394 for (i_size=0;i_size<VIDSIZENUM;i_size++){
395 if (smode!=-1 && i_size!=smode) continue;
396 printf ("%dx%d ",vidsizetable[i_size].width,vidsizetable[i_size].height);
398 if ((i_size%4)==3) printf ("\n");
401 stat = setmode(fbd,&pixelformattable[i_pix],&vidsizetable[i_size]);
402 if (stat==-2) perror ("fbtest: could not get fb_var-screeninfo from fb-device");
404 printf ("\nCould not set mode %s (%dx%d), possible reasons:\n"
405 "- you have a GTX (soz m8)\n"
406 "- your configuration does not have enough graphics RAM\n"
407 "- you found a bug\n"
408 "choose your poison accordingly...\n",
409 pixelformattable[i_pix].name,vidsizetable[i_size].width,vidsizetable[i_size].height);
414 if ((pixelformattable[i_pix].bpp==4)||
415 ((pixelformattable[i_pix].bpp==8)&&(pixelformattable[i_pix].red.length!=3))){
416 for (i_cmap=0;i_cmap<COLOURNUM;i_cmap++){
418 cmap.red=&colourtable[i_cmap].r;
419 cmap.green=&colourtable[i_cmap].g;
420 cmap.blue=&colourtable[i_cmap].b;
421 cmap.transp=&colourtable[i_cmap].a;
422 stat = ioctl (fbd, FBIOPUTCMAP, &cmap);
423 if (stat<0) printf ("setting colourmap failed\n");
426 // create the test image
427 draw4field(pfb,&pixelformattable[i_pix],&vidsizetable[i_size]);
431 r.x=r.y=0;r.width = vidsizetable[i_size].width; r.height = vidsizetable[i_size].height;
432 r.col = &colourtable[4];
433 drawrect(pfb,&r,&pixelformattable[i_pix],&vidsizetable[i_size]);
439 stat = munmap (pfb,fix.smem_len);
441 perror ("Error munmap'ing framebuffer device");