buffer_hub_base.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. #include <poll.h>
  2. #include <sys/epoll.h>
  3. #include <pdx/default_transport/client_channel.h>
  4. #include <pdx/default_transport/client_channel_factory.h>
  5. #include <private/dvr/buffer_hub_base.h>
  6. using android::pdx::LocalChannelHandle;
  7. using android::pdx::LocalHandle;
  8. using android::pdx::Status;
  9. using android::pdx::default_transport::ClientChannel;
  10. using android::pdx::default_transport::ClientChannelFactory;
  11. namespace android {
  12. namespace dvr {
  13. BufferHubBase::BufferHubBase(LocalChannelHandle channel_handle)
  14. : Client{pdx::default_transport::ClientChannel::Create(
  15. std::move(channel_handle))},
  16. id_(-1),
  17. cid_(-1) {}
  18. BufferHubBase::BufferHubBase(const std::string& endpoint_path)
  19. : Client{pdx::default_transport::ClientChannelFactory::Create(
  20. endpoint_path)},
  21. id_(-1),
  22. cid_(-1) {}
  23. BufferHubBase::~BufferHubBase() {
  24. // buffer_state and fence_state are not reset here. They will be used to
  25. // clean up epoll fd if necessary in ProducerChannel::RemoveConsumer method.
  26. if (metadata_header_ != nullptr) {
  27. metadata_buffer_.Unlock();
  28. }
  29. }
  30. Status<LocalChannelHandle> BufferHubBase::CreateConsumer() {
  31. Status<LocalChannelHandle> status =
  32. InvokeRemoteMethod<BufferHubRPC::NewConsumer>();
  33. ALOGE_IF(!status,
  34. "BufferHub::CreateConsumer: Failed to create consumer channel: %s",
  35. status.GetErrorMessage().c_str());
  36. return status;
  37. }
  38. int BufferHubBase::ImportBuffer() {
  39. ATRACE_NAME("BufferHubBase::ImportBuffer");
  40. Status<BufferDescription<LocalHandle>> status =
  41. InvokeRemoteMethod<BufferHubRPC::GetBuffer>();
  42. if (!status) {
  43. ALOGE("BufferHubBase::ImportBuffer: Failed to get buffer: %s",
  44. status.GetErrorMessage().c_str());
  45. return -status.error();
  46. } else if (status.get().id() < 0) {
  47. ALOGE("BufferHubBase::ImportBuffer: Received an invalid id!");
  48. return -EIO;
  49. }
  50. auto buffer_desc = status.take();
  51. // Stash the buffer id to replace the value in id_.
  52. const int new_id = buffer_desc.id();
  53. // Import the buffer.
  54. IonBuffer ion_buffer;
  55. ALOGD_IF(TRACE, "BufferHubBase::ImportBuffer: id=%d.", buffer_desc.id());
  56. if (const int ret = buffer_desc.ImportBuffer(&ion_buffer))
  57. return ret;
  58. // Import the metadata.
  59. IonBuffer metadata_buffer;
  60. if (const int ret = buffer_desc.ImportMetadata(&metadata_buffer)) {
  61. ALOGE("Failed to import metadata buffer, error=%d", ret);
  62. return ret;
  63. }
  64. size_t metadata_buf_size = metadata_buffer.width();
  65. if (metadata_buf_size < BufferHubDefs::kMetadataHeaderSize) {
  66. ALOGE("BufferHubBase::ImportBuffer: metadata buffer too small: %zu",
  67. metadata_buf_size);
  68. return -ENOMEM;
  69. }
  70. // If all imports succee, replace the previous buffer and id.
  71. buffer_ = std::move(ion_buffer);
  72. metadata_buffer_ = std::move(metadata_buffer);
  73. metadata_buf_size_ = metadata_buf_size;
  74. user_metadata_size_ = metadata_buf_size_ - BufferHubDefs::kMetadataHeaderSize;
  75. void* metadata_ptr = nullptr;
  76. if (const int ret =
  77. metadata_buffer_.Lock(BufferHubDefs::kMetadataUsage, /*x=*/0,
  78. /*y=*/0, metadata_buf_size_,
  79. /*height=*/1, &metadata_ptr)) {
  80. ALOGE("BufferHubBase::ImportBuffer: Failed to lock metadata.");
  81. return ret;
  82. }
  83. // Set up shared fences.
  84. shared_acquire_fence_ = buffer_desc.take_acquire_fence();
  85. shared_release_fence_ = buffer_desc.take_release_fence();
  86. if (!shared_acquire_fence_ || !shared_release_fence_) {
  87. ALOGE("BufferHubBase::ImportBuffer: Failed to import shared fences.");
  88. return -EIO;
  89. }
  90. metadata_header_ =
  91. reinterpret_cast<BufferHubDefs::MetadataHeader*>(metadata_ptr);
  92. if (user_metadata_size_) {
  93. user_metadata_ptr_ =
  94. reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(metadata_ptr) +
  95. BufferHubDefs::kMetadataHeaderSize);
  96. } else {
  97. user_metadata_ptr_ = nullptr;
  98. }
  99. id_ = new_id;
  100. cid_ = buffer_desc.buffer_cid();
  101. client_state_mask_ = buffer_desc.client_state_mask();
  102. // Note that here the buffer_state, fence_state and active_clients_bit_mask
  103. // are mapped from shared memory as an atomic object. The std::atomic's
  104. // constructor will not be called so that the original value stored in the
  105. // memory region will be preserved.
  106. buffer_state_ = &metadata_header_->bufferState;
  107. ALOGD_IF(TRACE,
  108. "BufferHubBase::ImportBuffer: id=%d, buffer_state=%" PRIx32 ".",
  109. id(), buffer_state_->load(std::memory_order_acquire));
  110. fence_state_ = &metadata_header_->fenceState;
  111. ALOGD_IF(TRACE,
  112. "BufferHubBase::ImportBuffer: id=%d, fence_state=%" PRIx32 ".", id(),
  113. fence_state_->load(std::memory_order_acquire));
  114. active_clients_bit_mask_ = &metadata_header_->activeClientsBitMask;
  115. ALOGD_IF(
  116. TRACE,
  117. "BufferHubBase::ImportBuffer: id=%d, active_clients_bit_mask=%" PRIx32
  118. ".",
  119. id(), active_clients_bit_mask_->load(std::memory_order_acquire));
  120. return 0;
  121. }
  122. int BufferHubBase::CheckMetadata(size_t user_metadata_size) const {
  123. if (user_metadata_size && !user_metadata_ptr_) {
  124. ALOGE("BufferHubBase::CheckMetadata: doesn't support custom metadata.");
  125. return -EINVAL;
  126. }
  127. if (user_metadata_size > user_metadata_size_) {
  128. ALOGE("BufferHubBase::CheckMetadata: too big: %zu, maximum: %zu.",
  129. user_metadata_size, user_metadata_size_);
  130. return -E2BIG;
  131. }
  132. return 0;
  133. }
  134. int BufferHubBase::UpdateSharedFence(const LocalHandle& new_fence,
  135. const LocalHandle& shared_fence) {
  136. if (pending_fence_fd_.Get() != new_fence.Get()) {
  137. // First, replace the old fd if there was already one. Skipping if the new
  138. // one is the same as the old.
  139. if (pending_fence_fd_.IsValid()) {
  140. const int ret = epoll_ctl(shared_fence.Get(), EPOLL_CTL_DEL,
  141. pending_fence_fd_.Get(), nullptr);
  142. ALOGW_IF(ret,
  143. "BufferHubBase::UpdateSharedFence: failed to remove old fence "
  144. "fd from epoll set, error: %s.",
  145. strerror(errno));
  146. }
  147. if (new_fence.IsValid()) {
  148. // If ready fence is valid, we put that into the epoll set.
  149. epoll_event event;
  150. event.events = EPOLLIN;
  151. event.data.u32 = client_state_mask();
  152. pending_fence_fd_ = new_fence.Duplicate();
  153. if (epoll_ctl(shared_fence.Get(), EPOLL_CTL_ADD, pending_fence_fd_.Get(),
  154. &event) < 0) {
  155. const int error = errno;
  156. ALOGE(
  157. "BufferHubBase::UpdateSharedFence: failed to add new fence fd "
  158. "into epoll set, error: %s.",
  159. strerror(error));
  160. return -error;
  161. }
  162. // Set bit in fence state to indicate that there is a fence from this
  163. // producer or consumer.
  164. fence_state_->fetch_or(client_state_mask());
  165. } else {
  166. // Unset bit in fence state to indicate that there is no fence, so that
  167. // when consumer to acquire or producer to acquire, it knows no need to
  168. // check fence for this buffer.
  169. fence_state_->fetch_and(~client_state_mask());
  170. }
  171. }
  172. return 0;
  173. }
  174. int BufferHubBase::Lock(int usage, int x, int y, int width, int height,
  175. void** address) {
  176. return buffer_.Lock(usage, x, y, width, height, address);
  177. }
  178. int BufferHubBase::Unlock() { return buffer_.Unlock(); }
  179. int BufferHubBase::GetBlobReadWritePointer(size_t size, void** addr) {
  180. int width = static_cast<int>(size);
  181. int height = 1;
  182. int ret = Lock(usage(), 0, 0, width, height, addr);
  183. if (ret == 0)
  184. Unlock();
  185. return ret;
  186. }
  187. } // namespace dvr
  188. } // namespace android