summaryrefslogtreecommitdiff
path: root/package/utils/fbtest/src/fbtest.c
blob: 021b80303ce8305c8bc6ed9792ed7dcf035df62e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/******************************************************************************
 *	fbtest - fbtest.c
 *	test program for the tuxbox-framebuffer device
 *	tests all GTX/eNX supported modes
 *                                                                            
 *	(c) 2003 Carsten Juttner (carjay@gmx.net)
 *
 * 	This program is free software; you can redistribute it and/or modify
 * 	it under the terms of the GNU General Public License as published by
 * 	The Free Software Foundation; either version 2 of the License, or
 * 	(at your option) any later version.
 *
 * 	This program is distributed in the hope that it will be useful,
 * 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * 	GNU General Public License for more details.
 *
 * 	You should have received a copy of the GNU General Public License
 * 	along with this program; if not, write to the Free Software
 * 	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  									      
 ******************************************************************************
 * $Id: fbtest.c,v 1.5 2005/01/14 23:14:41 carjay Exp $
 ******************************************************************************/

// TODO: - should restore the colour map and mode to what it was before
//	 - is colour map handled correctly?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <fcntl.h>

#include <linux/fb.h>

#define FBDEV "/dev/fb0"

struct vidsize{
	int width;
	int height;
};
static
const struct vidsize vidsizetable[]={	// all supported sizes
	{720,576},{720,480},{720,288},{720,240},
	{640,576},{640,480},{640,288},{640,240},
	{360,576},{360,480},{360,288},{360,240},
	{320,576},{320,480},{320,288},{320,240}
};
#define VIDSIZENUM (sizeof(vidsizetable)/sizeof(struct vidsize))

enum pixenum{	// keep in sync with pixname !
	CLUT4=0,
	CLUT8,
	RGB565,
	ARGB1555,
	ARGB
};
const char *pixname[] = {
	"CLUT4",
	"CLUT8",
	"RGB565",
	"ARGB1555",
	"ARGB"
};

struct pixelformat{
	char *name;
	struct fb_bitfield red;
	struct fb_bitfield green;
	struct fb_bitfield blue;
	struct fb_bitfield transp;
	char bpp;
	char pixenum;
};

static		// so far these are all modes supported by the eNX (only partially by GTX)
const struct pixelformat pixelformattable[] = {
	{ .name = "CLUT4 ARGB8888", 	// CLUT4 (ARGB8888)
		.bpp = 4, .pixenum = CLUT4,
		.red = 	 { .offset = 0, .length=8, .msb_right =0 },
		.green = { .offset = 0, .length=8, .msb_right =0 },
		.blue =  { .offset = 0, .length=8, .msb_right =0 },
		.transp=  { .offset = 0, .length=8, .msb_right =0 }
	},
	{ .name = "CLUT4 ARGB1555", 	// CLUT4 (ARGB1555)
		.bpp = 4, .pixenum = CLUT4,
		.red = 	 { .offset = 0, .length=5, .msb_right =0 },
		.green = { .offset = 0, .length=5, .msb_right =0 },
		.blue =  { .offset = 0, .length=5, .msb_right =0 },
		.transp=  { .offset = 0, .length=1, .msb_right =0 }
	},
	{ .name = "CLUT8 ARGB8888",	// CLUT8 (ARGB8888)
		.bpp = 8, .pixenum = CLUT8,
		.red = 	 { .offset = 0, .length=8, .msb_right =0 },
		.green = { .offset = 0, .length=8, .msb_right =0 },
		.blue =  { .offset = 0, .length=8, .msb_right =0 },
		.transp=  { .offset = 0, .length=8, .msb_right =0 }
	},
	{ .name = "CLUT8 ARGB1555",	// CLUT8 (ARGB1555)
		.bpp = 8, .pixenum = CLUT8,
		.red = 	 { .offset = 0, .length=5, .msb_right =0 },
		.green = { .offset = 0, .length=5, .msb_right =0 },
		.blue =  { .offset = 0, .length=5, .msb_right =0 },
		.transp=  { .offset = 0, .length=1, .msb_right =0 }
	},
	{ .name = "ARGB1555", 	// ARGB1555
		.bpp = 16, .pixenum = ARGB1555,
		.red = 	 { .offset = 10, .length=5, .msb_right =0 },
		.green = { .offset = 5,  .length=5, .msb_right =0 },
		.blue =  { .offset = 0,  .length=5, .msb_right =0 },
		.transp=  { .offset = 15, .length=1, .msb_right =0 }
	},
	{ .name = "RGB565", 		// RGB565
		.bpp = 16, .pixenum = RGB565,
		.red = 	 { .offset = 11, .length=5, .msb_right =0 },
		.green = { .offset = 5,  .length=6, .msb_right =0 },
		.blue =  { .offset = 0,  .length=5, .msb_right =0 },
		.transp=  { .offset = 0,  .length=0, .msb_right =0 }
	},
	{ .name = "ARGB",	// 32 f*cking bits, the real McCoy :)
		.bpp = 32, .pixenum = ARGB,
		.red = 	 { .offset = 16, .length=8, .msb_right =0 },
		.green = { .offset = 8,  .length=8, .msb_right =0 },
		.blue =  { .offset = 0,  .length=8, .msb_right =0 },
		.transp=  { .offset = 24, .length=8, .msb_right =0 }
	}
};
#define PIXELFORMATNUM (sizeof(pixelformattable)/sizeof(struct pixelformat))

struct colour {
	__u16 r;
	__u16 g;
	__u16 b;
	__u16 a;
};
static
struct colour colourtable[] = {
	{.r =0xffff, .g = 0xffff, .b=0xffff, .a=0xffff},	// fully transparent white
	{.r =0xffff, .g = 0x0000, .b=0x0000, .a=0x0000},	// red
	{.r =0x0000, .g = 0xffff, .b=0x0000, .a=0x0000},	// green
	{.r =0x0000, .g = 0x0000, .b=0xffff, .a=0x0000},	// blue
	{.r =0x0000, .g = 0x0000, .b=0x0000, .a=0x0000}		// black
};
#define COLOURNUM (sizeof(colourtable)/sizeof(struct colour))

struct rect{
	int x;
	int y;
	int width;
	int height;
	const struct colour *col;
};
struct pixel{		// up to 32 bits of pixel information
	char byte[4];
};

void col2pixel (struct pixel *pix, const struct pixelformat *pixf, const struct colour *col){
	switch (pixf->pixenum){
		case RGB565:
			pix->byte[0]=(col->r&0xf8)|(col->g&0xfc)>>5;
			pix->byte[1]=(col->g&0xfc)<<3|(col->b&0xf8)>>3;
			break;
		case ARGB1555:
			pix->byte[0]=(col->a&0x80)|(col->r&0xf8)>>1|(col->g&0xf8)>>6;
			pix->byte[1]=(col->g&0xf8)<<2|(col->b&0xf8)>>3;
			break;
		case ARGB:
			pix->byte[0]=col->a;
			pix->byte[1]=col->r;
			pix->byte[2]=col->g;
			pix->byte[3]=col->b;
			break;
		default:
			printf ("unknown pixelformat\n");
			exit(1);
	}
}

int setmode(int fbd, const struct pixelformat *pixf,const struct vidsize *vids){
	struct fb_var_screeninfo var;
	int stat;
	stat = ioctl (fbd, FBIOGET_VSCREENINFO,&var);
	if (stat<0) return -2;
	
	var.xres= vids->width;
	var.xres_virtual = vids->width;
	var.yres= vids->height;
	var.yres_virtual = vids->height;
	
	var.bits_per_pixel = pixf->bpp;
	var.red = pixf->red;
	var.green = pixf->green;
	var.blue = pixf->blue;
	var.transp = pixf->transp;

	stat = ioctl (fbd, FBIOPUT_VSCREENINFO,&var);
	if (stat<0) return -1;
	return 0;
}

// unefficient implementation, do NOT use it for your next ego shooter, please :)
// for 4-Bit only rectangles with even width are supported
// CLUT-modes use value of red component as index
void drawrect(void *videoram, struct rect *r, const struct pixelformat *pixf, const struct vidsize *vids){
	int x,y,corwidth, bpp = 0, tocopy = 1;
	struct pixel pix;
	unsigned char *pmem = videoram;
	corwidth = r->width;	// actually only "corrected" for 4 Bit

	if (pixf->pixenum!=CLUT4&&pixf->pixenum!=CLUT8){
		switch (pixf->pixenum){
			case ARGB1555:
			case RGB565:
				bpp = 16;
				tocopy = 2;
				break;
			case ARGB:
				bpp = 32;
				tocopy = 4;
				break;
			default:
				printf ("drawrect: unknown pixelformat(%d) bpp:%d\n",pixf->pixenum,pixf->bpp);
				exit(1);
		}
		col2pixel(&pix,pixf,r->col);
	} else {
		switch (pixf->pixenum){	// CLUT = Colour LookUp Table (palette)
			case CLUT4:	// take red value as index in this case
				pix.byte[0]=(r->col->r)<<4|(r->col->r&0xf);	// slightly cryptic... "rect->colour->red"
				corwidth>>=1;	// we copy bytes
				bpp=4;
				tocopy=1;
				break;
			case CLUT8:
				pix.byte[0]=(r->col->r&0xff);
				bpp=8;
				tocopy=1;
				break;
		}
	}
	pmem=videoram+((((r->y*vids->width)+r->x)*bpp)>>3);
	for (y=0;y<r->height;y++){
		int offset = 0;
		for (x=0;x<corwidth;x++){
			memcpy (pmem+offset,pix.byte,tocopy);
			offset+=tocopy;
		}
		pmem +=((vids->width*bpp)>>3);	// skip one whole line, actually should be taken from "fix-info"
	}
}
			
// create quick little test image, 4 colours from table
void draw4field(void *videoram, const struct pixelformat *pixf, const struct vidsize *vids){
	struct rect r;
	struct colour c;
	int height, width;
	c.r = 1;	// only used for the indexed modes, r is taken as index
	height = vids->height;
	width = vids->width;

	r.height = height>>1;
	r.width = width>>1;
	r.x = 0;	r.y = 0;
	if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) r.col = &c;
	else r.col = &colourtable[1];
	drawrect (videoram, &r, pixf, vids);

	r.x = width/2;	r.y = 0;
	if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) c.r = 2;
	else r.col = &colourtable[2];
	drawrect (videoram, &r, pixf, vids);

	r.x = 0;	r.y = height/2;
	if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) c.r = 3;
	else r.col = &colourtable[3];
	drawrect (videoram, &r, pixf, vids);

	r.x = width/2;	r.y = height/2;
	if (pixf->pixenum==CLUT4||pixf->pixenum==CLUT8) c.r = 0;
	else r.col = &colourtable[0];
	drawrect (videoram, &r, pixf, vids);
}

void usage(char *name){
 	printf ("Usage: %s [options]\n"
		"Options: -f<pixelformat>\n"
		"            where format is one of:\n"
		"              CLUT4,CLUT8,ARGB1555,RGB565,ARGB\n"
		"         -s<width>x<heigth>\n"
		"            where width is either 720,640,360,320\n"
		"                  and height is either 288,240,480,576\n"
		"         -n\n"
		"            disables clearing the framebuffer after drawing\n"
		"            the testimage. This can be useful to keep the last\n"
		"            drawn image onscreen.\n"
		"\nExample: %s -fRGB322\n",name,name);
	exit(0);
}

int main (int argc,char **argv){
	struct fb_fix_screeninfo fix;
	struct fb_var_screeninfo var;
	struct fb_cmap cmap;
	struct rect r;
	int fbd;
	unsigned char *pfb;
	int stat;
	int optchar,fmode=-1,smode=-1,clear=1;
	int i_cmap,i_size,i_pix;
	extern char *optarg;
	
	if (argc!=0&&argc>4) usage(argv[0]);
	while ( (optchar = getopt (argc,argv,"f:s:n"))!= -1){
		int i,height,width;
		switch (optchar){
			case 'f':
				for (i=0;i<(sizeof(pixname)/sizeof(char*));i++){
					if (!strncmp (optarg,pixname[i],strlen(pixname[i]))){
						fmode=i;
						printf ("displaying only %s-modes\n",pixname[i]);
						break;
					}
				}
				if (fmode==-1){
					printf ("unknown pixelformat\n");
					exit(0);
				}
				break;
			case 's':
				if (sscanf (optarg,"%dx%d",&width,&height)!=2){
					printf ("parsing size failed\n");
					exit(0);
				} else {
					printf ("requested size %dx%d\n",width,height);
					for (i=0;i<VIDSIZENUM;i++){
						if (vidsizetable[i].width == width &&
							vidsizetable[i].height == height){
							smode = i;
							break;
						}
					}
					if (smode==-1){
						printf ("this size is not supported\n");
						exit(0);
					}
				}
				break;
			case 'n':
				clear = 0;
				printf ("clearing framebuffer after drawing is disabled\n");
				break;
			case '?':
				usage (argv[0]);
		}
	}
	
	fbd = open (FBDEV, O_RDWR);
	if (fbd<0){
		perror ("Error opening framebuffer device");
		return 1;
	}
	stat = ioctl (fbd, FBIOGET_FSCREENINFO,&fix);
	if (stat<0){
		perror ("Error getting fix screeninfo");
		return 1;
	}
	stat = ioctl (fbd, FBIOGET_VSCREENINFO,&var);
	if (stat<0){
		perror ("Error getting var screeninfo");
		return 1;
	}
	stat = ioctl (fbd, FBIOPUT_VSCREENINFO,&var);
	if (stat<0){
		perror ("Error setting mode");
		return 1;
	}
	pfb = mmap (0, fix.smem_len, PROT_READ|PROT_WRITE, MAP_SHARED, fbd, 0);
	if (pfb == MAP_FAILED){
		perror ("Error mmap'ing framebuffer device");
		return 1;
	}

	// iterate over all modes
	for (i_pix=0;i_pix<PIXELFORMATNUM;i_pix++){
		if (fmode!=-1 && pixelformattable[i_pix].pixenum != fmode) continue;
		printf ("testing: %s",pixelformattable[i_pix].name);
		printf (" for sizes: \n");
		for (i_size=0;i_size<VIDSIZENUM;i_size++){
			if (smode!=-1 && i_size!=smode) continue;
			printf ("%dx%d ",vidsizetable[i_size].width,vidsizetable[i_size].height);
			fflush(stdout);
			if ((i_size%4)==3) printf ("\n");
			
			// try to set mode
			stat = setmode(fbd,&pixelformattable[i_pix],&vidsizetable[i_size]);
			if (stat==-2) perror ("fbtest: could not get fb_var-screeninfo from fb-device");
			else if (stat==-1){
				printf ("\nCould not set mode %s (%dx%d), possible reasons:\n"
					"- you have a GTX (soz m8)\n"
					"- your configuration does not have enough graphics RAM\n"
					"- you found a bug\n"
					"choose your poison accordingly...\n",
					pixelformattable[i_pix].name,vidsizetable[i_size].width,vidsizetable[i_size].height);
					continue;
			}
			// fill cmap;
			cmap.len = 1;
			if ((pixelformattable[i_pix].bpp==4)||
				((pixelformattable[i_pix].bpp==8)&&(pixelformattable[i_pix].red.length!=3))){
				for (i_cmap=0;i_cmap<COLOURNUM;i_cmap++){
					cmap.start=i_cmap;
					cmap.red=&colourtable[i_cmap].r;
					cmap.green=&colourtable[i_cmap].g;
					cmap.blue=&colourtable[i_cmap].b;
					cmap.transp=&colourtable[i_cmap].a;
					stat = ioctl (fbd, FBIOPUTCMAP, &cmap);
					if (stat<0) printf ("setting colourmap failed\n");
				}
			}
			// create the test image
			draw4field(pfb,&pixelformattable[i_pix],&vidsizetable[i_size]);
			usleep (500000);
			// clear screen
			if (clear){
				r.x=r.y=0;r.width = vidsizetable[i_size].width; r.height = vidsizetable[i_size].height;
				r.col = &colourtable[4];
				drawrect(pfb,&r,&pixelformattable[i_pix],&vidsizetable[i_size]);
			}
		}
		printf ("\n");
	}

	stat = munmap (pfb,fix.smem_len);
	if (stat<0){
		perror ("Error munmap'ing framebuffer device");
		return 1;
	}
	close (fbd);
	return 0;
}