blob: 522c272f425523d64bd989c006a4e6f4d340cd22 [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);
105 char* fp_arg = strdup(filePath.c_str());
106 char *cmd[] = {
107 (char*) "am",
108 (char*) "broadcast",
109 (char*) "am",
110 (char*) "android.intent.action.MEDIA_SCANNER_SCAN_FILE",
111 (char*) "-d",
112 (char*) fp_arg,
113 nullptr
114 };
115
116 int status;
117 int pid = fork();
118 if (pid < 0){
119 fprintf(stderr, "Unable to fork in order to send intent for media scanner.\n");
120 return UNKNOWN_ERROR;
121 }
122 if (pid == 0){
123 int fd = open("/dev/null", O_WRONLY);
124 if (fd < 0){
125 fprintf(stderr, "Unable to open /dev/null for media scanner stdout redirection.\n");
126 exit(1);
127 }
128 dup2(fd, 1);
129 int result = execvp(cmd[0], cmd);
130 close(fd);
131 exit(result);
132 }
133 wait(&status);
134
135 if (status < 0) {
Umair Khancfed2322014-01-15 08:08:50 -0500136 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
137 return UNKNOWN_ERROR;
138 }
139 return NO_ERROR;
140}
141
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700142int main(int argc, char** argv)
143{
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800144 std::optional<PhysicalDisplayId> displayId = SurfaceComposerClient::getInternalDisplayId();
145 if (!displayId) {
146 fprintf(stderr, "Failed to get token for internal display\n");
147 return 1;
148 }
149
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700150 const char* pname = argv[0];
151 bool png = false;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700152 int c;
153 while ((c = getopt(argc, argv, "phd:")) != -1) {
154 switch (c) {
155 case 'p':
156 png = true;
157 break;
158 case 'd':
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800159 displayId = atoll(optarg);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700160 break;
161 case '?':
162 case 'h':
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800163 usage(pname, *displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700164 return 1;
165 }
166 }
167 argc -= optind;
168 argv += optind;
169
170 int fd = -1;
Andreas Gampecfedceb2014-09-30 21:48:18 -0700171 const char* fn = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700172 if (argc == 0) {
173 fd = dup(STDOUT_FILENO);
174 } else if (argc == 1) {
Umair Khancfed2322014-01-15 08:08:50 -0500175 fn = argv[0];
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700176 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
177 if (fd == -1) {
178 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
179 return 1;
180 }
181 const int len = strlen(fn);
182 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
183 png = true;
184 }
185 }
Prathmesh Prabhubf89ae52016-03-10 15:23:34 -0800186
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700187 if (fd == -1) {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800188 usage(pname, *displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700189 return 1;
190 }
191
192 void const* mapbase = MAP_FAILED;
193 ssize_t mapsize = -1;
194
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000195 void* base = NULL;
Mathias Agopian0137fb82013-03-20 15:38:07 -0700196 uint32_t w, s, h, f;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700197 size_t size = 0;
198
Martijn Coenen48b74082017-07-24 09:19:26 +0200199 // setThreadPoolMaxThreadCount(0) actually tells the kernel it's
200 // not allowed to spawn any additional threads, but we still spawn
201 // a binder thread from userspace when we call startThreadPool().
202 // See b/36066697 for rationale
203 ProcessState::self()->setThreadPoolMaxThreadCount(0);
204 ProcessState::self()->startThreadPool();
205
chaviwa69a1b82019-04-30 16:53:33 -0700206 ui::Dataspace outDataspace;
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000207 sp<GraphicBuffer> outBuffer;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700208
chaviwa69a1b82019-04-30 16:53:33 -0700209 status_t result = ScreenshotClient::capture(*displayId, &outDataspace, &outBuffer);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000210 if (result != NO_ERROR) {
211 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700212 return 1;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700213 }
214
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000215 result = outBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base);
216
chaviw7f4dc7e2018-09-11 13:59:30 -0700217 if (base == nullptr || result != NO_ERROR) {
218 String8 reason;
chaviwa69a1b82019-04-30 16:53:33 -0700219 if (result != NO_ERROR) {
220 reason.appendFormat(" Error Code: %d", result);
chaviw7f4dc7e2018-09-11 13:59:30 -0700221 } else {
chaviwa69a1b82019-04-30 16:53:33 -0700222 reason = "Failed to write to buffer";
chaviw7f4dc7e2018-09-11 13:59:30 -0700223 }
224 fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str());
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000225 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700226 return 1;
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000227 }
228
229 w = outBuffer->getWidth();
230 h = outBuffer->getHeight();
231 s = outBuffer->getStride();
232 f = outBuffer->getPixelFormat();
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000233 size = s * h * bytesPerPixel(f);
234
235 if (png) {
236 const SkImageInfo info =
Peiyong Lin10a34d12018-09-19 13:56:12 -0700237 SkImageInfo::Make(w, h, flinger2skia(f), kPremul_SkAlphaType,
chaviwa69a1b82019-04-30 16:53:33 -0700238 dataSpaceToColorSpace(outDataspace));
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000239 SkPixmap pixmap(info, base, s * bytesPerPixel(f));
240 struct FDWStream final : public SkWStream {
241 size_t fBytesWritten = 0;
242 int fFd;
243 FDWStream(int f) : fFd(f) {}
244 size_t bytesWritten() const override { return fBytesWritten; }
245 bool write(const void* buffer, size_t size) override {
246 fBytesWritten += size;
247 return size == 0 || ::write(fFd, buffer, size) > 0;
248 }
249 } fdStream(fd);
250 (void)SkEncodeImage(&fdStream, pixmap, SkEncodedImageFormat::kPNG, 100);
251 if (fn != NULL) {
252 notifyMediaScanner(fn);
253 }
254 } else {
chaviwa69a1b82019-04-30 16:53:33 -0700255 uint32_t c = dataSpaceToInt(outDataspace);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000256 write(fd, &w, 4);
257 write(fd, &h, 4);
258 write(fd, &f, 4);
259 write(fd, &c, 4);
260 size_t Bpp = bytesPerPixel(f);
261 for (size_t y=0 ; y<h ; y++) {
262 write(fd, base, w*Bpp);
263 base = (void *)((char *)base + s*Bpp);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700264 }
265 }
266 close(fd);
267 if (mapbase != MAP_FAILED) {
268 munmap((void *)mapbase, mapsize);
269 }
Josh Gao90982582017-06-19 13:38:20 -0700270
Steven Morelanda89ae862018-05-24 17:48:28 -0700271 return 0;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700272}