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 
43 extern "C" {
44 #include <mm_camera_interface.h>
45 #include <mm_jpeg_interface.h>
46 }
47 #ifdef CDBG
48 #undef CDBG
49 #endif //#ifdef CDBG
50 #define CDBG(fmt, args...) ALOGD_IF(gCamHal3LogLevel >= 2, fmt, ##args)
51 
52 #ifdef CDBG_HIGH
53 #undef CDBG_HIGH
54 #endif //#ifdef CDBG_HIGH
55 #define CDBG_HIGH(fmt, args...) ALOGD_IF(gCamHal3LogLevel >= 1, fmt, ##args)
56 
57 using namespace android;
58 
59 namespace qcamera {
60 
61 #ifndef TRUE
62 #define TRUE 1
63 #endif
64 
65 #ifndef FALSE
66 #define FALSE 0
67 #endif
68 
69 /* Time related macros */
70 typedef int64_t nsecs_t;
71 #define NSEC_PER_SEC 1000000000LL
72 #define NSEC_PER_USEC 1000
73 #define NSEC_PER_33MSEC 33000000LL
74 
75 extern volatile uint32_t gCamHal3LogLevel;
76 
77 class QCamera3MetadataChannel;
78 class QCamera3PicChannel;
79 class QCamera3HeapMemory;
80 class QCamera3Exif;
81 
82 typedef struct {
83     camera3_stream_t *stream;
84     camera3_stream_buffer_set_t buffer_set;
85     stream_status_t status;
86     int registered;
87     QCamera3Channel *channel;
88 } stream_info_t;
89 
90 class QCamera3HardwareInterface {
91 public:
92     /* static variable and functions accessed by camera service */
93     static camera3_device_ops_t mCameraOps;
94     static int initialize(const struct camera3_device *,
95                 const camera3_callback_ops_t *callback_ops);
96     static int configure_streams(const struct camera3_device *,
97                 camera3_stream_configuration_t *stream_list);
98     static const camera_metadata_t* construct_default_request_settings(
99                                 const struct camera3_device *, int type);
100     static int process_capture_request(const struct camera3_device *,
101                                 camera3_capture_request_t *request);
102 
103     static void dump(const struct camera3_device *, int fd);
104     static int flush(const struct camera3_device *);
105     static int close_camera_device(struct hw_device_t* device);
106 
107 public:
108     QCamera3HardwareInterface(int cameraId,
109             const camera_module_callbacks_t *callbacks);
110     virtual ~QCamera3HardwareInterface();
111     static void camEvtHandle(uint32_t camera_handle, mm_camera_event_t *evt,
112                                           void *user_data);
113     int openCamera(struct hw_device_t **hw_device);
114     int getMetadata(int type);
115     camera_metadata_t* translateCapabilityToMetadata(int type);
116 
117     static int getCamInfo(int cameraId, struct camera_info *info);
118     static int initCapabilities(int cameraId);
119     static int initStaticMetadata(int cameraId);
120     static void makeTable(cam_dimension_t* dimTable, uint8_t size, int32_t* sizeTable);
121     static void makeFPSTable(cam_fps_range_t* fpsTable, uint8_t size,
122                                           int32_t* fpsRangesTable);
123     static void makeOverridesList(cam_scene_mode_overrides_t* overridesTable, uint8_t size,
124                                    uint8_t* overridesList, uint8_t* supported_indexes, int camera_id);
125     static uint8_t filterJpegSizes(int32_t* jpegSizes, int32_t* processedSizes,
126                                    uint8_t processedSizesCnt, uint8_t maxCount,
127                                    cam_rect_t active_array_size, uint8_t downscale_factor);
128     static void convertToRegions(cam_rect_t rect, int32_t* region, int weight);
129     static void convertFromRegions(cam_area_t* roi, const camera_metadata_t *settings,
130                                    uint32_t tag);
131     static bool resetIfNeededROI(cam_area_t* roi, const cam_crop_region_t* scalerCropRegion);
132     static void convertLandmarks(cam_face_detection_info_t face, int32_t* landmarks);
133     static void postproc_channel_cb_routine(mm_camera_super_buf_t *recvd_frame,
134                                             void *userdata);
135     static int32_t getScalarFormat(int32_t format);
136     static int32_t getSensorSensitivity(int32_t iso_mode);
137 
138     static double computeNoiseModelEntryS(int32_t sensitivity);
139     static double computeNoiseModelEntryO(int32_t sensitivity);
140 
141     static void captureResultCb(mm_camera_super_buf_t *metadata,
142                 camera3_stream_buffer_t *buffer, uint32_t frame_number,
143                 void *userdata);
144 
145     int initialize(const camera3_callback_ops_t *callback_ops);
146     int configureStreams(camera3_stream_configuration_t *stream_list);
147     int processCaptureRequest(camera3_capture_request_t *request);
148     void dump(int fd);
149     int flush();
150 
151     int setFrameParameters(camera3_capture_request_t *request,
152             cam_stream_ID_t streamID, uint32_t snapshotStreamId);
153     int32_t setReprocParameters(camera3_capture_request_t *request,
154             metadata_buffer_t *reprocParam, uint32_t snapshotStreamId);
155     int translateToHalMetadata(const camera3_capture_request_t *request,
156             metadata_buffer_t *parm, uint32_t snapshotStreamId);
157     camera_metadata_t* translateCbUrgentMetadataToResultMetadata (
158                              metadata_buffer_t *metadata);
159 
160     camera_metadata_t* translateFromHalMetadata(metadata_buffer_t *metadata,
161                             nsecs_t timestamp, int32_t request_id,
162                             const CameraMetadata& jpegMetadata, uint8_t pipeline_depth,
163                             uint8_t capture_intent);
164     int getJpegSettings(const camera_metadata_t *settings);
165     int initParameters();
166     void deinitParameters();
167     QCamera3ReprocessChannel *addOfflineReprocChannel(const reprocess_config_t &config,
168             QCamera3PicChannel *picChHandle, metadata_buffer_t *metadata);
169     bool needRotationReprocess();
170     bool needReprocess(uint32_t postprocess_mask);
171     bool needJpegRotation();
172     bool isWNREnabled();
173     bool isCACEnabled();
174     cam_denoise_process_type_t getWaveletDenoiseProcessPlate();
175 
176     void captureResultCb(mm_camera_super_buf_t *metadata,
177                 camera3_stream_buffer_t *buffer, uint32_t frame_number);
178 
179     typedef struct {
180         uint32_t fwk_name;
181         uint8_t hal_name;
182     } QCameraMap;
183 
184     typedef struct {
185         const char *const desc;
186         cam_cds_mode_type_t val;
187     } QCameraPropMap;
188 
189 private:
190 
191     int openCamera();
192     int closeCamera();
193     int AddSetParmEntryToBatch(parm_buffer_t *p_table,
194                                cam_intf_parm_type_t paramType,
195                                uint32_t paramLength,
196                                void *paramValue);
197     static int8_t lookupHalName(const QCameraMap arr[],
198                       int len, unsigned int fwk_name);
199     static int32_t lookupFwkName(const QCameraMap arr[],
200                       int len, int hal_name);
201     static cam_cds_mode_type_t lookupProp(const QCameraPropMap arr[],
202             int len, const char *name);
203     static int calcMaxJpegSize(uint8_t camera_id);
204     cam_dimension_t getMaxRawSize(uint8_t camera_id);
205 
206     int validateCaptureRequest(camera3_capture_request_t *request);
207     int validateStreamDimensions(camera3_stream_configuration_t *streamList);
208     void deriveMinFrameDuration();
209     int32_t handlePendingReprocResults(uint32_t frame_number);
210     int64_t getMinFrameDuration(const camera3_capture_request_t *request);
211     void handleMetadataWithLock(mm_camera_super_buf_t *metadata_buf);
212     void handleBufferWithLock(camera3_stream_buffer_t *buffer,
213         uint32_t frame_number);
214     void unblockRequestIfNecessary();
215     void dumpMetadataToFile(tuning_params_t &meta,
216                             uint32_t &dumpFrameCount,
217                             int32_t enabled,
218                             const char *type,
219                             uint32_t frameNumber);
220     static void getLogLevel();
221 
222     void cleanAndSortStreamInfo();
223     int queueReprocMetadata(metadata_buffer_t *metadata);
224     void extractJpegMetadata(CameraMetadata& jpegMetadata,
225             const camera3_capture_request_t *request);
226     bool isSupportChannelNeeded(camera3_stream_configuration_t *streamList);
227 public:
228     cam_dimension_t calcMaxJpegDim();
229     bool needOnlineRotation();
230     int getJpegQuality();
231     QCamera3Exif *getExifData();
232 private:
233     camera3_device_t   mCameraDevice;
234     uint8_t            mCameraId;
235     mm_camera_vtbl_t  *mCameraHandle;
236     bool               mCameraOpened;
237     bool               mCameraInitialized;
238     camera_metadata_t *mDefaultMetadata[CAMERA3_TEMPLATE_COUNT];
239     int mBlobRequest;
240 
241     const camera3_callback_ops_t *mCallbackOps;
242 
243     camera3_stream_t *mInputStream;
244     QCamera3MetadataChannel *mMetadataChannel;
245     QCamera3PicChannel *mPictureChannel;
246     QCamera3RawChannel *mRawChannel;
247     QCamera3SupportChannel *mSupportChannel;
248     QCamera3RawDumpChannel *mRawDumpChannel;
249 
250      //First request yet to be processed after configureStreams
251     bool mFirstRequest;
252     bool mFlush;
253     bool mEnableRawDump;
254     QCamera3HeapMemory *mParamHeap;
255     metadata_buffer_t* mParameters;
256     bool m_bWNROn;
257     bool m_bIsVideo;
258     bool m_bIs4KVideo;
259     uint8_t mEisEnable;
260 
261     /* Data structure to store pending request */
262     typedef struct {
263         camera3_stream_t *stream;
264         camera3_stream_buffer_t *buffer;
265     } RequestedBufferInfo;
266     typedef struct {
267         uint32_t frame_number;
268         uint32_t num_buffers;
269         int32_t request_id;
270         List<RequestedBufferInfo> buffers;
271         int blob_request;
272         uint8_t bUrgentReceived;
273         nsecs_t timestamp;
274         camera3_stream_buffer_t *input_buffer;
275         const camera_metadata_t *settings;
276         CameraMetadata jpegMetadata;
277         uint8_t pipeline_depth;
278         uint32_t partial_result_cnt;
279         uint8_t capture_intent;
280     } PendingRequestInfo;
281     typedef struct {
282         uint32_t frame_number;
283         uint32_t stream_ID;
284     } PendingFrameDropInfo;
285 
286     // Store the Pending buffers for Flushing
287     typedef struct {
288         // Frame number pertaining to the buffer
289         uint32_t frame_number;
290         camera3_stream_t *stream;
291         // Buffer handle
292         buffer_handle_t *buffer;
293     } PendingBufferInfo;
294 
295     typedef struct {
296         // Total number of buffer requests pending
297         uint32_t num_buffers;
298         // List of pending buffers
299         List<PendingBufferInfo> mPendingBufferList;
300     } PendingBuffersMap;
301 
302     typedef struct {
303         camera3_notify_msg_t notify_msg;
304         camera3_stream_buffer_t buffer;
305         uint32_t frame_number;
306     } PendingReprocessResult;
307 
308     typedef KeyedVector<uint32_t, Vector<PendingBufferInfo> > FlushMap;
309 
310     List<PendingReprocessResult> mPendingReprocessResultList;
311     List<PendingRequestInfo> mPendingRequestsList;
312     List<PendingFrameDropInfo> mPendingFrameDropList;
313     PendingBuffersMap mPendingBuffersMap;
314     pthread_cond_t mRequestCond;
315     int mPendingRequest;
316     bool mWokenUpByDaemon;
317     int32_t mCurrentRequestId;
318     camera3_capture_result_t *mLoopBackResult;
319     nsecs_t mLoopBackTimestamp;
320 
321     //mutex for serialized access to camera3_device_ops_t functions
322     pthread_mutex_t mMutex;
323 
324     List<stream_info_t*> mStreamInfo;
325 
326     int64_t mMinProcessedFrameDuration;
327     int64_t mMinJpegFrameDuration;
328     int64_t mMinRawFrameDuration;
329     power_module_t *m_pPowerModule;   // power module
330 
331     uint32_t mMetaFrameCount;
332     const camera_module_callbacks_t *mCallbacks;
333 
334     uint8_t mCaptureIntent;
335 
336     static const QCameraMap EFFECT_MODES_MAP[];
337     static const QCameraMap WHITE_BALANCE_MODES_MAP[];
338     static const QCameraMap SCENE_MODES_MAP[];
339     static const QCameraMap FOCUS_MODES_MAP[];
340     static const QCameraMap COLOR_ABERRATION_MAP[];
341     static const QCameraMap ANTIBANDING_MODES_MAP[];
342     static const QCameraMap AE_FLASH_MODE_MAP[];
343     static const QCameraMap FLASH_MODES_MAP[];
344     static const QCameraMap FACEDETECT_MODES_MAP[];
345     static const QCameraMap FOCUS_CALIBRATION_MAP[];
346     static const QCameraMap TEST_PATTERN_MAP[];
347     static const QCameraMap REFERENCE_ILLUMINANT_MAP[];
348     static const QCameraPropMap CDS_MAP[];
349 };
350 
351 }; // namespace qcamera
352 
353 #endif /* __QCAMERA2HARDWAREINTERFACE_H__ */
354