blob: a357b7fee8c96b684efb3502815f38d8d4e68ec0 [file] [log] [blame]
Talustus3019a912013-04-06 11:50:07 +02001/**
2 * fb2png Save screenshot into .png.
3 *
4 * Copyright (C) 2012 Kyan <kyan.ql.he@gmail.com>
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 2 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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20#include <stdio.h>
21#include <stdlib.h>
22#include <fcntl.h>
23#include <sys/ioctl.h>
24#include <sys/mman.h>
25#include <linux/fb.h>
26#include <errno.h>
27
28#include "log.h"
29#include "fb2png.h"
30#include "fb.h"
31
32/**
33 * Get the {@code struct fb} from device's framebuffer.
34 * Return
35 * 0 for success.
36 */
37int get_device_fb(const char* path, struct fb *fb)
38{
39 int fd;
40 int bytespp;
41 int offset;
42 char *x;
43 struct fb_var_screeninfo vinfo;
44
45 fd = open(path, O_RDONLY);
46 if (fd < 0) return -1;
47
48 if(ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) < 0) {
49 D("ioctl failed, %s\n", strerror(errno));
50 return -1;
51 }
52
53 bytespp = vinfo.bits_per_pixel / 8;
54
55 fb->bpp = vinfo.bits_per_pixel;
56 fb->size = vinfo.xres * vinfo.yres * bytespp;
57 fb->width = vinfo.xres;
58 fb->height = vinfo.yres;
59 fb->red_offset = vinfo.red.offset;
60 fb->red_length = vinfo.red.length;
61 fb->green_offset = vinfo.green.offset;
62 fb->green_length = vinfo.green.length;
63 fb->blue_offset = vinfo.blue.offset;
64 fb->blue_length = vinfo.blue.length;
65 fb->alpha_offset = vinfo.transp.offset;
66 fb->alpha_length = vinfo.transp.length;
67
68#ifdef ANDROID
69 /* HACK: for several of 3d cores a specific alignment
70 * is required so the start of the fb may not be an integer number of lines
71 * from the base. As a result we are storing the additional offset in
72 * xoffset. This is not the correct usage for xoffset, it should be added
73 * to each line, not just once at the beginning */
74
75 offset = vinfo.xoffset * bytespp;
76
77 /* Android use double-buffer, capture 2nd */
78 offset += vinfo.xres * vinfo.yoffset * bytespp;
79#else
80 offset = 0;
81#endif
82
83 x = malloc(fb->size);
84 if (!x) return -1;
85
86 lseek(fd, offset, SEEK_SET);
87
88 if (read(fd, x ,fb->size) != fb->size) goto oops;
89
90 fb->data = x;
91 close(fd);
92
93 return 0;
94
95oops:
96 close(fd);
97 free(x);
98 return -1;
99}
100
101int fb2png(const char *path)
102{
103 struct fb fb;
104 int ret;
105
106#ifdef ANDROID
107 ret = get_device_fb("/dev/graphics/fb0", &fb);
108#else
109 ret = get_device_fb("/dev/fb0", &fb);
110#endif
111
112 if (ret) {
113 D("Failed to read framebuffer.");
114 return -1;
115 }
116
117 fb_dump(&fb);
118
119 return fb_save_png(&fb, path);
120}
121