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