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 #ifndef BASE_CAMERA_ADAPTER_H
20 #define BASE_CAMERA_ADAPTER_H
21 
22 #include "CameraHal.h"
23 
24 namespace Ti {
25 namespace Camera {
26 
27 struct LUT {
28     const char * userDefinition;
29     int           halDefinition;
30 };
31 
32 struct LUTtypeHAL{
33     int size;
34     const LUT *Table;
35 };
36 
37 class BaseCameraAdapter : public CameraAdapter
38 {
39 
40 public:
41 
42     BaseCameraAdapter();
43     virtual ~BaseCameraAdapter();
44 
45     ///Initialzes the camera adapter creates any resources required
46     virtual status_t initialize(CameraProperties::Properties*) = 0;
47 
48     virtual int setErrorHandler(ErrorNotifier *errorNotifier);
49 
50     //Message/Frame notification APIs
51     virtual void enableMsgType(int32_t msgs, frame_callback callback=NULL, event_callback eventCb=NULL, void* cookie=NULL);
52     virtual void disableMsgType(int32_t msgs, void* cookie);
53     virtual void returnFrame(CameraBuffer * frameBuf, CameraFrame::FrameType frameType);
54     virtual void addFramePointers(CameraBuffer *frameBuf, void *y_uv);
55     virtual void removeFramePointers();
56 
57     //APIs to configure Camera adapter and get the current parameter set
58     virtual status_t setParameters(const android::CameraParameters& params) = 0;
59     virtual void getParameters(android::CameraParameters& params)  = 0;
60 
61     //API to send a command to the camera
62     virtual status_t sendCommand(CameraCommands operation, int value1 = 0, int value2 = 0, int value3 = 0, int value4 = 0 );
63 
64     virtual status_t registerImageReleaseCallback(release_image_buffers_callback callback, void *user_data);
65 
66     virtual status_t registerEndCaptureCallback(end_image_capture_callback callback, void *user_data);
67 
68     //Retrieves the current Adapter state
69     virtual AdapterState getState();
70     //Retrieves the next Adapter state
71     virtual AdapterState getNextState();
72 
setSharedAllocator(camera_request_memory shmem_alloc)73     virtual status_t setSharedAllocator(camera_request_memory shmem_alloc) { mSharedAllocator = shmem_alloc; return NO_ERROR; };
74 
75     // Rolls the state machine back to INTIALIZED_STATE from the current state
76     virtual status_t rollbackToInitializedState();
77 
78 protected:
79     //The first two methods will try to switch the adapter state.
80     //Every call to setState() should be followed by a corresponding
81     //call to commitState(). If the state switch fails, then it will
82     //get reset to the previous state via rollbackState().
83     virtual status_t setState(CameraCommands operation);
84     virtual status_t commitState();
85     virtual status_t rollbackState();
86 
87     // Retrieves the current Adapter state - for internal use (not locked)
88     virtual status_t getState(AdapterState &state);
89     // Retrieves the next Adapter state - for internal use (not locked)
90     virtual status_t getNextState(AdapterState &state);
91 
92     //-----------Interface that needs to be implemented by deriving classes --------------------
93 
94     //Should be implmented by deriving classes in order to start image capture
95     virtual status_t takePicture();
96 
97     //Should be implmented by deriving classes in order to start image capture
98     virtual status_t stopImageCapture();
99 
100     //Should be implmented by deriving classes in order to start temporal bracketing
101     virtual status_t startBracketing(int range);
102 
103     //Should be implemented by deriving classes in order to stop temporal bracketing
104     virtual status_t stopBracketing();
105 
106     //Should be implemented by deriving classes in oder to initiate autoFocus
107     virtual status_t autoFocus();
108 
109     //Should be implemented by deriving classes in oder to initiate autoFocus
110     virtual status_t cancelAutoFocus();
111 
112     //Should be called by deriving classes in order to do some bookkeeping
113     virtual status_t startVideoCapture();
114 
115     //Should be called by deriving classes in order to do some bookkeeping
116     virtual status_t stopVideoCapture();
117 
118     //Should be implemented by deriving classes in order to start camera preview
119     virtual status_t startPreview();
120 
121     //Should be implemented by deriving classes in order to stop camera preview
122     virtual status_t stopPreview();
123 
124     //Should be implemented by deriving classes in order to start smooth zoom
125     virtual status_t startSmoothZoom(int targetIdx);
126 
127     //Should be implemented by deriving classes in order to stop smooth zoom
128     virtual status_t stopSmoothZoom();
129 
130     //Should be implemented by deriving classes in order to stop smooth zoom
131     virtual status_t useBuffers(CameraMode mode, CameraBuffer* bufArr, int num, size_t length, unsigned int queueable);
132 
133     //Should be implemented by deriving classes in order queue a released buffer in CameraAdapter
134     virtual status_t fillThisBuffer(CameraBuffer* frameBuf, CameraFrame::FrameType frameType);
135 
136     //API to get the frame size required to be allocated. This size is used to override the size passed
137     //by camera service when VSTAB/VNF is turned ON for example
138     virtual status_t getFrameSize(size_t &width, size_t &height);
139 
140     //API to get required data frame size
141     virtual status_t getFrameDataSize(size_t &dataFrameSize, size_t bufferCount);
142 
143     //API to get required picture buffers size with the current configuration in CameraParameters
144     virtual status_t getPictureBufferSize(CameraFrame &frame, size_t bufferCount);
145 
146     // Should be implemented by deriving classes in order to start face detection
147     // ( if supported )
148     virtual status_t startFaceDetection();
149 
150     // Should be implemented by deriving classes in order to stop face detection
151     // ( if supported )
152     virtual status_t stopFaceDetection();
153 
154     virtual status_t switchToExecuting();
155 
156     virtual status_t setupTunnel(uint32_t SliceHeight, uint32_t EncoderHandle, uint32_t width, uint32_t height);
157 
158     virtual status_t destroyTunnel();
159 
160     virtual status_t cameraPreviewInitialization();
161 
162     // Receive orientation events from CameraHal
163     virtual void onOrientationEvent(uint32_t orientation, uint32_t tilt);
164 
165     // ---------------------Interface ends-----------------------------------
166 
167     status_t notifyFocusSubscribers(CameraHalEvent::FocusStatus status);
168     status_t notifyShutterSubscribers();
169     status_t notifyZoomSubscribers(int zoomIdx, bool targetReached);
170     status_t notifyMetadataSubscribers(android::sp<CameraMetadataResult> &meta);
171 
172     //Send the frame to subscribers
173     status_t sendFrameToSubscribers(CameraFrame *frame);
174 
175     //Resets the refCount for this particular frame
176     status_t resetFrameRefCount(CameraFrame &frame);
177 
178     //A couple of helper functions
179     void setFrameRefCount(CameraBuffer* frameBuf, CameraFrame::FrameType frameType, int refCount);
180     int getFrameRefCount(CameraBuffer* frameBuf, CameraFrame::FrameType frameType);
181     int setInitFrameRefCount(CameraBuffer* buf, unsigned int mask);
182     static const char* getLUTvalue_translateHAL(int Value, LUTtypeHAL LUT);
183 
184 // private member functions
185 private:
186     status_t __sendFrameToSubscribers(CameraFrame* frame,
187                                       android::KeyedVector<int, frame_callback> *subscribers,
188                                       CameraFrame::FrameType frameType);
189     status_t rollbackToPreviousState();
190 
191 // protected data types and variables
192 protected:
193     enum FrameState {
194         STOPPED = 0,
195         RUNNING
196     };
197 
198     enum FrameCommands {
199         START_PREVIEW = 0,
200         START_RECORDING,
201         RETURN_FRAME,
202         STOP_PREVIEW,
203         STOP_RECORDING,
204         DO_AUTOFOCUS,
205         TAKE_PICTURE,
206         FRAME_EXIT
207     };
208 
209     enum AdapterCommands {
210         ACK = 0,
211         ERROR
212     };
213 
214 #if PPM_INSTRUMENTATION || PPM_INSTRUMENTATION_ABS
215 
216     struct timeval mStartFocus;
217     struct timeval mStartCapture;
218 
219 #endif
220 
221     mutable android::Mutex mReturnFrameLock;
222 
223     //Lock protecting the Adapter state
224     mutable android::Mutex mLock;
225     AdapterState mAdapterState;
226     AdapterState mNextState;
227 
228     //Different frame subscribers get stored using these
229     android::KeyedVector<int, frame_callback> mFrameSubscribers;
230     android::KeyedVector<int, frame_callback> mSnapshotSubscribers;
231     android::KeyedVector<int, frame_callback> mFrameDataSubscribers;
232     android::KeyedVector<int, frame_callback> mVideoSubscribers;
233     android::KeyedVector<int, frame_callback> mVideoInSubscribers;
234     android::KeyedVector<int, frame_callback> mImageSubscribers;
235     android::KeyedVector<int, frame_callback> mRawSubscribers;
236     android::KeyedVector<int, event_callback> mFocusSubscribers;
237     android::KeyedVector<int, event_callback> mZoomSubscribers;
238     android::KeyedVector<int, event_callback> mShutterSubscribers;
239     android::KeyedVector<int, event_callback> mMetadataSubscribers;
240 
241     //Preview buffer management data
242     CameraBuffer *mPreviewBuffers;
243     int mPreviewBufferCount;
244     size_t mPreviewBuffersLength;
245     android::KeyedVector<CameraBuffer *, int> mPreviewBuffersAvailable;
246     mutable android::Mutex mPreviewBufferLock;
247 
248     //Snapshot buffer management data
249     android::KeyedVector<int, int> mSnapshotBuffersAvailable;
250     mutable android::Mutex mSnapshotBufferLock;
251 
252     //Video buffer management data
253     CameraBuffer *mVideoBuffers;
254     android::KeyedVector<CameraBuffer *, int> mVideoBuffersAvailable;
255     int mVideoBuffersCount;
256     size_t mVideoBuffersLength;
257     mutable android::Mutex mVideoBufferLock;
258 
259     //Image buffer management data
260     CameraBuffer *mCaptureBuffers;
261     android::KeyedVector<CameraBuffer *, int> mCaptureBuffersAvailable;
262     int mCaptureBuffersCount;
263     size_t mCaptureBuffersLength;
264     mutable android::Mutex mCaptureBufferLock;
265 
266     //Metadata buffermanagement
267     CameraBuffer *mPreviewDataBuffers;
268     android::KeyedVector<CameraBuffer *, int> mPreviewDataBuffersAvailable;
269     int mPreviewDataBuffersCount;
270     size_t mPreviewDataBuffersLength;
271     mutable android::Mutex mPreviewDataBufferLock;
272 
273     //Video input buffer management data (used for reproc pipe)
274     CameraBuffer *mVideoInBuffers;
275     android::KeyedVector<CameraBuffer *, int> mVideoInBuffersAvailable;
276     mutable android::Mutex mVideoInBufferLock;
277 
278     Utils::MessageQueue mFrameQ;
279     Utils::MessageQueue mAdapterQ;
280     mutable android::Mutex mSubscriberLock;
281     ErrorNotifier *mErrorNotifier;
282     release_image_buffers_callback mReleaseImageBuffersCallback;
283     end_image_capture_callback mEndImageCaptureCallback;
284     void *mReleaseData;
285     void *mEndCaptureData;
286     bool mRecording;
287 
288     camera_request_memory mSharedAllocator;
289 
290     uint32_t mFramesWithDucati;
291     uint32_t mFramesWithDisplay;
292     uint32_t mFramesWithEncoder;
293 
294 #ifdef CAMERAHAL_DEBUG
295     android::KeyedVector<int, bool> mBuffersWithDucati;
296 #endif
297 
298     android::KeyedVector<void *, CameraFrame *> mFrameQueue;
299 };
300 
301 } // namespace Camera
302 } // namespace Ti
303 
304 #endif //BASE_CAMERA_ADAPTER_H
305 
306 
307