blob: 35f8bbb57e501de74bb97aac4520c7a83aabd912 [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
Dan Stozacf70d712015-06-09 16:47:33 -070033#include <ui/DisplayInfo.h>
Mathias Agopian0137fb82013-03-20 15:38:07 -070034#include <ui/PixelFormat.h>
35
Romain Guy26a2b972017-04-17 09:39:51 -070036#include <system/graphics.h>
37
Andreas Gampecfedceb2014-09-30 21:48:18 -070038// TODO: Fix Skia.
39#pragma GCC diagnostic push
40#pragma GCC diagnostic ignored "-Wunused-parameter"
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070041#include <SkImageEncoder.h>
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070042#include <SkData.h>
Romain Guy26a2b972017-04-17 09:39:51 -070043#include <SkColorSpace.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070044#pragma GCC diagnostic pop
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070045
46using namespace android;
47
48static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain;
49
Romain Guy26a2b972017-04-17 09:39:51 -070050#define COLORSPACE_UNKNOWN 0
51#define COLORSPACE_SRGB 1
52#define COLORSPACE_DISPLAY_P3 2
53
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070054static void usage(const char* pname)
55{
56 fprintf(stderr,
57 "usage: %s [-hp] [-d display-id] [FILENAME]\n"
58 " -h: this message\n"
59 " -p: save the file as a png.\n"
60 " -d: specify the display id to capture, default %d.\n"
61 "If FILENAME ends with .png it will be saved as a png.\n"
62 "If FILENAME is not given, the results will be printed to stdout.\n",
63 pname, DEFAULT_DISPLAY_ID
64 );
65}
66
Mike Reedb9330552014-06-16 17:31:48 -040067static SkColorType flinger2skia(PixelFormat f)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070068{
69 switch (f) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070070 case PIXEL_FORMAT_RGB_565:
Mike Reedb9330552014-06-16 17:31:48 -040071 return kRGB_565_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070072 default:
Mike Reedb9330552014-06-16 17:31:48 -040073 return kN32_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070074 }
75}
76
Romain Guy26a2b972017-04-17 09:39:51 -070077static sk_sp<SkColorSpace> dataSpaceToColorSpace(android_dataspace d)
78{
79 switch (d) {
80 case HAL_DATASPACE_V0_SRGB:
81 return SkColorSpace::MakeSRGB();
82 case HAL_DATASPACE_DISPLAY_P3:
83 return SkColorSpace::MakeRGB(
84 SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kDCIP3_D65_Gamut);
85 default:
86 return nullptr;
87 }
88}
89
90static uint32_t dataSpaceToInt(android_dataspace d)
91{
92 switch (d) {
93 case HAL_DATASPACE_V0_SRGB:
94 return COLORSPACE_SRGB;
95 case HAL_DATASPACE_DISPLAY_P3:
96 return COLORSPACE_DISPLAY_P3;
97 default:
98 return COLORSPACE_UNKNOWN;
99 }
100}
101
Umair Khancfed2322014-01-15 08:08:50 -0500102static status_t notifyMediaScanner(const char* fileName) {
103 String8 cmd("am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://");
104 String8 fileUrl("\"");
105 fileUrl.append(fileName);
106 fileUrl.append("\"");
107 cmd.append(fileName);
108 cmd.append(" > /dev/null");
109 int result = system(cmd.string());
110 if (result < 0) {
111 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
112 return UNKNOWN_ERROR;
113 }
114 return NO_ERROR;
115}
116
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700117int main(int argc, char** argv)
118{
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700119 const char* pname = argv[0];
120 bool png = false;
121 int32_t displayId = DEFAULT_DISPLAY_ID;
122 int c;
123 while ((c = getopt(argc, argv, "phd:")) != -1) {
124 switch (c) {
125 case 'p':
126 png = true;
127 break;
128 case 'd':
129 displayId = atoi(optarg);
130 break;
131 case '?':
132 case 'h':
133 usage(pname);
134 return 1;
135 }
136 }
137 argc -= optind;
138 argv += optind;
139
140 int fd = -1;
Andreas Gampecfedceb2014-09-30 21:48:18 -0700141 const char* fn = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700142 if (argc == 0) {
143 fd = dup(STDOUT_FILENO);
144 } else if (argc == 1) {
Umair Khancfed2322014-01-15 08:08:50 -0500145 fn = argv[0];
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700146 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
147 if (fd == -1) {
148 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
149 return 1;
150 }
151 const int len = strlen(fn);
152 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
153 png = true;
154 }
155 }
Prathmesh Prabhubf89ae52016-03-10 15:23:34 -0800156
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700157 if (fd == -1) {
158 usage(pname);
159 return 1;
160 }
161
162 void const* mapbase = MAP_FAILED;
163 ssize_t mapsize = -1;
164
Andreas Gampecfedceb2014-09-30 21:48:18 -0700165 void const* base = NULL;
Mathias Agopian0137fb82013-03-20 15:38:07 -0700166 uint32_t w, s, h, f;
Romain Guy26a2b972017-04-17 09:39:51 -0700167 android_dataspace d;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700168 size_t size = 0;
169
Dan Stozacf70d712015-06-09 16:47:33 -0700170 // Maps orientations from DisplayInfo to ISurfaceComposer
171 static const uint32_t ORIENTATION_MAP[] = {
172 ISurfaceComposer::eRotateNone, // 0 == DISPLAY_ORIENTATION_0
173 ISurfaceComposer::eRotate270, // 1 == DISPLAY_ORIENTATION_90
174 ISurfaceComposer::eRotate180, // 2 == DISPLAY_ORIENTATION_180
175 ISurfaceComposer::eRotate90, // 3 == DISPLAY_ORIENTATION_270
176 };
177
Martijn Coenen48b74082017-07-24 09:19:26 +0200178 // setThreadPoolMaxThreadCount(0) actually tells the kernel it's
179 // not allowed to spawn any additional threads, but we still spawn
180 // a binder thread from userspace when we call startThreadPool().
181 // See b/36066697 for rationale
182 ProcessState::self()->setThreadPoolMaxThreadCount(0);
183 ProcessState::self()->startThreadPool();
184
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700185 ScreenshotClient screenshot;
186 sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId);
Dan Stozacf70d712015-06-09 16:47:33 -0700187 if (display == NULL) {
188 fprintf(stderr, "Unable to get handle for display %d\n", displayId);
Martijn Coenen48b74082017-07-24 09:19:26 +0200189 // b/36066697: Avoid running static destructors.
190 _exit(1);
Dan Stozacf70d712015-06-09 16:47:33 -0700191 }
192
193 Vector<DisplayInfo> configs;
194 SurfaceComposerClient::getDisplayConfigs(display, &configs);
195 int activeConfig = SurfaceComposerClient::getActiveConfig(display);
196 if (static_cast<size_t>(activeConfig) >= configs.size()) {
197 fprintf(stderr, "Active config %d not inside configs (size %zu)\n",
198 activeConfig, configs.size());
Martijn Coenen48b74082017-07-24 09:19:26 +0200199 // b/36066697: Avoid running static destructors.
200 _exit(1);
Dan Stozacf70d712015-06-09 16:47:33 -0700201 }
202 uint8_t displayOrientation = configs[activeConfig].orientation;
203 uint32_t captureOrientation = ORIENTATION_MAP[displayOrientation];
204
Robert Carrdb7ecbe2017-01-31 11:02:58 -0800205 status_t result = screenshot.update(display, Rect(),
206 0 /* reqWidth */, 0 /* reqHeight */,
207 INT32_MIN, INT32_MAX, /* all layers */
Dan Stozacf70d712015-06-09 16:47:33 -0700208 false, captureOrientation);
209 if (result == NO_ERROR) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700210 base = screenshot.getPixels();
211 w = screenshot.getWidth();
212 h = screenshot.getHeight();
Mathias Agopian0137fb82013-03-20 15:38:07 -0700213 s = screenshot.getStride();
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700214 f = screenshot.getFormat();
Romain Guy26a2b972017-04-17 09:39:51 -0700215 d = screenshot.getDataSpace();
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700216 size = screenshot.getSize();
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700217 }
218
Andreas Gampecfedceb2014-09-30 21:48:18 -0700219 if (base != NULL) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700220 if (png) {
Hal Canary10219fb2016-11-23 20:41:22 -0500221 const SkImageInfo info =
Romain Guy26a2b972017-04-17 09:39:51 -0700222 SkImageInfo::Make(w, h, flinger2skia(f), kPremul_SkAlphaType,
223 dataSpaceToColorSpace(d));
Hal Canary10219fb2016-11-23 20:41:22 -0500224 SkPixmap pixmap(info, base, s * bytesPerPixel(f));
225 struct FDWStream final : public SkWStream {
226 size_t fBytesWritten = 0;
227 int fFd;
228 FDWStream(int f) : fFd(f) {}
229 size_t bytesWritten() const override { return fBytesWritten; }
230 bool write(const void* buffer, size_t size) override {
231 fBytesWritten += size;
232 return size == 0 || ::write(fFd, buffer, size) > 0;
233 }
234 } fdStream(fd);
235 (void)SkEncodeImage(&fdStream, pixmap, SkEncodedImageFormat::kPNG, 100);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700236 if (fn != NULL) {
237 notifyMediaScanner(fn);
238 }
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700239 } else {
Romain Guy26a2b972017-04-17 09:39:51 -0700240 uint32_t c = dataSpaceToInt(d);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700241 write(fd, &w, 4);
242 write(fd, &h, 4);
243 write(fd, &f, 4);
Romain Guy26a2b972017-04-17 09:39:51 -0700244 write(fd, &c, 4);
Mathias Agopian0137fb82013-03-20 15:38:07 -0700245 size_t Bpp = bytesPerPixel(f);
246 for (size_t y=0 ; y<h ; y++) {
247 write(fd, base, w*Bpp);
248 base = (void *)((char *)base + s*Bpp);
249 }
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700250 }
251 }
252 close(fd);
253 if (mapbase != MAP_FAILED) {
254 munmap((void *)mapbase, mapsize);
255 }
Josh Gao90982582017-06-19 13:38:20 -0700256
257 // b/36066697: Avoid running static destructors.
Brian Carlstrom9377ce62017-06-21 22:14:40 -0700258 _exit(0);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700259}