1 /* 2 * Copyright (C) 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 #ifndef STAGEFRIGHT_CODEC2_PLATFORM_SUPPORT_H_ 18 #define STAGEFRIGHT_CODEC2_PLATFORM_SUPPORT_H_ 19 20 #include <C2Component.h> 21 #include <C2ComponentFactory.h> 22 23 #include <memory> 24 25 #include <android-base/unique_fd.h> 26 27 namespace aidl::android::hardware::media::c2 { 28 class IGraphicBufferAllocator; 29 } 30 31 namespace android { 32 33 /** 34 * Returns the platform allocator store. 35 * \retval nullptr if the platform allocator store could not be obtained 36 */ 37 std::shared_ptr<C2AllocatorStore> GetCodec2PlatformAllocatorStore(); 38 39 /** 40 * Platform allocator store IDs 41 */ 42 class C2PlatformAllocatorStore : public C2AllocatorStore { 43 public: 44 enum : id_t { 45 /** 46 * ID of the ion backed platform allocator. 47 * 48 * C2Handle consists of: 49 * fd shared ion buffer handle 50 * int size (lo 32 bits) 51 * int size (hi 32 bits) 52 * int magic '\xc2io\x00' 53 */ 54 ION = PLATFORM_START, 55 56 /* 57 * ID of the DMA-Buf Heap (ion replacement) backed platform allocator. 58 * 59 * C2Handle consists of: 60 * fd shared dmabuf buffer handle 61 * int size (lo 32 bits) 62 * int size (hi 32 bits) 63 * int magic '\xc2io\x00' 64 */ 65 DMABUFHEAP = ION, 66 67 /** 68 * ID of the gralloc backed platform allocator. 69 * 70 * C2Handle layout is not public. Use C2AllocatorGralloc::UnwrapNativeCodec2GrallocHandle 71 * to get the underlying gralloc handle from a C2Handle, and WrapNativeCodec2GrallocHandle 72 * to create a C2Handle from a gralloc handle - for C2Allocator::priorAllocation. 73 */ 74 GRALLOC, 75 76 /** 77 * ID of the bufferqueue backed platform allocator. 78 * 79 * C2Handle layout is not public. Use C2AllocatorGralloc::UnwrapNativeCodec2GrallocHandle 80 * to get the underlying handle from a C2Handle, and WrapNativeCodec2GrallocHandle 81 * to create a C2Handle from a handle - for C2Allocator::priorAllocation. 82 */ 83 BUFFERQUEUE, 84 85 /** 86 * ID of the gralloc backed platform allocator for linear blob buffer. 87 * 88 * C2Handle layout is not public. Use C2AllocatorGralloc::UnwrapNativeCodec2GrallocHandle 89 * to get the underlying gralloc handle from a C2Handle, and WrapNativeCodec2GrallocHandle 90 * to create a C2Handle from a gralloc handle - for C2Allocator::priorAllocation. 91 */ 92 BLOB, 93 94 /** 95 * ID of C2AIDL IGraphicBufferAllocator backed platform allocator. 96 * 97 * C2Handle layout is not public. Use C2AllocatorAhwb::UnwrapNativeCodec2AhwbHandle 98 * to get the underlying gralloc handle from a C2Handle, and WrapNativeCodec2AhwbHandle 99 * to create a C2Handle from a gralloc handle - for C2Allocator::priorAllocation. 100 */ 101 IGBA, 102 103 /** 104 * ID of indicating the end of platform allocator definition. 105 * 106 * \note always put this macro in the last place. 107 * 108 * Extended platform store plugin should use this macro as the start ID of its own allocator 109 * types. 110 */ 111 PLATFORM_END, 112 }; 113 }; 114 115 /** 116 * Retrieves a block pool for a component. 117 * 118 * \param id the local ID of the block pool 119 * \param component the component using the block pool (must be non-null) 120 * \param pool pointer to where the obtained block pool shall be stored on success. nullptr 121 * will be stored here on failure 122 * 123 * \retval C2_OK the operation was successful 124 * \retval C2_BAD_VALUE the component is null 125 * \retval C2_NOT_FOUND if the block pool does not exist 126 * \retval C2_NO_MEMORY not enough memory to fetch the block pool (this return value is only 127 * possible for basic pools) 128 * \retval C2_TIMED_OUT the operation timed out (this return value is only possible for basic pools) 129 * \retval C2_REFUSED no permission to complete any required allocation (this return value is only 130 * possible for basic pools) 131 * \retval C2_CORRUPTED some unknown, unrecoverable error occured during operation (unexpected, 132 * this return value is only possible for basic pools) 133 */ 134 c2_status_t GetCodec2BlockPool( 135 C2BlockPool::local_id_t id, std::shared_ptr<const C2Component> component, 136 std::shared_ptr<C2BlockPool> *pool); 137 138 /** 139 * Creates a block pool. 140 * \param allocatorId the allocator ID which is used to allocate blocks 141 * \param component the component using the block pool (must be non-null) 142 * \param pool pointer to where the created block pool shall be store on success. 143 * nullptr will be stored here on failure 144 * 145 * \retval C2_OK the operation was successful 146 * \retval C2_BAD_VALUE the component is null 147 * \retval C2_NOT_FOUND if the allocator does not exist 148 * \retval C2_NO_MEMORY not enough memory to create a block pool 149 */ 150 c2_status_t CreateCodec2BlockPool( 151 C2PlatformAllocatorStore::id_t allocatorId, 152 std::shared_ptr<const C2Component> component, 153 std::shared_ptr<C2BlockPool> *pool); 154 155 /** 156 * Creates a block pool. 157 * \param allocatorId the allocator ID which is used to allocate blocks 158 * \param components the components using the block pool 159 * \param pool pointer to where the created block pool shall be store on success. 160 * nullptr will be stored here on failure 161 * 162 * \retval C2_OK the operation was successful 163 * \retval C2_BAD_VALUE the component is null 164 * \retval C2_NOT_FOUND if the allocator does not exist 165 * \retval C2_NO_MEMORY not enough memory to create a block pool 166 */ 167 c2_status_t CreateCodec2BlockPool( 168 C2PlatformAllocatorStore::id_t allocatorId, 169 const std::vector<std::shared_ptr<const C2Component>> &components, 170 std::shared_ptr<C2BlockPool> *pool); 171 172 /** 173 * BlockPool creation parameters regarding allocator. 174 * 175 * igba, waitableFd are required only when allocatorId is 176 * C2PlatformAllocatorStore::IGBA. 177 */ 178 struct C2PlatformAllocatorDesc { 179 C2PlatformAllocatorStore::id_t allocatorId; 180 std::shared_ptr<::aidl::android::hardware::media::c2::IGraphicBufferAllocator> igba; 181 ::android::base::unique_fd waitableFd; // This will be passed and moved to C2Fence 182 // implementation. 183 }; 184 185 /** 186 * Creates a block pool. 187 * \param allocator allocator ID and parameters which are used to allocate blocks 188 * \param component the component using the block pool (must be non-null) 189 * \param pool pointer to where the created block pool shall be store on success. 190 * nullptr will be stored here on failure 191 * 192 * \retval C2_OK the operation was successful 193 * \retval C2_BAD_VALUE the component is null 194 * \retval C2_NOT_FOUND if the allocator does not exist 195 * \retval C2_NO_MEMORY not enough memory to create a block pool 196 */ 197 c2_status_t CreateCodec2BlockPool( 198 C2PlatformAllocatorDesc &allocator, 199 std::shared_ptr<const C2Component> component, 200 std::shared_ptr<C2BlockPool> *pool); 201 202 /** 203 * Creates a block pool. 204 * \param allocator allocator ID and parameters which are used to allocate blocks 205 * \param components the components using the block pool 206 * \param pool pointer to where the created block pool shall be store on success. 207 * nullptr will be stored here on failure 208 * 209 * \retval C2_OK the operation was successful 210 * \retval C2_BAD_VALUE the component is null 211 * \retval C2_NOT_FOUND if the allocator does not exist 212 * \retval C2_NO_MEMORY not enough memory to create a block pool 213 */ 214 c2_status_t CreateCodec2BlockPool( 215 C2PlatformAllocatorDesc &allocator, 216 const std::vector<std::shared_ptr<const C2Component>> &components, 217 std::shared_ptr<C2BlockPool> *pool); 218 219 /** 220 * Returns the platform component store. 221 * \retval nullptr if the platform component store could not be obtained 222 */ 223 std::shared_ptr<C2ComponentStore> GetCodec2PlatformComponentStore(); 224 225 /** 226 * Returns the platform component store. 227 * NOTE: For testing only 228 * \retval nullptr if the platform component store could not be obtained 229 */ 230 std::shared_ptr<C2ComponentStore> GetTestComponentStore( 231 std::vector<std::tuple<C2String, C2ComponentFactory::CreateCodec2FactoryFunc, 232 C2ComponentFactory::DestroyCodec2FactoryFunc>>); 233 234 /** 235 * Sets the preferred component store in this process for the sole purpose of accessing its 236 * interface. If this is not called, the default IComponentStore HAL (if exists) is the preferred 237 * store for this purpose. If the default IComponentStore HAL is not present, the platform 238 * component store is used. 239 */ 240 void SetPreferredCodec2ComponentStore(std::shared_ptr<C2ComponentStore> store); 241 242 /** 243 * Returns the pool mask. 244 * \retval the default pool mask should be adopted if it could not be obtained from property 245 * "debug.stagefright.c2-poolmask" 246 */ 247 int GetCodec2PoolMask(); 248 249 /** 250 * Returns the preferred linear buffer allocator id from param poolMask. 251 * C2PlatformAllocatorStore::ION should be chosen as fallback allocator if BLOB is not enabled from 252 * param poolMask. 253 */ 254 C2PlatformAllocatorStore::id_t GetPreferredLinearAllocatorId(int poolMask); 255 256 } // namespace android 257 258 #endif // STAGEFRIGHT_CODEC2_PLATFORM_SUPPORT_H_ 259