| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 1 | #include "buffer_hub.h" | 
|  | 2 |  | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 3 | #include <inttypes.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 <poll.h> | 
|  | 6 | #include <utils/Trace.h> | 
|  | 7 |  | 
|  | 8 | #include <iomanip> | 
|  | 9 | #include <sstream> | 
|  | 10 | #include <string> | 
|  | 11 | #include <thread> | 
|  | 12 |  | 
|  | 13 | #include <pdx/default_transport/service_endpoint.h> | 
|  | 14 | #include <private/dvr/bufferhub_rpc.h> | 
|  | 15 | #include "consumer_channel.h" | 
|  | 16 | #include "producer_channel.h" | 
|  | 17 | #include "producer_queue_channel.h" | 
|  | 18 |  | 
|  | 19 | using android::pdx::Channel; | 
| Corey Tabaka | 1db8a5d | 2017-03-22 02:12:52 -0700 | [diff] [blame] | 20 | using android::pdx::ErrorStatus; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 21 | using android::pdx::Message; | 
| Corey Tabaka | 1db8a5d | 2017-03-22 02:12:52 -0700 | [diff] [blame] | 22 | using android::pdx::Status; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 23 | using android::pdx::rpc::DispatchRemoteMethod; | 
|  | 24 | using android::pdx::default_transport::Endpoint; | 
|  | 25 |  | 
|  | 26 | namespace android { | 
|  | 27 | namespace dvr { | 
|  | 28 |  | 
|  | 29 | BufferHubService::BufferHubService() | 
|  | 30 | : BASE("BufferHub", Endpoint::Create(BufferHubRPC::kClientPath)) {} | 
|  | 31 |  | 
|  | 32 | BufferHubService::~BufferHubService() {} | 
|  | 33 |  | 
| Corey Tabaka | 1db8a5d | 2017-03-22 02:12:52 -0700 | [diff] [blame] | 34 | bool BufferHubService::IsInitialized() const { return BASE::IsInitialized(); } | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 35 |  | 
|  | 36 | std::string BufferHubService::DumpState(size_t /*max_length*/) { | 
|  | 37 | std::ostringstream stream; | 
|  | 38 | auto channels = GetChannels<BufferHubChannel>(); | 
|  | 39 |  | 
|  | 40 | std::sort(channels.begin(), channels.end(), | 
|  | 41 | [](const std::shared_ptr<BufferHubChannel>& a, | 
|  | 42 | const std::shared_ptr<BufferHubChannel>& b) { | 
|  | 43 | return a->buffer_id() < b->buffer_id(); | 
|  | 44 | }); | 
|  | 45 |  | 
|  | 46 | stream << "Active Producer Buffers:\n"; | 
|  | 47 | stream << std::right; | 
|  | 48 | stream << std::setw(6) << "Id"; | 
|  | 49 | stream << " "; | 
|  | 50 | stream << std::setw(9) << "Consumers"; | 
|  | 51 | stream << " "; | 
|  | 52 | stream << std::setw(14) << "Geometry"; | 
|  | 53 | stream << " "; | 
|  | 54 | stream << std::setw(6) << "Format"; | 
|  | 55 | stream << " "; | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 56 | stream << std::setw(21) << "Usage"; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 57 | stream << " "; | 
|  | 58 | stream << "Name"; | 
|  | 59 | stream << std::endl; | 
|  | 60 |  | 
|  | 61 | for (const auto& channel : channels) { | 
|  | 62 | if (channel->channel_type() == BufferHubChannel::kProducerType) { | 
|  | 63 | BufferHubChannel::BufferInfo info = channel->GetBufferInfo(); | 
|  | 64 |  | 
|  | 65 | stream << std::right; | 
|  | 66 | stream << std::setw(6) << info.id; | 
|  | 67 | stream << " "; | 
|  | 68 | stream << std::setw(9) << info.consumer_count; | 
|  | 69 | stream << " "; | 
|  | 70 | if (info.format == HAL_PIXEL_FORMAT_BLOB) { | 
|  | 71 | std::string size = std::to_string(info.width) + " B"; | 
|  | 72 | stream << std::setw(14) << size; | 
|  | 73 | } else { | 
|  | 74 | std::string dimensions = std::to_string(info.width) + "x" + | 
|  | 75 | std::to_string(info.height) + "x" + | 
|  | 76 | std::to_string(info.slice_count); | 
|  | 77 | stream << std::setw(14) << dimensions; | 
|  | 78 | } | 
|  | 79 | stream << " "; | 
|  | 80 | stream << std::setw(6) << info.format; | 
|  | 81 | stream << " "; | 
|  | 82 | stream << "0x" << std::hex << std::setfill('0'); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 83 | stream << std::setw(8) << info.producer_usage; | 
|  | 84 | stream << "0x"; | 
|  | 85 | stream << std::setw(8) << info.consumer_usage; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 86 | stream << std::dec << std::setfill(' '); | 
|  | 87 | stream << " "; | 
|  | 88 | stream << info.name; | 
|  | 89 | stream << std::endl; | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | stream << "Active Consumer Buffers:\n"; | 
|  | 94 | stream << std::right; | 
|  | 95 | stream << std::setw(6) << "Id"; | 
|  | 96 | stream << " "; | 
|  | 97 | stream << std::setw(14) << "Geometry"; | 
|  | 98 | stream << " "; | 
|  | 99 | stream << "Name"; | 
|  | 100 | stream << std::endl; | 
|  | 101 |  | 
|  | 102 | for (const auto& channel : channels) { | 
|  | 103 | if (channel->channel_type() == BufferHubChannel::kConsumerType) { | 
|  | 104 | BufferHubChannel::BufferInfo info = channel->GetBufferInfo(); | 
|  | 105 |  | 
|  | 106 | stream << std::right; | 
|  | 107 | stream << std::setw(6) << info.id; | 
|  | 108 | stream << " "; | 
|  | 109 |  | 
|  | 110 | if (info.consumer_count == 0) { | 
|  | 111 | // consumer_count is tracked by producer. When it's zero, producer must | 
|  | 112 | // have already hung up and the consumer is orphaned. | 
|  | 113 | stream << std::setw(14) << "Orphaned."; | 
| Corey Tabaka | 3079cb7 | 2017-01-19 15:07:26 -0800 | [diff] [blame] | 114 | stream << (" channel_id=" + std::to_string(channel->channel_id())); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 115 | stream << std::endl; | 
|  | 116 | continue; | 
|  | 117 | } | 
|  | 118 |  | 
|  | 119 | if (info.format == HAL_PIXEL_FORMAT_BLOB) { | 
|  | 120 | std::string size = std::to_string(info.width) + " B"; | 
|  | 121 | stream << std::setw(14) << size; | 
|  | 122 | } else { | 
|  | 123 | std::string dimensions = std::to_string(info.width) + "x" + | 
|  | 124 | std::to_string(info.height) + "x" + | 
|  | 125 | std::to_string(info.slice_count); | 
|  | 126 | stream << std::setw(14) << dimensions; | 
|  | 127 | } | 
|  | 128 | stream << " "; | 
|  | 129 | stream << info.name; | 
|  | 130 | stream << std::endl; | 
|  | 131 | } | 
|  | 132 | } | 
|  | 133 |  | 
|  | 134 | stream << std::endl; | 
|  | 135 | stream << "Active Producer Queues:\n"; | 
|  | 136 | stream << std::right << std::setw(6) << "Id"; | 
|  | 137 | stream << std::right << std::setw(12) << " Allocated"; | 
|  | 138 | stream << std::right << std::setw(12) << " Consumers"; | 
|  | 139 | stream << " UsageSetMask"; | 
|  | 140 | stream << " UsageClearMask"; | 
|  | 141 | stream << " UsageDenySetMask"; | 
|  | 142 | stream << " UsageDenyClearMask"; | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 143 | stream << " UsageSetMask"; | 
|  | 144 | stream << " UsageClearMask"; | 
|  | 145 | stream << " UsageDenySetMask"; | 
|  | 146 | stream << " UsageDenyClearMask"; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 147 | stream << " "; | 
|  | 148 | stream << std::endl; | 
|  | 149 |  | 
|  | 150 | for (const auto& channel : channels) { | 
|  | 151 | if (channel->channel_type() == BufferHubChannel::kProducerQueueType) { | 
|  | 152 | BufferHubChannel::BufferInfo info = channel->GetBufferInfo(); | 
|  | 153 |  | 
|  | 154 | stream << std::dec << std::setfill(' '); | 
|  | 155 | stream << std::right << std::setw(6) << info.id; | 
|  | 156 | stream << std::right << std::setw(12) << info.capacity; | 
|  | 157 | stream << std::right << std::setw(12) << info.consumer_count; | 
|  | 158 | stream << std::setw(5) << std::setfill(' ') << "0x"; | 
|  | 159 | stream << std::hex << std::setfill('0'); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 160 | stream << std::setw(8) << info.usage_policy.producer_set_mask; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 161 | stream << std::setw(7) << std::setfill(' ') << "0x"; | 
|  | 162 | stream << std::hex << std::setfill('0'); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 163 | stream << std::setw(8) << info.usage_policy.producer_clear_mask; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 164 | stream << std::setw(9) << std::setfill(' ') << "0x"; | 
|  | 165 | stream << std::hex << std::setfill('0'); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 166 | stream << std::setw(8) << info.usage_policy.producer_deny_set_mask; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 167 | stream << std::setw(11) << std::setfill(' ') << "0x"; | 
|  | 168 | stream << std::hex << std::setfill('0'); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 169 | stream << std::setw(8) << info.usage_policy.producer_deny_clear_mask; | 
|  | 170 | stream << std::setw(5) << std::setfill(' ') << "0x"; | 
|  | 171 | stream << std::hex << std::setfill('0'); | 
|  | 172 | stream << std::setw(8) << info.usage_policy.consumer_set_mask; | 
|  | 173 | stream << std::setw(7) << std::setfill(' ') << "0x"; | 
|  | 174 | stream << std::hex << std::setfill('0'); | 
|  | 175 | stream << std::setw(8) << info.usage_policy.consumer_clear_mask; | 
|  | 176 | stream << std::setw(9) << std::setfill(' ') << "0x"; | 
|  | 177 | stream << std::hex << std::setfill('0'); | 
|  | 178 | stream << std::setw(8) << info.usage_policy.consumer_deny_set_mask; | 
|  | 179 | stream << std::setw(11) << std::setfill(' ') << "0x"; | 
|  | 180 | stream << std::hex << std::setfill('0'); | 
|  | 181 | stream << std::setw(8) << info.usage_policy.consumer_deny_clear_mask; | 
|  | 182 | stream << std::hex << std::setfill('0'); | 
|  | 183 | stream << std::endl; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 184 | } | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | stream << std::endl; | 
|  | 188 | stream << "Active Consumer Queues:\n"; | 
|  | 189 | stream << std::dec << std::setfill(' '); | 
|  | 190 | stream << std::right << std::setw(6) << "Id"; | 
|  | 191 | stream << std::right << std::setw(12) << " Imported"; | 
|  | 192 | stream << " "; | 
|  | 193 | stream << std::endl; | 
|  | 194 |  | 
|  | 195 | for (const auto& channel : channels) { | 
|  | 196 | if (channel->channel_type() == BufferHubChannel::kConsumerQueueType) { | 
|  | 197 | BufferHubChannel::BufferInfo info = channel->GetBufferInfo(); | 
|  | 198 |  | 
|  | 199 | stream << std::right << std::setw(6) << info.id; | 
|  | 200 | stream << std::right << std::setw(12) << info.capacity; | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 201 | stream << std::endl; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 202 | } | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | return stream.str(); | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | void BufferHubService::HandleImpulse(Message& message) { | 
|  | 209 | ATRACE_NAME("BufferHubService::HandleImpulse"); | 
|  | 210 | if (auto channel = message.GetChannel<BufferHubChannel>()) | 
|  | 211 | channel->HandleImpulse(message); | 
|  | 212 | } | 
|  | 213 |  | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 214 | pdx::Status<void> BufferHubService::HandleMessage(Message& message) { | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 215 | ATRACE_NAME("BufferHubService::HandleMessage"); | 
|  | 216 | auto channel = message.GetChannel<BufferHubChannel>(); | 
|  | 217 |  | 
|  | 218 | ALOGD_IF( | 
|  | 219 | TRACE, | 
|  | 220 | "BufferHubService::HandleMessage: channel=%p channel_id=%d opcode=%d", | 
|  | 221 | channel.get(), message.GetChannelId(), message.GetOp()); | 
|  | 222 |  | 
|  | 223 | // If the channel is already set up, let it handle the message. | 
|  | 224 | if (channel && !channel->HandleMessage(message)) | 
|  | 225 | return DefaultHandleMessage(message); | 
|  | 226 |  | 
|  | 227 | // This channel has not been set up yet, the following are valid operations. | 
|  | 228 | switch (message.GetOp()) { | 
|  | 229 | case BufferHubRPC::CreateBuffer::Opcode: | 
|  | 230 | DispatchRemoteMethod<BufferHubRPC::CreateBuffer>( | 
|  | 231 | *this, &BufferHubService::OnCreateBuffer, message); | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 232 | return {}; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 233 |  | 
|  | 234 | case BufferHubRPC::CreatePersistentBuffer::Opcode: | 
|  | 235 | DispatchRemoteMethod<BufferHubRPC::CreatePersistentBuffer>( | 
|  | 236 | *this, &BufferHubService::OnCreatePersistentBuffer, message); | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 237 | return {}; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 238 |  | 
|  | 239 | case BufferHubRPC::GetPersistentBuffer::Opcode: | 
|  | 240 | DispatchRemoteMethod<BufferHubRPC::GetPersistentBuffer>( | 
|  | 241 | *this, &BufferHubService::OnGetPersistentBuffer, message); | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 242 | return {}; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 243 |  | 
|  | 244 | case BufferHubRPC::CreateProducerQueue::Opcode: | 
|  | 245 | DispatchRemoteMethod<BufferHubRPC::CreateProducerQueue>( | 
|  | 246 | *this, &BufferHubService::OnCreateProducerQueue, message); | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 247 | return {}; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 248 |  | 
|  | 249 | default: | 
|  | 250 | return DefaultHandleMessage(message); | 
|  | 251 | } | 
|  | 252 | } | 
|  | 253 |  | 
|  | 254 | void BufferHubService::OnChannelClose(Message&, | 
|  | 255 | const std::shared_ptr<Channel>& channel) { | 
|  | 256 | if (auto buffer = std::static_pointer_cast<BufferHubChannel>(channel)) | 
|  | 257 | buffer->Detach(); | 
|  | 258 | } | 
|  | 259 |  | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 260 | Status<void> BufferHubService::OnCreateBuffer(Message& message, uint32_t width, | 
|  | 261 | uint32_t height, uint32_t format, | 
|  | 262 | uint64_t producer_usage, | 
|  | 263 | uint64_t consumer_usage, | 
|  | 264 | size_t meta_size_bytes, | 
|  | 265 | size_t slice_count) { | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 266 | // Use the producer channel id as the global buffer id. | 
|  | 267 | const int buffer_id = message.GetChannelId(); | 
|  | 268 | ALOGD_IF(TRACE, | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 269 | "BufferHubService::OnCreateBuffer: buffer_id=%d width=%u height=%u " | 
|  | 270 | "format=%u producer_usage=%" PRIx64 " consumer_usage=%" PRIx64 | 
|  | 271 | " meta_size_bytes=%zu slice_count=%zu", | 
|  | 272 | buffer_id, width, height, format, producer_usage, consumer_usage, | 
|  | 273 | meta_size_bytes, slice_count); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 274 |  | 
|  | 275 | // See if this channel is already attached to a buffer. | 
|  | 276 | if (const auto channel = message.GetChannel<BufferHubChannel>()) { | 
|  | 277 | ALOGE("BufferHubService::OnCreateBuffer: Buffer already created: buffer=%d", | 
|  | 278 | buffer_id); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 279 | return ErrorStatus(EALREADY); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 280 | } | 
|  | 281 |  | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 282 | auto status = ProducerChannel::Create(this, buffer_id, width, height, format, | 
|  | 283 | producer_usage, consumer_usage, | 
|  | 284 | meta_size_bytes, slice_count); | 
|  | 285 | if (status) { | 
|  | 286 | message.SetChannel(status.take()); | 
|  | 287 | return {}; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 288 | } else { | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 289 | ALOGE("BufferHubService::OnCreateBuffer: Failed to create producer: %s", | 
|  | 290 | status.GetErrorMessage().c_str()); | 
|  | 291 | return status.error_status(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 292 | } | 
|  | 293 | } | 
|  | 294 |  | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 295 | Status<void> BufferHubService::OnCreatePersistentBuffer( | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 296 | Message& message, const std::string& name, int user_id, int group_id, | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 297 | uint32_t width, uint32_t height, uint32_t format, uint64_t producer_usage, | 
|  | 298 | uint64_t consumer_usage, size_t meta_size_bytes, size_t slice_count) { | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 299 | const int channel_id = message.GetChannelId(); | 
|  | 300 | ALOGD_IF(TRACE, | 
|  | 301 | "BufferHubService::OnCreatePersistentBuffer: channel_id=%d name=%s " | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 302 | "user_id=%d group_id=%d width=%u height=%u format=%u " | 
|  | 303 | "producer_usage=%" PRIx64 " consumer_usage=%" PRIx64 | 
|  | 304 | " meta_size_bytes=%zu slice_count=%zu", | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 305 | channel_id, name.c_str(), user_id, group_id, width, height, format, | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 306 | producer_usage, consumer_usage, meta_size_bytes, slice_count); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 307 |  | 
|  | 308 | // See if this channel is already attached to a buffer. | 
|  | 309 | if (const auto channel = message.GetChannel<BufferHubChannel>()) { | 
|  | 310 | ALOGE( | 
|  | 311 | "BufferHubService::OnCreatePersistentBuffer: Channel already attached " | 
|  | 312 | "to buffer: channel_id=%d buffer_id=%d", | 
|  | 313 | channel_id, channel->buffer_id()); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 314 | return ErrorStatus(EALREADY); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 315 | } | 
|  | 316 |  | 
|  | 317 | const int euid = message.GetEffectiveUserId(); | 
|  | 318 | const int egid = message.GetEffectiveGroupId(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 319 |  | 
|  | 320 | if (auto buffer = GetNamedBuffer(name)) { | 
|  | 321 | if (!buffer->CheckAccess(euid, egid)) { | 
|  | 322 | ALOGE( | 
|  | 323 | "BufferHubService::OnCreatePersistentBuffer: Requesting process does " | 
|  | 324 | "not have permission to access named buffer: name=%s euid=%d egid=%d", | 
|  | 325 | name.c_str(), euid, euid); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 326 | return ErrorStatus(EPERM); | 
|  | 327 | } else if (!buffer->CheckParameters(width, height, format, producer_usage, | 
|  | 328 | consumer_usage, meta_size_bytes, | 
|  | 329 | slice_count)) { | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 330 | ALOGE( | 
|  | 331 | "BufferHubService::OnCreatePersistentBuffer: Requested an existing " | 
|  | 332 | "buffer with different parameters: name=%s", | 
|  | 333 | name.c_str()); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 334 | return ErrorStatus(EINVAL); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 335 | } else if (!buffer->IsDetached()) { | 
|  | 336 | ALOGE( | 
|  | 337 | "BufferHubService::OnCreatePersistentBuffer: Requesting a persistent " | 
|  | 338 | "buffer that is already attached to a channel: name=%s", | 
|  | 339 | name.c_str()); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 340 | return ErrorStatus(EINVAL); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 341 | } else { | 
|  | 342 | buffer->Attach(channel_id); | 
|  | 343 | message.SetChannel(buffer); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 344 | return {}; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 345 | } | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 346 | } else { | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 347 | auto status = ProducerChannel::Create( | 
|  | 348 | this, channel_id, width, height, format, producer_usage, consumer_usage, | 
|  | 349 | meta_size_bytes, slice_count); | 
|  | 350 | if (!status) { | 
|  | 351 | ALOGE("BufferHubService::OnCreateBuffer: Failed to create producer!!"); | 
|  | 352 | return status.error_status(); | 
|  | 353 | } | 
|  | 354 | auto persistent_buffer = status.take(); | 
|  | 355 | auto make_persistent_status = persistent_buffer->OnProducerMakePersistent( | 
|  | 356 | message, name, user_id, group_id); | 
|  | 357 | if (make_persistent_status) | 
|  | 358 | message.SetChannel(persistent_buffer); | 
|  | 359 | return make_persistent_status; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 360 | } | 
|  | 361 | } | 
|  | 362 |  | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 363 | Status<void> BufferHubService::OnGetPersistentBuffer(Message& message, | 
|  | 364 | const std::string& name) { | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 365 | const int channel_id = message.GetChannelId(); | 
|  | 366 | ALOGD_IF(TRACE, | 
|  | 367 | "BufferHubService::OnGetPersistentBuffer: channel_id=%d name=%s", | 
|  | 368 | channel_id, name.c_str()); | 
|  | 369 |  | 
|  | 370 | // See if this channel is already attached to a buffer. | 
|  | 371 | if (const auto channel = message.GetChannel<BufferHubChannel>()) { | 
|  | 372 | ALOGE( | 
|  | 373 | "BufferHubService::OnGetPersistentBuffer: Channel already attached to " | 
|  | 374 | "buffer: channel_id=%d buffer_id=%d", | 
|  | 375 | channel_id, channel->buffer_id()); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 376 | return ErrorStatus(EALREADY); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 377 | } | 
|  | 378 |  | 
|  | 379 | const int euid = message.GetEffectiveUserId(); | 
|  | 380 | const int egid = message.GetEffectiveGroupId(); | 
|  | 381 |  | 
|  | 382 | if (auto buffer = GetNamedBuffer(name)) { | 
|  | 383 | if (!buffer->CheckAccess(euid, egid)) { | 
|  | 384 | ALOGE( | 
|  | 385 | "BufferHubService::OnGetPersistentBuffer: Requesting process does " | 
|  | 386 | "not have permission to access named buffer: name=%s euid=%d egid=%d", | 
|  | 387 | name.c_str(), euid, egid); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 388 | return ErrorStatus(EPERM); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 389 | } else if (!buffer->IsDetached()) { | 
|  | 390 | ALOGE( | 
|  | 391 | "BufferHubService::OnGetPersistentBuffer: Requesting a persistent " | 
|  | 392 | "buffer that is already attached to a channel: name=%s", | 
|  | 393 | name.c_str()); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 394 | return ErrorStatus(EINVAL); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 395 | } else { | 
|  | 396 | buffer->Attach(channel_id); | 
|  | 397 | message.SetChannel(buffer); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 398 | return {}; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 399 | } | 
|  | 400 | } else { | 
|  | 401 | ALOGE("BufferHubService::OnGetPersistentBuffer: Buffer \"%s\" not found!", | 
|  | 402 | name.c_str()); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 403 | return ErrorStatus(ENOENT); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 404 | } | 
|  | 405 | } | 
|  | 406 |  | 
| Corey Tabaka | 1db8a5d | 2017-03-22 02:12:52 -0700 | [diff] [blame] | 407 | Status<QueueInfo> BufferHubService::OnCreateProducerQueue( | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 408 | pdx::Message& message, size_t meta_size_bytes, | 
|  | 409 | const UsagePolicy& usage_policy) { | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 410 | // Use the producer channel id as the global queue id. | 
|  | 411 | const int queue_id = message.GetChannelId(); | 
|  | 412 | ALOGD_IF(TRACE, "BufferHubService::OnCreateProducerQueue: queue_id=%d", | 
|  | 413 | queue_id); | 
|  | 414 |  | 
|  | 415 | // See if this channel is already attached to another object. | 
|  | 416 | if (const auto channel = message.GetChannel<BufferHubChannel>()) { | 
|  | 417 | ALOGE("BufferHubService::OnCreateProducerQueue: already created: queue=%d", | 
|  | 418 | queue_id); | 
| Corey Tabaka | 1db8a5d | 2017-03-22 02:12:52 -0700 | [diff] [blame] | 419 | return ErrorStatus(EALREADY); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 420 | } | 
|  | 421 |  | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 422 | auto status = ProducerQueueChannel::Create(this, queue_id, meta_size_bytes, | 
|  | 423 | usage_policy); | 
|  | 424 | if (status) { | 
|  | 425 | message.SetChannel(status.take()); | 
| Corey Tabaka | 1db8a5d | 2017-03-22 02:12:52 -0700 | [diff] [blame] | 426 | return {{meta_size_bytes, queue_id}}; | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 427 | } else { | 
|  | 428 | ALOGE("BufferHubService::OnCreateBuffer: Failed to create producer!!"); | 
| Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 429 | return status.error_status(); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 430 | } | 
|  | 431 | } | 
|  | 432 |  | 
|  | 433 | bool BufferHubService::AddNamedBuffer( | 
|  | 434 | const std::string& name, const std::shared_ptr<ProducerChannel>& buffer) { | 
|  | 435 | auto search = named_buffers_.find(name); | 
|  | 436 | if (search == named_buffers_.end()) { | 
|  | 437 | named_buffers_.emplace(name, buffer); | 
|  | 438 | return true; | 
|  | 439 | } else { | 
|  | 440 | return false; | 
|  | 441 | } | 
|  | 442 | } | 
|  | 443 |  | 
|  | 444 | std::shared_ptr<ProducerChannel> BufferHubService::GetNamedBuffer( | 
|  | 445 | const std::string& name) { | 
|  | 446 | auto search = named_buffers_.find(name); | 
|  | 447 | if (search != named_buffers_.end()) | 
|  | 448 | return search->second; | 
|  | 449 | else | 
|  | 450 | return nullptr; | 
|  | 451 | } | 
|  | 452 |  | 
|  | 453 | bool BufferHubService::RemoveNamedBuffer(const ProducerChannel& buffer) { | 
|  | 454 | for (auto it = named_buffers_.begin(); it != named_buffers_.end();) { | 
|  | 455 | if (it->second.get() == &buffer) { | 
|  | 456 | named_buffers_.erase(it); | 
|  | 457 | return true; | 
|  | 458 | } | 
|  | 459 | ++it; | 
|  | 460 | } | 
|  | 461 | return false; | 
|  | 462 | } | 
|  | 463 |  | 
|  | 464 | void BufferHubChannel::SignalAvailable() { | 
|  | 465 | ATRACE_NAME("BufferHubChannel::SignalAvailable"); | 
| Corey Tabaka | 3079cb7 | 2017-01-19 15:07:26 -0800 | [diff] [blame] | 466 | ALOGD_IF(TRACE, | 
|  | 467 | "BufferHubChannel::SignalAvailable: channel_id=%d buffer_id=%d", | 
|  | 468 | channel_id(), buffer_id()); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 469 | if (!IsDetached()) { | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 470 | const auto status = service_->ModifyChannelEvents(channel_id_, 0, POLLIN); | 
|  | 471 | ALOGE_IF(!status, | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 472 | "BufferHubChannel::SignalAvailable: failed to signal availability " | 
|  | 473 | "channel_id=%d: %s", | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 474 | channel_id_, status.GetErrorMessage().c_str()); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 475 | } else { | 
|  | 476 | ALOGD_IF(TRACE, "BufferHubChannel::SignalAvailable: detached buffer."); | 
|  | 477 | } | 
|  | 478 | } | 
|  | 479 |  | 
|  | 480 | void BufferHubChannel::ClearAvailable() { | 
|  | 481 | ATRACE_NAME("BufferHubChannel::ClearAvailable"); | 
| Corey Tabaka | 3079cb7 | 2017-01-19 15:07:26 -0800 | [diff] [blame] | 482 | ALOGD_IF(TRACE, | 
|  | 483 | "BufferHubChannel::ClearAvailable: channel_id=%d buffer_id=%d", | 
|  | 484 | channel_id(), buffer_id()); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 485 | if (!IsDetached()) { | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 486 | const auto status = service_->ModifyChannelEvents(channel_id_, POLLIN, 0); | 
|  | 487 | ALOGE_IF(!status, | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 488 | "BufferHubChannel::ClearAvailable: failed to clear availability " | 
|  | 489 | "channel_id=%d: %s", | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 490 | channel_id_, status.GetErrorMessage().c_str()); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 491 | } else { | 
|  | 492 | ALOGD_IF(TRACE, "BufferHubChannel::ClearAvailable: detached buffer."); | 
|  | 493 | } | 
|  | 494 | } | 
|  | 495 |  | 
|  | 496 | void BufferHubChannel::Hangup() { | 
|  | 497 | ATRACE_NAME("BufferHubChannel::Hangup"); | 
| Corey Tabaka | 3079cb7 | 2017-01-19 15:07:26 -0800 | [diff] [blame] | 498 | ALOGD_IF(TRACE, "BufferHubChannel::Hangup: channel_id=%d buffer_id=%d", | 
|  | 499 | channel_id(), buffer_id()); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 500 | if (!IsDetached()) { | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 501 | const auto status = service_->ModifyChannelEvents(channel_id_, 0, POLLHUP); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 502 | ALOGE_IF( | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 503 | !status, | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 504 | "BufferHubChannel::Hangup: failed to signal hangup channel_id=%d: %s", | 
| Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 505 | channel_id_, status.GetErrorMessage().c_str()); | 
| Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 506 | } else { | 
|  | 507 | ALOGD_IF(TRACE, "BufferHubChannel::Hangup: detached buffer."); | 
|  | 508 | } | 
|  | 509 | } | 
|  | 510 |  | 
|  | 511 | }  // namespace dvr | 
|  | 512 | }  // namespace android |