Fix emulator build error
[platform/framework/web/chromium-efl.git] / base / android / android_hardware_buffer_compat.cc
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.
4
5 #include "base/android/android_hardware_buffer_compat.h"
6
7 #include <dlfcn.h>
8
9 #include "base/android/build_info.h"
10 #include "base/check.h"
11
12 namespace base {
13
14 AndroidHardwareBufferCompat::AndroidHardwareBufferCompat() {
15   DCHECK(IsSupportAvailable());
16
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.
22
23   // cf. base/android/linker/linker_jni.cc
24   void* main_dl_handle = dlopen(nullptr, RTLD_NOW);
25
26   *reinterpret_cast<void**>(&allocate_) =
27       dlsym(main_dl_handle, "AHardwareBuffer_allocate");
28   DCHECK(allocate_);
29
30   *reinterpret_cast<void**>(&acquire_) =
31       dlsym(main_dl_handle, "AHardwareBuffer_acquire");
32   DCHECK(acquire_);
33
34   *reinterpret_cast<void**>(&describe_) =
35       dlsym(main_dl_handle, "AHardwareBuffer_describe");
36   DCHECK(describe_);
37
38   *reinterpret_cast<void**>(&lock_) =
39       dlsym(main_dl_handle, "AHardwareBuffer_lock");
40   DCHECK(lock_);
41
42   *reinterpret_cast<void**>(&recv_handle_) =
43       dlsym(main_dl_handle, "AHardwareBuffer_recvHandleFromUnixSocket");
44   DCHECK(recv_handle_);
45
46   *reinterpret_cast<void**>(&release_) =
47       dlsym(main_dl_handle, "AHardwareBuffer_release");
48   DCHECK(release_);
49
50   *reinterpret_cast<void**>(&send_handle_) =
51       dlsym(main_dl_handle, "AHardwareBuffer_sendHandleToUnixSocket");
52   DCHECK(send_handle_);
53
54   *reinterpret_cast<void**>(&unlock_) =
55       dlsym(main_dl_handle, "AHardwareBuffer_unlock");
56   DCHECK(unlock_);
57 }
58
59 // static
60 bool AndroidHardwareBufferCompat::IsSupportAvailable() {
61   return base::android::BuildInfo::GetInstance()->sdk_int() >=
62          base::android::SDK_VERSION_OREO;
63 }
64
65 // static
66 AndroidHardwareBufferCompat& AndroidHardwareBufferCompat::GetInstance() {
67   static AndroidHardwareBufferCompat compat;
68   return compat;
69 }
70
71 void AndroidHardwareBufferCompat::Allocate(const AHardwareBuffer_Desc* desc,
72                                            AHardwareBuffer** out_buffer) {
73   DCHECK(IsSupportAvailable());
74   allocate_(desc, out_buffer);
75 }
76
77 void AndroidHardwareBufferCompat::Acquire(AHardwareBuffer* buffer) {
78   DCHECK(IsSupportAvailable());
79
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.
83   DCHECK(buffer);
84   acquire_(buffer);
85 }
86
87 void AndroidHardwareBufferCompat::Describe(const AHardwareBuffer* buffer,
88                                            AHardwareBuffer_Desc* out_desc) {
89   DCHECK(IsSupportAvailable());
90   describe_(buffer, out_desc);
91 }
92
93 int AndroidHardwareBufferCompat::Lock(AHardwareBuffer* buffer,
94                                       uint64_t usage,
95                                       int32_t fence,
96                                       const ARect* rect,
97                                       void** out_virtual_address) {
98   DCHECK(IsSupportAvailable());
99   return lock_(buffer, usage, fence, rect, out_virtual_address);
100 }
101
102 int AndroidHardwareBufferCompat::RecvHandleFromUnixSocket(
103     int socket_fd,
104     AHardwareBuffer** out_buffer) {
105   DCHECK(IsSupportAvailable());
106   return recv_handle_(socket_fd, out_buffer);
107 }
108
109 void AndroidHardwareBufferCompat::Release(AHardwareBuffer* buffer) {
110   DCHECK(IsSupportAvailable());
111   release_(buffer);
112 }
113
114 int AndroidHardwareBufferCompat::SendHandleToUnixSocket(
115     const AHardwareBuffer* buffer,
116     int socket_fd) {
117   DCHECK(IsSupportAvailable());
118   return send_handle_(buffer, socket_fd);
119 }
120
121 int AndroidHardwareBufferCompat::Unlock(AHardwareBuffer* buffer,
122                                         int32_t* fence) {
123   DCHECK(IsSupportAvailable());
124   return unlock_(buffer, fence);
125 }
126
127 }  // namespace base