blob: d7222d248911007a726b8bca4f329f1359cacb1b [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
Derek Sollenbergera3ef0942020-04-08 15:47:55 -040029#include <android/bitmap.h>
30
Mathias Agopian0678a8c2013-03-19 20:56:00 -070031#include <binder/ProcessState.h>
32
Chavi Weingarten797bdc92020-09-10 20:55:11 +000033#include <gui/ISurfaceComposer.h>
chaviwbc100492020-08-18 16:06:40 -070034#include <gui/SurfaceComposerClient.h>
35#include <gui/SyncScreenCaptureListener.h>
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070036
Peiyong Lin10a34d12018-09-19 13:56:12 -070037#include <ui/GraphicTypes.h>
Mathias Agopian0137fb82013-03-20 15:38:07 -070038#include <ui/PixelFormat.h>
39
Romain Guy26a2b972017-04-17 09:39:51 -070040#include <system/graphics.h>
41
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070042using namespace android;
43
Romain Guy26a2b972017-04-17 09:39:51 -070044#define COLORSPACE_UNKNOWN 0
45#define COLORSPACE_SRGB 1
46#define COLORSPACE_DISPLAY_P3 2
47
Huihong Luo41ea7d12022-09-26 11:32:52 -070048static void usage(const char* pname, std::optional<PhysicalDisplayId> displayId)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070049{
Huihong Luo41ea7d12022-09-26 11:32:52 -070050 std::string defaultDisplayStr = "";
51 if (!displayId) {
52 defaultDisplayStr = "";
53 } else {
54 defaultDisplayStr = " (default: " + to_string(*displayId) + ")";
55 }
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070056 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"
Huihong Luo41ea7d12022-09-26 11:32:52 -070060 " -d: specify the display ID to capture%s\n"
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080061 " see \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n"
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070062 "If FILENAME ends with .png it will be saved as a png.\n"
63 "If FILENAME is not given, the results will be printed to stdout.\n",
Huihong Luo41ea7d12022-09-26 11:32:52 -070064 pname, defaultDisplayStr.c_str());
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070065}
66
Derek Sollenbergera3ef0942020-04-08 15:47:55 -040067static int32_t flinger2bitmapFormat(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:
Derek Sollenbergera3ef0942020-04-08 15:47:55 -040071 return ANDROID_BITMAP_FORMAT_RGB_565;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070072 default:
Derek Sollenbergera3ef0942020-04-08 15:47:55 -040073 return ANDROID_BITMAP_FORMAT_RGBA_8888;
Romain Guy26a2b972017-04-17 09:39:51 -070074 }
75}
76
Peiyong Lin10a34d12018-09-19 13:56:12 -070077static uint32_t dataSpaceToInt(ui::Dataspace d)
Romain Guy26a2b972017-04-17 09:39:51 -070078{
79 switch (d) {
Peiyong Lin10a34d12018-09-19 13:56:12 -070080 case ui::Dataspace::V0_SRGB:
Romain Guy26a2b972017-04-17 09:39:51 -070081 return COLORSPACE_SRGB;
Peiyong Lin10a34d12018-09-19 13:56:12 -070082 case ui::Dataspace::DISPLAY_P3:
Romain Guy26a2b972017-04-17 09:39:51 -070083 return COLORSPACE_DISPLAY_P3;
84 default:
85 return COLORSPACE_UNKNOWN;
86 }
87}
88
Umair Khancfed2322014-01-15 08:08:50 -050089static status_t notifyMediaScanner(const char* fileName) {
Dichen Zhangd31f2192020-03-12 12:25:09 -070090 std::string filePath("file://");
91 filePath.append(fileName);
Dichen Zhangd31f2192020-03-12 12:25:09 -070092 char *cmd[] = {
93 (char*) "am",
94 (char*) "broadcast",
Dichen Zhange361a262020-04-17 10:05:49 -070095 (char*) "-a",
Dichen Zhangd31f2192020-03-12 12:25:09 -070096 (char*) "android.intent.action.MEDIA_SCANNER_SCAN_FILE",
97 (char*) "-d",
George Burgess IV5c46fb62020-03-16 12:07:30 -070098 &filePath[0],
Dichen Zhangd31f2192020-03-12 12:25:09 -070099 nullptr
100 };
101
102 int status;
103 int pid = fork();
104 if (pid < 0){
105 fprintf(stderr, "Unable to fork in order to send intent for media scanner.\n");
106 return UNKNOWN_ERROR;
107 }
108 if (pid == 0){
109 int fd = open("/dev/null", O_WRONLY);
110 if (fd < 0){
111 fprintf(stderr, "Unable to open /dev/null for media scanner stdout redirection.\n");
112 exit(1);
113 }
114 dup2(fd, 1);
115 int result = execvp(cmd[0], cmd);
116 close(fd);
117 exit(result);
118 }
119 wait(&status);
120
121 if (status < 0) {
Umair Khancfed2322014-01-15 08:08:50 -0500122 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
123 return UNKNOWN_ERROR;
124 }
125 return NO_ERROR;
126}
127
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700128int main(int argc, char** argv)
129{
Huihong Luo41ea7d12022-09-26 11:32:52 -0700130 const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds();
131 if (ids.empty()) {
132 fprintf(stderr, "Failed to get ID for any displays.\n");
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800133 return 1;
134 }
Huihong Luo41ea7d12022-09-26 11:32:52 -0700135 std::optional<PhysicalDisplayId> displayId;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700136 const char* pname = argv[0];
137 bool png = false;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700138 int c;
139 while ((c = getopt(argc, argv, "phd:")) != -1) {
140 switch (c) {
141 case 'p':
142 png = true;
143 break;
144 case 'd':
Huihong Luo41ea7d12022-09-26 11:32:52 -0700145 displayId = DisplayId::fromValue<PhysicalDisplayId>(atoll(optarg));
Dominik Laskowski0bda2e32021-03-23 16:24:01 -0700146 if (!displayId) {
Huihong Luo41ea7d12022-09-26 11:32:52 -0700147 fprintf(stderr, "Invalid display ID: %s\n", optarg);
Dominik Laskowski0bda2e32021-03-23 16:24:01 -0700148 return 1;
149 }
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700150 break;
151 case '?':
152 case 'h':
Huihong Luo41ea7d12022-09-26 11:32:52 -0700153 if (ids.size() == 1) {
154 displayId = ids.front();
155 }
156 usage(pname, displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700157 return 1;
158 }
159 }
Huihong Luo41ea7d12022-09-26 11:32:52 -0700160
161 if (!displayId) { // no diplsay id is specified
Huihong Luo6f645f72022-10-07 19:24:00 -0700162 displayId = ids.front();
163 if (ids.size() > 1) {
164 fprintf(stderr,
165 "[Warning] Multiple displays were found, but no display id was specified! "
166 "Defaulting to the first display found, however this default is not guaranteed "
167 "to be consistent across captures. A display id should be specified.\n");
168 fprintf(stderr, "A display ID can be specified with the [-d display-id] option.\n");
Huihong Luo41ea7d12022-09-26 11:32:52 -0700169 fprintf(stderr, "See \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n");
Huihong Luo41ea7d12022-09-26 11:32:52 -0700170 }
171 }
172
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700173 argc -= optind;
174 argv += optind;
175
176 int fd = -1;
Andreas Gampecfedceb2014-09-30 21:48:18 -0700177 const char* fn = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700178 if (argc == 0) {
179 fd = dup(STDOUT_FILENO);
180 } else if (argc == 1) {
Umair Khancfed2322014-01-15 08:08:50 -0500181 fn = argv[0];
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700182 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
183 if (fd == -1) {
184 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
185 return 1;
186 }
187 const int len = strlen(fn);
188 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
189 png = true;
190 }
191 }
Prathmesh Prabhubf89ae52016-03-10 15:23:34 -0800192
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700193 if (fd == -1) {
Huihong Luo41ea7d12022-09-26 11:32:52 -0700194 usage(pname, displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700195 return 1;
196 }
197
198 void const* mapbase = MAP_FAILED;
199 ssize_t mapsize = -1;
200
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000201 void* base = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700202
Martijn Coenen48b74082017-07-24 09:19:26 +0200203 // setThreadPoolMaxThreadCount(0) actually tells the kernel it's
204 // not allowed to spawn any additional threads, but we still spawn
205 // a binder thread from userspace when we call startThreadPool().
206 // See b/36066697 for rationale
207 ProcessState::self()->setThreadPoolMaxThreadCount(0);
208 ProcessState::self()->startThreadPool();
209
chaviwbc100492020-08-18 16:06:40 -0700210 sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
Dominik Laskowski0bda2e32021-03-23 16:24:01 -0700211 status_t result = ScreenshotClient::captureDisplay(*displayId, captureListener);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000212 if (result != NO_ERROR) {
213 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700214 return 1;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700215 }
216
chaviwbc100492020-08-18 16:06:40 -0700217 ScreenCaptureResults captureResults = captureListener->waitForResults();
Patrick Williams8d455722022-08-19 14:31:26 +0000218 if (!captureResults.fenceResult.ok()) {
chaviwbc100492020-08-18 16:06:40 -0700219 close(fd);
220 return 1;
221 }
chaviwbc27bc72020-07-27 16:44:35 -0700222 ui::Dataspace dataspace = captureResults.capturedDataspace;
223 sp<GraphicBuffer> buffer = captureResults.buffer;
224
225 result = buffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000226
chaviw7f4dc7e2018-09-11 13:59:30 -0700227 if (base == nullptr || result != NO_ERROR) {
228 String8 reason;
chaviwa69a1b82019-04-30 16:53:33 -0700229 if (result != NO_ERROR) {
230 reason.appendFormat(" Error Code: %d", result);
chaviw7f4dc7e2018-09-11 13:59:30 -0700231 } else {
chaviwa69a1b82019-04-30 16:53:33 -0700232 reason = "Failed to write to buffer";
chaviw7f4dc7e2018-09-11 13:59:30 -0700233 }
234 fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str());
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000235 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700236 return 1;
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000237 }
238
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000239 if (png) {
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400240 AndroidBitmapInfo info;
chaviwbc27bc72020-07-27 16:44:35 -0700241 info.format = flinger2bitmapFormat(buffer->getPixelFormat());
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400242 info.flags = ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
chaviwbc27bc72020-07-27 16:44:35 -0700243 info.width = buffer->getWidth();
244 info.height = buffer->getHeight();
245 info.stride = buffer->getStride() * bytesPerPixel(buffer->getPixelFormat());
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400246
chaviwbc27bc72020-07-27 16:44:35 -0700247 int result = AndroidBitmap_compress(&info, static_cast<int32_t>(dataspace), base,
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400248 ANDROID_BITMAP_COMPRESS_FORMAT_PNG, 100, &fd,
249 [](void* fdPtr, const void* data, size_t size) -> bool {
250 int bytesWritten = write(*static_cast<int*>(fdPtr),
251 data, size);
252 return bytesWritten == size;
253 });
254
255 if (result != ANDROID_BITMAP_RESULT_SUCCESS) {
256 fprintf(stderr, "Failed to compress PNG (error code: %d)\n", result);
257 }
258
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000259 if (fn != NULL) {
260 notifyMediaScanner(fn);
261 }
262 } else {
chaviwbc27bc72020-07-27 16:44:35 -0700263 uint32_t w = buffer->getWidth();
264 uint32_t h = buffer->getHeight();
265 uint32_t s = buffer->getStride();
266 uint32_t f = buffer->getPixelFormat();
267 uint32_t c = dataSpaceToInt(dataspace);
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400268
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000269 write(fd, &w, 4);
270 write(fd, &h, 4);
271 write(fd, &f, 4);
272 write(fd, &c, 4);
273 size_t Bpp = bytesPerPixel(f);
274 for (size_t y=0 ; y<h ; y++) {
275 write(fd, base, w*Bpp);
276 base = (void *)((char *)base + s*Bpp);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700277 }
278 }
279 close(fd);
280 if (mapbase != MAP_FAILED) {
281 munmap((void *)mapbase, mapsize);
282 }
Josh Gao90982582017-06-19 13:38:20 -0700283
Steven Morelanda89ae862018-05-24 17:48:28 -0700284 return 0;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700285}