1c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh/*
2c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * Copyright (C) 2016 The Android Open Source Project
3c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh *
4c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * Licensed under the Apache License, Version 2.0 (the "License");
5c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * you may not use this file except in compliance with the License.
6c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * You may obtain a copy of the License at
7c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh *
8c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh *      http://www.apache.org/licenses/LICENSE-2.0
9c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh *
10c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * Unless required by applicable law or agreed to in writing, software
11c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * distributed under the License is distributed on an "AS IS" BASIS,
12c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * See the License for the specific language governing permissions and
14c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh * limitations under the License.
15c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh */
16c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
17c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#ifndef _NDK_IMAGE_READER_PRIV_H
18c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#define _NDK_IMAGE_READER_PRIV_H
19c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
20c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <inttypes.h>
21c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
22c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include "NdkImageReader.h"
23c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
24c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <utils/List.h>
25c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <utils/Mutex.h>
26c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <utils/StrongPointer.h>
27c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
28c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <gui/CpuConsumer.h>
29c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <gui/Surface.h>
30c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
31c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <media/stagefright/foundation/ALooper.h>
32c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <media/stagefright/foundation/AHandler.h>
33c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#include <media/stagefright/foundation/AMessage.h>
34c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
35c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yehusing namespace android;
36c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
37c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yehnamespace {
38c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    enum {
39c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        IMAGE_READER_MAX_NUM_PLANES = 3,
40c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    };
41c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
42c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    enum {
43c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        ACQUIRE_SUCCESS = 0,
44c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        ACQUIRE_NO_BUFFERS = 1,
45c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        ACQUIRE_MAX_IMAGES = 2,
46c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    };
47c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh}
48c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
49c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yehstruct AImageReader : public RefBase {
50c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh  public:
51c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
52c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    static bool isSupportedFormat(int32_t format);
53c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    static int getNumPlanesForFormat(int32_t format);
54c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
55c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    AImageReader(int32_t width, int32_t height, int32_t format, int32_t maxImages);
56c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    ~AImageReader();
57c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
58c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    // Inintialize AImageReader, uninitialized or failed to initialize AImageReader
59c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    // should never be passed to application
60c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    media_status_t init();
61c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
62c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    media_status_t setImageListener(AImageReader_ImageListener* listener);
63c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
64c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    media_status_t acquireNextImage(/*out*/AImage** image);
65c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    media_status_t acquireLatestImage(/*out*/AImage** image);
66c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
67c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    ANativeWindow* getWindow()    const { return mWindow.get(); };
68c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    int32_t        getWidth()     const { return mWidth; };
69c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    int32_t        getHeight()    const { return mHeight; };
70c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    int32_t        getFormat()    const { return mFormat; };
71c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    int32_t        getMaxImages() const { return mMaxImages; };
72c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
73c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
74c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh  private:
75c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
76c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    friend struct AImage; // for grabing reader lock
77c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
78c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    media_status_t acquireCpuConsumerImageLocked(/*out*/AImage** image);
79c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    CpuConsumer::LockedBuffer* getLockedBufferLocked();
80c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    void returnLockedBufferLocked(CpuConsumer::LockedBuffer* buffer);
81c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
82c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    // Called by AImage to close image
83c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    void releaseImageLocked(AImage* image);
84c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
85c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    static int getBufferWidth(CpuConsumer::LockedBuffer* buffer);
86c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    static int getBufferHeight(CpuConsumer::LockedBuffer* buffer);
87c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
88c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    media_status_t setImageListenerLocked(AImageReader_ImageListener* listener);
89c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
90c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    // definition of handler and message
91c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    enum {
92c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        kWhatImageAvailable
93c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    };
94c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    static const char* kCallbackFpKey;
95c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    static const char* kContextKey;
96c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    class CallbackHandler : public AHandler {
97c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh      public:
98c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        CallbackHandler(AImageReader* reader) : mReader(reader) {}
99c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        void onMessageReceived(const sp<AMessage> &msg) override;
100c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh      private:
101c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        AImageReader* mReader;
102c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    };
103c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    sp<CallbackHandler> mHandler;
104c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    sp<ALooper>         mCbLooper; // Looper thread where callbacks actually happen on
105c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
106c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    List<CpuConsumer::LockedBuffer*> mBuffers;
107c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    const int32_t mWidth;
108c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    const int32_t mHeight;
109c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    const int32_t mFormat;
110c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    const int32_t mMaxImages;
111c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    const int32_t mNumPlanes;
112c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
113c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    struct FrameListener : public ConsumerBase::FrameAvailableListener {
114c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh      public:
115c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        FrameListener(AImageReader* parent) : mReader(parent) {}
116c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
117c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        void onFrameAvailable(const BufferItem& item) override;
118c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
119c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        media_status_t setImageListener(AImageReader_ImageListener* listener);
120c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
121c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh      private:
122c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        AImageReader_ImageListener mListener = {nullptr, nullptr};
123c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        wp<AImageReader>           mReader;
124c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh        Mutex                      mLock;
125c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    };
126c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    sp<FrameListener> mFrameListener;
127c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
128c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    int mHalFormat;
129c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    android_dataspace mHalDataSpace;
130c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
131c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    sp<IGraphicBufferProducer> mProducer;
132c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    sp<Surface>                mSurface;
133c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    sp<CpuConsumer>            mCpuConsumer;
134c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    sp<ANativeWindow>          mWindow;
135c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
136c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    List<AImage*>              mAcquiredImages;
137c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
138c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh    Mutex                      mLock;
139c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh};
140c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh
141c360382bf257d815b2a411152485d3c3b37a9f46Yin-Chia Yeh#endif // _NDK_IMAGE_READER_PRIV_H
142