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