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 | |
Mathias Agopian | 0678a8c | 2013-03-19 20:56:00 -0700 | [diff] [blame] | 29 | #include <binder/ProcessState.h> |
| 30 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 31 | #include <gui/SurfaceComposerClient.h> |
| 32 | #include <gui/ISurfaceComposer.h> |
| 33 | |
Dan Stoza | cf70d71 | 2015-06-09 16:47:33 -0700 | [diff] [blame] | 34 | #include <ui/DisplayInfo.h> |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 35 | #include <ui/GraphicTypes.h> |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 36 | #include <ui/PixelFormat.h> |
| 37 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 38 | #include <system/graphics.h> |
| 39 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 40 | // TODO: Fix Skia. |
| 41 | #pragma GCC diagnostic push |
| 42 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 43 | #include <SkImageEncoder.h> |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 44 | #include <SkData.h> |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 45 | #include <SkColorSpace.h> |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 46 | #pragma GCC diagnostic pop |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 47 | |
| 48 | using namespace android; |
| 49 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 50 | #define COLORSPACE_UNKNOWN 0 |
| 51 | #define COLORSPACE_SRGB 1 |
| 52 | #define COLORSPACE_DISPLAY_P3 2 |
| 53 | |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 54 | static void usage(const char* pname, PhysicalDisplayId displayId) |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 55 | { |
| 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" |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 60 | " -d: specify the physical display ID to capture (default: %" |
| 61 | ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ")\n" |
| 62 | " see \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n" |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 63 | "If FILENAME ends with .png it will be saved as a png.\n" |
| 64 | "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] | 65 | pname, displayId); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 68 | static SkColorType flinger2skia(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: |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 72 | return kRGB_565_SkColorType; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 73 | default: |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 74 | return kN32_SkColorType; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 78 | static sk_sp<SkColorSpace> dataSpaceToColorSpace(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 SkColorSpace::MakeSRGB(); |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 83 | case ui::Dataspace::DISPLAY_P3: |
Brian Osman | be8fac26 | 2019-01-14 17:02:23 -0500 | [diff] [blame] | 84 | return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 85 | default: |
| 86 | return nullptr; |
| 87 | } |
| 88 | } |
| 89 | |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 90 | static uint32_t dataSpaceToInt(ui::Dataspace d) |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 91 | { |
| 92 | switch (d) { |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 93 | case ui::Dataspace::V0_SRGB: |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 94 | return COLORSPACE_SRGB; |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 95 | case ui::Dataspace::DISPLAY_P3: |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 96 | return COLORSPACE_DISPLAY_P3; |
| 97 | default: |
| 98 | return COLORSPACE_UNKNOWN; |
| 99 | } |
| 100 | } |
| 101 | |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 102 | static status_t notifyMediaScanner(const char* fileName) { |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 103 | std::string filePath("file://"); |
| 104 | filePath.append(fileName); |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 105 | char *cmd[] = { |
| 106 | (char*) "am", |
| 107 | (char*) "broadcast", |
Dichen Zhang | e361a26 | 2020-04-17 10:05:49 -0700 | [diff] [blame^] | 108 | (char*) "-a", |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 109 | (char*) "android.intent.action.MEDIA_SCANNER_SCAN_FILE", |
| 110 | (char*) "-d", |
George Burgess IV | 5c46fb6 | 2020-03-16 12:07:30 -0700 | [diff] [blame] | 111 | &filePath[0], |
Dichen Zhang | d31f219 | 2020-03-12 12:25:09 -0700 | [diff] [blame] | 112 | nullptr |
| 113 | }; |
| 114 | |
| 115 | int status; |
| 116 | int pid = fork(); |
| 117 | if (pid < 0){ |
| 118 | fprintf(stderr, "Unable to fork in order to send intent for media scanner.\n"); |
| 119 | return UNKNOWN_ERROR; |
| 120 | } |
| 121 | if (pid == 0){ |
| 122 | int fd = open("/dev/null", O_WRONLY); |
| 123 | if (fd < 0){ |
| 124 | fprintf(stderr, "Unable to open /dev/null for media scanner stdout redirection.\n"); |
| 125 | exit(1); |
| 126 | } |
| 127 | dup2(fd, 1); |
| 128 | int result = execvp(cmd[0], cmd); |
| 129 | close(fd); |
| 130 | exit(result); |
| 131 | } |
| 132 | wait(&status); |
| 133 | |
| 134 | if (status < 0) { |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 135 | fprintf(stderr, "Unable to broadcast intent for media scanner.\n"); |
| 136 | return UNKNOWN_ERROR; |
| 137 | } |
| 138 | return NO_ERROR; |
| 139 | } |
| 140 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 141 | int main(int argc, char** argv) |
| 142 | { |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 143 | std::optional<PhysicalDisplayId> displayId = SurfaceComposerClient::getInternalDisplayId(); |
| 144 | if (!displayId) { |
| 145 | fprintf(stderr, "Failed to get token for internal display\n"); |
| 146 | return 1; |
| 147 | } |
| 148 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 149 | const char* pname = argv[0]; |
| 150 | bool png = false; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 151 | int c; |
| 152 | while ((c = getopt(argc, argv, "phd:")) != -1) { |
| 153 | switch (c) { |
| 154 | case 'p': |
| 155 | png = true; |
| 156 | break; |
| 157 | case 'd': |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 158 | displayId = atoll(optarg); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 159 | break; |
| 160 | case '?': |
| 161 | case 'h': |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 162 | usage(pname, *displayId); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 163 | return 1; |
| 164 | } |
| 165 | } |
| 166 | argc -= optind; |
| 167 | argv += optind; |
| 168 | |
| 169 | int fd = -1; |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 170 | const char* fn = NULL; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 171 | if (argc == 0) { |
| 172 | fd = dup(STDOUT_FILENO); |
| 173 | } else if (argc == 1) { |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 174 | fn = argv[0]; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 175 | fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664); |
| 176 | if (fd == -1) { |
| 177 | fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno)); |
| 178 | return 1; |
| 179 | } |
| 180 | const int len = strlen(fn); |
| 181 | if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) { |
| 182 | png = true; |
| 183 | } |
| 184 | } |
Prathmesh Prabhu | bf89ae5 | 2016-03-10 15:23:34 -0800 | [diff] [blame] | 185 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 186 | if (fd == -1) { |
Dominik Laskowski | 3316a0a | 2019-01-25 02:56:41 -0800 | [diff] [blame] | 187 | usage(pname, *displayId); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | void const* mapbase = MAP_FAILED; |
| 192 | ssize_t mapsize = -1; |
| 193 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 194 | void* base = NULL; |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 195 | uint32_t w, s, h, f; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 196 | size_t size = 0; |
| 197 | |
Martijn Coenen | 48b7408 | 2017-07-24 09:19:26 +0200 | [diff] [blame] | 198 | // setThreadPoolMaxThreadCount(0) actually tells the kernel it's |
| 199 | // not allowed to spawn any additional threads, but we still spawn |
| 200 | // a binder thread from userspace when we call startThreadPool(). |
| 201 | // See b/36066697 for rationale |
| 202 | ProcessState::self()->setThreadPoolMaxThreadCount(0); |
| 203 | ProcessState::self()->startThreadPool(); |
| 204 | |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 205 | ui::Dataspace outDataspace; |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 206 | sp<GraphicBuffer> outBuffer; |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 207 | |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 208 | status_t result = ScreenshotClient::capture(*displayId, &outDataspace, &outBuffer); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 209 | if (result != NO_ERROR) { |
| 210 | close(fd); |
Steven Moreland | a89ae86 | 2018-05-24 17:48:28 -0700 | [diff] [blame] | 211 | return 1; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 214 | result = outBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base); |
| 215 | |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 216 | if (base == nullptr || result != NO_ERROR) { |
| 217 | String8 reason; |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 218 | if (result != NO_ERROR) { |
| 219 | reason.appendFormat(" Error Code: %d", result); |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 220 | } else { |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 221 | reason = "Failed to write to buffer"; |
chaviw | 7f4dc7e | 2018-09-11 13:59:30 -0700 | [diff] [blame] | 222 | } |
| 223 | fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str()); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 224 | close(fd); |
Steven Moreland | a89ae86 | 2018-05-24 17:48:28 -0700 | [diff] [blame] | 225 | return 1; |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | w = outBuffer->getWidth(); |
| 229 | h = outBuffer->getHeight(); |
| 230 | s = outBuffer->getStride(); |
| 231 | f = outBuffer->getPixelFormat(); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 232 | size = s * h * bytesPerPixel(f); |
| 233 | |
| 234 | if (png) { |
| 235 | const SkImageInfo info = |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 236 | SkImageInfo::Make(w, h, flinger2skia(f), kPremul_SkAlphaType, |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 237 | dataSpaceToColorSpace(outDataspace)); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 238 | SkPixmap pixmap(info, base, s * bytesPerPixel(f)); |
| 239 | struct FDWStream final : public SkWStream { |
| 240 | size_t fBytesWritten = 0; |
| 241 | int fFd; |
| 242 | FDWStream(int f) : fFd(f) {} |
| 243 | size_t bytesWritten() const override { return fBytesWritten; } |
| 244 | bool write(const void* buffer, size_t size) override { |
| 245 | fBytesWritten += size; |
| 246 | return size == 0 || ::write(fFd, buffer, size) > 0; |
| 247 | } |
| 248 | } fdStream(fd); |
| 249 | (void)SkEncodeImage(&fdStream, pixmap, SkEncodedImageFormat::kPNG, 100); |
| 250 | if (fn != NULL) { |
| 251 | notifyMediaScanner(fn); |
| 252 | } |
| 253 | } else { |
chaviw | a69a1b8 | 2019-04-30 16:53:33 -0700 | [diff] [blame] | 254 | uint32_t c = dataSpaceToInt(outDataspace); |
Chavi Weingarten | d7ec64c | 2017-11-30 01:52:01 +0000 | [diff] [blame] | 255 | write(fd, &w, 4); |
| 256 | write(fd, &h, 4); |
| 257 | write(fd, &f, 4); |
| 258 | write(fd, &c, 4); |
| 259 | size_t Bpp = bytesPerPixel(f); |
| 260 | for (size_t y=0 ; y<h ; y++) { |
| 261 | write(fd, base, w*Bpp); |
| 262 | base = (void *)((char *)base + s*Bpp); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | close(fd); |
| 266 | if (mapbase != MAP_FAILED) { |
| 267 | munmap((void *)mapbase, mapsize); |
| 268 | } |
Josh Gao | 9098258 | 2017-06-19 13:38:20 -0700 | [diff] [blame] | 269 | |
Steven Moreland | a89ae86 | 2018-05-24 17:48:28 -0700 | [diff] [blame] | 270 | return 0; |
Peiyong Lin | 10a34d1 | 2018-09-19 13:56:12 -0700 | [diff] [blame] | 271 | } |