b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 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 | #define FBDEV "/dev/fb0" |
| 43 | |
| 44 | struct vidsize{ |
| 45 | int width; |
| 46 | int height; |
| 47 | }; |
| 48 | static |
| 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} |
| 54 | }; |
| 55 | #define VIDSIZENUM (sizeof(vidsizetable)/sizeof(struct vidsize)) |
| 56 | |
| 57 | enum pixenum{ // keep in sync with pixname ! |
| 58 | CLUT4=0, |
| 59 | CLUT8, |
| 60 | RGB565, |
| 61 | ARGB1555, |
| 62 | ARGB |
| 63 | }; |
| 64 | const char *pixname[] = { |
| 65 | "CLUT4", |
| 66 | "CLUT8", |
| 67 | "RGB565", |
| 68 | "ARGB1555", |
| 69 | "ARGB" |
| 70 | }; |
| 71 | |
| 72 | struct pixelformat{ |
| 73 | char *name; |
| 74 | struct fb_bitfield red; |
| 75 | struct fb_bitfield green; |
| 76 | struct fb_bitfield blue; |
| 77 | struct fb_bitfield transp; |
| 78 | char bpp; |
| 79 | char pixenum; |
| 80 | }; |
| 81 | |
| 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 } |
| 90 | }, |
| 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 } |
| 97 | }, |
| 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 } |
| 104 | }, |
| 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 } |
| 111 | }, |
| 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 } |
| 118 | }, |
| 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 } |
| 125 | }, |
| 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 } |
| 132 | } |
| 133 | }; |
| 134 | #define PIXELFORMATNUM (sizeof(pixelformattable)/sizeof(struct pixelformat)) |
| 135 | |
| 136 | struct colour { |
| 137 | __u16 r; |
| 138 | __u16 g; |
| 139 | __u16 b; |
| 140 | __u16 a; |
| 141 | }; |
| 142 | static |
| 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 |
| 149 | }; |
| 150 | #define COLOURNUM (sizeof(colourtable)/sizeof(struct colour)) |
| 151 | |
| 152 | struct rect{ |
| 153 | int x; |
| 154 | int y; |
| 155 | int width; |
| 156 | int height; |
| 157 | const struct colour *col; |
| 158 | }; |
| 159 | struct pixel{ // up to 32 bits of pixel information |
| 160 | char byte[4]; |
| 161 | }; |
| 162 | |
| 163 | void col2pixel (struct pixel *pix, const struct pixelformat *pixf, const struct colour *col){ |
| 164 | switch (pixf->pixenum){ |
| 165 | case RGB565: |
| 166 | pix->byte[0]=(col->r&0xf8)|(col->g&0xfc)>>5; |
| 167 | pix->byte[1]=(col->g&0xfc)<<3|(col->b&0xf8)>>3; |
| 168 | break; |
| 169 | case ARGB1555: |
| 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; |
| 172 | break; |
| 173 | case ARGB: |
| 174 | pix->byte[0]=col->a; |
| 175 | pix->byte[1]=col->r; |
| 176 | pix->byte[2]=col->g; |
| 177 | pix->byte[3]=col->b; |
| 178 | break; |
| 179 | default: |
| 180 | printf ("unknown pixelformat\n"); |
| 181 | exit(1); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | int setmode(int fbd, const struct pixelformat *pixf,const struct vidsize *vids){ |
| 186 | struct fb_var_screeninfo var; |
| 187 | int stat; |
| 188 | stat = ioctl (fbd, FBIOGET_VSCREENINFO,&var); |
| 189 | if (stat<0) return -2; |
| 190 | |
| 191 | var.xres= vids->width; |
| 192 | var.xres_virtual = vids->width; |
| 193 | var.yres= vids->height; |
| 194 | var.yres_virtual = vids->height; |
| 195 | |
| 196 | var.bits_per_pixel = pixf->bpp; |
| 197 | var.red = pixf->red; |
| 198 | var.green = pixf->green; |
| 199 | var.blue = pixf->blue; |
| 200 | var.transp = pixf->transp; |
| 201 | |
| 202 | stat = ioctl (fbd, FBIOPUT_VSCREENINFO,&var); |
| 203 | if (stat<0) return -1; |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 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; |
| 212 | struct pixel pix; |
| 213 | unsigned char *pmem = videoram; |
| 214 | corwidth = r->width; // actually only "corrected" for 4 Bit |
| 215 | |
| 216 | if (pixf->pixenum!=CLUT4&&pixf->pixenum!=CLUT8){ |
| 217 | switch (pixf->pixenum){ |
| 218 | case ARGB1555: |
| 219 | case RGB565: |
| 220 | bpp = 16; |
| 221 | tocopy = 2; |
| 222 | break; |
| 223 | case ARGB: |
| 224 | bpp = 32; |
| 225 | tocopy = 4; |
| 226 | break; |
| 227 | default: |
| 228 | printf ("drawrect: unknown pixelformat(%d) bpp:%d\n",pixf->pixenum,pixf->bpp); |
| 229 | exit(1); |
| 230 | } |
| 231 | col2pixel(&pix,pixf,r->col); |
| 232 | } else { |
| 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 |
| 237 | bpp=4; |
| 238 | tocopy=1; |
| 239 | break; |
| 240 | case CLUT8: |
| 241 | pix.byte[0]=(r->col->r&0xff); |
| 242 | bpp=8; |
| 243 | tocopy=1; |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | pmem=videoram+((((r->y*vids->width)+r->x)*bpp)>>3); |
| 248 | for (y=0;y<r->height;y++){ |
| 249 | int offset = 0; |
| 250 | for (x=0;x<corwidth;x++){ |
| 251 | memcpy (pmem+offset,pix.byte,tocopy); |
| 252 | offset+=tocopy; |
| 253 | } |
| 254 | pmem +=((vids->width*bpp)>>3); // skip one whole line, actually should be taken from "fix-info" |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | // create quick little test image, 4 colours from table |
| 259 | void draw4field(void *videoram, const struct pixelformat *pixf, const struct vidsize *vids){ |
| 260 | struct rect r; |
| 261 | struct colour c; |
| 262 | int height, width; |
| 263 | c.r = 1; // only used for the indexed modes, r is taken as index |
| 264 | height = vids->height; |
| 265 | width = vids->width; |
| 266 | |
| 267 | r.height = height>>1; |
| 268 | r.width = width>>1; |
| 269 | r.x = 0; r.y = 0; |
| 270 | if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) r.col = &c; |
| 271 | else r.col = &colourtable[1]; |
| 272 | drawrect (videoram, &r, pixf, vids); |
| 273 | |
| 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); |
| 278 | |
| 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); |
| 283 | |
| 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); |
| 288 | } |
| 289 | |
| 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" |
| 298 | " -n\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); |
| 303 | exit(0); |
| 304 | } |
| 305 | |
| 306 | int main (int argc,char **argv){ |
| 307 | struct fb_fix_screeninfo fix; |
| 308 | struct fb_var_screeninfo var; |
| 309 | struct fb_cmap cmap; |
| 310 | struct rect r; |
| 311 | int fbd; |
| 312 | unsigned char *pfb; |
| 313 | int stat; |
| 314 | int optchar,fmode=-1,smode=-1,clear=1; |
| 315 | int i_cmap,i_size,i_pix; |
| 316 | extern char *optarg; |
| 317 | |
| 318 | if (argc!=0&&argc>4) usage(argv[0]); |
| 319 | while ( (optchar = getopt (argc,argv,"f:s:n"))!= -1){ |
| 320 | int i,height,width; |
| 321 | switch (optchar){ |
| 322 | case 'f': |
| 323 | for (i=0;i<(sizeof(pixname)/sizeof(char*));i++){ |
| 324 | if (!strncmp (optarg,pixname[i],strlen(pixname[i]))){ |
| 325 | fmode=i; |
| 326 | printf ("displaying only %s-modes\n",pixname[i]); |
| 327 | break; |
| 328 | } |
| 329 | } |
| 330 | if (fmode==-1){ |
| 331 | printf ("unknown pixelformat\n"); |
| 332 | exit(0); |
| 333 | } |
| 334 | break; |
| 335 | case 's': |
| 336 | if (sscanf (optarg,"%dx%d",&width,&height)!=2){ |
| 337 | printf ("parsing size failed\n"); |
| 338 | exit(0); |
| 339 | } else { |
| 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){ |
| 344 | smode = i; |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | if (smode==-1){ |
| 349 | printf ("this size is not supported\n"); |
| 350 | exit(0); |
| 351 | } |
| 352 | } |
| 353 | break; |
| 354 | case 'n': |
| 355 | clear = 0; |
| 356 | printf ("clearing framebuffer after drawing is disabled\n"); |
| 357 | break; |
| 358 | case '?': |
| 359 | usage (argv[0]); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | fbd = open (FBDEV, O_RDWR); |
| 364 | if (fbd<0){ |
| 365 | perror ("Error opening framebuffer device"); |
| 366 | return 1; |
| 367 | } |
| 368 | stat = ioctl (fbd, FBIOGET_FSCREENINFO,&fix); |
| 369 | if (stat<0){ |
| 370 | perror ("Error getting fix screeninfo"); |
| 371 | return 1; |
| 372 | } |
| 373 | stat = ioctl (fbd, FBIOGET_VSCREENINFO,&var); |
| 374 | if (stat<0){ |
| 375 | perror ("Error getting var screeninfo"); |
| 376 | return 1; |
| 377 | } |
| 378 | stat = ioctl (fbd, FBIOPUT_VSCREENINFO,&var); |
| 379 | if (stat<0){ |
| 380 | perror ("Error setting mode"); |
| 381 | return 1; |
| 382 | } |
| 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"); |
| 386 | return 1; |
| 387 | } |
| 388 | |
| 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); |
| 397 | fflush(stdout); |
| 398 | if ((i_size%4)==3) printf ("\n"); |
| 399 | |
| 400 | // try to set mode |
| 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"); |
| 403 | else if (stat==-1){ |
| 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); |
| 410 | continue; |
| 411 | } |
| 412 | // fill cmap; |
| 413 | cmap.len = 1; |
| 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++){ |
| 417 | cmap.start=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"); |
| 424 | } |
| 425 | } |
| 426 | // create the test image |
| 427 | draw4field(pfb,&pixelformattable[i_pix],&vidsizetable[i_size]); |
| 428 | usleep (500000); |
| 429 | // clear screen |
| 430 | if (clear){ |
| 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]); |
| 434 | } |
| 435 | } |
| 436 | printf ("\n"); |
| 437 | } |
| 438 | |
| 439 | stat = munmap (pfb,fix.smem_len); |
| 440 | if (stat<0){ |
| 441 | perror ("Error munmap'ing framebuffer device"); |
| 442 | return 1; |
| 443 | } |
| 444 | close (fbd); |
| 445 | return 0; |
| 446 | } |