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