blob: 0bb1af13643c8b90458f0fe618aab6c74bc1fc44 [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
Romain Guy26a2b972017-04-17 09:39:51 -070049#define COLORSPACE_UNKNOWN 0
50#define COLORSPACE_SRGB 1
51#define COLORSPACE_DISPLAY_P3 2
52
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080053static void usage(const char* pname, PhysicalDisplayId displayId)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070054{
55 fprintf(stderr,
56 "usage: %s [-hp] [-d display-id] [FILENAME]\n"
57 " -h: this message\n"
58 " -p: save the file as a png.\n"
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080059 " -d: specify the physical display ID to capture (default: %"
60 ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ")\n"
61 " see \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n"
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070062 "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",
Dominik Laskowski3316a0a2019-01-25 02:56:41 -080064 pname, displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070065}
66
Mike Reedb9330552014-06-16 17:31:48 -040067static SkColorType flinger2skia(PixelFormat f)
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070068{
69 switch (f) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070070 case PIXEL_FORMAT_RGB_565:
Mike Reedb9330552014-06-16 17:31:48 -040071 return kRGB_565_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070072 default:
Mike Reedb9330552014-06-16 17:31:48 -040073 return kN32_SkColorType;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070074 }
75}
76
Peiyong Lin10a34d12018-09-19 13:56:12 -070077static sk_sp<SkColorSpace> dataSpaceToColorSpace(ui::Dataspace d)
Romain Guy26a2b972017-04-17 09:39:51 -070078{
79 switch (d) {
Peiyong Lin10a34d12018-09-19 13:56:12 -070080 case ui::Dataspace::V0_SRGB:
Romain Guy26a2b972017-04-17 09:39:51 -070081 return SkColorSpace::MakeSRGB();
Peiyong Lin10a34d12018-09-19 13:56:12 -070082 case ui::Dataspace::DISPLAY_P3:
Brian Osmanbe8fac262019-01-14 17:02:23 -050083 return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
Romain Guy26a2b972017-04-17 09:39:51 -070084 default:
85 return nullptr;
86 }
87}
88
Peiyong Lin10a34d12018-09-19 13:56:12 -070089static uint32_t dataSpaceToInt(ui::Dataspace d)
Romain Guy26a2b972017-04-17 09:39:51 -070090{
91 switch (d) {
Peiyong Lin10a34d12018-09-19 13:56:12 -070092 case ui::Dataspace::V0_SRGB:
Romain Guy26a2b972017-04-17 09:39:51 -070093 return COLORSPACE_SRGB;
Peiyong Lin10a34d12018-09-19 13:56:12 -070094 case ui::Dataspace::DISPLAY_P3:
Romain Guy26a2b972017-04-17 09:39:51 -070095 return COLORSPACE_DISPLAY_P3;
96 default:
97 return COLORSPACE_UNKNOWN;
98 }
99}
100
Umair Khancfed2322014-01-15 08:08:50 -0500101static status_t notifyMediaScanner(const char* fileName) {
102 String8 cmd("am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://");
Umair Khancfed2322014-01-15 08:08:50 -0500103 cmd.append(fileName);
104 cmd.append(" > /dev/null");
105 int result = system(cmd.string());
106 if (result < 0) {
107 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
108 return UNKNOWN_ERROR;
109 }
110 return NO_ERROR;
111}
112
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700113int main(int argc, char** argv)
114{
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800115 std::optional<PhysicalDisplayId> displayId = SurfaceComposerClient::getInternalDisplayId();
116 if (!displayId) {
117 fprintf(stderr, "Failed to get token for internal display\n");
118 return 1;
119 }
120
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700121 const char* pname = argv[0];
122 bool png = false;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700123 int c;
124 while ((c = getopt(argc, argv, "phd:")) != -1) {
125 switch (c) {
126 case 'p':
127 png = true;
128 break;
129 case 'd':
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800130 displayId = atoll(optarg);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700131 break;
132 case '?':
133 case 'h':
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800134 usage(pname, *displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700135 return 1;
136 }
137 }
138 argc -= optind;
139 argv += optind;
140
141 int fd = -1;
Andreas Gampecfedceb2014-09-30 21:48:18 -0700142 const char* fn = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700143 if (argc == 0) {
144 fd = dup(STDOUT_FILENO);
145 } else if (argc == 1) {
Umair Khancfed2322014-01-15 08:08:50 -0500146 fn = argv[0];
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700147 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
148 if (fd == -1) {
149 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
150 return 1;
151 }
152 const int len = strlen(fn);
153 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
154 png = true;
155 }
156 }
Prathmesh Prabhubf89ae52016-03-10 15:23:34 -0800157
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700158 if (fd == -1) {
Dominik Laskowski3316a0a2019-01-25 02:56:41 -0800159 usage(pname, *displayId);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700160 return 1;
161 }
162
163 void const* mapbase = MAP_FAILED;
164 ssize_t mapsize = -1;
165
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000166 void* base = NULL;
Mathias Agopian0137fb82013-03-20 15:38:07 -0700167 uint32_t w, s, h, f;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700168 size_t size = 0;
169
Martijn Coenen48b74082017-07-24 09:19:26 +0200170 // setThreadPoolMaxThreadCount(0) actually tells the kernel it's
171 // not allowed to spawn any additional threads, but we still spawn
172 // a binder thread from userspace when we call startThreadPool().
173 // See b/36066697 for rationale
174 ProcessState::self()->setThreadPoolMaxThreadCount(0);
175 ProcessState::self()->startThreadPool();
176
chaviwa69a1b82019-04-30 16:53:33 -0700177 ui::Dataspace outDataspace;
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000178 sp<GraphicBuffer> outBuffer;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700179
chaviwa69a1b82019-04-30 16:53:33 -0700180 status_t result = ScreenshotClient::capture(*displayId, &outDataspace, &outBuffer);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000181 if (result != NO_ERROR) {
182 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700183 return 1;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700184 }
185
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000186 result = outBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base);
187
chaviw7f4dc7e2018-09-11 13:59:30 -0700188 if (base == nullptr || result != NO_ERROR) {
189 String8 reason;
chaviwa69a1b82019-04-30 16:53:33 -0700190 if (result != NO_ERROR) {
191 reason.appendFormat(" Error Code: %d", result);
chaviw7f4dc7e2018-09-11 13:59:30 -0700192 } else {
chaviwa69a1b82019-04-30 16:53:33 -0700193 reason = "Failed to write to buffer";
chaviw7f4dc7e2018-09-11 13:59:30 -0700194 }
195 fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str());
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000196 close(fd);
Steven Morelanda89ae862018-05-24 17:48:28 -0700197 return 1;
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000198 }
199
200 w = outBuffer->getWidth();
201 h = outBuffer->getHeight();
202 s = outBuffer->getStride();
203 f = outBuffer->getPixelFormat();
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000204 size = s * h * bytesPerPixel(f);
205
206 if (png) {
207 const SkImageInfo info =
Peiyong Lin10a34d12018-09-19 13:56:12 -0700208 SkImageInfo::Make(w, h, flinger2skia(f), kPremul_SkAlphaType,
chaviwa69a1b82019-04-30 16:53:33 -0700209 dataSpaceToColorSpace(outDataspace));
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000210 SkPixmap pixmap(info, base, s * bytesPerPixel(f));
211 struct FDWStream final : public SkWStream {
212 size_t fBytesWritten = 0;
213 int fFd;
214 FDWStream(int f) : fFd(f) {}
215 size_t bytesWritten() const override { return fBytesWritten; }
216 bool write(const void* buffer, size_t size) override {
217 fBytesWritten += size;
218 return size == 0 || ::write(fFd, buffer, size) > 0;
219 }
220 } fdStream(fd);
221 (void)SkEncodeImage(&fdStream, pixmap, SkEncodedImageFormat::kPNG, 100);
222 if (fn != NULL) {
223 notifyMediaScanner(fn);
224 }
225 } else {
chaviwa69a1b82019-04-30 16:53:33 -0700226 uint32_t c = dataSpaceToInt(outDataspace);
Chavi Weingartend7ec64c2017-11-30 01:52:01 +0000227 write(fd, &w, 4);
228 write(fd, &h, 4);
229 write(fd, &f, 4);
230 write(fd, &c, 4);
231 size_t Bpp = bytesPerPixel(f);
232 for (size_t y=0 ; y<h ; y++) {
233 write(fd, base, w*Bpp);
234 base = (void *)((char *)base + s*Bpp);
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700235 }
236 }
237 close(fd);
238 if (mapbase != MAP_FAILED) {
239 munmap((void *)mapbase, mapsize);
240 }
Josh Gao90982582017-06-19 13:38:20 -0700241
Steven Morelanda89ae862018-05-24 17:48:28 -0700242 return 0;
Peiyong Lin10a34d12018-09-19 13:56:12 -0700243}