Subversion Repositories HelenOS

Rev

Rev 4669 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4669 Rev 4676
1
/*
1
/*
2
 * Copyright (c) 2008 Martin Decky
2
 * Copyright (c) 2008 Martin Decky
3
 * Copyright (c) 2006 Ondrej Palkovsky
3
 * Copyright (c) 2006 Ondrej Palkovsky
4
 * All rights reserved.
4
 * All rights reserved.
5
 *
5
 *
6
 * Redistribution and use in source and binary forms, with or without
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
7
 * modification, are permitted provided that the following conditions
8
 * are met:
8
 * are met:
9
 *
9
 *
10
 * - Redistributions of source code must retain the above copyright
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
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
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.
16
 *   derived from this software without specific prior written permission.
17
 *
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
28
 */
29
 
29
 
30
/** @addtogroup genarch
30
/** @addtogroup genarch
31
 * @{
31
 * @{
32
 */
32
 */
33
/** @file
33
/** @file
34
 */
34
 */
35
 
35
 
36
#include <genarch/fb/font-8x16.h>
36
#include <genarch/fb/font-8x16.h>
37
#include <genarch/fb/logo-196x66.h>
37
#include <genarch/fb/logo-196x66.h>
38
#include <genarch/fb/visuals.h>
38
#include <genarch/fb/visuals.h>
39
#include <genarch/fb/fb.h>
39
#include <genarch/fb/fb.h>
40
#include <console/chardev.h>
40
#include <console/chardev.h>
41
#include <console/console.h>
41
#include <console/console.h>
42
#include <sysinfo/sysinfo.h>
42
#include <sysinfo/sysinfo.h>
43
#include <mm/slab.h>
43
#include <mm/slab.h>
44
#include <align.h>
44
#include <align.h>
45
#include <panic.h>
45
#include <panic.h>
46
#include <memstr.h>
46
#include <memstr.h>
47
#include <config.h>
47
#include <config.h>
48
#include <bitops.h>
48
#include <bitops.h>
49
#include <print.h>
49
#include <print.h>
50
#include <string.h>
50
#include <string.h>
51
#include <ddi/ddi.h>
51
#include <ddi/ddi.h>
52
#include <arch/types.h>
52
#include <arch/types.h>
-
 
53
#include <byteorder.h>
53
 
54
 
54
SPINLOCK_INITIALIZE(fb_lock);
55
SPINLOCK_INITIALIZE(fb_lock);
55
 
56
 
56
static uint8_t *fb_addr;
57
static uint8_t *fb_addr;
57
static uint16_t *backbuf;
58
static uint16_t *backbuf;
58
static uint8_t *glyphs;
59
static uint8_t *glyphs;
59
static uint8_t *bgscan;
60
static uint8_t *bgscan;
60
 
61
 
61
static unsigned int xres;
62
static unsigned int xres;
62
static unsigned int yres;
63
static unsigned int yres;
63
 
64
 
64
static unsigned int ylogo;
65
static unsigned int ylogo;
65
static unsigned int ytrim;
66
static unsigned int ytrim;
66
static unsigned int rowtrim;
67
static unsigned int rowtrim;
67
 
68
 
68
static unsigned int scanline;
69
static unsigned int scanline;
69
static unsigned int glyphscanline;
70
static unsigned int glyphscanline;
70
 
71
 
71
static unsigned int pixelbytes;
72
static unsigned int pixelbytes;
72
static unsigned int glyphbytes;
73
static unsigned int glyphbytes;
73
static unsigned int bgscanbytes;
74
static unsigned int bgscanbytes;
74
 
75
 
75
static unsigned int cols;
76
static unsigned int cols;
76
static unsigned int rows;
77
static unsigned int rows;
77
static unsigned int position = 0;
78
static unsigned int position = 0;
78
 
79
 
79
#define BG_COLOR     0x000080
80
#define BG_COLOR     0x000080
80
#define FG_COLOR     0xffff00
81
#define FG_COLOR     0xffff00
81
#define INV_COLOR    0xaaaaaa
82
#define INV_COLOR    0xaaaaaa
82
 
83
 
83
#define RED(x, bits)         ((x >> (8 + 8 + 8 - bits)) & ((1 << bits) - 1))
84
#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 GREEN(x, bits)       (((x) >> (8 + 8 - (bits))) & ((1 << (bits)) - 1))
85
#define BLUE(x, bits)        ((x >> (8 - bits)) & ((1 << bits) - 1))
86
#define BLUE(x, bits)        (((x) >> (8 - (bits))) & ((1 << (bits)) - 1))
86
 
87
 
87
#define COL2X(col)           ((col) * FONT_WIDTH)
88
#define COL2X(col)           ((col) * FONT_WIDTH)
88
#define ROW2Y(row)           ((row) * FONT_SCANLINES)
89
#define ROW2Y(row)           ((row) * FONT_SCANLINES)
89
 
90
 
90
#define X2COL(x)             ((x) / FONT_WIDTH)
91
#define X2COL(x)             ((x) / FONT_WIDTH)
91
#define Y2ROW(y)             ((y) / FONT_SCANLINES)
92
#define Y2ROW(y)             ((y) / FONT_SCANLINES)
92
 
93
 
93
#define FB_POS(x, y)         ((y) * scanline + (x) * pixelbytes)
94
#define FB_POS(x, y)         ((y) * scanline + (x) * pixelbytes)
94
#define BB_POS(col, row)     ((row) * cols + (col))
95
#define BB_POS(col, row)     ((row) * cols + (col))
95
#define GLYPH_POS(glyph, y)  ((glyph) * glyphbytes + (y) * glyphscanline)
96
#define GLYPH_POS(glyph, y)  ((glyph) * glyphbytes + (y) * glyphscanline)
96
 
97
 
97
 
98
 
98
static void (*rgb_conv)(void *, uint32_t);
99
static void (*rgb_conv)(void *, uint32_t);
99
 
100
 
100
 
101
/*
101
/** ARGB 8:8:8:8 conversion
102
 * RGB conversion functions.
102
 *
103
 *
-
 
104
 * These functions write an RGB value to some memory in some predefined format.
-
 
105
 * The naming convention corresponds to the format created by these functions.
-
 
106
 * The functions use the so called network order (i.e. big endian) with respect
-
 
107
 * to their names.
103
 */
108
 */
-
 
109
 
104
static void rgb_0888(void *dst, uint32_t rgb)
110
static void rgb_0888(void *dst, uint32_t rgb)
105
{
111
{
106
    *((uint32_t *) dst) = rgb & 0xffffff;
112
    *((uint32_t *) dst) = host2uint32_t_be((0 << 24) |
-
 
113
        (RED(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | (BLUE(rgb, 8)));
107
}
114
}
108
 
115
 
109
 
-
 
110
/** ABGR 8:8:8:8 conversion
-
 
111
 *
-
 
112
 */
-
 
113
static void bgr_0888(void *dst, uint32_t rgb)
116
static void bgr_0888(void *dst, uint32_t rgb)
114
{
117
{
115
    *((uint32_t *) dst)
118
    *((uint32_t *) dst) = host2uint32_t_be((0 << 24) |
116
        = (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | RED(rgb, 8);
119
        (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | (RED(rgb, 8)));
117
}
120
}
118
 
121
 
119
static void rgb_8880(void *dst, uint32_t rgb)
122
static void rgb_8880(void *dst, uint32_t rgb)
120
{
123
{
121
    *((uint32_t *) dst)
124
    *((uint32_t *) dst) = host2uint32_t_be((RED(rgb, 8) << 24) |
122
       = (RED(rgb, 8) << 24) | (GREEN(rgb, 8) << 16) | (BLUE(rgb, 8) << 8);
125
        (GREEN(rgb, 8) << 16) | (BLUE(rgb, 8) << 8) | 0);
123
 
-
 
124
}
126
}
125
 
127
 
-
 
128
static void bgr_8880(void *dst, uint32_t rgb)
-
 
129
{
-
 
130
    *((uint32_t *) dst) = host2uint32_t_be((BLUE(rgb, 8) << 24) |
-
 
131
        (GREEN(rgb, 8) << 16) | (RED(rgb, 8) << 8) | 0);
-
 
132
}
126
 
133
 
127
/** RGB 8:8:8 conversion
-
 
128
 *
-
 
129
 */
-
 
130
static void rgb_888(void *dst, uint32_t rgb)
134
static void rgb_888(void *dst, uint32_t rgb)
131
{
135
{
132
    ((uint8_t *) dst)[0] = BLUE(rgb, 8);
136
    ((uint8_t *) dst)[0] = RED(rgb, 8);
133
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
137
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
134
    ((uint8_t *) dst)[2] = RED(rgb, 8);
138
    ((uint8_t *) dst)[2] = BLUE(rgb, 8);
135
}
139
}
136
 
140
 
137
 
-
 
138
/** BGR 8:8:8 conversion
-
 
139
 *
-
 
140
 */
-
 
141
static void bgr_888(void *dst, uint32_t rgb)
141
static void bgr_888(void *dst, uint32_t rgb)
142
{
142
{
143
    ((uint8_t *) dst)[0] = RED(rgb, 8);
143
    ((uint8_t *) dst)[0] = BLUE(rgb, 8);
144
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
144
    ((uint8_t *) dst)[1] = GREEN(rgb, 8);
145
    ((uint8_t *) dst)[2] = BLUE(rgb, 8);
145
    ((uint8_t *) dst)[2] = RED(rgb, 8);
146
}
146
}
147
 
147
 
148
 
-
 
149
/** RGB 5:5:5 conversion
-
 
150
 *
-
 
151
 */
-
 
152
static void rgb_555(void *dst, uint32_t rgb)
148
static void bgr_555(void *dst, uint32_t rgb)
153
{
149
{
154
    *((uint16_t *) dst)
150
    uint8_t hi = (BLUE(rgb, 5) | (GREEN(rgb, 5) << 5)) & 0xff;
155
        = (RED(rgb, 5) << 10) | (GREEN(rgb, 5) << 5) | BLUE(rgb, 5);
151
    uint8_t lo = (GREEN(rgb, 5) >> 3) | (RED(rgb, 5) << 2);
-
 
152
    *((uint16_t *) dst) = host2uint16_t_be((hi << 8) | lo);
156
}
153
}
157
 
154
 
158
 
-
 
159
/** RGB 5:6:5 conversion
-
 
160
 *
-
 
161
 */
-
 
162
static void rgb_565(void *dst, uint32_t rgb)
155
static void bgr_565(void *dst, uint32_t rgb)
163
{
156
{
164
    *((uint16_t *) dst)
157
    uint8_t hi = (BLUE(rgb, 5) | (GREEN(rgb, 6) << 5)) & 0xff;
165
        = (RED(rgb, 5) << 11) | (GREEN(rgb, 6) << 5) | BLUE(rgb, 5);
158
    uint8_t lo = (GREEN(rgb, 6) >> 3) | (RED(rgb, 5) << 3);
-
 
159
    *((uint16_t *) dst) = host2uint16_t_be((hi << 8) | lo);
166
}
160
}
167
 
161
 
168
 
162
 
169
/** RGB 3:2:3
163
/** BGR 3:2:3
170
 *
164
 *
171
 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
165
 * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
172
 * will most likely use a color palette. The color appearance
166
 * will most likely use a color palette. The color appearance
173
 * will be pretty random and depend on the default installed
167
 * will be pretty random and depend on the default installed
174
 * palette. This could be fixed by supporting custom palette
168
 * palette. This could be fixed by supporting custom palette
175
 * and setting it to simulate the 8-bit truecolor.
169
 * and setting it to simulate the 8-bit truecolor.
176
 *
170
 *
177
 * Currently we set the palette on the ia32, amd64 and sparc64 port.
171
 * Currently we set the palette on the ia32, amd64 and sparc64 port.
178
 *
172
 *
179
 * Note that the byte is being inverted by this function. The reason is
173
 * Note that the byte is being inverted by this function. The reason is
180
 * that we would like to use a color palette where the white color code
174
 * that we would like to use a color palette where the white color code
181
 * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
175
 * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
182
 * use these codes for black and white and prevent to set codes
176
 * use these codes for black and white and prevent to set codes
183
 * 0 and 255 to other colors.
177
 * 0 and 255 to other colors.
184
 *
178
 *
185
 */
179
 */
186
static void rgb_323(void *dst, uint32_t rgb)
180
static void bgr_323(void *dst, uint32_t rgb)
187
{
181
{
188
    *((uint8_t *) dst)
182
    *((uint8_t *) dst)
189
        = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
183
        = ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
190
}
184
}
191
 
185
 
192
 
186
 
193
/** Hide logo and refresh screen
187
/** Hide logo and refresh screen
194
 *
188
 *
195
 */
189
 */
196
static void logo_hide(bool silent)
190
static void logo_hide(bool silent)
197
{
191
{
198
    ylogo = 0;
192
    ylogo = 0;
199
    ytrim = yres;
193
    ytrim = yres;
200
    rowtrim = rows;
194
    rowtrim = rows;
201
    if (!silent)
195
    if (!silent)
202
        fb_redraw();
196
        fb_redraw();
203
}
197
}
204
 
198
 
205
 
199
 
206
/** Draw character at given position
200
/** Draw character at given position
207
 *
201
 *
208
 */
202
 */
209
static void glyph_draw(uint16_t glyph, unsigned int col, unsigned int row, bool silent, bool overlay)
203
static void glyph_draw(uint16_t glyph, unsigned int col, unsigned int row, bool silent, bool overlay)
210
{
204
{
211
    unsigned int x = COL2X(col);
205
    unsigned int x = COL2X(col);
212
    unsigned int y = ROW2Y(row);
206
    unsigned int y = ROW2Y(row);
213
    unsigned int yd;
207
    unsigned int yd;
214
   
208
   
215
    if (y >= ytrim)
209
    if (y >= ytrim)
216
        logo_hide(silent);
210
        logo_hide(silent);
217
   
211
   
218
    if (!overlay)
212
    if (!overlay)
219
        backbuf[BB_POS(col, row)] = glyph;
213
        backbuf[BB_POS(col, row)] = glyph;
220
   
214
   
221
    if (!silent) {
215
    if (!silent) {
222
        for (yd = 0; yd < FONT_SCANLINES; yd++)
216
        for (yd = 0; yd < FONT_SCANLINES; yd++)
223
            memcpy(&fb_addr[FB_POS(x, y + yd + ylogo)],
217
            memcpy(&fb_addr[FB_POS(x, y + yd + ylogo)],
224
                &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
218
                &glyphs[GLYPH_POS(glyph, yd)], glyphscanline);
225
    }
219
    }
226
}
220
}
227
 
221
 
228
 
222
 
229
/** Scroll screen down by one row
223
/** Scroll screen down by one row
230
 *
224
 *
231
 *
225
 *
232
 */
226
 */
233
static void screen_scroll(bool silent)
227
static void screen_scroll(bool silent)
234
{
228
{
235
    if (ylogo > 0) {
229
    if (ylogo > 0) {
236
        logo_hide(silent);
230
        logo_hide(silent);
237
        return;
231
        return;
238
    }
232
    }
239
   
233
   
240
    if (!silent) {
234
    if (!silent) {
241
        unsigned int row;
235
        unsigned int row;
242
       
236
       
243
        for (row = 0; row < rows; row++) {
237
        for (row = 0; row < rows; row++) {
244
            unsigned int y = ROW2Y(row);
238
            unsigned int y = ROW2Y(row);
245
            unsigned int yd;
239
            unsigned int yd;
246
           
240
           
247
            for (yd = 0; yd < FONT_SCANLINES; yd++) {
241
            for (yd = 0; yd < FONT_SCANLINES; yd++) {
248
                unsigned int x;
242
                unsigned int x;
249
                unsigned int col;
243
                unsigned int col;
250
               
244
               
251
                for (col = 0, x = 0; col < cols; col++,
245
                for (col = 0, x = 0; col < cols; col++,
252
                    x += FONT_WIDTH) {
246
                    x += FONT_WIDTH) {
253
                    uint16_t glyph;
247
                    uint16_t glyph;
254
                   
248
                   
255
                    if (row < rows - 1) {
249
                    if (row < rows - 1) {
256
                        if (backbuf[BB_POS(col, row)] ==
250
                        if (backbuf[BB_POS(col, row)] ==
257
                            backbuf[BB_POS(col, row + 1)])
251
                            backbuf[BB_POS(col, row + 1)])
258
                            continue;
252
                            continue;
259
                       
253
                       
260
                        glyph = backbuf[BB_POS(col, row + 1)];
254
                        glyph = backbuf[BB_POS(col, row + 1)];
261
                    } else
255
                    } else
262
                        glyph = 0;
256
                        glyph = 0;
263
                   
257
                   
264
                    memcpy(&fb_addr[FB_POS(x, y + yd)],
258
                    memcpy(&fb_addr[FB_POS(x, y + yd)],
265
                        &glyphs[GLYPH_POS(glyph, yd)],
259
                        &glyphs[GLYPH_POS(glyph, yd)],
266
                        glyphscanline);
260
                        glyphscanline);
267
                }
261
                }
268
            }
262
            }
269
        }
263
        }
270
    }
264
    }
271
   
265
   
272
    memmove(backbuf, &backbuf[BB_POS(0, 1)], cols * (rows - 1) * sizeof(uint16_t));
266
    memmove(backbuf, &backbuf[BB_POS(0, 1)], cols * (rows - 1) * sizeof(uint16_t));
273
    memsetw(&backbuf[BB_POS(0, rows - 1)], cols, 0);
267
    memsetw(&backbuf[BB_POS(0, rows - 1)], cols, 0);
274
}
268
}
275
 
269
 
276
 
270
 
277
static void cursor_put(bool silent)
271
static void cursor_put(bool silent)
278
{
272
{
279
    unsigned int col = position % cols;
273
    unsigned int col = position % cols;
280
    unsigned int row = position / cols;
274
    unsigned int row = position / cols;
281
   
275
   
282
    glyph_draw(fb_font_glyph(U_CURSOR), col, row, silent, true);
276
    glyph_draw(fb_font_glyph(U_CURSOR), col, row, silent, true);
283
}
277
}
284
 
278
 
285
 
279
 
286
static void cursor_remove(bool silent)
280
static void cursor_remove(bool silent)
287
{
281
{
288
    unsigned int col = position % cols;
282
    unsigned int col = position % cols;
289
    unsigned int row = position / cols;
283
    unsigned int row = position / cols;
290
   
284
   
291
    glyph_draw(backbuf[BB_POS(col, row)], col, row, silent, true);
285
    glyph_draw(backbuf[BB_POS(col, row)], col, row, silent, true);
292
}
286
}
293
 
287
 
294
 
288
 
295
/** Print character to screen
289
/** Print character to screen
296
 *
290
 *
297
 * Emulate basic terminal commands.
291
 * Emulate basic terminal commands.
298
 *
292
 *
299
 */
293
 */
300
static void fb_putchar(outdev_t *dev, wchar_t ch, bool silent)
294
static void fb_putchar(outdev_t *dev, wchar_t ch, bool silent)
301
{
295
{
302
    spinlock_lock(&fb_lock);
296
    spinlock_lock(&fb_lock);
303
   
297
   
304
    switch (ch) {
298
    switch (ch) {
305
    case '\n':
299
    case '\n':
306
        cursor_remove(silent);
300
        cursor_remove(silent);
307
        position += cols;
301
        position += cols;
308
        position -= position % cols;
302
        position -= position % cols;
309
        break;
303
        break;
310
    case '\r':
304
    case '\r':
311
        cursor_remove(silent);
305
        cursor_remove(silent);
312
        position -= position % cols;
306
        position -= position % cols;
313
        break;
307
        break;
314
    case '\b':
308
    case '\b':
315
        cursor_remove(silent);
309
        cursor_remove(silent);
316
        if (position % cols)
310
        if (position % cols)
317
            position--;
311
            position--;
318
        break;
312
        break;
319
    case '\t':
313
    case '\t':
320
        cursor_remove(silent);
314
        cursor_remove(silent);
321
        do {
315
        do {
322
            glyph_draw(fb_font_glyph(' '), position % cols,
316
            glyph_draw(fb_font_glyph(' '), position % cols,
323
                position / cols, silent, false);
317
                position / cols, silent, false);
324
            position++;
318
            position++;
325
        } while ((position % 8) && (position < cols * rows));
319
        } while ((position % 8) && (position < cols * rows));
326
        break;
320
        break;
327
    default:
321
    default:
328
        glyph_draw(fb_font_glyph(ch), position % cols,
322
        glyph_draw(fb_font_glyph(ch), position % cols,
329
            position / cols, silent, false);
323
            position / cols, silent, false);
330
        position++;
324
        position++;
331
    }
325
    }
332
   
326
   
333
    if (position >= cols * rows) {
327
    if (position >= cols * rows) {
334
        position -= cols;
328
        position -= cols;
335
        screen_scroll(silent);
329
        screen_scroll(silent);
336
    }
330
    }
337
   
331
   
338
    cursor_put(silent);
332
    cursor_put(silent);
339
   
333
   
340
    spinlock_unlock(&fb_lock);
334
    spinlock_unlock(&fb_lock);
341
}
335
}
342
 
336
 
343
static outdev_t fb_console;
337
static outdev_t fb_console;
344
static outdev_operations_t fb_ops = {
338
static outdev_operations_t fb_ops = {
345
    .write = fb_putchar
339
    .write = fb_putchar
346
};
340
};
347
 
341
 
348
 
342
 
349
/** Render glyphs
343
/** Render glyphs
350
 *
344
 *
351
 * Convert glyphs from device independent font
345
 * Convert glyphs from device independent font
352
 * description to current visual representation.
346
 * description to current visual representation.
353
 *
347
 *
354
 */
348
 */
355
static void glyphs_render(void)
349
static void glyphs_render(void)
356
{
350
{
357
    /* Prerender glyphs */
351
    /* Prerender glyphs */
358
    uint16_t glyph;
352
    uint16_t glyph;
359
   
353
   
360
    for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
354
    for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
361
        uint32_t fg_color;
355
        uint32_t fg_color;
362
       
356
       
363
        if (glyph == FONT_GLYPHS - 1)
357
        if (glyph == FONT_GLYPHS - 1)
364
            fg_color = INV_COLOR;
358
            fg_color = INV_COLOR;
365
        else
359
        else
366
            fg_color = FG_COLOR;
360
            fg_color = FG_COLOR;
367
       
361
       
368
        unsigned int y;
362
        unsigned int y;
369
       
363
       
370
        for (y = 0; y < FONT_SCANLINES; y++) {
364
        for (y = 0; y < FONT_SCANLINES; y++) {
371
            unsigned int x;
365
            unsigned int x;
372
           
366
           
373
            for (x = 0; x < FONT_WIDTH; x++) {
367
            for (x = 0; x < FONT_WIDTH; x++) {
374
                void *dst = &glyphs[GLYPH_POS(glyph, y) +
368
                void *dst = &glyphs[GLYPH_POS(glyph, y) +
375
                    x * pixelbytes];
369
                    x * pixelbytes];
376
                uint32_t rgb = (fb_font[glyph][y] &
370
                uint32_t rgb = (fb_font[glyph][y] &
377
                    (1 << (7 - x))) ? fg_color : BG_COLOR;
371
                    (1 << (7 - x))) ? fg_color : BG_COLOR;
378
                rgb_conv(dst, rgb);
372
                rgb_conv(dst, rgb);
379
            }
373
            }
380
        }
374
        }
381
    }
375
    }
382
   
376
   
383
    /* Prerender background scanline */
377
    /* Prerender background scanline */
384
    unsigned int x;
378
    unsigned int x;
385
   
379
   
386
    for (x = 0; x < xres; x++)
380
    for (x = 0; x < xres; x++)
387
        rgb_conv(&bgscan[x * pixelbytes], BG_COLOR);
381
        rgb_conv(&bgscan[x * pixelbytes], BG_COLOR);
388
}
382
}
389
 
383
 
390
 
384
 
391
/** Refresh the screen
385
/** Refresh the screen
392
 *
386
 *
393
 */
387
 */
394
void fb_redraw(void)
388
void fb_redraw(void)
395
{
389
{
396
    if (ylogo > 0) {
390
    if (ylogo > 0) {
397
        unsigned int y;
391
        unsigned int y;
398
       
392
       
399
        for (y = 0; y < LOGO_HEIGHT; y++) {
393
        for (y = 0; y < LOGO_HEIGHT; y++) {
400
            unsigned int x;
394
            unsigned int x;
401
           
395
           
402
            for (x = 0; x < xres; x++)
396
            for (x = 0; x < xres; x++)
403
                rgb_conv(&fb_addr[FB_POS(x, y)],
397
                rgb_conv(&fb_addr[FB_POS(x, y)],
404
                    (x < LOGO_WIDTH) ?
398
                    (x < LOGO_WIDTH) ?
405
                    fb_logo[y * LOGO_WIDTH + x] :
399
                    fb_logo[y * LOGO_WIDTH + x] :
406
                    LOGO_COLOR);
400
                    LOGO_COLOR);
407
        }
401
        }
408
    }
402
    }
409
   
403
   
410
    unsigned int row;
404
    unsigned int row;
411
   
405
   
412
    for (row = 0; row < rowtrim; row++) {
406
    for (row = 0; row < rowtrim; row++) {
413
        unsigned int y = ylogo + ROW2Y(row);
407
        unsigned int y = ylogo + ROW2Y(row);
414
        unsigned int yd;
408
        unsigned int yd;
415
       
409
       
416
        for (yd = 0; yd < FONT_SCANLINES; yd++) {
410
        for (yd = 0; yd < FONT_SCANLINES; yd++) {
417
            unsigned int x;
411
            unsigned int x;
418
            unsigned int col;
412
            unsigned int col;
419
           
413
           
420
            for (col = 0, x = 0; col < cols;
414
            for (col = 0, x = 0; col < cols;
421
                col++, x += FONT_WIDTH) {
415
                col++, x += FONT_WIDTH) {
422
                uint16_t glyph = backbuf[BB_POS(col, row)];
416
                uint16_t glyph = backbuf[BB_POS(col, row)];
423
                void *dst = &fb_addr[FB_POS(x, y + yd)];
417
                void *dst = &fb_addr[FB_POS(x, y + yd)];
424
                void *src = &glyphs[GLYPH_POS(glyph, yd)];
418
                void *src = &glyphs[GLYPH_POS(glyph, yd)];
425
                memcpy(dst, src, glyphscanline);
419
                memcpy(dst, src, glyphscanline);
426
            }
420
            }
427
        }
421
        }
428
    }
422
    }
429
   
423
   
430
    if (COL2X(cols) < xres) {
424
    if (COL2X(cols) < xres) {
431
        unsigned int y;
425
        unsigned int y;
432
        unsigned int size = (xres - COL2X(cols)) * pixelbytes;
426
        unsigned int size = (xres - COL2X(cols)) * pixelbytes;
433
       
427
       
434
        for (y = ylogo; y < yres; y++)
428
        for (y = ylogo; y < yres; y++)
435
            memcpy(&fb_addr[FB_POS(COL2X(cols), y)], bgscan, size);
429
            memcpy(&fb_addr[FB_POS(COL2X(cols), y)], bgscan, size);
436
    }
430
    }
437
   
431
   
438
    if (ROW2Y(rowtrim) + ylogo < yres) {
432
    if (ROW2Y(rowtrim) + ylogo < yres) {
439
        unsigned int y;
433
        unsigned int y;
440
       
434
       
441
        for (y = ROW2Y(rowtrim) + ylogo; y < yres; y++)
435
        for (y = ROW2Y(rowtrim) + ylogo; y < yres; y++)
442
            memcpy(&fb_addr[FB_POS(0, y)], bgscan, bgscanbytes);
436
            memcpy(&fb_addr[FB_POS(0, y)], bgscan, bgscanbytes);
443
    }
437
    }
444
}
438
}
445
 
439
 
446
 
440
 
447
/** Initialize framebuffer as a output character device
441
/** Initialize framebuffer as a output character device
448
 *
442
 *
449
 * @param addr   Physical address of the framebuffer
443
 * @param addr   Physical address of the framebuffer
450
 * @param x      Screen width in pixels
444
 * @param x      Screen width in pixels
451
 * @param y      Screen height in pixels
445
 * @param y      Screen height in pixels
452
 * @param scan   Bytes per one scanline
446
 * @param scan   Bytes per one scanline
453
 * @param visual Color model
447
 * @param visual Color model
454
 *
448
 *
455
 */
449
 */
456
void fb_init(fb_properties_t *props)
450
void fb_init(fb_properties_t *props)
457
{
451
{
458
    switch (props->visual) {
452
    switch (props->visual) {
459
    case VISUAL_INDIRECT_8:
453
    case VISUAL_INDIRECT_8:
460
        rgb_conv = rgb_323;
454
        rgb_conv = bgr_323;
461
        pixelbytes = 1;
455
        pixelbytes = 1;
462
        break;
456
        break;
463
    case VISUAL_RGB_5_5_5:
457
    case VISUAL_BGR_5_5_5:
464
        rgb_conv = rgb_555;
458
        rgb_conv = bgr_555;
465
        pixelbytes = 2;
459
        pixelbytes = 2;
466
        break;
460
        break;
467
    case VISUAL_RGB_5_6_5:
461
    case VISUAL_BGR_5_6_5:
468
        rgb_conv = rgb_565;
462
        rgb_conv = bgr_565;
469
        pixelbytes = 2;
463
        pixelbytes = 2;
470
        break;
464
        break;
471
    case VISUAL_RGB_8_8_8:
465
    case VISUAL_RGB_8_8_8:
472
        rgb_conv = rgb_888;
466
        rgb_conv = rgb_888;
473
        pixelbytes = 3;
467
        pixelbytes = 3;
474
        break;
468
        break;
475
    case VISUAL_BGR_8_8_8:
469
    case VISUAL_BGR_8_8_8:
476
        rgb_conv = bgr_888;
470
        rgb_conv = bgr_888;
477
        pixelbytes = 3;
471
        pixelbytes = 3;
478
        break;
472
        break;
479
    case VISUAL_RGB_8_8_8_0:
473
    case VISUAL_RGB_8_8_8_0:
480
        rgb_conv = rgb_8880;
474
        rgb_conv = rgb_8880;
481
        pixelbytes = 4;
475
        pixelbytes = 4;
482
        break;
476
        break;
483
    case VISUAL_RGB_0_8_8_8:
477
    case VISUAL_RGB_0_8_8_8:
484
        rgb_conv = rgb_0888;
478
        rgb_conv = rgb_0888;
485
        pixelbytes = 4;
479
        pixelbytes = 4;
486
        break;
480
        break;
487
    case VISUAL_BGR_0_8_8_8:
481
    case VISUAL_BGR_0_8_8_8:
488
        rgb_conv = bgr_0888;
482
        rgb_conv = bgr_0888;
489
        pixelbytes = 4;
483
        pixelbytes = 4;
-
 
484
        break;
-
 
485
    case VISUAL_BGR_8_8_8_0:
-
 
486
        rgb_conv = bgr_8880;
-
 
487
        pixelbytes = 4;
490
        break;
488
        break;
491
    default:
489
    default:
492
        panic("Unsupported visual.");
490
        panic("Unsupported visual.");
493
    }
491
    }
494
   
492
   
495
    xres = props->x;
493
    xres = props->x;
496
    yres = props->y;
494
    yres = props->y;
497
    scanline = props->scan;
495
    scanline = props->scan;
498
   
496
   
499
    cols = X2COL(xres);
497
    cols = X2COL(xres);
500
    rows = Y2ROW(yres);
498
    rows = Y2ROW(yres);
501
   
499
   
502
    if (yres > ylogo) {
500
    if (yres > ylogo) {
503
        ylogo = LOGO_HEIGHT;
501
        ylogo = LOGO_HEIGHT;
504
        rowtrim = rows - Y2ROW(ylogo);
502
        rowtrim = rows - Y2ROW(ylogo);
505
        if (ylogo % FONT_SCANLINES > 0)
503
        if (ylogo % FONT_SCANLINES > 0)
506
            rowtrim--;
504
            rowtrim--;
507
        ytrim = ROW2Y(rowtrim);
505
        ytrim = ROW2Y(rowtrim);
508
    } else {
506
    } else {
509
        ylogo = 0;
507
        ylogo = 0;
510
        ytrim = yres;
508
        ytrim = yres;
511
        rowtrim = rows;
509
        rowtrim = rows;
512
    }
510
    }
513
   
511
   
514
    glyphscanline = FONT_WIDTH * pixelbytes;
512
    glyphscanline = FONT_WIDTH * pixelbytes;
515
    glyphbytes = ROW2Y(glyphscanline);
513
    glyphbytes = ROW2Y(glyphscanline);
516
    bgscanbytes = xres * pixelbytes;
514
    bgscanbytes = xres * pixelbytes;
517
   
515
   
518
    size_t fbsize = scanline * yres;
516
    size_t fbsize = scanline * yres;
519
    size_t bbsize = cols * rows * sizeof(uint16_t);
517
    size_t bbsize = cols * rows * sizeof(uint16_t);
520
    size_t glyphsize = FONT_GLYPHS * glyphbytes;
518
    size_t glyphsize = FONT_GLYPHS * glyphbytes;
521
   
519
   
522
    backbuf = (uint16_t *) malloc(bbsize, 0);
520
    backbuf = (uint16_t *) malloc(bbsize, 0);
523
    if (!backbuf)
521
    if (!backbuf)
524
        panic("Unable to allocate backbuffer.");
522
        panic("Unable to allocate backbuffer.");
525
   
523
   
526
    glyphs = (uint8_t *) malloc(glyphsize, 0);
524
    glyphs = (uint8_t *) malloc(glyphsize, 0);
527
    if (!glyphs)
525
    if (!glyphs)
528
        panic("Unable to allocate glyphs.");
526
        panic("Unable to allocate glyphs.");
529
   
527
   
530
    bgscan = malloc(bgscanbytes, 0);
528
    bgscan = malloc(bgscanbytes, 0);
531
    if (!bgscan)
529
    if (!bgscan)
532
        panic("Unable to allocate background pixel.");
530
        panic("Unable to allocate background pixel.");
533
   
531
   
534
    memsetw(backbuf, cols * rows, 0);
532
    memsetw(backbuf, cols * rows, 0);
535
   
533
   
536
    glyphs_render();
534
    glyphs_render();
537
   
535
   
538
    fb_addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize);
536
    fb_addr = (uint8_t *) hw_map((uintptr_t) props->addr, fbsize);
539
   
537
   
540
    sysinfo_set_item_val("fb", NULL, true);
538
    sysinfo_set_item_val("fb", NULL, true);
541
    sysinfo_set_item_val("fb.kind", NULL, 1);
539
    sysinfo_set_item_val("fb.kind", NULL, 1);
542
    sysinfo_set_item_val("fb.width", NULL, xres);
540
    sysinfo_set_item_val("fb.width", NULL, xres);
543
    sysinfo_set_item_val("fb.height", NULL, yres);
541
    sysinfo_set_item_val("fb.height", NULL, yres);
544
    sysinfo_set_item_val("fb.scanline", NULL, scanline);
542
    sysinfo_set_item_val("fb.scanline", NULL, scanline);
545
    sysinfo_set_item_val("fb.visual", NULL, props->visual);
543
    sysinfo_set_item_val("fb.visual", NULL, props->visual);
546
    sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
544
    sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
547
   
545
   
548
    fb_redraw();
546
    fb_redraw();
549
   
547
   
550
    outdev_initialize("fb", &fb_console, &fb_ops);
548
    outdev_initialize("fb", &fb_console, &fb_ops);
551
    stdout = &fb_console;
549
    stdout = &fb_console;
552
}
550
}
553
 
551
 
554
/** @}
552
/** @}
555
 */
553
 */
556
 
554