blob: 5c08704a662391b308bb71308a28725e04799926 [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
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070033#include <gui/ISurfaceComposer.h>
chaviw35109242020-08-18 16:06:40 -070034#include <gui/SurfaceComposerClient.h>
35#include <gui/SyncScreenCaptureListener.h>
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070036
Dan Stozacf70d712015-06-09 16:47:33 -070037#include <ui/DisplayInfo.h>
Peiyong Lin10a34d12018-09-19 13:56:12 -070038#include <ui/GraphicTypes.h>
Mathias Agopian0137fb82013-03-20 15:38:07 -070039#include <ui/PixelFormat.h>
40
Romain Guy26a2b972017-04-17 09:39:51 -070041#include <system/graphics.h>
42
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070043using namespace android;
44
Romain Guy26a2b972017-04-17 09:39:51 -070045#define COLORSPACE_UNKNOWN 0
46#define COLORSPACE_SRGB 1
47#define COLORSPACE_DISPLAY_P3 2
48
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080049static void usage(const char* pname, PhysicalDisplayId displayId)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070050{
51 fprintf(stderr,
52 "usage: %s [-hp] [-d display-id] [FILENAME]\n"
53 " -h: this message\n"
54 " -p: save the file as a png.\n"
Marin Shalamanov11599ee2020-07-27 21:31:48 +020055 " -d: specify the physical display ID to capture (default: %s)\n"
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080056 " see \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n"
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070057 "If FILENAME ends with .png it will be saved as a png.\n"
58 "If FILENAME is not given, the results will be printed to stdout.\n",
Marin Shalamanov11599ee2020-07-27 21:31:48 +020059 pname, to_string(displayId).c_str());
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070060}
61
Derek Sollenbergera3ef0942020-04-08 15:47:55 -040062static int32_t flinger2bitmapFormat(PixelFormat f)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070063{
64 switch (f) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070065 case PIXEL_FORMAT_RGB_565:
Derek Sollenbergera3ef0942020-04-08 15:47:55 -040066 return ANDROID_BITMAP_FORMAT_RGB_565;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070067 default:
Derek Sollenbergera3ef0942020-04-08 15:47:55 -040068 return ANDROID_BITMAP_FORMAT_RGBA_8888;
Romain Guy26a2b972017-04-17 09:39:51 -070069 }
70}
71
Peiyong Lin10a34d12018-09-19 13:56:12 -070072static uint32_t dataSpaceToInt(ui::Dataspace d)
Romain Guy26a2b972017-04-17 09:39:51 -070073{
74 switch (d) {
Peiyong Lin10a34d12018-09-19 13:56:12 -070075 case ui::Dataspace::V0_SRGB:
Romain Guy26a2b972017-04-17 09:39:51 -070076 return COLORSPACE_SRGB;
Peiyong Lin10a34d12018-09-19 13:56:12 -070077 case ui::Dataspace::DISPLAY_P3:
Romain Guy26a2b972017-04-17 09:39:51 -070078 return COLORSPACE_DISPLAY_P3;
79 default:
80 return COLORSPACE_UNKNOWN;
81 }
82}
83
Umair Khancfed2322014-01-15 08:08:50 -050084static status_t notifyMediaScanner(const char* fileName) {
Dichen Zhangd31f2192020-03-12 12:25:09 -070085 std::string filePath("file://");
86 filePath.append(fileName);
Dichen Zhangd31f2192020-03-12 12:25:09 -070087 char *cmd[] = {
88 (char*) "am",
89 (char*) "broadcast",
Dichen Zhange361a262020-04-17 10:05:49 -070090 (char*) "-a",
Dichen Zhangd31f2192020-03-12 12:25:09 -070091 (char*) "android.intent.action.MEDIA_SCANNER_SCAN_FILE",
92 (char*) "-d",
George Burgess IV5c46fb62020-03-16 12:07:30 -070093 &filePath[0],
Dichen Zhangd31f2192020-03-12 12:25:09 -070094 nullptr
95 };
96
97 int status;
98 int pid = fork();
99 if (pid < 0){
100 fprintf(stderr, "Unable to fork in order to send intent for media scanner.\n");
101 return UNKNOWN_ERROR;
102 }
103 if (pid == 0){
104 int fd = open("/dev/null", O_WRONLY);
105 if (fd < 0){
106 fprintf(stderr, "Unable to open /dev/null for media scanner stdout redirection.\n");
107 exit(1);
108 }
109 dup2(fd, 1);
110 int result = execvp(cmd[0], cmd);
111 close(fd);
112 exit(result);
113 }
114 wait(&status);
115
116 if (status < 0) {
Umair Khancfed2322014-01-15 08:08:50 -0500117 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
118 return UNKNOWN_ERROR;
119 }
120 return NO_ERROR;
121}
122
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700123int main(int argc, char** argv)
124{
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800125 std::optional<PhysicalDisplayId> displayId = SurfaceComposerClient::getInternalDisplayId();
126 if (!displayId) {
127 fprintf(stderr, "Failed to get token for internal display\n");
128 return 1;
129 }
130
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700131 const char* pname = argv[0];
132 bool png = false;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700133 int c;
134 while ((c = getopt(argc, argv, "phd:")) != -1) {
135 switch (c) {
136 case 'p':
137 png = true;
138 break;
139 case 'd':
Marin Shalamanov11599ee2020-07-27 21:31:48 +0200140 displayId = PhysicalDisplayId(atoll(optarg));
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700141 break;
142 case '?':
143 case 'h':
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800144 usage(pname, *displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700145 return 1;
146 }
147 }
148 argc -= optind;
149 argv += optind;
150
151 int fd = -1;
Andreas Gampecfedceb2014-09-30 21:48:18 -0700152 const char* fn = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700153 if (argc == 0) {
154 fd = dup(STDOUT_FILENO);
155 } else if (argc == 1) {
Umair Khancfed2322014-01-15 08:08:50 -0500156 fn = argv[0];
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700157 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
158 if (fd == -1) {
159 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
160 return 1;
161 }
162 const int len = strlen(fn);
163 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
164 png = true;
165 }
166 }
Prathmesh Prabhubf89ae52016-03-10 15:23:34 -0800167
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700168 if (fd == -1) {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800169 usage(pname, *displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700170 return 1;
171 }
172
173 void const* mapbase = MAP_FAILED;
174 ssize_t mapsize = -1;
175
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000176 void* base = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700177
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
chaviw35109242020-08-18 16:06:40 -0700185 sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
186 status_t result = ScreenshotClient::captureDisplay(displayId->value, captureListener);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000187 if (result != NO_ERROR) {
188 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700189 return 1;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700190 }
191
chaviw35109242020-08-18 16:06:40 -0700192 ScreenCaptureResults captureResults = captureListener->waitForResults();
193 if (captureResults.result != NO_ERROR) {
194 close(fd);
195 return 1;
196 }
chaviwbc27bc72020-07-27 16:44:35 -0700197 ui::Dataspace dataspace = captureResults.capturedDataspace;
198 sp<GraphicBuffer> buffer = captureResults.buffer;
199
200 result = buffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000201
chaviw7f4dc7e2018-09-11 13:59:30 -0700202 if (base == nullptr || result != NO_ERROR) {
203 String8 reason;
chaviwa69a1b82019-04-30 16:53:33 -0700204 if (result != NO_ERROR) {
205 reason.appendFormat(" Error Code: %d", result);
chaviw7f4dc7e2018-09-11 13:59:30 -0700206 } else {
chaviwa69a1b82019-04-30 16:53:33 -0700207 reason = "Failed to write to buffer";
chaviw7f4dc7e2018-09-11 13:59:30 -0700208 }
209 fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str());
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000210 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700211 return 1;
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000212 }
213
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000214 if (png) {
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400215 AndroidBitmapInfo info;
chaviwbc27bc72020-07-27 16:44:35 -0700216 info.format = flinger2bitmapFormat(buffer->getPixelFormat());
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400217 info.flags = ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
chaviwbc27bc72020-07-27 16:44:35 -0700218 info.width = buffer->getWidth();
219 info.height = buffer->getHeight();
220 info.stride = buffer->getStride() * bytesPerPixel(buffer->getPixelFormat());
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400221
chaviwbc27bc72020-07-27 16:44:35 -0700222 int result = AndroidBitmap_compress(&info, static_cast<int32_t>(dataspace), base,
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400223 ANDROID_BITMAP_COMPRESS_FORMAT_PNG, 100, &fd,
224 [](void* fdPtr, const void* data, size_t size) -> bool {
225 int bytesWritten = write(*static_cast<int*>(fdPtr),
226 data, size);
227 return bytesWritten == size;
228 });
229
230 if (result != ANDROID_BITMAP_RESULT_SUCCESS) {
231 fprintf(stderr, "Failed to compress PNG (error code: %d)\n", result);
232 }
233
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000234 if (fn != NULL) {
235 notifyMediaScanner(fn);
236 }
237 } else {
chaviwbc27bc72020-07-27 16:44:35 -0700238 uint32_t w = buffer->getWidth();
239 uint32_t h = buffer->getHeight();
240 uint32_t s = buffer->getStride();
241 uint32_t f = buffer->getPixelFormat();
242 uint32_t c = dataSpaceToInt(dataspace);
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400243
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000244 write(fd, &w, 4);
245 write(fd, &h, 4);
246 write(fd, &f, 4);
247 write(fd, &c, 4);
248 size_t Bpp = bytesPerPixel(f);
249 for (size_t y=0 ; y<h ; y++) {
250 write(fd, base, w*Bpp);
251 base = (void *)((char *)base + s*Bpp);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700252 }
253 }
254 close(fd);
255 if (mapbase != MAP_FAILED) {
256 munmap((void *)mapbase, mapsize);
257 }
Josh Gao90982582017-06-19 13:38:20 -0700258
Steven Morelanda89ae862018-05-24 17:48:28 -0700259 return 0;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700260}