Subversion Repositories HelenOS

Rev

Rev 4647 | Details | Compare with Previous | Last modification | View Log | RSS feed

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