blob: f86e048c9f93e6b41f5f0e627d9534797d6882c3 [file] [log] [blame]
Pawin Vongmasa586b1982019-10-23 19:21:04 -07001/*
2 * Copyright 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 "Codec2-OutputBufferQueue"
Arun Johnson7ba67072023-11-06 22:23:04 +000019#define ATRACE_TAG ATRACE_TAG_VIDEO
Pawin Vongmasa586b1982019-10-23 19:21:04 -070020#include <android-base/logging.h>
Arun Johnson7ba67072023-11-06 22:23:04 +000021#include <utils/Trace.h>
Pawin Vongmasa586b1982019-10-23 19:21:04 -070022
23#include <android/hardware/graphics/bufferqueue/2.0/IGraphicBufferProducer.h>
Sungtak Leea714f112021-03-16 05:40:03 -070024#include <codec2/hidl/output.h>
25#include <cutils/ashmem.h>
Pawin Vongmasa586b1982019-10-23 19:21:04 -070026#include <gui/bufferqueue/2.0/B2HGraphicBufferProducer.h>
Sungtak Leea714f112021-03-16 05:40:03 -070027#include <sys/mman.h>
Pawin Vongmasa586b1982019-10-23 19:21:04 -070028
29#include <C2AllocatorGralloc.h>
30#include <C2BlockInternal.h>
31#include <C2Buffer.h>
32#include <C2PlatformSupport.h>
Sungtak Leea714f112021-03-16 05:40:03 -070033#include <C2SurfaceSyncObj.h>
Pawin Vongmasa586b1982019-10-23 19:21:04 -070034
35#include <iomanip>
36
37namespace android {
38namespace hardware {
39namespace media {
40namespace c2 {
Pawin Vongmasa586b1982019-10-23 19:21:04 -070041
42using HGraphicBufferProducer = ::android::hardware::graphics::bufferqueue::
43 V2_0::IGraphicBufferProducer;
44using B2HGraphicBufferProducer = ::android::hardware::graphics::bufferqueue::
45 V2_0::utils::B2HGraphicBufferProducer;
46
47namespace /* unnamed */ {
48
49// Create a GraphicBuffer object from a graphic block.
50sp<GraphicBuffer> createGraphicBuffer(const C2ConstGraphicBlock& block) {
51 uint32_t width;
52 uint32_t height;
53 uint32_t format;
54 uint64_t usage;
55 uint32_t stride;
56 uint32_t generation;
57 uint64_t bqId;
58 int32_t bqSlot;
59 _UnwrapNativeCodec2GrallocMetadata(
60 block.handle(), &width, &height, &format, &usage,
61 &stride, &generation, &bqId, reinterpret_cast<uint32_t*>(&bqSlot));
62 native_handle_t *grallocHandle =
63 UnwrapNativeCodec2GrallocHandle(block.handle());
64 sp<GraphicBuffer> graphicBuffer =
65 new GraphicBuffer(grallocHandle,
66 GraphicBuffer::CLONE_HANDLE,
67 width, height, format,
68 1, usage, stride);
69 native_handle_delete(grallocHandle);
70 return graphicBuffer;
71}
72
73template <typename BlockProcessor>
74void forEachBlock(C2FrameData& frameData,
75 BlockProcessor process) {
76 for (const std::shared_ptr<C2Buffer>& buffer : frameData.buffers) {
77 if (buffer) {
78 for (const C2ConstGraphicBlock& block :
79 buffer->data().graphicBlocks()) {
80 process(block);
81 }
82 }
83 }
84}
85
86template <typename BlockProcessor>
87void forEachBlock(const std::list<std::unique_ptr<C2Work>>& workList,
88 BlockProcessor process) {
89 for (const std::unique_ptr<C2Work>& work : workList) {
90 if (!work) {
91 continue;
92 }
93 for (const std::unique_ptr<C2Worklet>& worklet : work->worklets) {
94 if (worklet) {
95 forEachBlock(worklet->output, process);
96 }
97 }
98 }
99}
100
101sp<HGraphicBufferProducer> getHgbp(const sp<IGraphicBufferProducer>& igbp) {
102 sp<HGraphicBufferProducer> hgbp =
103 igbp->getHalInterface<HGraphicBufferProducer>();
104 return hgbp ? hgbp :
105 new B2HGraphicBufferProducer(igbp);
106}
107
108status_t attachToBufferQueue(const C2ConstGraphicBlock& block,
109 const sp<IGraphicBufferProducer>& igbp,
110 uint32_t generation,
Sungtak Leea714f112021-03-16 05:40:03 -0700111 int32_t* bqSlot,
112 std::shared_ptr<C2SurfaceSyncMemory> syncMem) {
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700113 if (!igbp) {
114 LOG(WARNING) << "attachToBufferQueue -- null producer.";
115 return NO_INIT;
116 }
117
118 sp<GraphicBuffer> graphicBuffer = createGraphicBuffer(block);
119 graphicBuffer->setGenerationNumber(generation);
120
121 LOG(VERBOSE) << "attachToBufferQueue -- attaching buffer:"
122 << " block dimension " << block.width() << "x"
123 << block.height()
124 << ", graphicBuffer dimension " << graphicBuffer->getWidth() << "x"
125 << graphicBuffer->getHeight()
126 << std::hex << std::setfill('0')
127 << ", format 0x" << std::setw(8) << graphicBuffer->getPixelFormat()
128 << ", usage 0x" << std::setw(16) << graphicBuffer->getUsage()
129 << std::dec << std::setfill(' ')
130 << ", stride " << graphicBuffer->getStride()
131 << ", generation " << graphicBuffer->getGenerationNumber();
132
Sungtak Leea714f112021-03-16 05:40:03 -0700133 C2SyncVariables *syncVar = syncMem ? syncMem->mem() : nullptr;
134 status_t result = OK;
135 if (syncVar) {
136 syncVar->lock();
137 if (!syncVar->isDequeueableLocked() ||
138 syncVar->getSyncStatusLocked() == C2SyncVariables::STATUS_SWITCHING) {
139 syncVar->unlock();
140 LOG(WARNING) << "attachToBufferQueue -- attachBuffer failed: "
141 "status = " << INVALID_OPERATION << ".";
142 return INVALID_OPERATION;
143 }
144 result = igbp->attachBuffer(bqSlot, graphicBuffer);
145 if (result == OK) {
146 syncVar->notifyDequeuedLocked();
147 }
148 syncVar->unlock();
149 } else {
150 result = igbp->attachBuffer(bqSlot, graphicBuffer);
151 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700152 if (result != OK) {
153 LOG(WARNING) << "attachToBufferQueue -- attachBuffer failed: "
154 "status = " << result << ".";
155 return result;
156 }
157 LOG(VERBOSE) << "attachToBufferQueue -- attachBuffer returned slot #"
158 << *bqSlot << ".";
159 return OK;
160}
161
162bool getBufferQueueAssignment(const C2ConstGraphicBlock& block,
163 uint32_t* generation,
164 uint64_t* bqId,
165 int32_t* bqSlot) {
166 return _C2BlockFactory::GetBufferQueueData(
167 _C2BlockFactory::GetGraphicBlockPoolData(block),
168 generation, bqId, bqSlot);
169}
170
171} // unnamed namespace
172
173OutputBufferQueue::OutputBufferQueue()
Sungtak Leec7da7a02022-05-05 08:45:33 +0000174 : mGeneration{0}, mBqId{0}, mStopped{false} {
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700175}
176
177OutputBufferQueue::~OutputBufferQueue() {
178}
179
180bool OutputBufferQueue::configure(const sp<IGraphicBufferProducer>& igbp,
181 uint32_t generation,
Sungtak Leea714f112021-03-16 05:40:03 -0700182 uint64_t bqId,
Sungtak Leedb14cba2021-04-10 00:50:23 -0700183 int maxDequeueBufferCount,
Sungtak Leea714f112021-03-16 05:40:03 -0700184 std::shared_ptr<V1_2::SurfaceSyncObj> *syncObj) {
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900185 uint64_t consumerUsage = 0;
Sungtak Leeb6388022021-07-21 00:40:26 -0700186 if (igbp && igbp->getConsumerUsage(&consumerUsage) != OK) {
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900187 ALOGW("failed to get consumer usage");
188 }
189
Sungtak Leea714f112021-03-16 05:40:03 -0700190 // TODO : Abstract creation process into C2SurfaceSyncMemory class.
191 // use C2LinearBlock instead ashmem.
192 std::shared_ptr<C2SurfaceSyncMemory> syncMem;
193 if (syncObj && igbp) {
194 bool mapped = false;
195 int memFd = ashmem_create_region("C2SurfaceMem", sizeof(C2SyncVariables));
196 size_t memSize = memFd < 0 ? 0 : ashmem_get_size_region(memFd);
197 if (memSize > 0) {
198 syncMem = C2SurfaceSyncMemory::Create(memFd, memSize);
199 if (syncMem) {
200 mapped = true;
201 *syncObj = std::make_shared<V1_2::SurfaceSyncObj>();
202 (*syncObj)->syncMemory = syncMem->handle();
203 (*syncObj)->bqId = bqId;
204 (*syncObj)->generationId = generation;
205 (*syncObj)->consumerUsage = consumerUsage;
206 ALOGD("C2SurfaceSyncMemory created %zu(%zu)", sizeof(C2SyncVariables), memSize);
207 }
208 }
209 if (!mapped) {
210 if (memFd >= 0) {
211 ::close(memFd);
212 }
213 ALOGW("SurfaceSyncObj creation failure");
214 }
215 }
216
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700217 size_t tryNum = 0;
218 size_t success = 0;
219 sp<GraphicBuffer> buffers[BufferQueueDefs::NUM_BUFFER_SLOTS];
220 std::weak_ptr<_C2BlockPoolData>
221 poolDatas[BufferQueueDefs::NUM_BUFFER_SLOTS];
Sungtak Lee3cc67e72023-05-08 17:00:05 +0000222 std::shared_ptr<C2SurfaceSyncMemory> oldMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700223 {
224 std::scoped_lock<std::mutex> l(mMutex);
Sungtak Leec7da7a02022-05-05 08:45:33 +0000225 bool stopped = mStopped;
226 mStopped = false;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700227 if (generation == mGeneration) {
Sungtak Leedb14cba2021-04-10 00:50:23 -0700228 // case of old BlockPool destruction
229 C2SyncVariables *var = mSyncMem ? mSyncMem->mem() : nullptr;
George Burgess IV3defea72021-04-23 18:43:57 -0700230 if (syncObj && var) {
Sungtak Leedb14cba2021-04-10 00:50:23 -0700231 *syncObj = std::make_shared<V1_2::SurfaceSyncObj>();
232 (*syncObj)->bqId = bqId;
233 (*syncObj)->syncMemory = mSyncMem->handle();
234 (*syncObj)->generationId = generation;
235 (*syncObj)->consumerUsage = consumerUsage;
236 mMaxDequeueBufferCount = maxDequeueBufferCount;
237 var->lock();
238 var->setSyncStatusLocked(C2SyncVariables::STATUS_INIT);
239 var->setInitialDequeueCountLocked(mMaxDequeueBufferCount, 0);
240 var->unlock();
241 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700242 return false;
243 }
Sungtak Lee3cc67e72023-05-08 17:00:05 +0000244 oldMem = mSyncMem;
Sungtak Leea714f112021-03-16 05:40:03 -0700245 C2SyncVariables *oldSync = mSyncMem ? mSyncMem->mem() : nullptr;
246 if (oldSync) {
247 oldSync->lock();
248 oldSync->setSyncStatusLocked(C2SyncVariables::STATUS_SWITCHING);
249 oldSync->unlock();
250 }
251 mSyncMem.reset();
252 if (syncMem) {
253 mSyncMem = syncMem;
254 }
255 C2SyncVariables *newSync = mSyncMem ? mSyncMem->mem() : nullptr;
256
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700257 mIgbp = igbp;
258 mGeneration = generation;
259 mBqId = bqId;
260 mOwner = std::make_shared<int>(0);
Sungtak Leedb14cba2021-04-10 00:50:23 -0700261 mMaxDequeueBufferCount = maxDequeueBufferCount;
Sungtak Leeb6388022021-07-21 00:40:26 -0700262 if (igbp == nullptr) {
263 return false;
264 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700265 for (int i = 0; i < BufferQueueDefs::NUM_BUFFER_SLOTS; ++i) {
Sungtak Leec7da7a02022-05-05 08:45:33 +0000266 if (mBqId == 0 || !mBuffers[i] || stopped) {
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700267 continue;
268 }
269 std::shared_ptr<_C2BlockPoolData> data = mPoolDatas[i].lock();
270 if (!data ||
271 !_C2BlockFactory::BeginAttachBlockToBufferQueue(data)) {
272 continue;
273 }
274 ++tryNum;
275 int bqSlot;
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900276
277 // Update buffer's generation and usage.
278 if ((mBuffers[i]->getUsage() & consumerUsage) != consumerUsage) {
279 mBuffers[i] = new GraphicBuffer(
280 mBuffers[i]->handle, GraphicBuffer::CLONE_HANDLE,
281 mBuffers[i]->width, mBuffers[i]->height,
282 mBuffers[i]->format, mBuffers[i]->layerCount,
283 mBuffers[i]->getUsage() | consumerUsage,
284 mBuffers[i]->stride);
285 if (mBuffers[i]->initCheck() != OK) {
286 ALOGW("%s() failed to update usage, original usage=%" PRIx64
287 ", consumer usage=%" PRIx64,
288 __func__, mBuffers[i]->getUsage(), consumerUsage);
289 continue;
290 }
291 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700292 mBuffers[i]->setGenerationNumber(generation);
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900293
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700294 status_t result = igbp->attachBuffer(&bqSlot, mBuffers[i]);
295 if (result != OK) {
296 continue;
297 }
298 bool attach =
299 _C2BlockFactory::EndAttachBlockToBufferQueue(
Sungtak Leea714f112021-03-16 05:40:03 -0700300 data, mOwner, getHgbp(mIgbp), mSyncMem,
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700301 generation, bqId, bqSlot);
302 if (!attach) {
303 igbp->cancelBuffer(bqSlot, Fence::NO_FENCE);
304 continue;
305 }
306 buffers[bqSlot] = mBuffers[i];
307 poolDatas[bqSlot] = data;
308 ++success;
309 }
310 for (int i = 0; i < BufferQueueDefs::NUM_BUFFER_SLOTS; ++i) {
311 mBuffers[i] = buffers[i];
312 mPoolDatas[i] = poolDatas[i];
313 }
Sungtak Leea714f112021-03-16 05:40:03 -0700314 if (newSync) {
Sungtak Leedb14cba2021-04-10 00:50:23 -0700315 newSync->lock();
316 newSync->setInitialDequeueCountLocked(mMaxDequeueBufferCount, success);
317 newSync->unlock();
Sungtak Leea714f112021-03-16 05:40:03 -0700318 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700319 }
Sungtak Lee3cc67e72023-05-08 17:00:05 +0000320 {
321 std::scoped_lock<std::mutex> l(mOldMutex);
322 mOldMem = oldMem;
323 }
Sungtak Leea714f112021-03-16 05:40:03 -0700324 ALOGD("remote graphic buffer migration %zu/%zu",
325 success, tryNum);
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700326 return true;
327}
328
Sungtak Lee3cc67e72023-05-08 17:00:05 +0000329void OutputBufferQueue::expireOldWaiters() {
330 std::scoped_lock<std::mutex> l(mOldMutex);
331 if (mOldMem) {
332 C2SyncVariables *oldSync = mOldMem->mem();
333 if (oldSync) {
334 oldSync->notifyAll();
335 }
336 mOldMem.reset();
337 }
338}
339
Sungtak Leec7da7a02022-05-05 08:45:33 +0000340void OutputBufferQueue::stop() {
Sungtak Leec0c05962023-10-25 08:14:13 +0000341 std::shared_ptr<C2SurfaceSyncMemory> oldMem;
342 {
343 std::scoped_lock<std::mutex> l(mMutex);
344 if (mStopped) {
345 return;
346 }
347 mStopped = true;
348 mOwner.reset(); // destructor of the block will not trigger IGBP::cancel()
349 // basically configuring null surface
350 oldMem = mSyncMem;
351 mSyncMem.reset();
352 mIgbp.clear();
353 mGeneration = 0;
354 mBqId = 0;
355 }
356 {
357 std::scoped_lock<std::mutex> l(mOldMutex);
358 mOldMem = oldMem;
359 }
Sungtak Leec7da7a02022-05-05 08:45:33 +0000360}
361
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700362bool OutputBufferQueue::registerBuffer(const C2ConstGraphicBlock& block) {
363 std::shared_ptr<_C2BlockPoolData> data =
364 _C2BlockFactory::GetGraphicBlockPoolData(block);
365 if (!data) {
366 return false;
367 }
368 std::scoped_lock<std::mutex> l(mMutex);
369
Sungtak Leec7da7a02022-05-05 08:45:33 +0000370 if (!mIgbp || mStopped) {
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700371 return false;
372 }
373
374 uint32_t oldGeneration;
375 uint64_t oldId;
376 int32_t oldSlot;
377 // If the block is not bufferqueue-based, do nothing.
378 if (!_C2BlockFactory::GetBufferQueueData(
379 data, &oldGeneration, &oldId, &oldSlot) || (oldId == 0)) {
380 return false;
381 }
382 // If the block's bqId is the same as the desired bqId, just hold.
383 if ((oldId == mBqId) && (oldGeneration == mGeneration)) {
384 LOG(VERBOSE) << "holdBufferQueueBlock -- import without attaching:"
385 << " bqId " << oldId
386 << ", bqSlot " << oldSlot
387 << ", generation " << mGeneration
388 << ".";
Sungtak Leea714f112021-03-16 05:40:03 -0700389 _C2BlockFactory::HoldBlockFromBufferQueue(data, mOwner, getHgbp(mIgbp), mSyncMem);
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700390 mPoolDatas[oldSlot] = data;
391 mBuffers[oldSlot] = createGraphicBuffer(block);
392 mBuffers[oldSlot]->setGenerationNumber(mGeneration);
393 return true;
394 }
395 int32_t d = (int32_t) mGeneration - (int32_t) oldGeneration;
396 LOG(WARNING) << "receiving stale buffer: generation "
397 << mGeneration << " , diff " << d << " : slot "
398 << oldSlot;
399 return false;
400}
401
402status_t OutputBufferQueue::outputBuffer(
403 const C2ConstGraphicBlock& block,
404 const BnGraphicBufferProducer::QueueBufferInput& input,
405 BnGraphicBufferProducer::QueueBufferOutput* output) {
406 uint32_t generation;
407 uint64_t bqId;
408 int32_t bqSlot;
Arun Johnson7ba67072023-11-06 22:23:04 +0000409 ScopedTrace trace(ATRACE_TAG,"Codec2-OutputBufferQueue::outputBuffer");
Sungtak Leea714f112021-03-16 05:40:03 -0700410 bool display = V1_0::utils::displayBufferQueueBlock(block);
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700411 if (!getBufferQueueAssignment(block, &generation, &bqId, &bqSlot) ||
412 bqId == 0) {
413 // Block not from bufferqueue -- it must be attached before queuing.
414
Sungtak Leea714f112021-03-16 05:40:03 -0700415 std::shared_ptr<C2SurfaceSyncMemory> syncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700416 mMutex.lock();
Sungtak Leec7da7a02022-05-05 08:45:33 +0000417 bool stopped = mStopped;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700418 sp<IGraphicBufferProducer> outputIgbp = mIgbp;
419 uint32_t outputGeneration = mGeneration;
Sungtak Leea714f112021-03-16 05:40:03 -0700420 syncMem = mSyncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700421 mMutex.unlock();
422
Sungtak Leec7da7a02022-05-05 08:45:33 +0000423 if (stopped) {
424 LOG(INFO) << "outputBuffer -- already stopped.";
425 return DEAD_OBJECT;
426 }
427
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700428 status_t status = attachToBufferQueue(
Sungtak Leea714f112021-03-16 05:40:03 -0700429 block, outputIgbp, outputGeneration, &bqSlot, syncMem);
430
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700431 if (status != OK) {
432 LOG(WARNING) << "outputBuffer -- attaching failed.";
433 return INVALID_OPERATION;
434 }
435
Sungtak Leea714f112021-03-16 05:40:03 -0700436 auto syncVar = syncMem ? syncMem->mem() : nullptr;
437 if(syncVar) {
438 syncVar->lock();
439 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
440 input, output);
441 if (status == OK) {
442 syncVar->notifyQueuedLocked();
443 }
444 syncVar->unlock();
445 } else {
446 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
447 input, output);
448 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700449 if (status != OK) {
450 LOG(ERROR) << "outputBuffer -- queueBuffer() failed "
451 "on non-bufferqueue-based block. "
452 "Error = " << status << ".";
453 return status;
454 }
455 return OK;
456 }
457
Sungtak Leea714f112021-03-16 05:40:03 -0700458 std::shared_ptr<C2SurfaceSyncMemory> syncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700459 mMutex.lock();
Sungtak Leec7da7a02022-05-05 08:45:33 +0000460 bool stopped = mStopped;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700461 sp<IGraphicBufferProducer> outputIgbp = mIgbp;
462 uint32_t outputGeneration = mGeneration;
463 uint64_t outputBqId = mBqId;
Sungtak Leea714f112021-03-16 05:40:03 -0700464 syncMem = mSyncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700465 mMutex.unlock();
466
Sungtak Leec7da7a02022-05-05 08:45:33 +0000467 if (stopped) {
468 LOG(INFO) << "outputBuffer -- already stopped.";
469 return DEAD_OBJECT;
470 }
471
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700472 if (!outputIgbp) {
473 LOG(VERBOSE) << "outputBuffer -- output surface is null.";
474 return NO_INIT;
475 }
476
477 if (!display) {
478 LOG(WARNING) << "outputBuffer -- cannot display "
479 "bufferqueue-based block to the bufferqueue.";
480 return UNKNOWN_ERROR;
481 }
482 if (bqId != outputBqId || generation != outputGeneration) {
483 int32_t diff = (int32_t) outputGeneration - (int32_t) generation;
484 LOG(WARNING) << "outputBuffer -- buffers from old generation to "
485 << outputGeneration << " , diff: " << diff
486 << " , slot: " << bqSlot;
487 return DEAD_OBJECT;
488 }
489
Sungtak Leea714f112021-03-16 05:40:03 -0700490 auto syncVar = syncMem ? syncMem->mem() : nullptr;
491 status_t status = OK;
492 if (syncVar) {
493 syncVar->lock();
494 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
495 input, output);
496 if (status == OK) {
497 syncVar->notifyQueuedLocked();
498 }
499 syncVar->unlock();
500 } else {
501 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
502 input, output);
503 }
504
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700505 if (status != OK) {
506 LOG(ERROR) << "outputBuffer -- queueBuffer() failed "
507 "on bufferqueue-based block. "
508 "Error = " << status << ".";
509 return status;
510 }
511 return OK;
512}
513
Brian Lindahl932bf602023-03-09 11:59:48 -0700514void OutputBufferQueue::pollForRenderedFrames(FrameEventHistoryDelta* delta) {
515 if (mIgbp) {
516 mIgbp->getFrameTimestamps(delta);
517 }
518}
519
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700520void OutputBufferQueue::holdBufferQueueBlocks(
521 const std::list<std::unique_ptr<C2Work>>& workList) {
522 forEachBlock(workList,
523 std::bind(&OutputBufferQueue::registerBuffer,
524 this, std::placeholders::_1));
525}
526
Sungtak Leea714f112021-03-16 05:40:03 -0700527void OutputBufferQueue::updateMaxDequeueBufferCount(int maxDequeueBufferCount) {
528 mMutex.lock();
529 mMaxDequeueBufferCount = maxDequeueBufferCount;
530 auto syncVar = mSyncMem ? mSyncMem->mem() : nullptr;
Sungtak Leec7da7a02022-05-05 08:45:33 +0000531 if (syncVar && !mStopped) {
Sungtak Leea714f112021-03-16 05:40:03 -0700532 syncVar->lock();
533 syncVar->updateMaxDequeueCountLocked(maxDequeueBufferCount);
534 syncVar->unlock();
535 }
536 mMutex.unlock();
Sungtak Leedb14cba2021-04-10 00:50:23 -0700537 ALOGD("set max dequeue count %d from update", maxDequeueBufferCount);
Sungtak Leea714f112021-03-16 05:40:03 -0700538}
539
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700540} // namespace c2
541} // namespace media
542} // namespace hardware
543} // namespace android