Subversion Repositories HelenOS

Rev

Rev 3876 | Rev 4177 | 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;
56
static uint8_t *backbuf;
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
836 palkovsky 80
 
3707 decky 81
#define CURSOR       219
1135 decky 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
 
3707 decky 119
 
3876 decky 120
/** RGB 8:8:8 conversion
3707 decky 121
 *
122
 */
123
static void rgb_888(void *dst, uint32_t rgb)
839 palkovsky 124
{
3710 decky 125
    ((uint8_t *) dst)[0] = BLUE(rgb, 8);
126
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
127
    ((uint8_t *) dst)[2] = RED(rgb, 8);
839 palkovsky 128
}
129
 
838 palkovsky 130
 
3876 decky 131
/** BGR 8:8:8 conversion
132
 *
133
 */
134
static void bgr_888(void *dst, uint32_t rgb)
135
{
136
    ((uint8_t *) dst)[0] = RED(rgb, 8);
137
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
138
    ((uint8_t *) dst)[2] = BLUE(rgb, 8);
139
}
140
 
141
 
3707 decky 142
/** RGB 5:5:5 conversion
143
 *
144
 */
145
static void rgb_555(void *dst, uint32_t rgb)
1993 decky 146
{
3707 decky 147
    *((uint16_t *) dst)
148
        = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
1993 decky 149
}
150
 
151
 
3707 decky 152
/** RGB 5:6:5 conversion
153
 *
154
 */
155
static void rgb_565(void *dst, uint32_t rgb)
839 palkovsky 156
{
3707 decky 157
    *((uint16_t *) dst)
158
        = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
839 palkovsky 159
}
160
 
161
 
3707 decky 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.
3692 rimsky 169
 *
3709 decky 170
 * Currently we set the palette on the ia32, amd64 and sparc64 port.
3692 rimsky 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
3709 decky 174
 * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
3692 rimsky 175
 * use these codes for black and white and prevent to set codes
176
 * 0 and 255 to other colors.
3709 decky 177
 *
1837 jermar 178
 */
3707 decky 179
static void rgb_323(void *dst, uint32_t rgb)
839 palkovsky 180
{
3707 decky 181
    *((uint8_t *) dst)
182
        = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
1135 decky 183
}
839 palkovsky 184
 
3707 decky 185
 
3710 decky 186
/** Hide logo and refresh screen
187
 *
188
 */
3844 decky 189
static void logo_hide(bool silent)
3710 decky 190
{
191
    ylogo = 0;
192
    ytrim = yres;
193
    rowtrim = rows;
3844 decky 194
    if (!silent)
195
        fb_redraw();
3710 decky 196
}
197
 
198
 
3707 decky 199
/** Draw character at given position
1837 jermar 200
 *
201
 */
3844 decky 202
static void glyph_draw(uint8_t glyph, unsigned int col, unsigned int row, bool silent)
1135 decky 203
{
3707 decky 204
    unsigned int x = COL2X(col);
205
    unsigned int y = ROW2Y(row);
206
    unsigned int yd;
207
 
3710 decky 208
    if (y >= ytrim)
3844 decky 209
        logo_hide(silent);
3710 decky 210
 
3707 decky 211
    backbuf[BB_POS(col, row)] = glyph;
212
 
3844 decky 213
    if (!silent) {
214
        for (yd = 0; yd < FONT_SCANLINES; yd++)
215
            memcpy(&fb_addr[FB_POS(x, y + yd + ylogo)],
216
                &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
217
    }
839 palkovsky 218
}
219
 
1682 palkovsky 220
 
3707 decky 221
/** Scroll screen down by one row
222
 *
223
 *
224
 */
3844 decky 225
static void screen_scroll(bool silent)
838 palkovsky 226
{
3733 decky 227
    if (ylogo > 0) {
3844 decky 228
        logo_hide(silent);
3733 decky 229
        return;
230
    }
3710 decky 231
 
3844 decky 232
    if (!silent) {
233
        unsigned int row;
2054 jermar 234
 
3844 decky 235
        for (row = 0; row < rows; row++) {
236
            unsigned int y = ROW2Y(row);
237
            unsigned int yd;
3707 decky 238
 
3844 decky 239
            for (yd = 0; yd < FONT_SCANLINES; yd++) {
240
                unsigned int x;
241
                unsigned int col;
3707 decky 242
 
3844 decky 243
                for (col = 0, x = 0; col < cols; col++,
244
                    x += FONT_WIDTH) {
245
                    uint8_t glyph;
3707 decky 246
 
3844 decky 247
                    if (row < rows - 1) {
248
                        if (backbuf[BB_POS(col, row)] ==
249
                            backbuf[BB_POS(col, row + 1)])
250
                            continue;
251
 
252
                        glyph = backbuf[BB_POS(col, row + 1)];
253
                    } else
254
                        glyph = 0;
255
 
256
                    memcpy(&fb_addr[FB_POS(x, y + yd)],
257
                        &glyphs[GLYPH_POS(glyph, yd)],
258
                        glyphscanline);
259
                }
3707 decky 260
            }
2086 jermar 261
        }
1682 palkovsky 262
    }
3707 decky 263
 
3752 svoboda 264
    memmove(backbuf, backbuf + cols, cols * (rows - 1));
3707 decky 265
    memsetb(&backbuf[BB_POS(0, rows - 1)], cols, 0);
838 palkovsky 266
}
267
 
268
 
3844 decky 269
static void cursor_put(bool silent)
836 palkovsky 270
{
3844 decky 271
    glyph_draw(CURSOR, position % cols, position / cols, silent);
836 palkovsky 272
}
273
 
274
 
3844 decky 275
static void cursor_remove(bool silent)
836 palkovsky 276
{
3844 decky 277
    glyph_draw(0, position % cols, position / cols, silent);
836 palkovsky 278
}
279
 
838 palkovsky 280
 
836 palkovsky 281
/** Print character to screen
282
 *
3707 decky 283
 * Emulate basic terminal commands.
284
 *
836 palkovsky 285
 */
3844 decky 286
static void fb_putchar(chardev_t *dev, char ch, bool silent)
836 palkovsky 287
{
1287 vana 288
    spinlock_lock(&fb_lock);
1135 decky 289
 
290
    switch (ch) {
1888 jermar 291
    case '\n':
3844 decky 292
        cursor_remove(silent);
3707 decky 293
        position += cols;
294
        position -= position % cols;
1888 jermar 295
        break;
296
    case '\r':
3844 decky 297
        cursor_remove(silent);
3707 decky 298
        position -= position % cols;
1888 jermar 299
        break;
300
    case '\b':
3844 decky 301
        cursor_remove(silent);
3707 decky 302
        if (position % cols)
1888 jermar 303
            position--;
304
        break;
305
    case '\t':
3844 decky 306
        cursor_remove(silent);
1888 jermar 307
        do {
3741 jermar 308
            glyph_draw((uint8_t) ' ', position % cols,
3844 decky 309
                position / cols, silent);
838 palkovsky 310
            position++;
3707 decky 311
        } while ((position % 8) && (position < cols * rows));
1888 jermar 312
        break;
313
    default:
3844 decky 314
        glyph_draw((uint8_t) ch, position % cols,
315
            position / cols, silent);
1888 jermar 316
        position++;
836 palkovsky 317
    }
1135 decky 318
 
3707 decky 319
    if (position >= cols * rows) {
320
        position -= cols;
3844 decky 321
        screen_scroll(silent);
836 palkovsky 322
    }
1135 decky 323
 
3844 decky 324
    cursor_put(silent);
1135 decky 325
 
1287 vana 326
    spinlock_unlock(&fb_lock);
836 palkovsky 327
}
328
 
329
static chardev_t framebuffer;
330
static chardev_operations_t fb_ops = {
331
    .write = fb_putchar,
332
};
333
 
334
 
3707 decky 335
/** Render glyphs
336
 *
337
 * Convert glyphs from device independent font
338
 * description to current visual representation.
339
 *
340
 */
3710 decky 341
static void glyphs_render(void)
3707 decky 342
{
3710 decky 343
    /* Prerender glyphs */
3707 decky 344
    unsigned int glyph;
345
 
346
    for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
347
        unsigned int y;
348
 
349
        for (y = 0; y < FONT_SCANLINES; y++) {
350
            unsigned int x;
351
 
3741 jermar 352
            for (x = 0; x < FONT_WIDTH; x++) {
353
                void *dst = &glyphs[GLYPH_POS(glyph, y) +
354
                    x * pixelbytes];
355
                uint32_t rgb = (fb_font[ROW2Y(glyph) + y] &
356
                    (1 << (7 - x))) ? FG_COLOR : BG_COLOR;
357
                rgb_conv(dst, rgb);
358
            }
3707 decky 359
        }
360
    }
361
 
3710 decky 362
    /* Prerender background scanline */
363
    unsigned int x;
364
 
365
    for (x = 0; x < xres; x++)
366
        rgb_conv(&bgscan[x * pixelbytes], BG_COLOR);
3707 decky 367
}
368
 
369
 
370
/** Refresh the screen
371
 *
372
 */
373
void fb_redraw(void)
374
{
3710 decky 375
    if (ylogo > 0) {
376
        unsigned int y;
377
 
378
        for (y = 0; y < LOGO_HEIGHT; y++) {
379
            unsigned int x;
380
 
381
            for (x = 0; x < xres; x++)
382
                rgb_conv(&fb_addr[FB_POS(x, y)],
3741 jermar 383
                    (x < LOGO_WIDTH) ?
384
                    fb_logo[y * LOGO_WIDTH + x] :
385
                    LOGO_COLOR);
3710 decky 386
        }
387
    }
388
 
3707 decky 389
    unsigned int row;
390
 
3710 decky 391
    for (row = 0; row < rowtrim; row++) {
392
        unsigned int y = ylogo + ROW2Y(row);
3707 decky 393
        unsigned int yd;
394
 
395
        for (yd = 0; yd < FONT_SCANLINES; yd++) {
396
            unsigned int x;
397
            unsigned int col;
398
 
3741 jermar 399
            for (col = 0, x = 0; col < cols;
400
                col++, x += FONT_WIDTH) {
401
                void *d = &fb_addr[FB_POS(x, y + yd)];
402
                void *s = &glyphs[GLYPH_POS(backbuf[BB_POS(col,
403
                    row)], yd)];
404
                memcpy(d, s, glyphscanline);
405
            }
3707 decky 406
        }
407
    }
408
 
409
    if (COL2X(cols) < xres) {
410
        unsigned int y;
3710 decky 411
        unsigned int size = (xres - COL2X(cols)) * pixelbytes;
3707 decky 412
 
3710 decky 413
        for (y = ylogo; y < yres; y++)
414
            memcpy(&fb_addr[FB_POS(COL2X(cols), y)], bgscan, size);
3707 decky 415
    }
416
 
3733 decky 417
    if (ROW2Y(rowtrim) + ylogo < yres) {
3707 decky 418
        unsigned int y;
419
 
3733 decky 420
        for (y = ROW2Y(rowtrim) + ylogo; y < yres; y++)
3710 decky 421
            memcpy(&fb_addr[FB_POS(0, y)], bgscan, bgscanbytes);
3707 decky 422
    }
423
}
424
 
425
 
836 palkovsky 426
/** Initialize framebuffer as a chardev output device
427
 *
3707 decky 428
 * @param addr   Physical address of the framebuffer
429
 * @param x      Screen width in pixels
430
 * @param y      Screen height in pixels
431
 * @param scan   Bytes per one scanline
432
 * @param visual Color model
433
 *
836 palkovsky 434
 */
3672 jermar 435
void fb_init(fb_properties_t *props)
836 palkovsky 436
{
3672 jermar 437
    switch (props->visual) {
1993 decky 438
    case VISUAL_INDIRECT_8:
3707 decky 439
        rgb_conv = rgb_323;
1991 jermar 440
        pixelbytes = 1;
441
        break;
1993 decky 442
    case VISUAL_RGB_5_5_5:
3707 decky 443
        rgb_conv = rgb_555;
1991 jermar 444
        pixelbytes = 2;
445
        break;
1993 decky 446
    case VISUAL_RGB_5_6_5:
3707 decky 447
        rgb_conv = rgb_565;
1993 decky 448
        pixelbytes = 2;
1991 jermar 449
        break;
1993 decky 450
    case VISUAL_RGB_8_8_8:
3707 decky 451
        rgb_conv = rgb_888;
1993 decky 452
        pixelbytes = 3;
453
        break;
3876 decky 454
    case VISUAL_BGR_8_8_8:
455
        rgb_conv = bgr_888;
456
        pixelbytes = 3;
457
        break;
1993 decky 458
    case VISUAL_RGB_8_8_8_0:
3707 decky 459
        rgb_conv = rgb_888;
1991 jermar 460
        pixelbytes = 4;
461
        break;
1993 decky 462
    case VISUAL_RGB_0_8_8_8:
3707 decky 463
        rgb_conv = rgb_0888;
1993 decky 464
        pixelbytes = 4;
465
        break;
1994 decky 466
    case VISUAL_BGR_0_8_8_8:
3707 decky 467
        rgb_conv = bgr_0888;
1994 decky 468
        pixelbytes = 4;
469
        break;
1991 jermar 470
    default:
3790 svoboda 471
        panic("Unsupported visual.");
839 palkovsky 472
    }
1371 decky 473
 
3672 jermar 474
    xres = props->x;
475
    yres = props->y;
476
    scanline = props->scan;
836 palkovsky 477
 
3710 decky 478
    cols = X2COL(xres);
479
    rows = Y2ROW(yres);
3707 decky 480
 
3710 decky 481
    if (yres > ylogo) {
482
        ylogo = LOGO_HEIGHT;
483
        rowtrim = rows - Y2ROW(ylogo);
484
        if (ylogo % FONT_SCANLINES > 0)
485
            rowtrim--;
486
        ytrim = ROW2Y(rowtrim);
487
    } else {
488
        ylogo = 0;
489
        ytrim = yres;
490
        rowtrim = rows;
491
    }
492
 
3707 decky 493
    glyphscanline = FONT_WIDTH * pixelbytes;
3710 decky 494
    glyphbytes = ROW2Y(glyphscanline);
495
    bgscanbytes = xres * pixelbytes;
3707 decky 496
 
497
    unsigned int fbsize = scanline * yres;
498
    unsigned int bbsize = cols * rows;
499
    unsigned int glyphsize = FONT_GLYPHS * glyphbytes;
500
 
501
    backbuf = (uint8_t *) malloc(bbsize, 0);
502
    if (!backbuf)
3790 svoboda 503
        panic("Unable to allocate backbuffer.");
3707 decky 504
 
505
    glyphs = (uint8_t *) malloc(glyphsize, 0);
506
    if (!glyphs)
3790 svoboda 507
        panic("Unable to allocate glyphs.");
3707 decky 508
 
3710 decky 509
    bgscan = malloc(bgscanbytes, 0);
510
    if (!bgscan)
3790 svoboda 511
        panic("Unable to allocate background pixel.");
3707 decky 512
 
513
    memsetb(backbuf, bbsize, 0);
514
 
3710 decky 515
    glyphs_render();
3707 decky 516
 
517
    fb_addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize);
518
 
1690 palkovsky 519
    sysinfo_set_item_val("fb", NULL, true);
520
    sysinfo_set_item_val("fb.kind", NULL, 1);
521
    sysinfo_set_item_val("fb.width", NULL, xres);
522
    sysinfo_set_item_val("fb.height", NULL, yres);
3707 decky 523
    sysinfo_set_item_val("fb.scanline", NULL, scanline);
3672 jermar 524
    sysinfo_set_item_val("fb.visual", NULL, props->visual);
525
    sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
1990 decky 526
 
3707 decky 527
    fb_redraw();
528
 
836 palkovsky 529
    chardev_initialize("fb", &framebuffer, &fb_ops);
530
    stdout = &framebuffer;
531
}
1702 cejka 532
 
1790 jermar 533
/** @}
1702 cejka 534
 */