Project

General

Profile

Revision 174

refactored graph

View differences:

graph.c
6 6

  
7 7
#include <stdio.h>
8 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. */
9
/// MACROS
10
#define FAR2PHYS(n)         ((((n)>>12) & 0xFFFFFFF0) + ((n) & 0x0000FFFF))
11

  
12
/// STRUCT
13
typedef struct __attribute__((packed)) {
14

  
15
    char        VbeSignature[4]     ;
16
    uint16_t    VbeVersion          ;
17
    uint32_t    OemStringPtr        ;
18
    uint8_t     Capabilities[4]     ;
19
    uint32_t    VideoModePtr        ;
20
    uint16_t    TotalMemory         ;
21

  
22
    uint16_t    OemSoftwareRev      ;
23
    uint32_t    OemVendorNamePtr    ;
24
    uint32_t    OemProductNamePtr   ;
25
    uint32_t    OemProductRevPtr    ;
26
    char        Reserved[222]       ;
27

  
28
    char        OemData[256]        ;
29
} VbeInfoBlock;
30

  
11 31
static vbe_mode_info_t vbe_mem_info;
12
static mmap_t mem_map;
13 32

  
14
int (get_permission)(unsigned int base_addr, unsigned int size) {
33
/// PUBLIC GET
34
uint16_t   (graph_get_XRes)         (void){ return vbe_mem_info.XResolution; }
35
uint16_t   (graph_get_YRes)         (void){ return vbe_mem_info.YResolution; }
36

  
37
/// PRIVATE GET
38
static uint16_t   (graph_get_bits_pixel)   (void){ return vbe_mem_info.BitsPerPixel; }
39
static uint16_t   (graph_get_bytes_pixel)  (void){ return (graph_get_bits_pixel() + 7) >> 3; }
40
static phys_bytes (graph_get_phys_addr)    (void){ return vbe_mem_info.PhysBasePtr; }
41
static unsigned   (graph_get_vram_size)    (void){ return vbe_mem_info.XResolution * vbe_mem_info.YResolution * graph_get_bytes_pixel(); }
42
//static uint16_t   (graph_get_RedMaskSize)  (void){ return vbe_mem_info.RedMaskSize  ; }
43
//static uint16_t   (graph_get_GreenMaskSize)(void){ return vbe_mem_info.GreenMaskSize; }
44
//static uint16_t   (graph_get_BlueMaskSize) (void){ return vbe_mem_info.BlueMaskSize ; }
45

  
46
static int (get_permission)(unsigned int base_addr, unsigned int size) {
15 47
    struct minix_mem_range mmr;
16 48
    mmr.mr_base = base_addr;
17 49
    mmr.mr_limit = base_addr + size;
18 50
    return sys_privctl(SELF, SYS_PRIV_ADD_MEM, &mmr);
19 51
}
20 52

  
21
//int (get_permissions_first_mbyte)(void) {
53
//static int (get_permissions_first_mbyte)(void) {
22 54
//    return get_permission(MBYTE_BASE, MBYTE_SIZE);
23 55
//}
24 56

  
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;
57
/// MEMORY
58
static void    *video_mem = NULL; /** @brief Frame-buffer VM address. */
59
static uint8_t *video_buf = NULL; /** @brief Primary buffer for drawing before copying to video_mem. */
60
static mmap_t mem_map;
61
static int (graph_free_memory)(void) {
62
    int r = SUCCESS;
63
    free(video_buf); video_buf = NULL;
64
    r = !lm_free(&mem_map);
65
    return r;
66
}
67
static int (graph_map_vram)(void) {
68
    int r;
69
    const unsigned vram_base = graph_get_phys_addr();
70
    const unsigned vram_size = graph_get_vram_size();
71
    if ((r = get_permission(vram_base, vram_size))) {
72
        if (graph_free_memory()) {
73
            printf("%s: lm_free failed\n", __func__);
74
        }
75
        panic("%s: sys_privctl (ADD MEM) failed: %d\n", __func__, r);
29 76
    }
30 77

  
31
    graph_map_vram(); // if function fails it aborts program
78
    video_mem = vm_map_phys(SELF, (void *)vram_base, vram_size);
32 79

  
33
    if (graph_set_mode(mode)) {
34
        printf("%s: failed to set graphic mode %x.\n", __func__, mode);
35
        return 1;
36
    };
80
    if (video_mem == MAP_FAILED) {
81
        if (graph_free_memory()) {
82
            printf("%s: lm_free failed\n", __func__);
83
        }
84
        panic("%s: couldn't map video memory.", __func__);
85
    }
86

  
87
    video_buf = malloc(vram_size);
88

  
37 89
    return SUCCESS;
38 90
}
39 91

  
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) {
92
/// INFO GET
93
static int (vbe_get_mode_information)(uint16_t mode) {
50 94
    memset(&vbe_mem_info, 0, sizeof(vbe_mode_info_t)); // reset values
51 95

  
52 96
    struct reg86 reg_86;
......
72 116
    memcpy((void*)&vbe_mem_info, (void*)virtual_addr, mem_map.size);
73 117
    return SUCCESS;
74 118
}
75

  
76
int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
119
/*
120
static int (vbe_get_controller_information)(vg_vbe_contr_info_t *info_p) {
77 121
    memset(info_p, 0, sizeof(vg_vbe_contr_info_t)); // reset values
78 122

  
79 123
    mmap_t controller_map;
......
147 191

  
148 192
    return SUCCESS;
149 193
}
194
*/
150 195

  
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) {
196
/// INIT
197
/**
198
 * @brief
199
 * @param mode
200
 * @return
201
 */
202
static int (graph_set_mode)(uint16_t mode) {
194 203
    struct reg86 reg_86;
195 204

  
196 205
    memset(&reg_86, 0, sizeof(struct reg86)); // reset struct
......
209 218

  
210 219
    return SUCCESS;
211 220
}
221
int (graph_init)(uint16_t mode){
222
    if (vbe_get_mode_information(mode)) {
223
        printf("%s: failed to get information for mode %x.\n", __func__, mode);
224
        return 1;
225
    }
212 226

  
227
    graph_map_vram(); // if function fails it aborts program
228

  
229
    if (graph_set_mode(mode)) {
230
        printf("%s: failed to set graphic mode %x.\n", __func__, mode);
231
        return 1;
232
    };
233
    return SUCCESS;
234
}
235

  
236
/// CLEANUP
237
int (graph_cleanup)(void){
238
    int r = SUCCESS;
239
    if ((r = vg_exit()))
240
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
241
    if ((r = graph_free_memory()))
242
        printf("%s: lm_free failed\n", __func__);
243
    return r;
244
}
245

  
246
/// PIXEL DRAWING
213 247
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
214 248
    if (x >= vbe_mem_info.XResolution || y >= vbe_mem_info.YResolution) {
215 249
        //printf("%s: invalid pixel.\n", __func__);
......
235 269
    //return set_pixel(x,y,color);
236 270
}
237 271

  
272
int (graph_clear_screen)(void){
273
    //return graph_paint_screen(BLACK);
274
    memset(video_buf, 0, graph_get_vram_size());
275
    return SUCCESS;
276
}
277

  
278
int (graph_draw)(void){
279
    memcpy((uint8_t*)video_mem, video_buf, graph_get_vram_size());
280
    return 0;
281
}
282

  
283
/// RECTANGLE
238 284
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
239 285
    int r;
240 286
    for (uint16_t i = 0; i < len; i++)
241 287
        if ((r = graph_set_pixel(x + i, y, color))) return r;
242 288
    return SUCCESS;
243 289
}
244
int (vg_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
245
    return graph_draw_hline(x,y,len,color);
246
}
247 290

  
248 291
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color)	{
249 292
    int r;
......
251 294
        if ((r = graph_draw_hline(x, y + i, width, color))) return r;
252 295
    return SUCCESS;
253 296
}
254
int (vg_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color){
255
    return graph_draw_rectangle(x,y,width,height, color);
256
}
257

  
258
int (graph_paint_screen)(uint32_t color){
259
    return graph_draw_rectangle(0,0,graph_get_XRes(),graph_get_YRes(),color);
260
}
261

  
262
int (graph_clear_screen)(void){
263
    //return graph_paint_screen(BLACK);
264
    memset(video_buf, 0, graph_get_vram_size());
265
    return SUCCESS;
266
}
267

  
268
int (graph_draw)(void){
269
    memcpy((uint8_t*)video_mem, video_buf, graph_get_vram_size());
270
    return 0;
271
}

Also available in: Unified diff