source: mainline/kernel/genarch/src/fb/fb.c@ 8af9950

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8af9950 was 20eb5e4d, checked in by Pavel Rimsky <rimskyp@…>, 17 years ago

Added forgotten stuff related to framebuffers with offsets. Got rid of VISUAL_SB1500_PALETTE.

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