blob: 7bf073b4b1a17271b1765e569e260949f289a5b1 [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://");
72 String8 fileUrl("\"");
73 fileUrl.append(fileName);
74 fileUrl.append("\"");
75 cmd.append(fileName);
76 cmd.append(" > /dev/null");
77 int result = system(cmd.string());
78 if (result < 0) {
79 fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
80 return UNKNOWN_ERROR;
81 }
82 return NO_ERROR;
83}
84
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070085int main(int argc, char** argv)
86{
Mathias Agopian0678a8c2013-03-19 20:56:00 -070087 ProcessState::self()->startThreadPool();
88
Mike Lockwoodc59b2f92012-10-24 12:31:10 -070089 const char* pname = argv[0];
90 bool png = false;
91 int32_t displayId = DEFAULT_DISPLAY_ID;
92 int c;
93 while ((c = getopt(argc, argv, "phd:")) != -1) {
94 switch (c) {
95 case 'p':
96 png = true;
97 break;
98 case 'd':
99 displayId = atoi(optarg);
100 break;
101 case '?':
102 case 'h':
103 usage(pname);
104 return 1;
105 }
106 }
107 argc -= optind;
108 argv += optind;
109
110 int fd = -1;
Andreas Gampecfedceb2014-09-30 21:48:18 -0700111 const char* fn = NULL;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700112 if (argc == 0) {
113 fd = dup(STDOUT_FILENO);
114 } else if (argc == 1) {
Umair Khancfed2322014-01-15 08:08:50 -0500115 fn = argv[0];
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700116 fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
117 if (fd == -1) {
118 fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
119 return 1;
120 }
121 const int len = strlen(fn);
122 if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
123 png = true;
124 }
125 }
Prathmesh Prabhubf89ae52016-03-10 15:23:34 -0800126
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700127 if (fd == -1) {
128 usage(pname);
129 return 1;
130 }
131
132 void const* mapbase = MAP_FAILED;
133 ssize_t mapsize = -1;
134
Andreas Gampecfedceb2014-09-30 21:48:18 -0700135 void const* base = NULL;
Mathias Agopian0137fb82013-03-20 15:38:07 -0700136 uint32_t w, s, h, f;
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700137 size_t size = 0;
138
Dan Stozacf70d712015-06-09 16:47:33 -0700139 // 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
145 };
146
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700147 ScreenshotClient screenshot;
148 sp<IBinder> display = SurfaceComposerClient::getBuiltInDisplay(displayId);
Dan Stozacf70d712015-06-09 16:47:33 -0700149 if (display == NULL) {
150 fprintf(stderr, "Unable to get handle for display %d\n", displayId);
151 return 1;
152 }
153
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());
160 return 1;
161 }
162 uint8_t displayOrientation = configs[activeConfig].orientation;
163 uint32_t captureOrientation = ORIENTATION_MAP[displayOrientation];
164
165 status_t result = screenshot.update(display, Rect(), 0, 0, 0, -1U,
166 false, captureOrientation);
167 if (result == NO_ERROR) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700168 base = screenshot.getPixels();
169 w = screenshot.getWidth();
170 h = screenshot.getHeight();
Mathias Agopian0137fb82013-03-20 15:38:07 -0700171 s = screenshot.getStride();
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700172 f = screenshot.getFormat();
173 size = screenshot.getSize();
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700174 }
175
Andreas Gampecfedceb2014-09-30 21:48:18 -0700176 if (base != NULL) {
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700177 if (png) {
Mike Reedb9330552014-06-16 17:31:48 -0400178 const SkImageInfo info = SkImageInfo::Make(w, h, flinger2skia(f),
179 kPremul_SkAlphaType);
Leon Scroggins III34497892015-01-20 15:52:43 -0500180 SkAutoTUnref<SkData> data(SkImageEncoder::EncodeData(info, base, s*bytesPerPixel(f),
181 SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality));
182 if (data.get()) {
183 write(fd, data->data(), data->size());
184 }
Andreas Gampecfedceb2014-09-30 21:48:18 -0700185 if (fn != NULL) {
186 notifyMediaScanner(fn);
187 }
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700188 } else {
189 write(fd, &w, 4);
190 write(fd, &h, 4);
191 write(fd, &f, 4);
Mathias Agopian0137fb82013-03-20 15:38:07 -0700192 size_t Bpp = bytesPerPixel(f);
193 for (size_t y=0 ; y<h ; y++) {
194 write(fd, base, w*Bpp);
195 base = (void *)((char *)base + s*Bpp);
196 }
Mike Lockwoodc59b2f92012-10-24 12:31:10 -0700197 }
198 }
199 close(fd);
200 if (mapbase != MAP_FAILED) {
201 munmap((void *)mapbase, mapsize);
202 }
203 return 0;
204}