Subversion Repositories HelenOS

Rev

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