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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "C2PlatformStorePluginLoader"
19 
20 #include <dlfcn.h>
21 
22 #include <utils/Log.h>
23 
24 #include "C2PlatformStorePluginLoader.h"
25 
26 /* static */ android::Mutex C2PlatformStorePluginLoader::sMutex;
27 /* static */ std::unique_ptr<C2PlatformStorePluginLoader> C2PlatformStorePluginLoader::sInstance;
28 
C2PlatformStorePluginLoader(const char * libPath)29 C2PlatformStorePluginLoader::C2PlatformStorePluginLoader(const char *libPath)
30     : mCreateBlockPool(nullptr) {
31     mLibHandle = dlopen(libPath, RTLD_NOW | RTLD_NODELETE);
32     if (mLibHandle == nullptr) {
33         ALOGD("Failed to load library: %s (%s)", libPath, dlerror());
34         return;
35     }
36     mCreateBlockPool = (CreateBlockPoolFunc)dlsym(mLibHandle, "CreateBlockPool");
37     if (mCreateBlockPool == nullptr) {
38         ALOGD("Failed to find symbol: CreateBlockPool (%s)", dlerror());
39     }
40     mCreateAllocator = (CreateAllocatorFunc)dlsym(mLibHandle, "CreateAllocator");
41     if (mCreateAllocator == nullptr) {
42         ALOGD("Failed to find symbol: CreateAllocator (%s)", dlerror());
43     }
44 }
45 
~C2PlatformStorePluginLoader()46 C2PlatformStorePluginLoader::~C2PlatformStorePluginLoader() {
47     if (mLibHandle != nullptr) {
48         ALOGV("Closing handle");
49         dlclose(mLibHandle);
50     }
51 }
52 
createBlockPool(::C2Allocator::id_t allocatorId,::C2BlockPool::local_id_t blockPoolId,std::shared_ptr<C2BlockPool> * pool)53 c2_status_t C2PlatformStorePluginLoader::createBlockPool(
54         ::C2Allocator::id_t allocatorId, ::C2BlockPool::local_id_t blockPoolId,
55         std::shared_ptr<C2BlockPool>* pool) {
56     if (mCreateBlockPool == nullptr) {
57         ALOGD("Handle or CreateBlockPool symbol is null");
58         return C2_NOT_FOUND;
59     }
60 
61     std::shared_ptr<::C2BlockPool> ptr(mCreateBlockPool(allocatorId, blockPoolId));
62     if (ptr) {
63         *pool = ptr;
64         return C2_OK;
65     }
66     ALOGD("Failed to CreateBlockPool by allocator id: %u", allocatorId);
67     return C2_BAD_INDEX;
68 }
69 
createAllocator(::C2Allocator::id_t allocatorId,std::shared_ptr<C2Allocator> * const allocator)70 c2_status_t C2PlatformStorePluginLoader::createAllocator(
71         ::C2Allocator::id_t allocatorId, std::shared_ptr<C2Allocator>* const allocator) {
72     if (mCreateAllocator == nullptr) {
73         ALOGD("Handle or CreateAllocator symbol is null");
74         return C2_NOT_FOUND;
75     }
76 
77     c2_status_t res = C2_CORRUPTED;
78     allocator->reset(mCreateAllocator(allocatorId, &res));
79     if (res != C2_OK) {
80         ALOGD("Failed to CreateAllocator by id: %u, res: %d", allocatorId, res);
81         allocator->reset();
82         return res;
83     }
84     return C2_OK;
85 }
86 
87 // static
GetInstance()88 const std::unique_ptr<C2PlatformStorePluginLoader>& C2PlatformStorePluginLoader::GetInstance() {
89     android::Mutex::Autolock _l(sMutex);
90     if (!sInstance) {
91         ALOGV("Loading library");
92         sInstance.reset(new C2PlatformStorePluginLoader("libstagefright_ccodec_ext.so"));
93     }
94     return sInstance;
95 }
96