Subversion Repositories HelenOS

Rev

Rev 3707 | Rev 3710 | 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/visuals.h>
  38. #include <genarch/fb/fb.h>
  39. #include <console/chardev.h>
  40. #include <console/console.h>
  41. #include <sysinfo/sysinfo.h>
  42. #include <mm/slab.h>
  43. #include <align.h>
  44. #include <panic.h>
  45. #include <memstr.h>
  46. #include <config.h>
  47. #include <bitops.h>
  48. #include <print.h>
  49. #include <ddi/ddi.h>
  50. #include <arch/types.h>
  51.  
  52. SPINLOCK_INITIALIZE(fb_lock);
  53.  
  54. /**< Physical memory area for fb. */
  55. static parea_t fb_parea;
  56.  
  57. static uint8_t *fb_addr;
  58. static uint8_t *backbuf;
  59. static uint8_t *glyphs;
  60.  
  61. static void *bgpixel;
  62.  
  63. static unsigned int xres;
  64. static unsigned int yres;
  65.  
  66. static unsigned int scanline;
  67. static unsigned int glyphscanline;
  68.  
  69. static unsigned int pixelbytes;
  70. static unsigned int glyphbytes;
  71.  
  72. static unsigned int cols;
  73. static unsigned int rows;
  74. static unsigned int position = 0;
  75.  
  76. #define BG_COLOR     0x000080
  77. #define FG_COLOR     0xffff00
  78.  
  79. #define CURSOR       219
  80.  
  81. #define RED(x, bits)         ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
  82. #define GREEN(x, bits)       ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
  83. #define BLUE(x, bits)        ((x >> (8 - bits)) & ((1 << bits) - 1))
  84.  
  85. #define COL2X(col)           ((col) * FONT_WIDTH)
  86. #define ROW2Y(row)           ((row) * FONT_SCANLINES)
  87.  
  88. #define X2COL(x)             ((x) / FONT_WIDTH)
  89. #define Y2ROW(y)             ((y) / FONT_SCANLINES)
  90.  
  91. #define FB_POS(x, y)         ((y) * scanline + (x) * pixelbytes)
  92. #define BB_POS(col, row)     ((row) * cols + (col))
  93. #define GLYPH_POS(glyph, y)  ((glyph) * glyphbytes + (y) * glyphscanline)
  94.  
  95.  
  96. static void (*rgb_conv)(void *, uint32_t);
  97.  
  98.  
  99. /** ARGB 8:8:8:8 conversion
  100.  *
  101.  */
  102. static void rgb_0888(void *dst, uint32_t rgb)
  103. {
  104.     *((uint32_t *) dst) = rgb & 0xffffff;
  105. }
  106.  
  107.  
  108. /** ABGR 8:8:8:8 conversion
  109.  *
  110.  */
  111. static void bgr_0888(void *dst, uint32_t rgb)
  112. {
  113.     *((uint32_t *) dst)
  114.         = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
  115. }
  116.  
  117.  
  118. /** BGR 8:8:8 conversion
  119.  *
  120.  */
  121. static void rgb_888(void *dst, uint32_t rgb)
  122. {
  123. #if defined(FB_INVERT_ENDIAN)
  124.     *((uint32_t *) dst)
  125.         = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8)
  126.         | (*((uint32_t *) dst) & 0xff0000);
  127. #else
  128.     *((uint32_t *) dst)
  129.         = (rgb & 0xffffff) | (*((uint32_t *) dst) & 0xff0000);
  130. #endif
  131. }
  132.  
  133.  
  134. /** RGB 5:5:5 conversion
  135.  *
  136.  */
  137. static void rgb_555(void *dst, uint32_t rgb)
  138. {
  139.     *((uint16_t *) dst)
  140.         = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
  141. }
  142.  
  143.  
  144. /** RGB 5:6:5 conversion
  145.  *
  146.  */
  147. static void rgb_565(void *dst, uint32_t rgb)
  148. {
  149.     *((uint16_t *) dst)
  150.         = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
  151. }
  152.  
  153.  
  154. /** RGB 3:2:3
  155.  *
  156.  * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
  157.  * will most likely use a color palette. The color appearance
  158.  * will be pretty random and depend on the default installed
  159.  * palette. This could be fixed by supporting custom palette
  160.  * and setting it to simulate the 8-bit truecolor.
  161.  *
  162.  * Currently we set the palette on the ia32, amd64 and sparc64 port.
  163.  *
  164.  * Note that the byte is being inverted by this function. The reason is
  165.  * that we would like to use a color palette where the white color code
  166.  * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
  167.  * use these codes for black and white and prevent to set codes
  168.  * 0 and 255 to other colors.
  169.  *
  170.  */
  171. static void rgb_323(void *dst, uint32_t rgb)
  172. {
  173.     *((uint8_t *) dst)
  174.         = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
  175. }
  176.  
  177.  
  178. /** Draw character at given position
  179.  *
  180.  */
  181. static void draw_glyph(uint8_t glyph, unsigned int col, unsigned int row)
  182. {
  183.     unsigned int x = COL2X(col);
  184.     unsigned int y = ROW2Y(row);
  185.     unsigned int yd;
  186.    
  187.     backbuf[BB_POS(col, row)] = glyph;
  188.    
  189.     for (yd = 0; yd < FONT_SCANLINES; yd++)
  190.         memcpy(&fb_addr[FB_POS(x, y + yd)],
  191.             &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
  192. }
  193.  
  194.  
  195. /** Scroll screen down by one row
  196.  *
  197.  *
  198.  */
  199. static void scroll_screen(void)
  200. {
  201.     unsigned int row;
  202.    
  203.     for (row = 0; row < rows; row++) {
  204.         unsigned int y = ROW2Y(row);
  205.         unsigned int yd;
  206.        
  207.         for (yd = 0; yd < FONT_SCANLINES; yd++) {
  208.             unsigned int x;
  209.             unsigned int col;
  210.            
  211.             for (col = 0, x = 0; col < cols; col++, x += FONT_WIDTH) {
  212.                 uint8_t glyph;
  213.                
  214.                 if (row < rows - 1) {
  215.                     if (backbuf[BB_POS(col, row)] == backbuf[BB_POS(col, row + 1)])
  216.                         continue;
  217.                    
  218.                     glyph = backbuf[BB_POS(col, row + 1)];
  219.                 } else
  220.                     glyph = 0;
  221.                
  222.                 memcpy(&fb_addr[FB_POS(x, y + yd)],
  223.                     &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
  224.             }
  225.         }
  226.     }
  227.    
  228.     memcpy(backbuf, backbuf + cols, cols * (rows - 1));
  229.     memsetb(&backbuf[BB_POS(0, rows - 1)], cols, 0);
  230. }
  231.  
  232.  
  233. static void cursor_put(void)
  234. {
  235.     draw_glyph(CURSOR, position % cols, position / cols);
  236. }
  237.  
  238.  
  239. static void cursor_remove(void)
  240. {
  241.     draw_glyph(0, position % cols, position / cols);
  242. }
  243.  
  244.  
  245. /** Print character to screen
  246.  *
  247.  * Emulate basic terminal commands.
  248.  *
  249.  */
  250. static void fb_putchar(chardev_t *dev, char ch)
  251. {
  252.     spinlock_lock(&fb_lock);
  253.    
  254.     switch (ch) {
  255.     case '\n':
  256.         cursor_remove();
  257.         position += cols;
  258.         position -= position % cols;
  259.         break;
  260.     case '\r':
  261.         cursor_remove();
  262.         position -= position % cols;
  263.         break;
  264.     case '\b':
  265.         cursor_remove();
  266.         if (position % cols)
  267.             position--;
  268.         break;
  269.     case '\t':
  270.         cursor_remove();
  271.         do {
  272.             draw_glyph((uint8_t) ' ', position % cols, position / cols);
  273.             position++;
  274.         } while ((position % 8) && (position < cols * rows));
  275.         break;
  276.     default:
  277.         draw_glyph((uint8_t) ch, position % cols, position / cols);
  278.         position++;
  279.     }
  280.    
  281.     if (position >= cols * rows) {
  282.         position -= cols;
  283.         scroll_screen();
  284.     }
  285.    
  286.     cursor_put();
  287.    
  288.     spinlock_unlock(&fb_lock);
  289. }
  290.  
  291. static chardev_t framebuffer;
  292. static chardev_operations_t fb_ops = {
  293.     .write = fb_putchar,
  294. };
  295.  
  296.  
  297. /** Render glyphs
  298.  *
  299.  * Convert glyphs from device independent font
  300.  * description to current visual representation.
  301.  *
  302.  */
  303. static void render_glyphs(void)
  304. {
  305.     unsigned int glyph;
  306.    
  307.     for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
  308.         unsigned int y;
  309.        
  310.         for (y = 0; y < FONT_SCANLINES; y++) {
  311.             unsigned int x;
  312.            
  313.             for (x = 0; x < FONT_WIDTH; x++)
  314.                 rgb_conv(&glyphs[GLYPH_POS(glyph, y) + x * pixelbytes],
  315.                     (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x))) ? FG_COLOR : BG_COLOR);
  316.         }
  317.     }
  318.    
  319.     rgb_conv(bgpixel, BG_COLOR);
  320. }
  321.  
  322.  
  323. /** Refresh the screen
  324.  *
  325.  */
  326. void fb_redraw(void)
  327. {
  328.     unsigned int row;
  329.    
  330.     for (row = 0; row < rows; row++) {
  331.         unsigned int y = ROW2Y(row);
  332.         unsigned int yd;
  333.        
  334.         for (yd = 0; yd < FONT_SCANLINES; yd++) {
  335.             unsigned int x;
  336.             unsigned int col;
  337.            
  338.             for (col = 0, x = 0; col < cols; col++, x += FONT_WIDTH)
  339.                 memcpy(&fb_addr[FB_POS(x, y + yd)],
  340.                 &glyphs[GLYPH_POS(backbuf[BB_POS(col, row)], yd)],
  341.                 glyphscanline);
  342.         }
  343.     }
  344.    
  345.     if (COL2X(cols) < xres) {
  346.         unsigned int y;
  347.        
  348.         for (y = 0; y < yres; y++) {
  349.             unsigned int x;
  350.            
  351.             for (x = COL2X(cols); x < xres; x++)
  352.                 memcpy(&fb_addr[FB_POS(x, y)], bgpixel, pixelbytes);
  353.         }
  354.     }
  355.    
  356.     if (ROW2Y(rows) < yres) {
  357.         unsigned int y;
  358.        
  359.         for (y = ROW2Y(rows); y < yres; y++) {
  360.             unsigned int x;
  361.            
  362.             for (x = 0; x < xres; x++)
  363.                 memcpy(&fb_addr[FB_POS(x, y)], bgpixel, pixelbytes);
  364.         }
  365.     }
  366. }
  367.  
  368.  
  369. /** Initialize framebuffer as a chardev output device
  370.  *
  371.  * @param addr   Physical address of the framebuffer
  372.  * @param x      Screen width in pixels
  373.  * @param y      Screen height in pixels
  374.  * @param scan   Bytes per one scanline
  375.  * @param visual Color model
  376.  *
  377.  */
  378. void fb_init(fb_properties_t *props)
  379. {
  380.     switch (props->visual) {
  381.     case VISUAL_INDIRECT_8:
  382.         rgb_conv = rgb_323;
  383.         pixelbytes = 1;
  384.         break;
  385.     case VISUAL_RGB_5_5_5:
  386.         rgb_conv = rgb_555;
  387.         pixelbytes = 2;
  388.         break;
  389.     case VISUAL_RGB_5_6_5:
  390.         rgb_conv = rgb_565;
  391.         pixelbytes = 2;
  392.         break;
  393.     case VISUAL_RGB_8_8_8:
  394.         rgb_conv = rgb_888;
  395.         pixelbytes = 3;
  396.         break;
  397.     case VISUAL_RGB_8_8_8_0:
  398.         rgb_conv = rgb_888;
  399.         pixelbytes = 4;
  400.         break;
  401.     case VISUAL_RGB_0_8_8_8:
  402.         rgb_conv = rgb_0888;
  403.         pixelbytes = 4;
  404.         break;
  405.     case VISUAL_BGR_0_8_8_8:
  406.         rgb_conv = bgr_0888;
  407.         pixelbytes = 4;
  408.         break;
  409.     default:
  410.         panic("Unsupported visual.\n");
  411.     }
  412.    
  413.     xres = props->x;
  414.     yres = props->y;
  415.     scanline = props->scan;
  416.    
  417.     cols = xres / FONT_WIDTH;
  418.     rows = yres / FONT_SCANLINES;
  419.    
  420.     glyphscanline = FONT_WIDTH * pixelbytes;
  421.     glyphbytes = glyphscanline * FONT_SCANLINES;
  422.    
  423.     unsigned int fbsize = scanline * yres;
  424.     unsigned int bbsize = cols * rows;
  425.     unsigned int glyphsize = FONT_GLYPHS * glyphbytes;
  426.    
  427.     backbuf = (uint8_t *) malloc(bbsize, 0);
  428.     if (!backbuf)
  429.         panic("Unable to allocate backbuffer.\n");
  430.    
  431.     glyphs = (uint8_t *) malloc(glyphsize, 0);
  432.     if (!glyphs)
  433.         panic("Unable to allocate glyphs.\n");
  434.    
  435.     bgpixel = malloc(pixelbytes, 0);
  436.     if (!bgpixel)
  437.         panic("Unable to allocate background pixel.\n");
  438.    
  439.     memsetb(backbuf, bbsize, 0);
  440.     memsetb(glyphs, glyphsize, 0);
  441.     memsetb(bgpixel, pixelbytes, 0);
  442.    
  443.     render_glyphs();
  444.    
  445.     fb_addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize);
  446.    
  447.     fb_parea.pbase = (uintptr_t) props->addr + props->offset;
  448.     fb_parea.vbase = (uintptr_t) fb_addr;
  449.     fb_parea.frames = SIZE2FRAMES(fbsize);
  450.     fb_parea.cacheable = false;
  451.     ddi_parea_register(&fb_parea);
  452.    
  453.     sysinfo_set_item_val("fb", NULL, true);
  454.     sysinfo_set_item_val("fb.kind", NULL, 1);
  455.     sysinfo_set_item_val("fb.width", NULL, xres);
  456.     sysinfo_set_item_val("fb.height", NULL, yres);
  457.     sysinfo_set_item_val("fb.scanline", NULL, scanline);
  458.     sysinfo_set_item_val("fb.visual", NULL, props->visual);
  459.     sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
  460.    
  461.     fb_redraw();
  462.    
  463.     chardev_initialize("fb", &framebuffer, &fb_ops);
  464.     stdout = &framebuffer;
  465. }
  466.  
  467. /** @}
  468.  */
  469.