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/late_latch.h> |
| 13 | #include <private/dvr/native_buffer.h> |
| 14 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 15 | using android::pdx::ErrorStatus; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 16 | using android::pdx::LocalHandle; |
| 17 | using android::pdx::LocalChannelHandle; |
| 18 | using android::pdx::Status; |
| 19 | using android::pdx::Transaction; |
| 20 | using android::pdx::rpc::IfAnyOf; |
| 21 | |
| 22 | namespace android { |
| 23 | namespace dvr { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 24 | namespace display { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 25 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 26 | Surface::Surface(LocalChannelHandle channel_handle, int* error) |
| 27 | : BASE{pdx::default_transport::ClientChannel::Create( |
| 28 | std::move(channel_handle))} { |
| 29 | auto status = InvokeRemoteMethod<DisplayProtocol::GetSurfaceInfo>(); |
| 30 | if (!status) { |
| 31 | ALOGE("Surface::Surface: Failed to get surface info: %s", |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 32 | status.GetErrorMessage().c_str()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 33 | Close(status.error()); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 34 | if (error) |
| 35 | *error = status.error(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 36 | } |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 37 | |
| 38 | surface_id_ = status.get().surface_id; |
| 39 | z_order_ = status.get().z_order; |
| 40 | visible_ = status.get().visible; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 43 | Surface::Surface(const SurfaceAttributes& attributes, int* error) |
| 44 | : BASE{pdx::default_transport::ClientChannelFactory::Create( |
| 45 | DisplayProtocol::kClientPath), |
| 46 | kInfiniteTimeout} { |
| 47 | auto status = InvokeRemoteMethod<DisplayProtocol::CreateSurface>(attributes); |
| 48 | if (!status) { |
| 49 | ALOGE("Surface::Surface: Failed to create display surface: %s", |
| 50 | status.GetErrorMessage().c_str()); |
| 51 | Close(status.error()); |
| 52 | if (error) |
| 53 | *error = status.error(); |
| 54 | } |
| 55 | |
| 56 | surface_id_ = status.get().surface_id; |
| 57 | z_order_ = status.get().z_order; |
| 58 | visible_ = status.get().visible; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 61 | Status<void> Surface::SetVisible(bool visible) { |
| 62 | return SetAttributes( |
| 63 | {{SurfaceAttribute::Visible, SurfaceAttributeValue{visible}}}); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 66 | Status<void> Surface::SetZOrder(int z_order) { |
| 67 | return SetAttributes( |
| 68 | {{SurfaceAttribute::ZOrder, SurfaceAttributeValue{z_order}}}); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 71 | Status<void> Surface::SetAttributes(const SurfaceAttributes& attributes) { |
| 72 | auto status = InvokeRemoteMethod<DisplayProtocol::SetAttributes>(attributes); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 73 | if (!status) { |
| 74 | ALOGE( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 75 | "Surface::SetAttributes: Failed to set display surface " |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 76 | "attributes: %s", |
| 77 | status.GetErrorMessage().c_str()); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 78 | return status.error_status(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | // Set the local cached copies of the attributes we care about from the full |
| 82 | // set of attributes sent to the display service. |
| 83 | for (const auto& attribute : attributes) { |
| 84 | const auto& key = attribute.first; |
| 85 | const auto* variant = &attribute.second; |
| 86 | bool invalid_value = false; |
| 87 | switch (key) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 88 | case SurfaceAttribute::Visible: |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 89 | invalid_value = |
| 90 | !IfAnyOf<int32_t, int64_t, bool>::Get(variant, &visible_); |
| 91 | break; |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 92 | case SurfaceAttribute::ZOrder: |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 93 | invalid_value = !IfAnyOf<int32_t>::Get(variant, &z_order_); |
| 94 | break; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | if (invalid_value) { |
| 98 | ALOGW( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 99 | "Surface::SetAttributes: Failed to set display surface " |
| 100 | "attribute %d because of incompatible type: %d", |
| 101 | key, variant->index()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 105 | return {}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 106 | } |
| 107 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 108 | Status<std::unique_ptr<ProducerQueue>> Surface::CreateQueue() { |
| 109 | ALOGD_IF(TRACE, "Surface::CreateQueue: Creating empty queue."); |
| 110 | auto status = InvokeRemoteMethod<DisplayProtocol::CreateQueue>(0); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 111 | if (!status) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 112 | ALOGE("Surface::CreateQueue: Failed to create queue: %s", |
| 113 | status.GetErrorMessage().c_str()); |
| 114 | return status.error_status(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 115 | } |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 116 | |
| 117 | auto producer_queue = ProducerQueue::Import(status.take()); |
| 118 | if (!producer_queue) { |
| 119 | ALOGE("Surface::CreateQueue: Failed to import producer queue!"); |
| 120 | return ErrorStatus(ENOMEM); |
| 121 | } |
| 122 | |
| 123 | return {std::move(producer_queue)}; |
| 124 | } |
| 125 | |
Hendrik Wagenaar | 108e84f | 2017-05-07 22:19:17 -0700 | [diff] [blame^] | 126 | Status<std::unique_ptr<ProducerQueue>> Surface::CreateQueue( |
| 127 | uint32_t width, uint32_t height, uint32_t layer_count, uint32_t format, |
| 128 | uint64_t usage, size_t capacity) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 129 | ALOGD_IF(TRACE, |
Hendrik Wagenaar | 108e84f | 2017-05-07 22:19:17 -0700 | [diff] [blame^] | 130 | "Surface::CreateQueue: width=%u height=%u layer_count=%u format=%u " |
| 131 | "usage=%" PRIx64 " capacity=%zu", |
| 132 | width, height, layer_count, format, usage, capacity); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 133 | auto status = CreateQueue(); |
| 134 | if (!status) |
| 135 | return status.error_status(); |
| 136 | |
| 137 | auto producer_queue = status.take(); |
| 138 | |
| 139 | ALOGD_IF(TRACE, "Surface::CreateQueue: Allocating %zu buffers...", capacity); |
| 140 | for (size_t i = 0; i < capacity; i++) { |
| 141 | size_t slot; |
Hendrik Wagenaar | 108e84f | 2017-05-07 22:19:17 -0700 | [diff] [blame^] | 142 | const int ret = producer_queue->AllocateBuffer(width, height, layer_count, |
| 143 | format, usage, &slot); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 144 | if (ret < 0) { |
| 145 | ALOGE( |
| 146 | "Surface::CreateQueue: Failed to allocate buffer on queue_id=%d: %s", |
| 147 | producer_queue->id(), strerror(-ret)); |
| 148 | return ErrorStatus(ENOMEM); |
| 149 | } |
| 150 | ALOGD_IF( |
| 151 | TRACE, |
| 152 | "Surface::CreateQueue: Allocated buffer at slot=%zu of capacity=%zu", |
| 153 | slot, capacity); |
| 154 | } |
| 155 | |
| 156 | return {std::move(producer_queue)}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | DisplayClient::DisplayClient(int* error) |
| 160 | : BASE(pdx::default_transport::ClientChannelFactory::Create( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 161 | DisplayProtocol::kClientPath), |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 162 | kInfiniteTimeout) { |
| 163 | if (error) |
| 164 | *error = Client::error(); |
| 165 | } |
| 166 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 167 | Status<Metrics> DisplayClient::GetDisplayMetrics() { |
| 168 | return InvokeRemoteMethod<DisplayProtocol::GetMetrics>(); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 171 | Status<std::unique_ptr<Surface>> DisplayClient::CreateSurface( |
| 172 | const SurfaceAttributes& attributes) { |
| 173 | int error; |
| 174 | if (auto client = Surface::Create(attributes, &error)) |
| 175 | return {std::move(client)}; |
| 176 | else |
| 177 | return ErrorStatus(error); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 178 | } |
| 179 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 180 | Status<std::unique_ptr<IonBuffer>> DisplayClient::GetNamedBuffer( |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 181 | const std::string& name) { |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 182 | auto status = InvokeRemoteMethod<DisplayProtocol::GetNamedBuffer>(name); |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 183 | if (!status) { |
| 184 | ALOGE( |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 185 | "DisplayClient::GetNamedBuffer: Failed to get named buffer: name=%s; " |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 186 | "error=%s", |
| 187 | name.c_str(), status.GetErrorMessage().c_str()); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 188 | return status.error_status(); |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 191 | auto ion_buffer = std::make_unique<IonBuffer>(); |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 192 | auto native_buffer_handle = status.take(); |
| 193 | const int ret = native_buffer_handle.Import(ion_buffer.get()); |
| 194 | if (ret < 0) { |
| 195 | ALOGE( |
| 196 | "DisplayClient::GetNamedBuffer: Failed to import named buffer: " |
| 197 | "name=%s; error=%s", |
| 198 | name.c_str(), strerror(-ret)); |
| 199 | return ErrorStatus(-ret); |
| 200 | } |
| 201 | |
| 202 | return {std::move(ion_buffer)}; |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 205 | Status<bool> DisplayClient::IsVrAppRunning() { |
| 206 | return InvokeRemoteMethod<DisplayProtocol::IsVrAppRunning>(); |
Albert Chaulk | b7c8a4b | 2017-03-20 13:03:39 -0400 | [diff] [blame] | 207 | } |
Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 208 | |
Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 209 | } // namespace display |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 210 | } // namespace dvr |
| 211 | } // namespace android |