Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 1 | #include "include/private/dvr/display_client.h" |
| 2 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 3 | #include <cutils/native_handle.h> |
Alex Vakulenko | 4fe6058 | 2017-02-02 11:35:59 -0800 | [diff] [blame] | 4 | #include <log/log.h> |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 5 | #include <pdx/default_transport/client_channel.h> |
| 6 | #include <pdx/default_transport/client_channel_factory.h> |
| 7 | #include <pdx/status.h> |
| 8 | |
| 9 | #include <mutex> |
| 10 | |
| 11 | #include <private/dvr/display_rpc.h> |
| 12 | #include <private/dvr/late_latch.h> |
| 13 | #include <private/dvr/native_buffer.h> |
| 14 | |
| 15 | using android::pdx::LocalHandle; |
| 16 | using android::pdx::LocalChannelHandle; |
| 17 | using android::pdx::Status; |
| 18 | using android::pdx::Transaction; |
| 19 | using android::pdx::rpc::IfAnyOf; |
| 20 | |
| 21 | namespace android { |
| 22 | namespace dvr { |
| 23 | |
| 24 | SurfaceClient::SurfaceClient(LocalChannelHandle channel_handle, |
| 25 | SurfaceType type) |
| 26 | : Client{pdx::default_transport::ClientChannel::Create( |
| 27 | std::move(channel_handle))}, |
| 28 | type_(type) {} |
| 29 | |
| 30 | SurfaceClient::SurfaceClient(const std::string& endpoint_path, SurfaceType type) |
| 31 | : Client{pdx::default_transport::ClientChannelFactory::Create( |
| 32 | endpoint_path), |
| 33 | kInfiniteTimeout}, |
| 34 | type_(type) {} |
| 35 | |
| 36 | int SurfaceClient::GetMetadataBufferFd(LocalHandle* out_fd) { |
| 37 | auto buffer_producer = GetMetadataBuffer(); |
| 38 | if (!buffer_producer) |
| 39 | return -ENOMEM; |
| 40 | |
| 41 | *out_fd = buffer_producer->GetBlobFd(); |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | std::shared_ptr<BufferProducer> SurfaceClient::GetMetadataBuffer() { |
| 46 | if (!metadata_buffer_) { |
| 47 | auto status = InvokeRemoteMethod<DisplayRPC::GetMetadataBuffer>(); |
| 48 | if (!status) { |
| 49 | ALOGE( |
| 50 | "SurfaceClient::AllocateMetadataBuffer: Failed to allocate buffer: " |
| 51 | "%s", |
| 52 | status.GetErrorMessage().c_str()); |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
| 56 | metadata_buffer_ = BufferProducer::Import(status.take()); |
| 57 | } |
| 58 | |
| 59 | return metadata_buffer_; |
| 60 | } |
| 61 | |
| 62 | DisplaySurfaceClient::DisplaySurfaceClient(int width, int height, int format, |
| 63 | int usage, int flags) |
| 64 | : BASE(DisplayRPC::kClientPath, SurfaceTypeEnum::Normal), |
| 65 | width_(width), |
| 66 | height_(height), |
| 67 | format_(format), |
| 68 | usage_(usage), |
| 69 | flags_(flags), |
| 70 | z_order_(0), |
| 71 | visible_(true), |
| 72 | exclude_from_blur_(false), |
| 73 | blur_behind_(true), |
| 74 | mapped_metadata_buffer_(nullptr) { |
| 75 | auto status = InvokeRemoteMethod<DisplayRPC::CreateSurface>( |
| 76 | width, height, format, usage, flags); |
| 77 | if (!status) { |
| 78 | ALOGE( |
| 79 | "DisplaySurfaceClient::DisplaySurfaceClient: Failed to create display " |
| 80 | "surface: %s", |
| 81 | status.GetErrorMessage().c_str()); |
| 82 | Close(status.error()); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void DisplaySurfaceClient::SetVisible(bool visible) { |
| 87 | SetAttributes({{DisplaySurfaceAttributeEnum::Visible, |
| 88 | DisplaySurfaceAttributeValue{visible}}}); |
| 89 | } |
| 90 | |
| 91 | void DisplaySurfaceClient::SetZOrder(int z_order) { |
| 92 | SetAttributes({{DisplaySurfaceAttributeEnum::ZOrder, |
| 93 | DisplaySurfaceAttributeValue{z_order}}}); |
| 94 | } |
| 95 | |
| 96 | void DisplaySurfaceClient::SetExcludeFromBlur(bool exclude_from_blur) { |
| 97 | SetAttributes({{DisplaySurfaceAttributeEnum::ExcludeFromBlur, |
| 98 | DisplaySurfaceAttributeValue{exclude_from_blur}}}); |
| 99 | } |
| 100 | |
| 101 | void DisplaySurfaceClient::SetBlurBehind(bool blur_behind) { |
| 102 | SetAttributes({{DisplaySurfaceAttributeEnum::BlurBehind, |
| 103 | DisplaySurfaceAttributeValue{blur_behind}}}); |
| 104 | } |
| 105 | |
| 106 | void DisplaySurfaceClient::SetAttributes( |
| 107 | const DisplaySurfaceAttributes& attributes) { |
| 108 | Status<int> status = |
| 109 | InvokeRemoteMethod<DisplayRPC::SetAttributes>(attributes); |
| 110 | if (!status) { |
| 111 | ALOGE( |
| 112 | "DisplaySurfaceClient::SetAttributes: Failed to set display surface " |
| 113 | "attributes: %s", |
| 114 | status.GetErrorMessage().c_str()); |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | // Set the local cached copies of the attributes we care about from the full |
| 119 | // set of attributes sent to the display service. |
| 120 | for (const auto& attribute : attributes) { |
| 121 | const auto& key = attribute.first; |
| 122 | const auto* variant = &attribute.second; |
| 123 | bool invalid_value = false; |
| 124 | switch (key) { |
| 125 | case DisplaySurfaceAttributeEnum::Visible: |
| 126 | invalid_value = |
| 127 | !IfAnyOf<int32_t, int64_t, bool>::Get(variant, &visible_); |
| 128 | break; |
| 129 | case DisplaySurfaceAttributeEnum::ZOrder: |
| 130 | invalid_value = !IfAnyOf<int32_t>::Get(variant, &z_order_); |
| 131 | break; |
| 132 | case DisplaySurfaceAttributeEnum::ExcludeFromBlur: |
| 133 | invalid_value = |
| 134 | !IfAnyOf<int32_t, int64_t, bool>::Get(variant, &exclude_from_blur_); |
| 135 | break; |
| 136 | case DisplaySurfaceAttributeEnum::BlurBehind: |
| 137 | invalid_value = |
| 138 | !IfAnyOf<int32_t, int64_t, bool>::Get(variant, &blur_behind_); |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | if (invalid_value) { |
| 143 | ALOGW( |
| 144 | "DisplaySurfaceClient::SetAttributes: Failed to set display " |
| 145 | "surface attribute '%s' because of incompatible type: %d", |
| 146 | DisplaySurfaceAttributeEnum::ToString(key).c_str(), variant->index()); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 151 | std::shared_ptr<ProducerQueue> DisplaySurfaceClient::GetProducerQueue() { |
| 152 | if (producer_queue_ == nullptr) { |
| 153 | // Create producer queue through DisplayRPC |
| 154 | auto status = InvokeRemoteMethod<DisplayRPC::CreateBufferQueue>(); |
| 155 | if (!status) { |
| 156 | ALOGE( |
| 157 | "DisplaySurfaceClient::GetProducerQueue: failed to create producer " |
| 158 | "queue: %s", |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 159 | status.GetErrorMessage().c_str()); |
Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 160 | return nullptr; |
| 161 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 162 | |
Corey Tabaka | 1db8a5d | 2017-03-22 02:12:52 -0700 | [diff] [blame] | 163 | producer_queue_ = ProducerQueue::Import(status.take()); |
Jiwen 'Steve' Cai | a361361 | 2017-03-08 17:41:48 -0800 | [diff] [blame] | 164 | } |
| 165 | return producer_queue_; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | volatile DisplaySurfaceMetadata* DisplaySurfaceClient::GetMetadataBufferPtr() { |
| 169 | if (!mapped_metadata_buffer_) { |
| 170 | if (auto buffer_producer = GetMetadataBuffer()) { |
| 171 | void* addr = nullptr; |
| 172 | const int ret = buffer_producer->GetBlobReadWritePointer( |
| 173 | sizeof(DisplaySurfaceMetadata), &addr); |
| 174 | if (ret < 0) { |
| 175 | ALOGE( |
| 176 | "DisplaySurfaceClient::GetMetadataBufferPtr: Failed to map surface " |
| 177 | "metadata: %s", |
| 178 | strerror(-ret)); |
| 179 | return nullptr; |
| 180 | } |
| 181 | mapped_metadata_buffer_ = static_cast<DisplaySurfaceMetadata*>(addr); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | return mapped_metadata_buffer_; |
| 186 | } |
| 187 | |
| 188 | LocalChannelHandle DisplaySurfaceClient::CreateVideoMeshSurface() { |
| 189 | auto status = InvokeRemoteMethod<DisplayRPC::CreateVideoMeshSurface>(); |
| 190 | if (!status) { |
| 191 | ALOGE( |
| 192 | "DisplaySurfaceClient::CreateVideoMeshSurface: Failed to create " |
| 193 | "video mesh surface: %s", |
| 194 | status.GetErrorMessage().c_str()); |
| 195 | } |
| 196 | return status.take(); |
| 197 | } |
| 198 | |
| 199 | DisplayClient::DisplayClient(int* error) |
| 200 | : BASE(pdx::default_transport::ClientChannelFactory::Create( |
| 201 | DisplayRPC::kClientPath), |
| 202 | kInfiniteTimeout) { |
| 203 | if (error) |
| 204 | *error = Client::error(); |
| 205 | } |
| 206 | |
| 207 | int DisplayClient::GetDisplayMetrics(SystemDisplayMetrics* metrics) { |
| 208 | auto status = InvokeRemoteMethod<DisplayRPC::GetMetrics>(); |
| 209 | if (!status) { |
| 210 | ALOGE("DisplayClient::GetDisplayMetrics: Failed to get metrics: %s", |
| 211 | status.GetErrorMessage().c_str()); |
| 212 | return -status.error(); |
| 213 | } |
| 214 | |
| 215 | *metrics = status.get(); |
| 216 | return 0; |
| 217 | } |
| 218 | |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame^] | 219 | pdx::Status<void> DisplayClient::SetViewerParams( |
| 220 | const ViewerParams& viewer_params) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 221 | auto status = InvokeRemoteMethod<DisplayRPC::SetViewerParams>(viewer_params); |
| 222 | if (!status) { |
| 223 | ALOGE("DisplayClient::SetViewerParams: Failed to set viewer params: %s", |
| 224 | status.GetErrorMessage().c_str()); |
| 225 | } |
| 226 | return status; |
| 227 | } |
| 228 | |
| 229 | int DisplayClient::GetLastFrameEdsTransform(LateLatchOutput* ll_out) { |
| 230 | auto status = InvokeRemoteMethod<DisplayRPC::GetEdsCapture>(); |
| 231 | if (!status) { |
| 232 | ALOGE( |
| 233 | "DisplayClient::GetLastFrameLateLatch: Failed to get most recent late" |
| 234 | " latch: %s", |
| 235 | status.GetErrorMessage().c_str()); |
| 236 | return -status.error(); |
| 237 | } |
| 238 | |
| 239 | if (status.get().size() != sizeof(LateLatchOutput)) { |
| 240 | ALOGE( |
| 241 | "DisplayClient::GetLastFrameLateLatch: Error expected to receive %zu " |
| 242 | "bytes but received %zu", |
| 243 | sizeof(LateLatchOutput), status.get().size()); |
| 244 | return -EIO; |
| 245 | } |
| 246 | |
| 247 | *ll_out = *reinterpret_cast<const LateLatchOutput*>(status.get().data()); |
| 248 | return 0; |
| 249 | } |
| 250 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 251 | std::unique_ptr<DisplaySurfaceClient> DisplayClient::CreateDisplaySurface( |
| 252 | int width, int height, int format, int usage, int flags) { |
| 253 | return DisplaySurfaceClient::Create(width, height, format, usage, flags); |
| 254 | } |
| 255 | |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame^] | 256 | std::unique_ptr<IonBuffer> DisplayClient::GetNamedBuffer( |
| 257 | const std::string& name) { |
| 258 | auto status = InvokeRemoteMethod<DisplayRPC::GetNamedBuffer>(name); |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 259 | if (!status) { |
| 260 | ALOGE( |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame^] | 261 | "DisplayClient::GetNamedBuffer: Failed to get pose buffer. name=%s, " |
| 262 | "error=%s", |
| 263 | name.c_str(), status.GetErrorMessage().c_str()); |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 264 | return nullptr; |
| 265 | } |
| 266 | |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame^] | 267 | auto ion_buffer = std::make_unique<IonBuffer>(); |
| 268 | status.take().Import(ion_buffer.get()); |
| 269 | return ion_buffer; |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Albert Chaulk | b7c8a4b | 2017-03-20 13:03:39 -0400 | [diff] [blame] | 272 | bool DisplayClient::IsVrAppRunning() { |
| 273 | auto status = InvokeRemoteMethod<DisplayRPC::IsVrAppRunning>(); |
| 274 | if (!status) |
| 275 | return 0; |
| 276 | return static_cast<bool>(status.get()); |
| 277 | } |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 278 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 279 | } // namespace dvr |
| 280 | } // namespace android |