Subversion Repositories HelenOS-historic

Rev

Rev 1683 | Rev 1760 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2006 Ondrej Palkovsky
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. #include <genarch/fb/font-8x16.h>
  30. #include <genarch/fb/fb.h>
  31. #include <console/chardev.h>
  32. #include <console/console.h>
  33. #include <sysinfo/sysinfo.h>
  34. #include <mm/slab.h>
  35. #include <align.h>
  36. #include <panic.h>
  37. #include <memstr.h>
  38. #include <config.h>
  39. #include <bitops.h>
  40. #include <print.h>
  41.  
  42. #include "helenos.xbm"
  43.  
  44. SPINLOCK_INITIALIZE(fb_lock);
  45.  
  46. static __u8 *fbaddress = NULL;
  47.  
  48. static __u8 *blankline = NULL;
  49. static __u8 *dbbuffer = NULL; /* Buffer for fast scrolling console */
  50. static int dboffset;
  51.  
  52. static unsigned int xres = 0;
  53. static unsigned int yres = 0;
  54. static unsigned int scanline = 0;
  55. static unsigned int bitspp = 0;
  56. static unsigned int pixelbytes = 0;
  57.  
  58. static unsigned int position = 0;
  59. static unsigned int columns = 0;
  60. static unsigned int rows = 0;
  61.  
  62.  
  63. #define COL_WIDTH   8
  64. #define ROW_BYTES   (scanline * FONT_SCANLINES)
  65.  
  66. #define BGCOLOR     0x000080
  67. #define FGCOLOR     0xffff00
  68. #define LOGOCOLOR   0x2020b0
  69.  
  70. #define RED(x, bits)    ((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
  71. #define GREEN(x, bits)  ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
  72. #define BLUE(x, bits)   ((x >> (8 - bits)) & ((1 << bits) - 1))
  73.  
  74. #define POINTPOS(x, y)  ((y) * scanline + (x) * pixelbytes)
  75.  
  76. /***************************************************************/
  77. /* Pixel specific fuctions */
  78.  
  79. static void (*rgb2scr)(void *, int);
  80. static int (*scr2rgb)(void *);
  81.  
  82. /* Conversion routines between different color representations */
  83. static void rgb_4byte(void *dst, int rgb)
  84. {
  85.     *(int *)dst = rgb;
  86. }
  87.  
  88. static int byte4_rgb(void *src)
  89. {
  90.     return (*(int *)src) & 0xffffff;
  91. }
  92.  
  93. static void rgb_3byte(void *dst, int rgb)
  94. {
  95.     __u8 *scr = dst;
  96. #if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
  97.     scr[0] = RED(rgb, 8);
  98.     scr[1] = GREEN(rgb, 8);
  99.     scr[2] = BLUE(rgb, 8);
  100. #else
  101.     scr[2] = RED(rgb, 8);
  102.     scr[1] = GREEN(rgb, 8);
  103.     scr[0] = BLUE(rgb, 8);
  104. #endif
  105. }
  106.  
  107. static int byte3_rgb(void *src)
  108. {
  109.     __u8 *scr = src;
  110. #if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
  111.     return scr[0] << 16 | scr[1] << 8 | scr[2];
  112. #else
  113.     return scr[2] << 16 | scr[1] << 8 | scr[0];
  114. #endif 
  115. }
  116.  
  117. /**  16-bit depth (5:6:5) */
  118. static void rgb_2byte(void *dst, int rgb)
  119. {
  120.     /* 5-bit, 6-bits, 5-bits */
  121.     *((__u16 *)(dst)) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5);
  122. }
  123.  
  124. /** 16-bit depth (5:6:5) */
  125. static int byte2_rgb(void *src)
  126. {
  127.     int color = *(__u16 *)(src);
  128.     return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
  129. }
  130.  
  131. /** Put pixel - 8-bit depth (3:2:3) */
  132. static void rgb_1byte(void *dst, int rgb)
  133. {
  134.     *(__u8 *)dst = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 | BLUE(rgb, 3);
  135. }
  136.  
  137. /** Return pixel color - 8-bit depth (3:2:3) */
  138. static int byte1_rgb(void *src)
  139. {
  140.     int color = *(__u8 *)src;
  141.     return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
  142. }
  143.  
  144. static void putpixel(unsigned int x, unsigned int y, int color)
  145. {
  146.     (*rgb2scr)(&fbaddress[POINTPOS(x,y)],color);
  147.  
  148.     if (dbbuffer) {
  149.         int dline = (y + dboffset) % yres;
  150.         (*rgb2scr)(&dbbuffer[POINTPOS(x,dline)],color);
  151.     }
  152. }
  153.  
  154. /** Get pixel from viewport */
  155. static int getpixel(unsigned int x, unsigned int y)
  156. {
  157.     if (dbbuffer) {
  158.         int dline = (y + dboffset) % yres;
  159.         return (*scr2rgb)(&dbbuffer[POINTPOS(x,dline)]);
  160.     }
  161.     return (*scr2rgb)(&fbaddress[POINTPOS(x,y)]);
  162. }
  163.  
  164.  
  165. /** Fill screen with background color */
  166. static void clear_screen(void)
  167. {
  168.     unsigned int y;
  169.  
  170.     for (y = 0; y < yres; y++) {
  171.         memcpy(&fbaddress[scanline*y], blankline, xres*pixelbytes);
  172.         if (dbbuffer)
  173.             memcpy(&dbbuffer[scanline*y], blankline, xres*pixelbytes);
  174.     }
  175. }
  176.  
  177.  
  178. /** Scroll screen one row up */
  179. static void scroll_screen(void)
  180. {
  181.     __u8 *lastline = &fbaddress[(rows - 1) * ROW_BYTES];
  182.     int firstsz;
  183.  
  184.     if (dbbuffer) {
  185.         memcpy(&dbbuffer[dboffset*scanline], blankline, FONT_SCANLINES*scanline);
  186.        
  187.         dboffset = (dboffset + FONT_SCANLINES) % yres;
  188.         firstsz = yres-dboffset;
  189.  
  190.         memcpy(fbaddress, &dbbuffer[scanline*dboffset], firstsz*scanline);
  191.         memcpy(&fbaddress[firstsz*scanline], dbbuffer, dboffset*scanline);
  192.     } else {
  193.         memcpy((void *) fbaddress, (void *) &fbaddress[ROW_BYTES], scanline * yres - ROW_BYTES);
  194.         /* Clear last row */
  195.         memcpy((void *) lastline, (void *) blankline, ROW_BYTES);
  196.     }
  197. }
  198.  
  199.  
  200. static void invert_pixel(unsigned int x, unsigned int y)
  201. {
  202.     putpixel(x, y, ~getpixel(x, y));
  203. }
  204.  
  205.  
  206. /** Draw one line of glyph at a given position */
  207. static void draw_glyph_line(unsigned int glline, unsigned int x, unsigned int y)
  208. {
  209.     unsigned int i;
  210.  
  211.     for (i = 0; i < 8; i++)
  212.         if (glline & (1 << (7 - i))) {
  213.             putpixel(x + i, y, FGCOLOR);
  214.         } else
  215.             putpixel(x + i, y, BGCOLOR);
  216. }
  217.  
  218. /***************************************************************/
  219. /* Character-console functions */
  220.  
  221. /** Draw character at given position */
  222. static void draw_glyph(__u8 glyph, unsigned int col, unsigned int row)
  223. {
  224.     unsigned int y;
  225.  
  226.     for (y = 0; y < FONT_SCANLINES; y++)
  227.         draw_glyph_line(fb_font[glyph * FONT_SCANLINES + y], col * COL_WIDTH, row * FONT_SCANLINES + y);
  228. }
  229.  
  230. /** Invert character at given position */
  231. static void invert_char(unsigned int col, unsigned int row)
  232. {
  233.     unsigned int x;
  234.     unsigned int y;
  235.  
  236.     for (x = 0; x < COL_WIDTH; x++)
  237.         for (y = 0; y < FONT_SCANLINES; y++)
  238.             invert_pixel(col * COL_WIDTH + x, row * FONT_SCANLINES + y);
  239. }
  240.  
  241. /** Draw character at default position */
  242. static void draw_char(char chr)
  243. {
  244.     draw_glyph(chr, position % columns, position / columns);
  245. }
  246.  
  247. static void draw_logo(unsigned int startx, unsigned int starty)
  248. {
  249.     unsigned int x;
  250.     unsigned int y;
  251.     unsigned int byte;
  252.     unsigned int rowbytes;
  253.  
  254.     rowbytes = (helenos_width - 1) / 8 + 1;
  255.  
  256.     for (y = 0; y < helenos_height; y++)
  257.         for (x = 0; x < helenos_width; x++) {
  258.             byte = helenos_bits[rowbytes * y + x / 8];
  259.             byte >>= x % 8;
  260.             if (byte & 1)
  261.                 putpixel(startx + x, starty + y, LOGOCOLOR);
  262.         }
  263. }
  264.  
  265. /***************************************************************/
  266. /* Stdout specific functions */
  267.  
  268. static void invert_cursor(void)
  269. {
  270.     invert_char(position % columns, position / columns);
  271. }
  272.  
  273. /** Print character to screen
  274.  *
  275.  *  Emulate basic terminal commands
  276.  */
  277. static void fb_putchar(chardev_t *dev, char ch)
  278. {
  279.     spinlock_lock(&fb_lock);
  280.    
  281.     switch (ch) {
  282.         case '\n':
  283.             invert_cursor();
  284.             position += columns;
  285.             position -= position % columns;
  286.             break;
  287.         case '\r':
  288.             invert_cursor();
  289.             position -= position % columns;
  290.             break;
  291.         case '\b':
  292.             invert_cursor();
  293.             if (position % columns)
  294.                 position--;
  295.             break;
  296.         case '\t':
  297.             invert_cursor();
  298.             do {
  299.                 draw_char(' ');
  300.                 position++;
  301.             } while ((position % 8) && position < columns * rows);
  302.             break;
  303.         default:
  304.             draw_char(ch);
  305.             position++;
  306.     }
  307.    
  308.     if (position >= columns * rows) {
  309.         position -= columns;
  310.         scroll_screen();
  311.     }
  312.    
  313.     invert_cursor();
  314.    
  315.     spinlock_unlock(&fb_lock);
  316. }
  317.  
  318. static chardev_t framebuffer;
  319. static chardev_operations_t fb_ops = {
  320.     .write = fb_putchar,
  321. };
  322.  
  323.  
  324. /** Initialize framebuffer as a chardev output device
  325.  *
  326.  * @param addr Physical address of the framebuffer
  327.  * @param x    Screen width in pixels
  328.  * @param y    Screen height in pixels
  329.  * @param bpp  Bits per pixel (8, 16, 24, 32)
  330.  * @param scan Bytes per one scanline
  331.  *
  332.  */
  333. void fb_init(__address addr, unsigned int x, unsigned int y, unsigned int bpp, unsigned int scan)
  334. {
  335.     switch (bpp) {
  336.         case 8:
  337.             rgb2scr = rgb_1byte;
  338.             scr2rgb = byte1_rgb;
  339.             pixelbytes = 1;
  340.             break;
  341.         case 16:
  342.             rgb2scr = rgb_2byte;
  343.             scr2rgb = byte2_rgb;
  344.             pixelbytes = 2;
  345.             break;
  346.         case 24:
  347.             rgb2scr = rgb_3byte;
  348.             scr2rgb = byte3_rgb;
  349.             pixelbytes = 3;
  350.             break;
  351.         case 32:
  352.             rgb2scr = rgb_4byte;
  353.             scr2rgb = byte4_rgb;
  354.             pixelbytes = 4;
  355.             break;
  356.         default:
  357.             panic("Unsupported bpp");
  358.     }
  359.    
  360.     unsigned int fbsize = scan * y;
  361.    
  362.     /* Map the framebuffer */
  363.     fbaddress = (__u8 *) hw_map((__address) addr, fbsize);
  364.    
  365.     xres = x;
  366.     yres = y;
  367.     bitspp = bpp;
  368.     scanline = scan;
  369.    
  370.     rows = y / FONT_SCANLINES;
  371.     columns = x / COL_WIDTH;
  372.  
  373.     sysinfo_set_item_val("fb", NULL, true);
  374.     sysinfo_set_item_val("fb.kind", NULL, 1);
  375.     sysinfo_set_item_val("fb.width", NULL, xres);
  376.     sysinfo_set_item_val("fb.height", NULL, yres);
  377.     sysinfo_set_item_val("fb.bpp", NULL, bpp);
  378.     sysinfo_set_item_val("fb.scanline", NULL, scan);
  379.     sysinfo_set_item_val("fb.address.physical", NULL, addr);
  380.  
  381.     /* Allocate double buffer */
  382.     int totsize = scanline * yres;
  383.     int pages = SIZE2FRAMES(totsize);
  384.     int order;
  385.     int rc;
  386.     if (pages == 1)
  387.         order = 0;
  388.     else
  389.         order = fnzb(pages-1)+1;
  390.  
  391.     pfn_t frame = frame_alloc_rc(order,FRAME_ATOMIC,&rc);
  392.     if (!rc)
  393.         dbbuffer = (void *)PA2KA(PFN2ADDR(frame));
  394.     else
  395.         printf("Failed to allocate scroll buffer.\n");
  396.     dboffset = 0;
  397.  
  398.     /* Initialized blank line */
  399.     blankline = (__u8 *) malloc(ROW_BYTES, FRAME_ATOMIC);
  400.     ASSERT(blankline);
  401.     for (y=0; y < FONT_SCANLINES; y++)
  402.         for (x=0; x < xres; x++)
  403.             (*rgb2scr)(&blankline[POINTPOS(x,y)],BGCOLOR);
  404.  
  405.     clear_screen();
  406.  
  407.     /* Update size of screen to match text area */
  408.     yres = rows * FONT_SCANLINES;
  409.  
  410.     draw_logo(xres - helenos_width, 0);
  411.     invert_cursor();
  412.  
  413.     chardev_initialize("fb", &framebuffer, &fb_ops);
  414.     stdout = &framebuffer;
  415.    
  416. }
  417.