blob: e58a1e438b2b8b8f98a3abf8c6ab6e7fd5a61630 [file] [log] [blame]
Wonsik Kim469c8342019-04-11 16:46:09 -07001/*
2 * Copyright 2019, 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 "CCodecBuffers"
19#include <utils/Log.h>
20
21#include <C2PlatformSupport.h>
22
23#include <media/stagefright/foundation/ADebug.h>
Pawin Vongmasa9b906982020-04-11 05:07:15 -070024#include <media/stagefright/MediaCodec.h>
Wonsik Kim469c8342019-04-11 16:46:09 -070025#include <media/stagefright/MediaCodecConstants.h>
Wonsik Kim155d5cb2019-10-09 12:49:49 -070026#include <media/stagefright/SkipCutBuffer.h>
Wonsik Kim41d83432020-04-27 16:40:49 -070027#include <mediadrm/ICrypto.h>
Wonsik Kim469c8342019-04-11 16:46:09 -070028
29#include "CCodecBuffers.h"
30
31namespace android {
32
33namespace {
34
35sp<GraphicBlockBuffer> AllocateGraphicBuffer(
36 const std::shared_ptr<C2BlockPool> &pool,
37 const sp<AMessage> &format,
38 uint32_t pixelFormat,
39 const C2MemoryUsage &usage,
40 const std::shared_ptr<LocalBufferPool> &localBufferPool) {
41 int32_t width, height;
42 if (!format->findInt32("width", &width) || !format->findInt32("height", &height)) {
43 ALOGD("format lacks width or height");
44 return nullptr;
45 }
46
47 std::shared_ptr<C2GraphicBlock> block;
48 c2_status_t err = pool->fetchGraphicBlock(
49 width, height, pixelFormat, usage, &block);
50 if (err != C2_OK) {
51 ALOGD("fetch graphic block failed: %d", err);
52 return nullptr;
53 }
54
55 return GraphicBlockBuffer::Allocate(
56 format,
57 block,
58 [localBufferPool](size_t capacity) {
59 return localBufferPool->newBuffer(capacity);
60 });
61}
62
63} // namespace
64
65// CCodecBuffers
66
67void CCodecBuffers::setFormat(const sp<AMessage> &format) {
68 CHECK(format != nullptr);
69 mFormat = format;
70}
71
72sp<AMessage> CCodecBuffers::dupFormat() {
73 return mFormat != nullptr ? mFormat->dup() : nullptr;
74}
75
76void CCodecBuffers::handleImageData(const sp<Codec2Buffer> &buffer) {
77 sp<ABuffer> imageDataCandidate = buffer->getImageData();
78 if (imageDataCandidate == nullptr) {
79 return;
80 }
81 sp<ABuffer> imageData;
82 if (!mFormat->findBuffer("image-data", &imageData)
83 || imageDataCandidate->size() != imageData->size()
84 || memcmp(imageDataCandidate->data(), imageData->data(), imageData->size()) != 0) {
85 ALOGD("[%s] updating image-data", mName);
86 sp<AMessage> newFormat = dupFormat();
87 newFormat->setBuffer("image-data", imageDataCandidate);
88 MediaImage2 *img = (MediaImage2*)imageDataCandidate->data();
89 if (img->mNumPlanes > 0 && img->mType != img->MEDIA_IMAGE_TYPE_UNKNOWN) {
90 int32_t stride = img->mPlane[0].mRowInc;
91 newFormat->setInt32(KEY_STRIDE, stride);
92 ALOGD("[%s] updating stride = %d", mName, stride);
93 if (img->mNumPlanes > 1 && stride > 0) {
94 int32_t vstride = (img->mPlane[1].mOffset - img->mPlane[0].mOffset) / stride;
95 newFormat->setInt32(KEY_SLICE_HEIGHT, vstride);
96 ALOGD("[%s] updating vstride = %d", mName, vstride);
97 }
98 }
99 setFormat(newFormat);
100 buffer->setFormat(newFormat);
101 }
102}
103
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700104// InputBuffers
105
106sp<Codec2Buffer> InputBuffers::cloneAndReleaseBuffer(const sp<MediaCodecBuffer> &buffer) {
107 sp<Codec2Buffer> copy = createNewBuffer();
108 if (copy == nullptr) {
109 return nullptr;
110 }
111 std::shared_ptr<C2Buffer> c2buffer;
112 if (!releaseBuffer(buffer, &c2buffer, true)) {
113 return nullptr;
114 }
115 if (!copy->canCopy(c2buffer)) {
116 return nullptr;
117 }
118 if (!copy->copy(c2buffer)) {
119 return nullptr;
120 }
121 return copy;
122}
123
Wonsik Kim469c8342019-04-11 16:46:09 -0700124// OutputBuffers
125
Wonsik Kim41d83432020-04-27 16:40:49 -0700126OutputBuffers::OutputBuffers(const char *componentName, const char *name)
127 : CCodecBuffers(componentName, name) { }
128
129OutputBuffers::~OutputBuffers() = default;
130
Wonsik Kim469c8342019-04-11 16:46:09 -0700131void OutputBuffers::initSkipCutBuffer(
132 int32_t delay, int32_t padding, int32_t sampleRate, int32_t channelCount) {
133 CHECK(mSkipCutBuffer == nullptr);
134 mDelay = delay;
135 mPadding = padding;
136 mSampleRate = sampleRate;
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700137 mChannelCount = channelCount;
138 setSkipCutBuffer(delay, padding);
Wonsik Kim469c8342019-04-11 16:46:09 -0700139}
140
141void OutputBuffers::updateSkipCutBuffer(int32_t sampleRate, int32_t channelCount) {
142 if (mSkipCutBuffer == nullptr) {
143 return;
144 }
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700145 if (mSampleRate == sampleRate && mChannelCount == channelCount) {
146 return;
147 }
Wonsik Kim469c8342019-04-11 16:46:09 -0700148 int32_t delay = mDelay;
149 int32_t padding = mPadding;
150 if (sampleRate != mSampleRate) {
151 delay = ((int64_t)delay * sampleRate) / mSampleRate;
152 padding = ((int64_t)padding * sampleRate) / mSampleRate;
153 }
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700154 mSampleRate = sampleRate;
155 mChannelCount = channelCount;
156 setSkipCutBuffer(delay, padding);
Wonsik Kim469c8342019-04-11 16:46:09 -0700157}
158
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700159void OutputBuffers::updateSkipCutBuffer(
160 const sp<AMessage> &format, bool notify) {
161 AString mediaType;
162 if (format->findString(KEY_MIME, &mediaType)
163 && mediaType == MIMETYPE_AUDIO_RAW) {
164 int32_t channelCount;
165 int32_t sampleRate;
166 if (format->findInt32(KEY_CHANNEL_COUNT, &channelCount)
167 && format->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
168 updateSkipCutBuffer(sampleRate, channelCount);
169 }
170 }
171 if (notify) {
172 mUnreportedFormat = nullptr;
173 }
174}
175
Wonsik Kim469c8342019-04-11 16:46:09 -0700176void OutputBuffers::submit(const sp<MediaCodecBuffer> &buffer) {
177 if (mSkipCutBuffer != nullptr) {
178 mSkipCutBuffer->submit(buffer);
179 }
180}
181
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700182void OutputBuffers::setSkipCutBuffer(int32_t skip, int32_t cut) {
Wonsik Kim469c8342019-04-11 16:46:09 -0700183 if (mSkipCutBuffer != nullptr) {
184 size_t prevSize = mSkipCutBuffer->size();
185 if (prevSize != 0u) {
186 ALOGD("[%s] Replacing SkipCutBuffer holding %zu bytes", mName, prevSize);
187 }
188 }
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700189 mSkipCutBuffer = new SkipCutBuffer(skip, cut, mChannelCount);
Wonsik Kim469c8342019-04-11 16:46:09 -0700190}
191
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700192void OutputBuffers::clearStash() {
193 mPending.clear();
194 mReorderStash.clear();
195 mDepth = 0;
196 mKey = C2Config::ORDINAL;
197 mUnreportedFormat = nullptr;
198}
199
200void OutputBuffers::flushStash() {
201 for (StashEntry& e : mPending) {
202 e.notify = false;
203 }
204 for (StashEntry& e : mReorderStash) {
205 e.notify = false;
206 }
207}
208
209uint32_t OutputBuffers::getReorderDepth() const {
210 return mDepth;
211}
212
213void OutputBuffers::setReorderDepth(uint32_t depth) {
214 mPending.splice(mPending.end(), mReorderStash);
215 mDepth = depth;
216}
217
218void OutputBuffers::setReorderKey(C2Config::ordinal_key_t key) {
219 mPending.splice(mPending.end(), mReorderStash);
220 mKey = key;
221}
222
223void OutputBuffers::pushToStash(
224 const std::shared_ptr<C2Buffer>& buffer,
225 bool notify,
226 int64_t timestamp,
227 int32_t flags,
228 const sp<AMessage>& format,
229 const C2WorkOrdinalStruct& ordinal) {
230 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
231 if (!buffer && eos) {
232 // TRICKY: we may be violating ordering of the stash here. Because we
233 // don't expect any more emplace() calls after this, the ordering should
234 // not matter.
235 mReorderStash.emplace_back(
236 buffer, notify, timestamp, flags, format, ordinal);
237 } else {
238 flags = flags & ~MediaCodec::BUFFER_FLAG_EOS;
239 auto it = mReorderStash.begin();
240 for (; it != mReorderStash.end(); ++it) {
241 if (less(ordinal, it->ordinal)) {
242 break;
243 }
244 }
245 mReorderStash.emplace(it,
246 buffer, notify, timestamp, flags, format, ordinal);
247 if (eos) {
248 mReorderStash.back().flags =
249 mReorderStash.back().flags | MediaCodec::BUFFER_FLAG_EOS;
250 }
251 }
252 while (!mReorderStash.empty() && mReorderStash.size() > mDepth) {
253 mPending.push_back(mReorderStash.front());
254 mReorderStash.pop_front();
255 }
256 ALOGV("[%s] %s: pushToStash -- pending size = %zu", mName, __func__, mPending.size());
257}
258
259OutputBuffers::BufferAction OutputBuffers::popFromStashAndRegister(
260 std::shared_ptr<C2Buffer>* c2Buffer,
261 size_t* index,
262 sp<MediaCodecBuffer>* outBuffer) {
263 if (mPending.empty()) {
264 return SKIP;
265 }
266
267 // Retrieve the first entry.
268 StashEntry &entry = mPending.front();
269
270 *c2Buffer = entry.buffer;
271 sp<AMessage> outputFormat = entry.format;
272
273 // The output format can be processed without a registered slot.
274 if (outputFormat) {
275 ALOGD("[%s] popFromStashAndRegister: output format changed to %s",
276 mName, outputFormat->debugString().c_str());
277 updateSkipCutBuffer(outputFormat, entry.notify);
278 }
279
280 if (entry.notify) {
281 if (outputFormat) {
282 setFormat(outputFormat);
283 } else if (mUnreportedFormat) {
284 outputFormat = mUnreportedFormat;
285 setFormat(outputFormat);
286 }
287 mUnreportedFormat = nullptr;
288 } else {
289 if (outputFormat) {
290 mUnreportedFormat = outputFormat;
291 } else if (!mUnreportedFormat) {
292 mUnreportedFormat = mFormat;
293 }
294 }
295
296 // Flushing mReorderStash because no other buffers should come after output
297 // EOS.
298 if (entry.flags & MediaCodec::BUFFER_FLAG_EOS) {
299 // Flush reorder stash
300 setReorderDepth(0);
301 }
302
303 if (!entry.notify) {
304 mPending.pop_front();
305 return DISCARD;
306 }
307
308 // Try to register the buffer.
309 status_t err = registerBuffer(*c2Buffer, index, outBuffer);
310 if (err != OK) {
311 if (err != WOULD_BLOCK) {
312 return REALLOCATE;
313 }
314 return RETRY;
315 }
316
317 // Append information from the front stash entry to outBuffer.
318 (*outBuffer)->meta()->setInt64("timeUs", entry.timestamp);
319 (*outBuffer)->meta()->setInt32("flags", entry.flags);
320 ALOGV("[%s] popFromStashAndRegister: "
321 "out buffer index = %zu [%p] => %p + %zu (%lld)",
322 mName, *index, outBuffer->get(),
323 (*outBuffer)->data(), (*outBuffer)->size(),
324 (long long)entry.timestamp);
325
326 // The front entry of mPending will be removed now that the registration
327 // succeeded.
328 mPending.pop_front();
329 return NOTIFY_CLIENT;
330}
331
332bool OutputBuffers::popPending(StashEntry *entry) {
333 if (mPending.empty()) {
334 return false;
335 }
336 *entry = mPending.front();
337 mPending.pop_front();
338 return true;
339}
340
341void OutputBuffers::deferPending(const OutputBuffers::StashEntry &entry) {
342 mPending.push_front(entry);
343}
344
345bool OutputBuffers::hasPending() const {
346 return !mPending.empty();
347}
348
349bool OutputBuffers::less(
350 const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2) const {
351 switch (mKey) {
352 case C2Config::ORDINAL: return o1.frameIndex < o2.frameIndex;
353 case C2Config::TIMESTAMP: return o1.timestamp < o2.timestamp;
354 case C2Config::CUSTOM: return o1.customOrdinal < o2.customOrdinal;
355 default:
356 ALOGD("Unrecognized key; default to timestamp");
357 return o1.frameIndex < o2.frameIndex;
358 }
359}
360
Wonsik Kim469c8342019-04-11 16:46:09 -0700361// LocalBufferPool
362
Wonsik Kim41d83432020-04-27 16:40:49 -0700363constexpr size_t kInitialPoolCapacity = kMaxLinearBufferSize;
364constexpr size_t kMaxPoolCapacity = kMaxLinearBufferSize * 32;
365
366std::shared_ptr<LocalBufferPool> LocalBufferPool::Create() {
367 return std::shared_ptr<LocalBufferPool>(new LocalBufferPool(kInitialPoolCapacity));
Wonsik Kim469c8342019-04-11 16:46:09 -0700368}
369
370sp<ABuffer> LocalBufferPool::newBuffer(size_t capacity) {
371 Mutex::Autolock lock(mMutex);
372 auto it = std::find_if(
373 mPool.begin(), mPool.end(),
374 [capacity](const std::vector<uint8_t> &vec) {
375 return vec.capacity() >= capacity;
376 });
377 if (it != mPool.end()) {
378 sp<ABuffer> buffer = new VectorBuffer(std::move(*it), shared_from_this());
379 mPool.erase(it);
380 return buffer;
381 }
382 if (mUsedSize + capacity > mPoolCapacity) {
383 while (!mPool.empty()) {
384 mUsedSize -= mPool.back().capacity();
385 mPool.pop_back();
386 }
Wonsik Kim41d83432020-04-27 16:40:49 -0700387 while (mUsedSize + capacity > mPoolCapacity && mPoolCapacity * 2 <= kMaxPoolCapacity) {
388 ALOGD("Increasing local buffer pool capacity from %zu to %zu",
389 mPoolCapacity, mPoolCapacity * 2);
390 mPoolCapacity *= 2;
391 }
Wonsik Kim469c8342019-04-11 16:46:09 -0700392 if (mUsedSize + capacity > mPoolCapacity) {
393 ALOGD("mUsedSize = %zu, capacity = %zu, mPoolCapacity = %zu",
394 mUsedSize, capacity, mPoolCapacity);
395 return nullptr;
396 }
397 }
398 std::vector<uint8_t> vec(capacity);
399 mUsedSize += vec.capacity();
400 return new VectorBuffer(std::move(vec), shared_from_this());
401}
402
403LocalBufferPool::VectorBuffer::VectorBuffer(
404 std::vector<uint8_t> &&vec, const std::shared_ptr<LocalBufferPool> &pool)
405 : ABuffer(vec.data(), vec.capacity()),
406 mVec(std::move(vec)),
407 mPool(pool) {
408}
409
410LocalBufferPool::VectorBuffer::~VectorBuffer() {
411 std::shared_ptr<LocalBufferPool> pool = mPool.lock();
412 if (pool) {
413 // If pool is alive, return the vector back to the pool so that
414 // it can be recycled.
415 pool->returnVector(std::move(mVec));
416 }
417}
418
419void LocalBufferPool::returnVector(std::vector<uint8_t> &&vec) {
420 Mutex::Autolock lock(mMutex);
421 mPool.push_front(std::move(vec));
422}
423
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700424// FlexBuffersImpl
425
Wonsik Kim469c8342019-04-11 16:46:09 -0700426size_t FlexBuffersImpl::assignSlot(const sp<Codec2Buffer> &buffer) {
427 for (size_t i = 0; i < mBuffers.size(); ++i) {
428 if (mBuffers[i].clientBuffer == nullptr
429 && mBuffers[i].compBuffer.expired()) {
430 mBuffers[i].clientBuffer = buffer;
431 return i;
432 }
433 }
434 mBuffers.push_back({ buffer, std::weak_ptr<C2Buffer>() });
435 return mBuffers.size() - 1;
436}
437
Wonsik Kim469c8342019-04-11 16:46:09 -0700438bool FlexBuffersImpl::releaseSlot(
439 const sp<MediaCodecBuffer> &buffer,
440 std::shared_ptr<C2Buffer> *c2buffer,
441 bool release) {
442 sp<Codec2Buffer> clientBuffer;
443 size_t index = mBuffers.size();
444 for (size_t i = 0; i < mBuffers.size(); ++i) {
445 if (mBuffers[i].clientBuffer == buffer) {
446 clientBuffer = mBuffers[i].clientBuffer;
447 if (release) {
448 mBuffers[i].clientBuffer.clear();
449 }
450 index = i;
451 break;
452 }
453 }
454 if (clientBuffer == nullptr) {
455 ALOGV("[%s] %s: No matching buffer found", mName, __func__);
456 return false;
457 }
458 std::shared_ptr<C2Buffer> result = mBuffers[index].compBuffer.lock();
459 if (!result) {
460 result = clientBuffer->asC2Buffer();
Wonsik Kimf9b32122020-04-02 11:30:17 -0700461 clientBuffer->clearC2BufferRefs();
Wonsik Kim469c8342019-04-11 16:46:09 -0700462 mBuffers[index].compBuffer = result;
463 }
464 if (c2buffer) {
465 *c2buffer = result;
466 }
467 return true;
468}
469
470bool FlexBuffersImpl::expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
471 for (size_t i = 0; i < mBuffers.size(); ++i) {
472 std::shared_ptr<C2Buffer> compBuffer =
473 mBuffers[i].compBuffer.lock();
474 if (!compBuffer || compBuffer != c2buffer) {
475 continue;
476 }
477 mBuffers[i].compBuffer.reset();
478 ALOGV("[%s] codec released buffer #%zu", mName, i);
479 return true;
480 }
481 ALOGV("[%s] codec released an unknown buffer", mName);
482 return false;
483}
484
485void FlexBuffersImpl::flush() {
486 ALOGV("[%s] buffers are flushed %zu", mName, mBuffers.size());
487 mBuffers.clear();
488}
489
490size_t FlexBuffersImpl::numClientBuffers() const {
491 return std::count_if(
492 mBuffers.begin(), mBuffers.end(),
493 [](const Entry &entry) {
494 return (entry.clientBuffer != nullptr);
495 });
496}
497
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700498size_t FlexBuffersImpl::numComponentBuffers() const {
499 return std::count_if(
500 mBuffers.begin(), mBuffers.end(),
501 [](const Entry &entry) {
502 return !entry.compBuffer.expired();
503 });
504}
505
Wonsik Kim469c8342019-04-11 16:46:09 -0700506// BuffersArrayImpl
507
508void BuffersArrayImpl::initialize(
509 const FlexBuffersImpl &impl,
510 size_t minSize,
511 std::function<sp<Codec2Buffer>()> allocate) {
512 mImplName = impl.mImplName + "[N]";
513 mName = mImplName.c_str();
514 for (size_t i = 0; i < impl.mBuffers.size(); ++i) {
515 sp<Codec2Buffer> clientBuffer = impl.mBuffers[i].clientBuffer;
516 bool ownedByClient = (clientBuffer != nullptr);
517 if (!ownedByClient) {
518 clientBuffer = allocate();
519 }
520 mBuffers.push_back({ clientBuffer, impl.mBuffers[i].compBuffer, ownedByClient });
521 }
522 ALOGV("[%s] converted %zu buffers to array mode of %zu", mName, mBuffers.size(), minSize);
523 for (size_t i = impl.mBuffers.size(); i < minSize; ++i) {
524 mBuffers.push_back({ allocate(), std::weak_ptr<C2Buffer>(), false });
525 }
526}
527
528status_t BuffersArrayImpl::grabBuffer(
529 size_t *index,
530 sp<Codec2Buffer> *buffer,
531 std::function<bool(const sp<Codec2Buffer> &)> match) {
532 // allBuffersDontMatch remains true if all buffers are available but
533 // match() returns false for every buffer.
534 bool allBuffersDontMatch = true;
535 for (size_t i = 0; i < mBuffers.size(); ++i) {
536 if (!mBuffers[i].ownedByClient && mBuffers[i].compBuffer.expired()) {
537 if (match(mBuffers[i].clientBuffer)) {
538 mBuffers[i].ownedByClient = true;
539 *buffer = mBuffers[i].clientBuffer;
540 (*buffer)->meta()->clear();
541 (*buffer)->setRange(0, (*buffer)->capacity());
542 *index = i;
543 return OK;
544 }
545 } else {
546 allBuffersDontMatch = false;
547 }
548 }
549 return allBuffersDontMatch ? NO_MEMORY : WOULD_BLOCK;
550}
551
552bool BuffersArrayImpl::returnBuffer(
553 const sp<MediaCodecBuffer> &buffer,
554 std::shared_ptr<C2Buffer> *c2buffer,
555 bool release) {
556 sp<Codec2Buffer> clientBuffer;
557 size_t index = mBuffers.size();
558 for (size_t i = 0; i < mBuffers.size(); ++i) {
559 if (mBuffers[i].clientBuffer == buffer) {
560 if (!mBuffers[i].ownedByClient) {
561 ALOGD("[%s] Client returned a buffer it does not own according to our record: %zu",
562 mName, i);
563 }
564 clientBuffer = mBuffers[i].clientBuffer;
565 if (release) {
566 mBuffers[i].ownedByClient = false;
567 }
568 index = i;
569 break;
570 }
571 }
572 if (clientBuffer == nullptr) {
573 ALOGV("[%s] %s: No matching buffer found", mName, __func__);
574 return false;
575 }
576 ALOGV("[%s] %s: matching buffer found (index=%zu)", mName, __func__, index);
577 std::shared_ptr<C2Buffer> result = mBuffers[index].compBuffer.lock();
578 if (!result) {
579 result = clientBuffer->asC2Buffer();
Wonsik Kimf9b32122020-04-02 11:30:17 -0700580 clientBuffer->clearC2BufferRefs();
Wonsik Kim469c8342019-04-11 16:46:09 -0700581 mBuffers[index].compBuffer = result;
582 }
583 if (c2buffer) {
584 *c2buffer = result;
585 }
586 return true;
587}
588
589bool BuffersArrayImpl::expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
590 for (size_t i = 0; i < mBuffers.size(); ++i) {
591 std::shared_ptr<C2Buffer> compBuffer =
592 mBuffers[i].compBuffer.lock();
593 if (!compBuffer) {
594 continue;
595 }
596 if (c2buffer == compBuffer) {
597 if (mBuffers[i].ownedByClient) {
598 // This should not happen.
599 ALOGD("[%s] codec released a buffer owned by client "
600 "(index %zu)", mName, i);
601 }
602 mBuffers[i].compBuffer.reset();
603 ALOGV("[%s] codec released buffer #%zu(array mode)", mName, i);
604 return true;
605 }
606 }
607 ALOGV("[%s] codec released an unknown buffer (array mode)", mName);
608 return false;
609}
610
611void BuffersArrayImpl::getArray(Vector<sp<MediaCodecBuffer>> *array) const {
612 array->clear();
613 for (const Entry &entry : mBuffers) {
614 array->push(entry.clientBuffer);
615 }
616}
617
618void BuffersArrayImpl::flush() {
619 for (Entry &entry : mBuffers) {
620 entry.ownedByClient = false;
621 }
622}
623
624void BuffersArrayImpl::realloc(std::function<sp<Codec2Buffer>()> alloc) {
625 size_t size = mBuffers.size();
626 mBuffers.clear();
627 for (size_t i = 0; i < size; ++i) {
628 mBuffers.push_back({ alloc(), std::weak_ptr<C2Buffer>(), false });
629 }
630}
631
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700632void BuffersArrayImpl::grow(
633 size_t newSize, std::function<sp<Codec2Buffer>()> alloc) {
634 CHECK_LT(mBuffers.size(), newSize);
635 while (mBuffers.size() < newSize) {
636 mBuffers.push_back({ alloc(), std::weak_ptr<C2Buffer>(), false });
637 }
638}
639
Wonsik Kim469c8342019-04-11 16:46:09 -0700640size_t BuffersArrayImpl::numClientBuffers() const {
641 return std::count_if(
642 mBuffers.begin(), mBuffers.end(),
643 [](const Entry &entry) {
644 return entry.ownedByClient;
645 });
646}
647
Wonsik Kima39882b2019-06-20 16:13:56 -0700648size_t BuffersArrayImpl::arraySize() const {
649 return mBuffers.size();
650}
651
Wonsik Kim469c8342019-04-11 16:46:09 -0700652// InputBuffersArray
653
654void InputBuffersArray::initialize(
655 const FlexBuffersImpl &impl,
656 size_t minSize,
657 std::function<sp<Codec2Buffer>()> allocate) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700658 mAllocate = allocate;
Wonsik Kim469c8342019-04-11 16:46:09 -0700659 mImpl.initialize(impl, minSize, allocate);
660}
661
662void InputBuffersArray::getArray(Vector<sp<MediaCodecBuffer>> *array) const {
663 mImpl.getArray(array);
664}
665
666bool InputBuffersArray::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
667 sp<Codec2Buffer> c2Buffer;
668 status_t err = mImpl.grabBuffer(index, &c2Buffer);
669 if (err == OK) {
670 c2Buffer->setFormat(mFormat);
671 handleImageData(c2Buffer);
672 *buffer = c2Buffer;
673 return true;
674 }
675 return false;
676}
677
678bool InputBuffersArray::releaseBuffer(
679 const sp<MediaCodecBuffer> &buffer,
680 std::shared_ptr<C2Buffer> *c2buffer,
681 bool release) {
682 return mImpl.returnBuffer(buffer, c2buffer, release);
683}
684
685bool InputBuffersArray::expireComponentBuffer(
686 const std::shared_ptr<C2Buffer> &c2buffer) {
687 return mImpl.expireComponentBuffer(c2buffer);
688}
689
690void InputBuffersArray::flush() {
691 mImpl.flush();
692}
693
694size_t InputBuffersArray::numClientBuffers() const {
695 return mImpl.numClientBuffers();
696}
697
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700698sp<Codec2Buffer> InputBuffersArray::createNewBuffer() {
699 return mAllocate();
700}
701
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800702// SlotInputBuffers
703
704bool SlotInputBuffers::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
705 sp<Codec2Buffer> newBuffer = createNewBuffer();
706 *index = mImpl.assignSlot(newBuffer);
707 *buffer = newBuffer;
708 return true;
709}
710
711bool SlotInputBuffers::releaseBuffer(
712 const sp<MediaCodecBuffer> &buffer,
713 std::shared_ptr<C2Buffer> *c2buffer,
714 bool release) {
715 return mImpl.releaseSlot(buffer, c2buffer, release);
716}
717
718bool SlotInputBuffers::expireComponentBuffer(
719 const std::shared_ptr<C2Buffer> &c2buffer) {
720 return mImpl.expireComponentBuffer(c2buffer);
721}
722
723void SlotInputBuffers::flush() {
724 mImpl.flush();
725}
726
727std::unique_ptr<InputBuffers> SlotInputBuffers::toArrayMode(size_t) {
728 TRESPASS("Array mode should not be called at non-legacy mode");
729 return nullptr;
730}
731
732size_t SlotInputBuffers::numClientBuffers() const {
733 return mImpl.numClientBuffers();
734}
735
736sp<Codec2Buffer> SlotInputBuffers::createNewBuffer() {
737 return new DummyContainerBuffer{mFormat, nullptr};
738}
739
Wonsik Kim469c8342019-04-11 16:46:09 -0700740// LinearInputBuffers
741
742bool LinearInputBuffers::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700743 sp<Codec2Buffer> newBuffer = createNewBuffer();
Wonsik Kim469c8342019-04-11 16:46:09 -0700744 if (newBuffer == nullptr) {
745 return false;
746 }
747 *index = mImpl.assignSlot(newBuffer);
748 *buffer = newBuffer;
749 return true;
750}
751
752bool LinearInputBuffers::releaseBuffer(
753 const sp<MediaCodecBuffer> &buffer,
754 std::shared_ptr<C2Buffer> *c2buffer,
755 bool release) {
756 return mImpl.releaseSlot(buffer, c2buffer, release);
757}
758
759bool LinearInputBuffers::expireComponentBuffer(
760 const std::shared_ptr<C2Buffer> &c2buffer) {
761 return mImpl.expireComponentBuffer(c2buffer);
762}
763
764void LinearInputBuffers::flush() {
765 // This is no-op by default unless we're in array mode where we need to keep
766 // track of the flushed work.
767 mImpl.flush();
768}
769
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700770std::unique_ptr<InputBuffers> LinearInputBuffers::toArrayMode(size_t size) {
Wonsik Kim469c8342019-04-11 16:46:09 -0700771 std::unique_ptr<InputBuffersArray> array(
772 new InputBuffersArray(mComponentName.c_str(), "1D-Input[N]"));
773 array->setPool(mPool);
774 array->setFormat(mFormat);
775 array->initialize(
776 mImpl,
777 size,
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700778 [pool = mPool, format = mFormat] () -> sp<Codec2Buffer> {
779 return Alloc(pool, format);
780 });
Wonsik Kim469c8342019-04-11 16:46:09 -0700781 return std::move(array);
782}
783
784size_t LinearInputBuffers::numClientBuffers() const {
785 return mImpl.numClientBuffers();
786}
787
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700788// static
789sp<Codec2Buffer> LinearInputBuffers::Alloc(
790 const std::shared_ptr<C2BlockPool> &pool, const sp<AMessage> &format) {
791 int32_t capacity = kLinearBufferSize;
792 (void)format->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
793 if ((size_t)capacity > kMaxLinearBufferSize) {
794 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
795 capacity = kMaxLinearBufferSize;
796 }
797
798 // TODO: read usage from intf
Wonsik Kim469c8342019-04-11 16:46:09 -0700799 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
800 std::shared_ptr<C2LinearBlock> block;
801
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700802 c2_status_t err = pool->fetchLinearBlock(capacity, usage, &block);
Wonsik Kim469c8342019-04-11 16:46:09 -0700803 if (err != C2_OK) {
804 return nullptr;
805 }
806
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700807 return LinearBlockBuffer::Allocate(format, block);
808}
809
810sp<Codec2Buffer> LinearInputBuffers::createNewBuffer() {
811 return Alloc(mPool, mFormat);
Wonsik Kim469c8342019-04-11 16:46:09 -0700812}
813
814// EncryptedLinearInputBuffers
815
816EncryptedLinearInputBuffers::EncryptedLinearInputBuffers(
817 bool secure,
818 const sp<MemoryDealer> &dealer,
819 const sp<ICrypto> &crypto,
820 int32_t heapSeqNum,
821 size_t capacity,
822 size_t numInputSlots,
823 const char *componentName, const char *name)
824 : LinearInputBuffers(componentName, name),
825 mUsage({0, 0}),
826 mDealer(dealer),
827 mCrypto(crypto),
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700828 mMemoryVector(new std::vector<Entry>){
Wonsik Kim469c8342019-04-11 16:46:09 -0700829 if (secure) {
830 mUsage = { C2MemoryUsage::READ_PROTECTED, 0 };
831 } else {
832 mUsage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
833 }
834 for (size_t i = 0; i < numInputSlots; ++i) {
835 sp<IMemory> memory = mDealer->allocate(capacity);
836 if (memory == nullptr) {
837 ALOGD("[%s] Failed to allocate memory from dealer: only %zu slots allocated",
838 mName, i);
839 break;
840 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700841 mMemoryVector->push_back({std::weak_ptr<C2LinearBlock>(), memory, heapSeqNum});
Wonsik Kim469c8342019-04-11 16:46:09 -0700842 }
843}
844
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700845std::unique_ptr<InputBuffers> EncryptedLinearInputBuffers::toArrayMode(size_t size) {
846 std::unique_ptr<InputBuffersArray> array(
847 new InputBuffersArray(mComponentName.c_str(), "1D-EncryptedInput[N]"));
848 array->setPool(mPool);
849 array->setFormat(mFormat);
850 array->initialize(
851 mImpl,
852 size,
853 [pool = mPool,
854 format = mFormat,
855 usage = mUsage,
856 memoryVector = mMemoryVector] () -> sp<Codec2Buffer> {
857 return Alloc(pool, format, usage, memoryVector);
858 });
859 return std::move(array);
860}
861
862
863// static
864sp<Codec2Buffer> EncryptedLinearInputBuffers::Alloc(
865 const std::shared_ptr<C2BlockPool> &pool,
866 const sp<AMessage> &format,
867 C2MemoryUsage usage,
868 const std::shared_ptr<std::vector<EncryptedLinearInputBuffers::Entry>> &memoryVector) {
869 int32_t capacity = kLinearBufferSize;
870 (void)format->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
871 if ((size_t)capacity > kMaxLinearBufferSize) {
872 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
873 capacity = kMaxLinearBufferSize;
874 }
875
Wonsik Kim469c8342019-04-11 16:46:09 -0700876 sp<IMemory> memory;
877 size_t slot = 0;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700878 int32_t heapSeqNum = -1;
879 for (; slot < memoryVector->size(); ++slot) {
880 if (memoryVector->at(slot).block.expired()) {
881 memory = memoryVector->at(slot).memory;
882 heapSeqNum = memoryVector->at(slot).heapSeqNum;
Wonsik Kim469c8342019-04-11 16:46:09 -0700883 break;
884 }
885 }
886 if (memory == nullptr) {
887 return nullptr;
888 }
889
890 std::shared_ptr<C2LinearBlock> block;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700891 c2_status_t err = pool->fetchLinearBlock(capacity, usage, &block);
Wonsik Kim469c8342019-04-11 16:46:09 -0700892 if (err != C2_OK || block == nullptr) {
893 return nullptr;
894 }
895
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700896 memoryVector->at(slot).block = block;
897 return new EncryptedLinearBlockBuffer(format, block, memory, heapSeqNum);
898}
899
900sp<Codec2Buffer> EncryptedLinearInputBuffers::createNewBuffer() {
901 // TODO: android_2020
902 return nullptr;
Wonsik Kim469c8342019-04-11 16:46:09 -0700903}
904
905// GraphicMetadataInputBuffers
906
907GraphicMetadataInputBuffers::GraphicMetadataInputBuffers(
908 const char *componentName, const char *name)
909 : InputBuffers(componentName, name),
910 mImpl(mName),
911 mStore(GetCodec2PlatformAllocatorStore()) { }
912
913bool GraphicMetadataInputBuffers::requestNewBuffer(
914 size_t *index, sp<MediaCodecBuffer> *buffer) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700915 sp<Codec2Buffer> newBuffer = createNewBuffer();
Wonsik Kim469c8342019-04-11 16:46:09 -0700916 if (newBuffer == nullptr) {
917 return false;
918 }
919 *index = mImpl.assignSlot(newBuffer);
920 *buffer = newBuffer;
921 return true;
922}
923
924bool GraphicMetadataInputBuffers::releaseBuffer(
925 const sp<MediaCodecBuffer> &buffer,
926 std::shared_ptr<C2Buffer> *c2buffer,
927 bool release) {
928 return mImpl.releaseSlot(buffer, c2buffer, release);
929}
930
931bool GraphicMetadataInputBuffers::expireComponentBuffer(
932 const std::shared_ptr<C2Buffer> &c2buffer) {
933 return mImpl.expireComponentBuffer(c2buffer);
934}
935
936void GraphicMetadataInputBuffers::flush() {
937 // This is no-op by default unless we're in array mode where we need to keep
938 // track of the flushed work.
939}
940
941std::unique_ptr<InputBuffers> GraphicMetadataInputBuffers::toArrayMode(
942 size_t size) {
943 std::shared_ptr<C2Allocator> alloc;
944 c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
945 if (err != C2_OK) {
946 return nullptr;
947 }
948 std::unique_ptr<InputBuffersArray> array(
949 new InputBuffersArray(mComponentName.c_str(), "2D-MetaInput[N]"));
950 array->setPool(mPool);
951 array->setFormat(mFormat);
952 array->initialize(
953 mImpl,
954 size,
955 [format = mFormat, alloc]() -> sp<Codec2Buffer> {
956 return new GraphicMetadataBuffer(format, alloc);
957 });
958 return std::move(array);
959}
960
961size_t GraphicMetadataInputBuffers::numClientBuffers() const {
962 return mImpl.numClientBuffers();
963}
964
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700965sp<Codec2Buffer> GraphicMetadataInputBuffers::createNewBuffer() {
966 std::shared_ptr<C2Allocator> alloc;
967 c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
968 if (err != C2_OK) {
969 return nullptr;
970 }
971 return new GraphicMetadataBuffer(mFormat, alloc);
972}
973
Wonsik Kim469c8342019-04-11 16:46:09 -0700974// GraphicInputBuffers
975
976GraphicInputBuffers::GraphicInputBuffers(
Wonsik Kim41d83432020-04-27 16:40:49 -0700977 const char *componentName, const char *name)
Wonsik Kim469c8342019-04-11 16:46:09 -0700978 : InputBuffers(componentName, name),
979 mImpl(mName),
Wonsik Kim41d83432020-04-27 16:40:49 -0700980 mLocalBufferPool(LocalBufferPool::Create()) { }
Wonsik Kim469c8342019-04-11 16:46:09 -0700981
982bool GraphicInputBuffers::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700983 sp<Codec2Buffer> newBuffer = createNewBuffer();
Wonsik Kim469c8342019-04-11 16:46:09 -0700984 if (newBuffer == nullptr) {
985 return false;
986 }
987 *index = mImpl.assignSlot(newBuffer);
988 handleImageData(newBuffer);
989 *buffer = newBuffer;
990 return true;
991}
992
993bool GraphicInputBuffers::releaseBuffer(
994 const sp<MediaCodecBuffer> &buffer,
995 std::shared_ptr<C2Buffer> *c2buffer,
996 bool release) {
997 return mImpl.releaseSlot(buffer, c2buffer, release);
998}
999
1000bool GraphicInputBuffers::expireComponentBuffer(
1001 const std::shared_ptr<C2Buffer> &c2buffer) {
1002 return mImpl.expireComponentBuffer(c2buffer);
1003}
1004
1005void GraphicInputBuffers::flush() {
1006 // This is no-op by default unless we're in array mode where we need to keep
1007 // track of the flushed work.
1008}
1009
1010std::unique_ptr<InputBuffers> GraphicInputBuffers::toArrayMode(size_t size) {
1011 std::unique_ptr<InputBuffersArray> array(
1012 new InputBuffersArray(mComponentName.c_str(), "2D-BB-Input[N]"));
1013 array->setPool(mPool);
1014 array->setFormat(mFormat);
1015 array->initialize(
1016 mImpl,
1017 size,
1018 [pool = mPool, format = mFormat, lbp = mLocalBufferPool]() -> sp<Codec2Buffer> {
1019 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
1020 return AllocateGraphicBuffer(
1021 pool, format, HAL_PIXEL_FORMAT_YV12, usage, lbp);
1022 });
1023 return std::move(array);
1024}
1025
1026size_t GraphicInputBuffers::numClientBuffers() const {
1027 return mImpl.numClientBuffers();
1028}
1029
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001030sp<Codec2Buffer> GraphicInputBuffers::createNewBuffer() {
1031 // TODO: read usage from intf
1032 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
1033 return AllocateGraphicBuffer(
1034 mPool, mFormat, HAL_PIXEL_FORMAT_YV12, usage, mLocalBufferPool);
1035}
1036
Wonsik Kim469c8342019-04-11 16:46:09 -07001037// OutputBuffersArray
1038
1039void OutputBuffersArray::initialize(
1040 const FlexBuffersImpl &impl,
1041 size_t minSize,
1042 std::function<sp<Codec2Buffer>()> allocate) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001043 mAlloc = allocate;
Wonsik Kim469c8342019-04-11 16:46:09 -07001044 mImpl.initialize(impl, minSize, allocate);
1045}
1046
1047status_t OutputBuffersArray::registerBuffer(
1048 const std::shared_ptr<C2Buffer> &buffer,
1049 size_t *index,
1050 sp<MediaCodecBuffer> *clientBuffer) {
1051 sp<Codec2Buffer> c2Buffer;
1052 status_t err = mImpl.grabBuffer(
1053 index,
1054 &c2Buffer,
1055 [buffer](const sp<Codec2Buffer> &clientBuffer) {
1056 return clientBuffer->canCopy(buffer);
1057 });
1058 if (err == WOULD_BLOCK) {
1059 ALOGV("[%s] buffers temporarily not available", mName);
1060 return err;
1061 } else if (err != OK) {
1062 ALOGD("[%s] grabBuffer failed: %d", mName, err);
1063 return err;
1064 }
1065 c2Buffer->setFormat(mFormat);
1066 if (!c2Buffer->copy(buffer)) {
1067 ALOGD("[%s] copy buffer failed", mName);
1068 return WOULD_BLOCK;
1069 }
1070 submit(c2Buffer);
1071 handleImageData(c2Buffer);
1072 *clientBuffer = c2Buffer;
1073 ALOGV("[%s] grabbed buffer %zu", mName, *index);
1074 return OK;
1075}
1076
1077status_t OutputBuffersArray::registerCsd(
1078 const C2StreamInitDataInfo::output *csd,
1079 size_t *index,
1080 sp<MediaCodecBuffer> *clientBuffer) {
1081 sp<Codec2Buffer> c2Buffer;
1082 status_t err = mImpl.grabBuffer(
1083 index,
1084 &c2Buffer,
1085 [csd](const sp<Codec2Buffer> &clientBuffer) {
1086 return clientBuffer->base() != nullptr
1087 && clientBuffer->capacity() >= csd->flexCount();
1088 });
1089 if (err != OK) {
1090 return err;
1091 }
1092 memcpy(c2Buffer->base(), csd->m.value, csd->flexCount());
1093 c2Buffer->setRange(0, csd->flexCount());
1094 c2Buffer->setFormat(mFormat);
1095 *clientBuffer = c2Buffer;
1096 return OK;
1097}
1098
1099bool OutputBuffersArray::releaseBuffer(
1100 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) {
1101 return mImpl.returnBuffer(buffer, c2buffer, true);
1102}
1103
1104void OutputBuffersArray::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1105 (void)flushedWork;
1106 mImpl.flush();
1107 if (mSkipCutBuffer != nullptr) {
1108 mSkipCutBuffer->clear();
1109 }
1110}
1111
1112void OutputBuffersArray::getArray(Vector<sp<MediaCodecBuffer>> *array) const {
1113 mImpl.getArray(array);
1114}
1115
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001116size_t OutputBuffersArray::numClientBuffers() const {
1117 return mImpl.numClientBuffers();
1118}
1119
Wonsik Kim469c8342019-04-11 16:46:09 -07001120void OutputBuffersArray::realloc(const std::shared_ptr<C2Buffer> &c2buffer) {
Wonsik Kim469c8342019-04-11 16:46:09 -07001121 switch (c2buffer->data().type()) {
1122 case C2BufferData::LINEAR: {
1123 uint32_t size = kLinearBufferSize;
Nick Desaulniersd09eaea2019-10-07 20:19:39 -07001124 const std::vector<C2ConstLinearBlock> &linear_blocks = c2buffer->data().linearBlocks();
1125 const uint32_t block_size = linear_blocks.front().size();
1126 if (block_size < kMaxLinearBufferSize / 2) {
1127 size = block_size * 2;
Wonsik Kim469c8342019-04-11 16:46:09 -07001128 } else {
1129 size = kMaxLinearBufferSize;
1130 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001131 mAlloc = [format = mFormat, size] {
Wonsik Kim469c8342019-04-11 16:46:09 -07001132 return new LocalLinearBuffer(format, new ABuffer(size));
1133 };
Wonsik Kima39882b2019-06-20 16:13:56 -07001134 ALOGD("[%s] reallocating with linear buffer of size %u", mName, size);
Wonsik Kim469c8342019-04-11 16:46:09 -07001135 break;
1136 }
1137
Wonsik Kima39882b2019-06-20 16:13:56 -07001138 case C2BufferData::GRAPHIC: {
1139 // This is only called for RawGraphicOutputBuffers.
1140 mAlloc = [format = mFormat,
Wonsik Kim41d83432020-04-27 16:40:49 -07001141 lbp = LocalBufferPool::Create()] {
Wonsik Kima39882b2019-06-20 16:13:56 -07001142 return ConstGraphicBlockBuffer::AllocateEmpty(
1143 format,
1144 [lbp](size_t capacity) {
1145 return lbp->newBuffer(capacity);
1146 });
1147 };
1148 ALOGD("[%s] reallocating with graphic buffer: format = %s",
1149 mName, mFormat->debugString().c_str());
1150 break;
1151 }
Wonsik Kim469c8342019-04-11 16:46:09 -07001152
1153 case C2BufferData::INVALID: [[fallthrough]];
1154 case C2BufferData::LINEAR_CHUNKS: [[fallthrough]];
1155 case C2BufferData::GRAPHIC_CHUNKS: [[fallthrough]];
1156 default:
1157 ALOGD("Unsupported type: %d", (int)c2buffer->data().type());
1158 return;
1159 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001160 mImpl.realloc(mAlloc);
Wonsik Kim469c8342019-04-11 16:46:09 -07001161}
1162
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001163void OutputBuffersArray::grow(size_t newSize) {
1164 mImpl.grow(newSize, mAlloc);
Wonsik Kim469c8342019-04-11 16:46:09 -07001165}
1166
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001167void OutputBuffersArray::transferFrom(OutputBuffers* source) {
1168 mFormat = source->mFormat;
1169 mSkipCutBuffer = source->mSkipCutBuffer;
1170 mUnreportedFormat = source->mUnreportedFormat;
1171 mPending = std::move(source->mPending);
1172 mReorderStash = std::move(source->mReorderStash);
1173 mDepth = source->mDepth;
1174 mKey = source->mKey;
1175}
1176
Wonsik Kim469c8342019-04-11 16:46:09 -07001177// FlexOutputBuffers
1178
1179status_t FlexOutputBuffers::registerBuffer(
1180 const std::shared_ptr<C2Buffer> &buffer,
1181 size_t *index,
1182 sp<MediaCodecBuffer> *clientBuffer) {
1183 sp<Codec2Buffer> newBuffer = wrap(buffer);
1184 if (newBuffer == nullptr) {
1185 return NO_MEMORY;
1186 }
1187 newBuffer->setFormat(mFormat);
1188 *index = mImpl.assignSlot(newBuffer);
1189 handleImageData(newBuffer);
1190 *clientBuffer = newBuffer;
1191 ALOGV("[%s] registered buffer %zu", mName, *index);
1192 return OK;
1193}
1194
1195status_t FlexOutputBuffers::registerCsd(
1196 const C2StreamInitDataInfo::output *csd,
1197 size_t *index,
1198 sp<MediaCodecBuffer> *clientBuffer) {
1199 sp<Codec2Buffer> newBuffer = new LocalLinearBuffer(
1200 mFormat, ABuffer::CreateAsCopy(csd->m.value, csd->flexCount()));
1201 *index = mImpl.assignSlot(newBuffer);
1202 *clientBuffer = newBuffer;
1203 return OK;
1204}
1205
1206bool FlexOutputBuffers::releaseBuffer(
1207 const sp<MediaCodecBuffer> &buffer,
1208 std::shared_ptr<C2Buffer> *c2buffer) {
1209 return mImpl.releaseSlot(buffer, c2buffer, true);
1210}
1211
1212void FlexOutputBuffers::flush(
1213 const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1214 (void) flushedWork;
1215 // This is no-op by default unless we're in array mode where we need to keep
1216 // track of the flushed work.
1217}
1218
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001219std::unique_ptr<OutputBuffersArray> FlexOutputBuffers::toArrayMode(size_t size) {
Wonsik Kim469c8342019-04-11 16:46:09 -07001220 std::unique_ptr<OutputBuffersArray> array(new OutputBuffersArray(mComponentName.c_str()));
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001221 array->transferFrom(this);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001222 std::function<sp<Codec2Buffer>()> alloc = getAlloc();
1223 array->initialize(mImpl, size, alloc);
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001224 return array;
Wonsik Kim469c8342019-04-11 16:46:09 -07001225}
1226
1227size_t FlexOutputBuffers::numClientBuffers() const {
1228 return mImpl.numClientBuffers();
1229}
1230
1231// LinearOutputBuffers
1232
1233void LinearOutputBuffers::flush(
1234 const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1235 if (mSkipCutBuffer != nullptr) {
1236 mSkipCutBuffer->clear();
1237 }
1238 FlexOutputBuffers::flush(flushedWork);
1239}
1240
1241sp<Codec2Buffer> LinearOutputBuffers::wrap(const std::shared_ptr<C2Buffer> &buffer) {
1242 if (buffer == nullptr) {
1243 ALOGV("[%s] using a dummy buffer", mName);
1244 return new LocalLinearBuffer(mFormat, new ABuffer(0));
1245 }
1246 if (buffer->data().type() != C2BufferData::LINEAR) {
1247 ALOGV("[%s] non-linear buffer %d", mName, buffer->data().type());
1248 // We expect linear output buffers from the component.
1249 return nullptr;
1250 }
1251 if (buffer->data().linearBlocks().size() != 1u) {
1252 ALOGV("[%s] no linear buffers", mName);
1253 // We expect one and only one linear block from the component.
1254 return nullptr;
1255 }
1256 sp<Codec2Buffer> clientBuffer = ConstLinearBlockBuffer::Allocate(mFormat, buffer);
1257 if (clientBuffer == nullptr) {
1258 ALOGD("[%s] ConstLinearBlockBuffer::Allocate failed", mName);
1259 return nullptr;
1260 }
1261 submit(clientBuffer);
1262 return clientBuffer;
1263}
1264
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001265std::function<sp<Codec2Buffer>()> LinearOutputBuffers::getAlloc() {
1266 return [format = mFormat]{
1267 // TODO: proper max output size
1268 return new LocalLinearBuffer(format, new ABuffer(kLinearBufferSize));
1269 };
Wonsik Kim469c8342019-04-11 16:46:09 -07001270}
1271
1272// GraphicOutputBuffers
1273
1274sp<Codec2Buffer> GraphicOutputBuffers::wrap(const std::shared_ptr<C2Buffer> &buffer) {
1275 return new DummyContainerBuffer(mFormat, buffer);
1276}
1277
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001278std::function<sp<Codec2Buffer>()> GraphicOutputBuffers::getAlloc() {
1279 return [format = mFormat]{
1280 return new DummyContainerBuffer(format);
1281 };
Wonsik Kim469c8342019-04-11 16:46:09 -07001282}
1283
1284// RawGraphicOutputBuffers
1285
1286RawGraphicOutputBuffers::RawGraphicOutputBuffers(
Wonsik Kim41d83432020-04-27 16:40:49 -07001287 const char *componentName, const char *name)
Wonsik Kim469c8342019-04-11 16:46:09 -07001288 : FlexOutputBuffers(componentName, name),
Wonsik Kim41d83432020-04-27 16:40:49 -07001289 mLocalBufferPool(LocalBufferPool::Create()) { }
Wonsik Kim469c8342019-04-11 16:46:09 -07001290
1291sp<Codec2Buffer> RawGraphicOutputBuffers::wrap(const std::shared_ptr<C2Buffer> &buffer) {
1292 if (buffer == nullptr) {
1293 sp<Codec2Buffer> c2buffer = ConstGraphicBlockBuffer::AllocateEmpty(
1294 mFormat,
1295 [lbp = mLocalBufferPool](size_t capacity) {
1296 return lbp->newBuffer(capacity);
1297 });
1298 if (c2buffer == nullptr) {
1299 ALOGD("[%s] ConstGraphicBlockBuffer::AllocateEmpty failed", mName);
1300 return nullptr;
1301 }
1302 c2buffer->setRange(0, 0);
1303 return c2buffer;
1304 } else {
1305 return ConstGraphicBlockBuffer::Allocate(
1306 mFormat,
1307 buffer,
1308 [lbp = mLocalBufferPool](size_t capacity) {
1309 return lbp->newBuffer(capacity);
1310 });
1311 }
1312}
1313
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001314std::function<sp<Codec2Buffer>()> RawGraphicOutputBuffers::getAlloc() {
1315 return [format = mFormat, lbp = mLocalBufferPool]{
1316 return ConstGraphicBlockBuffer::AllocateEmpty(
1317 format,
1318 [lbp](size_t capacity) {
1319 return lbp->newBuffer(capacity);
1320 });
1321 };
Wonsik Kim469c8342019-04-11 16:46:09 -07001322}
1323
1324} // namespace android