blob: 7e6a521ca46c4eca734a875ef99bd23b2bce0f17 [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
Dominik Laskowski0bda2e32021-03-23 16:24:01 -070048static void usage(const char* pname, DisplayId displayId)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070049{
50 fprintf(stderr,
51 "usage: %s [-hp] [-d display-id] [FILENAME]\n"
52 " -h: this message\n"
53 " -p: save the file as a png.\n"
Dominik Laskowski0bda2e32021-03-23 16:24:01 -070054 " -d: specify the display ID to capture (default: %s)\n"
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080055 " see \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n"
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070056 "If FILENAME ends with .png it will be saved as a png.\n"
57 "If FILENAME is not given, the results will be printed to stdout.\n",
Marin Shalamanov11599ee2020-07-27 21:31:48 +020058 pname, to_string(displayId).c_str());
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070059}
60
Derek Sollenbergera3ef0942020-04-08 15:47:55 -040061static int32_t flinger2bitmapFormat(PixelFormat f)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070062{
63 switch (f) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070064 case PIXEL_FORMAT_RGB_565:
Derek Sollenbergera3ef0942020-04-08 15:47:55 -040065 return ANDROID_BITMAP_FORMAT_RGB_565;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070066 default:
Derek Sollenbergera3ef0942020-04-08 15:47:55 -040067 return ANDROID_BITMAP_FORMAT_RGBA_8888;
Romain Guy26a2b972017-04-17 09:39:51 -070068 }
69}
70
Peiyong Lin10a34d12018-09-19 13:56:12 -070071static uint32_t dataSpaceToInt(ui::Dataspace d)
Romain Guy26a2b972017-04-17 09:39:51 -070072{
73 switch (d) {
Peiyong Lin10a34d12018-09-19 13:56:12 -070074 case ui::Dataspace::V0_SRGB:
Romain Guy26a2b972017-04-17 09:39:51 -070075 return COLORSPACE_SRGB;
Peiyong Lin10a34d12018-09-19 13:56:12 -070076 case ui::Dataspace::DISPLAY_P3:
Romain Guy26a2b972017-04-17 09:39:51 -070077 return COLORSPACE_DISPLAY_P3;
78 default:
79 return COLORSPACE_UNKNOWN;
80 }
81}
82
Umair Khancfed2322014-01-15 08:08:50 -050083static status_t notifyMediaScanner(const char* fileName) {
Dichen Zhangd31f2192020-03-12 12:25:09 -070084 std::string filePath("file://");
85 filePath.append(fileName);
Dichen Zhangd31f2192020-03-12 12:25:09 -070086 char *cmd[] = {
87 (char*) "am",
88 (char*) "broadcast",
Dichen Zhange361a262020-04-17 10:05:49 -070089 (char*) "-a",
Dichen Zhangd31f2192020-03-12 12:25:09 -070090 (char*) "android.intent.action.MEDIA_SCANNER_SCAN_FILE",
91 (char*) "-d",
George Burgess IV5c46fb62020-03-16 12:07:30 -070092 &filePath[0],
Dichen Zhangd31f2192020-03-12 12:25:09 -070093 nullptr
94 };
95
96 int status;
97 int pid = fork();
98 if (pid < 0){
99 fprintf(stderr, "Unable to fork in order to send intent for media scanner.\n");
100 return UNKNOWN_ERROR;
101 }
102 if (pid == 0){
103 int fd = open("/dev/null", O_WRONLY);
104 if (fd < 0){
105 fprintf(stderr, "Unable to open /dev/null for media scanner stdout redirection.\n");
106 exit(1);
107 }
108 dup2(fd, 1);
109 int result = execvp(cmd[0], cmd);
110 close(fd);
111 exit(result);
112 }
113 wait(&status);
114
115 if (status < 0) {
Umair Khancfed2322014-01-15 08:08:50 -0500116 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
117 return UNKNOWN_ERROR;
118 }
119 return NO_ERROR;
120}
121
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700122int main(int argc, char** argv)
123{
Dominik Laskowski0bda2e32021-03-23 16:24:01 -0700124 std::optional<DisplayId> displayId = SurfaceComposerClient::getInternalDisplayId();
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800125 if (!displayId) {
Dominik Laskowski0bda2e32021-03-23 16:24:01 -0700126 fprintf(stderr, "Failed to get ID for internal display\n");
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800127 return 1;
128 }
129
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700130 const char* pname = argv[0];
131 bool png = false;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700132 int c;
133 while ((c = getopt(argc, argv, "phd:")) != -1) {
134 switch (c) {
135 case 'p':
136 png = true;
137 break;
138 case 'd':
Dominik Laskowski0bda2e32021-03-23 16:24:01 -0700139 displayId = DisplayId::fromValue(atoll(optarg));
140 if (!displayId) {
141 fprintf(stderr, "Invalid display ID\n");
142 return 1;
143 }
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700144 break;
145 case '?':
146 case 'h':
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800147 usage(pname, *displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700148 return 1;
149 }
150 }
151 argc -= optind;
152 argv += optind;
153
154 int fd = -1;
Andreas Gampecfedceb2014-09-30 21:48:18 -0700155 const char* fn = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700156 if (argc == 0) {
157 fd = dup(STDOUT_FILENO);
158 } else if (argc == 1) {
Umair Khancfed2322014-01-15 08:08:50 -0500159 fn = argv[0];
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700160 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
161 if (fd == -1) {
162 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
163 return 1;
164 }
165 const int len = strlen(fn);
166 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
167 png = true;
168 }
169 }
Prathmesh Prabhubf89ae52016-03-10 15:23:34 -0800170
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700171 if (fd == -1) {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800172 usage(pname, *displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700173 return 1;
174 }
175
176 void const* mapbase = MAP_FAILED;
177 ssize_t mapsize = -1;
178
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000179 void* base = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700180
Martijn Coenen48b74082017-07-24 09:19:26 +0200181 // setThreadPoolMaxThreadCount(0) actually tells the kernel it's
182 // not allowed to spawn any additional threads, but we still spawn
183 // a binder thread from userspace when we call startThreadPool().
184 // See b/36066697 for rationale
185 ProcessState::self()->setThreadPoolMaxThreadCount(0);
186 ProcessState::self()->startThreadPool();
187
chaviwbc100492020-08-18 16:06:40 -0700188 sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener();
Dominik Laskowski0bda2e32021-03-23 16:24:01 -0700189 status_t result = ScreenshotClient::captureDisplay(*displayId, captureListener);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000190 if (result != NO_ERROR) {
191 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700192 return 1;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700193 }
194
chaviwbc100492020-08-18 16:06:40 -0700195 ScreenCaptureResults captureResults = captureListener->waitForResults();
196 if (captureResults.result != NO_ERROR) {
197 close(fd);
198 return 1;
199 }
chaviwbc27bc72020-07-27 16:44:35 -0700200 ui::Dataspace dataspace = captureResults.capturedDataspace;
201 sp<GraphicBuffer> buffer = captureResults.buffer;
202
203 result = buffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000204
chaviw7f4dc7e2018-09-11 13:59:30 -0700205 if (base == nullptr || result != NO_ERROR) {
206 String8 reason;
chaviwa69a1b82019-04-30 16:53:33 -0700207 if (result != NO_ERROR) {
208 reason.appendFormat(" Error Code: %d", result);
chaviw7f4dc7e2018-09-11 13:59:30 -0700209 } else {
chaviwa69a1b82019-04-30 16:53:33 -0700210 reason = "Failed to write to buffer";
chaviw7f4dc7e2018-09-11 13:59:30 -0700211 }
212 fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str());
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000213 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700214 return 1;
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000215 }
216
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000217 if (png) {
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400218 AndroidBitmapInfo info;
chaviwbc27bc72020-07-27 16:44:35 -0700219 info.format = flinger2bitmapFormat(buffer->getPixelFormat());
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400220 info.flags = ANDROID_BITMAP_FLAGS_ALPHA_PREMUL;
chaviwbc27bc72020-07-27 16:44:35 -0700221 info.width = buffer->getWidth();
222 info.height = buffer->getHeight();
223 info.stride = buffer->getStride() * bytesPerPixel(buffer->getPixelFormat());
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400224
chaviwbc27bc72020-07-27 16:44:35 -0700225 int result = AndroidBitmap_compress(&info, static_cast<int32_t>(dataspace), base,
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400226 ANDROID_BITMAP_COMPRESS_FORMAT_PNG, 100, &fd,
227 [](void* fdPtr, const void* data, size_t size) -> bool {
228 int bytesWritten = write(*static_cast<int*>(fdPtr),
229 data, size);
230 return bytesWritten == size;
231 });
232
233 if (result != ANDROID_BITMAP_RESULT_SUCCESS) {
234 fprintf(stderr, "Failed to compress PNG (error code: %d)\n", result);
235 }
236
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000237 if (fn != NULL) {
238 notifyMediaScanner(fn);
239 }
240 } else {
chaviwbc27bc72020-07-27 16:44:35 -0700241 uint32_t w = buffer->getWidth();
242 uint32_t h = buffer->getHeight();
243 uint32_t s = buffer->getStride();
244 uint32_t f = buffer->getPixelFormat();
245 uint32_t c = dataSpaceToInt(dataspace);
Derek Sollenbergera3ef0942020-04-08 15:47:55 -0400246
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000247 write(fd, &w, 4);
248 write(fd, &h, 4);
249 write(fd, &f, 4);
250 write(fd, &c, 4);
251 size_t Bpp = bytesPerPixel(f);
252 for (size_t y=0 ; y<h ; y++) {
253 write(fd, base, w*Bpp);
254 base = (void *)((char *)base + s*Bpp);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700255 }
256 }
257 close(fd);
258 if (mapbase != MAP_FAILED) {
259 munmap((void *)mapbase, mapsize);
260 }
Josh Gao90982582017-06-19 13:38:20 -0700261
Steven Morelanda89ae862018-05-24 17:48:28 -0700262 return 0;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700263}