blob: de03fad01f53059699e360c794cc2d6f1f6124a3 [file] [log] [blame]
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -07001#include <private/dvr/producer_buffer.h>
2
3using android::pdx::LocalChannelHandle;
4using android::pdx::LocalHandle;
5using android::pdx::Status;
6
7namespace android {
8namespace dvr {
9
10ProducerBuffer::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
38ProducerBuffer::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
66ProducerBuffer::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
77int ProducerBuffer::LocalPost(const DvrNativeBufferMetadata* meta,
78 const LocalHandle& ready_fence) {
79 if (const int error = CheckMetadata(meta->user_metadata_size))
80 return error;
81
Tianyuf669f6a2018-10-10 15:34:32 -070082 // The buffer can be posted iff the buffer state for this client is gained.
83 uint64_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=%" PRIx64 ".", __FUNCTION__, id(),
88 current_buffer_state);
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -070089 return -EBUSY;
90 }
91
Tianyuf669f6a2018-10-10 15:34:32 -070092 // Set the producer client buffer state to released, other clients' buffer
93 // state to posted.
94 uint64_t current_active_clients_bit_mask =
95 active_clients_bit_mask_->load(std::memory_order_acquire);
96 uint64_t updated_buffer_state = current_active_clients_bit_mask &
97 (~client_state_mask()) &
98 BufferHubDefs::kHighBitsMask;
99 while (!buffer_state_->compare_exchange_weak(
100 current_buffer_state, updated_buffer_state, std::memory_order_acq_rel,
101 std::memory_order_acquire)) {
102 ALOGD(
103 "%s: Failed to post the buffer. Current buffer state was changed to "
104 "%" PRIx64
105 " when trying to post the buffer and modify the buffer state to "
106 "%" PRIx64
107 ". About to try again if the buffer is still gained by this client.",
108 __FUNCTION__, current_buffer_state, updated_buffer_state);
109 if (!BufferHubDefs::IsClientGained(current_buffer_state,
110 client_state_mask())) {
111 ALOGE(
112 "%s: Failed to post the buffer. The buffer is no longer gained, "
113 "id=%d state=%" PRIx64 ".",
114 __FUNCTION__, id(), current_buffer_state);
115 return -EBUSY;
116 }
117 }
118
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700119 // Copy the canonical metadata.
120 void* metadata_ptr = reinterpret_cast<void*>(&metadata_header_->metadata);
121 memcpy(metadata_ptr, meta, sizeof(DvrNativeBufferMetadata));
122 // Copy extra user requested metadata.
123 if (meta->user_metadata_ptr && meta->user_metadata_size) {
124 void* metadata_src = reinterpret_cast<void*>(meta->user_metadata_ptr);
125 memcpy(user_metadata_ptr_, metadata_src, meta->user_metadata_size);
126 }
127
128 // Send out the acquire fence through the shared epoll fd. Note that during
129 // posting no consumer is not expected to be polling on the fence.
130 if (const int error = UpdateSharedFence(ready_fence, shared_acquire_fence_))
131 return error;
132
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700133 return 0;
134}
135
136int ProducerBuffer::Post(const LocalHandle& ready_fence, const void* meta,
137 size_t user_metadata_size) {
138 ATRACE_NAME("ProducerBuffer::Post");
139
140 // Populate cononical metadata for posting.
141 DvrNativeBufferMetadata canonical_meta;
142 canonical_meta.user_metadata_ptr = reinterpret_cast<uint64_t>(meta);
143 canonical_meta.user_metadata_size = user_metadata_size;
144
145 if (const int error = LocalPost(&canonical_meta, ready_fence))
146 return error;
147
148 return ReturnStatusOrError(InvokeRemoteMethod<BufferHubRPC::ProducerPost>(
149 BorrowedFence(ready_fence.Borrow())));
150}
151
152int ProducerBuffer::PostAsync(const DvrNativeBufferMetadata* meta,
153 const LocalHandle& ready_fence) {
154 ATRACE_NAME("ProducerBuffer::PostAsync");
155
156 if (const int error = LocalPost(meta, ready_fence))
157 return error;
158
159 return ReturnStatusOrError(SendImpulse(BufferHubRPC::ProducerPost::Opcode));
160}
161
162int ProducerBuffer::LocalGain(DvrNativeBufferMetadata* out_meta,
Tianyu5465d6c2018-08-14 13:03:10 -0700163 LocalHandle* out_fence, bool gain_posted_buffer) {
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700164 if (!out_meta)
165 return -EINVAL;
166
Tianyuf669f6a2018-10-10 15:34:32 -0700167 uint64_t current_buffer_state =
168 buffer_state_->load(std::memory_order_acquire);
169 ALOGD_IF(TRACE, "%s: buffer=%d, state=%" PRIx64 ".", __FUNCTION__, id(),
170 current_buffer_state);
171
172 if (BufferHubDefs::IsClientGained(current_buffer_state,
173 client_state_mask())) {
Tianyu Jiang8cfd42a2018-12-04 14:20:10 -0800174 ALOGV("%s: already gained id=%d.", __FUNCTION__, id());
Tianyu Jiang60887c92018-11-14 15:52:38 -0800175 return 0;
Tianyu5465d6c2018-08-14 13:03:10 -0700176 }
Tianyuf669f6a2018-10-10 15:34:32 -0700177 if (BufferHubDefs::AnyClientAcquired(current_buffer_state) ||
Tianyu Jiang60887c92018-11-14 15:52:38 -0800178 BufferHubDefs::AnyClientGained(current_buffer_state) ||
Tianyuf669f6a2018-10-10 15:34:32 -0700179 (BufferHubDefs::AnyClientPosted(current_buffer_state) &&
180 !gain_posted_buffer)) {
181 ALOGE("%s: not released id=%d state=%" PRIx64 ".", __FUNCTION__, id(),
182 current_buffer_state);
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700183 return -EBUSY;
184 }
Tianyuf669f6a2018-10-10 15:34:32 -0700185 // Change the buffer state to gained state.
186 uint64_t updated_buffer_state = client_state_mask();
187 while (!buffer_state_->compare_exchange_weak(
188 current_buffer_state, updated_buffer_state, std::memory_order_acq_rel,
189 std::memory_order_acquire)) {
190 ALOGD(
191 "%s: Failed to gain the buffer. Current buffer state was changed to "
192 "%" PRIx64
193 " when trying to gain the buffer and modify the buffer state to "
194 "%" PRIx64
195 ". About to try again if the buffer is still not read by other "
196 "clients.",
197 __FUNCTION__, current_buffer_state, updated_buffer_state);
198
199 if (BufferHubDefs::AnyClientAcquired(current_buffer_state) ||
Tianyu Jiang60887c92018-11-14 15:52:38 -0800200 BufferHubDefs::AnyClientGained(current_buffer_state) ||
Tianyuf669f6a2018-10-10 15:34:32 -0700201 (BufferHubDefs::AnyClientPosted(current_buffer_state) &&
202 !gain_posted_buffer)) {
203 ALOGE(
204 "%s: Failed to gain the buffer. The buffer is no longer released. "
205 "id=%d state=%" PRIx64 ".",
206 __FUNCTION__, id(), current_buffer_state);
207 return -EBUSY;
208 }
209 }
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700210
211 // Canonical metadata is undefined on Gain. Except for user_metadata and
212 // release_fence_mask. Fill in the user_metadata_ptr in address space of the
213 // local process.
214 if (metadata_header_->metadata.user_metadata_size && user_metadata_ptr_) {
215 out_meta->user_metadata_size =
216 metadata_header_->metadata.user_metadata_size;
217 out_meta->user_metadata_ptr =
218 reinterpret_cast<uint64_t>(user_metadata_ptr_);
219 } else {
220 out_meta->user_metadata_size = 0;
221 out_meta->user_metadata_ptr = 0;
222 }
223
Tianyuf669f6a2018-10-10 15:34:32 -0700224 uint64_t current_fence_state = fence_state_->load(std::memory_order_acquire);
225 uint64_t current_active_clients_bit_mask =
226 active_clients_bit_mask_->load(std::memory_order_acquire);
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700227 // If there is an release fence from consumer, we need to return it.
Tianyuf669f6a2018-10-10 15:34:32 -0700228 // TODO(b/112007999) add an atomic variable in metadata header in shared
229 // memory to indicate which client is the last producer of the buffer.
230 // Currently, assume the first client is the only producer to the buffer.
231 if (current_fence_state & current_active_clients_bit_mask &
232 (~BufferHubDefs::kFirstClientBitMask)) {
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700233 *out_fence = shared_release_fence_.Duplicate();
234 out_meta->release_fence_mask =
Tianyuf669f6a2018-10-10 15:34:32 -0700235 current_fence_state & current_active_clients_bit_mask;
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700236 }
237
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700238 return 0;
239}
240
Tianyu5465d6c2018-08-14 13:03:10 -0700241int ProducerBuffer::Gain(LocalHandle* release_fence, bool gain_posted_buffer) {
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700242 ATRACE_NAME("ProducerBuffer::Gain");
243
244 DvrNativeBufferMetadata meta;
Tianyu5465d6c2018-08-14 13:03:10 -0700245 if (const int error = LocalGain(&meta, release_fence, gain_posted_buffer))
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700246 return error;
247
248 auto status = InvokeRemoteMethod<BufferHubRPC::ProducerGain>();
249 if (!status)
250 return -status.error();
251 return 0;
252}
253
254int ProducerBuffer::GainAsync(DvrNativeBufferMetadata* out_meta,
Tianyu5465d6c2018-08-14 13:03:10 -0700255 LocalHandle* release_fence,
256 bool gain_posted_buffer) {
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700257 ATRACE_NAME("ProducerBuffer::GainAsync");
258
Tianyu5465d6c2018-08-14 13:03:10 -0700259 if (const int error = LocalGain(out_meta, release_fence, gain_posted_buffer))
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700260 return error;
261
262 return ReturnStatusOrError(SendImpulse(BufferHubRPC::ProducerGain::Opcode));
263}
264
265int ProducerBuffer::GainAsync() {
266 DvrNativeBufferMetadata meta;
267 LocalHandle fence;
268 return GainAsync(&meta, &fence);
269}
270
271std::unique_ptr<ProducerBuffer> ProducerBuffer::Import(
272 LocalChannelHandle channel) {
273 ALOGD_IF(TRACE, "ProducerBuffer::Import: channel=%d", channel.value());
274 return ProducerBuffer::Create(std::move(channel));
275}
276
277std::unique_ptr<ProducerBuffer> ProducerBuffer::Import(
278 Status<LocalChannelHandle> status) {
279 return Import(status ? status.take()
280 : LocalChannelHandle{nullptr, -status.error()});
281}
282
283Status<LocalChannelHandle> ProducerBuffer::Detach() {
Fan Xuddb90db2018-10-03 10:09:14 -0700284 // TODO(b/112338294) remove after migrate producer buffer to binder
285 ALOGW("ProducerBuffer::Detach: not supported operation during migration");
286 return {};
287
Fan Xub0eec512018-10-30 11:33:15 -0700288 // TODO(b/112338294) Keep here for reference. Remove it after new logic is
289 // written.
Tianyu Jiange2cdec92018-10-30 17:40:58 -0700290 /* uint64_t buffer_state = buffer_state_->load(std::memory_order_acquire);
Tianyuf669f6a2018-10-10 15:34:32 -0700291 if (!BufferHubDefs::IsClientGained(
292 buffer_state, BufferHubDefs::kFirstClientStateMask)) {
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700293 // Can only detach a ProducerBuffer when it's in gained state.
294 ALOGW("ProducerBuffer::Detach: The buffer (id=%d, state=0x%" PRIx64
295 ") is not in gained state.",
296 id(), buffer_state);
297 return {};
298 }
299
300 Status<LocalChannelHandle> status =
301 InvokeRemoteMethod<BufferHubRPC::ProducerBufferDetach>();
302 ALOGE_IF(!status,
303 "ProducerBuffer::Detach: Failed to detach buffer (id=%d): %s.", id(),
304 status.GetErrorMessage().c_str());
Fan Xub0eec512018-10-30 11:33:15 -0700305 return status; */
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700306}
307
308} // namespace dvr
309} // namespace android