blob: bee5880ac4ccfeebf48ad1434871b847b3791348 [file] [log] [blame]
Mathias Agopian88a5df92010-09-26 18:49:45 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onorato5cff0632010-12-03 16:01:57 -080017#include <errno.h>
Mathias Agopian88a5df92010-09-26 18:49:45 -070018#include <unistd.h>
Joe Onorato5cff0632010-12-03 16:01:57 -080019#include <stdio.h>
Mathias Agopian88a5df92010-09-26 18:49:45 -070020#include <fcntl.h>
21
Mathias Agopian9afc7b02011-02-08 20:43:00 -080022#include <linux/fb.h>
23#include <sys/ioctl.h>
24#include <sys/mman.h>
25
Mathias Agopian88a5df92010-09-26 18:49:45 -070026#include <binder/IMemory.h>
Mathias Agopian38ed2e32010-09-29 13:02:36 -070027#include <surfaceflinger/SurfaceComposerClient.h>
Mathias Agopian88a5df92010-09-26 18:49:45 -070028
Joe Onorato5cff0632010-12-03 16:01:57 -080029#include <SkImageEncoder.h>
30#include <SkBitmap.h>
Derek Sollenberger889a3fa2012-01-31 14:08:22 -050031#include <SkData.h>
Joe Onorato5cff0632010-12-03 16:01:57 -080032#include <SkStream.h>
33
Mathias Agopian88a5df92010-09-26 18:49:45 -070034using namespace android;
35
Mathias Agopian9afc7b02011-02-08 20:43:00 -080036static void usage(const char* pname)
Joe Onorato5cff0632010-12-03 16:01:57 -080037{
38 fprintf(stderr,
Mathias Agopian9afc7b02011-02-08 20:43:00 -080039 "usage: %s [-hp] [FILENAME]\n"
Joe Onorato5cff0632010-12-03 16:01:57 -080040 " -h: this message\n"
41 " -p: save the file as a png.\n"
42 "If FILENAME ends with .png it will be saved as a png.\n"
Mathias Agopian9afc7b02011-02-08 20:43:00 -080043 "If FILENAME is not given, the results will be printed to stdout.\n",
44 pname
Joe Onorato5cff0632010-12-03 16:01:57 -080045 );
46}
47
48static SkBitmap::Config flinger2skia(PixelFormat f)
49{
50 switch (f) {
51 case PIXEL_FORMAT_A_8:
52 case PIXEL_FORMAT_L_8:
53 return SkBitmap::kA8_Config;
54 case PIXEL_FORMAT_RGB_565:
55 return SkBitmap::kRGB_565_Config;
56 case PIXEL_FORMAT_RGBA_4444:
57 return SkBitmap::kARGB_4444_Config;
58 default:
59 return SkBitmap::kARGB_8888_Config;
60 }
61}
62
Mathias Agopian9afc7b02011-02-08 20:43:00 -080063static status_t vinfoToPixelFormat(const fb_var_screeninfo& vinfo,
64 uint32_t* bytespp, uint32_t* f)
65{
66
67 switch (vinfo.bits_per_pixel) {
68 case 16:
69 *f = PIXEL_FORMAT_RGB_565;
70 *bytespp = 2;
71 break;
72 case 24:
73 *f = PIXEL_FORMAT_RGB_888;
74 *bytespp = 3;
75 break;
76 case 32:
77 // TODO: do better decoding of vinfo here
78 *f = PIXEL_FORMAT_RGBX_8888;
79 *bytespp = 4;
80 break;
81 default:
82 return BAD_VALUE;
83 }
84 return NO_ERROR;
85}
86
Mathias Agopian88a5df92010-09-26 18:49:45 -070087int main(int argc, char** argv)
88{
Mathias Agopian9afc7b02011-02-08 20:43:00 -080089 const char* pname = argv[0];
Joe Onorato5cff0632010-12-03 16:01:57 -080090 bool png = false;
91 int c;
92 while ((c = getopt(argc, argv, "ph")) != -1) {
93 switch (c) {
94 case 'p':
95 png = true;
96 break;
97 case '?':
98 case 'h':
Mathias Agopian9afc7b02011-02-08 20:43:00 -080099 usage(pname);
Joe Onorato5cff0632010-12-03 16:01:57 -0800100 return 1;
101 }
102 }
103 argc -= optind;
104 argv += optind;
105
106 int fd = -1;
107 if (argc == 0) {
108 fd = dup(STDOUT_FILENO);
109 } else if (argc == 1) {
110 const char* fn = argv[0];
111 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
112 if (fd == -1) {
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800113 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
Joe Onorato5cff0632010-12-03 16:01:57 -0800114 return 1;
115 }
116 const int len = strlen(fn);
117 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
118 png = true;
119 }
120 }
121
122 if (fd == -1) {
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800123 usage(pname);
Joe Onorato5cff0632010-12-03 16:01:57 -0800124 return 1;
125 }
126
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800127 void const* mapbase = MAP_FAILED;
128 ssize_t mapsize = -1;
129
130 void const* base = 0;
131 uint32_t w, h, f;
132 size_t size = 0;
133
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700134 ScreenshotClient screenshot;
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800135 if (screenshot.update() == NO_ERROR) {
136 base = screenshot.getPixels();
137 w = screenshot.getWidth();
138 h = screenshot.getHeight();
139 f = screenshot.getFormat();
140 size = screenshot.getSize();
141 } else {
142 const char* fbpath = "/dev/graphics/fb0";
143 int fb = open(fbpath, O_RDONLY);
144 if (fb >= 0) {
145 struct fb_var_screeninfo vinfo;
146 if (ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) == 0) {
147 uint32_t bytespp;
148 if (vinfoToPixelFormat(vinfo, &bytespp, &f) == NO_ERROR) {
149 size_t offset = (vinfo.xoffset + vinfo.yoffset*vinfo.xres) * bytespp;
150 w = vinfo.xres;
151 h = vinfo.yres;
152 size = w*h*bytespp;
153 mapsize = offset + size;
154 mapbase = mmap(0, mapsize, PROT_READ, MAP_PRIVATE, fb, 0);
155 if (mapbase != MAP_FAILED) {
156 base = (void const *)((char const *)mapbase + offset);
157 }
158 }
159 }
160 close(fb);
161 }
Joe Onorato5cff0632010-12-03 16:01:57 -0800162 }
Mathias Agopian88a5df92010-09-26 18:49:45 -0700163
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800164 if (base) {
165 if (png) {
166 SkBitmap b;
167 b.setConfig(flinger2skia(f), w, h);
168 b.setPixels((void*)base);
169 SkDynamicMemoryWStream stream;
170 SkImageEncoder::EncodeStream(&stream, b,
171 SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);
Derek Sollenberger889a3fa2012-01-31 14:08:22 -0500172 SkData* streamData = stream.copyToData();
173 write(fd, streamData->data(), streamData->size());
174 streamData->unref();
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800175 } else {
176 write(fd, &w, 4);
177 write(fd, &h, 4);
178 write(fd, &f, 4);
179 write(fd, base, size);
180 }
Joe Onorato5cff0632010-12-03 16:01:57 -0800181 }
Mathias Agopian88a5df92010-09-26 18:49:45 -0700182 close(fd);
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800183 if (mapbase != MAP_FAILED) {
184 munmap((void *)mapbase, mapsize);
185 }
Mathias Agopian88a5df92010-09-26 18:49:45 -0700186 return 0;
187}