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 ENABLE_FRAME_SYNC
59 #define NODE_PREFIX "/dev/video"
61 #define NUM_MAX_STREAM_THREAD (5)
62 #define NUM_MAX_REQUEST_MGR_ENTRY (10)
63 #define NUM_MAX_DEQUEUED_REQUEST NUM_MAX_REQUEST_MGR_ENTRY
64 #define MAX_CAMERA_MEMORY_PLANE_NUM (4)
65 #define NUM_MAX_CAMERA_BUFFERS (16)
66 #define NUM_BAYER_BUFFERS (8)
67 #define NUM_SENSOR_QBUF (3)
69 #define PICTURE_GSC_NODE_NUM (2)
70 #define VIDEO_GSC_NODE_NUM (1)
72 #define STREAM_TYPE_DIRECT (0)
73 #define STREAM_TYPE_INDIRECT (1)
75 #define SIGNAL_MAIN_REQ_Q_NOT_EMPTY (SIGNAL_THREAD_COMMON_LAST<<1)
76 #define SIGNAL_MAIN_REPROCESS_Q_NOT_EMPTY (SIGNAL_THREAD_COMMON_LAST<<2)
77 #define SIGNAL_MAIN_STREAM_OUTPUT_DONE (SIGNAL_THREAD_COMMON_LAST<<3)
78 #define SIGNAL_SENSOR_START_REQ_PROCESSING (SIGNAL_THREAD_COMMON_LAST<<4)
79 #define SIGNAL_STREAM_GET_BUFFER (SIGNAL_THREAD_COMMON_LAST<<5)
80 #define SIGNAL_STREAM_PUT_BUFFER (SIGNAL_THREAD_COMMON_LAST<<6)
81 #define SIGNAL_STREAM_CHANGE_PARAMETER (SIGNAL_THREAD_COMMON_LAST<<7)
82 #define SIGNAL_THREAD_RELEASE (SIGNAL_THREAD_COMMON_LAST<<8)
83 #define SIGNAL_ISP_START_BAYER_INPUT (SIGNAL_THREAD_COMMON_LAST<<9)
84 #define SIGNAL_ISP_START_BAYER_DEQUEUE (SIGNAL_THREAD_COMMON_LAST<<10)
86 #define SIGNAL_STREAM_DATA_COMING (SIGNAL_THREAD_COMMON_LAST<<15)
88 #define NO_TRANSITION (0)
89 #define HAL_AFSTATE_INACTIVE (1)
90 #define HAL_AFSTATE_NEEDS_COMMAND (2)
91 #define HAL_AFSTATE_STARTED (3)
92 #define HAL_AFSTATE_SCANNING (4)
93 #define HAL_AFSTATE_LOCKED (5)
94 #define HAL_AFSTATE_FAILED (6)
95 #define HAL_AFSTATE_NEEDS_DETERMINATION (7)
96 #define HAL_AFSTATE_PASSIVE_FOCUSED (8)
99 SENSOR_NAME_S5K3H2
= 1,
100 SENSOR_NAME_S5K6A3
= 2,
101 SENSOR_NAME_S5K4E5
= 3,
102 SENSOR_NAME_S5K3H7
= 4,
103 SENSOR_NAME_CUSTOM
= 5,
107 enum is_subscenario_id
{
108 ISS_SUB_SCENARIO_STILL
,
109 ISS_SUB_SCENARIO_VIDEO
,
110 ISS_SUB_SCENARIO_SCENE1
,
111 ISS_SUB_SCENARIO_SCENE2
,
112 ISS_SUB_SCENARIO_SCENE3
,
116 int SUPPORT_THUMBNAIL_REAR_SIZE
[][2] =
123 int SUPPORT_THUMBNAIL_FRONT_SIZE
[][2] =
131 enum is_flash_scenario_state
{
133 IS_FLASH_ON_DONE
= 2,
134 IS_FLASH_AUTO_AE_AWB_LOCK
= 10,
136 IS_FLASH_AUTO_AE_AWB_LOCKED_AUTO_END
,
138 IS_FLASH_CAPTURE_JPEG
,
139 IS_FLASH_CAPTURE_END
,
143 enum is_af_flash_scenario_state
{
145 IS_FLASH_AF_ON_START
,
151 typedef struct node_info
{
158 enum v4l2_memory memory
;
159 enum v4l2_buf_type type
;
160 ion_client ionClient
;
161 ExynosBuffer buffer
[NUM_MAX_CAMERA_BUFFERS
];
166 typedef struct camera_hw_info
{
174 struct camera2_shot_ext dummy_shot
;
178 typedef enum request_entry_status
{
183 } request_entry_status_t
;
185 typedef struct request_manager_entry
{
186 request_entry_status_t status
;
187 camera_metadata_t
*original_request
;
188 struct camera2_shot_ext internal_shot
;
189 int output_stream_count
;
190 bool dynamic_meta_vaild
;
191 } request_manager_entry_t
;
193 class RequestManager
{
195 RequestManager(SignalDrivenThread
* main_thread
);
198 bool IsRequestQueueFull();
200 void RegisterRequest(camera_metadata_t
*new_request
);
201 void DeregisterRequest(camera_metadata_t
**deregistered_request
);
202 bool PrepareFrame(size_t *num_entries
, size_t *frame_size
,
203 camera_metadata_t
**prepared_frame
, int afState
);
204 int MarkProcessingRequest(ExynosBuffer
* buf
, int *afMode
);
205 void NotifyStreamOutput(int frameCnt
, int stream_id
);
206 void DumpInfoWithIndex(int index
);
207 void ApplyDynamicMetadata(struct camera2_shot_ext
*shot_ext
);
208 void CheckCompleted(int index
);
209 void UpdateIspParameters(struct camera2_shot_ext
*shot_ext
, int frameCnt
);
210 void RegisterTimestamp(int frameCnt
, nsecs_t
*frameTime
);
211 uint64_t GetTimestamp(int frameCnt
);
212 int FindFrameCnt(struct camera2_shot_ext
* shot_ext
);
213 int FindEntryIndexByFrameCnt(int frameCnt
);
215 int GetNextIndex(int index
);
216 void SetDefaultParameters(int cropX
);
217 void SetInitialSkip(int count
);
219 void SetFrameIndex(int index
);
223 MetadataConverter
*m_metadataConverter
;
224 SignalDrivenThread
*m_mainThread
;
226 int m_entryInsertionIndex
;
227 int m_entryProcessingIndex
;
228 int m_entryFrameOutputIndex
;
229 request_manager_entry_t entries
[NUM_MAX_REQUEST_MGR_ENTRY
];
230 int m_completedIndex
;
232 Mutex m_requestMutex
;
234 //TODO : alloc dynamically
235 char m_tempFrameMetadataBuf
[2000];
236 camera_metadata_t
*m_tempFrameMetadata
;
238 int m_sensorPipelineSkipCnt
;
248 typedef struct bayer_buf_entry
{
255 class BayerBufManager
{
259 int GetIndexForSensorEnqueue();
260 int MarkSensorEnqueue(int index
);
261 int MarkSensorDequeue(int index
, int reqFrameCnt
, nsecs_t
*timeStamp
);
262 int GetIndexForIspEnqueue(int *reqFrameCnt
);
263 int GetIndexForIspDequeue(int *reqFrameCnt
);
264 int MarkIspEnqueue(int index
);
265 int MarkIspDequeue(int index
);
266 int GetNumOnSensor();
267 int GetNumOnHalFilled();
271 int GetNextIndex(int index
);
273 int sensorEnqueueHead
;
274 int sensorDequeueHead
;
282 bayer_buf_entry_t entries
[NUM_BAYER_BUFFERS
];
286 #define NOT_AVAILABLE (0)
287 #define REQUIRES_DQ_FROM_SVC (1)
288 #define ON_DRIVER (2)
290 #define ON_SERVICE (4)
292 #define BAYER_NOT_AVAILABLE (0)
293 #define BAYER_ON_SENSOR (1)
294 #define BAYER_ON_HAL_FILLED (2)
295 #define BAYER_ON_ISP (3)
296 #define BAYER_ON_SERVICE (4)
297 #define BAYER_ON_HAL_EMPTY (5)
299 typedef struct stream_parameters
{
301 uint32_t outputWidth
;
302 uint32_t outputHeight
;
307 const camera2_stream_ops_t
* streamOps
;
311 int numOwnSvcBuffers
;
316 enum v4l2_memory memory
;
317 enum v4l2_buf_type halBuftype
;
319 buffer_handle_t svcBufHandle
[NUM_MAX_CAMERA_BUFFERS
];
320 ExynosBuffer svcBuffers
[NUM_MAX_CAMERA_BUFFERS
];
321 ExynosBuffer metaBuffers
[NUM_MAX_CAMERA_BUFFERS
];
322 int svcBufStatus
[NUM_MAX_CAMERA_BUFFERS
];
324 ion_client ionClient
;
326 } stream_parameters_t
;
328 typedef struct record_parameters
{
329 uint32_t outputWidth
;
330 uint32_t outputHeight
;
332 const camera2_stream_ops_t
* streamOps
;
335 int numOwnSvcBuffers
;
337 buffer_handle_t svcBufHandle
[NUM_MAX_CAMERA_BUFFERS
];
338 ExynosBuffer svcBuffers
[NUM_MAX_CAMERA_BUFFERS
];
339 int svcBufStatus
[NUM_MAX_CAMERA_BUFFERS
];
342 } record_parameters_t
;
344 typedef struct callback_parameters
{
345 uint32_t outputWidth
;
346 uint32_t outputHeight
;
350 const camera2_stream_ops_t
* streamOps
;
353 int numOwnSvcBuffers
;
355 buffer_handle_t svcBufHandle
[NUM_MAX_CAMERA_BUFFERS
];
356 ExynosBuffer svcBuffers
[NUM_MAX_CAMERA_BUFFERS
];
357 int svcBufStatus
[NUM_MAX_CAMERA_BUFFERS
];
360 } callback_parameters_t
;
362 class ExynosCameraHWInterface2
: public virtual RefBase
{
364 ExynosCameraHWInterface2(int cameraId
, camera2_device_t
*dev
, ExynosCamera2
* camera
, int *openInvalid
);
365 virtual ~ExynosCameraHWInterface2();
367 virtual void release();
369 inline int getCameraId() const;
371 virtual int setRequestQueueSrcOps(const camera2_request_queue_src_ops_t
*request_src_ops
);
372 virtual int notifyRequestQueueNotEmpty();
373 virtual int setFrameQueueDstOps(const camera2_frame_queue_dst_ops_t
*frame_dst_ops
);
374 virtual int getInProgressCount();
375 virtual int flushCapturesInProgress();
376 virtual int constructDefaultRequest(int request_template
, camera_metadata_t
**request
);
377 virtual int allocateStream(uint32_t width
, uint32_t height
,
378 int format
, const camera2_stream_ops_t
*stream_ops
,
379 uint32_t *stream_id
, uint32_t *format_actual
, uint32_t *usage
, uint32_t *max_buffers
);
380 virtual int registerStreamBuffers(uint32_t stream_id
, int num_buffers
, buffer_handle_t
*buffers
);
381 virtual int releaseStream(uint32_t stream_id
);
382 virtual int allocateReprocessStream(uint32_t width
, uint32_t height
,
383 uint32_t format
, const camera2_stream_in_ops_t
*reprocess_stream_ops
,
384 uint32_t *stream_id
, uint32_t *consumer_usage
, uint32_t *max_buffers
);
385 virtual int releaseReprocessStream(uint32_t stream_id
);
386 virtual int triggerAction(uint32_t trigger_id
, int ext1
, int ext2
);
387 virtual int setNotifyCallback(camera2_notify_callback notify_cb
, void *user
);
388 virtual int getMetadataVendorTagOps(vendor_tag_query_ops_t
**ops
);
389 virtual int dump(int fd
);
391 class MainThread
: public SignalDrivenThread
{
392 ExynosCameraHWInterface2
*mHardware
;
394 MainThread(ExynosCameraHWInterface2
*hw
):
395 SignalDrivenThread(),
397 // Start("MainThread", PRIORITY_DEFAULT, 0);
400 status_t
readyToRunInternal()
404 void threadFunctionInternal()
406 mHardware
->m_mainThreadFunc(this);
413 class SensorThread
: public SignalDrivenThread
{
414 ExynosCameraHWInterface2
*mHardware
;
416 SensorThread(ExynosCameraHWInterface2
*hw
):
417 SignalDrivenThread("SensorThread", PRIORITY_DEFAULT
, 0),
419 m_isBayerOutputEnabled(false) { }
421 status_t
readyToRunInternal() {
422 mHardware
->m_sensorThreadInitialize(this);
425 void threadFunctionInternal() {
426 mHardware
->m_sensorThreadFunc(this);
431 bool m_isBayerOutputEnabled
;
436 class IspThread
: public SignalDrivenThread
{
437 ExynosCameraHWInterface2
*mHardware
;
439 IspThread(ExynosCameraHWInterface2
*hw
):
440 SignalDrivenThread("IspThread", PRIORITY_DEFAULT
, 0),
443 status_t
readyToRunInternal() {
444 mHardware
->m_ispThreadInitialize(this);
447 void threadFunctionInternal() {
448 mHardware
->m_ispThreadFunc(this);
457 class StreamThread
: public SignalDrivenThread
{
458 ExynosCameraHWInterface2
*mHardware
;
460 StreamThread(ExynosCameraHWInterface2
*hw
, uint8_t new_index
):
461 SignalDrivenThread("StreamThread", PRIORITY_DEFAULT
, 0),
463 m_index(new_index
) { }
465 status_t
readyToRunInternal() {
466 mHardware
->m_streamThreadInitialize(this);
469 void threadFunctionInternal() {
470 mHardware
->m_streamThreadFunc(this);
473 void setRecordingParameter(record_parameters_t
* recordParm
);
474 void setCallbackParameter(callback_parameters_t
* callbackParm
);
475 void setParameter(stream_parameters_t
* new_parameters
);
476 void applyChange(void);
478 int findBufferIndex(void * bufAddr
);
484 stream_parameters_t m_parameters
;
485 stream_parameters_t
*m_tempParameters
;
486 record_parameters_t m_recordParameters
;
487 callback_parameters_t m_previewCbParameters
;
492 sp
<MainThread
> m_mainThread
;
493 sp
<SensorThread
> m_sensorThread
;
494 sp
<IspThread
> m_ispThread
;
495 sp
<StreamThread
> m_streamThreads
[NUM_MAX_STREAM_THREAD
];
499 RequestManager
*m_requestManager
;
500 BayerBufManager
*m_BayerManager
;
501 ExynosCamera2
*m_camera2
;
503 void m_mainThreadFunc(SignalDrivenThread
* self
);
504 void m_sensorThreadFunc(SignalDrivenThread
* self
);
505 void m_sensorThreadInitialize(SignalDrivenThread
* self
);
506 void m_ispThreadFunc(SignalDrivenThread
* self
);
507 void m_ispThreadInitialize(SignalDrivenThread
* self
);
508 void m_streamThreadFunc(SignalDrivenThread
* self
);
509 void m_streamThreadInitialize(SignalDrivenThread
* self
);
511 void m_streamFunc0(SignalDrivenThread
*self
);
512 void m_streamFunc1(SignalDrivenThread
*self
);
514 void m_streamBufferInit(SignalDrivenThread
*self
);
516 void m_getAlignedYUVSize(int colorFormat
, int w
, int h
,
518 bool m_getRatioSize(int src_w
, int src_h
,
519 int dst_w
, int dst_h
,
520 int *crop_x
, int *crop_y
,
521 int *crop_w
, int *crop_h
,
523 int createIonClient(ion_client ionClient
);
524 int deleteIonClient(ion_client ionClient
);
526 int allocCameraMemory(ion_client ionClient
, ExynosBuffer
*buf
, int iMemoryNum
);
527 void freeCameraMemory(ExynosBuffer
*buf
, int iMemoryNum
);
528 void initCameraMemory(ExynosBuffer
*buf
, int iMemoryNum
);
530 void DumpInfoWithShot(struct camera2_shot_ext
* shot_ext
);
531 bool m_checkThumbnailSize(int w
, int h
);
532 bool yuv2Jpeg(ExynosBuffer
*yuvBuf
,
533 ExynosBuffer
*jpegBuf
,
535 int InitializeISPChain();
538 void SetAfMode(enum aa_afmode afMode
);
539 void OnAfTriggerStart(int id
);
540 void OnAfTrigger(int id
);
541 void OnAfTriggerAutoMacro(int id
);
542 void OnAfTriggerCAFPicture(int id
);
543 void OnAfTriggerCAFVideo(int id
);
544 void OnAfCancel(int id
);
545 void OnAfCancelAutoMacro(int id
);
546 void OnAfCancelCAFPicture(int id
);
547 void OnAfCancelCAFVideo(int id
);
548 void OnAfNotification(enum aa_afstate noti
);
549 void OnAfNotificationAutoMacro(enum aa_afstate noti
);
550 void OnAfNotificationCAFPicture(enum aa_afstate noti
);
551 void OnAfNotificationCAFVideo(enum aa_afstate noti
);
552 void SetAfStateForService(int newState
);
553 int GetAfStateForService();
554 exif_attribute_t mExifInfo
;
555 void m_setExifFixedAttribute(void);
556 void m_setExifChangedAttribute(exif_attribute_t
*exifInfo
, ExynosRect
*rect
,
557 camera2_shot
*currentEntry
);
558 void flashSetter(struct camera2_shot_ext
* shot_ext
);
559 void flashListener(struct camera2_shot_ext
* shot_ext
);
560 void *m_exynosPictureCSC
;
561 void *m_exynosVideoCSC
;
563 int m_jpegEncodingFrameCnt
;
565 camera2_request_queue_src_ops_t
*m_requestQueueOps
;
566 camera2_frame_queue_dst_ops_t
*m_frameQueueOps
;
567 camera2_notify_callback m_notifyCb
;
568 void *m_callbackCookie
;
570 int m_numOfRemainingReqInSvc
;
571 bool m_isRequestQueuePending
;
572 bool m_isRequestQueueNull
;
573 camera2_device_t
*m_halDevice
;
574 static gralloc_module_t
const* m_grallocHal
;
577 camera_hw_info_t m_camera_info
;
579 ion_client m_ionCameraClient
;
581 bool m_isSensorThreadOn
;
582 bool m_isSensorStarted
;
585 int m_need_streamoff
;
590 int indexToQueue
[3+1];
595 ExynosBuffer m_resizeBuf
;
596 bool m_recordingEnabled
;
599 bool m_needsRecordBufferInit
;
600 ExynosBuffer m_previewCbBuf
;
601 int m_previewCbEnabled
;
602 int m_previewCbOutput
;
603 bool m_needsPreviewCbBufferInit
;
609 bool m_aspectChanged
;
610 uint32_t lastAfRegion
[4];
613 mutable Mutex m_qbufLock
;
617 enum aa_afmode m_afMode
;
618 enum aa_afmode m_afMode2
;
619 bool m_IsAfModeUpdateRequired
;
620 bool m_IsAfTriggerRequired
;
621 bool m_IsAfLockRequired
;
622 int m_serviceAfState
;
623 bool m_AfHwStateFailed
;
624 int m_afPendingTriggerId
;
625 int m_afModeWaitingCnt
;
626 struct camera2_shot m_jpegMetadata
;
627 int m_scpOutputSignalCnt
;
628 int m_scpOutputImageCnt
;
629 int m_nightCaptureCnt
;
630 int m_nightCaptureFrameCnt
;
633 bool m_afFlashEnableFlg
;
635 bool m_flashEnableFlg
;
636 bool m_flashCaptured
;
637 int m_flashFrameCount
;
643 }; // namespace android