Subversion Repositories HelenOS

Rev

Rev 3022 | Rev 4201 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3022 Rev 4055
Line 1... Line 1...
1
/*
1
/*
-
 
2
 * Copyright (c) 2008 Martin Decky
2
 * Copyright (c) 2006 Ondrej Palkovsky
3
 * Copyright (c) 2006 Ondrej Palkovsky
3
 * All rights reserved.
4
 * All rights reserved.
4
 *
5
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
Line 24... Line 25...
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 */
28
 
29
 
29
/** @addtogroup genarch
30
/** @addtogroup genarch
30
 * @{
31
 * @{
31
 */
32
 */
32
/** @file
33
/** @file
33
 */
34
 */
34
 
35
 
35
#include <genarch/fb/font-8x16.h>
36
#include <genarch/fb/font-8x16.h>
-
 
37
#include <genarch/fb/logo-196x66.h>
36
#include <genarch/fb/visuals.h>
38
#include <genarch/fb/visuals.h>
37
#include <genarch/fb/fb.h>
39
#include <genarch/fb/fb.h>
38
#include <console/chardev.h>
40
#include <console/chardev.h>
39
#include <console/console.h>
41
#include <console/console.h>
40
#include <sysinfo/sysinfo.h>
42
#include <sysinfo/sysinfo.h>
41
#include <mm/slab.h>
43
#include <mm/slab.h>
-
 
44
#include <align.h>
42
#include <panic.h>
45
#include <panic.h>
43
#include <memstr.h>
46
#include <memstr.h>
44
#include <config.h>
47
#include <config.h>
45
#include <bitops.h>
48
#include <bitops.h>
46
#include <print.h>
49
#include <print.h>
47
#include <ddi/ddi.h>
50
#include <ddi/ddi.h>
48
#include <arch/types.h>
51
#include <arch/types.h>
49
 
52
 
50
#include "helenos.xbm"
53
SPINLOCK_INITIALIZE(fb_lock);
51
 
54
 
-
 
55
static uint8_t *fb_addr;
-
 
56
static uint8_t *backbuf;
-
 
57
static uint8_t *glyphs;
-
 
58
static uint8_t *bgscan;
-
 
59
 
-
 
60
static unsigned int xres;
-
 
61
static unsigned int yres;
-
 
62
 
-
 
63
static unsigned int ylogo;
-
 
64
static unsigned int ytrim;
-
 
65
static unsigned int rowtrim;
-
 
66
 
-
 
67
static unsigned int scanline;
-
 
68
static unsigned int glyphscanline;
-
 
69
 
-
 
70
static unsigned int pixelbytes;
-
 
71
static unsigned int glyphbytes;
52
static parea_t fb_parea;        /**< Physical memory area for fb. */
72
static unsigned int bgscanbytes;
53
 
73
 
-
 
74
static unsigned int cols;
54
SPINLOCK_INITIALIZE(fb_lock);
75
static unsigned int rows;
-
 
76
static unsigned int position = 0;
55
 
77
 
-
 
78
#define BG_COLOR     0x000080
56
static uint8_t *fbaddress = NULL;
79
#define FG_COLOR     0xffff00
57
 
80
 
58
static uint8_t *blankline = NULL;
-
 
59
static uint8_t *dbbuffer = NULL;    /* Buffer for fast scrolling console */
-
 
60
static index_t dboffset;
-
 
61
 
-
 
62
static unsigned int xres = 0;
-
 
63
static unsigned int yres = 0;
-
 
64
static unsigned int scanline = 0;
-
 
65
static unsigned int pixelbytes = 0;
-
 
66
#ifdef FB_INVERT_COLORS
81
#define CURSOR       219
67
static bool invert_colors = true;
-
 
68
#else
-
 
69
static bool invert_colors = false;
-
 
70
#endif
-
 
71
 
82
 
72
static unsigned int position = 0;
83
#define RED(x, bits)         ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
73
static unsigned int columns = 0;
84
#define GREEN(x, bits)       ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
74
static unsigned int rows = 0;
85
#define BLUE(x, bits)        ((x >> (8 - bits)) & ((1 << bits) - 1))
75
 
86
 
76
#define COL_WIDTH   8
87
#define COL2X(col)           ((col) * FONT_WIDTH)
77
#define ROW_BYTES   (scanline * FONT_SCANLINES)
88
#define ROW2Y(row)           ((row) * FONT_SCANLINES)
78
 
89
 
79
#define BGCOLOR     0x000080
-
 
80
#define FGCOLOR     0xffff00
90
#define X2COL(x)             ((x) / FONT_WIDTH)
81
#define LOGOCOLOR   0x2020b0
91
#define Y2ROW(y)             ((y) / FONT_SCANLINES)
82
 
92
 
83
#define RED(x, bits)    ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
93
#define FB_POS(x, y)         ((y) * scanline + (x) * pixelbytes)
84
#define GREEN(x, bits)  ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
94
#define BB_POS(col, row)     ((row) * cols + (col))
85
#define BLUE(x, bits)   ((x >> (8 - bits)) & ((1 << bits) - 1))
95
#define GLYPH_POS(glyph, y)  ((glyph) * glyphbytes + (y) * glyphscanline)
86
 
96
 
87
#define POINTPOS(x, y)  ((y) * scanline + (x) * pixelbytes)
-
 
88
 
97
 
89
/***************************************************************/
-
 
90
/* Pixel specific fuctions */
98
static void (*rgb_conv)(void *, uint32_t);
91
 
99
 
92
static void (*rgb2scr)(void *, int);
-
 
93
static int (*scr2rgb)(void *);
-
 
94
 
100
 
-
 
101
/** ARGB 8:8:8:8 conversion
-
 
102
 *
-
 
103
 */
95
static inline int COLOR(int color)
104
static void rgb_0888(void *dst, uint32_t rgb)
96
{
105
{
97
    return invert_colors ? ~color : color;
106
    *((uint32_t *) dst) = rgb & 0xffffff;
98
}
107
}
99
 
108
 
100
/* Conversion routines between different color representations */
-
 
101
static void rgb_byte0888(void *dst, int rgb)
-
 
102
{
-
 
103
    *((int *) dst) = rgb;
-
 
104
}
-
 
105
 
109
 
-
 
110
/** ABGR 8:8:8:8 conversion
-
 
111
 *
-
 
112
 */
106
static int byte0888_rgb(void *src)
113
static void bgr_0888(void *dst, uint32_t rgb)
107
{
114
{
108
    return (*((int *) src)) & 0xffffff;
115
    *((uint32_t *) dst)
-
 
116
        = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
109
}
117
}
110
 
118
 
111
static void bgr_byte0888(void *dst, int rgb)
-
 
112
{
-
 
113
    *((uint32_t *) dst) = BLUE(rgb, 8) << 16 | GREEN(rgb, 8) << 8 |
-
 
114
        RED(rgb, 8);
-
 
115
}
-
 
116
 
119
 
-
 
120
/** RGB 8:8:8 conversion
-
 
121
 *
-
 
122
 */
117
static int byte0888_bgr(void *src)
123
static void rgb_888(void *dst, uint32_t rgb)
118
{
124
{
119
    int color = *(uint32_t *)(src);
125
    ((uint8_t *) dst)[0] = BLUE(rgb, 8);
120
    return ((color & 0xff) << 16) | (((color >> 8) & 0xff) << 8) |
126
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
121
        ((color >> 16) & 0xff);
127
    ((uint8_t *) dst)[2] = RED(rgb, 8);
122
}
128
}
123
 
129
 
124
static void rgb_byte888(void *dst, int rgb)
-
 
125
{
-
 
126
    uint8_t *scr = (uint8_t *) dst;
-
 
127
#if defined(FB_INVERT_ENDIAN)
-
 
128
    scr[0] = RED(rgb, 8);
-
 
129
    scr[1] = GREEN(rgb, 8);
-
 
130
    scr[2] = BLUE(rgb, 8);
-
 
131
#else
-
 
132
    scr[2] = RED(rgb, 8);
-
 
133
    scr[1] = GREEN(rgb, 8);
-
 
134
    scr[0] = BLUE(rgb, 8);
-
 
135
#endif
-
 
136
}
-
 
137
 
130
 
-
 
131
/** BGR 8:8:8 conversion
-
 
132
 *
-
 
133
 */
138
static int byte888_rgb(void *src)
134
static void bgr_888(void *dst, uint32_t rgb)
139
{
135
{
140
    uint8_t *scr = (uint8_t *) src;
136
    ((uint8_t *) dst)[0] = RED(rgb, 8);
141
#if defined(FB_INVERT_ENDIAN)
-
 
142
    return scr[0] << 16 | scr[1] << 8 | scr[2];
137
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
143
#else
-
 
144
    return scr[2] << 16 | scr[1] << 8 | scr[0];
138
    ((uint8_t *) dst)[2] = BLUE(rgb, 8);
145
#endif  
-
 
146
}
139
}
147
 
140
 
148
/**  16-bit depth (5:5:5) */
-
 
149
static void rgb_byte555(void *dst, int rgb)
-
 
150
{
-
 
151
    /* 5-bit, 5-bits, 5-bits */
-
 
152
    *((uint16_t *) dst) = RED(rgb, 5) << 10 | GREEN(rgb, 5) << 5 |
-
 
153
        BLUE(rgb, 5);
-
 
154
}
-
 
155
 
141
 
156
/** 16-bit depth (5:5:5) */
142
/** RGB 5:5:5 conversion
-
 
143
 *
-
 
144
 */
157
static int byte555_rgb(void *src)
145
static void rgb_555(void *dst, uint32_t rgb)
158
{
146
{
159
    int color = *(uint16_t *)(src);
147
    *((uint16_t *) dst)
160
    return (((color >> 10) & 0x1f) << (16 + 3)) |
-
 
161
        (((color >> 5) & 0x1f) << (8 + 3)) | ((color & 0x1f) << 3);
148
        = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
162
}
149
}
163
 
150
 
164
/**  16-bit depth (5:6:5) */
-
 
165
static void rgb_byte565(void *dst, int rgb)
-
 
166
{
-
 
167
    /* 5-bit, 6-bits, 5-bits */
-
 
168
    *((uint16_t *) dst) = RED(rgb, 5) << 11 | GREEN(rgb, 6) << 5 |
-
 
169
        BLUE(rgb, 5);
-
 
170
}
-
 
171
 
151
 
172
/** 16-bit depth (5:6:5) */
152
/** RGB 5:6:5 conversion
-
 
153
 *
-
 
154
 */
173
static int byte565_rgb(void *src)
155
static void rgb_565(void *dst, uint32_t rgb)
174
{
156
{
175
    int color = *(uint16_t *)(src);
157
    *((uint16_t *) dst)
176
    return (((color >> 11) & 0x1f) << (16 + 3)) |
-
 
177
        (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
158
        = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
178
}
159
}
179
 
160
 
-
 
161
 
180
/** Put pixel - 8-bit depth (color palette/3:2:3)
162
/** RGB 3:2:3
181
 *
163
 *
182
 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
164
 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
183
 * will most likely use a color palette. The color appearance
165
 * will most likely use a color palette. The color appearance
184
 * will be pretty random and depend on the default installed
166
 * will be pretty random and depend on the default installed
185
 * palette. This could be fixed by supporting custom palette
167
 * palette. This could be fixed by supporting custom palette
186
 * and setting it to simulate the 8-bit truecolor.
168
 * and setting it to simulate the 8-bit truecolor.
-
 
169
 *
-
 
170
 * Currently we set the palette on the ia32, amd64 and sparc64 port.
-
 
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
-
 
174
 * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
-
 
175
 * use these codes for black and white and prevent to set codes
-
 
176
 * 0 and 255 to other colors.
-
 
177
 *
187
 */
178
 */
188
static void rgb_byte8(void *dst, int rgb)
179
static void rgb_323(void *dst, uint32_t rgb)
189
{
180
{
190
    *((uint8_t *) dst) = RED(rgb, 3) << 5 | GREEN(rgb, 2) << 3 |
181
    *((uint8_t *) dst)
191
        BLUE(rgb, 3);
182
        = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
192
}
183
}
193
 
184
 
-
 
185
 
194
/** Return pixel color - 8-bit depth (color palette/3:2:3)
186
/** Hide logo and refresh screen
195
 *
187
 *
196
 * See the comment for rgb_byte().
-
 
197
 */
188
 */
198
static int byte8_rgb(void *src)
189
static void logo_hide(bool silent)
199
{
190
{
-
 
191
    ylogo = 0;
200
    int color = *(uint8_t *)src;
192
    ytrim = yres;
201
    return (((color >> 5) & 0x7) << (16 + 5)) |
193
    rowtrim = rows;
-
 
194
    if (!silent)
202
        (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
195
        fb_redraw();
203
}
196
}
204
 
197
 
205
static void putpixel(unsigned int x, unsigned int y, int color)
-
 
206
{
-
 
207
    (*rgb2scr)(&fbaddress[POINTPOS(x, y)], COLOR(color));
-
 
208
 
198
 
209
    if (dbbuffer) {
-
 
210
        int dline = (y + dboffset) % yres;
199
/** Draw character at given position
211
        (*rgb2scr)(&dbbuffer[POINTPOS(x, dline)], COLOR(color));
-
 
212
    }
200
 *
213
}
201
 */
214
 
-
 
215
/** Get pixel from viewport */
-
 
216
static int getpixel(unsigned int x, unsigned int y)
202
static void glyph_draw(uint8_t glyph, unsigned int col, unsigned int row, bool silent)
217
{
203
{
-
 
204
    unsigned int x = COL2X(col);
-
 
205
    unsigned int y = ROW2Y(row);
-
 
206
    unsigned int yd;
-
 
207
   
-
 
208
    if (y >= ytrim)
-
 
209
        logo_hide(silent);
-
 
210
   
-
 
211
    backbuf[BB_POS(col, row)] = glyph;
-
 
212
   
218
    if (dbbuffer) {
213
    if (!silent) {
219
        int dline = (y + dboffset) % yres;
214
        for (yd = 0; yd < FONT_SCANLINES; yd++)
-
 
215
            memcpy(&fb_addr[FB_POS(x, y + yd + ylogo)],
220
        return COLOR((*scr2rgb)(&dbbuffer[POINTPOS(x, dline)]));
216
                &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
221
    }
217
    }
222
    return COLOR((*scr2rgb)(&fbaddress[POINTPOS(x, y)]));
-
 
223
}
218
}
224
 
219
 
225
 
220
 
226
/** Fill screen with background color */
221
/** Scroll screen down by one row
-
 
222
 *
-
 
223
 *
-
 
224
 */
227
static void clear_screen(void)
225
static void screen_scroll(bool silent)
228
{
226
{
229
    unsigned int y;
-
 
230
 
-
 
231
    for (y = 0; y < yres; y++) {
227
    if (ylogo > 0) {
232
        memcpy(&fbaddress[scanline * y], blankline, xres * pixelbytes);
-
 
233
        if (dbbuffer)
228
        logo_hide(silent);
234
            memcpy(&dbbuffer[scanline * y], blankline,
-
 
235
                xres * pixelbytes);
229
        return;
236
    }
230
    }
237
}
231
   
238
 
-
 
239
 
-
 
240
/** Scroll screen one row up */
-
 
241
static void scroll_screen(void)
-
 
242
{
-
 
243
    if (dbbuffer) {
232
    if (!silent) {
244
        count_t first;
-
 
245
       
-
 
246
        /* Clear the last row */
-
 
247
        memcpy(&dbbuffer[dboffset * scanline], blankline, ROW_BYTES);
-
 
248
       
-
 
249
        dboffset = (dboffset + FONT_SCANLINES) % yres;
-
 
250
        first = yres - dboffset;
-
 
251
       
-
 
252
        /* Move all rows one row up */
-
 
253
        if (xres * pixelbytes == scanline) {
-
 
254
            memcpy(fbaddress, &dbbuffer[dboffset * scanline],
-
 
255
                first * scanline);
-
 
256
            memcpy(&fbaddress[first * scanline], dbbuffer,
-
 
257
                dboffset * scanline);
-
 
258
        } else {
-
 
259
            /*
-
 
260
             * When the scanline is bigger than number of bytes
-
 
261
             * in the X-resolution, chances are that the
-
 
262
             * frame buffer memory past the X-resolution is special
-
 
263
             * in some way. For example, the SUNW,ffb framebuffer
-
 
264
             * wraps this area around the beginning of the same
-
 
265
             * line. To avoid troubles, copy only memory as
-
 
266
             * specified by the resolution.
-
 
267
             */
-
 
268
            unsigned int i;
233
        unsigned int row;
269
 
-
 
270
            for (i = 0; i < first; i++)
-
 
271
                memcpy(&fbaddress[i * scanline],
-
 
272
                    &dbbuffer[(dboffset + i) * scanline],
-
 
273
                    xres * pixelbytes);
-
 
274
            for (i = 0; i < dboffset; i++)
-
 
275
                memcpy(&fbaddress[(first + i) * scanline],
-
 
276
                    &dbbuffer[i * scanline], xres * pixelbytes);
-
 
277
        }
-
 
278
    } else {
-
 
279
        uint8_t *lastline = &fbaddress[(rows - 1) * ROW_BYTES];
-
 
280
       
234
       
281
        if (xres * pixelbytes == scanline) {
235
        for (row = 0; row < rows; row++) {
282
            /* Move all rows one row up */
236
            unsigned int y = ROW2Y(row);
283
            memcpy((void *) fbaddress,
237
            unsigned int yd;
-
 
238
           
284
                (void *) &fbaddress[ROW_BYTES],
239
            for (yd = 0; yd < FONT_SCANLINES; yd++) {
285
                scanline * yres - ROW_BYTES);
240
                unsigned int x;
286
            /* Clear the last row */
241
                unsigned int col;
-
 
242
               
287
            memcpy((void *) lastline, (void *) blankline,
243
                for (col = 0, x = 0; col < cols; col++,
288
                ROW_BYTES);
244
                    x += FONT_WIDTH) {
289
        } else {
245
                    uint8_t glyph;
290
            /*
246
                   
291
             * See the comment in the dbbuffer case.
-
 
292
             */
-
 
293
            unsigned int i;
247
                    if (row < rows - 1) {
294
 
-
 
295
            /* Move all rows one row up */
248
                        if (backbuf[BB_POS(col, row)] ==
296
            for (i = 0; i < yres - FONT_SCANLINES; i++)
249
                            backbuf[BB_POS(col, row + 1)])
297
                memcpy(&fbaddress[i * scanline],
250
                            continue;
-
 
251
                       
298
                    &fbaddress[(i + FONT_SCANLINES) * scanline],
252
                        glyph = backbuf[BB_POS(col, row + 1)];
299
                    xres * pixelbytes);
253
                    } else
300
            /* Clear the last row */
254
                        glyph = 0;
301
            for (i = 0; i < FONT_SCANLINES; i++)
255
                   
302
                memcpy(&lastline[i * scanline],
256
                    memcpy(&fb_addr[FB_POS(x, y + yd)],
303
                    &blankline[i * scanline],
257
                        &glyphs[GLYPH_POS(glyph, yd)],
304
                    xres * pixelbytes);
258
                        glyphscanline);
-
 
259
                }
-
 
260
            }
305
        }
261
        }
306
    }
262
    }
-
 
263
   
-
 
264
    memmove(backbuf, backbuf + cols, cols * (rows - 1));
-
 
265
    memsetb(&backbuf[BB_POS(0, rows - 1)], cols, 0);
307
}
266
}
308
 
267
 
309
 
268
 
310
static void invert_pixel(unsigned int x, unsigned int y)
-
 
311
{
-
 
312
    putpixel(x, y, ~getpixel(x, y));
-
 
313
}
-
 
314
 
-
 
315
 
-
 
316
/** Draw one line of glyph at a given position */
-
 
317
static void draw_glyph_line(unsigned int glline, unsigned int x, unsigned int y)
-
 
318
{
-
 
319
    unsigned int i;
-
 
320
 
-
 
321
    for (i = 0; i < 8; i++)
-
 
322
        if (glline & (1 << (7 - i))) {
-
 
323
            putpixel(x + i, y, FGCOLOR);
-
 
324
        } else
-
 
325
            putpixel(x + i, y, BGCOLOR);
-
 
326
}
-
 
327
 
-
 
328
/***************************************************************/
-
 
329
/* Character-console functions */
-
 
330
 
-
 
331
/** Draw character at given position */
-
 
332
static void draw_glyph(uint8_t glyph, unsigned int col, unsigned int row)
-
 
333
{
-
 
334
    unsigned int y;
-
 
335
 
-
 
336
    for (y = 0; y < FONT_SCANLINES; y++)
-
 
337
        draw_glyph_line(fb_font[glyph * FONT_SCANLINES + y],
-
 
338
            col * COL_WIDTH, row * FONT_SCANLINES + y);
-
 
339
}
-
 
340
 
-
 
341
/** Invert character at given position */
-
 
342
static void invert_char(unsigned int col, unsigned int row)
269
static void cursor_put(bool silent)
343
{
270
{
344
    unsigned int x;
-
 
345
    unsigned int y;
-
 
346
 
-
 
347
    for (x = 0; x < COL_WIDTH; x++)
-
 
348
        for (y = 0; y < FONT_SCANLINES; y++)
271
    glyph_draw(CURSOR, position % cols, position / cols, silent);
349
            invert_pixel(col * COL_WIDTH + x,
-
 
350
                row * FONT_SCANLINES + y);
-
 
351
}
272
}
352
 
273
 
353
/** Draw character at default position */
-
 
354
static void draw_char(char chr)
-
 
355
{
-
 
356
    draw_glyph(chr, position % columns, position / columns);
-
 
357
}
-
 
358
 
274
 
359
static void draw_logo(unsigned int startx, unsigned int starty)
275
static void cursor_remove(bool silent)
360
{
276
{
361
    unsigned int x;
-
 
362
    unsigned int y;
-
 
363
    unsigned int byte;
-
 
364
    unsigned int rowbytes;
-
 
365
 
-
 
366
    rowbytes = (helenos_width - 1) / 8 + 1;
-
 
367
 
-
 
368
    for (y = 0; y < helenos_height; y++)
-
 
369
        for (x = 0; x < helenos_width; x++) {
-
 
370
            byte = helenos_bits[rowbytes * y + x / 8];
277
    glyph_draw(0, position % cols, position / cols, silent);
371
            byte >>= x % 8;
-
 
372
            if (byte & 1)
-
 
373
                putpixel(startx + x, starty + y,
-
 
374
                    COLOR(LOGOCOLOR));
-
 
375
        }
-
 
376
}
278
}
377
 
279
 
378
/***************************************************************/
-
 
379
/* Stdout specific functions */
-
 
380
 
-
 
381
static void invert_cursor(void)
-
 
382
{
-
 
383
    invert_char(position % columns, position / columns);
-
 
384
}
-
 
385
 
280
 
386
/** Print character to screen
281
/** Print character to screen
387
 *
282
 *
388
 *  Emulate basic terminal commands
283
 * Emulate basic terminal commands.
-
 
284
 *
389
 */
285
 */
390
static void fb_putchar(chardev_t *dev, char ch)
286
static void fb_putchar(chardev_t *dev, char ch, bool silent)
391
{
287
{
392
    spinlock_lock(&fb_lock);
288
    spinlock_lock(&fb_lock);
393
   
289
   
394
    switch (ch) {
290
    switch (ch) {
395
    case '\n':
291
    case '\n':
396
        invert_cursor();
292
        cursor_remove(silent);
397
        position += columns;
293
        position += cols;
398
        position -= position % columns;
294
        position -= position % cols;
399
        break;
295
        break;
400
    case '\r':
296
    case '\r':
401
        invert_cursor();
297
        cursor_remove(silent);
402
        position -= position % columns;
298
        position -= position % cols;
403
        break;
299
        break;
404
    case '\b':
300
    case '\b':
405
        invert_cursor();
301
        cursor_remove(silent);
406
        if (position % columns)
302
        if (position % cols)
407
            position--;
303
            position--;
408
        break;
304
        break;
409
    case '\t':
305
    case '\t':
410
        invert_cursor();
306
        cursor_remove(silent);
411
        do {
307
        do {
-
 
308
            glyph_draw((uint8_t) ' ', position % cols,
412
            draw_char(' ');
309
                position / cols, silent);
413
            position++;
310
            position++;
414
        } while ((position % 8) && position < columns * rows);
311
        } while ((position % 8) && (position < cols * rows));
415
        break;
312
        break;
416
    default:
313
    default:
-
 
314
        glyph_draw((uint8_t) ch, position % cols,
417
        draw_char(ch);
315
            position / cols, silent);
418
        position++;
316
        position++;
419
    }
317
    }
420
   
318
   
421
    if (position >= columns * rows) {
319
    if (position >= cols * rows) {
422
        position -= columns;
320
        position -= cols;
423
        scroll_screen();
321
        screen_scroll(silent);
424
    }
322
    }
425
   
323
   
426
    invert_cursor();
324
    cursor_put(silent);
427
   
325
   
428
    spinlock_unlock(&fb_lock);
326
    spinlock_unlock(&fb_lock);
429
}
327
}
430
 
328
 
431
static chardev_t framebuffer;
329
static chardev_t framebuffer;
432
static chardev_operations_t fb_ops = {
330
static chardev_operations_t fb_ops = {
433
    .write = fb_putchar,
331
    .write = fb_putchar,
434
};
332
};
435
 
333
 
436
 
334
 
-
 
335
/** Render glyphs
-
 
336
 *
-
 
337
 * Convert glyphs from device independent font
-
 
338
 * description to current visual representation.
-
 
339
 *
-
 
340
 */
-
 
341
static void glyphs_render(void)
-
 
342
{
-
 
343
    /* Prerender glyphs */
-
 
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
           
-
 
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
            }
-
 
359
        }
-
 
360
    }
-
 
361
   
-
 
362
    /* Prerender background scanline */
-
 
363
    unsigned int x;
-
 
364
   
-
 
365
    for (x = 0; x < xres; x++)
-
 
366
        rgb_conv(&bgscan[x * pixelbytes], BG_COLOR);
-
 
367
}
-
 
368
 
-
 
369
 
-
 
370
/** Refresh the screen
-
 
371
 *
-
 
372
 */
-
 
373
void fb_redraw(void)
-
 
374
{
-
 
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)],
-
 
383
                    (x < LOGO_WIDTH) ?
-
 
384
                    fb_logo[y * LOGO_WIDTH + x] :
-
 
385
                    LOGO_COLOR);
-
 
386
        }
-
 
387
    }
-
 
388
   
-
 
389
    unsigned int row;
-
 
390
   
-
 
391
    for (row = 0; row < rowtrim; row++) {
-
 
392
        unsigned int y = ylogo + ROW2Y(row);
-
 
393
        unsigned int yd;
-
 
394
       
-
 
395
        for (yd = 0; yd < FONT_SCANLINES; yd++) {
-
 
396
            unsigned int x;
-
 
397
            unsigned int col;
-
 
398
           
-
 
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
            }
-
 
406
        }
-
 
407
    }
-
 
408
   
-
 
409
    if (COL2X(cols) < xres) {
-
 
410
        unsigned int y;
-
 
411
        unsigned int size = (xres - COL2X(cols)) * pixelbytes;
-
 
412
       
-
 
413
        for (y = ylogo; y < yres; y++)
-
 
414
            memcpy(&fb_addr[FB_POS(COL2X(cols), y)], bgscan, size);
-
 
415
    }
-
 
416
   
-
 
417
    if (ROW2Y(rowtrim) + ylogo < yres) {
-
 
418
        unsigned int y;
-
 
419
       
-
 
420
        for (y = ROW2Y(rowtrim) + ylogo; y < yres; y++)
-
 
421
            memcpy(&fb_addr[FB_POS(0, y)], bgscan, bgscanbytes);
-
 
422
    }
-
 
423
}
-
 
424
 
-
 
425
 
437
/** Initialize framebuffer as a chardev output device
426
/** Initialize framebuffer as a chardev output device
438
 *
427
 *
439
 * @param addr   Physical address of the framebuffer
428
 * @param addr   Physical address of the framebuffer
440
 * @param x      Screen width in pixels
429
 * @param x      Screen width in pixels
441
 * @param y      Screen height in pixels
430
 * @param y      Screen height in pixels
442
 * @param scan   Bytes per one scanline
431
 * @param scan   Bytes per one scanline
443
 * @param visual Color model
432
 * @param visual Color model
444
 *
433
 *
445
 */
434
 */
446
void fb_init(uintptr_t addr, unsigned int x, unsigned int y, unsigned int scan,
-
 
447
    unsigned int visual)
435
void fb_init(fb_properties_t *props)
448
{
436
{
449
    switch (visual) {
437
    switch (props->visual) {
450
    case VISUAL_INDIRECT_8:
438
    case VISUAL_INDIRECT_8:
451
        rgb2scr = rgb_byte8;
439
        rgb_conv = rgb_323;
452
        scr2rgb = byte8_rgb;
-
 
453
        pixelbytes = 1;
440
        pixelbytes = 1;
454
        break;
441
        break;
455
    case VISUAL_RGB_5_5_5:
442
    case VISUAL_RGB_5_5_5:
456
        rgb2scr = rgb_byte555;
443
        rgb_conv = rgb_555;
457
        scr2rgb = byte555_rgb;
-
 
458
        pixelbytes = 2;
444
        pixelbytes = 2;
459
        break;
445
        break;
460
    case VISUAL_RGB_5_6_5:
446
    case VISUAL_RGB_5_6_5:
461
        rgb2scr = rgb_byte565;
447
        rgb_conv = rgb_565;
462
        scr2rgb = byte565_rgb;
-
 
463
        pixelbytes = 2;
448
        pixelbytes = 2;
464
        break;
449
        break;
465
    case VISUAL_RGB_8_8_8:
450
    case VISUAL_RGB_8_8_8:
466
        rgb2scr = rgb_byte888;
451
        rgb_conv = rgb_888;
-
 
452
        pixelbytes = 3;
-
 
453
        break;
-
 
454
    case VISUAL_BGR_8_8_8:
467
        scr2rgb = byte888_rgb;
455
        rgb_conv = bgr_888;
468
        pixelbytes = 3;
456
        pixelbytes = 3;
469
        break;
457
        break;
470
    case VISUAL_RGB_8_8_8_0:
458
    case VISUAL_RGB_8_8_8_0:
471
        rgb2scr = rgb_byte888;
459
        rgb_conv = rgb_888;
472
        scr2rgb = byte888_rgb;
-
 
473
        pixelbytes = 4;
460
        pixelbytes = 4;
474
        break;
461
        break;
475
    case VISUAL_RGB_0_8_8_8:
462
    case VISUAL_RGB_0_8_8_8:
476
        rgb2scr = rgb_byte0888;
463
        rgb_conv = rgb_0888;
477
        scr2rgb = byte0888_rgb;
-
 
478
        pixelbytes = 4;
464
        pixelbytes = 4;
479
        break;
465
        break;
480
    case VISUAL_BGR_0_8_8_8:
466
    case VISUAL_BGR_0_8_8_8:
481
        rgb2scr = bgr_byte0888;
467
        rgb_conv = bgr_0888;
482
        scr2rgb = byte0888_bgr;
-
 
483
        pixelbytes = 4;
468
        pixelbytes = 4;
484
        break;
469
        break;
485
    default:
470
    default:
486
        panic("Unsupported visual.\n");
471
        panic("Unsupported visual.");
487
    }
472
    }
488
   
473
   
-
 
474
    xres = props->x;
-
 
475
    yres = props->y;
489
    unsigned int fbsize = scan * y;
476
    scanline = props->scan;
490
   
477
   
491
    /* Map the framebuffer */
478
    cols = X2COL(xres);
492
    fbaddress = (uint8_t *) hw_map((uintptr_t) addr, fbsize);
479
    rows = Y2ROW(yres);
493
   
480
   
-
 
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 {
494
    xres = x;
488
        ylogo = 0;
495
    yres = y;
489
        ytrim = yres;
496
    scanline = scan;
490
        rowtrim = rows;
-
 
491
    }
-
 
492
   
-
 
493
    glyphscanline = FONT_WIDTH * pixelbytes;
-
 
494
    glyphbytes = ROW2Y(glyphscanline);
-
 
495
    bgscanbytes = xres * pixelbytes;
-
 
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)
-
 
503
        panic("Unable to allocate backbuffer.");
-
 
504
   
-
 
505
    glyphs = (uint8_t *) malloc(glyphsize, 0);
-
 
506
    if (!glyphs)
-
 
507
        panic("Unable to allocate glyphs.");
-
 
508
   
-
 
509
    bgscan = malloc(bgscanbytes, 0);
-
 
510
    if (!bgscan)
-
 
511
        panic("Unable to allocate background pixel.");
-
 
512
   
-
 
513
    memsetb(backbuf, bbsize, 0);
-
 
514
   
-
 
515
    glyphs_render();
-
 
516
   
-
 
517
    fb_addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize);
497
   
518
   
498
    rows = y / FONT_SCANLINES;
-
 
499
    columns = x / COL_WIDTH;
-
 
500
 
-
 
501
    fb_parea.pbase = (uintptr_t) addr;
-
 
502
    fb_parea.vbase = (uintptr_t) fbaddress;
-
 
503
    fb_parea.frames = SIZE2FRAMES(fbsize);
-
 
504
    fb_parea.cacheable = false;
-
 
505
    ddi_parea_register(&fb_parea);
-
 
506
 
-
 
507
    sysinfo_set_item_val("fb", NULL, true);
519
    sysinfo_set_item_val("fb", NULL, true);
508
    sysinfo_set_item_val("fb.kind", NULL, 1);
520
    sysinfo_set_item_val("fb.kind", NULL, 1);
509
    sysinfo_set_item_val("fb.width", NULL, xres);
521
    sysinfo_set_item_val("fb.width", NULL, xres);
510
    sysinfo_set_item_val("fb.height", NULL, yres);
522
    sysinfo_set_item_val("fb.height", NULL, yres);
511
    sysinfo_set_item_val("fb.scanline", NULL, scan);
523
    sysinfo_set_item_val("fb.scanline", NULL, scanline);
512
    sysinfo_set_item_val("fb.visual", NULL, visual);
524
    sysinfo_set_item_val("fb.visual", NULL, props->visual);
513
    sysinfo_set_item_val("fb.address.physical", NULL, addr);
525
    sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
514
    sysinfo_set_item_val("fb.invert-colors", NULL, invert_colors);
-
 
515
 
526
   
516
    /* Allocate double buffer */
-
 
517
    unsigned int order = fnzb(SIZE2FRAMES(fbsize) - 1) + 1;
-
 
518
    dbbuffer = (uint8_t *) frame_alloc(order, FRAME_ATOMIC | FRAME_KA);
-
 
519
    if (!dbbuffer)
-
 
520
        printf("Failed to allocate scroll buffer.\n");
-
 
521
    dboffset = 0;
527
    fb_redraw();
522
 
-
 
523
    /* Initialized blank line */
-
 
524
    blankline = (uint8_t *) malloc(ROW_BYTES, FRAME_ATOMIC);
-
 
525
    if (!blankline)
-
 
526
        panic("Failed to allocate blank line for framebuffer.");
-
 
527
    for (y = 0; y < FONT_SCANLINES; y++)
-
 
528
        for (x = 0; x < xres; x++)
-
 
529
            (*rgb2scr)(&blankline[POINTPOS(x, y)], COLOR(BGCOLOR));
-
 
530
   
528
   
531
    clear_screen();
-
 
532
 
-
 
533
    /* Update size of screen to match text area */
-
 
534
    yres = rows * FONT_SCANLINES;
-
 
535
 
-
 
536
    draw_logo(xres - helenos_width, 0);
-
 
537
    invert_cursor();
-
 
538
 
-
 
539
    chardev_initialize("fb", &framebuffer, &fb_ops);
529
    chardev_initialize("fb", &framebuffer, &fb_ops);
540
    stdout = &framebuffer;
530
    stdout = &framebuffer;
541
}
531
}
542
 
532
 
543
/** @}
533
/** @}