blob: 10f7e666a36d8bbdbbc3d89a3105d1ffb0dca6df [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) {
Taehwan Kimfd9b8092020-09-17 12:26:40 +090094 int64_t offsetDelta =
95 (int64_t)img->mPlane[1].mOffset - (int64_t)img->mPlane[0].mOffset;
96 int32_t vstride = int32_t(offsetDelta / stride);
Wonsik Kim469c8342019-04-11 16:46:09 -070097 newFormat->setInt32(KEY_SLICE_HEIGHT, vstride);
98 ALOGD("[%s] updating vstride = %d", mName, vstride);
99 }
100 }
101 setFormat(newFormat);
102 buffer->setFormat(newFormat);
103 }
104}
105
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700106// InputBuffers
107
108sp<Codec2Buffer> InputBuffers::cloneAndReleaseBuffer(const sp<MediaCodecBuffer> &buffer) {
109 sp<Codec2Buffer> copy = createNewBuffer();
110 if (copy == nullptr) {
111 return nullptr;
112 }
113 std::shared_ptr<C2Buffer> c2buffer;
114 if (!releaseBuffer(buffer, &c2buffer, true)) {
115 return nullptr;
116 }
117 if (!copy->canCopy(c2buffer)) {
118 return nullptr;
119 }
120 if (!copy->copy(c2buffer)) {
121 return nullptr;
122 }
123 return copy;
124}
125
Wonsik Kim469c8342019-04-11 16:46:09 -0700126// OutputBuffers
127
Wonsik Kim41d83432020-04-27 16:40:49 -0700128OutputBuffers::OutputBuffers(const char *componentName, const char *name)
129 : CCodecBuffers(componentName, name) { }
130
131OutputBuffers::~OutputBuffers() = default;
132
Wonsik Kim469c8342019-04-11 16:46:09 -0700133void OutputBuffers::initSkipCutBuffer(
134 int32_t delay, int32_t padding, int32_t sampleRate, int32_t channelCount) {
135 CHECK(mSkipCutBuffer == nullptr);
136 mDelay = delay;
137 mPadding = padding;
138 mSampleRate = sampleRate;
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700139 mChannelCount = channelCount;
140 setSkipCutBuffer(delay, padding);
Wonsik Kim469c8342019-04-11 16:46:09 -0700141}
142
143void OutputBuffers::updateSkipCutBuffer(int32_t sampleRate, int32_t channelCount) {
144 if (mSkipCutBuffer == nullptr) {
145 return;
146 }
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700147 if (mSampleRate == sampleRate && mChannelCount == channelCount) {
148 return;
149 }
Wonsik Kim469c8342019-04-11 16:46:09 -0700150 int32_t delay = mDelay;
151 int32_t padding = mPadding;
152 if (sampleRate != mSampleRate) {
153 delay = ((int64_t)delay * sampleRate) / mSampleRate;
154 padding = ((int64_t)padding * sampleRate) / mSampleRate;
155 }
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700156 mSampleRate = sampleRate;
157 mChannelCount = channelCount;
158 setSkipCutBuffer(delay, padding);
Wonsik Kim469c8342019-04-11 16:46:09 -0700159}
160
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700161void OutputBuffers::updateSkipCutBuffer(
162 const sp<AMessage> &format, bool notify) {
163 AString mediaType;
164 if (format->findString(KEY_MIME, &mediaType)
165 && mediaType == MIMETYPE_AUDIO_RAW) {
166 int32_t channelCount;
167 int32_t sampleRate;
168 if (format->findInt32(KEY_CHANNEL_COUNT, &channelCount)
169 && format->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
170 updateSkipCutBuffer(sampleRate, channelCount);
171 }
172 }
173 if (notify) {
174 mUnreportedFormat = nullptr;
175 }
176}
177
Wonsik Kim469c8342019-04-11 16:46:09 -0700178void OutputBuffers::submit(const sp<MediaCodecBuffer> &buffer) {
179 if (mSkipCutBuffer != nullptr) {
180 mSkipCutBuffer->submit(buffer);
181 }
182}
183
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700184void OutputBuffers::setSkipCutBuffer(int32_t skip, int32_t cut) {
Wonsik Kim469c8342019-04-11 16:46:09 -0700185 if (mSkipCutBuffer != nullptr) {
186 size_t prevSize = mSkipCutBuffer->size();
187 if (prevSize != 0u) {
188 ALOGD("[%s] Replacing SkipCutBuffer holding %zu bytes", mName, prevSize);
189 }
190 }
Wonsik Kim40b0b1d2020-03-25 15:47:35 -0700191 mSkipCutBuffer = new SkipCutBuffer(skip, cut, mChannelCount);
Wonsik Kim469c8342019-04-11 16:46:09 -0700192}
193
Pawin Vongmasa9b906982020-04-11 05:07:15 -0700194void OutputBuffers::clearStash() {
195 mPending.clear();
196 mReorderStash.clear();
197 mDepth = 0;
198 mKey = C2Config::ORDINAL;
199 mUnreportedFormat = nullptr;
200}
201
202void OutputBuffers::flushStash() {
203 for (StashEntry& e : mPending) {
204 e.notify = false;
205 }
206 for (StashEntry& e : mReorderStash) {
207 e.notify = false;
208 }
209}
210
211uint32_t OutputBuffers::getReorderDepth() const {
212 return mDepth;
213}
214
215void OutputBuffers::setReorderDepth(uint32_t depth) {
216 mPending.splice(mPending.end(), mReorderStash);
217 mDepth = depth;
218}
219
220void OutputBuffers::setReorderKey(C2Config::ordinal_key_t key) {
221 mPending.splice(mPending.end(), mReorderStash);
222 mKey = key;
223}
224
225void OutputBuffers::pushToStash(
226 const std::shared_ptr<C2Buffer>& buffer,
227 bool notify,
228 int64_t timestamp,
229 int32_t flags,
230 const sp<AMessage>& format,
231 const C2WorkOrdinalStruct& ordinal) {
232 bool eos = flags & MediaCodec::BUFFER_FLAG_EOS;
233 if (!buffer && eos) {
234 // TRICKY: we may be violating ordering of the stash here. Because we
235 // don't expect any more emplace() calls after this, the ordering should
236 // not matter.
237 mReorderStash.emplace_back(
238 buffer, notify, timestamp, flags, format, ordinal);
239 } else {
240 flags = flags & ~MediaCodec::BUFFER_FLAG_EOS;
241 auto it = mReorderStash.begin();
242 for (; it != mReorderStash.end(); ++it) {
243 if (less(ordinal, it->ordinal)) {
244 break;
245 }
246 }
247 mReorderStash.emplace(it,
248 buffer, notify, timestamp, flags, format, ordinal);
249 if (eos) {
250 mReorderStash.back().flags =
251 mReorderStash.back().flags | MediaCodec::BUFFER_FLAG_EOS;
252 }
253 }
254 while (!mReorderStash.empty() && mReorderStash.size() > mDepth) {
255 mPending.push_back(mReorderStash.front());
256 mReorderStash.pop_front();
257 }
258 ALOGV("[%s] %s: pushToStash -- pending size = %zu", mName, __func__, mPending.size());
259}
260
261OutputBuffers::BufferAction OutputBuffers::popFromStashAndRegister(
262 std::shared_ptr<C2Buffer>* c2Buffer,
263 size_t* index,
264 sp<MediaCodecBuffer>* outBuffer) {
265 if (mPending.empty()) {
266 return SKIP;
267 }
268
269 // Retrieve the first entry.
270 StashEntry &entry = mPending.front();
271
272 *c2Buffer = entry.buffer;
273 sp<AMessage> outputFormat = entry.format;
274
275 // The output format can be processed without a registered slot.
276 if (outputFormat) {
277 ALOGD("[%s] popFromStashAndRegister: output format changed to %s",
278 mName, outputFormat->debugString().c_str());
279 updateSkipCutBuffer(outputFormat, entry.notify);
280 }
281
282 if (entry.notify) {
283 if (outputFormat) {
284 setFormat(outputFormat);
285 } else if (mUnreportedFormat) {
286 outputFormat = mUnreportedFormat;
287 setFormat(outputFormat);
288 }
289 mUnreportedFormat = nullptr;
290 } else {
291 if (outputFormat) {
292 mUnreportedFormat = outputFormat;
293 } else if (!mUnreportedFormat) {
294 mUnreportedFormat = mFormat;
295 }
296 }
297
298 // Flushing mReorderStash because no other buffers should come after output
299 // EOS.
300 if (entry.flags & MediaCodec::BUFFER_FLAG_EOS) {
301 // Flush reorder stash
302 setReorderDepth(0);
303 }
304
305 if (!entry.notify) {
306 mPending.pop_front();
307 return DISCARD;
308 }
309
310 // Try to register the buffer.
311 status_t err = registerBuffer(*c2Buffer, index, outBuffer);
312 if (err != OK) {
313 if (err != WOULD_BLOCK) {
314 return REALLOCATE;
315 }
316 return RETRY;
317 }
318
319 // Append information from the front stash entry to outBuffer.
320 (*outBuffer)->meta()->setInt64("timeUs", entry.timestamp);
321 (*outBuffer)->meta()->setInt32("flags", entry.flags);
322 ALOGV("[%s] popFromStashAndRegister: "
323 "out buffer index = %zu [%p] => %p + %zu (%lld)",
324 mName, *index, outBuffer->get(),
325 (*outBuffer)->data(), (*outBuffer)->size(),
326 (long long)entry.timestamp);
327
328 // The front entry of mPending will be removed now that the registration
329 // succeeded.
330 mPending.pop_front();
331 return NOTIFY_CLIENT;
332}
333
334bool OutputBuffers::popPending(StashEntry *entry) {
335 if (mPending.empty()) {
336 return false;
337 }
338 *entry = mPending.front();
339 mPending.pop_front();
340 return true;
341}
342
343void OutputBuffers::deferPending(const OutputBuffers::StashEntry &entry) {
344 mPending.push_front(entry);
345}
346
347bool OutputBuffers::hasPending() const {
348 return !mPending.empty();
349}
350
351bool OutputBuffers::less(
352 const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2) const {
353 switch (mKey) {
354 case C2Config::ORDINAL: return o1.frameIndex < o2.frameIndex;
355 case C2Config::TIMESTAMP: return o1.timestamp < o2.timestamp;
356 case C2Config::CUSTOM: return o1.customOrdinal < o2.customOrdinal;
357 default:
358 ALOGD("Unrecognized key; default to timestamp");
359 return o1.frameIndex < o2.frameIndex;
360 }
361}
362
Wonsik Kim469c8342019-04-11 16:46:09 -0700363// LocalBufferPool
364
Wonsik Kim41d83432020-04-27 16:40:49 -0700365constexpr size_t kInitialPoolCapacity = kMaxLinearBufferSize;
366constexpr size_t kMaxPoolCapacity = kMaxLinearBufferSize * 32;
367
368std::shared_ptr<LocalBufferPool> LocalBufferPool::Create() {
369 return std::shared_ptr<LocalBufferPool>(new LocalBufferPool(kInitialPoolCapacity));
Wonsik Kim469c8342019-04-11 16:46:09 -0700370}
371
372sp<ABuffer> LocalBufferPool::newBuffer(size_t capacity) {
373 Mutex::Autolock lock(mMutex);
374 auto it = std::find_if(
375 mPool.begin(), mPool.end(),
376 [capacity](const std::vector<uint8_t> &vec) {
377 return vec.capacity() >= capacity;
378 });
379 if (it != mPool.end()) {
380 sp<ABuffer> buffer = new VectorBuffer(std::move(*it), shared_from_this());
381 mPool.erase(it);
382 return buffer;
383 }
384 if (mUsedSize + capacity > mPoolCapacity) {
385 while (!mPool.empty()) {
386 mUsedSize -= mPool.back().capacity();
387 mPool.pop_back();
388 }
Wonsik Kim41d83432020-04-27 16:40:49 -0700389 while (mUsedSize + capacity > mPoolCapacity && mPoolCapacity * 2 <= kMaxPoolCapacity) {
390 ALOGD("Increasing local buffer pool capacity from %zu to %zu",
391 mPoolCapacity, mPoolCapacity * 2);
392 mPoolCapacity *= 2;
393 }
Wonsik Kim469c8342019-04-11 16:46:09 -0700394 if (mUsedSize + capacity > mPoolCapacity) {
395 ALOGD("mUsedSize = %zu, capacity = %zu, mPoolCapacity = %zu",
396 mUsedSize, capacity, mPoolCapacity);
397 return nullptr;
398 }
399 }
400 std::vector<uint8_t> vec(capacity);
401 mUsedSize += vec.capacity();
402 return new VectorBuffer(std::move(vec), shared_from_this());
403}
404
405LocalBufferPool::VectorBuffer::VectorBuffer(
406 std::vector<uint8_t> &&vec, const std::shared_ptr<LocalBufferPool> &pool)
407 : ABuffer(vec.data(), vec.capacity()),
408 mVec(std::move(vec)),
409 mPool(pool) {
410}
411
412LocalBufferPool::VectorBuffer::~VectorBuffer() {
413 std::shared_ptr<LocalBufferPool> pool = mPool.lock();
414 if (pool) {
415 // If pool is alive, return the vector back to the pool so that
416 // it can be recycled.
417 pool->returnVector(std::move(mVec));
418 }
419}
420
421void LocalBufferPool::returnVector(std::vector<uint8_t> &&vec) {
422 Mutex::Autolock lock(mMutex);
423 mPool.push_front(std::move(vec));
424}
425
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700426// FlexBuffersImpl
427
Wonsik Kim469c8342019-04-11 16:46:09 -0700428size_t FlexBuffersImpl::assignSlot(const sp<Codec2Buffer> &buffer) {
429 for (size_t i = 0; i < mBuffers.size(); ++i) {
430 if (mBuffers[i].clientBuffer == nullptr
431 && mBuffers[i].compBuffer.expired()) {
432 mBuffers[i].clientBuffer = buffer;
433 return i;
434 }
435 }
436 mBuffers.push_back({ buffer, std::weak_ptr<C2Buffer>() });
437 return mBuffers.size() - 1;
438}
439
Wonsik Kim469c8342019-04-11 16:46:09 -0700440bool FlexBuffersImpl::releaseSlot(
441 const sp<MediaCodecBuffer> &buffer,
442 std::shared_ptr<C2Buffer> *c2buffer,
443 bool release) {
444 sp<Codec2Buffer> clientBuffer;
445 size_t index = mBuffers.size();
446 for (size_t i = 0; i < mBuffers.size(); ++i) {
447 if (mBuffers[i].clientBuffer == buffer) {
448 clientBuffer = mBuffers[i].clientBuffer;
449 if (release) {
450 mBuffers[i].clientBuffer.clear();
451 }
452 index = i;
453 break;
454 }
455 }
456 if (clientBuffer == nullptr) {
457 ALOGV("[%s] %s: No matching buffer found", mName, __func__);
458 return false;
459 }
460 std::shared_ptr<C2Buffer> result = mBuffers[index].compBuffer.lock();
461 if (!result) {
462 result = clientBuffer->asC2Buffer();
Wonsik Kimf9b32122020-04-02 11:30:17 -0700463 clientBuffer->clearC2BufferRefs();
Wonsik Kim469c8342019-04-11 16:46:09 -0700464 mBuffers[index].compBuffer = result;
465 }
466 if (c2buffer) {
467 *c2buffer = result;
468 }
469 return true;
470}
471
472bool FlexBuffersImpl::expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
473 for (size_t i = 0; i < mBuffers.size(); ++i) {
474 std::shared_ptr<C2Buffer> compBuffer =
475 mBuffers[i].compBuffer.lock();
476 if (!compBuffer || compBuffer != c2buffer) {
477 continue;
478 }
479 mBuffers[i].compBuffer.reset();
480 ALOGV("[%s] codec released buffer #%zu", mName, i);
481 return true;
482 }
483 ALOGV("[%s] codec released an unknown buffer", mName);
484 return false;
485}
486
487void FlexBuffersImpl::flush() {
488 ALOGV("[%s] buffers are flushed %zu", mName, mBuffers.size());
489 mBuffers.clear();
490}
491
492size_t FlexBuffersImpl::numClientBuffers() const {
493 return std::count_if(
494 mBuffers.begin(), mBuffers.end(),
495 [](const Entry &entry) {
496 return (entry.clientBuffer != nullptr);
497 });
498}
499
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700500size_t FlexBuffersImpl::numComponentBuffers() const {
501 return std::count_if(
502 mBuffers.begin(), mBuffers.end(),
503 [](const Entry &entry) {
504 return !entry.compBuffer.expired();
505 });
506}
507
Wonsik Kim469c8342019-04-11 16:46:09 -0700508// BuffersArrayImpl
509
510void BuffersArrayImpl::initialize(
511 const FlexBuffersImpl &impl,
512 size_t minSize,
513 std::function<sp<Codec2Buffer>()> allocate) {
514 mImplName = impl.mImplName + "[N]";
515 mName = mImplName.c_str();
516 for (size_t i = 0; i < impl.mBuffers.size(); ++i) {
517 sp<Codec2Buffer> clientBuffer = impl.mBuffers[i].clientBuffer;
518 bool ownedByClient = (clientBuffer != nullptr);
519 if (!ownedByClient) {
520 clientBuffer = allocate();
521 }
522 mBuffers.push_back({ clientBuffer, impl.mBuffers[i].compBuffer, ownedByClient });
523 }
524 ALOGV("[%s] converted %zu buffers to array mode of %zu", mName, mBuffers.size(), minSize);
525 for (size_t i = impl.mBuffers.size(); i < minSize; ++i) {
526 mBuffers.push_back({ allocate(), std::weak_ptr<C2Buffer>(), false });
527 }
528}
529
530status_t BuffersArrayImpl::grabBuffer(
531 size_t *index,
532 sp<Codec2Buffer> *buffer,
533 std::function<bool(const sp<Codec2Buffer> &)> match) {
534 // allBuffersDontMatch remains true if all buffers are available but
535 // match() returns false for every buffer.
536 bool allBuffersDontMatch = true;
537 for (size_t i = 0; i < mBuffers.size(); ++i) {
538 if (!mBuffers[i].ownedByClient && mBuffers[i].compBuffer.expired()) {
539 if (match(mBuffers[i].clientBuffer)) {
540 mBuffers[i].ownedByClient = true;
541 *buffer = mBuffers[i].clientBuffer;
542 (*buffer)->meta()->clear();
543 (*buffer)->setRange(0, (*buffer)->capacity());
544 *index = i;
545 return OK;
546 }
547 } else {
548 allBuffersDontMatch = false;
549 }
550 }
551 return allBuffersDontMatch ? NO_MEMORY : WOULD_BLOCK;
552}
553
554bool BuffersArrayImpl::returnBuffer(
555 const sp<MediaCodecBuffer> &buffer,
556 std::shared_ptr<C2Buffer> *c2buffer,
557 bool release) {
558 sp<Codec2Buffer> clientBuffer;
559 size_t index = mBuffers.size();
560 for (size_t i = 0; i < mBuffers.size(); ++i) {
561 if (mBuffers[i].clientBuffer == buffer) {
562 if (!mBuffers[i].ownedByClient) {
563 ALOGD("[%s] Client returned a buffer it does not own according to our record: %zu",
564 mName, i);
565 }
566 clientBuffer = mBuffers[i].clientBuffer;
567 if (release) {
568 mBuffers[i].ownedByClient = false;
569 }
570 index = i;
571 break;
572 }
573 }
574 if (clientBuffer == nullptr) {
575 ALOGV("[%s] %s: No matching buffer found", mName, __func__);
576 return false;
577 }
578 ALOGV("[%s] %s: matching buffer found (index=%zu)", mName, __func__, index);
579 std::shared_ptr<C2Buffer> result = mBuffers[index].compBuffer.lock();
580 if (!result) {
581 result = clientBuffer->asC2Buffer();
Wonsik Kimf9b32122020-04-02 11:30:17 -0700582 clientBuffer->clearC2BufferRefs();
Wonsik Kim469c8342019-04-11 16:46:09 -0700583 mBuffers[index].compBuffer = result;
584 }
585 if (c2buffer) {
586 *c2buffer = result;
587 }
588 return true;
589}
590
591bool BuffersArrayImpl::expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
592 for (size_t i = 0; i < mBuffers.size(); ++i) {
593 std::shared_ptr<C2Buffer> compBuffer =
594 mBuffers[i].compBuffer.lock();
595 if (!compBuffer) {
596 continue;
597 }
598 if (c2buffer == compBuffer) {
599 if (mBuffers[i].ownedByClient) {
600 // This should not happen.
601 ALOGD("[%s] codec released a buffer owned by client "
602 "(index %zu)", mName, i);
603 }
604 mBuffers[i].compBuffer.reset();
605 ALOGV("[%s] codec released buffer #%zu(array mode)", mName, i);
606 return true;
607 }
608 }
609 ALOGV("[%s] codec released an unknown buffer (array mode)", mName);
610 return false;
611}
612
613void BuffersArrayImpl::getArray(Vector<sp<MediaCodecBuffer>> *array) const {
614 array->clear();
615 for (const Entry &entry : mBuffers) {
616 array->push(entry.clientBuffer);
617 }
618}
619
620void BuffersArrayImpl::flush() {
621 for (Entry &entry : mBuffers) {
622 entry.ownedByClient = false;
623 }
624}
625
626void BuffersArrayImpl::realloc(std::function<sp<Codec2Buffer>()> alloc) {
627 size_t size = mBuffers.size();
628 mBuffers.clear();
629 for (size_t i = 0; i < size; ++i) {
630 mBuffers.push_back({ alloc(), std::weak_ptr<C2Buffer>(), false });
631 }
632}
633
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700634void BuffersArrayImpl::grow(
635 size_t newSize, std::function<sp<Codec2Buffer>()> alloc) {
636 CHECK_LT(mBuffers.size(), newSize);
637 while (mBuffers.size() < newSize) {
638 mBuffers.push_back({ alloc(), std::weak_ptr<C2Buffer>(), false });
639 }
640}
641
Wonsik Kim469c8342019-04-11 16:46:09 -0700642size_t BuffersArrayImpl::numClientBuffers() const {
643 return std::count_if(
644 mBuffers.begin(), mBuffers.end(),
645 [](const Entry &entry) {
646 return entry.ownedByClient;
647 });
648}
649
Wonsik Kima39882b2019-06-20 16:13:56 -0700650size_t BuffersArrayImpl::arraySize() const {
651 return mBuffers.size();
652}
653
Wonsik Kim469c8342019-04-11 16:46:09 -0700654// InputBuffersArray
655
656void InputBuffersArray::initialize(
657 const FlexBuffersImpl &impl,
658 size_t minSize,
659 std::function<sp<Codec2Buffer>()> allocate) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700660 mAllocate = allocate;
Wonsik Kim469c8342019-04-11 16:46:09 -0700661 mImpl.initialize(impl, minSize, allocate);
662}
663
664void InputBuffersArray::getArray(Vector<sp<MediaCodecBuffer>> *array) const {
665 mImpl.getArray(array);
666}
667
668bool InputBuffersArray::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
669 sp<Codec2Buffer> c2Buffer;
670 status_t err = mImpl.grabBuffer(index, &c2Buffer);
671 if (err == OK) {
672 c2Buffer->setFormat(mFormat);
673 handleImageData(c2Buffer);
674 *buffer = c2Buffer;
675 return true;
676 }
677 return false;
678}
679
680bool InputBuffersArray::releaseBuffer(
681 const sp<MediaCodecBuffer> &buffer,
682 std::shared_ptr<C2Buffer> *c2buffer,
683 bool release) {
684 return mImpl.returnBuffer(buffer, c2buffer, release);
685}
686
687bool InputBuffersArray::expireComponentBuffer(
688 const std::shared_ptr<C2Buffer> &c2buffer) {
689 return mImpl.expireComponentBuffer(c2buffer);
690}
691
692void InputBuffersArray::flush() {
693 mImpl.flush();
694}
695
696size_t InputBuffersArray::numClientBuffers() const {
697 return mImpl.numClientBuffers();
698}
699
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700700sp<Codec2Buffer> InputBuffersArray::createNewBuffer() {
701 return mAllocate();
702}
703
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800704// SlotInputBuffers
705
706bool SlotInputBuffers::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
707 sp<Codec2Buffer> newBuffer = createNewBuffer();
708 *index = mImpl.assignSlot(newBuffer);
709 *buffer = newBuffer;
710 return true;
711}
712
713bool SlotInputBuffers::releaseBuffer(
714 const sp<MediaCodecBuffer> &buffer,
715 std::shared_ptr<C2Buffer> *c2buffer,
716 bool release) {
717 return mImpl.releaseSlot(buffer, c2buffer, release);
718}
719
720bool SlotInputBuffers::expireComponentBuffer(
721 const std::shared_ptr<C2Buffer> &c2buffer) {
722 return mImpl.expireComponentBuffer(c2buffer);
723}
724
725void SlotInputBuffers::flush() {
726 mImpl.flush();
727}
728
729std::unique_ptr<InputBuffers> SlotInputBuffers::toArrayMode(size_t) {
730 TRESPASS("Array mode should not be called at non-legacy mode");
731 return nullptr;
732}
733
734size_t SlotInputBuffers::numClientBuffers() const {
735 return mImpl.numClientBuffers();
736}
737
738sp<Codec2Buffer> SlotInputBuffers::createNewBuffer() {
739 return new DummyContainerBuffer{mFormat, nullptr};
740}
741
Wonsik Kim469c8342019-04-11 16:46:09 -0700742// LinearInputBuffers
743
744bool LinearInputBuffers::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700745 sp<Codec2Buffer> newBuffer = createNewBuffer();
Wonsik Kim469c8342019-04-11 16:46:09 -0700746 if (newBuffer == nullptr) {
747 return false;
748 }
749 *index = mImpl.assignSlot(newBuffer);
750 *buffer = newBuffer;
751 return true;
752}
753
754bool LinearInputBuffers::releaseBuffer(
755 const sp<MediaCodecBuffer> &buffer,
756 std::shared_ptr<C2Buffer> *c2buffer,
757 bool release) {
758 return mImpl.releaseSlot(buffer, c2buffer, release);
759}
760
761bool LinearInputBuffers::expireComponentBuffer(
762 const std::shared_ptr<C2Buffer> &c2buffer) {
763 return mImpl.expireComponentBuffer(c2buffer);
764}
765
766void LinearInputBuffers::flush() {
767 // This is no-op by default unless we're in array mode where we need to keep
768 // track of the flushed work.
769 mImpl.flush();
770}
771
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700772std::unique_ptr<InputBuffers> LinearInputBuffers::toArrayMode(size_t size) {
Wonsik Kim469c8342019-04-11 16:46:09 -0700773 std::unique_ptr<InputBuffersArray> array(
774 new InputBuffersArray(mComponentName.c_str(), "1D-Input[N]"));
775 array->setPool(mPool);
776 array->setFormat(mFormat);
777 array->initialize(
778 mImpl,
779 size,
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700780 [pool = mPool, format = mFormat] () -> sp<Codec2Buffer> {
781 return Alloc(pool, format);
782 });
Wonsik Kim469c8342019-04-11 16:46:09 -0700783 return std::move(array);
784}
785
786size_t LinearInputBuffers::numClientBuffers() const {
787 return mImpl.numClientBuffers();
788}
789
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700790// static
791sp<Codec2Buffer> LinearInputBuffers::Alloc(
792 const std::shared_ptr<C2BlockPool> &pool, const sp<AMessage> &format) {
793 int32_t capacity = kLinearBufferSize;
794 (void)format->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
795 if ((size_t)capacity > kMaxLinearBufferSize) {
796 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
797 capacity = kMaxLinearBufferSize;
798 }
799
800 // TODO: read usage from intf
Wonsik Kim469c8342019-04-11 16:46:09 -0700801 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
802 std::shared_ptr<C2LinearBlock> block;
803
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700804 c2_status_t err = pool->fetchLinearBlock(capacity, usage, &block);
Wonsik Kim469c8342019-04-11 16:46:09 -0700805 if (err != C2_OK) {
806 return nullptr;
807 }
808
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700809 return LinearBlockBuffer::Allocate(format, block);
810}
811
812sp<Codec2Buffer> LinearInputBuffers::createNewBuffer() {
813 return Alloc(mPool, mFormat);
Wonsik Kim469c8342019-04-11 16:46:09 -0700814}
815
816// EncryptedLinearInputBuffers
817
818EncryptedLinearInputBuffers::EncryptedLinearInputBuffers(
819 bool secure,
820 const sp<MemoryDealer> &dealer,
821 const sp<ICrypto> &crypto,
822 int32_t heapSeqNum,
823 size_t capacity,
824 size_t numInputSlots,
825 const char *componentName, const char *name)
826 : LinearInputBuffers(componentName, name),
827 mUsage({0, 0}),
828 mDealer(dealer),
829 mCrypto(crypto),
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700830 mMemoryVector(new std::vector<Entry>){
Wonsik Kim469c8342019-04-11 16:46:09 -0700831 if (secure) {
832 mUsage = { C2MemoryUsage::READ_PROTECTED, 0 };
833 } else {
834 mUsage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
835 }
836 for (size_t i = 0; i < numInputSlots; ++i) {
837 sp<IMemory> memory = mDealer->allocate(capacity);
838 if (memory == nullptr) {
839 ALOGD("[%s] Failed to allocate memory from dealer: only %zu slots allocated",
840 mName, i);
841 break;
842 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700843 mMemoryVector->push_back({std::weak_ptr<C2LinearBlock>(), memory, heapSeqNum});
Wonsik Kim469c8342019-04-11 16:46:09 -0700844 }
845}
846
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700847std::unique_ptr<InputBuffers> EncryptedLinearInputBuffers::toArrayMode(size_t size) {
848 std::unique_ptr<InputBuffersArray> array(
849 new InputBuffersArray(mComponentName.c_str(), "1D-EncryptedInput[N]"));
850 array->setPool(mPool);
851 array->setFormat(mFormat);
852 array->initialize(
853 mImpl,
854 size,
855 [pool = mPool,
856 format = mFormat,
857 usage = mUsage,
858 memoryVector = mMemoryVector] () -> sp<Codec2Buffer> {
859 return Alloc(pool, format, usage, memoryVector);
860 });
861 return std::move(array);
862}
863
864
865// static
866sp<Codec2Buffer> EncryptedLinearInputBuffers::Alloc(
867 const std::shared_ptr<C2BlockPool> &pool,
868 const sp<AMessage> &format,
869 C2MemoryUsage usage,
870 const std::shared_ptr<std::vector<EncryptedLinearInputBuffers::Entry>> &memoryVector) {
871 int32_t capacity = kLinearBufferSize;
872 (void)format->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
873 if ((size_t)capacity > kMaxLinearBufferSize) {
874 ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
875 capacity = kMaxLinearBufferSize;
876 }
877
Wonsik Kim469c8342019-04-11 16:46:09 -0700878 sp<IMemory> memory;
879 size_t slot = 0;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700880 int32_t heapSeqNum = -1;
881 for (; slot < memoryVector->size(); ++slot) {
882 if (memoryVector->at(slot).block.expired()) {
883 memory = memoryVector->at(slot).memory;
884 heapSeqNum = memoryVector->at(slot).heapSeqNum;
Wonsik Kim469c8342019-04-11 16:46:09 -0700885 break;
886 }
887 }
888 if (memory == nullptr) {
889 return nullptr;
890 }
891
892 std::shared_ptr<C2LinearBlock> block;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700893 c2_status_t err = pool->fetchLinearBlock(capacity, usage, &block);
Wonsik Kim469c8342019-04-11 16:46:09 -0700894 if (err != C2_OK || block == nullptr) {
895 return nullptr;
896 }
897
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700898 memoryVector->at(slot).block = block;
899 return new EncryptedLinearBlockBuffer(format, block, memory, heapSeqNum);
900}
901
902sp<Codec2Buffer> EncryptedLinearInputBuffers::createNewBuffer() {
903 // TODO: android_2020
904 return nullptr;
Wonsik Kim469c8342019-04-11 16:46:09 -0700905}
906
907// GraphicMetadataInputBuffers
908
909GraphicMetadataInputBuffers::GraphicMetadataInputBuffers(
910 const char *componentName, const char *name)
911 : InputBuffers(componentName, name),
912 mImpl(mName),
913 mStore(GetCodec2PlatformAllocatorStore()) { }
914
915bool GraphicMetadataInputBuffers::requestNewBuffer(
916 size_t *index, sp<MediaCodecBuffer> *buffer) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700917 sp<Codec2Buffer> newBuffer = createNewBuffer();
Wonsik Kim469c8342019-04-11 16:46:09 -0700918 if (newBuffer == nullptr) {
919 return false;
920 }
921 *index = mImpl.assignSlot(newBuffer);
922 *buffer = newBuffer;
923 return true;
924}
925
926bool GraphicMetadataInputBuffers::releaseBuffer(
927 const sp<MediaCodecBuffer> &buffer,
928 std::shared_ptr<C2Buffer> *c2buffer,
929 bool release) {
930 return mImpl.releaseSlot(buffer, c2buffer, release);
931}
932
933bool GraphicMetadataInputBuffers::expireComponentBuffer(
934 const std::shared_ptr<C2Buffer> &c2buffer) {
935 return mImpl.expireComponentBuffer(c2buffer);
936}
937
938void GraphicMetadataInputBuffers::flush() {
939 // This is no-op by default unless we're in array mode where we need to keep
940 // track of the flushed work.
941}
942
943std::unique_ptr<InputBuffers> GraphicMetadataInputBuffers::toArrayMode(
944 size_t size) {
945 std::shared_ptr<C2Allocator> alloc;
946 c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
947 if (err != C2_OK) {
948 return nullptr;
949 }
950 std::unique_ptr<InputBuffersArray> array(
951 new InputBuffersArray(mComponentName.c_str(), "2D-MetaInput[N]"));
952 array->setPool(mPool);
953 array->setFormat(mFormat);
954 array->initialize(
955 mImpl,
956 size,
957 [format = mFormat, alloc]() -> sp<Codec2Buffer> {
958 return new GraphicMetadataBuffer(format, alloc);
959 });
960 return std::move(array);
961}
962
963size_t GraphicMetadataInputBuffers::numClientBuffers() const {
964 return mImpl.numClientBuffers();
965}
966
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700967sp<Codec2Buffer> GraphicMetadataInputBuffers::createNewBuffer() {
968 std::shared_ptr<C2Allocator> alloc;
969 c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
970 if (err != C2_OK) {
971 return nullptr;
972 }
973 return new GraphicMetadataBuffer(mFormat, alloc);
974}
975
Wonsik Kim469c8342019-04-11 16:46:09 -0700976// GraphicInputBuffers
977
978GraphicInputBuffers::GraphicInputBuffers(
Wonsik Kim41d83432020-04-27 16:40:49 -0700979 const char *componentName, const char *name)
Wonsik Kim469c8342019-04-11 16:46:09 -0700980 : InputBuffers(componentName, name),
981 mImpl(mName),
Wonsik Kim41d83432020-04-27 16:40:49 -0700982 mLocalBufferPool(LocalBufferPool::Create()) { }
Wonsik Kim469c8342019-04-11 16:46:09 -0700983
984bool GraphicInputBuffers::requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700985 sp<Codec2Buffer> newBuffer = createNewBuffer();
Wonsik Kim469c8342019-04-11 16:46:09 -0700986 if (newBuffer == nullptr) {
987 return false;
988 }
989 *index = mImpl.assignSlot(newBuffer);
990 handleImageData(newBuffer);
991 *buffer = newBuffer;
992 return true;
993}
994
995bool GraphicInputBuffers::releaseBuffer(
996 const sp<MediaCodecBuffer> &buffer,
997 std::shared_ptr<C2Buffer> *c2buffer,
998 bool release) {
999 return mImpl.releaseSlot(buffer, c2buffer, release);
1000}
1001
1002bool GraphicInputBuffers::expireComponentBuffer(
1003 const std::shared_ptr<C2Buffer> &c2buffer) {
1004 return mImpl.expireComponentBuffer(c2buffer);
1005}
1006
1007void GraphicInputBuffers::flush() {
1008 // This is no-op by default unless we're in array mode where we need to keep
1009 // track of the flushed work.
1010}
1011
1012std::unique_ptr<InputBuffers> GraphicInputBuffers::toArrayMode(size_t size) {
1013 std::unique_ptr<InputBuffersArray> array(
1014 new InputBuffersArray(mComponentName.c_str(), "2D-BB-Input[N]"));
1015 array->setPool(mPool);
1016 array->setFormat(mFormat);
1017 array->initialize(
1018 mImpl,
1019 size,
1020 [pool = mPool, format = mFormat, lbp = mLocalBufferPool]() -> sp<Codec2Buffer> {
1021 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
1022 return AllocateGraphicBuffer(
1023 pool, format, HAL_PIXEL_FORMAT_YV12, usage, lbp);
1024 });
1025 return std::move(array);
1026}
1027
1028size_t GraphicInputBuffers::numClientBuffers() const {
1029 return mImpl.numClientBuffers();
1030}
1031
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001032sp<Codec2Buffer> GraphicInputBuffers::createNewBuffer() {
1033 // TODO: read usage from intf
1034 C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
1035 return AllocateGraphicBuffer(
1036 mPool, mFormat, HAL_PIXEL_FORMAT_YV12, usage, mLocalBufferPool);
1037}
1038
Wonsik Kim469c8342019-04-11 16:46:09 -07001039// OutputBuffersArray
1040
1041void OutputBuffersArray::initialize(
1042 const FlexBuffersImpl &impl,
1043 size_t minSize,
1044 std::function<sp<Codec2Buffer>()> allocate) {
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001045 mAlloc = allocate;
Wonsik Kim469c8342019-04-11 16:46:09 -07001046 mImpl.initialize(impl, minSize, allocate);
1047}
1048
1049status_t OutputBuffersArray::registerBuffer(
1050 const std::shared_ptr<C2Buffer> &buffer,
1051 size_t *index,
1052 sp<MediaCodecBuffer> *clientBuffer) {
1053 sp<Codec2Buffer> c2Buffer;
1054 status_t err = mImpl.grabBuffer(
1055 index,
1056 &c2Buffer,
1057 [buffer](const sp<Codec2Buffer> &clientBuffer) {
1058 return clientBuffer->canCopy(buffer);
1059 });
1060 if (err == WOULD_BLOCK) {
1061 ALOGV("[%s] buffers temporarily not available", mName);
1062 return err;
1063 } else if (err != OK) {
1064 ALOGD("[%s] grabBuffer failed: %d", mName, err);
1065 return err;
1066 }
1067 c2Buffer->setFormat(mFormat);
1068 if (!c2Buffer->copy(buffer)) {
1069 ALOGD("[%s] copy buffer failed", mName);
1070 return WOULD_BLOCK;
1071 }
1072 submit(c2Buffer);
1073 handleImageData(c2Buffer);
1074 *clientBuffer = c2Buffer;
1075 ALOGV("[%s] grabbed buffer %zu", mName, *index);
1076 return OK;
1077}
1078
1079status_t OutputBuffersArray::registerCsd(
1080 const C2StreamInitDataInfo::output *csd,
1081 size_t *index,
1082 sp<MediaCodecBuffer> *clientBuffer) {
1083 sp<Codec2Buffer> c2Buffer;
1084 status_t err = mImpl.grabBuffer(
1085 index,
1086 &c2Buffer,
1087 [csd](const sp<Codec2Buffer> &clientBuffer) {
1088 return clientBuffer->base() != nullptr
1089 && clientBuffer->capacity() >= csd->flexCount();
1090 });
1091 if (err != OK) {
1092 return err;
1093 }
1094 memcpy(c2Buffer->base(), csd->m.value, csd->flexCount());
1095 c2Buffer->setRange(0, csd->flexCount());
1096 c2Buffer->setFormat(mFormat);
1097 *clientBuffer = c2Buffer;
1098 return OK;
1099}
1100
1101bool OutputBuffersArray::releaseBuffer(
1102 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) {
1103 return mImpl.returnBuffer(buffer, c2buffer, true);
1104}
1105
1106void OutputBuffersArray::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1107 (void)flushedWork;
1108 mImpl.flush();
1109 if (mSkipCutBuffer != nullptr) {
1110 mSkipCutBuffer->clear();
1111 }
1112}
1113
1114void OutputBuffersArray::getArray(Vector<sp<MediaCodecBuffer>> *array) const {
1115 mImpl.getArray(array);
1116}
1117
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001118size_t OutputBuffersArray::numClientBuffers() const {
1119 return mImpl.numClientBuffers();
1120}
1121
Wonsik Kim469c8342019-04-11 16:46:09 -07001122void OutputBuffersArray::realloc(const std::shared_ptr<C2Buffer> &c2buffer) {
Wonsik Kim469c8342019-04-11 16:46:09 -07001123 switch (c2buffer->data().type()) {
1124 case C2BufferData::LINEAR: {
1125 uint32_t size = kLinearBufferSize;
Nick Desaulniersd09eaea2019-10-07 20:19:39 -07001126 const std::vector<C2ConstLinearBlock> &linear_blocks = c2buffer->data().linearBlocks();
1127 const uint32_t block_size = linear_blocks.front().size();
1128 if (block_size < kMaxLinearBufferSize / 2) {
1129 size = block_size * 2;
Wonsik Kim469c8342019-04-11 16:46:09 -07001130 } else {
1131 size = kMaxLinearBufferSize;
1132 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001133 mAlloc = [format = mFormat, size] {
Wonsik Kim469c8342019-04-11 16:46:09 -07001134 return new LocalLinearBuffer(format, new ABuffer(size));
1135 };
Wonsik Kima39882b2019-06-20 16:13:56 -07001136 ALOGD("[%s] reallocating with linear buffer of size %u", mName, size);
Wonsik Kim469c8342019-04-11 16:46:09 -07001137 break;
1138 }
1139
Wonsik Kima39882b2019-06-20 16:13:56 -07001140 case C2BufferData::GRAPHIC: {
1141 // This is only called for RawGraphicOutputBuffers.
1142 mAlloc = [format = mFormat,
Wonsik Kim41d83432020-04-27 16:40:49 -07001143 lbp = LocalBufferPool::Create()] {
Wonsik Kima39882b2019-06-20 16:13:56 -07001144 return ConstGraphicBlockBuffer::AllocateEmpty(
1145 format,
1146 [lbp](size_t capacity) {
1147 return lbp->newBuffer(capacity);
1148 });
1149 };
1150 ALOGD("[%s] reallocating with graphic buffer: format = %s",
1151 mName, mFormat->debugString().c_str());
1152 break;
1153 }
Wonsik Kim469c8342019-04-11 16:46:09 -07001154
1155 case C2BufferData::INVALID: [[fallthrough]];
1156 case C2BufferData::LINEAR_CHUNKS: [[fallthrough]];
1157 case C2BufferData::GRAPHIC_CHUNKS: [[fallthrough]];
1158 default:
1159 ALOGD("Unsupported type: %d", (int)c2buffer->data().type());
1160 return;
1161 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001162 mImpl.realloc(mAlloc);
Wonsik Kim469c8342019-04-11 16:46:09 -07001163}
1164
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001165void OutputBuffersArray::grow(size_t newSize) {
1166 mImpl.grow(newSize, mAlloc);
Wonsik Kim469c8342019-04-11 16:46:09 -07001167}
1168
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001169void OutputBuffersArray::transferFrom(OutputBuffers* source) {
1170 mFormat = source->mFormat;
1171 mSkipCutBuffer = source->mSkipCutBuffer;
1172 mUnreportedFormat = source->mUnreportedFormat;
1173 mPending = std::move(source->mPending);
1174 mReorderStash = std::move(source->mReorderStash);
1175 mDepth = source->mDepth;
1176 mKey = source->mKey;
1177}
1178
Wonsik Kim469c8342019-04-11 16:46:09 -07001179// FlexOutputBuffers
1180
1181status_t FlexOutputBuffers::registerBuffer(
1182 const std::shared_ptr<C2Buffer> &buffer,
1183 size_t *index,
1184 sp<MediaCodecBuffer> *clientBuffer) {
1185 sp<Codec2Buffer> newBuffer = wrap(buffer);
1186 if (newBuffer == nullptr) {
1187 return NO_MEMORY;
1188 }
1189 newBuffer->setFormat(mFormat);
1190 *index = mImpl.assignSlot(newBuffer);
1191 handleImageData(newBuffer);
1192 *clientBuffer = newBuffer;
1193 ALOGV("[%s] registered buffer %zu", mName, *index);
1194 return OK;
1195}
1196
1197status_t FlexOutputBuffers::registerCsd(
1198 const C2StreamInitDataInfo::output *csd,
1199 size_t *index,
1200 sp<MediaCodecBuffer> *clientBuffer) {
1201 sp<Codec2Buffer> newBuffer = new LocalLinearBuffer(
1202 mFormat, ABuffer::CreateAsCopy(csd->m.value, csd->flexCount()));
1203 *index = mImpl.assignSlot(newBuffer);
1204 *clientBuffer = newBuffer;
1205 return OK;
1206}
1207
1208bool FlexOutputBuffers::releaseBuffer(
1209 const sp<MediaCodecBuffer> &buffer,
1210 std::shared_ptr<C2Buffer> *c2buffer) {
1211 return mImpl.releaseSlot(buffer, c2buffer, true);
1212}
1213
1214void FlexOutputBuffers::flush(
1215 const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1216 (void) flushedWork;
1217 // This is no-op by default unless we're in array mode where we need to keep
1218 // track of the flushed work.
1219}
1220
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001221std::unique_ptr<OutputBuffersArray> FlexOutputBuffers::toArrayMode(size_t size) {
Wonsik Kim469c8342019-04-11 16:46:09 -07001222 std::unique_ptr<OutputBuffersArray> array(new OutputBuffersArray(mComponentName.c_str()));
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001223 array->transferFrom(this);
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001224 std::function<sp<Codec2Buffer>()> alloc = getAlloc();
1225 array->initialize(mImpl, size, alloc);
Pawin Vongmasa9b906982020-04-11 05:07:15 -07001226 return array;
Wonsik Kim469c8342019-04-11 16:46:09 -07001227}
1228
1229size_t FlexOutputBuffers::numClientBuffers() const {
1230 return mImpl.numClientBuffers();
1231}
1232
1233// LinearOutputBuffers
1234
1235void LinearOutputBuffers::flush(
1236 const std::list<std::unique_ptr<C2Work>> &flushedWork) {
1237 if (mSkipCutBuffer != nullptr) {
1238 mSkipCutBuffer->clear();
1239 }
1240 FlexOutputBuffers::flush(flushedWork);
1241}
1242
1243sp<Codec2Buffer> LinearOutputBuffers::wrap(const std::shared_ptr<C2Buffer> &buffer) {
1244 if (buffer == nullptr) {
1245 ALOGV("[%s] using a dummy buffer", mName);
1246 return new LocalLinearBuffer(mFormat, new ABuffer(0));
1247 }
1248 if (buffer->data().type() != C2BufferData::LINEAR) {
1249 ALOGV("[%s] non-linear buffer %d", mName, buffer->data().type());
1250 // We expect linear output buffers from the component.
1251 return nullptr;
1252 }
1253 if (buffer->data().linearBlocks().size() != 1u) {
1254 ALOGV("[%s] no linear buffers", mName);
1255 // We expect one and only one linear block from the component.
1256 return nullptr;
1257 }
1258 sp<Codec2Buffer> clientBuffer = ConstLinearBlockBuffer::Allocate(mFormat, buffer);
1259 if (clientBuffer == nullptr) {
1260 ALOGD("[%s] ConstLinearBlockBuffer::Allocate failed", mName);
1261 return nullptr;
1262 }
1263 submit(clientBuffer);
1264 return clientBuffer;
1265}
1266
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001267std::function<sp<Codec2Buffer>()> LinearOutputBuffers::getAlloc() {
1268 return [format = mFormat]{
1269 // TODO: proper max output size
1270 return new LocalLinearBuffer(format, new ABuffer(kLinearBufferSize));
1271 };
Wonsik Kim469c8342019-04-11 16:46:09 -07001272}
1273
1274// GraphicOutputBuffers
1275
1276sp<Codec2Buffer> GraphicOutputBuffers::wrap(const std::shared_ptr<C2Buffer> &buffer) {
1277 return new DummyContainerBuffer(mFormat, buffer);
1278}
1279
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001280std::function<sp<Codec2Buffer>()> GraphicOutputBuffers::getAlloc() {
1281 return [format = mFormat]{
1282 return new DummyContainerBuffer(format);
1283 };
Wonsik Kim469c8342019-04-11 16:46:09 -07001284}
1285
1286// RawGraphicOutputBuffers
1287
1288RawGraphicOutputBuffers::RawGraphicOutputBuffers(
Wonsik Kim41d83432020-04-27 16:40:49 -07001289 const char *componentName, const char *name)
Wonsik Kim469c8342019-04-11 16:46:09 -07001290 : FlexOutputBuffers(componentName, name),
Wonsik Kim41d83432020-04-27 16:40:49 -07001291 mLocalBufferPool(LocalBufferPool::Create()) { }
Wonsik Kim469c8342019-04-11 16:46:09 -07001292
1293sp<Codec2Buffer> RawGraphicOutputBuffers::wrap(const std::shared_ptr<C2Buffer> &buffer) {
1294 if (buffer == nullptr) {
1295 sp<Codec2Buffer> c2buffer = ConstGraphicBlockBuffer::AllocateEmpty(
1296 mFormat,
1297 [lbp = mLocalBufferPool](size_t capacity) {
1298 return lbp->newBuffer(capacity);
1299 });
1300 if (c2buffer == nullptr) {
1301 ALOGD("[%s] ConstGraphicBlockBuffer::AllocateEmpty failed", mName);
1302 return nullptr;
1303 }
1304 c2buffer->setRange(0, 0);
1305 return c2buffer;
1306 } else {
1307 return ConstGraphicBlockBuffer::Allocate(
1308 mFormat,
1309 buffer,
1310 [lbp = mLocalBufferPool](size_t capacity) {
1311 return lbp->newBuffer(capacity);
1312 });
1313 }
1314}
1315
Wonsik Kim5ecf3832019-04-18 10:28:58 -07001316std::function<sp<Codec2Buffer>()> RawGraphicOutputBuffers::getAlloc() {
1317 return [format = mFormat, lbp = mLocalBufferPool]{
1318 return ConstGraphicBlockBuffer::AllocateEmpty(
1319 format,
1320 [lbp](size_t capacity) {
1321 return lbp->newBuffer(capacity);
1322 });
1323 };
Wonsik Kim469c8342019-04-11 16:46:09 -07001324}
1325
1326} // namespace android