blob: 4410f1c4570c0be1fd5ee91c25a8de49b5ac8fdb [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>
Dichen Zhangd31f2192020-03-12 12:25:09 -070027#include <sys/wait.h>
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070028
Mathias Agopian0678a8c2013-03-19 20:56:00 -070029#include <binder/ProcessState.h>
30
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070031#include <gui/SurfaceComposerClient.h>
32#include <gui/ISurfaceComposer.h>
33
Dan Stozacf70d712015-06-09 16:47:33 -070034#include <ui/DisplayInfo.h>
Peiyong Lin10a34d12018-09-19 13:56:12 -070035#include <ui/GraphicTypes.h>
Mathias Agopian0137fb82013-03-20 15:38:07 -070036#include <ui/PixelFormat.h>
37
Romain Guy26a2b972017-04-17 09:39:51 -070038#include <system/graphics.h>
39
Andreas Gampecfedceb2014-09-30 21:48:18 -070040// TODO: Fix Skia.
41#pragma GCC diagnostic push
42#pragma GCC diagnostic ignored "-Wunused-parameter"
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070043#include <SkImageEncoder.h>
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070044#include <SkData.h>
Romain Guy26a2b972017-04-17 09:39:51 -070045#include <SkColorSpace.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070046#pragma GCC diagnostic pop
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070047
48using namespace android;
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
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080054static void usage(const char* pname, PhysicalDisplayId displayId)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070055{
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"
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080060 " -d: specify the physical display ID to capture (default: %"
61 ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ")\n"
62 " see \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n"
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070063 "If FILENAME ends with .png it will be saved as a png.\n"
64 "If FILENAME is not given, the results will be printed to stdout.\n",
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080065 pname, displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070066}
67
Mike Reedb9330552014-06-16 17:31:48 -040068static SkColorType flinger2skia(PixelFormat f)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070069{
70 switch (f) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070071 case PIXEL_FORMAT_RGB_565:
Mike Reedb9330552014-06-16 17:31:48 -040072 return kRGB_565_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070073 default:
Mike Reedb9330552014-06-16 17:31:48 -040074 return kN32_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070075 }
76}
77
Peiyong Lin10a34d12018-09-19 13:56:12 -070078static sk_sp<SkColorSpace> dataSpaceToColorSpace(ui::Dataspace d)
Romain Guy26a2b972017-04-17 09:39:51 -070079{
80 switch (d) {
Peiyong Lin10a34d12018-09-19 13:56:12 -070081 case ui::Dataspace::V0_SRGB:
Romain Guy26a2b972017-04-17 09:39:51 -070082 return SkColorSpace::MakeSRGB();
Peiyong Lin10a34d12018-09-19 13:56:12 -070083 case ui::Dataspace::DISPLAY_P3:
Brian Osmanbe8fac262019-01-14 17:02:23 -050084 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
Romain Guy26a2b972017-04-17 09:39:51 -070085 default:
86 return nullptr;
87 }
88}
89
Peiyong Lin10a34d12018-09-19 13:56:12 -070090static uint32_t dataSpaceToInt(ui::Dataspace d)
Romain Guy26a2b972017-04-17 09:39:51 -070091{
92 switch (d) {
Peiyong Lin10a34d12018-09-19 13:56:12 -070093 case ui::Dataspace::V0_SRGB:
Romain Guy26a2b972017-04-17 09:39:51 -070094 return COLORSPACE_SRGB;
Peiyong Lin10a34d12018-09-19 13:56:12 -070095 case ui::Dataspace::DISPLAY_P3:
Romain Guy26a2b972017-04-17 09:39:51 -070096 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) {
Dichen Zhangd31f2192020-03-12 12:25:09 -0700103 std::string filePath("file://");
104 filePath.append(fileName);
Dichen Zhangd31f2192020-03-12 12:25:09 -0700105 char *cmd[] = {
106 (char*) "am",
107 (char*) "broadcast",
108 (char*) "am",
109 (char*) "android.intent.action.MEDIA_SCANNER_SCAN_FILE",
110 (char*) "-d",
George Burgess IV5c46fb62020-03-16 12:07:30 -0700111 &filePath[0],
Dichen Zhangd31f2192020-03-12 12:25:09 -0700112 nullptr
113 };
114
115 int status;
116 int pid = fork();
117 if (pid < 0){
118 fprintf(stderr, "Unable to fork in order to send intent for media scanner.\n");
119 return UNKNOWN_ERROR;
120 }
121 if (pid == 0){
122 int fd = open("/dev/null", O_WRONLY);
123 if (fd < 0){
124 fprintf(stderr, "Unable to open /dev/null for media scanner stdout redirection.\n");
125 exit(1);
126 }
127 dup2(fd, 1);
128 int result = execvp(cmd[0], cmd);
129 close(fd);
130 exit(result);
131 }
132 wait(&status);
133
134 if (status < 0) {
Umair Khancfed2322014-01-15 08:08:50 -0500135 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
136 return UNKNOWN_ERROR;
137 }
138 return NO_ERROR;
139}
140
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700141int main(int argc, char** argv)
142{
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800143 std::optional<PhysicalDisplayId> displayId = SurfaceComposerClient::getInternalDisplayId();
144 if (!displayId) {
145 fprintf(stderr, "Failed to get token for internal display\n");
146 return 1;
147 }
148
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700149 const char* pname = argv[0];
150 bool png = false;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700151 int c;
152 while ((c = getopt(argc, argv, "phd:")) != -1) {
153 switch (c) {
154 case 'p':
155 png = true;
156 break;
157 case 'd':
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800158 displayId = atoll(optarg);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700159 break;
160 case '?':
161 case 'h':
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800162 usage(pname, *displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700163 return 1;
164 }
165 }
166 argc -= optind;
167 argv += optind;
168
169 int fd = -1;
Andreas Gampecfedceb2014-09-30 21:48:18 -0700170 const char* fn = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700171 if (argc == 0) {
172 fd = dup(STDOUT_FILENO);
173 } else if (argc == 1) {
Umair Khancfed2322014-01-15 08:08:50 -0500174 fn = argv[0];
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700175 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
176 if (fd == -1) {
177 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
178 return 1;
179 }
180 const int len = strlen(fn);
181 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
182 png = true;
183 }
184 }
Prathmesh Prabhubf89ae52016-03-10 15:23:34 -0800185
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700186 if (fd == -1) {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800187 usage(pname, *displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700188 return 1;
189 }
190
191 void const* mapbase = MAP_FAILED;
192 ssize_t mapsize = -1;
193
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000194 void* base = NULL;
Mathias Agopian0137fb82013-03-20 15:38:07 -0700195 uint32_t w, s, h, f;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700196 size_t size = 0;
197
Martijn Coenen48b74082017-07-24 09:19:26 +0200198 // setThreadPoolMaxThreadCount(0) actually tells the kernel it's
199 // not allowed to spawn any additional threads, but we still spawn
200 // a binder thread from userspace when we call startThreadPool().
201 // See b/36066697 for rationale
202 ProcessState::self()->setThreadPoolMaxThreadCount(0);
203 ProcessState::self()->startThreadPool();
204
chaviwa69a1b82019-04-30 16:53:33 -0700205 ui::Dataspace outDataspace;
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000206 sp<GraphicBuffer> outBuffer;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700207
chaviwa69a1b82019-04-30 16:53:33 -0700208 status_t result = ScreenshotClient::capture(*displayId, &outDataspace, &outBuffer);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000209 if (result != NO_ERROR) {
210 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700211 return 1;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700212 }
213
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000214 result = outBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base);
215
chaviw7f4dc7e2018-09-11 13:59:30 -0700216 if (base == nullptr || result != NO_ERROR) {
217 String8 reason;
chaviwa69a1b82019-04-30 16:53:33 -0700218 if (result != NO_ERROR) {
219 reason.appendFormat(" Error Code: %d", result);
chaviw7f4dc7e2018-09-11 13:59:30 -0700220 } else {
chaviwa69a1b82019-04-30 16:53:33 -0700221 reason = "Failed to write to buffer";
chaviw7f4dc7e2018-09-11 13:59:30 -0700222 }
223 fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str());
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000224 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700225 return 1;
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000226 }
227
228 w = outBuffer->getWidth();
229 h = outBuffer->getHeight();
230 s = outBuffer->getStride();
231 f = outBuffer->getPixelFormat();
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000232 size = s * h * bytesPerPixel(f);
233
234 if (png) {
235 const SkImageInfo info =
Peiyong Lin10a34d12018-09-19 13:56:12 -0700236 SkImageInfo::Make(w, h, flinger2skia(f), kPremul_SkAlphaType,
chaviwa69a1b82019-04-30 16:53:33 -0700237 dataSpaceToColorSpace(outDataspace));
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000238 SkPixmap pixmap(info, base, s * bytesPerPixel(f));
239 struct FDWStream final : public SkWStream {
240 size_t fBytesWritten = 0;
241 int fFd;
242 FDWStream(int f) : fFd(f) {}
243 size_t bytesWritten() const override { return fBytesWritten; }
244 bool write(const void* buffer, size_t size) override {
245 fBytesWritten += size;
246 return size == 0 || ::write(fFd, buffer, size) > 0;
247 }
248 } fdStream(fd);
249 (void)SkEncodeImage(&fdStream, pixmap, SkEncodedImageFormat::kPNG, 100);
250 if (fn != NULL) {
251 notifyMediaScanner(fn);
252 }
253 } else {
chaviwa69a1b82019-04-30 16:53:33 -0700254 uint32_t c = dataSpaceToInt(outDataspace);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000255 write(fd, &w, 4);
256 write(fd, &h, 4);
257 write(fd, &f, 4);
258 write(fd, &c, 4);
259 size_t Bpp = bytesPerPixel(f);
260 for (size_t y=0 ; y<h ; y++) {
261 write(fd, base, w*Bpp);
262 base = (void *)((char *)base + s*Bpp);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700263 }
264 }
265 close(fd);
266 if (mapbase != MAP_FAILED) {
267 munmap((void *)mapbase, mapsize);
268 }
Josh Gao90982582017-06-19 13:38:20 -0700269
Steven Morelanda89ae862018-05-24 17:48:28 -0700270 return 0;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700271}