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