1/* Copyright (c) 2016-2017, 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 __QCAMERAFOVCONTROL_H__
31#define __QCAMERAFOVCONTROL_H__
32
33#include <utils/Mutex.h>
34#include "cam_intf.h"
35#include "QCameraExtZoomTranslator.h"
36
37using namespace android;
38
39namespace qcamera {
40
41typedef enum {
42    AE_SETTLED,
43    AE_CONVERGING
44} ae_status;
45
46typedef enum {
47    AF_VALID,
48    AF_INVALID
49} af_status;
50
51typedef enum {
52    CAM_POSITION_LEFT,
53    CAM_POSITION_RIGHT
54} cam_relative_position;
55
56typedef enum {
57    STATE_WIDE,
58    STATE_TRANSITION,
59    STATE_TELE
60} dual_cam_state;
61
62typedef enum {
63    ZOOM_STABLE,
64    ZOOM_IN,
65    ZOOM_OUT
66} dual_cam_zoom_dir;
67
68typedef enum {
69    CAM_TYPE_WIDE,
70    CAM_TYPE_TELE
71} cam_type;
72
73
74
75typedef struct {
76    ae_status status;
77    float     luxIndex;
78} ae_info;
79
80typedef struct {
81    af_status status;
82    uint32_t  focusDistCm;
83} af_info;
84
85typedef struct {
86    ae_info ae;
87    af_info af;
88} status_3A_t;
89
90typedef struct {
91    status_3A_t main;
92    status_3A_t aux;
93} dual_cam_3A_status_t;
94
95typedef struct {
96    int32_t shiftHorz;
97    int32_t shiftVert;
98} spatial_align_shift_t;
99
100typedef struct {
101    uint8_t               readyStatus;
102    uint8_t               camMasterHint;
103    uint8_t               camMasterPreview;
104    uint8_t               camMaster3A;
105    uint32_t              activeCameras;
106    spatial_align_shift_t shiftWide;
107    spatial_align_shift_t shiftTele;
108    spatial_align_shift_t shiftAfRoiWide;
109    spatial_align_shift_t shiftAfRoiTele;
110} spatial_align_result_t;
111
112typedef struct {
113    float    cropRatio;
114    float    cutOverFactor;
115    float    cutOverWideToTele;
116    float    cutOverTeleToWide;
117    float    transitionHigh;
118    float    transitionLow;
119    uint32_t waitTimeForHandoffMs;
120} dual_cam_transition_params_t;
121
122typedef struct {
123    bool                         configCompleted;
124    uint32_t                     zoomMain;
125    uint32_t                     zoomAux;
126    uint32_t                     zoomWide;
127    uint32_t                     zoomTele;
128    uint32_t                     zoomWidePrev;
129    uint32_t                     zoomMainPrev;
130    uint32_t                    *zoomRatioTable;
131    uint32_t                     zoomRatioTableCount;
132    uint32_t                     zoomStableCount;
133    dual_cam_zoom_dir            zoomDirection;
134    zoom_trans_init_data         zoomTransInitData;
135    cam_sync_type_t              camWide;
136    cam_sync_type_t              camTele;
137    dual_cam_state               camState;
138    dual_cam_3A_status_t         status3A;
139    cam_dimension_t              previewSize;
140    cam_dimension_t              ispOutSize;
141    spatial_align_result_t       spatialAlignResult;
142    uint32_t                     availableSpatialAlignSolns;
143    float                        camMainWidthMargin;
144    float                        camMainHeightMargin;
145    float                        camAuxWidthMargin;
146    float                        camAuxHeightMargin;
147    bool                         camcorderMode;
148    bool                         wideCamStreaming;
149    bool                         teleCamStreaming;
150    bool                         fallbackEnabled;
151    bool                         fallbackToWide;
152    float                        basicFovRatio;
153    uint32_t                     brightnessStableCount;
154    uint32_t                     focusDistStableCount;
155    dual_cam_transition_params_t transitionParams;
156    uint32_t                     afStatusMain;
157    uint32_t                     afStatusAux;
158    bool                         lpmEnabled;
159} fov_control_data_t;
160
161typedef struct {
162    bool     enablePostProcess;
163    float    zoomMin;
164    float    zoomMax;
165    uint16_t luxMin;
166    uint16_t focusDistanceMin;
167} snapshot_pp_config_t;
168
169typedef struct {
170    float    percentMarginHysterisis;
171    float    percentMarginAux;
172    float    percentMarginMain;
173    uint32_t waitTimeForHandoffMs;
174    uint16_t auxSwitchBrightnessMin;
175    uint16_t auxSwitchFocusDistCmMin;
176    uint16_t zoomStableCountThreshold;
177    uint16_t focusDistStableCountThreshold;
178    uint16_t brightnessStableCountThreshold;
179    snapshot_pp_config_t snapshotPPConfig;
180} fov_control_config_t;
181
182typedef struct{
183    uint32_t sensorStreamWidth;
184    uint32_t sensorStreamHeight;
185    float    focalLengthMm;
186    float    pixelPitchUm;
187} intrinsic_cam_params_t;
188
189typedef struct {
190    uint32_t               minFocusDistanceCm;
191    cam_relative_position  positionAux;
192    intrinsic_cam_params_t paramsMain;
193    intrinsic_cam_params_t paramsAux;
194} dual_cam_params_t;
195
196typedef struct {
197    bool            isValid;
198    cam_sync_type_t camMasterPreview;
199    cam_sync_type_t camMaster3A;
200    uint32_t        activeCameras;
201    bool            snapshotPostProcess;
202    bool            snapshotPostProcessZoomRange;
203} fov_control_result_t;
204
205
206class QCameraFOVControl {
207public:
208    ~QCameraFOVControl();
209    static QCameraFOVControl* create(cam_capability_t *capsMainCam, cam_capability_t* capsAuxCam);
210    int32_t updateConfigSettings(parm_buffer_t* paramsMainCam, parm_buffer_t* paramsAuxCam);
211    cam_capability_t consolidateCapabilities(cam_capability_t* capsMainCam,
212            cam_capability_t* capsAuxCam);
213    int32_t translateInputParams(parm_buffer_t* paramsMainCam, parm_buffer_t *paramsAuxCam);
214    metadata_buffer_t* processResultMetadata(metadata_buffer_t* metaMainCam,
215            metadata_buffer_t* metaAuxCam);
216    fov_control_result_t getFovControlResult();
217    cam_frame_margins_t getFrameMargins(int8_t masterCamera);
218
219private:
220    QCameraFOVControl();
221    bool validateAndExtractParameters(cam_capability_t  *capsMainCam,
222            cam_capability_t  *capsAuxCam);
223    bool calculateBasicFovRatio();
224    bool combineFovAdjustment();
225    void  calculateDualCamTransitionParams();
226    void convertUserZoomToWideAndTele(uint32_t zoom);
227    uint32_t readjustZoomForTele(uint32_t zoomWide);
228    uint32_t readjustZoomForWide(uint32_t zoomTele);
229    uint32_t findZoomRatio(uint32_t zoom);
230    inline uint32_t findZoomValue(uint32_t zoomRatio);
231    cam_face_detection_data_t translateRoiFD(cam_face_detection_data_t faceDetectionInfo,
232            cam_sync_type_t cam);
233    cam_roi_info_t translateFocusAreas(cam_roi_info_t roiAfMain, cam_sync_type_t cam);
234    cam_set_aec_roi_t translateMeteringAreas(cam_set_aec_roi_t roiAecMain, cam_sync_type_t cam);
235    void generateFovControlResult();
236    bool isMainCamFovWider();
237    bool isSpatialAlignmentReady();
238    void resetVars();
239    bool canSwitchMasterTo(cam_type cam);
240    bool sacRequestedDualZone();
241
242    Mutex                           mMutex;
243    fov_control_config_t            mFovControlConfig;
244    fov_control_data_t              mFovControlData;
245    fov_control_result_t            mFovControlResult;
246    dual_cam_params_t               mDualCamParams;
247    QCameraExtZoomTranslator       *mZoomTranslator;
248};
249
250}; // namespace qcamera
251
252#endif /* __QCAMERAFOVCONTROL_H__ */
253