Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
836 palkovsky 1
/*
4337 svoboda 2
 * Copyright (c) 2008 Martin Decky
2071 jermar 3
 * Copyright (c) 2006 Ondrej Palkovsky
836 palkovsky 4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
4337 svoboda 30
/** @addtogroup genarch
1702 cejka 31
 * @{
32
 */
33
/** @file
34
 */
35
 
837 palkovsky 36
#include <genarch/fb/font-8x16.h>
4337 svoboda 37
#include <genarch/fb/logo-196x66.h>
1993 decky 38
#include <genarch/fb/visuals.h>
837 palkovsky 39
#include <genarch/fb/fb.h>
836 palkovsky 40
#include <console/chardev.h>
41
#include <console/console.h>
1317 vana 42
#include <sysinfo/sysinfo.h>
1316 jermar 43
#include <mm/slab.h>
4337 svoboda 44
#include <align.h>
839 palkovsky 45
#include <panic.h>
896 jermar 46
#include <memstr.h>
1316 jermar 47
#include <config.h>
1682 palkovsky 48
#include <bitops.h>
49
#include <print.h>
2015 jermar 50
#include <ddi/ddi.h>
2054 jermar 51
#include <arch/types.h>
836 palkovsky 52
 
4337 svoboda 53
SPINLOCK_INITIALIZE(fb_lock);
862 palkovsky 54
 
4337 svoboda 55
/**< Physical memory area for fb. */
56
static parea_t fb_parea;
2015 jermar 57
 
4337 svoboda 58
static uint8_t *fb_addr;
59
static uint8_t *backbuf;
60
static uint8_t *glyphs;
61
static uint8_t *bgscan;
836 palkovsky 62
 
4337 svoboda 63
static unsigned int xres;
64
static unsigned int yres;
836 palkovsky 65
 
4337 svoboda 66
static unsigned int ylogo;
67
static unsigned int ytrim;
68
static unsigned int rowtrim;
1316 jermar 69
 
4337 svoboda 70
static unsigned int scanline;
71
static unsigned int glyphscanline;
836 palkovsky 72
 
4337 svoboda 73
static unsigned int pixelbytes;
74
static unsigned int glyphbytes;
75
static unsigned int bgscanbytes;
76
 
77
static unsigned int cols;
78
static unsigned int rows;
1135 decky 79
static unsigned int position = 0;
836 palkovsky 80
 
4337 svoboda 81
#define BG_COLOR     0x000080
82
#define FG_COLOR     0xffff00
836 palkovsky 83
 
4337 svoboda 84
#define CURSOR       219
1135 decky 85
 
4337 svoboda 86
#define RED(x, bits)         ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
87
#define GREEN(x, bits)       ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
88
#define BLUE(x, bits)        ((x >> (8 - bits)) & ((1 << bits) - 1))
1135 decky 89
 
4337 svoboda 90
#define COL2X(col)           ((col) * FONT_WIDTH)
91
#define ROW2Y(row)           ((row) * FONT_SCANLINES)
1135 decky 92
 
4337 svoboda 93
#define X2COL(x)             ((x) / FONT_WIDTH)
94
#define Y2ROW(y)             ((y) / FONT_SCANLINES)
838 palkovsky 95
 
4337 svoboda 96
#define FB_POS(x, y)         ((y) * scanline + (x) * pixelbytes)
97
#define BB_POS(col, row)     ((row) * cols + (col))
98
#define GLYPH_POS(glyph, y)  ((glyph) * glyphbytes + (y) * glyphscanline)
839 palkovsky 99
 
1875 jermar 100
 
4337 svoboda 101
static void (*rgb_conv)(void *, uint32_t);
836 palkovsky 102
 
839 palkovsky 103
 
4337 svoboda 104
/** ARGB 8:8:8:8 conversion
105
 *
106
 */
107
static void rgb_0888(void *dst, uint32_t rgb)
1994 decky 108
{
4337 svoboda 109
    *((uint32_t *) dst) = rgb & 0xffffff;
1994 decky 110
}
111
 
4337 svoboda 112
 
113
/** ABGR 8:8:8:8 conversion
114
 *
115
 */
116
static void bgr_0888(void *dst, uint32_t rgb)
1994 decky 117
{
4337 svoboda 118
    *((uint32_t *) dst)
119
        = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
1994 decky 120
}
121
 
4337 svoboda 122
 
4342 svoboda 123
/** RGB 8:8:8 conversion
4337 svoboda 124
 *
125
 */
126
static void rgb_888(void *dst, uint32_t rgb)
839 palkovsky 127
{
4337 svoboda 128
    ((uint8_t *) dst)[0] = BLUE(rgb, 8);
129
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
130
    ((uint8_t *) dst)[2] = RED(rgb, 8);
839 palkovsky 131
}
132
 
838 palkovsky 133
 
4342 svoboda 134
/** BGR 8:8:8 conversion
135
 *
136
 */
137
static void bgr_888(void *dst, uint32_t rgb)
138
{
139
    ((uint8_t *) dst)[0] = RED(rgb, 8);
140
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
141
    ((uint8_t *) dst)[2] = BLUE(rgb, 8);
142
}
143
 
144
 
4337 svoboda 145
/** RGB 5:5:5 conversion
146
 *
147
 */
148
static void rgb_555(void *dst, uint32_t rgb)
1993 decky 149
{
4337 svoboda 150
    *((uint16_t *) dst)
151
        = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
1993 decky 152
}
153
 
154
 
4337 svoboda 155
/** RGB 5:6:5 conversion
156
 *
157
 */
158
static void rgb_565(void *dst, uint32_t rgb)
839 palkovsky 159
{
4337 svoboda 160
    *((uint16_t *) dst)
161
        = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
839 palkovsky 162
}
163
 
164
 
4337 svoboda 165
/** RGB 3:2:3
1837 jermar 166
 *
167
 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
168
 * will most likely use a color palette. The color appearance
169
 * will be pretty random and depend on the default installed
170
 * palette. This could be fixed by supporting custom palette
171
 * and setting it to simulate the 8-bit truecolor.
4337 svoboda 172
 *
173
 * Currently we set the palette on the ia32, amd64 and sparc64 port.
174
 *
175
 * Note that the byte is being inverted by this function. The reason is
176
 * that we would like to use a color palette where the white color code
177
 * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
178
 * use these codes for black and white and prevent to set codes
179
 * 0 and 255 to other colors.
180
 *
1837 jermar 181
 */
4337 svoboda 182
static void rgb_323(void *dst, uint32_t rgb)
839 palkovsky 183
{
4337 svoboda 184
    *((uint8_t *) dst)
185
        = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
1135 decky 186
}
839 palkovsky 187
 
3674 svoboda 188
 
4337 svoboda 189
/** Hide logo and refresh screen
1837 jermar 190
 *
191
 */
4341 svoboda 192
static void logo_hide(bool silent)
1135 decky 193
{
4337 svoboda 194
    ylogo = 0;
195
    ytrim = yres;
196
    rowtrim = rows;
4341 svoboda 197
    if (!silent)
198
        fb_redraw();
839 palkovsky 199
}
200
 
1682 palkovsky 201
 
4337 svoboda 202
/** Draw character at given position
203
 *
204
 */
4341 svoboda 205
static void glyph_draw(uint8_t glyph, unsigned int col, unsigned int row, bool silent)
1682 palkovsky 206
{
4337 svoboda 207
    unsigned int x = COL2X(col);
208
    unsigned int y = ROW2Y(row);
209
    unsigned int yd;
210
 
211
    if (y >= ytrim)
4341 svoboda 212
        logo_hide(silent);
4337 svoboda 213
 
214
    backbuf[BB_POS(col, row)] = glyph;
215
 
4341 svoboda 216
    if (!silent) {
217
        for (yd = 0; yd < FONT_SCANLINES; yd++)
218
            memcpy(&fb_addr[FB_POS(x, y + yd + ylogo)],
219
                &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
220
    }
1682 palkovsky 221
}
1135 decky 222
 
1682 palkovsky 223
 
4337 svoboda 224
/** Scroll screen down by one row
225
 *
226
 *
227
 */
4341 svoboda 228
static void screen_scroll(bool silent)
839 palkovsky 229
{
4337 svoboda 230
    if (ylogo > 0) {
4341 svoboda 231
        logo_hide(silent);
4337 svoboda 232
        return;
1682 palkovsky 233
    }
4337 svoboda 234
 
4341 svoboda 235
    if (!silent) {
236
        unsigned int row;
2054 jermar 237
 
4341 svoboda 238
        for (row = 0; row < rows; row++) {
239
            unsigned int y = ROW2Y(row);
240
            unsigned int yd;
4337 svoboda 241
 
4341 svoboda 242
            for (yd = 0; yd < FONT_SCANLINES; yd++) {
243
                unsigned int x;
244
                unsigned int col;
4337 svoboda 245
 
4341 svoboda 246
                for (col = 0, x = 0; col < cols; col++,
247
                    x += FONT_WIDTH) {
248
                    uint8_t glyph;
4337 svoboda 249
 
4341 svoboda 250
                    if (row < rows - 1) {
251
                        if (backbuf[BB_POS(col, row)] ==
252
                            backbuf[BB_POS(col, row + 1)])
253
                            continue;
254
 
255
                        glyph = backbuf[BB_POS(col, row + 1)];
256
                    } else
257
                        glyph = 0;
258
 
259
                    memcpy(&fb_addr[FB_POS(x, y + yd)],
260
                        &glyphs[GLYPH_POS(glyph, yd)],
261
                        glyphscanline);
262
                }
4337 svoboda 263
            }
2086 jermar 264
        }
1682 palkovsky 265
    }
4337 svoboda 266
 
4338 svoboda 267
    memmove(backbuf, backbuf + cols, cols * (rows - 1));
4337 svoboda 268
    memsetb(&backbuf[BB_POS(0, rows - 1)], cols, 0);
838 palkovsky 269
}
270
 
271
 
4341 svoboda 272
static void cursor_put(bool silent)
836 palkovsky 273
{
4341 svoboda 274
    glyph_draw(CURSOR, position % cols, position / cols, silent);
836 palkovsky 275
}
276
 
277
 
4341 svoboda 278
static void cursor_remove(bool silent)
836 palkovsky 279
{
4341 svoboda 280
    glyph_draw(0, position % cols, position / cols, silent);
836 palkovsky 281
}
282
 
838 palkovsky 283
 
836 palkovsky 284
/** Print character to screen
285
 *
4337 svoboda 286
 * Emulate basic terminal commands.
287
 *
836 palkovsky 288
 */
4341 svoboda 289
static void fb_putchar(chardev_t *dev, char ch, bool silent)
836 palkovsky 290
{
1287 vana 291
    spinlock_lock(&fb_lock);
1135 decky 292
 
293
    switch (ch) {
1888 jermar 294
    case '\n':
4341 svoboda 295
        cursor_remove(silent);
4337 svoboda 296
        position += cols;
297
        position -= position % cols;
1888 jermar 298
        break;
299
    case '\r':
4341 svoboda 300
        cursor_remove(silent);
4337 svoboda 301
        position -= position % cols;
1888 jermar 302
        break;
303
    case '\b':
4341 svoboda 304
        cursor_remove(silent);
4337 svoboda 305
        if (position % cols)
1888 jermar 306
            position--;
307
        break;
308
    case '\t':
4341 svoboda 309
        cursor_remove(silent);
1888 jermar 310
        do {
4337 svoboda 311
            glyph_draw((uint8_t) ' ', position % cols,
4341 svoboda 312
                position / cols, silent);
838 palkovsky 313
            position++;
4337 svoboda 314
        } while ((position % 8) && (position < cols * rows));
1888 jermar 315
        break;
316
    default:
4341 svoboda 317
        glyph_draw((uint8_t) ch, position % cols,
318
            position / cols, silent);
1888 jermar 319
        position++;
836 palkovsky 320
    }
1135 decky 321
 
4337 svoboda 322
    if (position >= cols * rows) {
323
        position -= cols;
4341 svoboda 324
        screen_scroll(silent);
836 palkovsky 325
    }
1135 decky 326
 
4341 svoboda 327
    cursor_put(silent);
1135 decky 328
 
1287 vana 329
    spinlock_unlock(&fb_lock);
836 palkovsky 330
}
331
 
332
static chardev_t framebuffer;
333
static chardev_operations_t fb_ops = {
334
    .write = fb_putchar,
335
};
336
 
337
 
4337 svoboda 338
/** Render glyphs
339
 *
340
 * Convert glyphs from device independent font
341
 * description to current visual representation.
342
 *
343
 */
344
static void glyphs_render(void)
345
{
346
    /* Prerender glyphs */
347
    unsigned int glyph;
348
 
349
    for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
350
        unsigned int y;
351
 
352
        for (y = 0; y < FONT_SCANLINES; y++) {
353
            unsigned int x;
354
 
355
            for (x = 0; x < FONT_WIDTH; x++) {
356
                void *dst = &glyphs[GLYPH_POS(glyph, y) +
357
                    x * pixelbytes];
358
                uint32_t rgb = (fb_font[ROW2Y(glyph) + y] &
359
                    (1 << (7 - x))) ? FG_COLOR : BG_COLOR;
360
                rgb_conv(dst, rgb);
361
            }
362
        }
363
    }
364
 
365
    /* Prerender background scanline */
366
    unsigned int x;
367
 
368
    for (x = 0; x < xres; x++)
369
        rgb_conv(&bgscan[x * pixelbytes], BG_COLOR);
370
}
371
 
372
 
373
/** Refresh the screen
374
 *
375
 */
376
void fb_redraw(void)
377
{
378
    if (ylogo > 0) {
379
        unsigned int y;
380
 
381
        for (y = 0; y < LOGO_HEIGHT; y++) {
382
            unsigned int x;
383
 
384
            for (x = 0; x < xres; x++)
385
                rgb_conv(&fb_addr[FB_POS(x, y)],
386
                    (x < LOGO_WIDTH) ?
387
                    fb_logo[y * LOGO_WIDTH + x] :
388
                    LOGO_COLOR);
389
        }
390
    }
391
 
392
    unsigned int row;
393
 
394
    for (row = 0; row < rowtrim; row++) {
395
        unsigned int y = ylogo + ROW2Y(row);
396
        unsigned int yd;
397
 
398
        for (yd = 0; yd < FONT_SCANLINES; yd++) {
399
            unsigned int x;
400
            unsigned int col;
401
 
402
            for (col = 0, x = 0; col < cols;
403
                col++, x += FONT_WIDTH) {
404
                void *d = &fb_addr[FB_POS(x, y + yd)];
405
                void *s = &glyphs[GLYPH_POS(backbuf[BB_POS(col,
406
                    row)], yd)];
407
                memcpy(d, s, glyphscanline);
408
            }
409
        }
410
    }
411
 
412
    if (COL2X(cols) < xres) {
413
        unsigned int y;
414
        unsigned int size = (xres - COL2X(cols)) * pixelbytes;
415
 
416
        for (y = ylogo; y < yres; y++)
417
            memcpy(&fb_addr[FB_POS(COL2X(cols), y)], bgscan, size);
418
    }
419
 
420
    if (ROW2Y(rowtrim) + ylogo < yres) {
421
        unsigned int y;
422
 
423
        for (y = ROW2Y(rowtrim) + ylogo; y < yres; y++)
424
            memcpy(&fb_addr[FB_POS(0, y)], bgscan, bgscanbytes);
425
    }
426
}
427
 
428
 
836 palkovsky 429
/** Initialize framebuffer as a chardev output device
430
 *
4337 svoboda 431
 * @param addr   Physical address of the framebuffer
432
 * @param x      Screen width in pixels
433
 * @param y      Screen height in pixels
434
 * @param scan   Bytes per one scanline
435
 * @param visual Color model
436
 *
836 palkovsky 437
 */
3674 svoboda 438
void fb_init(fb_properties_t *props)
836 palkovsky 439
{
3674 svoboda 440
    switch (props->visual) {
1993 decky 441
    case VISUAL_INDIRECT_8:
4337 svoboda 442
        rgb_conv = rgb_323;
1991 jermar 443
        pixelbytes = 1;
444
        break;
1993 decky 445
    case VISUAL_RGB_5_5_5:
4337 svoboda 446
        rgb_conv = rgb_555;
1991 jermar 447
        pixelbytes = 2;
448
        break;
1993 decky 449
    case VISUAL_RGB_5_6_5:
4337 svoboda 450
        rgb_conv = rgb_565;
1993 decky 451
        pixelbytes = 2;
1991 jermar 452
        break;
1993 decky 453
    case VISUAL_RGB_8_8_8:
4337 svoboda 454
        rgb_conv = rgb_888;
1993 decky 455
        pixelbytes = 3;
456
        break;
4342 svoboda 457
    case VISUAL_BGR_8_8_8:
458
        rgb_conv = bgr_888;
459
        pixelbytes = 3;
460
        break;
1993 decky 461
    case VISUAL_RGB_8_8_8_0:
4337 svoboda 462
        rgb_conv = rgb_888;
1991 jermar 463
        pixelbytes = 4;
464
        break;
1993 decky 465
    case VISUAL_RGB_0_8_8_8:
4337 svoboda 466
        rgb_conv = rgb_0888;
1993 decky 467
        pixelbytes = 4;
468
        break;
1994 decky 469
    case VISUAL_BGR_0_8_8_8:
4337 svoboda 470
        rgb_conv = bgr_0888;
1994 decky 471
        pixelbytes = 4;
472
        break;
1991 jermar 473
    default:
4339 svoboda 474
        panic("Unsupported visual.");
839 palkovsky 475
    }
1371 decky 476
 
3674 svoboda 477
    xres = props->x;
478
    yres = props->y;
479
    scanline = props->scan;
836 palkovsky 480
 
4337 svoboda 481
    cols = X2COL(xres);
482
    rows = Y2ROW(yres);
483
 
484
    if (yres > ylogo) {
485
        ylogo = LOGO_HEIGHT;
486
        rowtrim = rows - Y2ROW(ylogo);
487
        if (ylogo % FONT_SCANLINES > 0)
488
            rowtrim--;
489
        ytrim = ROW2Y(rowtrim);
490
    } else {
491
        ylogo = 0;
492
        ytrim = yres;
493
        rowtrim = rows;
494
    }
495
 
496
    glyphscanline = FONT_WIDTH * pixelbytes;
497
    glyphbytes = ROW2Y(glyphscanline);
498
    bgscanbytes = xres * pixelbytes;
499
 
500
    unsigned int fbsize = scanline * yres;
501
    unsigned int bbsize = cols * rows;
502
    unsigned int glyphsize = FONT_GLYPHS * glyphbytes;
503
 
504
    backbuf = (uint8_t *) malloc(bbsize, 0);
505
    if (!backbuf)
4339 svoboda 506
        panic("Unable to allocate backbuffer.");
4337 svoboda 507
 
508
    glyphs = (uint8_t *) malloc(glyphsize, 0);
509
    if (!glyphs)
4339 svoboda 510
        panic("Unable to allocate glyphs.");
4337 svoboda 511
 
512
    bgscan = malloc(bgscanbytes, 0);
513
    if (!bgscan)
4339 svoboda 514
        panic("Unable to allocate background pixel.");
4337 svoboda 515
 
516
    memsetb(backbuf, bbsize, 0);
517
 
518
    glyphs_render();
519
 
520
    fb_addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize);
521
 
522
    fb_parea.pbase = (uintptr_t) props->addr + props->offset;
523
    fb_parea.vbase = (uintptr_t) fb_addr;
2015 jermar 524
    fb_parea.frames = SIZE2FRAMES(fbsize);
525
    fb_parea.cacheable = false;
526
    ddi_parea_register(&fb_parea);
4337 svoboda 527
 
1690 palkovsky 528
    sysinfo_set_item_val("fb", NULL, true);
529
    sysinfo_set_item_val("fb.kind", NULL, 1);
530
    sysinfo_set_item_val("fb.width", NULL, xres);
531
    sysinfo_set_item_val("fb.height", NULL, yres);
4337 svoboda 532
    sysinfo_set_item_val("fb.scanline", NULL, scanline);
3674 svoboda 533
    sysinfo_set_item_val("fb.visual", NULL, props->visual);
534
    sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
1990 decky 535
 
4337 svoboda 536
    fb_redraw();
537
 
836 palkovsky 538
    chardev_initialize("fb", &framebuffer, &fb_ops);
539
    stdout = &framebuffer;
540
}
1702 cejka 541
 
1790 jermar 542
/** @}
1702 cejka 543
 */