source: mainline/fb/fb.c@ 8c6b45f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8c6b45f was a2cd194, checked in by Ondrej Palkovsky <ondrap@…>, 19 years ago

Added some cleanups.

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