blob: 7a599e93e73b110190961053487f1aa830b084dc [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 Agopian38ed2e32010-09-29 13:02:36 -070027#include <surfaceflinger/SurfaceComposerClient.h>
Mathias Agopian88a5df92010-09-26 18:49:45 -070028
Joe Onorato5cff0632010-12-03 16:01:57 -080029#include <SkImageEncoder.h>
30#include <SkBitmap.h>
31#include <SkStream.h>
32
Mathias Agopian88a5df92010-09-26 18:49:45 -070033using namespace android;
34
Mathias Agopian9afc7b02011-02-08 20:43:00 -080035static void usage(const char* pname)
Joe Onorato5cff0632010-12-03 16:01:57 -080036{
37 fprintf(stderr,
Mathias Agopian9afc7b02011-02-08 20:43:00 -080038 "usage: %s [-hp] [FILENAME]\n"
Joe Onorato5cff0632010-12-03 16:01:57 -080039 " -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 Agopian9afc7b02011-02-08 20:43:00 -080042 "If FILENAME is not given, the results will be printed to stdout.\n",
43 pname
Joe Onorato5cff0632010-12-03 16:01:57 -080044 );
45}
46
47static 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 Agopian9afc7b02011-02-08 20:43:00 -080062static 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 Agopian88a5df92010-09-26 18:49:45 -070086int main(int argc, char** argv)
87{
Mathias Agopian9afc7b02011-02-08 20:43:00 -080088 const char* pname = argv[0];
Joe Onorato5cff0632010-12-03 16:01:57 -080089 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 Agopian9afc7b02011-02-08 20:43:00 -080098 usage(pname);
Joe Onorato5cff0632010-12-03 16:01:57 -080099 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 Agopian9afc7b02011-02-08 20:43:00 -0800112 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
Joe Onorato5cff0632010-12-03 16:01:57 -0800113 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 Agopian9afc7b02011-02-08 20:43:00 -0800122 usage(pname);
Joe Onorato5cff0632010-12-03 16:01:57 -0800123 return 1;
124 }
125
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800126 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 Agopian38ed2e32010-09-29 13:02:36 -0700133 ScreenshotClient screenshot;
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800134 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 Onorato5cff0632010-12-03 16:01:57 -0800161 }
Mathias Agopian88a5df92010-09-26 18:49:45 -0700162
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800163 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 Onorato5cff0632010-12-03 16:01:57 -0800178 }
Mathias Agopian88a5df92010-09-26 18:49:45 -0700179 close(fd);
Mathias Agopian9afc7b02011-02-08 20:43:00 -0800180 if (mapbase != MAP_FAILED) {
181 munmap((void *)mapbase, mapsize);
182 }
Mathias Agopian88a5df92010-09-26 18:49:45 -0700183 return 0;
184}