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