• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)60 c2_status_t C2PlatformStorePluginLoader::createBlockPool(
61         ::C2Allocator::id_t allocatorId, ::C2BlockPool::local_id_t blockPoolId,
62         std::shared_ptr<C2BlockPool>* pool) {
63     if (mCreateBlockPool == nullptr) {
64         ALOGD("Handle or CreateBlockPool symbol is null");
65         return C2_NOT_FOUND;
66     }
67 
68     std::shared_ptr<::C2BlockPool> ptr(mCreateBlockPool(allocatorId, blockPoolId));
69     if (ptr) {
70         *pool = ptr;
71         return C2_OK;
72     }
73     ALOGD("Failed to CreateBlockPool by allocator id: %u", allocatorId);
74     return C2_BAD_INDEX;
75 }
76 
createAllocator(::C2Allocator::id_t allocatorId,std::shared_ptr<C2Allocator> * const allocator)77 c2_status_t C2PlatformStorePluginLoader::createAllocator(
78         ::C2Allocator::id_t allocatorId, std::shared_ptr<C2Allocator>* const allocator) {
79     if (mCreateAllocator == nullptr) {
80         ALOGD("Handle or CreateAllocator symbol is null");
81         return C2_NOT_FOUND;
82     }
83 
84     c2_status_t res = C2_CORRUPTED;
85     allocator->reset(mCreateAllocator(allocatorId, &res));
86     if (res != C2_OK) {
87         ALOGD("Failed to CreateAllocator by id: %u, res: %d", allocatorId, res);
88         allocator->reset();
89         return res;
90     }
91     return C2_OK;
92 }
93 
94 // static
GetInstance()95 const std::unique_ptr<C2PlatformStorePluginLoader>& C2PlatformStorePluginLoader::GetInstance() {
96     android::Mutex::Autolock _l(sMutex);
97     if (!sInstance) {
98         ALOGV("Loading library");
99         sInstance.reset(new C2PlatformStorePluginLoader(kStorePluginPath));
100     }
101     return sInstance;
102 }
103