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 | |
Dominik Laskowski | 622c4ae | 2023-05-26 12:10:16 -0400 | [diff] [blame] | 33 | #include <ftl/concat.h> |
| 34 | #include <ftl/optional.h> |
Chavi Weingarten | 797bdc9 | 2020-09-10 20:55:11 +0000 | [diff] [blame] | 35 | #include <gui/ISurfaceComposer.h> |
chaviw | bc10049 | 2020-08-18 16:06:40 -0700 | [diff] [blame] | 36 | #include <gui/SurfaceComposerClient.h> |
| 37 | #include <gui/SyncScreenCaptureListener.h> |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 38 | |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 39 | #include <ui/GraphicTypes.h> |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 40 | #include <ui/PixelFormat.h> |
| 41 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 42 | #include <system/graphics.h> |
| 43 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 44 | using namespace android; |
| 45 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 46 | #define COLORSPACE_UNKNOWN 0 |
| 47 | #define COLORSPACE_SRGB 1 |
| 48 | #define COLORSPACE_DISPLAY_P3 2 |
| 49 | |
Dominik Laskowski | 622c4ae | 2023-05-26 12:10:16 -0400 | [diff] [blame] | 50 | void usage(const char* pname, ftl::Optional<DisplayId> displayIdOpt) { |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 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" |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame] | 55 | " -d: specify the display ID to capture%s\n" |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 56 | " see \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n" |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 57 | "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", |
Dominik Laskowski | 622c4ae | 2023-05-26 12:10:16 -0400 | [diff] [blame] | 59 | pname, |
| 60 | displayIdOpt |
| 61 | .transform([](DisplayId id) { |
| 62 | return std::string(ftl::Concat(" (default: ", id.value, ')').str()); |
| 63 | }) |
| 64 | .value_or(std::string()) |
| 65 | .c_str()); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 68 | static int32_t flinger2bitmapFormat(PixelFormat f) |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 69 | { |
| 70 | switch (f) { |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 71 | case PIXEL_FORMAT_RGB_565: |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 72 | return ANDROID_BITMAP_FORMAT_RGB_565; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 73 | default: |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 74 | return ANDROID_BITMAP_FORMAT_RGBA_8888; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 78 | static uint32_t dataSpaceToInt(ui::Dataspace d) |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 79 | { |
| 80 | switch (d) { |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 81 | case ui::Dataspace::V0_SRGB: |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 82 | return COLORSPACE_SRGB; |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 83 | case ui::Dataspace::DISPLAY_P3: |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 84 | return COLORSPACE_DISPLAY_P3; |
| 85 | default: |
| 86 | return COLORSPACE_UNKNOWN; |
| 87 | } |
| 88 | } |
| 89 | |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 90 | static status_t notifyMediaScanner(const char* fileName) { |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 91 | std::string filePath("file://"); |
| 92 | filePath.append(fileName); |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 93 | char *cmd[] = { |
| 94 | (char*) "am", |
| 95 | (char*) "broadcast", |
Dichen Zhang | e361a26 | 2020-04-17 10:05:49 -0700 | [diff] [blame] | 96 | (char*) "-a", |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 97 | (char*) "android.intent.action.MEDIA_SCANNER_SCAN_FILE", |
| 98 | (char*) "-d", |
George Burgess IV | 5c46fb6 | 2020-03-16 12:07:30 -0700 | [diff] [blame] | 99 | &filePath[0], |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 100 | nullptr |
| 101 | }; |
| 102 | |
| 103 | int status; |
| 104 | int pid = fork(); |
| 105 | if (pid < 0){ |
| 106 | fprintf(stderr, "Unable to fork in order to send intent for media scanner.\n"); |
| 107 | return UNKNOWN_ERROR; |
| 108 | } |
| 109 | if (pid == 0){ |
| 110 | int fd = open("/dev/null", O_WRONLY); |
| 111 | if (fd < 0){ |
| 112 | fprintf(stderr, "Unable to open /dev/null for media scanner stdout redirection.\n"); |
| 113 | exit(1); |
| 114 | } |
| 115 | dup2(fd, 1); |
| 116 | int result = execvp(cmd[0], cmd); |
| 117 | close(fd); |
| 118 | exit(result); |
| 119 | } |
| 120 | wait(&status); |
| 121 | |
| 122 | if (status < 0) { |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 123 | fprintf(stderr, "Unable to broadcast intent for media scanner.\n"); |
| 124 | return UNKNOWN_ERROR; |
| 125 | } |
| 126 | return NO_ERROR; |
| 127 | } |
| 128 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 129 | int main(int argc, char** argv) |
| 130 | { |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame] | 131 | const std::vector<PhysicalDisplayId> ids = SurfaceComposerClient::getPhysicalDisplayIds(); |
| 132 | if (ids.empty()) { |
| 133 | fprintf(stderr, "Failed to get ID for any displays.\n"); |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 134 | return 1; |
| 135 | } |
Dominik Laskowski | 622c4ae | 2023-05-26 12:10:16 -0400 | [diff] [blame] | 136 | std::optional<DisplayId> displayIdOpt; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 137 | const char* pname = argv[0]; |
| 138 | bool png = false; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 139 | int c; |
| 140 | while ((c = getopt(argc, argv, "phd:")) != -1) { |
| 141 | switch (c) { |
| 142 | case 'p': |
| 143 | png = true; |
| 144 | break; |
| 145 | case 'd': |
Dominik Laskowski | 622c4ae | 2023-05-26 12:10:16 -0400 | [diff] [blame] | 146 | displayIdOpt = DisplayId::fromValue(atoll(optarg)); |
| 147 | if (!displayIdOpt) { |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame] | 148 | fprintf(stderr, "Invalid display ID: %s\n", optarg); |
Dominik Laskowski | 0bda2e3 | 2021-03-23 16:24:01 -0700 | [diff] [blame] | 149 | return 1; |
| 150 | } |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 151 | break; |
| 152 | case '?': |
| 153 | case 'h': |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame] | 154 | if (ids.size() == 1) { |
Dominik Laskowski | 622c4ae | 2023-05-26 12:10:16 -0400 | [diff] [blame] | 155 | displayIdOpt = ids.front(); |
| 156 | } |
| 157 | usage(pname, displayIdOpt); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 158 | return 1; |
| 159 | } |
| 160 | } |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame] | 161 | |
Dominik Laskowski | 622c4ae | 2023-05-26 12:10:16 -0400 | [diff] [blame] | 162 | if (!displayIdOpt) { |
| 163 | displayIdOpt = ids.front(); |
Huihong Luo | 6f645f7 | 2022-10-07 19:24:00 -0700 | [diff] [blame] | 164 | if (ids.size() > 1) { |
| 165 | fprintf(stderr, |
| 166 | "[Warning] Multiple displays were found, but no display id was specified! " |
| 167 | "Defaulting to the first display found, however this default is not guaranteed " |
| 168 | "to be consistent across captures. A display id should be specified.\n"); |
| 169 | fprintf(stderr, "A display ID can be specified with the [-d display-id] option.\n"); |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame] | 170 | fprintf(stderr, "See \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n"); |
Huihong Luo | 41ea7d1 | 2022-09-26 11:32:52 -0700 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 174 | argc -= optind; |
| 175 | argv += optind; |
| 176 | |
| 177 | int fd = -1; |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 178 | const char* fn = NULL; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 179 | if (argc == 0) { |
| 180 | fd = dup(STDOUT_FILENO); |
| 181 | } else if (argc == 1) { |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 182 | fn = argv[0]; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 183 | fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664); |
| 184 | if (fd == -1) { |
| 185 | fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno)); |
| 186 | return 1; |
| 187 | } |
| 188 | const int len = strlen(fn); |
| 189 | if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) { |
| 190 | png = true; |
| 191 | } |
| 192 | } |
Prathmesh Prabhu | bf89ae5 | 2016-03-10 15:23:34 -0800 | [diff] [blame] | 193 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 194 | if (fd == -1) { |
Dominik Laskowski | 622c4ae | 2023-05-26 12:10:16 -0400 | [diff] [blame] | 195 | usage(pname, displayIdOpt); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 196 | return 1; |
| 197 | } |
| 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(); |
jeimysantiago | 8ee92d1 | 2023-07-21 15:40:13 +0000 | [diff] [blame^] | 209 | ScreenshotClient::captureDisplay(*displayIdOpt, captureListener); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 210 | |
chaviw | bc10049 | 2020-08-18 16:06:40 -0700 | [diff] [blame] | 211 | ScreenCaptureResults captureResults = captureListener->waitForResults(); |
Patrick Williams | 8d45572 | 2022-08-19 14:31:26 +0000 | [diff] [blame] | 212 | if (!captureResults.fenceResult.ok()) { |
chaviw | bc10049 | 2020-08-18 16:06:40 -0700 | [diff] [blame] | 213 | close(fd); |
jeimysantiago | 8ee92d1 | 2023-07-21 15:40:13 +0000 | [diff] [blame^] | 214 | fprintf(stderr, "Failed to take screenshot. Status: %d\n", |
| 215 | fenceStatus(captureResults.fenceResult)); |
chaviw | bc10049 | 2020-08-18 16:06:40 -0700 | [diff] [blame] | 216 | return 1; |
| 217 | } |
chaviw | bc27bc7 | 2020-07-27 16:44:35 -0700 | [diff] [blame] | 218 | ui::Dataspace dataspace = captureResults.capturedDataspace; |
| 219 | sp<GraphicBuffer> buffer = captureResults.buffer; |
| 220 | |
jeimysantiago | 8ee92d1 | 2023-07-21 15:40:13 +0000 | [diff] [blame^] | 221 | status_t result = buffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 222 | |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 223 | if (base == nullptr || result != NO_ERROR) { |
| 224 | String8 reason; |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 225 | if (result != NO_ERROR) { |
| 226 | reason.appendFormat(" Error Code: %d", result); |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 227 | } else { |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 228 | reason = "Failed to write to buffer"; |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 229 | } |
| 230 | fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str()); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 231 | close(fd); |
Steven Moreland | a89ae86 | 2018-05-24 17:48:28 -0700 | [diff] [blame] | 232 | return 1; |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 235 | if (png) { |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 236 | AndroidBitmapInfo info; |
chaviw | bc27bc7 | 2020-07-27 16:44:35 -0700 | [diff] [blame] | 237 | info.format = flinger2bitmapFormat(buffer->getPixelFormat()); |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 238 | info.flags = ANDROID_BITMAP_FLAGS_ALPHA_PREMUL; |
chaviw | bc27bc7 | 2020-07-27 16:44:35 -0700 | [diff] [blame] | 239 | info.width = buffer->getWidth(); |
| 240 | info.height = buffer->getHeight(); |
| 241 | info.stride = buffer->getStride() * bytesPerPixel(buffer->getPixelFormat()); |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 242 | |
chaviw | bc27bc7 | 2020-07-27 16:44:35 -0700 | [diff] [blame] | 243 | int result = AndroidBitmap_compress(&info, static_cast<int32_t>(dataspace), base, |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 244 | ANDROID_BITMAP_COMPRESS_FORMAT_PNG, 100, &fd, |
| 245 | [](void* fdPtr, const void* data, size_t size) -> bool { |
| 246 | int bytesWritten = write(*static_cast<int*>(fdPtr), |
| 247 | data, size); |
| 248 | return bytesWritten == size; |
| 249 | }); |
| 250 | |
| 251 | if (result != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 252 | fprintf(stderr, "Failed to compress PNG (error code: %d)\n", result); |
| 253 | } |
| 254 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 255 | if (fn != NULL) { |
| 256 | notifyMediaScanner(fn); |
| 257 | } |
| 258 | } else { |
chaviw | bc27bc7 | 2020-07-27 16:44:35 -0700 | [diff] [blame] | 259 | uint32_t w = buffer->getWidth(); |
| 260 | uint32_t h = buffer->getHeight(); |
| 261 | uint32_t s = buffer->getStride(); |
| 262 | uint32_t f = buffer->getPixelFormat(); |
| 263 | uint32_t c = dataSpaceToInt(dataspace); |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame] | 264 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 265 | write(fd, &w, 4); |
| 266 | write(fd, &h, 4); |
| 267 | write(fd, &f, 4); |
| 268 | write(fd, &c, 4); |
| 269 | size_t Bpp = bytesPerPixel(f); |
| 270 | for (size_t y=0 ; y<h ; y++) { |
| 271 | write(fd, base, w*Bpp); |
| 272 | base = (void *)((char *)base + s*Bpp); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | close(fd); |
Josh Gao | 9098258 | 2017-06-19 13:38:20 -0700 | [diff] [blame] | 276 | |
Steven Moreland | a89ae86 | 2018-05-24 17:48:28 -0700 | [diff] [blame] | 277 | return 0; |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 278 | } |