Repository navigation
Expand file tree
/
Copy pathfb_virtual.c
More file actions
375 lines (360 loc) · 14.5 KB
/
Copy pathfb_virtual.c
File metadata and controls
375 lines (360 loc) · 14.5 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#define _GNU_SOURCE
#include "fb_config.h"
#include "fb_geometry.h"
#include <errno.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <time.h>
#include <unistd.h>
static pthread_once_t config_once = PTHREAD_ONCE_INIT;
static pthread_once_t display_once = PTHREAD_ONCE_INIT;
static struct fb_config config;
static struct fb_output_rect output;
static pthread_t painter;
static int config_ok;
static int initialized_ok;
static int painter_created;
static int initialization_error = ENODEV;
static int actual_fd = -1;
static int shadow_fd = -1;
static int running;
static unsigned char *actual_memory;
static unsigned char *saved_memory;
static uint16_t *shadow_memory;
static struct fb_fix_screeninfo actual_fix;
static struct fb_var_screeninfo actual_var;
static struct stat shadow_identity;
static size_t source_bytes;
static uint32_t native_colors[65536];
static unsigned int *horizontal_source;
static void load_configuration(void)
{
const char *path = getenv("SR_ATS_FB_CONFIG");
fb_config_defaults(&config);
if (path == NULL || *path == '\0') {
path = FB_CONFIG_DEFAULT_PATH;
}
if (fb_config_load_file(&config, path) != 0 && errno != ENOENT) {
return;
}
if (fb_config_load_environment(&config) != 0 || fb_config_validate(&config, 0) != 0) {
return;
}
config_ok = 1;
}
static int selected_program(void)
{
const char *expected;
char executable[4096];
ssize_t length;
const char *name;
pthread_once(&config_once, load_configuration);
expected = getenv("SR_ATS_FB_PROGRAM");
if (expected == NULL || *expected == '\0') {
expected = config.program;
}
length = readlink("/proc/self/exe", executable, sizeof(executable) - 1);
if (!config_ok || expected == NULL || *expected == '\0' || length <= 0) {
return 0;
}
executable[length] = '\0';
name = strrchr(executable, '/');
return strcmp(name == NULL ? executable : name + 1, expected) == 0;
}
static uint32_t channel(unsigned int value, unsigned int maximum, struct fb_bitfield field)
{
uint32_t encoded = 0;
if (field.length != 0) {
encoded = (uint32_t)((value * ((1u << field.length) - 1u) + maximum / 2u) / maximum);
encoded <<= field.offset;
}
return encoded;
}
static void virtual_information(struct fb_var_screeninfo *var, struct fb_fix_screeninfo *fix)
{
if (var != NULL) {
memset(var, 0, sizeof(*var));
var->xres = var->xres_virtual = config.virtual_width;
var->yres = var->yres_virtual = config.virtual_height;
var->bits_per_pixel = 16;
var->red.offset = 11;
var->red.length = 5;
var->green.offset = 5;
var->green.length = 6;
var->blue.offset = 0;
var->blue.length = 5;
var->width = var->height = (uint32_t)-1;
}
if (fix != NULL) {
memset(fix, 0, sizeof(*fix));
memcpy(fix->id, "FramebufferScale", sizeof(fix->id));
fix->smem_len = (uint32_t)source_bytes;
fix->type = FB_TYPE_PACKED_PIXELS;
fix->visual = FB_VISUAL_TRUECOLOR;
fix->line_length = config.virtual_width * 2u;
fix->accel = FB_ACCEL_NONE;
}
}
static void restore_display(void)
{
struct fb_var_screeninfo current;
if (actual_memory != NULL && saved_memory != NULL &&
syscall(SYS_ioctl, actual_fd, FBIOGET_VSCREENINFO, ¤t) == 0 &&
current.xres == actual_var.xres && current.yres == actual_var.yres &&
current.bits_per_pixel == actual_var.bits_per_pixel && current.xoffset == actual_var.xoffset &&
current.yoffset == actual_var.yoffset) {
memcpy(actual_memory, saved_memory, actual_fix.smem_len);
}
}
static void *paint_frames(void *unused)
{
struct timespec now;
struct timespec deadline;
struct timespec next;
uint64_t frame;
uint64_t maximum_frames = config.seconds == 0 ? UINT64_MAX : (uint64_t)config.fps * config.seconds;
unsigned int x;
unsigned int y;
unsigned int bytes_per_pixel = actual_var.bits_per_pixel / 8u;
unsigned char *row = malloc((size_t)output.width * bytes_per_pixel);
uint16_t *snapshot = malloc(source_bytes);
(void)unused;
if (row != NULL && snapshot != NULL) {
clock_gettime(CLOCK_MONOTONIC, &now);
deadline = now;
deadline.tv_sec += config.seconds;
next = now;
for (y = 0; y < actual_var.yres; ++y) {
memset(actual_memory + (y + actual_var.yoffset) * actual_fix.line_length +
actual_var.xoffset * bytes_per_pixel, 0, actual_var.xres * bytes_per_pixel);
}
for (frame = 0; frame < maximum_frames && __atomic_load_n(&running, __ATOMIC_ACQUIRE); ++frame) {
clock_gettime(CLOCK_MONOTONIC, &now);
if (config.seconds != 0 && (now.tv_sec > deadline.tv_sec ||
(now.tv_sec == deadline.tv_sec && now.tv_nsec >= deadline.tv_nsec))) {
break;
}
if (frame == 0 || memcmp(snapshot, shadow_memory, source_bytes) != 0) {
memcpy(snapshot, shadow_memory, source_bytes);
for (y = 0; y < output.height; ++y) {
unsigned int source_y = (unsigned int)((uint64_t)y * config.virtual_height / output.height);
for (x = 0; x < output.width; ++x) {
uint32_t pixel = native_colors[snapshot[source_y * config.virtual_width + horizontal_source[x]]];
if (bytes_per_pixel == 4) {
((uint32_t *)row)[x] = pixel;
} else if (bytes_per_pixel == 2) {
((uint16_t *)row)[x] = (uint16_t)pixel;
} else {
memcpy(row + x * bytes_per_pixel, &pixel, bytes_per_pixel);
}
}
memcpy(actual_memory + (y + output.top + actual_var.yoffset) * actual_fix.line_length +
(output.left + actual_var.xoffset) * bytes_per_pixel, row,
(size_t)output.width * bytes_per_pixel);
}
}
next.tv_nsec += 1000000000L / config.fps;
while (next.tv_nsec >= 1000000000L) {
next.tv_nsec -= 1000000000L;
++next.tv_sec;
}
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &next, NULL);
}
}
free(snapshot);
free(row);
restore_display();
return NULL;
}
static void initialize_display(void)
{
unsigned int index;
unsigned int bytes_per_pixel;
pthread_once(&config_once, load_configuration);
if (!config_ok) {
initialization_error = EINVAL;
} else {
source_bytes = (size_t)config.virtual_width * config.virtual_height * 2u;
actual_fd = (int)syscall(SYS_openat, AT_FDCWD, "/dev/fb0", O_RDWR | O_CLOEXEC, 0);
if (actual_fd < 0) {
initialization_error = errno;
} else if (syscall(SYS_ioctl, actual_fd, FBIOGET_FSCREENINFO, &actual_fix) != 0 ||
syscall(SYS_ioctl, actual_fd, FBIOGET_VSCREENINFO, &actual_var) != 0) {
initialization_error = errno;
} else if (!config.system_auto && (actual_var.xres != config.system_width || actual_var.yres != config.system_height)) {
initialization_error = EINVAL;
} else if (actual_fix.type != FB_TYPE_PACKED_PIXELS || actual_fix.visual != FB_VISUAL_TRUECOLOR ||
actual_var.grayscale != 0 || actual_var.nonstd != 0 ||
(actual_var.bits_per_pixel != 16 && actual_var.bits_per_pixel != 24 && actual_var.bits_per_pixel != 32) ||
actual_var.xres == 0 || actual_var.yres == 0 || actual_var.xres > 8192 || actual_var.yres > 8192 ||
actual_fix.smem_len == 0 || actual_fix.smem_len > 256u * 1024u * 1024u ||
(uint64_t)(actual_var.xoffset + actual_var.xres) * (actual_var.bits_per_pixel / 8u) > actual_fix.line_length ||
((uint64_t)actual_var.yoffset + actual_var.yres) * actual_fix.line_length > actual_fix.smem_len) {
initialization_error = ENOTSUP;
} else if (actual_var.red.length > 8 || actual_var.green.length > 8 || actual_var.blue.length > 8 ||
actual_var.transp.length > 8 || actual_var.red.offset + actual_var.red.length > actual_var.bits_per_pixel ||
actual_var.green.offset + actual_var.green.length > actual_var.bits_per_pixel ||
actual_var.blue.offset + actual_var.blue.length > actual_var.bits_per_pixel ||
actual_var.transp.offset + actual_var.transp.length > actual_var.bits_per_pixel ||
actual_var.red.msb_right || actual_var.green.msb_right || actual_var.blue.msb_right || actual_var.transp.msb_right) {
initialization_error = ENOTSUP;
} else if (!fb_compute_output_rect(config.virtual_width, config.virtual_height, actual_var.xres,
actual_var.yres, config.stretch, &output)) {
initialization_error = EINVAL;
} else {
bytes_per_pixel = actual_var.bits_per_pixel / 8u;
horizontal_source = calloc(output.width, sizeof(*horizontal_source));
actual_memory = mmap(NULL, actual_fix.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, actual_fd, 0);
saved_memory = malloc(actual_fix.smem_len);
shadow_fd = (int)syscall(SYS_memfd_create, "framebuffer-scale-shadow", 1u);
if (horizontal_source == NULL || actual_memory == MAP_FAILED || saved_memory == NULL ||
shadow_fd < 0 || ftruncate(shadow_fd, (off_t)source_bytes) != 0 ||
fstat(shadow_fd, &shadow_identity) != 0) {
initialization_error = errno == 0 ? ENOMEM : errno;
if (actual_memory == MAP_FAILED) {
actual_memory = NULL;
}
} else {
shadow_memory = mmap(NULL, source_bytes, PROT_READ | PROT_WRITE, MAP_SHARED, shadow_fd, 0);
if (shadow_memory == MAP_FAILED) {
shadow_memory = NULL;
initialization_error = errno;
} else {
memcpy(saved_memory, actual_memory, actual_fix.smem_len);
for (index = 0; index < output.width; ++index) {
horizontal_source[index] = (unsigned int)((uint64_t)index * config.virtual_width / output.width);
}
for (index = 0; index < 65536; ++index) {
native_colors[index] = channel((index >> 11) & 31u, 31u, actual_var.red) |
channel((index >> 5) & 63u, 63u, actual_var.green) |
channel(index & 31u, 31u, actual_var.blue) |
channel(1, 1, actual_var.transp);
}
__atomic_store_n(&running, 1, __ATOMIC_RELEASE);
initialization_error = pthread_create(&painter, NULL, paint_frames, NULL);
if (initialization_error == 0) {
painter_created = initialized_ok = 1;
}
}
}
(void)bytes_per_pixel;
}
}
if (!initialized_ok) {
fprintf(stderr, "framebuffer-scale: initialization failed errno=%d\n", initialization_error);
}
}
static int virtual_descriptor(int fd)
{
struct stat identity;
return initialized_ok && fstat(fd, &identity) == 0 && identity.st_dev == shadow_identity.st_dev &&
identity.st_ino == shadow_identity.st_ino;
}
static int open_device(const char *path, int flags, mode_t mode)
{
int result;
if (path != NULL && strcmp(path, "/dev/fb0") == 0 && selected_program()) {
pthread_once(&display_once, initialize_display);
if (initialized_ok) {
result = fcntl(shadow_fd, (flags & O_CLOEXEC) != 0 ? F_DUPFD_CLOEXEC : F_DUPFD, 3);
} else {
errno = initialization_error;
result = -1;
}
} else {
result = (int)syscall(SYS_openat, AT_FDCWD, path, flags, mode);
}
return result;
}
int open(const char *path, int flags, ...)
{
mode_t mode = 0;
int result;
if ((flags & O_CREAT) != 0 || (flags & O_TMPFILE) == O_TMPFILE) {
va_list args;
va_start(args, flags);
mode = (mode_t)va_arg(args, int);
va_end(args);
}
result = open_device(path, flags, mode);
return result;
}
int open64(const char *path, int flags, ...)
{
mode_t mode = 0;
int result;
if ((flags & O_CREAT) != 0 || (flags & O_TMPFILE) == O_TMPFILE) {
va_list args;
va_start(args, flags);
mode = (mode_t)va_arg(args, int);
va_end(args);
}
result = open_device(path, flags | O_LARGEFILE, mode);
return result;
}
int ioctl(int fd, unsigned long request, ...)
{
va_list args;
void *argument;
int result = -1;
va_start(args, request);
argument = va_arg(args, void *);
va_end(args);
if (!virtual_descriptor(fd)) {
result = (int)syscall(SYS_ioctl, fd, request, argument);
} else if (argument == NULL && request != FBIOBLANK) {
errno = EFAULT;
} else if (request == FBIOGET_VSCREENINFO) {
virtual_information(argument, NULL);
result = 0;
} else if (request == FBIOGET_FSCREENINFO) {
virtual_information(NULL, argument);
result = 0;
} else if (request == FBIOPUT_VSCREENINFO || request == FBIOPAN_DISPLAY) {
struct fb_var_screeninfo *var = argument;
if (var->xres == config.virtual_width && var->yres == config.virtual_height && var->bits_per_pixel == 16 &&
var->xres_virtual == config.virtual_width && var->yres_virtual == config.virtual_height &&
var->xoffset == 0 && var->yoffset == 0) {
virtual_information(var, NULL);
result = 0;
} else {
errno = EINVAL;
}
} else if (request == FBIOBLANK || request == FBIOPUTCMAP) {
result = 0;
} else {
errno = ENOTTY;
}
return result;
}
__attribute__((destructor)) static void release_display(void)
{
if (painter_created) {
__atomic_store_n(&running, 0, __ATOMIC_RELEASE);
pthread_join(painter, NULL);
}
if (shadow_memory != NULL) {
munmap(shadow_memory, source_bytes);
}
if (shadow_fd >= 0) {
close(shadow_fd);
}
free(horizontal_source);
free(saved_memory);
if (actual_memory != NULL) {
munmap(actual_memory, actual_fix.smem_len);
}
if (actual_fd >= 0) {
close(actual_fd);
}
}