• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017, 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 "CCodecBufferChannel"
19 #include <utils/Log.h>
20 
21 #include <numeric>
22 
23 #include <C2AllocatorGralloc.h>
24 #include <C2PlatformSupport.h>
25 #include <C2BlockInternal.h>
26 #include <C2Config.h>
27 #include <C2Debug.h>
28 
29 #include <android/hardware/cas/native/1.0/IDescrambler.h>
30 #include <android-base/stringprintf.h>
31 #include <binder/MemoryDealer.h>
32 #include <gui/Surface.h>
33 #include <media/openmax/OMX_Core.h>
34 #include <media/stagefright/foundation/ABuffer.h>
35 #include <media/stagefright/foundation/ALookup.h>
36 #include <media/stagefright/foundation/AMessage.h>
37 #include <media/stagefright/foundation/AUtils.h>
38 #include <media/stagefright/foundation/hexdump.h>
39 #include <media/stagefright/MediaCodec.h>
40 #include <media/stagefright/MediaCodecConstants.h>
41 #include <media/MediaCodecBuffer.h>
42 #include <system/window.h>
43 
44 #include "CCodecBufferChannel.h"
45 #include "Codec2Buffer.h"
46 #include "SkipCutBuffer.h"
47 
48 namespace android {
49 
50 using android::base::StringPrintf;
51 using hardware::hidl_handle;
52 using hardware::hidl_string;
53 using hardware::hidl_vec;
54 using namespace hardware::cas::V1_0;
55 using namespace hardware::cas::native::V1_0;
56 
57 using CasStatus = hardware::cas::V1_0::Status;
58 
59 /**
60  * Base class for representation of buffers at one port.
61  */
62 class CCodecBufferChannel::Buffers {
63 public:
Buffers(const char * componentName,const char * name="Buffers")64     Buffers(const char *componentName, const char *name = "Buffers")
65         : mComponentName(componentName),
66           mChannelName(std::string(componentName) + ":" + name),
67           mName(mChannelName.c_str()) {
68     }
69     virtual ~Buffers() = default;
70 
71     /**
72      * Set format for MediaCodec-facing buffers.
73      */
setFormat(const sp<AMessage> & format)74     void setFormat(const sp<AMessage> &format) {
75         CHECK(format != nullptr);
76         mFormat = format;
77     }
78 
79     /**
80      * Return a copy of current format.
81      */
dupFormat()82     sp<AMessage> dupFormat() {
83         return mFormat != nullptr ? mFormat->dup() : nullptr;
84     }
85 
86     /**
87      * Returns true if the buffers are operating under array mode.
88      */
isArrayMode() const89     virtual bool isArrayMode() const { return false; }
90 
91     /**
92      * Fills the vector with MediaCodecBuffer's if in array mode; otherwise,
93      * no-op.
94      */
getArray(Vector<sp<MediaCodecBuffer>> *) const95     virtual void getArray(Vector<sp<MediaCodecBuffer>> *) const {}
96 
97 protected:
98     std::string mComponentName; ///< name of component for debugging
99     std::string mChannelName; ///< name of channel for debugging
100     const char *mName; ///< C-string version of channel name
101     // Format to be used for creating MediaCodec-facing buffers.
102     sp<AMessage> mFormat;
103 
104 private:
105     DISALLOW_EVIL_CONSTRUCTORS(Buffers);
106 };
107 
108 class CCodecBufferChannel::InputBuffers : public CCodecBufferChannel::Buffers {
109 public:
InputBuffers(const char * componentName,const char * name="Input[]")110     InputBuffers(const char *componentName, const char *name = "Input[]")
111         : Buffers(componentName, name) { }
112     virtual ~InputBuffers() = default;
113 
114     /**
115      * Set a block pool to obtain input memory blocks.
116      */
setPool(const std::shared_ptr<C2BlockPool> & pool)117     void setPool(const std::shared_ptr<C2BlockPool> &pool) { mPool = pool; }
118 
119     /**
120      * Get a new MediaCodecBuffer for input and its corresponding index.
121      * Returns false if no new buffer can be obtained at the moment.
122      */
123     virtual bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) = 0;
124 
125     /**
126      * Release the buffer obtained from requestNewBuffer() and get the
127      * associated C2Buffer object back. Returns true if the buffer was on file
128      * and released successfully.
129      */
130     virtual bool releaseBuffer(
131             const sp<MediaCodecBuffer> &buffer,
132             std::shared_ptr<C2Buffer> *c2buffer,
133             bool release) = 0;
134 
135     /**
136      * Release the buffer that is no longer used by the codec process. Return
137      * true if and only if the buffer was on file and released successfully.
138      */
139     virtual bool expireComponentBuffer(
140             const std::shared_ptr<C2Buffer> &c2buffer) = 0;
141 
142     /**
143      * Flush internal state. After this call, no index or buffer previously
144      * returned from requestNewBuffer() is valid.
145      */
146     virtual void flush() = 0;
147 
148     /**
149      * Return array-backed version of input buffers. The returned object
150      * shall retain the internal state so that it will honor index and
151      * buffer from previous calls of requestNewBuffer().
152      */
153     virtual std::unique_ptr<InputBuffers> toArrayMode(size_t size) = 0;
154 
155 protected:
156     // Pool to obtain blocks for input buffers.
157     std::shared_ptr<C2BlockPool> mPool;
158 
159 private:
160     DISALLOW_EVIL_CONSTRUCTORS(InputBuffers);
161 };
162 
163 class CCodecBufferChannel::OutputBuffers : public CCodecBufferChannel::Buffers {
164 public:
OutputBuffers(const char * componentName,const char * name="Output")165     OutputBuffers(const char *componentName, const char *name = "Output")
166         : Buffers(componentName, name) { }
167     virtual ~OutputBuffers() = default;
168 
169     /**
170      * Register output C2Buffer from the component and obtain corresponding
171      * index and MediaCodecBuffer object. Returns false if registration
172      * fails.
173      */
174     virtual status_t registerBuffer(
175             const std::shared_ptr<C2Buffer> &buffer,
176             size_t *index,
177             sp<MediaCodecBuffer> *clientBuffer) = 0;
178 
179     /**
180      * Register codec specific data as a buffer to be consistent with
181      * MediaCodec behavior.
182      */
183     virtual status_t registerCsd(
184             const C2StreamCsdInfo::output * /* csd */,
185             size_t * /* index */,
186             sp<MediaCodecBuffer> * /* clientBuffer */) = 0;
187 
188     /**
189      * Release the buffer obtained from registerBuffer() and get the
190      * associated C2Buffer object back. Returns true if the buffer was on file
191      * and released successfully.
192      */
193     virtual bool releaseBuffer(
194             const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) = 0;
195 
196     /**
197      * Flush internal state. After this call, no index or buffer previously
198      * returned from registerBuffer() is valid.
199      */
200     virtual void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) = 0;
201 
202     /**
203      * Return array-backed version of output buffers. The returned object
204      * shall retain the internal state so that it will honor index and
205      * buffer from previous calls of registerBuffer().
206      */
207     virtual std::unique_ptr<OutputBuffers> toArrayMode(size_t size) = 0;
208 
209     /**
210      * Initialize SkipCutBuffer object.
211      */
initSkipCutBuffer(int32_t delay,int32_t padding,int32_t sampleRate,int32_t channelCount)212     void initSkipCutBuffer(
213             int32_t delay, int32_t padding, int32_t sampleRate, int32_t channelCount) {
214         CHECK(mSkipCutBuffer == nullptr);
215         mDelay = delay;
216         mPadding = padding;
217         mSampleRate = sampleRate;
218         setSkipCutBuffer(delay, padding, channelCount);
219     }
220 
221     /**
222      * Update the SkipCutBuffer object. No-op if it's never initialized.
223      */
updateSkipCutBuffer(int32_t sampleRate,int32_t channelCount)224     void updateSkipCutBuffer(int32_t sampleRate, int32_t channelCount) {
225         if (mSkipCutBuffer == nullptr) {
226             return;
227         }
228         int32_t delay = mDelay;
229         int32_t padding = mPadding;
230         if (sampleRate != mSampleRate) {
231             delay = ((int64_t)delay * sampleRate) / mSampleRate;
232             padding = ((int64_t)padding * sampleRate) / mSampleRate;
233         }
234         setSkipCutBuffer(delay, padding, channelCount);
235     }
236 
237     /**
238      * Submit buffer to SkipCutBuffer object, if initialized.
239      */
submit(const sp<MediaCodecBuffer> & buffer)240     void submit(const sp<MediaCodecBuffer> &buffer) {
241         if (mSkipCutBuffer != nullptr) {
242             mSkipCutBuffer->submit(buffer);
243         }
244     }
245 
246     /**
247      * Transfer SkipCutBuffer object to the other Buffers object.
248      */
transferSkipCutBuffer(const sp<SkipCutBuffer> & scb)249     void transferSkipCutBuffer(const sp<SkipCutBuffer> &scb) {
250         mSkipCutBuffer = scb;
251     }
252 
253 protected:
254     sp<SkipCutBuffer> mSkipCutBuffer;
255 
256 private:
257     int32_t mDelay;
258     int32_t mPadding;
259     int32_t mSampleRate;
260 
setSkipCutBuffer(int32_t skip,int32_t cut,int32_t channelCount)261     void setSkipCutBuffer(int32_t skip, int32_t cut, int32_t channelCount) {
262         if (mSkipCutBuffer != nullptr) {
263             size_t prevSize = mSkipCutBuffer->size();
264             if (prevSize != 0u) {
265                 ALOGD("[%s] Replacing SkipCutBuffer holding %zu bytes", mName, prevSize);
266             }
267         }
268         mSkipCutBuffer = new SkipCutBuffer(skip, cut, channelCount);
269     }
270 
271     DISALLOW_EVIL_CONSTRUCTORS(OutputBuffers);
272 };
273 
274 namespace {
275 
276 // TODO: get this info from component
277 const static size_t kMinInputBufferArraySize = 4;
278 const static size_t kMaxPipelineCapacity = 18;
279 const static size_t kChannelOutputDelay = 0;
280 const static size_t kMinOutputBufferArraySize = kMaxPipelineCapacity +
281                                                 kChannelOutputDelay;
282 const static size_t kLinearBufferSize = 1048576;
283 // This can fit 4K RGBA frame, and most likely client won't need more than this.
284 const static size_t kMaxLinearBufferSize = 3840 * 2160 * 4;
285 
286 /**
287  * Simple local buffer pool backed by std::vector.
288  */
289 class LocalBufferPool : public std::enable_shared_from_this<LocalBufferPool> {
290 public:
291     /**
292      * Create a new LocalBufferPool object.
293      *
294      * \param poolCapacity  max total size of buffers managed by this pool.
295      *
296      * \return  a newly created pool object.
297      */
Create(size_t poolCapacity)298     static std::shared_ptr<LocalBufferPool> Create(size_t poolCapacity) {
299         return std::shared_ptr<LocalBufferPool>(new LocalBufferPool(poolCapacity));
300     }
301 
302     /**
303      * Return an ABuffer object whose size is at least |capacity|.
304      *
305      * \param   capacity  requested capacity
306      * \return  nullptr if the pool capacity is reached
307      *          an ABuffer object otherwise.
308      */
newBuffer(size_t capacity)309     sp<ABuffer> newBuffer(size_t capacity) {
310         Mutex::Autolock lock(mMutex);
311         auto it = std::find_if(
312                 mPool.begin(), mPool.end(),
313                 [capacity](const std::vector<uint8_t> &vec) {
314                     return vec.capacity() >= capacity;
315                 });
316         if (it != mPool.end()) {
317             sp<ABuffer> buffer = new VectorBuffer(std::move(*it), shared_from_this());
318             mPool.erase(it);
319             return buffer;
320         }
321         if (mUsedSize + capacity > mPoolCapacity) {
322             while (!mPool.empty()) {
323                 mUsedSize -= mPool.back().capacity();
324                 mPool.pop_back();
325             }
326             if (mUsedSize + capacity > mPoolCapacity) {
327                 ALOGD("mUsedSize = %zu, capacity = %zu, mPoolCapacity = %zu",
328                         mUsedSize, capacity, mPoolCapacity);
329                 return nullptr;
330             }
331         }
332         std::vector<uint8_t> vec(capacity);
333         mUsedSize += vec.capacity();
334         return new VectorBuffer(std::move(vec), shared_from_this());
335     }
336 
337 private:
338     /**
339      * ABuffer backed by std::vector.
340      */
341     class VectorBuffer : public ::android::ABuffer {
342     public:
343         /**
344          * Construct a VectorBuffer by taking the ownership of supplied vector.
345          *
346          * \param vec   backing vector of the buffer. this object takes
347          *              ownership at construction.
348          * \param pool  a LocalBufferPool object to return the vector at
349          *              destruction.
350          */
VectorBuffer(std::vector<uint8_t> && vec,const std::shared_ptr<LocalBufferPool> & pool)351         VectorBuffer(std::vector<uint8_t> &&vec, const std::shared_ptr<LocalBufferPool> &pool)
352             : ABuffer(vec.data(), vec.capacity()),
353               mVec(std::move(vec)),
354               mPool(pool) {
355         }
356 
~VectorBuffer()357         ~VectorBuffer() override {
358             std::shared_ptr<LocalBufferPool> pool = mPool.lock();
359             if (pool) {
360                 // If pool is alive, return the vector back to the pool so that
361                 // it can be recycled.
362                 pool->returnVector(std::move(mVec));
363             }
364         }
365 
366     private:
367         std::vector<uint8_t> mVec;
368         std::weak_ptr<LocalBufferPool> mPool;
369     };
370 
371     Mutex mMutex;
372     size_t mPoolCapacity;
373     size_t mUsedSize;
374     std::list<std::vector<uint8_t>> mPool;
375 
376     /**
377      * Private constructor to prevent constructing non-managed LocalBufferPool.
378      */
LocalBufferPool(size_t poolCapacity)379     explicit LocalBufferPool(size_t poolCapacity)
380         : mPoolCapacity(poolCapacity), mUsedSize(0) {
381     }
382 
383     /**
384      * Take back the ownership of vec from the destructed VectorBuffer and put
385      * it in front of the pool.
386      */
returnVector(std::vector<uint8_t> && vec)387     void returnVector(std::vector<uint8_t> &&vec) {
388         Mutex::Autolock lock(mMutex);
389         mPool.push_front(std::move(vec));
390     }
391 
392     DISALLOW_EVIL_CONSTRUCTORS(LocalBufferPool);
393 };
394 
AllocateGraphicBuffer(const std::shared_ptr<C2BlockPool> & pool,const sp<AMessage> & format,uint32_t pixelFormat,const C2MemoryUsage & usage,const std::shared_ptr<LocalBufferPool> & localBufferPool)395 sp<GraphicBlockBuffer> AllocateGraphicBuffer(
396         const std::shared_ptr<C2BlockPool> &pool,
397         const sp<AMessage> &format,
398         uint32_t pixelFormat,
399         const C2MemoryUsage &usage,
400         const std::shared_ptr<LocalBufferPool> &localBufferPool) {
401     int32_t width, height;
402     if (!format->findInt32("width", &width) || !format->findInt32("height", &height)) {
403         ALOGD("format lacks width or height");
404         return nullptr;
405     }
406 
407     std::shared_ptr<C2GraphicBlock> block;
408     c2_status_t err = pool->fetchGraphicBlock(
409             width, height, pixelFormat, usage, &block);
410     if (err != C2_OK) {
411         ALOGD("fetch graphic block failed: %d", err);
412         return nullptr;
413     }
414 
415     return GraphicBlockBuffer::Allocate(
416             format,
417             block,
418             [localBufferPool](size_t capacity) {
419                 return localBufferPool->newBuffer(capacity);
420             });
421 }
422 
423 class BuffersArrayImpl;
424 
425 /**
426  * Flexible buffer slots implementation.
427  */
428 class FlexBuffersImpl {
429 public:
FlexBuffersImpl(const char * name)430     FlexBuffersImpl(const char *name)
431         : mImplName(std::string(name) + ".Impl"),
432           mName(mImplName.c_str()) { }
433 
434     /**
435      * Assign an empty slot for a buffer and return the index. If there's no
436      * empty slot, just add one at the end and return it.
437      *
438      * \param buffer[in]  a new buffer to assign a slot.
439      * \return            index of the assigned slot.
440      */
assignSlot(const sp<Codec2Buffer> & buffer)441     size_t assignSlot(const sp<Codec2Buffer> &buffer) {
442         for (size_t i = 0; i < mBuffers.size(); ++i) {
443             if (mBuffers[i].clientBuffer == nullptr
444                     && mBuffers[i].compBuffer.expired()) {
445                 mBuffers[i].clientBuffer = buffer;
446                 return i;
447             }
448         }
449         mBuffers.push_back({ buffer, std::weak_ptr<C2Buffer>() });
450         return mBuffers.size() - 1;
451     }
452 
453     /**
454      * Release the slot from the client, and get the C2Buffer object back from
455      * the previously assigned buffer. Note that the slot is not completely free
456      * until the returned C2Buffer object is freed.
457      *
458      * \param   buffer[in]        the buffer previously assigned a slot.
459      * \param   c2buffer[in,out]  pointer to C2Buffer to be populated. Ignored
460      *                            if null.
461      * \return  true  if the buffer is successfully released from a slot
462      *          false otherwise
463      */
releaseSlot(const sp<MediaCodecBuffer> & buffer,std::shared_ptr<C2Buffer> * c2buffer,bool release)464     bool releaseSlot(
465             const sp<MediaCodecBuffer> &buffer,
466             std::shared_ptr<C2Buffer> *c2buffer,
467             bool release) {
468         sp<Codec2Buffer> clientBuffer;
469         size_t index = mBuffers.size();
470         for (size_t i = 0; i < mBuffers.size(); ++i) {
471             if (mBuffers[i].clientBuffer == buffer) {
472                 clientBuffer = mBuffers[i].clientBuffer;
473                 if (release) {
474                     mBuffers[i].clientBuffer.clear();
475                 }
476                 index = i;
477                 break;
478             }
479         }
480         if (clientBuffer == nullptr) {
481             ALOGV("[%s] %s: No matching buffer found", mName, __func__);
482             return false;
483         }
484         std::shared_ptr<C2Buffer> result = mBuffers[index].compBuffer.lock();
485         if (!result) {
486             result = clientBuffer->asC2Buffer();
487             mBuffers[index].compBuffer = result;
488         }
489         if (c2buffer) {
490             *c2buffer = result;
491         }
492         return true;
493     }
494 
expireComponentBuffer(const std::shared_ptr<C2Buffer> & c2buffer)495     bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
496         for (size_t i = 0; i < mBuffers.size(); ++i) {
497             std::shared_ptr<C2Buffer> compBuffer =
498                     mBuffers[i].compBuffer.lock();
499             if (!compBuffer || compBuffer != c2buffer) {
500                 continue;
501             }
502             mBuffers[i].compBuffer.reset();
503             ALOGV("[%s] codec released buffer #%zu", mName, i);
504             return true;
505         }
506         ALOGV("[%s] codec released an unknown buffer", mName);
507         return false;
508     }
509 
flush()510     void flush() {
511         ALOGV("[%s] buffers are flushed %zu", mName, mBuffers.size());
512         mBuffers.clear();
513     }
514 
515 private:
516     friend class BuffersArrayImpl;
517 
518     std::string mImplName; ///< name for debugging
519     const char *mName; ///< C-string version of name
520 
521     struct Entry {
522         sp<Codec2Buffer> clientBuffer;
523         std::weak_ptr<C2Buffer> compBuffer;
524     };
525     std::vector<Entry> mBuffers;
526 };
527 
528 /**
529  * Static buffer slots implementation based on a fixed-size array.
530  */
531 class BuffersArrayImpl {
532 public:
BuffersArrayImpl()533     BuffersArrayImpl()
534         : mImplName("BuffersArrayImpl"),
535           mName(mImplName.c_str()) { }
536 
537     /**
538      * Initialize buffer array from the original |impl|. The buffers known by
539      * the client is preserved, and the empty slots are populated so that the
540      * array size is at least |minSize|.
541      *
542      * \param impl[in]      FlexBuffersImpl object used so far.
543      * \param minSize[in]   minimum size of the buffer array.
544      * \param allocate[in]  function to allocate a client buffer for an empty slot.
545      */
initialize(const FlexBuffersImpl & impl,size_t minSize,std::function<sp<Codec2Buffer> ()> allocate)546     void initialize(
547             const FlexBuffersImpl &impl,
548             size_t minSize,
549             std::function<sp<Codec2Buffer>()> allocate) {
550         mImplName = impl.mImplName + "[N]";
551         mName = mImplName.c_str();
552         for (size_t i = 0; i < impl.mBuffers.size(); ++i) {
553             sp<Codec2Buffer> clientBuffer = impl.mBuffers[i].clientBuffer;
554             bool ownedByClient = (clientBuffer != nullptr);
555             if (!ownedByClient) {
556                 clientBuffer = allocate();
557             }
558             mBuffers.push_back({ clientBuffer, impl.mBuffers[i].compBuffer, ownedByClient });
559         }
560         ALOGV("[%s] converted %zu buffers to array mode of %zu", mName, mBuffers.size(), minSize);
561         for (size_t i = impl.mBuffers.size(); i < minSize; ++i) {
562             mBuffers.push_back({ allocate(), std::weak_ptr<C2Buffer>(), false });
563         }
564     }
565 
566     /**
567      * Grab a buffer from the underlying array which matches the criteria.
568      *
569      * \param index[out]    index of the slot.
570      * \param buffer[out]   the matching buffer.
571      * \param match[in]     a function to test whether the buffer matches the
572      *                      criteria or not.
573      * \return OK           if successful,
574      *         WOULD_BLOCK  if slots are being used,
575      *         NO_MEMORY    if no slot matches the criteria, even though it's
576      *                      available
577      */
grabBuffer(size_t * index,sp<Codec2Buffer> * buffer,std::function<bool (const sp<Codec2Buffer> &)> match=[](const sp<Codec2Buffer> &){})578     status_t grabBuffer(
579             size_t *index,
580             sp<Codec2Buffer> *buffer,
581             std::function<bool(const sp<Codec2Buffer> &)> match =
582                 [](const sp<Codec2Buffer> &) { return true; }) {
583         // allBuffersDontMatch remains true if all buffers are available but
584         // match() returns false for every buffer.
585         bool allBuffersDontMatch = true;
586         for (size_t i = 0; i < mBuffers.size(); ++i) {
587             if (!mBuffers[i].ownedByClient && mBuffers[i].compBuffer.expired()) {
588                 if (match(mBuffers[i].clientBuffer)) {
589                     mBuffers[i].ownedByClient = true;
590                     *buffer = mBuffers[i].clientBuffer;
591                     (*buffer)->meta()->clear();
592                     (*buffer)->setRange(0, (*buffer)->capacity());
593                     *index = i;
594                     return OK;
595                 }
596             } else {
597                 allBuffersDontMatch = false;
598             }
599         }
600         return allBuffersDontMatch ? NO_MEMORY : WOULD_BLOCK;
601     }
602 
603     /**
604      * Return the buffer from the client, and get the C2Buffer object back from
605      * the buffer. Note that the slot is not completely free until the returned
606      * C2Buffer object is freed.
607      *
608      * \param   buffer[in]        the buffer previously grabbed.
609      * \param   c2buffer[in,out]  pointer to C2Buffer to be populated. Ignored
610      *                            if null.
611      * \return  true  if the buffer is successfully returned
612      *          false otherwise
613      */
returnBuffer(const sp<MediaCodecBuffer> & buffer,std::shared_ptr<C2Buffer> * c2buffer,bool release)614     bool returnBuffer(
615             const sp<MediaCodecBuffer> &buffer,
616             std::shared_ptr<C2Buffer> *c2buffer,
617             bool release) {
618         sp<Codec2Buffer> clientBuffer;
619         size_t index = mBuffers.size();
620         for (size_t i = 0; i < mBuffers.size(); ++i) {
621             if (mBuffers[i].clientBuffer == buffer) {
622                 if (!mBuffers[i].ownedByClient) {
623                     ALOGD("[%s] Client returned a buffer it does not own according to our record: %zu", mName, i);
624                 }
625                 clientBuffer = mBuffers[i].clientBuffer;
626                 if (release) {
627                     mBuffers[i].ownedByClient = false;
628                 }
629                 index = i;
630                 break;
631             }
632         }
633         if (clientBuffer == nullptr) {
634             ALOGV("[%s] %s: No matching buffer found", mName, __func__);
635             return false;
636         }
637         ALOGV("[%s] %s: matching buffer found (index=%zu)", mName, __func__, index);
638         std::shared_ptr<C2Buffer> result = mBuffers[index].compBuffer.lock();
639         if (!result) {
640             result = clientBuffer->asC2Buffer();
641             mBuffers[index].compBuffer = result;
642         }
643         if (c2buffer) {
644             *c2buffer = result;
645         }
646         return true;
647     }
648 
expireComponentBuffer(const std::shared_ptr<C2Buffer> & c2buffer)649     bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &c2buffer) {
650         for (size_t i = 0; i < mBuffers.size(); ++i) {
651             std::shared_ptr<C2Buffer> compBuffer =
652                     mBuffers[i].compBuffer.lock();
653             if (!compBuffer) {
654                 continue;
655             }
656             if (c2buffer == compBuffer) {
657                 if (mBuffers[i].ownedByClient) {
658                     // This should not happen.
659                     ALOGD("[%s] codec released a buffer owned by client "
660                           "(index %zu)", mName, i);
661                 }
662                 mBuffers[i].compBuffer.reset();
663                 ALOGV("[%s] codec released buffer #%zu(array mode)", mName, i);
664                 return true;
665             }
666         }
667         ALOGV("[%s] codec released an unknown buffer (array mode)", mName);
668         return false;
669     }
670 
671     /**
672      * Populate |array| with the underlying buffer array.
673      *
674      * \param array[out]  an array to be filled with the underlying buffer array.
675      */
getArray(Vector<sp<MediaCodecBuffer>> * array) const676     void getArray(Vector<sp<MediaCodecBuffer>> *array) const {
677         array->clear();
678         for (const Entry &entry : mBuffers) {
679             array->push(entry.clientBuffer);
680         }
681     }
682 
683     /**
684      * The client abandoned all known buffers, so reclaim the ownership.
685      */
flush()686     void flush() {
687         for (Entry &entry : mBuffers) {
688             entry.ownedByClient = false;
689         }
690     }
691 
realloc(std::function<sp<Codec2Buffer> ()> alloc)692     void realloc(std::function<sp<Codec2Buffer>()> alloc) {
693         size_t size = mBuffers.size();
694         mBuffers.clear();
695         for (size_t i = 0; i < size; ++i) {
696             mBuffers.push_back({ alloc(), std::weak_ptr<C2Buffer>(), false });
697         }
698     }
699 
700 private:
701     std::string mImplName; ///< name for debugging
702     const char *mName; ///< C-string version of name
703 
704     struct Entry {
705         const sp<Codec2Buffer> clientBuffer;
706         std::weak_ptr<C2Buffer> compBuffer;
707         bool ownedByClient;
708     };
709     std::vector<Entry> mBuffers;
710 };
711 
712 class InputBuffersArray : public CCodecBufferChannel::InputBuffers {
713 public:
InputBuffersArray(const char * componentName,const char * name="Input[N]")714     InputBuffersArray(const char *componentName, const char *name = "Input[N]")
715         : InputBuffers(componentName, name) { }
716     ~InputBuffersArray() override = default;
717 
initialize(const FlexBuffersImpl & impl,size_t minSize,std::function<sp<Codec2Buffer> ()> allocate)718     void initialize(
719             const FlexBuffersImpl &impl,
720             size_t minSize,
721             std::function<sp<Codec2Buffer>()> allocate) {
722         mImpl.initialize(impl, minSize, allocate);
723     }
724 
isArrayMode() const725     bool isArrayMode() const final { return true; }
726 
toArrayMode(size_t)727     std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
728             size_t) final {
729         return nullptr;
730     }
731 
getArray(Vector<sp<MediaCodecBuffer>> * array) const732     void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
733         mImpl.getArray(array);
734     }
735 
requestNewBuffer(size_t * index,sp<MediaCodecBuffer> * buffer)736     bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
737         sp<Codec2Buffer> c2Buffer;
738         status_t err = mImpl.grabBuffer(index, &c2Buffer);
739         if (err == OK) {
740             c2Buffer->setFormat(mFormat);
741             *buffer = c2Buffer;
742             return true;
743         }
744         return false;
745     }
746 
releaseBuffer(const sp<MediaCodecBuffer> & buffer,std::shared_ptr<C2Buffer> * c2buffer,bool release)747     bool releaseBuffer(
748             const sp<MediaCodecBuffer> &buffer,
749             std::shared_ptr<C2Buffer> *c2buffer,
750             bool release) override {
751         return mImpl.returnBuffer(buffer, c2buffer, release);
752     }
753 
expireComponentBuffer(const std::shared_ptr<C2Buffer> & c2buffer)754     bool expireComponentBuffer(
755             const std::shared_ptr<C2Buffer> &c2buffer) override {
756         return mImpl.expireComponentBuffer(c2buffer);
757     }
758 
flush()759     void flush() override {
760         mImpl.flush();
761     }
762 
763 private:
764     BuffersArrayImpl mImpl;
765 };
766 
767 class LinearInputBuffers : public CCodecBufferChannel::InputBuffers {
768 public:
LinearInputBuffers(const char * componentName,const char * name="1D-Input")769     LinearInputBuffers(const char *componentName, const char *name = "1D-Input")
770         : InputBuffers(componentName, name),
771           mImpl(mName) { }
772 
requestNewBuffer(size_t * index,sp<MediaCodecBuffer> * buffer)773     bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
774         int32_t capacity = kLinearBufferSize;
775         (void)mFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
776         if ((size_t)capacity > kMaxLinearBufferSize) {
777             ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
778             capacity = kMaxLinearBufferSize;
779         }
780         // TODO: proper max input size
781         // TODO: read usage from intf
782         sp<Codec2Buffer> newBuffer = alloc((size_t)capacity);
783         if (newBuffer == nullptr) {
784             return false;
785         }
786         *index = mImpl.assignSlot(newBuffer);
787         *buffer = newBuffer;
788         return true;
789     }
790 
releaseBuffer(const sp<MediaCodecBuffer> & buffer,std::shared_ptr<C2Buffer> * c2buffer,bool release)791     bool releaseBuffer(
792             const sp<MediaCodecBuffer> &buffer,
793             std::shared_ptr<C2Buffer> *c2buffer,
794             bool release) override {
795         return mImpl.releaseSlot(buffer, c2buffer, release);
796     }
797 
expireComponentBuffer(const std::shared_ptr<C2Buffer> & c2buffer)798     bool expireComponentBuffer(
799             const std::shared_ptr<C2Buffer> &c2buffer) override {
800         return mImpl.expireComponentBuffer(c2buffer);
801     }
802 
flush()803     void flush() override {
804         // This is no-op by default unless we're in array mode where we need to keep
805         // track of the flushed work.
806         mImpl.flush();
807     }
808 
toArrayMode(size_t size)809     std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
810             size_t size) final {
811         int32_t capacity = kLinearBufferSize;
812         (void)mFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
813         if ((size_t)capacity > kMaxLinearBufferSize) {
814             ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
815             capacity = kMaxLinearBufferSize;
816         }
817         // TODO: proper max input size
818         // TODO: read usage from intf
819         std::unique_ptr<InputBuffersArray> array(
820                 new InputBuffersArray(mComponentName.c_str(), "1D-Input[N]"));
821         array->setPool(mPool);
822         array->setFormat(mFormat);
823         array->initialize(
824                 mImpl,
825                 size,
826                 [this, capacity] () -> sp<Codec2Buffer> { return alloc(capacity); });
827         return std::move(array);
828     }
829 
alloc(size_t size)830     virtual sp<Codec2Buffer> alloc(size_t size) {
831         C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
832         std::shared_ptr<C2LinearBlock> block;
833 
834         c2_status_t err = mPool->fetchLinearBlock(size, usage, &block);
835         if (err != C2_OK) {
836             return nullptr;
837         }
838 
839         return LinearBlockBuffer::Allocate(mFormat, block);
840     }
841 
842 private:
843     FlexBuffersImpl mImpl;
844 };
845 
846 class EncryptedLinearInputBuffers : public LinearInputBuffers {
847 public:
EncryptedLinearInputBuffers(bool secure,const sp<MemoryDealer> & dealer,const sp<ICrypto> & crypto,int32_t heapSeqNum,size_t capacity,const char * componentName,const char * name="EncryptedInput")848     EncryptedLinearInputBuffers(
849             bool secure,
850             const sp<MemoryDealer> &dealer,
851             const sp<ICrypto> &crypto,
852             int32_t heapSeqNum,
853             size_t capacity,
854             const char *componentName, const char *name = "EncryptedInput")
855         : LinearInputBuffers(componentName, name),
856           mUsage({0, 0}),
857           mDealer(dealer),
858           mCrypto(crypto),
859           mHeapSeqNum(heapSeqNum) {
860         if (secure) {
861             mUsage = { C2MemoryUsage::READ_PROTECTED, 0 };
862         } else {
863             mUsage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
864         }
865         for (size_t i = 0; i < kMinInputBufferArraySize; ++i) {
866             sp<IMemory> memory = mDealer->allocate(capacity);
867             if (memory == nullptr) {
868                 ALOGD("[%s] Failed to allocate memory from dealer: only %zu slots allocated", mName, i);
869                 break;
870             }
871             mMemoryVector.push_back({std::weak_ptr<C2LinearBlock>(), memory});
872         }
873     }
874 
~EncryptedLinearInputBuffers()875     ~EncryptedLinearInputBuffers() override {
876     }
877 
alloc(size_t size)878     sp<Codec2Buffer> alloc(size_t size) override {
879         sp<IMemory> memory;
880         size_t slot = 0;
881         for (; slot < mMemoryVector.size(); ++slot) {
882             if (mMemoryVector[slot].block.expired()) {
883                 memory = mMemoryVector[slot].memory;
884                 break;
885             }
886         }
887         if (memory == nullptr) {
888             return nullptr;
889         }
890 
891         std::shared_ptr<C2LinearBlock> block;
892         c2_status_t err = mPool->fetchLinearBlock(size, mUsage, &block);
893         if (err != C2_OK || block == nullptr) {
894             return nullptr;
895         }
896 
897         mMemoryVector[slot].block = block;
898         return new EncryptedLinearBlockBuffer(mFormat, block, memory, mHeapSeqNum);
899     }
900 
901 private:
902     C2MemoryUsage mUsage;
903     sp<MemoryDealer> mDealer;
904     sp<ICrypto> mCrypto;
905     int32_t mHeapSeqNum;
906     struct Entry {
907         std::weak_ptr<C2LinearBlock> block;
908         sp<IMemory> memory;
909     };
910     std::vector<Entry> mMemoryVector;
911 };
912 
913 class GraphicMetadataInputBuffers : public CCodecBufferChannel::InputBuffers {
914 public:
GraphicMetadataInputBuffers(const char * componentName,const char * name="2D-MetaInput")915     GraphicMetadataInputBuffers(const char *componentName, const char *name = "2D-MetaInput")
916         : InputBuffers(componentName, name),
917           mImpl(mName),
918           mStore(GetCodec2PlatformAllocatorStore()) { }
919     ~GraphicMetadataInputBuffers() override = default;
920 
requestNewBuffer(size_t * index,sp<MediaCodecBuffer> * buffer)921     bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
922         std::shared_ptr<C2Allocator> alloc;
923         c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
924         if (err != C2_OK) {
925             return false;
926         }
927         sp<GraphicMetadataBuffer> newBuffer = new GraphicMetadataBuffer(mFormat, alloc);
928         if (newBuffer == nullptr) {
929             return false;
930         }
931         *index = mImpl.assignSlot(newBuffer);
932         *buffer = newBuffer;
933         return true;
934     }
935 
releaseBuffer(const sp<MediaCodecBuffer> & buffer,std::shared_ptr<C2Buffer> * c2buffer,bool release)936     bool releaseBuffer(
937             const sp<MediaCodecBuffer> &buffer,
938             std::shared_ptr<C2Buffer> *c2buffer,
939             bool release) override {
940         return mImpl.releaseSlot(buffer, c2buffer, release);
941     }
942 
expireComponentBuffer(const std::shared_ptr<C2Buffer> & c2buffer)943     bool expireComponentBuffer(
944             const std::shared_ptr<C2Buffer> &c2buffer) override {
945         return mImpl.expireComponentBuffer(c2buffer);
946     }
947 
flush()948     void flush() override {
949         // This is no-op by default unless we're in array mode where we need to keep
950         // track of the flushed work.
951     }
952 
toArrayMode(size_t size)953     std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
954             size_t size) final {
955         std::shared_ptr<C2Allocator> alloc;
956         c2_status_t err = mStore->fetchAllocator(mPool->getAllocatorId(), &alloc);
957         if (err != C2_OK) {
958             return nullptr;
959         }
960         std::unique_ptr<InputBuffersArray> array(
961                 new InputBuffersArray(mComponentName.c_str(), "2D-MetaInput[N]"));
962         array->setPool(mPool);
963         array->setFormat(mFormat);
964         array->initialize(
965                 mImpl,
966                 size,
967                 [format = mFormat, alloc]() -> sp<Codec2Buffer> {
968                     return new GraphicMetadataBuffer(format, alloc);
969                 });
970         return std::move(array);
971     }
972 
973 private:
974     FlexBuffersImpl mImpl;
975     std::shared_ptr<C2AllocatorStore> mStore;
976 };
977 
978 class GraphicInputBuffers : public CCodecBufferChannel::InputBuffers {
979 public:
GraphicInputBuffers(const char * componentName,const char * name="2D-BB-Input")980     GraphicInputBuffers(const char *componentName, const char *name = "2D-BB-Input")
981         : InputBuffers(componentName, name),
982           mImpl(mName),
983           mLocalBufferPool(LocalBufferPool::Create(
984                   kMaxLinearBufferSize * kMinInputBufferArraySize)) { }
985     ~GraphicInputBuffers() override = default;
986 
requestNewBuffer(size_t * index,sp<MediaCodecBuffer> * buffer)987     bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) override {
988         // TODO: proper max input size
989         // TODO: read usage from intf
990         C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
991         sp<GraphicBlockBuffer> newBuffer = AllocateGraphicBuffer(
992                 mPool, mFormat, HAL_PIXEL_FORMAT_YV12, usage, mLocalBufferPool);
993         if (newBuffer == nullptr) {
994             return false;
995         }
996         *index = mImpl.assignSlot(newBuffer);
997         *buffer = newBuffer;
998         return true;
999     }
1000 
releaseBuffer(const sp<MediaCodecBuffer> & buffer,std::shared_ptr<C2Buffer> * c2buffer,bool release)1001     bool releaseBuffer(
1002             const sp<MediaCodecBuffer> &buffer,
1003             std::shared_ptr<C2Buffer> *c2buffer,
1004             bool release) override {
1005         return mImpl.releaseSlot(buffer, c2buffer, release);
1006     }
1007 
expireComponentBuffer(const std::shared_ptr<C2Buffer> & c2buffer)1008     bool expireComponentBuffer(
1009             const std::shared_ptr<C2Buffer> &c2buffer) override {
1010         return mImpl.expireComponentBuffer(c2buffer);
1011     }
flush()1012     void flush() override {
1013         // This is no-op by default unless we're in array mode where we need to keep
1014         // track of the flushed work.
1015     }
1016 
toArrayMode(size_t size)1017     std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
1018             size_t size) final {
1019         std::unique_ptr<InputBuffersArray> array(
1020                 new InputBuffersArray(mComponentName.c_str(), "2D-BB-Input[N]"));
1021         array->setPool(mPool);
1022         array->setFormat(mFormat);
1023         array->initialize(
1024                 mImpl,
1025                 size,
1026                 [pool = mPool, format = mFormat, lbp = mLocalBufferPool]() -> sp<Codec2Buffer> {
1027                     C2MemoryUsage usage = { C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE };
1028                     return AllocateGraphicBuffer(
1029                             pool, format, HAL_PIXEL_FORMAT_YV12, usage, lbp);
1030                 });
1031         return std::move(array);
1032     }
1033 
1034 private:
1035     FlexBuffersImpl mImpl;
1036     std::shared_ptr<LocalBufferPool> mLocalBufferPool;
1037 };
1038 
1039 class DummyInputBuffers : public CCodecBufferChannel::InputBuffers {
1040 public:
DummyInputBuffers(const char * componentName,const char * name="2D-Input")1041     DummyInputBuffers(const char *componentName, const char *name = "2D-Input")
1042         : InputBuffers(componentName, name) { }
1043 
requestNewBuffer(size_t *,sp<MediaCodecBuffer> *)1044     bool requestNewBuffer(size_t *, sp<MediaCodecBuffer> *) override {
1045         return false;
1046     }
1047 
releaseBuffer(const sp<MediaCodecBuffer> &,std::shared_ptr<C2Buffer> *,bool)1048     bool releaseBuffer(
1049             const sp<MediaCodecBuffer> &, std::shared_ptr<C2Buffer> *, bool) override {
1050         return false;
1051     }
1052 
expireComponentBuffer(const std::shared_ptr<C2Buffer> &)1053     bool expireComponentBuffer(const std::shared_ptr<C2Buffer> &) override {
1054         return false;
1055     }
1056 
flush()1057     void flush() override {
1058     }
1059 
toArrayMode(size_t)1060     std::unique_ptr<CCodecBufferChannel::InputBuffers> toArrayMode(
1061             size_t) final {
1062         return nullptr;
1063     }
1064 
isArrayMode() const1065     bool isArrayMode() const final { return true; }
1066 
getArray(Vector<sp<MediaCodecBuffer>> * array) const1067     void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
1068         array->clear();
1069     }
1070 };
1071 
1072 class OutputBuffersArray : public CCodecBufferChannel::OutputBuffers {
1073 public:
OutputBuffersArray(const char * componentName,const char * name="Output[N]")1074     OutputBuffersArray(const char *componentName, const char *name = "Output[N]")
1075         : OutputBuffers(componentName, name) { }
1076     ~OutputBuffersArray() override = default;
1077 
initialize(const FlexBuffersImpl & impl,size_t minSize,std::function<sp<Codec2Buffer> ()> allocate)1078     void initialize(
1079             const FlexBuffersImpl &impl,
1080             size_t minSize,
1081             std::function<sp<Codec2Buffer>()> allocate) {
1082         mImpl.initialize(impl, minSize, allocate);
1083     }
1084 
isArrayMode() const1085     bool isArrayMode() const final { return true; }
1086 
toArrayMode(size_t)1087     std::unique_ptr<CCodecBufferChannel::OutputBuffers> toArrayMode(
1088             size_t) final {
1089         return nullptr;
1090     }
1091 
registerBuffer(const std::shared_ptr<C2Buffer> & buffer,size_t * index,sp<MediaCodecBuffer> * clientBuffer)1092     status_t registerBuffer(
1093             const std::shared_ptr<C2Buffer> &buffer,
1094             size_t *index,
1095             sp<MediaCodecBuffer> *clientBuffer) final {
1096         sp<Codec2Buffer> c2Buffer;
1097         status_t err = mImpl.grabBuffer(
1098                 index,
1099                 &c2Buffer,
1100                 [buffer](const sp<Codec2Buffer> &clientBuffer) {
1101                     return clientBuffer->canCopy(buffer);
1102                 });
1103         if (err == WOULD_BLOCK) {
1104             ALOGV("[%s] buffers temporarily not available", mName);
1105             return err;
1106         } else if (err != OK) {
1107             ALOGD("[%s] grabBuffer failed: %d", mName, err);
1108             return err;
1109         }
1110         c2Buffer->setFormat(mFormat);
1111         if (!c2Buffer->copy(buffer)) {
1112             ALOGD("[%s] copy buffer failed", mName);
1113             return WOULD_BLOCK;
1114         }
1115         submit(c2Buffer);
1116         *clientBuffer = c2Buffer;
1117         ALOGV("[%s] grabbed buffer %zu", mName, *index);
1118         return OK;
1119     }
1120 
registerCsd(const C2StreamCsdInfo::output * csd,size_t * index,sp<MediaCodecBuffer> * clientBuffer)1121     status_t registerCsd(
1122             const C2StreamCsdInfo::output *csd,
1123             size_t *index,
1124             sp<MediaCodecBuffer> *clientBuffer) final {
1125         sp<Codec2Buffer> c2Buffer;
1126         status_t err = mImpl.grabBuffer(
1127                 index,
1128                 &c2Buffer,
1129                 [csd](const sp<Codec2Buffer> &clientBuffer) {
1130                     return clientBuffer->base() != nullptr
1131                             && clientBuffer->capacity() >= csd->flexCount();
1132                 });
1133         if (err != OK) {
1134             return err;
1135         }
1136         memcpy(c2Buffer->base(), csd->m.value, csd->flexCount());
1137         c2Buffer->setRange(0, csd->flexCount());
1138         c2Buffer->setFormat(mFormat);
1139         *clientBuffer = c2Buffer;
1140         return OK;
1141     }
1142 
releaseBuffer(const sp<MediaCodecBuffer> & buffer,std::shared_ptr<C2Buffer> * c2buffer)1143     bool releaseBuffer(
1144             const sp<MediaCodecBuffer> &buffer, std::shared_ptr<C2Buffer> *c2buffer) override {
1145         return mImpl.returnBuffer(buffer, c2buffer, true);
1146     }
1147 
flush(const std::list<std::unique_ptr<C2Work>> & flushedWork)1148     void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) override {
1149         (void)flushedWork;
1150         mImpl.flush();
1151         if (mSkipCutBuffer != nullptr) {
1152             mSkipCutBuffer->clear();
1153         }
1154     }
1155 
getArray(Vector<sp<MediaCodecBuffer>> * array) const1156     void getArray(Vector<sp<MediaCodecBuffer>> *array) const final {
1157         mImpl.getArray(array);
1158     }
1159 
realloc(const std::shared_ptr<C2Buffer> & c2buffer)1160     void realloc(const std::shared_ptr<C2Buffer> &c2buffer) {
1161         std::function<sp<Codec2Buffer>()> alloc;
1162         switch (c2buffer->data().type()) {
1163             case C2BufferData::LINEAR: {
1164                 uint32_t size = kLinearBufferSize;
1165                 const C2ConstLinearBlock &block = c2buffer->data().linearBlocks().front();
1166                 if (block.size() < kMaxLinearBufferSize / 2) {
1167                     size = block.size() * 2;
1168                 } else {
1169                     size = kMaxLinearBufferSize;
1170                 }
1171                 alloc = [format = mFormat, size] {
1172                     return new LocalLinearBuffer(format, new ABuffer(size));
1173                 };
1174                 break;
1175             }
1176 
1177             // TODO: add support
1178             case C2BufferData::GRAPHIC:         FALLTHROUGH_INTENDED;
1179 
1180             case C2BufferData::INVALID:         FALLTHROUGH_INTENDED;
1181             case C2BufferData::LINEAR_CHUNKS:   FALLTHROUGH_INTENDED;
1182             case C2BufferData::GRAPHIC_CHUNKS:  FALLTHROUGH_INTENDED;
1183             default:
1184                 ALOGD("Unsupported type: %d", (int)c2buffer->data().type());
1185                 return;
1186         }
1187         mImpl.realloc(alloc);
1188     }
1189 
1190 private:
1191     BuffersArrayImpl mImpl;
1192 };
1193 
1194 class FlexOutputBuffers : public CCodecBufferChannel::OutputBuffers {
1195 public:
FlexOutputBuffers(const char * componentName,const char * name="Output[]")1196     FlexOutputBuffers(const char *componentName, const char *name = "Output[]")
1197         : OutputBuffers(componentName, name),
1198           mImpl(mName) { }
1199 
registerBuffer(const std::shared_ptr<C2Buffer> & buffer,size_t * index,sp<MediaCodecBuffer> * clientBuffer)1200     status_t registerBuffer(
1201             const std::shared_ptr<C2Buffer> &buffer,
1202             size_t *index,
1203             sp<MediaCodecBuffer> *clientBuffer) override {
1204         sp<Codec2Buffer> newBuffer = wrap(buffer);
1205         newBuffer->setFormat(mFormat);
1206         *index = mImpl.assignSlot(newBuffer);
1207         *clientBuffer = newBuffer;
1208         ALOGV("[%s] registered buffer %zu", mName, *index);
1209         return OK;
1210     }
1211 
registerCsd(const C2StreamCsdInfo::output * csd,size_t * index,sp<MediaCodecBuffer> * clientBuffer)1212     status_t registerCsd(
1213             const C2StreamCsdInfo::output *csd,
1214             size_t *index,
1215             sp<MediaCodecBuffer> *clientBuffer) final {
1216         sp<Codec2Buffer> newBuffer = new LocalLinearBuffer(
1217                 mFormat, ABuffer::CreateAsCopy(csd->m.value, csd->flexCount()));
1218         *index = mImpl.assignSlot(newBuffer);
1219         *clientBuffer = newBuffer;
1220         return OK;
1221     }
1222 
releaseBuffer(const sp<MediaCodecBuffer> & buffer,std::shared_ptr<C2Buffer> * c2buffer)1223     bool releaseBuffer(
1224             const sp<MediaCodecBuffer> &buffer,
1225             std::shared_ptr<C2Buffer> *c2buffer) override {
1226         return mImpl.releaseSlot(buffer, c2buffer, true);
1227     }
1228 
flush(const std::list<std::unique_ptr<C2Work>> & flushedWork)1229     void flush(
1230             const std::list<std::unique_ptr<C2Work>> &flushedWork) override {
1231         (void) flushedWork;
1232         // This is no-op by default unless we're in array mode where we need to keep
1233         // track of the flushed work.
1234     }
1235 
toArrayMode(size_t size)1236     std::unique_ptr<CCodecBufferChannel::OutputBuffers> toArrayMode(
1237             size_t size) override {
1238         std::unique_ptr<OutputBuffersArray> array(new OutputBuffersArray(mComponentName.c_str()));
1239         array->setFormat(mFormat);
1240         array->transferSkipCutBuffer(mSkipCutBuffer);
1241         array->initialize(
1242                 mImpl,
1243                 size,
1244                 [this]() { return allocateArrayBuffer(); });
1245         return std::move(array);
1246     }
1247 
1248     /**
1249      * Return an appropriate Codec2Buffer object for the type of buffers.
1250      *
1251      * \param buffer  C2Buffer object to wrap.
1252      *
1253      * \return  appropriate Codec2Buffer object to wrap |buffer|.
1254      */
1255     virtual sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) = 0;
1256 
1257     /**
1258      * Return an appropriate Codec2Buffer object for the type of buffers, to be
1259      * used as an empty array buffer.
1260      *
1261      * \return  appropriate Codec2Buffer object which can copy() from C2Buffers.
1262      */
1263     virtual sp<Codec2Buffer> allocateArrayBuffer() = 0;
1264 
1265 private:
1266     FlexBuffersImpl mImpl;
1267 };
1268 
1269 class LinearOutputBuffers : public FlexOutputBuffers {
1270 public:
LinearOutputBuffers(const char * componentName,const char * name="1D-Output")1271     LinearOutputBuffers(const char *componentName, const char *name = "1D-Output")
1272         : FlexOutputBuffers(componentName, name) { }
1273 
flush(const std::list<std::unique_ptr<C2Work>> & flushedWork)1274     void flush(
1275             const std::list<std::unique_ptr<C2Work>> &flushedWork) override {
1276         if (mSkipCutBuffer != nullptr) {
1277             mSkipCutBuffer->clear();
1278         }
1279         FlexOutputBuffers::flush(flushedWork);
1280     }
1281 
wrap(const std::shared_ptr<C2Buffer> & buffer)1282     sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override {
1283         if (buffer == nullptr) {
1284             ALOGV("[%s] using a dummy buffer", mName);
1285             return new LocalLinearBuffer(mFormat, new ABuffer(0));
1286         }
1287         if (buffer->data().type() != C2BufferData::LINEAR) {
1288             ALOGV("[%s] non-linear buffer %d", mName, buffer->data().type());
1289             // We expect linear output buffers from the component.
1290             return nullptr;
1291         }
1292         if (buffer->data().linearBlocks().size() != 1u) {
1293             ALOGV("[%s] no linear buffers", mName);
1294             // We expect one and only one linear block from the component.
1295             return nullptr;
1296         }
1297         sp<Codec2Buffer> clientBuffer = ConstLinearBlockBuffer::Allocate(mFormat, buffer);
1298         submit(clientBuffer);
1299         return clientBuffer;
1300     }
1301 
allocateArrayBuffer()1302     sp<Codec2Buffer> allocateArrayBuffer() override {
1303         // TODO: proper max output size
1304         return new LocalLinearBuffer(mFormat, new ABuffer(kLinearBufferSize));
1305     }
1306 };
1307 
1308 class GraphicOutputBuffers : public FlexOutputBuffers {
1309 public:
GraphicOutputBuffers(const char * componentName,const char * name="2D-Output")1310     GraphicOutputBuffers(const char *componentName, const char *name = "2D-Output")
1311         : FlexOutputBuffers(componentName, name) { }
1312 
wrap(const std::shared_ptr<C2Buffer> & buffer)1313     sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override {
1314         return new DummyContainerBuffer(mFormat, buffer);
1315     }
1316 
allocateArrayBuffer()1317     sp<Codec2Buffer> allocateArrayBuffer() override {
1318         return new DummyContainerBuffer(mFormat);
1319     }
1320 };
1321 
1322 class RawGraphicOutputBuffers : public FlexOutputBuffers {
1323 public:
RawGraphicOutputBuffers(const char * componentName,const char * name="2D-BB-Output")1324     RawGraphicOutputBuffers(const char *componentName, const char *name = "2D-BB-Output")
1325         : FlexOutputBuffers(componentName, name),
1326           mLocalBufferPool(LocalBufferPool::Create(
1327                   kMaxLinearBufferSize * kMinOutputBufferArraySize)) { }
1328     ~RawGraphicOutputBuffers() override = default;
1329 
wrap(const std::shared_ptr<C2Buffer> & buffer)1330     sp<Codec2Buffer> wrap(const std::shared_ptr<C2Buffer> &buffer) override {
1331         if (buffer == nullptr) {
1332             sp<Codec2Buffer> c2buffer = ConstGraphicBlockBuffer::AllocateEmpty(
1333                     mFormat,
1334                     [lbp = mLocalBufferPool](size_t capacity) {
1335                         return lbp->newBuffer(capacity);
1336                     });
1337             c2buffer->setRange(0, 0);
1338             return c2buffer;
1339         } else {
1340             return ConstGraphicBlockBuffer::Allocate(
1341                     mFormat,
1342                     buffer,
1343                     [lbp = mLocalBufferPool](size_t capacity) {
1344                         return lbp->newBuffer(capacity);
1345                     });
1346         }
1347     }
1348 
allocateArrayBuffer()1349     sp<Codec2Buffer> allocateArrayBuffer() override {
1350         return ConstGraphicBlockBuffer::AllocateEmpty(
1351                 mFormat,
1352                 [lbp = mLocalBufferPool](size_t capacity) {
1353                     return lbp->newBuffer(capacity);
1354                 });
1355     }
1356 
1357 private:
1358     std::shared_ptr<LocalBufferPool> mLocalBufferPool;
1359 };
1360 
1361 }  // namespace
1362 
QueueGuard(CCodecBufferChannel::QueueSync & sync)1363 CCodecBufferChannel::QueueGuard::QueueGuard(
1364         CCodecBufferChannel::QueueSync &sync) : mSync(sync) {
1365     Mutex::Autolock l(mSync.mGuardLock);
1366     // At this point it's guaranteed that mSync is not under state transition,
1367     // as we are holding its mutex.
1368 
1369     Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount);
1370     if (count->value == -1) {
1371         mRunning = false;
1372     } else {
1373         ++count->value;
1374         mRunning = true;
1375     }
1376 }
1377 
~QueueGuard()1378 CCodecBufferChannel::QueueGuard::~QueueGuard() {
1379     if (mRunning) {
1380         // We are not holding mGuardLock at this point so that QueueSync::stop() can
1381         // keep holding the lock until mCount reaches zero.
1382         Mutexed<CCodecBufferChannel::QueueSync::Counter>::Locked count(mSync.mCount);
1383         --count->value;
1384         count->cond.broadcast();
1385     }
1386 }
1387 
start()1388 void CCodecBufferChannel::QueueSync::start() {
1389     Mutex::Autolock l(mGuardLock);
1390     // If stopped, it goes to running state; otherwise no-op.
1391     Mutexed<Counter>::Locked count(mCount);
1392     if (count->value == -1) {
1393         count->value = 0;
1394     }
1395 }
1396 
stop()1397 void CCodecBufferChannel::QueueSync::stop() {
1398     Mutex::Autolock l(mGuardLock);
1399     Mutexed<Counter>::Locked count(mCount);
1400     if (count->value == -1) {
1401         // no-op
1402         return;
1403     }
1404     // Holding mGuardLock here blocks creation of additional QueueGuard objects, so
1405     // mCount can only decrement. In other words, threads that acquired the lock
1406     // are allowed to finish execution but additional threads trying to acquire
1407     // the lock at this point will block, and then get QueueGuard at STOPPED
1408     // state.
1409     while (count->value != 0) {
1410         count.waitForCondition(count->cond);
1411     }
1412     count->value = -1;
1413 }
1414 
1415 // CCodecBufferChannel::PipelineCapacity
1416 
PipelineCapacity()1417 CCodecBufferChannel::PipelineCapacity::PipelineCapacity()
1418       : input(0), component(0),
1419         mName("<UNKNOWN COMPONENT>") {
1420 }
1421 
initialize(int newInput,int newComponent,const char * newName,const char * callerTag)1422 void CCodecBufferChannel::PipelineCapacity::initialize(
1423         int newInput,
1424         int newComponent,
1425         const char* newName,
1426         const char* callerTag) {
1427     input.store(newInput, std::memory_order_relaxed);
1428     component.store(newComponent, std::memory_order_relaxed);
1429     mName = newName;
1430     ALOGV("[%s] %s -- PipelineCapacity::initialize(): "
1431           "pipeline availability initialized ==> "
1432           "input = %d, component = %d",
1433             mName, callerTag ? callerTag : "*",
1434             newInput, newComponent);
1435 }
1436 
allocate(const char * callerTag)1437 bool CCodecBufferChannel::PipelineCapacity::allocate(const char* callerTag) {
1438     int prevInput = input.fetch_sub(1, std::memory_order_relaxed);
1439     int prevComponent = component.fetch_sub(1, std::memory_order_relaxed);
1440     if (prevInput > 0 && prevComponent > 0) {
1441         ALOGV("[%s] %s -- PipelineCapacity::allocate() returns true: "
1442               "pipeline availability -1 all ==> "
1443               "input = %d, component = %d",
1444                 mName, callerTag ? callerTag : "*",
1445                 prevInput - 1,
1446                 prevComponent - 1);
1447         return true;
1448     }
1449     input.fetch_add(1, std::memory_order_relaxed);
1450     component.fetch_add(1, std::memory_order_relaxed);
1451     ALOGV("[%s] %s -- PipelineCapacity::allocate() returns false: "
1452           "pipeline availability unchanged ==> "
1453           "input = %d, component = %d",
1454             mName, callerTag ? callerTag : "*",
1455             prevInput,
1456             prevComponent);
1457     return false;
1458 }
1459 
free(const char * callerTag)1460 void CCodecBufferChannel::PipelineCapacity::free(const char* callerTag) {
1461     int prevInput = input.fetch_add(1, std::memory_order_relaxed);
1462     int prevComponent = component.fetch_add(1, std::memory_order_relaxed);
1463     ALOGV("[%s] %s -- PipelineCapacity::free(): "
1464           "pipeline availability +1 all ==> "
1465           "input = %d, component = %d",
1466             mName, callerTag ? callerTag : "*",
1467             prevInput + 1,
1468             prevComponent + 1);
1469 }
1470 
freeInputSlots(size_t numDiscardedInputBuffers,const char * callerTag)1471 int CCodecBufferChannel::PipelineCapacity::freeInputSlots(
1472         size_t numDiscardedInputBuffers,
1473         const char* callerTag) {
1474     int prevInput = input.fetch_add(numDiscardedInputBuffers,
1475                                     std::memory_order_relaxed);
1476     ALOGV("[%s] %s -- PipelineCapacity::freeInputSlots(%zu): "
1477           "pipeline availability +%zu input ==> "
1478           "input = %d, component = %d",
1479             mName, callerTag ? callerTag : "*",
1480             numDiscardedInputBuffers,
1481             numDiscardedInputBuffers,
1482             prevInput + static_cast<int>(numDiscardedInputBuffers),
1483             component.load(std::memory_order_relaxed));
1484     return prevInput + static_cast<int>(numDiscardedInputBuffers);
1485 }
1486 
freeComponentSlot(const char * callerTag)1487 int CCodecBufferChannel::PipelineCapacity::freeComponentSlot(
1488         const char* callerTag) {
1489     int prevComponent = component.fetch_add(1, std::memory_order_relaxed);
1490     ALOGV("[%s] %s -- PipelineCapacity::freeComponentSlot(): "
1491           "pipeline availability +1 component ==> "
1492           "input = %d, component = %d",
1493             mName, callerTag ? callerTag : "*",
1494             input.load(std::memory_order_relaxed),
1495             prevComponent + 1);
1496     return prevComponent + 1;
1497 }
1498 
1499 // CCodecBufferChannel::ReorderStash
1500 
ReorderStash()1501 CCodecBufferChannel::ReorderStash::ReorderStash() {
1502     clear();
1503 }
1504 
clear()1505 void CCodecBufferChannel::ReorderStash::clear() {
1506     mPending.clear();
1507     mStash.clear();
1508     mDepth = 0;
1509     mKey = C2Config::ORDINAL;
1510 }
1511 
setDepth(uint32_t depth)1512 void CCodecBufferChannel::ReorderStash::setDepth(uint32_t depth) {
1513     mPending.splice(mPending.end(), mStash);
1514     mDepth = depth;
1515 }
setKey(C2Config::ordinal_key_t key)1516 void CCodecBufferChannel::ReorderStash::setKey(C2Config::ordinal_key_t key) {
1517     mPending.splice(mPending.end(), mStash);
1518     mKey = key;
1519 }
1520 
pop(Entry * entry)1521 bool CCodecBufferChannel::ReorderStash::pop(Entry *entry) {
1522     if (mPending.empty()) {
1523         return false;
1524     }
1525     entry->buffer     = mPending.front().buffer;
1526     entry->timestamp  = mPending.front().timestamp;
1527     entry->flags      = mPending.front().flags;
1528     entry->ordinal    = mPending.front().ordinal;
1529     mPending.pop_front();
1530     return true;
1531 }
1532 
emplace(const std::shared_ptr<C2Buffer> & buffer,int64_t timestamp,int32_t flags,const C2WorkOrdinalStruct & ordinal)1533 void CCodecBufferChannel::ReorderStash::emplace(
1534         const std::shared_ptr<C2Buffer> &buffer,
1535         int64_t timestamp,
1536         int32_t flags,
1537         const C2WorkOrdinalStruct &ordinal) {
1538     for (auto it = mStash.begin(); it != mStash.end(); ++it) {
1539         if (less(ordinal, it->ordinal)) {
1540             mStash.emplace(it, buffer, timestamp, flags, ordinal);
1541             return;
1542         }
1543     }
1544     mStash.emplace_back(buffer, timestamp, flags, ordinal);
1545     while (!mStash.empty() && mStash.size() > mDepth) {
1546         mPending.push_back(mStash.front());
1547         mStash.pop_front();
1548     }
1549 }
1550 
defer(const CCodecBufferChannel::ReorderStash::Entry & entry)1551 void CCodecBufferChannel::ReorderStash::defer(
1552         const CCodecBufferChannel::ReorderStash::Entry &entry) {
1553     mPending.push_front(entry);
1554 }
1555 
hasPending() const1556 bool CCodecBufferChannel::ReorderStash::hasPending() const {
1557     return !mPending.empty();
1558 }
1559 
less(const C2WorkOrdinalStruct & o1,const C2WorkOrdinalStruct & o2)1560 bool CCodecBufferChannel::ReorderStash::less(
1561         const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2) {
1562     switch (mKey) {
1563         case C2Config::ORDINAL:   return o1.frameIndex < o2.frameIndex;
1564         case C2Config::TIMESTAMP: return o1.timestamp < o2.timestamp;
1565         case C2Config::CUSTOM:    return o1.customOrdinal < o2.customOrdinal;
1566         default:
1567             ALOGD("Unrecognized key; default to timestamp");
1568             return o1.frameIndex < o2.frameIndex;
1569     }
1570 }
1571 
1572 // CCodecBufferChannel
1573 
CCodecBufferChannel(const std::shared_ptr<CCodecCallback> & callback)1574 CCodecBufferChannel::CCodecBufferChannel(
1575         const std::shared_ptr<CCodecCallback> &callback)
1576     : mHeapSeqNum(-1),
1577       mCCodecCallback(callback),
1578       mFrameIndex(0u),
1579       mFirstValidFrameIndex(0u),
1580       mMetaMode(MODE_NONE),
1581       mAvailablePipelineCapacity(),
1582       mInputMetEos(false) {
1583     Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
1584     buffers->reset(new DummyInputBuffers(""));
1585 }
1586 
~CCodecBufferChannel()1587 CCodecBufferChannel::~CCodecBufferChannel() {
1588     if (mCrypto != nullptr && mDealer != nullptr && mHeapSeqNum >= 0) {
1589         mCrypto->unsetHeap(mHeapSeqNum);
1590     }
1591 }
1592 
setComponent(const std::shared_ptr<Codec2Client::Component> & component)1593 void CCodecBufferChannel::setComponent(
1594         const std::shared_ptr<Codec2Client::Component> &component) {
1595     mComponent = component;
1596     mComponentName = component->getName() + StringPrintf("#%d", int(uintptr_t(component.get()) % 997));
1597     mName = mComponentName.c_str();
1598 }
1599 
setInputSurface(const std::shared_ptr<InputSurfaceWrapper> & surface)1600 status_t CCodecBufferChannel::setInputSurface(
1601         const std::shared_ptr<InputSurfaceWrapper> &surface) {
1602     ALOGV("[%s] setInputSurface", mName);
1603     mInputSurface = surface;
1604     return mInputSurface->connect(mComponent);
1605 }
1606 
signalEndOfInputStream()1607 status_t CCodecBufferChannel::signalEndOfInputStream() {
1608     if (mInputSurface == nullptr) {
1609         return INVALID_OPERATION;
1610     }
1611     return mInputSurface->signalEndOfInputStream();
1612 }
1613 
queueInputBufferInternal(const sp<MediaCodecBuffer> & buffer)1614 status_t CCodecBufferChannel::queueInputBufferInternal(const sp<MediaCodecBuffer> &buffer) {
1615     int64_t timeUs;
1616     CHECK(buffer->meta()->findInt64("timeUs", &timeUs));
1617 
1618     if (mInputMetEos) {
1619         ALOGD("[%s] buffers after EOS ignored (%lld us)", mName, (long long)timeUs);
1620         return OK;
1621     }
1622 
1623     int32_t flags = 0;
1624     int32_t tmp = 0;
1625     bool eos = false;
1626     if (buffer->meta()->findInt32("eos", &tmp) && tmp) {
1627         eos = true;
1628         mInputMetEos = true;
1629         ALOGV("[%s] input EOS", mName);
1630     }
1631     if (buffer->meta()->findInt32("csd", &tmp) && tmp) {
1632         flags |= C2FrameData::FLAG_CODEC_CONFIG;
1633     }
1634     ALOGV("[%s] queueInputBuffer: buffer->size() = %zu", mName, buffer->size());
1635     std::unique_ptr<C2Work> work(new C2Work);
1636     work->input.ordinal.timestamp = timeUs;
1637     work->input.ordinal.frameIndex = mFrameIndex++;
1638     // WORKAROUND: until codecs support handling work after EOS and max output sizing, use timestamp
1639     // manipulation to achieve image encoding via video codec, and to constrain encoded output.
1640     // Keep client timestamp in customOrdinal
1641     work->input.ordinal.customOrdinal = timeUs;
1642     work->input.buffers.clear();
1643 
1644     if (buffer->size() > 0u) {
1645         Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
1646         std::shared_ptr<C2Buffer> c2buffer;
1647         if (!(*buffers)->releaseBuffer(buffer, &c2buffer, false)) {
1648             return -ENOENT;
1649         }
1650         work->input.buffers.push_back(c2buffer);
1651     } else {
1652         mAvailablePipelineCapacity.freeInputSlots(1, "queueInputBufferInternal");
1653         if (eos) {
1654             flags |= C2FrameData::FLAG_END_OF_STREAM;
1655         }
1656     }
1657     work->input.flags = (C2FrameData::flags_t)flags;
1658     // TODO: fill info's
1659 
1660     work->input.configUpdate = std::move(mParamsToBeSet);
1661     work->worklets.clear();
1662     work->worklets.emplace_back(new C2Worklet);
1663 
1664     std::list<std::unique_ptr<C2Work>> items;
1665     items.push_back(std::move(work));
1666     c2_status_t err = mComponent->queue(&items);
1667 
1668     if (err == C2_OK && eos && buffer->size() > 0u) {
1669         mCCodecCallback->onWorkQueued(false);
1670         work.reset(new C2Work);
1671         work->input.ordinal.timestamp = timeUs;
1672         work->input.ordinal.frameIndex = mFrameIndex++;
1673         // WORKAROUND: keep client timestamp in customOrdinal
1674         work->input.ordinal.customOrdinal = timeUs;
1675         work->input.buffers.clear();
1676         work->input.flags = C2FrameData::FLAG_END_OF_STREAM;
1677 
1678         items.clear();
1679         items.push_back(std::move(work));
1680         err = mComponent->queue(&items);
1681     }
1682     if (err == C2_OK) {
1683         mCCodecCallback->onWorkQueued(eos);
1684 
1685         Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
1686         bool released = (*buffers)->releaseBuffer(buffer, nullptr, true);
1687         ALOGV("[%s] queueInputBuffer: buffer %sreleased", mName, released ? "" : "not ");
1688     }
1689 
1690     feedInputBufferIfAvailableInternal();
1691     return err;
1692 }
1693 
setParameters(std::vector<std::unique_ptr<C2Param>> & params)1694 status_t CCodecBufferChannel::setParameters(std::vector<std::unique_ptr<C2Param>> &params) {
1695     QueueGuard guard(mSync);
1696     if (!guard.isRunning()) {
1697         ALOGD("[%s] setParameters is only supported in the running state.", mName);
1698         return -ENOSYS;
1699     }
1700     mParamsToBeSet.insert(mParamsToBeSet.end(),
1701                           std::make_move_iterator(params.begin()),
1702                           std::make_move_iterator(params.end()));
1703     params.clear();
1704     return OK;
1705 }
1706 
queueInputBuffer(const sp<MediaCodecBuffer> & buffer)1707 status_t CCodecBufferChannel::queueInputBuffer(const sp<MediaCodecBuffer> &buffer) {
1708     QueueGuard guard(mSync);
1709     if (!guard.isRunning()) {
1710         ALOGD("[%s] No more buffers should be queued at current state.", mName);
1711         return -ENOSYS;
1712     }
1713     return queueInputBufferInternal(buffer);
1714 }
1715 
queueSecureInputBuffer(const sp<MediaCodecBuffer> & buffer,bool secure,const uint8_t * key,const uint8_t * iv,CryptoPlugin::Mode mode,CryptoPlugin::Pattern pattern,const CryptoPlugin::SubSample * subSamples,size_t numSubSamples,AString * errorDetailMsg)1716 status_t CCodecBufferChannel::queueSecureInputBuffer(
1717         const sp<MediaCodecBuffer> &buffer, bool secure, const uint8_t *key,
1718         const uint8_t *iv, CryptoPlugin::Mode mode, CryptoPlugin::Pattern pattern,
1719         const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
1720         AString *errorDetailMsg) {
1721     QueueGuard guard(mSync);
1722     if (!guard.isRunning()) {
1723         ALOGD("[%s] No more buffers should be queued at current state.", mName);
1724         return -ENOSYS;
1725     }
1726 
1727     if (!hasCryptoOrDescrambler()) {
1728         return -ENOSYS;
1729     }
1730     sp<EncryptedLinearBlockBuffer> encryptedBuffer((EncryptedLinearBlockBuffer *)buffer.get());
1731 
1732     ssize_t result = -1;
1733     ssize_t codecDataOffset = 0;
1734     if (mCrypto != nullptr) {
1735         ICrypto::DestinationBuffer destination;
1736         if (secure) {
1737             destination.mType = ICrypto::kDestinationTypeNativeHandle;
1738             destination.mHandle = encryptedBuffer->handle();
1739         } else {
1740             destination.mType = ICrypto::kDestinationTypeSharedMemory;
1741             destination.mSharedMemory = mDecryptDestination;
1742         }
1743         ICrypto::SourceBuffer source;
1744         encryptedBuffer->fillSourceBuffer(&source);
1745         result = mCrypto->decrypt(
1746                 key, iv, mode, pattern, source, buffer->offset(),
1747                 subSamples, numSubSamples, destination, errorDetailMsg);
1748         if (result < 0) {
1749             return result;
1750         }
1751         if (destination.mType == ICrypto::kDestinationTypeSharedMemory) {
1752             encryptedBuffer->copyDecryptedContent(mDecryptDestination, result);
1753         }
1754     } else {
1755         // Here we cast CryptoPlugin::SubSample to hardware::cas::native::V1_0::SubSample
1756         // directly, the structure definitions should match as checked in DescramblerImpl.cpp.
1757         hidl_vec<SubSample> hidlSubSamples;
1758         hidlSubSamples.setToExternal((SubSample *)subSamples, numSubSamples, false /*own*/);
1759 
1760         hardware::cas::native::V1_0::SharedBuffer srcBuffer;
1761         encryptedBuffer->fillSourceBuffer(&srcBuffer);
1762 
1763         DestinationBuffer dstBuffer;
1764         if (secure) {
1765             dstBuffer.type = BufferType::NATIVE_HANDLE;
1766             dstBuffer.secureMemory = hidl_handle(encryptedBuffer->handle());
1767         } else {
1768             dstBuffer.type = BufferType::SHARED_MEMORY;
1769             dstBuffer.nonsecureMemory = srcBuffer;
1770         }
1771 
1772         CasStatus status = CasStatus::OK;
1773         hidl_string detailedError;
1774         ScramblingControl sctrl = ScramblingControl::UNSCRAMBLED;
1775 
1776         if (key != nullptr) {
1777             sctrl = (ScramblingControl)key[0];
1778             // Adjust for the PES offset
1779             codecDataOffset = key[2] | (key[3] << 8);
1780         }
1781 
1782         auto returnVoid = mDescrambler->descramble(
1783                 sctrl,
1784                 hidlSubSamples,
1785                 srcBuffer,
1786                 0,
1787                 dstBuffer,
1788                 0,
1789                 [&status, &result, &detailedError] (
1790                         CasStatus _status, uint32_t _bytesWritten,
1791                         const hidl_string& _detailedError) {
1792                     status = _status;
1793                     result = (ssize_t)_bytesWritten;
1794                     detailedError = _detailedError;
1795                 });
1796 
1797         if (!returnVoid.isOk() || status != CasStatus::OK || result < 0) {
1798             ALOGI("[%s] descramble failed, trans=%s, status=%d, result=%zd",
1799                     mName, returnVoid.description().c_str(), status, result);
1800             return UNKNOWN_ERROR;
1801         }
1802 
1803         if (result < codecDataOffset) {
1804             ALOGD("invalid codec data offset: %zd, result %zd", codecDataOffset, result);
1805             return BAD_VALUE;
1806         }
1807 
1808         ALOGV("[%s] descramble succeeded, %zd bytes", mName, result);
1809 
1810         if (dstBuffer.type == BufferType::SHARED_MEMORY) {
1811             encryptedBuffer->copyDecryptedContentFromMemory(result);
1812         }
1813     }
1814 
1815     buffer->setRange(codecDataOffset, result - codecDataOffset);
1816     return queueInputBufferInternal(buffer);
1817 }
1818 
feedInputBufferIfAvailable()1819 void CCodecBufferChannel::feedInputBufferIfAvailable() {
1820     QueueGuard guard(mSync);
1821     if (!guard.isRunning()) {
1822         ALOGV("[%s] We're not running --- no input buffer reported", mName);
1823         return;
1824     }
1825     feedInputBufferIfAvailableInternal();
1826 }
1827 
feedInputBufferIfAvailableInternal()1828 void CCodecBufferChannel::feedInputBufferIfAvailableInternal() {
1829     while (!mInputMetEos &&
1830            !mReorderStash.lock()->hasPending() &&
1831            mAvailablePipelineCapacity.allocate("feedInputBufferIfAvailable")) {
1832         sp<MediaCodecBuffer> inBuffer;
1833         size_t index;
1834         {
1835             Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
1836             if (!(*buffers)->requestNewBuffer(&index, &inBuffer)) {
1837                 ALOGV("[%s] no new buffer available", mName);
1838                 mAvailablePipelineCapacity.free("feedInputBufferIfAvailable");
1839                 break;
1840             }
1841         }
1842         ALOGV("[%s] new input index = %zu [%p]", mName, index, inBuffer.get());
1843         mCallback->onInputBufferAvailable(index, inBuffer);
1844     }
1845 }
1846 
renderOutputBuffer(const sp<MediaCodecBuffer> & buffer,int64_t timestampNs)1847 status_t CCodecBufferChannel::renderOutputBuffer(
1848         const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) {
1849     ALOGV("[%s] renderOutputBuffer: %p", mName, buffer.get());
1850     std::shared_ptr<C2Buffer> c2Buffer;
1851     bool released = false;
1852     {
1853         Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
1854         if (*buffers) {
1855             released = (*buffers)->releaseBuffer(buffer, &c2Buffer);
1856         }
1857     }
1858     // NOTE: some apps try to releaseOutputBuffer() with timestamp and/or render
1859     //       set to true.
1860     sendOutputBuffers();
1861     // input buffer feeding may have been gated by pending output buffers
1862     feedInputBufferIfAvailable();
1863     if (!c2Buffer) {
1864         if (released) {
1865             ALOGD("[%s] The app is calling releaseOutputBuffer() with "
1866                   "timestamp or render=true with non-video buffers. Apps should "
1867                   "call releaseOutputBuffer() with render=false for those.",
1868                   mName);
1869         }
1870         return INVALID_OPERATION;
1871     }
1872 
1873 #if 0
1874     const std::vector<std::shared_ptr<const C2Info>> infoParams = c2Buffer->info();
1875     ALOGV("[%s] queuing gfx buffer with %zu infos", mName, infoParams.size());
1876     for (const std::shared_ptr<const C2Info> &info : infoParams) {
1877         AString res;
1878         for (size_t ix = 0; ix + 3 < info->size(); ix += 4) {
1879             if (ix) res.append(", ");
1880             res.append(*((int32_t*)info.get() + (ix / 4)));
1881         }
1882         ALOGV("  [%s]", res.c_str());
1883     }
1884 #endif
1885     std::shared_ptr<const C2StreamRotationInfo::output> rotation =
1886         std::static_pointer_cast<const C2StreamRotationInfo::output>(
1887                 c2Buffer->getInfo(C2StreamRotationInfo::output::PARAM_TYPE));
1888     bool flip = rotation && (rotation->flip & 1);
1889     uint32_t quarters = ((rotation ? rotation->value : 0) / 90) & 3;
1890     uint32_t transform = 0;
1891     switch (quarters) {
1892         case 0: // no rotation
1893             transform = flip ? HAL_TRANSFORM_FLIP_H : 0;
1894             break;
1895         case 1: // 90 degrees counter-clockwise
1896             transform = flip ? (HAL_TRANSFORM_FLIP_V | HAL_TRANSFORM_ROT_90)
1897                     : HAL_TRANSFORM_ROT_270;
1898             break;
1899         case 2: // 180 degrees
1900             transform = flip ? HAL_TRANSFORM_FLIP_V : HAL_TRANSFORM_ROT_180;
1901             break;
1902         case 3: // 90 degrees clockwise
1903             transform = flip ? (HAL_TRANSFORM_FLIP_H | HAL_TRANSFORM_ROT_90)
1904                     : HAL_TRANSFORM_ROT_90;
1905             break;
1906     }
1907 
1908     std::shared_ptr<const C2StreamSurfaceScalingInfo::output> surfaceScaling =
1909         std::static_pointer_cast<const C2StreamSurfaceScalingInfo::output>(
1910                 c2Buffer->getInfo(C2StreamSurfaceScalingInfo::output::PARAM_TYPE));
1911     uint32_t videoScalingMode = NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW;
1912     if (surfaceScaling) {
1913         videoScalingMode = surfaceScaling->value;
1914     }
1915 
1916     // Use dataspace from format as it has the default aspects already applied
1917     android_dataspace_t dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0
1918     (void)buffer->format()->findInt32("android._dataspace", (int32_t *)&dataSpace);
1919 
1920     // HDR static info
1921     std::shared_ptr<const C2StreamHdrStaticInfo::output> hdrStaticInfo =
1922         std::static_pointer_cast<const C2StreamHdrStaticInfo::output>(
1923                 c2Buffer->getInfo(C2StreamHdrStaticInfo::output::PARAM_TYPE));
1924 
1925     // HDR10 plus info
1926     std::shared_ptr<const C2StreamHdr10PlusInfo::output> hdr10PlusInfo =
1927         std::static_pointer_cast<const C2StreamHdr10PlusInfo::output>(
1928                 c2Buffer->getInfo(C2StreamHdr10PlusInfo::output::PARAM_TYPE));
1929 
1930     {
1931         Mutexed<OutputSurface>::Locked output(mOutputSurface);
1932         if (output->surface == nullptr) {
1933             ALOGI("[%s] cannot render buffer without surface", mName);
1934             return OK;
1935         }
1936     }
1937 
1938     std::vector<C2ConstGraphicBlock> blocks = c2Buffer->data().graphicBlocks();
1939     if (blocks.size() != 1u) {
1940         ALOGD("[%s] expected 1 graphic block, but got %zu", mName, blocks.size());
1941         return UNKNOWN_ERROR;
1942     }
1943     const C2ConstGraphicBlock &block = blocks.front();
1944 
1945     // TODO: revisit this after C2Fence implementation.
1946     android::IGraphicBufferProducer::QueueBufferInput qbi(
1947             timestampNs,
1948             false, // droppable
1949             dataSpace,
1950             Rect(blocks.front().crop().left,
1951                  blocks.front().crop().top,
1952                  blocks.front().crop().right(),
1953                  blocks.front().crop().bottom()),
1954             videoScalingMode,
1955             transform,
1956             Fence::NO_FENCE, 0);
1957     if (hdrStaticInfo || hdr10PlusInfo) {
1958         HdrMetadata hdr;
1959         if (hdrStaticInfo) {
1960             struct android_smpte2086_metadata smpte2086_meta = {
1961                 .displayPrimaryRed = {
1962                     hdrStaticInfo->mastering.red.x, hdrStaticInfo->mastering.red.y
1963                 },
1964                 .displayPrimaryGreen = {
1965                     hdrStaticInfo->mastering.green.x, hdrStaticInfo->mastering.green.y
1966                 },
1967                 .displayPrimaryBlue = {
1968                     hdrStaticInfo->mastering.blue.x, hdrStaticInfo->mastering.blue.y
1969                 },
1970                 .whitePoint = {
1971                     hdrStaticInfo->mastering.white.x, hdrStaticInfo->mastering.white.y
1972                 },
1973                 .maxLuminance = hdrStaticInfo->mastering.maxLuminance,
1974                 .minLuminance = hdrStaticInfo->mastering.minLuminance,
1975             };
1976 
1977             struct android_cta861_3_metadata cta861_meta = {
1978                 .maxContentLightLevel = hdrStaticInfo->maxCll,
1979                 .maxFrameAverageLightLevel = hdrStaticInfo->maxFall,
1980             };
1981 
1982             hdr.validTypes = HdrMetadata::SMPTE2086 | HdrMetadata::CTA861_3;
1983             hdr.smpte2086 = smpte2086_meta;
1984             hdr.cta8613 = cta861_meta;
1985         }
1986         if (hdr10PlusInfo) {
1987             hdr.validTypes |= HdrMetadata::HDR10PLUS;
1988             hdr.hdr10plus.assign(
1989                     hdr10PlusInfo->m.value,
1990                     hdr10PlusInfo->m.value + hdr10PlusInfo->flexCount());
1991         }
1992         qbi.setHdrMetadata(hdr);
1993     }
1994     // we don't have dirty regions
1995     qbi.setSurfaceDamage(Region::INVALID_REGION);
1996     android::IGraphicBufferProducer::QueueBufferOutput qbo;
1997     status_t result = mComponent->queueToOutputSurface(block, qbi, &qbo);
1998     if (result != OK) {
1999         ALOGI("[%s] queueBuffer failed: %d", mName, result);
2000         return result;
2001     }
2002     ALOGV("[%s] queue buffer successful", mName);
2003 
2004     int64_t mediaTimeUs = 0;
2005     (void)buffer->meta()->findInt64("timeUs", &mediaTimeUs);
2006     mCCodecCallback->onOutputFramesRendered(mediaTimeUs, timestampNs);
2007 
2008     return OK;
2009 }
2010 
discardBuffer(const sp<MediaCodecBuffer> & buffer)2011 status_t CCodecBufferChannel::discardBuffer(const sp<MediaCodecBuffer> &buffer) {
2012     ALOGV("[%s] discardBuffer: %p", mName, buffer.get());
2013     bool released = false;
2014     {
2015         Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2016         if (*buffers && (*buffers)->releaseBuffer(buffer, nullptr, true)) {
2017             buffers.unlock();
2018             released = true;
2019             mAvailablePipelineCapacity.freeInputSlots(1, "discardBuffer");
2020         }
2021     }
2022     {
2023         Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2024         if (*buffers && (*buffers)->releaseBuffer(buffer, nullptr)) {
2025             buffers.unlock();
2026             released = true;
2027         }
2028     }
2029     if (released) {
2030         sendOutputBuffers();
2031         feedInputBufferIfAvailable();
2032     } else {
2033         ALOGD("[%s] MediaCodec discarded an unknown buffer", mName);
2034     }
2035     return OK;
2036 }
2037 
getInputBufferArray(Vector<sp<MediaCodecBuffer>> * array)2038 void CCodecBufferChannel::getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
2039     array->clear();
2040     Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2041 
2042     if (!(*buffers)->isArrayMode()) {
2043         *buffers = (*buffers)->toArrayMode(kMinInputBufferArraySize);
2044     }
2045 
2046     (*buffers)->getArray(array);
2047 }
2048 
getOutputBufferArray(Vector<sp<MediaCodecBuffer>> * array)2049 void CCodecBufferChannel::getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) {
2050     array->clear();
2051     Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2052 
2053     if (!(*buffers)->isArrayMode()) {
2054         *buffers = (*buffers)->toArrayMode(kMinOutputBufferArraySize);
2055     }
2056 
2057     (*buffers)->getArray(array);
2058 }
2059 
start(const sp<AMessage> & inputFormat,const sp<AMessage> & outputFormat)2060 status_t CCodecBufferChannel::start(
2061         const sp<AMessage> &inputFormat, const sp<AMessage> &outputFormat) {
2062     C2StreamBufferTypeSetting::input iStreamFormat(0u);
2063     C2StreamBufferTypeSetting::output oStreamFormat(0u);
2064     C2PortReorderBufferDepthTuning::output reorderDepth;
2065     C2PortReorderKeySetting::output reorderKey;
2066     c2_status_t err = mComponent->query(
2067             {
2068                 &iStreamFormat,
2069                 &oStreamFormat,
2070                 &reorderDepth,
2071                 &reorderKey,
2072             },
2073             {},
2074             C2_DONT_BLOCK,
2075             nullptr);
2076     if (err == C2_BAD_INDEX) {
2077         if (!iStreamFormat || !oStreamFormat) {
2078             return UNKNOWN_ERROR;
2079         }
2080     } else if (err != C2_OK) {
2081         return UNKNOWN_ERROR;
2082     }
2083 
2084     {
2085         Mutexed<ReorderStash>::Locked reorder(mReorderStash);
2086         reorder->clear();
2087         if (reorderDepth) {
2088             reorder->setDepth(reorderDepth.value);
2089         }
2090         if (reorderKey) {
2091             reorder->setKey(reorderKey.value);
2092         }
2093     }
2094     // TODO: get this from input format
2095     bool secure = mComponent->getName().find(".secure") != std::string::npos;
2096 
2097     std::shared_ptr<C2AllocatorStore> allocatorStore = GetCodec2PlatformAllocatorStore();
2098     int poolMask = property_get_int32(
2099             "debug.stagefright.c2-poolmask",
2100             1 << C2PlatformAllocatorStore::ION |
2101             1 << C2PlatformAllocatorStore::BUFFERQUEUE);
2102 
2103     if (inputFormat != nullptr) {
2104         bool graphic = (iStreamFormat.value == C2FormatVideo);
2105         std::shared_ptr<C2BlockPool> pool;
2106         {
2107             Mutexed<BlockPools>::Locked pools(mBlockPools);
2108 
2109             // set default allocator ID.
2110             pools->inputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
2111                                                 : C2PlatformAllocatorStore::ION;
2112 
2113             // query C2PortAllocatorsTuning::input from component. If an allocator ID is obtained
2114             // from component, create the input block pool with given ID. Otherwise, use default IDs.
2115             std::vector<std::unique_ptr<C2Param>> params;
2116             err = mComponent->query({ },
2117                                     { C2PortAllocatorsTuning::input::PARAM_TYPE },
2118                                     C2_DONT_BLOCK,
2119                                     &params);
2120             if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
2121                 ALOGD("[%s] Query input allocators returned %zu params => %s (%u)",
2122                         mName, params.size(), asString(err), err);
2123             } else if (err == C2_OK && params.size() == 1) {
2124                 C2PortAllocatorsTuning::input *inputAllocators =
2125                     C2PortAllocatorsTuning::input::From(params[0].get());
2126                 if (inputAllocators && inputAllocators->flexCount() > 0) {
2127                     std::shared_ptr<C2Allocator> allocator;
2128                     // verify allocator IDs and resolve default allocator
2129                     allocatorStore->fetchAllocator(inputAllocators->m.values[0], &allocator);
2130                     if (allocator) {
2131                         pools->inputAllocatorId = allocator->getId();
2132                     } else {
2133                         ALOGD("[%s] component requested invalid input allocator ID %u",
2134                                 mName, inputAllocators->m.values[0]);
2135                     }
2136                 }
2137             }
2138 
2139             // TODO: use C2Component wrapper to associate this pool with ourselves
2140             if ((poolMask >> pools->inputAllocatorId) & 1) {
2141                 err = CreateCodec2BlockPool(pools->inputAllocatorId, nullptr, &pool);
2142                 ALOGD("[%s] Created input block pool with allocatorID %u => poolID %llu - %s (%d)",
2143                         mName, pools->inputAllocatorId,
2144                         (unsigned long long)(pool ? pool->getLocalId() : 111000111),
2145                         asString(err), err);
2146             } else {
2147                 err = C2_NOT_FOUND;
2148             }
2149             if (err != C2_OK) {
2150                 C2BlockPool::local_id_t inputPoolId =
2151                     graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
2152                 err = GetCodec2BlockPool(inputPoolId, nullptr, &pool);
2153                 ALOGD("[%s] Using basic input block pool with poolID %llu => got %llu - %s (%d)",
2154                         mName, (unsigned long long)inputPoolId,
2155                         (unsigned long long)(pool ? pool->getLocalId() : 111000111),
2156                         asString(err), err);
2157                 if (err != C2_OK) {
2158                     return NO_MEMORY;
2159                 }
2160             }
2161             pools->inputPool = pool;
2162         }
2163 
2164         bool forceArrayMode = false;
2165         Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2166         if (graphic) {
2167             if (mInputSurface) {
2168                 buffers->reset(new DummyInputBuffers(mName));
2169             } else if (mMetaMode == MODE_ANW) {
2170                 buffers->reset(new GraphicMetadataInputBuffers(mName));
2171             } else {
2172                 buffers->reset(new GraphicInputBuffers(mName));
2173             }
2174         } else {
2175             if (hasCryptoOrDescrambler()) {
2176                 int32_t capacity = kLinearBufferSize;
2177                 (void)inputFormat->findInt32(KEY_MAX_INPUT_SIZE, &capacity);
2178                 if ((size_t)capacity > kMaxLinearBufferSize) {
2179                     ALOGD("client requested %d, capped to %zu", capacity, kMaxLinearBufferSize);
2180                     capacity = kMaxLinearBufferSize;
2181                 }
2182                 if (mDealer == nullptr) {
2183                     mDealer = new MemoryDealer(
2184                             align(capacity, MemoryDealer::getAllocationAlignment())
2185                                 * (kMinInputBufferArraySize + 1),
2186                             "EncryptedLinearInputBuffers");
2187                     mDecryptDestination = mDealer->allocate((size_t)capacity);
2188                 }
2189                 if (mCrypto != nullptr && mHeapSeqNum < 0) {
2190                     mHeapSeqNum = mCrypto->setHeap(mDealer->getMemoryHeap());
2191                 } else {
2192                     mHeapSeqNum = -1;
2193                 }
2194                 buffers->reset(new EncryptedLinearInputBuffers(
2195                         secure, mDealer, mCrypto, mHeapSeqNum, (size_t)capacity, mName));
2196                 forceArrayMode = true;
2197             } else {
2198                 buffers->reset(new LinearInputBuffers(mName));
2199             }
2200         }
2201         (*buffers)->setFormat(inputFormat);
2202 
2203         if (err == C2_OK) {
2204             (*buffers)->setPool(pool);
2205         } else {
2206             // TODO: error
2207         }
2208 
2209         if (forceArrayMode) {
2210             *buffers = (*buffers)->toArrayMode(kMinInputBufferArraySize);
2211         }
2212     }
2213 
2214     if (outputFormat != nullptr) {
2215         sp<IGraphicBufferProducer> outputSurface;
2216         uint32_t outputGeneration;
2217         {
2218             Mutexed<OutputSurface>::Locked output(mOutputSurface);
2219             outputSurface = output->surface ?
2220                     output->surface->getIGraphicBufferProducer() : nullptr;
2221             outputGeneration = output->generation;
2222         }
2223 
2224         bool graphic = (oStreamFormat.value == C2FormatVideo);
2225         C2BlockPool::local_id_t outputPoolId_;
2226 
2227         {
2228             Mutexed<BlockPools>::Locked pools(mBlockPools);
2229 
2230             // set default allocator ID.
2231             pools->outputAllocatorId = (graphic) ? C2PlatformAllocatorStore::GRALLOC
2232                                                  : C2PlatformAllocatorStore::ION;
2233 
2234             // query C2PortAllocatorsTuning::output from component, or use default allocator if
2235             // unsuccessful.
2236             std::vector<std::unique_ptr<C2Param>> params;
2237             err = mComponent->query({ },
2238                                     { C2PortAllocatorsTuning::output::PARAM_TYPE },
2239                                     C2_DONT_BLOCK,
2240                                     &params);
2241             if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
2242                 ALOGD("[%s] Query output allocators returned %zu params => %s (%u)",
2243                         mName, params.size(), asString(err), err);
2244             } else if (err == C2_OK && params.size() == 1) {
2245                 C2PortAllocatorsTuning::output *outputAllocators =
2246                     C2PortAllocatorsTuning::output::From(params[0].get());
2247                 if (outputAllocators && outputAllocators->flexCount() > 0) {
2248                     std::shared_ptr<C2Allocator> allocator;
2249                     // verify allocator IDs and resolve default allocator
2250                     allocatorStore->fetchAllocator(outputAllocators->m.values[0], &allocator);
2251                     if (allocator) {
2252                         pools->outputAllocatorId = allocator->getId();
2253                     } else {
2254                         ALOGD("[%s] component requested invalid output allocator ID %u",
2255                                 mName, outputAllocators->m.values[0]);
2256                     }
2257                 }
2258             }
2259 
2260             // use bufferqueue if outputting to a surface.
2261             // query C2PortSurfaceAllocatorTuning::output from component, or use default allocator
2262             // if unsuccessful.
2263             if (outputSurface) {
2264                 params.clear();
2265                 err = mComponent->query({ },
2266                                         { C2PortSurfaceAllocatorTuning::output::PARAM_TYPE },
2267                                         C2_DONT_BLOCK,
2268                                         &params);
2269                 if ((err != C2_OK && err != C2_BAD_INDEX) || params.size() != 1) {
2270                     ALOGD("[%s] Query output surface allocator returned %zu params => %s (%u)",
2271                             mName, params.size(), asString(err), err);
2272                 } else if (err == C2_OK && params.size() == 1) {
2273                     C2PortSurfaceAllocatorTuning::output *surfaceAllocator =
2274                         C2PortSurfaceAllocatorTuning::output::From(params[0].get());
2275                     if (surfaceAllocator) {
2276                         std::shared_ptr<C2Allocator> allocator;
2277                         // verify allocator IDs and resolve default allocator
2278                         allocatorStore->fetchAllocator(surfaceAllocator->value, &allocator);
2279                         if (allocator) {
2280                             pools->outputAllocatorId = allocator->getId();
2281                         } else {
2282                             ALOGD("[%s] component requested invalid surface output allocator ID %u",
2283                                     mName, surfaceAllocator->value);
2284                             err = C2_BAD_VALUE;
2285                         }
2286                     }
2287                 }
2288                 if (pools->outputAllocatorId == C2PlatformAllocatorStore::GRALLOC
2289                         && err != C2_OK
2290                         && ((poolMask >> C2PlatformAllocatorStore::BUFFERQUEUE) & 1)) {
2291                     pools->outputAllocatorId = C2PlatformAllocatorStore::BUFFERQUEUE;
2292                 }
2293             }
2294 
2295             if ((poolMask >> pools->outputAllocatorId) & 1) {
2296                 err = mComponent->createBlockPool(
2297                         pools->outputAllocatorId, &pools->outputPoolId, &pools->outputPoolIntf);
2298                 ALOGI("[%s] Created output block pool with allocatorID %u => poolID %llu - %s",
2299                         mName, pools->outputAllocatorId,
2300                         (unsigned long long)pools->outputPoolId,
2301                         asString(err));
2302             } else {
2303                 err = C2_NOT_FOUND;
2304             }
2305             if (err != C2_OK) {
2306                 // use basic pool instead
2307                 pools->outputPoolId =
2308                     graphic ? C2BlockPool::BASIC_GRAPHIC : C2BlockPool::BASIC_LINEAR;
2309             }
2310 
2311             // Configure output block pool ID as parameter C2PortBlockPoolsTuning::output to
2312             // component.
2313             std::unique_ptr<C2PortBlockPoolsTuning::output> poolIdsTuning =
2314                     C2PortBlockPoolsTuning::output::AllocUnique({ pools->outputPoolId });
2315 
2316             std::vector<std::unique_ptr<C2SettingResult>> failures;
2317             err = mComponent->config({ poolIdsTuning.get() }, C2_MAY_BLOCK, &failures);
2318             ALOGD("[%s] Configured output block pool ids %llu => %s",
2319                     mName, (unsigned long long)poolIdsTuning->m.values[0], asString(err));
2320             outputPoolId_ = pools->outputPoolId;
2321         }
2322 
2323         Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2324 
2325         if (graphic) {
2326             if (outputSurface) {
2327                 buffers->reset(new GraphicOutputBuffers(mName));
2328             } else {
2329                 buffers->reset(new RawGraphicOutputBuffers(mName));
2330             }
2331         } else {
2332             buffers->reset(new LinearOutputBuffers(mName));
2333         }
2334         (*buffers)->setFormat(outputFormat->dup());
2335 
2336 
2337         // Try to set output surface to created block pool if given.
2338         if (outputSurface) {
2339             mComponent->setOutputSurface(
2340                     outputPoolId_,
2341                     outputSurface,
2342                     outputGeneration);
2343         }
2344 
2345         if (oStreamFormat.value == C2BufferData::LINEAR
2346                 && mComponentName.find("c2.qti.") == std::string::npos) {
2347             // WORKAROUND: if we're using early CSD workaround we convert to
2348             //             array mode, to appease apps assuming the output
2349             //             buffers to be of the same size.
2350             (*buffers) = (*buffers)->toArrayMode(kMinOutputBufferArraySize);
2351 
2352             int32_t channelCount;
2353             int32_t sampleRate;
2354             if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount)
2355                     && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
2356                 int32_t delay = 0;
2357                 int32_t padding = 0;;
2358                 if (!outputFormat->findInt32("encoder-delay", &delay)) {
2359                     delay = 0;
2360                 }
2361                 if (!outputFormat->findInt32("encoder-padding", &padding)) {
2362                     padding = 0;
2363                 }
2364                 if (delay || padding) {
2365                     // We need write access to the buffers, and we're already in
2366                     // array mode.
2367                     (*buffers)->initSkipCutBuffer(delay, padding, sampleRate, channelCount);
2368                 }
2369             }
2370         }
2371     }
2372 
2373     // Set up pipeline control. This has to be done after mInputBuffers and
2374     // mOutputBuffers are initialized to make sure that lingering callbacks
2375     // about buffers from the previous generation do not interfere with the
2376     // newly initialized pipeline capacity.
2377 
2378     // Query delays
2379     C2PortRequestedDelayTuning::input inputDelay;
2380     C2PortRequestedDelayTuning::output outputDelay;
2381     C2RequestedPipelineDelayTuning pipelineDelay;
2382 #if 0
2383     err = mComponent->query(
2384             { &inputDelay, &pipelineDelay, &outputDelay },
2385             {},
2386             C2_DONT_BLOCK,
2387             nullptr);
2388     mAvailablePipelineCapacity.initialize(
2389             inputDelay,
2390             inputDelay + pipelineDelay,
2391             inputDelay + pipelineDelay + outputDelay,
2392             mName);
2393 #else
2394     mAvailablePipelineCapacity.initialize(
2395             kMinInputBufferArraySize,
2396             kMaxPipelineCapacity,
2397             mName);
2398 #endif
2399 
2400     mInputMetEos = false;
2401     mSync.start();
2402     return OK;
2403 }
2404 
requestInitialInputBuffers()2405 status_t CCodecBufferChannel::requestInitialInputBuffers() {
2406     if (mInputSurface) {
2407         return OK;
2408     }
2409 
2410     C2StreamFormatConfig::output oStreamFormat(0u);
2411     c2_status_t err = mComponent->query({ &oStreamFormat }, {}, C2_DONT_BLOCK, nullptr);
2412     if (err != C2_OK) {
2413         return UNKNOWN_ERROR;
2414     }
2415     std::vector<sp<MediaCodecBuffer>> toBeQueued;
2416     // TODO: use proper buffer depth instead of this random value
2417     for (size_t i = 0; i < kMinInputBufferArraySize; ++i) {
2418         size_t index;
2419         sp<MediaCodecBuffer> buffer;
2420         {
2421             Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2422             if (!(*buffers)->requestNewBuffer(&index, &buffer)) {
2423                 if (i == 0) {
2424                     ALOGW("[%s] start: cannot allocate memory at all", mName);
2425                     return NO_MEMORY;
2426                 } else {
2427                     ALOGV("[%s] start: cannot allocate memory, only %zu buffers allocated",
2428                             mName, i);
2429                 }
2430                 break;
2431             }
2432         }
2433         if (buffer) {
2434             Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs);
2435             ALOGV("[%s] input buffer %zu available", mName, index);
2436             bool post = true;
2437             if (!configs->empty()) {
2438                 sp<ABuffer> config = configs->front();
2439                 if (buffer->capacity() >= config->size()) {
2440                     memcpy(buffer->base(), config->data(), config->size());
2441                     buffer->setRange(0, config->size());
2442                     buffer->meta()->clear();
2443                     buffer->meta()->setInt64("timeUs", 0);
2444                     buffer->meta()->setInt32("csd", 1);
2445                     post = false;
2446                 } else {
2447                     ALOGD("[%s] buffer capacity too small for the config (%zu < %zu)",
2448                             mName, buffer->capacity(), config->size());
2449                 }
2450             } else if (oStreamFormat.value == C2BufferData::LINEAR && i == 0
2451                     && mComponentName.find("c2.qti.") == std::string::npos) {
2452                 // WORKAROUND: Some apps expect CSD available without queueing
2453                 //             any input. Queue an empty buffer to get the CSD.
2454                 buffer->setRange(0, 0);
2455                 buffer->meta()->clear();
2456                 buffer->meta()->setInt64("timeUs", 0);
2457                 post = false;
2458             }
2459             if (mAvailablePipelineCapacity.allocate("requestInitialInputBuffers")) {
2460                 if (post) {
2461                     mCallback->onInputBufferAvailable(index, buffer);
2462                 } else {
2463                     toBeQueued.emplace_back(buffer);
2464                 }
2465             } else {
2466                 ALOGD("[%s] pipeline is full while requesting %zu-th input buffer",
2467                         mName, i);
2468             }
2469         }
2470     }
2471     for (const sp<MediaCodecBuffer> &buffer : toBeQueued) {
2472         if (queueInputBufferInternal(buffer) != OK) {
2473             mAvailablePipelineCapacity.freeComponentSlot("requestInitialInputBuffers");
2474         }
2475     }
2476     return OK;
2477 }
2478 
stop()2479 void CCodecBufferChannel::stop() {
2480     mSync.stop();
2481     mFirstValidFrameIndex = mFrameIndex.load(std::memory_order_relaxed);
2482     if (mInputSurface != nullptr) {
2483         mInputSurface.reset();
2484     }
2485 }
2486 
flush(const std::list<std::unique_ptr<C2Work>> & flushedWork)2487 void CCodecBufferChannel::flush(const std::list<std::unique_ptr<C2Work>> &flushedWork) {
2488     ALOGV("[%s] flush", mName);
2489     {
2490         Mutexed<std::list<sp<ABuffer>>>::Locked configs(mFlushedConfigs);
2491         for (const std::unique_ptr<C2Work> &work : flushedWork) {
2492             if (!(work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) {
2493                 continue;
2494             }
2495             if (work->input.buffers.empty()
2496                     || work->input.buffers.front()->data().linearBlocks().empty()) {
2497                 ALOGD("[%s] no linear codec config data found", mName);
2498                 continue;
2499             }
2500             C2ReadView view =
2501                     work->input.buffers.front()->data().linearBlocks().front().map().get();
2502             if (view.error() != C2_OK) {
2503                 ALOGD("[%s] failed to map flushed codec config data: %d", mName, view.error());
2504                 continue;
2505             }
2506             configs->push_back(ABuffer::CreateAsCopy(view.data(), view.capacity()));
2507             ALOGV("[%s] stashed flushed codec config data (size=%u)", mName, view.capacity());
2508         }
2509     }
2510     {
2511         Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2512         (*buffers)->flush();
2513     }
2514     {
2515         Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2516         (*buffers)->flush(flushedWork);
2517     }
2518 }
2519 
onWorkDone(std::unique_ptr<C2Work> work,const sp<AMessage> & outputFormat,const C2StreamInitDataInfo::output * initData,size_t numDiscardedInputBuffers)2520 void CCodecBufferChannel::onWorkDone(
2521         std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
2522         const C2StreamInitDataInfo::output *initData,
2523         size_t numDiscardedInputBuffers) {
2524     if (handleWork(std::move(work), outputFormat, initData)) {
2525         mAvailablePipelineCapacity.freeInputSlots(numDiscardedInputBuffers,
2526                                                   "onWorkDone");
2527         feedInputBufferIfAvailable();
2528     }
2529 }
2530 
onInputBufferDone(const std::shared_ptr<C2Buffer> & buffer)2531 void CCodecBufferChannel::onInputBufferDone(
2532         const std::shared_ptr<C2Buffer>& buffer) {
2533     bool newInputSlotAvailable;
2534     {
2535         Mutexed<std::unique_ptr<InputBuffers>>::Locked buffers(mInputBuffers);
2536         newInputSlotAvailable = (*buffers)->expireComponentBuffer(buffer);
2537         if (newInputSlotAvailable) {
2538             mAvailablePipelineCapacity.freeInputSlots(1, "onInputBufferDone");
2539         }
2540     }
2541     if (newInputSlotAvailable) {
2542         feedInputBufferIfAvailable();
2543     }
2544 }
2545 
handleWork(std::unique_ptr<C2Work> work,const sp<AMessage> & outputFormat,const C2StreamInitDataInfo::output * initData)2546 bool CCodecBufferChannel::handleWork(
2547         std::unique_ptr<C2Work> work,
2548         const sp<AMessage> &outputFormat,
2549         const C2StreamInitDataInfo::output *initData) {
2550     if ((work->input.ordinal.frameIndex - mFirstValidFrameIndex.load()).peek() < 0) {
2551         // Discard frames from previous generation.
2552         ALOGD("[%s] Discard frames from previous generation.", mName);
2553         return false;
2554     }
2555 
2556     if (work->worklets.size() != 1u
2557             || !work->worklets.front()
2558             || !(work->worklets.front()->output.flags & C2FrameData::FLAG_INCOMPLETE)) {
2559         mAvailablePipelineCapacity.freeComponentSlot("handleWork");
2560     }
2561 
2562     if (work->result == C2_NOT_FOUND) {
2563         ALOGD("[%s] flushed work; ignored.", mName);
2564         return true;
2565     }
2566 
2567     if (work->result != C2_OK) {
2568         ALOGD("[%s] work failed to complete: %d", mName, work->result);
2569         mCCodecCallback->onError(work->result, ACTION_CODE_FATAL);
2570         return false;
2571     }
2572 
2573     // NOTE: MediaCodec usage supposedly have only one worklet
2574     if (work->worklets.size() != 1u) {
2575         ALOGI("[%s] onWorkDone: incorrect number of worklets: %zu",
2576                 mName, work->worklets.size());
2577         mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2578         return false;
2579     }
2580 
2581     const std::unique_ptr<C2Worklet> &worklet = work->worklets.front();
2582 
2583     std::shared_ptr<C2Buffer> buffer;
2584     // NOTE: MediaCodec usage supposedly have only one output stream.
2585     if (worklet->output.buffers.size() > 1u) {
2586         ALOGI("[%s] onWorkDone: incorrect number of output buffers: %zu",
2587                 mName, worklet->output.buffers.size());
2588         mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2589         return false;
2590     } else if (worklet->output.buffers.size() == 1u) {
2591         buffer = worklet->output.buffers[0];
2592         if (!buffer) {
2593             ALOGD("[%s] onWorkDone: nullptr found in buffers; ignored.", mName);
2594         }
2595     }
2596 
2597     while (!worklet->output.configUpdate.empty()) {
2598         std::unique_ptr<C2Param> param;
2599         worklet->output.configUpdate.back().swap(param);
2600         worklet->output.configUpdate.pop_back();
2601         switch (param->coreIndex().coreIndex()) {
2602             case C2PortReorderBufferDepthTuning::CORE_INDEX: {
2603                 C2PortReorderBufferDepthTuning::output reorderDepth;
2604                 if (reorderDepth.updateFrom(*param)) {
2605                     mReorderStash.lock()->setDepth(reorderDepth.value);
2606                     ALOGV("[%s] onWorkDone: updated reorder depth to %u",
2607                           mName, reorderDepth.value);
2608                 } else {
2609                     ALOGD("[%s] onWorkDone: failed to read reorder depth", mName);
2610                 }
2611                 break;
2612             }
2613             case C2PortReorderKeySetting::CORE_INDEX: {
2614                 C2PortReorderKeySetting::output reorderKey;
2615                 if (reorderKey.updateFrom(*param)) {
2616                     mReorderStash.lock()->setKey(reorderKey.value);
2617                     ALOGV("[%s] onWorkDone: updated reorder key to %u",
2618                           mName, reorderKey.value);
2619                 } else {
2620                     ALOGD("[%s] onWorkDone: failed to read reorder key", mName);
2621                 }
2622                 break;
2623             }
2624             default:
2625                 ALOGV("[%s] onWorkDone: unrecognized config update (%08X)",
2626                       mName, param->index());
2627                 break;
2628         }
2629     }
2630 
2631     if (outputFormat != nullptr) {
2632         Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2633         ALOGD("[%s] onWorkDone: output format changed to %s",
2634                 mName, outputFormat->debugString().c_str());
2635         (*buffers)->setFormat(outputFormat);
2636 
2637         AString mediaType;
2638         if (outputFormat->findString(KEY_MIME, &mediaType)
2639                 && mediaType == MIMETYPE_AUDIO_RAW) {
2640             int32_t channelCount;
2641             int32_t sampleRate;
2642             if (outputFormat->findInt32(KEY_CHANNEL_COUNT, &channelCount)
2643                     && outputFormat->findInt32(KEY_SAMPLE_RATE, &sampleRate)) {
2644                 (*buffers)->updateSkipCutBuffer(sampleRate, channelCount);
2645             }
2646         }
2647     }
2648 
2649     int32_t flags = 0;
2650     if (worklet->output.flags & C2FrameData::FLAG_END_OF_STREAM) {
2651         flags |= MediaCodec::BUFFER_FLAG_EOS;
2652         ALOGV("[%s] onWorkDone: output EOS", mName);
2653     }
2654 
2655     sp<MediaCodecBuffer> outBuffer;
2656     size_t index;
2657 
2658     // WORKAROUND: adjust output timestamp based on client input timestamp and codec
2659     // input timestamp. Codec output timestamp (in the timestamp field) shall correspond to
2660     // the codec input timestamp, but client output timestamp should (reported in timeUs)
2661     // shall correspond to the client input timesamp (in customOrdinal). By using the
2662     // delta between the two, this allows for some timestamp deviation - e.g. if one input
2663     // produces multiple output.
2664     c2_cntr64_t timestamp =
2665         worklet->output.ordinal.timestamp + work->input.ordinal.customOrdinal
2666                 - work->input.ordinal.timestamp;
2667     ALOGV("[%s] onWorkDone: input %lld, codec %lld => output %lld => %lld",
2668           mName,
2669           work->input.ordinal.customOrdinal.peekll(),
2670           work->input.ordinal.timestamp.peekll(),
2671           worklet->output.ordinal.timestamp.peekll(),
2672           timestamp.peekll());
2673 
2674     if (initData != nullptr) {
2675         Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2676         if ((*buffers)->registerCsd(initData, &index, &outBuffer) == OK) {
2677             outBuffer->meta()->setInt64("timeUs", timestamp.peek());
2678             outBuffer->meta()->setInt32("flags", MediaCodec::BUFFER_FLAG_CODECCONFIG);
2679             ALOGV("[%s] onWorkDone: csd index = %zu [%p]", mName, index, outBuffer.get());
2680 
2681             buffers.unlock();
2682             mCallback->onOutputBufferAvailable(index, outBuffer);
2683             buffers.lock();
2684         } else {
2685             ALOGD("[%s] onWorkDone: unable to register csd", mName);
2686             buffers.unlock();
2687             mCCodecCallback->onError(UNKNOWN_ERROR, ACTION_CODE_FATAL);
2688             buffers.lock();
2689             return false;
2690         }
2691     }
2692 
2693     if (!buffer && !flags) {
2694         ALOGV("[%s] onWorkDone: Not reporting output buffer (%lld)",
2695               mName, work->input.ordinal.frameIndex.peekull());
2696         return true;
2697     }
2698 
2699     if (buffer) {
2700         for (const std::shared_ptr<const C2Info> &info : buffer->info()) {
2701             // TODO: properly translate these to metadata
2702             switch (info->coreIndex().coreIndex()) {
2703                 case C2StreamPictureTypeMaskInfo::CORE_INDEX:
2704                     if (((C2StreamPictureTypeMaskInfo *)info.get())->value & C2PictureTypeKeyFrame) {
2705                         flags |= MediaCodec::BUFFER_FLAG_SYNCFRAME;
2706                     }
2707                     break;
2708                 default:
2709                     break;
2710             }
2711         }
2712     }
2713 
2714     {
2715         Mutexed<ReorderStash>::Locked reorder(mReorderStash);
2716         reorder->emplace(buffer, timestamp.peek(), flags, worklet->output.ordinal);
2717         if (flags & MediaCodec::BUFFER_FLAG_EOS) {
2718             // Flush reorder stash
2719             reorder->setDepth(0);
2720         }
2721     }
2722     sendOutputBuffers();
2723     return true;
2724 }
2725 
sendOutputBuffers()2726 void CCodecBufferChannel::sendOutputBuffers() {
2727     ReorderStash::Entry entry;
2728     sp<MediaCodecBuffer> outBuffer;
2729     size_t index;
2730 
2731     while (true) {
2732         {
2733             Mutexed<ReorderStash>::Locked reorder(mReorderStash);
2734             if (!reorder->hasPending()) {
2735                 break;
2736             }
2737             if (!reorder->pop(&entry)) {
2738                 break;
2739             }
2740         }
2741         Mutexed<std::unique_ptr<OutputBuffers>>::Locked buffers(mOutputBuffers);
2742         status_t err = (*buffers)->registerBuffer(entry.buffer, &index, &outBuffer);
2743         if (err != OK) {
2744             if (err != WOULD_BLOCK) {
2745                 OutputBuffersArray *array = (OutputBuffersArray *)buffers->get();
2746                 array->realloc(entry.buffer);
2747                 mCCodecCallback->onOutputBuffersChanged();
2748             }
2749             buffers.unlock();
2750             ALOGV("[%s] sendOutputBuffers: unable to register output buffer", mName);
2751             mReorderStash.lock()->defer(entry);
2752             return;
2753         }
2754         buffers.unlock();
2755 
2756         outBuffer->meta()->setInt64("timeUs", entry.timestamp);
2757         outBuffer->meta()->setInt32("flags", entry.flags);
2758         ALOGV("[%s] sendOutputBuffers: out buffer index = %zu [%p] => %p + %zu",
2759                 mName, index, outBuffer.get(), outBuffer->data(), outBuffer->size());
2760         mCallback->onOutputBufferAvailable(index, outBuffer);
2761     }
2762 }
2763 
setSurface(const sp<Surface> & newSurface)2764 status_t CCodecBufferChannel::setSurface(const sp<Surface> &newSurface) {
2765     static std::atomic_uint32_t surfaceGeneration{0};
2766     uint32_t generation = (getpid() << 10) |
2767             ((surfaceGeneration.fetch_add(1, std::memory_order_relaxed) + 1)
2768                 & ((1 << 10) - 1));
2769 
2770     sp<IGraphicBufferProducer> producer;
2771     if (newSurface) {
2772         newSurface->setScalingMode(NATIVE_WINDOW_SCALING_MODE_SCALE_TO_WINDOW);
2773         newSurface->setMaxDequeuedBufferCount(kMinOutputBufferArraySize);
2774         producer = newSurface->getIGraphicBufferProducer();
2775         producer->setGenerationNumber(generation);
2776     } else {
2777         ALOGE("[%s] setting output surface to null", mName);
2778         return INVALID_OPERATION;
2779     }
2780 
2781     std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
2782     C2BlockPool::local_id_t outputPoolId;
2783     {
2784         Mutexed<BlockPools>::Locked pools(mBlockPools);
2785         outputPoolId = pools->outputPoolId;
2786         outputPoolIntf = pools->outputPoolIntf;
2787     }
2788 
2789     if (outputPoolIntf) {
2790         if (mComponent->setOutputSurface(
2791                 outputPoolId,
2792                 producer,
2793                 generation) != C2_OK) {
2794             ALOGI("[%s] setSurface: component setOutputSurface failed", mName);
2795             return INVALID_OPERATION;
2796         }
2797     }
2798 
2799     {
2800         Mutexed<OutputSurface>::Locked output(mOutputSurface);
2801         output->surface = newSurface;
2802         output->generation = generation;
2803     }
2804 
2805     return OK;
2806 }
2807 
setMetaMode(MetaMode mode)2808 void CCodecBufferChannel::setMetaMode(MetaMode mode) {
2809     mMetaMode = mode;
2810 }
2811 
toStatusT(c2_status_t c2s,c2_operation_t c2op)2812 status_t toStatusT(c2_status_t c2s, c2_operation_t c2op) {
2813     // C2_OK is always translated to OK.
2814     if (c2s == C2_OK) {
2815         return OK;
2816     }
2817 
2818     // Operation-dependent translation
2819     // TODO: Add as necessary
2820     switch (c2op) {
2821     case C2_OPERATION_Component_start:
2822         switch (c2s) {
2823         case C2_NO_MEMORY:
2824             return NO_MEMORY;
2825         default:
2826             return UNKNOWN_ERROR;
2827         }
2828     default:
2829         break;
2830     }
2831 
2832     // Backup operation-agnostic translation
2833     switch (c2s) {
2834     case C2_BAD_INDEX:
2835         return BAD_INDEX;
2836     case C2_BAD_VALUE:
2837         return BAD_VALUE;
2838     case C2_BLOCKING:
2839         return WOULD_BLOCK;
2840     case C2_DUPLICATE:
2841         return ALREADY_EXISTS;
2842     case C2_NO_INIT:
2843         return NO_INIT;
2844     case C2_NO_MEMORY:
2845         return NO_MEMORY;
2846     case C2_NOT_FOUND:
2847         return NAME_NOT_FOUND;
2848     case C2_TIMED_OUT:
2849         return TIMED_OUT;
2850     case C2_BAD_STATE:
2851     case C2_CANCELED:
2852     case C2_CANNOT_DO:
2853     case C2_CORRUPTED:
2854     case C2_OMITTED:
2855     case C2_REFUSED:
2856         return UNKNOWN_ERROR;
2857     default:
2858         return -static_cast<status_t>(c2s);
2859     }
2860 }
2861 
2862 }  // namespace android
2863