Subversion Repositories HelenOS

Rev

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