Subversion Repositories HelenOS

Rev

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

Rev Author Line No. Line
836 palkovsky 1
/*
4055 trochtova 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
 
4055 trochtova 30
/** @addtogroup genarch
1702 cejka 31
 * @{
32
 */
33
/** @file
34
 */
35
 
837 palkovsky 36
#include <genarch/fb/font-8x16.h>
4055 trochtova 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>
4055 trochtova 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>
4296 trochtova 50
#include <string.h>
2015 jermar 51
#include <ddi/ddi.h>
2054 jermar 52
#include <arch/types.h>
836 palkovsky 53
 
4055 trochtova 54
SPINLOCK_INITIALIZE(fb_lock);
862 palkovsky 55
 
4055 trochtova 56
static uint8_t *fb_addr;
4201 trochtova 57
static uint16_t *backbuf;
4055 trochtova 58
static uint8_t *glyphs;
59
static uint8_t *bgscan;
2015 jermar 60
 
4055 trochtova 61
static unsigned int xres;
62
static unsigned int yres;
836 palkovsky 63
 
4055 trochtova 64
static unsigned int ylogo;
65
static unsigned int ytrim;
66
static unsigned int rowtrim;
836 palkovsky 67
 
4055 trochtova 68
static unsigned int scanline;
69
static unsigned int glyphscanline;
1316 jermar 70
 
4055 trochtova 71
static unsigned int pixelbytes;
72
static unsigned int glyphbytes;
73
static unsigned int bgscanbytes;
836 palkovsky 74
 
4055 trochtova 75
static unsigned int cols;
76
static unsigned int rows;
1135 decky 77
static unsigned int position = 0;
836 palkovsky 78
 
4055 trochtova 79
#define BG_COLOR     0x000080
80
#define FG_COLOR     0xffff00
4201 trochtova 81
#define INV_COLOR    0xaaaaaa
836 palkovsky 82
 
4055 trochtova 83
#define RED(x, bits)         ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
84
#define GREEN(x, bits)       ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
85
#define BLUE(x, bits)        ((x >> (8 - bits)) & ((1 << bits) - 1))
1135 decky 86
 
4055 trochtova 87
#define COL2X(col)           ((col) * FONT_WIDTH)
88
#define ROW2Y(row)           ((row) * FONT_SCANLINES)
1135 decky 89
 
4055 trochtova 90
#define X2COL(x)             ((x) / FONT_WIDTH)
91
#define Y2ROW(y)             ((y) / FONT_SCANLINES)
838 palkovsky 92
 
4055 trochtova 93
#define FB_POS(x, y)         ((y) * scanline + (x) * pixelbytes)
94
#define BB_POS(col, row)     ((row) * cols + (col))
95
#define GLYPH_POS(glyph, y)  ((glyph) * glyphbytes + (y) * glyphscanline)
839 palkovsky 96
 
1875 jermar 97
 
4055 trochtova 98
static void (*rgb_conv)(void *, uint32_t);
836 palkovsky 99
 
839 palkovsky 100
 
4055 trochtova 101
/** ARGB 8:8:8:8 conversion
102
 *
103
 */
104
static void rgb_0888(void *dst, uint32_t rgb)
1994 decky 105
{
4055 trochtova 106
    *((uint32_t *) dst) = rgb & 0xffffff;
1994 decky 107
}
108
 
109
 
4055 trochtova 110
/** ABGR 8:8:8:8 conversion
111
 *
112
 */
113
static void bgr_0888(void *dst, uint32_t rgb)
839 palkovsky 114
{
4055 trochtova 115
    *((uint32_t *) dst)
116
        = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
839 palkovsky 117
}
118
 
838 palkovsky 119
 
4055 trochtova 120
/** RGB 8:8:8 conversion
121
 *
122
 */
123
static void rgb_888(void *dst, uint32_t rgb)
1993 decky 124
{
4055 trochtova 125
    ((uint8_t *) dst)[0] = BLUE(rgb, 8);
126
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
127
    ((uint8_t *) dst)[2] = RED(rgb, 8);
1993 decky 128
}
129
 
4055 trochtova 130
 
131
/** BGR 8:8:8 conversion
132
 *
133
 */
134
static void bgr_888(void *dst, uint32_t rgb)
1993 decky 135
{
4055 trochtova 136
    ((uint8_t *) dst)[0] = RED(rgb, 8);
137
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
138
    ((uint8_t *) dst)[2] = BLUE(rgb, 8);
1993 decky 139
}
140
 
4055 trochtova 141
 
142
/** RGB 5:5:5 conversion
143
 *
144
 */
145
static void rgb_555(void *dst, uint32_t rgb)
839 palkovsky 146
{
4055 trochtova 147
    *((uint16_t *) dst)
148
        = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
839 palkovsky 149
}
150
 
4055 trochtova 151
 
152
/** RGB 5:6:5 conversion
153
 *
154
 */
155
static void rgb_565(void *dst, uint32_t rgb)
839 palkovsky 156
{
4055 trochtova 157
    *((uint16_t *) dst)
158
        = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
839 palkovsky 159
}
160
 
4055 trochtova 161
 
162
/** RGB 3:2:3
1837 jermar 163
 *
164
 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
165
 * will most likely use a color palette. The color appearance
166
 * will be pretty random and depend on the default installed
167
 * palette. This could be fixed by supporting custom palette
168
 * and setting it to simulate the 8-bit truecolor.
4055 trochtova 169
 *
170
 * Currently we set the palette on the ia32, amd64 and sparc64 port.
171
 *
172
 * Note that the byte is being inverted by this function. The reason is
173
 * that we would like to use a color palette where the white color code
174
 * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
175
 * use these codes for black and white and prevent to set codes
176
 * 0 and 255 to other colors.
177
 *
1837 jermar 178
 */
4055 trochtova 179
static void rgb_323(void *dst, uint32_t rgb)
839 palkovsky 180
{
4055 trochtova 181
    *((uint8_t *) dst)
182
        = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
1135 decky 183
}
839 palkovsky 184
 
4055 trochtova 185
 
186
/** Hide logo and refresh screen
1837 jermar 187
 *
188
 */
4055 trochtova 189
static void logo_hide(bool silent)
1135 decky 190
{
4055 trochtova 191
    ylogo = 0;
192
    ytrim = yres;
193
    rowtrim = rows;
194
    if (!silent)
195
        fb_redraw();
839 palkovsky 196
}
197
 
1682 palkovsky 198
 
4055 trochtova 199
/** Draw character at given position
200
 *
201
 */
4296 trochtova 202
static void glyph_draw(uint16_t glyph, unsigned int col, unsigned int row, bool silent, bool overlay)
1682 palkovsky 203
{
4055 trochtova 204
    unsigned int x = COL2X(col);
205
    unsigned int y = ROW2Y(row);
206
    unsigned int yd;
207
 
208
    if (y >= ytrim)
209
        logo_hide(silent);
210
 
4296 trochtova 211
    if (!overlay)
212
        backbuf[BB_POS(col, row)] = glyph;
4055 trochtova 213
 
214
    if (!silent) {
215
        for (yd = 0; yd < FONT_SCANLINES; yd++)
216
            memcpy(&fb_addr[FB_POS(x, y + yd + ylogo)],
217
                &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
1682 palkovsky 218
    }
219
}
1135 decky 220
 
1682 palkovsky 221
 
4055 trochtova 222
/** Scroll screen down by one row
223
 *
224
 *
225
 */
226
static void screen_scroll(bool silent)
839 palkovsky 227
{
4055 trochtova 228
    if (ylogo > 0) {
229
        logo_hide(silent);
230
        return;
1682 palkovsky 231
    }
4055 trochtova 232
 
233
    if (!silent) {
234
        unsigned int row;
2054 jermar 235
 
4055 trochtova 236
        for (row = 0; row < rows; row++) {
237
            unsigned int y = ROW2Y(row);
238
            unsigned int yd;
239
 
240
            for (yd = 0; yd < FONT_SCANLINES; yd++) {
241
                unsigned int x;
242
                unsigned int col;
243
 
244
                for (col = 0, x = 0; col < cols; col++,
245
                    x += FONT_WIDTH) {
4201 trochtova 246
                    uint16_t glyph;
4055 trochtova 247
 
248
                    if (row < rows - 1) {
249
                        if (backbuf[BB_POS(col, row)] ==
250
                            backbuf[BB_POS(col, row + 1)])
251
                            continue;
252
 
253
                        glyph = backbuf[BB_POS(col, row + 1)];
254
                    } else
255
                        glyph = 0;
256
 
257
                    memcpy(&fb_addr[FB_POS(x, y + yd)],
258
                        &glyphs[GLYPH_POS(glyph, yd)],
259
                        glyphscanline);
260
                }
261
            }
2086 jermar 262
        }
1682 palkovsky 263
    }
4055 trochtova 264
 
4201 trochtova 265
    memmove(backbuf, &backbuf[BB_POS(0, 1)], cols * (rows - 1) * sizeof(uint16_t));
266
    memsetw(&backbuf[BB_POS(0, rows - 1)], cols, 0);
838 palkovsky 267
}
268
 
269
 
4055 trochtova 270
static void cursor_put(bool silent)
836 palkovsky 271
{
4296 trochtova 272
    unsigned int col = position % cols;
273
    unsigned int row = position / cols;
274
 
275
    glyph_draw(fb_font_glyph(U_CURSOR), col, row, silent, true);
836 palkovsky 276
}
277
 
278
 
4055 trochtova 279
static void cursor_remove(bool silent)
836 palkovsky 280
{
4296 trochtova 281
    unsigned int col = position % cols;
282
    unsigned int row = position / cols;
283
 
284
    glyph_draw(backbuf[BB_POS(col, row)], col, row, silent, true);
836 palkovsky 285
}
286
 
838 palkovsky 287
 
836 palkovsky 288
/** Print character to screen
289
 *
4055 trochtova 290
 * Emulate basic terminal commands.
291
 *
836 palkovsky 292
 */
4201 trochtova 293
static void fb_putchar(outdev_t *dev, wchar_t ch, bool silent)
836 palkovsky 294
{
1287 vana 295
    spinlock_lock(&fb_lock);
1135 decky 296
 
297
    switch (ch) {
1888 jermar 298
    case '\n':
4055 trochtova 299
        cursor_remove(silent);
300
        position += cols;
301
        position -= position % cols;
1888 jermar 302
        break;
303
    case '\r':
4055 trochtova 304
        cursor_remove(silent);
305
        position -= position % cols;
1888 jermar 306
        break;
307
    case '\b':
4055 trochtova 308
        cursor_remove(silent);
309
        if (position % cols)
1888 jermar 310
            position--;
311
        break;
312
    case '\t':
4055 trochtova 313
        cursor_remove(silent);
1888 jermar 314
        do {
4201 trochtova 315
            glyph_draw(fb_font_glyph(' '), position % cols,
4296 trochtova 316
                position / cols, silent, false);
838 palkovsky 317
            position++;
4055 trochtova 318
        } while ((position % 8) && (position < cols * rows));
1888 jermar 319
        break;
320
    default:
4201 trochtova 321
        glyph_draw(fb_font_glyph(ch), position % cols,
4296 trochtova 322
            position / cols, silent, false);
1888 jermar 323
        position++;
836 palkovsky 324
    }
1135 decky 325
 
4055 trochtova 326
    if (position >= cols * rows) {
327
        position -= cols;
328
        screen_scroll(silent);
836 palkovsky 329
    }
1135 decky 330
 
4055 trochtova 331
    cursor_put(silent);
1135 decky 332
 
1287 vana 333
    spinlock_unlock(&fb_lock);
836 palkovsky 334
}
335
 
4156 trochtova 336
static outdev_t fb_console;
337
static outdev_operations_t fb_ops = {
338
    .write = fb_putchar
836 palkovsky 339
};
340
 
341
 
4055 trochtova 342
/** Render glyphs
343
 *
344
 * Convert glyphs from device independent font
345
 * description to current visual representation.
346
 *
347
 */
348
static void glyphs_render(void)
349
{
350
    /* Prerender glyphs */
4201 trochtova 351
    uint16_t glyph;
4055 trochtova 352
 
353
    for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
4201 trochtova 354
        uint32_t fg_color;
355
 
356
        if (glyph == FONT_GLYPHS - 1)
357
            fg_color = INV_COLOR;
358
        else
359
            fg_color = FG_COLOR;
360
 
4055 trochtova 361
        unsigned int y;
362
 
363
        for (y = 0; y < FONT_SCANLINES; y++) {
364
            unsigned int x;
365
 
366
            for (x = 0; x < FONT_WIDTH; x++) {
367
                void *dst = &glyphs[GLYPH_POS(glyph, y) +
368
                    x * pixelbytes];
4201 trochtova 369
                uint32_t rgb = (fb_font[glyph][y] &
370
                    (1 << (7 - x))) ? fg_color : BG_COLOR;
4055 trochtova 371
                rgb_conv(dst, rgb);
372
            }
373
        }
374
    }
375
 
376
    /* Prerender background scanline */
377
    unsigned int x;
378
 
379
    for (x = 0; x < xres; x++)
380
        rgb_conv(&bgscan[x * pixelbytes], BG_COLOR);
381
}
382
 
383
 
384
/** Refresh the screen
385
 *
386
 */
387
void fb_redraw(void)
388
{
389
    if (ylogo > 0) {
390
        unsigned int y;
391
 
392
        for (y = 0; y < LOGO_HEIGHT; y++) {
393
            unsigned int x;
394
 
395
            for (x = 0; x < xres; x++)
396
                rgb_conv(&fb_addr[FB_POS(x, y)],
397
                    (x < LOGO_WIDTH) ?
398
                    fb_logo[y * LOGO_WIDTH + x] :
399
                    LOGO_COLOR);
400
        }
401
    }
402
 
403
    unsigned int row;
404
 
405
    for (row = 0; row < rowtrim; row++) {
406
        unsigned int y = ylogo + ROW2Y(row);
407
        unsigned int yd;
408
 
409
        for (yd = 0; yd < FONT_SCANLINES; yd++) {
410
            unsigned int x;
411
            unsigned int col;
412
 
413
            for (col = 0, x = 0; col < cols;
414
                col++, x += FONT_WIDTH) {
4201 trochtova 415
                uint16_t glyph = backbuf[BB_POS(col, row)];
416
                void *dst = &fb_addr[FB_POS(x, y + yd)];
417
                void *src = &glyphs[GLYPH_POS(glyph, yd)];
418
                memcpy(dst, src, glyphscanline);
4055 trochtova 419
            }
420
        }
421
    }
422
 
423
    if (COL2X(cols) < xres) {
424
        unsigned int y;
425
        unsigned int size = (xres - COL2X(cols)) * pixelbytes;
426
 
427
        for (y = ylogo; y < yres; y++)
428
            memcpy(&fb_addr[FB_POS(COL2X(cols), y)], bgscan, size);
429
    }
430
 
431
    if (ROW2Y(rowtrim) + ylogo < yres) {
432
        unsigned int y;
433
 
434
        for (y = ROW2Y(rowtrim) + ylogo; y < yres; y++)
435
            memcpy(&fb_addr[FB_POS(0, y)], bgscan, bgscanbytes);
436
    }
437
}
438
 
439
 
4156 trochtova 440
/** Initialize framebuffer as a output character device
836 palkovsky 441
 *
1993 decky 442
 * @param addr   Physical address of the framebuffer
443
 * @param x      Screen width in pixels
444
 * @param y      Screen height in pixels
445
 * @param scan   Bytes per one scanline
446
 * @param visual Color model
447
 *
836 palkovsky 448
 */
4055 trochtova 449
void fb_init(fb_properties_t *props)
836 palkovsky 450
{
4055 trochtova 451
    switch (props->visual) {
1993 decky 452
    case VISUAL_INDIRECT_8:
4055 trochtova 453
        rgb_conv = rgb_323;
1991 jermar 454
        pixelbytes = 1;
455
        break;
1993 decky 456
    case VISUAL_RGB_5_5_5:
4055 trochtova 457
        rgb_conv = rgb_555;
1991 jermar 458
        pixelbytes = 2;
459
        break;
1993 decky 460
    case VISUAL_RGB_5_6_5:
4055 trochtova 461
        rgb_conv = rgb_565;
1993 decky 462
        pixelbytes = 2;
1991 jermar 463
        break;
1993 decky 464
    case VISUAL_RGB_8_8_8:
4055 trochtova 465
        rgb_conv = rgb_888;
1993 decky 466
        pixelbytes = 3;
467
        break;
4055 trochtova 468
    case VISUAL_BGR_8_8_8:
469
        rgb_conv = bgr_888;
470
        pixelbytes = 3;
471
        break;
1993 decky 472
    case VISUAL_RGB_8_8_8_0:
4055 trochtova 473
        rgb_conv = rgb_888;
1991 jermar 474
        pixelbytes = 4;
475
        break;
1993 decky 476
    case VISUAL_RGB_0_8_8_8:
4055 trochtova 477
        rgb_conv = rgb_0888;
1993 decky 478
        pixelbytes = 4;
479
        break;
1994 decky 480
    case VISUAL_BGR_0_8_8_8:
4055 trochtova 481
        rgb_conv = bgr_0888;
1994 decky 482
        pixelbytes = 4;
483
        break;
1991 jermar 484
    default:
4055 trochtova 485
        panic("Unsupported visual.");
839 palkovsky 486
    }
1371 decky 487
 
4055 trochtova 488
    xres = props->x;
489
    yres = props->y;
490
    scanline = props->scan;
1371 decky 491
 
4055 trochtova 492
    cols = X2COL(xres);
493
    rows = Y2ROW(yres);
1371 decky 494
 
4055 trochtova 495
    if (yres > ylogo) {
496
        ylogo = LOGO_HEIGHT;
497
        rowtrim = rows - Y2ROW(ylogo);
498
        if (ylogo % FONT_SCANLINES > 0)
499
            rowtrim--;
500
        ytrim = ROW2Y(rowtrim);
501
    } else {
502
        ylogo = 0;
503
        ytrim = yres;
504
        rowtrim = rows;
505
    }
836 palkovsky 506
 
4055 trochtova 507
    glyphscanline = FONT_WIDTH * pixelbytes;
508
    glyphbytes = ROW2Y(glyphscanline);
509
    bgscanbytes = xres * pixelbytes;
510
 
4201 trochtova 511
    size_t fbsize = scanline * yres;
512
    size_t bbsize = cols * rows * sizeof(uint16_t);
513
    size_t glyphsize = FONT_GLYPHS * glyphbytes;
4055 trochtova 514
 
4201 trochtova 515
    backbuf = (uint16_t *) malloc(bbsize, 0);
4055 trochtova 516
    if (!backbuf)
517
        panic("Unable to allocate backbuffer.");
518
 
519
    glyphs = (uint8_t *) malloc(glyphsize, 0);
520
    if (!glyphs)
521
        panic("Unable to allocate glyphs.");
522
 
523
    bgscan = malloc(bgscanbytes, 0);
524
    if (!bgscan)
525
        panic("Unable to allocate background pixel.");
526
 
4201 trochtova 527
    memsetw(backbuf, cols * rows, 0);
4055 trochtova 528
 
529
    glyphs_render();
530
 
531
    fb_addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize);
532
 
1690 palkovsky 533
    sysinfo_set_item_val("fb", NULL, true);
534
    sysinfo_set_item_val("fb.kind", NULL, 1);
535
    sysinfo_set_item_val("fb.width", NULL, xres);
536
    sysinfo_set_item_val("fb.height", NULL, yres);
4055 trochtova 537
    sysinfo_set_item_val("fb.scanline", NULL, scanline);
538
    sysinfo_set_item_val("fb.visual", NULL, props->visual);
539
    sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
1990 decky 540
 
4055 trochtova 541
    fb_redraw();
542
 
4156 trochtova 543
    outdev_initialize("fb", &fb_console, &fb_ops);
544
    stdout = &fb_console;
836 palkovsky 545
}
1702 cejka 546
 
1790 jermar 547
/** @}
1702 cejka 548
 */