blob: 5a4ed958e2254b14e7c6c80feb9df0d013d6e757 [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 }
Sungtak Lee849fb752023-11-14 20:46:33 +0000144 syncVar->notifyDequeuedLocked();
Sungtak Leea714f112021-03-16 05:40:03 -0700145 syncVar->unlock();
Sungtak Lee849fb752023-11-14 20:46:33 +0000146 result = igbp->attachBuffer(bqSlot, graphicBuffer);
147 if (result != OK) {
148 syncVar->lock();
149 syncVar->notifyQueuedLocked();
150 syncVar->unlock();
151 }
Sungtak Leea714f112021-03-16 05:40:03 -0700152 } else {
153 result = igbp->attachBuffer(bqSlot, graphicBuffer);
154 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700155 if (result != OK) {
156 LOG(WARNING) << "attachToBufferQueue -- attachBuffer failed: "
157 "status = " << result << ".";
158 return result;
159 }
160 LOG(VERBOSE) << "attachToBufferQueue -- attachBuffer returned slot #"
161 << *bqSlot << ".";
162 return OK;
163}
164
165bool getBufferQueueAssignment(const C2ConstGraphicBlock& block,
166 uint32_t* generation,
167 uint64_t* bqId,
168 int32_t* bqSlot) {
169 return _C2BlockFactory::GetBufferQueueData(
170 _C2BlockFactory::GetGraphicBlockPoolData(block),
171 generation, bqId, bqSlot);
172}
173
174} // unnamed namespace
175
176OutputBufferQueue::OutputBufferQueue()
Sungtak Leec7da7a02022-05-05 08:45:33 +0000177 : mGeneration{0}, mBqId{0}, mStopped{false} {
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700178}
179
180OutputBufferQueue::~OutputBufferQueue() {
181}
182
183bool OutputBufferQueue::configure(const sp<IGraphicBufferProducer>& igbp,
184 uint32_t generation,
Sungtak Leea714f112021-03-16 05:40:03 -0700185 uint64_t bqId,
Sungtak Leedb14cba2021-04-10 00:50:23 -0700186 int maxDequeueBufferCount,
Sungtak Leea714f112021-03-16 05:40:03 -0700187 std::shared_ptr<V1_2::SurfaceSyncObj> *syncObj) {
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900188 uint64_t consumerUsage = 0;
Sungtak Leeb6388022021-07-21 00:40:26 -0700189 if (igbp && igbp->getConsumerUsage(&consumerUsage) != OK) {
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900190 ALOGW("failed to get consumer usage");
191 }
192
Sungtak Leea714f112021-03-16 05:40:03 -0700193 // TODO : Abstract creation process into C2SurfaceSyncMemory class.
194 // use C2LinearBlock instead ashmem.
195 std::shared_ptr<C2SurfaceSyncMemory> syncMem;
196 if (syncObj && igbp) {
197 bool mapped = false;
198 int memFd = ashmem_create_region("C2SurfaceMem", sizeof(C2SyncVariables));
199 size_t memSize = memFd < 0 ? 0 : ashmem_get_size_region(memFd);
200 if (memSize > 0) {
201 syncMem = C2SurfaceSyncMemory::Create(memFd, memSize);
202 if (syncMem) {
203 mapped = true;
204 *syncObj = std::make_shared<V1_2::SurfaceSyncObj>();
205 (*syncObj)->syncMemory = syncMem->handle();
206 (*syncObj)->bqId = bqId;
207 (*syncObj)->generationId = generation;
208 (*syncObj)->consumerUsage = consumerUsage;
209 ALOGD("C2SurfaceSyncMemory created %zu(%zu)", sizeof(C2SyncVariables), memSize);
210 }
211 }
212 if (!mapped) {
213 if (memFd >= 0) {
214 ::close(memFd);
215 }
216 ALOGW("SurfaceSyncObj creation failure");
217 }
218 }
219
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700220 size_t tryNum = 0;
221 size_t success = 0;
222 sp<GraphicBuffer> buffers[BufferQueueDefs::NUM_BUFFER_SLOTS];
223 std::weak_ptr<_C2BlockPoolData>
224 poolDatas[BufferQueueDefs::NUM_BUFFER_SLOTS];
Sungtak Lee3cc67e72023-05-08 17:00:05 +0000225 std::shared_ptr<C2SurfaceSyncMemory> oldMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700226 {
227 std::scoped_lock<std::mutex> l(mMutex);
Sungtak Leec7da7a02022-05-05 08:45:33 +0000228 bool stopped = mStopped;
229 mStopped = false;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700230 if (generation == mGeneration) {
Sungtak Leedb14cba2021-04-10 00:50:23 -0700231 // case of old BlockPool destruction
232 C2SyncVariables *var = mSyncMem ? mSyncMem->mem() : nullptr;
George Burgess IV3defea72021-04-23 18:43:57 -0700233 if (syncObj && var) {
Sungtak Leedb14cba2021-04-10 00:50:23 -0700234 *syncObj = std::make_shared<V1_2::SurfaceSyncObj>();
235 (*syncObj)->bqId = bqId;
236 (*syncObj)->syncMemory = mSyncMem->handle();
237 (*syncObj)->generationId = generation;
238 (*syncObj)->consumerUsage = consumerUsage;
239 mMaxDequeueBufferCount = maxDequeueBufferCount;
240 var->lock();
241 var->setSyncStatusLocked(C2SyncVariables::STATUS_INIT);
242 var->setInitialDequeueCountLocked(mMaxDequeueBufferCount, 0);
243 var->unlock();
244 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700245 return false;
246 }
Sungtak Lee3cc67e72023-05-08 17:00:05 +0000247 oldMem = mSyncMem;
Sungtak Leea714f112021-03-16 05:40:03 -0700248 C2SyncVariables *oldSync = mSyncMem ? mSyncMem->mem() : nullptr;
249 if (oldSync) {
250 oldSync->lock();
251 oldSync->setSyncStatusLocked(C2SyncVariables::STATUS_SWITCHING);
252 oldSync->unlock();
253 }
254 mSyncMem.reset();
255 if (syncMem) {
256 mSyncMem = syncMem;
257 }
258 C2SyncVariables *newSync = mSyncMem ? mSyncMem->mem() : nullptr;
259
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700260 mIgbp = igbp;
261 mGeneration = generation;
262 mBqId = bqId;
263 mOwner = std::make_shared<int>(0);
Sungtak Leedb14cba2021-04-10 00:50:23 -0700264 mMaxDequeueBufferCount = maxDequeueBufferCount;
Sungtak Leeb6388022021-07-21 00:40:26 -0700265 if (igbp == nullptr) {
266 return false;
267 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700268 for (int i = 0; i < BufferQueueDefs::NUM_BUFFER_SLOTS; ++i) {
Sungtak Leec7da7a02022-05-05 08:45:33 +0000269 if (mBqId == 0 || !mBuffers[i] || stopped) {
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700270 continue;
271 }
272 std::shared_ptr<_C2BlockPoolData> data = mPoolDatas[i].lock();
273 if (!data ||
274 !_C2BlockFactory::BeginAttachBlockToBufferQueue(data)) {
275 continue;
276 }
277 ++tryNum;
278 int bqSlot;
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900279
280 // Update buffer's generation and usage.
281 if ((mBuffers[i]->getUsage() & consumerUsage) != consumerUsage) {
282 mBuffers[i] = new GraphicBuffer(
283 mBuffers[i]->handle, GraphicBuffer::CLONE_HANDLE,
284 mBuffers[i]->width, mBuffers[i]->height,
285 mBuffers[i]->format, mBuffers[i]->layerCount,
286 mBuffers[i]->getUsage() | consumerUsage,
287 mBuffers[i]->stride);
288 if (mBuffers[i]->initCheck() != OK) {
289 ALOGW("%s() failed to update usage, original usage=%" PRIx64
290 ", consumer usage=%" PRIx64,
291 __func__, mBuffers[i]->getUsage(), consumerUsage);
292 continue;
293 }
294 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700295 mBuffers[i]->setGenerationNumber(generation);
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900296
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700297 status_t result = igbp->attachBuffer(&bqSlot, mBuffers[i]);
298 if (result != OK) {
299 continue;
300 }
301 bool attach =
302 _C2BlockFactory::EndAttachBlockToBufferQueue(
Sungtak Leea714f112021-03-16 05:40:03 -0700303 data, mOwner, getHgbp(mIgbp), mSyncMem,
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700304 generation, bqId, bqSlot);
305 if (!attach) {
306 igbp->cancelBuffer(bqSlot, Fence::NO_FENCE);
307 continue;
308 }
309 buffers[bqSlot] = mBuffers[i];
310 poolDatas[bqSlot] = data;
311 ++success;
312 }
313 for (int i = 0; i < BufferQueueDefs::NUM_BUFFER_SLOTS; ++i) {
314 mBuffers[i] = buffers[i];
315 mPoolDatas[i] = poolDatas[i];
316 }
Sungtak Leea714f112021-03-16 05:40:03 -0700317 if (newSync) {
Sungtak Leedb14cba2021-04-10 00:50:23 -0700318 newSync->lock();
319 newSync->setInitialDequeueCountLocked(mMaxDequeueBufferCount, success);
320 newSync->unlock();
Sungtak Leea714f112021-03-16 05:40:03 -0700321 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700322 }
Sungtak Lee3cc67e72023-05-08 17:00:05 +0000323 {
324 std::scoped_lock<std::mutex> l(mOldMutex);
325 mOldMem = oldMem;
326 }
Sungtak Leea714f112021-03-16 05:40:03 -0700327 ALOGD("remote graphic buffer migration %zu/%zu",
328 success, tryNum);
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700329 return true;
330}
331
Sungtak Lee3cc67e72023-05-08 17:00:05 +0000332void OutputBufferQueue::expireOldWaiters() {
333 std::scoped_lock<std::mutex> l(mOldMutex);
334 if (mOldMem) {
335 C2SyncVariables *oldSync = mOldMem->mem();
336 if (oldSync) {
337 oldSync->notifyAll();
338 }
339 mOldMem.reset();
340 }
341}
342
Sungtak Leec7da7a02022-05-05 08:45:33 +0000343void OutputBufferQueue::stop() {
Sungtak Leec0c05962023-10-25 08:14:13 +0000344 std::shared_ptr<C2SurfaceSyncMemory> oldMem;
345 {
346 std::scoped_lock<std::mutex> l(mMutex);
347 if (mStopped) {
348 return;
349 }
350 mStopped = true;
351 mOwner.reset(); // destructor of the block will not trigger IGBP::cancel()
352 // basically configuring null surface
353 oldMem = mSyncMem;
354 mSyncMem.reset();
355 mIgbp.clear();
356 mGeneration = 0;
357 mBqId = 0;
358 }
359 {
360 std::scoped_lock<std::mutex> l(mOldMutex);
361 mOldMem = oldMem;
362 }
Sungtak Leec7da7a02022-05-05 08:45:33 +0000363}
364
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700365bool OutputBufferQueue::registerBuffer(const C2ConstGraphicBlock& block) {
366 std::shared_ptr<_C2BlockPoolData> data =
367 _C2BlockFactory::GetGraphicBlockPoolData(block);
368 if (!data) {
369 return false;
370 }
371 std::scoped_lock<std::mutex> l(mMutex);
372
Sungtak Leec7da7a02022-05-05 08:45:33 +0000373 if (!mIgbp || mStopped) {
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700374 return false;
375 }
376
377 uint32_t oldGeneration;
378 uint64_t oldId;
379 int32_t oldSlot;
380 // If the block is not bufferqueue-based, do nothing.
381 if (!_C2BlockFactory::GetBufferQueueData(
382 data, &oldGeneration, &oldId, &oldSlot) || (oldId == 0)) {
383 return false;
384 }
385 // If the block's bqId is the same as the desired bqId, just hold.
386 if ((oldId == mBqId) && (oldGeneration == mGeneration)) {
387 LOG(VERBOSE) << "holdBufferQueueBlock -- import without attaching:"
388 << " bqId " << oldId
389 << ", bqSlot " << oldSlot
390 << ", generation " << mGeneration
391 << ".";
Sungtak Leea714f112021-03-16 05:40:03 -0700392 _C2BlockFactory::HoldBlockFromBufferQueue(data, mOwner, getHgbp(mIgbp), mSyncMem);
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700393 mPoolDatas[oldSlot] = data;
394 mBuffers[oldSlot] = createGraphicBuffer(block);
395 mBuffers[oldSlot]->setGenerationNumber(mGeneration);
396 return true;
397 }
398 int32_t d = (int32_t) mGeneration - (int32_t) oldGeneration;
399 LOG(WARNING) << "receiving stale buffer: generation "
400 << mGeneration << " , diff " << d << " : slot "
401 << oldSlot;
402 return false;
403}
404
405status_t OutputBufferQueue::outputBuffer(
406 const C2ConstGraphicBlock& block,
407 const BnGraphicBufferProducer::QueueBufferInput& input,
408 BnGraphicBufferProducer::QueueBufferOutput* output) {
409 uint32_t generation;
410 uint64_t bqId;
411 int32_t bqSlot;
Arun Johnson7ba67072023-11-06 22:23:04 +0000412 ScopedTrace trace(ATRACE_TAG,"Codec2-OutputBufferQueue::outputBuffer");
Sungtak Leea714f112021-03-16 05:40:03 -0700413 bool display = V1_0::utils::displayBufferQueueBlock(block);
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700414 if (!getBufferQueueAssignment(block, &generation, &bqId, &bqSlot) ||
415 bqId == 0) {
416 // Block not from bufferqueue -- it must be attached before queuing.
417
Sungtak Leea714f112021-03-16 05:40:03 -0700418 std::shared_ptr<C2SurfaceSyncMemory> syncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700419 mMutex.lock();
Sungtak Leec7da7a02022-05-05 08:45:33 +0000420 bool stopped = mStopped;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700421 sp<IGraphicBufferProducer> outputIgbp = mIgbp;
422 uint32_t outputGeneration = mGeneration;
Sungtak Leea714f112021-03-16 05:40:03 -0700423 syncMem = mSyncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700424 mMutex.unlock();
425
Sungtak Leec7da7a02022-05-05 08:45:33 +0000426 if (stopped) {
427 LOG(INFO) << "outputBuffer -- already stopped.";
428 return DEAD_OBJECT;
429 }
430
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700431 status_t status = attachToBufferQueue(
Sungtak Leea714f112021-03-16 05:40:03 -0700432 block, outputIgbp, outputGeneration, &bqSlot, syncMem);
433
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700434 if (status != OK) {
435 LOG(WARNING) << "outputBuffer -- attaching failed.";
436 return INVALID_OPERATION;
437 }
438
Sungtak Leea714f112021-03-16 05:40:03 -0700439 auto syncVar = syncMem ? syncMem->mem() : nullptr;
440 if(syncVar) {
Sungtak Leea714f112021-03-16 05:40:03 -0700441 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
442 input, output);
443 if (status == OK) {
Sungtak Lee7fefd562023-11-21 02:48:08 +0000444 if (output->bufferReplaced) {
445 syncVar->lock();
446 syncVar->notifyQueuedLocked();
447 syncVar->unlock();
448 }
Sungtak Leea714f112021-03-16 05:40:03 -0700449 }
Sungtak Leea714f112021-03-16 05:40:03 -0700450 } else {
451 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
452 input, output);
453 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700454 if (status != OK) {
455 LOG(ERROR) << "outputBuffer -- queueBuffer() failed "
456 "on non-bufferqueue-based block. "
457 "Error = " << status << ".";
458 return status;
459 }
460 return OK;
461 }
462
Sungtak Leea714f112021-03-16 05:40:03 -0700463 std::shared_ptr<C2SurfaceSyncMemory> syncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700464 mMutex.lock();
Sungtak Leec7da7a02022-05-05 08:45:33 +0000465 bool stopped = mStopped;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700466 sp<IGraphicBufferProducer> outputIgbp = mIgbp;
467 uint32_t outputGeneration = mGeneration;
468 uint64_t outputBqId = mBqId;
Sungtak Leea714f112021-03-16 05:40:03 -0700469 syncMem = mSyncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700470 mMutex.unlock();
471
Sungtak Leec7da7a02022-05-05 08:45:33 +0000472 if (stopped) {
473 LOG(INFO) << "outputBuffer -- already stopped.";
474 return DEAD_OBJECT;
475 }
476
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700477 if (!outputIgbp) {
478 LOG(VERBOSE) << "outputBuffer -- output surface is null.";
479 return NO_INIT;
480 }
481
482 if (!display) {
483 LOG(WARNING) << "outputBuffer -- cannot display "
484 "bufferqueue-based block to the bufferqueue.";
485 return UNKNOWN_ERROR;
486 }
487 if (bqId != outputBqId || generation != outputGeneration) {
488 int32_t diff = (int32_t) outputGeneration - (int32_t) generation;
489 LOG(WARNING) << "outputBuffer -- buffers from old generation to "
490 << outputGeneration << " , diff: " << diff
491 << " , slot: " << bqSlot;
492 return DEAD_OBJECT;
493 }
494
Sungtak Leea714f112021-03-16 05:40:03 -0700495 auto syncVar = syncMem ? syncMem->mem() : nullptr;
496 status_t status = OK;
497 if (syncVar) {
Sungtak Leea714f112021-03-16 05:40:03 -0700498 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
499 input, output);
500 if (status == OK) {
Sungtak Lee7fefd562023-11-21 02:48:08 +0000501 if (output->bufferReplaced) {
502 syncVar->lock();
503 syncVar->notifyQueuedLocked();
504 syncVar->unlock();
505 }
Sungtak Leea714f112021-03-16 05:40:03 -0700506 }
Sungtak Leea714f112021-03-16 05:40:03 -0700507 } else {
508 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
509 input, output);
510 }
511
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700512 if (status != OK) {
513 LOG(ERROR) << "outputBuffer -- queueBuffer() failed "
514 "on bufferqueue-based block. "
515 "Error = " << status << ".";
516 return status;
517 }
518 return OK;
519}
520
Sungtak Lee7fefd562023-11-21 02:48:08 +0000521void OutputBufferQueue::onBufferReleased(uint32_t generation) {
522 std::shared_ptr<C2SurfaceSyncMemory> syncMem;
523 mMutex.lock();
524 if (mStopped) {
525 return;
526 }
527 sp<IGraphicBufferProducer> outputIgbp = mIgbp;
528 uint32_t outputGeneration = mGeneration;
529 syncMem = mSyncMem;
530 mMutex.unlock();
531
532 if (outputIgbp && generation == outputGeneration) {
533 auto syncVar = syncMem ? syncMem->mem() : nullptr;
534 if (syncVar) {
535 syncVar->lock();
536 syncVar->notifyQueuedLocked();
537 syncVar->unlock();
538 }
539 }
540}
541
Brian Lindahl932bf602023-03-09 11:59:48 -0700542void OutputBufferQueue::pollForRenderedFrames(FrameEventHistoryDelta* delta) {
543 if (mIgbp) {
544 mIgbp->getFrameTimestamps(delta);
545 }
546}
547
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700548void OutputBufferQueue::holdBufferQueueBlocks(
549 const std::list<std::unique_ptr<C2Work>>& workList) {
550 forEachBlock(workList,
551 std::bind(&OutputBufferQueue::registerBuffer,
552 this, std::placeholders::_1));
553}
554
Sungtak Leea714f112021-03-16 05:40:03 -0700555void OutputBufferQueue::updateMaxDequeueBufferCount(int maxDequeueBufferCount) {
556 mMutex.lock();
557 mMaxDequeueBufferCount = maxDequeueBufferCount;
558 auto syncVar = mSyncMem ? mSyncMem->mem() : nullptr;
Sungtak Leec7da7a02022-05-05 08:45:33 +0000559 if (syncVar && !mStopped) {
Sungtak Leea714f112021-03-16 05:40:03 -0700560 syncVar->lock();
561 syncVar->updateMaxDequeueCountLocked(maxDequeueBufferCount);
562 syncVar->unlock();
563 }
564 mMutex.unlock();
Sungtak Leedb14cba2021-04-10 00:50:23 -0700565 ALOGD("set max dequeue count %d from update", maxDequeueBufferCount);
Sungtak Leea714f112021-03-16 05:40:03 -0700566}
567
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700568} // namespace c2
569} // namespace media
570} // namespace hardware
571} // namespace android