Project

General

Profile

Statistics
| Revision:

root / proj / graphics.c @ 165

History | View | Annotate | Download (7.92 KB)

1
#include "graphics.h"
2
#include "graphics_macros.h"
3
#include "errors.h"
4

    
5
#include <lcom/lcf.h>
6

    
7
#include <stdio.h>
8

    
9
static void *video_mem; /** @brief Frame-buffer VM address */
10
static vbe_mode_info_t vbe_mem_info;
11
static mmap_t mem_map;
12

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

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

    
24
int (vbe_get_mode_information)(uint16_t mode) {
25
    memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values
26

    
27
    struct reg86 reg_86;
28
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
29

    
30
    vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
31

    
32
    reg_86.intno = VC_BIOS_SERV;
33
    reg_86.ah = VBE_CALL;
34
    reg_86.al = VBE_MD_INFO;
35
    reg_86.cx = mode;
36
    reg_86.es = PB2BASE(mem_map.phys);
37
    reg_86.di = PB2OFF(mem_map.phys);
38
    // BIOS CALL
39
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
40
        printf("%s: sys_int86 failed\n", __func__);
41
        if (graph_free_memory_map()) {
42
            printf("%s: lm_free failed\n", __func__);
43
        }
44
        return BIOS_CALL_ERROR;
45
    }
46

    
47
    memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size);
48
    return SUCCESS;
49
}
50

    
51
int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
52
    memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
53

    
54
    mmap_t controller_map;
55

    
56
    struct reg86 reg_86;
57
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
58

    
59
    VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map);
60

    
61
    uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
62

    
63
    virtual_addr->VbeSignature[0] = 'V';
64
    virtual_addr->VbeSignature[1] = 'B';
65
    virtual_addr->VbeSignature[2] = 'E';
66
    virtual_addr->VbeSignature[3] = '2';
67

    
68

    
69
    reg_86.intno = VC_BIOS_SERV;
70
    reg_86.ah = VBE_CALL;
71
    reg_86.al = VBE_CTRL_INFO;
72
    reg_86.es = PB2BASE(controller_map.phys);
73
    reg_86.di = PB2OFF(controller_map.phys);
74
    // BIOS CALL
75
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
76
        printf("%s: sys_int86 failed\n", __func__);
77
        if (!lm_free(&controller_map)) {
78
            printf("%s: lm_free failed\n", __func__);
79
        }
80
        return BIOS_CALL_ERROR;
81
    }
82

    
83
    info_p->VBESignature[0] = virtual_addr->VbeSignature[0];
84
    info_p->VBESignature[1] = virtual_addr->VbeSignature[1];
85
    info_p->VBESignature[2] = virtual_addr->VbeSignature[2];
86
    info_p->VBESignature[3] = virtual_addr->VbeSignature[3];
87

    
88
    uint8_t lsb, msb;
89
    util_get_LSB(virtual_addr->VbeVersion, &lsb);
90
    util_get_MSB(virtual_addr->VbeVersion, &msb);
91
    info_p->VBEVersion[0] = lsb;
92
    info_p->VBEVersion[1] = msb;
93

    
94
    info_p->TotalMemory = (virtual_addr->TotalMemory << 6);
95

    
96
    // Convert Far Far Pointer to Virtual Address
97

    
98
    uint32_t phys_ptr = FAR2PHYS(virtual_addr->OemStringPtr);
99
    uint32_t virtual_ptr = phys_ptr + virtual_base;
100
    info_p->OEMString = (char*)(virtual_ptr);
101

    
102
    phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr);
103
    virtual_ptr = phys_ptr + virtual_base;
104
    info_p->VideoModeList = (uint16_t*)(virtual_ptr);
105

    
106
    phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr);
107
    virtual_ptr = phys_ptr + virtual_base;
108
    info_p->OEMVendorNamePtr = (char*)(virtual_ptr);
109

    
110
    phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr);
111
    virtual_ptr = phys_ptr + virtual_base;
112
    info_p->OEMProductNamePtr = (char*)(virtual_ptr);
113

    
114
    phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr);
115
    virtual_ptr = phys_ptr + virtual_base;
116
    info_p->OEMProductRevPtr = (char*)(virtual_ptr);
117

    
118
    if (!lm_free(&controller_map)) {
119
        printf("%s: lm_free failed\n", __func__);
120
        return LCF_ERROR;
121
    }
122

    
123
    return SUCCESS;
124
}
125

    
126
phys_bytes (graph_get_phys_addr)    (void){ return vbe_mem_info.PhysBasePtr; }
127
unsigned   (graph_get_vram_size)    (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * ((vbe_mem_info.BitsPerPixel + 7) >> 3); }
128
uint16_t   (graph_get_XRes)         (void){ return vbe_mem_info.XResolution; }
129
uint16_t   (graph_get_YRes)         (void){ return vbe_mem_info.YResolution; }
130
uint16_t   (graph_get_bits_pixel)   (void){ return vbe_mem_info.BitsPerPixel; }
131
uint16_t   (graph_get_bytes_pixel)  (void){ return (vbe_mem_info.BitsPerPixel + 7) >> 3; }
132
uint16_t   (graph_get_RedMaskSize)  (void){ return vbe_mem_info.RedMaskSize  ; }
133
uint16_t   (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
134
uint16_t   (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
135

    
136
int (graph_map_vram)(void) {
137
    int r;
138
    unsigned int vram_base = graph_get_phys_addr();
139
    unsigned int vram_size = graph_get_vram_size();
140
    if ((r = get_permission(vram_base, vram_size))) {
141
        if (graph_free_memory_map()) {
142
            printf("%s: lm_free failed\n", __func__);
143
        }
144
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
145
    }
146

    
147
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
148

    
149
    if (video_mem == MAP_FAILED) {
150
        if (graph_free_memory_map()) {
151
            printf("%s: lm_free failed\n", __func__);
152
        }
153
        panic("%s: couldn't map video memory.", __func__);
154
    }
155

    
156
    return SUCCESS;
157
}
158

    
159
int (graph_free_memory_map)(void) {
160
    return !lm_free(&mem_map);
161
}
162

    
163
int (graph_set_mode)(uint16_t mode) {
164
    struct reg86 reg_86;
165

    
166
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
167

    
168
    // Set Reg86
169
    reg_86.intno = VC_BIOS_SERV;
170
    reg_86.ah = VBE_CALL;
171
    reg_86.al = SET_VBE_MD;
172
    reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD;
173

    
174
    // BIOS CALL
175
    if (sys_int86(&reg_86) || reg_86.ah != AH_SUCCESS) {
176
        printf("%s: sys_int86 failed\n", __func__);
177
        return BIOS_CALL_ERROR;
178
    }
179

    
180
    return SUCCESS;
181
}
182

    
183
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
184
    if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
185
        printf("%s: invalid pixel.\n", __func__);
186
        return OUT_OF_RANGE;
187
    }
188
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
189
    memcpy((void*)((unsigned int)video_mem + pos), &color, graph_get_bytes_pixel());
190
    return SUCCESS;
191
}
192
int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha){
193
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
194
    uint32_t color_;
195
    memcpy(&color_, (void*)((unsigned int)video_mem + pos), graph_get_bytes_pixel());
196
    float a = 1.0-(alpha&0xFF)/(float)0xFF;
197
    uint8_t r = GET_RED(color)*a + GET_RED(color_)*(1.0-a);
198
    uint8_t g = GET_GRE(color)*a + GET_GRE(color_)*(1.0-a);
199
    uint8_t b = GET_BLU(color)*a + GET_BLU(color_)*(1.0-a);
200
    return graph_set_pixel(x,y,SET_RGB(r,g,b));
201
    //return set_pixel(x,y,color);
202
}
203

    
204
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
205
    int r;
206
    for (uint16_t i = 0; i < len; i++)
207
        if ((r = graph_set_pixel(x + i, y, color))) return r;
208
    return SUCCESS;
209
}
210
int (vg_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
211
    return graph_draw_hline(x,y,len,color);
212
}
213

    
214
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color)        {
215
    int r;
216
    for (uint16_t i = 0; i < height; i++)
217
        if ((r = graph_draw_hline(x, y + i, width, color))) return r;
218
    return SUCCESS;
219
}
220
int (vg_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color){
221
    return graph_draw_rectangle(x,y,width,height, color);
222
}
223

    
224
int (graph_paint_screen)(uint32_t color){
225
    return graph_draw_rectangle(0,0,graph_get_XRes(),graph_get_YRes(),color);
226
}
227

    
228
int (graph_clear_screen)(void){
229
    return graph_paint_screen(BLACK);
230
}
231

    
232
int (graph_draw)(void){
233
    return 0;
234
}