blob: dbc35af2c22d5bd004736de24d430b04bc8d780f [file] [log] [blame]
Mike Lockwoodc59b2f92012-10-24 12:31:10 -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
17#include <errno.h>
18#include <unistd.h>
19#include <stdio.h>
20#include <fcntl.h>
Umair Khancfed2322014-01-15 08:08:50 -050021#include <stdlib.h>
22#include <string.h>
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070023
24#include <linux/fb.h>
25#include <sys/ioctl.h>
26#include <sys/mman.h>
27
Mathias Agopian0678a8c2013-03-19 20:56:00 -070028#include <binder/ProcessState.h>
29
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070030#include <gui/SurfaceComposerClient.h>
31#include <gui/ISurfaceComposer.h>
32
Mathias Agopian0137fb82013-03-20 15:38:07 -070033#include <ui/PixelFormat.h>
34
Andreas Gampecfedceb2014-09-30 21:48:18 -070035// TODO: Fix Skia.
36#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wunused-parameter"
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070038#include <SkImageEncoder.h>
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070039#include <SkData.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070040#pragma GCC diagnostic pop
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070041
42using namespace android;
43
44static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain;
45
46static void usage(const char* pname)
47{
48 fprintf(stderr,
49 "usage: %s [-hp] [-d display-id] [FILENAME]\n"
50 " -h: this message\n"
51 " -p: save the file as a png.\n"
52 " -d: specify the display id to capture, default %d.\n"
53 "If FILENAME ends with .png it will be saved as a png.\n"
54 "If FILENAME is not given, the results will be printed to stdout.\n",
55 pname, DEFAULT_DISPLAY_ID
56 );
57}
58
Mike Reedb9330552014-06-16 17:31:48 -040059static SkColorType flinger2skia(PixelFormat f)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070060{
61 switch (f) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070062 case PIXEL_FORMAT_RGB_565:
Mike Reedb9330552014-06-16 17:31:48 -040063 return kRGB_565_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070064 default:
Mike Reedb9330552014-06-16 17:31:48 -040065 return kN32_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070066 }
67}
68
69static status_t vinfoToPixelFormat(const fb_var_screeninfo& vinfo,
70 uint32_t* bytespp, uint32_t* f)
71{
72
73 switch (vinfo.bits_per_pixel) {
74 case 16:
75 *f = PIXEL_FORMAT_RGB_565;
76 *bytespp = 2;
77 break;
78 case 24:
79 *f = PIXEL_FORMAT_RGB_888;
80 *bytespp = 3;
81 break;
82 case 32:
83 // TODO: do better decoding of vinfo here
84 *f = PIXEL_FORMAT_RGBX_8888;
85 *bytespp = 4;
86 break;
87 default:
88 return BAD_VALUE;
89 }
90 return NO_ERROR;
91}
92
Umair Khancfed2322014-01-15 08:08:50 -050093static status_t notifyMediaScanner(const char* fileName) {
94 String8 cmd("am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://");
95 String8 fileUrl("\"");
96 fileUrl.append(fileName);
97 fileUrl.append("\"");
98 cmd.append(fileName);
99 cmd.append(" > /dev/null");
100 int result = system(cmd.string());
101 if (result < 0) {
102 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
103 return UNKNOWN_ERROR;
104 }
105 return NO_ERROR;
106}
107
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700108int main(int argc, char** argv)
109{
Mathias Agopian0678a8c2013-03-19 20:56:00 -0700110 ProcessState::self()->startThreadPool();
111
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700112 const char* pname = argv[0];
113 bool png = false;
114 int32_t displayId = DEFAULT_DISPLAY_ID;
115 int c;
116 while ((c = getopt(argc, argv, "phd:")) != -1) {
117 switch (c) {
118 case 'p':
119 png = true;
120 break;
121 case 'd':
122 displayId = atoi(optarg);
123 break;
124 case '?':
125 case 'h':
126 usage(pname);
127 return 1;
128 }
129 }
130 argc -= optind;
131 argv += optind;
132
133 int fd = -1;
Andreas Gampecfedceb2014-09-30 21:48:18 -0700134 const char* fn = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700135 if (argc == 0) {
136 fd = dup(STDOUT_FILENO);
137 } else if (argc == 1) {
Umair Khancfed2322014-01-15 08:08:50 -0500138 fn = argv[0];
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700139 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
140 if (fd == -1) {
141 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
142 return 1;
143 }
144 const int len = strlen(fn);
145 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
146 png = true;
147 }
148 }
149
150 if (fd == -1) {
151 usage(pname);
152 return 1;
153 }
154
155 void const* mapbase = MAP_FAILED;
156 ssize_t mapsize = -1;
157
Andreas Gampecfedceb2014-09-30 21:48:18 -0700158 void const* base = NULL;
Mathias Agopian0137fb82013-03-20 15:38:07 -0700159 uint32_t w, s, h, f;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700160 size_t size = 0;
161
162 ScreenshotClient screenshot;
163 sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId);
Dan Stoza9890e3412014-05-22 16:12:54 -0700164 if (display != NULL && screenshot.update(display, Rect(), false) == NO_ERROR) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700165 base = screenshot.getPixels();
166 w = screenshot.getWidth();
167 h = screenshot.getHeight();
Mathias Agopian0137fb82013-03-20 15:38:07 -0700168 s = screenshot.getStride();
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700169 f = screenshot.getFormat();
170 size = screenshot.getSize();
171 } else {
172 const char* fbpath = "/dev/graphics/fb0";
173 int fb = open(fbpath, O_RDONLY);
174 if (fb >= 0) {
175 struct fb_var_screeninfo vinfo;
176 if (ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) == 0) {
177 uint32_t bytespp;
178 if (vinfoToPixelFormat(vinfo, &bytespp, &f) == NO_ERROR) {
179 size_t offset = (vinfo.xoffset + vinfo.yoffset*vinfo.xres) * bytespp;
180 w = vinfo.xres;
181 h = vinfo.yres;
Mathias Agopian0137fb82013-03-20 15:38:07 -0700182 s = vinfo.xres;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700183 size = w*h*bytespp;
184 mapsize = offset + size;
185 mapbase = mmap(0, mapsize, PROT_READ, MAP_PRIVATE, fb, 0);
186 if (mapbase != MAP_FAILED) {
187 base = (void const *)((char const *)mapbase + offset);
188 }
189 }
190 }
191 close(fb);
192 }
193 }
194
Andreas Gampecfedceb2014-09-30 21:48:18 -0700195 if (base != NULL) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700196 if (png) {
Mike Reedb9330552014-06-16 17:31:48 -0400197 const SkImageInfo info = SkImageInfo::Make(w, h, flinger2skia(f),
198 kPremul_SkAlphaType);
Leon Scroggins III34497892015-01-20 15:52:43 -0500199 SkAutoTUnref<SkData> data(SkImageEncoder::EncodeData(info, base, s*bytesPerPixel(f),
200 SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality));
201 if (data.get()) {
202 write(fd, data->data(), data->size());
203 }
Andreas Gampecfedceb2014-09-30 21:48:18 -0700204 if (fn != NULL) {
205 notifyMediaScanner(fn);
206 }
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700207 } else {
208 write(fd, &w, 4);
209 write(fd, &h, 4);
210 write(fd, &f, 4);
Mathias Agopian0137fb82013-03-20 15:38:07 -0700211 size_t Bpp = bytesPerPixel(f);
212 for (size_t y=0 ; y<h ; y++) {
213 write(fd, base, w*Bpp);
214 base = (void *)((char *)base + s*Bpp);
215 }
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700216 }
217 }
218 close(fd);
219 if (mapbase != MAP_FAILED) {
220 munmap((void *)mapbase, mapsize);
221 }
222 return 0;
223}