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