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 __QCAMERA_STATEMACHINE_H__
31 #define __QCAMERA_STATEMACHINE_H__
32 
33 // System dependencies
34 #include <pthread.h>
35 
36 // Camera dependencies
37 #include "QCameraQueue.h"
38 #include "QCameraChannel.h"
39 #include "cam_semaphore.h"
40 
41 extern "C" {
42 #include "mm_camera_interface.h"
43 }
44 
45 namespace qcamera {
46 
47 class QCamera2HardwareInterface;
48 
49 typedef enum {
50     /*******BEGIN OF: API EVT*********/
51     QCAMERA_SM_EVT_SET_PREVIEW_WINDOW = 1,   // set preview window
52     QCAMERA_SM_EVT_SET_CALLBACKS,            // set callbacks
53     QCAMERA_SM_EVT_ENABLE_MSG_TYPE,          // enable msg type
54     QCAMERA_SM_EVT_DISABLE_MSG_TYPE,         // disable msg type
55     QCAMERA_SM_EVT_MSG_TYPE_ENABLED,         // query certain msg type is enabled
56 
57     QCAMERA_SM_EVT_SET_PARAMS,               // set parameters
58     QCAMERA_SM_EVT_SET_PARAMS_STOP,          // stop camera after set params, if necessary
59     QCAMERA_SM_EVT_SET_PARAMS_COMMIT,        // commit set params
60     QCAMERA_SM_EVT_SET_PARAMS_RESTART,       // restart after set params, if necessary
61     QCAMERA_SM_EVT_GET_PARAMS,               // get parameters
62     QCAMERA_SM_EVT_PUT_PARAMS,               // put parameters, release param buf
63 
64     QCAMERA_SM_EVT_PREPARE_PREVIEW,          // prepare preview (zsl, camera mode, camcorder mode)
65     QCAMERA_SM_EVT_START_PREVIEW,            // start preview (zsl, camera mode, camcorder mode)
66     QCAMERA_SM_EVT_START_NODISPLAY_PREVIEW,  // start no display preview (zsl, camera mode, camcorder mode)
67     QCAMERA_SM_EVT_STOP_PREVIEW,             // stop preview (zsl, camera mode, camcorder mode)
68     QCAMERA_SM_EVT_PREVIEW_ENABLED,          // query if preview is running
69 
70     QCAMERA_SM_EVT_STORE_METADATA_IN_BUFS,   // request to store meta data in video buffers
71     QCAMERA_SM_EVT_PRE_START_RECORDING,      // pre start recording, to prepare for recording
72     QCAMERA_SM_EVT_START_RECORDING,          // start recording
73     QCAMERA_SM_EVT_STOP_RECORDING,           // stop recording
74     QCAMERA_SM_EVT_RECORDING_ENABLED,        // query if recording is running
75     QCAMERA_SM_EVT_RELEASE_RECORIDNG_FRAME,  // release recording frame
76 
77     QCAMERA_SM_EVT_PREPARE_SNAPSHOT,         // prepare snapshot in case LED needs to be flashed
78     QCAMERA_SM_EVT_PRE_TAKE_PICTURE,         // pre take picutre (to restart preview if necessary)
79     QCAMERA_SM_EVT_TAKE_PICTURE,             // take picutre (zsl, regualr capture, live snapshot
80     QCAMERA_SM_EVT_CANCEL_PICTURE,           // cancel picture
81 
82     QCAMERA_SM_EVT_START_AUTO_FOCUS,         // start auto focus
83     QCAMERA_SM_EVT_STOP_AUTO_FOCUS,          // stop auto focus
84     QCAMERA_SM_EVT_SEND_COMMAND,             // send command
85 
86     QCAMERA_SM_EVT_RELEASE,                  // release camera resource
87     QCAMERA_SM_EVT_DUMP,                     // dump
88     QCAMERA_SM_EVT_REG_FACE_IMAGE,           // register a face image in imaging lib
89     /*******END OF: API EVT*********/
90 
91     QCAMERA_SM_EVT_EVT_INTERNAL,             // internal evt notify
92     QCAMERA_SM_EVT_EVT_NOTIFY,               // evt notify from server
93     QCAMERA_SM_EVT_JPEG_EVT_NOTIFY,          // evt notify from jpeg
94     QCAMERA_SM_EVT_SNAPSHOT_DONE,            // internal evt that snapshot is done
95     QCAMERA_SM_EVT_THERMAL_NOTIFY,           // evt notify from thermal daemon
96     QCAMERA_SM_EVT_STOP_CAPTURE_CHANNEL,     // stop capture channel
97     QCAMERA_SM_EVT_RESTART_PERVIEW,          // internal preview restart
98     QCAMERA_SM_EVT_DELAYED_RESTART,          // preview restart needs delay (dual camera mode)
99     QCAMERA_SM_EVT_SEND_COMMAND_RESTART,     // restart after send command (if necessary)
100     QCAMERA_SM_EVT_RESTART_START_PREVIEW,    // preview start as part of restart (dual camera mode)
101     QCAMERA_SM_EVT_RESTART_STOP_PREVIEW,     // preview stop as part of restart (dual camera mode)
102     QCAMERA_SM_EVT_MAX
103 } qcamera_sm_evt_enum_t;
104 
105 typedef enum {
106     QCAMERA_API_RESULT_TYPE_DEF,             // default type, no additional info
107     QCAMERA_API_RESULT_TYPE_ENABLE_FLAG,     // msg_enabled, preview_enabled, recording_enabled
108     QCAMERA_API_RESULT_TYPE_PARAMS,          // returned parameters in string
109     QCAMERA_API_RESULT_TYPE_HANDLE,          // returned handle in int
110     QCAMERA_API_RESULT_TYPE_MAX
111 } qcamera_api_result_type_t;
112 
113 typedef struct {
114     int32_t status;                          // api call status
115     qcamera_sm_evt_enum_t request_api;       // api evt requested
116     qcamera_api_result_type_t result_type;   // result type
117     union {
118         int enabled;                          // result_type == QCAMERA_API_RESULT_TYPE_ENABLE_FLAG
119         char *params;                         // result_type == QCAMERA_API_RESULT_TYPE_PARAMS
120         int handle;                           // result_type ==QCAMERA_API_RESULT_TYPE_HANDLE
121     };
122 } qcamera_api_result_t;
123 
124 typedef struct api_result_list {
125    qcamera_api_result_t result;
126    struct api_result_list *next;
127 }api_result_list;
128 
129 // definition for payload type of setting callback
130 typedef struct {
131     camera_notify_callback notify_cb;
132     camera_data_callback data_cb;
133     camera_data_timestamp_callback data_cb_timestamp;
134     camera_request_memory get_memory;
135     void *user;
136 } qcamera_sm_evt_setcb_payload_t;
137 
138 // definition for payload type of sending command
139 typedef struct {
140     int32_t cmd;
141     int32_t arg1;
142     int32_t arg2;
143 } qcamera_sm_evt_command_payload_t;
144 
145 // definition for payload type of sending command
146 typedef struct {
147     void *img_ptr;
148     cam_pp_offline_src_config_t *config;
149 } qcamera_sm_evt_reg_face_payload_t;
150 
151 typedef enum {
152     QCAMERA_INTERNAL_EVT_FOCUS_UPDATE,       // focus updating result
153     QCAMERA_INTERNAL_EVT_PREP_SNAPSHOT_DONE, // prepare snapshot done
154     QCAMERA_INTERNAL_EVT_FACE_DETECT_RESULT, // face detection result
155     QCAMERA_INTERNAL_EVT_HISTOGRAM_STATS,    // histogram
156     QCAMERA_INTERNAL_EVT_CROP_INFO,          // crop info
157     QCAMERA_INTERNAL_EVT_ASD_UPDATE,         // asd update result
158     QCAMERA_INTERNAL_EVT_READY_FOR_SNAPSHOT, // Ready for Prepare Snapshot
159     QCAMERA_INTERNAL_EVT_LED_MODE_OVERRIDE, // Led mode override
160     QCAMERA_INTERNAL_EVT_AWB_UPDATE,         // awb update result
161     QCAMERA_INTERNAL_EVT_AE_UPDATE,          // ae update result
162     QCAMERA_INTERNAL_EVT_FOCUS_POS_UPDATE,   // focus position update result
163     QCAMERA_INTERNAL_EVT_HDR_UPDATE,         // HDR scene update
164     QCAMERA_INTERNAL_EVT_RETRO_AEC_UNLOCK,   // retro burst AEC unlock event
165     QCAMERA_INTERNAL_EVT_ZSL_CAPTURE_DONE,   // ZSL capture done event
166     QCAMERA_INTERNAL_EVT_MAX
167 } qcamera_internal_evt_type_t;
168 
169 typedef struct {
170     qcamera_internal_evt_type_t evt_type;
171     union {
172         cam_auto_focus_data_t focus_data;
173         cam_prep_snapshot_state_t prep_snapshot_state;
174         cam_faces_data_t faces_data;
175         cam_hist_stats_t stats_data;
176         cam_crop_data_t crop_data;
177         cam_asd_decision_t asd_data;
178         cam_flash_mode_t led_data;
179         cam_awb_params_t awb_data;
180         cam_3a_params_t ae_data;
181         cam_focus_pos_info_t focus_pos;
182         cam_asd_hdr_scene_data_t hdr_data;
183     };
184 } qcamera_sm_internal_evt_payload_t;
185 
186 class QCameraStateMachine
187 {
188 public:
189     QCameraStateMachine(QCamera2HardwareInterface *ctrl);
190     virtual ~QCameraStateMachine();
191     int32_t procAPI(qcamera_sm_evt_enum_t evt, void *api_payload);
192     int32_t procEvt(qcamera_sm_evt_enum_t evt, void *evt_payload);
193 
194     bool isPreviewRunning(); // check if preview is running
195     bool isPreviewReady(); // check if preview is ready
196     bool isCaptureRunning(); // check if image capture is running
197     bool isNonZSLCaptureRunning(); // check if image capture is running in non ZSL mode
198     String8 dump(); //returns the state information in a string
199     bool isPrepSnapStateRunning();
200     bool isRecording();
201     void releaseThread();
202 
isPreviewCallbackNeeded()203     bool isPreviewCallbackNeeded() { return m_bPreviewCallbackNeeded; };
setPreviewCallbackNeeded(bool enabled)204     int32_t setPreviewCallbackNeeded(bool enabled) {m_bPreviewCallbackNeeded=enabled; return 0;};
205 private:
206     typedef enum {
207         QCAMERA_SM_STATE_PREVIEW_STOPPED,          // preview is stopped
208         QCAMERA_SM_STATE_PREVIEW_READY,            // preview started but preview window is not set yet
209         QCAMERA_SM_STATE_PREVIEWING,               // previewing
210         QCAMERA_SM_STATE_PREPARE_SNAPSHOT,         // prepare snapshot in case aec estimation is
211                                                    // needed for LED flash
212         QCAMERA_SM_STATE_PIC_TAKING,               // taking picture (preview stopped)
213         QCAMERA_SM_STATE_RECORDING,                // recording (preview running)
214         QCAMERA_SM_STATE_VIDEO_PIC_TAKING,         // taking live snapshot during recording (preview running)
215         QCAMERA_SM_STATE_PREVIEW_PIC_TAKING        // taking ZSL/live snapshot (recording stopped but preview running)
216     } qcamera_state_enum_t;
217 
218     typedef enum
219     {
220         QCAMERA_SM_CMD_TYPE_API,                   // cmd from API
221         QCAMERA_SM_CMD_TYPE_EVT,                   // cmd from mm-camera-interface/mm-jpeg-interface event
222         QCAMERA_SM_CMD_TYPE_EXIT,                  // cmd for exiting statemachine cmdThread
223         QCAMERA_SM_CMD_TYPE_MAX
224     } qcamera_sm_cmd_type_t;
225 
226     typedef struct {
227         qcamera_sm_cmd_type_t cmd;                  // cmd type (where it comes from)
228         qcamera_sm_evt_enum_t evt;                  // event type
229         void *evt_payload;                          // ptr to payload
230     } qcamera_sm_cmd_t;
231 
232     int32_t stateMachine(qcamera_sm_evt_enum_t evt, void *payload);
233     int32_t procEvtPreviewStoppedState(qcamera_sm_evt_enum_t evt, void *payload);
234     int32_t procEvtPreviewReadyState(qcamera_sm_evt_enum_t evt, void *payload);
235     int32_t procEvtPreviewingState(qcamera_sm_evt_enum_t evt, void *payload);
236     int32_t procEvtPrepareSnapshotState(qcamera_sm_evt_enum_t evt, void *payload);
237     int32_t procEvtPicTakingState(qcamera_sm_evt_enum_t evt, void *payload);
238     int32_t procEvtRecordingState(qcamera_sm_evt_enum_t evt, void *payload);
239     int32_t procEvtVideoPicTakingState(qcamera_sm_evt_enum_t evt, void *payload);
240     int32_t procEvtPreviewPicTakingState(qcamera_sm_evt_enum_t evt, void *payload);
241 
242     // main statemachine process routine
243     static void *smEvtProcRoutine(void *data);
244 
245     int32_t applyDelayedMsgs();
246 
247     QCamera2HardwareInterface *m_parent;  // ptr to HWI
248     qcamera_state_enum_t m_state;         // statemachine state
249     QCameraQueue api_queue;               // cmd queue for APIs
250     QCameraQueue evt_queue;               // cmd queue for evt from mm-camera-intf/mm-jpeg-intf
251     pthread_t cmd_pid;                    // cmd thread ID
252     cam_semaphore_t cmd_sem;              // semaphore for cmd thread
253     bool m_bDelayPreviewMsgs;             // Delay preview callback enable during ZSL snapshot
254     bool m_bPreviewNeedsRestart;          // Preview needs restart
255     bool m_bPreviewDelayedRestart;        // Preview delayed restart
256     int32_t m_DelayedMsgs;
257     bool m_RestoreZSL;
258     bool m_bPreviewCallbackNeeded;
259 };
260 
261 }; // namespace qcamera
262 
263 #endif /* __QCAMERA_STATEMACHINE_H__ */
264