blob: 3d74f8b207af1f68937e0f201cab8ce6e4cce016 [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>
Peiyong Lin10a34d12018-09-19 13:56:12 -070034#include <ui/GraphicTypes.h>
Mathias Agopian0137fb82013-03-20 15:38:07 -070035#include <ui/PixelFormat.h>
36
Romain Guy26a2b972017-04-17 09:39:51 -070037#include <system/graphics.h>
38
Andreas Gampecfedceb2014-09-30 21:48:18 -070039// TODO: Fix Skia.
40#pragma GCC diagnostic push
41#pragma GCC diagnostic ignored "-Wunused-parameter"
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070042#include <SkImageEncoder.h>
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070043#include <SkData.h>
Romain Guy26a2b972017-04-17 09:39:51 -070044#include <SkColorSpace.h>
Andreas Gampecfedceb2014-09-30 21:48:18 -070045#pragma GCC diagnostic pop
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070046
47using namespace android;
48
49static uint32_t DEFAULT_DISPLAY_ID = ISurfaceComposer::eDisplayIdMain;
50
Romain Guy26a2b972017-04-17 09:39:51 -070051#define COLORSPACE_UNKNOWN 0
52#define COLORSPACE_SRGB 1
53#define COLORSPACE_DISPLAY_P3 2
54
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070055static void usage(const char* pname)
56{
57 fprintf(stderr,
58 "usage: %s [-hp] [-d display-id] [FILENAME]\n"
59 " -h: this message\n"
60 " -p: save the file as a png.\n"
61 " -d: specify the display id to capture, default %d.\n"
62 "If FILENAME ends with .png it will be saved as a png.\n"
63 "If FILENAME is not given, the results will be printed to stdout.\n",
64 pname, DEFAULT_DISPLAY_ID
65 );
66}
67
Mike Reedb9330552014-06-16 17:31:48 -040068static SkColorType flinger2skia(PixelFormat f)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070069{
70 switch (f) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070071 case PIXEL_FORMAT_RGB_565:
Mike Reedb9330552014-06-16 17:31:48 -040072 return kRGB_565_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070073 default:
Mike Reedb9330552014-06-16 17:31:48 -040074 return kN32_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070075 }
76}
77
Peiyong Lin10a34d12018-09-19 13:56:12 -070078static sk_sp<SkColorSpace> dataSpaceToColorSpace(ui::Dataspace d)
Romain Guy26a2b972017-04-17 09:39:51 -070079{
80 switch (d) {
Peiyong Lin10a34d12018-09-19 13:56:12 -070081 case ui::Dataspace::V0_SRGB:
Romain Guy26a2b972017-04-17 09:39:51 -070082 return SkColorSpace::MakeSRGB();
Peiyong Lin10a34d12018-09-19 13:56:12 -070083 case ui::Dataspace::DISPLAY_P3:
Brian Osmanbe8fac262019-01-14 17:02:23 -050084 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
Romain Guy26a2b972017-04-17 09:39:51 -070085 default:
86 return nullptr;
87 }
88}
89
Peiyong Lin10a34d12018-09-19 13:56:12 -070090static ui::Dataspace pickBestDataspace(ui::ColorMode colorMode)
91{
92 switch (colorMode) {
93 case ui::ColorMode::SRGB:
94 return ui::Dataspace::V0_SRGB;
95 case ui::ColorMode::DISPLAY_P3:
96 case ui::ColorMode::BT2100_PQ:
97 case ui::ColorMode::BT2100_HLG:
98 return ui::Dataspace::DISPLAY_P3;
99 default:
100 return ui::Dataspace::V0_SRGB;
101 }
102}
103
104static uint32_t dataSpaceToInt(ui::Dataspace d)
Romain Guy26a2b972017-04-17 09:39:51 -0700105{
106 switch (d) {
Peiyong Lin10a34d12018-09-19 13:56:12 -0700107 case ui::Dataspace::V0_SRGB:
Romain Guy26a2b972017-04-17 09:39:51 -0700108 return COLORSPACE_SRGB;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700109 case ui::Dataspace::DISPLAY_P3:
Romain Guy26a2b972017-04-17 09:39:51 -0700110 return COLORSPACE_DISPLAY_P3;
111 default:
112 return COLORSPACE_UNKNOWN;
113 }
114}
115
Umair Khancfed2322014-01-15 08:08:50 -0500116static status_t notifyMediaScanner(const char* fileName) {
117 String8 cmd("am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://");
Umair Khancfed2322014-01-15 08:08:50 -0500118 cmd.append(fileName);
119 cmd.append(" > /dev/null");
120 int result = system(cmd.string());
121 if (result < 0) {
122 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
123 return UNKNOWN_ERROR;
124 }
125 return NO_ERROR;
126}
127
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700128int main(int argc, char** argv)
129{
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700130 const char* pname = argv[0];
131 bool png = false;
132 int32_t displayId = DEFAULT_DISPLAY_ID;
133 int c;
134 while ((c = getopt(argc, argv, "phd:")) != -1) {
135 switch (c) {
136 case 'p':
137 png = true;
138 break;
139 case 'd':
140 displayId = atoi(optarg);
141 break;
142 case '?':
143 case 'h':
144 usage(pname);
145 return 1;
146 }
147 }
148 argc -= optind;
149 argv += optind;
150
151 int fd = -1;
Andreas Gampecfedceb2014-09-30 21:48:18 -0700152 const char* fn = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700153 if (argc == 0) {
154 fd = dup(STDOUT_FILENO);
155 } else if (argc == 1) {
Umair Khancfed2322014-01-15 08:08:50 -0500156 fn = argv[0];
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700157 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
158 if (fd == -1) {
159 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
160 return 1;
161 }
162 const int len = strlen(fn);
163 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
164 png = true;
165 }
166 }
Prathmesh Prabhubf89ae52016-03-10 15:23:34 -0800167
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700168 if (fd == -1) {
169 usage(pname);
170 return 1;
171 }
172
173 void const* mapbase = MAP_FAILED;
174 ssize_t mapsize = -1;
175
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000176 void* base = NULL;
Mathias Agopian0137fb82013-03-20 15:38:07 -0700177 uint32_t w, s, h, f;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700178 size_t size = 0;
179
Dan Stozacf70d712015-06-09 16:47:33 -0700180 // Maps orientations from DisplayInfo to ISurfaceComposer
181 static const uint32_t ORIENTATION_MAP[] = {
182 ISurfaceComposer::eRotateNone, // 0 == DISPLAY_ORIENTATION_0
183 ISurfaceComposer::eRotate270, // 1 == DISPLAY_ORIENTATION_90
184 ISurfaceComposer::eRotate180, // 2 == DISPLAY_ORIENTATION_180
185 ISurfaceComposer::eRotate90, // 3 == DISPLAY_ORIENTATION_270
186 };
187
Martijn Coenen48b74082017-07-24 09:19:26 +0200188 // setThreadPoolMaxThreadCount(0) actually tells the kernel it's
189 // not allowed to spawn any additional threads, but we still spawn
190 // a binder thread from userspace when we call startThreadPool().
191 // See b/36066697 for rationale
192 ProcessState::self()->setThreadPoolMaxThreadCount(0);
193 ProcessState::self()->startThreadPool();
194
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700195 sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId);
Dan Stozacf70d712015-06-09 16:47:33 -0700196 if (display == NULL) {
197 fprintf(stderr, "Unable to get handle for display %d\n", displayId);
Steven Morelanda89ae862018-05-24 17:48:28 -0700198 return 1;
Dan Stozacf70d712015-06-09 16:47:33 -0700199 }
200
201 Vector<DisplayInfo> configs;
202 SurfaceComposerClient::getDisplayConfigs(display, &configs);
203 int activeConfig = SurfaceComposerClient::getActiveConfig(display);
204 if (static_cast<size_t>(activeConfig) >= configs.size()) {
205 fprintf(stderr, "Active config %d not inside configs (size %zu)\n",
206 activeConfig, configs.size());
Steven Morelanda89ae862018-05-24 17:48:28 -0700207 return 1;
Dan Stozacf70d712015-06-09 16:47:33 -0700208 }
209 uint8_t displayOrientation = configs[activeConfig].orientation;
210 uint32_t captureOrientation = ORIENTATION_MAP[displayOrientation];
211
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000212 sp<GraphicBuffer> outBuffer;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700213 ui::Dataspace reqDataspace =
214 pickBestDataspace(SurfaceComposerClient::getActiveColorMode(display));
215
216 // Due to the fact that we hard code the way we write pixels into screenshot,
217 // we hard code RGBA_8888 here.
218 ui::PixelFormat reqPixelFormat = ui::PixelFormat::RGBA_8888;
219 status_t result = ScreenshotClient::capture(display, reqDataspace, reqPixelFormat, Rect(),
220 0 /* reqWidth */, 0 /* reqHeight */, false,
221 captureOrientation, &outBuffer);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000222 if (result != NO_ERROR) {
223 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700224 return 1;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700225 }
226
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000227 result = outBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base);
228
chaviw7f4dc7e2018-09-11 13:59:30 -0700229 if (base == nullptr || result != NO_ERROR) {
230 String8 reason;
231 if (base == nullptr) {
232 reason = "Failed to write to buffer";
233 } else {
234 reason.appendFormat("Error Code: %d", result);
235 }
236 fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str());
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000237 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700238 return 1;
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000239 }
240
241 w = outBuffer->getWidth();
242 h = outBuffer->getHeight();
243 s = outBuffer->getStride();
244 f = outBuffer->getPixelFormat();
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000245 size = s * h * bytesPerPixel(f);
246
247 if (png) {
248 const SkImageInfo info =
Peiyong Lin10a34d12018-09-19 13:56:12 -0700249 SkImageInfo::Make(w, h, flinger2skia(f), kPremul_SkAlphaType,
250 dataSpaceToColorSpace(reqDataspace));
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000251 SkPixmap pixmap(info, base, s * bytesPerPixel(f));
252 struct FDWStream final : public SkWStream {
253 size_t fBytesWritten = 0;
254 int fFd;
255 FDWStream(int f) : fFd(f) {}
256 size_t bytesWritten() const override { return fBytesWritten; }
257 bool write(const void* buffer, size_t size) override {
258 fBytesWritten += size;
259 return size == 0 || ::write(fFd, buffer, size) > 0;
260 }
261 } fdStream(fd);
262 (void)SkEncodeImage(&fdStream, pixmap, SkEncodedImageFormat::kPNG, 100);
263 if (fn != NULL) {
264 notifyMediaScanner(fn);
265 }
266 } else {
Peiyong Lin10a34d12018-09-19 13:56:12 -0700267 uint32_t c = dataSpaceToInt(reqDataspace);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000268 write(fd, &w, 4);
269 write(fd, &h, 4);
270 write(fd, &f, 4);
271 write(fd, &c, 4);
272 size_t Bpp = bytesPerPixel(f);
273 for (size_t y=0 ; y<h ; y++) {
274 write(fd, base, w*Bpp);
275 base = (void *)((char *)base + s*Bpp);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700276 }
277 }
278 close(fd);
279 if (mapbase != MAP_FAILED) {
280 munmap((void *)mapbase, mapsize);
281 }
Josh Gao90982582017-06-19 13:38:20 -0700282
Steven Morelanda89ae862018-05-24 17:48:28 -0700283 return 0;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700284}