Subversion Repositories HelenOS

Rev

Rev 3478 | Go to most recent revision | Blame | 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. /** @addtogroup genarch
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <genarch/fb/font-8x16.h>
  36. #include <genarch/fb/visuals.h>
  37. #include <genarch/fb/fb.h>
  38. #include <console/chardev.h>
  39. #include <console/console.h>
  40. #include <sysinfo/sysinfo.h>
  41. #include <mm/slab.h>
  42. #include <panic.h>
  43. #include <memstr.h>
  44. #include <config.h>
  45. #include <bitops.h>
  46. #include <print.h>
  47. #include <ddi/ddi.h>
  48. #include <arch/types.h>
  49.  
  50. #include "helenos.xbm"
  51.  
  52. static parea_t fb_parea;        /**< Physical memory area for fb. */
  53.  
  54. SPINLOCK_INITIALIZE(fb_lock);
  55.  
  56. static uint8_t *fbaddress = NULL;
  57.  
  58. static uint8_t *blankline = NULL;
  59. static uint8_t *dbbuffer = NULL;    /* Buffer for fast scrolling console */
  60. static index_t dboffset;
  61.  
  62. static unsigned int xres = 0;
  63. static unsigned int yres = 0;
  64. static unsigned int scanline = 0;
  65. static unsigned int pixelbytes = 0;
  66. #ifdef FB_INVERT_COLORS
  67. static bool invert_colors = true;
  68. #else
  69. static bool invert_colors = false;
  70. #endif
  71.  
  72. static unsigned int position = 0;
  73. static unsigned int columns = 0;
  74. static unsigned int rows = 0;
  75.  
  76. #define COL_WIDTH   8
  77. #define ROW_BYTES   (scanline * FONT_SCANLINES)
  78.  
  79. #define BGCOLOR     0x000080
  80. #define FGCOLOR     0xffff00
  81. #define LOGOCOLOR   0x2020b0
  82.  
  83. #define RED(x, bits)    ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
  84. #define GREEN(x, bits)  ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
  85. #define BLUE(x, bits)   ((x >> (8 - bits)) & ((1 << bits) - 1))
  86.  
  87. #define POINTPOS(x, y)  ((y) * scanline + (x) * pixelbytes)
  88.  
  89. /***************************************************************/
  90. /* Pixel specific fuctions */
  91.  
  92. static void (*rgb2scr)(void *, int);
  93. static int (*scr2rgb)(void *);
  94.  
  95. static inline int COLOR(int color)
  96. {
  97.     return invert_colors ? ~color : color;
  98. }
  99.  
  100. /* Conversion routines between different color representations */
  101. static void rgb_byte0888(void *dst, int rgb)
  102. {
  103.     *((int *) dst) = rgb;
  104. }
  105.  
  106. static int byte0888_rgb(void *src)
  107. {
  108.     return (*((int *) src)) & 0xffffff;
  109. }
  110.  
  111. static void bgr_byte0888(void *dst, int rgb)
  112. {
  113.     *((uint32_t *) dst) = BLUE(rgb, 8) << 16 | GREEN(rgb, 8) << 8 |
  114.         RED(rgb, 8);
  115. }
  116.  
  117. static int byte0888_bgr(void *src)
  118. {
  119.     int color = *(uint32_t *)(src);
  120.     return ((color & 0xff) << 16) | (((color >> 8) & 0xff) << 8) |
  121.         ((color >> 16) & 0xff);
  122. }
  123.  
  124. static void rgb_byte888(void *dst, int rgb)
  125. {
  126.     uint8_t *scr = (uint8_t *) dst;
  127. #if defined(FB_INVERT_ENDIAN)
  128.     scr[0] = RED(rgb, 8);
  129.     scr[1] = GREEN(rgb, 8);
  130.     scr[2] = BLUE(rgb, 8);
  131. #else
  132.     scr[2] = RED(rgb, 8);
  133.     scr[1] = GREEN(rgb, 8);
  134.     scr[0] = BLUE(rgb, 8);
  135. #endif
  136. }
  137.  
  138. static int byte888_rgb(void *src)
  139. {
  140.     uint8_t *scr = (uint8_t *) src;
  141. #if defined(FB_INVERT_ENDIAN)
  142.     return scr[0] << 16 | scr[1] << 8 | scr[2];
  143. #else
  144.     return scr[2] << 16 | scr[1] << 8 | scr[0];
  145. #endif 
  146. }
  147.  
  148. static void bgr_byte8880(void *dst, int rgb)
  149. {
  150.     uint8_t *scr = (uint8_t *) dst;
  151.     scr[3] = RED(rgb, 8);
  152.     scr[2] = GREEN(rgb, 8);
  153.     scr[1] = BLUE(rgb, 8);
  154. }
  155.  
  156. static int byte8880_bgr(void *src)
  157. {
  158.     uint8_t *scr = (uint8_t *) src;
  159.     return scr[3] << 16 | scr[2] << 8 | scr[1];
  160. }
  161.  
  162. /**  16-bit depth (5:5:5) */
  163. static void rgb_byte555(void *dst, int rgb)
  164. {
  165.     /* 5-bit, 5-bits, 5-bits */
  166.     *((uint16_t *) dst) = RED(rgb, 5) << 10 | GREEN(rgb, 5) << 5 |
  167.         BLUE(rgb, 5);
  168. }
  169.  
  170. /** 16-bit depth (5:5:5) */
  171. static int byte555_rgb(void *src)
  172. {
  173.     int color = *(uint16_t *)(src);
  174.     return (((color >> 10) & 0x1f) << (16 + 3)) |
  175.         (((color >> 5) & 0x1f) << (8 + 3)) | ((color & 0x1f) << 3);
  176. }
  177.  
  178. /**  16-bit depth (5:6:5) */
  179. static void rgb_byte565(void *dst, int rgb)
  180. {
  181.     /* 5-bit, 6-bits, 5-bits */
  182.     *((uint16_t *) dst) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 |
  183.         BLUE(rgb, 5);
  184. }
  185.  
  186. /** 16-bit depth (5:6:5) */
  187. static int byte565_rgb(void *src)
  188. {
  189.     int color = *(uint16_t *)(src);
  190.     return (((color >> 11) & 0x1f) << (16 + 3)) |
  191.         (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
  192. }
  193.  
  194. /** Put pixel - 8-bit depth (color palette/3:2:3)
  195.  *
  196.  * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
  197.  * will most likely use a color palette. The color appearance
  198.  * will be pretty random and depend on the default installed
  199.  * palette. This could be fixed by supporting custom palette
  200.  * and setting it to simulate the 8-bit truecolor.
  201.  */
  202. static void rgb_byte8(void *dst, int rgb)
  203. {
  204.     *((uint8_t *) dst) = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 |
  205.         BLUE(rgb, 3);
  206. }
  207.  
  208. /** Return pixel color - 8-bit depth (color palette/3:2:3)
  209.  *
  210.  * See the comment for rgb_byte().
  211.  */
  212. static int byte8_rgb(void *src)
  213. {
  214.     int color = *(uint8_t *)src;
  215.     return (((color >> 5) & 0x7) << (16 + 5)) |
  216.         (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
  217. }
  218.  
  219. static void putpixel(unsigned int x, unsigned int y, int color)
  220. {
  221.     (*rgb2scr)(&fbaddress[POINTPOS(x, y)], COLOR(color));
  222.  
  223.     if (dbbuffer) {
  224.         int dline = (y + dboffset) % yres;
  225.         (*rgb2scr)(&dbbuffer[POINTPOS(x, dline)], COLOR(color));
  226.     }
  227. }
  228.  
  229. /** Get pixel from viewport */
  230. static int getpixel(unsigned int x, unsigned int y)
  231. {
  232.     if (dbbuffer) {
  233.         int dline = (y + dboffset) % yres;
  234.         return COLOR((*scr2rgb)(&dbbuffer[POINTPOS(x, dline)]));
  235.     }
  236.     return COLOR((*scr2rgb)(&fbaddress[POINTPOS(x, y)]));
  237. }
  238.  
  239.  
  240. /** Fill screen with background color */
  241. static void clear_screen(void)
  242. {
  243.     unsigned int y;
  244.  
  245.     for (y = 0; y < yres; y++) {
  246.         memcpy(&fbaddress[scanline * y], blankline, xres * pixelbytes);
  247.         if (dbbuffer)
  248.             memcpy(&dbbuffer[scanline * y], blankline,
  249.                 xres * pixelbytes);
  250.     }
  251. }
  252.  
  253.  
  254. /** Scroll screen one row up */
  255. static void scroll_screen(void)
  256. {
  257.     if (dbbuffer) {
  258.         count_t first;
  259.        
  260.         /* Clear the last row */
  261.         memcpy(&dbbuffer[dboffset * scanline], blankline, ROW_BYTES);
  262.        
  263.         dboffset = (dboffset + FONT_SCANLINES) % yres;
  264.         first = yres - dboffset;
  265.        
  266.         /* Move all rows one row up */
  267.         if (xres * pixelbytes == scanline) {
  268.             memcpy(fbaddress, &dbbuffer[dboffset * scanline],
  269.                 first * scanline);
  270.             memcpy(&fbaddress[first * scanline], dbbuffer,
  271.                 dboffset * scanline);
  272.         } else {
  273.             /*
  274.              * When the scanline is bigger than number of bytes
  275.              * in the X-resolution, chances are that the
  276.              * frame buffer memory past the X-resolution is special
  277.              * in some way. For example, the SUNW,ffb framebuffer
  278.              * wraps this area around the beginning of the same
  279.              * line. To avoid troubles, copy only memory as
  280.              * specified by the resolution.
  281.              */
  282.             unsigned int i;
  283.  
  284.             for (i = 0; i < first; i++)
  285.                 memcpy(&fbaddress[i * scanline],
  286.                     &dbbuffer[(dboffset + i) * scanline],
  287.                     xres * pixelbytes);
  288.             for (i = 0; i < dboffset; i++)
  289.                 memcpy(&fbaddress[(first + i) * scanline],
  290.                     &dbbuffer[i * scanline], xres * pixelbytes);
  291.         }
  292.     } else {
  293.         uint8_t *lastline = &fbaddress[(rows - 1) * ROW_BYTES];
  294.        
  295.         if (xres * pixelbytes == scanline) {
  296.             /* Move all rows one row up */
  297.             memcpy((void *) fbaddress,
  298.                 (void *) &fbaddress[ROW_BYTES],
  299.                 scanline * yres - ROW_BYTES);
  300.             /* Clear the last row */
  301.             memcpy((void *) lastline, (void *) blankline,
  302.                 ROW_BYTES);
  303.         } else {
  304.             /*
  305.              * See the comment in the dbbuffer case.
  306.              */
  307.             unsigned int i;
  308.  
  309.             /* Move all rows one row up */
  310.             for (i = 0; i < yres - FONT_SCANLINES; i++)
  311.                 memcpy(&fbaddress[i * scanline],
  312.                     &fbaddress[(i + FONT_SCANLINES) * scanline],
  313.                     xres * pixelbytes);
  314.             /* Clear the last row */
  315.             for (i = 0; i < FONT_SCANLINES; i++)
  316.                 memcpy(&lastline[i * scanline],
  317.                     &blankline[i * scanline],
  318.                     xres * pixelbytes);
  319.         }
  320.     }
  321. }
  322.  
  323.  
  324. static void invert_pixel(unsigned int x, unsigned int y)
  325. {
  326.     putpixel(x, y, ~getpixel(x, y));
  327. }
  328.  
  329.  
  330. /** Draw one line of glyph at a given position */
  331. static void draw_glyph_line(unsigned int glline, unsigned int x, unsigned int y)
  332. {
  333.     unsigned int i;
  334.  
  335.     for (i = 0; i < 8; i++)
  336.         if (glline & (1 << (7 - i))) {
  337.             putpixel(x + i, y, FGCOLOR);
  338.         } else
  339.             putpixel(x + i, y, BGCOLOR);
  340. }
  341.  
  342. /***************************************************************/
  343. /* Character-console functions */
  344.  
  345. /** Draw character at given position */
  346. static void draw_glyph(uint8_t glyph, unsigned int col, unsigned int row)
  347. {
  348.     unsigned int y;
  349.  
  350.     for (y = 0; y < FONT_SCANLINES; y++)
  351.         draw_glyph_line(fb_font[glyph * FONT_SCANLINES + y],
  352.             col * COL_WIDTH, row * FONT_SCANLINES + y);
  353. }
  354.  
  355. /** Invert character at given position */
  356. static void invert_char(unsigned int col, unsigned int row)
  357. {
  358.     unsigned int x;
  359.     unsigned int y;
  360.  
  361.     for (x = 0; x < COL_WIDTH; x++)
  362.         for (y = 0; y < FONT_SCANLINES; y++)
  363.             invert_pixel(col * COL_WIDTH + x,
  364.                 row * FONT_SCANLINES + y);
  365. }
  366.  
  367. /** Draw character at default position */
  368. static void draw_char(char chr)
  369. {
  370.     draw_glyph(chr, position % columns, position / columns);
  371. }
  372.  
  373. static void draw_logo(unsigned int startx, unsigned int starty)
  374. {
  375.     unsigned int x;
  376.     unsigned int y;
  377.     unsigned int byte;
  378.     unsigned int rowbytes;
  379.  
  380.     rowbytes = (helenos_width - 1) / 8 + 1;
  381.  
  382.     for (y = 0; y < helenos_height; y++)
  383.         for (x = 0; x < helenos_width; x++) {
  384.             byte = helenos_bits[rowbytes * y + x / 8];
  385.             byte >>= x % 8;
  386.             if (byte & 1)
  387.                 putpixel(startx + x, starty + y,
  388.                     COLOR(LOGOCOLOR));
  389.         }
  390. }
  391.  
  392. /***************************************************************/
  393. /* Stdout specific functions */
  394.  
  395. static void invert_cursor(void)
  396. {
  397.     invert_char(position % columns, position / columns);
  398. }
  399.  
  400. /** Print character to screen
  401.  *
  402.  *  Emulate basic terminal commands
  403.  */
  404. static void fb_putchar(chardev_t *dev, char ch)
  405. {
  406.     spinlock_lock(&fb_lock);
  407.    
  408.     switch (ch) {
  409.     case '\n':
  410.         invert_cursor();
  411.         position += columns;
  412.         position -= position % columns;
  413.         break;
  414.     case '\r':
  415.         invert_cursor();
  416.         position -= position % columns;
  417.         break;
  418.     case '\b':
  419.         invert_cursor();
  420.         if (position % columns)
  421.             position--;
  422.         break;
  423.     case '\t':
  424.         invert_cursor();
  425.         do {
  426.             draw_char(' ');
  427.             position++;
  428.         } while ((position % 8) && position < columns * rows);
  429.         break;
  430.     default:
  431.         draw_char(ch);
  432.         position++;
  433.     }
  434.    
  435.     if (position >= columns * rows) {
  436.         position -= columns;
  437.         scroll_screen();
  438.     }
  439.    
  440.     invert_cursor();
  441.    
  442.     spinlock_unlock(&fb_lock);
  443. }
  444.  
  445. static chardev_t framebuffer;
  446. static chardev_operations_t fb_ops = {
  447.     .write = fb_putchar,
  448. };
  449.  
  450.  
  451. /** Initialize framebuffer as a chardev output device
  452.  *
  453.  * @param addr   Physical address of the framebuffer
  454.  * @param x      Screen width in pixels
  455.  * @param y      Screen height in pixels
  456.  * @param scan   Bytes per one scanline
  457.  * @param visual Color model
  458.  *
  459.  */
  460. void fb_init(uintptr_t addr, unsigned int x, unsigned int y, unsigned int scan,
  461.     unsigned int visual)
  462. {
  463.     switch (visual) {
  464.     case VISUAL_INDIRECT_8:
  465.         rgb2scr = rgb_byte8;
  466.         scr2rgb = byte8_rgb;
  467.         pixelbytes = 1;
  468.         break;
  469.     case VISUAL_RGB_5_5_5:
  470.         rgb2scr = rgb_byte555;
  471.         scr2rgb = byte555_rgb;
  472.         pixelbytes = 2;
  473.         break;
  474.     case VISUAL_RGB_5_6_5:
  475.         rgb2scr = rgb_byte565;
  476.         scr2rgb = byte565_rgb;
  477.         pixelbytes = 2;
  478.         break;
  479.     case VISUAL_RGB_8_8_8:
  480.         rgb2scr = rgb_byte888;
  481.         scr2rgb = byte888_rgb;
  482.         pixelbytes = 3;
  483.         break;
  484.     case VISUAL_RGB_8_8_8_0:
  485.         rgb2scr = rgb_byte888;
  486.         scr2rgb = byte888_rgb;
  487.         pixelbytes = 4;
  488.         break;
  489.     case VISUAL_RGB_0_8_8_8:
  490.         rgb2scr = rgb_byte0888;
  491.         scr2rgb = byte0888_rgb;
  492.         pixelbytes = 4;
  493.         break;
  494.     case VISUAL_BGR_0_8_8_8:
  495.         rgb2scr = bgr_byte0888;
  496.         scr2rgb = byte0888_bgr;
  497.         pixelbytes = 4;
  498.         break;
  499.     case VISUAL_BGR_8_8_8_0:
  500.         rgb2scr = bgr_byte8880;
  501.         scr2rgb = byte8880_bgr;
  502.         pixelbytes = 4;
  503.         break;
  504.     default:
  505.         panic("Unsupported visual.\n");
  506.     }
  507.    
  508.     unsigned int fbsize = scan * y;
  509.    
  510.     /* Map the framebuffer */
  511.     fbaddress = (uint8_t *) hw_map((uintptr_t) addr, fbsize);
  512.    
  513.     xres = x;
  514.     yres = y;
  515.     scanline = scan;
  516.    
  517.     rows = y / FONT_SCANLINES;
  518.     columns = x / COL_WIDTH;
  519.  
  520.     fb_parea.pbase = (uintptr_t) addr;
  521.     fb_parea.vbase = (uintptr_t) fbaddress;
  522.     fb_parea.frames = SIZE2FRAMES(fbsize);
  523.     fb_parea.cacheable = false;
  524.     ddi_parea_register(&fb_parea);
  525.  
  526.     sysinfo_set_item_val("fb", NULL, true);
  527.     sysinfo_set_item_val("fb.kind", NULL, 1);
  528.     sysinfo_set_item_val("fb.width", NULL, xres);
  529.     sysinfo_set_item_val("fb.height", NULL, yres);
  530.     sysinfo_set_item_val("fb.scanline", NULL, scan);
  531.     sysinfo_set_item_val("fb.visual", NULL, visual);
  532.     sysinfo_set_item_val("fb.address.physical", NULL, addr);
  533.     sysinfo_set_item_val("fb.invert-colors", NULL, invert_colors);
  534.  
  535.     /* Allocate double buffer */
  536.     unsigned int order = fnzb(SIZE2FRAMES(fbsize) - 1) + 1;
  537.     dbbuffer = (uint8_t *) frame_alloc(order, FRAME_ATOMIC | FRAME_KA);
  538.     if (!dbbuffer)
  539.         printf("Failed to allocate scroll buffer.\n");
  540.     dboffset = 0;
  541.  
  542.     /* Initialized blank line */
  543.     blankline = (uint8_t *) malloc(ROW_BYTES, FRAME_ATOMIC);
  544.     if (!blankline)
  545.         panic("Failed to allocate blank line for framebuffer.");
  546.     for (y = 0; y < FONT_SCANLINES; y++)
  547.         for (x = 0; x < xres; x++)
  548.             (*rgb2scr)(&blankline[POINTPOS(x, y)], COLOR(BGCOLOR));
  549.    
  550.     clear_screen();
  551.  
  552.     /* Update size of screen to match text area */
  553.     yres = rows * FONT_SCANLINES;
  554.  
  555.     draw_logo(xres - helenos_width, 0);
  556.     invert_cursor();
  557.  
  558.     chardev_initialize("fb", &framebuffer, &fb_ops);
  559.     stdout = &framebuffer;
  560. }
  561.  
  562. /** @}
  563.  */
  564.