• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #ifndef ANDROID_IMAGEREADERTESTHELPERS_H
19 #define ANDROID_IMAGEREADERTESTHELPERS_H
20 
21 #include <deque>
22 #include <mutex>
23 
24 #include <cstdlib>
25 #include <cstring>
26 #include <jni.h>
27 #include <media/NdkImage.h>
28 #include <media/NdkImageReader.h>
29 
30 class ImageReaderHelper {
31 public:
32   using ImagePtr = std::unique_ptr<AImage, decltype(&AImage_delete)>;
33 
34   ImageReaderHelper(int32_t width, int32_t height, int32_t format,
35                     uint64_t usage, int32_t maxImages);
36   ~ImageReaderHelper();
37   int initImageReader();
getNativeWindow()38   ANativeWindow *getNativeWindow() { return mImgReaderAnw; }
39   int getBufferFromCurrentImage(AHardwareBuffer **outBuffer);
40   void handleImageAvailable();
41   static void onImageAvailable(void *obj, AImageReader *);
42 
43 private:
44   int32_t mWidth;
45   int32_t mHeight;
46   int32_t mFormat;
47   uint64_t mUsage;
48   uint32_t mMaxImages;
49 
50   std::mutex mMutex;
51   // Number of images that's avaiable for acquire.
52   size_t mAvailableImages{0};
53   // Although AImageReader supports acquiring multiple images at a time, we
54   // don't really need it in this test. We only acquire one image that a time.
55   ImagePtr mAcquiredImage{nullptr, AImage_delete};
56 
57   AImageReader *mImgReader{nullptr};
58   ANativeWindow *mImgReaderAnw{nullptr};
59 
60   AImageReader_ImageListener mReaderAvailableCb{this, onImageAvailable};
61 };
62 
63 #endif // ANDROID_IMAGEREADERTESTHELPERS_H
64