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 __QCAMERA3_CHANNEL_H__ 31 #define __QCAMERA3_CHANNEL_H__ 32 33 #include <hardware/camera3.h> 34 #include "QCamera3Stream.h" 35 #include "QCamera3Mem.h" 36 #include "QCamera3PostProc.h" 37 #include "QCamera3HALHeader.h" 38 #include "utils/Vector.h" 39 #include <utils/List.h> 40 41 extern "C" { 42 #include <mm_camera_interface.h> 43 } 44 45 using namespace android; 46 47 #define MIN_STREAMING_BUFFER_NUM 7+11 48 49 namespace qcamera { 50 51 typedef void (*channel_cb_routine)(mm_camera_super_buf_t *metadata, 52 camera3_stream_buffer_t *buffer, 53 uint32_t frame_number, void *userdata); 54 class QCamera3Channel 55 { 56 public: 57 QCamera3Channel(uint32_t cam_handle, 58 mm_camera_ops_t *cam_ops, 59 channel_cb_routine cb_routine, 60 cam_padding_info_t *paddingInfo, 61 uint32_t postprocess_mask, 62 void *userData, uint32_t numBuffers); 63 QCamera3Channel(); 64 virtual ~QCamera3Channel(); 65 66 virtual int32_t start(); 67 virtual int32_t stop(); 68 virtual int32_t setBatchSize(uint32_t); 69 virtual int32_t queueBatchBuf(); 70 virtual int32_t setPerFrameMapUnmap(bool enable); 71 int32_t bufDone(mm_camera_super_buf_t *recvd_frame); 72 73 virtual uint32_t getStreamTypeMask(); 74 uint32_t getStreamID(uint32_t streamMask); 75 virtual int32_t initialize(cam_is_type_t isType) = 0; 76 virtual int32_t request(buffer_handle_t * /*buffer*/, 77 uint32_t /*frameNumber*/){ return 0;}; 78 virtual int32_t request(buffer_handle_t * /*buffer*/, 79 uint32_t /*frameNumber*/, 80 camera3_stream_buffer_t* /*pInputBuffer*/, 81 metadata_buffer_t* /*metadata*/){ return 0;}; 82 virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame, 83 QCamera3Stream *stream) = 0; 84 85 virtual int32_t registerBuffer(buffer_handle_t *buffer, cam_is_type_t isType) = 0; 86 virtual QCamera3Memory *getStreamBufs(uint32_t len) = 0; 87 virtual void putStreamBufs() = 0; 88 89 QCamera3Stream *getStreamByHandle(uint32_t streamHandle); 90 uint32_t getMyHandle() const {return m_handle;}; 91 uint32_t getNumOfStreams() const {return m_numStreams;}; 92 uint32_t getNumBuffers() const {return mNumBuffers;}; 93 QCamera3Stream *getStreamByIndex(uint32_t index); 94 95 static void streamCbRoutine(mm_camera_super_buf_t *super_frame, 96 QCamera3Stream *stream, void *userdata); 97 void dumpYUV(mm_camera_buf_def_t *frame, cam_dimension_t dim, 98 cam_frame_len_offset_t offset, uint8_t name); 99 100 void *mUserData; 101 cam_padding_info_t *mPaddingInfo; 102 QCamera3Stream *mStreams[MAX_STREAM_NUM_IN_BUNDLE]; 103 uint32_t m_numStreams; 104 protected: 105 106 virtual int32_t init(mm_camera_channel_attr_t *attr, 107 mm_camera_buf_notify_t dataCB); 108 int32_t addStream(cam_stream_type_t streamType, 109 cam_format_t streamFormat, 110 cam_dimension_t streamDim, 111 cam_rotation_t streamRotation, 112 uint8_t minStreamBufnum, 113 uint32_t postprocessMask, 114 cam_is_type_t isType, 115 uint32_t batchSize = 0); 116 int32_t allocateStreamInfoBuf(camera3_stream_t *stream); 117 118 uint32_t m_camHandle; 119 mm_camera_ops_t *m_camOps; 120 bool m_bIsActive; 121 122 uint32_t m_handle; 123 124 125 mm_camera_buf_notify_t mDataCB; 126 127 128 QCamera3HeapMemory *mStreamInfoBuf; 129 channel_cb_routine mChannelCB; 130 //cam_padding_info_t *mPaddingInfo; 131 uint32_t mPostProcMask; 132 uint8_t mYUVDump; 133 cam_is_type_t mIsType; 134 uint32_t mNumBuffers; 135 /* Enable unmapping of buffer before issuing buffer callback. Default value 136 * for this flag is true and is selectively set to false for the usecases 137 * such as HFR to avoid any performance hit due to mapping/unmapping */ 138 bool mPerFrameMapUnmapEnable; 139 }; 140 141 /* QCamera3ProcessingChannel is used to handle all streams that are directly 142 * generated by hardware and given to frameworks without any postprocessing at HAL. 143 * It also handles input streams that require reprocessing by hardware and then 144 * returned to frameworks. */ 145 class QCamera3ProcessingChannel : public QCamera3Channel 146 { 147 public: 148 QCamera3ProcessingChannel(uint32_t cam_handle, 149 mm_camera_ops_t *cam_ops, 150 channel_cb_routine cb_routine, 151 cam_padding_info_t *paddingInfo, 152 void *userData, 153 camera3_stream_t *stream, 154 cam_stream_type_t stream_type, 155 uint32_t postprocess_mask, 156 QCamera3Channel *metadataChannel, 157 uint32_t numBuffers = MAX_INFLIGHT_REQUESTS); 158 159 ~QCamera3ProcessingChannel(); 160 161 virtual int32_t initialize(cam_is_type_t isType); 162 virtual int32_t request(buffer_handle_t *buffer, 163 uint32_t frameNumber, 164 camera3_stream_buffer_t* pInputBuffer, 165 metadata_buffer_t* metadata); 166 virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame, 167 QCamera3Stream *stream); 168 virtual QCamera3Memory *getStreamBufs(uint32_t len); 169 virtual void putStreamBufs(); 170 virtual int32_t registerBuffer(buffer_handle_t *buffer, cam_is_type_t isType); 171 172 virtual reprocess_type_t getReprocessType() = 0; 173 174 void reprocessCbRoutine(buffer_handle_t *resultBuffer, 175 uint32_t resultFrameNumber); 176 177 int32_t queueReprocMetadata(mm_camera_super_buf_t *metadata); 178 int32_t metadataBufDone(mm_camera_super_buf_t *recvd_frame); 179 int32_t setReprocConfig(reprocess_config_t &reproc_cfg, 180 camera3_stream_buffer_t *pInputBuffer, 181 metadata_buffer_t *metadata, 182 cam_format_t streamFormat); 183 184 QCamera3PostProcessor m_postprocessor; // post processor 185 186 protected: 187 bool isWNREnabled() {return m_bWNROn;}; 188 void startPostProc(bool inputBufExists, 189 const reprocess_config_t &reproc_cfg); 190 191 QCamera3GrallocMemory mMemory; //output buffer allocated by fwk 192 camera3_stream_t *mCamera3Stream; 193 uint32_t mNumBufs; 194 cam_stream_type_t mStreamType; 195 cam_format_t mStreamFormat; 196 uint8_t mIntent; 197 198 bool mPostProcStarted; 199 bool mInputBufferConfig; // Set when the processing channel is configured 200 // for processing input(framework) buffers 201 202 QCamera3Channel *m_pMetaChannel; 203 mm_camera_super_buf_t *mMetaFrame; 204 QCamera3GrallocMemory mOfflineMemory; //reprocessing input buffer 205 QCamera3HeapMemory mOfflineMetaMemory; //reprocessing metadata buffer 206 207 private: 208 int32_t stop(); 209 210 bool m_bWNROn; 211 }; 212 213 /* QCamera3RegularChannel is used to handle all streams that are directly 214 * generated by hardware and given to frameworks without any postprocessing at HAL. 215 * Examples are: all IMPLEMENTATION_DEFINED streams, CPU_READ streams. */ 216 class QCamera3RegularChannel : public QCamera3ProcessingChannel 217 { 218 public: 219 QCamera3RegularChannel(uint32_t cam_handle, 220 mm_camera_ops_t *cam_ops, 221 channel_cb_routine cb_routine, 222 cam_padding_info_t *paddingInfo, 223 void *userData, 224 camera3_stream_t *stream, 225 cam_stream_type_t stream_type, 226 uint32_t postprocess_mask, 227 QCamera3Channel *metadataChannel, 228 uint32_t numBuffers = MAX_INFLIGHT_REQUESTS); 229 230 virtual ~QCamera3RegularChannel(); 231 232 virtual int32_t start(); 233 virtual int32_t setBatchSize(uint32_t batchSize); 234 virtual uint32_t getStreamTypeMask(); 235 virtual int32_t queueBatchBuf(); 236 virtual int32_t initialize(cam_is_type_t isType); 237 virtual int32_t request(buffer_handle_t *buffer, uint32_t frameNumber); 238 virtual reprocess_type_t getReprocessType(); 239 240 private: 241 int32_t initialize(struct private_handle_t *priv_handle); 242 243 cam_rotation_t mRotation; 244 uint32_t mBatchSize; 245 246 }; 247 248 /* QCamera3MetadataChannel is for metadata stream generated by camera daemon. */ 249 class QCamera3MetadataChannel : public QCamera3Channel 250 { 251 public: 252 QCamera3MetadataChannel(uint32_t cam_handle, 253 mm_camera_ops_t *cam_ops, 254 channel_cb_routine cb_routine, 255 cam_padding_info_t *paddingInfo, 256 uint32_t postprocess_mask, 257 void *userData, 258 uint32_t numBuffers = MIN_STREAMING_BUFFER_NUM); 259 virtual ~QCamera3MetadataChannel(); 260 261 virtual int32_t initialize(cam_is_type_t isType); 262 263 virtual int32_t request(buffer_handle_t *buffer, uint32_t frameNumber); 264 virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame, 265 QCamera3Stream *stream); 266 267 virtual QCamera3Memory *getStreamBufs(uint32_t le); 268 virtual void putStreamBufs(); 269 virtual int32_t registerBuffer(buffer_handle_t * /*buffer*/, cam_is_type_t /*isType*/) 270 { return NO_ERROR; }; 271 272 private: 273 QCamera3HeapMemory *mMemory; 274 }; 275 276 /* QCamera3RawChannel is for opaqueu/cross-platform raw stream containing 277 * vendor specific bayer data or 16-bit unpacked bayer data */ 278 class QCamera3RawChannel : public QCamera3RegularChannel 279 { 280 public: 281 QCamera3RawChannel(uint32_t cam_handle, 282 mm_camera_ops_t *cam_ops, 283 channel_cb_routine cb_routine, 284 cam_padding_info_t *paddingInfo, 285 void *userData, 286 camera3_stream_t *stream, 287 uint32_t postprocess_mask, 288 QCamera3Channel *metadataChannel, 289 bool raw_16 = false, 290 uint32_t numBuffers = MAX_INFLIGHT_REQUESTS); 291 292 virtual ~QCamera3RawChannel(); 293 294 virtual int32_t initialize(cam_is_type_t isType); 295 296 virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame, 297 QCamera3Stream *stream); 298 299 virtual reprocess_type_t getReprocessType(); 300 301 private: 302 bool mRawDump; 303 bool mIsRaw16; 304 305 void dumpRawSnapshot(mm_camera_buf_def_t *frame); 306 void convertLegacyToRaw16(mm_camera_buf_def_t *frame); 307 void convertMipiToRaw16(mm_camera_buf_def_t *frame); 308 }; 309 310 /* 311 * QCamera3RawDumpChannel is for internal use only for Raw dump 312 */ 313 314 class QCamera3RawDumpChannel : public QCamera3Channel 315 { 316 public: 317 QCamera3RawDumpChannel(uint32_t cam_handle, 318 mm_camera_ops_t *cam_ops, 319 cam_dimension_t rawDumpSize, 320 cam_padding_info_t *paddingInfo, 321 void *userData, 322 uint32_t postprocess_mask, uint32_t numBuffers = 3U); 323 virtual ~QCamera3RawDumpChannel(); 324 virtual int32_t initialize(cam_is_type_t isType); 325 virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame, 326 QCamera3Stream *stream); 327 virtual QCamera3Memory *getStreamBufs(uint32_t le); 328 virtual void putStreamBufs(); 329 virtual int32_t registerBuffer(buffer_handle_t * /*buffer*/, cam_is_type_t /*isType*/) 330 { return NO_ERROR; }; 331 virtual int32_t request(buffer_handle_t *buffer, uint32_t frameNumber); 332 void dumpRawSnapshot(mm_camera_buf_def_t *frame); 333 334 public: 335 cam_dimension_t mDim; 336 337 private: 338 bool mRawDump; 339 QCamera3HeapMemory *mMemory; 340 }; 341 342 /* QCamera3YUVChannel is used to handle flexible YUV streams that are directly 343 * generated by hardware and given to frameworks without any postprocessing at HAL. 344 * It is also used to handle input buffers that generate YUV outputs */ 345 class QCamera3YUVChannel : public QCamera3ProcessingChannel 346 { 347 public: 348 QCamera3YUVChannel(uint32_t cam_handle, 349 mm_camera_ops_t *cam_ops, 350 channel_cb_routine cb_routine, 351 cam_padding_info_t *paddingInfo, 352 void *userData, 353 camera3_stream_t *stream, 354 cam_stream_type_t stream_type, 355 uint32_t postprocess_mask, 356 QCamera3Channel *metadataChannel); 357 ~QCamera3YUVChannel(); 358 virtual int32_t initialize(cam_is_type_t isType); 359 virtual reprocess_type_t getReprocessType(); 360 virtual int32_t start(); 361 362 }; 363 364 /* QCamera3PicChannel is for JPEG stream, which contains a YUV stream generated 365 * by the hardware, and encoded to a JPEG stream */ 366 class QCamera3PicChannel : public QCamera3ProcessingChannel 367 { 368 public: 369 QCamera3PicChannel(uint32_t cam_handle, 370 mm_camera_ops_t *cam_ops, 371 channel_cb_routine cb_routine, 372 cam_padding_info_t *paddingInfo, 373 void *userData, 374 camera3_stream_t *stream, 375 uint32_t postprocess_mask, 376 bool is4KVideo, 377 QCamera3Channel *metadataChannel, 378 uint32_t numBuffers = MAX_INFLIGHT_REQUESTS); 379 ~QCamera3PicChannel(); 380 381 virtual int32_t initialize(cam_is_type_t isType); 382 virtual int32_t request(buffer_handle_t *buffer, 383 uint32_t frameNumber, 384 camera3_stream_buffer_t* pInputBuffer, 385 metadata_buffer_t* metadata); 386 virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame, 387 QCamera3Stream *stream); 388 389 virtual QCamera3Memory *getStreamBufs(uint32_t le); 390 virtual void putStreamBufs(); 391 virtual reprocess_type_t getReprocessType(); 392 393 QCamera3Exif *getExifData(metadata_buffer_t *metadata, 394 jpeg_settings_t *jpeg_settings); 395 void overrideYuvSize(uint32_t width, uint32_t height); 396 static void jpegEvtHandle(jpeg_job_status_t status, 397 uint32_t /*client_hdl*/, 398 uint32_t jobId, 399 mm_jpeg_output_t *p_output, 400 void *userdata); 401 static void dataNotifyCB(mm_camera_super_buf_t *recvd_frame, 402 void *userdata); 403 404 private: 405 int32_t queueJpegSetting(uint32_t out_buf_index, metadata_buffer_t *metadata); 406 407 public: 408 cam_dimension_t m_max_pic_dim; 409 410 private: 411 uint32_t mNumSnapshotBufs; 412 uint32_t mYuvWidth, mYuvHeight; 413 int32_t mCurrentBufIndex; 414 QCamera3HeapMemory *mYuvMemory; 415 // Keep a list of free buffers 416 Mutex mFreeBuffersLock; 417 List<uint32_t> mFreeBufferList; 418 }; 419 420 // reprocess channel class 421 class QCamera3ReprocessChannel : public QCamera3Channel 422 { 423 public: 424 QCamera3ReprocessChannel(uint32_t cam_handle, 425 mm_camera_ops_t *cam_ops, 426 channel_cb_routine cb_routine, 427 cam_padding_info_t *paddingInfo, 428 uint32_t postprocess_mask, 429 void *userData, void *ch_hdl); 430 QCamera3ReprocessChannel(); 431 virtual ~QCamera3ReprocessChannel(); 432 // offline reprocess 433 int32_t doReprocessOffline(qcamera_fwk_input_pp_data_t *frame); 434 int32_t doReprocess(int buf_fd, size_t buf_length, int32_t &ret_val, 435 mm_camera_super_buf_t *meta_buf); 436 int32_t extractFrameCropAndRotation(mm_camera_super_buf_t *frame, 437 mm_camera_buf_def_t *meta_buffer, 438 jpeg_settings_t *jpeg_settings, 439 qcamera_fwk_input_pp_data_t &fwk_frame); 440 int32_t extractCrop(qcamera_fwk_input_pp_data_t *frame); 441 virtual QCamera3Memory *getStreamBufs(uint32_t len); 442 virtual void putStreamBufs(); 443 virtual int32_t initialize(cam_is_type_t isType); 444 int32_t unmapOfflineBuffers(bool all); 445 virtual int32_t stop(); 446 virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame, 447 QCamera3Stream *stream); 448 static void dataNotifyCB(mm_camera_super_buf_t *recvd_frame, 449 void* userdata); 450 int32_t addReprocStreamsFromSource(cam_pp_feature_config_t &pp_config, 451 const reprocess_config_t &src_config, 452 cam_is_type_t is_type, 453 QCamera3Channel *pMetaChannel); 454 QCamera3Stream *getStreamBySrcHandle(uint32_t srcHandle); 455 QCamera3Stream *getSrcStreamBySrcHandle(uint32_t srcHandle); 456 virtual int32_t registerBuffer(buffer_handle_t * buffer, cam_is_type_t isType); 457 458 public: 459 void *inputChHandle; 460 461 private: 462 typedef struct { 463 QCamera3Stream *stream; 464 cam_mapping_buf_type type; 465 uint32_t index; 466 } OfflineBuffer; 467 468 android::List<OfflineBuffer> mOfflineBuffers; 469 android::List<OfflineBuffer> mOfflineMetaBuffers; 470 int32_t mOfflineBuffersIndex; 471 int32_t mOfflineMetaIndex; 472 reprocess_type_t mReprocessType; 473 uint32_t mSrcStreamHandles[MAX_STREAM_NUM_IN_BUNDLE]; 474 QCamera3ProcessingChannel *m_pSrcChannel; // ptr to source channel for reprocess 475 QCamera3Channel *m_pMetaChannel; 476 QCamera3HeapMemory *mMemory; 477 QCamera3GrallocMemory mGrallocMemory; 478 }; 479 480 481 /* QCamera3SupportChannel is for HAL internal consumption only */ 482 class QCamera3SupportChannel : public QCamera3Channel 483 { 484 public: 485 QCamera3SupportChannel(uint32_t cam_handle, 486 mm_camera_ops_t *cam_ops, 487 cam_padding_info_t *paddingInfo, 488 uint32_t postprocess_mask, 489 cam_stream_type_t streamType, 490 cam_dimension_t *dim, 491 void *userData, 492 uint32_t numBuffers = MIN_STREAMING_BUFFER_NUM); 493 virtual ~QCamera3SupportChannel(); 494 495 virtual int32_t initialize(cam_is_type_t isType); 496 497 virtual int32_t request(buffer_handle_t *buffer, uint32_t frameNumber); 498 virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame, 499 QCamera3Stream *stream); 500 501 virtual QCamera3Memory *getStreamBufs(uint32_t le); 502 virtual void putStreamBufs(); 503 virtual int32_t registerBuffer(buffer_handle_t * /*buffer*/, cam_is_type_t /*isType*/) 504 { return NO_ERROR; }; 505 506 static cam_dimension_t kDim; 507 private: 508 QCamera3HeapMemory *mMemory; 509 cam_dimension_t mDim; 510 cam_stream_type_t mStreamType; 511 }; 512 513 }; // namespace qcamera 514 515 #endif /* __QCAMERA_CHANNEL_H__ */ 516