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