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> |
| 21 | |
| 22 | #include <linux/fb.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/mman.h> |
| 25 | |
Mathias Agopian | 0678a8c | 2013-03-19 20:56:00 -0700 | [diff] [blame^] | 26 | #include <binder/ProcessState.h> |
| 27 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 28 | #include <gui/SurfaceComposerClient.h> |
| 29 | #include <gui/ISurfaceComposer.h> |
| 30 | |
| 31 | #include <SkImageEncoder.h> |
| 32 | #include <SkBitmap.h> |
| 33 | #include <SkData.h> |
| 34 | #include <SkStream.h> |
| 35 | |
| 36 | using namespace android; |
| 37 | |
| 38 | static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain; |
| 39 | |
| 40 | static void usage(const char* pname) |
| 41 | { |
| 42 | fprintf(stderr, |
| 43 | "usage: %s [-hp] [-d display-id] [FILENAME]\n" |
| 44 | " -h: this message\n" |
| 45 | " -p: save the file as a png.\n" |
| 46 | " -d: specify the display id to capture, default %d.\n" |
| 47 | "If FILENAME ends with .png it will be saved as a png.\n" |
| 48 | "If FILENAME is not given, the results will be printed to stdout.\n", |
| 49 | pname, DEFAULT_DISPLAY_ID |
| 50 | ); |
| 51 | } |
| 52 | |
| 53 | static SkBitmap::Config flinger2skia(PixelFormat f) |
| 54 | { |
| 55 | switch (f) { |
| 56 | case PIXEL_FORMAT_A_8: |
| 57 | return SkBitmap::kA8_Config; |
| 58 | case PIXEL_FORMAT_RGB_565: |
| 59 | return SkBitmap::kRGB_565_Config; |
| 60 | case PIXEL_FORMAT_RGBA_4444: |
| 61 | return SkBitmap::kARGB_4444_Config; |
| 62 | default: |
| 63 | return SkBitmap::kARGB_8888_Config; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | static status_t vinfoToPixelFormat(const fb_var_screeninfo& vinfo, |
| 68 | uint32_t* bytespp, uint32_t* f) |
| 69 | { |
| 70 | |
| 71 | switch (vinfo.bits_per_pixel) { |
| 72 | case 16: |
| 73 | *f = PIXEL_FORMAT_RGB_565; |
| 74 | *bytespp = 2; |
| 75 | break; |
| 76 | case 24: |
| 77 | *f = PIXEL_FORMAT_RGB_888; |
| 78 | *bytespp = 3; |
| 79 | break; |
| 80 | case 32: |
| 81 | // TODO: do better decoding of vinfo here |
| 82 | *f = PIXEL_FORMAT_RGBX_8888; |
| 83 | *bytespp = 4; |
| 84 | break; |
| 85 | default: |
| 86 | return BAD_VALUE; |
| 87 | } |
| 88 | return NO_ERROR; |
| 89 | } |
| 90 | |
| 91 | int main(int argc, char** argv) |
| 92 | { |
Mathias Agopian | 0678a8c | 2013-03-19 20:56:00 -0700 | [diff] [blame^] | 93 | ProcessState::self()->startThreadPool(); |
| 94 | |
Mike Lockwood | c59b2f9 | 2012-10-24 12:31:10 -0700 | [diff] [blame] | 95 | const char* pname = argv[0]; |
| 96 | bool png = false; |
| 97 | int32_t displayId = DEFAULT_DISPLAY_ID; |
| 98 | int c; |
| 99 | while ((c = getopt(argc, argv, "phd:")) != -1) { |
| 100 | switch (c) { |
| 101 | case 'p': |
| 102 | png = true; |
| 103 | break; |
| 104 | case 'd': |
| 105 | displayId = atoi(optarg); |
| 106 | break; |
| 107 | case '?': |
| 108 | case 'h': |
| 109 | usage(pname); |
| 110 | return 1; |
| 111 | } |
| 112 | } |
| 113 | argc -= optind; |
| 114 | argv += optind; |
| 115 | |
| 116 | int fd = -1; |
| 117 | if (argc == 0) { |
| 118 | fd = dup(STDOUT_FILENO); |
| 119 | } else if (argc == 1) { |
| 120 | const char* fn = argv[0]; |
| 121 | fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664); |
| 122 | if (fd == -1) { |
| 123 | fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno)); |
| 124 | return 1; |
| 125 | } |
| 126 | const int len = strlen(fn); |
| 127 | if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) { |
| 128 | png = true; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (fd == -1) { |
| 133 | usage(pname); |
| 134 | return 1; |
| 135 | } |
| 136 | |
| 137 | void const* mapbase = MAP_FAILED; |
| 138 | ssize_t mapsize = -1; |
| 139 | |
| 140 | void const* base = 0; |
| 141 | uint32_t w, h, f; |
| 142 | size_t size = 0; |
| 143 | |
| 144 | ScreenshotClient screenshot; |
| 145 | sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId); |
| 146 | if (display != NULL && screenshot.update(display) == NO_ERROR) { |
| 147 | base = screenshot.getPixels(); |
| 148 | w = screenshot.getWidth(); |
| 149 | h = screenshot.getHeight(); |
| 150 | f = screenshot.getFormat(); |
| 151 | size = screenshot.getSize(); |
| 152 | } else { |
| 153 | const char* fbpath = "/dev/graphics/fb0"; |
| 154 | int fb = open(fbpath, O_RDONLY); |
| 155 | if (fb >= 0) { |
| 156 | struct fb_var_screeninfo vinfo; |
| 157 | if (ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) == 0) { |
| 158 | uint32_t bytespp; |
| 159 | if (vinfoToPixelFormat(vinfo, &bytespp, &f) == NO_ERROR) { |
| 160 | size_t offset = (vinfo.xoffset + vinfo.yoffset*vinfo.xres) * bytespp; |
| 161 | w = vinfo.xres; |
| 162 | h = vinfo.yres; |
| 163 | size = w*h*bytespp; |
| 164 | mapsize = offset + size; |
| 165 | mapbase = mmap(0, mapsize, PROT_READ, MAP_PRIVATE, fb, 0); |
| 166 | if (mapbase != MAP_FAILED) { |
| 167 | base = (void const *)((char const *)mapbase + offset); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | close(fb); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | if (base) { |
| 176 | if (png) { |
| 177 | SkBitmap b; |
| 178 | b.setConfig(flinger2skia(f), w, h); |
| 179 | b.setPixels((void*)base); |
| 180 | SkDynamicMemoryWStream stream; |
| 181 | SkImageEncoder::EncodeStream(&stream, b, |
| 182 | SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality); |
| 183 | SkData* streamData = stream.copyToData(); |
| 184 | write(fd, streamData->data(), streamData->size()); |
| 185 | streamData->unref(); |
| 186 | } else { |
| 187 | write(fd, &w, 4); |
| 188 | write(fd, &h, 4); |
| 189 | write(fd, &f, 4); |
| 190 | write(fd, base, size); |
| 191 | } |
| 192 | } |
| 193 | close(fd); |
| 194 | if (mapbase != MAP_FAILED) { |
| 195 | munmap((void *)mapbase, mapsize); |
| 196 | } |
| 197 | return 0; |
| 198 | } |