1 /*
2  * Copyright 2018, 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_C2_PLATFORM_STORE_PLUGIN_LOADER_H_
18 
19 #define STAGEFRIGHT_C2_PLATFORM_STORE_PLUGIN_LOADER_H_
20 
21 #include <memory>
22 
23 #include <utils/Mutex.h>
24 
25 #include <C2.h>
26 #include <C2Buffer.h>
27 
28 /**
29  * Extern C interface for creating block pool from platform store extension.
30  *
31  * \param alloctorId  the ID of the backing allocator type.
32  * \parem blockPoolId the ID of created block pool.
33  *
34  * \return pointer of created C2BlockPool, nullptr on error.
35  */
36 typedef ::C2BlockPool* (*CreateBlockPoolFunc)(::C2Allocator::id_t, ::C2BlockPool::local_id_t);
37 
38 /**
39  * Extern C interface for creating allocator from platform store extension.
40  *
41  * \param alloctorId  the ID of the allocator to create.
42  * \param status      the returned status from creating allocator.
43  *
44  * \return pointer of created C2Allocator, nullptr on error.
45  */
46 typedef ::C2Allocator* (*CreateAllocatorFunc)(::C2Allocator::id_t, ::c2_status_t*);
47 
48 class C2PlatformStorePluginLoader {
49 public:
50     static const std::unique_ptr<C2PlatformStorePluginLoader>& GetInstance();
51     ~C2PlatformStorePluginLoader();
52 
53     /**
54      * Creates block pool from platform store extension.
55      *
56      * \param alloctorId  the ID of the backing allocator type.
57      * \param blockPoolId the ID of created block pool.
58      * \param pool        shared pointer where the created block pool is stored.
59      *
60      * \retval C2_OK        the block pool was created successfully.
61      * \retval C2_NOT_FOUND the extension symbol was not found.
62      * \retval C2_BAD_INDEX the input allocatorId is not defined in platform store extension.
63      */
64     c2_status_t createBlockPool(::C2Allocator::id_t allocatorId,
65                                 ::C2BlockPool::local_id_t blockPoolId,
66                                 std::shared_ptr<C2BlockPool>* pool);
67 
68     /**
69      * Creates allocator from platform store extension.
70      *
71      * Note that this allocator is not created as shared singleton as C2AllocatorStore does, because
72      * C interface only allows raw pointer transmission for extension.
73      *
74      * \param alloctorId  the ID of the allocator to create.
75      * \param allocator   shared pointer where the created allocator is stored.
76      *
77      * \retval C2_OK        the allocator was created successfully.
78      * \retval C2_TIMED_OUT could not create the allocator within the time limit (unexpected)
79      * \retval C2_CORRUPTED some unknown error prevented the creation of the allocator (unexpected)
80      * \retval C2_NOT_FOUND the extension symbol was not found.
81      * \retval C2_BAD_INDEX the input allocatorId is not defined in platform store extension.
82      * \retval C2_NO_MEMORY not enough memory to create the allocator
83      */
84     c2_status_t createAllocator(::C2Allocator::id_t allocatorId,
85                                 std::shared_ptr<C2Allocator>* const allocator);
86 
87 private:
88     explicit C2PlatformStorePluginLoader(const char *libPath);
89 
90     static android::Mutex sMutex;
91     static std::unique_ptr<C2PlatformStorePluginLoader> sInstance;
92 
93     void *mLibHandle;
94     CreateBlockPoolFunc mCreateBlockPool;
95     CreateAllocatorFunc mCreateAllocator;
96 };
97 
98 #endif  // STAGEFRIGHT_C2_PLATFORM_STORE_PLUGIN_LOADER_H_
99