Project

General

Profile

Statistics
| Revision:

root / proj / graph.c @ 168

History | View | Annotate | Download (8.85 KB)

1
#include "graph.h"
2
#include "graph_macros.h"
3
#include "errors.h"
4

    
5
#include <lcom/lcf.h>
6

    
7
#include <stdio.h>
8

    
9
static void *video_mem = NULL; /** @brief Frame-buffer VM address. */
10
static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */
11
static vbe_mode_info_t vbe_mem_info;
12
static mmap_t mem_map;
13

    
14
int (get_permission)(unsigned int base_addr, unsigned int size) {
15
    struct minix_mem_range mmr;
16
    mmr.mr_base = base_addr;
17
    mmr.mr_limit = base_addr + size;
18
    return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
19
}
20

    
21
//int (get_permissions_first_mbyte)(void) {
22
//    return get_permission(MBYTE_BASE, MBYTE_SIZE);
23
//}
24

    
25
int (graph_init)(uint16_t mode){
26
    if (vbe_get_mode_information(mode)) {
27
        printf("%s: failed to get information for mode %x.\n", __func__, mode);
28
        return 1;
29
    }
30

    
31
    graph_map_vram(); // if function fails it aborts program
32

    
33
    if (graph_set_mode(mode)) {
34
        printf("%s: failed to set graphic mode %x.\n", __func__, mode);
35
        return 1;
36
    };
37
    return SUCCESS;
38
}
39

    
40
int (graph_cleanup)(void){
41
    int r = SUCCESS;
42
    if ((r = vg_exit()))
43
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
44
    if ((r = graph_free_memory()))
45
        printf("%s: lm_free failed\n", __func__);
46
    return r;
47
}
48

    
49
int (vbe_get_mode_information)(uint16_t mode) {
50
    memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values
51

    
52
    struct reg86 reg_86;
53
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
54

    
55
    vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
56

    
57
    reg_86.intno = VC_BIOS_SERV;
58
    reg_86.ah = VBE_CALL;
59
    reg_86.al = VBE_MD_INFO;
60
    reg_86.cx = mode;
61
    reg_86.es = PB2BASE(mem_map.phys);
62
    reg_86.di = PB2OFF(mem_map.phys);
63
    // BIOS CALL
64
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
65
        printf("%s: sys_int86 failed\n", __func__);
66
        if (graph_free_memory()) {
67
            printf("%s: lm_free failed\n", __func__);
68
        }
69
        return BIOS_CALL_ERROR;
70
    }
71

    
72
    memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size);
73
    return SUCCESS;
74
}
75

    
76
int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
77
    memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
78

    
79
    mmap_t controller_map;
80

    
81
    struct reg86 reg_86;
82
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
83

    
84
    VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map);
85

    
86
    uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
87

    
88
    virtual_addr->VbeSignature[0] = 'V';
89
    virtual_addr->VbeSignature[1] = 'B';
90
    virtual_addr->VbeSignature[2] = 'E';
91
    virtual_addr->VbeSignature[3] = '2';
92

    
93

    
94
    reg_86.intno = VC_BIOS_SERV;
95
    reg_86.ah = VBE_CALL;
96
    reg_86.al = VBE_CTRL_INFO;
97
    reg_86.es = PB2BASE(controller_map.phys);
98
    reg_86.di = PB2OFF(controller_map.phys);
99
    // BIOS CALL
100
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
101
        printf("%s: sys_int86 failed\n", __func__);
102
        if (!lm_free(&controller_map)) {
103
            printf("%s: lm_free failed\n", __func__);
104
        }
105
        return BIOS_CALL_ERROR;
106
    }
107

    
108
    info_p->VBESignature[0] = virtual_addr->VbeSignature[0];
109
    info_p->VBESignature[1] = virtual_addr->VbeSignature[1];
110
    info_p->VBESignature[2] = virtual_addr->VbeSignature[2];
111
    info_p->VBESignature[3] = virtual_addr->VbeSignature[3];
112

    
113
    uint8_t lsb, msb;
114
    util_get_LSB(virtual_addr->VbeVersion, &lsb);
115
    util_get_MSB(virtual_addr->VbeVersion, &msb);
116
    info_p->VBEVersion[0] = lsb;
117
    info_p->VBEVersion[1] = msb;
118

    
119
    info_p->TotalMemory = (virtual_addr->TotalMemory << 6);
120

    
121
    // Convert Far Far Pointer to Virtual Address
122

    
123
    uint32_t phys_ptr = FAR2PHYS(virtual_addr->OemStringPtr);
124
    uint32_t virtual_ptr = phys_ptr + virtual_base;
125
    info_p->OEMString = (char*)(virtual_ptr);
126

    
127
    phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr);
128
    virtual_ptr = phys_ptr + virtual_base;
129
    info_p->VideoModeList = (uint16_t*)(virtual_ptr);
130

    
131
    phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr);
132
    virtual_ptr = phys_ptr + virtual_base;
133
    info_p->OEMVendorNamePtr = (char*)(virtual_ptr);
134

    
135
    phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr);
136
    virtual_ptr = phys_ptr + virtual_base;
137
    info_p->OEMProductNamePtr = (char*)(virtual_ptr);
138

    
139
    phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr);
140
    virtual_ptr = phys_ptr + virtual_base;
141
    info_p->OEMProductRevPtr = (char*)(virtual_ptr);
142

    
143
    if (!lm_free(&controller_map)) {
144
        printf("%s: lm_free failed\n", __func__);
145
        return LCF_ERROR;
146
    }
147

    
148
    return SUCCESS;
149
}
150

    
151
phys_bytes (graph_get_phys_addr)    (void){ return vbe_mem_info.PhysBasePtr; }
152
unsigned   (graph_get_vram_size)    (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); }
153
uint16_t   (graph_get_XRes)         (void){ return vbe_mem_info.XResolution; }
154
uint16_t   (graph_get_YRes)         (void){ return vbe_mem_info.YResolution; }
155
uint16_t   (graph_get_bits_pixel)   (void){ return vbe_mem_info.BitsPerPixel; }
156
uint16_t   (graph_get_bytes_pixel)  (void){ return (vbe_mem_info.BitsPerPixel + 7) >> 3; }
157
uint16_t   (graph_get_RedMaskSize)  (void){ return vbe_mem_info.RedMaskSize  ; }
158
uint16_t   (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
159
uint16_t   (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
160

    
161
int (graph_map_vram)(void) {
162
    int r;
163
    const unsigned vram_base = graph_get_phys_addr();
164
    const unsigned vram_size = graph_get_vram_size();
165
    if ((r = get_permission(vram_base, vram_size))) {
166
        if (graph_free_memory()) {
167
            printf("%s: lm_free failed\n", __func__);
168
        }
169
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
170
    }
171

    
172
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
173

    
174
    if (video_mem == MAP_FAILED) {
175
        if (graph_free_memory()) {
176
            printf("%s: lm_free failed\n", __func__);
177
        }
178
        panic("%s: couldn't map video memory.", __func__);
179
    }
180

    
181
    video_buf = malloc(vram_size);
182

    
183
    return SUCCESS;
184
}
185

    
186
int (graph_free_memory)(void) {
187
    int r = SUCCESS;
188
    free(video_buf); video_buf = NULL;
189
    r = !lm_free(&mem_map);
190
    return r;
191
}
192

    
193
int (graph_set_mode)(uint16_t mode) {
194
    struct reg86 reg_86;
195

    
196
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
197

    
198
    // Set Reg86
199
    reg_86.intno = VC_BIOS_SERV;
200
    reg_86.ah = VBE_CALL;
201
    reg_86.al = SET_VBE_MD;
202
    reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
203

    
204
    // BIOS CALL
205
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
206
        printf("%s: sys_int86 failed\n", __func__);
207
        return BIOS_CALL_ERROR;
208
    }
209

    
210
    return SUCCESS;
211
}
212

    
213
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
214
    if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
215
        printf("%s: invalid pixel.\n", __func__);
216
        return OUT_OF_RANGE;
217
    }
218
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
219
    memcpy(video_buf + pos, &color, graph_get_bytes_pixel());
220
    return SUCCESS;
221
}
222
int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha){
223
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
224
    uint32_t color_;
225
    memcpy(&color_, video_buf + pos, graph_get_bytes_pixel());
226
    float a = 1.0-(alpha&0xFF)/(float)0xFF;
227
    uint8_t r = GET_RED(color)*a + GET_RED(color_)*(1.0-a);
228
    uint8_t g = GET_GRE(color)*a + GET_GRE(color_)*(1.0-a);
229
    uint8_t b = GET_BLU(color)*a + GET_BLU(color_)*(1.0-a);
230
    return graph_set_pixel(x,y,SET_RGB(r,g,b));
231
    //return set_pixel(x,y,color);
232
}
233

    
234
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
235
    int r;
236
    for (uint16_t i = 0; i < len; i++)
237
        if ((r = graph_set_pixel(x + i, y, color))) return r;
238
    return SUCCESS;
239
}
240
int (vg_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
241
    return graph_draw_hline(x,y,len,color);
242
}
243

    
244
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color)        {
245
    int r;
246
    for (uint16_t i = 0; i < height; i++)
247
        if ((r = graph_draw_hline(x, y + i, width, color))) return r;
248
    return SUCCESS;
249
}
250
int (vg_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color){
251
    return graph_draw_rectangle(x,y,width,height, color);
252
}
253

    
254
int (graph_paint_screen)(uint32_t color){
255
    return graph_draw_rectangle(0,0,graph_get_XRes(),graph_get_YRes(),color);
256
}
257

    
258
int (graph_clear_screen)(void){
259
    //return graph_paint_screen(BLACK);
260
    memset(video_buf, 0, graph_get_vram_size());
261
    return SUCCESS;
262
}
263

    
264
int (graph_draw)(void){
265
    memcpy((uint8_t*)video_mem, video_buf, graph_get_vram_size());
266
    return 0;
267
}