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 | |
Jiwen 'Steve' Cai | 656f406 | 2017-05-22 13:02:33 -0700 | [diff] [blame] | 107 | Status<std::unique_ptr<ProducerQueue>> Surface::CreateQueue(uint32_t width, |
| 108 | uint32_t height, |
Jiwen 'Steve' Cai | 532e529 | 2017-06-02 15:36:44 -0700 | [diff] [blame^] | 109 | uint32_t format, |
| 110 | size_t metadata_size) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 111 | ALOGD_IF(TRACE, "Surface::CreateQueue: Creating empty queue."); |
Jiwen 'Steve' Cai | 656f406 | 2017-05-22 13:02:33 -0700 | [diff] [blame] | 112 | auto status = InvokeRemoteMethod<DisplayProtocol::CreateQueue>( |
| 113 | ProducerQueueConfigBuilder() |
| 114 | .SetDefaultWidth(width) |
| 115 | .SetDefaultHeight(height) |
| 116 | .SetDefaultFormat(format) |
Jiwen 'Steve' Cai | 532e529 | 2017-06-02 15:36:44 -0700 | [diff] [blame^] | 117 | .SetMetadataSize(metadata_size) |
Jiwen 'Steve' Cai | 656f406 | 2017-05-22 13:02:33 -0700 | [diff] [blame] | 118 | .Build()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 119 | if (!status) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 120 | ALOGE("Surface::CreateQueue: Failed to create queue: %s", |
| 121 | status.GetErrorMessage().c_str()); |
| 122 | return status.error_status(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 123 | } |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 124 | |
| 125 | auto producer_queue = ProducerQueue::Import(status.take()); |
| 126 | if (!producer_queue) { |
| 127 | ALOGE("Surface::CreateQueue: Failed to import producer queue!"); |
| 128 | return ErrorStatus(ENOMEM); |
| 129 | } |
| 130 | |
| 131 | return {std::move(producer_queue)}; |
| 132 | } |
| 133 | |
Hendrik Wagenaar | 108e84f | 2017-05-07 22:19:17 -0700 | [diff] [blame] | 134 | Status<std::unique_ptr<ProducerQueue>> Surface::CreateQueue( |
| 135 | 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^] | 136 | uint64_t usage, size_t capacity, size_t metadata_size) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 137 | ALOGD_IF(TRACE, |
Hendrik Wagenaar | 108e84f | 2017-05-07 22:19:17 -0700 | [diff] [blame] | 138 | "Surface::CreateQueue: width=%u height=%u layer_count=%u format=%u " |
| 139 | "usage=%" PRIx64 " capacity=%zu", |
| 140 | width, height, layer_count, format, usage, capacity); |
Jiwen 'Steve' Cai | 532e529 | 2017-06-02 15:36:44 -0700 | [diff] [blame^] | 141 | auto status = CreateQueue(width, height, format, metadata_size); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 142 | if (!status) |
| 143 | return status.error_status(); |
| 144 | |
| 145 | auto producer_queue = status.take(); |
| 146 | |
| 147 | ALOGD_IF(TRACE, "Surface::CreateQueue: Allocating %zu buffers...", capacity); |
| 148 | for (size_t i = 0; i < capacity; i++) { |
| 149 | size_t slot; |
Corey Tabaka | b7ca5de | 2017-05-08 18:55:02 -0700 | [diff] [blame] | 150 | auto allocate_status = producer_queue->AllocateBuffer( |
| 151 | width, height, layer_count, format, usage, &slot); |
| 152 | if (!allocate_status) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 153 | ALOGE( |
| 154 | "Surface::CreateQueue: Failed to allocate buffer on queue_id=%d: %s", |
Corey Tabaka | b7ca5de | 2017-05-08 18:55:02 -0700 | [diff] [blame] | 155 | producer_queue->id(), allocate_status.GetErrorMessage().c_str()); |
| 156 | return allocate_status.error_status(); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 157 | } |
| 158 | ALOGD_IF( |
| 159 | TRACE, |
| 160 | "Surface::CreateQueue: Allocated buffer at slot=%zu of capacity=%zu", |
| 161 | slot, capacity); |
| 162 | } |
| 163 | |
| 164 | return {std::move(producer_queue)}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | DisplayClient::DisplayClient(int* error) |
| 168 | : BASE(pdx::default_transport::ClientChannelFactory::Create( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 169 | DisplayProtocol::kClientPath), |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 170 | kInfiniteTimeout) { |
| 171 | if (error) |
| 172 | *error = Client::error(); |
| 173 | } |
| 174 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 175 | Status<Metrics> DisplayClient::GetDisplayMetrics() { |
| 176 | return InvokeRemoteMethod<DisplayProtocol::GetMetrics>(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Hendrik Wagenaar | bcb03d0 | 2017-05-23 14:59:08 -0700 | [diff] [blame] | 179 | Status<std::string> DisplayClient::GetConfigurationData( |
| 180 | ConfigFileType config_type) { |
| 181 | auto status = |
| 182 | InvokeRemoteMethod<DisplayProtocol::GetConfigurationData>(config_type); |
| 183 | if (!status && status.error() != ENOENT) { |
| 184 | ALOGE( |
| 185 | "DisplayClient::GetConfigurationData: Unable to get" |
| 186 | "configuration data. Error: %s", |
| 187 | status.GetErrorMessage().c_str()); |
| 188 | } |
| 189 | return status; |
| 190 | } |
| 191 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 192 | Status<std::unique_ptr<Surface>> DisplayClient::CreateSurface( |
| 193 | const SurfaceAttributes& attributes) { |
| 194 | int error; |
| 195 | if (auto client = Surface::Create(attributes, &error)) |
| 196 | return {std::move(client)}; |
| 197 | else |
| 198 | return ErrorStatus(error); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 201 | Status<std::unique_ptr<IonBuffer>> DisplayClient::GetGlobalBuffer( |
| 202 | DvrGlobalBufferKey key) { |
| 203 | auto status = InvokeRemoteMethod<DisplayProtocol::GetGlobalBuffer>(key); |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 204 | if (!status) { |
| 205 | ALOGE( |
Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 206 | "DisplayClient::GetGlobalBuffer: Failed to get named buffer: key=%d; " |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 207 | "error=%s", |
Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 208 | key, status.GetErrorMessage().c_str()); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 209 | return status.error_status(); |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 212 | auto ion_buffer = std::make_unique<IonBuffer>(); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 213 | auto native_buffer_handle = status.take(); |
| 214 | const int ret = native_buffer_handle.Import(ion_buffer.get()); |
| 215 | if (ret < 0) { |
| 216 | ALOGE( |
Okan Arikan | 36d2380 | 2017-05-15 15:20:39 -0700 | [diff] [blame] | 217 | "DisplayClient::GetGlobalBuffer: Failed to import global buffer: " |
| 218 | "key=%d; error=%s", |
| 219 | key, strerror(-ret)); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 220 | return ErrorStatus(-ret); |
| 221 | } |
| 222 | |
| 223 | return {std::move(ion_buffer)}; |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 226 | Status<bool> DisplayClient::IsVrAppRunning() { |
| 227 | return InvokeRemoteMethod<DisplayProtocol::IsVrAppRunning>(); |
Albert Chaulk | b7c8a4b | 2017-03-20 13:03:39 -0400 | [diff] [blame] | 228 | } |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 229 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 230 | } // namespace display |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 231 | } // namespace dvr |
| 232 | } // namespace android |