| 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; | 
| Hendrik Wagenaar | 108e84f | 2017-05-07 22:19:17 -0700 | [diff] [blame] | 141 | const int ret = producer_queue->AllocateBuffer(width, height, layer_count, | 
|  | 142 | format, usage, &slot); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 143 | if (ret < 0) { | 
|  | 144 | ALOGE( | 
|  | 145 | "Surface::CreateQueue: Failed to allocate buffer on queue_id=%d: %s", | 
|  | 146 | producer_queue->id(), strerror(-ret)); | 
|  | 147 | return ErrorStatus(ENOMEM); | 
|  | 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 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 170 | Status<std::unique_ptr<Surface>> DisplayClient::CreateSurface( | 
|  | 171 | const SurfaceAttributes& attributes) { | 
|  | 172 | int error; | 
|  | 173 | if (auto client = Surface::Create(attributes, &error)) | 
|  | 174 | return {std::move(client)}; | 
|  | 175 | else | 
|  | 176 | return ErrorStatus(error); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 179 | Status<std::unique_ptr<IonBuffer>> DisplayClient::GetNamedBuffer( | 
| Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 180 | const std::string& name) { | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 181 | auto status = InvokeRemoteMethod<DisplayProtocol::GetNamedBuffer>(name); | 
| Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 182 | if (!status) { | 
|  | 183 | ALOGE( | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 184 | "DisplayClient::GetNamedBuffer: Failed to get named buffer: name=%s; " | 
| Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 185 | "error=%s", | 
|  | 186 | name.c_str(), status.GetErrorMessage().c_str()); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 187 | return status.error_status(); | 
| Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| Hendrik Wagenaar | eaa5522 | 2017-04-06 10:56:23 -0700 | [diff] [blame] | 190 | auto ion_buffer = std::make_unique<IonBuffer>(); | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 191 | auto native_buffer_handle = status.take(); | 
|  | 192 | const int ret = native_buffer_handle.Import(ion_buffer.get()); | 
|  | 193 | if (ret < 0) { | 
|  | 194 | ALOGE( | 
|  | 195 | "DisplayClient::GetNamedBuffer: Failed to import named buffer: " | 
|  | 196 | "name=%s; error=%s", | 
|  | 197 | name.c_str(), strerror(-ret)); | 
|  | 198 | return ErrorStatus(-ret); | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | return {std::move(ion_buffer)}; | 
| Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 202 | } | 
|  | 203 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 204 | Status<bool> DisplayClient::IsVrAppRunning() { | 
|  | 205 | return InvokeRemoteMethod<DisplayProtocol::IsVrAppRunning>(); | 
| Albert Chaulk | b7c8a4b | 2017-03-20 13:03:39 -0400 | [diff] [blame] | 206 | } | 
| Hendrik Wagenaar | 10e68eb | 2017-03-15 13:29:02 -0700 | [diff] [blame] | 207 |  | 
| Corey Tabaka | 2251d82 | 2017-04-20 16:04:07 -0700 | [diff] [blame] | 208 | }  // namespace display | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 209 | }  // namespace dvr | 
|  | 210 | }  // namespace android |