Home | History | Annotate | Download | only in HAL
      1 /* Copyright (c) 2012-2016, The Linux Foundation. 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 // System dependencies
     34 #include <utils/Mutex.h>
     35 #include <utils/Condition.h>
     36 
     37 // Camera dependencies
     38 #include "camera.h"
     39 #include "QCameraAllocator.h"
     40 #include "QCameraChannel.h"
     41 #include "QCameraCmdThread.h"
     42 #include "QCameraDisplay.h"
     43 #include "QCameraMem.h"
     44 #include "QCameraParameters.h"
     45 #include "QCameraParametersIntf.h"
     46 #include "QCameraPerf.h"
     47 #include "QCameraPostProc.h"
     48 #include "QCameraQueue.h"
     49 #include "QCameraStream.h"
     50 #include "QCameraStateMachine.h"
     51 #include "QCameraThermalAdapter.h"
     52 
     53 #ifdef TARGET_TS_MAKEUP
     54 #include "ts_makeup_engine.h"
     55 #include "ts_detectface_engine.h"
     56 #endif
     57 extern "C" {
     58 #include "mm_camera_interface.h"
     59 #include "mm_jpeg_interface.h"
     60 }
     61 
     62 #include "QCameraTrace.h"
     63 
     64 namespace qcamera {
     65 
     66 #ifndef TRUE
     67 #define TRUE 1
     68 #endif
     69 
     70 #ifndef FALSE
     71 #define FALSE 0
     72 #endif
     73 
     74 typedef enum {
     75     QCAMERA_CH_TYPE_ZSL,
     76     QCAMERA_CH_TYPE_CAPTURE,
     77     QCAMERA_CH_TYPE_PREVIEW,
     78     QCAMERA_CH_TYPE_VIDEO,
     79     QCAMERA_CH_TYPE_SNAPSHOT,
     80     QCAMERA_CH_TYPE_RAW,
     81     QCAMERA_CH_TYPE_METADATA,
     82     QCAMERA_CH_TYPE_ANALYSIS,
     83     QCAMERA_CH_TYPE_CALLBACK,
     84     QCAMERA_CH_TYPE_MAX
     85 } qcamera_ch_type_enum_t;
     86 
     87 typedef struct {
     88     int32_t msg_type;
     89     int32_t ext1;
     90     int32_t ext2;
     91 } qcamera_evt_argm_t;
     92 
     93 #define QCAMERA_DUMP_FRM_PREVIEW             1
     94 #define QCAMERA_DUMP_FRM_VIDEO               (1<<1)
     95 #define QCAMERA_DUMP_FRM_SNAPSHOT            (1<<2)
     96 #define QCAMERA_DUMP_FRM_THUMBNAIL           (1<<3)
     97 #define QCAMERA_DUMP_FRM_RAW                 (1<<4)
     98 #define QCAMERA_DUMP_FRM_JPEG                (1<<5)
     99 #define QCAMERA_DUMP_FRM_INPUT_REPROCESS     (1<<6)
    100 
    101 #define QCAMERA_DUMP_FRM_MASK_ALL    0x000000ff
    102 
    103 #define QCAMERA_ION_USE_CACHE   true
    104 #define QCAMERA_ION_USE_NOCACHE false
    105 #define MAX_ONGOING_JOBS 25
    106 
    107 #define MAX(a, b) ((a) > (b) ? (a) : (b))
    108 
    109 #define EXIF_ASCII_PREFIX_SIZE           8   //(sizeof(ExifAsciiPrefix))
    110 
    111 typedef enum {
    112     QCAMERA_NOTIFY_CALLBACK,
    113     QCAMERA_DATA_CALLBACK,
    114     QCAMERA_DATA_TIMESTAMP_CALLBACK,
    115     QCAMERA_DATA_SNAPSHOT_CALLBACK
    116 } qcamera_callback_type_m;
    117 
    118 typedef void (*camera_release_callback)(void *user_data,
    119                                         void *cookie,
    120                                         int32_t cb_status);
    121 typedef void (*jpeg_data_callback)(int32_t msg_type,
    122         const camera_memory_t *data, unsigned int index,
    123         camera_frame_metadata_t *metadata, void *user,
    124         uint32_t frame_idx, camera_release_callback release_cb,
    125         void *release_cookie, void *release_data);
    126 
    127 typedef struct {
    128     qcamera_callback_type_m  cb_type;    // event type
    129     int32_t                  msg_type;   // msg type
    130     int32_t                  ext1;       // extended parameter
    131     int32_t                  ext2;       // extended parameter
    132     camera_memory_t *        data;       // ptr to data memory struct
    133     unsigned int             index;      // index of the buf in the whole buffer
    134     int64_t                  timestamp;  // buffer timestamp
    135     camera_frame_metadata_t *metadata;   // meta data
    136     void                    *user_data;  // any data needs to be released after callback
    137     void                    *cookie;     // release callback cookie
    138     camera_release_callback  release_cb; // release callback
    139     uint32_t                 frame_index;  // frame index for the buffer
    140 } qcamera_callback_argm_t;
    141 
    142 class QCameraCbNotifier {
    143 public:
    144     QCameraCbNotifier(QCamera2HardwareInterface *parent) :
    145                           mNotifyCb (NULL),
    146                           mDataCb (NULL),
    147                           mDataCbTimestamp (NULL),
    148                           mCallbackCookie (NULL),
    149                           mJpegCb(NULL),
    150                           mJpegCallbackCookie(NULL),
    151                           mParent (parent),
    152                           mDataQ(releaseNotifications, this),
    153                           mActive(false){}
    154 
    155     virtual ~QCameraCbNotifier();
    156 
    157     virtual int32_t notifyCallback(qcamera_callback_argm_t &cbArgs);
    158     virtual void setCallbacks(camera_notify_callback notifyCb,
    159                               camera_data_callback dataCb,
    160                               camera_data_timestamp_callback dataCbTimestamp,
    161                               void *callbackCookie);
    162     virtual void setJpegCallBacks(
    163             jpeg_data_callback jpegCb, void *callbackCookie);
    164     virtual int32_t startSnapshots();
    165     virtual void stopSnapshots();
    166     virtual void exit();
    167     static void * cbNotifyRoutine(void * data);
    168     static void releaseNotifications(void *data, void *user_data);
    169     static bool matchSnapshotNotifications(void *data, void *user_data);
    170     static bool matchPreviewNotifications(void *data, void *user_data);
    171     virtual int32_t flushPreviewNotifications();
    172 private:
    173 
    174     camera_notify_callback         mNotifyCb;
    175     camera_data_callback           mDataCb;
    176     camera_data_timestamp_callback mDataCbTimestamp;
    177     void                          *mCallbackCookie;
    178     jpeg_data_callback             mJpegCb;
    179     void                          *mJpegCallbackCookie;
    180     QCamera2HardwareInterface     *mParent;
    181 
    182     QCameraQueue     mDataQ;
    183     QCameraCmdThread mProcTh;
    184     bool             mActive;
    185 };
    186 class QCamera2HardwareInterface : public QCameraAllocator,
    187         public QCameraThermalCallback, public QCameraAdjustFPS
    188 {
    189 public:
    190     /* static variable and functions accessed by camera service */
    191     static camera_device_ops_t mCameraOps;
    192 
    193     static int set_preview_window(struct camera_device *,
    194         struct preview_stream_ops *window);
    195     static void set_CallBacks(struct camera_device *,
    196         camera_notify_callback notify_cb,
    197         camera_data_callback data_cb,
    198         camera_data_timestamp_callback data_cb_timestamp,
    199         camera_request_memory get_memory,
    200         void *user);
    201     static void enable_msg_type(struct camera_device *, int32_t msg_type);
    202     static void disable_msg_type(struct camera_device *, int32_t msg_type);
    203     static int msg_type_enabled(struct camera_device *, int32_t msg_type);
    204     static int start_preview(struct camera_device *);
    205     static void stop_preview(struct camera_device *);
    206     static int preview_enabled(struct camera_device *);
    207     static int store_meta_data_in_buffers(struct camera_device *, int enable);
    208     static int restart_start_preview(struct camera_device *);
    209     static int restart_stop_preview(struct camera_device *);
    210     static int pre_start_recording(struct camera_device *);
    211     static int start_recording(struct camera_device *);
    212     static void stop_recording(struct camera_device *);
    213     static int recording_enabled(struct camera_device *);
    214     static void release_recording_frame(struct camera_device *, const void *opaque);
    215     static int auto_focus(struct camera_device *);
    216     static int cancel_auto_focus(struct camera_device *);
    217     static int pre_take_picture(struct camera_device *);
    218     static int take_picture(struct camera_device *);
    219     int takeLiveSnapshot_internal();
    220     int takeBackendPic_internal(bool *JpegMemOpt, char *raw_format);
    221     void clearIntPendingEvents();
    222     void checkIntPicPending(bool JpegMemOpt, char *raw_format);
    223     static int cancel_picture(struct camera_device *);
    224     static int set_parameters(struct camera_device *, const char *parms);
    225     static int stop_after_set_params(struct camera_device *);
    226     static int commit_params(struct camera_device *);
    227     static int restart_after_set_params(struct camera_device *);
    228     static char* get_parameters(struct camera_device *);
    229     static void put_parameters(struct camera_device *, char *);
    230     static int send_command(struct camera_device *,
    231               int32_t cmd, int32_t arg1, int32_t arg2);
    232     static int send_command_restart(struct camera_device *,
    233             int32_t cmd, int32_t arg1, int32_t arg2);
    234     static void release(struct camera_device *);
    235     static int dump(struct camera_device *, int fd);
    236     static int close_camera_device(hw_device_t *);
    237 
    238     static int register_face_image(struct camera_device *,
    239                                    void *img_ptr,
    240                                    cam_pp_offline_src_config_t *config);
    241     static int prepare_preview(struct camera_device *);
    242     static int prepare_snapshot(struct camera_device *device);
    243 
    244 public:
    245     QCamera2HardwareInterface(uint32_t cameraId);
    246     virtual ~QCamera2HardwareInterface();
    247     int openCamera(struct hw_device_t **hw_device);
    248 
    249     // Dual camera specific oprations
    250     int bundleRelatedCameras(bool syncOn,
    251             uint32_t related_sensor_session_id);
    252     int getCameraSessionId(uint32_t* session_id);
    253     const cam_sync_related_sensors_event_info_t* getRelatedCamSyncInfo(
    254             void);
    255     int32_t setRelatedCamSyncInfo(
    256             cam_sync_related_sensors_event_info_t* info);
    257     int32_t setMpoComposition(bool enable);
    258     bool getMpoComposition(void);
    259     bool getRecordingHintValue(void);
    260     int32_t setRecordingHintValue(int32_t value);
    261     bool isPreviewRestartNeeded(void) { return mPreviewRestartNeeded; };
    262     static int getCapabilities(uint32_t cameraId,
    263             struct camera_info *info, cam_sync_type_t *cam_type);
    264     static int initCapabilities(uint32_t cameraId, mm_camera_vtbl_t *cameraHandle);
    265     cam_capability_t *getCamHalCapabilities();
    266 
    267     // Implementation of QCameraAllocator
    268     virtual QCameraMemory *allocateStreamBuf(cam_stream_type_t stream_type,
    269             size_t size, int stride, int scanline, uint8_t &bufferCnt);
    270     virtual int32_t allocateMoreStreamBuf(QCameraMemory *mem_obj,
    271             size_t size, uint8_t &bufferCnt);
    272 
    273     virtual QCameraHeapMemory *allocateStreamInfoBuf(cam_stream_type_t stream_type);
    274     virtual QCameraHeapMemory *allocateMiscBuf(cam_stream_info_t *streamInfo);
    275     virtual QCameraMemory *allocateStreamUserBuf(cam_stream_info_t *streamInfo);
    276     virtual void waitForDeferredAlloc(cam_stream_type_t stream_type);
    277 
    278     // Implementation of QCameraThermalCallback
    279     virtual int thermalEvtHandle(qcamera_thermal_level_enum_t *level,
    280             void *userdata, void *data);
    281 
    282     virtual int recalcFPSRange(int &minFPS, int &maxFPS,
    283             const float &minVideoFPS, const float &maxVideoFPS,
    284             cam_fps_range_t &adjustedRange);
    285 
    286     friend class QCameraStateMachine;
    287     friend class QCameraPostProcessor;
    288     friend class QCameraCbNotifier;
    289     friend class QCameraMuxer;
    290 
    291     void setJpegCallBacks(jpeg_data_callback jpegCb,
    292             void *callbackCookie);
    293     int32_t initJpegHandle();
    294     int32_t deinitJpegHandle();
    295     int32_t setJpegHandleInfo(mm_jpeg_ops_t *ops,
    296             mm_jpeg_mpo_ops_t *mpo_ops, uint32_t pJpegClientHandle);
    297     int32_t getJpegHandleInfo(mm_jpeg_ops_t *ops,
    298             mm_jpeg_mpo_ops_t *mpo_ops, uint32_t *pJpegClientHandle);
    299     uint32_t getCameraId() { return mCameraId; };
    300 private:
    301     int setPreviewWindow(struct preview_stream_ops *window);
    302     int setCallBacks(
    303         camera_notify_callback notify_cb,
    304         camera_data_callback data_cb,
    305         camera_data_timestamp_callback data_cb_timestamp,
    306         camera_request_memory get_memory,
    307         void *user);
    308     int enableMsgType(int32_t msg_type);
    309     int disableMsgType(int32_t msg_type);
    310     int msgTypeEnabled(int32_t msg_type);
    311     int msgTypeEnabledWithLock(int32_t msg_type);
    312     int startPreview();
    313     int stopPreview();
    314     int storeMetaDataInBuffers(int enable);
    315     int preStartRecording();
    316     int startRecording();
    317     int stopRecording();
    318     int releaseRecordingFrame(const void *opaque);
    319     int autoFocus();
    320     int cancelAutoFocus();
    321     int preTakePicture();
    322     int takePicture();
    323     int stopCaptureChannel(bool destroy);
    324     int cancelPicture();
    325     int takeLiveSnapshot();
    326     int takePictureInternal();
    327     int cancelLiveSnapshot();
    328     char* getParameters() {return mParameters.getParameters(); }
    329     int putParameters(char *);
    330     int sendCommand(int32_t cmd, int32_t &arg1, int32_t &arg2);
    331     int release();
    332     int dump(int fd);
    333     int registerFaceImage(void *img_ptr,
    334                           cam_pp_offline_src_config_t *config,
    335                           int32_t &faceID);
    336     int32_t longShot();
    337 
    338     uint32_t deferPPInit();
    339     int openCamera();
    340     int closeCamera();
    341 
    342     int processAPI(qcamera_sm_evt_enum_t api, void *api_payload);
    343     int processEvt(qcamera_sm_evt_enum_t evt, void *evt_payload);
    344     int processSyncEvt(qcamera_sm_evt_enum_t evt, void *evt_payload);
    345     void lockAPI();
    346     void waitAPIResult(qcamera_sm_evt_enum_t api_evt, qcamera_api_result_t *apiResult);
    347     void unlockAPI();
    348     void signalAPIResult(qcamera_api_result_t *result);
    349     void signalEvtResult(qcamera_api_result_t *result);
    350 
    351     int calcThermalLevel(qcamera_thermal_level_enum_t level,
    352             const int minFPSi, const int maxFPSi,
    353             const float &minVideoFPS, const float &maxVideoFPS,
    354             cam_fps_range_t &adjustedRange,
    355             enum msm_vfe_frame_skip_pattern &skipPattern);
    356     int updateThermalLevel(void *level);
    357 
    358     // update entris to set parameters and check if restart is needed
    359     int updateParameters(const char *parms, bool &needRestart);
    360     // send request to server to set parameters
    361     int commitParameterChanges();
    362 
    363     bool isCaptureShutterEnabled();
    364     bool needDebugFps();
    365     bool isRegularCapture();
    366     bool isCACEnabled();
    367     bool is4k2kResolution(cam_dimension_t* resolution);
    368     bool isPreviewRestartEnabled();
    369     bool needReprocess();
    370     bool needRotationReprocess();
    371     void debugShowVideoFPS();
    372     void debugShowPreviewFPS();
    373     void dumpJpegToFile(const void *data, size_t size, uint32_t index);
    374     void dumpFrameToFile(QCameraStream *stream,
    375             mm_camera_buf_def_t *frame, uint32_t dump_type, const char *misc = NULL);
    376     void dumpMetadataToFile(QCameraStream *stream,
    377                             mm_camera_buf_def_t *frame,char *type);
    378     void releaseSuperBuf(mm_camera_super_buf_t *super_buf);
    379     void playShutter();
    380     void getThumbnailSize(cam_dimension_t &dim);
    381     uint32_t getJpegQuality();
    382     QCameraExif *getExifData();
    383     cam_sensor_t getSensorType();
    384     bool isLowPowerMode();
    385 
    386     int32_t processAutoFocusEvent(cam_auto_focus_data_t &focus_data);
    387     int32_t processZoomEvent(cam_crop_data_t &crop_info);
    388     int32_t processPrepSnapshotDoneEvent(cam_prep_snapshot_state_t prep_snapshot_state);
    389     int32_t processASDUpdate(cam_asd_decision_t asd_decision);
    390     int32_t processJpegNotify(qcamera_jpeg_evt_payload_t *jpeg_job);
    391     int32_t processHDRData(cam_asd_hdr_scene_data_t hdr_scene);
    392     int32_t processRetroAECUnlock();
    393     int32_t processZSLCaptureDone();
    394     int32_t processSceneData(cam_scene_mode_type scene);
    395     int32_t transAwbMetaToParams(cam_awb_params_t &awb_params);
    396     int32_t processFocusPositionInfo(cam_focus_pos_info_t &cur_pos_info);
    397     int32_t processAEInfo(cam_3a_params_t &ae_params);
    398 
    399     int32_t sendEvtNotify(int32_t msg_type, int32_t ext1, int32_t ext2);
    400     int32_t sendDataNotify(int32_t msg_type,
    401             camera_memory_t *data,
    402             uint8_t index,
    403             camera_frame_metadata_t *metadata,
    404             uint32_t frame_idx);
    405 
    406     int32_t sendPreviewCallback(QCameraStream *stream,
    407             QCameraMemory *memory, uint32_t idx);
    408     int32_t selectScene(QCameraChannel *pChannel,
    409             mm_camera_super_buf_t *recvd_frame);
    410 
    411     int32_t addChannel(qcamera_ch_type_enum_t ch_type);
    412     int32_t startChannel(qcamera_ch_type_enum_t ch_type);
    413     int32_t stopChannel(qcamera_ch_type_enum_t ch_type);
    414     int32_t delChannel(qcamera_ch_type_enum_t ch_type, bool destroy = true);
    415     int32_t addPreviewChannel();
    416     int32_t addSnapshotChannel();
    417     int32_t addVideoChannel();
    418     int32_t addZSLChannel();
    419     int32_t addCaptureChannel();
    420     int32_t addRawChannel();
    421     int32_t addMetaDataChannel();
    422     int32_t addAnalysisChannel();
    423     QCameraReprocessChannel *addReprocChannel(QCameraChannel *pInputChannel,
    424             int8_t cur_channel_index = 0);
    425     QCameraReprocessChannel *addOfflineReprocChannel(
    426                                                 cam_pp_offline_src_config_t &img_config,
    427                                                 cam_pp_feature_config_t &pp_feature,
    428                                                 stream_cb_routine stream_cb,
    429                                                 void *userdata);
    430     int32_t addCallbackChannel();
    431 
    432     int32_t addStreamToChannel(QCameraChannel *pChannel,
    433                                cam_stream_type_t streamType,
    434                                stream_cb_routine streamCB,
    435                                void *userData);
    436     int32_t preparePreview();
    437     void unpreparePreview();
    438     int32_t prepareRawStream(QCameraChannel *pChannel);
    439     QCameraChannel *getChannelByHandle(uint32_t channelHandle);
    440     mm_camera_buf_def_t *getSnapshotFrame(mm_camera_super_buf_t *recvd_frame);
    441     int32_t processFaceDetectionResult(cam_faces_data_t *fd_data);
    442     bool needPreviewFDCallback(uint8_t num_faces);
    443     int32_t processHistogramStats(cam_hist_stats_t &stats_data);
    444     int32_t setHistogram(bool histogram_en);
    445     int32_t setFaceDetection(bool enabled);
    446     int32_t prepareHardwareForSnapshot(int32_t afNeeded);
    447     bool needProcessPreviewFrame(uint32_t frameID);
    448     bool needSendPreviewCallback();
    449     bool isNoDisplayMode() {return mParameters.isNoDisplayMode();};
    450     bool isZSLMode() {return mParameters.isZSLMode();};
    451     bool isRdiMode() {return mParameters.isRdiMode();};
    452     uint8_t numOfSnapshotsExpected() {
    453         return mParameters.getNumOfSnapshots();};
    454     bool isSecureMode() {return mParameters.isSecureMode();};
    455     bool isLongshotEnabled() { return mLongshotEnabled; };
    456     bool isHFRMode() {return mParameters.isHfrMode();};
    457     bool isLiveSnapshot() {return m_stateMachine.isRecording();};
    458     void setRetroPicture(bool enable) { bRetroPicture = enable; };
    459     bool isRetroPicture() {return bRetroPicture; };
    460     bool isHDRMode() {return mParameters.isHDREnabled();};
    461     uint8_t getBufNumRequired(cam_stream_type_t stream_type);
    462     bool needFDMetadata(qcamera_ch_type_enum_t channel_type);
    463     int32_t configureOnlineRotation(QCameraChannel &ch);
    464     int32_t declareSnapshotStreams();
    465     int32_t unconfigureAdvancedCapture();
    466     int32_t configureAdvancedCapture();
    467     int32_t configureAFBracketing(bool enable = true);
    468     int32_t configureHDRBracketing();
    469     int32_t stopAdvancedCapture(QCameraPicChannel *pChannel);
    470     int32_t startAdvancedCapture(QCameraPicChannel *pChannel);
    471     int32_t configureOptiZoom();
    472     int32_t configureStillMore();
    473     int32_t configureAEBracketing();
    474     int32_t updatePostPreviewParameters();
    475     inline void setOutputImageCount(uint32_t aCount) {mOutputCount = aCount;}
    476     inline uint32_t getOutputImageCount() {return mOutputCount;}
    477     bool processUFDumps(qcamera_jpeg_evt_payload_t *evt);
    478     void captureDone();
    479     int32_t updateMetadata(metadata_buffer_t *pMetaData);
    480     void fillFacesData(cam_faces_data_t &faces_data, metadata_buffer_t *metadata);
    481 
    482     int32_t getPPConfig(cam_pp_feature_config_t &pp_config,
    483             int8_t curIndex = 0, bool multipass = FALSE);
    484     virtual uint32_t scheduleBackgroundTask(BackgroundTask* bgTask);
    485     virtual int32_t waitForBackgroundTask(uint32_t &taskId);
    486     bool needDeferred(cam_stream_type_t stream_type);
    487     static void camEvtHandle(uint32_t camera_handle,
    488                           mm_camera_event_t *evt,
    489                           void *user_data);
    490     static void jpegEvtHandle(jpeg_job_status_t status,
    491                               uint32_t client_hdl,
    492                               uint32_t jobId,
    493                               mm_jpeg_output_t *p_buf,
    494                               void *userdata);
    495 
    496     static void *evtNotifyRoutine(void *data);
    497 
    498     // functions for different data notify cb
    499     static void zsl_channel_cb(mm_camera_super_buf_t *recvd_frame, void *userdata);
    500     static void capture_channel_cb_routine(mm_camera_super_buf_t *recvd_frame,
    501                                            void *userdata);
    502     static void postproc_channel_cb_routine(mm_camera_super_buf_t *recvd_frame,
    503                                             void *userdata);
    504     static void rdi_mode_stream_cb_routine(mm_camera_super_buf_t *frame,
    505                                               QCameraStream *stream,
    506                                               void *userdata);
    507     static void nodisplay_preview_stream_cb_routine(mm_camera_super_buf_t *frame,
    508                                                     QCameraStream *stream,
    509                                                     void *userdata);
    510     static void preview_stream_cb_routine(mm_camera_super_buf_t *frame,
    511                                           QCameraStream *stream,
    512                                           void *userdata);
    513     static void synchronous_stream_cb_routine(mm_camera_super_buf_t *frame,
    514             QCameraStream *stream, void *userdata);
    515     static void postview_stream_cb_routine(mm_camera_super_buf_t *frame,
    516                                            QCameraStream *stream,
    517                                            void *userdata);
    518     static void video_stream_cb_routine(mm_camera_super_buf_t *frame,
    519                                         QCameraStream *stream,
    520                                         void *userdata);
    521     static void snapshot_channel_cb_routine(mm_camera_super_buf_t *frame,
    522            void *userdata);
    523     static void raw_channel_cb_routine(mm_camera_super_buf_t *frame,
    524             void *userdata);
    525     static void raw_stream_cb_routine(mm_camera_super_buf_t *frame,
    526                                       QCameraStream *stream,
    527                                       void *userdata);
    528     static void preview_raw_stream_cb_routine(mm_camera_super_buf_t * super_frame,
    529                                               QCameraStream * stream,
    530                                               void * userdata);
    531     static void snapshot_raw_stream_cb_routine(mm_camera_super_buf_t * super_frame,
    532                                                QCameraStream * stream,
    533                                                void * userdata);
    534     static void metadata_stream_cb_routine(mm_camera_super_buf_t *frame,
    535                                            QCameraStream *stream,
    536                                            void *userdata);
    537     static void callback_stream_cb_routine(mm_camera_super_buf_t *frame,
    538             QCameraStream *stream, void *userdata);
    539     static void reprocess_stream_cb_routine(mm_camera_super_buf_t *frame,
    540                                             QCameraStream *stream,
    541                                             void *userdata);
    542 
    543     static void releaseCameraMemory(void *data,
    544                                     void *cookie,
    545                                     int32_t cbStatus);
    546     static void returnStreamBuffer(void *data,
    547                                    void *cookie,
    548                                    int32_t cbStatus);
    549     static void getLogLevel();
    550 
    551     int32_t startRAWChannel(QCameraChannel *pChannel);
    552     int32_t stopRAWChannel();
    553 
    554     inline bool getNeedRestart() {return m_bNeedRestart;}
    555     inline void setNeedRestart(bool needRestart) {m_bNeedRestart = needRestart;}
    556 
    557     /*Start display skip. Skip starts after
    558     skipCnt number of frames from current frame*/
    559     void setDisplaySkip(bool enabled, uint8_t skipCnt = 0);
    560     /*Caller can specify range frameID to skip.
    561     if end is 0, all the frames after start will be skipped*/
    562     void setDisplayFrameSkip(uint32_t start = 0, uint32_t end = 0);
    563     /*Verifies if frameId is valid to skip*/
    564     bool isDisplayFrameToSkip(uint32_t frameId);
    565 
    566 private:
    567     camera_device_t   mCameraDevice;
    568     uint32_t          mCameraId;
    569     mm_camera_vtbl_t *mCameraHandle;
    570     bool mCameraOpened;
    571 
    572     cam_jpeg_metadata_t mJpegMetadata;
    573     bool m_bRelCamCalibValid;
    574 
    575     preview_stream_ops_t *mPreviewWindow;
    576     QCameraParametersIntf mParameters;
    577     int32_t               mMsgEnabled;
    578     int                   mStoreMetaDataInFrame;
    579 
    580     camera_notify_callback         mNotifyCb;
    581     camera_data_callback           mDataCb;
    582     camera_data_timestamp_callback mDataCbTimestamp;
    583     camera_request_memory          mGetMemory;
    584     jpeg_data_callback             mJpegCb;
    585     void                          *mCallbackCookie;
    586     void                          *mJpegCallbackCookie;
    587     bool                           m_bMpoEnabled;
    588 
    589     QCameraStateMachine m_stateMachine;   // state machine
    590     bool m_smThreadActive;
    591     QCameraPostProcessor m_postprocessor; // post processor
    592     QCameraThermalAdapter &m_thermalAdapter;
    593     QCameraCbNotifier m_cbNotifier;
    594     QCameraPerfLock m_perfLock;
    595     pthread_mutex_t m_lock;
    596     pthread_cond_t m_cond;
    597     api_result_list *m_apiResultList;
    598     QCameraMemoryPool m_memoryPool;
    599 
    600     pthread_mutex_t m_evtLock;
    601     pthread_cond_t m_evtCond;
    602     qcamera_api_result_t m_evtResult;
    603 
    604 
    605     QCameraChannel *m_channels[QCAMERA_CH_TYPE_MAX]; // array holding channel ptr
    606 
    607     bool m_bPreviewStarted;             //flag indicates first preview frame callback is received
    608     bool m_bRecordStarted;             //flag indicates Recording is started for first time
    609 
    610     // Signifies if ZSL Retro Snapshots are enabled
    611     bool bRetroPicture;
    612     // Signifies AEC locked during zsl snapshots
    613     bool m_bLedAfAecLock;
    614     cam_af_state_t m_currentFocusState;
    615 
    616     uint32_t mDumpFrmCnt;  // frame dump count
    617     uint32_t mDumpSkipCnt; // frame skip count
    618     mm_jpeg_exif_params_t mExifParams;
    619     qcamera_thermal_level_enum_t mThermalLevel;
    620     bool mActiveAF;
    621     bool m_HDRSceneEnabled;
    622     bool mLongshotEnabled;
    623 
    624     pthread_t mLiveSnapshotThread;
    625     pthread_t mIntPicThread;
    626     bool mFlashNeeded;
    627     uint32_t mDeviceRotation;
    628     uint32_t mCaptureRotation;
    629     uint32_t mJpegExifRotation;
    630     bool mUseJpegExifRotation;
    631     bool mIs3ALocked;
    632     bool mPrepSnapRun;
    633     int32_t mZoomLevel;
    634     // Flag to indicate whether preview restart needed (for dual camera mode)
    635     bool mPreviewRestartNeeded;
    636 
    637     int mVFrameCount;
    638     int mVLastFrameCount;
    639     nsecs_t mVLastFpsTime;
    640     double mVFps;
    641     int mPFrameCount;
    642     int mPLastFrameCount;
    643     nsecs_t mPLastFpsTime;
    644     double mPFps;
    645     uint8_t mInstantAecFrameCount;
    646 
    647     //eztune variables for communication with eztune server at backend
    648     bool m_bIntJpegEvtPending;
    649     bool m_bIntRawEvtPending;
    650     char m_BackendFileName[QCAMERA_MAX_FILEPATH_LENGTH];
    651     size_t mBackendFileSize;
    652     pthread_mutex_t m_int_lock;
    653     pthread_cond_t m_int_cond;
    654 
    655     enum DeferredWorkCmd {
    656         CMD_DEF_ALLOCATE_BUFF,
    657         CMD_DEF_PPROC_START,
    658         CMD_DEF_PPROC_INIT,
    659         CMD_DEF_METADATA_ALLOC,
    660         CMD_DEF_CREATE_JPEG_SESSION,
    661         CMD_DEF_PARAM_ALLOC,
    662         CMD_DEF_PARAM_INIT,
    663         CMD_DEF_GENERIC,
    664         CMD_DEF_MAX
    665     };
    666 
    667     typedef struct {
    668         QCameraChannel *ch;
    669         cam_stream_type_t type;
    670     } DeferAllocBuffArgs;
    671 
    672     typedef struct {
    673         uint8_t bufferCnt;
    674         size_t size;
    675     } DeferMetadataAllocArgs;
    676 
    677     typedef struct {
    678         jpeg_encode_callback_t jpeg_cb;
    679         void *user_data;
    680     } DeferPProcInitArgs;
    681 
    682     typedef union {
    683         DeferAllocBuffArgs allocArgs;
    684         QCameraChannel *pprocArgs;
    685         DeferMetadataAllocArgs metadataAllocArgs;
    686         DeferPProcInitArgs pprocInitArgs;
    687         BackgroundTask *genericArgs;
    688     } DeferWorkArgs;
    689 
    690     typedef struct {
    691         uint32_t mDefJobId;
    692 
    693         //Job status is needed to check job was successful or failed
    694         //Error code when job was not sucessful and there is error
    695         //0 when is initialized.
    696         //for sucessfull job, do not need to maintain job status
    697         int32_t mDefJobStatus;
    698     } DefOngoingJob;
    699 
    700     DefOngoingJob mDefOngoingJobs[MAX_ONGOING_JOBS];
    701 
    702     struct DefWork
    703     {
    704         DefWork(DeferredWorkCmd cmd_,
    705                  uint32_t id_,
    706                  DeferWorkArgs args_)
    707             : cmd(cmd_),
    708               id(id_),
    709               args(args_){};
    710 
    711         DeferredWorkCmd cmd;
    712         uint32_t id;
    713         DeferWorkArgs args;
    714     };
    715 
    716     QCameraCmdThread      mDeferredWorkThread;
    717     QCameraQueue          mCmdQueue;
    718 
    719     Mutex                 mDefLock;
    720     Condition             mDefCond;
    721 
    722     uint32_t queueDeferredWork(DeferredWorkCmd cmd,
    723                                DeferWorkArgs args);
    724     uint32_t dequeueDeferredWork(DefWork* dw, int32_t jobStatus);
    725     int32_t waitDeferredWork(uint32_t &job_id);
    726     static void *deferredWorkRoutine(void *obj);
    727     bool checkDeferredWork(uint32_t &job_id);
    728     int32_t getDefJobStatus(uint32_t &job_id);
    729 
    730     uint32_t mReprocJob;
    731     uint32_t mJpegJob;
    732     uint32_t mMetadataAllocJob;
    733     uint32_t mInitPProcJob;
    734     uint32_t mParamAllocJob;
    735     uint32_t mParamInitJob;
    736     uint32_t mOutputCount;
    737     uint32_t mInputCount;
    738     bool mAdvancedCaptureConfigured;
    739     bool mHDRBracketingEnabled;
    740     int32_t mNumPreviewFaces;
    741     // Jpeg Handle shared between HWI instances
    742     mm_jpeg_ops_t         mJpegHandle;
    743     // MPO handle shared between HWI instances
    744     // this is needed for MPO composition of related
    745     // cam images
    746     mm_jpeg_mpo_ops_t     mJpegMpoHandle;
    747     uint32_t              mJpegClientHandle;
    748     bool                  mJpegHandleOwner;
    749 
    750    //ts add for makeup
    751 #ifdef TARGET_TS_MAKEUP
    752     TSRect mFaceRect;
    753     bool TsMakeupProcess_Preview(mm_camera_buf_def_t *pFrame,QCameraStream * pStream);
    754     bool TsMakeupProcess_Snapshot(mm_camera_buf_def_t *pFrame,QCameraStream * pStream);
    755     bool TsMakeupProcess(mm_camera_buf_def_t *frame,QCameraStream * stream,TSRect& faceRect);
    756 #endif
    757     QCameraMemory *mMetadataMem;
    758 
    759     static uint32_t sNextJobId;
    760 
    761     //Gralloc memory details
    762     pthread_mutex_t mGrallocLock;
    763     uint8_t mEnqueuedBuffers;
    764     bool mCACDoneReceived;
    765 
    766     //GPU library to read buffer padding details.
    767     void *lib_surface_utils;
    768     int (*LINK_get_surface_pixel_alignment)();
    769     uint32_t mSurfaceStridePadding;
    770 
    771     //QCamera Display Object
    772     QCameraDisplay mCameraDisplay;
    773 
    774     bool m_bNeedRestart;
    775     Mutex mMapLock;
    776     Condition mMapCond;
    777 
    778     //Used to decide the next frameID to be skipped
    779     uint32_t mLastPreviewFrameID;
    780     //FrameID to start frame skip.
    781     uint32_t mFrameSkipStart;
    782     /*FrameID to stop frameskip. If this is not set,
    783     all frames are skipped till we set this*/
    784     uint32_t mFrameSkipEnd;
    785 };
    786 
    787 }; // namespace qcamera
    788 
    789 #endif /* __QCAMERA2HARDWAREINTERFACE_H__ */
    790