blob: 1bfdc8f2143e61893fedd32705ad55d83e21ca02 [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())) {
174 ALOGI("%s: already gained id=%d.", __FUNCTION__, id());
Tianyu5465d6c2018-08-14 13:03:10 -0700175 return -EALREADY;
176 }
Tianyuf669f6a2018-10-10 15:34:32 -0700177 if (BufferHubDefs::AnyClientAcquired(current_buffer_state) ||
178 (BufferHubDefs::AnyClientPosted(current_buffer_state) &&
179 !gain_posted_buffer)) {
180 ALOGE("%s: not released id=%d state=%" PRIx64 ".", __FUNCTION__, id(),
181 current_buffer_state);
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700182 return -EBUSY;
183 }
Tianyuf669f6a2018-10-10 15:34:32 -0700184 // Change the buffer state to gained state.
185 uint64_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 "%" PRIx64
192 " when trying to gain the buffer and modify the buffer state to "
193 "%" PRIx64
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::AnyClientAcquired(current_buffer_state) ||
199 (BufferHubDefs::AnyClientPosted(current_buffer_state) &&
200 !gain_posted_buffer)) {
201 ALOGE(
202 "%s: Failed to gain the buffer. The buffer is no longer released. "
203 "id=%d state=%" PRIx64 ".",
204 __FUNCTION__, id(), current_buffer_state);
205 return -EBUSY;
206 }
207 }
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700208
209 // Canonical metadata is undefined on Gain. Except for user_metadata and
210 // release_fence_mask. Fill in the user_metadata_ptr in address space of the
211 // local process.
212 if (metadata_header_->metadata.user_metadata_size && user_metadata_ptr_) {
213 out_meta->user_metadata_size =
214 metadata_header_->metadata.user_metadata_size;
215 out_meta->user_metadata_ptr =
216 reinterpret_cast<uint64_t>(user_metadata_ptr_);
217 } else {
218 out_meta->user_metadata_size = 0;
219 out_meta->user_metadata_ptr = 0;
220 }
221
Tianyuf669f6a2018-10-10 15:34:32 -0700222 uint64_t current_fence_state = fence_state_->load(std::memory_order_acquire);
223 uint64_t current_active_clients_bit_mask =
224 active_clients_bit_mask_->load(std::memory_order_acquire);
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700225 // If there is an release fence from consumer, we need to return it.
Tianyuf669f6a2018-10-10 15:34:32 -0700226 // TODO(b/112007999) add an atomic variable in metadata header in shared
227 // memory to indicate which client is the last producer of the buffer.
228 // Currently, assume the first client is the only producer to the buffer.
229 if (current_fence_state & current_active_clients_bit_mask &
230 (~BufferHubDefs::kFirstClientBitMask)) {
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700231 *out_fence = shared_release_fence_.Duplicate();
232 out_meta->release_fence_mask =
Tianyuf669f6a2018-10-10 15:34:32 -0700233 current_fence_state & current_active_clients_bit_mask;
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700234 }
235
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700236 return 0;
237}
238
Tianyu5465d6c2018-08-14 13:03:10 -0700239int ProducerBuffer::Gain(LocalHandle* release_fence, bool gain_posted_buffer) {
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700240 ATRACE_NAME("ProducerBuffer::Gain");
241
242 DvrNativeBufferMetadata meta;
Tianyu5465d6c2018-08-14 13:03:10 -0700243 if (const int error = LocalGain(&meta, release_fence, gain_posted_buffer))
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700244 return error;
245
246 auto status = InvokeRemoteMethod<BufferHubRPC::ProducerGain>();
247 if (!status)
248 return -status.error();
249 return 0;
250}
251
252int ProducerBuffer::GainAsync(DvrNativeBufferMetadata* out_meta,
Tianyu5465d6c2018-08-14 13:03:10 -0700253 LocalHandle* release_fence,
254 bool gain_posted_buffer) {
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700255 ATRACE_NAME("ProducerBuffer::GainAsync");
256
Tianyu5465d6c2018-08-14 13:03:10 -0700257 if (const int error = LocalGain(out_meta, release_fence, gain_posted_buffer))
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700258 return error;
259
260 return ReturnStatusOrError(SendImpulse(BufferHubRPC::ProducerGain::Opcode));
261}
262
263int ProducerBuffer::GainAsync() {
264 DvrNativeBufferMetadata meta;
265 LocalHandle fence;
266 return GainAsync(&meta, &fence);
267}
268
269std::unique_ptr<ProducerBuffer> ProducerBuffer::Import(
270 LocalChannelHandle channel) {
271 ALOGD_IF(TRACE, "ProducerBuffer::Import: channel=%d", channel.value());
272 return ProducerBuffer::Create(std::move(channel));
273}
274
275std::unique_ptr<ProducerBuffer> ProducerBuffer::Import(
276 Status<LocalChannelHandle> status) {
277 return Import(status ? status.take()
278 : LocalChannelHandle{nullptr, -status.error()});
279}
280
281Status<LocalChannelHandle> ProducerBuffer::Detach() {
Fan Xuddb90db2018-10-03 10:09:14 -0700282 // TODO(b/112338294) remove after migrate producer buffer to binder
283 ALOGW("ProducerBuffer::Detach: not supported operation during migration");
284 return {};
285
Fan Xub0eec512018-10-30 11:33:15 -0700286 // TODO(b/112338294) Keep here for reference. Remove it after new logic is
287 // written.
Tianyu Jiange2cdec92018-10-30 17:40:58 -0700288 /* uint64_t buffer_state = buffer_state_->load(std::memory_order_acquire);
Tianyuf669f6a2018-10-10 15:34:32 -0700289 if (!BufferHubDefs::IsClientGained(
290 buffer_state, BufferHubDefs::kFirstClientStateMask)) {
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700291 // Can only detach a ProducerBuffer when it's in gained state.
292 ALOGW("ProducerBuffer::Detach: The buffer (id=%d, state=0x%" PRIx64
293 ") is not in gained state.",
294 id(), buffer_state);
295 return {};
296 }
297
298 Status<LocalChannelHandle> status =
299 InvokeRemoteMethod<BufferHubRPC::ProducerBufferDetach>();
300 ALOGE_IF(!status,
301 "ProducerBuffer::Detach: Failed to detach buffer (id=%d): %s.", id(),
302 status.GetErrorMessage().c_str());
Fan Xub0eec512018-10-30 11:33:15 -0700303 return status; */
Jiwen 'Steve' Caic6fcf2f2018-09-27 23:34:45 -0700304}
305
306} // namespace dvr
307} // namespace android