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