• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
12 #include <log/log.h>
13 
14 namespace android {
15 namespace {
16 const char* kLibPath = "libv4l2_codec2_vendor_allocator.so";
17 const char* kCreateAllocatorFuncName = "CreateAllocator";
18 }  // namespace
19 
20 // static
Create()21 std::unique_ptr<VendorAllocatorLoader> VendorAllocatorLoader::Create() {
22     ALOGV("%s()", __func__);
23 
24     void* libHandle = dlopen(kLibPath, RTLD_NOW | RTLD_NODELETE);
25     if (!libHandle) {
26         ALOGI("%s(): Failed to load library: %s", __func__, kLibPath);
27         return nullptr;
28     }
29 
30     auto createAllocatorFunc = (CreateAllocatorFunc)dlsym(libHandle, kCreateAllocatorFuncName);
31     if (!createAllocatorFunc) {
32         ALOGE("%s(): Failed to load functions: %s", __func__, kCreateAllocatorFuncName);
33         dlclose(libHandle);
34         return nullptr;
35     }
36 
37     return std::unique_ptr<VendorAllocatorLoader>(
38             new VendorAllocatorLoader(libHandle, createAllocatorFunc));
39 }
40 
VendorAllocatorLoader(void * libHandle,CreateAllocatorFunc createAllocatorFunc)41 VendorAllocatorLoader::VendorAllocatorLoader(void* libHandle,
42                                              CreateAllocatorFunc createAllocatorFunc)
43       : mLibHandle(libHandle), mCreateAllocatorFunc(createAllocatorFunc) {
44     ALOGV("%s()", __func__);
45 }
46 
~VendorAllocatorLoader()47 VendorAllocatorLoader::~VendorAllocatorLoader() {
48     ALOGV("%s()", __func__);
49 
50     dlclose(mLibHandle);
51 }
52 
createAllocator(C2Allocator::id_t allocatorId)53 C2Allocator* VendorAllocatorLoader::createAllocator(C2Allocator::id_t allocatorId) {
54     ALOGV("%s(%d)", __func__, allocatorId);
55 
56     return mCreateAllocatorFunc(allocatorId);
57 }
58 
59 }  // namespace android
60