1/* Copyright (c) 2012-2015, 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#include "QCamera3CropRegionMapper.h"
41#include "QCameraPerf.h"
42
43extern "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#ifdef CDBG_FATAL_IF
58#undef CDBG_FATAL_IF
59#endif //#ifdef CDBG_FATAL_IF
60#define CDBG_FATAL_IF(cond, ...) LOG_ALWAYS_FATAL_IF(cond, ## __VA_ARGS__)
61
62using namespace android;
63
64namespace qcamera {
65
66#ifndef TRUE
67#define TRUE 1
68#endif
69
70#ifndef FALSE
71#define FALSE 0
72#endif
73
74/* Time related macros */
75typedef int64_t nsecs_t;
76#define NSEC_PER_SEC 1000000000LLU
77#define NSEC_PER_USEC 1000LLU
78#define NSEC_PER_33MSEC 33000000LLU
79
80typedef enum {
81    SET_ENABLE,
82    SET_CONTROLENABLE,
83    SET_RELOAD_CHROMATIX,
84    SET_STATUS,
85} optype_t;
86
87#define MODULE_ALL 0
88
89extern volatile uint32_t gCamHal3LogLevel;
90
91class QCamera3MetadataChannel;
92class QCamera3PicChannel;
93class QCamera3HeapMemory;
94class QCamera3Exif;
95
96typedef struct {
97    camera3_stream_t *stream;
98    camera3_stream_buffer_set_t buffer_set;
99    stream_status_t status;
100    int registered;
101    QCamera3ProcessingChannel *channel;
102} stream_info_t;
103
104class QCamera3HardwareInterface {
105public:
106    /* static variable and functions accessed by camera service */
107    static camera3_device_ops_t mCameraOps;
108    static int initialize(const struct camera3_device *,
109                const camera3_callback_ops_t *callback_ops);
110    static int configure_streams(const struct camera3_device *,
111                camera3_stream_configuration_t *stream_list);
112    static const camera_metadata_t* construct_default_request_settings(
113                                const struct camera3_device *, int type);
114    static int process_capture_request(const struct camera3_device *,
115                                camera3_capture_request_t *request);
116
117    static void dump(const struct camera3_device *, int fd);
118    static int flush(const struct camera3_device *);
119    static int close_camera_device(struct hw_device_t* device);
120
121public:
122    QCamera3HardwareInterface(uint32_t cameraId,
123            const camera_module_callbacks_t *callbacks);
124    virtual ~QCamera3HardwareInterface();
125    static void camEvtHandle(uint32_t camera_handle, mm_camera_event_t *evt,
126                                          void *user_data);
127    int openCamera(struct hw_device_t **hw_device);
128    camera_metadata_t* translateCapabilityToMetadata(int type);
129
130    static int getCamInfo(uint32_t cameraId, struct camera_info *info);
131    static int initCapabilities(uint32_t cameraId);
132    static int initStaticMetadata(uint32_t cameraId);
133    static void makeTable(cam_dimension_t *dimTable, size_t size,
134            size_t max_size, int32_t *sizeTable);
135    static void makeFPSTable(cam_fps_range_t *fpsTable, size_t size,
136            size_t max_size, int32_t *fpsRangesTable);
137    static void makeOverridesList(cam_scene_mode_overrides_t *overridesTable,
138            size_t size, size_t max_size, uint8_t *overridesList,
139            uint8_t *supported_indexes, uint32_t camera_id);
140    static size_t filterJpegSizes(int32_t *jpegSizes, int32_t *processedSizes,
141            size_t processedSizesCnt, size_t maxCount, cam_rect_t active_array_size,
142            uint8_t downscale_factor);
143    static void convertToRegions(cam_rect_t rect, int32_t* region, int weight);
144    static void convertFromRegions(cam_area_t &roi, const camera_metadata_t *settings,
145                                   uint32_t tag);
146    static bool resetIfNeededROI(cam_area_t* roi, const cam_crop_region_t* scalerCropRegion);
147    static void convertLandmarks(cam_face_detection_info_t face, int32_t* landmarks);
148    static int32_t getScalarFormat(int32_t format);
149    static int32_t getSensorSensitivity(int32_t iso_mode);
150
151    double computeNoiseModelEntryS(int32_t sensitivity);
152    double computeNoiseModelEntryO(int32_t sensitivity);
153
154    static void captureResultCb(mm_camera_super_buf_t *metadata,
155                camera3_stream_buffer_t *buffer, uint32_t frame_number,
156                bool isInputBuffer, void *userdata);
157
158    int initialize(const camera3_callback_ops_t *callback_ops);
159    int configureStreams(camera3_stream_configuration_t *stream_list);
160    int configureStreamsPerfLocked(camera3_stream_configuration_t *stream_list);
161    int processCaptureRequest(camera3_capture_request_t *request);
162    void dump(int fd);
163    int flush();
164
165    int setFrameParameters(camera3_capture_request_t *request,
166            cam_stream_ID_t streamID, int blob_request, uint32_t snapshotStreamId);
167    int32_t setReprocParameters(camera3_capture_request_t *request,
168            metadata_buffer_t *reprocParam, uint32_t snapshotStreamId);
169    int translateToHalMetadata(const camera3_capture_request_t *request,
170            metadata_buffer_t *parm, uint32_t snapshotStreamId);
171    camera_metadata_t* translateCbUrgentMetadataToResultMetadata (
172                             metadata_buffer_t *metadata, bool lastUrgentMetadataInBatch);
173    camera_metadata_t* translateFromHalMetadata(metadata_buffer_t *metadata,
174                            nsecs_t timestamp, int32_t request_id,
175                            const CameraMetadata& jpegMetadata, uint8_t pipeline_depth,
176                            uint8_t capture_intent, uint8_t hybrid_ae_enable,
177                            bool pprocDone, bool dynamic_blklvl,
178                            bool lastMetadataInBatch);
179    camera_metadata_t* saveRequestSettings(const CameraMetadata& jpegMetadata,
180                            camera3_capture_request_t *request);
181    int initParameters();
182    void deinitParameters();
183    QCamera3ReprocessChannel *addOfflineReprocChannel(const reprocess_config_t &config,
184            QCamera3ProcessingChannel *inputChHandle);
185    bool needRotationReprocess();
186    bool needReprocess(uint32_t postprocess_mask);
187    bool needJpegRotation();
188    cam_denoise_process_type_t getWaveletDenoiseProcessPlate();
189    cam_denoise_process_type_t getTemporalDenoiseProcessPlate();
190
191    void captureResultCb(mm_camera_super_buf_t *metadata,
192                camera3_stream_buffer_t *buffer, uint32_t frame_number,
193                bool isInputBuffer);
194    cam_dimension_t calcMaxJpegDim();
195    bool needOnlineRotation();
196    uint32_t getJpegQuality();
197    QCamera3Exif *getExifData();
198    mm_jpeg_exif_params_t get3AExifParams();
199    uint8_t getMobicatMask();
200    static void getFlashInfo(const int cameraId,
201            bool& hasFlash,
202            char (&flashNode)[QCAMERA_MAX_FILEPATH_LENGTH]);
203    const char *getEepromVersionInfo();
204    const uint32_t *getLdafCalib();
205    void get3AVersion(cam_q3a_version_t &swVersion);
206
207    template <typename fwkType, typename halType> struct QCameraMap {
208        fwkType fwk_name;
209        halType hal_name;
210    };
211
212    typedef struct {
213        const char *const desc;
214        cam_cds_mode_type_t val;
215    } QCameraPropMap;
216
217    bool getBlackLevelRegion(int (&opticalBlackRegions)[4]);
218    void sendDynamicBlackLevel(float blacklevel[4], uint32_t frame_number);
219    void sendDynamicBlackLevelWithLock(float blacklevel[4], uint32_t frame_number);
220
221private:
222
223    int openCamera();
224    int closeCamera();
225    static size_t calcMaxJpegSize(uint32_t camera_id);
226    cam_dimension_t getMaxRawSize(uint32_t camera_id);
227    static void addStreamConfig(Vector<int32_t> &available_stream_configs,
228            int32_t scalar_format, const cam_dimension_t &dim,
229            int32_t config_type);
230
231    int validateCaptureRequest(camera3_capture_request_t *request);
232    int validateStreamDimensions(camera3_stream_configuration_t *streamList);
233    int validateStreamRotations(camera3_stream_configuration_t *streamList);
234    int validateUsageFlags(const camera3_stream_configuration_t *streamList);
235    void deriveMinFrameDuration();
236    int32_t handlePendingReprocResults(uint32_t frame_number);
237    int64_t getMinFrameDuration(const camera3_capture_request_t *request);
238    void handleMetadataWithLock(mm_camera_super_buf_t *metadata_buf,
239            bool free_and_bufdone_meta_buf,
240            bool lastUrgentMetadataInBatch,
241            bool lastMetadataInBatch);
242    void handleBatchMetadata(mm_camera_super_buf_t *metadata_buf,
243            bool free_and_bufdone_meta_buf);
244    void handleBufferWithLock(camera3_stream_buffer_t *buffer,
245            uint32_t frame_number);
246    void handleInputBufferWithLock(camera3_stream_buffer_t *buffer,
247            uint32_t frame_number);
248    void unblockRequestIfNecessary();
249    void dumpMetadataToFile(tuning_params_t &meta, uint32_t &dumpFrameCount,
250            bool enabled, const char *type, uint32_t frameNumber);
251    static void getLogLevel();
252
253    void cleanAndSortStreamInfo();
254    void extractJpegMetadata(CameraMetadata& jpegMetadata,
255            const camera3_capture_request_t *request);
256
257    bool isSupportChannelNeeded(camera3_stream_configuration_t *streamList,
258            cam_stream_size_info_t stream_config_info);
259    int32_t setMobicat();
260
261    int32_t setHalFpsRange(const CameraMetadata &settings,
262            metadata_buffer_t *hal_metadata);
263    int32_t extractSceneMode(const CameraMetadata &frame_settings, uint8_t metaMode,
264            metadata_buffer_t *hal_metadata);
265    int32_t numOfSizesOnEncoder(const camera3_stream_configuration_t *streamList,
266            const cam_dimension_t &maxViewfinderSize);
267
268    void enablePowerHint();
269    void disablePowerHint();
270    int32_t getSensorOutputSize(cam_dimension_t &sensor_dim);
271    int32_t dynamicUpdateMetaStreamInfo();
272    int32_t startAllChannels();
273    int32_t stopAllChannels();
274    int32_t notifyErrorForPendingRequests();
275    int32_t getReprocessibleOutputStreamId(uint32_t &id);
276
277    bool isOnEncoder(const cam_dimension_t max_viewfinder_size,
278            uint32_t width, uint32_t height);
279    void hdrPlusPerfLock(mm_camera_super_buf_t *metadata_buf);
280
281    int32_t setBundleInfo();
282
283    camera3_device_t   mCameraDevice;
284    uint32_t           mCameraId;
285    mm_camera_vtbl_t  *mCameraHandle;
286    bool               mCameraOpened;
287    bool               mCameraInitialized;
288    camera_metadata_t *mDefaultMetadata[CAMERA3_TEMPLATE_COUNT];
289    const camera3_callback_ops_t *mCallbackOps;
290
291    QCamera3MetadataChannel *mMetadataChannel;
292    QCamera3PicChannel *mPictureChannel;
293    QCamera3RawChannel *mRawChannel;
294    QCamera3SupportChannel *mSupportChannel;
295    QCamera3SupportChannel *mAnalysisChannel;
296    QCamera3RawDumpChannel *mRawDumpChannel;
297    QCamera3RegularChannel *mDummyBatchChannel;
298    QCameraPerfLock m_perfLock;
299
300    uint32_t mChannelHandle;
301
302    void saveExifParams(metadata_buffer_t *metadata);
303    mm_jpeg_exif_params_t mExifParams;
304
305     //First request yet to be processed after configureStreams
306    bool mFirstRequest;
307    bool mFirstConfiguration;
308    bool mFlush;
309    bool mEnableRawDump;
310    QCamera3HeapMemory *mParamHeap;
311    metadata_buffer_t* mParameters;
312    metadata_buffer_t* mPrevParameters;
313    CameraMetadata mCurJpegMeta;
314    bool m_bIsVideo;
315    bool m_bIs4KVideo;
316    bool m_bEisSupportedSize;
317    bool m_bEisEnable;
318    typedef struct {
319        cam_dimension_t dim;
320        int format;
321        uint32_t usage;
322    } InputStreamInfo;
323
324    InputStreamInfo mInputStreamInfo;
325    uint8_t m_MobicatMask;
326    uint8_t mSupportedFaceDetectMode;
327    uint8_t m_bTnrEnabled;
328    uint8_t m_bTnrPreview;
329    uint8_t m_bTnrVideo;
330    cam_cds_mode_type_t m_CdsPreference;
331    /* Data structure to store pending request */
332    typedef struct {
333        camera3_stream_t *stream;
334        camera3_stream_buffer_t *buffer;
335        // metadata needs to be consumed by the corresponding stream
336        // in order to generate the buffer.
337        bool need_metadata;
338    } RequestedBufferInfo;
339    typedef struct {
340        uint32_t frame_number;
341        uint32_t num_buffers;
342        int32_t request_id;
343        List<RequestedBufferInfo> buffers;
344        int blob_request;
345        uint8_t bUrgentReceived;
346        nsecs_t timestamp;
347        camera3_stream_buffer_t *input_buffer;
348        const camera_metadata_t *settings;
349        CameraMetadata jpegMetadata;
350        uint8_t pipeline_depth;
351        uint32_t partial_result_cnt;
352        uint8_t capture_intent;
353        bool shutter_notified;
354        uint8_t hybrid_ae_enable;
355        bool need_dynamic_blklvl;
356        bool pending_extra_result;
357    } PendingRequestInfo;
358    typedef struct {
359        uint32_t frame_number;
360        uint32_t stream_ID;
361    } PendingFrameDropInfo;
362
363    // Store the Pending buffers for Flushing
364    typedef struct {
365        // Frame number pertaining to the buffer
366        uint32_t frame_number;
367        camera3_stream_t *stream;
368        // Buffer handle
369        buffer_handle_t *buffer;
370
371    } PendingBufferInfo;
372
373    typedef struct {
374        // Total number of buffer requests pending
375        uint32_t num_buffers;
376        // List of pending buffers
377        List<PendingBufferInfo> mPendingBufferList;
378        // Last frame number requested
379        uint32_t last_frame_number;
380    } PendingBuffersMap;
381
382    typedef struct {
383        camera3_notify_msg_t notify_msg;
384        camera3_stream_buffer_t buffer;
385        uint32_t frame_number;
386    } PendingReprocessResult;
387
388    typedef KeyedVector<uint32_t, Vector<PendingBufferInfo> > FlushMap;
389    typedef List<QCamera3HardwareInterface::PendingRequestInfo>::iterator
390            pendingRequestIterator;
391    typedef List<QCamera3HardwareInterface::RequestedBufferInfo>::iterator
392            pendingBufferIterator;
393
394    List<PendingReprocessResult> mPendingReprocessResultList;
395    List<PendingRequestInfo> mPendingRequestsList;
396    List<PendingFrameDropInfo> mPendingFrameDropList;
397    /* Use last frame number of the batch as key and first frame number of the
398     * batch as value for that key */
399    KeyedVector<uint32_t, uint32_t> mPendingBatchMap;
400
401    PendingBuffersMap mPendingBuffersMap;
402    pthread_cond_t mRequestCond;
403    uint32_t mPendingLiveRequest;
404    bool mWokenUpByDaemon;
405    int32_t mCurrentRequestId;
406    cam_stream_size_info_t mStreamConfigInfo;
407
408    //mutex for serialized access to camera3_device_ops_t functions
409    pthread_mutex_t mMutex;
410
411    List<stream_info_t*> mStreamInfo;
412
413    int64_t mMinProcessedFrameDuration;
414    int64_t mMinJpegFrameDuration;
415    int64_t mMinRawFrameDuration;
416
417    uint32_t mMetaFrameCount;
418    bool    mUpdateDebugLevel;
419    const camera_module_callbacks_t *mCallbacks;
420
421    uint8_t mCaptureIntent;
422    uint8_t mHybridAeEnable;
423    metadata_buffer_t mReprocMeta; //scratch meta buffer
424    /* 0: Not batch, non-zero: Number of image buffers in a batch */
425    uint8_t mBatchSize;
426    // Used only in batch mode
427    uint8_t mToBeQueuedVidBufs;
428    // Fixed video fps
429    float mHFRVideoFps;
430    uint8_t mOpMode;
431    uint32_t mFirstFrameNumberInBatch;
432    camera3_stream_t mDummyBatchStream;
433    bool mNeedSensorRestart;
434
435    /* sensor output size with current stream configuration */
436    QCamera3CropRegionMapper mCropRegionMapper;
437
438    /* Ldaf calibration data */
439    bool mLdafCalibExist;
440    uint32_t mLdafCalib[2];
441    bool mPowerHintEnabled;
442    int32_t mLastCustIntentFrmNum;
443
444    static const QCameraMap<camera_metadata_enum_android_control_effect_mode_t,
445            cam_effect_mode_type> EFFECT_MODES_MAP[];
446    static const QCameraMap<camera_metadata_enum_android_control_awb_mode_t,
447            cam_wb_mode_type> WHITE_BALANCE_MODES_MAP[];
448    static const QCameraMap<camera_metadata_enum_android_control_scene_mode_t,
449            cam_scene_mode_type> SCENE_MODES_MAP[];
450    static const QCameraMap<camera_metadata_enum_android_control_af_mode_t,
451            cam_focus_mode_type> FOCUS_MODES_MAP[];
452    static const QCameraMap<camera_metadata_enum_android_color_correction_aberration_mode_t,
453            cam_aberration_mode_t> COLOR_ABERRATION_MAP[];
454    static const QCameraMap<camera_metadata_enum_android_control_ae_antibanding_mode_t,
455            cam_antibanding_mode_type> ANTIBANDING_MODES_MAP[];
456    static const QCameraMap<camera_metadata_enum_android_lens_state_t,
457            cam_af_lens_state_t> LENS_STATE_MAP[];
458    static const QCameraMap<camera_metadata_enum_android_control_ae_mode_t,
459            cam_flash_mode_t> AE_FLASH_MODE_MAP[];
460    static const QCameraMap<camera_metadata_enum_android_flash_mode_t,
461            cam_flash_mode_t> FLASH_MODES_MAP[];
462    static const QCameraMap<camera_metadata_enum_android_statistics_face_detect_mode_t,
463            cam_face_detect_mode_t> FACEDETECT_MODES_MAP[];
464    static const QCameraMap<camera_metadata_enum_android_lens_info_focus_distance_calibration_t,
465            cam_focus_calibration_t> FOCUS_CALIBRATION_MAP[];
466    static const QCameraMap<camera_metadata_enum_android_sensor_test_pattern_mode_t,
467            cam_test_pattern_mode_t> TEST_PATTERN_MAP[];
468    static const QCameraMap<camera_metadata_enum_android_sensor_reference_illuminant1_t,
469            cam_illuminat_t> REFERENCE_ILLUMINANT_MAP[];
470    static const QCameraMap<int32_t,
471            cam_hfr_mode_t> HFR_MODE_MAP[];
472
473    static const QCameraPropMap CDS_MAP[];
474
475    pendingRequestIterator erasePendingRequest(pendingRequestIterator i);
476};
477
478}; // namespace qcamera
479
480#endif /* __QCAMERA2HARDWAREINTERFACE_H__ */
481