1/*
2 * Copyright (C) Texas Instruments - http://www.ti.com/
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
19#include "CameraHal.h"
20#include <ui/GraphicBufferMapper.h>
21#include <hal_public.h>
22
23namespace Ti {
24namespace Camera {
25
26/**
27 * Display handler class - This class basically handles the buffer posting to display
28 */
29
30class ANativeWindowDisplayAdapter : public DisplayAdapter
31{
32public:
33
34    typedef struct
35        {
36        CameraBuffer *mBuffer;
37        void *mUser;
38        int mOffset;
39        int mWidth;
40        int mHeight;
41        int mWidthStride;
42        int mHeightStride;
43        int mLength;
44        CameraFrame::FrameType mType;
45        } DisplayFrame;
46
47    enum DisplayStates
48        {
49        DISPLAY_INIT = 0,
50        DISPLAY_STARTED,
51        DISPLAY_STOPPED,
52        DISPLAY_EXITED
53        };
54
55public:
56
57    ANativeWindowDisplayAdapter();
58    virtual ~ANativeWindowDisplayAdapter();
59
60    ///Initializes the display adapter creates any resources required
61    virtual status_t initialize();
62
63    virtual int setPreviewWindow(struct preview_stream_ops *window);
64    virtual int setFrameProvider(FrameNotifier *frameProvider);
65    virtual int setErrorHandler(ErrorNotifier *errorNotifier);
66    virtual int enableDisplay(int width, int height, struct timeval *refTime = NULL);
67    virtual int disableDisplay(bool cancel_buffer = true);
68    virtual status_t pauseDisplay(bool pause);
69
70#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
71
72    //Used for shot to snapshot measurement
73    virtual status_t setSnapshotTimeRef(struct timeval *refTime = NULL);
74
75#endif
76
77    virtual bool supportsExternalBuffering();
78
79    //Implementation of inherited interfaces
80    virtual CameraBuffer * allocateBufferList(int width, int height, const char* format, int &bytes, int numBufs);
81    virtual CameraBuffer *getBufferList(int *numBufs);
82    virtual uint32_t * getOffsets() ;
83    virtual int getFd() ;
84    virtual int freeBufferList(CameraBuffer * buflist);
85
86    virtual status_t maxQueueableBuffers(unsigned int& queueable);
87    virtual status_t minUndequeueableBuffers(int& unqueueable);
88
89    ///Class specific functions
90    static void frameCallbackRelay(CameraFrame* caFrame);
91    void frameCallback(CameraFrame* caFrame);
92
93    void displayThread();
94
95    private:
96    void destroy();
97    bool processHalMsg();
98    status_t PostFrame(ANativeWindowDisplayAdapter::DisplayFrame &dispFrame);
99    bool handleFrameReturn();
100    status_t returnBuffersToWindow();
101
102public:
103
104    static const int DISPLAY_TIMEOUT;
105    static const int FAILED_DQS_TO_SUSPEND;
106
107    class DisplayThread : public android::Thread
108        {
109        ANativeWindowDisplayAdapter* mDisplayAdapter;
110        Utils::MessageQueue mDisplayThreadQ;
111
112        public:
113            DisplayThread(ANativeWindowDisplayAdapter* da)
114            : Thread(false), mDisplayAdapter(da) { }
115
116        ///Returns a reference to the display message Q for display adapter to post messages
117            Utils::MessageQueue& msgQ()
118                {
119                return mDisplayThreadQ;
120                }
121
122            virtual bool threadLoop()
123                {
124                mDisplayAdapter->displayThread();
125                return false;
126                }
127
128            enum DisplayThreadCommands
129                {
130                DISPLAY_START,
131                DISPLAY_STOP,
132                DISPLAY_FRAME,
133                DISPLAY_EXIT
134                };
135        };
136
137    //friend declarations
138friend class DisplayThread;
139
140private:
141    int postBuffer(void* displayBuf);
142
143private:
144    bool mFirstInit;
145    bool mSuspend;
146    int mFailedDQs;
147    bool mPaused; //Pause state
148    preview_stream_ops_t*  mANativeWindow;
149    android::sp<DisplayThread> mDisplayThread;
150    FrameProvider *mFrameProvider; ///Pointer to the frame provider interface
151    Utils::MessageQueue mDisplayQ;
152    unsigned int mDisplayState;
153    ///@todo Have a common class for these members
154    mutable android::Mutex mLock;
155    bool mDisplayEnabled;
156    int mBufferCount;
157    CameraBuffer *mBuffers;
158    //buffer_handle_t** mBufferHandleMap; // -> frames[i].BufferHandle
159    //IMG_native_handle_t** mGrallocHandleMap; // -> frames[i].GrallocHandle
160    uint32_t* mOffsetsMap; // -> frames[i].Offset
161    int mFD;
162    android::KeyedVector<buffer_handle_t *, int> mFramesWithCameraAdapterMap;
163    android::KeyedVector<int, int> mFramesType;
164    android::sp<ErrorNotifier> mErrorNotifier;
165
166    uint32_t mFrameWidth;
167    uint32_t mFrameHeight;
168    uint32_t mPreviewWidth;
169    uint32_t mPreviewHeight;
170
171    uint32_t mXOff;
172    uint32_t mYOff;
173
174    const char *mPixelFormat;
175
176#if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
177    //Used for calculating standby to first shot
178    struct timeval mStandbyToShot;
179    bool mMeasureStandby;
180    //Used for shot to snapshot/shot calculation
181    struct timeval mStartCapture;
182    bool mShotToShot;
183
184#endif
185
186};
187
188} // namespace Camera
189} // namespace Ti
190