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"
58 #define NODE_PREFIX "/dev/video"
60 #define NUM_MAX_STREAM_THREAD (5)
61 #define NUM_MAX_REQUEST_MGR_ENTRY (10)
62 #define NUM_MAX_DEQUEUED_REQUEST NUM_MAX_REQUEST_MGR_ENTRY
63 #define MAX_CAMERA_MEMORY_PLANE_NUM (4)
64 #define NUM_MAX_CAMERA_BUFFERS (16)
65 #define NUM_BAYER_BUFFERS (8)
66 #define NUM_SENSOR_QBUF (3)
68 #define PREVIEW_GSC_NODE_NUM (1)
69 #define PICTURE_GSC_NODE_NUM (2)
71 #define STREAM_TYPE_DIRECT (0)
72 #define STREAM_TYPE_INDIRECT (1)
74 #define SIGNAL_MAIN_REQ_Q_NOT_EMPTY (SIGNAL_THREAD_COMMON_LAST<<1)
75 #define SIGNAL_MAIN_REPROCESS_Q_NOT_EMPTY (SIGNAL_THREAD_COMMON_LAST<<2)
76 #define SIGNAL_MAIN_STREAM_OUTPUT_DONE (SIGNAL_THREAD_COMMON_LAST<<3)
77 #define SIGNAL_SENSOR_START_REQ_PROCESSING (SIGNAL_THREAD_COMMON_LAST<<4)
78 #define SIGNAL_STREAM_GET_BUFFER (SIGNAL_THREAD_COMMON_LAST<<5)
79 #define SIGNAL_STREAM_PUT_BUFFER (SIGNAL_THREAD_COMMON_LAST<<6)
80 #define SIGNAL_STREAM_CHANGE_PARAMETER (SIGNAL_THREAD_COMMON_LAST<<7)
81 #define SIGNAL_THREAD_RELEASE (SIGNAL_THREAD_COMMON_LAST<<8)
82 #define SIGNAL_ISP_START_BAYER_INPUT (SIGNAL_THREAD_COMMON_LAST<<9)
83 #define SIGNAL_ISP_START_BAYER_DEQUEUE (SIGNAL_THREAD_COMMON_LAST<<10)
85 #define SIGNAL_STREAM_DATA_COMING (SIGNAL_THREAD_COMMON_LAST<<15)
87 #define NO_TRANSITION (0)
88 #define HAL_AFSTATE_INACTIVE (1)
89 #define HAL_AFSTATE_NEEDS_COMMAND (2)
90 #define HAL_AFSTATE_STARTED (3)
91 #define HAL_AFSTATE_SCANNING (4)
92 #define HAL_AFSTATE_LOCKED (5)
93 #define HAL_AFSTATE_FAILED (6)
94 #define HAL_AFSTATE_NEEDS_DETERMINATION (7)
95 #define HAL_AFSTATE_PASSIVE_FOCUSED (8)
98 SENSOR_NAME_S5K3H2
= 1,
99 SENSOR_NAME_S5K6A3
= 2,
100 SENSOR_NAME_S5K4E5
= 3,
101 SENSOR_NAME_S5K3H7
= 4,
102 SENSOR_NAME_CUSTOM
= 5,
107 typedef struct node_info
{
114 enum v4l2_memory memory
;
115 enum v4l2_buf_type type
;
116 ion_client ionClient
;
117 ExynosBuffer buffer
[NUM_MAX_CAMERA_BUFFERS
];
121 typedef struct camera_hw_info
{
129 struct camera2_shot_ext dummy_shot
;
133 typedef enum request_entry_status
{
138 } request_entry_status_t
;
140 typedef struct request_manager_entry
{
141 request_entry_status_t status
;
142 camera_metadata_t
*original_request
;
143 struct camera2_shot_ext internal_shot
;
144 int output_stream_count
;
145 bool dynamic_meta_vaild
;
146 } request_manager_entry_t
;
148 class RequestManager
{
150 RequestManager(SignalDrivenThread
* main_thread
);
153 bool IsRequestQueueFull();
155 void RegisterRequest(camera_metadata_t
*new_request
);
156 void DeregisterRequest(camera_metadata_t
**deregistered_request
);
157 bool PrepareFrame(size_t *num_entries
, size_t *frame_size
,
158 camera_metadata_t
**prepared_frame
, int afState
);
159 int MarkProcessingRequest(ExynosBuffer
* buf
, int *afMode
);
160 void NotifyStreamOutput(int frameCnt
, int stream_id
);
161 void DumpInfoWithIndex(int index
);
162 void ApplyDynamicMetadata(struct camera2_shot_ext
*shot_ext
);
163 void CheckCompleted(int index
);
164 void UpdateIspParameters(struct camera2_shot_ext
*shot_ext
, int frameCnt
, bool afTrigger
);
165 void RegisterTimestamp(int frameCnt
, nsecs_t
*frameTime
);
166 uint64_t GetTimestamp(int frameCnt
);
167 int FindFrameCnt(struct camera2_shot_ext
* shot_ext
);
168 int FindEntryIndexByFrameCnt(int frameCnt
);
170 int GetNextIndex(int index
);
171 void SetDefaultParameters(int cropX
);
172 void SetInitialSkip(int count
);
174 void SetFrameIndex(int index
);
178 MetadataConverter
*m_metadataConverter
;
179 SignalDrivenThread
*m_mainThread
;
181 int m_entryInsertionIndex
;
182 int m_entryProcessingIndex
;
183 int m_entryFrameOutputIndex
;
184 request_manager_entry_t entries
[NUM_MAX_REQUEST_MGR_ENTRY
];
185 int m_completedIndex
;
187 Mutex m_requestMutex
;
189 //TODO : alloc dynamically
190 char m_tempFrameMetadataBuf
[2000];
191 camera_metadata_t
*m_tempFrameMetadata
;
193 int m_sensorPipelineSkipCnt
;
203 typedef struct bayer_buf_entry
{
210 class BayerBufManager
{
214 int GetIndexForSensorEnqueue();
215 int MarkSensorEnqueue(int index
);
216 int MarkSensorDequeue(int index
, int reqFrameCnt
, nsecs_t
*timeStamp
);
217 int GetIndexForIspEnqueue(int *reqFrameCnt
);
218 int GetIndexForIspDequeue(int *reqFrameCnt
);
219 int MarkIspEnqueue(int index
);
220 int MarkIspDequeue(int index
);
221 int GetNumOnSensor();
222 int GetNumOnHalFilled();
226 int GetNextIndex(int index
);
228 int sensorEnqueueHead
;
229 int sensorDequeueHead
;
237 bayer_buf_entry_t entries
[NUM_BAYER_BUFFERS
];
241 #define NOT_AVAILABLE (0)
242 #define REQUIRES_DQ_FROM_SVC (1)
243 #define ON_DRIVER (2)
245 #define ON_SERVICE (4)
247 #define BAYER_NOT_AVAILABLE (0)
248 #define BAYER_ON_SENSOR (1)
249 #define BAYER_ON_HAL_FILLED (2)
250 #define BAYER_ON_ISP (3)
251 #define BAYER_ON_SERVICE (4)
252 #define BAYER_ON_HAL_EMPTY (5)
254 typedef struct stream_parameters
{
256 uint32_t outputWidth
;
257 uint32_t outputHeight
;
262 const camera2_stream_ops_t
* streamOps
;
266 int numOwnSvcBuffers
;
270 enum v4l2_memory memory
;
271 enum v4l2_buf_type halBuftype
;
273 buffer_handle_t svcBufHandle
[NUM_MAX_CAMERA_BUFFERS
];
274 ExynosBuffer svcBuffers
[NUM_MAX_CAMERA_BUFFERS
];
275 int svcBufStatus
[NUM_MAX_CAMERA_BUFFERS
];
277 ion_client ionClient
;
279 } stream_parameters_t
;
281 typedef struct record_parameters
{
282 uint32_t outputWidth
;
283 uint32_t outputHeight
;
285 const camera2_stream_ops_t
* streamOps
;
288 int numOwnSvcBuffers
;
290 buffer_handle_t svcBufHandle
[NUM_MAX_CAMERA_BUFFERS
];
291 ExynosBuffer svcBuffers
[NUM_MAX_CAMERA_BUFFERS
];
292 int svcBufStatus
[NUM_MAX_CAMERA_BUFFERS
];
295 } record_parameters_t
;
297 class ExynosCameraHWInterface2
: public virtual RefBase
{
299 ExynosCameraHWInterface2(int cameraId
, camera2_device_t
*dev
, ExynosCamera2
* camera
);
300 virtual ~ExynosCameraHWInterface2();
302 virtual void release();
304 inline int getCameraId() const;
306 virtual int setRequestQueueSrcOps(const camera2_request_queue_src_ops_t
*request_src_ops
);
307 virtual int notifyRequestQueueNotEmpty();
308 virtual int setFrameQueueDstOps(const camera2_frame_queue_dst_ops_t
*frame_dst_ops
);
309 virtual int getInProgressCount();
310 virtual int flushCapturesInProgress();
311 virtual int constructDefaultRequest(int request_template
, camera_metadata_t
**request
);
312 virtual int allocateStream(uint32_t width
, uint32_t height
,
313 int format
, const camera2_stream_ops_t
*stream_ops
,
314 uint32_t *stream_id
, uint32_t *format_actual
, uint32_t *usage
, uint32_t *max_buffers
);
315 virtual int registerStreamBuffers(uint32_t stream_id
, int num_buffers
, buffer_handle_t
*buffers
);
316 virtual int releaseStream(uint32_t stream_id
);
317 virtual int allocateReprocessStream(uint32_t width
, uint32_t height
,
318 uint32_t format
, const camera2_stream_in_ops_t
*reprocess_stream_ops
,
319 uint32_t *stream_id
, uint32_t *consumer_usage
, uint32_t *max_buffers
);
320 virtual int releaseReprocessStream(uint32_t stream_id
);
321 virtual int triggerAction(uint32_t trigger_id
, int ext1
, int ext2
);
322 virtual int setNotifyCallback(camera2_notify_callback notify_cb
, void *user
);
323 virtual int getMetadataVendorTagOps(vendor_tag_query_ops_t
**ops
);
324 virtual int dump(int fd
);
326 class MainThread
: public SignalDrivenThread
{
327 ExynosCameraHWInterface2
*mHardware
;
329 MainThread(ExynosCameraHWInterface2
*hw
):
330 SignalDrivenThread(),
332 // Start("MainThread", PRIORITY_DEFAULT, 0);
335 status_t
readyToRunInternal()
339 void threadFunctionInternal()
341 mHardware
->m_mainThreadFunc(this);
347 class SensorThread
: public SignalDrivenThread
{
348 ExynosCameraHWInterface2
*mHardware
;
350 SensorThread(ExynosCameraHWInterface2
*hw
):
351 SignalDrivenThread("SensorThread", PRIORITY_DEFAULT
, 0),
353 m_isBayerOutputEnabled(false) { }
355 status_t
readyToRunInternal() {
356 mHardware
->m_sensorThreadInitialize(this);
359 void threadFunctionInternal() {
360 mHardware
->m_sensorThreadFunc(this);
365 bool m_isBayerOutputEnabled
;
369 class IspThread
: public SignalDrivenThread
{
370 ExynosCameraHWInterface2
*mHardware
;
372 IspThread(ExynosCameraHWInterface2
*hw
):
373 SignalDrivenThread("IspThread", PRIORITY_DEFAULT
, 0),
376 status_t
readyToRunInternal() {
377 mHardware
->m_ispThreadInitialize(this);
380 void threadFunctionInternal() {
381 mHardware
->m_ispThreadFunc(this);
389 class StreamThread
: public SignalDrivenThread
{
390 ExynosCameraHWInterface2
*mHardware
;
392 StreamThread(ExynosCameraHWInterface2
*hw
, uint8_t new_index
):
393 SignalDrivenThread("StreamThread", PRIORITY_DEFAULT
, 0),
395 m_index(new_index
) { }
397 status_t
readyToRunInternal() {
398 mHardware
->m_streamThreadInitialize(this);
401 void threadFunctionInternal() {
402 mHardware
->m_streamThreadFunc(this);
405 void setRecordingParameter(record_parameters_t
* recordParm
);
406 void setParameter(stream_parameters_t
* new_parameters
);
407 void applyChange(void);
409 int findBufferIndex(void * bufAddr
);
415 stream_parameters_t m_parameters
;
416 stream_parameters_t
*m_tempParameters
;
417 record_parameters_t m_recordParameters
;
422 sp
<MainThread
> m_mainThread
;
423 sp
<SensorThread
> m_sensorThread
;
424 sp
<IspThread
> m_ispThread
;
425 sp
<StreamThread
> m_streamThreads
[NUM_MAX_STREAM_THREAD
];
429 RequestManager
*m_requestManager
;
430 BayerBufManager
*m_BayerManager
;
431 ExynosCamera2
*m_camera2
;
433 void m_mainThreadFunc(SignalDrivenThread
* self
);
434 void m_sensorThreadFunc(SignalDrivenThread
* self
);
435 void m_sensorThreadInitialize(SignalDrivenThread
* self
);
436 void m_ispThreadFunc(SignalDrivenThread
* self
);
437 void m_ispThreadInitialize(SignalDrivenThread
* self
);
438 void m_streamThreadFunc(SignalDrivenThread
* self
);
439 void m_streamThreadInitialize(SignalDrivenThread
* self
);
441 void m_getAlignedYUVSize(int colorFormat
, int w
, int h
,
443 bool m_getRatioSize(int src_w
, int src_h
,
444 int dst_w
, int dst_h
,
445 int *crop_x
, int *crop_y
,
446 int *crop_w
, int *crop_h
,
448 int createIonClient(ion_client ionClient
);
449 int deleteIonClient(ion_client ionClient
);
451 int allocCameraMemory(ion_client ionClient
, ExynosBuffer
*buf
, int iMemoryNum
);
452 void freeCameraMemory(ExynosBuffer
*buf
, int iMemoryNum
);
453 void initCameraMemory(ExynosBuffer
*buf
, int iMemoryNum
);
455 void DumpInfoWithShot(struct camera2_shot_ext
* shot_ext
);
456 bool yuv2Jpeg(ExynosBuffer
*yuvBuf
,
457 ExynosBuffer
*jpegBuf
,
459 void InitializeISPChain();
462 void SetAfMode(enum aa_afmode afMode
);
463 void OnAfTrigger(int id
);
464 void OnAfTriggerAutoMacro(int id
);
465 void OnAfTriggerCAFPicture(int id
);
466 void OnAfTriggerCAFVideo(int id
);
467 void OnAfCancel(int id
);
468 void OnAfCancelAutoMacro(int id
);
469 void OnAfCancelCAFPicture(int id
);
470 void OnAfCancelCAFVideo(int id
);
471 void OnAfNotification(enum aa_afstate noti
);
472 void OnAfNotificationAutoMacro(enum aa_afstate noti
);
473 void OnAfNotificationCAFPicture(enum aa_afstate noti
);
474 void OnAfNotificationCAFVideo(enum aa_afstate noti
);
475 void SetAfStateForService(int newState
);
476 int GetAfStateForService();
477 exif_attribute_t mExifInfo
;
478 void *m_exynosPictureCSC
;
479 void *m_exynosVideoCSC
;
481 int m_jpegEncodingFrameCnt
;
483 camera2_request_queue_src_ops_t
*m_requestQueueOps
;
484 camera2_frame_queue_dst_ops_t
*m_frameQueueOps
;
485 camera2_notify_callback m_notifyCb
;
486 void *m_callbackCookie
;
488 int m_numOfRemainingReqInSvc
;
489 bool m_isRequestQueuePending
;
490 bool m_isRequestQueueNull
;
491 camera2_device_t
*m_halDevice
;
492 static gralloc_module_t
const* m_grallocHal
;
495 camera_hw_info_t m_camera_info
;
497 ion_client m_ionCameraClient
;
499 bool m_isSensorThreadOn
;
500 bool m_isSensorStarted
;
503 int m_need_streamoff
;
507 int m_ispProcessingFrameCnt
;
509 int indexToQueue
[3+1];
514 ExynosBuffer m_resizeBuf
;
515 bool m_recordingEnabled
;
518 bool m_needsRecordBufferInit
;
524 mutable Mutex m_qbufLock
;
528 enum aa_afmode m_afMode
;
529 enum aa_afmode m_afMode2
;
530 bool m_IsAfModeUpdateRequired
;
531 bool m_IsAfTriggerRequired
;
532 bool m_IsAfLockRequired
;
533 int m_serviceAfState
;
536 }; // namespace android