1 //
2 // Copyright 2021 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 #include "libANGLE/renderer/vulkan/android/AHBFunctions.h"
8
9 #include "common/debug.h"
10
11 #include <dlfcn.h>
12
13 namespace rx
14 {
15
16 namespace
17 {
18
19 template <class T>
AssignFn(void * handle,const char * name,T & fn)20 void AssignFn(void *handle, const char *name, T &fn)
21 {
22 fn = reinterpret_cast<T>(dlsym(handle, name));
23 }
24
25 constexpr char kNativeWindowLibraryName[] = "libnativewindow.so";
26 constexpr char kAhbAcquireFunctionName[] = "AHardwareBuffer_acquire";
27 constexpr char kAhbDescribeFunctionName[] = "AHardwareBuffer_describe";
28 constexpr char kAhbReleaseFunctionName[] = "AHardwareBuffer_release";
29
30 } // namespace
31
AHBFunctions()32 AHBFunctions::AHBFunctions() : mLibNativeWindowHandle(nullptr)
33 {
34 void *handle = dlopen(nullptr, RTLD_NOW);
35 getAhbProcAddresses(handle);
36
37 // Some services load "libnativewindow.so" with RTLD_LOCAL flag resulting in AHB function
38 // symbols being unresolvable through dlsym. Account for such cases and explicitly dlopen the
39 // library.
40 if (!valid())
41 {
42 mLibNativeWindowHandle = dlopen(kNativeWindowLibraryName, RTLD_NOW);
43 ASSERT(mLibNativeWindowHandle);
44 getAhbProcAddresses(mLibNativeWindowHandle);
45 }
46 }
47
~AHBFunctions()48 AHBFunctions::~AHBFunctions()
49 {
50 if (mLibNativeWindowHandle)
51 {
52 dlclose(mLibNativeWindowHandle);
53 }
54 }
55
getAhbProcAddresses(void * handle)56 void AHBFunctions::getAhbProcAddresses(void *handle)
57 {
58 AssignFn(handle, kAhbAcquireFunctionName, mAcquireFn);
59 AssignFn(handle, kAhbDescribeFunctionName, mDescribeFn);
60 AssignFn(handle, kAhbReleaseFunctionName, mReleaseFn);
61 }
62
63 } // namespace rx
64