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