*in progress*
[winnie] / src / gfx.cc
1 #include <errno.h>
2 #include <limits.h>
3 #include <stdio.h>
4 #include <string.h>
5
6 #include <fcntl.h>
7 #include <sys/ioctl.h>
8 #include <sys/mman.h>
9 #include <unistd.h>
10
11 #include <linux/fb.h>
12
13 #include "gfx.h"
14
15 #define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
16
17 static unsigned char *framebuffer;
18 static int dev_fd = -1;
19
20 static Rect screen_rect;
21 static int color_depth; //bits per pixel
22
23 bool init_gfx()
24 {
25         if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
26                 fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
27                 return false;
28         }
29
30         fb_var_screeninfo sinfo;
31         if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
32                 close(dev_fd);
33                 dev_fd = -1;
34                 fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
35                 return false;
36         }
37
38         printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
39         printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
40
41         screen_rect.x = screen_rect.y = 0;
42         screen_rect.width = sinfo.xres_virtual;
43         screen_rect.height = sinfo.yres_virtual;
44         color_depth = sinfo.bits_per_pixel;
45
46         int sz = FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth);
47         framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
48
49         if(framebuffer == (void*)-1) {
50                 close(dev_fd);
51                 dev_fd = -1;
52                 fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
53                 return false;
54         }
55
56         return true;
57 }
58
59 void destroy_gfx()
60 {
61         clear_screen(0, 0, 0);
62
63         if(dev_fd != -1) {
64                 close(dev_fd);
65         }
66
67         dev_fd = -1;
68
69         munmap(framebuffer, FRAMEBUFFER_SIZE(screen_rect.width, screen_rect.height, color_depth));
70         framebuffer = 0;
71 }
72
73 unsigned char *get_framebuffer()
74 {
75         return framebuffer;
76 }
77
78 Rect get_screen_size()
79 {
80         return screen_rect;
81 }
82
83 int get_color_depth()
84 {
85         return color_depth;
86 }
87
88 void clear_screen(int r, int g, int b)
89 {
90         fill_rect(screen_rect, r, g, b);
91 }
92
93 void fill_rect(const Rect &rect, int r, int g, int b)
94 {
95         Rect drect = rect;
96
97         if(drect.x < 0) {
98                 drect.x = 0;
99         }
100
101         if(drect.y < 0) {
102                 drect.y = 0;
103         }
104
105         unsigned char *fb = framebuffer + (drect.x + screen_rect.width * drect.y) * 4;
106         for(int i=0; i<drect.height; i++) {
107                 for(int j=0; j<drect.width; j++) {
108                         fb[j * 4] = b;
109                         fb[j * 4 + 1] = g;
110                         fb[j * 4 + 2] = r;
111                 }
112                 fb += screen_rect.width * 4;
113         }
114 }
115
116 void set_cursor_visibility(bool visible)
117 {
118         fb_cursor curs;
119         curs.enable = visible ? 1 : 0;
120
121         if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
122                 fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
123         }
124 }
125
126 void blit(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
127                 const Rect &dest_rect, int dest_x, int dest_y)
128 {
129         int width = src_rect.width;
130         int height = src_rect.height;
131
132         int xoffs = dest_x - dest_rect.x;
133         if(xoffs < 0) {
134                 dest_x = dest_rect.x;
135                 width += xoffs;
136         }
137
138         int yoffs = dest_y - dest_rect.y;
139         if(yoffs < 0) {
140                 dest_y = dest_rect.y;
141                 height += yoffs;
142         }
143
144         int xend = dest_x + width;
145         if(xend >= dest_rect.width) {
146                 width -= xend - dest_rect.width;
147         }
148
149         int yend = dest_y + height;
150         if(yend >= dest_rect.height) {
151                 height -= yend - dest_rect.height;
152         }
153
154         if(width <= 0 || height <= 0) {
155                 return;
156         }
157
158         unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
159         unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
160
161         for(int i=0; i<height; i++) {
162                 memcpy(dptr, sptr, width * 4);
163                 sptr += src_rect.width * 4;
164                 dptr += dest_rect.width * 4;
165         }
166 }
167
168 void blit_key(unsigned char *src_img, const Rect &src_rect, unsigned char* dest_img,
169                 const Rect &dest_rect, int dest_x, int dest_y, int key_r, int key_g, int key_b)
170 {
171         int width = src_rect.width;
172         int height = src_rect.height;
173
174         int xoffs = dest_x - dest_rect.x;
175         if(xoffs < 0) {
176                 dest_x = dest_rect.x;
177                 width += xoffs;
178         }
179
180         int yoffs = dest_y - dest_rect.y;
181         if(yoffs < 0) {
182                 dest_y = dest_rect.y;
183                 height += yoffs;
184         }
185
186         int xend = dest_x + width;
187         if(xend >= dest_rect.width) {
188                 width -= xend - dest_rect.width;
189         }
190
191         int yend = dest_y + height;
192         if(yend >= dest_rect.height) {
193                 height -= yend - dest_rect.height;
194         }
195
196         if(width <= 0 || height <= 0) {
197                 return;
198         }
199
200         unsigned char *sptr = src_img + (src_rect.y * src_rect.width + src_rect.x) * 4;
201         unsigned char *dptr = dest_img + (dest_y * dest_rect.width + dest_x) * 4;
202
203         for(int i=0; i<height; i++) {
204                 for(int j=0; j<width; j++) {
205                         int r = sptr[j * 4];
206                         int g = sptr[j * 4 + 1];
207                         int b = sptr[j * 4 + 2];
208
209                         if(r != key_r || g != key_g || b != key_b) {
210                                 dptr[j * 4] = r;
211                                 dptr[j * 4 + 1] = g;
212                                 dptr[j * 4 + 2] = b;
213                         }
214                 }
215
216                 sptr += src_rect.width * 4;
217                 dptr += dest_rect.width * 4;
218         }
219 }