added struct subsys so that we know each
[winnie] / src / fbdev / gfx.cc
1 /*
2 winnie - an experimental window system
3
4 Copyright (C) 2013 Eleni Maria Stea
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19 Author: Eleni Maria Stea <elene.mst@gmail.com>
20 */
21
22 #ifdef WINNIE_FBDEV
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <fcntl.h>
30 #include <sys/ioctl.h>
31 #include <sys/mman.h>
32 #include <sys/time.h>
33 #include <unistd.h>
34
35 #include <linux/fb.h>
36
37 #include "gfx.h"
38 #include "shalloc.h"
39 #include "winnie.h"
40
41 #define FRAMEBUFFER_SIZE(xsz, ysz, bpp) ((xsz) * (ysz) * (bpp) / CHAR_BIT)
42
43 static unsigned char *framebuffer;
44 static int dev_fd;
45 static int rgb_order[3];
46
47 struct Graphics {
48         Rect screen_rect;
49         Rect clipping_rect;
50         int color_depth;
51         Pixmap *pixmap;
52 };
53
54 static Graphics *gfx;
55
56 bool init_gfx()
57 {
58         if(!(gfx = (Graphics*)sh_malloc(sizeof *gfx))) {
59                 return false;
60         }
61
62         get_subsys()->graphics_offset = (int)((char*)gfx - (char*)get_pool());
63
64         dev_fd = -1;
65
66         if((dev_fd = open("/dev/fb0", O_RDWR)) == -1) {
67                 fprintf(stderr, "Cannot open /dev/fb0 : %s\n", strerror(errno));
68                 return false;
69         }
70
71         fb_var_screeninfo sinfo;
72         if(ioctl(dev_fd, FBIOGET_VSCREENINFO, &sinfo) == -1) {
73                 close(dev_fd);
74                 dev_fd = -1;
75                 fprintf(stderr, "Unable to get screen info : %s\n", strerror(errno));
76                 return false;
77         }
78
79         printf("width : %d height : %d\n : bpp : %d\n", sinfo.xres, sinfo.yres, sinfo.bits_per_pixel);
80         printf("virtual w: %d virtual h: %d\n", sinfo.xres_virtual, sinfo.yres_virtual);
81
82         gfx->screen_rect.x = gfx->screen_rect.y = 0;
83         gfx->screen_rect.width = sinfo.xres_virtual;
84         gfx->screen_rect.height = sinfo.yres_virtual;
85         gfx->color_depth = sinfo.bits_per_pixel;
86
87         rgb_order[0] = sinfo.red.offset / 8;
88         rgb_order[1] = sinfo.green.offset / 8;
89         rgb_order[2] = sinfo.blue.offset / 8;
90
91         set_clipping_rect(gfx->screen_rect);
92
93         int sz = FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth);
94         framebuffer = (unsigned char*)mmap(0, sz, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, 0);
95
96         if(framebuffer == (void*)-1) {
97                 close(dev_fd);
98                 dev_fd = -1;
99                 fprintf(stderr, "Cannot map the framebuffer to memory : %s\n", strerror(errno));
100                 return false;
101         }
102
103 // TODO: uncomment when I find how to use intelfb instead of i915 GRRRR.-       
104         fb_vblank vblank;
105         if(ioctl(dev_fd, FBIOGET_VBLANK, &vblank) == -1) {
106 //              fprintf(stderr, "FBIOGET_VBLANK error: %s\n", strerror(errno));
107         }
108 /*      
109         else {
110                 printf("flags: %x\n", vblank.flags);
111                 printf("count: %d\n", vblank.count);
112                 printf("beam position: %d, %d\n", vblank.hcount, vblank.vcount);
113         }
114 */
115
116         if(!(gfx->pixmap = (Pixmap*)sh_malloc(sizeof(Pixmap)))) {
117                 fprintf(stderr, "Failed to allocate pixmap.\n");
118                 return false;
119         }
120
121         gfx->pixmap->width = gfx->screen_rect.width;
122         gfx->pixmap->height = gfx->screen_rect.height;
123
124         int fbsize = gfx->pixmap->width * gfx->pixmap->height * gfx->color_depth / 8;
125         if(!(gfx->pixmap->pixels = (unsigned char*)sh_malloc(fbsize))) {
126                 fprintf(stderr, "failed to allocate the pixmap framebuffer.\n");
127                 return false;
128         }
129
130         return true;
131 }
132
133 void destroy_gfx()
134 {
135         clear_screen(0, 0, 0);
136         gfx_update(gfx->screen_rect);
137
138         if(dev_fd != -1) {
139                 close(dev_fd);
140         }
141
142         dev_fd = -1;
143
144         munmap(framebuffer, FRAMEBUFFER_SIZE(gfx->screen_rect.width, gfx->screen_rect.height, gfx->color_depth));
145         framebuffer = 0;
146
147         sh_free(gfx->pixmap->pixels);
148         gfx->pixmap->pixels = 0;
149         sh_free(gfx->pixmap);
150         sh_free(gfx);
151 }
152
153 unsigned char *get_framebuffer()
154 {
155         return gfx->pixmap->pixels;
156 }
157
158 Pixmap *get_framebuffer_pixmap()
159 {
160         return gfx->pixmap;
161 }
162
163 Rect get_screen_size()
164 {
165         return gfx->screen_rect;
166 }
167
168 int get_color_depth()
169 {
170         return gfx->color_depth;
171 }
172
173 void set_clipping_rect(const Rect &rect)
174 {
175         gfx->clipping_rect = rect_intersection(rect, get_screen_size());
176 }
177
178 const Rect &get_clipping_rect()
179 {
180         return gfx->clipping_rect;
181 }
182
183 void set_cursor_visibility(bool visible)
184 {
185         fb_cursor curs;
186         curs.enable = visible ? 1 : 0;
187
188         if(ioctl(dev_fd, FBIO_CURSOR, &curs) == -1) {
189                 fprintf(stderr, "Cannot toggle cursor visibility : %s\n", strerror(errno));
190         }
191 }
192
193 void gfx_update(const Rect &upd_rect)
194 {
195         Rect rect = rect_intersection(upd_rect, gfx->screen_rect);
196         unsigned char *sptr = gfx->pixmap->pixels + (rect.y * gfx->screen_rect.width + rect.x) * 4;
197         unsigned char *dptr = framebuffer + (rect.y * gfx->screen_rect.width + rect.x) * 4;
198
199         for(int i=0; i<rect.height; i++) {
200                 memcpy(dptr, sptr, rect.width * 4);
201                 sptr += gfx->screen_rect.width * 4;
202                 dptr += gfx->screen_rect.width * 4;
203         }
204 }
205
206 void wait_vsync()
207 {
208         unsigned long arg = 0;
209         if(ioctl(dev_fd, FBIO_WAITFORVSYNC, &arg) == -1) {
210 //              printf("ioctl error %s\n", strerror(errno));
211         }
212 }
213
214 void get_rgb_order(int *r, int *g, int *b)
215 {
216         *r = rgb_order[0];
217         *g = rgb_order[1];
218         *b = rgb_order[2];
219 }
220
221 #endif // WINNIE_FBDEV