Subversion Repositories HelenOS

Rev

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

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