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, uint8_t hybrid_ae_enable, 177 bool pprocDone, bool dynamic_blklvl); 178 camera_metadata_t* saveRequestSettings(const CameraMetadata& jpegMetadata, 179 camera3_capture_request_t *request); 180 int initParameters(); 181 void deinitParameters(); 182 QCamera3ReprocessChannel *addOfflineReprocChannel(const reprocess_config_t &config, 183 QCamera3ProcessingChannel *inputChHandle); 184 bool needRotationReprocess(); 185 bool needReprocess(uint32_t postprocess_mask); 186 bool needJpegRotation(); 187 cam_denoise_process_type_t getWaveletDenoiseProcessPlate(); 188 cam_denoise_process_type_t getTemporalDenoiseProcessPlate(); 189 190 void captureResultCb(mm_camera_super_buf_t *metadata, 191 camera3_stream_buffer_t *buffer, uint32_t frame_number, 192 bool isInputBuffer); 193 cam_dimension_t calcMaxJpegDim(); 194 bool needOnlineRotation(); 195 uint32_t getJpegQuality(); 196 QCamera3Exif *getExifData(); 197 mm_jpeg_exif_params_t get3AExifParams(); 198 uint8_t getMobicatMask(); 199 static void getFlashInfo(const int cameraId, 200 bool& hasFlash, 201 char (&flashNode)[QCAMERA_MAX_FILEPATH_LENGTH]); 202 const char *getEepromVersionInfo(); 203 const uint32_t *getLdafCalib(); 204 void get3AVersion(cam_q3a_version_t &swVersion); 205 206 template <typename fwkType, typename halType> struct QCameraMap { 207 fwkType fwk_name; 208 halType hal_name; 209 }; 210 211 typedef struct { 212 const char *const desc; 213 cam_cds_mode_type_t val; 214 } QCameraPropMap; 215 216 bool getBlackLevelRegion(int (&opticalBlackRegions)[4]); 217 void sendDynamicBlackLevel(float blacklevel[4], uint32_t frame_number); 218 void sendDynamicBlackLevelWithLock(float blacklevel[4], uint32_t frame_number); 219 220 private: 221 222 int openCamera(); 223 int closeCamera(); 224 static size_t calcMaxJpegSize(uint32_t camera_id); 225 cam_dimension_t getMaxRawSize(uint32_t camera_id); 226 static void addStreamConfig(Vector<int32_t> &available_stream_configs, 227 int32_t scalar_format, const cam_dimension_t &dim, 228 int32_t config_type); 229 230 int validateCaptureRequest(camera3_capture_request_t *request); 231 int validateStreamDimensions(camera3_stream_configuration_t *streamList); 232 int validateStreamRotations(camera3_stream_configuration_t *streamList); 233 void deriveMinFrameDuration(); 234 int32_t handlePendingReprocResults(uint32_t frame_number); 235 int64_t getMinFrameDuration(const camera3_capture_request_t *request); 236 void handleMetadataWithLock(mm_camera_super_buf_t *metadata_buf, 237 bool free_and_bufdone_meta_buf); 238 void handleBatchMetadata(mm_camera_super_buf_t *metadata_buf, 239 bool free_and_bufdone_meta_buf); 240 void handleBufferWithLock(camera3_stream_buffer_t *buffer, 241 uint32_t frame_number); 242 void handleInputBufferWithLock(uint32_t frame_number); 243 void unblockRequestIfNecessary(); 244 void dumpMetadataToFile(tuning_params_t &meta, uint32_t &dumpFrameCount, 245 bool enabled, const char *type, uint32_t frameNumber); 246 static void getLogLevel(); 247 248 void cleanAndSortStreamInfo(); 249 void extractJpegMetadata(CameraMetadata& jpegMetadata, 250 const camera3_capture_request_t *request); 251 252 bool isSupportChannelNeeded(camera3_stream_configuration_t *streamList, 253 cam_stream_size_info_t stream_config_info); 254 int32_t setMobicat(); 255 256 int32_t setHalFpsRange(const CameraMetadata &settings, 257 metadata_buffer_t *hal_metadata); 258 int32_t extractSceneMode(const CameraMetadata &frame_settings, uint8_t metaMode, 259 metadata_buffer_t *hal_metadata); 260 int32_t numOfSizesOnEncoder(const camera3_stream_configuration_t *streamList, 261 const cam_dimension_t &maxViewfinderSize); 262 263 void enablePowerHint(); 264 void disablePowerHint(); 265 int32_t getSensorOutputSize(cam_dimension_t &sensor_dim); 266 int32_t dynamicUpdateMetaStreamInfo(); 267 int32_t startAllChannels(); 268 int32_t stopAllChannels(); 269 int32_t notifyErrorForPendingRequests(); 270 int32_t getReprocessibleOutputStreamId(uint32_t &id); 271 272 bool isOnEncoder(const cam_dimension_t max_viewfinder_size, 273 uint32_t width, uint32_t height); 274 void hdrPlusPerfLock(mm_camera_super_buf_t *metadata_buf); 275 276 int32_t setBundleInfo(); 277 278 camera3_device_t mCameraDevice; 279 uint32_t mCameraId; 280 mm_camera_vtbl_t *mCameraHandle; 281 bool mCameraOpened; 282 bool mCameraInitialized; 283 camera_metadata_t *mDefaultMetadata[CAMERA3_TEMPLATE_COUNT]; 284 const camera3_callback_ops_t *mCallbackOps; 285 286 QCamera3MetadataChannel *mMetadataChannel; 287 QCamera3PicChannel *mPictureChannel; 288 QCamera3RawChannel *mRawChannel; 289 QCamera3SupportChannel *mSupportChannel; 290 QCamera3SupportChannel *mAnalysisChannel; 291 QCamera3RawDumpChannel *mRawDumpChannel; 292 QCamera3RegularChannel *mDummyBatchChannel; 293 QCameraPerfLock m_perfLock; 294 295 uint32_t mChannelHandle; 296 297 void saveExifParams(metadata_buffer_t *metadata); 298 mm_jpeg_exif_params_t mExifParams; 299 300 //First request yet to be processed after configureStreams 301 bool mFirstRequest; 302 bool mFirstConfiguration; 303 bool mFlush; 304 bool mEnableRawDump; 305 QCamera3HeapMemory *mParamHeap; 306 metadata_buffer_t* mParameters; 307 metadata_buffer_t* mPrevParameters; 308 CameraMetadata mCurJpegMeta; 309 bool m_bIsVideo; 310 bool m_bIs4KVideo; 311 bool m_bEisSupportedSize; 312 bool m_bEisEnable; 313 typedef struct { 314 cam_dimension_t dim; 315 int format; 316 uint32_t usage; 317 } InputStreamInfo; 318 319 InputStreamInfo mInputStreamInfo; 320 uint8_t m_MobicatMask; 321 uint8_t mSupportedFaceDetectMode; 322 uint8_t m_bTnrEnabled; 323 uint8_t m_bTnrPreview; 324 uint8_t m_bTnrVideo; 325 cam_cds_mode_type_t m_CdsPreference; 326 /* Data structure to store pending request */ 327 typedef struct { 328 camera3_stream_t *stream; 329 camera3_stream_buffer_t *buffer; 330 // metadata needs to be consumed by the corresponding stream 331 // in order to generate the buffer. 332 bool need_metadata; 333 } RequestedBufferInfo; 334 typedef struct { 335 uint32_t frame_number; 336 uint32_t num_buffers; 337 int32_t request_id; 338 List<RequestedBufferInfo> buffers; 339 int blob_request; 340 uint8_t bUrgentReceived; 341 nsecs_t timestamp; 342 camera3_stream_buffer_t *input_buffer; 343 const camera_metadata_t *settings; 344 CameraMetadata jpegMetadata; 345 uint8_t pipeline_depth; 346 uint32_t partial_result_cnt; 347 uint8_t capture_intent; 348 bool shutter_notified; 349 uint8_t hybrid_ae_enable; 350 bool need_dynamic_blklvl; 351 bool pending_extra_result; 352 } PendingRequestInfo; 353 typedef struct { 354 uint32_t frame_number; 355 uint32_t stream_ID; 356 } PendingFrameDropInfo; 357 358 // Store the Pending buffers for Flushing 359 typedef struct { 360 // Frame number pertaining to the buffer 361 uint32_t frame_number; 362 camera3_stream_t *stream; 363 // Buffer handle 364 buffer_handle_t *buffer; 365 366 } PendingBufferInfo; 367 368 typedef struct { 369 // Total number of buffer requests pending 370 uint32_t num_buffers; 371 // List of pending buffers 372 List<PendingBufferInfo> mPendingBufferList; 373 // Last frame number requested 374 uint32_t last_frame_number; 375 } PendingBuffersMap; 376 377 typedef struct { 378 camera3_notify_msg_t notify_msg; 379 camera3_stream_buffer_t buffer; 380 uint32_t frame_number; 381 } PendingReprocessResult; 382 383 typedef KeyedVector<uint32_t, Vector<PendingBufferInfo> > FlushMap; 384 typedef List<QCamera3HardwareInterface::PendingRequestInfo>::iterator 385 pendingRequestIterator; 386 typedef List<QCamera3HardwareInterface::RequestedBufferInfo>::iterator 387 pendingBufferIterator; 388 389 List<PendingReprocessResult> mPendingReprocessResultList; 390 List<PendingRequestInfo> mPendingRequestsList; 391 List<PendingFrameDropInfo> mPendingFrameDropList; 392 /* Use last frame number of the batch as key and first frame number of the 393 * batch as value for that key */ 394 KeyedVector<uint32_t, uint32_t> mPendingBatchMap; 395 396 PendingBuffersMap mPendingBuffersMap; 397 pthread_cond_t mRequestCond; 398 uint32_t mPendingLiveRequest; 399 bool mWokenUpByDaemon; 400 int32_t mCurrentRequestId; 401 cam_stream_size_info_t mStreamConfigInfo; 402 403 //mutex for serialized access to camera3_device_ops_t functions 404 pthread_mutex_t mMutex; 405 406 List<stream_info_t*> mStreamInfo; 407 408 int64_t mMinProcessedFrameDuration; 409 int64_t mMinJpegFrameDuration; 410 int64_t mMinRawFrameDuration; 411 412 uint32_t mMetaFrameCount; 413 bool mUpdateDebugLevel; 414 const camera_module_callbacks_t *mCallbacks; 415 416 uint8_t mCaptureIntent; 417 uint8_t mHybridAeEnable; 418 metadata_buffer_t mReprocMeta; //scratch meta buffer 419 /* 0: Not batch, non-zero: Number of image buffers in a batch */ 420 uint8_t mBatchSize; 421 // Used only in batch mode 422 uint8_t mToBeQueuedVidBufs; 423 // Fixed video fps 424 float mHFRVideoFps; 425 uint8_t mOpMode; 426 uint32_t mFirstFrameNumberInBatch; 427 camera3_stream_t mDummyBatchStream; 428 bool mNeedSensorRestart; 429 430 /* sensor output size with current stream configuration */ 431 QCamera3CropRegionMapper mCropRegionMapper; 432 433 /* Ldaf calibration data */ 434 bool mLdafCalibExist; 435 uint32_t mLdafCalib[2]; 436 bool mPowerHintEnabled; 437 int32_t mLastCustIntentFrmNum; 438 439 static const QCameraMap<camera_metadata_enum_android_control_effect_mode_t, 440 cam_effect_mode_type> EFFECT_MODES_MAP[]; 441 static const QCameraMap<camera_metadata_enum_android_control_awb_mode_t, 442 cam_wb_mode_type> WHITE_BALANCE_MODES_MAP[]; 443 static const QCameraMap<camera_metadata_enum_android_control_scene_mode_t, 444 cam_scene_mode_type> SCENE_MODES_MAP[]; 445 static const QCameraMap<camera_metadata_enum_android_control_af_mode_t, 446 cam_focus_mode_type> FOCUS_MODES_MAP[]; 447 static const QCameraMap<camera_metadata_enum_android_color_correction_aberration_mode_t, 448 cam_aberration_mode_t> COLOR_ABERRATION_MAP[]; 449 static const QCameraMap<camera_metadata_enum_android_control_ae_antibanding_mode_t, 450 cam_antibanding_mode_type> ANTIBANDING_MODES_MAP[]; 451 static const QCameraMap<camera_metadata_enum_android_lens_state_t, 452 cam_af_lens_state_t> LENS_STATE_MAP[]; 453 static const QCameraMap<camera_metadata_enum_android_control_ae_mode_t, 454 cam_flash_mode_t> AE_FLASH_MODE_MAP[]; 455 static const QCameraMap<camera_metadata_enum_android_flash_mode_t, 456 cam_flash_mode_t> FLASH_MODES_MAP[]; 457 static const QCameraMap<camera_metadata_enum_android_statistics_face_detect_mode_t, 458 cam_face_detect_mode_t> FACEDETECT_MODES_MAP[]; 459 static const QCameraMap<camera_metadata_enum_android_lens_info_focus_distance_calibration_t, 460 cam_focus_calibration_t> FOCUS_CALIBRATION_MAP[]; 461 static const QCameraMap<camera_metadata_enum_android_sensor_test_pattern_mode_t, 462 cam_test_pattern_mode_t> TEST_PATTERN_MAP[]; 463 static const QCameraMap<camera_metadata_enum_android_sensor_reference_illuminant1_t, 464 cam_illuminat_t> REFERENCE_ILLUMINANT_MAP[]; 465 static const QCameraMap<int32_t, 466 cam_hfr_mode_t> HFR_MODE_MAP[]; 467 468 static const QCameraPropMap CDS_MAP[]; 469 470 pendingRequestIterator erasePendingRequest(pendingRequestIterator i); 471 }; 472 473 }; // namespace qcamera 474 475 #endif /* __QCAMERA2HARDWAREINTERFACE_H__ */ 476