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