1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "C2BqBuffer"
19 #include <android/hardware_buffer.h>
20 #include <utils/Log.h>
21
22 #include <ui/BufferQueueDefs.h>
23 #include <ui/GraphicBuffer.h>
24 #include <ui/Fence.h>
25
26 #include <types.h>
27
28 #include <hidl/HidlSupport.h>
29
30 #include <C2AllocatorGralloc.h>
31 #include <C2BqBufferPriv.h>
32 #include <C2BlockInternal.h>
33 #include <C2FenceFactory.h>
34 #include <C2SurfaceSyncObj.h>
35
36 #include <list>
37 #include <map>
38 #include <mutex>
39
40 using ::android::BufferQueueDefs::NUM_BUFFER_SLOTS;
41 using ::android::C2AllocatorGralloc;
42 using ::android::C2AndroidMemoryUsage;
43 using ::android::Fence;
44 using ::android::GraphicBuffer;
45 using ::android::sp;
46 using ::android::status_t;
47 using ::android::wp;
48 using ::android::hardware::hidl_handle;
49 using ::android::hardware::Return;
50
51 using HBuffer = ::android::hardware::graphics::common::V1_2::HardwareBuffer;
52 using HStatus = ::android::hardware::graphics::bufferqueue::V2_0::Status;
53 using ::android::hardware::graphics::bufferqueue::V2_0::utils::b2h;
54 using ::android::hardware::graphics::bufferqueue::V2_0::utils::h2b;
55 using ::android::hardware::graphics::bufferqueue::V2_0::utils::HFenceWrapper;
56
57 using HGraphicBufferProducer = ::android::hardware::graphics::bufferqueue::V2_0
58 ::IGraphicBufferProducer;
59
GetBufferQueueData(const std::shared_ptr<const _C2BlockPoolData> & data,uint32_t * generation,uint64_t * bqId,int32_t * bqSlot)60 bool _C2BlockFactory::GetBufferQueueData(
61 const std::shared_ptr<const _C2BlockPoolData>& data,
62 uint32_t* generation, uint64_t* bqId, int32_t* bqSlot) {
63 if (data && data->getType() == _C2BlockPoolData::TYPE_BUFFERQUEUE) {
64 const std::shared_ptr<const C2BufferQueueBlockPoolData> poolData =
65 std::static_pointer_cast<const C2BufferQueueBlockPoolData>(data);
66 poolData->getBufferQueueData(generation, bqId, bqSlot);
67 return true;
68 }
69 return false;
70 }
71
HoldBlockFromBufferQueue(const std::shared_ptr<_C2BlockPoolData> & data,const std::shared_ptr<int> & owner,const sp<HGraphicBufferProducer> & igbp,std::shared_ptr<C2SurfaceSyncMemory> syncMem)72 bool _C2BlockFactory::HoldBlockFromBufferQueue(
73 const std::shared_ptr<_C2BlockPoolData>& data,
74 const std::shared_ptr<int>& owner,
75 const sp<HGraphicBufferProducer>& igbp,
76 std::shared_ptr<C2SurfaceSyncMemory> syncMem) {
77 const std::shared_ptr<C2BufferQueueBlockPoolData> poolData =
78 std::static_pointer_cast<C2BufferQueueBlockPoolData>(data);
79 return poolData->holdBlockFromBufferQueue(owner, igbp, syncMem);
80 }
81
BeginTransferBlockToClient(const std::shared_ptr<_C2BlockPoolData> & data)82 bool _C2BlockFactory::BeginTransferBlockToClient(
83 const std::shared_ptr<_C2BlockPoolData>& data) {
84 const std::shared_ptr<C2BufferQueueBlockPoolData> poolData =
85 std::static_pointer_cast<C2BufferQueueBlockPoolData>(data);
86 return poolData->beginTransferBlockToClient();
87 }
88
EndTransferBlockToClient(const std::shared_ptr<_C2BlockPoolData> & data,bool transfer)89 bool _C2BlockFactory::EndTransferBlockToClient(
90 const std::shared_ptr<_C2BlockPoolData>& data,
91 bool transfer) {
92 const std::shared_ptr<C2BufferQueueBlockPoolData> poolData =
93 std::static_pointer_cast<C2BufferQueueBlockPoolData>(data);
94 return poolData->endTransferBlockToClient(transfer);
95 }
96
BeginAttachBlockToBufferQueue(const std::shared_ptr<_C2BlockPoolData> & data)97 bool _C2BlockFactory::BeginAttachBlockToBufferQueue(
98 const std::shared_ptr<_C2BlockPoolData>& data) {
99 const std::shared_ptr<C2BufferQueueBlockPoolData> poolData =
100 std::static_pointer_cast<C2BufferQueueBlockPoolData>(data);
101 return poolData->beginAttachBlockToBufferQueue();
102 }
103
104 // if display was tried during attach, buffer should be retired ASAP.
EndAttachBlockToBufferQueue(const std::shared_ptr<_C2BlockPoolData> & data,const std::shared_ptr<int> & owner,const sp<HGraphicBufferProducer> & igbp,std::shared_ptr<C2SurfaceSyncMemory> syncMem,uint32_t generation,uint64_t bqId,int32_t bqSlot)105 bool _C2BlockFactory::EndAttachBlockToBufferQueue(
106 const std::shared_ptr<_C2BlockPoolData>& data,
107 const std::shared_ptr<int>& owner,
108 const sp<HGraphicBufferProducer>& igbp,
109 std::shared_ptr<C2SurfaceSyncMemory> syncMem,
110 uint32_t generation,
111 uint64_t bqId,
112 int32_t bqSlot) {
113 const std::shared_ptr<C2BufferQueueBlockPoolData> poolData =
114 std::static_pointer_cast<C2BufferQueueBlockPoolData>(data);
115 return poolData->endAttachBlockToBufferQueue(owner, igbp, syncMem, generation, bqId, bqSlot);
116 }
117
DisplayBlockToBufferQueue(const std::shared_ptr<_C2BlockPoolData> & data)118 bool _C2BlockFactory::DisplayBlockToBufferQueue(
119 const std::shared_ptr<_C2BlockPoolData>& data) {
120 const std::shared_ptr<C2BufferQueueBlockPoolData> poolData =
121 std::static_pointer_cast<C2BufferQueueBlockPoolData>(data);
122 return poolData->displayBlockToBufferQueue();
123 }
124
CreateGraphicBlock(const C2Handle * handle)125 std::shared_ptr<C2GraphicBlock> _C2BlockFactory::CreateGraphicBlock(
126 const C2Handle *handle) {
127 // TODO: get proper allocator? and mutex?
128 static std::unique_ptr<C2AllocatorGralloc> sAllocator = std::make_unique<C2AllocatorGralloc>(0);
129
130 std::shared_ptr<C2GraphicAllocation> alloc;
131 if (C2AllocatorGralloc::CheckHandle(handle)) {
132 uint32_t width;
133 uint32_t height;
134 uint32_t format;
135 uint64_t usage;
136 uint32_t stride;
137 uint32_t generation;
138 uint64_t bqId;
139 uint32_t bqSlot;
140 android::_UnwrapNativeCodec2GrallocMetadata(
141 handle, &width, &height, &format, &usage, &stride, &generation, &bqId, &bqSlot);
142 c2_status_t err = sAllocator->priorGraphicAllocation(handle, &alloc);
143 if (err == C2_OK) {
144 std::shared_ptr<C2GraphicBlock> block;
145 if (bqId || bqSlot) {
146 // BQBBP
147 std::shared_ptr<C2BufferQueueBlockPoolData> poolData =
148 std::make_shared<C2BufferQueueBlockPoolData>(generation,
149 bqId,
150 (int32_t)bqSlot,
151 nullptr,
152 nullptr);
153 block = _C2BlockFactory::CreateGraphicBlock(alloc, poolData);
154 } else {
155 block = _C2BlockFactory::CreateGraphicBlock(alloc);
156 }
157 return block;
158 }
159 }
160 return nullptr;
161 }
162
163 namespace {
164
getTimestampNow()165 int64_t getTimestampNow() {
166 int64_t stamp;
167 struct timespec ts;
168 // TODO: CLOCK_MONOTONIC_COARSE?
169 clock_gettime(CLOCK_MONOTONIC, &ts);
170 stamp = ts.tv_nsec / 1000;
171 stamp += (ts.tv_sec * 1000000LL);
172 return stamp;
173 }
174
175 // Do not rely on AHardwareBuffer module for GraphicBuffer handling since AHardwareBuffer
176 // module is linked to framework which could have a different implementation of GraphicBuffer
177 // than mainline/vndk implementation.(See b/203347494.)
178 //
179 // b2h/h2b between HardwareBuffer and GraphicBuffer cannot be used. (b2h/h2b depend on
180 // AHardwareBuffer module for the conversion between HardwareBuffer and GraphicBuffer.)
181 // hgbp_ prefixed methods are added to be used instead of b2h/h2b.
182 //
183 // TODO: Remove dependency with existing AHwB module. Also clean up conversions.(conversions here
184 // and h2b/b2h coversions)
hgbp_AHBuffer_to_GraphicBuffer(const AHardwareBuffer * buffer)185 const GraphicBuffer* hgbp_AHBuffer_to_GraphicBuffer(const AHardwareBuffer* buffer) {
186 return GraphicBuffer::fromAHardwareBuffer(buffer);
187 }
188
hgbp_createFromHandle(const AHardwareBuffer_Desc * desc,const native_handle_t * handle,sp<GraphicBuffer> * outBuffer)189 int hgbp_createFromHandle(const AHardwareBuffer_Desc* desc,
190 const native_handle_t* handle,
191 sp<GraphicBuffer> *outBuffer) {
192
193 if (!desc || !handle || !outBuffer) return ::android::BAD_VALUE;
194 if (desc->rfu0 != 0 || desc->rfu1 != 0) return ::android::BAD_VALUE;
195 if (desc->format == AHARDWAREBUFFER_FORMAT_BLOB && desc->height != 1)
196 return ::android::BAD_VALUE;
197
198 const int format = uint32_t(desc->format);
199 const uint64_t usage = uint64_t(desc->usage);
200 sp<GraphicBuffer> gbuffer(new GraphicBuffer(handle,
201 GraphicBuffer::HandleWrapMethod::CLONE_HANDLE,
202 desc->width, desc->height,
203 format, desc->layers, usage, desc->stride));
204 status_t err = gbuffer->initCheck();
205 if (err != 0 || gbuffer->handle == 0) return err;
206
207 *outBuffer = gbuffer;
208
209 return ::android::NO_ERROR;
210 }
211
hgbp_describe(const AHardwareBuffer * buffer,AHardwareBuffer_Desc * outDesc)212 void hgbp_describe(const AHardwareBuffer* buffer,
213 AHardwareBuffer_Desc* outDesc) {
214 if (!buffer || !outDesc) return;
215
216 const GraphicBuffer* gbuffer = hgbp_AHBuffer_to_GraphicBuffer(buffer);
217
218 outDesc->width = gbuffer->getWidth();
219 outDesc->height = gbuffer->getHeight();
220 outDesc->layers = gbuffer->getLayerCount();
221 outDesc->format = uint32_t(gbuffer->getPixelFormat());
222 outDesc->usage = uint64_t(gbuffer->getUsage());
223 outDesc->stride = gbuffer->getStride();
224 outDesc->rfu0 = 0;
225 outDesc->rfu1 = 0;
226 }
227
228
hgbp_h2b(HBuffer const & from,sp<GraphicBuffer> * to)229 bool hgbp_h2b(HBuffer const& from, sp<GraphicBuffer>* to) {
230 AHardwareBuffer_Desc const* desc =
231 reinterpret_cast<AHardwareBuffer_Desc const*>(
232 from.description.data());
233 native_handle_t const* handle = from.nativeHandle;
234 if (hgbp_createFromHandle(desc, handle, to) != ::android::OK) {
235 return false;
236 }
237 return true;
238 }
239
hgbp_b2h(sp<GraphicBuffer> const & from,HBuffer * to,uint32_t * toGenerationNumber)240 bool hgbp_b2h(sp<GraphicBuffer> const& from, HBuffer* to,
241 uint32_t* toGenerationNumber) {
242 if (!from) {
243 return false;
244 }
245 AHardwareBuffer* hwBuffer = from->toAHardwareBuffer();
246 to->nativeHandle.setTo(
247 const_cast<native_handle_t*>(from->handle),
248 false);
249 hgbp_describe(
250 hwBuffer,
251 reinterpret_cast<AHardwareBuffer_Desc*>(to->description.data()));
252 if (toGenerationNumber) {
253 *toGenerationNumber = from->getGenerationNumber();
254 }
255 return true;
256 }
257
258 // End of hgbp methods for GraphicBuffer creation.
259
getGenerationNumberAndUsage(const sp<HGraphicBufferProducer> & producer,uint32_t * generation,uint64_t * usage)260 bool getGenerationNumberAndUsage(const sp<HGraphicBufferProducer> &producer,
261 uint32_t *generation, uint64_t *usage) {
262 status_t status{};
263 int slot{};
264 bool bufferNeedsReallocation{};
265 sp<Fence> fence = new Fence();
266
267 using Input = HGraphicBufferProducer::DequeueBufferInput;
268 using Output = HGraphicBufferProducer::DequeueBufferOutput;
269 Return<void> transResult = producer->dequeueBuffer(
270 Input{640, 480, HAL_PIXEL_FORMAT_YCBCR_420_888, 0},
271 [&status, &slot, &bufferNeedsReallocation, &fence]
272 (HStatus hStatus, int32_t hSlot, Output const& hOutput) {
273 slot = static_cast<int>(hSlot);
274 if (!h2b(hStatus, &status) || !h2b(hOutput.fence, &fence)) {
275 status = ::android::BAD_VALUE;
276 } else {
277 bufferNeedsReallocation =
278 hOutput.bufferNeedsReallocation;
279 }
280 });
281 if (!transResult.isOk() || status != android::OK) {
282 return false;
283 }
284 HFenceWrapper hFenceWrapper{};
285 if (!b2h(fence, &hFenceWrapper)) {
286 (void)producer->detachBuffer(static_cast<int32_t>(slot)).isOk();
287 ALOGE("Invalid fence received from dequeueBuffer.");
288 return false;
289 }
290 sp<GraphicBuffer> slotBuffer = new GraphicBuffer();
291 // N.B. This assumes requestBuffer# returns an existing allocation
292 // instead of a new allocation.
293 transResult = producer->requestBuffer(
294 slot,
295 [&status, &slotBuffer, &generation, &usage](
296 HStatus hStatus,
297 HBuffer const& hBuffer,
298 uint32_t generationNumber){
299 if (h2b(hStatus, &status) &&
300 hgbp_h2b(hBuffer, &slotBuffer) &&
301 slotBuffer) {
302 *generation = generationNumber;
303 *usage = slotBuffer->getUsage();
304 slotBuffer->setGenerationNumber(generationNumber);
305 } else {
306 status = android::BAD_VALUE;
307 }
308 });
309 if (!transResult.isOk()) {
310 return false;
311 } else if (status != android::NO_ERROR) {
312 (void)producer->detachBuffer(static_cast<int32_t>(slot)).isOk();
313 return false;
314 }
315 (void)producer->detachBuffer(static_cast<int32_t>(slot)).isOk();
316 return true;
317 }
318
319 };
320
321 class C2BufferQueueBlockPool::Impl
322 : public std::enable_shared_from_this<C2BufferQueueBlockPool::Impl> {
323 private:
dequeueBuffer(uint32_t width,uint32_t height,uint32_t format,C2AndroidMemoryUsage androidUsage,int * slot,bool * needsRealloc,sp<Fence> * fence)324 c2_status_t dequeueBuffer(
325 uint32_t width,
326 uint32_t height,
327 uint32_t format,
328 C2AndroidMemoryUsage androidUsage,
329 int *slot, bool *needsRealloc, sp<Fence> *fence) {
330 status_t status{};
331 using Input = HGraphicBufferProducer::DequeueBufferInput;
332 using Output = HGraphicBufferProducer::DequeueBufferOutput;
333 Return<void> transResult = mProducer->dequeueBuffer(
334 Input{
335 width,
336 height,
337 format,
338 androidUsage.asGrallocUsage()},
339 [&status, slot, needsRealloc,
340 fence](HStatus hStatus,
341 int32_t hSlot,
342 Output const& hOutput) {
343 *slot = static_cast<int>(hSlot);
344 if (!h2b(hStatus, &status) ||
345 !h2b(hOutput.fence, fence)) {
346 status = ::android::BAD_VALUE;
347 } else {
348 *needsRealloc =
349 hOutput.bufferNeedsReallocation;
350 }
351 });
352 if (!transResult.isOk() || status != android::OK) {
353 if (transResult.isOk()) {
354 ++mDqFailure;
355 if (status == android::INVALID_OPERATION ||
356 status == android::TIMED_OUT ||
357 status == android::WOULD_BLOCK) {
358 // Dequeue buffer is blocked temporarily. Retrying is
359 // required.
360 return C2_BLOCKING;
361 }
362 }
363 ALOGD("cannot dequeue buffer %d", status);
364 return C2_BAD_VALUE;
365 }
366 return C2_OK;
367 }
368
fetchFromIgbp_l(uint32_t width,uint32_t height,uint32_t format,C2MemoryUsage usage,std::shared_ptr<C2GraphicBlock> * block,C2Fence * c2Fence)369 c2_status_t fetchFromIgbp_l(
370 uint32_t width,
371 uint32_t height,
372 uint32_t format,
373 C2MemoryUsage usage,
374 std::shared_ptr<C2GraphicBlock> *block /* nonnull */,
375 C2Fence *c2Fence) {
376 // We have an IGBP now.
377 C2AndroidMemoryUsage androidUsage = usage;
378 status_t status{};
379 int slot{};
380 bool bufferNeedsReallocation{};
381 sp<Fence> fence = new Fence();
382 ALOGV("tries to dequeue buffer");
383
384 C2SyncVariables *syncVar = mSyncMem ? mSyncMem->mem(): nullptr;
385 { // Call dequeueBuffer().
386 c2_status_t c2Status;
387 if (syncVar) {
388 uint32_t waitId;
389 syncVar->lock();
390 if (!syncVar->isDequeueableLocked(&waitId)) {
391 syncVar->unlock();
392 if (c2Fence) {
393 *c2Fence = _C2FenceFactory::CreateSurfaceFence(mSyncMem, waitId);
394 }
395 return C2_BLOCKING;
396 }
397 if (syncVar->getSyncStatusLocked() != C2SyncVariables::STATUS_ACTIVE) {
398 waitId = syncVar->getWaitIdLocked();
399 syncVar->unlock();
400 if (c2Fence) {
401 *c2Fence = _C2FenceFactory::CreateSurfaceFence(mSyncMem, waitId);
402 }
403 return C2_BLOCKING;
404 }
405 syncVar->notifyDequeuedLocked();
406 syncVar->unlock();
407 c2Status = dequeueBuffer(width, height, format, androidUsage,
408 &slot, &bufferNeedsReallocation, &fence);
409 if (c2Status != C2_OK) {
410 syncVar->lock();
411 syncVar->notifyQueuedLocked();
412 syncVar->unlock();
413 }
414 } else {
415 c2Status = dequeueBuffer(width, height, format, usage,
416 &slot, &bufferNeedsReallocation, &fence);
417 }
418 if (c2Status != C2_OK) {
419 return c2Status;
420 }
421 mDqFailure = 0;
422 mLastDqTs = getTimestampNow();
423 }
424 HFenceWrapper hFenceWrapper{};
425 if (!b2h(fence, &hFenceWrapper)) {
426 ALOGE("Invalid fence received from dequeueBuffer.");
427 return C2_BAD_VALUE;
428 }
429 ALOGV("dequeued a buffer successfully");
430 bool dequeueable = false;
431 uint32_t waitId;
432 if (fence) {
433 static constexpr int kFenceWaitTimeMs = 10;
434
435 if (bufferNeedsReallocation) {
436 mBuffers[slot].clear();
437 }
438
439 status_t status = fence->wait(kFenceWaitTimeMs);
440 if (status == -ETIME) {
441 // fence is not signalled yet.
442 if (syncVar) {
443 (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk();
444 syncVar->lock();
445 dequeueable = syncVar->notifyQueuedLocked(&waitId);
446 syncVar->unlock();
447 if (c2Fence) {
448 *c2Fence = dequeueable ? C2Fence() :
449 _C2FenceFactory::CreateSurfaceFence(mSyncMem, waitId);
450 }
451 } else {
452 (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk();
453 }
454 return C2_BLOCKING;
455 }
456 if (status != android::NO_ERROR) {
457 ALOGD("buffer fence wait error %d", status);
458 if (syncVar) {
459 (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk();
460 syncVar->lock();
461 syncVar->notifyQueuedLocked();
462 syncVar->unlock();
463 if (c2Fence) {
464 *c2Fence = C2Fence();
465 }
466 } else {
467 (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk();
468 }
469 return C2_BAD_VALUE;
470 } else if (mRenderCallback) {
471 nsecs_t signalTime = fence->getSignalTime();
472 if (signalTime >= 0 && signalTime < INT64_MAX) {
473 mRenderCallback(mProducerId, slot, signalTime);
474 } else {
475 ALOGV("got fence signal time of %lld", (long long)signalTime);
476 }
477 }
478 }
479
480 sp<GraphicBuffer> &slotBuffer = mBuffers[slot];
481 uint32_t outGeneration;
482 if (bufferNeedsReallocation || !slotBuffer) {
483 if (!slotBuffer) {
484 slotBuffer = new GraphicBuffer();
485 }
486 // N.B. This assumes requestBuffer# returns an existing allocation
487 // instead of a new allocation.
488 Return<void> transResult = mProducer->requestBuffer(
489 slot,
490 [&status, &slotBuffer, &outGeneration](
491 HStatus hStatus,
492 HBuffer const& hBuffer,
493 uint32_t generationNumber){
494 if (h2b(hStatus, &status) &&
495 hgbp_h2b(hBuffer, &slotBuffer) &&
496 slotBuffer) {
497 slotBuffer->setGenerationNumber(generationNumber);
498 outGeneration = generationNumber;
499 } else {
500 status = android::BAD_VALUE;
501 }
502 });
503 if (!transResult.isOk()) {
504 slotBuffer.clear();
505 return C2_BAD_VALUE;
506 } else if (status != android::NO_ERROR) {
507 slotBuffer.clear();
508 if (syncVar) {
509 (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk();
510 syncVar->lock();
511 syncVar->notifyQueuedLocked();
512 syncVar->unlock();
513 if (c2Fence) {
514 *c2Fence = C2Fence();
515 }
516 } else {
517 (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk();
518 }
519 return C2_BAD_VALUE;
520 }
521 if (mGeneration == 0) {
522 // getting generation # lazily due to dequeue failure.
523 mGeneration = outGeneration;
524 }
525 }
526 if (slotBuffer) {
527 ALOGV("buffer wraps %llu %d", (unsigned long long)mProducerId, slot);
528 C2Handle *c2Handle = android::WrapNativeCodec2GrallocHandle(
529 slotBuffer->handle,
530 slotBuffer->width,
531 slotBuffer->height,
532 slotBuffer->format,
533 slotBuffer->usage,
534 slotBuffer->stride,
535 slotBuffer->getGenerationNumber(),
536 mProducerId, slot);
537 if (c2Handle) {
538 std::shared_ptr<C2GraphicAllocation> alloc;
539 c2_status_t err = mAllocator->priorGraphicAllocation(c2Handle, &alloc);
540 if (err != C2_OK) {
541 native_handle_close(c2Handle);
542 native_handle_delete(c2Handle);
543 return err;
544 }
545 std::shared_ptr<C2BufferQueueBlockPoolData> poolData =
546 std::make_shared<C2BufferQueueBlockPoolData>(
547 slotBuffer->getGenerationNumber(),
548 mProducerId, slot,
549 mIgbpValidityToken, mProducer, mSyncMem);
550 mPoolDatas[slot] = poolData;
551 *block = _C2BlockFactory::CreateGraphicBlock(alloc, poolData);
552 return C2_OK;
553 }
554 // Block was not created. call requestBuffer# again next time.
555 slotBuffer.clear();
556 if (syncVar) {
557 (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk();
558 syncVar->lock();
559 syncVar->notifyQueuedLocked();
560 syncVar->unlock();
561 if (c2Fence) {
562 *c2Fence = C2Fence();
563 }
564 } else {
565 (void)mProducer->cancelBuffer(slot, hFenceWrapper.getHandle()).isOk();
566 }
567 return C2_BAD_VALUE;
568 }
569 if (c2Fence) {
570 *c2Fence = C2Fence();
571 }
572 return C2_BAD_VALUE;
573 }
574
575 public:
Impl(const std::shared_ptr<C2Allocator> & allocator)576 Impl(const std::shared_ptr<C2Allocator> &allocator)
577 : mInit(C2_OK), mProducerId(0), mGeneration(0),
578 mConsumerUsage(0), mDqFailure(0), mLastDqTs(0),
579 mLastDqLogTs(0), mAllocator(allocator), mIgbpValidityToken(std::make_shared<int>(0)) {
580 }
581
~Impl()582 ~Impl() {
583 mIgbpValidityToken.reset();
584 for (int i = 0; i < NUM_BUFFER_SLOTS; ++i) {
585 mBuffers[i].clear();
586 }
587 }
588
fetchGraphicBlock(uint32_t width,uint32_t height,uint32_t format,C2MemoryUsage usage,std::shared_ptr<C2GraphicBlock> * block,C2Fence * fence)589 c2_status_t fetchGraphicBlock(
590 uint32_t width,
591 uint32_t height,
592 uint32_t format,
593 C2MemoryUsage usage,
594 std::shared_ptr<C2GraphicBlock> *block /* nonnull */,
595 C2Fence *fence) {
596 block->reset();
597 if (mInit != C2_OK) {
598 return mInit;
599 }
600
601 static int kMaxIgbpRetryDelayUs = 10000;
602
603 std::unique_lock<std::mutex> lock(mMutex);
604 if (mLastDqLogTs == 0) {
605 mLastDqLogTs = getTimestampNow();
606 } else {
607 int64_t now = getTimestampNow();
608 if (now >= mLastDqLogTs + 5000000) {
609 if (now >= mLastDqTs + 1000000 || mDqFailure > 5) {
610 ALOGW("last successful dequeue was %lld us ago, "
611 "%zu consecutive failures",
612 (long long)(now - mLastDqTs), mDqFailure);
613 }
614 mLastDqLogTs = now;
615 }
616 }
617 if (mProducerId == 0) {
618 std::shared_ptr<C2GraphicAllocation> alloc;
619 c2_status_t err = mAllocator->newGraphicAllocation(
620 width, height, format, usage, &alloc);
621 if (err != C2_OK) {
622 return err;
623 }
624 std::shared_ptr<C2BufferQueueBlockPoolData> poolData =
625 std::make_shared<C2BufferQueueBlockPoolData>(
626 0, (uint64_t)0, ~0, nullptr, nullptr, nullptr);
627 *block = _C2BlockFactory::CreateGraphicBlock(alloc, poolData);
628 ALOGV("allocated a buffer successfully");
629
630 return C2_OK;
631 }
632 c2_status_t status = fetchFromIgbp_l(width, height, format, usage, block, fence);
633 if (status == C2_BLOCKING) {
634 lock.unlock();
635 if (!fence) {
636 // in order not to drain cpu from component's spinning
637 ::usleep(kMaxIgbpRetryDelayUs);
638 }
639 }
640 return status;
641 }
642
setRenderCallback(const OnRenderCallback & renderCallback)643 void setRenderCallback(const OnRenderCallback &renderCallback) {
644 std::scoped_lock<std::mutex> lock(mMutex);
645 mRenderCallback = renderCallback;
646 }
647
648 /* This is for Old HAL request for compatibility */
configureProducer(const sp<HGraphicBufferProducer> & producer)649 void configureProducer(const sp<HGraphicBufferProducer> &producer) {
650 uint64_t producerId = 0;
651 uint32_t generation = 0;
652 uint64_t usage = 0;
653 bool bqInformation = false;
654 if (producer) {
655 Return<uint64_t> transResult = producer->getUniqueId();
656 if (!transResult.isOk()) {
657 ALOGD("configureProducer -- failed to connect to the producer");
658 return;
659 }
660 producerId = static_cast<uint64_t>(transResult);
661 bqInformation = getGenerationNumberAndUsage(producer, &generation, &usage);
662 if (!bqInformation) {
663 ALOGW("get generationNumber failed %llu",
664 (unsigned long long)producerId);
665 }
666 }
667 configureProducer(producer, nullptr, producerId, generation, usage, bqInformation);
668 }
669
configureProducer(const sp<HGraphicBufferProducer> & producer,native_handle_t * syncHandle,uint64_t producerId,uint32_t generation,uint64_t usage,bool bqInformation)670 void configureProducer(const sp<HGraphicBufferProducer> &producer,
671 native_handle_t *syncHandle,
672 uint64_t producerId,
673 uint32_t generation,
674 uint64_t usage,
675 bool bqInformation) {
676 std::shared_ptr<C2SurfaceSyncMemory> c2SyncMem;
677 if (syncHandle) {
678 if (!producer) {
679 native_handle_close(syncHandle);
680 native_handle_delete(syncHandle);
681 } else {
682 c2SyncMem = C2SurfaceSyncMemory::Import(syncHandle);
683 }
684 }
685 int migrated = 0;
686 std::shared_ptr<C2SurfaceSyncMemory> oldMem;
687 // poolDatas dtor should not be called during lock is held.
688 std::shared_ptr<C2BufferQueueBlockPoolData>
689 poolDatas[NUM_BUFFER_SLOTS];
690 {
691 sp<GraphicBuffer> buffers[NUM_BUFFER_SLOTS];
692 std::scoped_lock<std::mutex> lock(mMutex);
693 int32_t oldGeneration = mGeneration;
694 if (producer) {
695 mProducer = producer;
696 mProducerId = producerId;
697 mGeneration = bqInformation ? generation : 0;
698 } else {
699 mProducer = nullptr;
700 mProducerId = 0;
701 mGeneration = 0;
702 ALOGD("configuring null producer: igbp_information(%d)", bqInformation);
703 }
704 oldMem = mSyncMem; // preven destruction while locked.
705 mSyncMem = c2SyncMem;
706 C2SyncVariables *syncVar = mSyncMem ? mSyncMem->mem() : nullptr;
707 if (syncVar) {
708 syncVar->lock();
709 syncVar->setSyncStatusLocked(C2SyncVariables::STATUS_ACTIVE);
710 syncVar->unlock();
711 }
712 if (mProducer && bqInformation) { // migrate buffers
713 for (int i = 0; i < NUM_BUFFER_SLOTS; ++i) {
714 std::shared_ptr<C2BufferQueueBlockPoolData> data =
715 mPoolDatas[i].lock();
716 if (data) {
717 int slot = data->migrate(
718 mProducer, generation, usage,
719 producerId, mBuffers[i], oldGeneration, mSyncMem);
720 if (slot >= 0) {
721 buffers[slot] = mBuffers[i];
722 poolDatas[slot] = data;
723 ++migrated;
724 }
725 }
726 }
727 } else {
728 // old buffers should not be cancelled since the associated IGBP
729 // is no longer valid.
730 mIgbpValidityToken = std::make_shared<int>(0);
731 }
732 for (int i = 0; i < NUM_BUFFER_SLOTS; ++i) {
733 mBuffers[i] = buffers[i];
734 mPoolDatas[i] = poolDatas[i];
735 }
736 }
737 if (producer && bqInformation) {
738 ALOGD("local generation change %u , "
739 "bqId: %llu migrated buffers # %d",
740 generation, (unsigned long long)producerId, migrated);
741 }
742 mConsumerUsage = usage;
743 }
744
getConsumerUsage(uint64_t * consumeUsage)745 void getConsumerUsage(uint64_t *consumeUsage) {
746 *consumeUsage = mConsumerUsage;
747 }
748
749 private:
750 friend struct C2BufferQueueBlockPoolData;
751
752 c2_status_t mInit;
753 uint64_t mProducerId;
754 uint32_t mGeneration;
755 uint64_t mConsumerUsage;
756 OnRenderCallback mRenderCallback;
757
758 size_t mDqFailure;
759 int64_t mLastDqTs;
760 int64_t mLastDqLogTs;
761
762 const std::shared_ptr<C2Allocator> mAllocator;
763
764 std::mutex mMutex;
765 sp<HGraphicBufferProducer> mProducer;
766 sp<HGraphicBufferProducer> mSavedProducer;
767
768 sp<GraphicBuffer> mBuffers[NUM_BUFFER_SLOTS];
769 std::weak_ptr<C2BufferQueueBlockPoolData> mPoolDatas[NUM_BUFFER_SLOTS];
770
771 std::shared_ptr<C2SurfaceSyncMemory> mSyncMem;
772
773 // IGBP invalidation notification token.
774 // The buffers(C2BufferQueueBlockPoolData) has the reference to the IGBP where
775 // they belong in order to call IGBP::cancelBuffer() when they are of no use.
776 //
777 // In certain cases, IGBP is no longer used by this class(actually MediaCodec)
778 // any more and the situation needs to be addressed quickly. In order to
779 // achieve those, std::shared_ptr<> is used as a token for quick IGBP invalidation
780 // notification from the buffers.
781 //
782 // The buffer side will have the reference of the token as std::weak_ptr<>.
783 // if the token has been expired, the buffers will not call IGBP::cancelBuffer()
784 // when they are no longer used.
785 std::shared_ptr<int> mIgbpValidityToken;
786 };
787
C2BufferQueueBlockPoolData(uint32_t generation,uint64_t bqId,int32_t bqSlot,const std::shared_ptr<int> & owner,const sp<HGraphicBufferProducer> & producer)788 C2BufferQueueBlockPoolData::C2BufferQueueBlockPoolData(
789 uint32_t generation, uint64_t bqId, int32_t bqSlot,
790 const std::shared_ptr<int>& owner,
791 const sp<HGraphicBufferProducer>& producer) :
792 mLocal(false), mHeld(producer && bqId != 0),
793 mGeneration(generation), mBqId(bqId), mBqSlot(bqSlot),
794 mCurrentGeneration(generation), mCurrentBqId(bqId),
795 mTransfer(false), mAttach(false), mDisplay(false),
796 mOwner(owner), mIgbp(producer) {
797 }
798
C2BufferQueueBlockPoolData(uint32_t generation,uint64_t bqId,int32_t bqSlot,const std::shared_ptr<int> & owner,const android::sp<HGraphicBufferProducer> & producer,std::shared_ptr<C2SurfaceSyncMemory> syncMem)799 C2BufferQueueBlockPoolData::C2BufferQueueBlockPoolData(
800 uint32_t generation, uint64_t bqId, int32_t bqSlot,
801 const std::shared_ptr<int>& owner,
802 const android::sp<HGraphicBufferProducer>& producer,
803 std::shared_ptr<C2SurfaceSyncMemory> syncMem) :
804 mLocal(true), mHeld(true),
805 mGeneration(generation), mBqId(bqId), mBqSlot(bqSlot),
806 mCurrentGeneration(generation), mCurrentBqId(bqId),
807 mTransfer(false), mAttach(false), mDisplay(false),
808 mOwner(owner), mIgbp(producer), mSyncMem(syncMem) {
809 }
810
~C2BufferQueueBlockPoolData()811 C2BufferQueueBlockPoolData::~C2BufferQueueBlockPoolData() {
812 if (!mHeld || mBqId == 0 || !mIgbp) {
813 return;
814 }
815
816 if (mLocal) {
817 if (mGeneration == mCurrentGeneration && mBqId == mCurrentBqId && !mOwner.expired()) {
818 C2SyncVariables *syncVar = mSyncMem ? mSyncMem->mem() : nullptr;
819 if (syncVar) {
820 mIgbp->cancelBuffer(mBqSlot, hidl_handle{}).isOk();
821 syncVar->lock();
822 syncVar->notifyQueuedLocked(nullptr,
823 syncVar->getSyncStatusLocked() == C2SyncVariables::STATUS_ACTIVE);
824 syncVar->unlock();
825 } else {
826 mIgbp->cancelBuffer(mBqSlot, hidl_handle{}).isOk();
827 }
828 }
829 } else if (!mOwner.expired()) {
830 C2SyncVariables *syncVar = mSyncMem ? mSyncMem->mem() : nullptr;
831 if (syncVar) {
832 mIgbp->cancelBuffer(mBqSlot, hidl_handle{}).isOk();
833 syncVar->lock();
834 syncVar->notifyQueuedLocked(nullptr,
835 syncVar->getSyncStatusLocked() != C2SyncVariables::STATUS_SWITCHING);
836 syncVar->unlock();
837 } else {
838 mIgbp->cancelBuffer(mBqSlot, hidl_handle{}).isOk();
839 }
840 }
841 }
842
getType() const843 C2BufferQueueBlockPoolData::type_t C2BufferQueueBlockPoolData::getType() const {
844 return TYPE_BUFFERQUEUE;
845 }
846
migrate(const sp<HGraphicBufferProducer> & producer,uint32_t toGeneration,uint64_t toUsage,uint64_t toBqId,sp<GraphicBuffer> & graphicBuffer,uint32_t oldGeneration,std::shared_ptr<C2SurfaceSyncMemory> syncMem)847 int C2BufferQueueBlockPoolData::migrate(
848 const sp<HGraphicBufferProducer>& producer,
849 uint32_t toGeneration, uint64_t toUsage, uint64_t toBqId,
850 sp<GraphicBuffer>& graphicBuffer, uint32_t oldGeneration,
851 std::shared_ptr<C2SurfaceSyncMemory> syncMem) {
852 std::scoped_lock<std::mutex> l(mLock);
853
854 mCurrentBqId = toBqId;
855 mCurrentGeneration = toGeneration;
856
857 if (!mHeld || mBqId == 0) {
858 ALOGV("buffer is not owned");
859 return -1;
860 }
861 if (!mLocal) {
862 ALOGV("pool is not local");
863 return -1;
864 }
865 if (mBqSlot < 0 || mBqSlot >= NUM_BUFFER_SLOTS) {
866 ALOGV("slot is not in effect");
867 return -1;
868 }
869 if (!graphicBuffer) {
870 ALOGV("buffer is null");
871 return -1;
872 }
873 if (toGeneration == mGeneration && mBqId == toBqId) {
874 ALOGV("cannot migrate to same bufferqueue");
875 return -1;
876 }
877 if (oldGeneration != mGeneration) {
878 ALOGV("cannot migrate stale buffer");
879 return -1;
880 }
881 if (mTransfer) {
882 // either transferred or detached.
883 ALOGV("buffer is in transfer");
884 return -1;
885 }
886
887 if (toUsage != graphicBuffer->getUsage()) {
888 sp<GraphicBuffer> newBuffer = new GraphicBuffer(
889 graphicBuffer->handle, GraphicBuffer::CLONE_HANDLE,
890 graphicBuffer->width, graphicBuffer->height, graphicBuffer->format,
891 graphicBuffer->layerCount, toUsage | graphicBuffer->getUsage(), graphicBuffer->stride);
892 if (newBuffer->initCheck() == android::NO_ERROR) {
893 graphicBuffer = std::move(newBuffer);
894 } else {
895 ALOGW("%s() failed to update usage, original usage=%" PRIx64 ", toUsage=%" PRIx64,
896 __func__, graphicBuffer->getUsage(), toUsage);
897 }
898 }
899 graphicBuffer->setGenerationNumber(toGeneration);
900
901 HBuffer hBuffer{};
902 uint32_t hGenerationNumber{};
903 if (!hgbp_b2h(graphicBuffer, &hBuffer, &hGenerationNumber)) {
904 ALOGD("I to O conversion failed");
905 return -1;
906 }
907
908 bool converted{};
909 status_t bStatus{};
910 int slot;
911 int *outSlot = &slot;
912 Return<void> transResult =
913 producer->attachBuffer(hBuffer, hGenerationNumber,
914 [&converted, &bStatus, outSlot](
915 HStatus hStatus, int32_t hSlot, bool releaseAll) {
916 converted = h2b(hStatus, &bStatus);
917 *outSlot = static_cast<int>(hSlot);
918 if (converted && releaseAll && bStatus == android::OK) {
919 bStatus = android::INVALID_OPERATION;
920 }
921 });
922 if (!transResult.isOk() || !converted || bStatus != android::OK) {
923 ALOGD("attach failed %d", static_cast<int>(bStatus));
924 return -1;
925 }
926 ALOGV("local migration from gen %u : %u slot %d : %d",
927 mGeneration, toGeneration, mBqSlot, slot);
928 mIgbp = producer;
929 mGeneration = toGeneration;
930 mBqId = toBqId;
931 mBqSlot = slot;
932 mSyncMem = syncMem;
933
934 C2SyncVariables *syncVar = syncMem ? syncMem->mem() : nullptr;
935 if (syncVar) {
936 syncVar->lock();
937 syncVar->notifyDequeuedLocked();
938 syncVar->unlock();
939 }
940 return slot;
941 }
942
getBufferQueueData(uint32_t * generation,uint64_t * bqId,int32_t * bqSlot) const943 void C2BufferQueueBlockPoolData::getBufferQueueData(
944 uint32_t* generation, uint64_t* bqId, int32_t* bqSlot) const {
945 if (generation) {
946 std::scoped_lock<std::mutex> lock(mLock);
947 *generation = mGeneration;
948 if (bqId) {
949 *bqId = mBqId;
950 }
951 if (bqSlot) {
952 *bqSlot = mBqSlot;
953 }
954 }
955 }
956
holdBlockFromBufferQueue(const std::shared_ptr<int> & owner,const sp<HGraphicBufferProducer> & igbp,std::shared_ptr<C2SurfaceSyncMemory> syncMem)957 bool C2BufferQueueBlockPoolData::holdBlockFromBufferQueue(
958 const std::shared_ptr<int>& owner,
959 const sp<HGraphicBufferProducer>& igbp,
960 std::shared_ptr<C2SurfaceSyncMemory> syncMem) {
961 std::scoped_lock<std::mutex> lock(mLock);
962 if (!mLocal) {
963 mOwner = owner;
964 mIgbp = igbp;
965 mSyncMem = syncMem;
966 }
967 if (mHeld) {
968 return false;
969 }
970 mHeld = true;
971 return true;
972 }
973
beginTransferBlockToClient()974 bool C2BufferQueueBlockPoolData::beginTransferBlockToClient() {
975 std::scoped_lock<std::mutex> lock(mLock);
976 mTransfer = true;
977 return true;
978 }
979
endTransferBlockToClient(bool transfer)980 bool C2BufferQueueBlockPoolData::endTransferBlockToClient(bool transfer) {
981 std::scoped_lock<std::mutex> lock(mLock);
982 mTransfer = false;
983 if (transfer) {
984 mHeld = false;
985 }
986 return true;
987 }
988
beginAttachBlockToBufferQueue()989 bool C2BufferQueueBlockPoolData::beginAttachBlockToBufferQueue() {
990 std::scoped_lock<std::mutex> lock(mLock);
991 if (mLocal || mDisplay ||
992 mAttach || !mHeld) {
993 return false;
994 }
995 if (mBqId == 0) {
996 return false;
997 }
998 mAttach = true;
999 return true;
1000 }
1001
endAttachBlockToBufferQueue(const std::shared_ptr<int> & owner,const sp<HGraphicBufferProducer> & igbp,std::shared_ptr<C2SurfaceSyncMemory> syncMem,uint32_t generation,uint64_t bqId,int32_t bqSlot)1002 bool C2BufferQueueBlockPoolData::endAttachBlockToBufferQueue(
1003 const std::shared_ptr<int>& owner,
1004 const sp<HGraphicBufferProducer>& igbp,
1005 std::shared_ptr<C2SurfaceSyncMemory> syncMem,
1006 uint32_t generation,
1007 uint64_t bqId,
1008 int32_t bqSlot) {
1009 std::scoped_lock<std::mutex> lock(mLock);
1010 if (mLocal || !mAttach) {
1011 return false;
1012 }
1013 if (mDisplay) {
1014 mAttach = false;
1015 mHeld = false;
1016 return false;
1017 }
1018 mAttach = false;
1019 mHeld = true;
1020 mOwner = owner;
1021 mIgbp = igbp;
1022 mSyncMem = syncMem;
1023 mGeneration = generation;
1024 mBqId = bqId;
1025 mBqSlot = bqSlot;
1026 return true;
1027 }
1028
displayBlockToBufferQueue()1029 bool C2BufferQueueBlockPoolData::displayBlockToBufferQueue() {
1030 std::scoped_lock<std::mutex> lock(mLock);
1031 if (mLocal || mDisplay || !mHeld) {
1032 return false;
1033 }
1034 if (mBqId == 0) {
1035 return false;
1036 }
1037 mDisplay = true;
1038 if (mAttach) {
1039 return false;
1040 }
1041 mHeld = false;
1042 return true;
1043 }
1044
C2BufferQueueBlockPool(const std::shared_ptr<C2Allocator> & allocator,const local_id_t localId)1045 C2BufferQueueBlockPool::C2BufferQueueBlockPool(
1046 const std::shared_ptr<C2Allocator> &allocator, const local_id_t localId)
1047 : mAllocator(allocator), mLocalId(localId), mImpl(new Impl(allocator)) {}
1048
~C2BufferQueueBlockPool()1049 C2BufferQueueBlockPool::~C2BufferQueueBlockPool() {}
1050
fetchGraphicBlock(uint32_t width,uint32_t height,uint32_t format,C2MemoryUsage usage,std::shared_ptr<C2GraphicBlock> * block)1051 c2_status_t C2BufferQueueBlockPool::fetchGraphicBlock(
1052 uint32_t width,
1053 uint32_t height,
1054 uint32_t format,
1055 C2MemoryUsage usage,
1056 std::shared_ptr<C2GraphicBlock> *block /* nonnull */) {
1057 if (mImpl) {
1058 return mImpl->fetchGraphicBlock(width, height, format, usage, block, nullptr);
1059 }
1060 return C2_CORRUPTED;
1061 }
1062
fetchGraphicBlock(uint32_t width,uint32_t height,uint32_t format,C2MemoryUsage usage,std::shared_ptr<C2GraphicBlock> * block,C2Fence * fence)1063 c2_status_t C2BufferQueueBlockPool::fetchGraphicBlock(
1064 uint32_t width,
1065 uint32_t height,
1066 uint32_t format,
1067 C2MemoryUsage usage,
1068 std::shared_ptr<C2GraphicBlock> *block /* nonnull */,
1069 C2Fence *fence /* nonnull */) {
1070 if (mImpl) {
1071 return mImpl->fetchGraphicBlock(width, height, format, usage, block, fence);
1072 }
1073 return C2_CORRUPTED;
1074 }
1075
configureProducer(const sp<HGraphicBufferProducer> & producer)1076 void C2BufferQueueBlockPool::configureProducer(const sp<HGraphicBufferProducer> &producer) {
1077 if (mImpl) {
1078 mImpl->configureProducer(producer);
1079 }
1080 }
1081
configureProducer(const sp<HGraphicBufferProducer> & producer,native_handle_t * syncMemory,uint64_t bqId,uint32_t generationId,uint64_t consumerUsage)1082 void C2BufferQueueBlockPool::configureProducer(
1083 const sp<HGraphicBufferProducer> &producer,
1084 native_handle_t *syncMemory,
1085 uint64_t bqId,
1086 uint32_t generationId,
1087 uint64_t consumerUsage) {
1088 if (mImpl) {
1089 mImpl->configureProducer(
1090 producer, syncMemory, bqId, generationId, consumerUsage, true);
1091 }
1092 }
1093
setRenderCallback(const OnRenderCallback & renderCallback)1094 void C2BufferQueueBlockPool::setRenderCallback(const OnRenderCallback &renderCallback) {
1095 if (mImpl) {
1096 mImpl->setRenderCallback(renderCallback);
1097 }
1098 }
1099
getConsumerUsage(uint64_t * consumeUsage)1100 void C2BufferQueueBlockPool::getConsumerUsage(uint64_t *consumeUsage) {
1101 if (mImpl) {
1102 mImpl->getConsumerUsage(consumeUsage);
1103 }
1104 }
1105
1106