• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     bool blockFenceSupport;
184 
C2PlatformAllocatorDescC2PlatformAllocatorDesc185     C2PlatformAllocatorDesc()
186             : allocatorId(C2AllocatorStore::DEFAULT_LINEAR), blockFenceSupport(false) {}
187 };
188 
189 /**
190  * Creates a block pool.
191  * \param allocator     allocator ID and parameters which are used to allocate blocks
192  * \param component     the component using the block pool (must be non-null)
193  * \param pool          pointer to where the created block pool shall be store on success.
194  *                      nullptr will be stored here on failure
195  *
196  * \retval C2_OK        the operation was successful
197  * \retval C2_BAD_VALUE the component is null
198  * \retval C2_NOT_FOUND if the allocator does not exist
199  * \retval C2_NO_MEMORY not enough memory to create a block pool
200  */
201 c2_status_t CreateCodec2BlockPool(
202         C2PlatformAllocatorDesc &allocator,
203         std::shared_ptr<const C2Component> component,
204         std::shared_ptr<C2BlockPool> *pool);
205 
206 /**
207  * Creates a block pool.
208  * \param allocator     allocator ID and parameters which are used to allocate blocks
209  * \param components    the components using the block pool
210  * \param pool          pointer to where the created block pool shall be store on success.
211  *                      nullptr will be stored here on failure
212  *
213  * \retval C2_OK        the operation was successful
214  * \retval C2_BAD_VALUE the component is null
215  * \retval C2_NOT_FOUND if the allocator does not exist
216  * \retval C2_NO_MEMORY not enough memory to create a block pool
217  */
218 c2_status_t CreateCodec2BlockPool(
219         C2PlatformAllocatorDesc &allocator,
220         const std::vector<std::shared_ptr<const C2Component>> &components,
221         std::shared_ptr<C2BlockPool> *pool);
222 
223 /**
224  * Returns the platform component store.
225  * \retval nullptr if the platform component store could not be obtained
226  */
227 std::shared_ptr<C2ComponentStore> GetCodec2PlatformComponentStore();
228 
229 /**
230  * Returns the platform component store.
231  * NOTE: For testing only
232  * \retval nullptr if the platform component store could not be obtained
233  */
234 std::shared_ptr<C2ComponentStore> GetTestComponentStore(
235         std::vector<std::tuple<C2String, C2ComponentFactory::CreateCodec2FactoryFunc,
236         C2ComponentFactory::DestroyCodec2FactoryFunc>>);
237 
238 /**
239  * Sets the preferred component store in this process for the sole purpose of accessing its
240  * interface. If this is not called, the default IComponentStore HAL (if exists) is the preferred
241  * store for this purpose. If the default IComponentStore HAL is not present, the platform
242  * component store is used.
243  */
244 void SetPreferredCodec2ComponentStore(std::shared_ptr<C2ComponentStore> store);
245 
246 /**
247  * Returns the pool mask.
248  * \retval the default pool mask should be adopted if it could not be obtained from property
249  *         "debug.stagefright.c2-poolmask"
250  */
251 int GetCodec2PoolMask();
252 
253 /**
254  * Returns the preferred linear buffer allocator id from param poolMask.
255  * C2PlatformAllocatorStore::ION should be chosen as fallback allocator if BLOB is not enabled from
256  * param poolMask.
257  */
258 C2PlatformAllocatorStore::id_t GetPreferredLinearAllocatorId(int poolMask);
259 
260 } // namespace android
261 
262 #endif // STAGEFRIGHT_CODEC2_PLATFORM_SUPPORT_H_
263