3 ** Copyright 2008, The Android Open Source Project
4 ** Copyright 2012, Samsung Electronics Co. LTD
6 ** Licensed under the Apache License, Version 2.0 (the "License");
7 ** you may not use this file except in compliance with the License.
8 ** You may obtain a copy of the License at
10 ** http://www.apache.org/licenses/LICENSE-2.0
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
20 * \file ExynosCameraHWInterface2.h
21 * \brief header file for Android Camera API 2.0 HAL
22 * \author Sungjoong Kang(sj3.kang@samsung.com)
25 * <b>Revision History: </b>
26 * - 2012/05/31 : Sungjoong Kang(sj3.kang@samsung.com) \n
29 * - 2012/07/10 : Sungjoong Kang(sj3.kang@samsung.com) \n
34 #ifndef EXYNOS_CAMERA_HW_INTERFACE_2_H
35 #define EXYNOS_CAMERA_HW_INTERFACE_2_H
37 #include <hardware/camera2.h>
38 #include <camera/Camera.h>
39 #include <camera/CameraParameters.h>
40 #include <utils/List.h>
41 #include "SignalDrivenThread.h"
42 #include "MetadataConverter.h"
43 #include "exynos_v4l2.h"
44 #include "ExynosRect.h"
45 #include "ExynosBuffer.h"
46 #include "videodev2_exynos_camera.h"
47 #include "gralloc_priv.h"
48 #include "ExynosJpegEncoderForCamera.h"
50 #include "fimc-is-metadata.h"
52 #include "ExynosExif.h"
54 #include "ExynosCamera2.h"
55 #include "cutils/properties.h"
59 //#define EXYNOS_CAMERA_LOG
61 //#define ENABLE_FRAME_SYNC
62 #define NODE_PREFIX "/dev/video"
64 #define NUM_MAX_STREAM_THREAD (5)
65 #define NUM_MAX_REQUEST_MGR_ENTRY (4)
66 #define NUM_MAX_CAMERA_BUFFERS (16)
67 #define NUM_BAYER_BUFFERS (8)
68 #define NUM_MIN_SENSOR_QBUF (3)
70 #define PICTURE_GSC_NODE_NUM (2)
71 #define VIDEO_GSC_NODE_NUM (1)
73 #define STREAM_TYPE_DIRECT (0)
74 #define STREAM_TYPE_INDIRECT (1)
76 #define SIGNAL_MAIN_REQ_Q_NOT_EMPTY (SIGNAL_THREAD_COMMON_LAST<<1)
77 #define SIGNAL_MAIN_REPROCESS_Q_NOT_EMPTY (SIGNAL_THREAD_COMMON_LAST<<2)
78 #define SIGNAL_MAIN_STREAM_OUTPUT_DONE (SIGNAL_THREAD_COMMON_LAST<<3)
79 #define SIGNAL_SENSOR_START_REQ_PROCESSING (SIGNAL_THREAD_COMMON_LAST<<4)
80 #define SIGNAL_STREAM_GET_BUFFER (SIGNAL_THREAD_COMMON_LAST<<5)
81 #define SIGNAL_STREAM_PUT_BUFFER (SIGNAL_THREAD_COMMON_LAST<<6)
82 #define SIGNAL_STREAM_CHANGE_PARAMETER (SIGNAL_THREAD_COMMON_LAST<<7)
83 #define SIGNAL_THREAD_RELEASE (SIGNAL_THREAD_COMMON_LAST<<8)
84 #define SIGNAL_ISP_START_BAYER_INPUT (SIGNAL_THREAD_COMMON_LAST<<9)
85 #define SIGNAL_ISP_START_BAYER_DEQUEUE (SIGNAL_THREAD_COMMON_LAST<<10)
87 #define SIGNAL_STREAM_DATA_COMING (SIGNAL_THREAD_COMMON_LAST<<15)
89 #define NO_TRANSITION (0)
90 #define HAL_AFSTATE_INACTIVE (1)
91 #define HAL_AFSTATE_NEEDS_COMMAND (2)
92 #define HAL_AFSTATE_STARTED (3)
93 #define HAL_AFSTATE_SCANNING (4)
94 #define HAL_AFSTATE_LOCKED (5)
95 #define HAL_AFSTATE_FAILED (6)
96 #define HAL_AFSTATE_NEEDS_DETERMINATION (7)
97 #define HAL_AFSTATE_PASSIVE_FOCUSED (8)
99 #ifdef EXYNOS_CAMERA_LOG
100 #define CAM_LOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
101 #define CAM_LOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
102 #define CAM_LOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
103 #define CAM_LOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
105 #define CAM_LOGV(...) ((void)0)
106 #define CAM_LOGD(...) ((void)0)
107 #define CAM_LOGW(...) ((void)0)
108 #define CAM_LOGE(...) ((void)0)
112 SENSOR_NAME_S5K3H2
= 1,
113 SENSOR_NAME_S5K6A3
= 2,
114 SENSOR_NAME_S5K4E5
= 3,
115 SENSOR_NAME_S5K3H7
= 4,
116 SENSOR_NAME_CUSTOM
= 5,
120 enum is_subscenario_id
{
121 ISS_SUB_SCENARIO_STILL
,
122 ISS_SUB_SCENARIO_VIDEO
,
123 ISS_SUB_SCENARIO_SCENE1
,
124 ISS_SUB_SCENARIO_SCENE2
,
125 ISS_SUB_SCENARIO_SCENE3
,
129 int SUPPORT_THUMBNAIL_REAR_SIZE
[][2] =
136 int SUPPORT_THUMBNAIL_FRONT_SIZE
[][2] =
144 enum is_flash_scenario_state
{
146 IS_FLASH_ON_DONE
= 2,
147 IS_FLASH_AE_AWB_LOCK_WAIT
= 10,
148 IS_FLASH_AUTO_WAIT
= 10,
150 IS_FLASH_CAPTURE_WAIT
,
151 IS_FLASH_CAPTURE_JPEG
,
152 IS_FLASH_CAPTURE_END
,
156 enum is_af_flash_scenario_state
{
158 IS_FLASH_AF_ON_START
,
160 IS_FLASH_AF_AUTO_AE_AWB_LOCK
,
161 IS_FLASH_AF_AUTO_AE_AWB_LOCK_WAIT
,
162 IS_FLASH_AF_AUTO_OFF_WAIT
,
163 IS_FLASH_AF_AUTO_END
,
168 typedef struct node_info
{
175 enum v4l2_memory memory
;
176 enum v4l2_buf_type type
;
177 ion_client ionClient
;
178 ExynosBuffer buffer
[NUM_MAX_CAMERA_BUFFERS
];
183 typedef struct camera_hw_info
{
191 struct camera2_shot_ext dummy_shot
;
195 typedef enum request_entry_status
{
200 } request_entry_status_t
;
202 typedef struct request_manager_entry
{
203 request_entry_status_t status
;
204 camera_metadata_t
*original_request
;
205 struct camera2_shot_ext internal_shot
;
206 int output_stream_count
;
207 bool dynamic_meta_vaild
;
208 } request_manager_entry_t
;
210 // structure related to a specific function of camera
211 typedef struct flash_control_info
{
212 // UI flash mode indicator
213 enum aa_aemode i_flashMode
;
215 bool m_afFlashDoneFlg
;
216 bool m_afFlashEnableFlg
;
219 bool m_flashEnableFlg
;
220 bool m_flashCaptured
;
221 int m_flashFrameCount
;
226 // At flash auto mode only : 1 -> flash is needed, 0 -> normal case
227 bool m_flashDecisionResult
;
228 // torch indicator. this will be replaced by flashMode meta
229 bool m_flashTorchMode
;
232 typedef struct request_control_info
{
233 ctl_flash_info_t flash
;
235 } ctl_request_info_t
;
237 class RequestManager
{
239 RequestManager(SignalDrivenThread
* main_thread
);
242 bool IsRequestQueueFull();
244 void RegisterRequest(camera_metadata_t
*new_request
);
245 void DeregisterRequest(camera_metadata_t
**deregistered_request
);
246 bool PrepareFrame(size_t *num_entries
, size_t *frame_size
,
247 camera_metadata_t
**prepared_frame
, int afState
);
248 int MarkProcessingRequest(ExynosBuffer
* buf
, int *afMode
);
249 void NotifyStreamOutput(int frameCnt
, int stream_id
);
250 void DumpInfoWithIndex(int index
);
251 void ApplyDynamicMetadata(struct camera2_shot_ext
*shot_ext
);
252 void CheckCompleted(int index
);
253 void UpdateIspParameters(struct camera2_shot_ext
*shot_ext
, int frameCnt
);
254 void RegisterTimestamp(int frameCnt
, nsecs_t
*frameTime
);
255 uint64_t GetTimestamp(int frameCnt
);
256 int FindFrameCnt(struct camera2_shot_ext
* shot_ext
);
257 int FindEntryIndexByFrameCnt(int frameCnt
);
259 int GetNextIndex(int index
);
260 void SetDefaultParameters(int cropX
);
261 void SetInitialSkip(int count
);
263 void SetFrameIndex(int index
);
265 void pushSensorQ(int index
);
267 void releaseSensorQ();
270 MetadataConverter
*m_metadataConverter
;
271 SignalDrivenThread
*m_mainThread
;
273 int m_entryInsertionIndex
;
274 int m_entryProcessingIndex
;
275 int m_entryFrameOutputIndex
;
276 request_manager_entry_t entries
[NUM_MAX_REQUEST_MGR_ENTRY
];
277 int m_completedIndex
;
279 Mutex m_requestMutex
;
281 //TODO : alloc dynamically
282 char m_tempFrameMetadataBuf
[2000];
283 camera_metadata_t
*m_tempFrameMetadata
;
285 int m_sensorPipelineSkipCnt
;
296 typedef struct bayer_buf_entry
{
303 class BayerBufManager
{
307 int GetIndexForSensorEnqueue();
308 int MarkSensorEnqueue(int index
);
309 int MarkSensorDequeue(int index
, int reqFrameCnt
, nsecs_t
*timeStamp
);
310 int GetIndexForIspEnqueue(int *reqFrameCnt
);
311 int GetIndexForIspDequeue(int *reqFrameCnt
);
312 int MarkIspEnqueue(int index
);
313 int MarkIspDequeue(int index
);
314 int GetNumOnSensor();
315 int GetNumOnHalFilled();
319 int GetNextIndex(int index
);
321 int sensorEnqueueHead
;
322 int sensorDequeueHead
;
330 bayer_buf_entry_t entries
[NUM_BAYER_BUFFERS
];
334 #define NOT_AVAILABLE (0)
335 #define REQUIRES_DQ_FROM_SVC (1)
336 #define ON_DRIVER (2)
338 #define ON_SERVICE (4)
340 #define BAYER_NOT_AVAILABLE (0)
341 #define BAYER_ON_SENSOR (1)
342 #define BAYER_ON_HAL_FILLED (2)
343 #define BAYER_ON_ISP (3)
344 #define BAYER_ON_SERVICE (4)
345 #define BAYER_ON_HAL_EMPTY (5)
347 typedef struct stream_parameters
{
349 uint32_t outputWidth
;
350 uint32_t outputHeight
;
355 const camera2_stream_ops_t
* streamOps
;
359 int numOwnSvcBuffers
;
364 enum v4l2_memory memory
;
365 enum v4l2_buf_type halBuftype
;
367 buffer_handle_t svcBufHandle
[NUM_MAX_CAMERA_BUFFERS
];
368 ExynosBuffer svcBuffers
[NUM_MAX_CAMERA_BUFFERS
];
369 ExynosBuffer metaBuffers
[NUM_MAX_CAMERA_BUFFERS
];
370 int svcBufStatus
[NUM_MAX_CAMERA_BUFFERS
];
372 ion_client ionClient
;
374 } stream_parameters_t
;
376 typedef struct record_parameters
{
377 uint32_t outputWidth
;
378 uint32_t outputHeight
;
380 const camera2_stream_ops_t
* streamOps
;
383 int numOwnSvcBuffers
;
385 buffer_handle_t svcBufHandle
[NUM_MAX_CAMERA_BUFFERS
];
386 ExynosBuffer svcBuffers
[NUM_MAX_CAMERA_BUFFERS
];
387 int svcBufStatus
[NUM_MAX_CAMERA_BUFFERS
];
390 } record_parameters_t
;
392 typedef struct callback_parameters
{
393 uint32_t outputWidth
;
394 uint32_t outputHeight
;
398 const camera2_stream_ops_t
* streamOps
;
401 int numOwnSvcBuffers
;
403 buffer_handle_t svcBufHandle
[NUM_MAX_CAMERA_BUFFERS
];
404 ExynosBuffer svcBuffers
[NUM_MAX_CAMERA_BUFFERS
];
405 int svcBufStatus
[NUM_MAX_CAMERA_BUFFERS
];
408 } callback_parameters_t
;
410 class ExynosCameraHWInterface2
: public virtual RefBase
{
412 ExynosCameraHWInterface2(int cameraId
, camera2_device_t
*dev
, ExynosCamera2
* camera
, int *openInvalid
);
413 virtual ~ExynosCameraHWInterface2();
415 virtual void release();
417 inline int getCameraId() const;
419 virtual int setRequestQueueSrcOps(const camera2_request_queue_src_ops_t
*request_src_ops
);
420 virtual int notifyRequestQueueNotEmpty();
421 virtual int setFrameQueueDstOps(const camera2_frame_queue_dst_ops_t
*frame_dst_ops
);
422 virtual int getInProgressCount();
423 virtual int flushCapturesInProgress();
424 virtual int constructDefaultRequest(int request_template
, camera_metadata_t
**request
);
425 virtual int allocateStream(uint32_t width
, uint32_t height
,
426 int format
, const camera2_stream_ops_t
*stream_ops
,
427 uint32_t *stream_id
, uint32_t *format_actual
, uint32_t *usage
, uint32_t *max_buffers
);
428 virtual int registerStreamBuffers(uint32_t stream_id
, int num_buffers
, buffer_handle_t
*buffers
);
429 virtual int releaseStream(uint32_t stream_id
);
430 virtual int allocateReprocessStream(uint32_t width
, uint32_t height
,
431 uint32_t format
, const camera2_stream_in_ops_t
*reprocess_stream_ops
,
432 uint32_t *stream_id
, uint32_t *consumer_usage
, uint32_t *max_buffers
);
433 virtual int releaseReprocessStream(uint32_t stream_id
);
434 virtual int triggerAction(uint32_t trigger_id
, int ext1
, int ext2
);
435 virtual int setNotifyCallback(camera2_notify_callback notify_cb
, void *user
);
436 virtual int getMetadataVendorTagOps(vendor_tag_query_ops_t
**ops
);
437 virtual int dump(int fd
);
439 class MainThread
: public SignalDrivenThread
{
440 ExynosCameraHWInterface2
*mHardware
;
442 MainThread(ExynosCameraHWInterface2
*hw
):
443 SignalDrivenThread(),
445 // Start("MainThread", PRIORITY_DEFAULT, 0);
448 status_t
readyToRunInternal()
452 void threadFunctionInternal()
454 mHardware
->m_mainThreadFunc(this);
461 class SensorThread
: public SignalDrivenThread
{
462 ExynosCameraHWInterface2
*mHardware
;
464 SensorThread(ExynosCameraHWInterface2
*hw
):
465 SignalDrivenThread("SensorThread", PRIORITY_DEFAULT
, 0),
467 m_isBayerOutputEnabled(false) { }
469 status_t
readyToRunInternal() {
470 mHardware
->m_sensorThreadInitialize(this);
473 void threadFunctionInternal() {
474 mHardware
->m_sensorThreadFunc(this);
479 bool m_isBayerOutputEnabled
;
484 class IspThread
: public SignalDrivenThread
{
485 ExynosCameraHWInterface2
*mHardware
;
487 IspThread(ExynosCameraHWInterface2
*hw
):
488 SignalDrivenThread("IspThread", PRIORITY_DEFAULT
, 0),
491 status_t
readyToRunInternal() {
492 mHardware
->m_ispThreadInitialize(this);
495 void threadFunctionInternal() {
496 mHardware
->m_ispThreadFunc(this);
505 class StreamThread
: public SignalDrivenThread
{
506 ExynosCameraHWInterface2
*mHardware
;
508 StreamThread(ExynosCameraHWInterface2
*hw
, uint8_t new_index
):
509 SignalDrivenThread("StreamThread", PRIORITY_DEFAULT
, 0),
511 m_index(new_index
) { }
513 status_t
readyToRunInternal() {
514 mHardware
->m_streamThreadInitialize(this);
517 void threadFunctionInternal() {
518 mHardware
->m_streamThreadFunc(this);
521 void setRecordingParameter(record_parameters_t
* recordParm
);
522 void setCallbackParameter(callback_parameters_t
* callbackParm
);
523 void setParameter(stream_parameters_t
* new_parameters
);
524 void applyChange(void);
526 int findBufferIndex(void * bufAddr
);
532 stream_parameters_t m_parameters
;
533 stream_parameters_t
*m_tempParameters
;
534 record_parameters_t m_recordParameters
;
535 callback_parameters_t m_previewCbParameters
;
540 sp
<MainThread
> m_mainThread
;
541 sp
<SensorThread
> m_sensorThread
;
542 sp
<IspThread
> m_ispThread
;
543 sp
<StreamThread
> m_streamThreads
[NUM_MAX_STREAM_THREAD
];
547 RequestManager
*m_requestManager
;
548 BayerBufManager
*m_BayerManager
;
549 ExynosCamera2
*m_camera2
;
551 void m_mainThreadFunc(SignalDrivenThread
* self
);
552 void m_sensorThreadFunc(SignalDrivenThread
* self
);
553 void m_sensorThreadInitialize(SignalDrivenThread
* self
);
554 void m_ispThreadFunc(SignalDrivenThread
* self
);
555 void m_ispThreadInitialize(SignalDrivenThread
* self
);
556 void m_streamThreadFunc(SignalDrivenThread
* self
);
557 void m_streamThreadInitialize(SignalDrivenThread
* self
);
559 void m_streamFunc0(SignalDrivenThread
*self
);
560 void m_streamFunc1(SignalDrivenThread
*self
);
562 void m_streamBufferInit(SignalDrivenThread
*self
);
564 void m_getAlignedYUVSize(int colorFormat
, int w
, int h
,
566 bool m_getRatioSize(int src_w
, int src_h
,
567 int dst_w
, int dst_h
,
568 int *crop_x
, int *crop_y
,
569 int *crop_w
, int *crop_h
,
571 int createIonClient(ion_client ionClient
);
572 int deleteIonClient(ion_client ionClient
);
574 int allocCameraMemory(ion_client ionClient
, ExynosBuffer
*buf
, int iMemoryNum
);
575 int allocCameraMemory(ion_client ionClient
, ExynosBuffer
*buf
, int iMemoryNum
, int cacheFlag
);
576 void freeCameraMemory(ExynosBuffer
*buf
, int iMemoryNum
);
577 void initCameraMemory(ExynosBuffer
*buf
, int iMemoryNum
);
579 void DumpInfoWithShot(struct camera2_shot_ext
* shot_ext
);
580 bool m_checkThumbnailSize(int w
, int h
);
581 bool yuv2Jpeg(ExynosBuffer
*yuvBuf
,
582 ExynosBuffer
*jpegBuf
,
584 int InitializeISPChain();
587 void SetAfMode(enum aa_afmode afMode
);
588 void OnAfTriggerStart(int id
);
589 void OnAfTrigger(int id
);
590 void OnAfTriggerAutoMacro(int id
);
591 void OnAfTriggerCAFPicture(int id
);
592 void OnAfTriggerCAFVideo(int id
);
593 void OnAfCancel(int id
);
594 void OnAfCancelAutoMacro(int id
);
595 void OnAfCancelCAFPicture(int id
);
596 void OnAfCancelCAFVideo(int id
);
597 void OnAfNotification(enum aa_afstate noti
);
598 void OnAfNotificationAutoMacro(enum aa_afstate noti
);
599 void OnAfNotificationCAFPicture(enum aa_afstate noti
);
600 void OnAfNotificationCAFVideo(enum aa_afstate noti
);
601 void SetAfStateForService(int newState
);
602 int GetAfStateForService();
603 exif_attribute_t mExifInfo
;
604 void m_setExifFixedAttribute(void);
605 void m_setExifChangedAttribute(exif_attribute_t
*exifInfo
, ExynosRect
*rect
,
606 camera2_shot
*currentEntry
);
607 void flashSetter(struct camera2_shot_ext
* shot_ext
);
608 void flashListenerSensor(struct camera2_shot_ext
* shot_ext
);
609 void flashListenerISP(struct camera2_shot_ext
* shot_ext
);
610 void *m_exynosPictureCSC
;
611 void *m_exynosVideoCSC
;
613 int m_jpegEncodingFrameCnt
;
615 camera2_request_queue_src_ops_t
*m_requestQueueOps
;
616 camera2_frame_queue_dst_ops_t
*m_frameQueueOps
;
617 camera2_notify_callback m_notifyCb
;
618 void *m_callbackCookie
;
620 int m_numOfRemainingReqInSvc
;
621 bool m_isRequestQueuePending
;
622 bool m_isRequestQueueNull
;
623 camera2_device_t
*m_halDevice
;
624 static gralloc_module_t
const* m_grallocHal
;
627 camera_hw_info_t m_camera_info
;
629 ion_client m_ionCameraClient
;
631 bool m_isSensorThreadOn
;
632 bool m_isSensorStarted
;
635 int m_need_streamoff
;
640 int indexToQueue
[3+1];
645 ExynosBuffer m_resizeBuf
;
646 bool m_recordingEnabled
;
649 bool m_needsRecordBufferInit
;
650 ExynosBuffer m_previewCbBuf
;
651 int m_previewCbEnabled
;
652 int m_previewCbOutput
;
653 bool m_needsPreviewCbBufferInit
;
659 bool m_aspectChanged
;
660 uint32_t lastAfRegion
[4];
663 mutable Mutex m_qbufLock
;
667 enum aa_afmode m_afMode
;
668 enum aa_afmode m_afMode2
;
669 bool m_IsAfModeUpdateRequired
;
670 bool m_IsAfTriggerRequired
;
671 bool m_IsAfLockRequired
;
672 int m_serviceAfState
;
673 bool m_AfHwStateFailed
;
674 int m_afPendingTriggerId
;
675 int m_afModeWaitingCnt
;
676 struct camera2_shot m_jpegMetadata
;
677 int m_scpOutputSignalCnt
;
678 int m_scpOutputImageCnt
;
679 int m_nightCaptureCnt
;
680 int m_nightCaptureFrameCnt
;
683 ctl_request_info_t m_ctlInfo
;
686 }; // namespace android