blob: a1ea81ae6fe1d0504589e16b455fd1c84f95335e [file] [log] [blame]
Mathias Agopian88a5df92010-09-26 18:49:45 -07001/*
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 Onorato5cff0632010-12-03 16:01:57 -080017#include <errno.h>
Mathias Agopian88a5df92010-09-26 18:49:45 -070018#include <unistd.h>
Joe Onorato5cff0632010-12-03 16:01:57 -080019#include <stdio.h>
Mathias Agopian88a5df92010-09-26 18:49:45 -070020#include <fcntl.h>
21
Mathias Agopian9afc7b02011-02-08 20:43:00 -080022#include <linux/fb.h>
23#include <sys/ioctl.h>
24#include <sys/mman.h>
25
Mathias Agopian88a5df92010-09-26 18:49:45 -070026#include <binder/IMemory.h>
Mathias Agopian8335f1c2012-02-25 18:48:35 -080027#include <gui/SurfaceComposerClient.h>
Jeff Brown0b722fe2012-08-24 22:40:14 -070028#include <gui/ISurfaceComposer.h>
Mathias Agopian88a5df92010-09-26 18:49:45 -070029
Joe Onorato5cff0632010-12-03 16:01:57 -080030#include <SkImageEncoder.h>
31#include <SkBitmap.h>
Derek Sollenberger889a3fa2012-01-31 14:08:22 -050032#include <SkData.h>
Joe Onorato5cff0632010-12-03 16:01:57 -080033#include <SkStream.h>
34
Mathias Agopian88a5df92010-09-26 18:49:45 -070035using namespace android;
36
Jeff Brown0b722fe2012-08-24 22:40:14 -070037static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain;
38
Mathias Agopian9afc7b02011-02-08 20:43:00 -080039static void usage(const char* pname)
Joe Onorato5cff0632010-12-03 16:01:57 -080040{
41 fprintf(stderr,
Jeff Brown0b722fe2012-08-24 22:40:14 -070042 "usage: %s [-hp] [-d display-id] [FILENAME]\n"
Joe Onorato5cff0632010-12-03 16:01:57 -080043 " -h: this message\n"
44 " -p: save the file as a png.\n"
Jeff Brown0b722fe2012-08-24 22:40:14 -070045 " -d: specify the display id to capture, default %d.\n"
Joe Onorato5cff0632010-12-03 16:01:57 -080046 "If FILENAME ends with .png it will be saved as a png.\n"
Mathias Agopian9afc7b02011-02-08 20:43:00 -080047 "If FILENAME is not given, the results will be printed to stdout.\n",
Jeff Brown0b722fe2012-08-24 22:40:14 -070048 pname, DEFAULT_DISPLAY_ID
Joe Onorato5cff0632010-12-03 16:01:57 -080049 );
50}
51
52static SkBitmap::Config flinger2skia(PixelFormat f)
53{
54 switch (f) {
55 case PIXEL_FORMAT_A_8:
Joe Onorato5cff0632010-12-03 16:01:57 -080056 return SkBitmap::kA8_Config;
57 case PIXEL_FORMAT_RGB_565:
58 return SkBitmap::kRGB_565_Config;
59 case PIXEL_FORMAT_RGBA_4444:
60 return SkBitmap::kARGB_4444_Config;
61 default:
62 return SkBitmap::kARGB_8888_Config;
63 }
64}
65
Mathias Agopian9afc7b02011-02-08 20:43:00 -080066static status_t vinfoToPixelFormat(const fb_var_screeninfo& vinfo,
67 uint32_t* bytespp, uint32_t* f)
68{
69
70 switch (vinfo.bits_per_pixel) {
71 case 16:
72 *f = PIXEL_FORMAT_RGB_565;
73 *bytespp = 2;
74 break;
75 case 24:
76 *f = PIXEL_FORMAT_RGB_888;
77 *bytespp = 3;
78 break;
79 case 32:
80 // TODO: do better decoding of vinfo here
81 *f = PIXEL_FORMAT_RGBX_8888;
82 *bytespp = 4;
83 break;
84 default:
85 return BAD_VALUE;
86 }
87 return NO_ERROR;
88}
89
Mathias Agopian88a5df92010-09-26 18:49:45 -070090int main(int argc, char** argv)
91{
Mathias Agopian9afc7b02011-02-08 20:43:00 -080092 const char* pname = argv[0];
Joe Onorato5cff0632010-12-03 16:01:57 -080093 bool png = false;
Jeff Brown0b722fe2012-08-24 22:40:14 -070094 int32_t displayId = DEFAULT_DISPLAY_ID;
Joe Onorato5cff0632010-12-03 16:01:57 -080095 int c;
Jeff Brown0b722fe2012-08-24 22:40:14 -070096 while ((c = getopt(argc, argv, "phd:")) != -1) {
Joe Onorato5cff0632010-12-03 16:01:57 -080097 switch (c) {
98 case 'p':
99 png = true;
100 break;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700101 case 'd':
102 displayId = atoi(optarg);
103 break;
Joe Onorato5cff0632010-12-03 16:01:57 -0800104 case '?':
105 case 'h':
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800106 usage(pname);
Joe Onorato5cff0632010-12-03 16:01:57 -0800107 return 1;
108 }
109 }
110 argc -= optind;
111 argv += optind;
112
113 int fd = -1;
114 if (argc == 0) {
115 fd = dup(STDOUT_FILENO);
116 } else if (argc == 1) {
117 const char* fn = argv[0];
118 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
119 if (fd == -1) {
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800120 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
Joe Onorato5cff0632010-12-03 16:01:57 -0800121 return 1;
122 }
123 const int len = strlen(fn);
124 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
125 png = true;
126 }
127 }
128
129 if (fd == -1) {
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800130 usage(pname);
Joe Onorato5cff0632010-12-03 16:01:57 -0800131 return 1;
132 }
133
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800134 void const* mapbase = MAP_FAILED;
135 ssize_t mapsize = -1;
136
137 void const* base = 0;
138 uint32_t w, h, f;
139 size_t size = 0;
140
Mathias Agopian38ed2e32010-09-29 13:02:36 -0700141 ScreenshotClient screenshot;
Jeff Brown0b722fe2012-08-24 22:40:14 -0700142 sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId);
143 if (display != NULL && screenshot.update(display) == NO_ERROR) {
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800144 base = screenshot.getPixels();
145 w = screenshot.getWidth();
146 h = screenshot.getHeight();
147 f = screenshot.getFormat();
148 size = screenshot.getSize();
149 } else {
150 const char* fbpath = "/dev/graphics/fb0";
151 int fb = open(fbpath, O_RDONLY);
152 if (fb >= 0) {
153 struct fb_var_screeninfo vinfo;
154 if (ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) == 0) {
155 uint32_t bytespp;
156 if (vinfoToPixelFormat(vinfo, &bytespp, &f) == NO_ERROR) {
157 size_t offset = (vinfo.xoffset + vinfo.yoffset*vinfo.xres) * bytespp;
158 w = vinfo.xres;
159 h = vinfo.yres;
160 size = w*h*bytespp;
161 mapsize = offset + size;
162 mapbase = mmap(0, mapsize, PROT_READ, MAP_PRIVATE, fb, 0);
163 if (mapbase != MAP_FAILED) {
164 base = (void const *)((char const *)mapbase + offset);
165 }
166 }
167 }
168 close(fb);
169 }
Joe Onorato5cff0632010-12-03 16:01:57 -0800170 }
Mathias Agopian88a5df92010-09-26 18:49:45 -0700171
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800172 if (base) {
173 if (png) {
174 SkBitmap b;
175 b.setConfig(flinger2skia(f), w, h);
176 b.setPixels((void*)base);
177 SkDynamicMemoryWStream stream;
178 SkImageEncoder::EncodeStream(&stream, b,
179 SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);
Derek Sollenberger889a3fa2012-01-31 14:08:22 -0500180 SkData* streamData = stream.copyToData();
181 write(fd, streamData->data(), streamData->size());
182 streamData->unref();
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800183 } else {
184 write(fd, &w, 4);
185 write(fd, &h, 4);
186 write(fd, &f, 4);
187 write(fd, base, size);
188 }
Joe Onorato5cff0632010-12-03 16:01:57 -0800189 }
Mathias Agopian88a5df92010-09-26 18:49:45 -0700190 close(fd);
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800191 if (mapbase != MAP_FAILED) {
192 munmap((void *)mapbase, mapsize);
193 }
Mathias Agopian88a5df92010-09-26 18:49:45 -0700194 return 0;
195}