Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame^] | 1 | #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 | |
| 10 | using android::pdx::Transaction; |
| 11 | using android::pdx::rpc::ClientPayload; |
| 12 | using android::pdx::rpc::MessageBuffer; |
| 13 | using android::pdx::rpc::ReplyBuffer; |
| 14 | |
| 15 | namespace android { |
| 16 | namespace dvr { |
| 17 | |
| 18 | namespace { |
| 19 | // Maximum supported pixels for screenshot capture. If the actual target buffer |
| 20 | // is more than this, an error will be reported. |
| 21 | constexpr int kMaxScreenshotPixels = 6000 * 4000; |
| 22 | } // namespace |
| 23 | |
| 24 | int 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 | |
| 49 | ScreenshotClient::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 |