在分析 CCodecBufferChannel 遇到了 InputBuffers这个变量,这篇就单独看 CCodecBuffers
/frameworks/av/media/codec2/sfplugin/CCodecBuffers.h
98 class InputBuffers : public CCodecBuffers {
99 public:
100 InputBuffers(const char *componentName, const char *name = "Input[]")
101 : CCodecBuffers(componentName, name) { }
103
104 /**
105 * Set a block pool to obtain input memory blocks.
106 */
107 void setPool(const std::shared_ptr<C2BlockPool> &pool) { mPool = pool; }
109 /**
110 * Get a new MediaCodecBuffer for input and its corresponding index.
111 * Returns false if no new buffer can be obtained at the moment.
112 */
113 virtual bool requestNewBuffer(size_t *index, sp<MediaCodecBuffer> *buffer) = 0;
.....
144
145 /**
146 * Release the buffer obtained from requestNewBuffer(), and create a deep
147 * copy clone of the buffer.
148 *
149 * \return the deep copy clone of the buffer; nullptr if cloning is not
150 * possible.
151 */
152 sp<Codec2Buffer> cloneAndReleaseBuffer(const sp<MediaCodecBuffer> &buffer);
153
154 protected:
155 virtual sp<Codec2Buffer> createNewBuffer() = 0;
156
157 // Pool to obtain blocks for input buffers.
158 std::shared_ptr<C2BlockPool> mPool;
159
160 private:
161 DISALLOW_EVIL_CONSTRUCTORS(InputBuffers);
162 };
/frameworks/av/media/codec2/core/include/C2Buffer.h
849 /**
850 * Block pools are used by components to obtain output buffers in an efficient way. They can
851 * support either linear (1D), circular (1D) or graphic (2D) blocks.
852 *
853 * Block pools decouple the recycling of memory/allocations from the components. They are meant to
854 * be an opaque service (there are no public APIs other than obtaining blocks) provided by the
855 * platform. Block pools are also meant to decouple allocations from memory used by buffers. This
856 * is accomplished by allowing pools to allot multiple memory 'blocks' on a single allocation. As
857 * their name suggest, block pools maintain a pool of memory blocks. When a component asks for
858 * a memory block, pools will try to return a free memory block already in the pool. If no such
859 * block exists, they will allocate memory using the backing allocator and allot a block on that
860 * allocation. When blocks are no longer used in the system, they are recycled back to the block
861 * pool and are available as free blocks.
862 *
863 * Never constructed on stack.
864 */
865 class C2BlockPool {
866 public:
867 /**
868 * Block pool ID type.
869 */
870 typedef uint64_t local_id_t;
871
872 enum : local_id_t {
873 BASIC_LINEAR = 0, ///< ID of basic (unoptimized) block pool for fetching 1D blocks
874 BASIC_GRAPHIC = 1, ///< ID of basic (unoptimized) block pool for fetching 2D blocks
875 PLATFORM_START = 0x10,
876 };
877
878 /**
879 * Returns the ID for this block pool. This ID is used to get this block pool from the platform.
880 * It is only valid in the current process.
881 *
882 * This method MUST be "non-blocking" and return within 1ms.
883 *
884 * \return a local ID for this block pool.
885 */
886 virtual local_id_t getLocalId() const = 0;
887
888 /**
889 * Returns the ID of the backing allocator of this block pool.
890 *
891 * This method MUST be "non-blocking" and return within 1ms.
892 *
893 * \return the ID of the backing allocator of this block pool.
894 */
895 virtual C2Allocator::id_t getAllocatorId() const = 0;
896
897 /**
898 * Obtains a linear writeable block of given |capacity| and |usage|. If successful, the
899 * block is stored in |block|. Otherwise, |block| is set to 'nullptr'.
900 *
901 * \note The returned buffer may have a larger capacity than requested. In this case the
902 * larger (returned) capacity may be fully used.
903 *
904 * \note There is no guarantee on the alignedness of the returned block. The only guarantee is
905 * that its capacity is equal to or larger than the requested capacity.
906 *
907 * \param capacity the size of requested block.
908 * \param usage the memory usage info for the requested block. Returned blocks will be
909 * optimized for this usage, but may be used with any usage. One exception:
910 * protected blocks/buffers can only be used in a protected scenario.
911 * \param block pointer to where the obtained block shall be stored on success. nullptr will
912 * be stored here on failure
913 *
914 * \retval C2_OK the operation was successful
915 * \retval C2_NO_MEMORY not enough memory to complete any required allocation
916 * \retval C2_TIMED_OUT the operation timed out
917 * \retval C2_BLOCKING the operation is blocked
918 * \retval C2_REFUSED no permission to complete any required allocation
919 * \retval C2_BAD_VALUE capacity or usage are not supported (invalid) (caller error)
920 * \retval C2_OMITTED this pool does not support linear blocks
921 * \retval C2_CORRUPTED some unknown, unrecoverable error occurred during operation
922 * (unexpected)
923 */
924 virtual c2_status_t fetchLinearBlock(
925 uint32_t capacity __unused, C2MemoryUsage usage __unused,
926 std::shared_ptr<C2LinearBlock> *block /* nonnull */) {
927 *block = nullptr;
928 return C2_OMITTED;
929 }
960 virtual c2_status_t fetchCircularBlock(...
965 }
999 virtual c2_status_t fetchGraphicBlock(...
1005 }
1104 protected:
1105 C2BlockPool() = default;
1106 };
那么问题又来了 Block 又是什么?从上面来看获取block 主要有3种:
- fetchCircularBlock C2CircularBlock
- fetchLinearBlock C2LinearBlock
- fetchGraphicBlock C2GraphicBlock
/frameworks/av/media/codec2/core/include/C2Buffer.h
// 线性 block,一次性写好给消费的去使用
1280 /**
1281 * Linear block is a writeable 1D block. Once written, it can be shared in whole or in parts with
1282 * consumers/readers as read-only const linear block(s).
1283 */
1284 class C2LinearBlock : public C2Block1D {
1285 public:
1286 /**
1287 * Maps this block into memory and returns a write view for it.
1288 *
1289 * \return a write view for this block.
1290 */
1291 C2Acquirable<C2WriteView> map();
1292
1293 /**
1294 * Creates a read-only const linear block for a portion of this block; optionally protected
1295 * by an acquire fence. There are two ways to use this:
1296 *
1297 * 1) share ready block after writing data into the block. In this case no fence shall be
1298 * supplied, and the block shall not be modified after calling this method.
1299 * 2) share block metadata before actually (finishing) writing the data into the block. In
1300 * this case a fence must be supplied that will be triggered when the data is written.
1301 * The block shall be modified only until firing the event for the fence.
1302 */
1303 C2ConstLinearBlock share(size_t offset, size_t size, C2Fence fence);
1304
1305 protected:
1306 C2LinearBlock(std::shared_ptr<Impl> impl, const _C2LinearRangeAspect &range);
1307
1308 friend struct _C2BlockFactory;
1309 };
------------------
// Graphic Block 和Liner 类似,一次性写完后有consumer 读取消耗,不过这个是 2D 的,专门为图像
2066 /**
2067 * Graphic block is a writeable 2D block. Once written, it can be shared in whole or in part with
2068 * consumers/readers as read-only const graphic block.
2069 */
2070 class C2GraphicBlock : public C2Block2D {
2071 public:
2072 /**
2073 * Maps this block into memory and returns a write view for it.
2074 *
2075 * \return a write view for this block.
2076 */
2077 C2Acquirable<C2GraphicView> map();
2078
2079 /**
2080 * Creates a read-only const linear block for a portion of this block; optionally protected
2081 * by an acquire fence. There are two ways to use this:
2082 *
2083 * 1) share ready block after writing data into the block. In this case no fence shall be
2084 * supplied, and the block shall not be modified after calling this method.
2085 * 2) share block metadata before actually (finishing) writing the data into the block. In
2086 * this case a fence must be supplied that will be triggered when the data is written.
2087 * The block shall be modified only until firing the event for the fence.
2088 */
2089 C2ConstGraphicBlock share(const C2Rect &crop, C2Fence fence);
2090
2091 protected:
2092 C2GraphicBlock(std::shared_ptr<Impl> impl, const _C2PlanarSectionAspect §ion);
2093
2094 friend struct _C2BlockFactory;
2095 };
--------------------
//环形buffer:可以一边写一边读,循环使用
1320 /**
1321 * Circular blocks can be used to share data between a writer and a reader (and/or other consumers)-
1322 * in a memory-efficient way by reusing a section of memory. Circular blocks are a bit more complex
1323 * than single reader/single writer schemes to facilitate block-based consuming of data.
1324 *
1325 * They can operate in two modes:
1326 *
1327 * 1) one writer that creates blocks to be consumed (this model can be used by components)
1328 *
1329 * 2) one writer that writes continuously, and one reader that can creates blocks to be consumed
1330 * by further recipients (this model is used by the framework, and cannot be used by components.)
1331 *
1332 * Circular blocks have four segments with running pointers:
1333 * - reserved: data reserved and available for the writer
1334 * - committed: data committed by the writer and available to the reader (if present)
1335 * - used: data used by consumers (if present)
1336 * - available: unused data available to be reserved
1337 */
1338 class C2CircularBlock : public C2Block1D {
1339 // TODO: add methods
1340
1341 private:
1342 size_t mReserved __unused; // end of reserved section
1343 size_t mCommitted __unused; // end of committed section
1344 size_t mUsed __unused; // end of used section
1345 size_t mFree __unused; // end of free section
1346 };
再来看这些 Block 的父类
/frameworks/av/media/codec2/core/include/C2Buffer.h
1120 /**
1121 * A 1D block.
1122 *
1123 * \note capacity() is not meaningful for users of blocks; instead size() is the capacity of the
1124 * usable portion. Use and offset() and size() if accessing the block directly through its handle
1125 * to represent the allotted range of the underlying allocation to this block.
1126 */
1127 class C2Block1D : public _C2LinearRangeAspect {
1128 public:
1129 /**
1130 * Returns the underlying handle for this allocation.
1131 *
1132 * \note that the block and its block pool has shared ownership of the handle
1133 * and if all references to the block are released, the underlying block
1134 * allocation may get reused even if a client keeps a clone of this handle.
1135 */
1136 const C2Handle *handle() const;
1137
1138 /**
1139 * Returns the allocator's ID that created the underlying allocation for this block. This
1140 * provides the context for understanding the handle.
1141 */
1142 C2Allocator::id_t getAllocatorId() const;
1143
1144 protected:
1145 class Impl;
1146 /** construct a block. */
1147 C2Block1D(std::shared_ptr<Impl> impl, const _C2LinearRangeAspect &range);
1148
1149 friend struct _C2BlockFactory;
1150 std::shared_ptr<Impl> mImpl;
1151 };
--------------------
1937 /**
1938 * A 2D block.
1939 *
1940 * \note width()/height() is not meaningful for users of blocks; instead, crop().width() and
1941 * crop().height() is the capacity of the usable portion. Use and crop() if accessing the block
1942 * directly through its handle to represent the allotted region of the underlying allocation to this
1943 * block.
1944 */
1945 class C2Block2D : public _C2PlanarSectionAspect {
1946 public:
1947 /**
1948 * Returns the underlying handle for this allocation.
1949 *
1950 * \note that the block and its block pool has shared ownership of the handle
1951 * and if all references to the block are released, the underlying block
1952 * allocation may get reused even if a client keeps a clone of this handle.
1953 */
1954 const C2Handle *handle() const;
1955
1956 /**
1957 * Returns the allocator's ID that created the underlying allocation for this block. This
1958 * provides the context for understanding the handle.
1959 */
1960 C2Allocator::id_t getAllocatorId() const;
1961
1962 protected:
1963 class Impl;
1964 C2Block2D(std::shared_ptr<Impl> impl, const _C2PlanarSectionAspect §ion);
1965
1966 friend struct _C2BlockFactory;
1967 std::shared_ptr<Impl> mImpl;
1968 };