android_hardware_buffer_compat.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2017 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. #include "base/android/android_hardware_buffer_compat.h"
  5. #include <dlfcn.h>
  6. #include "base/android/build_info.h"
  7. #include "base/check.h"
  8. namespace base {
  9. AndroidHardwareBufferCompat::AndroidHardwareBufferCompat() {
  10. DCHECK(IsSupportAvailable());
  11. // TODO(klausw): If the Chromium build requires __ANDROID_API__ >= 26 at some
  12. // point in the future, we could directly use the global functions instead of
  13. // dynamic loading. However, since this would be incompatible with pre-Oreo
  14. // devices, this is unlikely to happen in the foreseeable future, so just
  15. // unconditionally use dynamic loading.
  16. // cf. base/android/linker/modern_linker_jni.cc
  17. void* main_dl_handle = dlopen(nullptr, RTLD_NOW);
  18. *reinterpret_cast<void**>(&allocate_) =
  19. dlsym(main_dl_handle, "AHardwareBuffer_allocate");
  20. DCHECK(allocate_);
  21. *reinterpret_cast<void**>(&acquire_) =
  22. dlsym(main_dl_handle, "AHardwareBuffer_acquire");
  23. DCHECK(acquire_);
  24. *reinterpret_cast<void**>(&describe_) =
  25. dlsym(main_dl_handle, "AHardwareBuffer_describe");
  26. DCHECK(describe_);
  27. *reinterpret_cast<void**>(&lock_) =
  28. dlsym(main_dl_handle, "AHardwareBuffer_lock");
  29. DCHECK(lock_);
  30. *reinterpret_cast<void**>(&recv_handle_) =
  31. dlsym(main_dl_handle, "AHardwareBuffer_recvHandleFromUnixSocket");
  32. DCHECK(recv_handle_);
  33. *reinterpret_cast<void**>(&release_) =
  34. dlsym(main_dl_handle, "AHardwareBuffer_release");
  35. DCHECK(release_);
  36. *reinterpret_cast<void**>(&send_handle_) =
  37. dlsym(main_dl_handle, "AHardwareBuffer_sendHandleToUnixSocket");
  38. DCHECK(send_handle_);
  39. *reinterpret_cast<void**>(&unlock_) =
  40. dlsym(main_dl_handle, "AHardwareBuffer_unlock");
  41. DCHECK(unlock_);
  42. }
  43. // static
  44. bool AndroidHardwareBufferCompat::IsSupportAvailable() {
  45. return base::android::BuildInfo::GetInstance()->sdk_int() >=
  46. base::android::SDK_VERSION_OREO;
  47. }
  48. // static
  49. AndroidHardwareBufferCompat& AndroidHardwareBufferCompat::GetInstance() {
  50. static AndroidHardwareBufferCompat compat;
  51. return compat;
  52. }
  53. void AndroidHardwareBufferCompat::Allocate(const AHardwareBuffer_Desc* desc,
  54. AHardwareBuffer** out_buffer) {
  55. DCHECK(IsSupportAvailable());
  56. allocate_(desc, out_buffer);
  57. }
  58. void AndroidHardwareBufferCompat::Acquire(AHardwareBuffer* buffer) {
  59. DCHECK(IsSupportAvailable());
  60. // Null |buffer| is not allowed by |acquire_| and it fails somewhere in
  61. // android framework code. Hence adding a DCHECK here for documenting this
  62. // info and fail before.
  63. DCHECK(buffer);
  64. acquire_(buffer);
  65. }
  66. void AndroidHardwareBufferCompat::Describe(const AHardwareBuffer* buffer,
  67. AHardwareBuffer_Desc* out_desc) {
  68. DCHECK(IsSupportAvailable());
  69. describe_(buffer, out_desc);
  70. }
  71. int AndroidHardwareBufferCompat::Lock(AHardwareBuffer* buffer,
  72. uint64_t usage,
  73. int32_t fence,
  74. const ARect* rect,
  75. void** out_virtual_address) {
  76. DCHECK(IsSupportAvailable());
  77. return lock_(buffer, usage, fence, rect, out_virtual_address);
  78. }
  79. int AndroidHardwareBufferCompat::RecvHandleFromUnixSocket(
  80. int socket_fd,
  81. AHardwareBuffer** out_buffer) {
  82. DCHECK(IsSupportAvailable());
  83. return recv_handle_(socket_fd, out_buffer);
  84. }
  85. void AndroidHardwareBufferCompat::Release(AHardwareBuffer* buffer) {
  86. DCHECK(IsSupportAvailable());
  87. release_(buffer);
  88. }
  89. int AndroidHardwareBufferCompat::SendHandleToUnixSocket(
  90. const AHardwareBuffer* buffer,
  91. int socket_fd) {
  92. DCHECK(IsSupportAvailable());
  93. return send_handle_(buffer, socket_fd);
  94. }
  95. int AndroidHardwareBufferCompat::Unlock(AHardwareBuffer* buffer,
  96. int32_t* fence) {
  97. DCHECK(IsSupportAvailable());
  98. return unlock_(buffer, fence);
  99. }
  100. } // namespace base