Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 1 | #include <private/dvr/producer_buffer.h> |
| 2 | |
| 3 | using android::pdx::LocalChannelHandle; |
| 4 | using android::pdx::LocalHandle; |
| 5 | using android::pdx::Status; |
| 6 | |
| 7 | namespace android { |
| 8 | namespace dvr { |
| 9 | |
| 10 | ProducerBuffer::ProducerBuffer(uint32_t width, uint32_t height, uint32_t format, |
| 11 | uint64_t usage, size_t user_metadata_size) |
| 12 | : BASE(BufferHubRPC::kClientPath) { |
| 13 | ATRACE_NAME("ProducerBuffer::ProducerBuffer"); |
| 14 | ALOGD_IF(TRACE, |
| 15 | "ProducerBuffer::ProducerBuffer: fd=%d width=%u height=%u format=%u " |
| 16 | "usage=%" PRIx64 " user_metadata_size=%zu", |
| 17 | event_fd(), width, height, format, usage, user_metadata_size); |
| 18 | |
| 19 | auto status = InvokeRemoteMethod<BufferHubRPC::CreateBuffer>( |
| 20 | width, height, format, usage, user_metadata_size); |
| 21 | if (!status) { |
| 22 | ALOGE( |
| 23 | "ProducerBuffer::ProducerBuffer: Failed to create producer buffer: %s", |
| 24 | status.GetErrorMessage().c_str()); |
| 25 | Close(-status.error()); |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | const int ret = ImportBuffer(); |
| 30 | if (ret < 0) { |
| 31 | ALOGE( |
| 32 | "ProducerBuffer::ProducerBuffer: Failed to import producer buffer: %s", |
| 33 | strerror(-ret)); |
| 34 | Close(ret); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | ProducerBuffer::ProducerBuffer(uint64_t usage, size_t size) |
| 39 | : BASE(BufferHubRPC::kClientPath) { |
| 40 | ATRACE_NAME("ProducerBuffer::ProducerBuffer"); |
| 41 | ALOGD_IF(TRACE, "ProducerBuffer::ProducerBuffer: usage=%" PRIx64 " size=%zu", |
| 42 | usage, size); |
| 43 | const int width = static_cast<int>(size); |
| 44 | const int height = 1; |
| 45 | const int format = HAL_PIXEL_FORMAT_BLOB; |
| 46 | const size_t user_metadata_size = 0; |
| 47 | |
| 48 | auto status = InvokeRemoteMethod<BufferHubRPC::CreateBuffer>( |
| 49 | width, height, format, usage, user_metadata_size); |
| 50 | if (!status) { |
| 51 | ALOGE("ProducerBuffer::ProducerBuffer: Failed to create blob: %s", |
| 52 | status.GetErrorMessage().c_str()); |
| 53 | Close(-status.error()); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | const int ret = ImportBuffer(); |
| 58 | if (ret < 0) { |
| 59 | ALOGE( |
| 60 | "ProducerBuffer::ProducerBuffer: Failed to import producer buffer: %s", |
| 61 | strerror(-ret)); |
| 62 | Close(ret); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | ProducerBuffer::ProducerBuffer(LocalChannelHandle channel) |
| 67 | : BASE(std::move(channel)) { |
| 68 | const int ret = ImportBuffer(); |
| 69 | if (ret < 0) { |
| 70 | ALOGE( |
| 71 | "ProducerBuffer::ProducerBuffer: Failed to import producer buffer: %s", |
| 72 | strerror(-ret)); |
| 73 | Close(ret); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | int ProducerBuffer::LocalPost(const DvrNativeBufferMetadata* meta, |
| 78 | const LocalHandle& ready_fence) { |
| 79 | if (const int error = CheckMetadata(meta->user_metadata_size)) |
| 80 | return error; |
| 81 | |
Tianyu | f669f6a | 2018-10-10 15:34:32 -0700 | [diff] [blame] | 82 | // The buffer can be posted iff the buffer state for this client is gained. |
| 83 | uint64_t current_buffer_state = |
| 84 | buffer_state_->load(std::memory_order_acquire); |
| 85 | if (!BufferHubDefs::IsClientGained(current_buffer_state, |
| 86 | client_state_mask())) { |
| 87 | ALOGE("%s: not gained, id=%d state=%" PRIx64 ".", __FUNCTION__, id(), |
| 88 | current_buffer_state); |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 89 | return -EBUSY; |
| 90 | } |
| 91 | |
Tianyu | f669f6a | 2018-10-10 15:34:32 -0700 | [diff] [blame] | 92 | // Set the producer client buffer state to released, other clients' buffer |
| 93 | // state to posted. |
| 94 | uint64_t current_active_clients_bit_mask = |
| 95 | active_clients_bit_mask_->load(std::memory_order_acquire); |
| 96 | uint64_t updated_buffer_state = current_active_clients_bit_mask & |
| 97 | (~client_state_mask()) & |
| 98 | BufferHubDefs::kHighBitsMask; |
| 99 | while (!buffer_state_->compare_exchange_weak( |
| 100 | current_buffer_state, updated_buffer_state, std::memory_order_acq_rel, |
| 101 | std::memory_order_acquire)) { |
| 102 | ALOGD( |
| 103 | "%s: Failed to post the buffer. Current buffer state was changed to " |
| 104 | "%" PRIx64 |
| 105 | " when trying to post the buffer and modify the buffer state to " |
| 106 | "%" PRIx64 |
| 107 | ". About to try again if the buffer is still gained by this client.", |
| 108 | __FUNCTION__, current_buffer_state, updated_buffer_state); |
| 109 | if (!BufferHubDefs::IsClientGained(current_buffer_state, |
| 110 | client_state_mask())) { |
| 111 | ALOGE( |
| 112 | "%s: Failed to post the buffer. The buffer is no longer gained, " |
| 113 | "id=%d state=%" PRIx64 ".", |
| 114 | __FUNCTION__, id(), current_buffer_state); |
| 115 | return -EBUSY; |
| 116 | } |
| 117 | } |
| 118 | |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 119 | // Copy the canonical metadata. |
| 120 | void* metadata_ptr = reinterpret_cast<void*>(&metadata_header_->metadata); |
| 121 | memcpy(metadata_ptr, meta, sizeof(DvrNativeBufferMetadata)); |
| 122 | // Copy extra user requested metadata. |
| 123 | if (meta->user_metadata_ptr && meta->user_metadata_size) { |
| 124 | void* metadata_src = reinterpret_cast<void*>(meta->user_metadata_ptr); |
| 125 | memcpy(user_metadata_ptr_, metadata_src, meta->user_metadata_size); |
| 126 | } |
| 127 | |
| 128 | // Send out the acquire fence through the shared epoll fd. Note that during |
| 129 | // posting no consumer is not expected to be polling on the fence. |
| 130 | if (const int error = UpdateSharedFence(ready_fence, shared_acquire_fence_)) |
| 131 | return error; |
| 132 | |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | int ProducerBuffer::Post(const LocalHandle& ready_fence, const void* meta, |
| 137 | size_t user_metadata_size) { |
| 138 | ATRACE_NAME("ProducerBuffer::Post"); |
| 139 | |
| 140 | // Populate cononical metadata for posting. |
| 141 | DvrNativeBufferMetadata canonical_meta; |
| 142 | canonical_meta.user_metadata_ptr = reinterpret_cast<uint64_t>(meta); |
| 143 | canonical_meta.user_metadata_size = user_metadata_size; |
| 144 | |
| 145 | if (const int error = LocalPost(&canonical_meta, ready_fence)) |
| 146 | return error; |
| 147 | |
| 148 | return ReturnStatusOrError(InvokeRemoteMethod<BufferHubRPC::ProducerPost>( |
| 149 | BorrowedFence(ready_fence.Borrow()))); |
| 150 | } |
| 151 | |
| 152 | int ProducerBuffer::PostAsync(const DvrNativeBufferMetadata* meta, |
| 153 | const LocalHandle& ready_fence) { |
| 154 | ATRACE_NAME("ProducerBuffer::PostAsync"); |
| 155 | |
| 156 | if (const int error = LocalPost(meta, ready_fence)) |
| 157 | return error; |
| 158 | |
| 159 | return ReturnStatusOrError(SendImpulse(BufferHubRPC::ProducerPost::Opcode)); |
| 160 | } |
| 161 | |
| 162 | int ProducerBuffer::LocalGain(DvrNativeBufferMetadata* out_meta, |
Tianyu | 5465d6c | 2018-08-14 13:03:10 -0700 | [diff] [blame] | 163 | LocalHandle* out_fence, bool gain_posted_buffer) { |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 164 | if (!out_meta) |
| 165 | return -EINVAL; |
| 166 | |
Tianyu | f669f6a | 2018-10-10 15:34:32 -0700 | [diff] [blame] | 167 | uint64_t current_buffer_state = |
| 168 | buffer_state_->load(std::memory_order_acquire); |
| 169 | ALOGD_IF(TRACE, "%s: buffer=%d, state=%" PRIx64 ".", __FUNCTION__, id(), |
| 170 | current_buffer_state); |
| 171 | |
| 172 | if (BufferHubDefs::IsClientGained(current_buffer_state, |
| 173 | client_state_mask())) { |
Tianyu Jiang | 8cfd42a | 2018-12-04 14:20:10 -0800 | [diff] [blame^] | 174 | ALOGV("%s: already gained id=%d.", __FUNCTION__, id()); |
Tianyu Jiang | 60887c9 | 2018-11-14 15:52:38 -0800 | [diff] [blame] | 175 | return 0; |
Tianyu | 5465d6c | 2018-08-14 13:03:10 -0700 | [diff] [blame] | 176 | } |
Tianyu | f669f6a | 2018-10-10 15:34:32 -0700 | [diff] [blame] | 177 | if (BufferHubDefs::AnyClientAcquired(current_buffer_state) || |
Tianyu Jiang | 60887c9 | 2018-11-14 15:52:38 -0800 | [diff] [blame] | 178 | BufferHubDefs::AnyClientGained(current_buffer_state) || |
Tianyu | f669f6a | 2018-10-10 15:34:32 -0700 | [diff] [blame] | 179 | (BufferHubDefs::AnyClientPosted(current_buffer_state) && |
| 180 | !gain_posted_buffer)) { |
| 181 | ALOGE("%s: not released id=%d state=%" PRIx64 ".", __FUNCTION__, id(), |
| 182 | current_buffer_state); |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 183 | return -EBUSY; |
| 184 | } |
Tianyu | f669f6a | 2018-10-10 15:34:32 -0700 | [diff] [blame] | 185 | // Change the buffer state to gained state. |
| 186 | uint64_t updated_buffer_state = client_state_mask(); |
| 187 | while (!buffer_state_->compare_exchange_weak( |
| 188 | current_buffer_state, updated_buffer_state, std::memory_order_acq_rel, |
| 189 | std::memory_order_acquire)) { |
| 190 | ALOGD( |
| 191 | "%s: Failed to gain the buffer. Current buffer state was changed to " |
| 192 | "%" PRIx64 |
| 193 | " when trying to gain the buffer and modify the buffer state to " |
| 194 | "%" PRIx64 |
| 195 | ". About to try again if the buffer is still not read by other " |
| 196 | "clients.", |
| 197 | __FUNCTION__, current_buffer_state, updated_buffer_state); |
| 198 | |
| 199 | if (BufferHubDefs::AnyClientAcquired(current_buffer_state) || |
Tianyu Jiang | 60887c9 | 2018-11-14 15:52:38 -0800 | [diff] [blame] | 200 | BufferHubDefs::AnyClientGained(current_buffer_state) || |
Tianyu | f669f6a | 2018-10-10 15:34:32 -0700 | [diff] [blame] | 201 | (BufferHubDefs::AnyClientPosted(current_buffer_state) && |
| 202 | !gain_posted_buffer)) { |
| 203 | ALOGE( |
| 204 | "%s: Failed to gain the buffer. The buffer is no longer released. " |
| 205 | "id=%d state=%" PRIx64 ".", |
| 206 | __FUNCTION__, id(), current_buffer_state); |
| 207 | return -EBUSY; |
| 208 | } |
| 209 | } |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 210 | |
| 211 | // Canonical metadata is undefined on Gain. Except for user_metadata and |
| 212 | // release_fence_mask. Fill in the user_metadata_ptr in address space of the |
| 213 | // local process. |
| 214 | if (metadata_header_->metadata.user_metadata_size && user_metadata_ptr_) { |
| 215 | out_meta->user_metadata_size = |
| 216 | metadata_header_->metadata.user_metadata_size; |
| 217 | out_meta->user_metadata_ptr = |
| 218 | reinterpret_cast<uint64_t>(user_metadata_ptr_); |
| 219 | } else { |
| 220 | out_meta->user_metadata_size = 0; |
| 221 | out_meta->user_metadata_ptr = 0; |
| 222 | } |
| 223 | |
Tianyu | f669f6a | 2018-10-10 15:34:32 -0700 | [diff] [blame] | 224 | uint64_t current_fence_state = fence_state_->load(std::memory_order_acquire); |
| 225 | uint64_t current_active_clients_bit_mask = |
| 226 | active_clients_bit_mask_->load(std::memory_order_acquire); |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 227 | // If there is an release fence from consumer, we need to return it. |
Tianyu | f669f6a | 2018-10-10 15:34:32 -0700 | [diff] [blame] | 228 | // TODO(b/112007999) add an atomic variable in metadata header in shared |
| 229 | // memory to indicate which client is the last producer of the buffer. |
| 230 | // Currently, assume the first client is the only producer to the buffer. |
| 231 | if (current_fence_state & current_active_clients_bit_mask & |
| 232 | (~BufferHubDefs::kFirstClientBitMask)) { |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 233 | *out_fence = shared_release_fence_.Duplicate(); |
| 234 | out_meta->release_fence_mask = |
Tianyu | f669f6a | 2018-10-10 15:34:32 -0700 | [diff] [blame] | 235 | current_fence_state & current_active_clients_bit_mask; |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 236 | } |
| 237 | |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
Tianyu | 5465d6c | 2018-08-14 13:03:10 -0700 | [diff] [blame] | 241 | int ProducerBuffer::Gain(LocalHandle* release_fence, bool gain_posted_buffer) { |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 242 | ATRACE_NAME("ProducerBuffer::Gain"); |
| 243 | |
| 244 | DvrNativeBufferMetadata meta; |
Tianyu | 5465d6c | 2018-08-14 13:03:10 -0700 | [diff] [blame] | 245 | if (const int error = LocalGain(&meta, release_fence, gain_posted_buffer)) |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 246 | return error; |
| 247 | |
| 248 | auto status = InvokeRemoteMethod<BufferHubRPC::ProducerGain>(); |
| 249 | if (!status) |
| 250 | return -status.error(); |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | int ProducerBuffer::GainAsync(DvrNativeBufferMetadata* out_meta, |
Tianyu | 5465d6c | 2018-08-14 13:03:10 -0700 | [diff] [blame] | 255 | LocalHandle* release_fence, |
| 256 | bool gain_posted_buffer) { |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 257 | ATRACE_NAME("ProducerBuffer::GainAsync"); |
| 258 | |
Tianyu | 5465d6c | 2018-08-14 13:03:10 -0700 | [diff] [blame] | 259 | if (const int error = LocalGain(out_meta, release_fence, gain_posted_buffer)) |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 260 | return error; |
| 261 | |
| 262 | return ReturnStatusOrError(SendImpulse(BufferHubRPC::ProducerGain::Opcode)); |
| 263 | } |
| 264 | |
| 265 | int ProducerBuffer::GainAsync() { |
| 266 | DvrNativeBufferMetadata meta; |
| 267 | LocalHandle fence; |
| 268 | return GainAsync(&meta, &fence); |
| 269 | } |
| 270 | |
| 271 | std::unique_ptr<ProducerBuffer> ProducerBuffer::Import( |
| 272 | LocalChannelHandle channel) { |
| 273 | ALOGD_IF(TRACE, "ProducerBuffer::Import: channel=%d", channel.value()); |
| 274 | return ProducerBuffer::Create(std::move(channel)); |
| 275 | } |
| 276 | |
| 277 | std::unique_ptr<ProducerBuffer> ProducerBuffer::Import( |
| 278 | Status<LocalChannelHandle> status) { |
| 279 | return Import(status ? status.take() |
| 280 | : LocalChannelHandle{nullptr, -status.error()}); |
| 281 | } |
| 282 | |
| 283 | Status<LocalChannelHandle> ProducerBuffer::Detach() { |
Fan Xu | ddb90db | 2018-10-03 10:09:14 -0700 | [diff] [blame] | 284 | // TODO(b/112338294) remove after migrate producer buffer to binder |
| 285 | ALOGW("ProducerBuffer::Detach: not supported operation during migration"); |
| 286 | return {}; |
| 287 | |
Fan Xu | b0eec51 | 2018-10-30 11:33:15 -0700 | [diff] [blame] | 288 | // TODO(b/112338294) Keep here for reference. Remove it after new logic is |
| 289 | // written. |
Tianyu Jiang | e2cdec9 | 2018-10-30 17:40:58 -0700 | [diff] [blame] | 290 | /* uint64_t buffer_state = buffer_state_->load(std::memory_order_acquire); |
Tianyu | f669f6a | 2018-10-10 15:34:32 -0700 | [diff] [blame] | 291 | if (!BufferHubDefs::IsClientGained( |
| 292 | buffer_state, BufferHubDefs::kFirstClientStateMask)) { |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 293 | // Can only detach a ProducerBuffer when it's in gained state. |
| 294 | ALOGW("ProducerBuffer::Detach: The buffer (id=%d, state=0x%" PRIx64 |
| 295 | ") is not in gained state.", |
| 296 | id(), buffer_state); |
| 297 | return {}; |
| 298 | } |
| 299 | |
| 300 | Status<LocalChannelHandle> status = |
| 301 | InvokeRemoteMethod<BufferHubRPC::ProducerBufferDetach>(); |
| 302 | ALOGE_IF(!status, |
| 303 | "ProducerBuffer::Detach: Failed to detach buffer (id=%d): %s.", id(), |
| 304 | status.GetErrorMessage().c_str()); |
Fan Xu | b0eec51 | 2018-10-30 11:33:15 -0700 | [diff] [blame] | 305 | return status; */ |
Jiwen 'Steve' Cai | c6fcf2f | 2018-09-27 23:34:45 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | } // namespace dvr |
| 309 | } // namespace android |