blob: 3d0d112c9f9fb4eb9d8c494135ab54a9eb55422a [file] [log] [blame]
Alex Vakulenko4f079d02017-01-27 14:41:25 -08001// screencap is a tool for taking screenshots using the screenshot service.
2
3#include <fstream>
4#include <iostream>
5#include <string>
6#include <vector>
7
8#include <private/dvr/image_io.h>
9#include <private/dvr/screenshot_client.h>
10
11namespace {
12
13// Attempt to take a screenshot and save it to |filename|.
14// Returns zero on success, or a non-zero exit code otherwise.
15int TakeScreenshot(const std::string& app_name, const std::string& filename,
16 int index) {
17 auto error_out = [app_name]() -> std::ostream& {
18 return std::cerr << app_name << ": ";
19 };
20
21 auto info_out = [app_name]() -> std::ostream& {
22 return std::cout << app_name << ": ";
23 };
24
25 auto client = android::dvr::ScreenshotClient::Create();
26
27 if (client->format() != HAL_PIXEL_FORMAT_RGB_888) {
28 error_out() << "The screenshot format for this device is not supported."
29 << std::endl;
30 return 1;
31 }
32
33 std::vector<uint8_t> image;
34 int width = 0;
35 int height = 0;
36 if (client->Take(&image, index, &width, &height) != 0) {
37 error_out() << "Failed to take screenshot." << std::endl;
38 return 1;
39 }
40
41 info_out() << "Got " << width << "x" << height << " screenshot." << std::endl;
42
43 if (!image_io_write_rgb888(filename.c_str(), width, height, image.data())) {
44 error_out() << "Failed to write image to output file " << filename
45 << std::endl;
46 return 1;
47 }
48
49 return 0;
50}
51
52} // namespace
53
54int main(int argc, char** argv) {
55 // Parse arguments
56 if (argc != 2 && argc != 3) {
57 std::cerr
58 << "Usage: " << argv[0]
59 << " filename.[" DVR_IMAGE_IO_SUPPORTED_WRITE
60 "] [INDEX]\n"
61 "INDEX: specify 1..n to grab hw_composer layers by index.\n"
62 " specify -n to grab pre-warp layers (-1 is base layer).\n"
63 " the default is 1 (the base hw_composer layer).\n"
64 " an invalid index will result in an error.\n";
65 return 1;
66 }
67 const std::string filename(argv[1]);
68 int index = 1;
69 if (argc > 2)
70 index = atoi(argv[2]);
71
72 // Perform the actual screenshot.
73 return TakeScreenshot(argv[0], filename, index);
74}