hardware: samsung_slsi: libcamera2: Add support for metadata
[GitHub/LineageOS/android_hardware_samsung_slsi_exynos5.git] / libcamera2 / ExynosCameraHWInterface2.h
1 /*
2 **
3 ** Copyright 2008, The Android Open Source Project
4 ** Copyright 2012, Samsung Electronics Co. LTD
5 **
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
9 **
10 ** http://www.apache.org/licenses/LICENSE-2.0
11 **
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.
17 */
18
19 /*!
20 * \file ExynosCameraHWInterface2.h
21 * \brief header file for Android Camera API 2.0 HAL
22 * \author Sungjoong Kang(sj3.kang@samsung.com)
23 * \date 2012/07/10
24 *
25 * <b>Revision History: </b>
26 * - 2012/05/31 : Sungjoong Kang(sj3.kang@samsung.com) \n
27 * Initial Release
28 *
29 * - 2012/07/10 : Sungjoong Kang(sj3.kang@samsung.com) \n
30 * 2nd Release
31 *
32 */
33
34 #ifndef EXYNOS_CAMERA_HW_INTERFACE_2_H
35 #define EXYNOS_CAMERA_HW_INTERFACE_2_H
36
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"
48 #include <fcntl.h>
49 #include "fimc-is-metadata.h"
50 #include "ion.h"
51 #include "ExynosExif.h"
52 #include "csc.h"
53 #include "ExynosCamera2.h"
54
55 namespace android {
56
57
58 #define NODE_PREFIX "/dev/video"
59
60 #define NUM_MAX_STREAM_THREAD (5)
61 #define NUM_MAX_DEQUEUED_REQUEST (6)
62 /* #define NUM_MAX_REQUEST_MGR_ENTRY NUM_MAX_DEQUEUED_REQUEST */
63 #define NUM_MAX_REQUEST_MGR_ENTRY (10)
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)
68
69 #define PREVIEW_GSC_NODE_NUM (1)
70 #define PICTURE_GSC_NODE_NUM (2)
71
72 #define SIGNAL_MAIN_REQ_Q_NOT_EMPTY (SIGNAL_THREAD_COMMON_LAST<<1)
73 #define SIGNAL_MAIN_REPROCESS_Q_NOT_EMPTY (SIGNAL_THREAD_COMMON_LAST<<2)
74 #define SIGNAL_MAIN_STREAM_OUTPUT_DONE (SIGNAL_THREAD_COMMON_LAST<<3)
75 #define SIGNAL_SENSOR_START_REQ_PROCESSING (SIGNAL_THREAD_COMMON_LAST<<4)
76 #define SIGNAL_STREAM_GET_BUFFER (SIGNAL_THREAD_COMMON_LAST<<5)
77 #define SIGNAL_STREAM_PUT_BUFFER (SIGNAL_THREAD_COMMON_LAST<<6)
78 #define SIGNAL_STREAM_CHANGE_PARAMETER (SIGNAL_THREAD_COMMON_LAST<<7)
79 #define SIGNAL_THREAD_RELEASE (SIGNAL_THREAD_COMMON_LAST<<8)
80 #define SIGNAL_ISP_START_BAYER_INPUT (SIGNAL_THREAD_COMMON_LAST<<9)
81 #define SIGNAL_ISP_START_BAYER_DEQUEUE (SIGNAL_THREAD_COMMON_LAST<<10)
82
83 #define SIGNAL_STREAM_DATA_COMING (SIGNAL_THREAD_COMMON_LAST<<15)
84
85
86
87 enum sensor_name {
88 SENSOR_NAME_S5K3H2 = 1,
89 SENSOR_NAME_S5K6A3 = 2,
90 SENSOR_NAME_S5K4E5 = 3,
91 SENSOR_NAME_S5K3H7 = 4,
92 SENSOR_NAME_CUSTOM = 5,
93 SENSOR_NAME_END
94 };
95
96
97 typedef struct node_info {
98 int fd;
99 int width;
100 int height;
101 int format;
102 int planes;
103 int buffers;
104 enum v4l2_memory memory;
105 enum v4l2_buf_type type;
106 ion_client ionClient;
107 ExynosBuffer buffer[NUM_MAX_CAMERA_BUFFERS];
108 bool streamOn;
109 } node_info_t;
110
111
112 typedef struct camera_hw_info {
113 int sensor_id;
114
115 node_info_t sensor;
116 node_info_t isp;
117 node_info_t capture;
118
119 /*shot*/ // temp
120 struct camera2_shot_ext dummy_shot;
121
122 } camera_hw_info_t;
123
124 typedef enum request_entry_status {
125 EMPTY,
126 REGISTERED,
127 REQUESTED,
128 CAPTURED
129 } request_entry_status_t;
130
131 typedef struct request_manager_entry {
132 request_entry_status_t status;
133 camera_metadata_t *original_request;
134 struct camera2_shot_ext internal_shot;
135 int output_stream_count;
136 bool dynamic_meta_vaild;
137 } request_manager_entry_t;
138
139 class RequestManager {
140 public:
141 RequestManager(SignalDrivenThread* main_thread);
142 ~RequestManager();
143 int GetNumEntries();
144 bool IsRequestQueueFull();
145
146 void RegisterRequest(camera_metadata_t *new_request);
147 void DeregisterRequest(camera_metadata_t **deregistered_request);
148 bool PrepareFrame(size_t *num_entries, size_t *frame_size,
149 camera_metadata_t **prepared_frame);
150 int MarkProcessingRequest(ExynosBuffer *buf);
151 void NotifyStreamOutput(int frameCnt, int stream_id);
152 void DumpInfoWithIndex(int index);
153 void ApplyDynamicMetadata(struct camera2_shot_ext *shot_ext, int frameCnt);
154 void CheckCompleted(int index);
155 void UpdateIspParameters(struct camera2_shot_ext *shot_ext, int frameCnt);
156 void RegisterTimestamp(int frameCnt, nsecs_t *frameTime);
157 uint64_t GetTimestamp(int frameCnt);
158 int FindFrameCnt(struct camera2_shot_ext * shot_ext);
159 int FindEntryIndexByFrameCnt(int frameCnt);
160 void Dump(void);
161 int GetNextIndex(int index);
162 void SetDefaultParameters(int cropX);
163 void SetInitialSkip(int count);
164 private:
165
166 MetadataConverter *m_metadataConverter;
167 SignalDrivenThread *m_mainThread;
168 int m_numOfEntries;
169 int m_entryInsertionIndex;
170 int m_entryProcessingIndex;
171 int m_entryFrameOutputIndex;
172 request_manager_entry_t entries[NUM_MAX_REQUEST_MGR_ENTRY];
173 int m_completedIndex;
174
175 Mutex m_requestMutex;
176
177 //TODO : alloc dynamically
178 char m_tempFrameMetadataBuf[2000];
179 camera_metadata_t *m_tempFrameMetadata;
180
181 int m_sensorPipelineSkipCnt;
182 int m_cropX;
183
184 };
185
186
187 typedef struct bayer_buf_entry {
188 int status;
189 int reqFrameCnt;
190 nsecs_t timeStamp;
191 } bayer_buf_entry_t;
192
193
194 class BayerBufManager {
195 public:
196 BayerBufManager();
197 ~BayerBufManager();
198 int GetIndexForSensorEnqueue();
199 int MarkSensorEnqueue(int index);
200 int MarkSensorDequeue(int index, int reqFrameCnt, nsecs_t *timeStamp);
201 int GetIndexForIspEnqueue(int *reqFrameCnt);
202 int GetIndexForIspDequeue(int *reqFrameCnt);
203 int MarkIspEnqueue(int index);
204 int MarkIspDequeue(int index);
205 int GetNumOnSensor();
206 int GetNumOnHalFilled();
207 int GetNumOnIsp();
208
209 private:
210 int GetNextIndex(int index);
211
212 int sensorEnqueueHead;
213 int sensorDequeueHead;
214 int ispEnqueueHead;
215 int ispDequeueHead;
216 int numOnSensor;
217 int numOnIsp;
218 int numOnHalFilled;
219 int numOnHalEmpty;
220
221 bayer_buf_entry_t entries[NUM_BAYER_BUFFERS];
222 };
223
224
225 #define NOT_AVAILABLE (0)
226 #define REQUIRES_DQ_FROM_SVC (1)
227 #define ON_DRIVER (2)
228 #define ON_HAL (3)
229 #define ON_SERVICE (4)
230
231 #define BAYER_NOT_AVAILABLE (0)
232 #define BAYER_ON_SENSOR (1)
233 #define BAYER_ON_HAL_FILLED (2)
234 #define BAYER_ON_ISP (3)
235 #define BAYER_ON_SERVICE (4)
236 #define BAYER_ON_HAL_EMPTY (5)
237
238 typedef struct stream_parameters {
239 int streamType;
240 uint32_t outputWidth;
241 uint32_t outputHeight;
242 uint32_t nodeWidth;
243 uint32_t nodeHeight;
244 int outputFormat;
245 int nodeFormat;
246 const camera2_stream_ops_t* streamOps;
247 uint32_t usage;
248 int numHwBuffers;
249 int numSvcBuffers;
250 int numOwnSvcBuffers;
251 int fd;
252 int svcPlanes;
253 int nodePlanes;
254 enum v4l2_memory memory;
255 enum v4l2_buf_type halBuftype;
256 int numSvcBufsInHal;
257 buffer_handle_t svcBufHandle[NUM_MAX_CAMERA_BUFFERS];
258 ExynosBuffer svcBuffers[NUM_MAX_CAMERA_BUFFERS];
259 int svcBufStatus[NUM_MAX_CAMERA_BUFFERS];
260 int svcBufIndex;
261 ion_client ionClient;
262 node_info_t node;
263 } stream_parameters_t;
264
265 typedef struct record_parameters {
266 uint32_t outputWidth;
267 uint32_t outputHeight;
268 int outputFormat;
269 const camera2_stream_ops_t* streamOps;
270 uint32_t usage;
271 int numSvcBuffers;
272 int numOwnSvcBuffers;
273 int svcPlanes;
274 buffer_handle_t svcBufHandle[NUM_MAX_CAMERA_BUFFERS];
275 ExynosBuffer svcBuffers[NUM_MAX_CAMERA_BUFFERS];
276 int svcBufStatus[NUM_MAX_CAMERA_BUFFERS];
277 int svcBufIndex;
278 int numSvcBufsInHal;
279 } record_parameters_t;
280
281 class ExynosCameraHWInterface2 : public virtual RefBase {
282 public:
283 ExynosCameraHWInterface2(int cameraId, camera2_device_t *dev, ExynosCamera2 * camera);
284 virtual ~ExynosCameraHWInterface2();
285
286 virtual void release();
287
288 inline int getCameraId() const;
289
290 virtual int setRequestQueueSrcOps(const camera2_request_queue_src_ops_t *request_src_ops);
291 virtual int notifyRequestQueueNotEmpty();
292 virtual int setFrameQueueDstOps(const camera2_frame_queue_dst_ops_t *frame_dst_ops);
293 virtual int getInProgressCount();
294 virtual int flushCapturesInProgress();
295 virtual int constructDefaultRequest(int request_template, camera_metadata_t **request);
296 virtual int allocateStream(uint32_t width, uint32_t height,
297 int format, const camera2_stream_ops_t *stream_ops,
298 uint32_t *stream_id, uint32_t *format_actual, uint32_t *usage, uint32_t *max_buffers);
299 virtual int registerStreamBuffers(uint32_t stream_id, int num_buffers, buffer_handle_t *buffers);
300 virtual int releaseStream(uint32_t stream_id);
301 virtual int allocateReprocessStream(uint32_t width, uint32_t height,
302 uint32_t format, const camera2_stream_in_ops_t *reprocess_stream_ops,
303 uint32_t *stream_id, uint32_t *consumer_usage, uint32_t *max_buffers);
304 virtual int releaseReprocessStream(uint32_t stream_id);
305 virtual int triggerAction(uint32_t trigger_id, int ext1, int ext2);
306 virtual int setNotifyCallback(camera2_notify_callback notify_cb, void *user);
307 virtual int getMetadataVendorTagOps(vendor_tag_query_ops_t **ops);
308 virtual int dump(int fd);
309 private:
310 class MainThread : public SignalDrivenThread {
311 ExynosCameraHWInterface2 *mHardware;
312 public:
313 MainThread(ExynosCameraHWInterface2 *hw):
314 SignalDrivenThread(),
315 mHardware(hw) {
316 // Start("MainThread", PRIORITY_DEFAULT, 0);
317 }
318 ~MainThread();
319 status_t readyToRunInternal()
320 {
321 return NO_ERROR;
322 }
323 void threadFunctionInternal()
324 {
325 mHardware->m_mainThreadFunc(this);
326 return;
327 }
328 void release(void);
329 };
330
331 class SensorThread : public SignalDrivenThread {
332 ExynosCameraHWInterface2 *mHardware;
333 public:
334 SensorThread(ExynosCameraHWInterface2 *hw):
335 SignalDrivenThread("SensorThread", PRIORITY_DEFAULT, 0),
336 mHardware(hw),
337 m_isBayerOutputEnabled(false) { }
338 ~SensorThread();
339 status_t readyToRunInternal() {
340 mHardware->m_sensorThreadInitialize(this);
341 return NO_ERROR;
342 }
343 void threadFunctionInternal() {
344 mHardware->m_sensorThreadFunc(this);
345 return;
346 }
347 void release(void);
348 //private:
349 bool m_isBayerOutputEnabled;
350 int m_sensorFd;
351 };
352
353 class IspThread : public SignalDrivenThread {
354 ExynosCameraHWInterface2 *mHardware;
355 public:
356 IspThread(ExynosCameraHWInterface2 *hw):
357 SignalDrivenThread("IspThread", PRIORITY_DEFAULT, 0),
358 mHardware(hw) { }
359 ~IspThread();
360 status_t readyToRunInternal() {
361 mHardware->m_ispThreadInitialize(this);
362 return NO_ERROR;
363 }
364 void threadFunctionInternal() {
365 mHardware->m_ispThreadFunc(this);
366 return;
367 }
368 void release(void);
369 //private:
370 int m_ispFd;
371 };
372
373 class StreamThread : public SignalDrivenThread {
374 ExynosCameraHWInterface2 *mHardware;
375 public:
376 StreamThread(ExynosCameraHWInterface2 *hw, uint8_t new_index):
377 SignalDrivenThread("StreamThread", PRIORITY_DEFAULT, 0),
378 mHardware(hw),
379 m_index(new_index) { }
380 ~StreamThread();
381 status_t readyToRunInternal() {
382 mHardware->m_streamThreadInitialize(this);
383 return NO_ERROR;
384 }
385 void threadFunctionInternal() {
386 mHardware->m_streamThreadFunc(this);
387 return;
388 }
389 void setRecordingParameter(record_parameters_t * recordParm);
390 void setParameter(stream_parameters_t * new_parameters);
391 void applyChange(void);
392 void release(void);
393 int findBufferIndex(void * bufAddr);
394
395
396 uint8_t m_index;
397 bool m_activated;
398 //private:
399 stream_parameters_t m_parameters;
400 stream_parameters_t *m_tempParameters;
401 record_parameters_t m_recordParameters;
402 bool m_isBufferInit;
403 bool m_releasing;
404 };
405
406 sp<MainThread> m_mainThread;
407 sp<SensorThread> m_sensorThread;
408 sp<IspThread> m_ispThread;
409 sp<StreamThread> m_streamThreads[NUM_MAX_STREAM_THREAD];
410
411
412
413 RequestManager *m_requestManager;
414 BayerBufManager *m_BayerManager;
415 ExynosCamera2 *m_camera2;
416
417 void m_mainThreadFunc(SignalDrivenThread * self);
418 void m_sensorThreadFunc(SignalDrivenThread * self);
419 void m_sensorThreadInitialize(SignalDrivenThread * self);
420 void m_ispThreadFunc(SignalDrivenThread * self);
421 void m_ispThreadInitialize(SignalDrivenThread * self);
422 void m_streamThreadFunc(SignalDrivenThread * self);
423 void m_streamThreadInitialize(SignalDrivenThread * self);
424
425 void m_getAlignedYUVSize(int colorFormat, int w, int h,
426 ExynosBuffer *buf);
427 bool m_getRatioSize(int src_w, int src_h,
428 int dst_w, int dst_h,
429 int *crop_x, int *crop_y,
430 int *crop_w, int *crop_h,
431 int zoom);
432 int createIonClient(ion_client ionClient);
433 int deleteIonClient(ion_client ionClient);
434
435 int allocCameraMemory(ion_client ionClient, ExynosBuffer *buf, int iMemoryNum);
436 void freeCameraMemory(ExynosBuffer *buf, int iMemoryNum);
437 void initCameraMemory(ExynosBuffer *buf, int iMemoryNum);
438
439 void DumpInfoWithShot(struct camera2_shot_ext * shot_ext);
440 bool yuv2Jpeg(ExynosBuffer *yuvBuf,
441 ExynosBuffer *jpegBuf,
442 ExynosRect *rect);
443 exif_attribute_t mExifInfo;
444 void *m_exynosPictureCSC;
445 void *m_exynosVideoCSC;
446
447 int m_jpegEncodingFrameCnt;
448
449 camera2_request_queue_src_ops_t *m_requestQueueOps;
450 camera2_frame_queue_dst_ops_t *m_frameQueueOps;
451 camera2_notify_callback m_notifyCb;
452 void *m_callbackCookie;
453
454 int m_numOfRemainingReqInSvc;
455 bool m_isRequestQueuePending;
456 bool m_isRequestQueueNull;
457 camera2_device_t *m_halDevice;
458 static gralloc_module_t const* m_grallocHal;
459
460
461 camera_hw_info_t m_camera_info;
462
463 ion_client m_ionCameraClient;
464
465 bool m_isSensorThreadOn;
466 bool m_isSensorStarted;
467
468
469
470 bool m_initFlag1;
471 bool m_initFlag2;
472 int m_ispProcessingFrameCnt;
473
474 int indexToQueue[3+1];
475 int m_fd_scp;
476
477 bool m_scp_flushing;
478 bool m_closing;
479 ExynosBuffer m_resizeBuf;
480 bool m_recordingEnabled;
481 int m_previewOutput;
482 int m_recordOutput;
483 bool m_needsRecordBufferInit;
484 int lastFrameCnt;
485 int m_cameraId;
486 bool m_scp_closing;
487 bool m_scp_closed;
488
489 };
490
491 }; // namespace android
492
493 #endif