1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 //#define LOG_NDEBUG 0
6 #define LOG_TAG "VendorAllocatorLoader"
7
8 #include <v4l2_codec2/plugin_store/VendorAllocatorLoader.h>
9
10 #include <dlfcn.h>
11 #include <cinttypes>
12
13 #include <log/log.h>
14
15 namespace android {
16 namespace {
17 const char* kLibPath = "libv4l2_codec2_vendor_allocator.so";
18 const char* kCreateAllocatorFuncName = "CreateAllocator";
19 const char* kCreateBlockPoolFuncName = "CreateBlockPool";
20 } // namespace
21
22 // static
Create()23 std::unique_ptr<VendorAllocatorLoader> VendorAllocatorLoader::Create() {
24 ALOGV("%s()", __func__);
25
26 void* libHandle = dlopen(kLibPath, RTLD_NOW | RTLD_NODELETE);
27 if (!libHandle) {
28 ALOGI("%s(): Failed to load library: %s", __func__, kLibPath);
29 return nullptr;
30 }
31
32 auto createAllocatorFunc = (CreateAllocatorFunc)dlsym(libHandle, kCreateAllocatorFuncName);
33 if (!createAllocatorFunc) {
34 ALOGW("%s(): Failed to load functions: %s", __func__, kCreateAllocatorFuncName);
35 }
36
37 auto crateBlockPoolFunc = (CreateBlockPoolFunc)dlsym(libHandle, kCreateBlockPoolFuncName);
38 if (!crateBlockPoolFunc) {
39 ALOGW("%s(): Failed to load functions: %s", __func__, kCreateAllocatorFuncName);
40 }
41
42 return std::unique_ptr<VendorAllocatorLoader>(
43 new VendorAllocatorLoader(libHandle, createAllocatorFunc, crateBlockPoolFunc));
44 }
45
VendorAllocatorLoader(void * libHandle,CreateAllocatorFunc createAllocatorFunc,CreateBlockPoolFunc createBlockPoolFunc)46 VendorAllocatorLoader::VendorAllocatorLoader(void* libHandle,
47 CreateAllocatorFunc createAllocatorFunc,
48 CreateBlockPoolFunc createBlockPoolFunc)
49 : mLibHandle(libHandle),
50 mCreateAllocatorFunc(createAllocatorFunc),
51 mCreateBlockPoolFunc(createBlockPoolFunc) {
52 ALOGV("%s()", __func__);
53 }
54
~VendorAllocatorLoader()55 VendorAllocatorLoader::~VendorAllocatorLoader() {
56 ALOGV("%s()", __func__);
57
58 dlclose(mLibHandle);
59 }
60
createAllocator(C2Allocator::id_t allocatorId)61 C2Allocator* VendorAllocatorLoader::createAllocator(C2Allocator::id_t allocatorId) {
62 ALOGV("%s(%d)", __func__, allocatorId);
63
64 if (!mCreateAllocatorFunc) {
65 return nullptr;
66 }
67
68 return mCreateAllocatorFunc(allocatorId);
69 }
70
createBlockPool(C2Allocator::id_t allocatorId,C2BlockPool::local_id_t poolId)71 C2BlockPool* VendorAllocatorLoader::createBlockPool(C2Allocator::id_t allocatorId,
72 C2BlockPool::local_id_t poolId) {
73 ALOGV("%s(allocatorId=%d, poolId=%" PRIu64 " )", __func__, allocatorId, poolId);
74
75 if (!mCreateBlockPoolFunc) {
76 return nullptr;
77 }
78
79 return mCreateBlockPoolFunc(allocatorId, poolId);
80 }
81
82 } // namespace android
83