root / proj / src / graph.c @ 185
History | View | Annotate | Download (10.4 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "graph.h" |
4 |
|
5 |
#include "errors.h" |
6 |
#include <stdio.h> |
7 |
|
8 |
/// MACROS
|
9 |
#define FAR2PHYS(n) ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF)) |
10 |
|
11 |
/// STRUCT
|
12 |
typedef struct __attribute__((packed)) { |
13 |
|
14 |
char VbeSignature[4] ; |
15 |
uint16_t VbeVersion ; |
16 |
uint32_t OemStringPtr ; |
17 |
uint8_t Capabilities[4] ;
|
18 |
uint32_t VideoModePtr ; |
19 |
uint16_t TotalMemory ; |
20 |
|
21 |
uint16_t OemSoftwareRev ; |
22 |
uint32_t OemVendorNamePtr ; |
23 |
uint32_t OemProductNamePtr ; |
24 |
uint32_t OemProductRevPtr ; |
25 |
char Reserved[222] ; |
26 |
|
27 |
char OemData[256] ; |
28 |
} VbeInfoBlock; |
29 |
|
30 |
static vbe_mode_info_t vbe_mem_info;
|
31 |
|
32 |
/// PUBLIC GET
|
33 |
uint16_t (graph_get_XRes) (void){ return vbe_mem_info.XResolution; } |
34 |
uint16_t (graph_get_YRes) (void){ return vbe_mem_info.YResolution; } |
35 |
|
36 |
/// PRIVATE GET
|
37 |
static uint16_t (graph_get_bits_pixel) (void){ return vbe_mem_info.BitsPerPixel; } |
38 |
static uint16_t (graph_get_bytes_pixel) (void){ return (graph_get_bits_pixel() + 7) >> 3; } |
39 |
static phys_bytes (graph_get_phys_addr) (void){ return vbe_mem_info.PhysBasePtr; } |
40 |
static unsigned (graph_get_vram_size) (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); } |
41 |
//static uint16_t (graph_get_RedMaskSize) (void){ return vbe_mem_info.RedMaskSize ; }
|
42 |
//static uint16_t (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
|
43 |
//static uint16_t (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
|
44 |
|
45 |
static int (get_permission)(unsigned int base_addr, unsigned int size) { |
46 |
struct minix_mem_range mmr;
|
47 |
mmr.mr_base = base_addr; |
48 |
mmr.mr_limit = base_addr + size; |
49 |
return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
|
50 |
} |
51 |
|
52 |
//static int (get_permissions_first_mbyte)(void) {
|
53 |
// return get_permission(MBYTE_BASE, MBYTE_SIZE);
|
54 |
//}
|
55 |
|
56 |
/// MEMORY
|
57 |
static uint8_t *video_mem = NULL; /** @brief Frame-buffer VM address. */ |
58 |
static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */ |
59 |
static mmap_t mem_map;
|
60 |
static int (graph_free_memory)(void) { |
61 |
int r = SUCCESS;
|
62 |
free(video_buf); video_buf = NULL;
|
63 |
r = !lm_free(&mem_map); |
64 |
return r;
|
65 |
} |
66 |
static int (graph_map_vram)(void) { |
67 |
int r;
|
68 |
const unsigned vram_base = graph_get_phys_addr(); |
69 |
const unsigned vram_size = graph_get_vram_size(); |
70 |
if ((r = get_permission(vram_base, vram_size))) {
|
71 |
if (graph_free_memory()) {
|
72 |
printf("%s: lm_free failed\n", __func__);
|
73 |
} |
74 |
panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
|
75 |
} |
76 |
|
77 |
video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
|
78 |
|
79 |
if (video_mem == MAP_FAILED) {
|
80 |
if (graph_free_memory()) {
|
81 |
printf("%s: lm_free failed\n", __func__);
|
82 |
} |
83 |
panic("%s: couldn't map video memory.", __func__);
|
84 |
} |
85 |
|
86 |
video_buf = malloc(vram_size); |
87 |
|
88 |
return SUCCESS;
|
89 |
} |
90 |
|
91 |
/// INFO GET
|
92 |
static int (vbe_get_mode_information)(uint16_t mode) { |
93 |
memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values |
94 |
|
95 |
struct reg86 reg_86;
|
96 |
memset(®_86, 0, sizeof(struct reg86)); // reset struct |
97 |
|
98 |
vbe_mode_info_t *virtual_addr = lm_alloc(sizeof(vbe_mode_info_t), &mem_map);
|
99 |
|
100 |
reg_86.intno = VC_BIOS_SERV; |
101 |
reg_86.ah = VBE_CALL; |
102 |
reg_86.al = VBE_MD_INFO; |
103 |
reg_86.cx = mode; |
104 |
reg_86.es = PB2BASE(mem_map.phys); |
105 |
reg_86.di = PB2OFF(mem_map.phys); |
106 |
// BIOS CALL
|
107 |
if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) {
|
108 |
printf("%s: sys_int86 failed\n", __func__);
|
109 |
if (graph_free_memory()) {
|
110 |
printf("%s: lm_free failed\n", __func__);
|
111 |
} |
112 |
return BIOS_CALL_ERROR;
|
113 |
} |
114 |
|
115 |
memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size); |
116 |
return SUCCESS;
|
117 |
} |
118 |
/*
|
119 |
static int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
|
120 |
memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
|
121 |
|
122 |
mmap_t controller_map;
|
123 |
|
124 |
struct reg86 reg_86;
|
125 |
memset(®_86, 0, sizeof(struct reg86)); // reset struct
|
126 |
|
127 |
VbeInfoBlock *virtual_addr = lm_alloc(sizeof(VbeInfoBlock), &controller_map);
|
128 |
|
129 |
uint32_t virtual_base = (uint32_t)(virtual_addr) - controller_map.phys;
|
130 |
|
131 |
virtual_addr->VbeSignature[0] = 'V';
|
132 |
virtual_addr->VbeSignature[1] = 'B';
|
133 |
virtual_addr->VbeSignature[2] = 'E';
|
134 |
virtual_addr->VbeSignature[3] = '2';
|
135 |
|
136 |
|
137 |
reg_86.intno = VC_BIOS_SERV;
|
138 |
reg_86.ah = VBE_CALL;
|
139 |
reg_86.al = VBE_CTRL_INFO;
|
140 |
reg_86.es = PB2BASE(controller_map.phys);
|
141 |
reg_86.di = PB2OFF(controller_map.phys);
|
142 |
// BIOS CALL
|
143 |
if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) {
|
144 |
printf("%s: sys_int86 failed\n", __func__);
|
145 |
if (!lm_free(&controller_map)) {
|
146 |
printf("%s: lm_free failed\n", __func__);
|
147 |
}
|
148 |
return BIOS_CALL_ERROR;
|
149 |
}
|
150 |
|
151 |
info_p->VBESignature[0] = virtual_addr->VbeSignature[0];
|
152 |
info_p->VBESignature[1] = virtual_addr->VbeSignature[1];
|
153 |
info_p->VBESignature[2] = virtual_addr->VbeSignature[2];
|
154 |
info_p->VBESignature[3] = virtual_addr->VbeSignature[3];
|
155 |
|
156 |
uint8_t lsb, msb;
|
157 |
util_get_LSB(virtual_addr->VbeVersion, &lsb);
|
158 |
util_get_MSB(virtual_addr->VbeVersion, &msb);
|
159 |
info_p->VBEVersion[0] = lsb;
|
160 |
info_p->VBEVersion[1] = msb;
|
161 |
|
162 |
info_p->TotalMemory = (virtual_addr->TotalMemory << 6);
|
163 |
|
164 |
// Convert Far Far Pointer to Virtual Address
|
165 |
|
166 |
uint32_t phys_ptr = FAR2PHYS(virtual_addr->OemStringPtr);
|
167 |
uint32_t virtual_ptr = phys_ptr + virtual_base;
|
168 |
info_p->OEMString = (char*)(virtual_ptr);
|
169 |
|
170 |
phys_ptr = FAR2PHYS(virtual_addr->VideoModePtr);
|
171 |
virtual_ptr = phys_ptr + virtual_base;
|
172 |
info_p->VideoModeList = (uint16_t*)(virtual_ptr);
|
173 |
|
174 |
phys_ptr = FAR2PHYS(virtual_addr->OemVendorNamePtr);
|
175 |
virtual_ptr = phys_ptr + virtual_base;
|
176 |
info_p->OEMVendorNamePtr = (char*)(virtual_ptr);
|
177 |
|
178 |
phys_ptr = FAR2PHYS(virtual_addr->OemProductNamePtr);
|
179 |
virtual_ptr = phys_ptr + virtual_base;
|
180 |
info_p->OEMProductNamePtr = (char*)(virtual_ptr);
|
181 |
|
182 |
phys_ptr = FAR2PHYS(virtual_addr->OemProductRevPtr);
|
183 |
virtual_ptr = phys_ptr + virtual_base;
|
184 |
info_p->OEMProductRevPtr = (char*)(virtual_ptr);
|
185 |
|
186 |
if (!lm_free(&controller_map)) {
|
187 |
printf("%s: lm_free failed\n", __func__);
|
188 |
return LCF_ERROR;
|
189 |
}
|
190 |
|
191 |
return SUCCESS;
|
192 |
}
|
193 |
*/
|
194 |
|
195 |
/// INIT
|
196 |
/**
|
197 |
* @brief
|
198 |
* @param mode
|
199 |
* @return
|
200 |
*/
|
201 |
static int (graph_set_mode)(uint16_t mode) { |
202 |
struct reg86 reg_86;
|
203 |
|
204 |
memset(®_86, 0, sizeof(struct reg86)); // reset struct |
205 |
|
206 |
// Set Reg86
|
207 |
reg_86.intno = VC_BIOS_SERV; |
208 |
reg_86.ah = VBE_CALL; |
209 |
reg_86.al = SET_VBE_MD; |
210 |
reg_86.bx = mode | LINEAR_FRAME_BUFFER_MD; |
211 |
|
212 |
// BIOS CALL
|
213 |
if (sys_int86(®_86) || reg_86.ah != AH_SUCCESS) {
|
214 |
printf("%s: sys_int86 failed\n", __func__);
|
215 |
return BIOS_CALL_ERROR;
|
216 |
} |
217 |
|
218 |
return SUCCESS;
|
219 |
} |
220 |
int (graph_init)(uint16_t mode){
|
221 |
if (vbe_get_mode_information(mode)) {
|
222 |
printf("%s: failed to get information for mode %x.\n", __func__, mode);
|
223 |
return 1; |
224 |
} |
225 |
|
226 |
graph_map_vram(); // if function fails it aborts program
|
227 |
|
228 |
if (graph_set_mode(mode)) {
|
229 |
printf("%s: failed to set graphic mode %x.\n", __func__, mode);
|
230 |
return 1; |
231 |
}; |
232 |
return SUCCESS;
|
233 |
} |
234 |
|
235 |
/// CLEANUP
|
236 |
int (graph_cleanup)(void){ |
237 |
int r = SUCCESS;
|
238 |
if ((r = vg_exit()))
|
239 |
printf("%s: vg_exit failed to exit to text mode.\n", __func__);
|
240 |
if ((r = graph_free_memory()))
|
241 |
printf("%s: lm_free failed\n", __func__);
|
242 |
return r;
|
243 |
} |
244 |
|
245 |
/// PIXEL DRAWING
|
246 |
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
|
247 |
/*
|
248 |
if (x < 0 || vbe_mem_info.XResolution <= x || y < 0 || vbe_mem_info.YResolution <= y) {
|
249 |
//printf("%s: invalid pixel.\n", __func__);
|
250 |
return OUT_OF_RANGE;
|
251 |
}
|
252 |
unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
|
253 |
memcpy(video_buf + pos, &color, graph_get_bytes_pixel());
|
254 |
return SUCCESS;
|
255 |
*/
|
256 |
return graph_set_pixel_buffer(x, y, color, video_buf, graph_get_XRes(), graph_get_YRes());
|
257 |
} |
258 |
int (graph_set_pixel_buffer)(uint16_t x, uint16_t y, uint32_t color, uint8_t *buf, uint16_t W, uint16_t H) {
|
259 |
if (x < 0 || W <= x || y < 0 || H <= y) { |
260 |
//printf("%s: invalid pixel.\n", __func__);
|
261 |
return OUT_OF_RANGE;
|
262 |
} |
263 |
unsigned int pos = (x + y * W) * graph_get_bytes_pixel(); |
264 |
memcpy(buf + pos, &color, graph_get_bytes_pixel()); |
265 |
return SUCCESS;
|
266 |
} |
267 |
int (graph_set_pixel_alpha_buffer)(uint16_t x, uint16_t y, uint8_t alpha, uint8_t *alp_buf, uint16_t W, uint16_t H) {
|
268 |
if (x < 0 || W <= x || y < 0 || H <= y) { |
269 |
//printf("%s: invalid pixel.\n", __func__);
|
270 |
return OUT_OF_RANGE;
|
271 |
} |
272 |
unsigned int pos = x + y * W; |
273 |
memcpy(alp_buf + pos, &alpha, 1);
|
274 |
return SUCCESS;
|
275 |
} |
276 |
int (graph_set_pixel_alpha)(uint16_t x, uint16_t y, uint32_t color, uint8_t alpha){
|
277 |
if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
|
278 |
//printf("%s: invalid pixel.\n", __func__);
|
279 |
return OUT_OF_RANGE;
|
280 |
} |
281 |
//printf("COLOR= %X, ALPHA = %X\n", color, alpha);
|
282 |
unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel(); |
283 |
uint32_t color_; |
284 |
memcpy(&color_, video_buf + pos, graph_get_bytes_pixel()); |
285 |
float a = 1.0-(alpha&0xFF)/(float)0xFF; |
286 |
uint8_t r = GET_RED(color)*a + GET_RED(color_)*(1.0-a); |
287 |
uint8_t g = GET_GRE(color)*a + GET_GRE(color_)*(1.0-a); |
288 |
uint8_t b = GET_BLU(color)*a + GET_BLU(color_)*(1.0-a); |
289 |
return graph_set_pixel(x,y,SET_RGB(r,g,b));
|
290 |
//return set_pixel(x,y,color);
|
291 |
} |
292 |
|
293 |
int (graph_clear_screen)(void){ |
294 |
//return graph_paint_screen(BLACK);
|
295 |
memset(video_buf, 0, graph_get_vram_size());
|
296 |
return SUCCESS;
|
297 |
} |
298 |
|
299 |
int (graph_draw)(void){ |
300 |
memcpy(video_mem, video_buf, graph_get_vram_size()); |
301 |
return 0; |
302 |
} |
303 |
|
304 |
/// RECTANGLE
|
305 |
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
|
306 |
int r;
|
307 |
for (uint16_t i = 0; i < len; i++) |
308 |
if ((r = graph_set_pixel(x + i, y, color))) return r; |
309 |
return SUCCESS;
|
310 |
} |
311 |
|
312 |
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color) {
|
313 |
int r;
|
314 |
for (uint16_t i = 0; i < height; i++) |
315 |
if ((r = graph_draw_hline(x, y + i, width, color))) return r; |
316 |
return SUCCESS;
|
317 |
} |