consumer_buffer.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include <private/dvr/consumer_buffer.h>
  2. using android::pdx::LocalChannelHandle;
  3. using android::pdx::LocalHandle;
  4. using android::pdx::Status;
  5. namespace android {
  6. namespace dvr {
  7. ConsumerBuffer::ConsumerBuffer(LocalChannelHandle channel)
  8. : BASE(std::move(channel)) {
  9. const int ret = ImportBuffer();
  10. if (ret < 0) {
  11. ALOGE("ConsumerBuffer::ConsumerBuffer: Failed to import buffer: %s",
  12. strerror(-ret));
  13. Close(ret);
  14. }
  15. }
  16. std::unique_ptr<ConsumerBuffer> ConsumerBuffer::Import(
  17. LocalChannelHandle channel) {
  18. ATRACE_NAME("ConsumerBuffer::Import");
  19. ALOGD_IF(TRACE, "ConsumerBuffer::Import: channel=%d", channel.value());
  20. return ConsumerBuffer::Create(std::move(channel));
  21. }
  22. std::unique_ptr<ConsumerBuffer> ConsumerBuffer::Import(
  23. Status<LocalChannelHandle> status) {
  24. return Import(status ? status.take()
  25. : LocalChannelHandle{nullptr, -status.error()});
  26. }
  27. int ConsumerBuffer::LocalAcquire(DvrNativeBufferMetadata* out_meta,
  28. LocalHandle* out_fence) {
  29. if (!out_meta)
  30. return -EINVAL;
  31. // The buffer can be acquired iff the buffer state for this client is posted.
  32. uint32_t current_buffer_state =
  33. buffer_state_->load(std::memory_order_acquire);
  34. if (!BufferHubDefs::isClientPosted(current_buffer_state,
  35. client_state_mask())) {
  36. ALOGE(
  37. "%s: Failed to acquire the buffer. The buffer is not posted, id=%d "
  38. "state=%" PRIx32 " client_state_mask=%" PRIx32 ".",
  39. __FUNCTION__, id(), current_buffer_state, client_state_mask());
  40. return -EBUSY;
  41. }
  42. // Change the buffer state for this consumer from posted to acquired.
  43. uint32_t updated_buffer_state = current_buffer_state ^ client_state_mask();
  44. while (!buffer_state_->compare_exchange_weak(
  45. current_buffer_state, updated_buffer_state, std::memory_order_acq_rel,
  46. std::memory_order_acquire)) {
  47. ALOGD(
  48. "%s Failed to acquire the buffer. Current buffer state was changed to "
  49. "%" PRIx32
  50. " when trying to acquire the buffer and modify the buffer state to "
  51. "%" PRIx32 ". About to try again if the buffer is still posted.",
  52. __FUNCTION__, current_buffer_state, updated_buffer_state);
  53. if (!BufferHubDefs::isClientPosted(current_buffer_state,
  54. client_state_mask())) {
  55. ALOGE(
  56. "%s: Failed to acquire the buffer. The buffer is no longer posted, "
  57. "id=%d state=%" PRIx32 " client_state_mask=%" PRIx32 ".",
  58. __FUNCTION__, id(), current_buffer_state, client_state_mask());
  59. return -EBUSY;
  60. }
  61. // The failure of compare_exchange_weak updates current_buffer_state.
  62. updated_buffer_state = current_buffer_state ^ client_state_mask();
  63. }
  64. // Copy the canonical metadata.
  65. void* metadata_ptr = reinterpret_cast<void*>(&metadata_header_->metadata);
  66. memcpy(out_meta, metadata_ptr, sizeof(DvrNativeBufferMetadata));
  67. // Fill in the user_metadata_ptr in address space of the local process.
  68. if (out_meta->user_metadata_size) {
  69. out_meta->user_metadata_ptr =
  70. reinterpret_cast<uint64_t>(user_metadata_ptr_);
  71. } else {
  72. out_meta->user_metadata_ptr = 0;
  73. }
  74. uint32_t fence_state = fence_state_->load(std::memory_order_acquire);
  75. // If there is an acquire fence from producer, we need to return it.
  76. // The producer state bit mask is kFirstClientBitMask for now.
  77. if (fence_state & BufferHubDefs::kFirstClientBitMask) {
  78. *out_fence = shared_acquire_fence_.Duplicate();
  79. }
  80. return 0;
  81. }
  82. int ConsumerBuffer::Acquire(LocalHandle* ready_fence) {
  83. return Acquire(ready_fence, nullptr, 0);
  84. }
  85. int ConsumerBuffer::Acquire(LocalHandle* ready_fence, void* meta,
  86. size_t user_metadata_size) {
  87. ATRACE_NAME("ConsumerBuffer::Acquire");
  88. if (const int error = CheckMetadata(user_metadata_size))
  89. return error;
  90. DvrNativeBufferMetadata canonical_meta;
  91. if (const int error = LocalAcquire(&canonical_meta, ready_fence))
  92. return error;
  93. if (meta && user_metadata_size) {
  94. void* metadata_src =
  95. reinterpret_cast<void*>(canonical_meta.user_metadata_ptr);
  96. if (metadata_src) {
  97. memcpy(meta, metadata_src, user_metadata_size);
  98. } else {
  99. ALOGW("ConsumerBuffer::Acquire: no user-defined metadata.");
  100. }
  101. }
  102. auto status = InvokeRemoteMethod<BufferHubRPC::ConsumerAcquire>();
  103. if (!status)
  104. return -status.error();
  105. return 0;
  106. }
  107. int ConsumerBuffer::AcquireAsync(DvrNativeBufferMetadata* out_meta,
  108. LocalHandle* out_fence) {
  109. ATRACE_NAME("ConsumerBuffer::AcquireAsync");
  110. if (const int error = LocalAcquire(out_meta, out_fence))
  111. return error;
  112. auto status = SendImpulse(BufferHubRPC::ConsumerAcquire::Opcode);
  113. if (!status)
  114. return -status.error();
  115. return 0;
  116. }
  117. int ConsumerBuffer::LocalRelease(const DvrNativeBufferMetadata* meta,
  118. const LocalHandle& release_fence) {
  119. if (const int error = CheckMetadata(meta->user_metadata_size))
  120. return error;
  121. // Set the buffer state of this client to released if it is not already in
  122. // released state.
  123. uint32_t current_buffer_state =
  124. buffer_state_->load(std::memory_order_acquire);
  125. if (BufferHubDefs::isClientReleased(current_buffer_state,
  126. client_state_mask())) {
  127. return 0;
  128. }
  129. uint32_t updated_buffer_state = current_buffer_state & (~client_state_mask());
  130. while (!buffer_state_->compare_exchange_weak(
  131. current_buffer_state, updated_buffer_state, std::memory_order_acq_rel,
  132. std::memory_order_acquire)) {
  133. ALOGD(
  134. "%s: Failed to release the buffer. Current buffer state was changed to "
  135. "%" PRIx32
  136. " when trying to release the buffer and modify the buffer state to "
  137. "%" PRIx32 ". About to try again.",
  138. __FUNCTION__, current_buffer_state, updated_buffer_state);
  139. // The failure of compare_exchange_weak updates current_buffer_state.
  140. updated_buffer_state = current_buffer_state & (~client_state_mask());
  141. }
  142. // On release, only the user requested metadata is copied back into the shared
  143. // memory for metadata. Since there are multiple consumers, it doesn't make
  144. // sense to send the canonical metadata back to the producer. However, one of
  145. // the consumer can still choose to write up to user_metadata_size bytes of
  146. // data into user_metadata_ptr.
  147. if (meta->user_metadata_ptr && meta->user_metadata_size) {
  148. void* metadata_src = reinterpret_cast<void*>(meta->user_metadata_ptr);
  149. memcpy(user_metadata_ptr_, metadata_src, meta->user_metadata_size);
  150. }
  151. // Send out the release fence through the shared epoll fd. Note that during
  152. // releasing the producer is not expected to be polling on the fence.
  153. if (const int error = UpdateSharedFence(release_fence, shared_release_fence_))
  154. return error;
  155. return 0;
  156. }
  157. int ConsumerBuffer::Release(const LocalHandle& release_fence) {
  158. ATRACE_NAME("ConsumerBuffer::Release");
  159. DvrNativeBufferMetadata meta;
  160. if (const int error = LocalRelease(&meta, release_fence))
  161. return error;
  162. return ReturnStatusOrError(InvokeRemoteMethod<BufferHubRPC::ConsumerRelease>(
  163. BorrowedFence(release_fence.Borrow())));
  164. }
  165. int ConsumerBuffer::ReleaseAsync() {
  166. DvrNativeBufferMetadata meta;
  167. return ReleaseAsync(&meta, LocalHandle());
  168. }
  169. int ConsumerBuffer::ReleaseAsync(const DvrNativeBufferMetadata* meta,
  170. const LocalHandle& release_fence) {
  171. ATRACE_NAME("ConsumerBuffer::ReleaseAsync");
  172. if (const int error = LocalRelease(meta, release_fence))
  173. return error;
  174. return ReturnStatusOrError(
  175. SendImpulse(BufferHubRPC::ConsumerRelease::Opcode));
  176. }
  177. int ConsumerBuffer::Discard() { return Release(LocalHandle()); }
  178. } // namespace dvr
  179. } // namespace android