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