source: mainline/kernel/genarch/src/fb/fb.c@ f8fb03b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f8fb03b was ca645a2, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Slightly improve KFB scrolling speed

  • Property mode set to 100644
File size: 17.7 KB
Line 
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 kernel_genarch
31 * @{
32 */
33/** @file
34 */
35
36#include <assert.h>
37#include <debug.h>
38#include <genarch/fb/font-8x16.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/km.h>
44#include <stdlib.h>
45#include <align.h>
46#include <panic.h>
47#include <mem.h>
48#include <config.h>
49#include <bitops.h>
50#include <str.h>
51#include <ddi/ddi.h>
52#include <typedefs.h>
53#include <byteorder.h>
54
55#define BG_COLOR 0x001620
56#define FG_COLOR 0xf3cf65
57#define INV_COLOR 0xaaaaaa
58
59#define FB_PAGES 8
60
61#define RED(x, bits) (((x) >> (8 + 8 + 8 - (bits))) & ((1 << (bits)) - 1))
62#define GREEN(x, bits) (((x) >> (8 + 8 - (bits))) & ((1 << (bits)) - 1))
63#define BLUE(x, bits) (((x) >> (8 - (bits))) & ((1 << (bits)) - 1))
64
65#define COL2X(col) ((col) * FONT_WIDTH)
66#define ROW2Y(row) ((row) * FONT_SCANLINES)
67
68#define X2COL(x) ((x) / FONT_WIDTH)
69#define Y2ROW(y) ((y) / FONT_SCANLINES)
70
71#define FB_POS(instance, x, y) \
72 ((y) * (instance)->scanline + (x) * (instance)->pixelbytes)
73
74#define BB_POS(instance, col, row) \
75 ((((instance)->start_row + (row)) % (instance)->rows) * \
76 (instance)->cols + (col))
77
78#define BB_NEXT_COL(pos) (++(pos))
79
80#define GLYPH_POS(instance, glyph, y) \
81 ((glyph) * (instance)->glyphbytes + (y) * (instance)->glyphscanline)
82
83typedef void (*rgb_conv_t)(void *, uint32_t);
84
85typedef struct {
86 SPINLOCK_DECLARE(lock);
87
88 parea_t parea;
89
90 uint8_t *addr;
91 uint16_t *backbuf;
92 uint8_t *glyphs;
93 uint8_t *bgscan;
94
95 rgb_conv_t rgb_conv;
96
97 unsigned int xres;
98 unsigned int yres;
99
100 /** Number of rows that fit on framebuffer */
101 unsigned int rowtrim;
102
103 unsigned int scanline;
104 unsigned int glyphscanline;
105
106 unsigned int pixelbytes;
107 unsigned int glyphbytes;
108 unsigned int bgscanbytes;
109
110 /** Number of columns in the backbuffer */
111 unsigned int cols;
112 /** Number of rows in the backbuffer */
113 unsigned int rows;
114
115 /** Starting row in the cyclic backbuffer */
116 unsigned int start_row;
117
118 /** Top-most visible row (relative to start_row) */
119 unsigned int offset_row;
120
121 /** Current backbuffer position */
122 unsigned int position;
123} fb_instance_t;
124
125static void fb_putwchar(outdev_t *, wchar_t);
126static void fb_redraw(outdev_t *);
127static void fb_scroll_up(outdev_t *);
128static void fb_scroll_down(outdev_t *);
129
130static outdev_operations_t fbdev_ops = {
131 .write = fb_putwchar,
132 .redraw = fb_redraw,
133 .scroll_up = fb_scroll_up,
134 .scroll_down = fb_scroll_down
135};
136
137/*
138 * RGB conversion functions.
139 *
140 * These functions write an RGB value to some memory in some predefined format.
141 * The naming convention corresponds to the format created by these functions.
142 * The functions use the so called network order (i.e. big endian) with respect
143 * to their names.
144 */
145
146static void rgb_0888(void *dst, uint32_t rgb)
147{
148 *((uint32_t *) dst) = host2uint32_t_be((0 << 24) |
149 (RED(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | (BLUE(rgb, 8)));
150}
151
152static void bgr_0888(void *dst, uint32_t rgb)
153{
154 *((uint32_t *) dst) = host2uint32_t_be((0 << 24) |
155 (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | (RED(rgb, 8)));
156}
157
158static void rgb_8880(void *dst, uint32_t rgb)
159{
160 *((uint32_t *) dst) = host2uint32_t_be((RED(rgb, 8) << 24) |
161 (GREEN(rgb, 8) << 16) | (BLUE(rgb, 8) << 8) | 0);
162}
163
164static void bgr_8880(void *dst, uint32_t rgb)
165{
166 *((uint32_t *) dst) = host2uint32_t_be((BLUE(rgb, 8) << 24) |
167 (GREEN(rgb, 8) << 16) | (RED(rgb, 8) << 8) | 0);
168}
169
170static void rgb_888(void *dst, uint32_t rgb)
171{
172 ((uint8_t *) dst)[0] = RED(rgb, 8);
173 ((uint8_t *) dst)[1] = GREEN(rgb, 8);
174 ((uint8_t *) dst)[2] = BLUE(rgb, 8);
175}
176
177static void bgr_888(void *dst, uint32_t rgb)
178{
179 ((uint8_t *) dst)[0] = BLUE(rgb, 8);
180 ((uint8_t *) dst)[1] = GREEN(rgb, 8);
181 ((uint8_t *) dst)[2] = RED(rgb, 8);
182}
183
184static void rgb_555_be(void *dst, uint32_t rgb)
185{
186 *((uint16_t *) dst) = host2uint16_t_be(RED(rgb, 5) << 10 |
187 GREEN(rgb, 5) << 5 | BLUE(rgb, 5));
188}
189
190static void rgb_555_le(void *dst, uint32_t rgb)
191{
192 *((uint16_t *) dst) = host2uint16_t_le(RED(rgb, 5) << 10 |
193 GREEN(rgb, 5) << 5 | BLUE(rgb, 5));
194}
195
196static void rgb_565_be(void *dst, uint32_t rgb)
197{
198 *((uint16_t *) dst) = host2uint16_t_be(RED(rgb, 5) << 11 |
199 GREEN(rgb, 6) << 5 | BLUE(rgb, 5));
200}
201
202static void rgb_565_le(void *dst, uint32_t rgb)
203{
204 *((uint16_t *) dst) = host2uint16_t_le(RED(rgb, 5) << 11 |
205 GREEN(rgb, 6) << 5 | BLUE(rgb, 5));
206}
207
208/** BGR 3:2:3
209 *
210 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
211 * will most likely use a color palette. The color appearance
212 * will be pretty random and depend on the default installed
213 * palette. This could be fixed by supporting custom palette
214 * and setting it to simulate the 8-bit truecolor.
215 *
216 * Currently we set the palette on the ia32, amd64, ppc32 and sparc64 port.
217 *
218 * Note that the byte is being inverted by this function. The reason is
219 * that we would like to use a color palette where the white color code
220 * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
221 * use these codes for black and white and prevent to set codes
222 * 0 and 255 to other colors.
223 *
224 */
225static void bgr_323(void *dst, uint32_t rgb)
226{
227 *((uint8_t *) dst) =
228 ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
229}
230
231/** Draw character at given position
232 *
233 */
234static void glyph_draw(fb_instance_t *instance, uint16_t glyph,
235 unsigned int col, unsigned int row, bool overlay)
236{
237 if (!overlay)
238 instance->backbuf[BB_POS(instance, col, row)] = glyph;
239
240 /* Do not output if the framebuffer is used by user space */
241 if ((instance->parea.mapped) && (!console_override))
242 return;
243
244 /* Check whether the glyph should be visible */
245 if (row < instance->offset_row)
246 return;
247
248 unsigned int rel_row = row - instance->offset_row;
249 if (rel_row >= instance->rowtrim)
250 return;
251
252 unsigned int x = COL2X(col);
253 unsigned int y = ROW2Y(rel_row);
254
255 for (unsigned int yd = 0; yd < FONT_SCANLINES; yd++)
256 memcpy(&instance->addr[FB_POS(instance, x, y + yd)],
257 &instance->glyphs[GLYPH_POS(instance, glyph, yd)],
258 instance->glyphscanline);
259}
260
261/** Scroll screen down by one row
262 *
263 */
264static void screen_scroll(fb_instance_t *instance)
265{
266 if ((!instance->parea.mapped) || (console_override)) {
267 for (unsigned int rel_row = 0; rel_row < instance->rowtrim; rel_row++) {
268 unsigned int y = ROW2Y(rel_row);
269 unsigned int row = rel_row + instance->offset_row;
270
271 for (unsigned int yd = 0; yd < FONT_SCANLINES; yd++) {
272 unsigned int x;
273 unsigned int col;
274 size_t bb_pos = BB_POS(instance, 0, row);
275 size_t bb_pos1 = BB_POS(instance, 0, row + 1);
276
277 for (col = 0, x = 0; col < instance->cols;
278 col++, x += FONT_WIDTH) {
279 uint16_t glyph;
280
281 if (row < instance->rows - 1) {
282 if (instance->backbuf[bb_pos] ==
283 instance->backbuf[bb_pos1])
284 goto skip;
285
286 glyph = instance->backbuf[bb_pos1];
287 } else
288 glyph = 0;
289
290 memcpy(&instance->addr[FB_POS(instance, x, y + yd)],
291 &instance->glyphs[GLYPH_POS(instance, glyph, yd)],
292 instance->glyphscanline);
293 skip:
294 BB_NEXT_COL(bb_pos);
295 BB_NEXT_COL(bb_pos1);
296 }
297 }
298 }
299 }
300
301 /*
302 * Implement backbuffer scrolling by wrapping around
303 * the cyclic buffer.
304 */
305
306 instance->start_row++;
307 if (instance->start_row == instance->rows)
308 instance->start_row = 0;
309
310 memsetw(&instance->backbuf[BB_POS(instance, 0, instance->rows - 1)],
311 instance->cols, 0);
312}
313
314static void cursor_put(fb_instance_t *instance)
315{
316 unsigned int col = instance->position % instance->cols;
317 unsigned int row = instance->position / instance->cols;
318
319 glyph_draw(instance, fb_font_glyph(U_CURSOR), col, row, true);
320}
321
322static void cursor_remove(fb_instance_t *instance)
323{
324 unsigned int col = instance->position % instance->cols;
325 unsigned int row = instance->position / instance->cols;
326
327 glyph_draw(instance, instance->backbuf[BB_POS(instance, col, row)],
328 col, row, true);
329}
330
331/** Render glyphs
332 *
333 * Convert glyphs from device independent font
334 * description to current visual representation.
335 *
336 */
337static void glyphs_render(fb_instance_t *instance)
338{
339 /* Prerender glyphs */
340 uint16_t glyph;
341
342 for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
343 uint32_t fg_color;
344
345 if (glyph == FONT_GLYPHS - 1)
346 fg_color = INV_COLOR;
347 else
348 fg_color = FG_COLOR;
349
350 unsigned int y;
351
352 for (y = 0; y < FONT_SCANLINES; y++) {
353 unsigned int x;
354
355 for (x = 0; x < FONT_WIDTH; x++) {
356 void *dst =
357 &instance->glyphs[GLYPH_POS(instance, glyph, y) +
358 x * instance->pixelbytes];
359 uint32_t rgb = (fb_font[glyph][y] &
360 (1 << (7 - x))) ? fg_color : BG_COLOR;
361 instance->rgb_conv(dst, rgb);
362 }
363 }
364 }
365
366 /* Prerender background scanline */
367 unsigned int x;
368
369 for (x = 0; x < instance->xres; x++)
370 instance->rgb_conv(&instance->bgscan[x * instance->pixelbytes], BG_COLOR);
371}
372
373static void fb_redraw_internal(fb_instance_t *instance)
374{
375 for (unsigned int rel_row = 0; rel_row < instance->rowtrim; rel_row++) {
376 unsigned int y = ROW2Y(rel_row);
377 unsigned int row = rel_row + instance->offset_row;
378
379 for (unsigned int yd = 0; yd < FONT_SCANLINES; yd++) {
380 unsigned int x;
381 unsigned int col;
382 size_t bb_pos = BB_POS(instance, 0, row);
383
384 for (col = 0, x = 0; col < instance->cols;
385 col++, x += FONT_WIDTH) {
386 uint16_t glyph =
387 instance->backbuf[bb_pos];
388 void *dst = &instance->addr[FB_POS(instance, x, y + yd)];
389 void *src = &instance->glyphs[GLYPH_POS(instance, glyph, yd)];
390 memcpy(dst, src, instance->glyphscanline);
391 BB_NEXT_COL(bb_pos);
392 }
393 }
394 }
395
396 if (COL2X(instance->cols) < instance->xres) {
397 unsigned int y;
398 unsigned int size =
399 (instance->xres - COL2X(instance->cols)) * instance->pixelbytes;
400
401 for (y = 0; y < instance->yres; y++)
402 memcpy(&instance->addr[FB_POS(instance, COL2X(instance->cols), y)],
403 instance->bgscan, size);
404 }
405
406 if (ROW2Y(instance->rowtrim) < instance->yres) {
407 unsigned int y;
408
409 for (y = ROW2Y(instance->rowtrim); y < instance->yres; y++)
410 memcpy(&instance->addr[FB_POS(instance, 0, y)],
411 instance->bgscan, instance->bgscanbytes);
412 }
413}
414
415/** Print character to screen
416 *
417 * Emulate basic terminal commands.
418 *
419 */
420static void fb_putwchar(outdev_t *dev, wchar_t ch)
421{
422 fb_instance_t *instance = (fb_instance_t *) dev->data;
423 spinlock_lock(&instance->lock);
424
425 switch (ch) {
426 case '\n':
427 cursor_remove(instance);
428 instance->position += instance->cols;
429 instance->position -= instance->position % instance->cols;
430 break;
431 case '\r':
432 cursor_remove(instance);
433 instance->position -= instance->position % instance->cols;
434 break;
435 case '\b':
436 cursor_remove(instance);
437 if (instance->position % instance->cols)
438 instance->position--;
439 break;
440 case '\t':
441 cursor_remove(instance);
442 do {
443 glyph_draw(instance, fb_font_glyph(' '),
444 instance->position % instance->cols,
445 instance->position / instance->cols, false);
446 instance->position++;
447 } while (((instance->position % instance->cols) % 8 != 0) &&
448 (instance->position < instance->cols * instance->rows));
449 break;
450 default:
451 glyph_draw(instance, fb_font_glyph(ch),
452 instance->position % instance->cols,
453 instance->position / instance->cols, false);
454 instance->position++;
455 }
456
457 if (instance->position >= instance->cols * instance->rows) {
458 instance->position -= instance->cols;
459 screen_scroll(instance);
460 }
461
462 cursor_put(instance);
463
464 spinlock_unlock(&instance->lock);
465}
466
467/** Scroll the framebuffer up
468 *
469 */
470static void fb_scroll_up(outdev_t *dev)
471{
472 fb_instance_t *instance = (fb_instance_t *) dev->data;
473 spinlock_lock(&instance->lock);
474
475 if (instance->offset_row >= instance->rowtrim / 2)
476 instance->offset_row -= instance->rowtrim / 2;
477 else
478 instance->offset_row = 0;
479
480 fb_redraw_internal(instance);
481 cursor_put(instance);
482
483 spinlock_unlock(&instance->lock);
484}
485
486/** Scroll the framebuffer down
487 *
488 */
489static void fb_scroll_down(outdev_t *dev)
490{
491 fb_instance_t *instance = (fb_instance_t *) dev->data;
492 spinlock_lock(&instance->lock);
493
494 if (instance->offset_row + instance->rowtrim / 2 <=
495 instance->rows - instance->rowtrim)
496 instance->offset_row += instance->rowtrim / 2;
497 else
498 instance->offset_row = instance->rows - instance->rowtrim;
499
500 fb_redraw_internal(instance);
501 cursor_put(instance);
502
503 spinlock_unlock(&instance->lock);
504}
505
506/** Refresh the screen
507 *
508 */
509static void fb_redraw(outdev_t *dev)
510{
511 fb_instance_t *instance = (fb_instance_t *) dev->data;
512
513 spinlock_lock(&instance->lock);
514 fb_redraw_internal(instance);
515 spinlock_unlock(&instance->lock);
516}
517
518/** Initialize framebuffer as a output character device
519 *
520 */
521outdev_t *fb_init(fb_properties_t *props)
522{
523 assert(props);
524 assert(props->x > 0);
525 assert(props->y > 0);
526 assert(props->scan > 0);
527
528 rgb_conv_t rgb_conv;
529 unsigned int pixelbytes;
530
531 switch (props->visual) {
532 case VISUAL_INDIRECT_8:
533 rgb_conv = bgr_323;
534 pixelbytes = 1;
535 break;
536 case VISUAL_RGB_5_5_5_LE:
537 rgb_conv = rgb_555_le;
538 pixelbytes = 2;
539 break;
540 case VISUAL_RGB_5_5_5_BE:
541 rgb_conv = rgb_555_be;
542 pixelbytes = 2;
543 break;
544 case VISUAL_RGB_5_6_5_LE:
545 rgb_conv = rgb_565_le;
546 pixelbytes = 2;
547 break;
548 case VISUAL_RGB_5_6_5_BE:
549 rgb_conv = rgb_565_be;
550 pixelbytes = 2;
551 break;
552 case VISUAL_RGB_8_8_8:
553 rgb_conv = rgb_888;
554 pixelbytes = 3;
555 break;
556 case VISUAL_BGR_8_8_8:
557 rgb_conv = bgr_888;
558 pixelbytes = 3;
559 break;
560 case VISUAL_RGB_8_8_8_0:
561 rgb_conv = rgb_8880;
562 pixelbytes = 4;
563 break;
564 case VISUAL_RGB_0_8_8_8:
565 rgb_conv = rgb_0888;
566 pixelbytes = 4;
567 break;
568 case VISUAL_BGR_0_8_8_8:
569 rgb_conv = bgr_0888;
570 pixelbytes = 4;
571 break;
572 case VISUAL_BGR_8_8_8_0:
573 rgb_conv = bgr_8880;
574 pixelbytes = 4;
575 break;
576 default:
577 LOG("Unsupported visual.");
578 return NULL;
579 }
580
581 outdev_t *fbdev = malloc(sizeof(outdev_t));
582 if (!fbdev)
583 return NULL;
584
585 fb_instance_t *instance = malloc(sizeof(fb_instance_t));
586 if (!instance) {
587 free(fbdev);
588 return NULL;
589 }
590
591 outdev_initialize("fbdev", fbdev, &fbdev_ops);
592 fbdev->data = instance;
593
594 spinlock_initialize(&instance->lock, "*fb.instance.lock");
595
596 instance->rgb_conv = rgb_conv;
597 instance->pixelbytes = pixelbytes;
598 instance->xres = props->x;
599 instance->yres = props->y;
600 instance->scanline = props->scan;
601
602 instance->rowtrim = Y2ROW(instance->yres);
603
604 instance->cols = X2COL(instance->xres);
605 instance->rows = FB_PAGES * instance->rowtrim;
606
607 instance->start_row = instance->rows - instance->rowtrim;
608 instance->offset_row = instance->start_row;
609 instance->position = instance->start_row * instance->cols;
610
611 instance->glyphscanline = FONT_WIDTH * instance->pixelbytes;
612 instance->glyphbytes = ROW2Y(instance->glyphscanline);
613 instance->bgscanbytes = instance->xres * instance->pixelbytes;
614
615 size_t fbsize = instance->scanline * instance->yres;
616 size_t bbsize = instance->cols * instance->rows * sizeof(uint16_t);
617 size_t glyphsize = FONT_GLYPHS * instance->glyphbytes;
618
619 instance->addr = (uint8_t *) km_map((uintptr_t) props->addr, fbsize,
620 KM_NATURAL_ALIGNMENT, PAGE_WRITE | PAGE_NOT_CACHEABLE);
621 if (!instance->addr) {
622 LOG("Unable to map framebuffer.");
623 free(instance);
624 free(fbdev);
625 return NULL;
626 }
627
628 instance->backbuf = (uint16_t *) malloc(bbsize);
629 if (!instance->backbuf) {
630 LOG("Unable to allocate backbuffer.");
631 free(instance);
632 free(fbdev);
633 return NULL;
634 }
635
636 instance->glyphs = (uint8_t *) malloc(glyphsize);
637 if (!instance->glyphs) {
638 LOG("Unable to allocate glyphs.");
639 free(instance->backbuf);
640 free(instance);
641 free(fbdev);
642 return NULL;
643 }
644
645 instance->bgscan = malloc(instance->bgscanbytes);
646 if (!instance->bgscan) {
647 LOG("Unable to allocate background pixel.");
648 free(instance->glyphs);
649 free(instance->backbuf);
650 free(instance);
651 free(fbdev);
652 return NULL;
653 }
654
655 memsetw(instance->backbuf, instance->cols * instance->rows, 0);
656 glyphs_render(instance);
657
658 ddi_parea_init(&instance->parea);
659 instance->parea.pbase = props->addr;
660 instance->parea.frames = SIZE2FRAMES(fbsize);
661 instance->parea.unpriv = false;
662 instance->parea.mapped = false;
663 ddi_parea_register(&instance->parea);
664
665 if (!fb_exported) {
666 /*
667 * We export the kernel framebuffer for uspace usage.
668 * This is used in the case the uspace framebuffer
669 * driver is not self-sufficient.
670 */
671 sysinfo_set_item_val("fb", NULL, true);
672 sysinfo_set_item_val("fb.kind", NULL, 1);
673 sysinfo_set_item_val("fb.width", NULL, instance->xres);
674 sysinfo_set_item_val("fb.height", NULL, instance->yres);
675 sysinfo_set_item_val("fb.scanline", NULL, instance->scanline);
676 sysinfo_set_item_val("fb.visual", NULL, props->visual);
677 sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
678
679 fb_exported = true;
680 }
681
682 fb_redraw(fbdev);
683 return fbdev;
684}
685
686/** @}
687 */
Note: See TracBrowser for help on using the repository browser.