• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 #ifndef BASE_ANDROID_ANDROID_IMAGE_READER_COMPAT_H_
6 #define BASE_ANDROID_ANDROID_IMAGE_READER_COMPAT_H_
7 
8 #include "base/android/android_image_reader_abi.h"
9 #include "base/base_export.h"
10 #include "base/macros.h"
11 #include "base/no_destructor.h"
12 
13 namespace base {
14 namespace android {
15 
16 // This class provides runtime support for working with AImage, AImageReader and
17 // ANativeWindow objects on Android O systems without requiring building for the
18 // Android O NDK level. Don't call GetInstance() unless IsSupported() returns
19 // true.
20 class BASE_EXPORT AndroidImageReader {
21  public:
22   // Thread safe GetInstance.
23   static AndroidImageReader& GetInstance();
24 
25   // Check if the image reader usage is supported. This function returns TRUE
26   // if android version is >=OREO, the media flag is enabled and all the
27   // required functions are loaded.
28   bool IsSupported();
29 
30   // Naming convention of all the below functions are chosen to exactly match
31   // the function names in the NDK.
32   void AImage_delete(AImage* image);
33   void AImage_deleteAsync(AImage* image, int releaseFenceFd);
34   media_status_t AImage_getHardwareBuffer(const AImage* image,
35                                           AHardwareBuffer** buffer);
36   media_status_t AImage_getWidth(const AImage* image, int32_t* width);
37   media_status_t AImage_getHeight(const AImage* image, int32_t* height);
38   media_status_t AImageReader_new(int32_t width,
39                                   int32_t height,
40                                   int32_t format,
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_getWindow(AImageReader* reader,
48                                         ANativeWindow** window);
49   media_status_t AImageReader_acquireLatestImageAsync(AImageReader* reader,
50                                                       AImage** image,
51                                                       int* acquireFenceFd);
52   jobject ANativeWindow_toSurface(JNIEnv* env, ANativeWindow* window);
53 
54  private:
55   friend class base::NoDestructor<AndroidImageReader>;
56 
57   AndroidImageReader();
58   bool LoadFunctions();
59 
60   bool is_supported_;
61   pAImage_delete AImage_delete_;
62   pAImage_deleteAsync AImage_deleteAsync_;
63   pAImage_getHardwareBuffer AImage_getHardwareBuffer_;
64   pAImage_getWidth AImage_getWidth_;
65   pAImage_getHeight AImage_getHeight_;
66   pAImageReader_new AImageReader_new_;
67   pAImageReader_setImageListener AImageReader_setImageListener_;
68   pAImageReader_delete AImageReader_delete_;
69   pAImageReader_getWindow AImageReader_getWindow_;
70   pAImageReader_acquireLatestImageAsync AImageReader_acquireLatestImageAsync_;
71   pANativeWindow_toSurface ANativeWindow_toSurface_;
72 
73   DISALLOW_COPY_AND_ASSIGN(AndroidImageReader);
74 };
75 
76 }  // namespace android
77 }  // namespace base
78 
79 #endif  // BASE_ANDROID_ANDROID_IMAGE_READER_COMPAT_H_
80