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 <CameraMetadata.h>
35 #include <map>
36 #include <mutex>
37 #include <pthread.h>
38 #include <utils/KeyedVector.h>
39 #include <utils/List.h>
40 // Camera dependencies
41 #include "hardware/camera3.h"
42 #include "QCamera3Channel.h"
43 #include "QCamera3CropRegionMapper.h"
44 #include "QCamera3HALHeader.h"
45 #include "QCamera3Mem.h"
46 #include "QCameraPerf.h"
47 #include "QCameraCommon.h"
48 #include "QCamera3VendorTags.h"
49 #include "QCameraDualCamSettings.h"
50 #include "QCamera3HdrPlusListenerThread.h"
51 
52 #include "EaselManagerClient.h"
53 #include "HdrPlusClient.h"
54 
55 extern "C" {
56 #include "mm_camera_interface.h"
57 #include "mm_jpeg_interface.h"
58 }
59 
60 using ::android::hardware::camera::common::V1_0::helper::CameraMetadata;
61 using namespace android;
62 
63 namespace qcamera {
64 
65 #ifndef TRUE
66 #define TRUE 1
67 #endif
68 
69 #ifndef FALSE
70 #define FALSE 0
71 #endif
72 
73 /* Time related macros */
74 typedef int64_t nsecs_t;
75 #define NSEC_PER_SEC 1000000000LLU
76 #define NSEC_PER_USEC 1000LLU
77 #define NSEC_PER_33MSEC 33000000LLU
78 
79 /*Orchestrate Macros */
80 #define EV_COMP_SETTLE_DELAY   2
81 #define GB_HDR_HALF_STEP_EV -6
82 #define GB_HDR_2X_STEP_EV 6
83 
84 #define FRAME_REGISTER_LRU_SIZE 256
85 #define INTERNAL_FRAME_STARTING_NUMBER 800
86 #define EMPTY_FRAMEWORK_FRAME_NUMBER 0xFFFFFFFF
87 
88 typedef enum {
89     SET_ENABLE,
90     SET_CONTROLENABLE,
91     SET_RELOAD_CHROMATIX,
92     SET_STATUS,
93 } optype_t;
94 
95 #define MODULE_ALL 0
96 
97 extern volatile uint32_t gCamHal3LogLevel;
98 
99 class QCamera3MetadataChannel;
100 class QCamera3PicChannel;
101 class QCamera3HeapMemory;
102 class QCamera3Exif;
103 class ShutterDispatcher;
104 class BufferDispatcher;
105 
106 typedef struct {
107     camera3_stream_t *stream;
108     camera3_stream_buffer_set_t buffer_set;
109     stream_status_t status;
110     int registered;
111     QCamera3ProcessingChannel *channel;
112     uint32_t id; // unique ID
113 } stream_info_t;
114 
115 typedef struct {
116     // Stream handle
117     camera3_stream_t *stream;
118     // Buffer handle
119     buffer_handle_t *buffer;
120     // Buffer status
121     camera3_buffer_status_t bufStatus = CAMERA3_BUFFER_STATUS_OK;
122 } PendingBufferInfo;
123 
124 typedef struct {
125     // Frame number corresponding to request
126     uint32_t frame_number;
127     // Time when request queued into system
128     nsecs_t timestamp;
129     nsecs_t av_timestamp;
130     List<PendingBufferInfo> mPendingBufferList;
131     bool hdrplus;
132 } PendingBuffersInRequest;
133 
134 class PendingBuffersMap {
135 public:
136     // Number of outstanding buffers at flush
137     uint32_t numPendingBufsAtFlush;
138     // List of pending buffers per request
139     List<PendingBuffersInRequest> mPendingBuffersInRequest;
140     uint32_t get_num_overall_buffers();
141     void removeBuf(buffer_handle_t *buffer);
142     int32_t getBufErrStatus(buffer_handle_t *buffer);
143 };
144 
145 class FrameNumberRegistry {
146 public:
147 
148     FrameNumberRegistry();
149     ~FrameNumberRegistry();
150     int32_t allocStoreInternalFrameNumber(uint32_t frameworkFrameNumber,
151             uint32_t &internalFrameNumber);
152     int32_t generateStoreInternalFrameNumber(uint32_t &internalFrameNumber);
153     int32_t freeInternalFrameNumber(uint32_t internalFrameNumber);
154     int32_t getFrameworkFrameNumber(uint32_t internalFrameNumber, uint32_t &frameworkFrameNumber);
155     void purgeOldEntriesLocked();
156 
157 private:
158     std::map<uint32_t, uint32_t> _register;
159     uint32_t _nextFreeInternalNumber;
160     Mutex mRegistryLock;
161 };
162 
163 class QCamera3HardwareInterface;
164 
165 /*
166  * ShutterDispatcher class dispatches shutter callbacks in order of the frame
167  * number. It will dispatch a shutter callback only after all shutter callbacks
168  * of previous frames were dispatched.
169  */
170 class ShutterDispatcher {
171 public:
172     ShutterDispatcher(QCamera3HardwareInterface *parent);
173     virtual ~ShutterDispatcher() = default;
174 
175     // Tell dispatch to expect a shutter for a frame number.
176     void expectShutter(uint32_t frameNumber, bool isReprocess);
177     // Mark a shutter callback for a frame ready.
178     void markShutterReady(uint32_t frameNumber, uint64_t timestamp);
179     // Discard a pending shutter for frame number.
180     void clear(uint32_t frameNumber);
181     // Discard all pending shutters.
182     void clear();
183 
184 private:
185     struct Shutter {
186         bool ready; // If the shutter is ready.
187         uint64_t timestamp; // Timestamp of the shutter.
ShutterShutter188         Shutter() : ready(false), timestamp(0) {};
189     };
190 
191     std::mutex mLock;
192 
193     // frame number -> shutter map. Protected by mLock.
194     std::map<uint32_t, Shutter> mShutters;
195     std::map<uint32_t, Shutter> mReprocessShutters;
196 
197     QCamera3HardwareInterface *mParent;
198 };
199 
200 /*
201  * BufferDispatcher class dispatches output buffers in a stream in order of the
202  * frame number. It will dispatch an output buffer in a stream only after all
203  * previous output buffers in the same stream were dispatched.
204  */
205 class OutputBufferDispatcher {
206 public:
207     OutputBufferDispatcher(QCamera3HardwareInterface *parent);
208     virtual ~OutputBufferDispatcher() = default;
209 
210     // Configure streams.
211     status_t configureStreams(camera3_stream_configuration_t *streamList);
212     // Tell dispatcher to expect a buffer for a stream for a frame number.
213     status_t expectBuffer(uint32_t frameNumber, camera3_stream_t *stream);
214     // Mark a buffer ready for a stream for a frame number.
215     void markBufferReady(uint32_t frameNumber, const camera3_stream_buffer_t &buffer);
216     // Discard all pending buffers. If clearConfiguredStreams is true, discard configured streams
217     // as well.
218     void clear(bool clearConfiguredStreams = true);
219 
220 private:
221     struct Buffer {
222         bool ready; // If the buffer is ready.
223         camera3_stream_buffer_t buffer;
BufferBuffer224         Buffer() : ready(false), buffer({}) {};
225     };
226 
227     std::mutex mLock;
228 
229     // A two-level map: stream -> (frame number -> buffer). Protected by mLock.
230     std::map<camera3_stream_t*, std::map<uint32_t, Buffer>> mStreamBuffers;
231 
232     QCamera3HardwareInterface *mParent;
233 };
234 
235 class QCamera3HardwareInterface : public HdrPlusClientListener,
236                                   public EaselManagerClientListener {
237 public:
238     /* static variable and functions accessed by camera service */
239     static camera3_device_ops_t mCameraOps;
240     //Id of each session in bundle/link
241     static uint32_t sessionId[MM_CAMERA_MAX_NUM_SENSORS];
242     static int initialize(const struct camera3_device *,
243                 const camera3_callback_ops_t *callback_ops);
244     static int configure_streams(const struct camera3_device *,
245                 camera3_stream_configuration_t *stream_list);
246     static const camera_metadata_t* construct_default_request_settings(
247                                 const struct camera3_device *, int type);
248     static int process_capture_request(const struct camera3_device *,
249                                 camera3_capture_request_t *request);
250 
251     static void dump(const struct camera3_device *, int fd);
252     static int flush(const struct camera3_device *);
253     static int close_camera_device(struct hw_device_t* device);
254 
255 public:
256     QCamera3HardwareInterface(uint32_t cameraId,
257             const camera_module_callbacks_t *callbacks);
258     virtual ~QCamera3HardwareInterface();
259     static void camEvtHandle(uint32_t camera_handle, mm_camera_event_t *evt,
260                                           void *user_data);
261     int openCamera(struct hw_device_t **hw_device);
262     camera_metadata_t* translateCapabilityToMetadata(int type);
263 
264     typedef struct {
265         camera3_stream_t *stream;
266         bool need_metadata;
267         bool meteringOnly;
268     } InternalRequest;
269 
270     static int getCamInfo(uint32_t cameraId, struct camera_info *info);
271     static cam_capability_t *getCapabilities(mm_camera_ops_t *ops,
272             uint32_t cam_handle);
273     static int initCapabilities(uint32_t cameraId);
274     static int initStaticMetadata(uint32_t cameraId);
275     static int initHdrPlusClientLocked();
276     static void makeTable(cam_dimension_t *dimTable, size_t size,
277             size_t max_size, int32_t *sizeTable);
278     static void makeFPSTable(cam_fps_range_t *fpsTable, size_t size,
279             size_t max_size, int32_t *fpsRangesTable);
280     static void makeOverridesList(cam_scene_mode_overrides_t *overridesTable,
281             size_t size, size_t max_size, uint8_t *overridesList,
282             uint8_t *supported_indexes, uint32_t camera_id);
283     static size_t filterJpegSizes(int32_t *jpegSizes, int32_t *processedSizes,
284             size_t processedSizesCnt, size_t maxCount, cam_rect_t active_array_size,
285             uint8_t downscale_factor);
286     static void convertToRegions(cam_rect_t rect, int32_t* region, int weight);
287     static void convertFromRegions(cam_area_t &roi, const CameraMetadata &frame_settings,
288                                    uint32_t tag);
289     static bool resetIfNeededROI(cam_area_t* roi, const cam_crop_region_t* scalerCropRegion);
290     static int32_t getSensorSensitivity(int32_t iso_mode);
291 
292     double computeNoiseModelEntryS(int32_t sensitivity);
293     double computeNoiseModelEntryO(int32_t sensitivity);
294 
295     static void captureResultCb(mm_camera_super_buf_t *metadata,
296                 camera3_stream_buffer_t *buffer, uint32_t frame_number,
297                 bool isInputBuffer, void *userdata);
298 
299     int initialize(const camera3_callback_ops_t *callback_ops);
300     int configureStreams(camera3_stream_configuration_t *stream_list);
301     int configureStreamsPerfLocked(camera3_stream_configuration_t *stream_list);
302     int processCaptureRequest(camera3_capture_request_t *request,
303                               List<InternalRequest> &internalReqs);
304     int orchestrateRequest(camera3_capture_request_t *request);
305     void orchestrateResult(camera3_capture_result_t *result);
306     void orchestrateNotify(camera3_notify_msg_t *notify_msg);
307 
308     void dump(int fd);
309     int flushPerf();
310 
311     int setFrameParameters(camera3_capture_request_t *request,
312             cam_stream_ID_t streamID, int blob_request, uint32_t snapshotStreamId);
313     int32_t setReprocParameters(camera3_capture_request_t *request,
314             metadata_buffer_t *reprocParam, uint32_t snapshotStreamId);
315     int translateToHalMetadata(const camera3_capture_request_t *request,
316             metadata_buffer_t *parm, uint32_t snapshotStreamId);
317     int translateFwkMetadataToHalMetadata(const camera_metadata_t *frameworkMetadata,
318             metadata_buffer_t *hal_metadata, uint32_t snapshotStreamId, int64_t minFrameDuration);
319     camera_metadata_t* translateCbUrgentMetadataToResultMetadata (
320                              metadata_buffer_t *metadata, bool lastUrgentMetadataInBatch,
321                              uint32_t frame_number, bool isJumpstartMetadata);
322     camera_metadata_t* saveRequestSettings(const CameraMetadata& jpegMetadata,
323                             camera3_capture_request_t *request);
324     int initParameters();
325     void deinitParameters();
326     QCamera3ReprocessChannel *addOfflineReprocChannel(const reprocess_config_t &config,
327             QCamera3ProcessingChannel *inputChHandle);
328     bool needRotationReprocess();
329     bool needJpegExifRotation();
330     bool needReprocess(cam_feature_mask_t postprocess_mask);
331     bool needJpegRotation();
332     cam_denoise_process_type_t getWaveletDenoiseProcessPlate();
333     cam_denoise_process_type_t getTemporalDenoiseProcessPlate();
334 
335     void captureResultCb(mm_camera_super_buf_t *metadata,
336                 camera3_stream_buffer_t *buffer, uint32_t frame_number,
337                 bool isInputBuffer);
338     cam_dimension_t calcMaxJpegDim();
339     bool needOnlineRotation();
340     uint32_t getJpegQuality();
341     QCamera3Exif *getExifData();
342     mm_jpeg_exif_params_t get3AExifParams();
343     uint8_t getMobicatMask();
344     static void getFlashInfo(const int cameraId,
345             bool& hasFlash,
346             char (&flashNode)[QCAMERA_MAX_FILEPATH_LENGTH]);
347     const char *getEepromVersionInfo();
348     const uint32_t *getLdafCalib();
349     const char *getEaselFwVersion();
350     void get3AVersion(cam_q3a_version_t &swVersion);
351     static void setBufferErrorStatus(QCamera3Channel*, uint32_t frameNumber,
352             camera3_buffer_status_t err, void *userdata);
353     void setBufferErrorStatus(QCamera3Channel*, uint32_t frameNumber,
354             camera3_buffer_status_t err);
355     bool is60HzZone();
356 
357     // Get dual camera related info
isDeviceLinked()358     bool isDeviceLinked() {return mIsDeviceLinked;}
isMainCamera()359     bool isMainCamera() {return mIsMainCamera;}
360     uint32_t getSensorMountAngle();
361     const cam_related_system_calibration_data_t *getRelatedCalibrationData();
362 
363     template <typename fwkType, typename halType> struct QCameraMap {
364         fwkType fwk_name;
365         halType hal_name;
366     };
367 
368     typedef struct {
369         const char *const desc;
370         cam_cds_mode_type_t val;
371     } QCameraPropMap;
372 
373 private:
374 
375     // State transition conditions:
376     // "\" means not applicable
377     // "x" means not valid
378     // +------------+----------+----------+-------------+------------+---------+-------+--------+
379     // |            |  CLOSED  |  OPENED  | INITIALIZED | CONFIGURED | STARTED | ERROR | DEINIT |
380     // +------------+----------+----------+-------------+------------+---------+-------+--------+
381     // |  CLOSED    |    \     |   open   |     x       |    x       |    x    |   x   |   x    |
382     // +------------+----------+----------+-------------+------------+---------+-------+--------+
383     // |  OPENED    |  close   |    \     | initialize  |    x       |    x    | error |   x    |
384     // +------------+----------+----------+-------------+------------+---------+-------+--------+
385     // |INITIALIZED |  close   |    x     |     \       | configure  |   x     | error |   x    |
386     // +------------+----------+----------+-------------+------------+---------+-------+--------+
387     // | CONFIGURED |  close   |    x     |     x       | configure  | request | error |   x    |
388     // +------------+----------+----------+-------------+------------+---------+-------+--------+
389     // |  STARTED   |  close   |    x     |     x       | configure  |    \    | error |   x    |
390     // +------------+----------+----------+-------------+------------+---------+-------+--------+
391     // |   ERROR    |  close   |    x     |     x       |     x      |    x    |   \   |  any   |
392     // +------------+----------+----------+-------------+------------+---------+-------+--------+
393     // |   DEINIT   |  close   |    x     |     x       |     x      |    x    |   x   |   \    |
394     // +------------+----------+----------+-------------+------------+---------+-------+--------+
395 
396     typedef enum {
397         CLOSED,
398         OPENED,
399         INITIALIZED,
400         CONFIGURED,
401         STARTED,
402         ERROR,
403         DEINIT
404     } State;
405 
406     int openCamera();
407     int closeCamera();
408     int flush(bool restartChannels, bool stopChannelImmediately = false);
409     static size_t calcMaxJpegSize(uint32_t camera_id);
410     cam_dimension_t getMaxRawSize(uint32_t camera_id);
411     static void addStreamConfig(Vector<int32_t> &available_stream_configs,
412             int32_t scalar_format, const cam_dimension_t &dim,
413             int32_t config_type);
414 
415     int validateCaptureRequest(camera3_capture_request_t *request,
416                                List<InternalRequest> &internallyRequestedStreams);
417     int validateStreamDimensions(camera3_stream_configuration_t *streamList);
418     int validateStreamRotations(camera3_stream_configuration_t *streamList);
419     int validateUsageFlags(const camera3_stream_configuration_t *streamList);
420     int validateUsageFlagsForEis(const camera3_stream_configuration_t *streamList);
421     void deriveMinFrameDuration();
422     void handleBuffersDuringFlushLock(camera3_stream_buffer_t *buffer);
423     int64_t getMinFrameDuration(const camera3_capture_request_t *request);
424     void handleMetadataWithLock(mm_camera_super_buf_t *metadata_buf,
425             bool free_and_bufdone_meta_buf,
426             bool lastUrgentMetadataInBatch,
427             bool lastMetadataInBatch,
428             bool *p_is_metabuf_queued);
429     void handleBatchMetadata(mm_camera_super_buf_t *metadata_buf,
430             bool free_and_bufdone_meta_buf);
431     void handleBufferWithLock(camera3_stream_buffer_t *buffer,
432             uint32_t frame_number);
433     void handleInputBufferWithLock(uint32_t frame_number);
434     // Handle pending results when a new result metadata of a frame is received.
435     // metadata callbacks are invoked in the order of frame number.
436     void handlePendingResultMetadataWithLock(uint32_t frameNumber,
437             camera_metadata_t *resultMetadata);
438     // Going through pending request list and send out result metadata for requests
439     // that are ready.
440     // frameNumber is the lastest frame whose result metadata is ready.
441     // isLiveRequest is whether the frame belongs to a live request.
442     void dispatchResultMetadataWithLock(uint32_t frameNumber, bool isLiveRequest);
443     void handleDepthDataLocked(const cam_depth_data_t &depthData,
444             uint32_t frameNumber, uint8_t valid);
445     void notifyErrorFoPendingDepthData(QCamera3DepthChannel *depthCh);
446     void unblockRequestIfNecessary();
447     void dumpMetadataToFile(tuning_params_t &meta, uint32_t &dumpFrameCount,
448             bool enabled, const char *type, uint32_t frameNumber);
449     static void getLogLevel();
450     static int32_t getPDStatIndex(cam_capability_t *caps);
451 
452     void cleanAndSortStreamInfo();
453     void extractJpegMetadata(CameraMetadata& jpegMetadata,
454             const camera3_capture_request_t *request);
455 
456     bool isSupportChannelNeeded(camera3_stream_configuration_t *streamList,
457             cam_stream_size_info_t stream_config_info);
458     bool isHdrSnapshotRequest(camera3_capture_request *request);
459     int32_t setMobicat();
460 
461     int32_t getSensorModeInfo(cam_sensor_mode_info_t &sensorModeInfo);
462     // Get information of the sensor mode that is currently selected.
463     int32_t getCurrentSensorModeInfo(cam_sensor_mode_info_t &sensorModeInfo);
464     int32_t setHalFpsRange(const CameraMetadata &settings,
465             metadata_buffer_t *hal_metadata);
466     int32_t extractSceneMode(const CameraMetadata &frame_settings, uint8_t metaMode,
467             metadata_buffer_t *hal_metadata);
468     int32_t setVideoHdrMode(metadata_buffer_t *hal_metadata,
469             cam_video_hdr_mode_t vhdr);
470     int32_t numOfSizesOnEncoder(const camera3_stream_configuration_t *streamList,
471             const cam_dimension_t &maxViewfinderSize);
472 
473     void addToPPFeatureMask(int stream_format, uint32_t stream_idx);
474     void updateFpsInPreviewBuffer(metadata_buffer_t *metadata, uint32_t frame_number);
475     void updateTimeStampInPendingBuffers(uint32_t frameNumber, nsecs_t timestamp);
476 
477     void enablePowerHint();
478     void disablePowerHint();
479     int32_t dynamicUpdateMetaStreamInfo();
480     int32_t startAllChannels();
481     int32_t stopAllChannels();
482     int32_t notifyErrorForPendingRequests();
483     void notifyError(uint32_t frameNumber,
484             camera3_error_msg_code_t errorCode);
485     int32_t getReprocessibleOutputStreamId(uint32_t &id);
486     int32_t handleCameraDeviceError(bool stopChannelImmediately = false);
487 
488     bool isEISEnabled(const CameraMetadata& meta);
489     bool isOnEncoder(const cam_dimension_t max_viewfinder_size,
490             uint32_t width, uint32_t height);
491     void hdrPlusPerfLock(mm_camera_super_buf_t *metadata_buf);
492 
493     static bool supportBurstCapture(uint32_t cameraId);
494     int32_t setBundleInfo();
495     int32_t setInstantAEC(const CameraMetadata &meta);
496 
497     static void convertLandmarks(cam_face_landmarks_info_t face, int32_t* landmarks);
498     static void setInvalidLandmarks(int32_t* landmarks);
499 
500     static void setPAAFSupport(cam_feature_mask_t& feature_mask,
501             cam_stream_type_t stream_type,
502             cam_color_filter_arrangement_t filter_arrangement);
503     int32_t setSensorHDR(metadata_buffer_t *hal_metadata, bool enable,
504             bool isVideoHdrEnable = false);
505 
506     template <typename T>
507     static void adjustBlackLevelForCFA(T input[BLACK_LEVEL_PATTERN_CNT],
508             T output[BLACK_LEVEL_PATTERN_CNT],
509             cam_color_filter_arrangement_t color_arrangement);
510 
511     int32_t startChannelLocked();
512     void stopChannelLocked(bool stopChannelImmediately);
513 
514     camera3_device_t   mCameraDevice;
515     uint32_t           mCameraId;
516     mm_camera_vtbl_t  *mCameraHandle;
517     bool               mCameraInitialized;
518     camera_metadata_t *mDefaultMetadata[CAMERA3_TEMPLATE_COUNT];
519     const camera3_callback_ops_t *mCallbackOps;
520 
521     QCamera3MetadataChannel *mMetadataChannel;
522     QCamera3PicChannel *mPictureChannel;
523     QCamera3RawChannel *mRawChannel;
524     QCamera3SupportChannel *mSupportChannel;
525     QCamera3SupportChannel *mAnalysisChannel;
526     QCamera3RawDumpChannel *mRawDumpChannel;
527     QCamera3HdrPlusRawSrcChannel *mHdrPlusRawSrcChannel;
528     QCamera3RegularChannel *mDummyBatchChannel;
529     QCamera3DepthChannel *mDepthChannel;
530     cam_sensor_pd_data_t mDepthCloudMode; //Cache last configured mode
531     QCameraPerfLockMgr mPerfLockMgr;
532 
533     uint32_t mChannelHandle;
534 
535     void saveExifParams(metadata_buffer_t *metadata);
536     mm_jpeg_exif_params_t mExifParams;
537 
538      //First request yet to be processed after configureStreams
539     bool mFirstConfiguration;
540     bool mFlush;
541     bool mFlushPerf;
542     bool mEnableRawDump;
543     bool mForceHdrSnapshot;
544     QCamera3HeapMemory *mParamHeap;
545     metadata_buffer_t* mParameters;
546     metadata_buffer_t* mPrevParameters;
547     CameraMetadata mCurJpegMeta;
548     cam_is_type_t m_ISTypeVideo;
549     bool m_bIsVideo;
550     bool m_bIs4KVideo;
551     bool m_bEisSupportedSize;
552     bool m_bEisEnable;
553     bool m_bEis3PropertyEnabled;
554     bool m_bEisSupported;
555     bool m_bAVTimerEnabled;
556     typedef struct {
557         cam_dimension_t dim;
558         int format;
559         uint32_t usage;
560     } InputStreamInfo;
561 
562     InputStreamInfo mInputStreamInfo;
563     uint8_t m_MobicatMask;
564     uint8_t m_bTnrEnabled;
565     int8_t  mSupportedFaceDetectMode;
566     uint8_t m_bTnrPreview;
567     uint8_t m_bSwTnrPreview;
568     uint8_t m_bTnrVideo;
569     uint8_t m_debug_avtimer;
570     uint8_t m_bVideoHdrEnabled;
571     uint8_t m_cacModeDisabled;
572     uint8_t m_bForceInfinityAf;
573 
574     /* Data structure to store pending request */
575     typedef struct {
576         camera3_stream_t *stream;
577         camera3_stream_buffer_t *buffer;
578         // metadata needs to be consumed by the corresponding stream
579         // in order to generate the buffer.
580         bool need_metadata;
581     } RequestedBufferInfo;
582 
583     typedef struct {
584         uint32_t frame_number;
585         uint32_t num_buffers;
586         int32_t request_id;
587         List<RequestedBufferInfo> buffers;
588         List<InternalRequest> internalRequestList;
589         int blob_request;
590         uint8_t bUseFirstPartial; // Use first available partial result in case of jumpstart.
591         nsecs_t timestamp;
592         nsecs_t expectedFrameDuration;
593         camera3_stream_buffer_t *input_buffer;
594         const camera_metadata_t *settings;
595         const camera_metadata_t *resultMetadata; // Result metadata for this request.
596         CameraMetadata jpegMetadata;
597         uint8_t pipeline_depth;
598         uint32_t partial_result_cnt;
599         uint8_t capture_intent;
600         uint8_t fwkCacMode;
601         uint8_t hybrid_ae_enable;
602         uint8_t motion_detection_enable;
603         /* DevCamDebug metadata PendingRequestInfo */
604         uint8_t DevCamDebug_meta_enable;
605         /* DevCamDebug metadata end */
606 
607         bool focusStateSent = false;
608         bool focusStateValid = false;
609         uint8_t focusState = ANDROID_CONTROL_AF_STATE_INACTIVE;
610 
611         bool enableZsl; // If ZSL is enabled.
612         bool hdrplus; // If this is an HDR+ request.
613         uint8_t requestedLensShadingMapMode; // Lens shading map mode for this request.
614         uint8_t requestedFaceDetectMode; // Face detect mode for this request.
615         bool partialResultDropped; // Whether partial metadata is dropped.
616         uint8_t requestedOisDataMode; // OIS data mode for this request.
617     } PendingRequestInfo;
618     typedef struct {
619         uint32_t frame_number;
620         uint32_t stream_ID;
621     } PendingFrameDropInfo;
622 
623     class FrameNumberRegistry _orchestrationDb;
624     typedef KeyedVector<uint32_t, Vector<PendingBufferInfo> > FlushMap;
625     typedef List<QCamera3HardwareInterface::PendingRequestInfo>::iterator
626             pendingRequestIterator;
627     typedef List<QCamera3HardwareInterface::RequestedBufferInfo>::iterator
628             pendingBufferIterator;
629 
630     List<PendingRequestInfo> mPendingRequestsList;
631     List<PendingFrameDropInfo> mPendingFrameDropList;
632     /* Use last frame number of the batch as key and first frame number of the
633      * batch as value for that key */
634     KeyedVector<uint32_t, uint32_t> mPendingBatchMap;
635     cam_stream_ID_t mBatchedStreamsArray;
636 
637     PendingBuffersMap mPendingBuffersMap;
638     pthread_cond_t mRequestCond;
639     uint32_t mPendingLiveRequest;
640     bool mWokenUpByDaemon;
641     int32_t mCurrentRequestId;
642     cam_stream_size_info_t mStreamConfigInfo;
643 
644     ShutterDispatcher mShutterDispatcher;
645     OutputBufferDispatcher mOutputBufferDispatcher;
646 
647     //mutex for serialized access to camera3_device_ops_t functions
648     pthread_mutex_t mMutex;
649 
650     //condition used to signal flush after buffers have returned
651     pthread_cond_t mBuffersCond;
652 
653     List<stream_info_t*> mStreamInfo;
654 
655     int64_t mMinProcessedFrameDuration;
656     int64_t mMinJpegFrameDuration;
657     int64_t mMinRawFrameDuration;
658     nsecs_t mExpectedFrameDuration;
659     nsecs_t mExpectedInflightDuration;
660     static const nsecs_t kDefaultExpectedDuration = 100000000; // 100 ms
661 
662     uint32_t mMetaFrameCount;
663     bool    mUpdateDebugLevel;
664     const camera_module_callbacks_t *mCallbacks;
665 
666     uint8_t mCaptureIntent;
667     uint8_t mCacMode;
668     // DevCamDebug metadata internal variable
669     uint8_t mDevCamDebugMetaEnable;
670     /* DevCamDebug metadata end */
671 
672     metadata_buffer_t mReprocMeta; //scratch meta buffer
673     /* 0: Not batch, non-zero: Number of image buffers in a batch */
674     uint8_t mBatchSize;
675     // Used only in batch mode
676     uint8_t mToBeQueuedVidBufs;
677     // Fixed video fps
678     float mHFRVideoFps;
679 public:
680     uint32_t mOpMode;
681     bool mStreamConfig;
682     QCameraCommon   mCommon;
683 private:
684     uint32_t mFirstFrameNumberInBatch;
685     camera3_stream_t mDummyBatchStream;
686     bool mNeedSensorRestart;
687     bool mPreviewStarted;
688     uint32_t mMinInFlightRequests;
689     uint32_t mMaxInFlightRequests;
690     bool mPDSupported;
691     int32_t mPDIndex;
692     // Param to trigger instant AEC.
693     bool mInstantAEC;
694     // Param to know when to reset AEC
695     bool mResetInstantAEC;
696     // Frame number, untill which we need to drop the frames.
697     uint32_t mInstantAECSettledFrameNumber;
698     // Max number of frames, that HAL will hold without displaying, for instant AEC mode.
699     uint8_t mAecSkipDisplayFrameBound;
700     // Counter to keep track of number of frames that took for AEC convergence.
701     uint8_t mInstantAecFrameIdxCount;
702     /* sensor output size with current stream configuration */
703     QCamera3CropRegionMapper mCropRegionMapper;
704     // Last lens shading map mode framework requsted.
705     uint8_t mLastRequestedLensShadingMapMode;
706     // Last face detect mode framework requsted.
707     uint8_t mLastRequestedFaceDetectMode;
708     // Last OIS data mode framework requested.
709     uint8_t mLastRequestedOisDataMode;
710 
711     cam_feature_mask_t mCurrFeatureState;
712     /* Ldaf calibration data */
713     bool mLdafCalibExist;
714     uint32_t mLdafCalib[2];
715     int32_t mLastCustIntentFrmNum;
716     // Easel firmware version
717     char mEaselFwVersion[FW_VER_SIZE];
718     bool mEaselFwUpdated;
719     static const QCameraMap<camera_metadata_enum_android_control_effect_mode_t,
720             cam_effect_mode_type> EFFECT_MODES_MAP[];
721     static const QCameraMap<camera_metadata_enum_android_control_awb_mode_t,
722             cam_wb_mode_type> WHITE_BALANCE_MODES_MAP[];
723     static const QCameraMap<camera_metadata_enum_android_control_scene_mode_t,
724             cam_scene_mode_type> SCENE_MODES_MAP[];
725     static const QCameraMap<camera_metadata_enum_android_control_af_mode_t,
726             cam_focus_mode_type> FOCUS_MODES_MAP[];
727     static const QCameraMap<camera_metadata_enum_android_color_correction_aberration_mode_t,
728             cam_aberration_mode_t> COLOR_ABERRATION_MAP[];
729     static const QCameraMap<camera_metadata_enum_android_control_ae_antibanding_mode_t,
730             cam_antibanding_mode_type> ANTIBANDING_MODES_MAP[];
731     static const QCameraMap<camera_metadata_enum_android_lens_state_t,
732             cam_af_lens_state_t> LENS_STATE_MAP[];
733     static const QCameraMap<camera_metadata_enum_android_control_ae_mode_t,
734             cam_flash_mode_t> AE_FLASH_MODE_MAP[];
735     static const QCameraMap<camera_metadata_enum_android_flash_mode_t,
736             cam_flash_mode_t> FLASH_MODES_MAP[];
737     static const QCameraMap<camera_metadata_enum_android_statistics_face_detect_mode_t,
738             cam_face_detect_mode_t> FACEDETECT_MODES_MAP[];
739     static const QCameraMap<camera_metadata_enum_android_lens_info_focus_distance_calibration_t,
740             cam_focus_calibration_t> FOCUS_CALIBRATION_MAP[];
741     static const QCameraMap<camera_metadata_enum_android_sensor_test_pattern_mode_t,
742             cam_test_pattern_mode_t> TEST_PATTERN_MAP[];
743     static const QCameraMap<camera_metadata_enum_android_video_hdr_mode_t,
744             cam_video_hdr_mode_t> VIDEO_HDR_MODES_MAP[];
745     static const QCameraMap<camera_metadata_enum_android_sensor_reference_illuminant1_t,
746             cam_illuminat_t> REFERENCE_ILLUMINANT_MAP[];
747     static const QCameraMap<int32_t,
748             cam_hfr_mode_t> HFR_MODE_MAP[];
749     static const QCameraMap<camera_metadata_enum_android_ir_mode_t,
750             cam_ir_mode_type_t> IR_MODES_MAP[];
751     static const QCameraMap<qcamera3_ext_instant_aec_mode_t,
752             cam_aec_convergence_type> INSTANT_AEC_MODES_MAP[];
753     static const QCameraMap<camera_metadata_enum_android_binning_correction_mode_t,
754             cam_binning_correction_mode_t> BINNING_CORRECTION_MODES_MAP[];
755     static const QCameraMap<qcamera3_ext_exposure_meter_mode_t,
756             cam_auto_exposure_mode_type> AEC_MODES_MAP[];
757     static const QCameraMap<qcamera3_ext_iso_mode_t,
758             cam_iso_mode_type> ISO_MODES_MAP[];
759     static const QCameraPropMap CDS_MAP[];
760 
761     pendingRequestIterator erasePendingRequest(pendingRequestIterator i);
762 
763     // Remove unrequested metadata due to Easel HDR+.
764     void removeUnrequestedMetadata(pendingRequestIterator requestIter,
765             camera_metadata_t *resultMetadata);
766 
767     //GPU library to read buffer padding details.
768     void *lib_surface_utils;
769     int (*LINK_get_surface_pixel_alignment)();
770     uint32_t mSurfaceStridePadding;
771 
772     bool mFirstMetadataCallback;
773     void sendPartialMetadataWithLock(metadata_buffer_t *metadata,
774             const pendingRequestIterator requestIter,
775             bool lastUrgentMetadataInBatch, bool isJumpstartMetadata);
776 
777     camera_metadata_t* translateFromHalMetadata(metadata_buffer_t *metadata,
778                             const PendingRequestInfo& pendingRequest,
779                             /* DevCamDebug metadata end */
780                             bool pprocDone,
781                             bool lastMetadataInBatch,
782                             const bool *enableZsl);
783 
784     State mState;
785     //Dual camera related params
786     bool mIsDeviceLinked;
787     bool mIsMainCamera;
788     uint8_t mLinkedCameraId;
789     QCamera3HeapMemory *m_pDualCamCmdHeap;
790     cam_dual_camera_cmd_info_t *m_pDualCamCmdPtr;
791     cam_sync_related_sensors_event_info_t m_relCamSyncInfo;
792     Mutex mFlushLock;
793     bool m60HzZone;
794 
795     // Issue an additional RAW for every 10 requests to control RAW capture rate. Requesting RAW
796     // too often will cause frame drops due to latency of sending RAW to HDR+ service.
797     const static uint32_t kHdrPlusRawPeriod = 10;
798 
799     // Define a pending HDR+ request submitted to HDR+ service and not yet received by HAL.
800     struct HdrPlusPendingRequest {
801         // HDR+ stream ID -> output buffer to be filled by HDR+ client with an HDR+ processed frame.
802         std::map<uint32_t, std::shared_ptr<mm_camera_buf_def_t>> outputBuffers;
803 
804         // HDR+ stream ID -> output buffers in camera framework's request.
805         std::map<uint32_t, camera3_stream_buffer_t> frameworkOutputBuffers;
806 
807         // Settings in camera framework's request.
808         std::shared_ptr<metadata_buffer_t> settings;
809     };
810 
811     // Fill pbcamera::StreamConfiguration based on the channel stream.
812     status_t fillPbStreamConfig(pbcamera::StreamConfiguration *config, uint32_t pbStreamId,
813             QCamera3Channel *channel, uint32_t streamIndex);
814 
815     // Open HDR+ client asynchronously.
816     status_t openHdrPlusClientAsyncLocked();
817 
818     // Enable HDR+ mode. Easel will start capturing ZSL buffers.
819     status_t enableHdrPlusModeLocked();
820 
821     // Disable HDR+ mode. Easel will stop capturing ZSL buffers.
822     void disableHdrPlusModeLocked();
823 
824     // Return if current session with configured streams is compatible with HDR+ mode.
825     bool isSessionHdrPlusModeCompatible();
826 
827     // Return if the request is compatible with HDR+.
828     bool isRequestHdrPlusCompatible(
829             const camera3_capture_request_t &request, const CameraMetadata &metadata);
830 
831     // Configure streams for HDR+.
832     status_t configureHdrPlusStreamsLocked();
833 
834     // Try to submit an HDR+ request. Returning true if an HDR+ request was submitted. Returning
835     // false if it is not an HDR+ request or submitting an HDR+ request failed. Must be called with
836     // gHdrPlusClientLock held.
837     bool trySubmittingHdrPlusRequestLocked(HdrPlusPendingRequest *hdrPlusRequest,
838         const camera3_capture_request_t &request, const CameraMetadata &metadata);
839 
840     // Abort an HDR+ request that was not submitted successfully in
841     // trySubmittingHdrPlusRequestLocked.
842     void abortPendingHdrplusRequest(HdrPlusPendingRequest *hdrPlusRequest);
843 
844     // Update HDR+ result metadata with the still capture's request settings.
845     void updateHdrPlusResultMetadata(CameraMetadata &resultMetadata,
846             std::shared_ptr<metadata_buffer_t> settings);
847 
848     // Wait until opening HDR+ client completes if it's being opened.
849     void finishHdrPlusClientOpeningLocked(std::unique_lock<std::mutex> &lock);
850 
851     // Handle Easel error asynchronuously in another thread.
852     void handleEaselFatalErrorAsync();
853 
854     // Handle Easel error.
855     void handleEaselFatalError();
856 
857     // Easel manager client callbacks.
858     void onEaselFatalError(std::string errMsg);
859 
860     // Clean up and wait for Easel error future.
861     void cleanupEaselErrorFuture();
862 
863     // HDR+ client callbacks.
864     void onOpened(std::unique_ptr<HdrPlusClient> client) override;
865     void onOpenFailed(status_t err) override;
866     void onFatalError() override;
867     void onCaptureResult(pbcamera::CaptureResult *result,
868             const camera_metadata_t &resultMetadata) override;
869     void onFailedCaptureResult(pbcamera::CaptureResult *failedResult) override;
870     void onShutter(uint32_t requestId, int64_t apSensorTimestampNs) override;
871     void onNextCaptureReady(uint32_t requestId) override;
872     void onPostview(uint32_t requestId, std::unique_ptr<std::vector<uint8_t>> postview,
873             uint32_t width, uint32_t height, uint32_t stride, int32_t format) override;
874 
875     nsecs_t calculateMaxExpectedDuration(const camera_metadata_t *request);
876     void getExpectedFrameDuration(const camera_metadata_t *request, nsecs_t *frameDuration);
877 
878     // Map from frame number to frame. Must be protected by mHdrPlusPendingRequestsLock.
879     std::map<uint32_t, HdrPlusPendingRequest> mHdrPlusPendingRequests;
880     Mutex mHdrPlusPendingRequestsLock;
881 
882     // If HDR+ mode is enabled i.e. if Easel is capturing ZSL buffers.
883     bool mHdrPlusModeEnabled;
884 
885     // If ZSL is enabled (android.control.enableZsl).
886     bool mZslEnabled;
887 
888     // If Easel MIPI has been started.
889     bool mEaselMipiStarted;
890 
891     // If HAL provides RAW input buffers to Easel. This is just for prototyping.
892     bool mIsApInputUsedForHdrPlus;
893 
894     // Current sensor mode information.
895     cam_sensor_mode_info_t mSensorModeInfo;
896 
897     // If there is a capture request with preview intent since stream configuration.
898     bool mFirstPreviewIntentSeen;
899 
900     bool m_bSensorHDREnabled;
901 
902     cam_trigger_t mAfTrigger;
903 
904     int32_t mSceneDistance;
905 
906     std::mutex mEaselErrorFutureLock;
907     std::future<void> mEaselErrorFuture;
908 
909     // Thread to handle callbacks from HDR+ client. Protected by gHdrPlusClientLock.
910     sp<QCamera3HdrPlusListenerThread> mQCamera3HdrPlusListenerThread;
911 
912     // Read sensor calibration XML file for lens calibration fields. On failure to read
913     // the file, leaves passed-in values unchanged and returns false.
914     static bool readSensorCalibration(int activeArrayWidth,
915             float poseRotation[4], float poseTranslation[3],
916             float cameraIntrinsics[5], float radialDistortion[6]);
917 
918     // Parse a string of form " [ x; y; z ...]" into a floating-point array.
919     // Returns false on parse error
920     static bool parseStringArray(const char *str, float *dest, int count);
921 
922     float mLastFocusDistance;
923 };
924 
925 }; // namespace qcamera
926 
927 #endif /* __QCAMERA2HARDWAREINTERFACE_H__ */
928