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> |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 34 | #include <ui/PixelFormat.h> |
| 35 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 36 | #include <system/graphics.h> |
| 37 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 38 | // TODO: Fix Skia. |
| 39 | #pragma GCC diagnostic push |
| 40 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 41 | #include <SkImageEncoder.h> |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 42 | #include <SkData.h> |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 43 | #include <SkColorSpace.h> |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 44 | #pragma GCC diagnostic pop |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 45 | |
| 46 | using namespace android; |
| 47 | |
| 48 | static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain; |
| 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 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 54 | static void usage(const char* pname) |
| 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" |
| 60 | " -d: specify the display id to capture, default %d.\n" |
| 61 | "If FILENAME ends with .png it will be saved as a png.\n" |
| 62 | "If FILENAME is not given, the results will be printed to stdout.\n", |
| 63 | pname, DEFAULT_DISPLAY_ID |
| 64 | ); |
| 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 | |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 77 | static sk_sp<SkColorSpace> dataSpaceToColorSpace(android_dataspace d) |
| 78 | { |
| 79 | switch (d) { |
| 80 | case HAL_DATASPACE_V0_SRGB: |
| 81 | return SkColorSpace::MakeSRGB(); |
| 82 | case HAL_DATASPACE_DISPLAY_P3: |
| 83 | return SkColorSpace::MakeRGB( |
| 84 | SkColorSpace::kSRGB_RenderTargetGamma, SkColorSpace::kDCIP3_D65_Gamut); |
| 85 | default: |
| 86 | return nullptr; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static uint32_t dataSpaceToInt(android_dataspace d) |
| 91 | { |
| 92 | switch (d) { |
| 93 | case HAL_DATASPACE_V0_SRGB: |
| 94 | return COLORSPACE_SRGB; |
| 95 | case HAL_DATASPACE_DISPLAY_P3: |
| 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) { |
| 103 | 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] | 104 | cmd.append(fileName); |
| 105 | cmd.append(" > /dev/null"); |
| 106 | int result = system(cmd.string()); |
| 107 | if (result < 0) { |
| 108 | fprintf(stderr, "Unable to broadcast intent for media scanner.\n"); |
| 109 | return UNKNOWN_ERROR; |
| 110 | } |
| 111 | return NO_ERROR; |
| 112 | } |
| 113 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 114 | int main(int argc, char** argv) |
| 115 | { |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 116 | const char* pname = argv[0]; |
| 117 | bool png = false; |
| 118 | int32_t displayId = DEFAULT_DISPLAY_ID; |
| 119 | int c; |
| 120 | while ((c = getopt(argc, argv, "phd:")) != -1) { |
| 121 | switch (c) { |
| 122 | case 'p': |
| 123 | png = true; |
| 124 | break; |
| 125 | case 'd': |
| 126 | displayId = atoi(optarg); |
| 127 | break; |
| 128 | case '?': |
| 129 | case 'h': |
| 130 | usage(pname); |
| 131 | return 1; |
| 132 | } |
| 133 | } |
| 134 | argc -= optind; |
| 135 | argv += optind; |
| 136 | |
| 137 | int fd = -1; |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 138 | const char* fn = NULL; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 139 | if (argc == 0) { |
| 140 | fd = dup(STDOUT_FILENO); |
| 141 | } else if (argc == 1) { |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 142 | fn = argv[0]; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 143 | fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664); |
| 144 | if (fd == -1) { |
| 145 | fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno)); |
| 146 | return 1; |
| 147 | } |
| 148 | const int len = strlen(fn); |
| 149 | if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) { |
| 150 | png = true; |
| 151 | } |
| 152 | } |
Prathmesh Prabhu | bf89ae5 | 2016-03-10 15:23:34 -0800 | [diff] [blame] | 153 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 154 | if (fd == -1) { |
| 155 | usage(pname); |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | void const* mapbase = MAP_FAILED; |
| 160 | ssize_t mapsize = -1; |
| 161 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 162 | void const* base = NULL; |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 163 | uint32_t w, s, h, f; |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 164 | android_dataspace d; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 165 | size_t size = 0; |
| 166 | |
Dan Stoza | cf70d71 | 2015-06-09 16:47:33 -0700 | [diff] [blame] | 167 | // Maps orientations from DisplayInfo to ISurfaceComposer |
| 168 | static const uint32_t ORIENTATION_MAP[] = { |
| 169 | ISurfaceComposer::eRotateNone, // 0 == DISPLAY_ORIENTATION_0 |
| 170 | ISurfaceComposer::eRotate270, // 1 == DISPLAY_ORIENTATION_90 |
| 171 | ISurfaceComposer::eRotate180, // 2 == DISPLAY_ORIENTATION_180 |
| 172 | ISurfaceComposer::eRotate90, // 3 == DISPLAY_ORIENTATION_270 |
| 173 | }; |
| 174 | |
Martijn Coenen | 48b7408 | 2017-07-24 09:19:26 +0200 | [diff] [blame] | 175 | // setThreadPoolMaxThreadCount(0) actually tells the kernel it's |
| 176 | // not allowed to spawn any additional threads, but we still spawn |
| 177 | // a binder thread from userspace when we call startThreadPool(). |
| 178 | // See b/36066697 for rationale |
| 179 | ProcessState::self()->setThreadPoolMaxThreadCount(0); |
| 180 | ProcessState::self()->startThreadPool(); |
| 181 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 182 | ScreenshotClient screenshot; |
| 183 | sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId); |
Dan Stoza | cf70d71 | 2015-06-09 16:47:33 -0700 | [diff] [blame] | 184 | if (display == NULL) { |
| 185 | fprintf(stderr, "Unable to get handle for display %d\n", displayId); |
Martijn Coenen | 48b7408 | 2017-07-24 09:19:26 +0200 | [diff] [blame] | 186 | // b/36066697: Avoid running static destructors. |
| 187 | _exit(1); |
Dan Stoza | cf70d71 | 2015-06-09 16:47:33 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | Vector<DisplayInfo> configs; |
| 191 | SurfaceComposerClient::getDisplayConfigs(display, &configs); |
| 192 | int activeConfig = SurfaceComposerClient::getActiveConfig(display); |
| 193 | if (static_cast<size_t>(activeConfig) >= configs.size()) { |
| 194 | fprintf(stderr, "Active config %d not inside configs (size %zu)\n", |
| 195 | activeConfig, configs.size()); |
Martijn Coenen | 48b7408 | 2017-07-24 09:19:26 +0200 | [diff] [blame] | 196 | // b/36066697: Avoid running static destructors. |
| 197 | _exit(1); |
Dan Stoza | cf70d71 | 2015-06-09 16:47:33 -0700 | [diff] [blame] | 198 | } |
| 199 | uint8_t displayOrientation = configs[activeConfig].orientation; |
| 200 | uint32_t captureOrientation = ORIENTATION_MAP[displayOrientation]; |
| 201 | |
Robert Carr | db7ecbe | 2017-01-31 11:02:58 -0800 | [diff] [blame] | 202 | status_t result = screenshot.update(display, Rect(), |
| 203 | 0 /* reqWidth */, 0 /* reqHeight */, |
| 204 | INT32_MIN, INT32_MAX, /* all layers */ |
Dan Stoza | cf70d71 | 2015-06-09 16:47:33 -0700 | [diff] [blame] | 205 | false, captureOrientation); |
| 206 | if (result == NO_ERROR) { |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 207 | base = screenshot.getPixels(); |
| 208 | w = screenshot.getWidth(); |
| 209 | h = screenshot.getHeight(); |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 210 | s = screenshot.getStride(); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 211 | f = screenshot.getFormat(); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 212 | d = screenshot.getDataSpace(); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 213 | size = screenshot.getSize(); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 216 | if (base != NULL) { |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 217 | if (png) { |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 218 | const SkImageInfo info = |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 219 | SkImageInfo::Make(w, h, flinger2skia(f), kPremul_SkAlphaType, |
| 220 | dataSpaceToColorSpace(d)); |
Hal Canary | 10219fb | 2016-11-23 20:41:22 -0500 | [diff] [blame] | 221 | SkPixmap pixmap(info, base, s * bytesPerPixel(f)); |
| 222 | struct FDWStream final : public SkWStream { |
| 223 | size_t fBytesWritten = 0; |
| 224 | int fFd; |
| 225 | FDWStream(int f) : fFd(f) {} |
| 226 | size_t bytesWritten() const override { return fBytesWritten; } |
| 227 | bool write(const void* buffer, size_t size) override { |
| 228 | fBytesWritten += size; |
| 229 | return size == 0 || ::write(fFd, buffer, size) > 0; |
| 230 | } |
| 231 | } fdStream(fd); |
| 232 | (void)SkEncodeImage(&fdStream, pixmap, SkEncodedImageFormat::kPNG, 100); |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 233 | if (fn != NULL) { |
| 234 | notifyMediaScanner(fn); |
| 235 | } |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 236 | } else { |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 237 | uint32_t c = dataSpaceToInt(d); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 238 | write(fd, &w, 4); |
| 239 | write(fd, &h, 4); |
| 240 | write(fd, &f, 4); |
Romain Guy | 26a2b97 | 2017-04-17 09:39:51 -0700 | [diff] [blame] | 241 | write(fd, &c, 4); |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 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); |
| 246 | } |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | close(fd); |
| 250 | if (mapbase != MAP_FAILED) { |
| 251 | munmap((void *)mapbase, mapsize); |
| 252 | } |
Josh Gao | 9098258 | 2017-06-19 13:38:20 -0700 | [diff] [blame] | 253 | |
| 254 | // b/36066697: Avoid running static destructors. |
Brian Carlstrom | 9377ce6 | 2017-06-21 22:14:40 -0700 | [diff] [blame] | 255 | _exit(0); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 256 | } |