blob: 461fbd5a941ed8afc7ff981dfeda0f700dccecdc [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#ifndef CCODEC_BUFFERS_H_
18
19#define CCODEC_BUFFERS_H_
20
21#include <string>
22
23#include <C2Config.h>
24#include <media/stagefright/foundation/AMessage.h>
25#include <media/MediaCodecBuffer.h>
26
27#include "Codec2Buffer.h"
28#include "SkipCutBuffer.h"
29
30namespace android {
31
32constexpr size_t kLinearBufferSize = 1048576;
33// This can fit 4K RGBA frame, and most likely client won't need more than this.
34constexpr size_t kMaxLinearBufferSize = 3840 * 2160 * 4;
35
36/**
37 * Base class for representation of buffers at one port.
38 */
39class CCodecBuffers {
40public:
41 CCodecBuffers(const char *componentName, const char *name = "Buffers")
42 : mComponentName(componentName),
43 mChannelName(std::string(componentName) + ":" + name),
44 mName(mChannelName.c_str()) {
45 }
46 virtual ~CCodecBuffers() = default;
47
48 /**
49 * Set format for MediaCodec-facing buffers.
50 */
51 void setFormat(const sp<AMessage> &format);
52
53 /**
54 * Return a copy of current format.
55 */
56 sp<AMessage> dupFormat();
57
58 /**
59 * Returns true if the buffers are operating under array mode.
60 */
61 virtual bool isArrayMode() const { return false; }
62
63 /**
64 * Fills the vector with MediaCodecBuffer's if in array mode; otherwise,
65 * no-op.
66 */
67 virtual void getArray(Vector<sp<MediaCodecBuffer>> *) const {}
68
69 /**
70 * Return number of buffers the client owns.
71 */
72 virtual size_t numClientBuffers() const = 0;
73
74 /**
75 * Examine image data from the buffer and update the format if necessary.
76 */
77 void handleImageData(const sp<Codec2Buffer> &buffer);
78
79protected:
80 std::string mComponentName; ///< name of component for debugging
81 std::string mChannelName; ///< name of channel for debugging
82 const char *mName; ///< C-string version of channel name
83 // Format to be used for creating MediaCodec-facing buffers.
84 sp<AMessage> mFormat;
85
86private:
87 DISALLOW_EVIL_CONSTRUCTORS(CCodecBuffers);
88};
89
90class InputBuffers : public CCodecBuffers {
91public:
92 InputBuffers(const char *componentName, const char *name = "Input[]")
93 : CCodecBuffers(componentName, name) { }
94 virtual ~InputBuffers() = default;
95
96 /**
97 * Set a block pool to obtain input memory blocks.
98 */
99 void setPool(const std::shared_ptr<C2BlockPool> &pool) { mPool = pool; }
100
101 /**
102 * Get a new MediaCodecBuffer for input and its corresponding index.
103 * Returns false if no new buffer can be obtained at the moment.
104 */
105 virtual bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) = 0;
106
107 /**
108 * Release the buffer obtained from requestNewBuffer() and get the
109 * associated C2Buffer object back. Returns true if the buffer was on file
110 * and released successfully.
111 */
112 virtual bool releaseBuffer(
113 const sp<MediaCodecBuffer> &buffer,
114 std::shared_ptr<C2Buffer> *c2buffer,
115 bool release) = 0;
116
117 /**
118 * Release the buffer that is no longer used by the codec process. Return
119 * true if and only if the buffer was on file and released successfully.
120 */
121 virtual bool expireComponentBuffer(
122 const std::shared_ptr<C2Buffer> &c2buffer) = 0;
123
124 /**
125 * Flush internal state. After this call, no index or buffer previously
126 * returned from requestNewBuffer() is valid.
127 */
128 virtual void flush() = 0;
129
130 /**
131 * Return array-backed version of input buffers. The returned object
132 * shall retain the internal state so that it will honor index and
133 * buffer from previous calls of requestNewBuffer().
134 */
135 virtual std::unique_ptr<InputBuffers> toArrayMode(size_t size) = 0;
136
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700137 /**
138 * Release the buffer obtained from requestNewBuffer(), and create a deep
139 * copy clone of the buffer.
140 *
141 * \return the deep copy clone of the buffer; nullptr if cloning is not
142 * possible.
143 */
144 sp<Codec2Buffer> cloneAndReleaseBuffer(const sp<MediaCodecBuffer> &buffer);
145
Wonsik Kim469c8342019-04-11 16:46:09 -0700146protected:
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700147 virtual sp<Codec2Buffer> createNewBuffer() = 0;
148
Wonsik Kim469c8342019-04-11 16:46:09 -0700149 // Pool to obtain blocks for input buffers.
150 std::shared_ptr<C2BlockPool> mPool;
151
152private:
153 DISALLOW_EVIL_CONSTRUCTORS(InputBuffers);
154};
155
156class OutputBuffers : public CCodecBuffers {
157public:
158 OutputBuffers(const char *componentName, const char *name = "Output")
159 : CCodecBuffers(componentName, name) { }
160 virtual ~OutputBuffers() = default;
161
162 /**
163 * Register output C2Buffer from the component and obtain corresponding
164 * index and MediaCodecBuffer object. Returns false if registration
165 * fails.
166 */
167 virtual status_t registerBuffer(
168 const std::shared_ptr<C2Buffer> &buffer,
169 size_t *index,
170 sp<MediaCodecBuffer> *clientBuffer) = 0;
171
172 /**
173 * Register codec specific data as a buffer to be consistent with
174 * MediaCodec behavior.
175 */
176 virtual status_t registerCsd(
177 const C2StreamInitDataInfo::output * /* csd */,
178 size_t * /* index */,
179 sp<MediaCodecBuffer> * /* clientBuffer */) = 0;
180
181 /**
182 * Release the buffer obtained from registerBuffer() and get the
183 * associated C2Buffer object back. Returns true if the buffer was on file
184 * and released successfully.
185 */
186 virtual bool releaseBuffer(
187 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) = 0;
188
189 /**
190 * Flush internal state. After this call, no index or buffer previously
191 * returned from registerBuffer() is valid.
192 */
193 virtual void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) = 0;
194
195 /**
196 * Return array-backed version of output buffers. The returned object
197 * shall retain the internal state so that it will honor index and
198 * buffer from previous calls of registerBuffer().
199 */
200 virtual std::unique_ptr<OutputBuffers> toArrayMode(size_t size) = 0;
201
202 /**
203 * Initialize SkipCutBuffer object.
204 */
205 void initSkipCutBuffer(
206 int32_t delay, int32_t padding, int32_t sampleRate, int32_t channelCount);
207
208 /**
209 * Update the SkipCutBuffer object. No-op if it's never initialized.
210 */
211 void updateSkipCutBuffer(int32_t sampleRate, int32_t channelCount);
212
213 /**
214 * Submit buffer to SkipCutBuffer object, if initialized.
215 */
216 void submit(const sp<MediaCodecBuffer> &buffer);
217
218 /**
219 * Transfer SkipCutBuffer object to the other Buffers object.
220 */
221 void transferSkipCutBuffer(const sp<SkipCutBuffer> &scb);
222
223protected:
224 sp<SkipCutBuffer> mSkipCutBuffer;
225
226private:
227 int32_t mDelay;
228 int32_t mPadding;
229 int32_t mSampleRate;
230
231 void setSkipCutBuffer(int32_t skip, int32_t cut, int32_t channelCount);
232
233 DISALLOW_EVIL_CONSTRUCTORS(OutputBuffers);
234};
235
236/**
237 * Simple local buffer pool backed by std::vector.
238 */
239class LocalBufferPool : public std::enable_shared_from_this<LocalBufferPool> {
240public:
241 /**
242 * Create a new LocalBufferPool object.
243 *
244 * \param poolCapacity max total size of buffers managed by this pool.
245 *
246 * \return a newly created pool object.
247 */
248 static std::shared_ptr<LocalBufferPool> Create(size_t poolCapacity);
249
250 /**
251 * Return an ABuffer object whose size is at least |capacity|.
252 *
253 * \param capacity requested capacity
254 * \return nullptr if the pool capacity is reached
255 * an ABuffer object otherwise.
256 */
257 sp<ABuffer> newBuffer(size_t capacity);
258
259private:
260 /**
261 * ABuffer backed by std::vector.
262 */
263 class VectorBuffer : public ::android::ABuffer {
264 public:
265 /**
266 * Construct a VectorBuffer by taking the ownership of supplied vector.
267 *
268 * \param vec backing vector of the buffer. this object takes
269 * ownership at construction.
270 * \param pool a LocalBufferPool object to return the vector at
271 * destruction.
272 */
273 VectorBuffer(std::vector<uint8_t> &&vec, const std::shared_ptr<LocalBufferPool> &pool);
274
275 ~VectorBuffer() override;
276
277 private:
278 std::vector<uint8_t> mVec;
279 std::weak_ptr<LocalBufferPool> mPool;
280 };
281
282 Mutex mMutex;
283 size_t mPoolCapacity;
284 size_t mUsedSize;
285 std::list<std::vector<uint8_t>> mPool;
286
287 /**
288 * Private constructor to prevent constructing non-managed LocalBufferPool.
289 */
290 explicit LocalBufferPool(size_t poolCapacity)
291 : mPoolCapacity(poolCapacity), mUsedSize(0) {
292 }
293
294 /**
295 * Take back the ownership of vec from the destructed VectorBuffer and put
296 * it in front of the pool.
297 */
298 void returnVector(std::vector<uint8_t> &&vec);
299
300 DISALLOW_EVIL_CONSTRUCTORS(LocalBufferPool);
301};
302
303class BuffersArrayImpl;
304
305/**
306 * Flexible buffer slots implementation.
307 */
308class FlexBuffersImpl {
309public:
310 FlexBuffersImpl(const char *name)
311 : mImplName(std::string(name) + ".Impl"),
312 mName(mImplName.c_str()) { }
313
314 /**
315 * Assign an empty slot for a buffer and return the index. If there's no
316 * empty slot, just add one at the end and return it.
317 *
318 * \param buffer[in] a new buffer to assign a slot.
319 * \return index of the assigned slot.
320 */
321 size_t assignSlot(const sp<Codec2Buffer> &buffer);
322
323 /**
324 * Release the slot from the client, and get the C2Buffer object back from
325 * the previously assigned buffer. Note that the slot is not completely free
326 * until the returned C2Buffer object is freed.
327 *
328 * \param buffer[in] the buffer previously assigned a slot.
329 * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored
330 * if null.
331 * \return true if the buffer is successfully released from a slot
332 * false otherwise
333 */
334 bool releaseSlot(
335 const sp<MediaCodecBuffer> &buffer,
336 std::shared_ptr<C2Buffer> *c2buffer,
337 bool release);
338
339 /**
340 * Expire the C2Buffer object in the slot.
341 *
342 * \param c2buffer[in] C2Buffer object which the component released.
343 * \return true if the buffer is found in one of the slots and
344 * successfully released
345 * false otherwise
346 */
347 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer);
348
349 /**
350 * The client abandoned all known buffers, so reclaim the ownership.
351 */
352 void flush();
353
354 /**
355 * Return the number of buffers that are sent to the client but not released
356 * yet.
357 */
358 size_t numClientBuffers() const;
359
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700360 /**
361 * Return the number of buffers that are sent to the component but not
362 * returned back yet.
363 */
364 size_t numComponentBuffers() const;
365
Wonsik Kim469c8342019-04-11 16:46:09 -0700366private:
367 friend class BuffersArrayImpl;
368
369 std::string mImplName; ///< name for debugging
370 const char *mName; ///< C-string version of name
371
372 struct Entry {
373 sp<Codec2Buffer> clientBuffer;
374 std::weak_ptr<C2Buffer> compBuffer;
375 };
376 std::vector<Entry> mBuffers;
377};
378
379/**
380 * Static buffer slots implementation based on a fixed-size array.
381 */
382class BuffersArrayImpl {
383public:
384 BuffersArrayImpl()
385 : mImplName("BuffersArrayImpl"),
386 mName(mImplName.c_str()) { }
387
388 /**
389 * Initialize buffer array from the original |impl|. The buffers known by
390 * the client is preserved, and the empty slots are populated so that the
391 * array size is at least |minSize|.
392 *
393 * \param impl[in] FlexBuffersImpl object used so far.
394 * \param minSize[in] minimum size of the buffer array.
395 * \param allocate[in] function to allocate a client buffer for an empty slot.
396 */
397 void initialize(
398 const FlexBuffersImpl &impl,
399 size_t minSize,
400 std::function<sp<Codec2Buffer>()> allocate);
401
402 /**
403 * Grab a buffer from the underlying array which matches the criteria.
404 *
405 * \param index[out] index of the slot.
406 * \param buffer[out] the matching buffer.
407 * \param match[in] a function to test whether the buffer matches the
408 * criteria or not.
409 * \return OK if successful,
410 * WOULD_BLOCK if slots are being used,
411 * NO_MEMORY if no slot matches the criteria, even though it's
412 * available
413 */
414 status_t grabBuffer(
415 size_t *index,
416 sp<Codec2Buffer> *buffer,
417 std::function<bool(const sp<Codec2Buffer> &)> match =
418 [](const sp<Codec2Buffer> &) { return true; });
419
420 /**
421 * Return the buffer from the client, and get the C2Buffer object back from
422 * the buffer. Note that the slot is not completely free until the returned
423 * C2Buffer object is freed.
424 *
425 * \param buffer[in] the buffer previously grabbed.
426 * \param c2buffer[in,out] pointer to C2Buffer to be populated. Ignored
427 * if null.
428 * \return true if the buffer is successfully returned
429 * false otherwise
430 */
431 bool returnBuffer(
432 const sp<MediaCodecBuffer> &buffer,
433 std::shared_ptr<C2Buffer> *c2buffer,
434 bool release);
435
436 /**
437 * Expire the C2Buffer object in the slot.
438 *
439 * \param c2buffer[in] C2Buffer object which the component released.
440 * \return true if the buffer is found in one of the slots and
441 * successfully released
442 * false otherwise
443 */
444 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer);
445
446 /**
447 * Populate |array| with the underlying buffer array.
448 *
449 * \param array[out] an array to be filled with the underlying buffer array.
450 */
451 void getArray(Vector<sp<MediaCodecBuffer>> *array) const;
452
453 /**
454 * The client abandoned all known buffers, so reclaim the ownership.
455 */
456 void flush();
457
458 /**
459 * Reallocate the array with the given allocation function.
460 *
461 * \param alloc[in] the allocation function for client buffers.
462 */
463 void realloc(std::function<sp<Codec2Buffer>()> alloc);
464
465 /**
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700466 * Grow the array to the new size. It is a programming error to supply
467 * smaller size as the new size.
468 *
469 * \param newSize[in] new size of the array.
470 * \param alloc[in] the alllocation function for client buffers to fill
471 * the new empty slots.
472 */
473 void grow(size_t newSize, std::function<sp<Codec2Buffer>()> alloc);
474
475 /**
Wonsik Kim469c8342019-04-11 16:46:09 -0700476 * Return the number of buffers that are sent to the client but not released
477 * yet.
478 */
479 size_t numClientBuffers() const;
480
481private:
482 std::string mImplName; ///< name for debugging
483 const char *mName; ///< C-string version of name
484
485 struct Entry {
486 const sp<Codec2Buffer> clientBuffer;
487 std::weak_ptr<C2Buffer> compBuffer;
488 bool ownedByClient;
489 };
490 std::vector<Entry> mBuffers;
491};
492
493class InputBuffersArray : public InputBuffers {
494public:
495 InputBuffersArray(const char *componentName, const char *name = "Input[N]")
496 : InputBuffers(componentName, name) { }
497 ~InputBuffersArray() override = default;
498
499 /**
500 * Initialize this object from the non-array state. We keep existing slots
501 * at the same index, and for empty slots we allocate client buffers with
502 * the given allocate function. If the number of slots is less than minSize,
503 * we fill the array to the minimum size.
504 *
505 * \param impl[in] existing non-array state
506 * \param minSize[in] minimum size of the array
507 * \param allocate[in] allocate function to fill empty slots
508 */
509 void initialize(
510 const FlexBuffersImpl &impl,
511 size_t minSize,
512 std::function<sp<Codec2Buffer>()> allocate);
513
514 bool isArrayMode() const final { return true; }
515
516 std::unique_ptr<InputBuffers> toArrayMode(size_t) final {
517 return nullptr;
518 }
519
520 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final;
521
522 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
523
524 bool releaseBuffer(
525 const sp<MediaCodecBuffer> &buffer,
526 std::shared_ptr<C2Buffer> *c2buffer,
527 bool release) override;
528
529 bool expireComponentBuffer(
530 const std::shared_ptr<C2Buffer> &c2buffer) override;
531
532 void flush() override;
533
534 size_t numClientBuffers() const final;
535
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700536protected:
537 sp<Codec2Buffer> createNewBuffer() override;
538
Wonsik Kim469c8342019-04-11 16:46:09 -0700539private:
540 BuffersArrayImpl mImpl;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700541 std::function<sp<Codec2Buffer>()> mAllocate;
Wonsik Kim469c8342019-04-11 16:46:09 -0700542};
543
544class LinearInputBuffers : public InputBuffers {
545public:
546 LinearInputBuffers(const char *componentName, const char *name = "1D-Input")
547 : InputBuffers(componentName, name),
548 mImpl(mName) { }
549 ~LinearInputBuffers() override = default;
550
551 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
552
553 bool releaseBuffer(
554 const sp<MediaCodecBuffer> &buffer,
555 std::shared_ptr<C2Buffer> *c2buffer,
556 bool release) override;
557
558 bool expireComponentBuffer(
559 const std::shared_ptr<C2Buffer> &c2buffer) override;
560
561 void flush() override;
562
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700563 std::unique_ptr<InputBuffers> toArrayMode(size_t size) override;
Wonsik Kim469c8342019-04-11 16:46:09 -0700564
565 size_t numClientBuffers() const final;
566
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700567protected:
568 sp<Codec2Buffer> createNewBuffer() override;
569
570 FlexBuffersImpl mImpl;
Wonsik Kim469c8342019-04-11 16:46:09 -0700571
572private:
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700573 static sp<Codec2Buffer> Alloc(
574 const std::shared_ptr<C2BlockPool> &pool, const sp<AMessage> &format);
Wonsik Kim469c8342019-04-11 16:46:09 -0700575};
576
577class EncryptedLinearInputBuffers : public LinearInputBuffers {
578public:
579 EncryptedLinearInputBuffers(
580 bool secure,
581 const sp<MemoryDealer> &dealer,
582 const sp<ICrypto> &crypto,
583 int32_t heapSeqNum,
584 size_t capacity,
585 size_t numInputSlots,
586 const char *componentName, const char *name = "EncryptedInput");
587
588 ~EncryptedLinearInputBuffers() override = default;
589
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700590 std::unique_ptr<InputBuffers> toArrayMode(size_t size) override;
591
592protected:
593 sp<Codec2Buffer> createNewBuffer() override;
Wonsik Kim469c8342019-04-11 16:46:09 -0700594
595private:
Wonsik Kim469c8342019-04-11 16:46:09 -0700596 struct Entry {
597 std::weak_ptr<C2LinearBlock> block;
598 sp<IMemory> memory;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700599 int32_t heapSeqNum;
Wonsik Kim469c8342019-04-11 16:46:09 -0700600 };
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700601
602 static sp<Codec2Buffer> Alloc(
603 const std::shared_ptr<C2BlockPool> &pool,
604 const sp<AMessage> &format,
605 C2MemoryUsage usage,
606 const std::shared_ptr<std::vector<Entry>> &memoryVector);
607
608 C2MemoryUsage mUsage;
609 sp<MemoryDealer> mDealer;
610 sp<ICrypto> mCrypto;
611 std::shared_ptr<std::vector<Entry>> mMemoryVector;
Wonsik Kim469c8342019-04-11 16:46:09 -0700612};
613
614class GraphicMetadataInputBuffers : public InputBuffers {
615public:
616 GraphicMetadataInputBuffers(const char *componentName, const char *name = "2D-MetaInput");
617 ~GraphicMetadataInputBuffers() override = default;
618
619 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
620
621 bool releaseBuffer(
622 const sp<MediaCodecBuffer> &buffer,
623 std::shared_ptr<C2Buffer> *c2buffer,
624 bool release) override;
625
626 bool expireComponentBuffer(
627 const std::shared_ptr<C2Buffer> &c2buffer) override;
628
629 void flush() override;
630
631 std::unique_ptr<InputBuffers> toArrayMode(size_t size) final;
632
633 size_t numClientBuffers() const final;
634
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700635protected:
636 sp<Codec2Buffer> createNewBuffer() override;
637
Wonsik Kim469c8342019-04-11 16:46:09 -0700638private:
639 FlexBuffersImpl mImpl;
640 std::shared_ptr<C2AllocatorStore> mStore;
641};
642
643class GraphicInputBuffers : public InputBuffers {
644public:
645 GraphicInputBuffers(
646 size_t numInputSlots, const char *componentName, const char *name = "2D-BB-Input");
647 ~GraphicInputBuffers() override = default;
648
649 bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override;
650
651 bool releaseBuffer(
652 const sp<MediaCodecBuffer> &buffer,
653 std::shared_ptr<C2Buffer> *c2buffer,
654 bool release) override;
655
656 bool expireComponentBuffer(
657 const std::shared_ptr<C2Buffer> &c2buffer) override;
658
659 void flush() override;
660
661 std::unique_ptr<InputBuffers> toArrayMode(
662 size_t size) final;
663
664 size_t numClientBuffers() const final;
665
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700666protected:
667 sp<Codec2Buffer> createNewBuffer() override;
668
Wonsik Kim469c8342019-04-11 16:46:09 -0700669private:
670 FlexBuffersImpl mImpl;
671 std::shared_ptr<LocalBufferPool> mLocalBufferPool;
672};
673
674class DummyInputBuffers : public InputBuffers {
675public:
676 DummyInputBuffers(const char *componentName, const char *name = "2D-Input")
677 : InputBuffers(componentName, name) { }
678 ~DummyInputBuffers() override = default;
679
680 bool requestNewBuffer(size_t *, sp<MediaCodecBuffer> *) override {
681 return false;
682 }
683
684 bool releaseBuffer(
685 const sp<MediaCodecBuffer> &, std::shared_ptr<C2Buffer> *, bool) override {
686 return false;
687 }
688
689 bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &) override {
690 return false;
691 }
692 void flush() override {
693 }
694
695 std::unique_ptr<InputBuffers> toArrayMode(size_t) final {
696 return nullptr;
697 }
698
699 bool isArrayMode() const final { return true; }
700
701 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
702 array->clear();
703 }
704
705 size_t numClientBuffers() const final {
706 return 0u;
707 }
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700708
709protected:
710 sp<Codec2Buffer> createNewBuffer() override {
711 return nullptr;
712 }
Wonsik Kim469c8342019-04-11 16:46:09 -0700713};
714
715class OutputBuffersArray : public OutputBuffers {
716public:
717 OutputBuffersArray(const char *componentName, const char *name = "Output[N]")
718 : OutputBuffers(componentName, name) { }
719 ~OutputBuffersArray() override = default;
720
721 /**
722 * Initialize this object from the non-array state. We keep existing slots
723 * at the same index, and for empty slots we allocate client buffers with
724 * the given allocate function. If the number of slots is less than minSize,
725 * we fill the array to the minimum size.
726 *
727 * \param impl[in] existing non-array state
728 * \param minSize[in] minimum size of the array
729 * \param allocate[in] allocate function to fill empty slots
730 */
731 void initialize(
732 const FlexBuffersImpl &impl,
733 size_t minSize,
734 std::function<sp<Codec2Buffer>()> allocate);
735
736 bool isArrayMode() const final { return true; }
737
738 std::unique_ptr<OutputBuffers> toArrayMode(size_t) final {
739 return nullptr;
740 }
741
742 status_t registerBuffer(
743 const std::shared_ptr<C2Buffer> &buffer,
744 size_t *index,
745 sp<MediaCodecBuffer> *clientBuffer) final;
746
747 status_t registerCsd(
748 const C2StreamInitDataInfo::output *csd,
749 size_t *index,
750 sp<MediaCodecBuffer> *clientBuffer) final;
751
752 bool releaseBuffer(
753 const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override;
754
755 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) override;
756
757 void getArray(Vector<sp<MediaCodecBuffer>> *array) const final;
758
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700759 size_t numClientBuffers() const final;
760
Wonsik Kim469c8342019-04-11 16:46:09 -0700761 /**
762 * Reallocate the array, filled with buffers with the same size as given
763 * buffer.
764 *
765 * \param c2buffer[in] the reference buffer
766 */
767 void realloc(const std::shared_ptr<C2Buffer> &c2buffer);
768
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700769 /**
770 * Grow the array to the new size. It is a programming error to supply
771 * smaller size as the new size.
772 *
773 * \param newSize[in] new size of the array.
774 */
775 void grow(size_t newSize);
Wonsik Kim469c8342019-04-11 16:46:09 -0700776
777private:
778 BuffersArrayImpl mImpl;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700779 std::function<sp<Codec2Buffer>()> mAlloc;
Wonsik Kim469c8342019-04-11 16:46:09 -0700780};
781
782class FlexOutputBuffers : public OutputBuffers {
783public:
784 FlexOutputBuffers(const char *componentName, const char *name = "Output[]")
785 : OutputBuffers(componentName, name),
786 mImpl(mName) { }
787
788 status_t registerBuffer(
789 const std::shared_ptr<C2Buffer> &buffer,
790 size_t *index,
791 sp<MediaCodecBuffer> *clientBuffer) override;
792
793 status_t registerCsd(
794 const C2StreamInitDataInfo::output *csd,
795 size_t *index,
796 sp<MediaCodecBuffer> *clientBuffer) final;
797
798 bool releaseBuffer(
799 const sp<MediaCodecBuffer> &buffer,
800 std::shared_ptr<C2Buffer> *c2buffer) override;
801
802 void flush(
803 const std::list<std::unique_ptr<C2Work>> &flushedWork) override;
804
805 std::unique_ptr<OutputBuffers> toArrayMode(size_t size) override;
806
807 size_t numClientBuffers() const final;
808
809 /**
810 * Return an appropriate Codec2Buffer object for the type of buffers.
811 *
812 * \param buffer C2Buffer object to wrap.
813 *
814 * \return appropriate Codec2Buffer object to wrap |buffer|.
815 */
816 virtual sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) = 0;
817
818 /**
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700819 * Return a function that allocates an appropriate Codec2Buffer object for
820 * the type of buffers, to be used as an empty array buffer. The function
821 * must not refer to this pointer, since it may be used after this object
822 * destructs.
Wonsik Kim469c8342019-04-11 16:46:09 -0700823 *
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700824 * \return a function that allocates appropriate Codec2Buffer object,
825 * which can copy() from C2Buffers.
Wonsik Kim469c8342019-04-11 16:46:09 -0700826 */
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700827 virtual std::function<sp<Codec2Buffer>()> getAlloc() = 0;
Wonsik Kim469c8342019-04-11 16:46:09 -0700828
829private:
830 FlexBuffersImpl mImpl;
831};
832
833class LinearOutputBuffers : public FlexOutputBuffers {
834public:
835 LinearOutputBuffers(const char *componentName, const char *name = "1D-Output")
836 : FlexOutputBuffers(componentName, name) { }
837
838 void flush(
839 const std::list<std::unique_ptr<C2Work>> &flushedWork) override;
840
841 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override;
842
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700843 std::function<sp<Codec2Buffer>()> getAlloc() override;
Wonsik Kim469c8342019-04-11 16:46:09 -0700844};
845
846class GraphicOutputBuffers : public FlexOutputBuffers {
847public:
848 GraphicOutputBuffers(const char *componentName, const char *name = "2D-Output")
849 : FlexOutputBuffers(componentName, name) { }
850
851 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override;
852
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700853 std::function<sp<Codec2Buffer>()> getAlloc() override;
Wonsik Kim469c8342019-04-11 16:46:09 -0700854};
855
856class RawGraphicOutputBuffers : public FlexOutputBuffers {
857public:
858 RawGraphicOutputBuffers(
859 size_t numOutputSlots, const char *componentName, const char *name = "2D-BB-Output");
860 ~RawGraphicOutputBuffers() override = default;
861
862 sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override;
863
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700864 std::function<sp<Codec2Buffer>()> getAlloc() override;
Wonsik Kim469c8342019-04-11 16:46:09 -0700865
866private:
867 std::shared_ptr<LocalBufferPool> mLocalBufferPool;
868};
869
870} // namespace android
871
872#endif // CCODEC_BUFFERS_H_