Subversion Repositories HelenOS-historic

Rev

Rev 1327 | Rev 1376 | 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 <panic.h>
  36. #include <memstr.h>
  37. #include <config.h>
  38.  
  39. #include "helenos.xbm"
  40.  
  41. SPINLOCK_INITIALIZE(fb_lock);
  42.  
  43. static __u8 *fbaddress = NULL;
  44.  
  45. static __u8 *blankline = NULL;
  46.  
  47. static unsigned int xres = 0;
  48. static unsigned int yres = 0;
  49. static unsigned int scanline = 0;
  50. static unsigned int bitspp = 0;
  51. static unsigned int pixelbytes = 0;
  52.  
  53. static unsigned int position = 0;
  54. static unsigned int columns = 0;
  55. static unsigned int rows = 0;
  56.  
  57.  
  58. #define COL_WIDTH   8
  59. #define ROW_BYTES   (scanline * FONT_SCANLINES)
  60.  
  61. #define BGCOLOR     0x000080
  62. #define FGCOLOR     0xffff00
  63. #define LOGOCOLOR   0x2020b0
  64.  
  65. #define RED(x, bits)    ((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
  66. #define GREEN(x, bits)  ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
  67. #define BLUE(x, bits)   ((x >> (8 - bits)) & ((1 << bits) - 1))
  68.  
  69. #define POINTPOS(x, y)  (y * scanline + x * pixelbytes)
  70.  
  71. /***************************************************************/
  72. /* Pixel specific fuctions */
  73.  
  74. static void (*putpixel)(unsigned int x, unsigned int y, int color);
  75. static int (*getpixel)(unsigned int x, unsigned int y);
  76.  
  77. /** Put pixel - 24-bit depth, 1 free byte */
  78. static void putpixel_4byte(unsigned int x, unsigned int y, int color)
  79. {
  80.     *((__u32 *)(fbaddress + POINTPOS(x, y))) = color;
  81. }
  82.  
  83. /** Return pixel color - 24-bit depth, 1 free byte */
  84. static int getpixel_4byte(unsigned int x, unsigned int y)
  85. {
  86.     return *((__u32 *)(fbaddress + POINTPOS(x, y))) & 0xffffff;
  87. }
  88.  
  89. /** Put pixel - 24-bit depth */
  90. static void putpixel_3byte(unsigned int x, unsigned int y, int color)
  91. {
  92.     unsigned int startbyte = POINTPOS(x, y);
  93.  
  94. #if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
  95.     fbaddress[startbyte] = RED(color, 8);
  96.     fbaddress[startbyte + 1] = GREEN(color, 8);
  97.     fbaddress[startbyte + 2] = BLUE(color, 8);
  98. #else
  99.     fbaddress[startbyte + 2] = RED(color, 8);
  100.     fbaddress[startbyte + 1] = GREEN(color, 8);
  101.     fbaddress[startbyte + 0] = BLUE(color, 8);
  102. #endif 
  103. }
  104.  
  105. /** Return pixel color - 24-bit depth */
  106. static int getpixel_3byte(unsigned int x, unsigned int y)
  107. {
  108.     unsigned int startbyte = POINTPOS(x, y);
  109.  
  110. #if (defined(BIG_ENDIAN) || defined(FB_BIG_ENDIAN))
  111.     return fbaddress[startbyte] << 16 | fbaddress[startbyte + 1] << 8 | fbaddress[startbyte + 2];
  112. #else
  113.     return fbaddress[startbyte + 2] << 16 | fbaddress[startbyte + 1] << 8 | fbaddress[startbyte + 0];
  114. #endif 
  115. }
  116.  
  117. /** Put pixel - 16-bit depth (5:6:6) */
  118. static void putpixel_2byte(unsigned int x, unsigned int y, int color)
  119. {
  120.     /* 5-bit, 5-bits, 5-bits */
  121.     *((__u16 *)(fbaddress + POINTPOS(x, y))) = RED(color, 5) << 11 | GREEN(color, 6) << 5 | BLUE(color, 5);
  122. }
  123.  
  124. /** Return pixel color - 16-bit depth (5:6:6) */
  125. static int getpixel_2byte(unsigned int x, unsigned int y)
  126. {
  127.     int color = *((__u16 *)(fbaddress + POINTPOS(x, y)));
  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 putpixel_1byte(unsigned int x, unsigned int y, int color)
  133. {
  134.     fbaddress[POINTPOS(x, y)] = RED(color, 3) << 5 | GREEN(color, 2) << 3 | BLUE(color, 3);
  135. }
  136.  
  137. /** Return pixel color - 8-bit depth (3:2:3) */
  138. static int getpixel_1byte(unsigned int x, unsigned int y)
  139. {
  140.     int color = fbaddress[POINTPOS(x, y)];
  141.     return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
  142. }
  143.  
  144. /** Fill line with color BGCOLOR */
  145. static void clear_line(unsigned int y)
  146. {
  147.     unsigned int x;
  148.    
  149.     for (x = 0; x < xres; x++)
  150.         (*putpixel)(x, y, BGCOLOR);
  151. }
  152.  
  153.  
  154. /** Fill screen with background color */
  155. static void clear_screen(void)
  156. {
  157.     unsigned int y;
  158.  
  159.     for (y = 0; y < yres; y++)
  160.         clear_line(y);
  161. }
  162.  
  163.  
  164. /** Scroll screen one row up */
  165. static void scroll_screen(void)
  166. {
  167.     unsigned int i;
  168.     __u8 *lastline = &fbaddress[(rows - 1) * ROW_BYTES];
  169.  
  170.     memcpy((void *) fbaddress, (void *) &fbaddress[ROW_BYTES], scanline * yres - ROW_BYTES);
  171.  
  172.     /* Clear last row */
  173.     if (blankline) {
  174.         memcpy((void *) lastline, (void *) blankline, ROW_BYTES);
  175.     } else {
  176.         for (i = 0; i < FONT_SCANLINES; i++)
  177.             clear_line((rows - 1) * FONT_SCANLINES + i);
  178.  
  179.         if (config.mm_initialized) {
  180.             /* Save a blank line aside. */
  181.             blankline = (__u8 *) malloc(ROW_BYTES, FRAME_ATOMIC);
  182.             if (blankline)
  183.                 memcpy((void *) blankline, (void *) lastline, ROW_BYTES);
  184.         }
  185.     }
  186. }
  187.  
  188.  
  189. static void invert_pixel(unsigned int x, unsigned int y)
  190. {
  191.     (*putpixel)(x, y, ~(*getpixel)(x, y));
  192. }
  193.  
  194.  
  195. /** Draw one line of glyph at a given position */
  196. static void draw_glyph_line(unsigned int glline, unsigned int x, unsigned int y)
  197. {
  198.     unsigned int i;
  199.  
  200.     for (i = 0; i < 8; i++)
  201.         if (glline & (1 << (7 - i))) {
  202.             (*putpixel)(x + i, y, FGCOLOR);
  203.         } else
  204.             (*putpixel)(x + i, y, BGCOLOR);
  205. }
  206.  
  207. /***************************************************************/
  208. /* Character-console functions */
  209.  
  210. /** Draw character at given position */
  211. static void draw_glyph(__u8 glyph, unsigned int col, unsigned int row)
  212. {
  213.     unsigned int y;
  214.  
  215.     for (y = 0; y < FONT_SCANLINES; y++)
  216.         draw_glyph_line(fb_font[glyph * FONT_SCANLINES + y], col * COL_WIDTH, row * FONT_SCANLINES + y);
  217. }
  218.  
  219. /** Invert character at given position */
  220. static void invert_char(unsigned int col, unsigned int row)
  221. {
  222.     unsigned int x;
  223.     unsigned int y;
  224.  
  225.     for (x = 0; x < COL_WIDTH; x++)
  226.         for (y = 0; y < FONT_SCANLINES; y++)
  227.             invert_pixel(col * COL_WIDTH + x, row * FONT_SCANLINES + y);
  228. }
  229.  
  230. /** Draw character at default position */
  231. static void draw_char(char chr)
  232. {
  233.     draw_glyph(chr, position % columns, position / columns);
  234. }
  235.  
  236. static void draw_logo(unsigned int startx, unsigned int starty)
  237. {
  238.     unsigned int x;
  239.     unsigned int y;
  240.     unsigned int byte;
  241.     unsigned int rowbytes;
  242.  
  243.     rowbytes = (helenos_width - 1) / 8 + 1;
  244.  
  245.     for (y = 0; y < helenos_height; y++)
  246.         for (x = 0; x < helenos_width; x++) {
  247.             byte = helenos_bits[rowbytes * y + x / 8];
  248.             byte >>= x % 8;
  249.             if (byte & 1)
  250.                 (*putpixel)(startx + x, starty + y, LOGOCOLOR);
  251.         }
  252. }
  253.  
  254. /***************************************************************/
  255. /* Stdout specific functions */
  256.  
  257. static void invert_cursor(void)
  258. {
  259.     invert_char(position % columns, position / columns);
  260. }
  261.  
  262. /** Print character to screen
  263.  *
  264.  *  Emulate basic terminal commands
  265.  */
  266. static void fb_putchar(chardev_t *dev, char ch)
  267. {
  268.     spinlock_lock(&fb_lock);
  269.    
  270.     switch (ch) {
  271.         case '\n':
  272.             invert_cursor();
  273.             position += columns;
  274.             position -= position % columns;
  275.             break;
  276.         case '\r':
  277.             invert_cursor();
  278.             position -= position % columns;
  279.             break;
  280.         case '\b':
  281.             invert_cursor();
  282.             if (position % columns)
  283.                 position--;
  284.             break;
  285.         case '\t':
  286.             invert_cursor();
  287.             do {
  288.                 draw_char(' ');
  289.                 position++;
  290.             } while ((position % 8) && position < columns * rows);
  291.             break;
  292.         default:
  293.             draw_char(ch);
  294.             position++;
  295.     }
  296.    
  297.     if (position >= columns * rows) {
  298.         position -= columns;
  299.         scroll_screen();
  300.     }
  301.    
  302.     invert_cursor();
  303.    
  304.     spinlock_unlock(&fb_lock);
  305. }
  306.  
  307. static chardev_t framebuffer;
  308. static chardev_operations_t fb_ops = {
  309.     .write = fb_putchar,
  310. };
  311.  
  312.  
  313. /** Initialize framebuffer as a chardev output device
  314.  *
  315.  * @param addr Address of theframebuffer
  316.  * @param x    Screen width in pixels
  317.  * @param y    Screen height in pixels
  318.  * @param bpp  Bits per pixel (8, 16, 24, 32)
  319.  * @param scan Bytes per one scanline
  320.  *
  321.  */
  322. void fb_init(__address addr, unsigned int x, unsigned int y, unsigned int bpp, unsigned int scan)
  323. {
  324.     switch (bpp) {
  325.         case 8:
  326.             putpixel = putpixel_1byte;
  327.             getpixel = getpixel_1byte;
  328.             pixelbytes = 1;
  329.             break;
  330.         case 16:
  331.             putpixel = putpixel_2byte;
  332.             getpixel = getpixel_2byte;
  333.             pixelbytes = 2;
  334.             break;
  335.         case 24:
  336.             putpixel = putpixel_3byte;
  337.             getpixel = getpixel_3byte;
  338.             pixelbytes = 3;
  339.             break;
  340.         case 32:
  341.             putpixel = putpixel_4byte;
  342.             getpixel = getpixel_4byte;
  343.             pixelbytes = 4;
  344.             break;
  345.         default:
  346.             panic("Unsupported bpp");
  347.     }
  348.        
  349.     fbaddress = (unsigned char *) addr;
  350.     xres = x;
  351.     yres = y;
  352.     bitspp = bpp;
  353.     scanline = scan;
  354.    
  355.     rows = y / FONT_SCANLINES;
  356.     columns = x / COL_WIDTH;
  357.  
  358.     clear_screen();
  359.     draw_logo(xres - helenos_width, 0);
  360.     invert_cursor();
  361.  
  362.     chardev_initialize("fb", &framebuffer, &fb_ops);
  363.     stdout = &framebuffer;
  364. }
  365.  
  366.  
  367. /** Register framebuffer in sysinfo
  368.  *
  369.  */
  370. void fb_register(void)
  371. {
  372.     sysinfo_set_item_val("fb", NULL, true);
  373.     sysinfo_set_item_val("fb.width", NULL, xres);
  374.     sysinfo_set_item_val("fb.height", NULL, yres);
  375.     sysinfo_set_item_val("fb.scanline", NULL, scanline);
  376.     sysinfo_set_item_val("fb.bpp", NULL, bitspp);
  377.     sysinfo_set_item_val("fb.address.virtual", NULL, (__address) fbaddress);
  378. }
  379.