• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
ProducerBuffer(uint32_t width,uint32_t height,uint32_t format,uint64_t usage,size_t user_metadata_size)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 
ProducerBuffer(uint64_t usage,size_t size)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 
ProducerBuffer(LocalChannelHandle channel)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 
LocalPost(const DvrNativeBufferMetadata * meta,const LocalHandle & ready_fence)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 
82   // The buffer can be posted iff the buffer state for this client is gained.
83   uint32_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=%" PRIx32 ".", __FUNCTION__, id(),
88           current_buffer_state);
89     return -EBUSY;
90   }
91 
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;
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 "
101         "%" PRIx32
102         " when trying to post the buffer and modify the buffer state to "
103         "%" PRIx32
104         ". About to try again if the buffer is still gained by this client.",
105         __FUNCTION__, current_buffer_state, updated_buffer_state);
106     if (!BufferHubDefs::isClientGained(current_buffer_state,
107                                        client_state_mask())) {
108       ALOGE(
109           "%s: Failed to post the buffer. The buffer is no longer gained, "
110           "id=%d state=%" PRIx32 ".",
111           __FUNCTION__, id(), current_buffer_state);
112       return -EBUSY;
113     }
114   }
115 
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 
130   return 0;
131 }
132 
Post(const LocalHandle & ready_fence,const void * meta,size_t user_metadata_size)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 
PostAsync(const DvrNativeBufferMetadata * meta,const LocalHandle & ready_fence)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 
LocalGain(DvrNativeBufferMetadata * out_meta,LocalHandle * out_fence,bool gain_posted_buffer)159 int ProducerBuffer::LocalGain(DvrNativeBufferMetadata* out_meta,
160                               LocalHandle* out_fence, bool gain_posted_buffer) {
161   if (!out_meta)
162     return -EINVAL;
163 
164   uint32_t current_buffer_state =
165       buffer_state_->load(std::memory_order_acquire);
166   ALOGD_IF(TRACE, "%s: buffer=%d, state=%" PRIx32 ".", __FUNCTION__, id(),
167            current_buffer_state);
168 
169   if (BufferHubDefs::isClientGained(current_buffer_state,
170                                     client_state_mask())) {
171     ALOGV("%s: already gained id=%d.", __FUNCTION__, id());
172     return 0;
173   }
174   if (BufferHubDefs::isAnyClientAcquired(current_buffer_state) ||
175       BufferHubDefs::isAnyClientGained(current_buffer_state) ||
176       (BufferHubDefs::isAnyClientPosted(
177            current_buffer_state &
178            active_clients_bit_mask_->load(std::memory_order_acquire)) &&
179        !gain_posted_buffer)) {
180     ALOGE("%s: not released id=%d state=%" PRIx32 ".", __FUNCTION__, id(),
181           current_buffer_state);
182     return -EBUSY;
183   }
184   // Change the buffer state to gained state.
185   uint32_t updated_buffer_state = client_state_mask();
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 "
191         "%" PRIx32
192         " when trying to gain the buffer and modify the buffer state to "
193         "%" PRIx32
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 
198     if (BufferHubDefs::isAnyClientAcquired(current_buffer_state) ||
199         BufferHubDefs::isAnyClientGained(current_buffer_state) ||
200         (BufferHubDefs::isAnyClientPosted(
201              current_buffer_state &
202              active_clients_bit_mask_->load(std::memory_order_acquire)) &&
203          !gain_posted_buffer)) {
204       ALOGE(
205           "%s: Failed to gain the buffer. The buffer is no longer released. "
206           "id=%d state=%" PRIx32 ".",
207           __FUNCTION__, id(), current_buffer_state);
208       return -EBUSY;
209     }
210   }
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 
225   uint32_t current_fence_state = fence_state_->load(std::memory_order_acquire);
226   uint32_t current_active_clients_bit_mask =
227       active_clients_bit_mask_->load(std::memory_order_acquire);
228   // If there are release fence(s) from consumer(s), we need to return it to the
229   // consumer(s).
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)) {
235     *out_fence = shared_release_fence_.Duplicate();
236     out_meta->release_fence_mask = current_fence_state &
237                                    current_active_clients_bit_mask &
238                                    (~BufferHubDefs::kFirstClientBitMask);
239   }
240 
241   return 0;
242 }
243 
Gain(LocalHandle * release_fence,bool gain_posted_buffer)244 int ProducerBuffer::Gain(LocalHandle* release_fence, bool gain_posted_buffer) {
245   ATRACE_NAME("ProducerBuffer::Gain");
246 
247   DvrNativeBufferMetadata meta;
248   if (const int error = LocalGain(&meta, release_fence, gain_posted_buffer))
249     return error;
250 
251   auto status = InvokeRemoteMethod<BufferHubRPC::ProducerGain>();
252   if (!status)
253     return -status.error();
254   return 0;
255 }
256 
GainAsync(DvrNativeBufferMetadata * out_meta,LocalHandle * release_fence,bool gain_posted_buffer)257 int ProducerBuffer::GainAsync(DvrNativeBufferMetadata* out_meta,
258                               LocalHandle* release_fence,
259                               bool gain_posted_buffer) {
260   ATRACE_NAME("ProducerBuffer::GainAsync");
261 
262   if (const int error = LocalGain(out_meta, release_fence, gain_posted_buffer))
263     return error;
264 
265   return ReturnStatusOrError(SendImpulse(BufferHubRPC::ProducerGain::Opcode));
266 }
267 
GainAsync()268 int ProducerBuffer::GainAsync() {
269   DvrNativeBufferMetadata meta;
270   LocalHandle fence;
271   return GainAsync(&meta, &fence);
272 }
273 
Import(LocalChannelHandle channel)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 
Import(Status<LocalChannelHandle> status)280 std::unique_ptr<ProducerBuffer> ProducerBuffer::Import(
281     Status<LocalChannelHandle> status) {
282   return Import(status ? status.take()
283                        : LocalChannelHandle{nullptr, -status.error()});
284 }
285 
Detach()286 Status<LocalChannelHandle> ProducerBuffer::Detach() {
287   // TODO(b/112338294) remove after migrate producer buffer to binder
288   ALOGW("ProducerBuffer::Detach: not supported operation during migration");
289   return {};
290 
291   // TODO(b/112338294) Keep here for reference. Remove it after new logic is
292   // written.
293   /* uint32_t buffer_state = buffer_state_->load(std::memory_order_acquire);
294   if (!BufferHubDefs::isClientGained(
295       buffer_state, BufferHubDefs::kFirstClientStateMask)) {
296     // Can only detach a ProducerBuffer when it's in gained state.
297     ALOGW("ProducerBuffer::Detach: The buffer (id=%d, state=0x%" PRIx32
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());
308   return status; */
309 }
310 
311 }  // namespace dvr
312 }  // namespace android
313