source: mainline/genarch/src/fb/fb.c@ 8b473ce

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8b473ce was a2c4445, checked in by Martin Decky <martin@…>, 19 years ago

framebuffer code cleanup, support for non-standard scanline sizes
ppc32: get framebuffer parameters from boot loader

  • Property mode set to 100644
File size: 8.5 KB
RevLine 
[bbf5657]1/*
2 * Copyright (C) 2006 Ondrej Palkovsky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * 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 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[fe050b7]29#include <genarch/fb/font-8x16.h>
30#include <genarch/fb/fb.h>
[bbf5657]31#include <console/chardev.h>
32#include <console/console.h>
[fcbca14f]33#include <panic.h>
[d87c3f3]34#include <memstr.h>
[bbf5657]35
[c2942d8]36#include "helenos.xbm"
37
[bbf5657]38SPINLOCK_INITIALIZE(fb_lock);
39
[a2c4445]40static __u8 *fbaddress = NULL;
[bbf5657]41
[a2c4445]42static unsigned int xres = 0;
43static unsigned int yres = 0;
44static unsigned int scanline = 0;
45static unsigned int pixelbytes = 0;
[bbf5657]46
[a2c4445]47static unsigned int position = 0;
48static unsigned int columns = 0;
49static unsigned int rows = 0;
[bbf5657]50
51
[a2c4445]52#define COL_WIDTH 8
53#define ROW_BYTES (scanline * FONT_SCANLINES)
54
55#define BGCOLOR 0x000080
56#define FGCOLOR 0xffff00
57#define LOGOCOLOR 0x2020b0
58
59#define RED(x, bits) ((x >> (16 + 8 - bits)) & ((1 << bits) - 1))
60#define GREEN(x, bits) ((x >> (8 + 8 - bits)) & ((1 << bits) - 1))
61#define BLUE(x, bits) ((x >> (8 - bits)) & ((1 << bits) - 1))
62
63#define POINTPOS(x, y) (y * scanline + x * pixelbytes)
[95c7526]64
65/***************************************************************/
66/* Pixel specific fuctions */
[bbf5657]67
[a2c4445]68static void (*putpixel)(unsigned int x, unsigned int y, int color);
69static int (*getpixel)(unsigned int x, unsigned int y);
[fcbca14f]70
71/** Put pixel - 24-bit depth, 1 free byte */
[a2c4445]72static void putpixel_4byte(unsigned int x, unsigned int y, int color)
[bbf5657]73{
[a2c4445]74 *((__u32 *)(fbaddress + POINTPOS(x, y))) = color;
[95c7526]75}
76
[a2c4445]77/** Return pixel color - 24-bit depth, 1 free byte */
78static int getpixel_4byte(unsigned int x, unsigned int y)
[fcbca14f]79{
[a2c4445]80 return *((__u32 *)(fbaddress + POINTPOS(x, y))) & 0xffffff;
[fcbca14f]81}
82
[a2c4445]83/** Put pixel - 24-bit depth */
84static void putpixel_3byte(unsigned int x, unsigned int y, int color)
[fcbca14f]85{
[a2c4445]86 unsigned int startbyte = POINTPOS(x, y);
[fcbca14f]87
[a2c4445]88 fbaddress[startbyte] = RED(color, 8);
89 fbaddress[startbyte + 1] = GREEN(color, 8);
90 fbaddress[startbyte + 2] = BLUE(color, 8);
[fcbca14f]91}
92
[a2c4445]93/** Return pixel color - 24-bit depth */
94static int getpixel_3byte(unsigned int x, unsigned int y)
[95c7526]95{
[a2c4445]96 unsigned int startbyte = POINTPOS(x, y);
[95c7526]97
[a2c4445]98 return fbaddress[startbyte] << 16 | fbaddress[startbyte + 1] << 8 | fbaddress[startbyte + 2];
[95c7526]99}
100
[fcbca14f]101/** Put pixel - 16-bit depth (5:6:6) */
[a2c4445]102static void putpixel_2byte(unsigned int x, unsigned int y, int color)
[fcbca14f]103{
104 /* 5-bit, 5-bits, 5-bits */
[a2c4445]105 *((__u16 *)(fbaddress + POINTPOS(x, y))) = RED(color, 5) << 11 | GREEN(color, 6) << 5 | BLUE(color, 5);
[fcbca14f]106}
107
[a2c4445]108/** Return pixel color - 16-bit depth (5:6:6) */
109static int getpixel_2byte(unsigned int x, unsigned int y)
[fcbca14f]110{
[a2c4445]111 int color = *((__u16 *)(fbaddress + POINTPOS(x, y)));
112 return (((color >> 11) & 0x1f) << (16 + 3)) | (((color >> 5) & 0x3f) << (8 + 2)) | ((color & 0x1f) << 3);
[fcbca14f]113}
114
[4ca363f]115/** Put pixel - 8-bit depth (3:2:3) */
[a2c4445]116static void putpixel_1byte(unsigned int x, unsigned int y, int color)
[fcbca14f]117{
[a2c4445]118 fbaddress[POINTPOS(x, y)] = RED(color, 3) << 5 | GREEN(color, 2) << 3 | BLUE(color, 3);
[fcbca14f]119}
120
[a2c4445]121/** Return pixel color - 8-bit depth (3:2:3) */
122static int getpixel_1byte(unsigned int x, unsigned int y)
[fcbca14f]123{
[a2c4445]124 int color = fbaddress[POINTPOS(x, y)];
125 return (((color >> 5) & 0x7) << (16 + 5)) | (((color >> 3) & 0x3) << (8 + 6)) | ((color & 0x7) << 5);
[fcbca14f]126}
127
[a2c4445]128/** Fill line with color BGCOLOR */
129static void clear_line(unsigned int y)
[95c7526]130{
[a2c4445]131 unsigned int x;
[95c7526]132
[a2c4445]133 for (x = 0; x < xres; x++)
134 (*putpixel)(x, y, BGCOLOR);
[bbf5657]135}
136
[95c7526]137
[bbf5657]138/** Fill screen with background color */
139static void clear_screen(void)
140{
[a2c4445]141 unsigned int y;
[bbf5657]142
[a2c4445]143 for (y = 0; y < yres; y++)
[bbf5657]144 clear_line(y);
145}
146
[a2c4445]147
148/** Scroll screen one row up */
149static void scroll_screen(void)
[bbf5657]150{
[a2c4445]151 unsigned int i;
152
153 memcpy((void *) fbaddress, (void *) &fbaddress[ROW_BYTES], scanline * yres - ROW_BYTES);
154
155 /* Clear last row */
156 for (i = 0; i < FONT_SCANLINES; i++)
157 clear_line((rows - 1) * FONT_SCANLINES + i);
158}
159
160
161static void invert_pixel(unsigned int x, unsigned int y)
162{
163 (*putpixel)(x, y, ~(*getpixel)(x, y));
[bbf5657]164}
165
166
167/** Draw one line of glyph at a given position */
[a2c4445]168static void draw_glyph_line(unsigned int glline, unsigned int x, unsigned int y)
[bbf5657]169{
[a2c4445]170 unsigned int i;
[bbf5657]171
[a2c4445]172 for (i = 0; i < 8; i++)
173 if (glline & (1 << (7 - i))) {
174 (*putpixel)(x + i, y, FGCOLOR);
[bbf5657]175 } else
[a2c4445]176 (*putpixel)(x + i, y, BGCOLOR);
[bbf5657]177}
178
[95c7526]179/***************************************************************/
180/* Character-console functions */
181
[bbf5657]182/** Draw character at given position */
[a2c4445]183static void draw_glyph(__u8 glyph, unsigned int col, unsigned int row)
[bbf5657]184{
[a2c4445]185 unsigned int y;
[bbf5657]186
[a2c4445]187 for (y = 0; y < FONT_SCANLINES; y++)
188 draw_glyph_line(fb_font[glyph * FONT_SCANLINES + y], col * COL_WIDTH, row * FONT_SCANLINES + y);
[bbf5657]189}
190
191/** Invert character at given position */
[a2c4445]192static void invert_char(unsigned int col, unsigned int row)
[bbf5657]193{
[a2c4445]194 unsigned int x;
195 unsigned int y;
[bbf5657]196
[a2c4445]197 for (x = 0; x < COL_WIDTH; x++)
198 for (y = 0; y < FONT_SCANLINES; y++)
199 invert_pixel(col * COL_WIDTH + x, row * FONT_SCANLINES + y);
[bbf5657]200}
201
202/** Draw character at default position */
203static void draw_char(char chr)
204{
[a2c4445]205 draw_glyph(chr, position % columns, position / columns);
[bbf5657]206}
207
[a2c4445]208static void draw_logo(unsigned int startx, unsigned int starty)
[c2942d8]209{
[a2c4445]210 unsigned int x;
211 unsigned int y;
212 unsigned int byte;
213 unsigned int rowbytes;
[c2942d8]214
[a2c4445]215 rowbytes = (helenos_width - 1) / 8 + 1;
[c2942d8]216
[a2c4445]217 for (y = 0; y < helenos_height; y++)
218 for (x = 0; x < helenos_width; x++) {
219 byte = helenos_bits[rowbytes * y + x / 8];
[c2942d8]220 byte >>= x % 8;
221 if (byte & 1)
[a2c4445]222 (*putpixel)(startx + x, starty + y, LOGOCOLOR);
[c2942d8]223 }
224}
225
[95c7526]226/***************************************************************/
227/* Stdout specific functions */
228
[bbf5657]229static void invert_cursor(void)
230{
[a2c4445]231 invert_char(position % columns, position / columns);
[bbf5657]232}
233
234/** Print character to screen
235 *
236 * Emulate basic terminal commands
237 */
238static void fb_putchar(chardev_t *dev, char ch)
239{
240 spinlock_lock(&fb->lock);
[a2c4445]241
242 switch (ch) {
243 case '\n':
244 invert_cursor();
245 position += columns;
246 position -= position % columns;
247 break;
248 case '\r':
249 invert_cursor();
250 position -= position % columns;
251 break;
252 case '\b':
253 invert_cursor();
254 if (position % columns)
255 position--;
256 break;
257 case '\t':
258 invert_cursor();
259 do {
260 draw_char(' ');
261 position++;
262 } while (position % 8);
263 break;
264 default:
265 draw_char(ch);
[95c7526]266 position++;
[bbf5657]267 }
[a2c4445]268
269 if (position >= columns * rows) {
[bbf5657]270 position -= columns;
271 scroll_screen();
272 }
[a2c4445]273
[bbf5657]274 invert_cursor();
[a2c4445]275
[bbf5657]276 spinlock_unlock(&fb->lock);
277}
278
279static chardev_t framebuffer;
280static chardev_operations_t fb_ops = {
281 .write = fb_putchar,
282};
283
284
285/** Initialize framebuffer as a chardev output device
286 *
[a2c4445]287 * @param addr Address of theframebuffer
288 * @param x Screen width in pixels
289 * @param y Screen height in pixels
290 * @param bpp Bits per pixel (8, 16, 24, 32)
291 * @param scan Bytes per one scanline
292 *
[bbf5657]293 */
[a2c4445]294void fb_init(__address addr, unsigned int x, unsigned int y, unsigned int bpp, unsigned int scan)
[bbf5657]295{
[a2c4445]296 switch (bpp) {
297 case 8:
298 putpixel = putpixel_1byte;
299 getpixel = getpixel_1byte;
300 pixelbytes = 1;
301 break;
302 case 16:
303 putpixel = putpixel_2byte;
304 getpixel = getpixel_2byte;
305 pixelbytes = 2;
306 break;
307 case 24:
308 putpixel = putpixel_3byte;
309 getpixel = getpixel_3byte;
310 pixelbytes = 3;
311 break;
312 case 32:
313 putpixel = putpixel_4byte;
314 getpixel = getpixel_4byte;
315 pixelbytes = 4;
316 break;
317 default:
318 panic("Unsupported bpp");
[fcbca14f]319 }
[a2c4445]320
321 fbaddress = (unsigned char *) addr;
[bbf5657]322 xres = x;
323 yres = y;
[a2c4445]324 scanline = scan;
[bbf5657]325
[a2c4445]326 rows = y / FONT_SCANLINES;
327 columns = x / COL_WIDTH;
[bbf5657]328
329 clear_screen();
[a2c4445]330 draw_logo(xres - helenos_width, 0);
[bbf5657]331 invert_cursor();
332
333 chardev_initialize("fb", &framebuffer, &fb_ops);
334 stdout = &framebuffer;
335}
Note: See TracBrowser for help on using the repository browser.