1 | /*
|
---|
2 | * Copyright (c) 2008 Martin Decky
|
---|
3 | * Copyright (c) 2006 Ondrej 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 |
|
---|
30 | /** @addtogroup kernel_genarch
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <debug.h>
|
---|
38 | #include <genarch/fb/font-8x16.h>
|
---|
39 | #include <genarch/fb/fb.h>
|
---|
40 | #include <console/chardev.h>
|
---|
41 | #include <console/console.h>
|
---|
42 | #include <sysinfo/sysinfo.h>
|
---|
43 | #include <mm/km.h>
|
---|
44 | #include <stdlib.h>
|
---|
45 | #include <align.h>
|
---|
46 | #include <panic.h>
|
---|
47 | #include <memw.h>
|
---|
48 | #include <config.h>
|
---|
49 | #include <bitops.h>
|
---|
50 | #include <str.h>
|
---|
51 | #include <ddi/ddi.h>
|
---|
52 | #include <typedefs.h>
|
---|
53 | #include <byteorder.h>
|
---|
54 |
|
---|
55 | #define BG_COLOR 0x001620
|
---|
56 | #define FG_COLOR 0xf3cf65
|
---|
57 | #define INV_COLOR 0xaaaaaa
|
---|
58 |
|
---|
59 | #define FB_PAGES 8
|
---|
60 |
|
---|
61 | #define RED(x, bits) (((x) >> (8 + 8 + 8 - (bits))) & ((1 << (bits)) - 1))
|
---|
62 | #define GREEN(x, bits) (((x) >> (8 + 8 - (bits))) & ((1 << (bits)) - 1))
|
---|
63 | #define BLUE(x, bits) (((x) >> (8 - (bits))) & ((1 << (bits)) - 1))
|
---|
64 |
|
---|
65 | #define COL2X(col) ((col) * FONT_WIDTH)
|
---|
66 | #define ROW2Y(row) ((row) * FONT_SCANLINES)
|
---|
67 |
|
---|
68 | #define X2COL(x) ((x) / FONT_WIDTH)
|
---|
69 | #define Y2ROW(y) ((y) / FONT_SCANLINES)
|
---|
70 |
|
---|
71 | #define FB_POS(instance, x, y) \
|
---|
72 | ((y) * (instance)->scanline + (x) * (instance)->pixelbytes)
|
---|
73 |
|
---|
74 | #define BB_POS(instance, col, row) \
|
---|
75 | ((((instance)->start_row + (row)) % (instance)->rows) * \
|
---|
76 | (instance)->cols + (col))
|
---|
77 |
|
---|
78 | #define BB_NEXT_COL(pos) (++(pos))
|
---|
79 |
|
---|
80 | #define GLYPH_POS(instance, glyph, y) \
|
---|
81 | ((glyph) * (instance)->glyphbytes + (y) * (instance)->glyphscanline)
|
---|
82 |
|
---|
83 | #define TAB_WIDTH 8
|
---|
84 |
|
---|
85 | typedef void (*rgb_conv_t)(void *, uint32_t);
|
---|
86 |
|
---|
87 | typedef struct {
|
---|
88 | SPINLOCK_DECLARE(lock);
|
---|
89 |
|
---|
90 | parea_t parea;
|
---|
91 |
|
---|
92 | uint8_t *addr;
|
---|
93 | uint16_t *backbuf;
|
---|
94 | uint8_t *glyphs;
|
---|
95 | uint8_t *bgscan;
|
---|
96 |
|
---|
97 | rgb_conv_t rgb_conv;
|
---|
98 |
|
---|
99 | unsigned int xres;
|
---|
100 | unsigned int yres;
|
---|
101 |
|
---|
102 | /** Number of rows that fit on framebuffer */
|
---|
103 | unsigned int screen_rows;
|
---|
104 |
|
---|
105 | unsigned int scanline;
|
---|
106 | unsigned int glyphscanline;
|
---|
107 |
|
---|
108 | unsigned int pixelbytes;
|
---|
109 | unsigned int glyphbytes;
|
---|
110 | unsigned int bgscanbytes;
|
---|
111 |
|
---|
112 | /** Number of columns in the backbuffer */
|
---|
113 | unsigned int cols;
|
---|
114 | /** Number of rows in the backbuffer */
|
---|
115 | unsigned int rows;
|
---|
116 |
|
---|
117 | /** Starting row in the cyclic backbuffer */
|
---|
118 | unsigned int start_row;
|
---|
119 |
|
---|
120 | /** Top-most visible row (relative to start_row) */
|
---|
121 | unsigned int offset_row;
|
---|
122 |
|
---|
123 | /** Current backbuffer position */
|
---|
124 | unsigned int position;
|
---|
125 |
|
---|
126 | /** Partial character between writes */
|
---|
127 | mbstate_t mbstate;
|
---|
128 |
|
---|
129 | unsigned int row;
|
---|
130 | unsigned int column;
|
---|
131 | } fb_instance_t;
|
---|
132 |
|
---|
133 | static void fb_write(outdev_t *, const char *, size_t);
|
---|
134 | static void fb_redraw(outdev_t *);
|
---|
135 | static void fb_scroll_up(outdev_t *);
|
---|
136 | static void fb_scroll_down(outdev_t *);
|
---|
137 |
|
---|
138 | static outdev_operations_t fbdev_ops = {
|
---|
139 | .write = fb_write,
|
---|
140 | .redraw = fb_redraw,
|
---|
141 | .scroll_up = fb_scroll_up,
|
---|
142 | .scroll_down = fb_scroll_down
|
---|
143 | };
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * RGB conversion functions.
|
---|
147 | *
|
---|
148 | * These functions write an RGB value to some memory in some predefined format.
|
---|
149 | * The naming convention corresponds to the format created by these functions.
|
---|
150 | * The functions use the so called network order (i.e. big endian) with respect
|
---|
151 | * to their names.
|
---|
152 | */
|
---|
153 |
|
---|
154 | static void rgb_0888(void *dst, uint32_t rgb)
|
---|
155 | {
|
---|
156 | *((uint32_t *) dst) = host2uint32_t_be((0 << 24) |
|
---|
157 | (RED(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | (BLUE(rgb, 8)));
|
---|
158 | }
|
---|
159 |
|
---|
160 | static void bgr_0888(void *dst, uint32_t rgb)
|
---|
161 | {
|
---|
162 | *((uint32_t *) dst) = host2uint32_t_be((0 << 24) |
|
---|
163 | (BLUE(rgb, 8) << 16) | (GREEN(rgb, 8) << 8) | (RED(rgb, 8)));
|
---|
164 | }
|
---|
165 |
|
---|
166 | static void rgb_8880(void *dst, uint32_t rgb)
|
---|
167 | {
|
---|
168 | *((uint32_t *) dst) = host2uint32_t_be((RED(rgb, 8) << 24) |
|
---|
169 | (GREEN(rgb, 8) << 16) | (BLUE(rgb, 8) << 8) | 0);
|
---|
170 | }
|
---|
171 |
|
---|
172 | static void bgr_8880(void *dst, uint32_t rgb)
|
---|
173 | {
|
---|
174 | *((uint32_t *) dst) = host2uint32_t_be((BLUE(rgb, 8) << 24) |
|
---|
175 | (GREEN(rgb, 8) << 16) | (RED(rgb, 8) << 8) | 0);
|
---|
176 | }
|
---|
177 |
|
---|
178 | static void rgb_888(void *dst, uint32_t rgb)
|
---|
179 | {
|
---|
180 | ((uint8_t *) dst)[0] = RED(rgb, 8);
|
---|
181 | ((uint8_t *) dst)[1] = GREEN(rgb, 8);
|
---|
182 | ((uint8_t *) dst)[2] = BLUE(rgb, 8);
|
---|
183 | }
|
---|
184 |
|
---|
185 | static void bgr_888(void *dst, uint32_t rgb)
|
---|
186 | {
|
---|
187 | ((uint8_t *) dst)[0] = BLUE(rgb, 8);
|
---|
188 | ((uint8_t *) dst)[1] = GREEN(rgb, 8);
|
---|
189 | ((uint8_t *) dst)[2] = RED(rgb, 8);
|
---|
190 | }
|
---|
191 |
|
---|
192 | static void rgb_555_be(void *dst, uint32_t rgb)
|
---|
193 | {
|
---|
194 | *((uint16_t *) dst) = host2uint16_t_be(RED(rgb, 5) << 10 |
|
---|
195 | GREEN(rgb, 5) << 5 | BLUE(rgb, 5));
|
---|
196 | }
|
---|
197 |
|
---|
198 | static void rgb_555_le(void *dst, uint32_t rgb)
|
---|
199 | {
|
---|
200 | *((uint16_t *) dst) = host2uint16_t_le(RED(rgb, 5) << 10 |
|
---|
201 | GREEN(rgb, 5) << 5 | BLUE(rgb, 5));
|
---|
202 | }
|
---|
203 |
|
---|
204 | static void rgb_565_be(void *dst, uint32_t rgb)
|
---|
205 | {
|
---|
206 | *((uint16_t *) dst) = host2uint16_t_be(RED(rgb, 5) << 11 |
|
---|
207 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5));
|
---|
208 | }
|
---|
209 |
|
---|
210 | static void rgb_565_le(void *dst, uint32_t rgb)
|
---|
211 | {
|
---|
212 | *((uint16_t *) dst) = host2uint16_t_le(RED(rgb, 5) << 11 |
|
---|
213 | GREEN(rgb, 6) << 5 | BLUE(rgb, 5));
|
---|
214 | }
|
---|
215 |
|
---|
216 | /** BGR 3:2:3
|
---|
217 | *
|
---|
218 | * Even though we try 3:2:3 color scheme here, an 8-bit framebuffer
|
---|
219 | * will most likely use a color palette. The color appearance
|
---|
220 | * will be pretty random and depend on the default installed
|
---|
221 | * palette. This could be fixed by supporting custom palette
|
---|
222 | * and setting it to simulate the 8-bit truecolor.
|
---|
223 | *
|
---|
224 | * Currently we set the palette on the ia32, amd64, ppc32 and sparc64 port.
|
---|
225 | *
|
---|
226 | * Note that the byte is being inverted by this function. The reason is
|
---|
227 | * that we would like to use a color palette where the white color code
|
---|
228 | * is 0 and the black color code is 255, as some machines (Sun Blade 1500)
|
---|
229 | * use these codes for black and white and prevent to set codes
|
---|
230 | * 0 and 255 to other colors.
|
---|
231 | *
|
---|
232 | */
|
---|
233 | static void bgr_323(void *dst, uint32_t rgb)
|
---|
234 | {
|
---|
235 | *((uint8_t *) dst) =
|
---|
236 | ~((RED(rgb, 3) << 5) | (GREEN(rgb, 2) << 3) | BLUE(rgb, 3));
|
---|
237 | }
|
---|
238 |
|
---|
239 | /** Draw character at given position
|
---|
240 | *
|
---|
241 | */
|
---|
242 | static void glyph_draw(fb_instance_t *instance, uint16_t glyph,
|
---|
243 | unsigned int col, unsigned int row, bool overlay)
|
---|
244 | {
|
---|
245 | if (!overlay)
|
---|
246 | instance->backbuf[BB_POS(instance, col, row)] = glyph;
|
---|
247 |
|
---|
248 | /* Do not output if the framebuffer is used by user space */
|
---|
249 | if ((instance->parea.mapped) && (!console_override))
|
---|
250 | return;
|
---|
251 |
|
---|
252 | /* Check whether the glyph should be visible */
|
---|
253 | if (row < instance->offset_row)
|
---|
254 | return;
|
---|
255 |
|
---|
256 | unsigned int rel_row = row - instance->offset_row;
|
---|
257 | if (rel_row >= instance->screen_rows)
|
---|
258 | return;
|
---|
259 |
|
---|
260 | unsigned int x = COL2X(col);
|
---|
261 | unsigned int y = ROW2Y(rel_row);
|
---|
262 |
|
---|
263 | for (unsigned int yd = 0; yd < FONT_SCANLINES; yd++)
|
---|
264 | memcpy(&instance->addr[FB_POS(instance, x, y + yd)],
|
---|
265 | &instance->glyphs[GLYPH_POS(instance, glyph, yd)],
|
---|
266 | instance->glyphscanline);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /** Scroll screen down by one row
|
---|
270 | *
|
---|
271 | */
|
---|
272 | static void screen_scroll(fb_instance_t *instance)
|
---|
273 | {
|
---|
274 | if ((!instance->parea.mapped) || (console_override)) {
|
---|
275 | for (unsigned int rel_row = 0; rel_row < instance->screen_rows; rel_row++) {
|
---|
276 | unsigned int y = ROW2Y(rel_row);
|
---|
277 | unsigned int row = rel_row + instance->offset_row;
|
---|
278 |
|
---|
279 | for (unsigned int yd = 0; yd < FONT_SCANLINES; yd++) {
|
---|
280 | unsigned int x;
|
---|
281 | unsigned int col;
|
---|
282 | size_t bb_pos = BB_POS(instance, 0, row);
|
---|
283 | size_t bb_pos1 = BB_POS(instance, 0, row + 1);
|
---|
284 |
|
---|
285 | for (col = 0, x = 0; col < instance->cols;
|
---|
286 | col++, x += FONT_WIDTH) {
|
---|
287 | uint16_t glyph;
|
---|
288 |
|
---|
289 | if (row < instance->rows - 1) {
|
---|
290 | if (instance->backbuf[bb_pos] ==
|
---|
291 | instance->backbuf[bb_pos1])
|
---|
292 | goto skip;
|
---|
293 |
|
---|
294 | glyph = instance->backbuf[bb_pos1];
|
---|
295 | } else
|
---|
296 | glyph = 0;
|
---|
297 |
|
---|
298 | memcpy(&instance->addr[FB_POS(instance, x, y + yd)],
|
---|
299 | &instance->glyphs[GLYPH_POS(instance, glyph, yd)],
|
---|
300 | instance->glyphscanline);
|
---|
301 | skip:
|
---|
302 | BB_NEXT_COL(bb_pos);
|
---|
303 | BB_NEXT_COL(bb_pos1);
|
---|
304 | }
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|
308 |
|
---|
309 | /*
|
---|
310 | * Implement backbuffer scrolling by wrapping around
|
---|
311 | * the cyclic buffer.
|
---|
312 | */
|
---|
313 |
|
---|
314 | instance->start_row++;
|
---|
315 | if (instance->start_row == instance->rows)
|
---|
316 | instance->start_row = 0;
|
---|
317 |
|
---|
318 | memsetw(&instance->backbuf[BB_POS(instance, 0, instance->rows - 1)],
|
---|
319 | instance->cols, 0);
|
---|
320 | }
|
---|
321 |
|
---|
322 | static void cursor_put(fb_instance_t *instance)
|
---|
323 | {
|
---|
324 | unsigned int col = instance->column;
|
---|
325 | unsigned int row = instance->row;
|
---|
326 |
|
---|
327 | glyph_draw(instance, fb_font_glyph(U_CURSOR), col, row, true);
|
---|
328 | }
|
---|
329 |
|
---|
330 | static void cursor_remove(fb_instance_t *instance)
|
---|
331 | {
|
---|
332 | unsigned int col = instance->column;
|
---|
333 | unsigned int row = instance->row;
|
---|
334 |
|
---|
335 | glyph_draw(instance, instance->backbuf[BB_POS(instance, col, row)],
|
---|
336 | col, row, true);
|
---|
337 | }
|
---|
338 |
|
---|
339 | /** Render glyphs
|
---|
340 | *
|
---|
341 | * Convert glyphs from device independent font
|
---|
342 | * description to current visual representation.
|
---|
343 | *
|
---|
344 | */
|
---|
345 | static void glyphs_render(fb_instance_t *instance)
|
---|
346 | {
|
---|
347 | /* Prerender glyphs */
|
---|
348 | uint16_t glyph;
|
---|
349 |
|
---|
350 | for (glyph = 0; glyph < FONT_GLYPHS; glyph++) {
|
---|
351 | uint32_t fg_color;
|
---|
352 |
|
---|
353 | if (glyph == FONT_GLYPHS - 1)
|
---|
354 | fg_color = INV_COLOR;
|
---|
355 | else
|
---|
356 | fg_color = FG_COLOR;
|
---|
357 |
|
---|
358 | unsigned int y;
|
---|
359 |
|
---|
360 | for (y = 0; y < FONT_SCANLINES; y++) {
|
---|
361 | unsigned int x;
|
---|
362 |
|
---|
363 | for (x = 0; x < FONT_WIDTH; x++) {
|
---|
364 | void *dst =
|
---|
365 | &instance->glyphs[GLYPH_POS(instance, glyph, y) +
|
---|
366 | x * instance->pixelbytes];
|
---|
367 | uint32_t rgb = (fb_font[glyph][y] &
|
---|
368 | (1 << (7 - x))) ? fg_color : BG_COLOR;
|
---|
369 | instance->rgb_conv(dst, rgb);
|
---|
370 | }
|
---|
371 | }
|
---|
372 | }
|
---|
373 |
|
---|
374 | /* Prerender background scanline */
|
---|
375 | unsigned int x;
|
---|
376 |
|
---|
377 | for (x = 0; x < instance->xres; x++)
|
---|
378 | instance->rgb_conv(&instance->bgscan[x * instance->pixelbytes], BG_COLOR);
|
---|
379 | }
|
---|
380 |
|
---|
381 | static void fb_redraw_internal(fb_instance_t *instance)
|
---|
382 | {
|
---|
383 | for (unsigned int rel_row = 0; rel_row < instance->screen_rows; rel_row++) {
|
---|
384 | unsigned int y = ROW2Y(rel_row);
|
---|
385 | unsigned int row = rel_row + instance->offset_row;
|
---|
386 |
|
---|
387 | for (unsigned int yd = 0; yd < FONT_SCANLINES; yd++) {
|
---|
388 | unsigned int x;
|
---|
389 | unsigned int col;
|
---|
390 | size_t bb_pos = BB_POS(instance, 0, row);
|
---|
391 |
|
---|
392 | for (col = 0, x = 0; col < instance->cols;
|
---|
393 | col++, x += FONT_WIDTH) {
|
---|
394 | uint16_t glyph =
|
---|
395 | instance->backbuf[bb_pos];
|
---|
396 | void *dst = &instance->addr[FB_POS(instance, x, y + yd)];
|
---|
397 | void *src = &instance->glyphs[GLYPH_POS(instance, glyph, yd)];
|
---|
398 | memcpy(dst, src, instance->glyphscanline);
|
---|
399 | BB_NEXT_COL(bb_pos);
|
---|
400 | }
|
---|
401 | }
|
---|
402 | }
|
---|
403 |
|
---|
404 | if (COL2X(instance->cols) < instance->xres) {
|
---|
405 | unsigned int y;
|
---|
406 | unsigned int size =
|
---|
407 | (instance->xres - COL2X(instance->cols)) * instance->pixelbytes;
|
---|
408 |
|
---|
409 | for (y = 0; y < instance->yres; y++)
|
---|
410 | memcpy(&instance->addr[FB_POS(instance, COL2X(instance->cols), y)],
|
---|
411 | instance->bgscan, size);
|
---|
412 | }
|
---|
413 |
|
---|
414 | if (ROW2Y(instance->screen_rows) < instance->yres) {
|
---|
415 | unsigned int y;
|
---|
416 |
|
---|
417 | for (y = ROW2Y(instance->screen_rows); y < instance->yres; y++)
|
---|
418 | memcpy(&instance->addr[FB_POS(instance, 0, y)],
|
---|
419 | instance->bgscan, instance->bgscanbytes);
|
---|
420 | }
|
---|
421 | }
|
---|
422 |
|
---|
423 | static void _advance_row(fb_instance_t *instance)
|
---|
424 | {
|
---|
425 | instance->column = 0;
|
---|
426 | instance->row++;
|
---|
427 | }
|
---|
428 |
|
---|
429 | static void _advance_column(fb_instance_t *instance)
|
---|
430 | {
|
---|
431 | instance->column++;
|
---|
432 | if (instance->column == instance->cols)
|
---|
433 | _advance_row(instance);
|
---|
434 | }
|
---|
435 |
|
---|
436 | /** Print character to screen
|
---|
437 | *
|
---|
438 | * Emulate basic terminal commands.
|
---|
439 | *
|
---|
440 | */
|
---|
441 | static void _putuchar(fb_instance_t *instance, char32_t ch)
|
---|
442 | {
|
---|
443 | switch (ch) {
|
---|
444 | case '\n':
|
---|
445 | _advance_row(instance);
|
---|
446 | break;
|
---|
447 | case '\r':
|
---|
448 | instance->column = 0;
|
---|
449 | break;
|
---|
450 | case '\b':
|
---|
451 | if (instance->column > 0)
|
---|
452 | instance->column--;
|
---|
453 | break;
|
---|
454 | case '\t':
|
---|
455 | do {
|
---|
456 | glyph_draw(instance, fb_font_glyph(' '),
|
---|
457 | instance->column,
|
---|
458 | instance->row, false);
|
---|
459 | _advance_column(instance);
|
---|
460 | } while (instance->column % TAB_WIDTH != 0);
|
---|
461 | break;
|
---|
462 | default:
|
---|
463 | glyph_draw(instance, fb_font_glyph(ch),
|
---|
464 | instance->column,
|
---|
465 | instance->row, false);
|
---|
466 | _advance_column(instance);
|
---|
467 | }
|
---|
468 |
|
---|
469 | while (instance->row >= instance->rows) {
|
---|
470 | instance->row--;
|
---|
471 | screen_scroll(instance);
|
---|
472 | }
|
---|
473 | }
|
---|
474 |
|
---|
475 | static void fb_write(outdev_t *dev, const char *s, size_t n)
|
---|
476 | {
|
---|
477 | fb_instance_t *instance = (fb_instance_t *) dev->data;
|
---|
478 |
|
---|
479 | spinlock_lock(&instance->lock);
|
---|
480 | cursor_remove(instance);
|
---|
481 |
|
---|
482 | size_t offset = 0;
|
---|
483 | char32_t ch;
|
---|
484 |
|
---|
485 | while ((ch = str_decode_r(s, &offset, n, U_SPECIAL, &instance->mbstate)))
|
---|
486 | _putuchar(instance, ch);
|
---|
487 |
|
---|
488 | cursor_put(instance);
|
---|
489 | spinlock_unlock(&instance->lock);
|
---|
490 | }
|
---|
491 |
|
---|
492 | /** Scroll the framebuffer up
|
---|
493 | *
|
---|
494 | */
|
---|
495 | static void fb_scroll_up(outdev_t *dev)
|
---|
496 | {
|
---|
497 | fb_instance_t *instance = (fb_instance_t *) dev->data;
|
---|
498 | spinlock_lock(&instance->lock);
|
---|
499 |
|
---|
500 | if (instance->offset_row >= instance->screen_rows / 2)
|
---|
501 | instance->offset_row -= instance->screen_rows / 2;
|
---|
502 | else
|
---|
503 | instance->offset_row = 0;
|
---|
504 |
|
---|
505 | fb_redraw_internal(instance);
|
---|
506 | cursor_put(instance);
|
---|
507 |
|
---|
508 | spinlock_unlock(&instance->lock);
|
---|
509 | }
|
---|
510 |
|
---|
511 | /** Scroll the framebuffer down
|
---|
512 | *
|
---|
513 | */
|
---|
514 | static void fb_scroll_down(outdev_t *dev)
|
---|
515 | {
|
---|
516 | fb_instance_t *instance = (fb_instance_t *) dev->data;
|
---|
517 | spinlock_lock(&instance->lock);
|
---|
518 |
|
---|
519 | if (instance->offset_row + instance->screen_rows / 2 <=
|
---|
520 | instance->rows - instance->screen_rows)
|
---|
521 | instance->offset_row += instance->screen_rows / 2;
|
---|
522 | else
|
---|
523 | instance->offset_row = instance->rows - instance->screen_rows;
|
---|
524 |
|
---|
525 | fb_redraw_internal(instance);
|
---|
526 | cursor_put(instance);
|
---|
527 |
|
---|
528 | spinlock_unlock(&instance->lock);
|
---|
529 | }
|
---|
530 |
|
---|
531 | /** Refresh the screen
|
---|
532 | *
|
---|
533 | */
|
---|
534 | static void fb_redraw(outdev_t *dev)
|
---|
535 | {
|
---|
536 | fb_instance_t *instance = (fb_instance_t *) dev->data;
|
---|
537 |
|
---|
538 | spinlock_lock(&instance->lock);
|
---|
539 | fb_redraw_internal(instance);
|
---|
540 | spinlock_unlock(&instance->lock);
|
---|
541 | }
|
---|
542 |
|
---|
543 | /** Framebuffer was mapped or unmapped.
|
---|
544 | *
|
---|
545 | * @param arg Framebuffer instance
|
---|
546 | */
|
---|
547 | static void fb_mapped_changed(void *arg)
|
---|
548 | {
|
---|
549 | fb_instance_t *instance = (fb_instance_t *) arg;
|
---|
550 |
|
---|
551 | if (!instance->parea.mapped) {
|
---|
552 | spinlock_lock(&instance->lock);
|
---|
553 | fb_redraw_internal(instance);
|
---|
554 | spinlock_unlock(&instance->lock);
|
---|
555 | }
|
---|
556 | }
|
---|
557 |
|
---|
558 | /** Initialize framebuffer as a output character device
|
---|
559 | *
|
---|
560 | */
|
---|
561 | outdev_t *fb_init(fb_properties_t *props)
|
---|
562 | {
|
---|
563 | assert(props);
|
---|
564 | assert(props->x > 0);
|
---|
565 | assert(props->y > 0);
|
---|
566 | assert(props->scan > 0);
|
---|
567 |
|
---|
568 | rgb_conv_t rgb_conv;
|
---|
569 | unsigned int pixelbytes;
|
---|
570 |
|
---|
571 | switch (props->visual) {
|
---|
572 | case VISUAL_INDIRECT_8:
|
---|
573 | rgb_conv = bgr_323;
|
---|
574 | pixelbytes = 1;
|
---|
575 | break;
|
---|
576 | case VISUAL_RGB_5_5_5_LE:
|
---|
577 | rgb_conv = rgb_555_le;
|
---|
578 | pixelbytes = 2;
|
---|
579 | break;
|
---|
580 | case VISUAL_RGB_5_5_5_BE:
|
---|
581 | rgb_conv = rgb_555_be;
|
---|
582 | pixelbytes = 2;
|
---|
583 | break;
|
---|
584 | case VISUAL_RGB_5_6_5_LE:
|
---|
585 | rgb_conv = rgb_565_le;
|
---|
586 | pixelbytes = 2;
|
---|
587 | break;
|
---|
588 | case VISUAL_RGB_5_6_5_BE:
|
---|
589 | rgb_conv = rgb_565_be;
|
---|
590 | pixelbytes = 2;
|
---|
591 | break;
|
---|
592 | case VISUAL_RGB_8_8_8:
|
---|
593 | rgb_conv = rgb_888;
|
---|
594 | pixelbytes = 3;
|
---|
595 | break;
|
---|
596 | case VISUAL_BGR_8_8_8:
|
---|
597 | rgb_conv = bgr_888;
|
---|
598 | pixelbytes = 3;
|
---|
599 | break;
|
---|
600 | case VISUAL_RGB_8_8_8_0:
|
---|
601 | rgb_conv = rgb_8880;
|
---|
602 | pixelbytes = 4;
|
---|
603 | break;
|
---|
604 | case VISUAL_RGB_0_8_8_8:
|
---|
605 | rgb_conv = rgb_0888;
|
---|
606 | pixelbytes = 4;
|
---|
607 | break;
|
---|
608 | case VISUAL_BGR_0_8_8_8:
|
---|
609 | rgb_conv = bgr_0888;
|
---|
610 | pixelbytes = 4;
|
---|
611 | break;
|
---|
612 | case VISUAL_BGR_8_8_8_0:
|
---|
613 | rgb_conv = bgr_8880;
|
---|
614 | pixelbytes = 4;
|
---|
615 | break;
|
---|
616 | default:
|
---|
617 | LOG("Unsupported visual.");
|
---|
618 | return NULL;
|
---|
619 | }
|
---|
620 |
|
---|
621 | outdev_t *fbdev = malloc(sizeof(outdev_t));
|
---|
622 | if (!fbdev)
|
---|
623 | return NULL;
|
---|
624 |
|
---|
625 | fb_instance_t *instance = malloc(sizeof(fb_instance_t));
|
---|
626 | if (!instance) {
|
---|
627 | free(fbdev);
|
---|
628 | return NULL;
|
---|
629 | }
|
---|
630 |
|
---|
631 | outdev_initialize("fbdev", fbdev, &fbdev_ops);
|
---|
632 | fbdev->data = instance;
|
---|
633 |
|
---|
634 | spinlock_initialize(&instance->lock, "*fb.instance.lock");
|
---|
635 |
|
---|
636 | instance->rgb_conv = rgb_conv;
|
---|
637 | instance->pixelbytes = pixelbytes;
|
---|
638 | instance->xres = props->x;
|
---|
639 | instance->yres = props->y;
|
---|
640 | instance->scanline = props->scan;
|
---|
641 |
|
---|
642 | instance->screen_rows = Y2ROW(instance->yres);
|
---|
643 |
|
---|
644 | instance->cols = X2COL(instance->xres);
|
---|
645 | instance->rows = FB_PAGES * instance->screen_rows;
|
---|
646 |
|
---|
647 | instance->start_row = instance->rows - instance->screen_rows;
|
---|
648 | instance->offset_row = instance->start_row;
|
---|
649 | instance->row = instance->start_row;
|
---|
650 | instance->column = 0;
|
---|
651 |
|
---|
652 | instance->glyphscanline = FONT_WIDTH * instance->pixelbytes;
|
---|
653 | instance->glyphbytes = ROW2Y(instance->glyphscanline);
|
---|
654 | instance->bgscanbytes = instance->xres * instance->pixelbytes;
|
---|
655 |
|
---|
656 | size_t fbsize = instance->scanline * instance->yres;
|
---|
657 | size_t bbsize = instance->cols * instance->rows * sizeof(uint16_t);
|
---|
658 | size_t glyphsize = FONT_GLYPHS * instance->glyphbytes;
|
---|
659 |
|
---|
660 | instance->addr = (uint8_t *) km_map((uintptr_t) props->addr, fbsize,
|
---|
661 | KM_NATURAL_ALIGNMENT, PAGE_WRITE | PAGE_WRITE_COMBINE);
|
---|
662 | if (!instance->addr) {
|
---|
663 | LOG("Unable to map framebuffer.");
|
---|
664 | free(instance);
|
---|
665 | free(fbdev);
|
---|
666 | return NULL;
|
---|
667 | }
|
---|
668 |
|
---|
669 | instance->backbuf = (uint16_t *) malloc(bbsize);
|
---|
670 | if (!instance->backbuf) {
|
---|
671 | LOG("Unable to allocate backbuffer.");
|
---|
672 | free(instance);
|
---|
673 | free(fbdev);
|
---|
674 | return NULL;
|
---|
675 | }
|
---|
676 |
|
---|
677 | instance->glyphs = (uint8_t *) malloc(glyphsize);
|
---|
678 | if (!instance->glyphs) {
|
---|
679 | LOG("Unable to allocate glyphs.");
|
---|
680 | free(instance->backbuf);
|
---|
681 | free(instance);
|
---|
682 | free(fbdev);
|
---|
683 | return NULL;
|
---|
684 | }
|
---|
685 |
|
---|
686 | instance->bgscan = malloc(instance->bgscanbytes);
|
---|
687 | if (!instance->bgscan) {
|
---|
688 | LOG("Unable to allocate background pixel.");
|
---|
689 | free(instance->glyphs);
|
---|
690 | free(instance->backbuf);
|
---|
691 | free(instance);
|
---|
692 | free(fbdev);
|
---|
693 | return NULL;
|
---|
694 | }
|
---|
695 |
|
---|
696 | memsetw(instance->backbuf, instance->cols * instance->rows, 0);
|
---|
697 | glyphs_render(instance);
|
---|
698 |
|
---|
699 | ddi_parea_init(&instance->parea);
|
---|
700 | instance->parea.pbase = props->addr;
|
---|
701 | instance->parea.frames = SIZE2FRAMES(fbsize);
|
---|
702 | instance->parea.unpriv = false;
|
---|
703 | instance->parea.mapped = false;
|
---|
704 | instance->parea.mapped_changed = fb_mapped_changed;
|
---|
705 | instance->parea.arg = (void *) instance;
|
---|
706 | ddi_parea_register(&instance->parea);
|
---|
707 |
|
---|
708 | if (!fb_exported) {
|
---|
709 | /*
|
---|
710 | * We export the kernel framebuffer for uspace usage.
|
---|
711 | * This is used in the case the uspace framebuffer
|
---|
712 | * driver is not self-sufficient.
|
---|
713 | */
|
---|
714 | sysinfo_set_item_val("fb", NULL, true);
|
---|
715 | sysinfo_set_item_val("fb.kind", NULL, 1);
|
---|
716 | sysinfo_set_item_val("fb.width", NULL, instance->xres);
|
---|
717 | sysinfo_set_item_val("fb.height", NULL, instance->yres);
|
---|
718 | sysinfo_set_item_val("fb.scanline", NULL, instance->scanline);
|
---|
719 | sysinfo_set_item_val("fb.visual", NULL, props->visual);
|
---|
720 | sysinfo_set_item_val("fb.address.physical", NULL, props->addr);
|
---|
721 |
|
---|
722 | fb_exported = true;
|
---|
723 | }
|
---|
724 |
|
---|
725 | fb_redraw(fbdev);
|
---|
726 | return fbdev;
|
---|
727 | }
|
---|
728 |
|
---|
729 | /** @}
|
---|
730 | */
|
---|