-
Notifications
You must be signed in to change notification settings - Fork 21
/
fb2png.c
199 lines (173 loc) · 5.66 KB
/
fb2png.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
/**
* fb2png Save screenshot into .png.
*
* Copyright (C) 2012 Kyan <kyan.ql.he@gmail.com>
* Copyright (C) 2014, philz-cwm6 <phytowardt@gmail.com>
* Copyright (C) 2014, Mikael Berthe "McKael" <mikael@lilotux.net>
*
* 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 2 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, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/fb.h>
#include <errno.h>
#include "log.h"
#include "fb2png.h"
#include "fb.h"
// multi buffering support
// -1: will be auto detect (default)
// 0 for single buffering, 1 for double, 2 for triple, 3 for 4x buffering
int user_set_buffers_num = -1;
/**
* Get the {@code struct fb} from device's framebuffer.
* Return
* 0 for success.
*/
int get_device_fb(const char* path, struct fb *fb)
{
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
unsigned char *raw;
unsigned int bytespp;
unsigned int raw_size;
unsigned int raw_line_length;
ssize_t read_size;
int fd;
fd = open(path, O_RDONLY);
if (fd < 0) return -1;
if(ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) < 0) {
D("ioctl failed, %s", strerror(errno));
close(fd);
return -1;
}
if (ioctl(fd, FBIOGET_FSCREENINFO, &finfo) < 0) {
D("ioctl fixed failed, %s", strerror(errno));
close(fd);
return -1;
}
bytespp = vinfo.bits_per_pixel / 8;
raw_line_length = finfo.line_length; // (xres + padding_offset) * bytespp
raw_size = vinfo.yres * raw_line_length;
// output data handler struct
fb->bpp = vinfo.bits_per_pixel;
fb->size = vinfo.xres * vinfo.yres * bytespp;
fb->width = vinfo.xres;
fb->height = vinfo.yres;
fb->red_offset = vinfo.red.offset;
fb->red_length = vinfo.red.length;
fb->green_offset = vinfo.green.offset;
fb->green_length = vinfo.green.length;
fb->blue_offset = vinfo.blue.offset;
fb->blue_length = vinfo.blue.length;
fb->alpha_offset = vinfo.transp.offset;
fb->alpha_length = vinfo.transp.length;
// container for raw bits from the active frame buffer
raw = malloc(raw_size);
if (!raw) {
D("raw: memory error");
close(fd);
return -1;
}
// capture active buffer: n is 0 for first buffer, 1 for second
// graphics.c -> set_active_framebuffer() -> vi.yoffset = n * vi.yres;
unsigned int active_buffer_offset = 0;
int num_buffers = user_set_buffers_num;
if (num_buffers < 0) {
// default: auto detect
num_buffers = (int)(vinfo.yoffset / vinfo.yres);
if (num_buffers > MAX_ALLOWED_FB_BUFFERS)
num_buffers = 0;
}
if (finfo.smem_len >= (raw_size * (num_buffers + 1))) {
active_buffer_offset = raw_size * num_buffers;
}
// display debug fb info
fb_dump(fb);
D("%13s : %u", "bytespp", bytespp);
D("%13s : %u", "raw size", raw_size);
D("%13s : %u", "yoffset", vinfo.yoffset);
D("%13s : %u", "pad offset", (raw_line_length / bytespp) - fb->width);
D("%13s : %u", "buffer offset", active_buffer_offset);
// copy the active frame buffer bits into the raw container
lseek(fd, active_buffer_offset, SEEK_SET);
read_size = read(fd, raw, raw_size);
if (read_size < 0 || (unsigned)read_size != raw_size) {
D("read buffer error");
goto oops;
}
/*
Image padding (needed on some RGBX_8888 formats, maybe others?)
we have padding_offset in bytes and bytespp = bits_per_pixel / 8
raw_line_length = (width + padding_offset) * bytespp
This gives: padding_offset = (raw_line_length / bytespp) - width
*/
unsigned int padding_offset = (raw_line_length / bytespp) - fb->width;
if (padding_offset) {
unsigned char *data;
unsigned char *pdata;
unsigned char *praw;
const unsigned char *data_buffer_end;
const unsigned char *raw_buffer_end;
// container for final aligned image data
data = malloc(fb->size);
if (!data) {
D("data: memory error");
goto oops;
}
pdata = data;
praw = raw;
data_buffer_end = data + fb->size;
raw_buffer_end = raw + raw_size;
// Add a margin to prevent buffer overflow during copy
data_buffer_end -= bytespp * fb->width;
raw_buffer_end -= raw_line_length;
while (praw < raw_buffer_end && pdata < data_buffer_end) {
memcpy(pdata, praw, bytespp * fb->width);
pdata += bytespp * fb->width;
praw += raw_line_length;
}
D("Padding done.");
fb->data = data;
free(raw);
} else {
fb->data = raw;
}
close(fd);
return 0;
oops:
free(raw);
close(fd);
return -1;
}
int fb2png(const char *path)
{
struct fb fb;
int ret;
#ifdef ANDROID
ret = get_device_fb("/dev/graphics/fb0", &fb);
#else
ret = get_device_fb("/dev/fb0", &fb);
#endif
if (ret) {
D("Failed to read framebuffer.");
return -1;
}
return fb_save_png(&fb, path);
}