aboutsummaryrefslogtreecommitdiff
path: root/drivers/gfx.c
blob: b781adcdb4adbf423b77a68f6587aa419f62115c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "gfx_font.h"
#include "log.h"
#include "multiboot.h"
#include "panic.h"
#include "gfx.h"

uint8_t *framebuffer = NULL;
uint16_t fb_width;
uint16_t fb_height;

/* this is BYTES per pixel */
uint8_t  fb_bpp;
uint16_t fb_stride;
const uint8_t *gfx_bpp = &fb_bpp;

const uint16_t *gfx_width = &fb_width;
const uint16_t *gfx_height = &fb_height;

static int cursor_x, cursor_y;
uint32_t _gfx_fgcol, _gfx_bgcol;

void (*gfx_clear)(void);
void (*gfx_drawpixel)(int x, int y);

void gfx_set_background(uint32_t col)
{
    _gfx_bgcol = col;
}

uint32_t gfx_get_background(void)
{
    return _gfx_bgcol;
}

void gfx_set_foreground(uint32_t col)
{
    _gfx_fgcol = col;
}

uint32_t gfx_get_foreground(void)
{
    return _gfx_fgcol;
}

/* assembly */
void gfx_drawpixel_test(int x, int y)
{
    ((uint32_t*)framebuffer)[y * fb_stride + x] = _gfx_fgcol;
}
/* implemented in assembly now */
/*
void gfx_clear(uint32_t col)
{
    uint8_t *p = framebuffer;
    uint8_t *stop = framebuffer + fb_width * fb_height * fb_bpp;
    while(p < stop)
    {
        *(uint32_t*)p = col;
        p += fb_bpp;
    }
}
*/

void gfx_clear_unpacked(void)
{
    uint8_t *fb = framebuffer;
    const uint32_t bg = _gfx_bgcol;

    const uint16_t padding = fb_stride - (fb_bpp * fb_width);

    for(int y = 0; y < fb_height; ++y)
    {
        for(int x = 0; x < fb_width; ++x)
        {
            *(uint32_t*)fb++ = bg;
        }
        fb += padding;
    }
}

void gfx_reset(void)
{
    _gfx_fgcol = VGA_RGBPACK(0xff, 0xff, 0xff);
    _gfx_bgcol = VGA_RGBPACK(0, 0, 0);
    gfx_clear();
    cursor_y = 0;
    cursor_x = 0;
}

void gfx_drawchar(int x, int y, int c)
{
    uint8_t *line_addr = framebuffer + (x * fb_bpp) + (y * fb_stride);
    const uint32_t fg = _gfx_fgcol;
    const uint16_t stride = fb_stride;
    const uint8_t stop_y = MIN(FONT_HEIGHT, fb_height - y);
    const uint8_t stop_x = MIN(FONT_WIDTH, fb_width - x);
    if(c < 0 || c > 132)
        return;
    for(int i = 0; i < stop_y; ++i)
    {
        uint8_t mask_table[8] = {128, 64, 32, 16, 8, 4, 2, 1};
        for(int j = 0; j < stop_x; ++j)
        {
            if(gfx_font[c][i] & mask_table[j])
                ((uint32_t*)line_addr)[j] = fg;
        }
        line_addr += stride;
    }
}

void gfx_drawchar_bg(int x, int y, int c)
{
    uint8_t *line_addr = framebuffer + (x * fb_bpp) + (y * fb_stride);
    const uint32_t fg = _gfx_fgcol;
    const uint16_t stride = fb_stride;
    const uint8_t stop_y = MIN(FONT_HEIGHT, fb_height - y);
    const uint8_t stop_x = MIN(FONT_WIDTH, fb_width - x);
    if(c < 0 || c > 132)
        return;
    for(int i = 0; i < stop_y; ++i)
    {
        uint8_t mask_table[8] = {128, 64, 32, 16, 8, 4, 2, 1};
        for(int j = 0; j < stop_x; ++j)
        {
            if(gfx_font[c][i] & mask_table[j])
                ((uint32_t*)line_addr)[j] = fg;
            else
                ((uint32_t*)line_addr)[j] = _gfx_bgcol;
        }
        line_addr += stride;
    }
}

void gfx_putchar(int ch)
{
    if(ch != '\n' && ch != '\b')
    {
        gfx_drawchar(cursor_x, cursor_y, ch);
        cursor_x += FONT_WIDTH;
        if(cursor_x >= fb_width)
        {
            cursor_x = 0;
            cursor_y += FONT_HEIGHT;
            if(cursor_y >= fb_height)
            {
                gfx_clear();
                cursor_y = 0;
            }
        }
    }
    else if(ch == '\n')
    {
        cursor_x = 0;
        cursor_y += FONT_HEIGHT;
        if(cursor_y >= fb_height)
        {
            gfx_clear();
            cursor_y = 0;
        }
    }
    else if(ch == '\b')
    {
        int temp_x = cursor_x - FONT_WIDTH;
        if(temp_x < 0)
        {
            cursor_x = 0;
            int temp_y = cursor_y - FONT_HEIGHT;
            cursor_y = (temp_y < 0) ? 0 : temp_y;
        }
        else
        {
            cursor_x = temp_x;
        }
        gfx_drawchar_bg(cursor_x, cursor_y, ' ');
    }
}

void gfx_puts(const char* str)
{
    while(*str)
    {
        gfx_putchar(*str++);
    }
}

/* implemented in assembly now */
#if 0
void gfx_hline(int x1, int x2, int y)
{
    /* make sure x1 is to the left of x2 */
    if(x2 < x1)
    {
        int temp = x1;
        x1 = x2;
        x2 = temp;
    }

    uint8_t *base = framebuffer + y * fb_stride;

    uint8_t *dest = base + x1 * fb_bpp;
    uint8_t *stop = base + x2 * fb_bpp;
    const uint32_t col = _gfx_fgcol;
    while(dest < stop)
    {
        *(uint32_t*)dest = col;
        dest += fb_bpp;
    }
}

void gfx_vline(int y1, int y2, int x)
{
    /* make sure y1 is above y2 */
    if(y2 < y1)
    {
        int temp = y1;
        y1 = y2;
        y2 = temp;
    }

    uint8_t *dest = framebuffer + y1 * fb_stride + x * fb_bpp;
    uint8_t *stop = framebuffer + y2 * fb_stride + x * fb_bpp;
    const uint32_t col = _gfx_fgcol;
    const uint16_t stride = fb_stride;
    while(dest < stop)
    {
        *(uint32_t*)dest = col;
        dest += stride;
    }
}
#endif

void gfx_fillrect(int x, int y, int w, int h)
{
    for(int i = 0; i < h; ++i)
    {
        gfx_hline(x, x + w, y + i);
    }
}

void gfx_drawline(int x1, int y1, int x2, int y2)
{
    int dx =  abs(x2 - x1);
    int sx =  x1 < x2 ? 1 : -1;
    int dy = -abs(y2 - y1);
    int sy =  y1 < y2 ? 1 : -1;
    int err = dx + dy;
    int e2; /* error value e_xy */

    while(1)
    {
        gfx_drawpixel(x1, y1);
        if (x1 == x2 && y1 == y2)
            break;
        e2 = err << 1;
        if (e2 >= dy)
        {
            err += dy;
            x1 += sx;
        }
        if (e2 <= dx)
        {
            err += dx;
            y1 += sy;
        }
    }
}

bool gfx_init(struct vbe_info_t *vbe_mode_info)
{
    framebuffer = (uint8_t*)vbe_mode_info->physbase;
    fb_width = vbe_mode_info->Xres;
    fb_height = vbe_mode_info->Yres;
    fb_bpp = vbe_mode_info->bpp / 8;
    fb_stride = vbe_mode_info->pitch;
    if(fb_bpp != 4)
    {
        printf("WARNING: BPP != 32, falling back to text mode...\n");
        return false;
    }
    else
    {
        extern void gfx_drawpixel_32bpp(int, int);
        gfx_drawpixel = &gfx_drawpixel_32bpp;
    }

    set_putchar(gfx_putchar);
    set_puts(gfx_puts);

    /* A bit of fragile code here... don't call gfx_reset() before setting gfx_clear! */

    if(fb_stride != fb_bpp * fb_width)
    {
        gfx_clear = &gfx_clear_unpacked;
        gfx_reset();
        printf("WARNING: Internal framebuffer padding detected, support is experimental.\n");
    }
    else
    {
        /* assembly */
        extern void gfx_clear_packed(void);
        gfx_clear = &gfx_clear_packed;
        gfx_reset();
    }

    return true;
}