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