fbtest.c 13 KB

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