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