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 41 #include <hardware/power.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 using namespace android; 58 59 namespace qcamera { 60 61 #ifndef TRUE 62 #define TRUE 1 63 #endif 64 65 #ifndef FALSE 66 #define FALSE 0 67 #endif 68 69 #define MAX(a, b) ((a) > (b) ? (a) : (b)) 70 #define MIN(a, b) ((a) < (b) ? (a) : (b)) 71 72 /* Time related macros */ 73 typedef int64_t nsecs_t; 74 #define NSEC_PER_SEC 1000000000LL 75 #define NSEC_PER_USEC 1000 76 #define NSEC_PER_33MSEC 33000000LL 77 78 extern volatile uint32_t gCamHal3LogLevel; 79 80 class QCamera3MetadataChannel; 81 class QCamera3PicChannel; 82 class QCamera3HeapMemory; 83 class QCamera3Exif; 84 85 typedef struct { 86 camera3_stream_t *stream; 87 camera3_stream_buffer_set_t buffer_set; 88 stream_status_t status; 89 int registered; 90 QCamera3Channel *channel; 91 } stream_info_t; 92 93 class QCamera3HardwareInterface { 94 public: 95 /* static variable and functions accessed by camera service */ 96 static camera3_device_ops_t mCameraOps; 97 static int initialize(const struct camera3_device *, 98 const camera3_callback_ops_t *callback_ops); 99 static int configure_streams(const struct camera3_device *, 100 camera3_stream_configuration_t *stream_list); 101 static const camera_metadata_t* construct_default_request_settings( 102 const struct camera3_device *, int type); 103 static int process_capture_request(const struct camera3_device *, 104 camera3_capture_request_t *request); 105 106 static void dump(const struct camera3_device *, int fd); 107 static int flush(const struct camera3_device *); 108 static int close_camera_device(struct hw_device_t* device); 109 110 public: 111 QCamera3HardwareInterface(int cameraId, 112 const camera_module_callbacks_t *callbacks); 113 virtual ~QCamera3HardwareInterface(); 114 static void camEvtHandle(uint32_t camera_handle, mm_camera_event_t *evt, 115 void *user_data); 116 int openCamera(struct hw_device_t **hw_device); 117 int getMetadata(int type); 118 camera_metadata_t* translateCapabilityToMetadata(int type); 119 120 static int getCamInfo(int cameraId, struct camera_info *info); 121 static int initCapabilities(int cameraId); 122 static int initStaticMetadata(int cameraId); 123 static void makeTable(cam_dimension_t* dimTable, uint8_t size, int32_t* sizeTable); 124 static void makeFPSTable(cam_fps_range_t* fpsTable, uint8_t size, 125 int32_t* fpsRangesTable); 126 static void makeOverridesList(cam_scene_mode_overrides_t* overridesTable, uint8_t size, 127 uint8_t* overridesList, uint8_t* supported_indexes, int camera_id); 128 static uint8_t filterJpegSizes(int32_t* jpegSizes, int32_t* processedSizes, 129 uint8_t processedSizesCnt, uint8_t maxCount, 130 cam_rect_t active_array_size, uint8_t downscale_factor); 131 static void convertToRegions(cam_rect_t rect, int32_t* region, int weight); 132 static void convertFromRegions(cam_area_t* roi, const camera_metadata_t *settings, 133 uint32_t tag); 134 static bool resetIfNeededROI(cam_area_t* roi, const cam_crop_region_t* scalerCropRegion); 135 static void convertLandmarks(cam_face_detection_info_t face, int32_t* landmarks); 136 static void postproc_channel_cb_routine(mm_camera_super_buf_t *recvd_frame, 137 void *userdata); 138 static int32_t getScalarFormat(int32_t format); 139 static int32_t getSensorSensitivity(int32_t iso_mode); 140 141 static double computeNoiseModelEntryS(int32_t sensitivity); 142 static double computeNoiseModelEntryO(int32_t sensitivity); 143 144 static void captureResultCb(mm_camera_super_buf_t *metadata, 145 camera3_stream_buffer_t *buffer, uint32_t frame_number, 146 void *userdata); 147 148 int initialize(const camera3_callback_ops_t *callback_ops); 149 int configureStreams(camera3_stream_configuration_t *stream_list); 150 int processCaptureRequest(camera3_capture_request_t *request); 151 void dump(int fd); 152 int flush(); 153 154 int setFrameParameters(camera3_capture_request_t *request, 155 cam_stream_ID_t streamID, int blob_request, uint32_t snapshotStreamId); 156 int32_t setReprocParameters(camera3_capture_request_t *request, 157 metadata_buffer_t *reprocParam, uint32_t snapshotStreamId); 158 int translateToHalMetadata(const camera3_capture_request_t *request, 159 metadata_buffer_t *parm, uint32_t snapshotStreamId); 160 camera_metadata_t* translateCbUrgentMetadataToResultMetadata ( 161 metadata_buffer_t *metadata); 162 163 camera_metadata_t* translateFromHalMetadata(metadata_buffer_t *metadata, 164 nsecs_t timestamp, int32_t request_id, 165 const CameraMetadata& jpegMetadata, uint8_t pipeline_depth, 166 uint8_t capture_intent); 167 int getJpegSettings(const camera_metadata_t *settings); 168 int initParameters(); 169 void deinitParameters(); 170 QCamera3ReprocessChannel *addOfflineReprocChannel(const reprocess_config_t &config, 171 QCamera3PicChannel *picChHandle, metadata_buffer_t *metadata); 172 bool needRotationReprocess(); 173 bool needReprocess(uint32_t postprocess_mask); 174 bool needJpegRotation(); 175 bool isWNREnabled(); 176 bool isCACEnabled(); 177 cam_denoise_process_type_t getWaveletDenoiseProcessPlate(); 178 179 void captureResultCb(mm_camera_super_buf_t *metadata, 180 camera3_stream_buffer_t *buffer, uint32_t frame_number); 181 182 typedef struct { 183 uint32_t fwk_name; 184 uint8_t hal_name; 185 } QCameraMap; 186 187 typedef struct { 188 const char *const desc; 189 cam_cds_mode_type_t val; 190 } QCameraPropMap; 191 192 private: 193 194 int openCamera(); 195 int closeCamera(); 196 int AddSetParmEntryToBatch(parm_buffer_t *p_table, 197 cam_intf_parm_type_t paramType, 198 uint32_t paramLength, 199 void *paramValue); 200 static int8_t lookupHalName(const QCameraMap arr[], 201 int len, unsigned int fwk_name); 202 static int32_t lookupFwkName(const QCameraMap arr[], 203 int len, int hal_name); 204 static cam_cds_mode_type_t lookupProp(const QCameraPropMap arr[], 205 int len, const char *name); 206 static int calcMaxJpegSize(uint8_t camera_id); 207 cam_dimension_t getMaxRawSize(uint8_t camera_id); 208 209 int validateCaptureRequest(camera3_capture_request_t *request); 210 int validateStreamDimensions(camera3_stream_configuration_t *streamList); 211 int validateStreamRotations(camera3_stream_configuration_t *streamList); 212 void deriveMinFrameDuration(); 213 int32_t handlePendingReprocResults(uint32_t frame_number); 214 int64_t getMinFrameDuration(const camera3_capture_request_t *request); 215 void handleMetadataWithLock(mm_camera_super_buf_t *metadata_buf); 216 void handleBufferWithLock(camera3_stream_buffer_t *buffer, 217 uint32_t frame_number); 218 void unblockRequestIfNecessary(); 219 void dumpMetadataToFile(tuning_params_t &meta, 220 uint32_t &dumpFrameCount, 221 int32_t enabled, 222 const char *type, 223 uint32_t frameNumber); 224 static void getLogLevel(); 225 226 void cleanAndSortStreamInfo(); 227 int queueReprocMetadata(metadata_buffer_t *metadata); 228 void extractJpegMetadata(CameraMetadata& jpegMetadata, 229 const camera3_capture_request_t *request); 230 bool isSupportChannelNeeded(camera3_stream_configuration_t *streamList, 231 cam_stream_size_info_t stream_config_info); 232 public: 233 cam_dimension_t calcMaxJpegDim(); 234 bool needOnlineRotation(); 235 int getJpegQuality(); 236 QCamera3Exif *getExifData(); 237 static void getFlashInfo(const int cameraId, 238 bool& hasFlash, 239 char (&flashNode)[QCAMERA_MAX_FILEPATH_LENGTH]); 240 241 private: 242 camera3_device_t mCameraDevice; 243 uint8_t mCameraId; 244 mm_camera_vtbl_t *mCameraHandle; 245 bool mCameraOpened; 246 bool mCameraInitialized; 247 camera_metadata_t *mDefaultMetadata[CAMERA3_TEMPLATE_COUNT]; 248 int mBlobRequest; 249 250 const camera3_callback_ops_t *mCallbackOps; 251 252 QCamera3MetadataChannel *mMetadataChannel; 253 QCamera3PicChannel *mPictureChannel; 254 QCamera3RawChannel *mRawChannel; 255 QCamera3SupportChannel *mSupportChannel; 256 QCamera3RawDumpChannel *mRawDumpChannel; 257 258 //First request yet to be processed after configureStreams 259 bool mFirstRequest; 260 bool mFlush; 261 bool mEnableRawDump; 262 QCamera3HeapMemory *mParamHeap; 263 metadata_buffer_t* mParameters; 264 metadata_buffer_t* mPrevParameters; 265 bool m_bWNROn; 266 bool m_bIsVideo; 267 bool m_bIs4KVideo; 268 uint8_t mEisEnable; 269 270 /* Data structure to store pending request */ 271 typedef struct { 272 camera3_stream_t *stream; 273 camera3_stream_buffer_t *buffer; 274 } RequestedBufferInfo; 275 typedef struct { 276 uint32_t frame_number; 277 uint32_t num_buffers; 278 int32_t request_id; 279 List<RequestedBufferInfo> buffers; 280 int blob_request; 281 uint8_t bUrgentReceived; 282 nsecs_t timestamp; 283 camera3_stream_buffer_t *input_buffer; 284 const camera_metadata_t *settings; 285 CameraMetadata jpegMetadata; 286 uint8_t pipeline_depth; 287 uint32_t partial_result_cnt; 288 uint8_t capture_intent; 289 } PendingRequestInfo; 290 typedef struct { 291 uint32_t frame_number; 292 uint32_t stream_ID; 293 } PendingFrameDropInfo; 294 295 // Store the Pending buffers for Flushing 296 typedef struct { 297 // Frame number pertaining to the buffer 298 uint32_t frame_number; 299 camera3_stream_t *stream; 300 // Buffer handle 301 buffer_handle_t *buffer; 302 } PendingBufferInfo; 303 304 typedef struct { 305 // Total number of buffer requests pending 306 uint32_t num_buffers; 307 // List of pending buffers 308 List<PendingBufferInfo> mPendingBufferList; 309 // Last frame number requested 310 uint32_t last_frame_number; 311 } PendingBuffersMap; 312 313 typedef struct { 314 camera3_notify_msg_t notify_msg; 315 camera3_stream_buffer_t buffer; 316 uint32_t frame_number; 317 } PendingReprocessResult; 318 319 typedef KeyedVector<uint32_t, Vector<PendingBufferInfo> > FlushMap; 320 321 List<PendingReprocessResult> mPendingReprocessResultList; 322 List<PendingRequestInfo> mPendingRequestsList; 323 List<PendingFrameDropInfo> mPendingFrameDropList; 324 PendingBuffersMap mPendingBuffersMap; 325 pthread_cond_t mRequestCond; 326 int mPendingRequest; 327 bool mWokenUpByDaemon; 328 int32_t mCurrentRequestId; 329 camera3_capture_result_t *mLoopBackResult; 330 nsecs_t mLoopBackTimestamp; 331 332 //mutex for serialized access to camera3_device_ops_t functions 333 pthread_mutex_t mMutex; 334 335 List<stream_info_t*> mStreamInfo; 336 337 int64_t mMinProcessedFrameDuration; 338 int64_t mMinJpegFrameDuration; 339 int64_t mMinRawFrameDuration; 340 power_module_t *m_pPowerModule; // power module 341 342 uint32_t mMetaFrameCount; 343 const camera_module_callbacks_t *mCallbacks; 344 345 uint8_t mCaptureIntent; 346 cam_stream_size_info_t mStreamConfigInfo; 347 348 static const QCameraMap EFFECT_MODES_MAP[]; 349 static const QCameraMap WHITE_BALANCE_MODES_MAP[]; 350 static const QCameraMap SCENE_MODES_MAP[]; 351 static const QCameraMap FOCUS_MODES_MAP[]; 352 static const QCameraMap COLOR_ABERRATION_MAP[]; 353 static const QCameraMap ANTIBANDING_MODES_MAP[]; 354 static const QCameraMap AE_FLASH_MODE_MAP[]; 355 static const QCameraMap FLASH_MODES_MAP[]; 356 static const QCameraMap FACEDETECT_MODES_MAP[]; 357 static const QCameraMap FOCUS_CALIBRATION_MAP[]; 358 static const QCameraMap TEST_PATTERN_MAP[]; 359 static const QCameraMap REFERENCE_ILLUMINANT_MAP[]; 360 static const QCameraPropMap CDS_MAP[]; 361 }; 362 363 }; // namespace qcamera 364 365 #endif /* __QCAMERA2HARDWAREINTERFACE_H__ */ 366