Subversion Repositories HelenOS

Rev

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

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