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 __QCAMERA3HARDWAREINTERFACE_H__
31#define __QCAMERA3HARDWAREINTERFACE_H__
32
33#include <pthread.h>
34#include <utils/List.h>
35#include <utils/KeyedVector.h>
36#include <hardware/camera3.h>
37#include <camera/CameraMetadata.h>
38#include "QCamera3HALHeader.h"
39#include "QCamera3Channel.h"
40
41#include <hardware/power.h>
42
43extern "C" {
44#include <mm_camera_interface.h>
45#include <mm_jpeg_interface.h>
46}
47
48using namespace android;
49
50namespace qcamera {
51
52#ifndef TRUE
53#define TRUE 1
54#endif
55
56#ifndef FALSE
57#define FALSE 0
58#endif
59
60/* Time related macros */
61typedef int64_t nsecs_t;
62#define NSEC_PER_SEC 1000000000LL
63#define NSEC_PER_USEC 1000
64#define NSEC_PER_33MSEC 33000000LL
65
66class QCamera3MetadataChannel;
67class QCamera3PicChannel;
68class QCamera3HeapMemory;
69class QCamera3Exif;
70
71class QCamera3HardwareInterface {
72public:
73    /* static variable and functions accessed by camera service */
74    static camera3_device_ops_t mCameraOps;
75    static int initialize(const struct camera3_device *,
76                const camera3_callback_ops_t *callback_ops);
77    static int configure_streams(const struct camera3_device *,
78                camera3_stream_configuration_t *stream_list);
79    static int register_stream_buffers(const struct camera3_device *,
80                const camera3_stream_buffer_set_t *buffer_set);
81    static const camera_metadata_t* construct_default_request_settings(
82                                const struct camera3_device *, int type);
83    static int process_capture_request(const struct camera3_device *,
84                                camera3_capture_request_t *request);
85    static void get_metadata_vendor_tag_ops(const struct camera3_device *,
86                                               vendor_tag_query_ops_t* ops);
87    static void dump(const struct camera3_device *, int fd);
88    static int flush(const struct camera3_device *);
89    static int close_camera_device(struct hw_device_t* device);
90public:
91    QCamera3HardwareInterface(int cameraId);
92    virtual ~QCamera3HardwareInterface();
93    int openCamera(struct hw_device_t **hw_device);
94    int getMetadata(int type);
95    camera_metadata_t* translateCapabilityToMetadata(int type);
96
97    static int getCamInfo(int cameraId, struct camera_info *info);
98    static int initCapabilities(int cameraId);
99    static int initStaticMetadata(int cameraId);
100    static void makeTable(cam_dimension_t* dimTable, uint8_t size, int32_t* sizeTable);
101    static void makeFPSTable(cam_fps_range_t* fpsTable, uint8_t size,
102                                          int32_t* fpsRangesTable);
103    static void makeOverridesList(cam_scene_mode_overrides_t* overridesTable, uint8_t size,
104                                   uint8_t* overridesList, uint8_t* supported_indexes, int camera_id);
105    static void convertToRegions(cam_rect_t rect, int32_t* region, int weight);
106    static void convertFromRegions(cam_area_t* roi, const camera_metadata_t *settings,
107                                   uint32_t tag);
108    static bool resetIfNeededROI(cam_area_t* roi, const cam_crop_region_t* scalerCropRegion);
109    static void convertLandmarks(cam_face_detection_info_t face, int32_t* landmarks);
110    static void postproc_channel_cb_routine(mm_camera_super_buf_t *recvd_frame,
111                                            void *userdata);
112    static int32_t getScalarFormat(int32_t format);
113    static int32_t getSensorSensitivity(int32_t iso_mode);
114    static void captureResultCb(mm_camera_super_buf_t *metadata,
115                camera3_stream_buffer_t *buffer, uint32_t frame_number,
116                void *userdata);
117
118    int initialize(const camera3_callback_ops_t *callback_ops);
119    int configureStreams(camera3_stream_configuration_t *stream_list);
120    int registerStreamBuffers(const camera3_stream_buffer_set_t *buffer_set);
121    int processCaptureRequest(camera3_capture_request_t *request);
122    void getMetadataVendorTagOps(vendor_tag_query_ops_t* ops);
123    void dump(int fd);
124    int flush();
125
126    int setFrameParameters(camera3_capture_request_t *request, cam_stream_ID_t streamID);
127    int translateMetadataToParameters(const camera3_capture_request_t *request);
128    camera_metadata_t* translateCbUrgentMetadataToResultMetadata (
129                             metadata_buffer_t *metadata);
130
131    camera_metadata_t* translateCbMetadataToResultMetadata(metadata_buffer_t *metadata,
132                            nsecs_t timestamp, int32_t request_id, int32_t BlobRequest,
133                            jpeg_settings_t* InputJpegSettings);
134    int getJpegSettings(const camera_metadata_t *settings);
135    int initParameters();
136    void deinitParameters();
137    int getMaxUnmatchedFramesInQueue();
138    QCamera3ReprocessChannel *addOnlineReprocChannel(QCamera3Channel *pInputChannel, QCamera3PicChannel *picChHandle);
139    bool needRotationReprocess();
140    bool needReprocess();
141    bool isWNREnabled();
142    cam_denoise_process_type_t getWaveletDenoiseProcessPlate();
143
144    void captureResultCb(mm_camera_super_buf_t *metadata,
145                camera3_stream_buffer_t *buffer, uint32_t frame_number);
146
147    typedef struct {
148        uint8_t fwk_name;
149        uint8_t hal_name;
150    } QCameraMap;
151
152private:
153
154    int openCamera();
155    int closeCamera();
156    int AddSetParmEntryToBatch(parm_buffer_t *p_table,
157                               cam_intf_parm_type_t paramType,
158                               uint32_t paramLength,
159                               void *paramValue);
160    static int8_t lookupHalName(const QCameraMap arr[],
161                      int len, int fwk_name);
162    static int8_t lookupFwkName(const QCameraMap arr[],
163                      int len, int hal_name);
164
165    int validateCaptureRequest(camera3_capture_request_t *request);
166
167    void deriveMinFrameDuration();
168    int64_t getMinFrameDuration(const camera3_capture_request_t *request);
169
170    void handleMetadataWithLock(mm_camera_super_buf_t *metadata_buf);
171    void handleBufferWithLock(camera3_stream_buffer_t *buffer,
172        uint32_t frame_number);
173    void unblockRequestIfNecessary();
174public:
175
176    bool needOnlineRotation();
177    void getThumbnailSize(cam_dimension_t &dim);
178    int getJpegQuality();
179    int calcMaxJpegSize();
180    QCamera3Exif *getExifData();
181public:
182    static int kMaxInFlight;
183private:
184    camera3_device_t   mCameraDevice;
185    uint8_t            mCameraId;
186    mm_camera_vtbl_t  *mCameraHandle;
187    bool               mCameraOpened;
188    bool               mCameraInitialized;
189    camera_metadata_t *mDefaultMetadata[CAMERA3_TEMPLATE_COUNT];
190    int mBlobRequest;
191
192    const camera3_callback_ops_t *mCallbackOps;
193
194    camera3_stream_t *mInputStream;
195    QCamera3MetadataChannel *mMetadataChannel;
196    QCamera3PicChannel *mPictureChannel;
197
198     //First request yet to be processed after configureStreams
199    bool mFirstRequest;
200    QCamera3HeapMemory *mParamHeap;
201    parm_buffer_t* mParameters;
202    bool m_bWNROn;
203
204    /* Data structure to store pending request */
205    typedef struct {
206        camera3_stream_t *stream;
207        camera3_stream_buffer_t *buffer;
208    } RequestedBufferInfo;
209    typedef struct {
210        uint32_t frame_number;
211        uint32_t num_buffers;
212        int32_t request_id;
213        List<RequestedBufferInfo> buffers;
214        int blob_request;
215        jpeg_settings_t input_jpeg_settings;
216        nsecs_t timestamp;
217        uint8_t bNotified;
218        int input_buffer_present;
219    } PendingRequestInfo;
220    typedef struct {
221        uint32_t frame_number;
222        uint32_t stream_ID;
223    } PendingFrameDropInfo;
224    typedef KeyedVector<camera3_stream_t *, uint32_t> PendingBuffersMap;
225    /*Data structure to store metadata information*/
226    typedef struct {
227       mm_camera_super_buf_t* meta_buf;
228       buffer_handle_t*       zsl_buf_hdl;
229       uint32_t               frame_number;
230    }MetadataBufferInfo;
231
232    List<MetadataBufferInfo> mStoredMetadataList;
233    List<PendingRequestInfo> mPendingRequestsList;
234    List<PendingFrameDropInfo> mPendingFrameDropList;
235    PendingBuffersMap mPendingBuffersMap;
236    pthread_cond_t mRequestCond;
237    int mPendingRequest;
238    int32_t mCurrentRequestId;
239
240    //mutex for serialized access to camera3_device_ops_t functions
241    pthread_mutex_t mMutex;
242
243    jpeg_settings_t* mJpegSettings;
244    metadata_response_t mMetadataResponse;
245    List<stream_info_t*> mStreamInfo;
246    bool mIsZslMode;
247
248    int64_t mMinProcessedFrameDuration;
249    int64_t mMinJpegFrameDuration;
250    int64_t mMinRawFrameDuration;
251
252    power_module_t *m_pPowerModule;   // power module
253
254#ifdef HAS_MULTIMEDIA_HINTS
255    bool mHdrHint;
256#endif
257    static const QCameraMap EFFECT_MODES_MAP[];
258    static const QCameraMap WHITE_BALANCE_MODES_MAP[];
259    static const QCameraMap SCENE_MODES_MAP[];
260    static const QCameraMap FOCUS_MODES_MAP[];
261    static const QCameraMap ANTIBANDING_MODES_MAP[];
262    static const QCameraMap AE_FLASH_MODE_MAP[];
263    static const QCameraMap FLASH_MODES_MAP[];
264    static const QCameraMap FACEDETECT_MODES_MAP[];
265
266    static pthread_mutex_t mCameraSessionLock;
267    static unsigned int mCameraSessionActive;
268};
269
270}; // namespace qcamera
271
272#endif /* __QCAMERA2HARDWAREINTERFACE_H__ */
273