Subversion Repositories HelenOS

Rev

Rev 4167 | Rev 4226 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1363 vana 1
/*
3707 decky 2
 * Copyright (c) 2008 Martin Decky
2071 jermar 3
 * Copyright (c) 2006 Jakub Vana
4
 * Copyright (c) 2006 Ondrej Palkovsky
1363 vana 5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
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.
18
 *
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.
29
 */
30
 
1740 jermar 31
/**
32
 * @defgroup fb Graphical framebuffer
33
 * @brief	HelenOS graphical framebuffer.
1649 cejka 34
 * @ingroup fbs
35
 * @{
3707 decky 36
 */
1888 jermar 37
 
1649 cejka 38
/** @file
39
 */
40
 
1430 jermar 41
#include <stdlib.h>
42
#include <unistd.h>
43
#include <string.h>
1363 vana 44
#include <ddi.h>
45
#include <sysinfo.h>
46
#include <align.h>
47
#include <as.h>
48
#include <ipc/fb.h>
49
#include <ipc/ipc.h>
1430 jermar 50
#include <ipc/ns.h>
1363 vana 51
#include <ipc/services.h>
52
#include <kernel/errno.h>
1993 decky 53
#include <kernel/genarch/fb/visuals.h>
3767 svoboda 54
#include <console/color.h>
55
#include <console/style.h>
1392 palkovsky 56
#include <async.h>
1993 decky 57
#include <bool.h>
1505 palkovsky 58
 
1404 palkovsky 59
#include "font-8x16.h"
1363 vana 60
#include "fb.h"
1505 palkovsky 61
#include "main.h"
62
#include "../console/screenbuffer.h"
1547 palkovsky 63
#include "ppm.h"
1363 vana 64
 
1711 palkovsky 65
#include "pointer.xbm"
66
#include "pointer_mask.xbm"
67
 
3707 decky 68
#define DEFAULT_BGCOLOR  0xf0f0f0
69
#define DEFAULT_FGCOLOR  0x000000
1363 vana 70
 
3707 decky 71
#define MAX_ANIM_LEN     8
72
#define MAX_ANIMATIONS   4
73
#define MAX_PIXMAPS      256  /**< Maximum number of saved pixmaps */
74
#define MAX_VIEWPORTS    128  /**< Viewport is a rectangular area on the screen */
1363 vana 75
 
3744 svoboda 76
/** Function to render a pixel from a RGB value. */
3707 decky 77
typedef void (*rgb_conv_t)(void *, uint32_t);
1363 vana 78
 
3744 svoboda 79
/** Function to draw a glyph. */
80
typedef void (*dg_t)(unsigned int x, unsigned int y, bool cursor,
81
    uint8_t *glyphs, uint8_t glyph, uint32_t fg_color, uint32_t bg_color);
82
 
1485 palkovsky 83
struct {
3707 decky 84
	uint8_t *fb_addr;
85
 
1875 jermar 86
	unsigned int xres;
87
	unsigned int yres;
3707 decky 88
 
1875 jermar 89
	unsigned int scanline;
3707 decky 90
	unsigned int glyphscanline;
91
 
1875 jermar 92
	unsigned int pixelbytes;
3707 decky 93
	unsigned int glyphbytes;
94
 
95
	rgb_conv_t rgb_conv;
1485 palkovsky 96
} screen;
1363 vana 97
 
3767 svoboda 98
/** Backbuffer character cell. */
1485 palkovsky 99
typedef struct {
4211 svoboda 100
	uint32_t glyph;
3767 svoboda 101
	uint32_t fg_color;
102
	uint32_t bg_color;
103
} bb_cell_t;
104
 
105
typedef struct {
3707 decky 106
	bool initialized;
107
	unsigned int x;
108
	unsigned int y;
109
	unsigned int width;
110
	unsigned int height;
111
 
1485 palkovsky 112
	/* Text support in window */
3707 decky 113
	unsigned int cols;
114
	unsigned int rows;
115
 
3744 svoboda 116
	/*
117
	 * Style and glyphs for text printing
118
	 */
119
 
3767 svoboda 120
	/** Current attributes. */
121
	attr_rgb_t attr;
3744 svoboda 122
 
123
	/** Pre-rendered mask for rendering glyphs. Different viewports
124
	 * might use different drawing functions depending on whether their
125
	 * scanlines are aligned on a word boundary.*/
3707 decky 126
	uint8_t *glyphs;
3744 svoboda 127
 
3707 decky 128
	uint8_t *bgpixel;
3744 svoboda 129
 
130
	/** Glyph drawing function for this viewport. */
131
	dg_t dglyph;
3707 decky 132
 
1485 palkovsky 133
	/* Auto-cursor position */
3707 decky 134
	bool cursor_active;
135
	unsigned int cur_col;
136
	unsigned int cur_row;
137
	bool cursor_shown;
138
 
139
	/* Back buffer */
3767 svoboda 140
	bb_cell_t *backbuf;
3707 decky 141
	unsigned int bbsize;
1485 palkovsky 142
} viewport_t;
1363 vana 143
 
1646 palkovsky 144
typedef struct {
3707 decky 145
	bool initialized;
146
	bool enabled;
1646 palkovsky 147
	unsigned int vp;
3707 decky 148
 
1646 palkovsky 149
	unsigned int pos;
150
	unsigned int animlen;
151
	unsigned int pixmaps[MAX_ANIM_LEN];
152
} animation_t;
3707 decky 153
 
1646 palkovsky 154
static animation_t animations[MAX_ANIMATIONS];
3707 decky 155
static bool anims_enabled;
1646 palkovsky 156
 
1552 palkovsky 157
typedef struct {
158
	unsigned int width;
159
	unsigned int height;
1781 jermar 160
	uint8_t *data;
1552 palkovsky 161
} pixmap_t;
3707 decky 162
 
1552 palkovsky 163
static pixmap_t pixmaps[MAX_PIXMAPS];
1485 palkovsky 164
static viewport_t viewports[128];
165
 
3707 decky 166
static bool client_connected = false;  /**< Allow only 1 connection */
1485 palkovsky 167
 
3767 svoboda 168
static uint32_t color_table[16] = {
169
	[COLOR_BLACK]		= 0x000000,
170
	[COLOR_BLUE]		= 0x0000f0,
171
	[COLOR_GREEN]		= 0x00f000,
172
	[COLOR_CYAN]		= 0x00f0f0,
173
	[COLOR_RED]		= 0xf00000,
174
	[COLOR_MAGENTA]		= 0xf000f0,
175
	[COLOR_YELLOW]		= 0xf0f000,
176
	[COLOR_WHITE]		= 0xf0f0f0,
177
 
178
	[8 + COLOR_BLACK]	= 0x000000,
179
	[8 + COLOR_BLUE]	= 0x0000ff,
180
	[8 + COLOR_GREEN]	= 0x00ff00,
181
	[8 + COLOR_CYAN]	= 0x00ffff,
182
	[8 + COLOR_RED]		= 0xff0000,
183
	[8 + COLOR_MAGENTA]	= 0xff00ff,
184
	[8 + COLOR_YELLOW]	= 0xffff00,
185
	[8 + COLOR_WHITE]	= 0xffffff,
186
};
187
 
3792 svoboda 188
static int rgb_from_attr(attr_rgb_t *rgb, const attrs_t *a);
3767 svoboda 189
static int rgb_from_style(attr_rgb_t *rgb, int style);
190
static int rgb_from_idx(attr_rgb_t *rgb, ipcarg_t fg_color,
191
    ipcarg_t bg_color, ipcarg_t flags);
192
 
193
static int fb_set_color(viewport_t *vport, ipcarg_t fg_color,
194
    ipcarg_t bg_color, ipcarg_t attr);
195
 
3744 svoboda 196
static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
4211 svoboda 197
    uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color);
3744 svoboda 198
static void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
4211 svoboda 199
    uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color);
3744 svoboda 200
 
3739 svoboda 201
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
3723 svoboda 202
    unsigned int row);
203
 
204
 
3707 decky 205
#define RED(x, bits)                 ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
206
#define GREEN(x, bits)               ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
207
#define BLUE(x, bits)                ((x >> (8 - bits)) & ((1 << bits) - 1))
1363 vana 208
 
3707 decky 209
#define COL2X(col)                   ((col) * FONT_WIDTH)
210
#define ROW2Y(row)                   ((row) * FONT_SCANLINES)
1363 vana 211
 
3707 decky 212
#define X2COL(x)                     ((x) / FONT_WIDTH)
213
#define Y2ROW(y)                     ((y) / FONT_SCANLINES)
1363 vana 214
 
3707 decky 215
#define FB_POS(x, y)                 ((y) * screen.scanline + (x) * screen.pixelbytes)
216
#define BB_POS(vport, col, row)      ((row) * vport->cols + (col))
217
#define GLYPH_POS(glyph, y, cursor)  (((glyph) + (cursor) * FONT_GLYPHS) * screen.glyphbytes + (y) * screen.glyphscanline)
1875 jermar 218
 
1363 vana 219
 
3707 decky 220
/** ARGB 8:8:8:8 conversion
221
 *
222
 */
223
static void rgb_0888(void *dst, uint32_t rgb)
1363 vana 224
{
3707 decky 225
	*((uint32_t *) dst) = rgb & 0xffffff;
1363 vana 226
}
227
 
1994 decky 228
 
3707 decky 229
/** ABGR 8:8:8:8 conversion
230
 *
231
 */
232
static void bgr_0888(void *dst, uint32_t rgb)
1994 decky 233
{
3707 decky 234
	*((uint32_t *) dst)
235
	    = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
1994 decky 236
}
237
 
3707 decky 238
 
3882 decky 239
/** RGB 8:8:8 conversion
3707 decky 240
 *
241
 */
242
static void rgb_888(void *dst, uint32_t rgb)
1363 vana 243
{
3710 decky 244
	((uint8_t *) dst)[0] = BLUE(rgb, 8);
245
	((uint8_t *) dst)[1] = GREEN(rgb, 8);
246
	((uint8_t *) dst)[2] = RED(rgb, 8);
1363 vana 247
}
248
 
249
 
3882 decky 250
/** BGR 8:8:8 conversion
251
 *
252
 */
253
static void bgr_888(void *dst, uint32_t rgb)
254
{
255
	((uint8_t *) dst)[0] = RED(rgb, 8);
256
	((uint8_t *) dst)[1] = GREEN(rgb, 8);
257
	((uint8_t *) dst)[2] = BLUE(rgb, 8);
258
}
259
 
260
 
3707 decky 261
/** RGB 5:5:5 conversion
262
 *
263
 */
264
static void rgb_555(void *dst, uint32_t rgb)
1993 decky 265
{
3707 decky 266
	*((uint16_t *) dst)
267
	    = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
1993 decky 268
}
269
 
270
 
3707 decky 271
/** RGB 5:6:5 conversion
272
 *
273
 */
274
static void rgb_565(void *dst, uint32_t rgb)
1363 vana 275
{
3707 decky 276
	*((uint16_t *) dst)
277
	    = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
1363 vana 278
}
279
 
280
 
3707 decky 281
/** RGB 3:2:3
282
 *
283
 */
284
static void rgb_323(void *dst, uint32_t rgb)
1363 vana 285
{
3707 decky 286
	*((uint8_t *) dst)
287
	    = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
1363 vana 288
}
289
 
3725 svoboda 290
/** Draw a filled rectangle.
291
 *
292
 * @note Need real implementation that does not access VRAM twice.
293
 */
3723 svoboda 294
static void draw_filled_rect(unsigned int x0, unsigned int y0, unsigned int x1,
295
    unsigned int y1, uint32_t color)
296
{
297
	unsigned int x, y;
3725 svoboda 298
	unsigned int copy_bytes;
299
 
300
	uint8_t *sp, *dp;
3723 svoboda 301
	uint8_t cbuf[4];
1363 vana 302
 
3725 svoboda 303
	if (y0 >= y1 || x0 >= x1) return;
3723 svoboda 304
	screen.rgb_conv(cbuf, color);
305
 
3725 svoboda 306
	sp = &screen.fb_addr[FB_POS(x0, y0)];
307
	dp = sp;
308
 
309
	/* Draw the first line. */
310
	for (x = x0; x < x1; x++) {
311
		memcpy(dp, cbuf, screen.pixelbytes);
312
		dp += screen.pixelbytes;
3723 svoboda 313
	}
3725 svoboda 314
 
315
	dp = sp + screen.scanline;
316
	copy_bytes = (x1 - x0) * screen.pixelbytes;
317
 
318
	/* Draw the remaining lines by copying. */
319
	for (y = y0 + 1; y < y1; y++) {
320
		memcpy(dp, sp, copy_bytes);
321
		dp += screen.scanline;
322
	}
3723 svoboda 323
}
324
 
325
/** Redraw viewport.
1498 palkovsky 326
 *
3707 decky 327
 * @param vport Viewport to redraw
328
 *
1498 palkovsky 329
 */
3707 decky 330
static void vport_redraw(viewport_t *vport)
1485 palkovsky 331
{
3723 svoboda 332
	unsigned int row, col;
333
 
3707 decky 334
	for (row = 0; row < vport->rows; row++) {
3723 svoboda 335
		for (col = 0; col < vport->cols; col++) {
3739 svoboda 336
			draw_vp_glyph(vport, false, col, row);
3707 decky 337
		}
1672 palkovsky 338
	}
3723 svoboda 339
 
3707 decky 340
	if (COL2X(vport->cols) < vport->width) {
3723 svoboda 341
		draw_filled_rect(
342
		    vport->x + COL2X(vport->cols), vport->y,
343
		    vport->x + vport->width, vport->y + vport->height,
3767 svoboda 344
		    vport->attr.bg_color);
1672 palkovsky 345
	}
3723 svoboda 346
 
3707 decky 347
	if (ROW2Y(vport->rows) < vport->height) {
3723 svoboda 348
		draw_filled_rect(
349
		    vport->x, vport->y + ROW2Y(vport->rows),
350
		    vport->x + vport->width, vport->y + vport->height,
3767 svoboda 351
		    vport->attr.bg_color);
1672 palkovsky 352
	}
1559 palkovsky 353
}
354
 
3767 svoboda 355
static void backbuf_clear(bb_cell_t *backbuf, size_t len, uint32_t fg_color,
356
    uint32_t bg_color)
357
{
358
	unsigned i;
3707 decky 359
 
3767 svoboda 360
	for (i = 0; i < len; i++) {
361
		backbuf[i].glyph = 0;
362
		backbuf[i].fg_color = fg_color;
363
		backbuf[i].bg_color = bg_color;
364
	}
365
}
366
 
3724 svoboda 367
/** Clear viewport.
3707 decky 368
 *
369
 * @param vport Viewport to clear
370
 *
371
 */
372
static void vport_clear(viewport_t *vport)
1363 vana 373
{
3767 svoboda 374
	backbuf_clear(vport->backbuf, vport->cols * vport->rows,
375
	    vport->attr.fg_color, vport->attr.bg_color);
3707 decky 376
	vport_redraw(vport);
1363 vana 377
}
378
 
3724 svoboda 379
/** Scroll viewport by the specified number of lines.
1485 palkovsky 380
 *
1709 jermar 381
 * @param vport Viewport to scroll
3707 decky 382
 * @param lines Number of lines to scroll
383
 *
1485 palkovsky 384
 */
3707 decky 385
static void vport_scroll(viewport_t *vport, int lines)
1363 vana 386
{
3726 svoboda 387
	unsigned int row, col;
3739 svoboda 388
	unsigned int x, y;
389
	uint8_t glyph;
3767 svoboda 390
	uint32_t fg_color;
391
	uint32_t bg_color;
392
	bb_cell_t *bbp, *xbp;
3726 svoboda 393
 
3724 svoboda 394
	/*
3739 svoboda 395
	 * Redraw.
396
	 */
397
 
398
	y = vport->y;
399
	for (row = 0; row < vport->rows; row++) {
400
		x = vport->x;
401
		for (col = 0; col < vport->cols; col++) {
402
			if ((row + lines >= 0) && (row + lines < vport->rows)) {
3767 svoboda 403
				xbp = &vport->backbuf[BB_POS(vport, col, row + lines)];
404
				bbp = &vport->backbuf[BB_POS(vport, col, row)];
3739 svoboda 405
 
3767 svoboda 406
				glyph = xbp->glyph;
407
				fg_color = xbp->fg_color;
408
				bg_color = xbp->bg_color;
409
 
410
				if (bbp->glyph == glyph &&
411
				    bbp->fg_color == xbp->fg_color &&
412
				    bbp->bg_color == xbp->bg_color) {
3739 svoboda 413
					x += FONT_WIDTH;
414
					continue;
415
				}
416
			} else {
417
				glyph = 0;
3767 svoboda 418
				fg_color = vport->attr.fg_color;
419
				bg_color = vport->attr.bg_color;
3739 svoboda 420
			}
421
 
3744 svoboda 422
			(*vport->dglyph)(x, y, false, vport->glyphs, glyph,
3767 svoboda 423
			    fg_color, bg_color);
3739 svoboda 424
			x += FONT_WIDTH;
425
		}
426
		y += FONT_SCANLINES;
427
	}
428
 
429
	/*
3724 svoboda 430
	 * Scroll backbuffer.
431
	 */
3723 svoboda 432
 
1672 palkovsky 433
	if (lines > 0) {
3739 svoboda 434
		memmove(vport->backbuf, vport->backbuf + vport->cols * lines,
3767 svoboda 435
		    vport->cols * (vport->rows - lines) * sizeof(bb_cell_t));
436
		backbuf_clear(&vport->backbuf[BB_POS(vport, 0, vport->rows - lines)],
437
		    vport->cols * lines, vport->attr.fg_color, vport->attr.bg_color);
3707 decky 438
	} else {
3739 svoboda 439
		memmove(vport->backbuf - vport->cols * lines, vport->backbuf,
3767 svoboda 440
		    vport->cols * (vport->rows + lines) * sizeof(bb_cell_t));
441
		backbuf_clear(vport->backbuf, - vport->cols * lines,
442
		    vport->attr.fg_color, vport->attr.bg_color);
1672 palkovsky 443
	}
444
}
445
 
3707 decky 446
/** Render glyphs
1558 palkovsky 447
 *
3707 decky 448
 * Convert glyphs from device independent font
449
 * description to current visual representation.
450
 *
451
 * @param vport Viewport
452
 *
1558 palkovsky 453
 */
3707 decky 454
static void render_glyphs(viewport_t* vport)
1363 vana 455
{
3707 decky 456
	unsigned int glyph;
457
 
458
	for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
459
		unsigned int y;
460
 
461
		for (y = 0; y < FONT_SCANLINES; y++) {
462
			unsigned int x;
463
 
464
			for (x = 0; x < FONT_WIDTH; x++) {
465
				screen.rgb_conv(&vport->glyphs[GLYPH_POS(glyph, y, false) + x * screen.pixelbytes],
466
				    (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x)))
3744 svoboda 467
				    ? 0xffffff : 0x000000);
3707 decky 468
 
3744 svoboda 469
				screen.rgb_conv(&vport->glyphs[GLYPH_POS(glyph, y, true) + x * screen.pixelbytes],
470
				    (fb_font[glyph * FONT_SCANLINES + y] & (1 << (7 - x)))
471
				    ? 0x000000 : 0xffffff);
3707 decky 472
			}
1509 palkovsky 473
		}
474
	}
3707 decky 475
 
3767 svoboda 476
	screen.rgb_conv(vport->bgpixel, vport->attr.bg_color);
1363 vana 477
}
478
 
479
 
1485 palkovsky 480
/** Create new viewport
1363 vana 481
 *
3707 decky 482
 * @param x      Origin of the viewport (x).
483
 * @param y      Origin of the viewport (y).
484
 * @param width  Width of the viewport.
485
 * @param height Height of the viewport.
486
 *
487
 * @return New viewport number.
488
 *
1363 vana 489
 */
3707 decky 490
static int vport_create(unsigned int x, unsigned int y,
491
    unsigned int width, unsigned int height)
1363 vana 492
{
3707 decky 493
	unsigned int i;
494
 
2070 jermar 495
	for (i = 0; i < MAX_VIEWPORTS; i++) {
1485 palkovsky 496
		if (!viewports[i].initialized)
1363 vana 497
			break;
498
	}
1485 palkovsky 499
	if (i == MAX_VIEWPORTS)
500
		return ELIMIT;
3707 decky 501
 
502
	unsigned int cols = width / FONT_WIDTH;
503
	unsigned int rows = height / FONT_SCANLINES;
3767 svoboda 504
	unsigned int bbsize = cols * rows * sizeof(bb_cell_t);
3707 decky 505
	unsigned int glyphsize = 2 * FONT_GLYPHS * screen.glyphbytes;
3744 svoboda 506
	unsigned int word_size = sizeof(unsigned long);
3707 decky 507
 
3767 svoboda 508
	bb_cell_t *backbuf = (bb_cell_t *) malloc(bbsize);
3707 decky 509
	if (!backbuf)
510
		return ENOMEM;
511
 
512
	uint8_t *glyphs = (uint8_t *) malloc(glyphsize);
513
	if (!glyphs) {
514
		free(backbuf);
515
		return ENOMEM;
516
	}
517
 
518
	uint8_t *bgpixel = (uint8_t *) malloc(screen.pixelbytes);
519
	if (!bgpixel) {
520
		free(glyphs);
521
		free(backbuf);
522
		return ENOMEM;
523
	}
3767 svoboda 524
 
525
	backbuf_clear(backbuf, cols * rows, DEFAULT_FGCOLOR, DEFAULT_BGCOLOR);
3707 decky 526
	memset(glyphs, 0, glyphsize);
527
	memset(bgpixel, 0, screen.pixelbytes);
528
 
1485 palkovsky 529
	viewports[i].x = x;
530
	viewports[i].y = y;
531
	viewports[i].width = width;
532
	viewports[i].height = height;
1363 vana 533
 
3707 decky 534
	viewports[i].cols = cols;
535
	viewports[i].rows = rows;
536
 
3767 svoboda 537
	viewports[i].attr.bg_color = DEFAULT_BGCOLOR;
538
	viewports[i].attr.fg_color = DEFAULT_FGCOLOR;
1363 vana 539
 
3707 decky 540
	viewports[i].glyphs = glyphs;
541
	viewports[i].bgpixel = bgpixel;
3744 svoboda 542
 
543
	/*
544
	 * Conditions necessary  to select aligned version:
545
	 *
546
	 *   - word size is divisible by pixelbytes
547
	 *   - cell scanline size is divisible by word size
548
	 *   - cell scanlines are word-aligned
549
	 */
550
	if ((word_size % screen.pixelbytes) == 0 &&
551
	    (FONT_WIDTH * screen.pixelbytes) % word_size == 0 &&
552
	    (x * screen.pixelbytes) % word_size == 0 &&
553
	    screen.scanline % word_size == 0) {
554
 
555
		viewports[i].dglyph = draw_glyph_aligned;
556
	} else {
557
		viewports[i].dglyph = draw_glyph_fallback;
558
	}
559
 
1489 palkovsky 560
	viewports[i].cur_col = 0;
561
	viewports[i].cur_row = 0;
3707 decky 562
	viewports[i].cursor_active = false;
563
	viewports[i].cursor_shown = false;
564
 
565
	viewports[i].bbsize = bbsize;
566
	viewports[i].backbuf = backbuf;
567
 
568
	viewports[i].initialized = true;
569
 
570
	render_glyphs(&viewports[i]);
571
 
1485 palkovsky 572
	return i;
1363 vana 573
}
574
 
3707 decky 575
 
1363 vana 576
/** Initialize framebuffer as a chardev output device
577
 *
3707 decky 578
 * @param addr   Address of the framebuffer
579
 * @param xres   Screen width in pixels
580
 * @param yres   Screen height in pixels
581
 * @param visual Bits per pixel (8, 16, 24, 32)
582
 * @param scan   Bytes per one scanline
1363 vana 583
 *
584
 */
3707 decky 585
static bool screen_init(void *addr, unsigned int xres, unsigned int yres,
586
    unsigned int scan, unsigned int visual)
1363 vana 587
{
1993 decky 588
	switch (visual) {
589
	case VISUAL_INDIRECT_8:
3707 decky 590
		screen.rgb_conv = rgb_323;
1886 jermar 591
		screen.pixelbytes = 1;
592
		break;
1993 decky 593
	case VISUAL_RGB_5_5_5:
3707 decky 594
		screen.rgb_conv = rgb_555;
1886 jermar 595
		screen.pixelbytes = 2;
596
		break;
1993 decky 597
	case VISUAL_RGB_5_6_5:
3707 decky 598
		screen.rgb_conv = rgb_565;
1993 decky 599
		screen.pixelbytes = 2;
1886 jermar 600
		break;
1993 decky 601
	case VISUAL_RGB_8_8_8:
3707 decky 602
		screen.rgb_conv = rgb_888;
1993 decky 603
		screen.pixelbytes = 3;
604
		break;
3882 decky 605
	case VISUAL_BGR_8_8_8:
606
		screen.rgb_conv = bgr_888;
607
		screen.pixelbytes = 3;
608
		break;
1993 decky 609
	case VISUAL_RGB_8_8_8_0:
3707 decky 610
		screen.rgb_conv = rgb_888;
1886 jermar 611
		screen.pixelbytes = 4;
612
		break;
1993 decky 613
	case VISUAL_RGB_0_8_8_8:
3707 decky 614
		screen.rgb_conv = rgb_0888;
1993 decky 615
		screen.pixelbytes = 4;
616
		break;
1994 decky 617
	case VISUAL_BGR_0_8_8_8:
3707 decky 618
		screen.rgb_conv = bgr_0888;
1994 decky 619
		screen.pixelbytes = 4;
620
		break;
1993 decky 621
	default:
622
		return false;
1363 vana 623
	}
624
 
3707 decky 625
	screen.fb_addr = (unsigned char *) addr;
1485 palkovsky 626
	screen.xres = xres;
627
	screen.yres = yres;
628
	screen.scanline = scan;
1363 vana 629
 
3707 decky 630
	screen.glyphscanline = FONT_WIDTH * screen.pixelbytes;
631
	screen.glyphbytes = screen.glyphscanline * FONT_SCANLINES;
632
 
1485 palkovsky 633
	/* Create first viewport */
3707 decky 634
	vport_create(0, 0, xres, yres);
1993 decky 635
 
636
	return true;
1485 palkovsky 637
}
1363 vana 638
 
3707 decky 639
 
3744 svoboda 640
/** Draw a glyph, takes advantage of alignment.
3707 decky 641
 *
3744 svoboda 642
 * This version can only be used if the following conditions are met:
3739 svoboda 643
 *
3744 svoboda 644
 *   - word size is divisible by pixelbytes
645
 *   - cell scanline size is divisible by word size
646
 *   - cell scanlines are word-aligned
647
 *
648
 * It makes use of the pre-rendered mask to process (possibly) several
649
 * pixels at once (word size / pixelbytes pixels at a time are processed)
650
 * making it very fast. Most notably this version is not applicable at 24 bits
651
 * per pixel.
652
 *
653
 * @param x		x coordinate of top-left corner on screen.
654
 * @param y		y coordinate of top-left corner on screen.
655
 * @param cursor	Draw glyph with cursor
656
 * @param glyphs	Pointer to font bitmap.
657
 * @param glyph		Code of the glyph to draw.
658
 * @param fg_color	Foreground color.
659
 * @param bg_color	Backgroudn color.
3739 svoboda 660
 */
3744 svoboda 661
static void draw_glyph_aligned(unsigned int x, unsigned int y, bool cursor,
4211 svoboda 662
    uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color)
3739 svoboda 663
{
3744 svoboda 664
	unsigned int i, yd;
665
	unsigned long fg_buf, bg_buf;
666
	unsigned long *maskp, *dp;
667
	unsigned long mask;
668
	unsigned int ww, d_add;
669
 
4211 svoboda 670
	/* Check glyph range. */
671
	if (glyph >= FONT_GLYPHS)
672
		return;
673
 
3744 svoboda 674
	/*
675
	 * Prepare a pair of words, one filled with foreground-color
676
	 * pattern and the other filled with background-color pattern.
677
	 */
678
	for (i = 0; i < sizeof(unsigned long) / screen.pixelbytes; i++) {
679
		screen.rgb_conv(&((uint8_t *)&fg_buf)[i * screen.pixelbytes],
680
		    fg_color);
681
		screen.rgb_conv(&((uint8_t *)&bg_buf)[i * screen.pixelbytes],
682
		    bg_color);
683
	}
684
 
685
	/* Pointer to the current position in the mask. */
686
	maskp = (unsigned long *) &glyphs[GLYPH_POS(glyph, 0, cursor)];
687
 
688
	/* Pointer to the current position on the screen. */
689
	dp = (unsigned long *) &screen.fb_addr[FB_POS(x, y)];
690
 
691
	/* Width of the character cell in words. */
692
	ww = FONT_WIDTH * screen.pixelbytes / sizeof(unsigned long);
693
 
694
	/* Offset to add when moving to another screen scanline. */
695
	d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
696
 
697
	for (yd = 0; yd < FONT_SCANLINES; yd++) {
698
		/*
699
		 * Now process the cell scanline, combining foreground
700
		 * and background color patters using the pre-rendered mask.
701
		 */
702
		for (i = 0; i < ww; i++) {
703
			mask = *maskp++;
704
			*dp++ = (fg_buf & mask) | (bg_buf & ~mask);
705
		}
706
 
707
		/* Move to the beginning of the next scanline of the cell. */
708
		dp = (unsigned long *) ((uint8_t *) dp + d_add);
709
	}
3739 svoboda 710
}
711
 
3744 svoboda 712
/** Draw a glyph, fallback version.
713
 *
714
 * This version does not make use of the pre-rendered mask, it uses
715
 * the font bitmap directly. It works always, but it is slower.
716
 *
717
 * @param x		x coordinate of top-left corner on screen.
718
 * @param y		y coordinate of top-left corner on screen.
719
 * @param cursor	Draw glyph with cursor
720
 * @param glyphs	Pointer to font bitmap.
721
 * @param glyph		Code of the glyph to draw.
722
 * @param fg_color	Foreground color.
723
 * @param bg_color	Backgroudn color.
724
 */
725
void draw_glyph_fallback(unsigned int x, unsigned int y, bool cursor,
4211 svoboda 726
    uint8_t *glyphs, uint32_t glyph, uint32_t fg_color, uint32_t bg_color)
3744 svoboda 727
{
728
	unsigned int i, j, yd;
729
	uint8_t fg_buf[4], bg_buf[4];
730
	uint8_t *dp, *sp;
731
	unsigned int d_add;
732
	uint8_t b;
733
 
4211 svoboda 734
	/* Check glyph range. */
735
	if (glyph >= FONT_GLYPHS)
736
		return;
737
 
3744 svoboda 738
	/* Pre-render 1x the foreground and background color pixels. */
739
	if (cursor) {
740
		screen.rgb_conv(fg_buf, bg_color);
741
		screen.rgb_conv(bg_buf, fg_color);
742
	} else {
743
		screen.rgb_conv(fg_buf, fg_color);
744
		screen.rgb_conv(bg_buf, bg_color);
745
	}
746
 
747
	/* Pointer to the current position on the screen. */
748
	dp = (uint8_t *) &screen.fb_addr[FB_POS(x, y)];
749
 
750
	/* Offset to add when moving to another screen scanline. */
751
	d_add = screen.scanline - FONT_WIDTH * screen.pixelbytes;
752
 
753
	for (yd = 0; yd < FONT_SCANLINES; yd++) {
754
		/* Byte containing bits of the glyph scanline. */
755
		b = fb_font[glyph * FONT_SCANLINES + yd];
756
 
757
		for (i = 0; i < FONT_WIDTH; i++) {
758
			/* Choose color based on the current bit. */
759
			sp = (b & 0x80) ? fg_buf : bg_buf;
760
 
761
			/* Copy the pixel. */
762
			for (j = 0; j < screen.pixelbytes; j++) {
763
				*dp++ = *sp++;
764
			}
765
 
766
			/* Move to the next bit. */
767
			b = b << 1;
768
		}
769
 
770
		/* Move to the beginning of the next scanline of the cell. */
771
		dp += d_add;
772
	}
773
}
774
 
3739 svoboda 775
/** Draw glyph at specified position in viewport. 
776
 *
3707 decky 777
 * @param vport  Viewport identification
778
 * @param cursor Draw glyph with cursor
779
 * @param col    Screen position relative to viewport
780
 * @param row    Screen position relative to viewport
781
 *
782
 */
3739 svoboda 783
static void draw_vp_glyph(viewport_t *vport, bool cursor, unsigned int col,
784
    unsigned int row)
1500 palkovsky 785
{
3707 decky 786
	unsigned int x = vport->x + COL2X(col);
787
	unsigned int y = vport->y + ROW2Y(row);
3767 svoboda 788
 
4211 svoboda 789
	uint32_t glyph;
3767 svoboda 790
	uint32_t fg_color;
791
	uint32_t bg_color;
3707 decky 792
 
3767 svoboda 793
	glyph = vport->backbuf[BB_POS(vport, col, row)].glyph;
794
	fg_color = vport->backbuf[BB_POS(vport, col, row)].fg_color;
795
	bg_color = vport->backbuf[BB_POS(vport, col, row)].bg_color;
3744 svoboda 796
 
797
	(*vport->dglyph)(x, y, cursor, vport->glyphs, glyph,
3767 svoboda 798
	    fg_color, bg_color);
3707 decky 799
}
800
 
801
/** Hide cursor if it is shown
802
 *
803
 */
804
static void cursor_hide(viewport_t *vport)
805
{
806
	if ((vport->cursor_active) && (vport->cursor_shown)) {
3739 svoboda 807
		draw_vp_glyph(vport, false, vport->cur_col, vport->cur_row);
3707 decky 808
		vport->cursor_shown = false;
1500 palkovsky 809
	}
810
}
811
 
3707 decky 812
 
813
/** Show cursor if cursor showing is enabled
814
 *
815
 */
816
static void cursor_show(viewport_t *vport)
1500 palkovsky 817
{
818
	/* Do not check for cursor_shown */
819
	if (vport->cursor_active) {
3739 svoboda 820
		draw_vp_glyph(vport, true, vport->cur_col, vport->cur_row);
3707 decky 821
		vport->cursor_shown = true;
1500 palkovsky 822
	}
823
}
824
 
3707 decky 825
 
826
/** Invert cursor, if it is enabled
827
 *
828
 */
829
static void cursor_blink(viewport_t *vport)
1500 palkovsky 830
{
831
	if (vport->cursor_shown)
1673 palkovsky 832
		cursor_hide(vport);
1500 palkovsky 833
	else
3707 decky 834
		cursor_show(vport);
1500 palkovsky 835
}
836
 
3707 decky 837
 
838
/** Draw character at given position relative to viewport
839
 *
840
 * @param vport  Viewport identification
841
 * @param c      Character to draw
842
 * @param col    Screen position relative to viewport
843
 * @param row    Screen position relative to viewport
844
 *
1498 palkovsky 845
 */
4211 svoboda 846
static void draw_char(viewport_t *vport, wchar_t c, unsigned int col, unsigned int row)
1486 palkovsky 847
{
3767 svoboda 848
	bb_cell_t *bbp;
849
 
3707 decky 850
	/* Do not hide cursor if we are going to overwrite it */
851
	if ((vport->cursor_active) && (vport->cursor_shown) &&
852
	    ((vport->cur_col != col) || (vport->cur_row != row)))
853
		cursor_hide(vport);
3767 svoboda 854
 
855
	bbp = &vport->backbuf[BB_POS(vport, col, row)];
4211 svoboda 856
	bbp->glyph = (uint32_t) c;
3767 svoboda 857
	bbp->fg_color = vport->attr.fg_color;
858
	bbp->bg_color = vport->attr.bg_color;
859
 
3739 svoboda 860
	draw_vp_glyph(vport, false, col, row);
3707 decky 861
 
1489 palkovsky 862
	vport->cur_col = col;
863
	vport->cur_row = row;
3707 decky 864
 
1489 palkovsky 865
	vport->cur_col++;
2025 jermar 866
	if (vport->cur_col >= vport->cols) {
1489 palkovsky 867
		vport->cur_col = 0;
868
		vport->cur_row++;
869
		if (vport->cur_row >= vport->rows)
870
			vport->cur_row--;
1486 palkovsky 871
	}
3707 decky 872
 
873
	cursor_show(vport);
1486 palkovsky 874
}
875
 
4167 svoboda 876
/** Draw text data to viewport.
1552 palkovsky 877
 *
1709 jermar 878
 * @param vport Viewport id
4167 svoboda 879
 * @param data  Text data.
880
 * @param x	Leftmost column of the area.
881
 * @param y	Topmost row of the area.
882
 * @param w	Number of rows.
883
 * @param h	Number of columns.
1552 palkovsky 884
 */
4167 svoboda 885
static void draw_text_data(viewport_t *vport, keyfield_t *data, unsigned int x,
886
    unsigned int y, unsigned int w, unsigned int h)
1505 palkovsky 887
{
4167 svoboda 888
	unsigned int i, j;
3767 svoboda 889
	bb_cell_t *bbp;
890
	attrs_t *a;
891
	attr_rgb_t rgb;
892
 
4167 svoboda 893
	for (j = 0; j < h; j++) {
894
		for (i = 0; i < w; i++) {
895
			unsigned int col = x + i;
896
			unsigned int row = y + j;
3767 svoboda 897
 
4167 svoboda 898
			bbp = &vport->backbuf[BB_POS(vport, col, row)];
4211 svoboda 899
			uint32_t glyph = bbp->glyph;
3767 svoboda 900
 
4167 svoboda 901
			a = &data[j * w + i].attrs;
902
			rgb_from_attr(&rgb, a);
903
 
904
			bbp->glyph = data[j * w + i].character;
905
			bbp->fg_color = rgb.fg_color;
906
			bbp->bg_color = rgb.bg_color;
907
 
908
			draw_vp_glyph(vport, false, col, row);
909
		}
1505 palkovsky 910
	}
3707 decky 911
	cursor_show(vport);
1505 palkovsky 912
}
913
 
3707 decky 914
 
915
static void putpixel_pixmap(void *data, unsigned int x, unsigned int y, uint32_t color)
1555 palkovsky 916
{
3707 decky 917
	int pm = *((int *) data);
918
	pixmap_t *pmap = &pixmaps[pm];
919
	unsigned int pos = (y * pmap->width + x) * screen.pixelbytes;
1555 palkovsky 920
 
3707 decky 921
	screen.rgb_conv(&pmap->data[pos], color);
922
}
923
 
924
 
925
static void putpixel(void *data, unsigned int x, unsigned int y, uint32_t color)
926
{
927
	viewport_t *vport = (viewport_t *) data;
928
	unsigned int dx = vport->x + x;
929
	unsigned int dy = vport->y + y;
930
 
931
	screen.rgb_conv(&screen.fb_addr[FB_POS(dx, dy)], color);
932
}
933
 
934
 
935
/** Return first free pixmap
936
 *
937
 */
938
static int find_free_pixmap(void)
939
{
940
	unsigned int i;
941
 
942
	for (i = 0; i < MAX_PIXMAPS; i++)
1555 palkovsky 943
		if (!pixmaps[i].data)
944
			return i;
3707 decky 945
 
1555 palkovsky 946
	return -1;
947
}
948
 
949
 
3707 decky 950
/** Create a new pixmap and return appropriate ID
951
 *
952
 */
953
static int shm2pixmap(unsigned char *shm, size_t size)
1555 palkovsky 954
{
955
	int pm;
956
	pixmap_t *pmap;
3707 decky 957
 
1555 palkovsky 958
	pm = find_free_pixmap();
959
	if (pm == -1)
960
		return ELIMIT;
3707 decky 961
 
1555 palkovsky 962
	pmap = &pixmaps[pm];
963
 
964
	if (ppm_get_data(shm, size, &pmap->width, &pmap->height))
965
		return EINVAL;
966
 
967
	pmap->data = malloc(pmap->width * pmap->height * screen.pixelbytes);
968
	if (!pmap->data)
969
		return ENOMEM;
3707 decky 970
 
971
	ppm_draw(shm, size, 0, 0, pmap->width, pmap->height, putpixel_pixmap, (void *) &pm);
972
 
1555 palkovsky 973
	return pm;
974
}
975
 
3707 decky 976
 
1552 palkovsky 977
/** Handle shared memory communication calls
978
 *
979
 * Protocol for drawing pixmaps:
980
 * - FB_PREPARE_SHM(client shm identification)
2025 jermar 981
 * - IPC_M_AS_AREA_SEND
3707 decky 982
 * - FB_DRAW_PPM(startx, starty)
1552 palkovsky 983
 * - FB_DROP_SHM
984
 *
985
 * Protocol for text drawing
2025 jermar 986
 * - IPC_M_AS_AREA_SEND
1552 palkovsky 987
 * - FB_DRAW_TEXT_DATA
988
 *
989
 * @param callid Callid of the current call
3707 decky 990
 * @param call   Current call data
991
 * @param vp     Active viewport
1552 palkovsky 992
 *
3707 decky 993
 * @return false if the call was not handled byt this function, true otherwise
994
 *
995
 * Note: this function is not threads safe, you would have
1552 palkovsky 996
 * to redefine static variables with __thread
3707 decky 997
 *
1552 palkovsky 998
 */
3707 decky 999
static bool shm_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1547 palkovsky 1000
{
1001
	static keyfield_t *interbuffer = NULL;
1002
	static size_t intersize = 0;
3707 decky 1003
 
1720 palkovsky 1004
	static unsigned char *shm = NULL;
1555 palkovsky 1005
	static ipcarg_t shm_id = 0;
1006
	static size_t shm_size;
3707 decky 1007
 
1008
	bool handled = true;
1009
	int retval = EOK;
1547 palkovsky 1010
	viewport_t *vport = &viewports[vp];
3707 decky 1011
	unsigned int x;
1012
	unsigned int y;
4167 svoboda 1013
	unsigned int w;
1014
	unsigned int h;
3707 decky 1015
 
1547 palkovsky 1016
	switch (IPC_GET_METHOD(*call)) {
2677 jermar 1017
	case IPC_M_SHARE_OUT:
1547 palkovsky 1018
		/* We accept one area for data interchange */
1555 palkovsky 1019
		if (IPC_GET_ARG1(*call) == shm_id) {
2141 jermar 1020
			void *dest = as_get_mappable_page(IPC_GET_ARG2(*call));
1555 palkovsky 1021
			shm_size = IPC_GET_ARG2(*call);
3707 decky 1022
			if (!ipc_answer_1(callid, EOK, (sysarg_t) dest))
1555 palkovsky 1023
				shm = dest;
1547 palkovsky 1024
			else
1555 palkovsky 1025
				shm_id = 0;
3707 decky 1026
 
1555 palkovsky 1027
			if (shm[0] != 'P')
3707 decky 1028
				return false;
1029
 
1030
			return true;
1547 palkovsky 1031
		} else {
1032
			intersize = IPC_GET_ARG2(*call);
2015 jermar 1033
			receive_comm_area(callid, call, (void *) &interbuffer);
1547 palkovsky 1034
		}
3707 decky 1035
		return true;
1547 palkovsky 1036
	case FB_PREPARE_SHM:
1555 palkovsky 1037
		if (shm_id)
1547 palkovsky 1038
			retval = EBUSY;
1039
		else 
1555 palkovsky 1040
			shm_id = IPC_GET_ARG1(*call);
1547 palkovsky 1041
		break;
1042
 
1043
	case FB_DROP_SHM:
1555 palkovsky 1044
		if (shm) {
1045
			as_area_destroy(shm);
1046
			shm = NULL;
1547 palkovsky 1047
		}
1555 palkovsky 1048
		shm_id = 0;
1547 palkovsky 1049
		break;
3707 decky 1050
 
1555 palkovsky 1051
	case FB_SHM2PIXMAP:
1052
		if (!shm) {
1053
			retval = EINVAL;
1054
			break;
1055
		}
1056
		retval = shm2pixmap(shm, shm_size);
1057
		break;
1547 palkovsky 1058
	case FB_DRAW_PPM:
1555 palkovsky 1059
		if (!shm) {
1547 palkovsky 1060
			retval = EINVAL;
1061
			break;
1062
		}
1063
		x = IPC_GET_ARG1(*call);
1064
		y = IPC_GET_ARG2(*call);
3707 decky 1065
 
1066
		if ((x > vport->width) || (y > vport->height)) {
1547 palkovsky 1067
			retval = EINVAL;
1068
			break;
1069
		}
1070
 
2025 jermar 1071
		ppm_draw(shm, shm_size, IPC_GET_ARG1(*call),
3707 decky 1072
		    IPC_GET_ARG2(*call), vport->width - x, vport->height - y, putpixel, (void *) vport);
1547 palkovsky 1073
		break;
1074
	case FB_DRAW_TEXT_DATA:
4167 svoboda 1075
		x = IPC_GET_ARG1(*call);
1076
		y = IPC_GET_ARG2(*call);
1077
		w = IPC_GET_ARG3(*call);
1078
		h = IPC_GET_ARG4(*call);
1547 palkovsky 1079
		if (!interbuffer) {
1080
			retval = EINVAL;
1081
			break;
1082
		}
4167 svoboda 1083
		if (x + w > vport->cols || y + h > vport->rows) {
1547 palkovsky 1084
			retval = EINVAL;
1085
			break;
1086
		}
4167 svoboda 1087
		if (intersize < w * h * sizeof(*interbuffer)) {
1088
			retval = EINVAL;
1089
			break;
1090
		}
1091
		draw_text_data(vport, interbuffer, x, y, w, h);
1547 palkovsky 1092
		break;
1093
	default:
3707 decky 1094
		handled = false;
1547 palkovsky 1095
	}
1096
 
1097
	if (handled)
2619 jermar 1098
		ipc_answer_0(callid, retval);
1547 palkovsky 1099
	return handled;
1100
}
1101
 
3707 decky 1102
 
1103
static void copy_vp_to_pixmap(viewport_t *vport, pixmap_t *pmap)
1552 palkovsky 1104
{
3707 decky 1105
	unsigned int width = vport->width;
1106
	unsigned int height = vport->height;
1107
 
1711 palkovsky 1108
	if (width + vport->x > screen.xres)
1109
		width = screen.xres - vport->x;
2301 decky 1110
	if (height + vport->y > screen.yres)
1711 palkovsky 1111
		height = screen.yres - vport->y;
2301 decky 1112
 
3707 decky 1113
	unsigned int realwidth = pmap->width <= width ? pmap->width : width;
1114
	unsigned int realheight = pmap->height <= height ? pmap->height : height;
2301 decky 1115
 
3707 decky 1116
	unsigned int srcrowsize = vport->width * screen.pixelbytes;
1117
	unsigned int realrowsize = realwidth * screen.pixelbytes;
1118
 
1119
	unsigned int y;
2301 decky 1120
	for (y = 0; y < realheight; y++) {
3707 decky 1121
		unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
1122
		memcpy(pmap->data + srcrowsize * y, screen.fb_addr + tmp, realrowsize);
1711 palkovsky 1123
	}
1124
}
1125
 
3707 decky 1126
 
1127
/** Save viewport to pixmap
1128
 *
1129
 */
1130
static int save_vp_to_pixmap(viewport_t *vport)
1711 palkovsky 1131
{
1132
	int pm;
1133
	pixmap_t *pmap;
3707 decky 1134
 
1552 palkovsky 1135
	pm = find_free_pixmap();
1136
	if (pm == -1)
1137
		return ELIMIT;
1138
 
1139
	pmap = &pixmaps[pm];
1140
	pmap->data = malloc(screen.pixelbytes * vport->width * vport->height);
1141
	if (!pmap->data)
1142
		return ENOMEM;
3707 decky 1143
 
1552 palkovsky 1144
	pmap->width = vport->width;
1145
	pmap->height = vport->height;
3707 decky 1146
 
1711 palkovsky 1147
	copy_vp_to_pixmap(vport, pmap);
1552 palkovsky 1148
 
1149
	return pm;
1150
}
1151
 
3707 decky 1152
 
1552 palkovsky 1153
/** Draw pixmap on screen
1154
 *
1155
 * @param vp Viewport to draw on
1156
 * @param pm Pixmap identifier
3707 decky 1157
 *
1552 palkovsky 1158
 */
1159
static int draw_pixmap(int vp, int pm)
1160
{
1161
	pixmap_t *pmap = &pixmaps[pm];
1162
	viewport_t *vport = &viewports[vp];
3707 decky 1163
 
1164
	unsigned int width = vport->width;
1165
	unsigned int height = vport->height;
1166
 
1711 palkovsky 1167
	if (width + vport->x > screen.xres)
1168
		width = screen.xres - vport->x;
1169
	if (height + vport->y > screen.yres)
1170
		height = screen.yres - vport->y;
3707 decky 1171
 
1552 palkovsky 1172
	if (!pmap->data)
1173
		return EINVAL;
3707 decky 1174
 
1175
	unsigned int realwidth = pmap->width <= width ? pmap->width : width;
1176
	unsigned int realheight = pmap->height <= height ? pmap->height : height;
1177
 
1178
	unsigned int srcrowsize = vport->width * screen.pixelbytes;
1179
	unsigned int realrowsize = realwidth * screen.pixelbytes;
1180
 
1181
	unsigned int y;
2070 jermar 1182
	for (y = 0; y < realheight; y++) {
3707 decky 1183
		unsigned int tmp = (vport->y + y) * screen.scanline + vport->x * screen.pixelbytes;
1184
		memcpy(screen.fb_addr + tmp, pmap->data + y * srcrowsize, realrowsize);
1552 palkovsky 1185
	}
3707 decky 1186
 
1187
	return EOK;
1552 palkovsky 1188
}
1189
 
3707 decky 1190
 
1191
/** Tick animation one step forward
1192
 *
1193
 */
1194
static void anims_tick(void)
1646 palkovsky 1195
{
3707 decky 1196
	unsigned int i;
1646 palkovsky 1197
	static int counts = 0;
1198
 
1199
	/* Limit redrawing */
2025 jermar 1200
	counts = (counts + 1) % 8;
1646 palkovsky 1201
	if (counts)
1202
		return;
1203
 
2070 jermar 1204
	for (i = 0; i < MAX_ANIMATIONS; i++) {
3707 decky 1205
		if ((!animations[i].animlen) || (!animations[i].initialized) ||
1206
		    (!animations[i].enabled))
1646 palkovsky 1207
			continue;
3707 decky 1208
 
1209
		draw_pixmap(animations[i].vp, animations[i].pixmaps[animations[i].pos]);
1210
		animations[i].pos = (animations[i].pos + 1) % animations[i].animlen;
1646 palkovsky 1211
	}
1212
}
1213
 
1711 palkovsky 1214
 
3707 decky 1215
static unsigned int pointer_x;
1216
static unsigned int pointer_y;
1217
static bool pointer_shown, pointer_enabled;
1711 palkovsky 1218
static int pointer_vport = -1;
1219
static int pointer_pixmap = -1;
1220
 
3707 decky 1221
 
1222
static void mouse_show(void)
1711 palkovsky 1223
{
2070 jermar 1224
	int i, j;
1711 palkovsky 1225
	int visibility;
1226
	int color;
1227
	int bytepos;
3707 decky 1228
 
1229
	if ((pointer_shown) || (!pointer_enabled))
1723 palkovsky 1230
		return;
3707 decky 1231
 
3723 svoboda 1232
	/* Save image under the pointer. */
1711 palkovsky 1233
	if (pointer_vport == -1) {
3707 decky 1234
		pointer_vport = vport_create(pointer_x, pointer_y, pointer_width, pointer_height);
1711 palkovsky 1235
		if (pointer_vport < 0)
1236
			return;
1237
	} else {
1238
		viewports[pointer_vport].x = pointer_x;
1239
		viewports[pointer_vport].y = pointer_y;
1240
	}
3707 decky 1241
 
1711 palkovsky 1242
	if (pointer_pixmap == -1)
1243
		pointer_pixmap = save_vp_to_pixmap(&viewports[pointer_vport]);
1244
	else
3707 decky 1245
		copy_vp_to_pixmap(&viewports[pointer_vport], &pixmaps[pointer_pixmap]);
1246
 
3723 svoboda 1247
	/* Draw mouse pointer. */
2025 jermar 1248
	for (i = 0; i < pointer_height; i++)
1249
		for (j = 0; j < pointer_width; j++) {
1250
			bytepos = i * ((pointer_width - 1) / 8 + 1) + j / 8;
2070 jermar 1251
			visibility = pointer_mask_bits[bytepos] &
2619 jermar 1252
			    (1 << (j % 8));
1711 palkovsky 1253
			if (visibility) {
2619 jermar 1254
				color = pointer_bits[bytepos] &
1255
				    (1 << (j % 8)) ? 0 : 0xffffff;
2025 jermar 1256
				if (pointer_x + j < screen.xres && pointer_y +
2619 jermar 1257
				    i < screen.yres)
2025 jermar 1258
					putpixel(&viewports[0], pointer_x + j,
2619 jermar 1259
					    pointer_y + i, color);
1711 palkovsky 1260
			}
1261
		}
1262
	pointer_shown = 1;
1263
}
1264
 
3707 decky 1265
 
1266
static void mouse_hide(void)
1711 palkovsky 1267
{
3723 svoboda 1268
	/* Restore image under the pointer. */
1711 palkovsky 1269
	if (pointer_shown) {
1270
		draw_pixmap(pointer_vport, pointer_pixmap);
1271
		pointer_shown = 0;
1272
	}
1273
}
1274
 
3707 decky 1275
 
1276
static void mouse_move(unsigned int x, unsigned int y)
1711 palkovsky 1277
{
1278
	mouse_hide();
1279
	pointer_x = x;
1280
	pointer_y = y;
1281
	mouse_show();
1282
}
1283
 
3707 decky 1284
 
1285
static int anim_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1646 palkovsky 1286
{
3707 decky 1287
	bool handled = true;
1288
	int retval = EOK;
1289
	int i, nvp;
1646 palkovsky 1290
	int newval;
3707 decky 1291
 
1646 palkovsky 1292
	switch (IPC_GET_METHOD(*call)) {
1293
	case FB_ANIM_CREATE:
1294
		nvp = IPC_GET_ARG1(*call);
1295
		if (nvp == -1)
1296
			nvp = vp;
2025 jermar 1297
		if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1298
			!viewports[nvp].initialized) {
1646 palkovsky 1299
			retval = EINVAL;
1300
			break;
1301
		}
2025 jermar 1302
		for (i = 0; i < MAX_ANIMATIONS; i++) {
1303
			if (!animations[i].initialized)
1646 palkovsky 1304
				break;
1305
		}
1306
		if (i == MAX_ANIMATIONS) {
1307
			retval = ELIMIT;
1308
			break;
1309
		}
1310
		animations[i].initialized = 1;
1311
		animations[i].animlen = 0;
1312
		animations[i].pos = 0;
1313
		animations[i].enabled = 0;
1314
		animations[i].vp = nvp;
1315
		retval = i;
1316
		break;
1317
	case FB_ANIM_DROP:
1318
		i = IPC_GET_ARG1(*call);
1720 palkovsky 1319
		if (i >= MAX_ANIMATIONS || i < 0) {
1646 palkovsky 1320
			retval = EINVAL;
1321
			break;
1322
		}
1323
		animations[i].initialized = 0;
1324
		break;
1325
	case FB_ANIM_ADDPIXMAP:
1326
		i = IPC_GET_ARG1(*call);
2025 jermar 1327
		if (i >= MAX_ANIMATIONS || i < 0 ||
1328
			!animations[i].initialized) {
1646 palkovsky 1329
			retval = EINVAL;
1330
			break;
1331
		}
1332
		if (animations[i].animlen == MAX_ANIM_LEN) {
1333
			retval = ELIMIT;
1334
			break;
1335
		}
1336
		newval = IPC_GET_ARG2(*call);
2025 jermar 1337
		if (newval < 0 || newval > MAX_PIXMAPS ||
1338
			!pixmaps[newval].data) {
1646 palkovsky 1339
			retval = EINVAL;
1340
			break;
1341
		}
1342
		animations[i].pixmaps[animations[i].animlen++] = newval;
1343
		break;
1344
	case FB_ANIM_CHGVP:
1345
		i = IPC_GET_ARG1(*call);
1346
		if (i >= MAX_ANIMATIONS || i < 0) {
1347
			retval = EINVAL;
1348
			break;
1349
		}
1350
		nvp = IPC_GET_ARG2(*call);
1351
		if (nvp == -1)
1352
			nvp = vp;
2025 jermar 1353
		if (nvp >= MAX_VIEWPORTS || nvp < 0 ||
1354
			!viewports[nvp].initialized) {
1646 palkovsky 1355
			retval = EINVAL;
1356
			break;
1357
		}
1358
		animations[i].vp = nvp;
1359
		break;
1360
	case FB_ANIM_START:
1361
	case FB_ANIM_STOP:
1362
		i = IPC_GET_ARG1(*call);
1363
		if (i >= MAX_ANIMATIONS || i < 0) {
1364
			retval = EINVAL;
1365
			break;
1366
		}
1367
		newval = (IPC_GET_METHOD(*call) == FB_ANIM_START);
1368
		if (newval ^ animations[i].enabled) {
1369
			animations[i].enabled = newval;
1370
			anims_enabled += newval ? 1 : -1;
1371
		}
1372
		break;
1373
	default:
1374
		handled = 0;
1375
	}
1376
	if (handled)
2619 jermar 1377
		ipc_answer_0(callid, retval);
1646 palkovsky 1378
	return handled;
1379
}
1380
 
3707 decky 1381
 
1382
/** Handler for messages concerning pixmap handling
1383
 *
1384
 */
1385
static int pixmap_handle(ipc_callid_t callid, ipc_call_t *call, int vp)
1552 palkovsky 1386
{
3707 decky 1387
	bool handled = true;
1388
	int retval = EOK;
1389
	int i, nvp;
1390
 
1552 palkovsky 1391
	switch (IPC_GET_METHOD(*call)) {
1392
	case FB_VP_DRAW_PIXMAP:
1393
		nvp = IPC_GET_ARG1(*call);
1394
		if (nvp == -1)
1395
			nvp = vp;
2025 jermar 1396
		if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1397
			!viewports[nvp].initialized) {
1552 palkovsky 1398
			retval = EINVAL;
1399
			break;
1400
		}
1401
		i = IPC_GET_ARG2(*call);
1402
		retval = draw_pixmap(nvp, i);
1403
		break;
1404
	case FB_VP2PIXMAP:
1405
		nvp = IPC_GET_ARG1(*call);
1406
		if (nvp == -1)
1407
			nvp = vp;
2025 jermar 1408
		if (nvp < 0 || nvp >= MAX_VIEWPORTS ||
1409
			!viewports[nvp].initialized)
1552 palkovsky 1410
			retval = EINVAL;
1411
		else
1711 palkovsky 1412
			retval = save_vp_to_pixmap(&viewports[nvp]);
1552 palkovsky 1413
		break;
1414
	case FB_DROP_PIXMAP:
1415
		i = IPC_GET_ARG1(*call);
1416
		if (i >= MAX_PIXMAPS) {
1417
			retval = EINVAL;
1418
			break;
1419
		}
1420
		if (pixmaps[i].data) {
1421
			free(pixmaps[i].data);
1422
			pixmaps[i].data = NULL;
1423
		}
1424
		break;
1425
	default:
1426
		handled = 0;
1427
	}
3707 decky 1428
 
1552 palkovsky 1429
	if (handled)
2619 jermar 1430
		ipc_answer_0(callid, retval);
1552 palkovsky 1431
	return handled;
1432
 
1433
}
1434
 
3767 svoboda 1435
static int rgb_from_style(attr_rgb_t *rgb, int style)
1436
{
1437
	switch (style) {
1438
	case STYLE_NORMAL:
1439
		rgb->fg_color = color_table[COLOR_BLACK];
1440
		rgb->bg_color = color_table[COLOR_WHITE];
1441
		break;
1442
	case STYLE_EMPHASIS:
1443
		rgb->fg_color = color_table[COLOR_RED];
1444
		rgb->bg_color = color_table[COLOR_WHITE];
1445
		break;
1446
	default:
1447
		return EINVAL;
1448
	}
1449
 
1450
	return EOK;
1451
}
1452
 
1453
static int rgb_from_idx(attr_rgb_t *rgb, ipcarg_t fg_color,
1454
    ipcarg_t bg_color, ipcarg_t flags)
1455
{
1456
	fg_color = (fg_color & 7) | ((flags & CATTR_BRIGHT) ? 8 : 0);
1457
	bg_color = (bg_color & 7) | ((flags & CATTR_BRIGHT) ? 8 : 0);
1458
 
1459
	rgb->fg_color = color_table[fg_color];
1460
	rgb->bg_color = color_table[bg_color];
1461
 
1462
	return EOK;
1463
}
1464
 
3792 svoboda 1465
static int rgb_from_attr(attr_rgb_t *rgb, const attrs_t *a)
1466
{
1467
	int rc;
1468
 
1469
	switch (a->t) {
1470
	case at_style:
1471
		rc = rgb_from_style(rgb, a->a.s.style);
1472
		break;
1473
	case at_idx:
1474
		rc = rgb_from_idx(rgb, a->a.i.fg_color,
1475
		    a->a.i.bg_color, a->a.i.flags);
1476
		break;
1477
	case at_rgb:
1478
		*rgb = a->a.r;
1479
		rc = EOK;
1480
		break;
1481
	}
1482
 
1483
	return rc;
1484
}
1485
 
3767 svoboda 1486
static int fb_set_style(viewport_t *vport, ipcarg_t style)
1487
{
1488
	return rgb_from_style(&vport->attr, (int) style);
1489
}
1490
 
1491
static int fb_set_color(viewport_t *vport, ipcarg_t fg_color,
1492
    ipcarg_t bg_color, ipcarg_t flags)
1493
{
1494
	return rgb_from_idx(&vport->attr, fg_color, bg_color, flags);
1495
}
1496
 
1498 palkovsky 1497
/** Function for handling connections to FB
1498
 *
1499
 */
3707 decky 1500
static void fb_client_connection(ipc_callid_t iid, ipc_call_t *icall)
1363 vana 1501
{
3707 decky 1502
	unsigned int vp = 0;
1503
	viewport_t *vport = &viewports[vp];
1504
 
1485 palkovsky 1505
	if (client_connected) {
2619 jermar 1506
		ipc_answer_0(iid, ELIMIT);
1485 palkovsky 1507
		return;
1508
	}
3707 decky 1509
 
1510
	/* Accept connection */
1511
	client_connected = true;
1512
	ipc_answer_0(iid, EOK);
1513
 
1514
	while (true) {
1515
		ipc_callid_t callid;
1516
		ipc_call_t call;
1517
		int retval;
1518
		unsigned int i;
1519
		int scroll;
4211 svoboda 1520
		uint32_t glyph;
3707 decky 1521
		unsigned int row, col;
1522
 
1523
		if ((vport->cursor_active) || (anims_enabled))
2070 jermar 1524
			callid = async_get_call_timeout(&call, 250000);
1640 palkovsky 1525
		else
1526
			callid = async_get_call(&call);
3707 decky 1527
 
1713 palkovsky 1528
		mouse_hide();
1500 palkovsky 1529
		if (!callid) {
1673 palkovsky 1530
			cursor_blink(vport);
1646 palkovsky 1531
			anims_tick();
1723 palkovsky 1532
			mouse_show();
1500 palkovsky 1533
			continue;
1534
		}
3707 decky 1535
 
1547 palkovsky 1536
		if (shm_handle(callid, &call, vp))
1537
			continue;
3707 decky 1538
 
1552 palkovsky 1539
		if (pixmap_handle(callid, &call, vp))
1540
			continue;
3707 decky 1541
 
1646 palkovsky 1542
		if (anim_handle(callid, &call, vp))
1543
			continue;
3707 decky 1544
 
1545
		switch (IPC_GET_METHOD(call)) {
1485 palkovsky 1546
		case IPC_M_PHONE_HUNGUP:
3707 decky 1547
			client_connected = false;
1548
 
1549
			/* Cleanup other viewports */
2025 jermar 1550
			for (i = 1; i < MAX_VIEWPORTS; i++)
3707 decky 1551
				vport->initialized = false;
1552
 
1553
			/* Exit thread */
1554
			return;
1555
 
1485 palkovsky 1556
		case FB_PUTCHAR:
3707 decky 1557
			glyph = IPC_GET_ARG1(call);
1485 palkovsky 1558
			row = IPC_GET_ARG2(call);
1559
			col = IPC_GET_ARG3(call);
3707 decky 1560
 
1561
			if ((col >= vport->cols) || (row >= vport->rows)) {
1485 palkovsky 1562
				retval = EINVAL;
1563
				break;
1564
			}
2619 jermar 1565
			ipc_answer_0(callid, EOK);
3707 decky 1566
 
1567
			draw_char(vport, glyph, col, row);
1568
 
1569
			/* Message already answered */
1570
			continue;
1485 palkovsky 1571
		case FB_CLEAR:
3707 decky 1572
			vport_clear(vport);
1573
			cursor_show(vport);
1574
			retval = EOK;
1485 palkovsky 1575
			break;
3707 decky 1576
		case FB_CURSOR_GOTO:
1486 palkovsky 1577
			row = IPC_GET_ARG1(call);
1578
			col = IPC_GET_ARG2(call);
3707 decky 1579
 
1580
			if ((col >= vport->cols) || (row >= vport->rows)) {
1486 palkovsky 1581
				retval = EINVAL;
1582
				break;
1583
			}
3707 decky 1584
			retval = EOK;
1585
 
1673 palkovsky 1586
			cursor_hide(vport);
1486 palkovsky 1587
			vport->cur_col = col;
1588
			vport->cur_row = row;
3707 decky 1589
			cursor_show(vport);
1590
			break;
1486 palkovsky 1591
		case FB_CURSOR_VISIBILITY:
1673 palkovsky 1592
			cursor_hide(vport);
1500 palkovsky 1593
			vport->cursor_active = IPC_GET_ARG1(call);
3707 decky 1594
			cursor_show(vport);
1595
			retval = EOK;
1486 palkovsky 1596
			break;
1485 palkovsky 1597
		case FB_GET_CSIZE:
2619 jermar 1598
			ipc_answer_2(callid, EOK, vport->rows, vport->cols);
1485 palkovsky 1599
			continue;
1486 palkovsky 1600
		case FB_SCROLL:
3707 decky 1601
			scroll = IPC_GET_ARG1(call);
1602
			if ((scroll > (int) vport->rows) || (scroll < (-(int) vport->rows))) {
1486 palkovsky 1603
				retval = EINVAL;
1604
				break;
1605
			}
1673 palkovsky 1606
			cursor_hide(vport);
3707 decky 1607
			vport_scroll(vport, scroll);
1608
			cursor_show(vport);
1609
			retval = EOK;
1486 palkovsky 1610
			break;
1611
		case FB_VIEWPORT_SWITCH:
1612
			i = IPC_GET_ARG1(call);
3707 decky 1613
			if (i >= MAX_VIEWPORTS) {
1486 palkovsky 1614
				retval = EINVAL;
1615
				break;
1616
			}
3707 decky 1617
			if (!viewports[i].initialized) {
1486 palkovsky 1618
				retval = EADDRNOTAVAIL;
1619
				break;
1620
			}
1673 palkovsky 1621
			cursor_hide(vport);
1486 palkovsky 1622
			vp = i;
1623
			vport = &viewports[vp];
3707 decky 1624
			cursor_show(vport);
1625
			retval = EOK;
1486 palkovsky 1626
			break;
1627
		case FB_VIEWPORT_CREATE:
3707 decky 1628
			retval = vport_create(IPC_GET_ARG1(call) >> 16,
2619 jermar 1629
			    IPC_GET_ARG1(call) & 0xffff,
1630
			    IPC_GET_ARG2(call) >> 16,
1631
			    IPC_GET_ARG2(call) & 0xffff);
1486 palkovsky 1632
			break;
1633
		case FB_VIEWPORT_DELETE:
1634
			i = IPC_GET_ARG1(call);
3707 decky 1635
			if (i >= MAX_VIEWPORTS) {
1486 palkovsky 1636
				retval = EINVAL;
1637
				break;
1638
			}
3707 decky 1639
			if (!viewports[i].initialized) {
1486 palkovsky 1640
				retval = EADDRNOTAVAIL;
1641
				break;
1642
			}
3707 decky 1643
			viewports[i].initialized = false;
1644
			if (viewports[i].glyphs)
1645
				free(viewports[i].glyphs);
1646
			if (viewports[i].bgpixel)
1647
				free(viewports[i].bgpixel);
1648
			if (viewports[i].backbuf)
1649
				free(viewports[i].backbuf);
1650
			retval = EOK;
1486 palkovsky 1651
			break;
1652
		case FB_SET_STYLE:
3767 svoboda 1653
			retval = fb_set_style(vport, IPC_GET_ARG1(call));
1654
			break;
1655
		case FB_SET_COLOR:
1656
			retval = fb_set_color(vport, IPC_GET_ARG1(call),
1657
			    IPC_GET_ARG2(call), IPC_GET_ARG3(call));
1658
			break;
1659
		case FB_SET_RGB_COLOR:
1660
			vport->attr.fg_color = IPC_GET_ARG1(call);
1661
			vport->attr.bg_color = IPC_GET_ARG2(call);
3707 decky 1662
			retval = EOK;
1486 palkovsky 1663
			break;
1664
		case FB_GET_RESOLUTION:
2619 jermar 1665
			ipc_answer_2(callid, EOK, screen.xres, screen.yres);
1486 palkovsky 1666
			continue;
1707 palkovsky 1667
		case FB_POINTER_MOVE:
3707 decky 1668
			pointer_enabled = true;
1711 palkovsky 1669
			mouse_move(IPC_GET_ARG1(call), IPC_GET_ARG2(call));
3707 decky 1670
			retval = EOK;
1707 palkovsky 1671
			break;
1485 palkovsky 1672
		default:
1673
			retval = ENOENT;
1674
		}
2619 jermar 1675
		ipc_answer_0(callid, retval);
1363 vana 1676
	}
1485 palkovsky 1677
}
1363 vana 1678
 
3707 decky 1679
/** Initialization of framebuffer
1680
 *
1681
 */
1682
int fb_init(void)
1485 palkovsky 1683
{
1490 palkovsky 1684
	async_set_client_connection(fb_client_connection);
1485 palkovsky 1685
 
3707 decky 1686
	void *fb_ph_addr = (void *) sysinfo_value("fb.address.physical");
1687
	unsigned int fb_offset = sysinfo_value("fb.offset");
1688
	unsigned int fb_width = sysinfo_value("fb.width");
1689
	unsigned int fb_height = sysinfo_value("fb.height");
1690
	unsigned int fb_scanline = sysinfo_value("fb.scanline");
1691
	unsigned int fb_visual = sysinfo_value("fb.visual");
1692
 
1693
	unsigned int fbsize = fb_scanline * fb_height;
1694
	void *fb_addr = as_get_mappable_page(fbsize);
1695
 
3908 decky 1696
	if (physmem_map(fb_ph_addr + fb_offset, fb_addr,
1697
	    ALIGN_UP(fbsize, PAGE_SIZE) >> PAGE_WIDTH, AS_AREA_READ | AS_AREA_WRITE) != 0)
1698
		return -1;
3707 decky 1699
 
1700
	if (screen_init(fb_addr, fb_width, fb_height, fb_scanline, fb_visual))
1993 decky 1701
		return 0;
1702
 
1703
	return -1;
1363 vana 1704
}
1490 palkovsky 1705
 
3707 decky 1706
/**
1649 cejka 1707
 * @}
1708
 */