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