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* translateCbUrgentMetadataToResultMetadata (
324                              metadata_buffer_t *metadata, bool lastUrgentMetadataInBatch,
325                              uint32_t frame_number, bool isJumpstartMetadata);
326     camera_metadata_t* saveRequestSettings(const CameraMetadata& jpegMetadata,
327                             camera3_capture_request_t *request);
328     int initParameters();
329     void deinitParameters();
330     QCamera3ReprocessChannel *addOfflineReprocChannel(const reprocess_config_t &config,
331             QCamera3ProcessingChannel *inputChHandle);
332     bool needRotationReprocess();
333     bool needJpegExifRotation();
334     bool needReprocess(cam_feature_mask_t postprocess_mask);
335     bool needJpegRotation();
336     cam_denoise_process_type_t getWaveletDenoiseProcessPlate();
337     cam_denoise_process_type_t getTemporalDenoiseProcessPlate();
338 
339     void captureResultCb(mm_camera_super_buf_t *metadata,
340                 camera3_stream_buffer_t *buffer, uint32_t frame_number,
341                 bool isInputBuffer);
342     cam_dimension_t calcMaxJpegDim();
343     bool needOnlineRotation();
344     uint32_t getJpegQuality();
345     QCamera3Exif *getExifData();
346     mm_jpeg_exif_params_t get3AExifParams();
347     uint8_t getMobicatMask();
348     static void getFlashInfo(const int cameraId,
349             bool& hasFlash,
350             char (&flashNode)[QCAMERA_MAX_FILEPATH_LENGTH]);
351     const char *getEepromVersionInfo();
352     const uint32_t *getLdafCalib();
353     const char *getEaselFwVersion();
354     void get3AVersion(cam_q3a_version_t &swVersion);
355     static void setBufferErrorStatus(QCamera3Channel*, uint32_t frameNumber,
356             camera3_buffer_status_t err, void *userdata);
357     void setBufferErrorStatus(QCamera3Channel*, uint32_t frameNumber,
358             camera3_buffer_status_t err);
359     bool is60HzZone();
360 
361     // Get dual camera related info
isDeviceLinked()362     bool isDeviceLinked() {return mIsDeviceLinked;}
isMainCamera()363     bool isMainCamera() {return mIsMainCamera;}
364     uint32_t getSensorMountAngle();
365     const cam_related_system_calibration_data_t *getRelatedCalibrationData();
366 
367     template <typename fwkType, typename halType> struct QCameraMap {
368         fwkType fwk_name;
369         halType hal_name;
370     };
371 
372     typedef struct {
373         const char *const desc;
374         cam_cds_mode_type_t val;
375     } QCameraPropMap;
376 
377 private:
378 
379     // State transition conditions:
380     // "\" means not applicable
381     // "x" means not valid
382     // +------------+----------+----------+-------------+------------+---------+-------+--------+
383     // |            |  CLOSED  |  OPENED  | INITIALIZED | CONFIGURED | STARTED | ERROR | DEINIT |
384     // +------------+----------+----------+-------------+------------+---------+-------+--------+
385     // |  CLOSED    |    \     |   open   |     x       |    x       |    x    |   x   |   x    |
386     // +------------+----------+----------+-------------+------------+---------+-------+--------+
387     // |  OPENED    |  close   |    \     | initialize  |    x       |    x    | error |   x    |
388     // +------------+----------+----------+-------------+------------+---------+-------+--------+
389     // |INITIALIZED |  close   |    x     |     \       | configure  |   x     | error |   x    |
390     // +------------+----------+----------+-------------+------------+---------+-------+--------+
391     // | CONFIGURED |  close   |    x     |     x       | configure  | request | error |   x    |
392     // +------------+----------+----------+-------------+------------+---------+-------+--------+
393     // |  STARTED   |  close   |    x     |     x       | configure  |    \    | error |   x    |
394     // +------------+----------+----------+-------------+------------+---------+-------+--------+
395     // |   ERROR    |  close   |    x     |     x       |     x      |    x    |   \   |  any   |
396     // +------------+----------+----------+-------------+------------+---------+-------+--------+
397     // |   DEINIT   |  close   |    x     |     x       |     x      |    x    |   x   |   \    |
398     // +------------+----------+----------+-------------+------------+---------+-------+--------+
399 
400     typedef enum {
401         CLOSED,
402         OPENED,
403         INITIALIZED,
404         CONFIGURED,
405         STARTED,
406         ERROR,
407         DEINIT
408     } State;
409 
410     int openCamera();
411     int closeCamera();
412     int flush(bool restartChannels, bool stopChannelImmediately = false);
413     static size_t calcMaxJpegSize(uint32_t camera_id);
414     cam_dimension_t getMaxRawSize(uint32_t camera_id);
415     static void addStreamConfig(Vector<int32_t> &available_stream_configs,
416             int32_t scalar_format, const cam_dimension_t &dim,
417             int32_t config_type);
418 
419     struct StreamValidateStatus {
420         bool bIsVideo, bIs4KVideo, bEisSupportedSize, depthPresent, bUseCommonFeatureMask;
421         bool isZsl, bSmallJpegSize, bYuv888OverrideJpeg, bEisSupported, bY80OnEncoder;
422         camera3_stream *inputStream;
423         cam_feature_mask_t commonFeatureMask;
424         size_t numStreamsOnEncoder;
425         uint32_t videoWidth, videoHeight;
426         cam_dimension_t maxViewfinderSize, largeYuv888Size;
StreamValidateStatusStreamValidateStatus427         StreamValidateStatus() :
428                 bIsVideo(false), bIs4KVideo(false), bEisSupportedSize(true), depthPresent(false),
429                 bUseCommonFeatureMask(false), isZsl(false), bSmallJpegSize(false),
430                 bYuv888OverrideJpeg(false), bEisSupported(false), bY80OnEncoder(false),
431                 inputStream(nullptr), commonFeatureMask(0), numStreamsOnEncoder(0),
432                 videoWidth(0U), videoHeight(0U) {};
433     };
434     static int32_t validateStreamCombination(uint32_t cameraId,
435             camera3_stream_configuration_t *streamList /*in*/,
436             StreamValidateStatus *status /*out*/);
437 
438     int validateCaptureRequest(camera3_capture_request_t *request,
439                                List<InternalRequest> &internallyRequestedStreams);
440     static int validateStreamDimensions(uint32_t cameraId,
441             camera3_stream_configuration_t *streamList);
442     static int validateStreamRotations(camera3_stream_configuration_t *streamList);
443     static int validateUsageFlags(uint32_t cameraId,
444             const camera3_stream_configuration_t *streamList);
445     static int validateUsageFlagsForEis(bool bEisEnable, bool bEisSupportedSize,
446             const camera3_stream_configuration_t *streamList);
447     void deriveMinFrameDuration();
448     void handleBuffersDuringFlushLock(camera3_stream_buffer_t *buffer);
449     int64_t getMinFrameDuration(const camera3_capture_request_t *request);
450     void handleMetadataWithLock(mm_camera_super_buf_t *metadata_buf,
451             bool free_and_bufdone_meta_buf,
452             bool lastUrgentMetadataInBatch,
453             bool lastMetadataInBatch,
454             bool *p_is_metabuf_queued);
455     void handleBatchMetadata(mm_camera_super_buf_t *metadata_buf,
456             bool free_and_bufdone_meta_buf);
457     void handleBufferWithLock(camera3_stream_buffer_t *buffer,
458             uint32_t frame_number);
459     void handleInputBufferWithLock(uint32_t frame_number);
460     // Handle pending results when a new result metadata of a frame is received.
461     // metadata callbacks are invoked in the order of frame number.
462     void handlePendingResultMetadataWithLock(uint32_t frameNumber,
463             camera_metadata_t *resultMetadata);
464     // Going through pending request list and send out result metadata for requests
465     // that are ready.
466     // frameNumber is the lastest frame whose result metadata is ready.
467     typedef enum {
468         NORMAL,
469         REPROCESS,
470         ZSL
471     } RequestType;
472     void dispatchResultMetadataWithLock(uint32_t frameNumber,
473             RequestType requestType, bool isHdrPlus);
474     void handleDepthDataLocked(const cam_depth_data_t &depthData,
475             uint32_t frameNumber, uint8_t valid);
476     void notifyErrorFoPendingDepthData(QCamera3DepthChannel *depthCh);
477     void unblockRequestIfNecessary();
478     void dumpMetadataToFile(tuning_params_t &meta, uint32_t &dumpFrameCount,
479             bool enabled, const char *type, uint32_t frameNumber);
480     static void getLogLevel();
481     static int32_t getPDStatIndex(cam_capability_t *caps);
482 
483     void cleanAndSortStreamInfo();
484     void extractJpegMetadata(CameraMetadata& jpegMetadata,
485             const camera3_capture_request_t *request);
486 
487     bool isSupportChannelNeeded(camera3_stream_configuration_t *streamList,
488             cam_stream_size_info_t stream_config_info);
489     bool isHdrSnapshotRequest(camera3_capture_request *request);
490     int32_t setMobicat();
491 
492     int32_t getSensorModeInfo(cam_sensor_mode_info_t &sensorModeInfo);
493     // Get information of the sensor mode that is currently selected.
494     int32_t getCurrentSensorModeInfo(cam_sensor_mode_info_t &sensorModeInfo);
495     int32_t setHalFpsRange(const CameraMetadata &settings,
496             metadata_buffer_t *hal_metadata);
497     int32_t extractSceneMode(const CameraMetadata &frame_settings, uint8_t metaMode,
498             metadata_buffer_t *hal_metadata);
499     int32_t setVideoHdrMode(metadata_buffer_t *hal_metadata,
500             cam_video_hdr_mode_t vhdr);
501     int32_t numOfSizesOnEncoder(const camera3_stream_configuration_t *streamList,
502             const cam_dimension_t &maxViewfinderSize);
503 
504     void addToPPFeatureMask(int stream_format, uint32_t stream_idx);
505     void updateFpsInPreviewBuffer(metadata_buffer_t *metadata, uint32_t frame_number);
506     void updateTimeStampInPendingBuffers(uint32_t frameNumber, nsecs_t timestamp);
507 
508     void enablePowerHint();
509     void disablePowerHint();
510     int32_t dynamicUpdateMetaStreamInfo();
511     int32_t startAllChannels();
512     int32_t stopAllChannels();
513     int32_t notifyErrorForPendingRequests();
514     void notifyError(uint32_t frameNumber,
515             camera3_error_msg_code_t errorCode);
516     int32_t getReprocessibleOutputStreamId(uint32_t &id);
517     int32_t handleCameraDeviceError(bool stopChannelImmediately = false);
518 
519     bool isEISEnabled(const CameraMetadata& meta);
520     static bool isOnEncoder(const cam_dimension_t max_viewfinder_size,
521             uint32_t width, uint32_t height);
522     void hdrPlusPerfLock(mm_camera_super_buf_t *metadata_buf);
523 
524     static bool supportBurstCapture(uint32_t cameraId);
525     int32_t setBundleInfo();
526     int32_t setInstantAEC(const CameraMetadata &meta);
527 
528     static void convertLandmarks(cam_face_landmarks_info_t face, int32_t* landmarks);
529     static void setInvalidLandmarks(int32_t* landmarks);
530 
531     static void setPAAFSupport(cam_feature_mask_t& feature_mask,
532             cam_stream_type_t stream_type,
533             cam_color_filter_arrangement_t filter_arrangement);
534     int32_t setSensorHDR(metadata_buffer_t *hal_metadata, bool enable,
535             bool isVideoHdrEnable = false);
536 
537     template <typename T>
538     static void adjustBlackLevelForCFA(T input[BLACK_LEVEL_PATTERN_CNT],
539             T output[BLACK_LEVEL_PATTERN_CNT],
540             cam_color_filter_arrangement_t color_arrangement);
541 
542     int32_t startChannelLocked();
543     void stopChannelLocked(bool stopChannelImmediately);
544 
545     camera3_device_t   mCameraDevice;
546     uint32_t           mCameraId;
547     mm_camera_vtbl_t  *mCameraHandle;
548     bool               mCameraInitialized;
549     camera_metadata_t *mDefaultMetadata[CAMERA3_TEMPLATE_COUNT];
550     const camera3_callback_ops_t *mCallbackOps;
551 
552     QCamera3MetadataChannel *mMetadataChannel;
553     QCamera3PicChannel *mPictureChannel;
554     QCamera3RawChannel *mRawChannel;
555     QCamera3SupportChannel *mSupportChannel;
556     QCamera3SupportChannel *mAnalysisChannel;
557     QCamera3RawDumpChannel *mRawDumpChannel;
558     QCamera3HdrPlusRawSrcChannel *mHdrPlusRawSrcChannel;
559     QCamera3RegularChannel *mDummyBatchChannel;
560     QCamera3DepthChannel *mDepthChannel;
561     cam_sensor_pd_data_t mDepthCloudMode; //Cache last configured mode
562     QCameraPerfLockMgr mPerfLockMgr;
563 
564     uint32_t mChannelHandle;
565 
566     void saveExifParams(metadata_buffer_t *metadata);
567     mm_jpeg_exif_params_t mExifParams;
568 
569      //First request yet to be processed after configureStreams
570     bool mFirstConfiguration;
571     bool mFlush;
572     bool mFlushPerf;
573     bool mEnableRawDump;
574     bool mForceHdrSnapshot;
575     QCamera3HeapMemory *mParamHeap;
576     metadata_buffer_t* mParameters;
577     metadata_buffer_t* mPrevParameters;
578     CameraMetadata mCurJpegMeta;
579     cam_is_type_t m_ISTypeVideo;
580     bool m_bIsVideo;
581     bool m_bIs4KVideo;
582     bool m_bEisSupportedSize;
583     bool m_bEisEnable;
584     bool m_bEis3PropertyEnabled;
585     bool m_bEisSupported;
586     bool m_bAVTimerEnabled;
587     typedef struct {
588         cam_dimension_t dim;
589         int format;
590         uint32_t usage;
591     } InputStreamInfo;
592 
593     InputStreamInfo mInputStreamInfo;
594     uint8_t m_MobicatMask;
595     uint8_t m_bTnrEnabled;
596     int8_t  mSupportedFaceDetectMode;
597     uint8_t m_bTnrPreview;
598     uint8_t m_bSwTnrPreview;
599     uint8_t m_bTnrVideo;
600     uint8_t m_debug_avtimer;
601     uint8_t m_bVideoHdrEnabled;
602     uint8_t m_cacModeDisabled;
603     uint8_t m_bForceInfinityAf;
604 
605     /* Data structure to store pending request */
606     typedef struct {
607         camera3_stream_t *stream;
608         camera3_stream_buffer_t *buffer;
609         // metadata needs to be consumed by the corresponding stream
610         // in order to generate the buffer.
611         bool need_metadata;
612         // Do we need additional crop due to EIS.
613         bool need_crop;
614         cam_eis_crop_info_t crop_info;
615     } RequestedBufferInfo;
616 
617     typedef struct {
618         uint32_t frame_number;
619         uint32_t num_buffers;
620         int32_t request_id;
621         List<RequestedBufferInfo> buffers;
622         List<InternalRequest> internalRequestList;
623         int blob_request;
624         uint8_t bUseFirstPartial; // Use first available partial result in case of jumpstart.
625         nsecs_t timestamp;
626         nsecs_t expectedFrameDuration;
627         camera3_stream_buffer_t *input_buffer;
628         const camera_metadata_t *settings;
629         const camera_metadata_t *resultMetadata; // Result metadata for this request.
630         CameraMetadata jpegMetadata;
631         uint8_t pipeline_depth;
632         uint32_t partial_result_cnt;
633         uint8_t capture_intent;
634         uint8_t fwkCacMode;
635         uint8_t hybrid_ae_enable;
636         uint8_t motion_detection_enable;
637         /* DevCamDebug metadata PendingRequestInfo */
638         uint8_t DevCamDebug_meta_enable;
639         /* DevCamDebug metadata end */
640 
641         bool focusStateSent = false;
642         bool focusStateValid = false;
643         uint8_t focusState = ANDROID_CONTROL_AF_STATE_INACTIVE;
644 
645         bool enableZsl; // If ZSL is enabled.
646         bool hdrplus; // If this is an HDR+ request.
647         uint8_t requestedLensShadingMapMode; // Lens shading map mode for this request.
648         uint8_t requestedFaceDetectMode; // Face detect mode for this request.
649         bool partialResultDropped; // Whether partial metadata is dropped.
650         uint8_t requestedOisDataMode; // OIS data mode for this request.
651     } PendingRequestInfo;
652     typedef struct {
653         uint32_t frame_number;
654         uint32_t stream_ID;
655     } PendingFrameDropInfo;
656 
657     class FrameNumberRegistry _orchestrationDb;
658     typedef KeyedVector<uint32_t, Vector<PendingBufferInfo> > FlushMap;
659     typedef List<QCamera3HardwareInterface::PendingRequestInfo>::iterator
660             pendingRequestIterator;
661     typedef List<QCamera3HardwareInterface::RequestedBufferInfo>::iterator
662             pendingBufferIterator;
663 
664     List<PendingRequestInfo> mPendingRequestsList;
665     List<PendingFrameDropInfo> mPendingFrameDropList;
666     /* Use last frame number of the batch as key and first frame number of the
667      * batch as value for that key */
668     KeyedVector<uint32_t, uint32_t> mPendingBatchMap;
669     cam_stream_ID_t mBatchedStreamsArray;
670 
671     PendingBuffersMap mPendingBuffersMap;
672     pthread_cond_t mRequestCond;
673     uint32_t mPendingLiveRequest;
674     bool mWokenUpByDaemon;
675     int32_t mCurrentRequestId;
676     cam_stream_size_info_t mStreamConfigInfo;
677 
678     ShutterDispatcher mShutterDispatcher;
679     OutputBufferDispatcher mOutputBufferDispatcher;
680 
681     //mutex for serialized access to camera3_device_ops_t functions
682     pthread_mutex_t mMutex;
683 
684     //condition used to signal flush after buffers have returned
685     pthread_cond_t mBuffersCond;
686 
687     List<stream_info_t*> mStreamInfo;
688 
689     int64_t mMinProcessedFrameDuration;
690     int64_t mMinJpegFrameDuration;
691     int64_t mMinRawFrameDuration;
692     nsecs_t mExpectedFrameDuration;
693     nsecs_t mExpectedInflightDuration;
694     static const nsecs_t kDefaultExpectedDuration = 100000000; // 100 ms
695 
696     uint32_t mMetaFrameCount;
697     bool    mUpdateDebugLevel;
698     const camera_module_callbacks_t *mCallbacks;
699 
700     uint8_t mCaptureIntent;
701     uint8_t mCacMode;
702     // DevCamDebug metadata internal variable
703     uint8_t mDevCamDebugMetaEnable;
704     /* DevCamDebug metadata end */
705 
706     metadata_buffer_t mReprocMeta; //scratch meta buffer
707     /* 0: Not batch, non-zero: Number of image buffers in a batch */
708     uint8_t mBatchSize;
709     // Used only in batch mode
710     uint8_t mToBeQueuedVidBufs;
711     // Fixed video fps
712     float mHFRVideoFps;
713 public:
714     uint32_t mOpMode;
715     bool mStreamConfig;
716     QCameraCommon   mCommon;
717 private:
718     uint32_t mFirstFrameNumberInBatch;
719     camera3_stream_t mDummyBatchStream;
720     bool mNeedSensorRestart;
721     bool mPreviewStarted;
722     uint32_t mMinInFlightRequests;
723     uint32_t mMaxInFlightRequests;
724     bool mPDSupported;
725     int32_t mPDIndex;
726     // Param to trigger instant AEC.
727     bool mInstantAEC;
728     // Param to know when to reset AEC
729     bool mResetInstantAEC;
730     // Frame number, untill which we need to drop the frames.
731     uint32_t mInstantAECSettledFrameNumber;
732     // Max number of frames, that HAL will hold without displaying, for instant AEC mode.
733     uint8_t mAecSkipDisplayFrameBound;
734     // Counter to keep track of number of frames that took for AEC convergence.
735     uint8_t mInstantAecFrameIdxCount;
736     /* sensor output size with current stream configuration */
737     QCamera3CropRegionMapper mCropRegionMapper;
738     // Last lens shading map mode framework requsted.
739     uint8_t mLastRequestedLensShadingMapMode;
740     // Last face detect mode framework requsted.
741     uint8_t mLastRequestedFaceDetectMode;
742     // Last OIS data mode framework requested.
743     uint8_t mLastRequestedOisDataMode;
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 
818     State mState;
819     //Dual camera related params
820     bool mIsDeviceLinked;
821     bool mIsMainCamera;
822     uint8_t mLinkedCameraId;
823     QCamera3HeapMemory *m_pDualCamCmdHeap;
824     cam_dual_camera_cmd_info_t *m_pDualCamCmdPtr;
825     cam_sync_related_sensors_event_info_t m_relCamSyncInfo;
826     Mutex mFlushLock;
827     bool m60HzZone;
828 
829     // Issue an additional RAW for every 10 requests to control RAW capture rate. Requesting RAW
830     // too often will cause frame drops due to latency of sending RAW to HDR+ service.
831     const static uint32_t kHdrPlusRawPeriod = 10;
832 
833     // Define a pending HDR+ request submitted to HDR+ service and not yet received by HAL.
834     struct HdrPlusPendingRequest {
835         // HDR+ stream ID -> output buffer to be filled by HDR+ client with an HDR+ processed frame.
836         std::map<uint32_t, std::shared_ptr<mm_camera_buf_def_t>> outputBuffers;
837 
838         // HDR+ stream ID -> output buffers in camera framework's request.
839         std::map<uint32_t, camera3_stream_buffer_t> frameworkOutputBuffers;
840 
841         // Settings in camera framework's request.
842         std::shared_ptr<metadata_buffer_t> settings;
843     };
844 
845     // Fill pbcamera::StreamConfiguration based on the channel stream.
846     status_t fillPbStreamConfig(pbcamera::StreamConfiguration *config, uint32_t pbStreamId,
847             QCamera3Channel *channel, uint32_t streamIndex);
848 
849     // Open HDR+ client asynchronously.
850     status_t openHdrPlusClientAsyncLocked();
851 
852     // Enable HDR+ mode. Easel will start capturing ZSL buffers.
853     status_t enableHdrPlusModeLocked();
854 
855     // Disable HDR+ mode. Easel will stop capturing ZSL buffers.
856     void disableHdrPlusModeLocked();
857 
858     // Return if current session with configured streams is compatible with HDR+ mode.
859     bool isSessionHdrPlusModeCompatible();
860 
861     // Return if the request is compatible with HDR+.
862     bool isRequestHdrPlusCompatible(
863             const camera3_capture_request_t &request, const CameraMetadata &metadata);
864 
865     // Configure streams for HDR+.
866     status_t configureHdrPlusStreamsLocked();
867 
868     // Check whether additional EIS crop is needed.
869     bool isEISCropInSnapshotNeeded(const CameraMetadata &metadata) const;
870 
871     // Various crop sanity checks.
872     bool isCropValid(int32_t startX, int32_t startY, int32_t width,
873             int32_t height, int32_t maxWidth, int32_t maxHeight) const;
874 
875     // Try to submit an HDR+ request. Returning true if an HDR+ request was submitted. Returning
876     // false if it is not an HDR+ request or submitting an HDR+ request failed. Must be called with
877     // gHdrPlusClientLock held.
878     bool trySubmittingHdrPlusRequestLocked(HdrPlusPendingRequest *hdrPlusRequest,
879         const camera3_capture_request_t &request, const CameraMetadata &metadata);
880 
881     // Abort an HDR+ request that was not submitted successfully in
882     // trySubmittingHdrPlusRequestLocked.
883     void abortPendingHdrplusRequest(HdrPlusPendingRequest *hdrPlusRequest);
884 
885     // Update HDR+ result metadata with the still capture's request settings.
886     void updateHdrPlusResultMetadata(CameraMetadata &resultMetadata,
887             std::shared_ptr<metadata_buffer_t> settings);
888 
889     // Wait until opening HDR+ client completes if it's being opened.
890     void finishHdrPlusClientOpeningLocked(std::unique_lock<std::mutex> &lock);
891 
892     // Handle Easel error asynchronuously in another thread.
893     void handleEaselFatalErrorAsync();
894 
895     // Handle Easel error.
896     void handleEaselFatalError();
897 
898     // Close HDR+ client. Must be protected by gHdrPlusClientLock.
899     void closeHdrPlusClientLocked();
900 
901     // Easel manager client callbacks.
902     void onEaselFatalError(std::string errMsg);
903     void onThermalThrottle();
904 
905     // Clean up and wait for Easel error future.
906     void cleanupEaselErrorFuture();
907 
908     // HDR+ client callbacks.
909     void onOpened(std::unique_ptr<HdrPlusClient> client) override;
910     void onOpenFailed(status_t err) override;
911     void onFatalError() override;
912     void onCaptureResult(pbcamera::CaptureResult *result,
913             const camera_metadata_t &resultMetadata) override;
914     void onFailedCaptureResult(pbcamera::CaptureResult *failedResult) override;
915     void onShutter(uint32_t requestId, int64_t apSensorTimestampNs) override;
916     void onNextCaptureReady(uint32_t requestId) override;
917     void onPostview(uint32_t requestId, std::unique_ptr<std::vector<uint8_t>> postview,
918             uint32_t width, uint32_t height, uint32_t stride, int32_t format) override;
919 
920     nsecs_t calculateMaxExpectedDuration(const camera_metadata_t *request);
921     void getExpectedFrameDuration(const camera_metadata_t *request, nsecs_t *frameDuration);
922 
923     // Map from frame number to frame. Must be protected by mHdrPlusPendingRequestsLock.
924     std::map<uint32_t, HdrPlusPendingRequest> mHdrPlusPendingRequests;
925     Mutex mHdrPlusPendingRequestsLock;
926 
927     // If HDR+ mode is enabled i.e. if Easel is capturing ZSL buffers.
928     bool mHdrPlusModeEnabled;
929 
930     // If ZSL is enabled (android.control.enableZsl).
931     bool mZslEnabled;
932 
933     // If Easel MIPI has been started.
934     bool mEaselMipiStarted;
935 
936     // If HAL provides RAW input buffers to Easel. This is just for prototyping.
937     bool mIsApInputUsedForHdrPlus;
938 
939     // Current sensor mode information.
940     cam_sensor_mode_info_t mSensorModeInfo;
941 
942     // If there is a capture request with preview intent since stream configuration.
943     bool mFirstPreviewIntentSeen;
944 
945     bool m_bSensorHDREnabled;
946 
947     cam_trigger_t mAfTrigger;
948 
949     int32_t mSceneDistance;
950 
951     std::mutex mEaselErrorFutureLock;
952     std::future<void> mEaselErrorFuture;
953 
954     // If HDR+ should be thermal throttled.
955     std::atomic<bool> mEaselThermalThrottled = false;
956 
957     // Thread to handle callbacks from HDR+ client. Protected by gHdrPlusClientLock.
958     sp<QCamera3HdrPlusListenerThread> mQCamera3HdrPlusListenerThread;
959 
960     // Read sensor calibration XML file for lens calibration fields. On failure to read
961     // the file, leaves passed-in values unchanged and returns false.
962     static bool readSensorCalibration(int activeArrayWidth,
963             float poseRotation[4], float poseTranslation[3],
964             float cameraIntrinsics[5], float radialDistortion[6]);
965 
966     // Parse a string of form " [ x; y; z ...]" into a floating-point array.
967     // Returns false on parse error
968     static bool parseStringArray(const char *str, float *dest, int count);
969 
isStillZsl(const PendingRequestInfo & requestInfo)970     static bool isStillZsl(const PendingRequestInfo& requestInfo) {
971         return requestInfo.enableZsl &&
972                 requestInfo.capture_intent == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
973     }
974 
975     float mLastFocusDistance;
976 
977     // Last cached EIS crop info.
978     cam_eis_crop_info_t mLastEISCropInfo;
979 
980     // Maps between active region and specific stream crop.
981     QCamera3CropRegionMapper mStreamCropMapper;
982 };
983 
984 }; // namespace qcamera
985 
986 #endif /* __QCAMERA2HARDWAREINTERFACE_H__ */
987