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 }
41
~C2PlatformStorePluginLoader()42 C2PlatformStorePluginLoader::~C2PlatformStorePluginLoader() {
43 if (mLibHandle != nullptr) {
44 ALOGV("Closing handle");
45 dlclose(mLibHandle);
46 }
47 }
48
createBlockPool(::C2Allocator::id_t allocatorId,::C2BlockPool::local_id_t blockPoolId,std::shared_ptr<C2BlockPool> * pool)49 c2_status_t C2PlatformStorePluginLoader::createBlockPool(
50 ::C2Allocator::id_t allocatorId, ::C2BlockPool::local_id_t blockPoolId,
51 std::shared_ptr<C2BlockPool>* pool) {
52 if (mCreateBlockPool == nullptr) {
53 ALOGD("Handle or CreateBlockPool symbol is null");
54 return C2_NOT_FOUND;
55 }
56
57 std::shared_ptr<::C2BlockPool> ptr(mCreateBlockPool(allocatorId, blockPoolId));
58 if (ptr) {
59 *pool = ptr;
60 return C2_OK;
61 }
62 ALOGD("Failed to CreateBlockPool by allocator id: %u", allocatorId);
63 return C2_BAD_INDEX;
64 }
65
66 // static
GetInstance()67 const std::unique_ptr<C2PlatformStorePluginLoader>& C2PlatformStorePluginLoader::GetInstance() {
68 android::Mutex::Autolock _l(sMutex);
69 if (!sInstance) {
70 ALOGV("Loading library");
71 sInstance.reset(new C2PlatformStorePluginLoader("libstagefright_ccodec_ext.so"));
72 }
73 return sInstance;
74 }
75