Home | History | Annotate | Download | only in HAL
      1 /* Copyright (c) 2012-2013, The Linux Foundataion. All rights reserved.
      2  *
      3  * Redistribution and use in source and binary forms, with or without
      4  * modification, are permitted provided that the following conditions are
      5  * met:
      6  *     * Redistributions of source code must retain the above copyright
      7  *       notice, this list of conditions and the following disclaimer.
      8  *     * Redistributions in binary form must reproduce the above
      9  *       copyright notice, this list of conditions and the following
     10  *       disclaimer in the documentation and/or other materials provided
     11  *       with the distribution.
     12  *     * Neither the name of The Linux Foundation nor the names of its
     13  *       contributors may be used to endorse or promote products derived
     14  *       from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 
     30 #ifndef __QCAMERA2HARDWAREINTERFACE_H__
     31 #define __QCAMERA2HARDWAREINTERFACE_H__
     32 
     33 #include <hardware/camera.h>
     34 #include <hardware/power.h>
     35 #include <utils/Log.h>
     36 #include <QCameraParameters.h>
     37 
     38 #include "QCameraQueue.h"
     39 #include "QCameraCmdThread.h"
     40 #include "QCameraChannel.h"
     41 #include "QCameraStream.h"
     42 #include "QCameraStateMachine.h"
     43 #include "QCameraAllocator.h"
     44 #include "QCameraPostProc.h"
     45 #include "QCameraThermalAdapter.h"
     46 
     47 extern "C" {
     48 #include <mm_camera_interface.h>
     49 #include <mm_jpeg_interface.h>
     50 }
     51 
     52 #if DISABLE_DEBUG_LOG
     53 
     54 inline void __null_log(int, const char *, const char *, ...) {}
     55 
     56 #ifdef ALOGD
     57 #undef ALOGD
     58 #define ALOGD(...) do { __null_log(0, LOG_TAG,__VA_ARGS__); } while (0)
     59 #endif
     60 
     61 #ifdef ALOGI
     62 #undef ALOGI
     63 #define ALOGI(...) do { __null_log(0, LOG_TAG,__VA_ARGS__); } while (0)
     64 #endif
     65 
     66 #endif
     67 
     68 namespace qcamera {
     69 
     70 #ifndef TRUE
     71 #define TRUE 1
     72 #endif
     73 
     74 #ifndef FALSE
     75 #define FALSE 0
     76 #endif
     77 
     78 typedef enum {
     79     QCAMERA_CH_TYPE_ZSL,
     80     QCAMERA_CH_TYPE_CAPTURE,
     81     QCAMERA_CH_TYPE_PREVIEW,
     82     QCAMERA_CH_TYPE_VIDEO,
     83     QCAMERA_CH_TYPE_SNAPSHOT,
     84     QCAMERA_CH_TYPE_RAW,
     85     QCAMERA_CH_TYPE_METADATA,
     86     QCAMERA_CH_TYPE_MAX
     87 } qcamera_ch_type_enum_t;
     88 
     89 typedef struct {
     90     int32_t msg_type;
     91     int32_t ext1;
     92     int32_t ext2;
     93 } qcamera_evt_argm_t;
     94 
     95 #define QCAMERA_DUMP_FRM_PREVIEW    1
     96 #define QCAMERA_DUMP_FRM_VIDEO      (1<<1)
     97 #define QCAMERA_DUMP_FRM_SNAPSHOT   (1<<2)
     98 #define QCAMERA_DUMP_FRM_THUMBNAIL  (1<<3)
     99 #define QCAMERA_DUMP_FRM_RAW        (1<<4)
    100 #define QCAMERA_DUMP_FRM_JPEG       (1<<5)
    101 
    102 #define QCAMERA_DUMP_FRM_MASK_ALL    0x000000ff
    103 
    104 #define QCAMERA_ION_USE_CACHE   true
    105 #define QCAMERA_ION_USE_NOCACHE false
    106 
    107 typedef enum {
    108     QCAMERA_NOTIFY_CALLBACK,
    109     QCAMERA_DATA_CALLBACK,
    110     QCAMERA_DATA_TIMESTAMP_CALLBACK,
    111     QCAMERA_DATA_SNAPSHOT_CALLBACK
    112 } qcamera_callback_type_m;
    113 
    114 typedef void (*camera_release_callback)(void *user_data, void *cookie);
    115 
    116 typedef struct {
    117     qcamera_callback_type_m  cb_type;    // event type
    118     int32_t                  msg_type;   // msg type
    119     int32_t                  ext1;       // extended parameter
    120     int32_t                  ext2;       // extended parameter
    121     camera_memory_t *        data;       // ptr to data memory struct
    122     unsigned int             index;      // index of the buf in the whole buffer
    123     int64_t                  timestamp;  // buffer timestamp
    124     camera_frame_metadata_t *metadata;   // meta data
    125     void                    *user_data;  // any data needs to be released after callback
    126     void                    *cookie;     // release callback cookie
    127     camera_release_callback  release_cb; // release callback
    128 } qcamera_callback_argm_t;
    129 
    130 class QCameraCbNotifier {
    131 public:
    132     QCameraCbNotifier(QCamera2HardwareInterface *parent) :
    133                           mNotifyCb (NULL),
    134                           mDataCb (NULL),
    135                           mDataCbTimestamp (NULL),
    136                           mCallbackCookie (NULL),
    137                           mParent (parent),
    138                           mDataQ(releaseNotifications, this) {}
    139 
    140     virtual ~QCameraCbNotifier();
    141 
    142     virtual int32_t notifyCallback(qcamera_callback_argm_t &cbArgs);
    143     virtual void setCallbacks(camera_notify_callback notifyCb,
    144                               camera_data_callback dataCb,
    145                               camera_data_timestamp_callback dataCbTimestamp,
    146                               void *callbackCookie);
    147     virtual int32_t startSnapshots();
    148     virtual void stopSnapshots();
    149     static void * cbNotifyRoutine(void * data);
    150     static void releaseNotifications(void *data, void *user_data);
    151     static bool matchSnapshotNotifications(void *data, void *user_data);
    152 private:
    153 
    154     camera_notify_callback         mNotifyCb;
    155     camera_data_callback           mDataCb;
    156     camera_data_timestamp_callback mDataCbTimestamp;
    157     void                          *mCallbackCookie;
    158     QCamera2HardwareInterface     *mParent;
    159 
    160     QCameraQueue     mDataQ;
    161     QCameraCmdThread mProcTh;
    162 };
    163 class QCamera2HardwareInterface : public QCameraAllocator,
    164                                     public QCameraThermalCallback
    165 {
    166 public:
    167     /* static variable and functions accessed by camera service */
    168     static camera_device_ops_t mCameraOps;
    169 
    170     static int set_preview_window(struct camera_device *,
    171         struct preview_stream_ops *window);
    172     static void set_CallBacks(struct camera_device *,
    173         camera_notify_callback notify_cb,
    174         camera_data_callback data_cb,
    175         camera_data_timestamp_callback data_cb_timestamp,
    176         camera_request_memory get_memory,
    177         void *user);
    178     static void enable_msg_type(struct camera_device *, int32_t msg_type);
    179     static void disable_msg_type(struct camera_device *, int32_t msg_type);
    180     static int msg_type_enabled(struct camera_device *, int32_t msg_type);
    181     static int start_preview(struct camera_device *);
    182     static void stop_preview(struct camera_device *);
    183     static int preview_enabled(struct camera_device *);
    184     static int store_meta_data_in_buffers(struct camera_device *, int enable);
    185     static int start_recording(struct camera_device *);
    186     static void stop_recording(struct camera_device *);
    187     static int recording_enabled(struct camera_device *);
    188     static void release_recording_frame(struct camera_device *, const void *opaque);
    189     static int auto_focus(struct camera_device *);
    190     static int cancel_auto_focus(struct camera_device *);
    191     static int take_picture(struct camera_device *);
    192     static int cancel_picture(struct camera_device *);
    193     static int set_parameters(struct camera_device *, const char *parms);
    194     static char* get_parameters(struct camera_device *);
    195     static void put_parameters(struct camera_device *, char *);
    196     static int send_command(struct camera_device *,
    197               int32_t cmd, int32_t arg1, int32_t arg2);
    198     static void release(struct camera_device *);
    199     static int dump(struct camera_device *, int fd);
    200     static int close_camera_device(hw_device_t *);
    201 
    202     static int register_face_image(struct camera_device *,
    203                                    void *img_ptr,
    204                                    cam_pp_offline_src_config_t *config);
    205 public:
    206     QCamera2HardwareInterface(int cameraId);
    207     virtual ~QCamera2HardwareInterface();
    208     int openCamera(struct hw_device_t **hw_device);
    209 
    210     static int getCapabilities(int cameraId, struct camera_info *info);
    211     static int initCapabilities(int cameraId);
    212 
    213     // Implementation of QCameraAllocator
    214     virtual QCameraMemory *allocateStreamBuf(cam_stream_type_t stream_type,
    215                                              int size,
    216                                              uint8_t &bufferCnt);
    217     virtual QCameraHeapMemory *allocateStreamInfoBuf(cam_stream_type_t stream_type);
    218 
    219     // Implementation of QCameraThermalCallback
    220     virtual int thermalEvtHandle(qcamera_thermal_level_enum_t level,
    221             void *userdata, void *data);
    222 
    223     friend class QCameraStateMachine;
    224     friend class QCameraPostProcessor;
    225     friend class QCameraCbNotifier;
    226 
    227 private:
    228     int setPreviewWindow(struct preview_stream_ops *window);
    229     int setCallBacks(
    230         camera_notify_callback notify_cb,
    231         camera_data_callback data_cb,
    232         camera_data_timestamp_callback data_cb_timestamp,
    233         camera_request_memory get_memory,
    234         void *user);
    235     int enableMsgType(int32_t msg_type);
    236     int disableMsgType(int32_t msg_type);
    237     int msgTypeEnabled(int32_t msg_type);
    238     int msgTypeEnabledWithLock(int32_t msg_type);
    239     int startPreview();
    240     int stopPreview();
    241     int storeMetaDataInBuffers(int enable);
    242     int startRecording();
    243     int stopRecording();
    244     int releaseRecordingFrame(const void *opaque);
    245     int autoFocus();
    246     int cancelAutoFocus();
    247     int takePicture();
    248     int cancelPicture();
    249     int takeLiveSnapshot();
    250     int cancelLiveSnapshot();
    251     char* getParameters();
    252     int putParameters(char *);
    253     int sendCommand(int32_t cmd, int32_t arg1, int32_t arg2);
    254     int release();
    255     int dump(int fd);
    256     int registerFaceImage(void *img_ptr,
    257                           cam_pp_offline_src_config_t *config,
    258                           int32_t &faceID);
    259 
    260     int openCamera();
    261     int closeCamera();
    262 
    263     int processAPI(qcamera_sm_evt_enum_t api, void *api_payload);
    264     int processEvt(qcamera_sm_evt_enum_t evt, void *evt_payload);
    265     int processSyncEvt(qcamera_sm_evt_enum_t evt, void *evt_payload);
    266     void lockAPI();
    267     void waitAPIResult(qcamera_sm_evt_enum_t api_evt);
    268     void unlockAPI();
    269     void signalAPIResult(qcamera_api_result_t *result);
    270     void signalEvtResult(qcamera_api_result_t *result);
    271 
    272     int updateThermalLevel(qcamera_thermal_level_enum_t level);
    273 
    274     // update entris to set parameters and check if restart is needed
    275     int updateParameters(const char *parms, bool &needRestart);
    276     // send request to server to set parameters
    277     int commitParameterChanges();
    278 
    279     bool needDebugFps();
    280     bool isCACEnabled();
    281     bool needReprocess();
    282     bool needRotationReprocess();
    283     void debugShowVideoFPS();
    284     void debugShowPreviewFPS();
    285     void dumpFrameToFile(const void *data, uint32_t size,
    286                          int index, int dump_type);
    287     void releaseSuperBuf(mm_camera_super_buf_t *super_buf);
    288     void playShutter();
    289     void getThumbnailSize(cam_dimension_t &dim);
    290     int getJpegQuality();
    291     int getJpegRotation();
    292     QCameraExif *getExifData();
    293 
    294     int32_t processAutoFocusEvent(cam_auto_focus_data_t &focus_data);
    295     int32_t processZoomEvent(cam_crop_data_t &crop_info);
    296     int32_t processPrepSnapshotDoneEvent(cam_prep_snapshot_state_t prep_snapshot_state);
    297     int32_t processJpegNotify(qcamera_jpeg_evt_payload_t *jpeg_job);
    298 
    299     int32_t sendEvtNotify(int32_t msg_type, int32_t ext1, int32_t ext2);
    300     int32_t sendDataNotify(int32_t msg_type,
    301                            camera_memory_t *data,
    302                            uint8_t index,
    303                            camera_frame_metadata_t *metadata);
    304 
    305     int32_t addChannel(qcamera_ch_type_enum_t ch_type);
    306     int32_t startChannel(qcamera_ch_type_enum_t ch_type);
    307     int32_t stopChannel(qcamera_ch_type_enum_t ch_type);
    308     int32_t delChannel(qcamera_ch_type_enum_t ch_type);
    309     int32_t addPreviewChannel();
    310     int32_t addSnapshotChannel();
    311     int32_t addVideoChannel();
    312     int32_t addZSLChannel();
    313     int32_t addCaptureChannel();
    314     int32_t addRawChannel();
    315     int32_t addMetaDataChannel();
    316     QCameraReprocessChannel *addOnlineReprocChannel(QCameraChannel *pInputChannel);
    317     QCameraReprocessChannel *addOfflineReprocChannel(
    318                                                 cam_pp_offline_src_config_t &img_config,
    319                                                 cam_pp_feature_config_t &pp_feature,
    320                                                 stream_cb_routine stream_cb,
    321                                                 void *userdata);
    322     int32_t addStreamToChannel(QCameraChannel *pChannel,
    323                                cam_stream_type_t streamType,
    324                                stream_cb_routine streamCB,
    325                                void *userData);
    326     int32_t preparePreview();
    327     void unpreparePreview();
    328     QCameraChannel *getChannelByHandle(uint32_t channelHandle);
    329     mm_camera_buf_def_t *getSnapshotFrame(mm_camera_super_buf_t *recvd_frame);
    330     int32_t processFaceDetectionResult(cam_face_detection_data_t *fd_data);
    331     int32_t processHistogramStats(cam_hist_stats_t &stats_data);
    332     int32_t setHistogram(bool histogram_en);
    333     int32_t setFaceDetection(bool enabled);
    334     int32_t prepareHardwareForSnapshot(int32_t afNeeded);
    335     bool needProcessPreviewFrame() {return m_stateMachine.isPreviewRunning();};
    336     bool isNoDisplayMode() {return mParameters.isNoDisplayMode();};
    337     bool isZSLMode() {return mParameters.isZSLMode();};
    338     uint8_t numOfSnapshotsExpected() {return mParameters.getNumOfSnapshots();};
    339     uint8_t getBufNumRequired(cam_stream_type_t stream_type);
    340 
    341     static void camEvtHandle(uint32_t camera_handle,
    342                           mm_camera_event_t *evt,
    343                           void *user_data);
    344     static void jpegEvtHandle(jpeg_job_status_t status,
    345                               uint32_t client_hdl,
    346                               uint32_t jobId,
    347                               mm_jpeg_output_t *p_buf,
    348                               void *userdata);
    349 
    350     static void *evtNotifyRoutine(void *data);
    351 
    352     // functions for different data notify cb
    353     static void zsl_channel_cb(mm_camera_super_buf_t *recvd_frame, void *userdata);
    354     static void capture_channel_cb_routine(mm_camera_super_buf_t *recvd_frame,
    355                                            void *userdata);
    356     static void postproc_channel_cb_routine(mm_camera_super_buf_t *recvd_frame,
    357                                             void *userdata);
    358     static void nodisplay_preview_stream_cb_routine(mm_camera_super_buf_t *frame,
    359                                                     QCameraStream *stream,
    360                                                     void *userdata);
    361     static void preview_stream_cb_routine(mm_camera_super_buf_t *frame,
    362                                           QCameraStream *stream,
    363                                           void *userdata);
    364     static void postview_stream_cb_routine(mm_camera_super_buf_t *frame,
    365                                            QCameraStream *stream,
    366                                            void *userdata);
    367     static void video_stream_cb_routine(mm_camera_super_buf_t *frame,
    368                                         QCameraStream *stream,
    369                                         void *userdata);
    370     static void snapshot_stream_cb_routine(mm_camera_super_buf_t *frame,
    371                                            QCameraStream *stream,
    372                                            void *userdata);
    373     static void raw_stream_cb_routine(mm_camera_super_buf_t *frame,
    374                                       QCameraStream *stream,
    375                                       void *userdata);
    376     static void metadata_stream_cb_routine(mm_camera_super_buf_t *frame,
    377                                            QCameraStream *stream,
    378                                            void *userdata);
    379     static void reprocess_stream_cb_routine(mm_camera_super_buf_t *frame,
    380                                             QCameraStream *stream,
    381                                             void *userdata);
    382 
    383     static void releaseCameraMemory(void *data, void *cookie);
    384     static void returnStreamBuffer(void *data, void *cookie);
    385 
    386 private:
    387     camera_device_t   mCameraDevice;
    388     uint8_t           mCameraId;
    389     mm_camera_vtbl_t *mCameraHandle;
    390     bool mCameraOpened;
    391 
    392     preview_stream_ops_t *mPreviewWindow;
    393     QCameraParameters mParameters;
    394     int32_t               mMsgEnabled;
    395     int                   mStoreMetaDataInFrame;
    396 
    397     camera_notify_callback         mNotifyCb;
    398     camera_data_callback           mDataCb;
    399     camera_data_timestamp_callback mDataCbTimestamp;
    400     camera_request_memory          mGetMemory;
    401     void                          *mCallbackCookie;
    402 
    403     QCameraStateMachine m_stateMachine;   // state machine
    404     QCameraPostProcessor m_postprocessor; // post processor
    405     QCameraThermalAdapter &m_thermalAdapter;
    406     QCameraCbNotifier m_cbNotifier;
    407     pthread_mutex_t m_lock;
    408     pthread_cond_t m_cond;
    409     qcamera_api_result_t m_apiResult;
    410 
    411     pthread_mutex_t m_evtLock;
    412     pthread_cond_t m_evtCond;
    413     qcamera_api_result_t m_evtResult;
    414 
    415     QCameraChannel *m_channels[QCAMERA_CH_TYPE_MAX]; // array holding channel ptr
    416 
    417     bool m_bShutterSoundPlayed;         // if shutter sound had been played
    418 
    419     // if auto focus is running, in other words, when auto_focus is called from service,
    420     // and beforeany focus callback/cancel_focus happens. This flag is not an indication
    421     // of whether lens is moving or not.
    422     bool m_bAutoFocusRunning;
    423     cam_autofocus_state_t m_currentFocusState;
    424 
    425     // If start_zsl_snapshot is called to notify camera daemon about zsl snapshot
    426     bool m_bStartZSLSnapshotCalled;
    427 
    428     power_module_t *m_pPowerModule;   // power module
    429 
    430     int mDumpFrmCnt;  // frame dump count
    431     int mDumpSkipCnt; // frame skip count
    432 };
    433 
    434 }; // namespace qcamera
    435 
    436 #endif /* __QCAMERA2HARDWAREINTERFACE_H__ */
    437