android_image_reader_compat.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2018 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. #ifndef BASE_ANDROID_ANDROID_IMAGE_READER_COMPAT_H_
  5. #define BASE_ANDROID_ANDROID_IMAGE_READER_COMPAT_H_
  6. #include "base/android/android_image_reader_abi.h"
  7. #include "base/base_export.h"
  8. #include "base/macros.h"
  9. #include "base/no_destructor.h"
  10. namespace base {
  11. namespace android {
  12. // This class provides runtime support for working with AImage, AImageReader and
  13. // ANativeWindow objects on Android O systems without requiring building for the
  14. // Android O NDK level. Don't call GetInstance() unless IsSupported() returns
  15. // true.
  16. class BASE_EXPORT AndroidImageReader {
  17. public:
  18. // Thread safe GetInstance.
  19. static AndroidImageReader& GetInstance();
  20. // Check if the image reader usage is supported. This function returns TRUE
  21. // if android version is >=OREO, image reader support is not disabled and all
  22. // the required functions are loaded.
  23. bool IsSupported();
  24. // Some devices do not support more than 1 image to be acquired from the
  25. // AImageReader.(crbug.com/1051705). This method returns true for those
  26. // devices.
  27. static bool LimitAImageReaderMaxSizeToOne();
  28. // Naming convention of all the below functions are chosen to exactly match
  29. // the function names in the NDK.
  30. void AImage_delete(AImage* image);
  31. void AImage_deleteAsync(AImage* image, int releaseFenceFd);
  32. media_status_t AImage_getHardwareBuffer(const AImage* image,
  33. AHardwareBuffer** buffer);
  34. media_status_t AImage_getWidth(const AImage* image, int32_t* width);
  35. media_status_t AImage_getHeight(const AImage* image, int32_t* height);
  36. media_status_t AImage_getCropRect(const AImage* image, AImageCropRect* rect);
  37. media_status_t AImageReader_newWithUsage(int32_t width,
  38. int32_t height,
  39. int32_t format,
  40. uint64_t usage,
  41. int32_t maxImages,
  42. AImageReader** reader);
  43. media_status_t AImageReader_setImageListener(
  44. AImageReader* reader,
  45. AImageReader_ImageListener* listener);
  46. void AImageReader_delete(AImageReader* reader);
  47. media_status_t AImageReader_getFormat(const AImageReader* reader,
  48. int32_t* format);
  49. media_status_t AImageReader_getWindow(AImageReader* reader,
  50. ANativeWindow** window);
  51. media_status_t AImageReader_acquireLatestImageAsync(AImageReader* reader,
  52. AImage** image,
  53. int* acquireFenceFd);
  54. media_status_t AImageReader_acquireNextImageAsync(AImageReader* reader,
  55. AImage** image,
  56. int* acquireFenceFd);
  57. jobject ANativeWindow_toSurface(JNIEnv* env, ANativeWindow* window);
  58. private:
  59. friend class base::NoDestructor<AndroidImageReader>;
  60. AndroidImageReader();
  61. bool LoadFunctions();
  62. const bool is_supported_;
  63. pAImage_delete AImage_delete_;
  64. pAImage_deleteAsync AImage_deleteAsync_;
  65. pAImage_getHardwareBuffer AImage_getHardwareBuffer_;
  66. pAImage_getWidth AImage_getWidth_;
  67. pAImage_getHeight AImage_getHeight_;
  68. pAImage_getCropRect AImage_getCropRect_;
  69. pAImageReader_newWithUsage AImageReader_newWithUsage_;
  70. pAImageReader_setImageListener AImageReader_setImageListener_;
  71. pAImageReader_delete AImageReader_delete_;
  72. pAImageReader_getFormat AImageReader_getFormat_;
  73. pAImageReader_getWindow AImageReader_getWindow_;
  74. pAImageReader_acquireLatestImageAsync AImageReader_acquireLatestImageAsync_;
  75. pAImageReader_acquireNextImageAsync AImageReader_acquireNextImageAsync_;
  76. pANativeWindow_toSurface ANativeWindow_toSurface_;
  77. DISALLOW_COPY_AND_ASSIGN(AndroidImageReader);
  78. };
  79. } // namespace android
  80. } // namespace base
  81. #endif // BASE_ANDROID_ANDROID_IMAGE_READER_COMPAT_H_