Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 1 | /* |
| 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 Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 23 | |
| 24 | #include <linux/fb.h> |
| 25 | #include <sys/ioctl.h> |
| 26 | #include <sys/mman.h> |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 27 | #include <sys/wait.h> |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 28 | |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 29 | #include <android/bitmap.h> |
| 30 | |
Mathias Agopian | 0678a8c | 2013-03-19 20:56:00 -0700 | [diff] [blame] | 31 | #include <binder/ProcessState.h> |
| 32 | |
Chavi Weingarten | 797bdc9 | 2020-09-10 20:55:11 +0000 | [diff] [blame] | 33 | #include <gui/ISurfaceComposer.h> |
chaviw | bc10049 | 2020-08-18 16:06:40 -0700 | [diff] [blame] | 34 | #include <gui/SurfaceComposerClient.h> |
| 35 | #include <gui/SyncScreenCaptureListener.h> |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 36 | |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 37 | #include <ui/GraphicTypes.h> |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 38 | #include <ui/PixelFormat.h> |
| 39 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 40 | #include <system/graphics.h> |
| 41 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 42 | using namespace android; |
| 43 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 44 | #define COLORSPACE_UNKNOWN 0 |
| 45 | #define COLORSPACE_SRGB 1 |
| 46 | #define COLORSPACE_DISPLAY_P3 2 |
| 47 | |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame^] | 48 | static void usage(const char* pname, std::optional<PhysicalDisplayId> displayId) |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 49 | { |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame^] | 50 | std::string defaultDisplayStr = ""; |
| 51 | if (!displayId) { |
| 52 | defaultDisplayStr = ""; |
| 53 | } else { |
| 54 | defaultDisplayStr = " (default: " + to_string(*displayId) + ")"; |
| 55 | } |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 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" |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame^] | 60 | " -d: specify the display ID to capture%s\n" |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 61 | " see \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n" |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 62 | "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 Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame^] | 64 | pname, defaultDisplayStr.c_str()); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 67 | static int32_t flinger2bitmapFormat(PixelFormat f) |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 68 | { |
| 69 | switch (f) { |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 70 | case PIXEL_FORMAT_RGB_565: |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 71 | return ANDROID_BITMAP_FORMAT_RGB_565; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 72 | default: |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 73 | return ANDROID_BITMAP_FORMAT_RGBA_8888; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 77 | static uint32_t dataSpaceToInt(ui::Dataspace d) |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 78 | { |
| 79 | switch (d) { |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 80 | case ui::Dataspace::V0_SRGB: |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 81 | return COLORSPACE_SRGB; |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 82 | case ui::Dataspace::DISPLAY_P3: |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 83 | return COLORSPACE_DISPLAY_P3; |
| 84 | default: |
| 85 | return COLORSPACE_UNKNOWN; |
| 86 | } |
| 87 | } |
| 88 | |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 89 | static status_t notifyMediaScanner(const char* fileName) { |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 90 | std::string filePath("file://"); |
| 91 | filePath.append(fileName); |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 92 | char *cmd[] = { |
| 93 | (char*) "am", |
| 94 | (char*) "broadcast", |
Dichen Zhang | e361a26 | 2020-04-17 10:05:49 -0700 | [diff] [blame] | 95 | (char*) "-a", |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 96 | (char*) "android.intent.action.MEDIA_SCANNER_SCAN_FILE", |
| 97 | (char*) "-d", |
George Burgess IV | 5c46fb6 | 2020-03-16 12:07:30 -0700 | [diff] [blame] | 98 | &filePath[0], |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 99 | 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 Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 122 | fprintf(stderr, "Unable to broadcast intent for media scanner.\n"); |
| 123 | return UNKNOWN_ERROR; |
| 124 | } |
| 125 | return NO_ERROR; |
| 126 | } |
| 127 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 128 | int main(int argc, char** argv) |
| 129 | { |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame^] | 130 | const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds(); |
| 131 | if (ids.empty()) { |
| 132 | fprintf(stderr, "Failed to get ID for any displays.\n"); |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 133 | return 1; |
| 134 | } |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame^] | 135 | std::optional<PhysicalDisplayId> displayId; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 136 | const char* pname = argv[0]; |
| 137 | bool png = false; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 138 | 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 Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame^] | 145 | displayId = DisplayId::fromValue<PhysicalDisplayId>(atoll(optarg)); |
Dominik Laskowski | 0bda2e3 | 2021-03-23 16:24:01 -0700 | [diff] [blame] | 146 | if (!displayId) { |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame^] | 147 | fprintf(stderr, "Invalid display ID: %s\n", optarg); |
Dominik Laskowski | 0bda2e3 | 2021-03-23 16:24:01 -0700 | [diff] [blame] | 148 | return 1; |
| 149 | } |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 150 | break; |
| 151 | case '?': |
| 152 | case 'h': |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame^] | 153 | if (ids.size() == 1) { |
| 154 | displayId = ids.front(); |
| 155 | } |
| 156 | usage(pname, displayId); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 157 | return 1; |
| 158 | } |
| 159 | } |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame^] | 160 | |
| 161 | if (!displayId) { // no diplsay id is specified |
| 162 | if (ids.size() == 1) { |
| 163 | displayId = ids.front(); |
| 164 | } else { |
| 165 | fprintf(stderr, "Please specify a display ID (-d display-id) for multi-display device.\n"); |
| 166 | fprintf(stderr, "See \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n"); |
| 167 | return 1; |
| 168 | } |
| 169 | } |
| 170 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 171 | argc -= optind; |
| 172 | argv += optind; |
| 173 | |
| 174 | int fd = -1; |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 175 | const char* fn = NULL; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 176 | if (argc == 0) { |
| 177 | fd = dup(STDOUT_FILENO); |
| 178 | } else if (argc == 1) { |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 179 | fn = argv[0]; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 180 | fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664); |
| 181 | if (fd == -1) { |
| 182 | fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno)); |
| 183 | return 1; |
| 184 | } |
| 185 | const int len = strlen(fn); |
| 186 | if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) { |
| 187 | png = true; |
| 188 | } |
| 189 | } |
Prathmesh Prabhu | bf89ae5 | 2016-03-10 15:23:34 -0800 | [diff] [blame] | 190 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 191 | if (fd == -1) { |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame^] | 192 | usage(pname, displayId); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 193 | return 1; |
| 194 | } |
| 195 | |
| 196 | void const* mapbase = MAP_FAILED; |
| 197 | ssize_t mapsize = -1; |
| 198 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 199 | void* base = NULL; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 200 | |
Martijn Coenen | 48b7408 | 2017-07-24 09:19:26 +0200 | [diff] [blame] | 201 | // setThreadPoolMaxThreadCount(0) actually tells the kernel it's |
| 202 | // not allowed to spawn any additional threads, but we still spawn |
| 203 | // a binder thread from userspace when we call startThreadPool(). |
| 204 | // See b/36066697 for rationale |
| 205 | ProcessState::self()->setThreadPoolMaxThreadCount(0); |
| 206 | ProcessState::self()->startThreadPool(); |
| 207 | |
chaviw | bc10049 | 2020-08-18 16:06:40 -0700 | [diff] [blame] | 208 | sp<SyncScreenCaptureListener> captureListener = new SyncScreenCaptureListener(); |
Dominik Laskowski | 0bda2e3 | 2021-03-23 16:24:01 -0700 | [diff] [blame] | 209 | status_t result = ScreenshotClient::captureDisplay(*displayId, captureListener); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 210 | if (result != NO_ERROR) { |
| 211 | close(fd); |
Steven Moreland | a89ae86 | 2018-05-24 17:48:28 -0700 | [diff] [blame] | 212 | return 1; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 213 | } |
| 214 | |
chaviw | bc10049 | 2020-08-18 16:06:40 -0700 | [diff] [blame] | 215 | ScreenCaptureResults captureResults = captureListener->waitForResults(); |
Patrick Williams | 8d45572 | 2022-08-19 14:31:26 +0000 | [diff] [blame] | 216 | if (!captureResults.fenceResult.ok()) { |
chaviw | bc10049 | 2020-08-18 16:06:40 -0700 | [diff] [blame] | 217 | close(fd); |
| 218 | return 1; |
| 219 | } |
chaviw | bc27bc7 | 2020-07-27 16:44:35 -0700 | [diff] [blame] | 220 | ui::Dataspace dataspace = captureResults.capturedDataspace; |
| 221 | sp<GraphicBuffer> buffer = captureResults.buffer; |
| 222 | |
| 223 | result = buffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 224 | |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 225 | if (base == nullptr || result != NO_ERROR) { |
| 226 | String8 reason; |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 227 | if (result != NO_ERROR) { |
| 228 | reason.appendFormat(" Error Code: %d", result); |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 229 | } else { |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 230 | reason = "Failed to write to buffer"; |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 231 | } |
| 232 | fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str()); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 233 | close(fd); |
Steven Moreland | a89ae86 | 2018-05-24 17:48:28 -0700 | [diff] [blame] | 234 | return 1; |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 237 | if (png) { |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 238 | AndroidBitmapInfo info; |
chaviw | bc27bc7 | 2020-07-27 16:44:35 -0700 | [diff] [blame] | 239 | info.format = flinger2bitmapFormat(buffer->getPixelFormat()); |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 240 | info.flags = ANDROID_BITMAP_FLAGS_ALPHA_PREMUL; |
chaviw | bc27bc7 | 2020-07-27 16:44:35 -0700 | [diff] [blame] | 241 | info.width = buffer->getWidth(); |
| 242 | info.height = buffer->getHeight(); |
| 243 | info.stride = buffer->getStride() * bytesPerPixel(buffer->getPixelFormat()); |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 244 | |
chaviw | bc27bc7 | 2020-07-27 16:44:35 -0700 | [diff] [blame] | 245 | int result = AndroidBitmap_compress(&info, static_cast<int32_t>(dataspace), base, |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 246 | ANDROID_BITMAP_COMPRESS_FORMAT_PNG, 100, &fd, |
| 247 | [](void* fdPtr, const void* data, size_t size) -> bool { |
| 248 | int bytesWritten = write(*static_cast<int*>(fdPtr), |
| 249 | data, size); |
| 250 | return bytesWritten == size; |
| 251 | }); |
| 252 | |
| 253 | if (result != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 254 | fprintf(stderr, "Failed to compress PNG (error code: %d)\n", result); |
| 255 | } |
| 256 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 257 | if (fn != NULL) { |
| 258 | notifyMediaScanner(fn); |
| 259 | } |
| 260 | } else { |
chaviw | bc27bc7 | 2020-07-27 16:44:35 -0700 | [diff] [blame] | 261 | uint32_t w = buffer->getWidth(); |
| 262 | uint32_t h = buffer->getHeight(); |
| 263 | uint32_t s = buffer->getStride(); |
| 264 | uint32_t f = buffer->getPixelFormat(); |
| 265 | uint32_t c = dataSpaceToInt(dataspace); |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 266 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 267 | write(fd, &w, 4); |
| 268 | write(fd, &h, 4); |
| 269 | write(fd, &f, 4); |
| 270 | write(fd, &c, 4); |
| 271 | size_t Bpp = bytesPerPixel(f); |
| 272 | for (size_t y=0 ; y<h ; y++) { |
| 273 | write(fd, base, w*Bpp); |
| 274 | base = (void *)((char *)base + s*Bpp); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | close(fd); |
| 278 | if (mapbase != MAP_FAILED) { |
| 279 | munmap((void *)mapbase, mapsize); |
| 280 | } |
Josh Gao | 9098258 | 2017-06-19 13:38:20 -0700 | [diff] [blame] | 281 | |
Steven Moreland | a89ae86 | 2018-05-24 17:48:28 -0700 | [diff] [blame] | 282 | return 0; |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 283 | } |