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