2
* Copyright (c) 2008 Martin Decky
3
* Copyright (c) 2006 Jakub Vana
4
* Copyright (c) 2006 Ondrej Palkovsky
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions
11
* - Redistributions of source code must retain the above copyright
12
* notice, this list of conditions and the following disclaimer.
13
* - Redistributions in binary form must reproduce the above copyright
14
* notice, this list of conditions and the following disclaimer in the
15
* documentation and/or other materials provided with the distribution.
16
* - The name of the author may not be used to endorse or promote products
17
* derived from this software without specific prior written permission.
19
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
* @defgroup fb Graphical framebuffer
33
* @brief HelenOS graphical framebuffer.
51
#include <ipc/services.h>
52
#include <kernel/errno.h>
53
#include <kernel/genarch/fb/visuals.h>
60
#include <byteorder.h>
62
#include "font-8x16.h"
65
#include "../console/screenbuffer.h"
68
#include "pointer.xbm"
69
#include "pointer_mask.xbm"
71
#define DEFAULT_BGCOLOR 0xf0f0f0
72
#define DEFAULT_FGCOLOR 0x000000
74
#define GLYPH_UNAVAIL '?'
76
#define MAX_ANIM_LEN 8
77
#define MAX_ANIMATIONS 4
78
#define MAX_PIXMAPS 256 /**< Maximum number of saved pixmaps */
79
#define MAX_VIEWPORTS 128 /**< Viewport is a rectangular area on the screen */
81
/** Function to render a pixel from a RGB value. */
82
typedef void (*rgb_conv_t)(void *, uint32_t);
84
/** Function to render a bit mask. */
85
typedef void (*mask_conv_t)(void *, bool);
87
/** Function to draw a glyph. */
88
typedef void (*dg_t)(unsigned int x, unsigned int y, bool cursor,
89
uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color);
97
unsigned int scanline;
98
unsigned int glyphscanline;
100
unsigned int pixelbytes;
101
unsigned int glyphbytes;
103
/** Pre-rendered mask for rendering glyphs. Specific for the visual. */
107
mask_conv_t mask_conv;
110
/** Backbuffer character cell. */
124
/* Text support in window */
129
* Style and glyphs for text printing
132
/** Current attributes. */
138
* Glyph drawing function for this viewport. Different viewports
139
* might use different drawing functions depending on whether their
140
* scanlines are aligned on a word boundary.
144
/* Auto-cursor position */
146
unsigned int cur_col;
147
unsigned int cur_row;
161
unsigned int animlen;
162
unsigned int pixmaps[MAX_ANIM_LEN];
165
static animation_t animations[MAX_ANIMATIONS];
166
static bool anims_enabled;
174
static pixmap_t pixmaps[MAX_PIXMAPS];
175
static viewport_t viewports[128];
177
static bool client_connected = false; /**< Allow only 1 connection */
179
static uint32_t color_table[16] = {
180
[COLOR_BLACK] = 0x000000,
181
[COLOR_BLUE] = 0x0000f0,
182
[COLOR_GREEN] = 0x00f000,
183
[COLOR_CYAN] = 0x00f0f0,
184
[COLOR_RED] = 0xf00000,
185
[COLOR_MAGENTA] = 0xf000f0,
186
[COLOR_YELLOW] = 0xf0f000,
187
[COLOR_WHITE] = 0xf0f0f0,
189
[8 + COLOR_BLACK] = 0x000000,
190
[8 + COLOR_BLUE] = 0x0000ff,
191
[8 + COLOR_GREEN] = 0x00ff00,
192
[8 + COLOR_CYAN] = 0x00ffff,
193
[8 + COLOR_RED] = 0xff0000,
194
[8 + COLOR_MAGENTA] = 0xff00ff,
195
[8 + COLOR_YELLOW] = 0xffff00,
196
[8 + COLOR_WHITE] = 0xffffff,
199
static int rgb_from_attr(attr_rgb_t *rgb, const attrs_t *a);
200
static int rgb_from_style(attr_rgb_t *rgb, int style);
201
static int rgb_from_idx(attr_rgb_t *rgb, ipcarg_t fg_color,
202
ipcarg_t bg_color, ipcarg_t flags);
204
static int fb_set_color(viewport_t *vport, ipcarg_t fg_color,
205
ipcarg_t bg_color, ipcarg_t attr);
207
static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
208
uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color);
209
static void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
210
uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color);
212
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
216
#define RED(x, bits) (((x) >> (8 + 8 + 8 - (bits))) & ((1 << (bits)) - 1))
217
#define GREEN(x, bits) (((x) >> (8 + 8 - (bits))) & ((1 << (bits)) - 1))
218
#define BLUE(x, bits) (((x) >> (8 - (bits))) & ((1 << (bits)) - 1))
220
#define COL2X(col) ((col) * FONT_WIDTH)
221
#define ROW2Y(row) ((row) * FONT_SCANLINES)
223
#define X2COL(x) ((x) / FONT_WIDTH)
224
#define Y2ROW(y) ((y) / FONT_SCANLINES)
226
#define FB_POS(x, y) ((y) * screen.scanline + (x) * screen.pixelbytes)
227
#define BB_POS(vport, col, row) ((row) * vport->cols + (col))
228
#define GLYPH_POS(glyph, y, cursor) (((glyph) + (cursor) * FONT_GLYPHS) * screen.glyphbytes + (y) * screen.glyphscanline)
231
* RGB conversion and mask functions.
233
* These functions write an RGB value to some memory in some predefined format.
234
* The naming convention corresponds to the format created by these functions.
235
* The functions use the so called network order (i.e. big endian) with respect
239
static void rgb_0888(void *dst, uint32_t rgb)
241
*((uint32_t *) dst) = host2uint32_t_be((0 << 24) |
242
(RED(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | (BLUE(rgb, 8)));
245
static void bgr_0888(void *dst, uint32_t rgb)
247
*((uint32_t *) dst) = host2uint32_t_be((0 << 24) |
248
(BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | (RED(rgb, 8)));
251
static void mask_0888(void *dst, bool mask)
253
bgr_0888(dst, mask ? 0xffffff : 0);
256
static void rgb_8880(void *dst, uint32_t rgb)
258
*((uint32_t *) dst) = host2uint32_t_be((RED(rgb, 8) << 24) |
259
(GREEN(rgb, 8) << 16) | (BLUE(rgb, 8) << 8) | 0);
262
static void bgr_8880(void *dst, uint32_t rgb)
264
*((uint32_t *) dst) = host2uint32_t_be((BLUE(rgb, 8) << 24) |
265
(GREEN(rgb, 8) << 16) | (RED(rgb, 8) << 8) | 0);
268
static void mask_8880(void *dst, bool mask)
270
bgr_8880(dst, mask ? 0xffffff : 0);
273
static void rgb_888(void *dst, uint32_t rgb)
275
((uint8_t *) dst)[0] = RED(rgb, 8);
276
((uint8_t *) dst)[1] = GREEN(rgb, 8);
277
((uint8_t *) dst)[2] = BLUE(rgb, 8);
280
static void bgr_888(void *dst, uint32_t rgb)
282
((uint8_t *) dst)[0] = BLUE(rgb, 8);
283
((uint8_t *) dst)[1] = GREEN(rgb, 8);
284
((uint8_t *) dst)[2] = RED(rgb, 8);
287
static void mask_888(void *dst, bool mask)
289
bgr_888(dst, mask ? 0xffffff : 0);
292
static void rgb_555_be(void *dst, uint32_t rgb)
294
*((uint16_t *) dst) = host2uint16_t_be(RED(rgb, 5) << 10 |
295
GREEN(rgb, 5) << 5 | BLUE(rgb, 5));
298
static void rgb_555_le(void *dst, uint32_t rgb)
300
*((uint16_t *) dst) = host2uint16_t_le(RED(rgb, 5) << 10 |
301
GREEN(rgb, 5) << 5 | BLUE(rgb, 5));
304
static void rgb_565_be(void *dst, uint32_t rgb)
306
*((uint16_t *) dst) = host2uint16_t_be(RED(rgb, 5) << 11 |
307
GREEN(rgb, 6) << 5 | BLUE(rgb, 5));
310
static void rgb_565_le(void *dst, uint32_t rgb)
312
*((uint16_t *) dst) = host2uint16_t_le(RED(rgb, 5) << 11 |
313
GREEN(rgb, 6) << 5 | BLUE(rgb, 5));
316
static void mask_555(void *dst, bool mask)
318
rgb_555_be(dst, mask ? 0xffffff : 0);
321
static void mask_565(void *dst, bool mask)
323
rgb_565_be(dst, mask ? 0xffffff : 0);
326
static void bgr_323(void *dst, uint32_t rgb)
329
= ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
332
static void mask_323(void *dst, bool mask)
334
bgr_323(dst, mask ? 0x0 : ~0x0);
337
/** Draw a filled rectangle.
339
* @note Need real implementation that does not access VRAM twice.
342
static void draw_filled_rect(unsigned int x0, unsigned int y0, unsigned int x1,
343
unsigned int y1, uint32_t color)
347
unsigned int copy_bytes;
353
if ((y0 >= y1) || (x0 >= x1))
356
screen.rgb_conv(cbuf, color);
358
sp = &screen.fb_addr[FB_POS(x0, y0)];
361
/* Draw the first line. */
362
for (x = x0; x < x1; x++) {
363
memcpy(dp, cbuf, screen.pixelbytes);
364
dp += screen.pixelbytes;
367
dp = sp + screen.scanline;
368
copy_bytes = (x1 - x0) * screen.pixelbytes;
370
/* Draw the remaining lines by copying. */
371
for (y = y0 + 1; y < y1; y++) {
372
memcpy(dp, sp, copy_bytes);
373
dp += screen.scanline;
379
* @param vport Viewport to redraw
382
static void vport_redraw(viewport_t *vport)
387
for (row = 0; row < vport->rows; row++) {
388
for (col = 0; col < vport->cols; col++) {
389
draw_vp_glyph(vport, false, col, row);
393
if (COL2X(vport->cols) < vport->width) {
395
vport->x + COL2X(vport->cols), vport->y,
396
vport->x + vport->width, vport->y + vport->height,
397
vport->attr.bg_color);
400
if (ROW2Y(vport->rows) < vport->height) {
402
vport->x, vport->y + ROW2Y(vport->rows),
403
vport->x + vport->width, vport->y + vport->height,
404
vport->attr.bg_color);
408
static void backbuf_clear(bb_cell_t *backbuf, size_t len, uint32_t fg_color,
413
for (i = 0; i < len; i++) {
414
backbuf[i].glyph = 0;
415
backbuf[i].fg_color = fg_color;
416
backbuf[i].bg_color = bg_color;
422
* @param vport Viewport to clear
425
static void vport_clear(viewport_t *vport)
427
backbuf_clear(vport->backbuf, vport->cols * vport->rows,
428
vport->attr.fg_color, vport->attr.bg_color);
432
/** Scroll viewport by the specified number of lines.
434
* @param vport Viewport to scroll
435
* @param lines Number of lines to scroll
438
static void vport_scroll(viewport_t *vport, int lines)
455
for (row = 0; row < vport->rows; row++) {
457
for (col = 0; col < vport->cols; col++) {
458
if (((int) row + lines >= 0) &&
459
((int) row + lines < (int) vport->rows)) {
460
xbp = &vport->backbuf[BB_POS(vport, col, row + lines)];
461
bbp = &vport->backbuf[BB_POS(vport, col, row)];
464
fg_color = xbp->fg_color;
465
bg_color = xbp->bg_color;
467
if ((bbp->glyph == glyph)
468
&& (bbp->fg_color == xbp->fg_color)
469
&& (bbp->bg_color == xbp->bg_color)) {
475
fg_color = vport->attr.fg_color;
476
bg_color = vport->attr.bg_color;
479
(*vport->dglyph)(x, y, false, screen.glyphs, glyph,
491
memmove(vport->backbuf, vport->backbuf + vport->cols * lines,
492
vport->cols * (vport->rows - lines) * sizeof(bb_cell_t));
493
backbuf_clear(&vport->backbuf[BB_POS(vport, 0, vport->rows - lines)],
494
vport->cols * lines, vport->attr.fg_color, vport->attr.bg_color);
496
memmove(vport->backbuf - vport->cols * lines, vport->backbuf,
497
vport->cols * (vport->rows + lines) * sizeof(bb_cell_t));
498
backbuf_clear(vport->backbuf, - vport->cols * lines,
499
vport->attr.fg_color, vport->attr.bg_color);
505
* Convert glyphs from device independent font
506
* description to current visual representation.
509
static void render_glyphs(void)
513
for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
516
for (y = 0; y < FONT_SCANLINES; y++) {
519
for (x = 0; x < FONT_WIDTH; x++) {
520
screen.mask_conv(&screen.glyphs[GLYPH_POS(glyph, y, false) + x * screen.pixelbytes],
521
(fb_font[glyph][y] & (1 << (7 - x))) ? true : false);
523
screen.mask_conv(&screen.glyphs[GLYPH_POS(glyph, y, true) + x * screen.pixelbytes],
524
(fb_font[glyph][y] & (1 << (7 - x))) ? false : true);
530
/** Create new viewport
532
* @param x Origin of the viewport (x).
533
* @param y Origin of the viewport (y).
534
* @param width Width of the viewport.
535
* @param height Height of the viewport.
537
* @return New viewport number.
540
static int vport_create(unsigned int x, unsigned int y,
541
unsigned int width, unsigned int height)
545
for (i = 0; i < MAX_VIEWPORTS; i++) {
546
if (!viewports[i].initialized)
550
if (i == MAX_VIEWPORTS)
553
unsigned int cols = width / FONT_WIDTH;
554
unsigned int rows = height / FONT_SCANLINES;
555
unsigned int bbsize = cols * rows * sizeof(bb_cell_t);
556
unsigned int word_size = sizeof(unsigned long);
558
bb_cell_t *backbuf = (bb_cell_t *) malloc(bbsize);
562
uint8_t *bgpixel = (uint8_t *) malloc(screen.pixelbytes);
568
backbuf_clear(backbuf, cols * rows, DEFAULT_FGCOLOR, DEFAULT_BGCOLOR);
569
memset(bgpixel, 0, screen.pixelbytes);
573
viewports[i].width = width;
574
viewports[i].height = height;
576
viewports[i].cols = cols;
577
viewports[i].rows = rows;
579
viewports[i].attr.bg_color = DEFAULT_BGCOLOR;
580
viewports[i].attr.fg_color = DEFAULT_FGCOLOR;
582
viewports[i].bgpixel = bgpixel;
585
* Conditions necessary to select aligned version:
586
* - word size is divisible by pixelbytes
587
* - cell scanline size is divisible by word size
588
* - cell scanlines are word-aligned
591
if (((word_size % screen.pixelbytes) == 0)
592
&& ((FONT_WIDTH * screen.pixelbytes) % word_size == 0)
593
&& ((x * screen.pixelbytes) % word_size == 0)
594
&& (screen.scanline % word_size == 0)) {
595
viewports[i].dglyph = draw_glyph_aligned;
597
viewports[i].dglyph = draw_glyph_fallback;
600
viewports[i].cur_col = 0;
601
viewports[i].cur_row = 0;
602
viewports[i].cursor_active = false;
603
viewports[i].cursor_shown = false;
605
viewports[i].bbsize = bbsize;
606
viewports[i].backbuf = backbuf;
608
viewports[i].initialized = true;
610
screen.rgb_conv(viewports[i].bgpixel, viewports[i].attr.bg_color);
616
/** Initialize framebuffer as a chardev output device
618
* @param addr Address of the framebuffer
619
* @param xres Screen width in pixels
620
* @param yres Screen height in pixels
621
* @param visual Bits per pixel (8, 16, 24, 32)
622
* @param scan Bytes per one scanline
625
static bool screen_init(void *addr, unsigned int xres, unsigned int yres,
626
unsigned int scan, unsigned int visual)
629
case VISUAL_INDIRECT_8:
630
screen.rgb_conv = bgr_323;
631
screen.mask_conv = mask_323;
632
screen.pixelbytes = 1;
634
case VISUAL_RGB_5_5_5_LE:
635
screen.rgb_conv = rgb_555_le;
636
screen.mask_conv = mask_555;
637
screen.pixelbytes = 2;
639
case VISUAL_RGB_5_5_5_BE:
640
screen.rgb_conv = rgb_555_be;
641
screen.mask_conv = mask_555;
642
screen.pixelbytes = 2;
644
case VISUAL_RGB_5_6_5_LE:
645
screen.rgb_conv = rgb_565_le;
646
screen.mask_conv = mask_565;
647
screen.pixelbytes = 2;
649
case VISUAL_RGB_5_6_5_BE:
650
screen.rgb_conv = rgb_565_be;
651
screen.mask_conv = mask_565;
652
screen.pixelbytes = 2;
654
case VISUAL_RGB_8_8_8:
655
screen.rgb_conv = rgb_888;
656
screen.mask_conv = mask_888;
657
screen.pixelbytes = 3;
659
case VISUAL_BGR_8_8_8:
660
screen.rgb_conv = bgr_888;
661
screen.mask_conv = mask_888;
662
screen.pixelbytes = 3;
664
case VISUAL_RGB_8_8_8_0:
665
screen.rgb_conv = rgb_8880;
666
screen.mask_conv = mask_8880;
667
screen.pixelbytes = 4;
669
case VISUAL_RGB_0_8_8_8:
670
screen.rgb_conv = rgb_0888;
671
screen.mask_conv = mask_0888;
672
screen.pixelbytes = 4;
674
case VISUAL_BGR_0_8_8_8:
675
screen.rgb_conv = bgr_0888;
676
screen.mask_conv = mask_0888;
677
screen.pixelbytes = 4;
679
case VISUAL_BGR_8_8_8_0:
680
screen.rgb_conv = bgr_8880;
681
screen.mask_conv = mask_8880;
682
screen.pixelbytes = 4;
688
screen.fb_addr = (unsigned char *) addr;
691
screen.scanline = scan;
693
screen.glyphscanline = FONT_WIDTH * screen.pixelbytes;
694
screen.glyphbytes = screen.glyphscanline * FONT_SCANLINES;
696
size_t glyphsize = 2 * FONT_GLYPHS * screen.glyphbytes;
697
uint8_t *glyphs = (uint8_t *) malloc(glyphsize);
701
memset(glyphs, 0, glyphsize);
702
screen.glyphs = glyphs;
706
/* Create first viewport */
707
vport_create(0, 0, xres, yres);
713
/** Draw a glyph, takes advantage of alignment.
715
* This version can only be used if the following conditions are met:
717
* - word size is divisible by pixelbytes
718
* - cell scanline size is divisible by word size
719
* - cell scanlines are word-aligned
721
* It makes use of the pre-rendered mask to process (possibly) several
722
* pixels at once (word size / pixelbytes pixels at a time are processed)
723
* making it very fast. Most notably this version is not applicable at 24 bits
726
* @param x x coordinate of top-left corner on screen.
727
* @param y y coordinate of top-left corner on screen.
728
* @param cursor Draw glyph with cursor
729
* @param glyphs Pointer to font bitmap.
730
* @param glyph Code of the glyph to draw.
731
* @param fg_color Foreground color.
732
* @param bg_color Backgroudn color.
735
static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
736
uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color)
740
unsigned long fg_buf;
741
unsigned long bg_buf;
745
* Prepare a pair of words, one filled with foreground-color
746
* pattern and the other filled with background-color pattern.
748
for (i = 0; i < sizeof(unsigned long) / screen.pixelbytes; i++) {
749
screen.rgb_conv(&((uint8_t *) &fg_buf)[i * screen.pixelbytes],
751
screen.rgb_conv(&((uint8_t *) &bg_buf)[i * screen.pixelbytes],
755
/* Pointer to the current position in the mask. */
756
unsigned long *maskp = (unsigned long *) &glyphs[GLYPH_POS(glyph, 0, cursor)];
758
/* Pointer to the current position on the screen. */
759
unsigned long *dp = (unsigned long *) &screen.fb_addr[FB_POS(x, y)];
761
/* Width of the character cell in words. */
762
unsigned int ww = FONT_WIDTH * screen.pixelbytes / sizeof(unsigned long);
764
/* Offset to add when moving to another screen scanline. */
765
unsigned int d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
767
for (yd = 0; yd < FONT_SCANLINES; yd++) {
769
* Now process the cell scanline, combining foreground
770
* and background color patters using the pre-rendered mask.
772
for (i = 0; i < ww; i++) {
774
*dp++ = (fg_buf & mask) | (bg_buf & ~mask);
777
/* Move to the beginning of the next scanline of the cell. */
778
dp = (unsigned long *) ((uint8_t *) dp + d_add);
782
/** Draw a glyph, fallback version.
784
* This version does not make use of the pre-rendered mask, it uses
785
* the font bitmap directly. It works always, but it is slower.
787
* @param x x coordinate of top-left corner on screen.
788
* @param y y coordinate of top-left corner on screen.
789
* @param cursor Draw glyph with cursor
790
* @param glyphs Pointer to font bitmap.
791
* @param glyph Code of the glyph to draw.
792
* @param fg_color Foreground color.
793
* @param bg_color Backgroudn color.
796
void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
797
uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color)
807
/* Pre-render 1x the foreground and background color pixels. */
809
screen.rgb_conv(fg_buf, bg_color);
810
screen.rgb_conv(bg_buf, fg_color);
812
screen.rgb_conv(fg_buf, fg_color);
813
screen.rgb_conv(bg_buf, bg_color);
816
/* Pointer to the current position on the screen. */
817
uint8_t *dp = (uint8_t *) &screen.fb_addr[FB_POS(x, y)];
819
/* Offset to add when moving to another screen scanline. */
820
unsigned int d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
822
for (yd = 0; yd < FONT_SCANLINES; yd++) {
823
/* Byte containing bits of the glyph scanline. */
824
b = fb_font[glyph][yd];
826
for (i = 0; i < FONT_WIDTH; i++) {
827
/* Choose color based on the current bit. */
828
sp = (b & 0x80) ? fg_buf : bg_buf;
830
/* Copy the pixel. */
831
for (j = 0; j < screen.pixelbytes; j++) {
835
/* Move to the next bit. */
839
/* Move to the beginning of the next scanline of the cell. */
844
/** Draw glyph at specified position in viewport.
846
* @param vport Viewport identification
847
* @param cursor Draw glyph with cursor
848
* @param col Screen position relative to viewport
849
* @param row Screen position relative to viewport
852
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
855
unsigned int x = vport->x + COL2X(col);
856
unsigned int y = vport->y + ROW2Y(row);
858
uint32_t glyph = vport->backbuf[BB_POS(vport, col, row)].glyph;
859
uint32_t fg_color = vport->backbuf[BB_POS(vport, col, row)].fg_color;
860
uint32_t bg_color = vport->backbuf[BB_POS(vport, col, row)].bg_color;
862
(*vport->dglyph)(x, y, cursor, screen.glyphs, glyph,
866
/** Hide cursor if it is shown
869
static void cursor_hide(viewport_t *vport)
871
if ((vport->cursor_active) && (vport->cursor_shown)) {
872
draw_vp_glyph(vport, false, vport->cur_col, vport->cur_row);
873
vport->cursor_shown = false;
878
/** Show cursor if cursor showing is enabled
881
static void cursor_show(viewport_t *vport)
883
/* Do not check for cursor_shown */
884
if (vport->cursor_active) {
885
draw_vp_glyph(vport, true, vport->cur_col, vport->cur_row);
886
vport->cursor_shown = true;
891
/** Invert cursor, if it is enabled
894
static void cursor_blink(viewport_t *vport)
896
if (vport->cursor_shown)
903
/** Draw character at given position relative to viewport
905
* @param vport Viewport identification
906
* @param c Character to draw
907
* @param col Screen position relative to viewport
908
* @param row Screen position relative to viewport
911
static void draw_char(viewport_t *vport, wchar_t c, unsigned int col, unsigned int row)
915
/* Do not hide cursor if we are going to overwrite it */
916
if ((vport->cursor_active) && (vport->cursor_shown) &&
917
((vport->cur_col != col) || (vport->cur_row != row)))
920
bbp = &vport->backbuf[BB_POS(vport, col, row)];
921
bbp->glyph = fb_font_glyph(c);
922
bbp->fg_color = vport->attr.fg_color;
923
bbp->bg_color = vport->attr.bg_color;
925
draw_vp_glyph(vport, false, col, row);
927
vport->cur_col = col;
928
vport->cur_row = row;
931
if (vport->cur_col >= vport->cols) {
934
if (vport->cur_row >= vport->rows)
941
/** Draw text data to viewport.
943
* @param vport Viewport id
944
* @param data Text data.
945
* @param x Leftmost column of the area.
946
* @param y Topmost row of the area.
947
* @param w Number of rows.
948
* @param h Number of columns.
951
static void draw_text_data(viewport_t *vport, keyfield_t *data, unsigned int x,
952
unsigned int y, unsigned int w, unsigned int h)
960
for (j = 0; j < h; j++) {
961
for (i = 0; i < w; i++) {
962
unsigned int col = x + i;
963
unsigned int row = y + j;
965
bbp = &vport->backbuf[BB_POS(vport, col, row)];
967
a = &data[j * w + i].attrs;
968
rgb_from_attr(&rgb, a);
970
bbp->glyph = fb_font_glyph(data[j * w + i].character);
971
bbp->fg_color = rgb.fg_color;
972
bbp->bg_color = rgb.bg_color;
974
draw_vp_glyph(vport, false, col, row);
981
static void putpixel_pixmap(void *data, unsigned int x, unsigned int y, uint32_t color)
983
int pm = *((int *) data);
984
pixmap_t *pmap = &pixmaps[pm];
985
unsigned int pos = (y * pmap->width + x) * screen.pixelbytes;
987
screen.rgb_conv(&pmap->data[pos], color);
991
static void putpixel(void *data, unsigned int x, unsigned int y, uint32_t color)
993
viewport_t *vport = (viewport_t *) data;
994
unsigned int dx = vport->x + x;
995
unsigned int dy = vport->y + y;
997
screen.rgb_conv(&screen.fb_addr[FB_POS(dx, dy)], color);
1001
/** Return first free pixmap
1004
static int find_free_pixmap(void)
1008
for (i = 0; i < MAX_PIXMAPS; i++)
1009
if (!pixmaps[i].data)
1016
/** Create a new pixmap and return appropriate ID
1019
static int shm2pixmap(unsigned char *shm, size_t size)
1024
pm = find_free_pixmap();
1028
pmap = &pixmaps[pm];
1030
if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
1033
pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
1037
ppm_draw(shm, size, 0, 0, pmap->width, pmap->height, putpixel_pixmap, (void *) &pm);
1043
/** Handle shared memory communication calls
1045
* Protocol for drawing pixmaps:
1046
* - FB_PREPARE_SHM(client shm identification)
1047
* - IPC_M_AS_AREA_SEND
1048
* - FB_DRAW_PPM(startx, starty)
1051
* Protocol for text drawing
1052
* - IPC_M_AS_AREA_SEND
1053
* - FB_DRAW_TEXT_DATA
1055
* @param callid Callid of the current call
1056
* @param call Current call data
1057
* @param vp Active viewport
1059
* @return false if the call was not handled byt this function, true otherwise
1061
* Note: this function is not thread-safe, you would have
1062
* to redefine static variables with fibril_local.
1065
static bool shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1067
static keyfield_t *interbuffer = NULL;
1068
static size_t intersize = 0;
1070
static unsigned char *shm = NULL;
1071
static ipcarg_t shm_id = 0;
1072
static size_t shm_size;
1074
bool handled = true;
1076
viewport_t *vport = &viewports[vp];
1082
switch (IPC_GET_METHOD(*call)) {
1083
case IPC_M_SHARE_OUT:
1084
/* We accept one area for data interchange */
1085
if (IPC_GET_ARG1(*call) == shm_id) {
1086
void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
1087
shm_size = IPC_GET_ARG2(*call);
1088
if (ipc_answer_1(callid, EOK, (sysarg_t) dest)) {
1099
intersize = IPC_GET_ARG2(*call);
1100
receive_comm_area(callid, call, (void *) &interbuffer);
1103
case FB_PREPARE_SHM:
1107
shm_id = IPC_GET_ARG1(*call);
1112
as_area_destroy(shm);
1123
retval = shm2pixmap(shm, shm_size);
1130
x = IPC_GET_ARG1(*call);
1131
y = IPC_GET_ARG2(*call);
1133
if ((x > vport->width) || (y > vport->height)) {
1138
ppm_draw(shm, shm_size, IPC_GET_ARG1(*call),
1139
IPC_GET_ARG2(*call), vport->width - x, vport->height - y, putpixel, (void *) vport);
1141
case FB_DRAW_TEXT_DATA:
1142
x = IPC_GET_ARG1(*call);
1143
y = IPC_GET_ARG2(*call);
1144
w = IPC_GET_ARG3(*call);
1145
h = IPC_GET_ARG4(*call);
1150
if (x + w > vport->cols || y + h > vport->rows) {
1154
if (intersize < w * h * sizeof(*interbuffer)) {
1158
draw_text_data(vport, interbuffer, x, y, w, h);
1165
ipc_answer_0(callid, retval);
1170
static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
1172
unsigned int width = vport->width;
1173
unsigned int height = vport->height;
1175
if (width + vport->x > screen.xres)
1176
width = screen.xres - vport->x;
1177
if (height + vport->y > screen.yres)
1178
height = screen.yres - vport->y;
1180
unsigned int realwidth = pmap->width <= width ? pmap->width : width;
1181
unsigned int realheight = pmap->height <= height ? pmap->height : height;
1183
unsigned int srcrowsize = vport->width * screen.pixelbytes;
1184
unsigned int realrowsize = realwidth * screen.pixelbytes;
1187
for (y = 0; y < realheight; y++) {
1188
unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
1189
memcpy(pmap->data + srcrowsize * y, screen.fb_addr + tmp, realrowsize);
1194
/** Save viewport to pixmap
1197
static int save_vp_to_pixmap(viewport_t *vport)
1202
pm = find_free_pixmap();
1206
pmap = &pixmaps[pm];
1207
pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
1211
pmap->width = vport->width;
1212
pmap->height = vport->height;
1214
copy_vp_to_pixmap(vport, pmap);
1220
/** Draw pixmap on screen
1222
* @param vp Viewport to draw on
1223
* @param pm Pixmap identifier
1226
static int draw_pixmap(int vp, int pm)
1228
pixmap_t *pmap = &pixmaps[pm];
1229
viewport_t *vport = &viewports[vp];
1231
unsigned int width = vport->width;
1232
unsigned int height = vport->height;
1234
if (width + vport->x > screen.xres)
1235
width = screen.xres - vport->x;
1236
if (height + vport->y > screen.yres)
1237
height = screen.yres - vport->y;
1242
unsigned int realwidth = pmap->width <= width ? pmap->width : width;
1243
unsigned int realheight = pmap->height <= height ? pmap->height : height;
1245
unsigned int srcrowsize = vport->width * screen.pixelbytes;
1246
unsigned int realrowsize = realwidth * screen.pixelbytes;
1249
for (y = 0; y < realheight; y++) {
1250
unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
1251
memcpy(screen.fb_addr + tmp, pmap->data + y * srcrowsize, realrowsize);
1258
/** Tick animation one step forward
1261
static void anims_tick(void)
1264
static int counts = 0;
1266
/* Limit redrawing */
1267
counts = (counts + 1) % 8;
1271
for (i = 0; i < MAX_ANIMATIONS; i++) {
1272
if ((!animations[i].animlen) || (!animations[i].initialized) ||
1273
(!animations[i].enabled))
1276
draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
1277
animations[i].pos = (animations[i].pos + 1) % animations[i].animlen;
1282
static unsigned int pointer_x;
1283
static unsigned int pointer_y;
1284
static bool pointer_shown, pointer_enabled;
1285
static int pointer_vport = -1;
1286
static int pointer_pixmap = -1;
1289
static void mouse_show(void)
1296
if ((pointer_shown) || (!pointer_enabled))
1299
/* Save image under the pointer. */
1300
if (pointer_vport == -1) {
1301
pointer_vport = vport_create(pointer_x, pointer_y, pointer_width, pointer_height);
1302
if (pointer_vport < 0)
1305
viewports[pointer_vport].x = pointer_x;
1306
viewports[pointer_vport].y = pointer_y;
1309
if (pointer_pixmap == -1)
1310
pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
1312
copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
1314
/* Draw mouse pointer. */
1315
for (i = 0; i < pointer_height; i++)
1316
for (j = 0; j < pointer_width; j++) {
1317
bytepos = i * ((pointer_width - 1) / 8 + 1) + j / 8;
1318
visibility = pointer_mask_bits[bytepos] &
1321
color = pointer_bits[bytepos] &
1322
(1 << (j % 8)) ? 0 : 0xffffff;
1323
if (pointer_x + j < screen.xres && pointer_y +
1325
putpixel(&viewports[0], pointer_x + j,
1326
pointer_y + i, color);
1333
static void mouse_hide(void)
1335
/* Restore image under the pointer. */
1336
if (pointer_shown) {
1337
draw_pixmap(pointer_vport, pointer_pixmap);
1343
static void mouse_move(unsigned int x, unsigned int y)
1352
static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1354
bool handled = true;
1359
switch (IPC_GET_METHOD(*call)) {
1360
case FB_ANIM_CREATE:
1361
nvp = IPC_GET_ARG1(*call);
1364
if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1365
!viewports[nvp].initialized) {
1369
for (i = 0; i < MAX_ANIMATIONS; i++) {
1370
if (!animations[i].initialized)
1373
if (i == MAX_ANIMATIONS) {
1377
animations[i].initialized = 1;
1378
animations[i].animlen = 0;
1379
animations[i].pos = 0;
1380
animations[i].enabled = 0;
1381
animations[i].vp = nvp;
1385
i = IPC_GET_ARG1(*call);
1386
if (i >= MAX_ANIMATIONS || i < 0) {
1390
animations[i].initialized = 0;
1392
case FB_ANIM_ADDPIXMAP:
1393
i = IPC_GET_ARG1(*call);
1394
if (i >= MAX_ANIMATIONS || i < 0 ||
1395
!animations[i].initialized) {
1399
if (animations[i].animlen == MAX_ANIM_LEN) {
1403
newval = IPC_GET_ARG2(*call);
1404
if (newval < 0 || newval > MAX_PIXMAPS ||
1405
!pixmaps[newval].data) {
1409
animations[i].pixmaps[animations[i].animlen++] = newval;
1412
i = IPC_GET_ARG1(*call);
1413
if (i >= MAX_ANIMATIONS || i < 0) {
1417
nvp = IPC_GET_ARG2(*call);
1420
if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1421
!viewports[nvp].initialized) {
1425
animations[i].vp = nvp;
1429
i = IPC_GET_ARG1(*call);
1430
if (i >= MAX_ANIMATIONS || i < 0) {
1434
newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
1435
if (newval ^ animations[i].enabled) {
1436
animations[i].enabled = newval;
1437
anims_enabled += newval ? 1 : -1;
1444
ipc_answer_0(callid, retval);
1449
/** Handler for messages concerning pixmap handling
1452
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1454
bool handled = true;
1458
switch (IPC_GET_METHOD(*call)) {
1459
case FB_VP_DRAW_PIXMAP:
1460
nvp = IPC_GET_ARG1(*call);
1463
if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1464
!viewports[nvp].initialized) {
1468
i = IPC_GET_ARG2(*call);
1469
retval = draw_pixmap(nvp, i);
1472
nvp = IPC_GET_ARG1(*call);
1475
if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1476
!viewports[nvp].initialized)
1479
retval = save_vp_to_pixmap(&viewports[nvp]);
1481
case FB_DROP_PIXMAP:
1482
i = IPC_GET_ARG1(*call);
1483
if (i >= MAX_PIXMAPS) {
1487
if (pixmaps[i].data) {
1488
free(pixmaps[i].data);
1489
pixmaps[i].data = NULL;
1497
ipc_answer_0(callid, retval);
1502
static int rgb_from_style(attr_rgb_t *rgb, int style)
1506
rgb->fg_color = color_table[COLOR_BLACK];
1507
rgb->bg_color = color_table[COLOR_WHITE];
1509
case STYLE_EMPHASIS:
1510
rgb->fg_color = color_table[COLOR_RED];
1511
rgb->bg_color = color_table[COLOR_WHITE];
1520
static int rgb_from_idx(attr_rgb_t *rgb, ipcarg_t fg_color,
1521
ipcarg_t bg_color, ipcarg_t flags)
1523
fg_color = (fg_color & 7) | ((flags & CATTR_BRIGHT) ? 8 : 0);
1524
bg_color = (bg_color & 7) | ((flags & CATTR_BRIGHT) ? 8 : 0);
1526
rgb->fg_color = color_table[fg_color];
1527
rgb->bg_color = color_table[bg_color];
1532
static int rgb_from_attr(attr_rgb_t *rgb, const attrs_t *a)
1538
rc = rgb_from_style(rgb, a->a.s.style);
1541
rc = rgb_from_idx(rgb, a->a.i.fg_color,
1542
a->a.i.bg_color, a->a.i.flags);
1553
static int fb_set_style(viewport_t *vport, ipcarg_t style)
1555
return rgb_from_style(&vport->attr, (int) style);
1558
static int fb_set_color(viewport_t *vport, ipcarg_t fg_color,
1559
ipcarg_t bg_color, ipcarg_t flags)
1561
return rgb_from_idx(&vport->attr, fg_color, bg_color, flags);
1564
/** Function for handling connections to FB
1567
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1569
unsigned int vp = 0;
1570
viewport_t *vport = &viewports[vp];
1572
if (client_connected) {
1573
ipc_answer_0(iid, ELIMIT);
1577
/* Accept connection */
1578
client_connected = true;
1579
ipc_answer_0(iid, EOK);
1582
ipc_callid_t callid;
1588
unsigned int col, row;
1590
if ((vport->cursor_active) || (anims_enabled))
1591
callid = async_get_call_timeout(&call, 250000);
1593
callid = async_get_call(&call);
1597
cursor_blink(vport);
1603
if (shm_handle(callid, &call, vp))
1606
if (pixmap_handle(callid, &call, vp))
1609
if (anim_handle(callid, &call, vp))
1612
switch (IPC_GET_METHOD(call)) {
1613
case IPC_M_PHONE_HUNGUP:
1614
client_connected = false;
1616
/* Cleanup other viewports */
1617
for (i = 1; i < MAX_VIEWPORTS; i++)
1618
vport->initialized = false;
1624
ch = IPC_GET_ARG1(call);
1625
col = IPC_GET_ARG2(call);
1626
row = IPC_GET_ARG3(call);
1628
if ((col >= vport->cols) || (row >= vport->rows)) {
1632
ipc_answer_0(callid, EOK);
1634
draw_char(vport, ch, col, row);
1636
/* Message already answered */
1643
case FB_CURSOR_GOTO:
1644
col = IPC_GET_ARG1(call);
1645
row = IPC_GET_ARG2(call);
1647
if ((col >= vport->cols) || (row >= vport->rows)) {
1654
vport->cur_col = col;
1655
vport->cur_row = row;
1658
case FB_CURSOR_VISIBILITY:
1660
vport->cursor_active = IPC_GET_ARG1(call);
1665
ipc_answer_2(callid, EOK, vport->cols, vport->rows);
1667
case FB_GET_COLOR_CAP:
1668
ipc_answer_1(callid, EOK, FB_CCAP_RGB);
1671
scroll = IPC_GET_ARG1(call);
1672
if ((scroll > (int) vport->rows) || (scroll < (-(int) vport->rows))) {
1677
vport_scroll(vport, scroll);
1681
case FB_VIEWPORT_SWITCH:
1682
i = IPC_GET_ARG1(call);
1683
if (i >= MAX_VIEWPORTS) {
1687
if (!viewports[i].initialized) {
1688
retval = EADDRNOTAVAIL;
1693
vport = &viewports[vp];
1697
case FB_VIEWPORT_CREATE:
1698
retval = vport_create(IPC_GET_ARG1(call) >> 16,
1699
IPC_GET_ARG1(call) & 0xffff,
1700
IPC_GET_ARG2(call) >> 16,
1701
IPC_GET_ARG2(call) & 0xffff);
1703
case FB_VIEWPORT_DELETE:
1704
i = IPC_GET_ARG1(call);
1705
if (i >= MAX_VIEWPORTS) {
1709
if (!viewports[i].initialized) {
1710
retval = EADDRNOTAVAIL;
1713
viewports[i].initialized = false;
1714
if (viewports[i].bgpixel)
1715
free(viewports[i].bgpixel);
1716
if (viewports[i].backbuf)
1717
free(viewports[i].backbuf);
1721
retval = fb_set_style(vport, IPC_GET_ARG1(call));
1724
retval = fb_set_color(vport, IPC_GET_ARG1(call),
1725
IPC_GET_ARG2(call), IPC_GET_ARG3(call));
1727
case FB_SET_RGB_COLOR:
1728
vport->attr.fg_color = IPC_GET_ARG1(call);
1729
vport->attr.bg_color = IPC_GET_ARG2(call);
1732
case FB_GET_RESOLUTION:
1733
ipc_answer_2(callid, EOK, screen.xres, screen.yres);
1735
case FB_POINTER_MOVE:
1736
pointer_enabled = true;
1737
mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
1740
case FB_SCREEN_YIELD:
1741
case FB_SCREEN_RECLAIM:
1747
ipc_answer_0(callid, retval);
1751
/** Initialization of framebuffer
1756
async_set_client_connection(fb_client_connection);
1758
void *fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
1759
unsigned int fb_offset = sysinfo_value("fb.offset");
1760
unsigned int fb_width = sysinfo_value("fb.width");
1761
unsigned int fb_height = sysinfo_value("fb.height");
1762
unsigned int fb_scanline = sysinfo_value("fb.scanline");
1763
unsigned int fb_visual = sysinfo_value("fb.visual");
1765
unsigned int fbsize = fb_scanline * fb_height;
1766
void *fb_addr = as_get_mappable_page(fbsize);
1768
if (physmem_map(fb_ph_addr + fb_offset, fb_addr,
1769
ALIGN_UP(fbsize, PAGE_SIZE) >> PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE) != 0)
1772
if (screen_init(fb_addr, fb_width, fb_height, fb_scanline, fb_visual))