| 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 |  | 
| Corey Tabaka | 3f82d31 | 2017-04-20 14:42:08 -0700 | [diff] [blame] | 11 | #include <private/dvr/display_protocol.h> | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 12 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 13 | using android::pdx::ErrorStatus; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 14 | using android::pdx::LocalHandle; | 
|  | 15 | using android::pdx::LocalChannelHandle; | 
|  | 16 | using android::pdx::Status; | 
|  | 17 | using android::pdx::Transaction; | 
|  | 18 | using android::pdx::rpc::IfAnyOf; | 
|  | 19 |  | 
|  | 20 | namespace android { | 
|  | 21 | namespace dvr { | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 22 | namespace display { | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 23 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 24 | Surface::Surface(LocalChannelHandle channel_handle, int* error) | 
|  | 25 | : BASE{pdx::default_transport::ClientChannel::Create( | 
|  | 26 | std::move(channel_handle))} { | 
|  | 27 | auto status = InvokeRemoteMethod<DisplayProtocol::GetSurfaceInfo>(); | 
|  | 28 | if (!status) { | 
|  | 29 | ALOGE("Surface::Surface: Failed to get surface info: %s", | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 30 | status.GetErrorMessage().c_str()); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 31 | Close(status.error()); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 32 | if (error) | 
|  | 33 | *error = status.error(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 34 | } | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 35 |  | 
|  | 36 | surface_id_ = status.get().surface_id; | 
|  | 37 | z_order_ = status.get().z_order; | 
|  | 38 | visible_ = status.get().visible; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 39 | } | 
|  | 40 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 41 | Surface::Surface(const SurfaceAttributes& attributes, int* error) | 
|  | 42 | : BASE{pdx::default_transport::ClientChannelFactory::Create( | 
|  | 43 | DisplayProtocol::kClientPath), | 
|  | 44 | kInfiniteTimeout} { | 
|  | 45 | auto status = InvokeRemoteMethod<DisplayProtocol::CreateSurface>(attributes); | 
|  | 46 | if (!status) { | 
|  | 47 | ALOGE("Surface::Surface: Failed to create display surface: %s", | 
|  | 48 | status.GetErrorMessage().c_str()); | 
|  | 49 | Close(status.error()); | 
|  | 50 | if (error) | 
|  | 51 | *error = status.error(); | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | surface_id_ = status.get().surface_id; | 
|  | 55 | z_order_ = status.get().z_order; | 
|  | 56 | visible_ = status.get().visible; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 57 | } | 
|  | 58 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 59 | Status<void> Surface::SetVisible(bool visible) { | 
|  | 60 | return SetAttributes( | 
|  | 61 | {{SurfaceAttribute::Visible, SurfaceAttributeValue{visible}}}); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 62 | } | 
|  | 63 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 64 | Status<void> Surface::SetZOrder(int z_order) { | 
|  | 65 | return SetAttributes( | 
|  | 66 | {{SurfaceAttribute::ZOrder, SurfaceAttributeValue{z_order}}}); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 67 | } | 
|  | 68 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 69 | Status<void> Surface::SetAttributes(const SurfaceAttributes& attributes) { | 
|  | 70 | auto status = InvokeRemoteMethod<DisplayProtocol::SetAttributes>(attributes); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 71 | if (!status) { | 
|  | 72 | ALOGE( | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 73 | "Surface::SetAttributes: Failed to set display surface " | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 74 | "attributes: %s", | 
|  | 75 | status.GetErrorMessage().c_str()); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 76 | return status.error_status(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | // Set the local cached copies of the attributes we care about from the full | 
|  | 80 | // set of attributes sent to the display service. | 
|  | 81 | for (const auto& attribute : attributes) { | 
|  | 82 | const auto& key = attribute.first; | 
|  | 83 | const auto* variant = &attribute.second; | 
|  | 84 | bool invalid_value = false; | 
|  | 85 | switch (key) { | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 86 | case SurfaceAttribute::Visible: | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 87 | invalid_value = | 
|  | 88 | !IfAnyOf<int32_t, int64_t, bool>::Get(variant, &visible_); | 
|  | 89 | break; | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 90 | case SurfaceAttribute::ZOrder: | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 91 | invalid_value = !IfAnyOf<int32_t>::Get(variant, &z_order_); | 
|  | 92 | break; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 93 | } | 
|  | 94 |  | 
|  | 95 | if (invalid_value) { | 
|  | 96 | ALOGW( | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 97 | "Surface::SetAttributes: Failed to set display surface " | 
|  | 98 | "attribute %d because of incompatible type: %d", | 
|  | 99 | key, variant->index()); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 100 | } | 
|  | 101 | } | 
|  | 102 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 103 | return {}; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 104 | } | 
|  | 105 |  | 
| Corey Tabaka | 99c2d73 | 2017-06-07 17:54:33 -0700 | [diff] [blame] | 106 | Status<std::unique_ptr<ProducerQueue>> Surface::CreateQueue( | 
|  | 107 | uint32_t width, uint32_t height, uint32_t format, size_t metadata_size) { | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 108 | ALOGD_IF(TRACE, "Surface::CreateQueue: Creating empty queue."); | 
| Jiwen 'Steve' Cai | 656f406 | 2017-05-22 13:02:33 -0700 | [diff] [blame] | 109 | auto status = InvokeRemoteMethod<DisplayProtocol::CreateQueue>( | 
|  | 110 | ProducerQueueConfigBuilder() | 
|  | 111 | .SetDefaultWidth(width) | 
|  | 112 | .SetDefaultHeight(height) | 
|  | 113 | .SetDefaultFormat(format) | 
| Jiwen 'Steve' Cai | 532e529 | 2017-06-02 15:36:44 -0700 | [diff] [blame] | 114 | .SetMetadataSize(metadata_size) | 
| Jiwen 'Steve' Cai | 656f406 | 2017-05-22 13:02:33 -0700 | [diff] [blame] | 115 | .Build()); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 116 | if (!status) { | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 117 | ALOGE("Surface::CreateQueue: Failed to create queue: %s", | 
|  | 118 | status.GetErrorMessage().c_str()); | 
|  | 119 | return status.error_status(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 120 | } | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 121 |  | 
|  | 122 | auto producer_queue = ProducerQueue::Import(status.take()); | 
|  | 123 | if (!producer_queue) { | 
|  | 124 | ALOGE("Surface::CreateQueue: Failed to import producer queue!"); | 
|  | 125 | return ErrorStatus(ENOMEM); | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | return {std::move(producer_queue)}; | 
|  | 129 | } | 
|  | 130 |  | 
| Hendrik Wagenaar | 108e84f | 2017-05-07 22:19:17 -0700 | [diff] [blame] | 131 | Status<std::unique_ptr<ProducerQueue>> Surface::CreateQueue( | 
|  | 132 | uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format, | 
| Jiwen 'Steve' Cai | 532e529 | 2017-06-02 15:36:44 -0700 | [diff] [blame] | 133 | uint64_t usage, size_t capacity, size_t metadata_size) { | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 134 | ALOGD_IF(TRACE, | 
| Hendrik Wagenaar | 108e84f | 2017-05-07 22:19:17 -0700 | [diff] [blame] | 135 | "Surface::CreateQueue: width=%u height=%u layer_count=%u format=%u " | 
|  | 136 | "usage=%" PRIx64 " capacity=%zu", | 
|  | 137 | width, height, layer_count, format, usage, capacity); | 
| Jiwen 'Steve' Cai | 532e529 | 2017-06-02 15:36:44 -0700 | [diff] [blame] | 138 | auto status = CreateQueue(width, height, format, metadata_size); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 139 | if (!status) | 
|  | 140 | return status.error_status(); | 
|  | 141 |  | 
|  | 142 | auto producer_queue = status.take(); | 
|  | 143 |  | 
|  | 144 | ALOGD_IF(TRACE, "Surface::CreateQueue: Allocating %zu buffers...", capacity); | 
| Jiwen 'Steve' Cai | 8fa4e10 | 2017-05-24 23:16:54 -0700 | [diff] [blame] | 145 | auto allocate_status = producer_queue->AllocateBuffers( | 
|  | 146 | width, height, layer_count, format, usage, capacity); | 
|  | 147 | if (!allocate_status) { | 
|  | 148 | ALOGE("Surface::CreateQueue: Failed to allocate buffer on queue_id=%d: %s", | 
| Corey Tabaka | b7ca5de | 2017-05-08 18:55:02 -0700 | [diff] [blame] | 149 | producer_queue->id(), allocate_status.GetErrorMessage().c_str()); | 
| Jiwen 'Steve' Cai | 8fa4e10 | 2017-05-24 23:16:54 -0700 | [diff] [blame] | 150 | return allocate_status.error_status(); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 151 | } | 
|  | 152 |  | 
|  | 153 | return {std::move(producer_queue)}; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 154 | } | 
|  | 155 |  | 
|  | 156 | DisplayClient::DisplayClient(int* error) | 
|  | 157 | : BASE(pdx::default_transport::ClientChannelFactory::Create( | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 158 | DisplayProtocol::kClientPath), | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 159 | kInfiniteTimeout) { | 
|  | 160 | if (error) | 
|  | 161 | *error = Client::error(); | 
|  | 162 | } | 
|  | 163 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 164 | Status<Metrics> DisplayClient::GetDisplayMetrics() { | 
|  | 165 | return InvokeRemoteMethod<DisplayProtocol::GetMetrics>(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
| Hendrik Wagenaar | bcb03d0 | 2017-05-23 14:59:08 -0700 | [diff] [blame] | 168 | Status<std::string> DisplayClient::GetConfigurationData( | 
|  | 169 | ConfigFileType config_type) { | 
|  | 170 | auto status = | 
|  | 171 | InvokeRemoteMethod<DisplayProtocol::GetConfigurationData>(config_type); | 
|  | 172 | if (!status && status.error() != ENOENT) { | 
|  | 173 | ALOGE( | 
|  | 174 | "DisplayClient::GetConfigurationData: Unable to get" | 
|  | 175 | "configuration data. Error: %s", | 
|  | 176 | status.GetErrorMessage().c_str()); | 
|  | 177 | } | 
|  | 178 | return status; | 
|  | 179 | } | 
|  | 180 |  | 
| mamik | 94e91f6 | 2019-08-19 09:11:33 -0700 | [diff] [blame] | 181 | Status<uint8_t> DisplayClient::GetDisplayIdentificationPort() { | 
|  | 182 | return InvokeRemoteMethod<DisplayProtocol::GetDisplayIdentificationPort>(); | 
|  | 183 | } | 
|  | 184 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 185 | Status<std::unique_ptr<Surface>> DisplayClient::CreateSurface( | 
|  | 186 | const SurfaceAttributes& attributes) { | 
|  | 187 | int error; | 
|  | 188 | if (auto client = Surface::Create(attributes, &error)) | 
|  | 189 | return {std::move(client)}; | 
|  | 190 | else | 
|  | 191 | return ErrorStatus(error); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 192 | } | 
|  | 193 |  | 
| Corey Tabaka | 99c2d73 | 2017-06-07 17:54:33 -0700 | [diff] [blame] | 194 | pdx::Status<std::unique_ptr<IonBuffer>> DisplayClient::SetupGlobalBuffer( | 
|  | 195 | DvrGlobalBufferKey key, size_t size, uint64_t usage) { | 
|  | 196 | auto status = | 
|  | 197 | InvokeRemoteMethod<DisplayProtocol::SetupGlobalBuffer>(key, size, usage); | 
|  | 198 | if (!status) { | 
|  | 199 | ALOGE( | 
|  | 200 | "DisplayClient::SetupGlobalBuffer: Failed to create the global buffer " | 
|  | 201 | "%s", | 
|  | 202 | status.GetErrorMessage().c_str()); | 
|  | 203 | return status.error_status(); | 
|  | 204 | } | 
|  | 205 |  | 
|  | 206 | auto ion_buffer = std::make_unique<IonBuffer>(); | 
|  | 207 | auto native_buffer_handle = status.take(); | 
|  | 208 | const int ret = native_buffer_handle.Import(ion_buffer.get()); | 
|  | 209 | if (ret < 0) { | 
|  | 210 | ALOGE( | 
|  | 211 | "DisplayClient::GetGlobalBuffer: Failed to import global buffer: " | 
|  | 212 | "key=%d; error=%s", | 
|  | 213 | key, strerror(-ret)); | 
|  | 214 | return ErrorStatus(-ret); | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | return {std::move(ion_buffer)}; | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | pdx::Status<void> DisplayClient::DeleteGlobalBuffer(DvrGlobalBufferKey key) { | 
|  | 221 | auto status = InvokeRemoteMethod<DisplayProtocol::DeleteGlobalBuffer>(key); | 
|  | 222 | if (!status) { | 
|  | 223 | ALOGE("DisplayClient::DeleteGlobalBuffer Failed: %s", | 
|  | 224 | status.GetErrorMessage().c_str()); | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | return status; | 
|  | 228 | } | 
|  | 229 |  | 
| Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 230 | Status<std::unique_ptr<IonBuffer>> DisplayClient::GetGlobalBuffer( | 
|  | 231 | DvrGlobalBufferKey key) { | 
|  | 232 | auto status = InvokeRemoteMethod<DisplayProtocol::GetGlobalBuffer>(key); | 
| Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 233 | if (!status) { | 
|  | 234 | ALOGE( | 
| Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 235 | "DisplayClient::GetGlobalBuffer: Failed to get named buffer: key=%d; " | 
| Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 236 | "error=%s", | 
| Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 237 | key, status.GetErrorMessage().c_str()); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 238 | return status.error_status(); | 
| Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 239 | } | 
|  | 240 |  | 
| Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 241 | auto ion_buffer = std::make_unique<IonBuffer>(); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 242 | auto native_buffer_handle = status.take(); | 
|  | 243 | const int ret = native_buffer_handle.Import(ion_buffer.get()); | 
|  | 244 | if (ret < 0) { | 
|  | 245 | ALOGE( | 
| Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 246 | "DisplayClient::GetGlobalBuffer: Failed to import global buffer: " | 
|  | 247 | "key=%d; error=%s", | 
|  | 248 | key, strerror(-ret)); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 249 | return ErrorStatus(-ret); | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | return {std::move(ion_buffer)}; | 
| Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 253 | } | 
|  | 254 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 255 | Status<bool> DisplayClient::IsVrAppRunning() { | 
|  | 256 | return InvokeRemoteMethod<DisplayProtocol::IsVrAppRunning>(); | 
| Albert Chaulk | b7c8a4b | 2017-03-20 13:03:39 -0400 | [diff] [blame] | 257 | } | 
| Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 258 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 259 | }  // namespace display | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 260 | }  // namespace dvr | 
|  | 261 | }  // namespace android |