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