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 | |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 33 | #include <ui/PixelFormat.h> |
| 34 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 35 | // TODO: Fix Skia. |
| 36 | #pragma GCC diagnostic push |
| 37 | #pragma GCC diagnostic ignored "-Wunused-parameter" |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 38 | #include <SkImageEncoder.h> |
| 39 | #include <SkBitmap.h> |
| 40 | #include <SkData.h> |
| 41 | #include <SkStream.h> |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 42 | #pragma GCC diagnostic pop |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 43 | |
| 44 | using namespace android; |
| 45 | |
| 46 | static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain; |
| 47 | |
| 48 | static void usage(const char* pname) |
| 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" |
| 54 | " -d: specify the display id to capture, default %d.\n" |
| 55 | "If FILENAME ends with .png it will be saved as a png.\n" |
| 56 | "If FILENAME is not given, the results will be printed to stdout.\n", |
| 57 | pname, DEFAULT_DISPLAY_ID |
| 58 | ); |
| 59 | } |
| 60 | |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 61 | static SkColorType flinger2skia(PixelFormat f) |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 62 | { |
| 63 | switch (f) { |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 64 | case PIXEL_FORMAT_RGB_565: |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 65 | return kRGB_565_SkColorType; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 66 | default: |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 67 | return kN32_SkColorType; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | static status_t vinfoToPixelFormat(const fb_var_screeninfo& vinfo, |
| 72 | uint32_t* bytespp, uint32_t* f) |
| 73 | { |
| 74 | |
| 75 | switch (vinfo.bits_per_pixel) { |
| 76 | case 16: |
| 77 | *f = PIXEL_FORMAT_RGB_565; |
| 78 | *bytespp = 2; |
| 79 | break; |
| 80 | case 24: |
| 81 | *f = PIXEL_FORMAT_RGB_888; |
| 82 | *bytespp = 3; |
| 83 | break; |
| 84 | case 32: |
| 85 | // TODO: do better decoding of vinfo here |
| 86 | *f = PIXEL_FORMAT_RGBX_8888; |
| 87 | *bytespp = 4; |
| 88 | break; |
| 89 | default: |
| 90 | return BAD_VALUE; |
| 91 | } |
| 92 | return NO_ERROR; |
| 93 | } |
| 94 | |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 95 | static status_t notifyMediaScanner(const char* fileName) { |
| 96 | String8 cmd("am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://"); |
| 97 | String8 fileUrl("\""); |
| 98 | fileUrl.append(fileName); |
| 99 | fileUrl.append("\""); |
| 100 | cmd.append(fileName); |
| 101 | cmd.append(" > /dev/null"); |
| 102 | int result = system(cmd.string()); |
| 103 | if (result < 0) { |
| 104 | fprintf(stderr, "Unable to broadcast intent for media scanner.\n"); |
| 105 | return UNKNOWN_ERROR; |
| 106 | } |
| 107 | return NO_ERROR; |
| 108 | } |
| 109 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 110 | int main(int argc, char** argv) |
| 111 | { |
Mathias Agopian | 0678a8c | 2013-03-19 20:56:00 -0700 | [diff] [blame] | 112 | ProcessState::self()->startThreadPool(); |
| 113 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 114 | const char* pname = argv[0]; |
| 115 | bool png = false; |
| 116 | int32_t displayId = DEFAULT_DISPLAY_ID; |
| 117 | int c; |
| 118 | while ((c = getopt(argc, argv, "phd:")) != -1) { |
| 119 | switch (c) { |
| 120 | case 'p': |
| 121 | png = true; |
| 122 | break; |
| 123 | case 'd': |
| 124 | displayId = atoi(optarg); |
| 125 | break; |
| 126 | case '?': |
| 127 | case 'h': |
| 128 | usage(pname); |
| 129 | return 1; |
| 130 | } |
| 131 | } |
| 132 | argc -= optind; |
| 133 | argv += optind; |
| 134 | |
| 135 | int fd = -1; |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 136 | const char* fn = NULL; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 137 | if (argc == 0) { |
| 138 | fd = dup(STDOUT_FILENO); |
| 139 | } else if (argc == 1) { |
Umair Khan | cfed232 | 2014-01-15 08:08:50 -0500 | [diff] [blame] | 140 | fn = argv[0]; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 141 | fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664); |
| 142 | if (fd == -1) { |
| 143 | fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno)); |
| 144 | return 1; |
| 145 | } |
| 146 | const int len = strlen(fn); |
| 147 | if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) { |
| 148 | png = true; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if (fd == -1) { |
| 153 | usage(pname); |
| 154 | return 1; |
| 155 | } |
| 156 | |
| 157 | void const* mapbase = MAP_FAILED; |
| 158 | ssize_t mapsize = -1; |
| 159 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 160 | void const* base = NULL; |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 161 | uint32_t w, s, h, f; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 162 | size_t size = 0; |
| 163 | |
| 164 | ScreenshotClient screenshot; |
| 165 | sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId); |
Dan Stoza | 9890e341 | 2014-05-22 16:12:54 -0700 | [diff] [blame] | 166 | if (display != NULL && screenshot.update(display, Rect(), false) == NO_ERROR) { |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 167 | base = screenshot.getPixels(); |
| 168 | w = screenshot.getWidth(); |
| 169 | h = screenshot.getHeight(); |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 170 | s = screenshot.getStride(); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 171 | f = screenshot.getFormat(); |
| 172 | size = screenshot.getSize(); |
| 173 | } else { |
| 174 | const char* fbpath = "/dev/graphics/fb0"; |
| 175 | int fb = open(fbpath, O_RDONLY); |
| 176 | if (fb >= 0) { |
| 177 | struct fb_var_screeninfo vinfo; |
| 178 | if (ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) == 0) { |
| 179 | uint32_t bytespp; |
| 180 | if (vinfoToPixelFormat(vinfo, &bytespp, &f) == NO_ERROR) { |
| 181 | size_t offset = (vinfo.xoffset + vinfo.yoffset*vinfo.xres) * bytespp; |
| 182 | w = vinfo.xres; |
| 183 | h = vinfo.yres; |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 184 | s = vinfo.xres; |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 185 | size = w*h*bytespp; |
| 186 | mapsize = offset + size; |
| 187 | mapbase = mmap(0, mapsize, PROT_READ, MAP_PRIVATE, fb, 0); |
| 188 | if (mapbase != MAP_FAILED) { |
| 189 | base = (void const *)((char const *)mapbase + offset); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | close(fb); |
| 194 | } |
| 195 | } |
| 196 | |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 197 | if (base != NULL) { |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 198 | if (png) { |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 199 | const SkImageInfo info = SkImageInfo::Make(w, h, flinger2skia(f), |
| 200 | kPremul_SkAlphaType); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 201 | SkBitmap b; |
Mike Reed | b933055 | 2014-06-16 17:31:48 -0400 | [diff] [blame] | 202 | b.installPixels(info, const_cast<void*>(base), s*bytesPerPixel(f)); |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 203 | SkDynamicMemoryWStream stream; |
| 204 | SkImageEncoder::EncodeStream(&stream, b, |
| 205 | SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality); |
| 206 | SkData* streamData = stream.copyToData(); |
| 207 | write(fd, streamData->data(), streamData->size()); |
| 208 | streamData->unref(); |
Andreas Gampe | cfedceb | 2014-09-30 21:48:18 -0700 | [diff] [blame] | 209 | if (fn != NULL) { |
| 210 | notifyMediaScanner(fn); |
| 211 | } |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 212 | } else { |
| 213 | write(fd, &w, 4); |
| 214 | write(fd, &h, 4); |
| 215 | write(fd, &f, 4); |
Mathias Agopian | 0137fb8 | 2013-03-20 15:38:07 -0700 | [diff] [blame] | 216 | size_t Bpp = bytesPerPixel(f); |
| 217 | for (size_t y=0 ; y<h ; y++) { |
| 218 | write(fd, base, w*Bpp); |
| 219 | base = (void *)((char *)base + s*Bpp); |
| 220 | } |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | close(fd); |
| 224 | if (mapbase != MAP_FAILED) { |
| 225 | munmap((void *)mapbase, mapsize); |
| 226 | } |
| 227 | return 0; |
| 228 | } |