Subversion Repositories HelenOS-historic

Rev

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