This repository has been archived by the owner on Apr 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfx.c
273 lines (212 loc) · 4.77 KB
/
gfx.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fb.h>
#include "gfx.h"
/******************************************************************************/
static int fb_fd = -1;
static const char *fb_dev;
static struct fb_var_screeninfo fb_vinfo;
static struct fb_fix_screeninfo fb_finfo;
static void *fb_pixels;
static int fb_frontbuffer_id;
static unsigned short fb_saved_red[256], fb_saved_green[256], fb_saved_blue[256];
static int fb_saved_palette;
static int fb_screensize;
static int fb_doublebuffer;
/******************************************************************************/
#define fb_xres (fb_vinfo.xres)
#define fb_yres (fb_vinfo.yres)
#define fb_rowbytes (fb_finfo.line_length)
/******************************************************************************/
int gfx_setpal(unsigned i, unsigned n, unsigned short *r, unsigned short *g,
unsigned short *b);
/******************************************************************************/
static int
gfx_save_pal(void)
{
struct fb_cmap pal;
if (fb_fd == -1)
return -1;
pal.start = 0;
pal.len = 256;
pal.red = fb_saved_red;
pal.green = fb_saved_green;
pal.blue = fb_saved_blue;
pal.transp = 0;
fb_saved_palette = 0;
if (ioctl(fb_fd, FBIOGETCMAP, &pal)) {
perror(fb_dev);
return -1;
}
fb_saved_palette = 1;
return 0;
}
static int
gfx_show_primary(void)
{
fb_frontbuffer_id = 0;
if (fb_vinfo.yoffset != 0) {
fb_vinfo.yoffset = 0;
if (ioctl(fb_fd, FBIOPAN_DISPLAY, &fb_vinfo))
return -1;
}
return 0;
}
static void
gfx_unmap_buffer(void)
{
if (fb_pixels) {
if (munmap(fb_pixels, fb_finfo.smem_len))
perror(fb_dev);
fb_pixels = NULL;
}
}
static void *
gfx_map_buffer(void)
{
if (fb_fd == -1)
return NULL;
fb_pixels = mmap(NULL, fb_finfo.smem_len, PROT_WRITE, MAP_SHARED, fb_fd, 0);
if (fb_pixels == (void*)(MAP_FAILED)) {
perror(fb_dev);
fb_pixels = NULL;
return NULL;
}
fb_screensize = fb_rowbytes * fb_yres;
// TODO: round up by ypanstep
if (fb_screensize * 2u <= fb_finfo.smem_len)
fb_doublebuffer = 1;
else
fb_doublebuffer = 0;
return fb_pixels;
}
static void
gfx_dump_info(void)
{
dprintf(STDERR_FILENO, "%s:xres=%d yres=%d\n", fb_dev, fb_xres, fb_yres);
dprintf(STDERR_FILENO, "%s:bpp=%d\n", fb_dev, fb_vinfo.bits_per_pixel);
}
void
gfx_close(void)
{
if (fb_fd == -1)
return;
if (fb_saved_palette)
gfx_setpal(0, 256, fb_saved_red, fb_saved_green, fb_saved_blue);
gfx_unmap_buffer();
close(fb_fd);
fb_fd = -1;
memset(&fb_vinfo, 0, sizeof(fb_vinfo));
memset(&fb_finfo, 0, sizeof(fb_finfo));
}
int
gfx_open(unsigned *xres, unsigned *yres, size_t *rowbytes)
{
fb_dev = getenv("FBDEV") ? : "/dev/fb0";
if (fb_fd != -1)
return -1;
fb_fd = open(fb_dev, O_RDWR);
if (fb_fd == -1)
goto failure;
if (ioctl(fb_fd, FBIOGET_VSCREENINFO, &fb_vinfo))
goto failure;
if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &fb_finfo))
goto failure;
if (gfx_show_primary())
goto failure;
if (xres)
*xres = fb_xres;
if (yres)
*yres = fb_yres;
if (rowbytes)
*rowbytes = fb_rowbytes ? : fb_xres * 4; // HACK!
gfx_dump_info();
return 0;
failure:
perror(fb_dev);
if (fb_fd != -1)
close(fb_fd);
fb_fd = -1;
return -1;
}
int
gfx_setbpp(unsigned bpp, size_t *rowbytes)
{
struct fb_var_screeninfo vinfo;
if (fb_fd == -1)
return -1;
vinfo = fb_vinfo;
vinfo.bits_per_pixel = bpp;
if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vinfo))
goto failure;
if (vinfo.bits_per_pixel != bpp) {
dprintf(STDERR_FILENO, "%s:unable to set %d bpp\n", fb_dev, bpp);
return -1;
}
/* HACK: maybe refresh line_lenght? (I don't know really) */
if (ioctl(fb_fd, FBIOGET_FSCREENINFO, &fb_finfo))
goto failure;
if (rowbytes)
*rowbytes = fb_rowbytes ? : fb_xres * 4; // HACK!
gfx_dump_info();
return 0;
failure:
perror(fb_dev);
return -1;
}
void *
gfx_buffer(size_t *len)
{
if (!fb_pixels)
gfx_map_buffer();
if (!fb_pixels)
return NULL;
if (len)
*len = fb_screensize;
if (!fb_doublebuffer || !fb_frontbuffer_id)
return fb_pixels;
return fb_pixels + fb_screensize;
}
int
gfx_setpal(unsigned i, unsigned n, unsigned short *r, unsigned short *g, unsigned short *b)
{
struct fb_cmap pal;
if (fb_fd == -1)
return -1;
if (!fb_saved_palette) {
if (gfx_save_pal())
return -1;
}
pal.start = i;
pal.len = n;
pal.red = r;
pal.green = g;
pal.blue = b;
pal.transp = 0;
if (ioctl(fb_fd, FBIOPUTCMAP, &pal)) {
perror(fb_dev);
return -1;
}
return 0;
}
int
gfx_swapbuffers(void)
{
if (!fb_doublebuffer)
return 0;
fb_vinfo.yoffset = fb_frontbuffer_id ? fb_screensize : 0;
if (ioctl(fb_fd, FBIOPAN_DISPLAY, &fb_vinfo)) {
perror(fb_dev);
return -1;
}
fb_frontbuffer_id = !fb_frontbuffer_id;
return 0;
}