Subversion Repositories HelenOS-historic

Rev

Rev 1430 | Rev 1447 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1363 vana 1
/*
2
 * Copyright (C) 2006 Jakub Vana
1404 palkovsky 3
 * Copyright (C) 2006 Ondrej Palkovsky
1363 vana 4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
30
#include <stdio.h>
1430 jermar 31
#include <stdlib.h>
32
#include <unistd.h>
33
#include <string.h>
1363 vana 34
#include <ddi.h>
35
#include <task.h>
36
#include <sysinfo.h>
37
#include <align.h>
38
#include <as.h>
39
#include <ipc/fb.h>
40
#include <ipc/ipc.h>
1430 jermar 41
#include <ipc/ns.h>
1363 vana 42
#include <ipc/services.h>
43
#include <kernel/errno.h>
1392 palkovsky 44
#include <async.h>
1404 palkovsky 45
#include "font-8x16.h"
46
#include "helenos.xbm"
1363 vana 47
#include "fb.h"
48
 
49
#define EFB (-1)
50
 
51
#define DEFAULT_BGCOLOR		0x000080
52
#define DEFAULT_FGCOLOR		0xffff00
53
#define DEFAULT_LOGOCOLOR	0x0000ff
54
 
55
#define MAIN_BGCOLOR		0x404000
56
#define MAIN_FGCOLOR		0x000000
57
#define MAIN_LOGOCOLOR	0x404000
58
 
59
#define SPACING (2)
60
 
61
#define H_NO_VFBS 3
62
#define V_NO_VFBS 3
63
 
64
static void fb_putchar(int item,char ch);
65
int create_window(int item,unsigned int x, unsigned int y,unsigned int x_size, unsigned int y_size,
66
	unsigned int BGCOLOR,unsigned int FGCOLOR,unsigned int LOGOCOLOR);
67
void fb_init(int item,__address addr, unsigned int x, unsigned int y, unsigned int bpp, unsigned int scan,
68
	unsigned int BGCOLOR,unsigned int FGCOLOR,unsigned int LOGOCOLOR);
69
 
70
unsigned int mod_col(unsigned int col,int mod);
71
 
1392 palkovsky 72
static int init_fb(void)
1363 vana 73
{
74
	__address fb_ph_addr;
75
	unsigned int fb_width;
76
	unsigned int fb_height;
77
	unsigned int fb_bpp;
78
	unsigned int fb_scanline;
79
	__address fb_addr;
80
	int a=0;
1392 palkovsky 81
	int i,j,k;
82
	int w;
83
	char text[]="HelenOS Framebuffer driver\non Virtual Framebuffer\nVFB ";
1363 vana 84
 
85
	fb_ph_addr=sysinfo_value("fb.address.physical");
86
	fb_width=sysinfo_value("fb.width");
87
	fb_height=sysinfo_value("fb.height");
88
	fb_bpp=sysinfo_value("fb.bpp");
89
	fb_scanline=sysinfo_value("fb.scanline");
90
 
91
	fb_addr=ALIGN_UP(((__address)set_maxheapsize(USER_ADDRESS_SPACE_SIZE_ARCH>>1)),PAGE_SIZE);
1392 palkovsky 92
 
1363 vana 93
	map_physmem(task_get_id(),(void *)((__address)fb_ph_addr),(void *)fb_addr,
1430 jermar 94
		    (fb_scanline*fb_height+PAGE_SIZE-1)>>PAGE_WIDTH,
95
		    AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE);
1363 vana 96
 
97
	fb_init(0,fb_addr, fb_width, fb_height, fb_bpp, fb_scanline,
98
		MAIN_BGCOLOR,MAIN_FGCOLOR,MAIN_LOGOCOLOR);
99
 
100
	fb_putchar(0,'\n');
101
	fb_putchar(0,' ');
102
 
1392 palkovsky 103
	for(i=0;i<H_NO_VFBS;i++)
104
		for(j=0;j<V_NO_VFBS;j++) {
105
			w = create_window(0,(fb_width/H_NO_VFBS)*i+SPACING,
106
					  (fb_height/V_NO_VFBS)*j+SPACING,(fb_width/H_NO_VFBS)-2*SPACING ,
107
					  (fb_height/V_NO_VFBS)-2*SPACING,mod_col(DEFAULT_BGCOLOR,/*i+j*H_NO_VFBS*/0),
108
					  mod_col(DEFAULT_FGCOLOR,/*i+j*H_NO_VFBS*/0),
109
					  mod_col(DEFAULT_LOGOCOLOR,/*i+j*H_NO_VFBS)*/0));
110
 
111
			if( w== EFB)
112
				return -1;
113
 
114
			for(k=0;text[k];k++) 
115
				fb_putchar(w,text[k]);
116
			fb_putchar(w,w+'0');
117
			fb_putchar(w,'\n');
118
		}
119
	return 0;
120
}
1363 vana 121
 
1392 palkovsky 122
int vfb_no = 1;
123
void client_connection(ipc_callid_t iid, ipc_call_t *icall)
124
{
125
	ipc_callid_t callid;
126
	ipc_call_t call;
127
	int vfb = vfb_no++;
1363 vana 128
 
1392 palkovsky 129
	if (vfb > 9) {
130
		ipc_answer_fast(iid, ELIMIT, 0,0);
131
		return;
132
	}
133
	ipc_answer_fast(iid, 0, 0, 0);
1363 vana 134
 
1392 palkovsky 135
	while (1) {
136
		callid = async_get_call(&call);
137
		switch (IPC_GET_METHOD(call)) {
138
		case IPC_M_PHONE_HUNGUP:
139
			ipc_answer_fast(callid,0,0,0);
140
			return; /* Exit thread */
1363 vana 141
 
1392 palkovsky 142
		case FB_PUTCHAR:
143
			ipc_answer_fast(callid,0,0,0);
144
			fb_putchar(vfb,IPC_GET_ARG2(call));
145
			break;
146
		default:
147
			ipc_answer_fast(callid,ENOENT,0,0);
148
		}
1363 vana 149
	}
1392 palkovsky 150
}
1363 vana 151
 
1392 palkovsky 152
int main(int argc, char *argv[])
153
{
1363 vana 154
	ipc_call_t call;
155
	ipc_callid_t callid;
156
	char connected = 0;
157
	int res;
158
	int c;
159
	ipcarg_t phonead;
160
 
161
	ipcarg_t retval, arg1, arg2;
162
 
1445 cejka 163
	if(!sysinfo_value("fb")) {
164
		printf("Framebuffer initialization failed.\n");
165
		return -1;
166
	}
1363 vana 167
 
168
	if ((res = ipc_connect_to_me(PHONE_NS, SERVICE_VIDEO, 0, &phonead)) != 0) 
169
		return -1;
1392 palkovsky 170
 
1445 cejka 171
	if (init_fb() != 0) {
172
		printf("Framebuffer initialization failed.\n");
173
		return -1;
174
	}
1363 vana 175
 
1392 palkovsky 176
	async_manager();
177
	/* Never reached */
1363 vana 178
	return 0;
179
}
180
 
181
 
182
#define GRAPHICS_ITEMS 1024
183
 
184
 
185
/***************************************************************/
186
/* Pixel specific fuctions */
187
 
188
typedef void (*putpixel_fn_t)(int item,unsigned int x, unsigned int y, int color);
189
typedef int (*getpixel_fn_t)(int item,unsigned int x, unsigned int y);
190
 
191
typedef struct framebuffer_descriptor
192
{
193
	__u8 *fbaddress ;
194
 
195
	unsigned int xres ;
196
	unsigned int yres ;
197
	unsigned int scanline ;
198
	unsigned int pixelbytes ;
199
 
200
	unsigned int position ;
201
	unsigned int columns ;
202
	unsigned int rows ;
203
 
204
	unsigned int BGCOLOR;
205
	unsigned int FGCOLOR;
206
	unsigned int LOGOCOLOR;
207
 
208
	putpixel_fn_t putpixel;
209
	getpixel_fn_t getpixel;
210
 
211
}framebuffer_descriptor_t;
212
 
213
void * graphics_items[GRAPHICS_ITEMS+1]={NULL};
214
 
215
#define FB(__a__,__b__) (((framebuffer_descriptor_t*)(graphics_items[__a__]))->__b__)
216
 
217
#define COL_WIDTH	8
218
#define ROW_BYTES	(FB(item,scanline) * FONT_SCANLINES)
219
#define RED(x, bits)	((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
220
#define GREEN(x, bits)	((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
221
#define BLUE(x, bits)	((x >> (8 - bits)) & ((1 << bits) - 1))
222
 
223
#define POINTPOS(x, y)	(y * FB(item,scanline) + x * FB(item,pixelbytes))
224
 
225
 
226
 
227
 
228
/** Put pixel - 24-bit depth, 1 free byte */
229
static void putpixel_4byte(int item,unsigned int x, unsigned int y, int color)
230
{
231
	*((__u32 *)(FB(item,fbaddress) + POINTPOS(x, y))) = color;
232
}
233
 
234
/** Return pixel color - 24-bit depth, 1 free byte */
235
static int getpixel_4byte(int item,unsigned int x, unsigned int y)
236
{
237
	return *((__u32 *)(FB(item,fbaddress) + POINTPOS(x, y))) & 0xffffff;
238
}
239
 
240
/** Put pixel - 24-bit depth */
241
static void putpixel_3byte(int item,unsigned int x, unsigned int y, int color)
242
{
243
	unsigned int startbyte = POINTPOS(x, y);
244
 
245
#if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
246
	FB(item,fbaddress)[startbyte] = RED(color, 8);
247
	FB(item,fbaddress)[startbyte + 1] = GREEN(color, 8);
248
	FB(item,fbaddress)[startbyte + 2] = BLUE(color, 8);
249
#else
250
	FB(item,fbaddress)[startbyte + 2] = RED(color, 8);
251
	FB(item,fbaddress)[startbyte + 1] = GREEN(color, 8);
252
	FB(item,fbaddress)[startbyte + 0] = BLUE(color, 8);
253
#endif
254
 
255
}
256
 
257
/** Return pixel color - 24-bit depth */
258
static int getpixel_3byte(int item,unsigned int x, unsigned int y)
259
{
260
	unsigned int startbyte = POINTPOS(x, y);
261
 
262
 
263
 
264
#if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
265
	return FB(item,fbaddress)[startbyte] << 16 | FB(item,fbaddress)[startbyte + 1] << 8 | FB(item,fbaddress)[startbyte + 2];
266
#else
267
	return FB(item,fbaddress)[startbyte + 2] << 16 | FB(item,fbaddress)[startbyte + 1] << 8 | FB(item,fbaddress)[startbyte + 0];
268
#endif
269
 
270
 
271
}
272
 
273
/** Put pixel - 16-bit depth (5:6:5) */
274
static void putpixel_2byte(int item,unsigned int x, unsigned int y, int color)
275
{
276
	/* 5-bit, 6-bits, 5-bits */ 
277
	*((__u16 *)(FB(item,fbaddress) + POINTPOS(x, y))) = RED(color, 5) << 11 | GREEN(color, 6) << 5 | BLUE(color, 5);
278
}
279
 
280
/** Return pixel color - 16-bit depth (5:6:5) */
281
static int getpixel_2byte(int item,unsigned int x, unsigned int y)
282
{
283
	int color = *((__u16 *)(FB(item,fbaddress) + POINTPOS(x, y)));
284
	return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
285
}
286
 
287
/** Put pixel - 8-bit depth (3:2:3) */
288
static void putpixel_1byte(int item,unsigned int x, unsigned int y, int color)
289
{
290
	FB(item,fbaddress)[POINTPOS(x, y)] = RED(color, 3) << 5 | GREEN(color, 2) << 3 | BLUE(color, 3);
291
}
292
 
293
/** Return pixel color - 8-bit depth (3:2:3) */
294
static int getpixel_1byte(int item,unsigned int x, unsigned int y)
295
{
296
	int color = FB(item,fbaddress)[POINTPOS(x, y)];
297
	return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
298
}
299
 
300
/** Fill line with color BGCOLOR */
301
static void clear_line(int item,unsigned int y)
302
{
303
	unsigned int x;
304
	for (x = 0; x < FB(item,xres); x++)
305
		FB(item,putpixel)(item,x, y, FB(item,BGCOLOR));
306
}
307
 
308
 
309
/** Fill screen with background color */
310
static void clear_screen(int item)
311
{
312
	unsigned int y;
313
	for (y = 0; y < FB(item,yres); y++)
314
	{
1392 palkovsky 315
		clear_line(item,y);
1363 vana 316
	}	
317
}
318
 
319
 
320
/** Scroll screen one row up */
321
static void scroll_screen(int item)
322
{
323
	unsigned int i;
324
	unsigned int j;
325
 
326
	for(j=0;j<FB(item,yres)-FONT_SCANLINES;j++)
327
		memcpy((void *) FB(item,fbaddress)+j*FB(item,scanline), 
328
			(void *) &FB(item,fbaddress)[ROW_BYTES+j*FB(item,scanline)], FB(item,pixelbytes)*FB(item,xres));
329
 
330
	//memcpy((void *) FB(item,fbaddress), (void *) &FB(item,fbaddress)[ROW_BYTES], FB(item,scanline) * FB(item,yres) - ROW_BYTES);
331
 
332
	/* Clear last row */
333
	for (i = 0; i < FONT_SCANLINES; i++)
334
		clear_line(item,(FB(item,rows) - 1) * FONT_SCANLINES + i);
335
}
336
 
337
 
338
static void invert_pixel(int item,unsigned int x, unsigned int y)
339
{
340
	FB(item,putpixel)(item, x, y, ~FB(item,getpixel)(item, x, y));
341
}
342
 
343
 
344
/** Draw one line of glyph at a given position */
345
static void draw_glyph_line(int item,unsigned int glline, unsigned int x, unsigned int y)
346
{
347
	unsigned int i;
348
 
349
	for (i = 0; i < 8; i++)
350
		if (glline & (1 << (7 - i))) {
351
			FB(item,putpixel)(item,x + i, y, FB(item,FGCOLOR));
352
		} else
353
			FB(item,putpixel)(item,x + i, y, FB(item,BGCOLOR));
354
}
355
 
356
/***************************************************************/
357
/* Character-console functions */
358
 
359
/** Draw character at given position */
360
static void draw_glyph(int item,__u8 glyph, unsigned int col, unsigned int row)
361
{
362
	unsigned int y;
363
 
364
	for (y = 0; y < FONT_SCANLINES; y++)
365
		draw_glyph_line(item ,fb_font[glyph * FONT_SCANLINES + y], col * COL_WIDTH, row * FONT_SCANLINES + y);
366
}
367
 
368
/** Invert character at given position */
369
static void invert_char(int item,unsigned int col, unsigned int row)
370
{
371
	unsigned int x;
372
	unsigned int y;
373
 
374
	for (x = 0; x < COL_WIDTH; x++)
375
		for (y = 0; y < FONT_SCANLINES; y++)
376
			invert_pixel(item,col * COL_WIDTH + x, row * FONT_SCANLINES + y);
377
}
378
 
379
/** Draw character at default position */
380
static void draw_char(int item,char chr)
381
{
382
	draw_glyph(item ,chr, FB(item,position) % FB(item,columns), FB(item,position) / FB(item,columns));
383
}
384
 
385
static void draw_logo(int item,unsigned int startx, unsigned int starty)
386
{
387
	unsigned int x;
388
	unsigned int y;
389
	unsigned int byte;
390
	unsigned int rowbytes;
391
 
392
	rowbytes = (helenos_width - 1) / 8 + 1;
393
 
394
	for (y = 0; y < helenos_height; y++)
395
		for (x = 0; x < helenos_width; x++) {
396
			byte = helenos_bits[rowbytes * y + x / 8];
397
			byte >>= x % 8;
398
			if (byte & 1)
399
				FB(item,putpixel)(item,startx + x, starty + y, FB(item,LOGOCOLOR));
400
		}
401
}
402
 
403
/***************************************************************/
404
/* Stdout specific functions */
405
 
406
static void invert_cursor(int item)
407
{
408
	invert_char(item,FB(item,position) % FB(item,columns), FB(item,position) / FB(item,columns));
409
}
410
 
411
/** Print character to screen
412
 *
413
 *  Emulate basic terminal commands
414
 */
415
static void fb_putchar(int item,char ch)
416
{
417
 
418
	switch (ch) {
419
		case '\n':
420
			invert_cursor(item);
421
			FB(item,position) += FB(item,columns);
422
			FB(item,position) -= FB(item,position) % FB(item,columns);
423
			break;
424
		case '\r':
425
			invert_cursor(item);
426
			FB(item,position) -= FB(item,position) % FB(item,columns);
427
			break;
428
		case '\b':
429
			invert_cursor(item);
430
			if (FB(item,position) % FB(item,columns))
431
				FB(item,position)--;
432
			break;
433
		case '\t':
434
			invert_cursor(item);
435
			do {
436
				draw_char(item,' ');
437
				FB(item,position)++;
438
			} while (FB(item,position) % 8);
439
			break;
440
		default:
441
			draw_char(item,ch);
442
			FB(item,position)++;
443
	}
444
 
445
	if (FB(item,position) >= FB(item,columns) * FB(item,rows)) {
446
		FB(item,position) -= FB(item,columns);
447
		scroll_screen(item);
448
	}
449
 
450
	invert_cursor(item);
451
 
452
}
453
 
454
 
455
/** Initialize framebuffer as a chardev output device
456
 *
457
 * @param addr Address of theframebuffer
458
 * @param x    Screen width in pixels
459
 * @param y    Screen height in pixels
460
 * @param bpp  Bits per pixel (8, 16, 24, 32)
461
 * @param scan Bytes per one scanline
462
 *
463
 */
464
void fb_init(int item,__address addr, unsigned int x, unsigned int y, unsigned int bpp, unsigned int scan,
465
	unsigned int BGCOLOR,unsigned int FGCOLOR,unsigned int LOGOCOLOR)
466
{
467
 
468
	if( (graphics_items[item]=malloc(sizeof(framebuffer_descriptor_t))) ==NULL) 
469
	{
470
		return;
471
	}
472
 
473
	switch (bpp) {
474
		case 8:
475
			FB(item,putpixel) = putpixel_1byte;
476
			FB(item,getpixel) = getpixel_1byte;
477
			FB(item,pixelbytes) = 1;
478
			break;
479
		case 16:
480
			FB(item,putpixel) = putpixel_2byte;
481
			FB(item,getpixel) = getpixel_2byte;
482
			FB(item,pixelbytes) = 2;
483
			break;
484
		case 24:
485
			FB(item,putpixel) = putpixel_3byte;
486
			FB(item,getpixel) = getpixel_3byte;
487
			FB(item,pixelbytes) = 3;
488
			break;
489
		case 32:
490
			FB(item,putpixel) = putpixel_4byte;
491
			FB(item,getpixel) = getpixel_4byte;
492
			FB(item,pixelbytes) = 4;
493
			break;
494
	}
495
 
496
 
1392 palkovsky 497
	FB(item,fbaddress) = (unsigned char *) addr;
498
	FB(item,xres) = x;
499
	FB(item,yres) = y;
500
	FB(item,scanline) = scan;
1363 vana 501
 
502
 
1392 palkovsky 503
	FB(item,rows) = y / FONT_SCANLINES;
504
	FB(item,columns) = x / COL_WIDTH;
1363 vana 505
 
506
	FB(item,BGCOLOR)=BGCOLOR;
507
	FB(item,FGCOLOR)=FGCOLOR;
508
	FB(item,LOGOCOLOR)=LOGOCOLOR;
509
 
510
 
1392 palkovsky 511
	clear_screen(item);
512
	draw_logo(item,FB(item,xres) - helenos_width, 0);
513
	invert_cursor(item);
1363 vana 514
 
515
}
516
 
517
 
518
static int get_free_item()
519
{
520
	int item;
521
	for(item=0;graphics_items[item]!=NULL;item++);
522
	return (item==GRAPHICS_ITEMS)?EFB:item;
523
}
524
 
525
unsigned int mod_col(unsigned int col,int mod)
526
{
527
	if(mod & 1) col^=0xff;
528
	if(mod & 2) col^=0xff00;
529
	if(mod & 4) col^=0xff0000;
530
	return col;
531
}
532
 
533
 
534
int create_window(int item,unsigned int x, unsigned int y,unsigned int x_size, unsigned int y_size,
535
	unsigned int BGCOLOR,unsigned int FGCOLOR,unsigned int LOGOCOLOR)
536
{
537
	int item_new;
538
 
539
	if(EFB==(item_new=get_free_item()))return EFB;
540
 
541
 
542
	if( (graphics_items[item_new]=malloc(sizeof(framebuffer_descriptor_t))) ==NULL) 
543
	{
544
		return EFB;
545
	}
546
 
547
 
548
 
549
	FB(item_new,putpixel) = FB(item,putpixel);
550
	FB(item_new,getpixel) = FB(item,getpixel);
551
	FB(item_new,pixelbytes) = FB(item,pixelbytes);
552
 
553
	FB(item_new,fbaddress) = FB(item,fbaddress) + POINTPOS(x, y) ; 
554
	FB(item_new,xres) = x_size; 
555
	FB(item_new,yres) = y_size; 
556
	FB(item_new,scanline) =  FB(item,scanline);
557
 
558
 
559
	FB(item_new,rows) = y_size / FONT_SCANLINES; 
560
	FB(item_new,columns) = x_size / COL_WIDTH; 
561
 
562
	FB(item_new,BGCOLOR)=BGCOLOR;
563
	FB(item_new,FGCOLOR)=FGCOLOR;
564
	FB(item_new,LOGOCOLOR)=LOGOCOLOR;
565
 
566
 
567
	clear_screen(item_new); 
568
	draw_logo(item_new,FB(item_new,xres) - helenos_width, 0); 
569
	invert_cursor(item_new); 
570
 
571
	return item_new;
572
}
573
 
574
 
575