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 "SignalDrivenThread.h"
41 #include "MetadataConverter.h"
42 #include "exynos_v4l2.h"
43 #include "ExynosRect.h"
44 #include "ExynosBuffer.h"
45 #include "videodev2_exynos_camera.h"
46 #include "gralloc_priv.h"
47 #include "ExynosJpegEncoderForCamera.h"
49 #include "fimc-is-metadata.h"
51 #include "ExynosExif.h"
53 #include "ExynosCamera2.h"
54 #include "cutils/properties.h"
58 //#define EXYNOS_CAMERA_LOG
60 //#define ENABLE_FRAME_SYNC
61 #define NODE_PREFIX "/dev/video"
63 #define NUM_MAX_STREAM_THREAD (5)
64 #define NUM_MAX_REQUEST_MGR_ENTRY (10)
65 #define NUM_MAX_DEQUEUED_REQUEST NUM_MAX_REQUEST_MGR_ENTRY
66 #define MAX_CAMERA_MEMORY_PLANE_NUM (4)
67 #define NUM_MAX_CAMERA_BUFFERS (16)
68 #define NUM_BAYER_BUFFERS (8)
69 #define NUM_SENSOR_QBUF (3)
71 #define PICTURE_GSC_NODE_NUM (2)
72 #define VIDEO_GSC_NODE_NUM (1)
74 #define STREAM_TYPE_DIRECT (0)
75 #define STREAM_TYPE_INDIRECT (1)
77 #define SIGNAL_MAIN_REQ_Q_NOT_EMPTY (SIGNAL_THREAD_COMMON_LAST<<1)
78 #define SIGNAL_MAIN_REPROCESS_Q_NOT_EMPTY (SIGNAL_THREAD_COMMON_LAST<<2)
79 #define SIGNAL_MAIN_STREAM_OUTPUT_DONE (SIGNAL_THREAD_COMMON_LAST<<3)
80 #define SIGNAL_SENSOR_START_REQ_PROCESSING (SIGNAL_THREAD_COMMON_LAST<<4)
81 #define SIGNAL_STREAM_GET_BUFFER (SIGNAL_THREAD_COMMON_LAST<<5)
82 #define SIGNAL_STREAM_PUT_BUFFER (SIGNAL_THREAD_COMMON_LAST<<6)
83 #define SIGNAL_STREAM_CHANGE_PARAMETER (SIGNAL_THREAD_COMMON_LAST<<7)
84 #define SIGNAL_THREAD_RELEASE (SIGNAL_THREAD_COMMON_LAST<<8)
85 #define SIGNAL_ISP_START_BAYER_INPUT (SIGNAL_THREAD_COMMON_LAST<<9)
86 #define SIGNAL_ISP_START_BAYER_DEQUEUE (SIGNAL_THREAD_COMMON_LAST<<10)
88 #define SIGNAL_STREAM_DATA_COMING (SIGNAL_THREAD_COMMON_LAST<<15)
90 #define NO_TRANSITION (0)
91 #define HAL_AFSTATE_INACTIVE (1)
92 #define HAL_AFSTATE_NEEDS_COMMAND (2)
93 #define HAL_AFSTATE_STARTED (3)
94 #define HAL_AFSTATE_SCANNING (4)
95 #define HAL_AFSTATE_LOCKED (5)
96 #define HAL_AFSTATE_FAILED (6)
97 #define HAL_AFSTATE_NEEDS_DETERMINATION (7)
98 #define HAL_AFSTATE_PASSIVE_FOCUSED (8)
100 #ifdef EXYNOS_CAMERA_LOG
101 #define CAM_LOGV(...) ((void)ALOG(LOG_VERBOSE, LOG_TAG, __VA_ARGS__))
102 #define CAM_LOGD(...) ((void)ALOG(LOG_DEBUG, LOG_TAG, __VA_ARGS__))
103 #define CAM_LOGW(...) ((void)ALOG(LOG_WARN, LOG_TAG, __VA_ARGS__))
104 #define CAM_LOGE(...) ((void)ALOG(LOG_ERROR, LOG_TAG, __VA_ARGS__))
106 #define CAM_LOGV(...) ((void)0)
107 #define CAM_LOGD(...) ((void)0)
108 #define CAM_LOGW(...) ((void)0)
109 #define CAM_LOGE(...) ((void)0)
113 SENSOR_NAME_S5K3H2
= 1,
114 SENSOR_NAME_S5K6A3
= 2,
115 SENSOR_NAME_S5K4E5
= 3,
116 SENSOR_NAME_S5K3H7
= 4,
117 SENSOR_NAME_CUSTOM
= 5,
121 enum is_subscenario_id
{
122 ISS_SUB_SCENARIO_STILL
,
123 ISS_SUB_SCENARIO_VIDEO
,
124 ISS_SUB_SCENARIO_SCENE1
,
125 ISS_SUB_SCENARIO_SCENE2
,
126 ISS_SUB_SCENARIO_SCENE3
,
130 int SUPPORT_THUMBNAIL_REAR_SIZE
[][2] =
137 int SUPPORT_THUMBNAIL_FRONT_SIZE
[][2] =
145 enum is_flash_scenario_state
{
147 IS_FLASH_ON_DONE
= 2,
148 IS_FLASH_AE_AWB_LOCK_WAIT
= 10,
149 IS_FLASH_AUTO_WAIT
= 10,
151 IS_FLASH_CAPTURE_WAIT
,
152 IS_FLASH_CAPTURE_JPEG
,
153 IS_FLASH_CAPTURE_END
,
157 enum is_af_flash_scenario_state
{
159 IS_FLASH_AF_ON_START
,
161 IS_FLASH_AF_AUTO_AE_AWB_LOCK
,
162 IS_FLASH_AF_AUTO_AE_AWB_LOCK_WAIT
,
163 IS_FLASH_AF_AUTO_OFF_WAIT
,
164 IS_FLASH_AF_AUTO_END
,
169 typedef struct node_info
{
176 enum v4l2_memory memory
;
177 enum v4l2_buf_type type
;
178 ion_client ionClient
;
179 ExynosBuffer buffer
[NUM_MAX_CAMERA_BUFFERS
];
184 typedef struct camera_hw_info
{
192 struct camera2_shot_ext dummy_shot
;
196 typedef enum request_entry_status
{
201 } request_entry_status_t
;
203 typedef struct request_manager_entry
{
204 request_entry_status_t status
;
205 camera_metadata_t
*original_request
;
206 struct camera2_shot_ext internal_shot
;
207 int output_stream_count
;
208 bool dynamic_meta_vaild
;
209 } request_manager_entry_t
;
211 // structure related to a specific function of camera
212 typedef struct flash_control_info
{
213 // UI flash mode indicator
214 enum aa_aemode i_flashMode
;
216 bool m_afFlashDoneFlg
;
217 bool m_afFlashEnableFlg
;
220 bool m_flashEnableFlg
;
221 bool m_flashCaptured
;
222 int m_flashFrameCount
;
227 // At flash auto mode only : 1 -> flash is needed, 0 -> normal case
228 bool m_flashDecisionResult
;
229 // torch indicator. this will be replaced by flashMode meta
230 bool m_flashTorchMode
;
233 typedef struct request_control_info
{
234 ctl_flash_info_t flash
;
236 } ctl_request_info_t
;
238 class RequestManager
{
240 RequestManager(SignalDrivenThread
* main_thread
);
243 bool IsRequestQueueFull();
245 void RegisterRequest(camera_metadata_t
*new_request
);
246 void DeregisterRequest(camera_metadata_t
**deregistered_request
);
247 bool PrepareFrame(size_t *num_entries
, size_t *frame_size
,
248 camera_metadata_t
**prepared_frame
, int afState
);
249 int MarkProcessingRequest(ExynosBuffer
* buf
, int *afMode
);
250 void NotifyStreamOutput(int frameCnt
, int stream_id
);
251 void DumpInfoWithIndex(int index
);
252 void ApplyDynamicMetadata(struct camera2_shot_ext
*shot_ext
);
253 void CheckCompleted(int index
);
254 void UpdateIspParameters(struct camera2_shot_ext
*shot_ext
, int frameCnt
);
255 void RegisterTimestamp(int frameCnt
, nsecs_t
*frameTime
);
256 uint64_t GetTimestamp(int frameCnt
);
257 int FindFrameCnt(struct camera2_shot_ext
* shot_ext
);
258 int FindEntryIndexByFrameCnt(int frameCnt
);
260 int GetNextIndex(int index
);
261 void SetDefaultParameters(int cropX
);
262 void SetInitialSkip(int count
);
264 void SetFrameIndex(int index
);
268 MetadataConverter
*m_metadataConverter
;
269 SignalDrivenThread
*m_mainThread
;
271 int m_entryInsertionIndex
;
272 int m_entryProcessingIndex
;
273 int m_entryFrameOutputIndex
;
274 request_manager_entry_t entries
[NUM_MAX_REQUEST_MGR_ENTRY
];
275 int m_completedIndex
;
277 Mutex m_requestMutex
;
279 //TODO : alloc dynamically
280 char m_tempFrameMetadataBuf
[2000];
281 camera_metadata_t
*m_tempFrameMetadata
;
283 int m_sensorPipelineSkipCnt
;
293 typedef struct bayer_buf_entry
{
300 class BayerBufManager
{
304 int GetIndexForSensorEnqueue();
305 int MarkSensorEnqueue(int index
);
306 int MarkSensorDequeue(int index
, int reqFrameCnt
, nsecs_t
*timeStamp
);
307 int GetIndexForIspEnqueue(int *reqFrameCnt
);
308 int GetIndexForIspDequeue(int *reqFrameCnt
);
309 int MarkIspEnqueue(int index
);
310 int MarkIspDequeue(int index
);
311 int GetNumOnSensor();
312 int GetNumOnHalFilled();
316 int GetNextIndex(int index
);
318 int sensorEnqueueHead
;
319 int sensorDequeueHead
;
327 bayer_buf_entry_t entries
[NUM_BAYER_BUFFERS
];
331 #define NOT_AVAILABLE (0)
332 #define REQUIRES_DQ_FROM_SVC (1)
333 #define ON_DRIVER (2)
335 #define ON_SERVICE (4)
337 #define BAYER_NOT_AVAILABLE (0)
338 #define BAYER_ON_SENSOR (1)
339 #define BAYER_ON_HAL_FILLED (2)
340 #define BAYER_ON_ISP (3)
341 #define BAYER_ON_SERVICE (4)
342 #define BAYER_ON_HAL_EMPTY (5)
344 typedef struct stream_parameters
{
346 uint32_t outputWidth
;
347 uint32_t outputHeight
;
352 const camera2_stream_ops_t
* streamOps
;
356 int numOwnSvcBuffers
;
361 enum v4l2_memory memory
;
362 enum v4l2_buf_type halBuftype
;
364 buffer_handle_t svcBufHandle
[NUM_MAX_CAMERA_BUFFERS
];
365 ExynosBuffer svcBuffers
[NUM_MAX_CAMERA_BUFFERS
];
366 ExynosBuffer metaBuffers
[NUM_MAX_CAMERA_BUFFERS
];
367 int svcBufStatus
[NUM_MAX_CAMERA_BUFFERS
];
369 ion_client ionClient
;
371 } stream_parameters_t
;
373 typedef struct record_parameters
{
374 uint32_t outputWidth
;
375 uint32_t outputHeight
;
377 const camera2_stream_ops_t
* streamOps
;
380 int numOwnSvcBuffers
;
382 buffer_handle_t svcBufHandle
[NUM_MAX_CAMERA_BUFFERS
];
383 ExynosBuffer svcBuffers
[NUM_MAX_CAMERA_BUFFERS
];
384 int svcBufStatus
[NUM_MAX_CAMERA_BUFFERS
];
387 } record_parameters_t
;
389 typedef struct callback_parameters
{
390 uint32_t outputWidth
;
391 uint32_t outputHeight
;
395 const camera2_stream_ops_t
* streamOps
;
398 int numOwnSvcBuffers
;
400 buffer_handle_t svcBufHandle
[NUM_MAX_CAMERA_BUFFERS
];
401 ExynosBuffer svcBuffers
[NUM_MAX_CAMERA_BUFFERS
];
402 int svcBufStatus
[NUM_MAX_CAMERA_BUFFERS
];
405 } callback_parameters_t
;
407 class ExynosCameraHWInterface2
: public virtual RefBase
{
409 ExynosCameraHWInterface2(int cameraId
, camera2_device_t
*dev
, ExynosCamera2
* camera
, int *openInvalid
);
410 virtual ~ExynosCameraHWInterface2();
412 virtual void release();
414 inline int getCameraId() const;
416 virtual int setRequestQueueSrcOps(const camera2_request_queue_src_ops_t
*request_src_ops
);
417 virtual int notifyRequestQueueNotEmpty();
418 virtual int setFrameQueueDstOps(const camera2_frame_queue_dst_ops_t
*frame_dst_ops
);
419 virtual int getInProgressCount();
420 virtual int flushCapturesInProgress();
421 virtual int constructDefaultRequest(int request_template
, camera_metadata_t
**request
);
422 virtual int allocateStream(uint32_t width
, uint32_t height
,
423 int format
, const camera2_stream_ops_t
*stream_ops
,
424 uint32_t *stream_id
, uint32_t *format_actual
, uint32_t *usage
, uint32_t *max_buffers
);
425 virtual int registerStreamBuffers(uint32_t stream_id
, int num_buffers
, buffer_handle_t
*buffers
);
426 virtual int releaseStream(uint32_t stream_id
);
427 virtual int allocateReprocessStream(uint32_t width
, uint32_t height
,
428 uint32_t format
, const camera2_stream_in_ops_t
*reprocess_stream_ops
,
429 uint32_t *stream_id
, uint32_t *consumer_usage
, uint32_t *max_buffers
);
430 virtual int releaseReprocessStream(uint32_t stream_id
);
431 virtual int triggerAction(uint32_t trigger_id
, int ext1
, int ext2
);
432 virtual int setNotifyCallback(camera2_notify_callback notify_cb
, void *user
);
433 virtual int getMetadataVendorTagOps(vendor_tag_query_ops_t
**ops
);
434 virtual int dump(int fd
);
436 class MainThread
: public SignalDrivenThread
{
437 ExynosCameraHWInterface2
*mHardware
;
439 MainThread(ExynosCameraHWInterface2
*hw
):
440 SignalDrivenThread(),
442 // Start("MainThread", PRIORITY_DEFAULT, 0);
445 status_t
readyToRunInternal()
449 void threadFunctionInternal()
451 mHardware
->m_mainThreadFunc(this);
458 class SensorThread
: public SignalDrivenThread
{
459 ExynosCameraHWInterface2
*mHardware
;
461 SensorThread(ExynosCameraHWInterface2
*hw
):
462 SignalDrivenThread("SensorThread", PRIORITY_DEFAULT
, 0),
464 m_isBayerOutputEnabled(false) { }
466 status_t
readyToRunInternal() {
467 mHardware
->m_sensorThreadInitialize(this);
470 void threadFunctionInternal() {
471 mHardware
->m_sensorThreadFunc(this);
476 bool m_isBayerOutputEnabled
;
481 class IspThread
: public SignalDrivenThread
{
482 ExynosCameraHWInterface2
*mHardware
;
484 IspThread(ExynosCameraHWInterface2
*hw
):
485 SignalDrivenThread("IspThread", PRIORITY_DEFAULT
, 0),
488 status_t
readyToRunInternal() {
489 mHardware
->m_ispThreadInitialize(this);
492 void threadFunctionInternal() {
493 mHardware
->m_ispThreadFunc(this);
502 class StreamThread
: public SignalDrivenThread
{
503 ExynosCameraHWInterface2
*mHardware
;
505 StreamThread(ExynosCameraHWInterface2
*hw
, uint8_t new_index
):
506 SignalDrivenThread("StreamThread", PRIORITY_DEFAULT
, 0),
508 m_index(new_index
) { }
510 status_t
readyToRunInternal() {
511 mHardware
->m_streamThreadInitialize(this);
514 void threadFunctionInternal() {
515 mHardware
->m_streamThreadFunc(this);
518 void setRecordingParameter(record_parameters_t
* recordParm
);
519 void setCallbackParameter(callback_parameters_t
* callbackParm
);
520 void setParameter(stream_parameters_t
* new_parameters
);
521 void applyChange(void);
523 int findBufferIndex(void * bufAddr
);
529 stream_parameters_t m_parameters
;
530 stream_parameters_t
*m_tempParameters
;
531 record_parameters_t m_recordParameters
;
532 callback_parameters_t m_previewCbParameters
;
537 sp
<MainThread
> m_mainThread
;
538 sp
<SensorThread
> m_sensorThread
;
539 sp
<IspThread
> m_ispThread
;
540 sp
<StreamThread
> m_streamThreads
[NUM_MAX_STREAM_THREAD
];
544 RequestManager
*m_requestManager
;
545 BayerBufManager
*m_BayerManager
;
546 ExynosCamera2
*m_camera2
;
548 void m_mainThreadFunc(SignalDrivenThread
* self
);
549 void m_sensorThreadFunc(SignalDrivenThread
* self
);
550 void m_sensorThreadInitialize(SignalDrivenThread
* self
);
551 void m_ispThreadFunc(SignalDrivenThread
* self
);
552 void m_ispThreadInitialize(SignalDrivenThread
* self
);
553 void m_streamThreadFunc(SignalDrivenThread
* self
);
554 void m_streamThreadInitialize(SignalDrivenThread
* self
);
556 void m_streamFunc0(SignalDrivenThread
*self
);
557 void m_streamFunc1(SignalDrivenThread
*self
);
559 void m_streamBufferInit(SignalDrivenThread
*self
);
561 void m_getAlignedYUVSize(int colorFormat
, int w
, int h
,
563 bool m_getRatioSize(int src_w
, int src_h
,
564 int dst_w
, int dst_h
,
565 int *crop_x
, int *crop_y
,
566 int *crop_w
, int *crop_h
,
568 int createIonClient(ion_client ionClient
);
569 int deleteIonClient(ion_client ionClient
);
571 int allocCameraMemory(ion_client ionClient
, ExynosBuffer
*buf
, int iMemoryNum
);
572 int allocCameraMemory(ion_client ionClient
, ExynosBuffer
*buf
, int iMemoryNum
, int cacheFlag
);
573 void freeCameraMemory(ExynosBuffer
*buf
, int iMemoryNum
);
574 void initCameraMemory(ExynosBuffer
*buf
, int iMemoryNum
);
576 void DumpInfoWithShot(struct camera2_shot_ext
* shot_ext
);
577 bool m_checkThumbnailSize(int w
, int h
);
578 bool yuv2Jpeg(ExynosBuffer
*yuvBuf
,
579 ExynosBuffer
*jpegBuf
,
581 int InitializeISPChain();
584 void SetAfMode(enum aa_afmode afMode
);
585 void OnAfTriggerStart(int id
);
586 void OnAfTrigger(int id
);
587 void OnAfTriggerAutoMacro(int id
);
588 void OnAfTriggerCAFPicture(int id
);
589 void OnAfTriggerCAFVideo(int id
);
590 void OnAfCancel(int id
);
591 void OnAfCancelAutoMacro(int id
);
592 void OnAfCancelCAFPicture(int id
);
593 void OnAfCancelCAFVideo(int id
);
594 void OnAfNotification(enum aa_afstate noti
);
595 void OnAfNotificationAutoMacro(enum aa_afstate noti
);
596 void OnAfNotificationCAFPicture(enum aa_afstate noti
);
597 void OnAfNotificationCAFVideo(enum aa_afstate noti
);
598 void SetAfStateForService(int newState
);
599 int GetAfStateForService();
600 exif_attribute_t mExifInfo
;
601 void m_setExifFixedAttribute(void);
602 void m_setExifChangedAttribute(exif_attribute_t
*exifInfo
, ExynosRect
*rect
,
603 camera2_shot
*currentEntry
);
604 void flashSetter(struct camera2_shot_ext
* shot_ext
);
605 void flashListenerSensor(struct camera2_shot_ext
* shot_ext
);
606 void flashListenerISP(struct camera2_shot_ext
* shot_ext
);
607 void *m_exynosPictureCSC
;
608 void *m_exynosVideoCSC
;
610 int m_jpegEncodingFrameCnt
;
612 camera2_request_queue_src_ops_t
*m_requestQueueOps
;
613 camera2_frame_queue_dst_ops_t
*m_frameQueueOps
;
614 camera2_notify_callback m_notifyCb
;
615 void *m_callbackCookie
;
617 int m_numOfRemainingReqInSvc
;
618 bool m_isRequestQueuePending
;
619 bool m_isRequestQueueNull
;
620 camera2_device_t
*m_halDevice
;
621 static gralloc_module_t
const* m_grallocHal
;
624 camera_hw_info_t m_camera_info
;
626 ion_client m_ionCameraClient
;
628 bool m_isSensorThreadOn
;
629 bool m_isSensorStarted
;
632 int m_need_streamoff
;
637 int indexToQueue
[3+1];
642 ExynosBuffer m_resizeBuf
;
643 bool m_recordingEnabled
;
646 bool m_needsRecordBufferInit
;
647 ExynosBuffer m_previewCbBuf
;
648 int m_previewCbEnabled
;
649 int m_previewCbOutput
;
650 bool m_needsPreviewCbBufferInit
;
656 bool m_aspectChanged
;
657 uint32_t lastAfRegion
[4];
660 mutable Mutex m_qbufLock
;
664 enum aa_afmode m_afMode
;
665 enum aa_afmode m_afMode2
;
666 bool m_IsAfModeUpdateRequired
;
667 bool m_IsAfTriggerRequired
;
668 bool m_IsAfLockRequired
;
669 int m_serviceAfState
;
670 bool m_AfHwStateFailed
;
671 int m_afPendingTriggerId
;
672 int m_afModeWaitingCnt
;
673 struct camera2_shot m_jpegMetadata
;
674 int m_scpOutputSignalCnt
;
675 int m_scpOutputImageCnt
;
676 int m_nightCaptureCnt
;
677 int m_nightCaptureFrameCnt
;
680 ctl_request_info_t m_ctlInfo
;
683 }; // namespace android