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