blob: 445665eae2c4d6f0b627caa1bb2344cbcd0d0003 [file] [log] [blame]
Mike Lockwoodc59b2f92012-10-24 12:31:10 -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
17#include <errno.h>
18#include <unistd.h>
19#include <stdio.h>
20#include <fcntl.h>
Umair Khancfed2322014-01-15 08:08:50 -050021#include <stdlib.h>
22#include <string.h>
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070023
24#include <linux/fb.h>
25#include <sys/ioctl.h>
26#include <sys/mman.h>
27
Mathias Agopian0678a8c2013-03-19 20:56:00 -070028#include <binder/ProcessState.h>
29
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070030#include <gui/SurfaceComposerClient.h>
31#include <gui/ISurfaceComposer.h>
32
Dan Stozacf70d712015-06-09 16:47:33 -070033#include <ui/DisplayInfo.h>
Mathias Agopian0137fb82013-03-20 15:38:07 -070034#include <ui/PixelFormat.h>
35
Andreas Gampecfedceb2014-09-30 21:48:18 -070036// TODO: Fix Skia.
37#pragma GCC diagnostic push
38#pragma GCC diagnostic ignored "-Wunused-parameter"
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070039#include <SkImageEncoder.h>
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070040#include <SkData.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070041#pragma GCC diagnostic pop
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070042
43using namespace android;
44
45static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain;
46
47static void usage(const char* pname)
48{
49 fprintf(stderr,
50 "usage: %s [-hp] [-d display-id] [FILENAME]\n"
51 " -h: this message\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
57 );
58}
59
Mike Reedb9330552014-06-16 17:31:48 -040060static SkColorType flinger2skia(PixelFormat f)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070061{
62 switch (f) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070063 case PIXEL_FORMAT_RGB_565:
Mike Reedb9330552014-06-16 17:31:48 -040064 return kRGB_565_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070065 default:
Mike Reedb9330552014-06-16 17:31:48 -040066 return kN32_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070067 }
68}
69
Umair Khancfed2322014-01-15 08:08:50 -050070static status_t notifyMediaScanner(const char* fileName) {
71 String8 cmd("am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://");
Umair Khancfed2322014-01-15 08:08:50 -050072 cmd.append(fileName);
73 cmd.append(" > /dev/null");
74 int result = system(cmd.string());
75 if (result < 0) {
76 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
77 return UNKNOWN_ERROR;
78 }
79 return NO_ERROR;
80}
81
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070082int main(int argc, char** argv)
83{
Martijn Coenen3b53fc52017-05-11 09:40:45 -070084 // setThreadPoolMaxThreadCount(0) actually tells the kernel it's
85 // not allowed to spawn any additional threads, but we still spawn
86 // a binder thread from userspace when we call startThreadPool().
87 // See b/36066697 for rationale
88 ProcessState::self()->setThreadPoolMaxThreadCount(0);
Mathias Agopian0678a8c2013-03-19 20:56:00 -070089 ProcessState::self()->startThreadPool();
90
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070091 const char* pname = argv[0];
92 bool png = false;
93 int32_t displayId = DEFAULT_DISPLAY_ID;
94 int c;
95 while ((c = getopt(argc, argv, "phd:")) != -1) {
96 switch (c) {
97 case 'p':
98 png = true;
99 break;
100 case 'd':
101 displayId = atoi(optarg);
102 break;
103 case '?':
104 case 'h':
105 usage(pname);
106 return 1;
107 }
108 }
109 argc -= optind;
110 argv += optind;
111
112 int fd = -1;
Andreas Gampecfedceb2014-09-30 21:48:18 -0700113 const char* fn = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700114 if (argc == 0) {
115 fd = dup(STDOUT_FILENO);
116 } else if (argc == 1) {
Umair Khancfed2322014-01-15 08:08:50 -0500117 fn = argv[0];
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700118 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
119 if (fd == -1) {
120 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
121 return 1;
122 }
123 const int len = strlen(fn);
124 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
125 png = true;
126 }
127 }
Prathmesh Prabhubf89ae52016-03-10 15:23:34 -0800128
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700129 if (fd == -1) {
130 usage(pname);
131 return 1;
132 }
133
134 void const* mapbase = MAP_FAILED;
135 ssize_t mapsize = -1;
136
Andreas Gampecfedceb2014-09-30 21:48:18 -0700137 void const* base = NULL;
Mathias Agopian0137fb82013-03-20 15:38:07 -0700138 uint32_t w, s, h, f;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700139 size_t size = 0;
140
Dan Stozacf70d712015-06-09 16:47:33 -0700141 // Maps orientations from DisplayInfo to ISurfaceComposer
142 static const uint32_t ORIENTATION_MAP[] = {
143 ISurfaceComposer::eRotateNone, // 0 == DISPLAY_ORIENTATION_0
144 ISurfaceComposer::eRotate270, // 1 == DISPLAY_ORIENTATION_90
145 ISurfaceComposer::eRotate180, // 2 == DISPLAY_ORIENTATION_180
146 ISurfaceComposer::eRotate90, // 3 == DISPLAY_ORIENTATION_270
147 };
148
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700149 ScreenshotClient screenshot;
150 sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId);
Dan Stozacf70d712015-06-09 16:47:33 -0700151 if (display == NULL) {
152 fprintf(stderr, "Unable to get handle for display %d\n", displayId);
153 return 1;
154 }
155
156 Vector<DisplayInfo> configs;
157 SurfaceComposerClient::getDisplayConfigs(display, &configs);
158 int activeConfig = SurfaceComposerClient::getActiveConfig(display);
159 if (static_cast<size_t>(activeConfig) >= configs.size()) {
160 fprintf(stderr, "Active config %d not inside configs (size %zu)\n",
161 activeConfig, configs.size());
162 return 1;
163 }
164 uint8_t displayOrientation = configs[activeConfig].orientation;
165 uint32_t captureOrientation = ORIENTATION_MAP[displayOrientation];
166
Robert Carrdb7ecbe2017-01-31 11:02:58 -0800167 status_t result = screenshot.update(display, Rect(),
168 0 /* reqWidth */, 0 /* reqHeight */,
169 INT32_MIN, INT32_MAX, /* all layers */
Dan Stozacf70d712015-06-09 16:47:33 -0700170 false, captureOrientation);
171 if (result == NO_ERROR) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700172 base = screenshot.getPixels();
173 w = screenshot.getWidth();
174 h = screenshot.getHeight();
Mathias Agopian0137fb82013-03-20 15:38:07 -0700175 s = screenshot.getStride();
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700176 f = screenshot.getFormat();
177 size = screenshot.getSize();
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700178 }
179
Andreas Gampecfedceb2014-09-30 21:48:18 -0700180 if (base != NULL) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700181 if (png) {
Hal Canary10219fb2016-11-23 20:41:22 -0500182 const SkImageInfo info =
183 SkImageInfo::Make(w, h, flinger2skia(f), kPremul_SkAlphaType);
184 SkPixmap pixmap(info, base, s * bytesPerPixel(f));
185 struct FDWStream final : public SkWStream {
186 size_t fBytesWritten = 0;
187 int fFd;
188 FDWStream(int f) : fFd(f) {}
189 size_t bytesWritten() const override { return fBytesWritten; }
190 bool write(const void* buffer, size_t size) override {
191 fBytesWritten += size;
192 return size == 0 || ::write(fFd, buffer, size) > 0;
193 }
194 } fdStream(fd);
195 (void)SkEncodeImage(&fdStream, pixmap, SkEncodedImageFormat::kPNG, 100);
Andreas Gampecfedceb2014-09-30 21:48:18 -0700196 if (fn != NULL) {
197 notifyMediaScanner(fn);
198 }
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700199 } else {
200 write(fd, &w, 4);
201 write(fd, &h, 4);
202 write(fd, &f, 4);
Mathias Agopian0137fb82013-03-20 15:38:07 -0700203 size_t Bpp = bytesPerPixel(f);
204 for (size_t y=0 ; y<h ; y++) {
205 write(fd, base, w*Bpp);
206 base = (void *)((char *)base + s*Bpp);
207 }
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700208 }
209 }
210 close(fd);
211 if (mapbase != MAP_FAILED) {
212 munmap((void *)mapbase, mapsize);
213 }
Josh Gao90982582017-06-19 13:38:20 -0700214
215 // b/36066697: Avoid running static destructors.
Brian Carlstrom9377ce62017-06-21 22:14:40 -0700216 _exit(0);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700217}