blob: 8cd4934ce3965e6bbaf79559a9b9c99e1999248a [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"
19#include <android-base/logging.h>
20
21#include <android/hardware/graphics/bufferqueue/2.0/IGraphicBufferProducer.h>
Sungtak Leea714f112021-03-16 05:40:03 -070022#include <codec2/hidl/output.h>
23#include <cutils/ashmem.h>
Pawin Vongmasa586b1982019-10-23 19:21:04 -070024#include <gui/bufferqueue/2.0/B2HGraphicBufferProducer.h>
Sungtak Leea714f112021-03-16 05:40:03 -070025#include <sys/mman.h>
Pawin Vongmasa586b1982019-10-23 19:21:04 -070026
27#include <C2AllocatorGralloc.h>
28#include <C2BlockInternal.h>
29#include <C2Buffer.h>
30#include <C2PlatformSupport.h>
Sungtak Leea714f112021-03-16 05:40:03 -070031#include <C2SurfaceSyncObj.h>
Pawin Vongmasa586b1982019-10-23 19:21:04 -070032
33#include <iomanip>
34
35namespace android {
36namespace hardware {
37namespace media {
38namespace c2 {
Pawin Vongmasa586b1982019-10-23 19:21:04 -070039
40using HGraphicBufferProducer = ::android::hardware::graphics::bufferqueue::
41 V2_0::IGraphicBufferProducer;
42using B2HGraphicBufferProducer = ::android::hardware::graphics::bufferqueue::
43 V2_0::utils::B2HGraphicBufferProducer;
44
45namespace /* unnamed */ {
46
47// Create a GraphicBuffer object from a graphic block.
48sp<GraphicBuffer> createGraphicBuffer(const C2ConstGraphicBlock& block) {
49 uint32_t width;
50 uint32_t height;
51 uint32_t format;
52 uint64_t usage;
53 uint32_t stride;
54 uint32_t generation;
55 uint64_t bqId;
56 int32_t bqSlot;
57 _UnwrapNativeCodec2GrallocMetadata(
58 block.handle(), &width, &height, &format, &usage,
59 &stride, &generation, &bqId, reinterpret_cast<uint32_t*>(&bqSlot));
60 native_handle_t *grallocHandle =
61 UnwrapNativeCodec2GrallocHandle(block.handle());
62 sp<GraphicBuffer> graphicBuffer =
63 new GraphicBuffer(grallocHandle,
64 GraphicBuffer::CLONE_HANDLE,
65 width, height, format,
66 1, usage, stride);
67 native_handle_delete(grallocHandle);
68 return graphicBuffer;
69}
70
71template <typename BlockProcessor>
72void forEachBlock(C2FrameData& frameData,
73 BlockProcessor process) {
74 for (const std::shared_ptr<C2Buffer>& buffer : frameData.buffers) {
75 if (buffer) {
76 for (const C2ConstGraphicBlock& block :
77 buffer->data().graphicBlocks()) {
78 process(block);
79 }
80 }
81 }
82}
83
84template <typename BlockProcessor>
85void forEachBlock(const std::list<std::unique_ptr<C2Work>>& workList,
86 BlockProcessor process) {
87 for (const std::unique_ptr<C2Work>& work : workList) {
88 if (!work) {
89 continue;
90 }
91 for (const std::unique_ptr<C2Worklet>& worklet : work->worklets) {
92 if (worklet) {
93 forEachBlock(worklet->output, process);
94 }
95 }
96 }
97}
98
99sp<HGraphicBufferProducer> getHgbp(const sp<IGraphicBufferProducer>& igbp) {
100 sp<HGraphicBufferProducer> hgbp =
101 igbp->getHalInterface<HGraphicBufferProducer>();
102 return hgbp ? hgbp :
103 new B2HGraphicBufferProducer(igbp);
104}
105
106status_t attachToBufferQueue(const C2ConstGraphicBlock& block,
107 const sp<IGraphicBufferProducer>& igbp,
108 uint32_t generation,
Sungtak Leea714f112021-03-16 05:40:03 -0700109 int32_t* bqSlot,
110 std::shared_ptr<C2SurfaceSyncMemory> syncMem) {
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700111 if (!igbp) {
112 LOG(WARNING) << "attachToBufferQueue -- null producer.";
113 return NO_INIT;
114 }
115
116 sp<GraphicBuffer> graphicBuffer = createGraphicBuffer(block);
117 graphicBuffer->setGenerationNumber(generation);
118
119 LOG(VERBOSE) << "attachToBufferQueue -- attaching buffer:"
120 << " block dimension " << block.width() << "x"
121 << block.height()
122 << ", graphicBuffer dimension " << graphicBuffer->getWidth() << "x"
123 << graphicBuffer->getHeight()
124 << std::hex << std::setfill('0')
125 << ", format 0x" << std::setw(8) << graphicBuffer->getPixelFormat()
126 << ", usage 0x" << std::setw(16) << graphicBuffer->getUsage()
127 << std::dec << std::setfill(' ')
128 << ", stride " << graphicBuffer->getStride()
129 << ", generation " << graphicBuffer->getGenerationNumber();
130
Sungtak Leea714f112021-03-16 05:40:03 -0700131 C2SyncVariables *syncVar = syncMem ? syncMem->mem() : nullptr;
132 status_t result = OK;
133 if (syncVar) {
134 syncVar->lock();
135 if (!syncVar->isDequeueableLocked() ||
136 syncVar->getSyncStatusLocked() == C2SyncVariables::STATUS_SWITCHING) {
137 syncVar->unlock();
138 LOG(WARNING) << "attachToBufferQueue -- attachBuffer failed: "
139 "status = " << INVALID_OPERATION << ".";
140 return INVALID_OPERATION;
141 }
142 result = igbp->attachBuffer(bqSlot, graphicBuffer);
143 if (result == OK) {
144 syncVar->notifyDequeuedLocked();
145 }
146 syncVar->unlock();
147 } else {
148 result = igbp->attachBuffer(bqSlot, graphicBuffer);
149 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700150 if (result != OK) {
151 LOG(WARNING) << "attachToBufferQueue -- attachBuffer failed: "
152 "status = " << result << ".";
153 return result;
154 }
155 LOG(VERBOSE) << "attachToBufferQueue -- attachBuffer returned slot #"
156 << *bqSlot << ".";
157 return OK;
158}
159
160bool getBufferQueueAssignment(const C2ConstGraphicBlock& block,
161 uint32_t* generation,
162 uint64_t* bqId,
163 int32_t* bqSlot) {
164 return _C2BlockFactory::GetBufferQueueData(
165 _C2BlockFactory::GetGraphicBlockPoolData(block),
166 generation, bqId, bqSlot);
167}
168
169} // unnamed namespace
170
171OutputBufferQueue::OutputBufferQueue()
172 : mGeneration{0}, mBqId{0} {
173}
174
175OutputBufferQueue::~OutputBufferQueue() {
176}
177
178bool OutputBufferQueue::configure(const sp<IGraphicBufferProducer>& igbp,
179 uint32_t generation,
Sungtak Leea714f112021-03-16 05:40:03 -0700180 uint64_t bqId,
Sungtak Leedb14cba2021-04-10 00:50:23 -0700181 int maxDequeueBufferCount,
Sungtak Leea714f112021-03-16 05:40:03 -0700182 std::shared_ptr<V1_2::SurfaceSyncObj> *syncObj) {
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900183 uint64_t consumerUsage = 0;
184 if (igbp->getConsumerUsage(&consumerUsage) != OK) {
185 ALOGW("failed to get consumer usage");
186 }
187
Sungtak Leea714f112021-03-16 05:40:03 -0700188 // TODO : Abstract creation process into C2SurfaceSyncMemory class.
189 // use C2LinearBlock instead ashmem.
190 std::shared_ptr<C2SurfaceSyncMemory> syncMem;
191 if (syncObj && igbp) {
192 bool mapped = false;
193 int memFd = ashmem_create_region("C2SurfaceMem", sizeof(C2SyncVariables));
194 size_t memSize = memFd < 0 ? 0 : ashmem_get_size_region(memFd);
195 if (memSize > 0) {
196 syncMem = C2SurfaceSyncMemory::Create(memFd, memSize);
197 if (syncMem) {
198 mapped = true;
199 *syncObj = std::make_shared<V1_2::SurfaceSyncObj>();
200 (*syncObj)->syncMemory = syncMem->handle();
201 (*syncObj)->bqId = bqId;
202 (*syncObj)->generationId = generation;
203 (*syncObj)->consumerUsage = consumerUsage;
204 ALOGD("C2SurfaceSyncMemory created %zu(%zu)", sizeof(C2SyncVariables), memSize);
205 }
206 }
207 if (!mapped) {
208 if (memFd >= 0) {
209 ::close(memFd);
210 }
211 ALOGW("SurfaceSyncObj creation failure");
212 }
213 }
214
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700215 size_t tryNum = 0;
216 size_t success = 0;
217 sp<GraphicBuffer> buffers[BufferQueueDefs::NUM_BUFFER_SLOTS];
218 std::weak_ptr<_C2BlockPoolData>
219 poolDatas[BufferQueueDefs::NUM_BUFFER_SLOTS];
220 {
221 std::scoped_lock<std::mutex> l(mMutex);
222 if (generation == mGeneration) {
Sungtak Leedb14cba2021-04-10 00:50:23 -0700223 // case of old BlockPool destruction
224 C2SyncVariables *var = mSyncMem ? mSyncMem->mem() : nullptr;
George Burgess IV3defea72021-04-23 18:43:57 -0700225 if (syncObj && var) {
Sungtak Leedb14cba2021-04-10 00:50:23 -0700226 *syncObj = std::make_shared<V1_2::SurfaceSyncObj>();
227 (*syncObj)->bqId = bqId;
228 (*syncObj)->syncMemory = mSyncMem->handle();
229 (*syncObj)->generationId = generation;
230 (*syncObj)->consumerUsage = consumerUsage;
231 mMaxDequeueBufferCount = maxDequeueBufferCount;
232 var->lock();
233 var->setSyncStatusLocked(C2SyncVariables::STATUS_INIT);
234 var->setInitialDequeueCountLocked(mMaxDequeueBufferCount, 0);
235 var->unlock();
236 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700237 return false;
238 }
Sungtak Leea714f112021-03-16 05:40:03 -0700239 std::shared_ptr<C2SurfaceSyncMemory> oldMem = mSyncMem;
240 C2SyncVariables *oldSync = mSyncMem ? mSyncMem->mem() : nullptr;
241 if (oldSync) {
242 oldSync->lock();
243 oldSync->setSyncStatusLocked(C2SyncVariables::STATUS_SWITCHING);
244 oldSync->unlock();
245 }
246 mSyncMem.reset();
247 if (syncMem) {
248 mSyncMem = syncMem;
249 }
250 C2SyncVariables *newSync = mSyncMem ? mSyncMem->mem() : nullptr;
251
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700252 mIgbp = igbp;
253 mGeneration = generation;
254 mBqId = bqId;
255 mOwner = std::make_shared<int>(0);
Sungtak Leedb14cba2021-04-10 00:50:23 -0700256 mMaxDequeueBufferCount = maxDequeueBufferCount;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700257 for (int i = 0; i < BufferQueueDefs::NUM_BUFFER_SLOTS; ++i) {
258 if (mBqId == 0 || !mBuffers[i]) {
259 continue;
260 }
261 std::shared_ptr<_C2BlockPoolData> data = mPoolDatas[i].lock();
262 if (!data ||
263 !_C2BlockFactory::BeginAttachBlockToBufferQueue(data)) {
264 continue;
265 }
266 ++tryNum;
267 int bqSlot;
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900268
269 // Update buffer's generation and usage.
270 if ((mBuffers[i]->getUsage() & consumerUsage) != consumerUsage) {
271 mBuffers[i] = new GraphicBuffer(
272 mBuffers[i]->handle, GraphicBuffer::CLONE_HANDLE,
273 mBuffers[i]->width, mBuffers[i]->height,
274 mBuffers[i]->format, mBuffers[i]->layerCount,
275 mBuffers[i]->getUsage() | consumerUsage,
276 mBuffers[i]->stride);
277 if (mBuffers[i]->initCheck() != OK) {
278 ALOGW("%s() failed to update usage, original usage=%" PRIx64
279 ", consumer usage=%" PRIx64,
280 __func__, mBuffers[i]->getUsage(), consumerUsage);
281 continue;
282 }
283 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700284 mBuffers[i]->setGenerationNumber(generation);
Chih-Yu Huangb5ec89e2021-02-09 17:37:32 +0900285
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700286 status_t result = igbp->attachBuffer(&bqSlot, mBuffers[i]);
287 if (result != OK) {
288 continue;
289 }
290 bool attach =
291 _C2BlockFactory::EndAttachBlockToBufferQueue(
Sungtak Leea714f112021-03-16 05:40:03 -0700292 data, mOwner, getHgbp(mIgbp), mSyncMem,
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700293 generation, bqId, bqSlot);
294 if (!attach) {
295 igbp->cancelBuffer(bqSlot, Fence::NO_FENCE);
296 continue;
297 }
298 buffers[bqSlot] = mBuffers[i];
299 poolDatas[bqSlot] = data;
300 ++success;
301 }
302 for (int i = 0; i < BufferQueueDefs::NUM_BUFFER_SLOTS; ++i) {
303 mBuffers[i] = buffers[i];
304 mPoolDatas[i] = poolDatas[i];
305 }
Sungtak Leea714f112021-03-16 05:40:03 -0700306 if (newSync) {
Sungtak Leedb14cba2021-04-10 00:50:23 -0700307 newSync->lock();
308 newSync->setInitialDequeueCountLocked(mMaxDequeueBufferCount, success);
309 newSync->unlock();
Sungtak Leea714f112021-03-16 05:40:03 -0700310 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700311 }
Sungtak Leea714f112021-03-16 05:40:03 -0700312 ALOGD("remote graphic buffer migration %zu/%zu",
313 success, tryNum);
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700314 return true;
315}
316
317bool OutputBufferQueue::registerBuffer(const C2ConstGraphicBlock& block) {
318 std::shared_ptr<_C2BlockPoolData> data =
319 _C2BlockFactory::GetGraphicBlockPoolData(block);
320 if (!data) {
321 return false;
322 }
323 std::scoped_lock<std::mutex> l(mMutex);
324
325 if (!mIgbp) {
326 return false;
327 }
328
329 uint32_t oldGeneration;
330 uint64_t oldId;
331 int32_t oldSlot;
332 // If the block is not bufferqueue-based, do nothing.
333 if (!_C2BlockFactory::GetBufferQueueData(
334 data, &oldGeneration, &oldId, &oldSlot) || (oldId == 0)) {
335 return false;
336 }
337 // If the block's bqId is the same as the desired bqId, just hold.
338 if ((oldId == mBqId) && (oldGeneration == mGeneration)) {
339 LOG(VERBOSE) << "holdBufferQueueBlock -- import without attaching:"
340 << " bqId " << oldId
341 << ", bqSlot " << oldSlot
342 << ", generation " << mGeneration
343 << ".";
Sungtak Leea714f112021-03-16 05:40:03 -0700344 _C2BlockFactory::HoldBlockFromBufferQueue(data, mOwner, getHgbp(mIgbp), mSyncMem);
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700345 mPoolDatas[oldSlot] = data;
346 mBuffers[oldSlot] = createGraphicBuffer(block);
347 mBuffers[oldSlot]->setGenerationNumber(mGeneration);
348 return true;
349 }
350 int32_t d = (int32_t) mGeneration - (int32_t) oldGeneration;
351 LOG(WARNING) << "receiving stale buffer: generation "
352 << mGeneration << " , diff " << d << " : slot "
353 << oldSlot;
354 return false;
355}
356
357status_t OutputBufferQueue::outputBuffer(
358 const C2ConstGraphicBlock& block,
359 const BnGraphicBufferProducer::QueueBufferInput& input,
360 BnGraphicBufferProducer::QueueBufferOutput* output) {
361 uint32_t generation;
362 uint64_t bqId;
363 int32_t bqSlot;
Sungtak Leea714f112021-03-16 05:40:03 -0700364 bool display = V1_0::utils::displayBufferQueueBlock(block);
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700365 if (!getBufferQueueAssignment(block, &generation, &bqId, &bqSlot) ||
366 bqId == 0) {
367 // Block not from bufferqueue -- it must be attached before queuing.
368
Sungtak Leea714f112021-03-16 05:40:03 -0700369 std::shared_ptr<C2SurfaceSyncMemory> syncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700370 mMutex.lock();
371 sp<IGraphicBufferProducer> outputIgbp = mIgbp;
372 uint32_t outputGeneration = mGeneration;
Sungtak Leea714f112021-03-16 05:40:03 -0700373 syncMem = mSyncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700374 mMutex.unlock();
375
376 status_t status = attachToBufferQueue(
Sungtak Leea714f112021-03-16 05:40:03 -0700377 block, outputIgbp, outputGeneration, &bqSlot, syncMem);
378
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700379 if (status != OK) {
380 LOG(WARNING) << "outputBuffer -- attaching failed.";
381 return INVALID_OPERATION;
382 }
383
Sungtak Leea714f112021-03-16 05:40:03 -0700384 auto syncVar = syncMem ? syncMem->mem() : nullptr;
385 if(syncVar) {
386 syncVar->lock();
387 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
388 input, output);
389 if (status == OK) {
390 syncVar->notifyQueuedLocked();
391 }
392 syncVar->unlock();
393 } else {
394 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
395 input, output);
396 }
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700397 if (status != OK) {
398 LOG(ERROR) << "outputBuffer -- queueBuffer() failed "
399 "on non-bufferqueue-based block. "
400 "Error = " << status << ".";
401 return status;
402 }
403 return OK;
404 }
405
Sungtak Leea714f112021-03-16 05:40:03 -0700406 std::shared_ptr<C2SurfaceSyncMemory> syncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700407 mMutex.lock();
408 sp<IGraphicBufferProducer> outputIgbp = mIgbp;
409 uint32_t outputGeneration = mGeneration;
410 uint64_t outputBqId = mBqId;
Sungtak Leea714f112021-03-16 05:40:03 -0700411 syncMem = mSyncMem;
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700412 mMutex.unlock();
413
414 if (!outputIgbp) {
415 LOG(VERBOSE) << "outputBuffer -- output surface is null.";
416 return NO_INIT;
417 }
418
419 if (!display) {
420 LOG(WARNING) << "outputBuffer -- cannot display "
421 "bufferqueue-based block to the bufferqueue.";
422 return UNKNOWN_ERROR;
423 }
424 if (bqId != outputBqId || generation != outputGeneration) {
425 int32_t diff = (int32_t) outputGeneration - (int32_t) generation;
426 LOG(WARNING) << "outputBuffer -- buffers from old generation to "
427 << outputGeneration << " , diff: " << diff
428 << " , slot: " << bqSlot;
429 return DEAD_OBJECT;
430 }
431
Sungtak Leea714f112021-03-16 05:40:03 -0700432 auto syncVar = syncMem ? syncMem->mem() : nullptr;
433 status_t status = OK;
434 if (syncVar) {
435 syncVar->lock();
436 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
437 input, output);
438 if (status == OK) {
439 syncVar->notifyQueuedLocked();
440 }
441 syncVar->unlock();
442 } else {
443 status = outputIgbp->queueBuffer(static_cast<int>(bqSlot),
444 input, output);
445 }
446
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700447 if (status != OK) {
448 LOG(ERROR) << "outputBuffer -- queueBuffer() failed "
449 "on bufferqueue-based block. "
450 "Error = " << status << ".";
451 return status;
452 }
453 return OK;
454}
455
456void OutputBufferQueue::holdBufferQueueBlocks(
457 const std::list<std::unique_ptr<C2Work>>& workList) {
458 forEachBlock(workList,
459 std::bind(&OutputBufferQueue::registerBuffer,
460 this, std::placeholders::_1));
461}
462
Sungtak Leea714f112021-03-16 05:40:03 -0700463void OutputBufferQueue::updateMaxDequeueBufferCount(int maxDequeueBufferCount) {
464 mMutex.lock();
465 mMaxDequeueBufferCount = maxDequeueBufferCount;
466 auto syncVar = mSyncMem ? mSyncMem->mem() : nullptr;
467 if (syncVar) {
468 syncVar->lock();
469 syncVar->updateMaxDequeueCountLocked(maxDequeueBufferCount);
470 syncVar->unlock();
471 }
472 mMutex.unlock();
Sungtak Leedb14cba2021-04-10 00:50:23 -0700473 ALOGD("set max dequeue count %d from update", maxDequeueBufferCount);
Sungtak Leea714f112021-03-16 05:40:03 -0700474}
475
Pawin Vongmasa586b1982019-10-23 19:21:04 -0700476} // namespace c2
477} // namespace media
478} // namespace hardware
479} // namespace android
480