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