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