1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/android/android_hardware_buffer_compat.h"
9 #include "base/android/build_info.h"
10 #include "base/check.h"
14 AndroidHardwareBufferCompat::AndroidHardwareBufferCompat() {
15 DCHECK(IsSupportAvailable());
17 // TODO(https://crbug.com/1382595): If the Chromium build requires
18 // __ANDROID_API__ >= 26 at some point in the future, we could directly use
19 // the global functions instead of dynamic loading. However, since this would
20 // be incompatible with pre-Oreo devices, this is unlikely to happen in the
21 // foreseeable future, so just unconditionally use dynamic loading.
23 // cf. base/android/linker/linker_jni.cc
24 void* main_dl_handle = dlopen(nullptr, RTLD_NOW);
26 *reinterpret_cast<void**>(&allocate_) =
27 dlsym(main_dl_handle, "AHardwareBuffer_allocate");
30 *reinterpret_cast<void**>(&acquire_) =
31 dlsym(main_dl_handle, "AHardwareBuffer_acquire");
34 *reinterpret_cast<void**>(&describe_) =
35 dlsym(main_dl_handle, "AHardwareBuffer_describe");
38 *reinterpret_cast<void**>(&lock_) =
39 dlsym(main_dl_handle, "AHardwareBuffer_lock");
42 *reinterpret_cast<void**>(&recv_handle_) =
43 dlsym(main_dl_handle, "AHardwareBuffer_recvHandleFromUnixSocket");
46 *reinterpret_cast<void**>(&release_) =
47 dlsym(main_dl_handle, "AHardwareBuffer_release");
50 *reinterpret_cast<void**>(&send_handle_) =
51 dlsym(main_dl_handle, "AHardwareBuffer_sendHandleToUnixSocket");
54 *reinterpret_cast<void**>(&unlock_) =
55 dlsym(main_dl_handle, "AHardwareBuffer_unlock");
60 bool AndroidHardwareBufferCompat::IsSupportAvailable() {
61 return base::android::BuildInfo::GetInstance()->sdk_int() >=
62 base::android::SDK_VERSION_OREO;
66 AndroidHardwareBufferCompat& AndroidHardwareBufferCompat::GetInstance() {
67 static AndroidHardwareBufferCompat compat;
71 void AndroidHardwareBufferCompat::Allocate(const AHardwareBuffer_Desc* desc,
72 AHardwareBuffer** out_buffer) {
73 DCHECK(IsSupportAvailable());
74 allocate_(desc, out_buffer);
77 void AndroidHardwareBufferCompat::Acquire(AHardwareBuffer* buffer) {
78 DCHECK(IsSupportAvailable());
80 // Null |buffer| is not allowed by |acquire_| and it fails somewhere in
81 // android framework code. Hence adding a DCHECK here for documenting this
82 // info and fail before.
87 void AndroidHardwareBufferCompat::Describe(const AHardwareBuffer* buffer,
88 AHardwareBuffer_Desc* out_desc) {
89 DCHECK(IsSupportAvailable());
90 describe_(buffer, out_desc);
93 int AndroidHardwareBufferCompat::Lock(AHardwareBuffer* buffer,
97 void** out_virtual_address) {
98 DCHECK(IsSupportAvailable());
99 return lock_(buffer, usage, fence, rect, out_virtual_address);
102 int AndroidHardwareBufferCompat::RecvHandleFromUnixSocket(
104 AHardwareBuffer** out_buffer) {
105 DCHECK(IsSupportAvailable());
106 return recv_handle_(socket_fd, out_buffer);
109 void AndroidHardwareBufferCompat::Release(AHardwareBuffer* buffer) {
110 DCHECK(IsSupportAvailable());
114 int AndroidHardwareBufferCompat::SendHandleToUnixSocket(
115 const AHardwareBuffer* buffer,
117 DCHECK(IsSupportAvailable());
118 return send_handle_(buffer, socket_fd);
121 int AndroidHardwareBufferCompat::Unlock(AHardwareBuffer* buffer,
123 DCHECK(IsSupportAvailable());
124 return unlock_(buffer, fence);