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 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 33 | #include <gui/SurfaceComposerClient.h> |
| 34 | #include <gui/ISurfaceComposer.h> |
| 35 | |
Dan Stoza | cf70d71 | 2015-06-09 16:47:33 -0700 | [diff] [blame] | 36 | #include <ui/DisplayInfo.h> |
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 | |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 48 | static void usage(const char* pname, PhysicalDisplayId displayId) |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 49 | { |
| 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 Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 54 | " -d: specify the physical display ID to capture (default: %" |
| 55 | ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ")\n" |
| 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 | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 59 | pname, displayId); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame^] | 62 | static int32_t flinger2bitmapFormat(PixelFormat f) |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 63 | { |
| 64 | switch (f) { |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 65 | case PIXEL_FORMAT_RGB_565: |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame^] | 66 | return ANDROID_BITMAP_FORMAT_RGB_565; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 67 | default: |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame^] | 68 | return ANDROID_BITMAP_FORMAT_RGBA_8888; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 72 | static uint32_t dataSpaceToInt(ui::Dataspace d) |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 73 | { |
| 74 | switch (d) { |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 75 | case ui::Dataspace::V0_SRGB: |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 76 | return COLORSPACE_SRGB; |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 77 | case ui::Dataspace::DISPLAY_P3: |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 78 | return COLORSPACE_DISPLAY_P3; |
| 79 | default: |
| 80 | return COLORSPACE_UNKNOWN; |
| 81 | } |
| 82 | } |
| 83 | |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 84 | static status_t notifyMediaScanner(const char* fileName) { |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 85 | std::string filePath("file://"); |
| 86 | filePath.append(fileName); |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 87 | char *cmd[] = { |
| 88 | (char*) "am", |
| 89 | (char*) "broadcast", |
| 90 | (char*) "am", |
| 91 | (char*) "android.intent.action.MEDIA_SCANNER_SCAN_FILE", |
| 92 | (char*) "-d", |
George Burgess IV | 5c46fb6 | 2020-03-16 12:07:30 -0700 | [diff] [blame] | 93 | &filePath[0], |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 94 | 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 Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 117 | fprintf(stderr, "Unable to broadcast intent for media scanner.\n"); |
| 118 | return UNKNOWN_ERROR; |
| 119 | } |
| 120 | return NO_ERROR; |
| 121 | } |
| 122 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 123 | int main(int argc, char** argv) |
| 124 | { |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 125 | 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 Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 131 | const char* pname = argv[0]; |
| 132 | bool png = false; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 133 | int c; |
| 134 | while ((c = getopt(argc, argv, "phd:")) != -1) { |
| 135 | switch (c) { |
| 136 | case 'p': |
| 137 | png = true; |
| 138 | break; |
| 139 | case 'd': |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 140 | displayId = atoll(optarg); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 141 | break; |
| 142 | case '?': |
| 143 | case 'h': |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 144 | usage(pname, *displayId); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 145 | return 1; |
| 146 | } |
| 147 | } |
| 148 | argc -= optind; |
| 149 | argv += optind; |
| 150 | |
| 151 | int fd = -1; |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 152 | const char* fn = NULL; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 153 | if (argc == 0) { |
| 154 | fd = dup(STDOUT_FILENO); |
| 155 | } else if (argc == 1) { |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 156 | fn = argv[0]; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 157 | 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 Prabhu | bf89ae5 | 2016-03-10 15:23:34 -0800 | [diff] [blame] | 167 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 168 | if (fd == -1) { |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 169 | usage(pname, *displayId); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | void const* mapbase = MAP_FAILED; |
| 174 | ssize_t mapsize = -1; |
| 175 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 176 | void* base = NULL; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 177 | |
Martijn Coenen | 48b7408 | 2017-07-24 09:19:26 +0200 | [diff] [blame] | 178 | // 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 | |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 185 | ui::Dataspace outDataspace; |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 186 | sp<GraphicBuffer> outBuffer; |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 187 | |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 188 | status_t result = ScreenshotClient::capture(*displayId, &outDataspace, &outBuffer); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 189 | if (result != NO_ERROR) { |
| 190 | close(fd); |
Steven Moreland | a89ae86 | 2018-05-24 17:48:28 -0700 | [diff] [blame] | 191 | return 1; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 194 | result = outBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base); |
| 195 | |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 196 | if (base == nullptr || result != NO_ERROR) { |
| 197 | String8 reason; |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 198 | if (result != NO_ERROR) { |
| 199 | reason.appendFormat(" Error Code: %d", result); |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 200 | } else { |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 201 | reason = "Failed to write to buffer"; |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 202 | } |
| 203 | fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str()); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 204 | close(fd); |
Steven Moreland | a89ae86 | 2018-05-24 17:48:28 -0700 | [diff] [blame] | 205 | return 1; |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 208 | if (png) { |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame^] | 209 | AndroidBitmapInfo info; |
| 210 | info.format = flinger2bitmapFormat(outBuffer->getPixelFormat()); |
| 211 | info.flags = ANDROID_BITMAP_FLAGS_ALPHA_PREMUL; |
| 212 | info.width = outBuffer->getWidth(); |
| 213 | info.height = outBuffer->getHeight(); |
| 214 | info.stride = outBuffer->getStride() * bytesPerPixel(outBuffer->getPixelFormat()); |
| 215 | |
| 216 | int result = AndroidBitmap_compress(&info, static_cast<int32_t>(outDataspace), base, |
| 217 | ANDROID_BITMAP_COMPRESS_FORMAT_PNG, 100, &fd, |
| 218 | [](void* fdPtr, const void* data, size_t size) -> bool { |
| 219 | int bytesWritten = write(*static_cast<int*>(fdPtr), |
| 220 | data, size); |
| 221 | return bytesWritten == size; |
| 222 | }); |
| 223 | |
| 224 | if (result != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 225 | fprintf(stderr, "Failed to compress PNG (error code: %d)\n", result); |
| 226 | } |
| 227 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 228 | if (fn != NULL) { |
| 229 | notifyMediaScanner(fn); |
| 230 | } |
| 231 | } else { |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame^] | 232 | uint32_t w = outBuffer->getWidth(); |
| 233 | uint32_t h = outBuffer->getHeight(); |
| 234 | uint32_t s = outBuffer->getStride(); |
| 235 | uint32_t f = outBuffer->getPixelFormat(); |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 236 | uint32_t c = dataSpaceToInt(outDataspace); |
Derek Sollenberger | a3ef094 | 2020-04-08 15:47:55 -0400 | [diff] [blame^] | 237 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 238 | write(fd, &w, 4); |
| 239 | write(fd, &h, 4); |
| 240 | write(fd, &f, 4); |
| 241 | write(fd, &c, 4); |
| 242 | size_t Bpp = bytesPerPixel(f); |
| 243 | for (size_t y=0 ; y<h ; y++) { |
| 244 | write(fd, base, w*Bpp); |
| 245 | base = (void *)((char *)base + s*Bpp); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | close(fd); |
| 249 | if (mapbase != MAP_FAILED) { |
| 250 | munmap((void *)mapbase, mapsize); |
| 251 | } |
Josh Gao | 9098258 | 2017-06-19 13:38:20 -0700 | [diff] [blame] | 252 | |
Steven Moreland | a89ae86 | 2018-05-24 17:48:28 -0700 | [diff] [blame] | 253 | return 0; |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 254 | } |