Subversion Repositories HelenOS

Rev

Rev 3692 | Rev 3709 | 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 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 (SunBlade 1500)
  167.  * use these codes for black and white and prevent to set codes
  168.  * 0 and 255 to other colors.
  169.  */
  170. static void rgb_323(void *dst, uint32_t rgb)
  171. {
  172.     *((uint8_t *) dst)
  173.         = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
  174. }
  175.  
  176.  
  177. /** Draw character at given position
  178.  *
  179.  */
  180. static void draw_glyph(uint8_t glyph, unsigned int col, unsigned int row)
  181. {
  182.     unsigned int x = COL2X(col);
  183.     unsigned int y = ROW2Y(row);
  184.     unsigned int yd;
  185.    
  186.     backbuf[BB_POS(col, row)] = glyph;
  187.    
  188.     for (yd = 0; yd < FONT_SCANLINES; yd++)
  189.         memcpy(&fb_addr[FB_POS(x, y + yd)],
  190.             &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
  191. }
  192.  
  193.  
  194. /** Scroll screen down by one row
  195.  *
  196.  *
  197.  */
  198. static void scroll_screen(void)
  199. {
  200.     unsigned int row;
  201.    
  202.     for (row = 0; row < rows; row++) {
  203.         unsigned int y = ROW2Y(row);
  204.         unsigned int yd;
  205.        
  206.         for (yd = 0; yd < FONT_SCANLINES; yd++) {
  207.             unsigned int x;
  208.             unsigned int col;
  209.            
  210.             for (col = 0, x = 0; col < cols; col++, x += FONT_WIDTH) {
  211.                 uint8_t glyph;
  212.                
  213.                 if (row < rows - 1) {
  214.                     if (backbuf[BB_POS(col, row)] == backbuf[BB_POS(col, row + 1)])
  215.                         continue;
  216.                    
  217.                     glyph = backbuf[BB_POS(col, row + 1)];
  218.                 } else
  219.                     glyph = 0;
  220.                
  221.                 memcpy(&fb_addr[FB_POS(x, y + yd)],
  222.                     &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
  223.             }
  224.         }
  225.     }
  226.    
  227.     memcpy(backbuf, backbuf + cols, cols * (rows - 1));
  228.     memsetb(&backbuf[BB_POS(0, rows - 1)], cols, 0);
  229. }
  230.  
  231.  
  232. static void cursor_put(void)
  233. {
  234.     draw_glyph(CURSOR, position % cols, position / cols);
  235. }
  236.  
  237.  
  238. static void cursor_remove(void)
  239. {
  240.     draw_glyph(0, position % cols, position / cols);
  241. }
  242.  
  243.  
  244. /** Print character to screen
  245.  *
  246.  * Emulate basic terminal commands.
  247.  *
  248.  */
  249. static void fb_putchar(chardev_t *dev, char ch)
  250. {
  251.     spinlock_lock(&fb_lock);
  252.    
  253.     switch (ch) {
  254.     case '\n':
  255.         cursor_remove();
  256.         position += cols;
  257.         position -= position % cols;
  258.         break;
  259.     case '\r':
  260.         cursor_remove();
  261.         position -= position % cols;
  262.         break;
  263.     case '\b':
  264.         cursor_remove();
  265.         if (position % cols)
  266.             position--;
  267.         break;
  268.     case '\t':
  269.         cursor_remove();
  270.         do {
  271.             draw_glyph((uint8_t) ' ', position % cols, position / cols);
  272.             position++;
  273.         } while ((position % 8) && (position < cols * rows));
  274.         break;
  275.     default:
  276.         draw_glyph((uint8_t) ch, position % cols, position / cols);
  277.         position++;
  278.     }
  279.    
  280.     if (position >= cols * rows) {
  281.         position -= cols;
  282.         scroll_screen();
  283.     }
  284.    
  285.     cursor_put();
  286.    
  287.     spinlock_unlock(&fb_lock);
  288. }
  289.  
  290. static chardev_t framebuffer;
  291. static chardev_operations_t fb_ops = {
  292.     .write = fb_putchar,
  293. };
  294.  
  295.  
  296. /** Render glyphs
  297.  *
  298.  * Convert glyphs from device independent font
  299.  * description to current visual representation.
  300.  *
  301.  */
  302. static void render_glyphs(void)
  303. {
  304.     unsigned int glyph;
  305.    
  306.     for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
  307.         unsigned int y;
  308.        
  309.         for (y = 0; y < FONT_SCANLINES; y++) {
  310.             unsigned int x;
  311.            
  312.             for (x = 0; x < FONT_WIDTH; x++)
  313.                 rgb_conv(&glyphs[GLYPH_POS(glyph, y) + x * pixelbytes],
  314.                     (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x))) ? FG_COLOR : BG_COLOR);
  315.         }
  316.     }
  317.    
  318.     rgb_conv(bgpixel, BG_COLOR);
  319. }
  320.  
  321.  
  322. /** Refresh the screen
  323.  *
  324.  */
  325. void fb_redraw(void)
  326. {
  327.     unsigned int row;
  328.    
  329.     for (row = 0; row < rows; row++) {
  330.         unsigned int y = ROW2Y(row);
  331.         unsigned int yd;
  332.        
  333.         for (yd = 0; yd < FONT_SCANLINES; yd++) {
  334.             unsigned int x;
  335.             unsigned int col;
  336.            
  337.             for (col = 0, x = 0; col < cols; col++, x += FONT_WIDTH)
  338.                 memcpy(&fb_addr[FB_POS(x, y + yd)],
  339.                 &glyphs[GLYPH_POS(backbuf[BB_POS(col, row)], yd)],
  340.                 glyphscanline);
  341.         }
  342.     }
  343.    
  344.     if (COL2X(cols) < xres) {
  345.         unsigned int y;
  346.        
  347.         for (y = 0; y < yres; y++) {
  348.             unsigned int x;
  349.            
  350.             for (x = COL2X(cols); x < xres; x++)
  351.                 memcpy(&fb_addr[FB_POS(x, y)], bgpixel, pixelbytes);
  352.         }
  353.     }
  354.    
  355.     if (ROW2Y(rows) < yres) {
  356.         unsigned int y;
  357.        
  358.         for (y = ROW2Y(rows); y < yres; y++) {
  359.             unsigned int x;
  360.            
  361.             for (x = 0; x < xres; x++)
  362.                 memcpy(&fb_addr[FB_POS(x, y)], bgpixel, pixelbytes);
  363.         }
  364.     }
  365. }
  366.  
  367.  
  368. /** Initialize framebuffer as a chardev output device
  369.  *
  370.  * @param addr   Physical address of the framebuffer
  371.  * @param x      Screen width in pixels
  372.  * @param y      Screen height in pixels
  373.  * @param scan   Bytes per one scanline
  374.  * @param visual Color model
  375.  *
  376.  */
  377. void fb_init(fb_properties_t *props)
  378. {
  379.     switch (props->visual) {
  380.     case VISUAL_INDIRECT_8:
  381.         rgb_conv = rgb_323;
  382.         pixelbytes = 1;
  383.         break;
  384.     case VISUAL_RGB_5_5_5:
  385.         rgb_conv = rgb_555;
  386.         pixelbytes = 2;
  387.         break;
  388.     case VISUAL_RGB_5_6_5:
  389.         rgb_conv = rgb_565;
  390.         pixelbytes = 2;
  391.         break;
  392.     case VISUAL_RGB_8_8_8:
  393.         rgb_conv = rgb_888;
  394.         pixelbytes = 3;
  395.         break;
  396.     case VISUAL_RGB_8_8_8_0:
  397.         rgb_conv = rgb_888;
  398.         pixelbytes = 4;
  399.         break;
  400.     case VISUAL_RGB_0_8_8_8:
  401.         rgb_conv = rgb_0888;
  402.         pixelbytes = 4;
  403.         break;
  404.     case VISUAL_BGR_0_8_8_8:
  405.         rgb_conv = bgr_0888;
  406.         pixelbytes = 4;
  407.         break;
  408.     default:
  409.         panic("Unsupported visual.\n");
  410.     }
  411.    
  412.     xres = props->x;
  413.     yres = props->y;
  414.     scanline = props->scan;
  415.    
  416.     cols = xres / FONT_WIDTH;
  417.     rows = yres / FONT_SCANLINES;
  418.    
  419.     glyphscanline = FONT_WIDTH * pixelbytes;
  420.     glyphbytes = glyphscanline * FONT_SCANLINES;
  421.    
  422.     unsigned int fbsize = scanline * yres;
  423.     unsigned int bbsize = cols * rows;
  424.     unsigned int glyphsize = FONT_GLYPHS * glyphbytes;
  425.    
  426.     backbuf = (uint8_t *) malloc(bbsize, 0);
  427.     if (!backbuf)
  428.         panic("Unable to allocate backbuffer.\n");
  429.    
  430.     glyphs = (uint8_t *) malloc(glyphsize, 0);
  431.     if (!glyphs)
  432.         panic("Unable to allocate glyphs.\n");
  433.    
  434.     bgpixel = malloc(pixelbytes, 0);
  435.     if (!bgpixel)
  436.         panic("Unable to allocate background pixel.\n");
  437.    
  438.     memsetb(backbuf, bbsize, 0);
  439.     memsetb(glyphs, glyphsize, 0);
  440.     memsetb(bgpixel, pixelbytes, 0);
  441.    
  442.     render_glyphs();
  443.    
  444.     fb_addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize);
  445.    
  446.     fb_parea.pbase = (uintptr_t) props->addr + props->offset;
  447.     fb_parea.vbase = (uintptr_t) fb_addr;
  448.     fb_parea.frames = SIZE2FRAMES(fbsize);
  449.     fb_parea.cacheable = false;
  450.     ddi_parea_register(&fb_parea);
  451.    
  452.     sysinfo_set_item_val("fb", NULL, true);
  453.     sysinfo_set_item_val("fb.kind", NULL, 1);
  454.     sysinfo_set_item_val("fb.width", NULL, xres);
  455.     sysinfo_set_item_val("fb.height", NULL, yres);
  456.     sysinfo_set_item_val("fb.scanline", NULL, scanline);
  457.     sysinfo_set_item_val("fb.visual", NULL, props->visual);
  458.     sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
  459.    
  460.     fb_redraw();
  461.    
  462.     chardev_initialize("fb", &framebuffer, &fb_ops);
  463.     stdout = &framebuffer;
  464. }
  465.  
  466. /** @}
  467.  */
  468.