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