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