2012-10-24 12:31:10 -07:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
2014-01-15 08:08:50 -05:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-10-24 12:31:10 -07:00
|
|
|
|
|
|
|
#include <linux/fb.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2013-03-19 20:56:00 -07:00
|
|
|
#include <binder/ProcessState.h>
|
|
|
|
|
2012-10-24 12:31:10 -07:00
|
|
|
#include <gui/SurfaceComposerClient.h>
|
|
|
|
#include <gui/ISurfaceComposer.h>
|
|
|
|
|
2015-06-09 16:47:33 -07:00
|
|
|
#include <ui/DisplayInfo.h>
|
2018-09-19 13:56:12 -07:00
|
|
|
#include <ui/GraphicTypes.h>
|
2013-03-20 15:38:07 -07:00
|
|
|
#include <ui/PixelFormat.h>
|
|
|
|
|
2017-04-17 09:39:51 -07:00
|
|
|
#include <system/graphics.h>
|
|
|
|
|
2014-09-30 21:48:18 -07:00
|
|
|
// TODO: Fix Skia.
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
2012-10-24 12:31:10 -07:00
|
|
|
#include <SkImageEncoder.h>
|
|
|
|
#include <SkData.h>
|
2017-04-17 09:39:51 -07:00
|
|
|
#include <SkColorSpace.h>
|
2014-09-30 21:48:18 -07:00
|
|
|
#pragma GCC diagnostic pop
|
2012-10-24 12:31:10 -07:00
|
|
|
|
|
|
|
using namespace android;
|
|
|
|
|
2017-04-17 09:39:51 -07:00
|
|
|
#define COLORSPACE_UNKNOWN 0
|
|
|
|
#define COLORSPACE_SRGB 1
|
|
|
|
#define COLORSPACE_DISPLAY_P3 2
|
|
|
|
|
2019-01-25 02:56:41 -08:00
|
|
|
static void usage(const char* pname, PhysicalDisplayId displayId)
|
2012-10-24 12:31:10 -07:00
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"usage: %s [-hp] [-d display-id] [FILENAME]\n"
|
|
|
|
" -h: this message\n"
|
|
|
|
" -p: save the file as a png.\n"
|
2019-01-25 02:56:41 -08:00
|
|
|
" -d: specify the physical display ID to capture (default: %"
|
|
|
|
ANDROID_PHYSICAL_DISPLAY_ID_FORMAT ")\n"
|
|
|
|
" see \"dumpsys SurfaceFlinger --display-id\" for valid display IDs.\n"
|
2012-10-24 12:31:10 -07:00
|
|
|
"If FILENAME ends with .png it will be saved as a png.\n"
|
|
|
|
"If FILENAME is not given, the results will be printed to stdout.\n",
|
2019-01-25 02:56:41 -08:00
|
|
|
pname, displayId);
|
2012-10-24 12:31:10 -07:00
|
|
|
}
|
|
|
|
|
2014-06-16 17:31:48 -04:00
|
|
|
static SkColorType flinger2skia(PixelFormat f)
|
2012-10-24 12:31:10 -07:00
|
|
|
{
|
|
|
|
switch (f) {
|
|
|
|
case PIXEL_FORMAT_RGB_565:
|
2014-06-16 17:31:48 -04:00
|
|
|
return kRGB_565_SkColorType;
|
2012-10-24 12:31:10 -07:00
|
|
|
default:
|
2014-06-16 17:31:48 -04:00
|
|
|
return kN32_SkColorType;
|
2012-10-24 12:31:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 13:56:12 -07:00
|
|
|
static sk_sp<SkColorSpace> dataSpaceToColorSpace(ui::Dataspace d)
|
2017-04-17 09:39:51 -07:00
|
|
|
{
|
|
|
|
switch (d) {
|
2018-09-19 13:56:12 -07:00
|
|
|
case ui::Dataspace::V0_SRGB:
|
2017-04-17 09:39:51 -07:00
|
|
|
return SkColorSpace::MakeSRGB();
|
2018-09-19 13:56:12 -07:00
|
|
|
case ui::Dataspace::DISPLAY_P3:
|
2019-01-14 17:02:23 -05:00
|
|
|
return SkColorSpace::MakeRGB(SkNamedTransferFn::kSRGB, SkNamedGamut::kDCIP3);
|
2017-04-17 09:39:51 -07:00
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-19 13:56:12 -07:00
|
|
|
static uint32_t dataSpaceToInt(ui::Dataspace d)
|
2017-04-17 09:39:51 -07:00
|
|
|
{
|
|
|
|
switch (d) {
|
2018-09-19 13:56:12 -07:00
|
|
|
case ui::Dataspace::V0_SRGB:
|
2017-04-17 09:39:51 -07:00
|
|
|
return COLORSPACE_SRGB;
|
2018-09-19 13:56:12 -07:00
|
|
|
case ui::Dataspace::DISPLAY_P3:
|
2017-04-17 09:39:51 -07:00
|
|
|
return COLORSPACE_DISPLAY_P3;
|
|
|
|
default:
|
|
|
|
return COLORSPACE_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-15 08:08:50 -05:00
|
|
|
static status_t notifyMediaScanner(const char* fileName) {
|
|
|
|
String8 cmd("am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file://");
|
|
|
|
cmd.append(fileName);
|
|
|
|
cmd.append(" > /dev/null");
|
|
|
|
int result = system(cmd.string());
|
|
|
|
if (result < 0) {
|
|
|
|
fprintf(stderr, "Unable to broadcast intent for media scanner.\n");
|
|
|
|
return UNKNOWN_ERROR;
|
|
|
|
}
|
|
|
|
return NO_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-10-24 12:31:10 -07:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2019-01-25 02:56:41 -08:00
|
|
|
std::optional<PhysicalDisplayId> displayId = SurfaceComposerClient::getInternalDisplayId();
|
|
|
|
if (!displayId) {
|
|
|
|
fprintf(stderr, "Failed to get token for internal display\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-10-24 12:31:10 -07:00
|
|
|
const char* pname = argv[0];
|
|
|
|
bool png = false;
|
|
|
|
int c;
|
|
|
|
while ((c = getopt(argc, argv, "phd:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'p':
|
|
|
|
png = true;
|
|
|
|
break;
|
|
|
|
case 'd':
|
2019-01-25 02:56:41 -08:00
|
|
|
displayId = atoll(optarg);
|
2012-10-24 12:31:10 -07:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
case 'h':
|
2019-01-25 02:56:41 -08:00
|
|
|
usage(pname, *displayId);
|
2012-10-24 12:31:10 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
int fd = -1;
|
2014-09-30 21:48:18 -07:00
|
|
|
const char* fn = NULL;
|
2012-10-24 12:31:10 -07:00
|
|
|
if (argc == 0) {
|
|
|
|
fd = dup(STDOUT_FILENO);
|
|
|
|
} else if (argc == 1) {
|
2014-01-15 08:08:50 -05:00
|
|
|
fn = argv[0];
|
2012-10-24 12:31:10 -07:00
|
|
|
fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC, 0664);
|
|
|
|
if (fd == -1) {
|
|
|
|
fprintf(stderr, "Error opening file: %s (%s)\n", fn, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
const int len = strlen(fn);
|
|
|
|
if (len >= 4 && 0 == strcmp(fn+len-4, ".png")) {
|
|
|
|
png = true;
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 15:23:34 -08:00
|
|
|
|
2012-10-24 12:31:10 -07:00
|
|
|
if (fd == -1) {
|
2019-01-25 02:56:41 -08:00
|
|
|
usage(pname, *displayId);
|
2012-10-24 12:31:10 -07:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void const* mapbase = MAP_FAILED;
|
|
|
|
ssize_t mapsize = -1;
|
|
|
|
|
2017-11-30 01:52:01 +00:00
|
|
|
void* base = NULL;
|
2013-03-20 15:38:07 -07:00
|
|
|
uint32_t w, s, h, f;
|
2012-10-24 12:31:10 -07:00
|
|
|
size_t size = 0;
|
|
|
|
|
2017-07-24 09:19:26 +02:00
|
|
|
// setThreadPoolMaxThreadCount(0) actually tells the kernel it's
|
|
|
|
// not allowed to spawn any additional threads, but we still spawn
|
|
|
|
// a binder thread from userspace when we call startThreadPool().
|
|
|
|
// See b/36066697 for rationale
|
|
|
|
ProcessState::self()->setThreadPoolMaxThreadCount(0);
|
|
|
|
ProcessState::self()->startThreadPool();
|
|
|
|
|
2019-04-30 16:53:33 -07:00
|
|
|
ui::Dataspace outDataspace;
|
2017-11-30 01:52:01 +00:00
|
|
|
sp<GraphicBuffer> outBuffer;
|
2018-09-19 13:56:12 -07:00
|
|
|
|
2019-04-30 16:53:33 -07:00
|
|
|
status_t result = ScreenshotClient::capture(*displayId, &outDataspace, &outBuffer);
|
2017-11-30 01:52:01 +00:00
|
|
|
if (result != NO_ERROR) {
|
|
|
|
close(fd);
|
2018-05-24 17:48:28 -07:00
|
|
|
return 1;
|
2017-11-30 01:52:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result = outBuffer->lock(GraphicBuffer::USAGE_SW_READ_OFTEN, &base);
|
|
|
|
|
2018-09-11 13:59:30 -07:00
|
|
|
if (base == nullptr || result != NO_ERROR) {
|
|
|
|
String8 reason;
|
2019-04-30 16:53:33 -07:00
|
|
|
if (result != NO_ERROR) {
|
|
|
|
reason.appendFormat(" Error Code: %d", result);
|
2018-09-11 13:59:30 -07:00
|
|
|
} else {
|
2019-04-30 16:53:33 -07:00
|
|
|
reason = "Failed to write to buffer";
|
2018-09-11 13:59:30 -07:00
|
|
|
}
|
|
|
|
fprintf(stderr, "Failed to take screenshot (%s)\n", reason.c_str());
|
2017-11-30 01:52:01 +00:00
|
|
|
close(fd);
|
2018-05-24 17:48:28 -07:00
|
|
|
return 1;
|
2012-10-24 12:31:10 -07:00
|
|
|
}
|
|
|
|
|
2017-11-30 01:52:01 +00:00
|
|
|
w = outBuffer->getWidth();
|
|
|
|
h = outBuffer->getHeight();
|
|
|
|
s = outBuffer->getStride();
|
|
|
|
f = outBuffer->getPixelFormat();
|
|
|
|
size = s * h * bytesPerPixel(f);
|
|
|
|
|
|
|
|
if (png) {
|
|
|
|
const SkImageInfo info =
|
2018-09-19 13:56:12 -07:00
|
|
|
SkImageInfo::Make(w, h, flinger2skia(f), kPremul_SkAlphaType,
|
2019-04-30 16:53:33 -07:00
|
|
|
dataSpaceToColorSpace(outDataspace));
|
2017-11-30 01:52:01 +00:00
|
|
|
SkPixmap pixmap(info, base, s * bytesPerPixel(f));
|
|
|
|
struct FDWStream final : public SkWStream {
|
|
|
|
size_t fBytesWritten = 0;
|
|
|
|
int fFd;
|
|
|
|
FDWStream(int f) : fFd(f) {}
|
|
|
|
size_t bytesWritten() const override { return fBytesWritten; }
|
|
|
|
bool write(const void* buffer, size_t size) override {
|
|
|
|
fBytesWritten += size;
|
|
|
|
return size == 0 || ::write(fFd, buffer, size) > 0;
|
|
|
|
}
|
|
|
|
} fdStream(fd);
|
|
|
|
(void)SkEncodeImage(&fdStream, pixmap, SkEncodedImageFormat::kPNG, 100);
|
|
|
|
if (fn != NULL) {
|
|
|
|
notifyMediaScanner(fn);
|
|
|
|
}
|
|
|
|
} else {
|
2019-04-30 16:53:33 -07:00
|
|
|
uint32_t c = dataSpaceToInt(outDataspace);
|
2017-11-30 01:52:01 +00:00
|
|
|
write(fd, &w, 4);
|
|
|
|
write(fd, &h, 4);
|
|
|
|
write(fd, &f, 4);
|
|
|
|
write(fd, &c, 4);
|
|
|
|
size_t Bpp = bytesPerPixel(f);
|
|
|
|
for (size_t y=0 ; y<h ; y++) {
|
|
|
|
write(fd, base, w*Bpp);
|
|
|
|
base = (void *)((char *)base + s*Bpp);
|
2012-10-24 12:31:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
if (mapbase != MAP_FAILED) {
|
|
|
|
munmap((void *)mapbase, mapsize);
|
|
|
|
}
|
2017-06-19 13:38:20 -07:00
|
|
|
|
2018-05-24 17:48:28 -07:00
|
|
|
return 0;
|
2018-09-19 13:56:12 -07:00
|
|
|
}
|