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