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