2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 #include <sys/ioctl.h>
28 #include <binder/ProcessState.h>
30 #include <gui/SurfaceComposerClient.h>
31 #include <gui/ISurfaceComposer.h>
33 #include <ui/DisplayInfo.h>
34 #include <ui/PixelFormat.h>
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wunused-parameter"
39 #include <SkImageEncoder.h>
41 #pragma GCC diagnostic pop
43 using namespace android;
45 static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain;
47 static void usage(const char* pname)
50 "usage: %s [-hp] [-d display-id] [FILENAME]\n"
52 " -p: save the file as a png.\n"
53 " -d: specify the display id to capture, default %d.\n"
54 "If FILENAME ends with .png it will be saved as a png.\n"
55 "If FILENAME is not given, the results will be printed to stdout.\n",
56 pname, DEFAULT_DISPLAY_ID
60 static SkColorType flinger2skia(PixelFormat f)
63 case PIXEL_FORMAT_RGB_565:
64 return kRGB_565_SkColorType;
66 return kN32_SkColorType;
70 static status_t notifyMediaScanner(const char* fileName) {
71 String8 cmd("am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://");
72 String8 fileUrl("\"");
73 fileUrl.append(fileName);
76 cmd.append(" > /dev/null");
77 int result = system(cmd.string());
79 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
85 int main(int argc, char** argv)
87 ProcessState::self()->startThreadPool();
89 const char* pname = argv[0];
91 int32_t displayId = DEFAULT_DISPLAY_ID;
93 while ((c = getopt(argc, argv, "phd:")) != -1) {
99 displayId = atoi(optarg);
111 const char* fn = NULL;
113 fd = dup(STDOUT_FILENO);
114 } else if (argc == 1) {
116 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
118 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
121 const int len = strlen(fn);
122 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
132 void const* mapbase = MAP_FAILED;
133 ssize_t mapsize = -1;
135 void const* base = NULL;
139 // Maps orientations from DisplayInfo to ISurfaceComposer
140 static const uint32_t ORIENTATION_MAP[] = {
141 ISurfaceComposer::eRotateNone, // 0 == DISPLAY_ORIENTATION_0
142 ISurfaceComposer::eRotate270, // 1 == DISPLAY_ORIENTATION_90
143 ISurfaceComposer::eRotate180, // 2 == DISPLAY_ORIENTATION_180
144 ISurfaceComposer::eRotate90, // 3 == DISPLAY_ORIENTATION_270
147 ScreenshotClient screenshot;
148 sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId);
149 if (display == NULL) {
150 fprintf(stderr, "Unable to get handle for display %d\n", displayId);
154 Vector<DisplayInfo> configs;
155 SurfaceComposerClient::getDisplayConfigs(display, &configs);
156 int activeConfig = SurfaceComposerClient::getActiveConfig(display);
157 if (static_cast<size_t>(activeConfig) >= configs.size()) {
158 fprintf(stderr, "Active config %d not inside configs (size %zu)\n",
159 activeConfig, configs.size());
162 uint8_t displayOrientation = configs[activeConfig].orientation;
163 uint32_t captureOrientation = ORIENTATION_MAP[displayOrientation];
165 status_t result = screenshot.update(display, Rect(),
166 0 /* reqWidth */, 0 /* reqHeight */,
167 INT32_MIN, INT32_MAX, /* all layers */
168 false, captureOrientation);
169 if (result == NO_ERROR) {
170 base = screenshot.getPixels();
171 w = screenshot.getWidth();
172 h = screenshot.getHeight();
173 s = screenshot.getStride();
174 f = screenshot.getFormat();
175 size = screenshot.getSize();
180 const SkImageInfo info =
181 SkImageInfo::Make(w, h, flinger2skia(f), kPremul_SkAlphaType);
182 SkPixmap pixmap(info, base, s * bytesPerPixel(f));
183 struct FDWStream final : public SkWStream {
184 size_t fBytesWritten = 0;
186 FDWStream(int f) : fFd(f) {}
187 size_t bytesWritten() const override { return fBytesWritten; }
188 bool write(const void* buffer, size_t size) override {
189 fBytesWritten += size;
190 return size == 0 || ::write(fFd, buffer, size) > 0;
193 (void)SkEncodeImage(&fdStream, pixmap, SkEncodedImageFormat::kPNG, 100);
195 notifyMediaScanner(fn);
201 size_t Bpp = bytesPerPixel(f);
202 for (size_t y=0 ; y<h ; y++) {
203 write(fd, base, w*Bpp);
204 base = (void *)((char *)base + s*Bpp);
209 if (mapbase != MAP_FAILED) {
210 munmap((void *)mapbase, mapsize);