blob: 78f5e0fe495fc75b60395bb7fa5420fc4ce9bff4 [file] [log] [blame]
Alex Vakulenkoe4eec202017-01-27 14:41:04 -08001#include "include/private/dvr/screenshot_client.h"
2
3#include <cutils/log.h>
4
5#include <mutex>
6
7#include <pdx/default_transport/client_channel_factory.h>
8#include <private/dvr/display_rpc.h>
9
10using android::pdx::Transaction;
11using android::pdx::rpc::ClientPayload;
12using android::pdx::rpc::MessageBuffer;
13using android::pdx::rpc::ReplyBuffer;
14
15namespace android {
16namespace dvr {
17
18namespace {
19// Maximum supported pixels for screenshot capture. If the actual target buffer
20// is more than this, an error will be reported.
21constexpr int kMaxScreenshotPixels = 6000 * 4000;
22} // namespace
23
24int ScreenshotClient::Take(std::vector<uint8_t>* out_image, int index,
25 int* return_width, int* return_height) {
26 if (format_ != HAL_PIXEL_FORMAT_RGB_888) {
27 ALOGE("ScreenshotClient::Take: Unsupported layout format: format=%d",
28 format_);
29 return -ENOSYS;
30 }
31
32 // TODO(eieio): Make a cleaner way to ensure enough capacity for send or
33 // receive buffers. This method assumes TLS buffers that will maintain
34 // capacity across calls within the same thread.
35 MessageBuffer<ReplyBuffer>::Reserve(kMaxScreenshotPixels * 3);
36 auto status = InvokeRemoteMethod<DisplayScreenshotRPC::TakeScreenshot>(index);
37 if (!status) {
38 ALOGE("ScreenshotClient::Take: Failed to take screenshot: %s",
39 status.GetErrorMessage().c_str());
40 return -status.error();
41 }
42
43 *return_width = status.get().width;
44 *return_height = status.get().height;
45 *out_image = std::move(status.take().buffer);
46 return 0;
47}
48
49ScreenshotClient::ScreenshotClient()
50 : BASE(pdx::default_transport::ClientChannelFactory::Create(
51 DisplayScreenshotRPC::kClientPath)) {
52 auto status = InvokeRemoteMethod<DisplayScreenshotRPC::GetFormat>();
53 if (!status) {
54 ALOGE(
55 "ScreenshotClient::ScreenshotClient: Failed to retrieve screenshot "
56 "layout: %s",
57 status.GetErrorMessage().c_str());
58
59 Close(status.error());
60 } else {
61 format_ = status.get();
62 }
63}
64
65} // namespace dvr
66} // namespace android