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