fbtest.c 13 KB

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