Subversion Repositories HelenOS

Rev

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