Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 1 | #include "producer_channel.h" |
| 2 | |
Alex Vakulenko | 4fe6058 | 2017-02-02 11:35:59 -0800 | [diff] [blame] | 3 | #include <log/log.h> |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 4 | #include <sync/sync.h> |
| 5 | #include <sys/poll.h> |
| 6 | #include <utils/Trace.h> |
| 7 | |
| 8 | #include <algorithm> |
| 9 | #include <atomic> |
| 10 | #include <thread> |
| 11 | |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 12 | #include <private/dvr/bufferhub_rpc.h> |
| 13 | #include "consumer_channel.h" |
| 14 | |
| 15 | using android::pdx::BorrowedHandle; |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 16 | using android::pdx::ErrorStatus; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 17 | using android::pdx::Message; |
| 18 | using android::pdx::RemoteChannelHandle; |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 19 | using android::pdx::Status; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 20 | using android::pdx::rpc::BufferWrapper; |
| 21 | using android::pdx::rpc::DispatchRemoteMethod; |
| 22 | using android::pdx::rpc::WrapBuffer; |
| 23 | |
| 24 | namespace android { |
| 25 | namespace dvr { |
| 26 | |
| 27 | ProducerChannel::ProducerChannel(BufferHubService* service, int channel_id, |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 28 | uint32_t width, uint32_t height, |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 29 | uint32_t format, uint64_t usage, |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 30 | size_t meta_size_bytes, size_t slice_count, |
| 31 | int* error) |
| 32 | : BufferHubChannel(service, channel_id, channel_id, kProducerType), |
| 33 | pending_consumers_(0), |
| 34 | slices_(std::max(static_cast<size_t>(1), slice_count)), |
| 35 | producer_owns_(true), |
| 36 | meta_size_bytes_(meta_size_bytes), |
| 37 | meta_(meta_size_bytes ? new uint8_t[meta_size_bytes] : nullptr) { |
| 38 | for (auto& ion_buffer : slices_) { |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 39 | const int ret = ion_buffer.Alloc(width, height, format, usage); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 40 | if (ret < 0) { |
| 41 | ALOGE("ProducerChannel::ProducerChannel: Failed to allocate buffer: %s", |
| 42 | strerror(-ret)); |
| 43 | *error = ret; |
| 44 | return; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // Success. |
| 49 | *error = 0; |
| 50 | } |
| 51 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 52 | Status<std::shared_ptr<ProducerChannel>> ProducerChannel::Create( |
| 53 | BufferHubService* service, int channel_id, uint32_t width, uint32_t height, |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 54 | uint32_t format, uint64_t usage, size_t meta_size_bytes, |
| 55 | size_t slice_count) { |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 56 | int error; |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 57 | std::shared_ptr<ProducerChannel> producer( |
| 58 | new ProducerChannel(service, channel_id, width, height, format, usage, |
| 59 | meta_size_bytes, slice_count, &error)); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 60 | if (error < 0) |
| 61 | return ErrorStatus(-error); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 62 | else |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 63 | return {std::move(producer)}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | ProducerChannel::~ProducerChannel() { |
Corey Tabaka | 3079cb7 | 2017-01-19 15:07:26 -0800 | [diff] [blame] | 67 | ALOGD_IF(TRACE, |
| 68 | "ProducerChannel::~ProducerChannel: channel_id=%d buffer_id=%d", |
| 69 | channel_id(), buffer_id()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 70 | for (auto consumer : consumer_channels_) |
| 71 | consumer->OnProducerClosed(); |
| 72 | } |
| 73 | |
| 74 | BufferHubChannel::BufferInfo ProducerChannel::GetBufferInfo() const { |
| 75 | return BufferInfo(buffer_id(), consumer_channels_.size(), slices_[0].width(), |
| 76 | slices_[0].height(), slices_[0].format(), |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 77 | slices_[0].usage(), slices_.size(), name_); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void ProducerChannel::HandleImpulse(Message& message) { |
| 81 | ATRACE_NAME("ProducerChannel::HandleImpulse"); |
| 82 | switch (message.GetOp()) { |
| 83 | case BufferHubRPC::ProducerGain::Opcode: |
| 84 | OnProducerGain(message); |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | bool ProducerChannel::HandleMessage(Message& message) { |
| 90 | ATRACE_NAME("ProducerChannel::HandleMessage"); |
| 91 | switch (message.GetOp()) { |
| 92 | case BufferHubRPC::GetBuffer::Opcode: |
| 93 | DispatchRemoteMethod<BufferHubRPC::GetBuffer>( |
| 94 | *this, &ProducerChannel::OnGetBuffer, message); |
| 95 | return true; |
| 96 | |
| 97 | case BufferHubRPC::GetBuffers::Opcode: |
| 98 | DispatchRemoteMethod<BufferHubRPC::GetBuffers>( |
| 99 | *this, &ProducerChannel::OnGetBuffers, message); |
| 100 | return true; |
| 101 | |
| 102 | case BufferHubRPC::NewConsumer::Opcode: |
| 103 | DispatchRemoteMethod<BufferHubRPC::NewConsumer>( |
| 104 | *this, &ProducerChannel::OnNewConsumer, message); |
| 105 | return true; |
| 106 | |
| 107 | case BufferHubRPC::ProducerPost::Opcode: |
| 108 | DispatchRemoteMethod<BufferHubRPC::ProducerPost>( |
| 109 | *this, &ProducerChannel::OnProducerPost, message); |
| 110 | return true; |
| 111 | |
| 112 | case BufferHubRPC::ProducerGain::Opcode: |
| 113 | DispatchRemoteMethod<BufferHubRPC::ProducerGain>( |
| 114 | *this, &ProducerChannel::OnProducerGain, message); |
| 115 | return true; |
| 116 | |
| 117 | case BufferHubRPC::ProducerMakePersistent::Opcode: |
| 118 | DispatchRemoteMethod<BufferHubRPC::ProducerMakePersistent>( |
| 119 | *this, &ProducerChannel::OnProducerMakePersistent, message); |
| 120 | return true; |
| 121 | |
| 122 | case BufferHubRPC::ProducerRemovePersistence::Opcode: |
| 123 | DispatchRemoteMethod<BufferHubRPC::ProducerRemovePersistence>( |
| 124 | *this, &ProducerChannel::OnRemovePersistence, message); |
| 125 | return true; |
| 126 | |
| 127 | default: |
| 128 | return false; |
| 129 | } |
| 130 | } |
| 131 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 132 | Status<NativeBufferHandle<BorrowedHandle>> ProducerChannel::OnGetBuffer( |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 133 | Message& message, unsigned index) { |
| 134 | ATRACE_NAME("ProducerChannel::OnGetBuffer"); |
| 135 | ALOGD_IF(TRACE, "ProducerChannel::OnGetBuffer: buffer=%d", buffer_id()); |
| 136 | if (index < slices_.size()) { |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 137 | return {NativeBufferHandle<BorrowedHandle>(slices_[index], buffer_id())}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 138 | } else { |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 139 | return ErrorStatus(EINVAL); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 143 | Status<std::vector<NativeBufferHandle<BorrowedHandle>>> |
| 144 | ProducerChannel::OnGetBuffers(Message&) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 145 | ATRACE_NAME("ProducerChannel::OnGetBuffers"); |
| 146 | ALOGD_IF(TRACE, "ProducerChannel::OnGetBuffers: buffer_id=%d", buffer_id()); |
| 147 | std::vector<NativeBufferHandle<BorrowedHandle>> buffer_handles; |
| 148 | for (const auto& buffer : slices_) |
| 149 | buffer_handles.emplace_back(buffer, buffer_id()); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 150 | return {std::move(buffer_handles)}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 151 | } |
| 152 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 153 | Status<RemoteChannelHandle> ProducerChannel::CreateConsumer(Message& message) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 154 | ATRACE_NAME("ProducerChannel::CreateConsumer"); |
| 155 | ALOGD_IF(TRACE, "ProducerChannel::CreateConsumer: buffer_id=%d", buffer_id()); |
| 156 | |
| 157 | int channel_id; |
| 158 | auto status = message.PushChannel(0, nullptr, &channel_id); |
| 159 | if (!status) { |
| 160 | ALOGE( |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 161 | "ProducerChannel::CreateConsumer: Failed to push consumer channel: %s", |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 162 | status.GetErrorMessage().c_str()); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 163 | return ErrorStatus(ENOMEM); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | auto consumer = std::make_shared<ConsumerChannel>( |
| 167 | service(), buffer_id(), channel_id, shared_from_this()); |
Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 168 | const auto channel_status = service()->SetChannel(channel_id, consumer); |
| 169 | if (!channel_status) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 170 | ALOGE( |
| 171 | "ProducerChannel::CreateConsumer: failed to set new consumer channel: " |
| 172 | "%s", |
Alex Vakulenko | f0a7bd0 | 2017-03-31 18:06:19 -0700 | [diff] [blame] | 173 | channel_status.GetErrorMessage().c_str()); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 174 | return ErrorStatus(ENOMEM); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | if (!producer_owns_) { |
| 178 | // Signal the new consumer when adding it to a posted producer. |
| 179 | if (consumer->OnProducerPosted()) |
| 180 | pending_consumers_++; |
| 181 | } |
| 182 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 183 | return {status.take()}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 186 | Status<RemoteChannelHandle> ProducerChannel::OnNewConsumer(Message& message) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 187 | ATRACE_NAME("ProducerChannel::OnNewConsumer"); |
| 188 | ALOGD_IF(TRACE, "ProducerChannel::OnNewConsumer: buffer_id=%d", buffer_id()); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 189 | return CreateConsumer(message); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 190 | } |
| 191 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 192 | Status<void> ProducerChannel::OnProducerPost( |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 193 | Message&, LocalFence acquire_fence, |
| 194 | BufferWrapper<std::vector<std::uint8_t>> metadata) { |
| 195 | ATRACE_NAME("ProducerChannel::OnProducerPost"); |
| 196 | ALOGD_IF(TRACE, "ProducerChannel::OnProducerPost: buffer_id=%d", buffer_id()); |
| 197 | if (!producer_owns_) { |
| 198 | ALOGE("ProducerChannel::OnProducerPost: Not in gained state!"); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 199 | return ErrorStatus(EBUSY); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Corey Tabaka | 8a4e6a9 | 2017-04-20 13:42:02 -0700 | [diff] [blame^] | 202 | if (meta_size_bytes_ != metadata.size()) { |
| 203 | ALOGD_IF(TRACE, |
| 204 | "ProducerChannel::OnProducerPost: Expected meta_size_bytes=%zu " |
| 205 | "got size=%zu", |
| 206 | meta_size_bytes_, metadata.size()); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 207 | return ErrorStatus(EINVAL); |
Corey Tabaka | 8a4e6a9 | 2017-04-20 13:42:02 -0700 | [diff] [blame^] | 208 | } |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 209 | |
Corey Tabaka | 8a4e6a9 | 2017-04-20 13:42:02 -0700 | [diff] [blame^] | 210 | std::copy(metadata.begin(), metadata.end(), meta_.get()); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 211 | post_fence_ = std::move(acquire_fence); |
| 212 | producer_owns_ = false; |
| 213 | |
| 214 | // Signal any interested consumers. If there are none, automatically release |
| 215 | // the buffer. |
| 216 | pending_consumers_ = 0; |
| 217 | for (auto consumer : consumer_channels_) { |
| 218 | if (consumer->OnProducerPosted()) |
| 219 | pending_consumers_++; |
| 220 | } |
| 221 | if (pending_consumers_ == 0) |
| 222 | SignalAvailable(); |
| 223 | ALOGD_IF(TRACE, "ProducerChannel::OnProducerPost: %d pending consumers", |
| 224 | pending_consumers_); |
| 225 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 226 | return {}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 229 | Status<LocalFence> ProducerChannel::OnProducerGain(Message& message) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 230 | ATRACE_NAME("ProducerChannel::OnGain"); |
| 231 | ALOGD_IF(TRACE, "ProducerChannel::OnGain: buffer_id=%d", buffer_id()); |
| 232 | if (producer_owns_) { |
| 233 | ALOGE("ProducerChanneL::OnGain: Already in gained state: channel=%d", |
| 234 | channel_id()); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 235 | return ErrorStatus(EALREADY); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // There are still pending consumers, return busy. |
| 239 | if (pending_consumers_ > 0) |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 240 | return ErrorStatus(EBUSY); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 241 | |
| 242 | ClearAvailable(); |
| 243 | producer_owns_ = true; |
Alex Vakulenko | 052f3ae | 2017-03-31 09:10:43 -0700 | [diff] [blame] | 244 | post_fence_.close(); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 245 | return {std::move(returned_fence_)}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 248 | Status<std::pair<BorrowedFence, BufferWrapper<std::uint8_t*>>> |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 249 | ProducerChannel::OnConsumerAcquire(Message& message, |
| 250 | std::size_t metadata_size) { |
| 251 | ATRACE_NAME("ProducerChannel::OnConsumerAcquire"); |
| 252 | ALOGD_IF(TRACE, "ProducerChannel::OnConsumerAcquire: buffer_id=%d", |
| 253 | buffer_id()); |
| 254 | if (producer_owns_) { |
| 255 | ALOGE("ProducerChannel::OnConsumerAcquire: Not in posted state!"); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 256 | return ErrorStatus(EBUSY); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // Return a borrowed fd to avoid unnecessary duplication of the underlying fd. |
| 260 | // Serialization just needs to read the handle. |
| 261 | if (metadata_size == 0) |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 262 | return {std::make_pair(post_fence_.borrow(), |
| 263 | WrapBuffer<std::uint8_t>(nullptr, 0))}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 264 | else |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 265 | return {std::make_pair(post_fence_.borrow(), |
| 266 | WrapBuffer(meta_.get(), meta_size_bytes_))}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 269 | Status<void> ProducerChannel::OnConsumerRelease(Message&, |
| 270 | LocalFence release_fence) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 271 | ATRACE_NAME("ProducerChannel::OnConsumerRelease"); |
| 272 | ALOGD_IF(TRACE, "ProducerChannel::OnConsumerRelease: buffer_id=%d", |
| 273 | buffer_id()); |
| 274 | if (producer_owns_) { |
| 275 | ALOGE("ProducerChannel::OnConsumerRelease: Not in acquired state!"); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 276 | return ErrorStatus(EBUSY); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | // Attempt to merge the fences if necessary. |
| 280 | if (release_fence) { |
| 281 | if (returned_fence_) { |
Corey Tabaka | 3079cb7 | 2017-01-19 15:07:26 -0800 | [diff] [blame] | 282 | LocalFence merged_fence(sync_merge("bufferhub_merged", |
| 283 | returned_fence_.get_fd(), |
| 284 | release_fence.get_fd())); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 285 | const int error = errno; |
| 286 | if (!merged_fence) { |
| 287 | ALOGE("ProducerChannel::OnConsumerRelease: Failed to merge fences: %s", |
| 288 | strerror(error)); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 289 | return ErrorStatus(error); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 290 | } |
| 291 | returned_fence_ = std::move(merged_fence); |
| 292 | } else { |
| 293 | returned_fence_ = std::move(release_fence); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | OnConsumerIgnored(); |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 298 | return {}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void ProducerChannel::OnConsumerIgnored() { |
| 302 | if (!--pending_consumers_) |
| 303 | SignalAvailable(); |
| 304 | ALOGD_IF(TRACE, |
| 305 | "ProducerChannel::OnConsumerIgnored: buffer_id=%d %d consumers left", |
| 306 | buffer_id(), pending_consumers_); |
| 307 | } |
| 308 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 309 | Status<void> ProducerChannel::OnProducerMakePersistent(Message& message, |
| 310 | const std::string& name, |
| 311 | int user_id, |
| 312 | int group_id) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 313 | ATRACE_NAME("ProducerChannel::OnProducerMakePersistent"); |
| 314 | ALOGD_IF(TRACE, |
| 315 | "ProducerChannel::OnProducerMakePersistent: buffer_id=%d name=%s " |
| 316 | "user_id=%d group_id=%d", |
| 317 | buffer_id(), name.c_str(), user_id, group_id); |
| 318 | |
| 319 | if (name.empty() || (user_id < 0 && user_id != kNoCheckId) || |
| 320 | (group_id < 0 && group_id != kNoCheckId)) { |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 321 | return ErrorStatus(EINVAL); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | // Try to add this buffer with the requested name. |
| 325 | if (service()->AddNamedBuffer(name, std::static_pointer_cast<ProducerChannel>( |
| 326 | shared_from_this()))) { |
| 327 | // If successful, set the requested permissions. |
| 328 | |
| 329 | // A value of zero indicates that the ids from the sending process should be |
| 330 | // used. |
| 331 | if (user_id == kUseCallerId) |
| 332 | user_id = message.GetEffectiveUserId(); |
| 333 | if (group_id == kUseCallerId) |
| 334 | group_id = message.GetEffectiveGroupId(); |
| 335 | |
| 336 | owner_user_id_ = user_id; |
| 337 | owner_group_id_ = group_id; |
| 338 | name_ = name; |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 339 | return {}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 340 | } else { |
| 341 | // Otherwise a buffer with that name already exists. |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 342 | return ErrorStatus(EALREADY); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 346 | Status<void> ProducerChannel::OnRemovePersistence(Message&) { |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 347 | if (service()->RemoveNamedBuffer(*this)) |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 348 | return {}; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 349 | else |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 350 | return ErrorStatus(ENOENT); |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void ProducerChannel::AddConsumer(ConsumerChannel* channel) { |
| 354 | consumer_channels_.push_back(channel); |
| 355 | } |
| 356 | |
| 357 | void ProducerChannel::RemoveConsumer(ConsumerChannel* channel) { |
| 358 | consumer_channels_.erase( |
| 359 | std::find(consumer_channels_.begin(), consumer_channels_.end(), channel)); |
| 360 | } |
| 361 | |
| 362 | // Returns true if either the user or group ids match the owning ids or both |
| 363 | // owning ids are not set, in which case access control does not apply. |
| 364 | bool ProducerChannel::CheckAccess(int euid, int egid) { |
| 365 | const bool no_check = |
| 366 | owner_user_id_ == kNoCheckId && owner_group_id_ == kNoCheckId; |
| 367 | const bool euid_check = euid == owner_user_id_ || euid == kRootId; |
| 368 | const bool egid_check = egid == owner_group_id_ || egid == kRootId; |
| 369 | return no_check || euid_check || egid_check; |
| 370 | } |
| 371 | |
| 372 | // Returns true if the given parameters match the underlying buffer parameters. |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 373 | bool ProducerChannel::CheckParameters(uint32_t width, uint32_t height, |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 374 | uint32_t format, uint64_t usage, |
Corey Tabaka | cd52dd9 | 2017-04-07 18:03:57 -0700 | [diff] [blame] | 375 | size_t meta_size_bytes, |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 376 | size_t slice_count) { |
Corey Tabaka | 3079cb7 | 2017-01-19 15:07:26 -0800 | [diff] [blame] | 377 | return slices_.size() == slice_count && meta_size_bytes == meta_size_bytes_ && |
| 378 | slices_[0].width() == width && slices_[0].height() == height && |
Jiwen 'Steve' Cai | 0057fdd | 2017-05-02 11:21:18 -0700 | [diff] [blame] | 379 | slices_[0].format() == format && slices_[0].usage() == usage; |
Alex Vakulenko | e4eec20 | 2017-01-27 14:41:04 -0800 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | } // namespace dvr |
| 383 | } // namespace android |