Subversion Repositories HelenOS

Rev

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

  1. /*
  2.  * Copyright (c) 2008 Martin Decky
  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. /** @addtogroup genarch
  31.  * @{
  32.  */
  33. /** @file
  34.  */
  35.  
  36. #include <genarch/fb/font-8x16.h>
  37. #include <genarch/fb/logo-196x66.h>
  38. #include <genarch/fb/visuals.h>
  39. #include <genarch/fb/fb.h>
  40. #include <console/chardev.h>
  41. #include <console/console.h>
  42. #include <sysinfo/sysinfo.h>
  43. #include <mm/slab.h>
  44. #include <align.h>
  45. #include <panic.h>
  46. #include <memstr.h>
  47. #include <config.h>
  48. #include <bitops.h>
  49. #include <print.h>
  50. #include <string.h>
  51. #include <ddi/ddi.h>
  52. #include <arch/types.h>
  53.  
  54. SPINLOCK_INITIALIZE(fb_lock);
  55.  
  56. static uint8_t *fb_addr;
  57. static uint16_t *backbuf;
  58. static uint8_t *glyphs;
  59. static uint8_t *bgscan;
  60.  
  61. static unsigned int xres;
  62. static unsigned int yres;
  63.  
  64. static unsigned int ylogo;
  65. static unsigned int ytrim;
  66. static unsigned int rowtrim;
  67.  
  68. static unsigned int scanline;
  69. static unsigned int glyphscanline;
  70.  
  71. static unsigned int pixelbytes;
  72. static unsigned int glyphbytes;
  73. static unsigned int bgscanbytes;
  74.  
  75. static unsigned int cols;
  76. static unsigned int rows;
  77. static unsigned int position = 0;
  78.  
  79. #define BG_COLOR     0x000080
  80. #define FG_COLOR     0xffff00
  81. #define INV_COLOR    0xaaaaaa
  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 COL2X(col)           ((col) * FONT_WIDTH)
  88. #define ROW2Y(row)           ((row) * FONT_SCANLINES)
  89.  
  90. #define X2COL(x)             ((x) / FONT_WIDTH)
  91. #define Y2ROW(y)             ((y) / FONT_SCANLINES)
  92.  
  93. #define FB_POS(x, y)         ((y) * scanline + (x) * pixelbytes)
  94. #define BB_POS(col, row)     ((row) * cols + (col))
  95. #define GLYPH_POS(glyph, y)  ((glyph) * glyphbytes + (y) * glyphscanline)
  96.  
  97.  
  98. static void (*rgb_conv)(void *, uint32_t);
  99.  
  100.  
  101. /** ARGB 8:8:8:8 conversion
  102.  *
  103.  */
  104. static void rgb_0888(void *dst, uint32_t rgb)
  105. {
  106.     *((uint32_t *) dst) = rgb & 0xffffff;
  107. }
  108.  
  109.  
  110. /** ABGR 8:8:8:8 conversion
  111.  *
  112.  */
  113. static void bgr_0888(void *dst, uint32_t rgb)
  114. {
  115.     *((uint32_t *) dst)
  116.         = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
  117. }
  118.  
  119. static void rgb_8880(void *dst, uint32_t rgb)
  120. {
  121.     *((uint32_t *) dst)
  122.        = (RED(rgb, 8) << 24) | (GREEN(rgb, 8) << 16) | (BLUE(rgb, 8) << 8);
  123.  
  124. }
  125.  
  126.  
  127. /** RGB 8:8:8 conversion
  128.  *
  129.  */
  130. static void rgb_888(void *dst, uint32_t rgb)
  131. {
  132.     ((uint8_t *) dst)[0] = BLUE(rgb, 8);
  133.     ((uint8_t *) dst)[1] = GREEN(rgb, 8);
  134.     ((uint8_t *) dst)[2] = RED(rgb, 8);
  135. }
  136.  
  137.  
  138. /** BGR 8:8:8 conversion
  139.  *
  140.  */
  141. static void bgr_888(void *dst, uint32_t rgb)
  142. {
  143.     ((uint8_t *) dst)[0] = RED(rgb, 8);
  144.     ((uint8_t *) dst)[1] = GREEN(rgb, 8);
  145.     ((uint8_t *) dst)[2] = BLUE(rgb, 8);
  146. }
  147.  
  148.  
  149. /** RGB 5:5:5 conversion
  150.  *
  151.  */
  152. static void rgb_555(void *dst, uint32_t rgb)
  153. {
  154.     *((uint16_t *) dst)
  155.         = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
  156. }
  157.  
  158.  
  159. /** RGB 5:6:5 conversion
  160.  *
  161.  */
  162. static void rgb_565(void *dst, uint32_t rgb)
  163. {
  164.     *((uint16_t *) dst)
  165.         = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
  166. }
  167.  
  168.  
  169. /** RGB 3:2:3
  170.  *
  171.  * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
  172.  * will most likely use a color palette. The color appearance
  173.  * will be pretty random and depend on the default installed
  174.  * palette. This could be fixed by supporting custom palette
  175.  * and setting it to simulate the 8-bit truecolor.
  176.  *
  177.  * Currently we set the palette on the ia32, amd64 and sparc64 port.
  178.  *
  179.  * Note that the byte is being inverted by this function. The reason is
  180.  * that we would like to use a color palette where the white color code
  181.  * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
  182.  * use these codes for black and white and prevent to set codes
  183.  * 0 and 255 to other colors.
  184.  *
  185.  */
  186. static void rgb_323(void *dst, uint32_t rgb)
  187. {
  188.     *((uint8_t *) dst)
  189.         = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
  190. }
  191.  
  192.  
  193. /** Hide logo and refresh screen
  194.  *
  195.  */
  196. static void logo_hide(bool silent)
  197. {
  198.     ylogo = 0;
  199.     ytrim = yres;
  200.     rowtrim = rows;
  201.     if (!silent)
  202.         fb_redraw();
  203. }
  204.  
  205.  
  206. /** Draw character at given position
  207.  *
  208.  */
  209. static void glyph_draw(uint16_t glyph, unsigned int col, unsigned int row, bool silent, bool overlay)
  210. {
  211.     unsigned int x = COL2X(col);
  212.     unsigned int y = ROW2Y(row);
  213.     unsigned int yd;
  214.    
  215.     if (y >= ytrim)
  216.         logo_hide(silent);
  217.    
  218.     if (!overlay)
  219.         backbuf[BB_POS(col, row)] = glyph;
  220.    
  221.     if (!silent) {
  222.         for (yd = 0; yd < FONT_SCANLINES; yd++)
  223.             memcpy(&fb_addr[FB_POS(x, y + yd + ylogo)],
  224.                 &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
  225.     }
  226. }
  227.  
  228.  
  229. /** Scroll screen down by one row
  230.  *
  231.  *
  232.  */
  233. static void screen_scroll(bool silent)
  234. {
  235.     if (ylogo > 0) {
  236.         logo_hide(silent);
  237.         return;
  238.     }
  239.    
  240.     if (!silent) {
  241.         unsigned int row;
  242.        
  243.         for (row = 0; row < rows; row++) {
  244.             unsigned int y = ROW2Y(row);
  245.             unsigned int yd;
  246.            
  247.             for (yd = 0; yd < FONT_SCANLINES; yd++) {
  248.                 unsigned int x;
  249.                 unsigned int col;
  250.                
  251.                 for (col = 0, x = 0; col < cols; col++,
  252.                     x += FONT_WIDTH) {
  253.                     uint16_t glyph;
  254.                    
  255.                     if (row < rows - 1) {
  256.                         if (backbuf[BB_POS(col, row)] ==
  257.                             backbuf[BB_POS(col, row + 1)])
  258.                             continue;
  259.                        
  260.                         glyph = backbuf[BB_POS(col, row + 1)];
  261.                     } else
  262.                         glyph = 0;
  263.                    
  264.                     memcpy(&fb_addr[FB_POS(x, y + yd)],
  265.                         &glyphs[GLYPH_POS(glyph, yd)],
  266.                         glyphscanline);
  267.                 }
  268.             }
  269.         }
  270.     }
  271.    
  272.     memmove(backbuf, &backbuf[BB_POS(0, 1)], cols * (rows - 1) * sizeof(uint16_t));
  273.     memsetw(&backbuf[BB_POS(0, rows - 1)], cols, 0);
  274. }
  275.  
  276.  
  277. static void cursor_put(bool silent)
  278. {
  279.     unsigned int col = position % cols;
  280.     unsigned int row = position / cols;
  281.    
  282.     glyph_draw(fb_font_glyph(U_CURSOR), col, row, silent, true);
  283. }
  284.  
  285.  
  286. static void cursor_remove(bool silent)
  287. {
  288.     unsigned int col = position % cols;
  289.     unsigned int row = position / cols;
  290.    
  291.     glyph_draw(backbuf[BB_POS(col, row)], col, row, silent, true);
  292. }
  293.  
  294.  
  295. /** Print character to screen
  296.  *
  297.  * Emulate basic terminal commands.
  298.  *
  299.  */
  300. static void fb_putchar(outdev_t *dev, wchar_t ch, bool silent)
  301. {
  302.     spinlock_lock(&fb_lock);
  303.    
  304.     switch (ch) {
  305.     case '\n':
  306.         cursor_remove(silent);
  307.         position += cols;
  308.         position -= position % cols;
  309.         break;
  310.     case '\r':
  311.         cursor_remove(silent);
  312.         position -= position % cols;
  313.         break;
  314.     case '\b':
  315.         cursor_remove(silent);
  316.         if (position % cols)
  317.             position--;
  318.         break;
  319.     case '\t':
  320.         cursor_remove(silent);
  321.         do {
  322.             glyph_draw(fb_font_glyph(' '), position % cols,
  323.                 position / cols, silent, false);
  324.             position++;
  325.         } while ((position % 8) && (position < cols * rows));
  326.         break;
  327.     default:
  328.         glyph_draw(fb_font_glyph(ch), position % cols,
  329.             position / cols, silent, false);
  330.         position++;
  331.     }
  332.    
  333.     if (position >= cols * rows) {
  334.         position -= cols;
  335.         screen_scroll(silent);
  336.     }
  337.    
  338.     cursor_put(silent);
  339.    
  340.     spinlock_unlock(&fb_lock);
  341. }
  342.  
  343. static outdev_t fb_console;
  344. static outdev_operations_t fb_ops = {
  345.     .write = fb_putchar
  346. };
  347.  
  348.  
  349. /** Render glyphs
  350.  *
  351.  * Convert glyphs from device independent font
  352.  * description to current visual representation.
  353.  *
  354.  */
  355. static void glyphs_render(void)
  356. {
  357.     /* Prerender glyphs */
  358.     uint16_t glyph;
  359.    
  360.     for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
  361.         uint32_t fg_color;
  362.        
  363.         if (glyph == FONT_GLYPHS - 1)
  364.             fg_color = INV_COLOR;
  365.         else
  366.             fg_color = FG_COLOR;
  367.        
  368.         unsigned int y;
  369.        
  370.         for (y = 0; y < FONT_SCANLINES; y++) {
  371.             unsigned int x;
  372.            
  373.             for (x = 0; x < FONT_WIDTH; x++) {
  374.                 void *dst = &glyphs[GLYPH_POS(glyph, y) +
  375.                     x * pixelbytes];
  376.                 uint32_t rgb = (fb_font[glyph][y] &
  377.                     (1 << (7 - x))) ? fg_color : BG_COLOR;
  378.                 rgb_conv(dst, rgb);
  379.             }
  380.         }
  381.     }
  382.    
  383.     /* Prerender background scanline */
  384.     unsigned int x;
  385.    
  386.     for (x = 0; x < xres; x++)
  387.         rgb_conv(&bgscan[x * pixelbytes], BG_COLOR);
  388. }
  389.  
  390.  
  391. /** Refresh the screen
  392.  *
  393.  */
  394. void fb_redraw(void)
  395. {
  396.     if (ylogo > 0) {
  397.         unsigned int y;
  398.        
  399.         for (y = 0; y < LOGO_HEIGHT; y++) {
  400.             unsigned int x;
  401.            
  402.             for (x = 0; x < xres; x++)
  403.                 rgb_conv(&fb_addr[FB_POS(x, y)],
  404.                     (x < LOGO_WIDTH) ?
  405.                     fb_logo[y * LOGO_WIDTH + x] :
  406.                     LOGO_COLOR);
  407.         }
  408.     }
  409.    
  410.     unsigned int row;
  411.    
  412.     for (row = 0; row < rowtrim; row++) {
  413.         unsigned int y = ylogo + ROW2Y(row);
  414.         unsigned int yd;
  415.        
  416.         for (yd = 0; yd < FONT_SCANLINES; yd++) {
  417.             unsigned int x;
  418.             unsigned int col;
  419.            
  420.             for (col = 0, x = 0; col < cols;
  421.                 col++, x += FONT_WIDTH) {
  422.                 uint16_t glyph = backbuf[BB_POS(col, row)];
  423.                 void *dst = &fb_addr[FB_POS(x, y + yd)];
  424.                 void *src = &glyphs[GLYPH_POS(glyph, yd)];
  425.                 memcpy(dst, src, glyphscanline);
  426.             }
  427.         }
  428.     }
  429.    
  430.     if (COL2X(cols) < xres) {
  431.         unsigned int y;
  432.         unsigned int size = (xres - COL2X(cols)) * pixelbytes;
  433.        
  434.         for (y = ylogo; y < yres; y++)
  435.             memcpy(&fb_addr[FB_POS(COL2X(cols), y)], bgscan, size);
  436.     }
  437.    
  438.     if (ROW2Y(rowtrim) + ylogo < yres) {
  439.         unsigned int y;
  440.        
  441.         for (y = ROW2Y(rowtrim) + ylogo; y < yres; y++)
  442.             memcpy(&fb_addr[FB_POS(0, y)], bgscan, bgscanbytes);
  443.     }
  444. }
  445.  
  446.  
  447. /** Initialize framebuffer as a output character device
  448.  *
  449.  * @param addr   Physical address of the framebuffer
  450.  * @param x      Screen width in pixels
  451.  * @param y      Screen height in pixels
  452.  * @param scan   Bytes per one scanline
  453.  * @param visual Color model
  454.  *
  455.  */
  456. void fb_init(fb_properties_t *props)
  457. {
  458.     switch (props->visual) {
  459.     case VISUAL_INDIRECT_8:
  460.         rgb_conv = rgb_323;
  461.         pixelbytes = 1;
  462.         break;
  463.     case VISUAL_RGB_5_5_5:
  464.         rgb_conv = rgb_555;
  465.         pixelbytes = 2;
  466.         break;
  467.     case VISUAL_RGB_5_6_5:
  468.         rgb_conv = rgb_565;
  469.         pixelbytes = 2;
  470.         break;
  471.     case VISUAL_RGB_8_8_8:
  472.         rgb_conv = rgb_888;
  473.         pixelbytes = 3;
  474.         break;
  475.     case VISUAL_BGR_8_8_8:
  476.         rgb_conv = bgr_888;
  477.         pixelbytes = 3;
  478.         break;
  479.     case VISUAL_RGB_8_8_8_0:
  480.         rgb_conv = rgb_8880;
  481.         pixelbytes = 4;
  482.         break;
  483.     case VISUAL_RGB_0_8_8_8:
  484.         rgb_conv = rgb_0888;
  485.         pixelbytes = 4;
  486.         break;
  487.     case VISUAL_BGR_0_8_8_8:
  488.         rgb_conv = bgr_0888;
  489.         pixelbytes = 4;
  490.         break;
  491.     default:
  492.         panic("Unsupported visual.");
  493.     }
  494.    
  495.     xres = props->x;
  496.     yres = props->y;
  497.     scanline = props->scan;
  498.    
  499.     cols = X2COL(xres);
  500.     rows = Y2ROW(yres);
  501.    
  502.     if (yres > ylogo) {
  503.         ylogo = LOGO_HEIGHT;
  504.         rowtrim = rows - Y2ROW(ylogo);
  505.         if (ylogo % FONT_SCANLINES > 0)
  506.             rowtrim--;
  507.         ytrim = ROW2Y(rowtrim);
  508.     } else {
  509.         ylogo = 0;
  510.         ytrim = yres;
  511.         rowtrim = rows;
  512.     }
  513.    
  514.     glyphscanline = FONT_WIDTH * pixelbytes;
  515.     glyphbytes = ROW2Y(glyphscanline);
  516.     bgscanbytes = xres * pixelbytes;
  517.    
  518.     size_t fbsize = scanline * yres;
  519.     size_t bbsize = cols * rows * sizeof(uint16_t);
  520.     size_t glyphsize = FONT_GLYPHS * glyphbytes;
  521.    
  522.     backbuf = (uint16_t *) malloc(bbsize, 0);
  523.     if (!backbuf)
  524.         panic("Unable to allocate backbuffer.");
  525.    
  526.     glyphs = (uint8_t *) malloc(glyphsize, 0);
  527.     if (!glyphs)
  528.         panic("Unable to allocate glyphs.");
  529.    
  530.     bgscan = malloc(bgscanbytes, 0);
  531.     if (!bgscan)
  532.         panic("Unable to allocate background pixel.");
  533.    
  534.     memsetw(backbuf, cols * rows, 0);
  535.    
  536.     glyphs_render();
  537.    
  538.     fb_addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize);
  539.    
  540.     sysinfo_set_item_val("fb", NULL, true);
  541.     sysinfo_set_item_val("fb.kind", NULL, 1);
  542.     sysinfo_set_item_val("fb.width", NULL, xres);
  543.     sysinfo_set_item_val("fb.height", NULL, yres);
  544.     sysinfo_set_item_val("fb.scanline", NULL, scanline);
  545.     sysinfo_set_item_val("fb.visual", NULL, props->visual);
  546.     sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
  547.    
  548.     fb_redraw();
  549.    
  550.     outdev_initialize("fb", &fb_console, &fb_ops);
  551.     stdout = &fb_console;
  552. }
  553.  
  554. /** @}
  555.  */
  556.