1 /* Copyright (c) 2012-2013, The Linux Foundataion. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #ifndef __QCAMERA3_CHANNEL_H__
31 #define __QCAMERA3_CHANNEL_H__
32 
33 #include <hardware/camera3.h>
34 #include "QCamera3Stream.h"
35 #include "QCamera3Mem.h"
36 #include "QCamera3PostProc.h"
37 #include "QCamera3HALHeader.h"
38 #include "utils/Vector.h"
39 #include <utils/List.h>
40 
41 extern "C" {
42 #include <mm_camera_interface.h>
43 }
44 
45 using namespace android;
46 
47 namespace qcamera {
48 
49 typedef void (*channel_cb_routine)(mm_camera_super_buf_t *metadata,
50                                 camera3_stream_buffer_t *buffer,
51                                 uint32_t frame_number, void *userdata);
52 
53 class QCamera3Channel
54 {
55 public:
56     QCamera3Channel(uint32_t cam_handle,
57                    mm_camera_ops_t *cam_ops,
58                    channel_cb_routine cb_routine,
59                    cam_padding_info_t *paddingInfo,
60                    uint32_t postprocess_mask,
61                    void *userData);
62     QCamera3Channel();
63     virtual ~QCamera3Channel();
64 
65     int32_t addStream(cam_stream_type_t streamType,
66                       cam_format_t streamFormat,
67                       cam_dimension_t streamDim,
68                       uint8_t minStreamBufnum,
69                       uint32_t postprocessMask,
70                       cam_is_type_t isType);
71     virtual int32_t start();
72     virtual int32_t stop();
73     int32_t bufDone(mm_camera_super_buf_t *recvd_frame);
74 
75     uint32_t getStreamTypeMask();
76     uint32_t getStreamID(uint32_t streamMask);
77     virtual int32_t initialize(cam_is_type_t isType,
78             uint8_t intent) = 0;
request(buffer_handle_t *,uint32_t)79     virtual int32_t request(buffer_handle_t * /*buffer*/,
80                 uint32_t /*frameNumber*/){ return 0;};
request(buffer_handle_t *,uint32_t,camera3_stream_buffer_t *,metadata_buffer_t *)81     virtual int32_t request(buffer_handle_t * /*buffer*/,
82                 uint32_t /*frameNumber*/,
83                 camera3_stream_buffer_t* /*pInputBuffer*/,
84                 metadata_buffer_t* /*metadata*/){ return 0;};
85     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
86                             QCamera3Stream *stream) = 0;
87 
88     virtual int32_t registerBuffer(buffer_handle_t *buffer, cam_is_type_t isType,
89             uint8_t intent) = 0;
90     virtual QCamera3Memory *getStreamBufs(uint32_t len) = 0;
91     virtual void putStreamBufs() = 0;
92 
93     QCamera3Stream *getStreamByHandle(uint32_t streamHandle);
getMyHandle()94     uint32_t getMyHandle() const {return m_handle;};
getNumOfStreams()95     uint8_t getNumOfStreams() const {return m_numStreams;};
96     QCamera3Stream *getStreamByIndex(uint8_t index);
97 
98     static void streamCbRoutine(mm_camera_super_buf_t *super_frame,
99                 QCamera3Stream *stream, void *userdata);
100     void dumpYUV(mm_camera_buf_def_t *frame, cam_dimension_t dim,
101                  cam_frame_len_offset_t offset, uint8_t name);
102 
103     void *mUserData;
104     cam_padding_info_t *mPaddingInfo;
105     QCamera3Stream *mStreams[MAX_STREAM_NUM_IN_BUNDLE];
106     uint8_t m_numStreams;
107 protected:
108 
109    virtual int32_t init(mm_camera_channel_attr_t *attr,
110                          mm_camera_buf_notify_t dataCB);
111     int32_t allocateStreamInfoBuf(camera3_stream_t *stream);
112 
113     uint32_t m_camHandle;
114     mm_camera_ops_t *m_camOps;
115     bool m_bIsActive;
116 
117     uint32_t m_handle;
118 
119 
120     mm_camera_buf_notify_t mDataCB;
121 
122 
123     QCamera3HeapMemory *mStreamInfoBuf;
124     channel_cb_routine mChannelCB;
125     //cam_padding_info_t *mPaddingInfo;
126     uint32_t mPostProcMask;
127     uint8_t mYUVDump;
128     cam_is_type_t mIsType;
129     uint8_t mIntent;
130 };
131 
132 /* QCamera3RegularChannel is used to handle all streams that are directly
133  * generated by hardware and given to frameworks without any postprocessing at HAL.
134  * Examples are: all IMPLEMENTATION_DEFINED streams, CPU_READ streams. */
135 class QCamera3RegularChannel : public QCamera3Channel
136 {
137 public:
138     QCamera3RegularChannel(uint32_t cam_handle,
139                     mm_camera_ops_t *cam_ops,
140                     channel_cb_routine cb_routine,
141                     cam_padding_info_t *paddingInfo,
142                     void *userData,
143                     camera3_stream_t *stream,
144                     cam_stream_type_t stream_type,
145                     uint32_t postprocess_mask);
146     QCamera3RegularChannel(uint32_t cam_handle,
147                     mm_camera_ops_t *cam_ops,
148                     channel_cb_routine cb_routine,
149                     cam_padding_info_t *paddingInfo,
150                     void *userData,
151                     camera3_stream_t *stream,
152                     cam_stream_type_t stream_type,
153                     uint32_t postprocess_mask,
154                     uint32_t width, uint32_t height);
155     virtual ~QCamera3RegularChannel();
156 
157     virtual int32_t start();
158     virtual int32_t initialize(cam_is_type_t isType,
159             uint8_t intent);
160     virtual int32_t request(buffer_handle_t *buffer, uint32_t frameNumber);
161     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
162                                             QCamera3Stream *stream);
163 
164     virtual QCamera3Memory *getStreamBufs(uint32_t le);
165     virtual void putStreamBufs();
166     virtual int32_t registerBuffer(buffer_handle_t *buffer, cam_is_type_t isType,
167             uint8_t intent);
168 
169 public:
170     static int kMaxBuffers;
171 protected:
172     QCamera3GrallocMemory mMemory;
173 private:
174     int32_t initialize(struct private_handle_t *priv_handle);
175 
176     camera3_stream_t *mCamera3Stream;
177     uint32_t mNumBufs;
178     cam_stream_type_t mStreamType; // Stream type
179     uint32_t mWidth, mHeight;
180     uint8_t mIntent;
181 };
182 
183 /* QCamera3MetadataChannel is for metadata stream generated by camera daemon. */
184 class QCamera3MetadataChannel : public QCamera3Channel
185 {
186 public:
187     QCamera3MetadataChannel(uint32_t cam_handle,
188                     mm_camera_ops_t *cam_ops,
189                     channel_cb_routine cb_routine,
190                     cam_padding_info_t *paddingInfo,
191                     uint32_t postprocess_mask,
192                     void *userData);
193     virtual ~QCamera3MetadataChannel();
194 
195     virtual int32_t initialize(cam_is_type_t isType,
196             uint8_t intent);
197 
198     virtual int32_t request(buffer_handle_t *buffer, uint32_t frameNumber);
199     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
200                             QCamera3Stream *stream);
201 
202     virtual QCamera3Memory *getStreamBufs(uint32_t le);
203     virtual void putStreamBufs();
registerBuffer(buffer_handle_t *,cam_is_type_t,uint8_t)204     virtual int32_t registerBuffer(buffer_handle_t * /*buffer*/, cam_is_type_t /*isType*/,
205             uint8_t /*intent*/)
206             { return NO_ERROR; };
207 
208 private:
209     QCamera3HeapMemory *mMemory;
210 };
211 
212 /* QCamera3RawChannel is for opaqueu/cross-platform raw stream containing
213  * vendor specific bayer data or 16-bit unpacked bayer data */
214 class QCamera3RawChannel : public QCamera3RegularChannel
215 {
216 public:
217     QCamera3RawChannel(uint32_t cam_handle,
218                     mm_camera_ops_t *cam_ops,
219                     channel_cb_routine cb_routine,
220                     cam_padding_info_t *paddingInfo,
221                     void *userData,
222                     camera3_stream_t *stream,
223                     uint32_t postprocess_mask,
224                     bool raw_16 = false);
225     virtual ~QCamera3RawChannel();
226 
227     virtual int32_t initialize(cam_is_type_t isType,
228             uint8_t intent);
229 
230     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
231                             QCamera3Stream *stream);
232 
233 public:
234     static int kMaxBuffers;
235 
236 private:
237     bool mRawDump;
238     bool mIsRaw16;
239 
240     void dumpRawSnapshot(mm_camera_buf_def_t *frame);
241     void convertLegacyToRaw16(mm_camera_buf_def_t *frame);
242     void convertMipiToRaw16(mm_camera_buf_def_t *frame);
243 };
244 
245 /*
246  * QCamera3RawDumpChannel is for internal use only for Raw dump
247  */
248 
249 class QCamera3RawDumpChannel : public QCamera3Channel
250 {
251 public:
252     QCamera3RawDumpChannel(uint32_t cam_handle,
253                     mm_camera_ops_t *cam_ops,
254                     cam_dimension_t rawDumpSize,
255                     cam_padding_info_t *paddingInfo,
256                     void *userData,
257                     uint32_t postprocess_mask);
258     virtual ~QCamera3RawDumpChannel();
259     virtual int32_t initialize(cam_is_type_t isType, uint8_t intent);
260     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
261                             QCamera3Stream *stream);
262     virtual QCamera3Memory *getStreamBufs(uint32_t le);
263     virtual void putStreamBufs();
registerBuffer(buffer_handle_t *,cam_is_type_t,uint8_t)264     virtual int32_t registerBuffer(buffer_handle_t * /*buffer*/, cam_is_type_t /*isType*/,
265             uint8_t /*intent*/)
266             { return NO_ERROR; };
267     virtual int32_t request(buffer_handle_t *buffer, uint32_t frameNumber);
268     void dumpRawSnapshot(mm_camera_buf_def_t *frame);
269 
270 public:
271     static int kMaxBuffers;
272     cam_dimension_t mDim;
273 
274 private:
275     bool mRawDump;
276     QCamera3HeapMemory *mMemory;
277 };
278 
279 /* QCamera3PicChannel is for JPEG stream, which contains a YUV stream generated
280  * by the hardware, and encoded to a JPEG stream */
281 class QCamera3PicChannel : public QCamera3Channel
282 {
283 public:
284     QCamera3PicChannel(uint32_t cam_handle,
285             mm_camera_ops_t *cam_ops,
286             channel_cb_routine cb_routine,
287             cam_padding_info_t *paddingInfo,
288             void *userData,
289             camera3_stream_t *stream,
290             uint32_t postprocess_mask,
291             bool is4KVideo,
292             QCamera3Channel *metadataChannel);
293     ~QCamera3PicChannel();
294 
295     virtual int32_t initialize(cam_is_type_t isType,
296             uint8_t intent);
297     virtual int32_t stop();
298     virtual int32_t request(buffer_handle_t *buffer,
299             uint32_t frameNumber,
300             camera3_stream_buffer_t* pInputBuffer,
301             metadata_buffer_t* metadata);
302     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
303             QCamera3Stream *stream);
304 
305     virtual QCamera3Memory *getStreamBufs(uint32_t le);
306     virtual void putStreamBufs();
307 
isWNREnabled()308     bool isWNREnabled() {return m_bWNROn;};
309     bool needOnlineRotation();
310     int32_t metadataBufDone(mm_camera_super_buf_t *recvd_frame);
311     QCamera3Exif *getExifData(metadata_buffer_t *metadata,
312             jpeg_settings_t *jpeg_settings);
313     void overrideYuvSize(uint32_t width, uint32_t height);
314     static void jpegEvtHandle(jpeg_job_status_t status,
315             uint32_t /*client_hdl*/,
316             uint32_t jobId,
317             mm_jpeg_output_t *p_output,
318             void *userdata);
319     static void dataNotifyCB(mm_camera_super_buf_t *recvd_frame,
320             void *userdata);
321     virtual int32_t registerBuffer(buffer_handle_t *buffer, cam_is_type_t isType,
322             uint8_t intent);
323     int32_t queueReprocMetadata(mm_camera_super_buf_t *metadata);
324 
325 private:
326     int32_t queueJpegSetting(int32_t out_buf_index, metadata_buffer_t *metadata);
327 
328 public:
329     static int kMaxBuffers;
330     QCamera3PostProcessor m_postprocessor; // post processor
331     cam_dimension_t m_max_pic_dim;
332 
333 private:
334     camera3_stream_t *mCamera3Stream;
335     uint32_t mNumBufsRegistered;
336     uint32_t mNumSnapshotBufs;
337     uint32_t mYuvWidth, mYuvHeight;
338     int32_t mCurrentBufIndex;
339     cam_stream_type_t mStreamType;
340     cam_format_t mStreamFormat;
341     bool m_bWNROn;
342     bool mPostProcStarted;
343     bool mInputBufferConfig;   // Set when the picture channel is configured
344                                // for processing input(framework) buffers
345 
346     QCamera3GrallocMemory mMemory;
347     QCamera3HeapMemory *mYuvMemory;
348     QCamera3Channel *m_pMetaChannel;
349     mm_camera_super_buf_t *mMetaFrame;
350     QCamera3GrallocMemory mOfflineMemory;
351     QCamera3HeapMemory mOfflineMetaMemory;
352 
353     // Keep a list of free buffers
354     Mutex mFreeBuffersLock;
355     List<uint32_t> mFreeBufferList;
356 };
357 
358 // reprocess channel class
359 class QCamera3ReprocessChannel : public QCamera3Channel
360 {
361 public:
362     QCamera3ReprocessChannel(uint32_t cam_handle,
363                             mm_camera_ops_t *cam_ops,
364                             channel_cb_routine cb_routine,
365                             cam_padding_info_t *paddingInfo,
366                             uint32_t postprocess_mask,
367                             void *userData, void *ch_hdl);
368     QCamera3ReprocessChannel();
369     virtual ~QCamera3ReprocessChannel();
370     // offline reprocess
371     int32_t doReprocessOffline(qcamera_fwk_input_pp_data_t *frame);
372     int32_t doReprocess(int buf_fd, uint32_t buf_length, int32_t &ret_val,
373                         mm_camera_super_buf_t *meta_buf);
374     int32_t extractFrameCropAndRotation(mm_camera_super_buf_t *frame,
375             mm_camera_buf_def_t *meta_buffer,
376             jpeg_settings_t *jpeg_settings,
377             qcamera_fwk_input_pp_data_t &fwk_frame);
378     int32_t extractCrop(qcamera_fwk_input_pp_data_t *frame);
379     virtual QCamera3Memory *getStreamBufs(uint32_t len);
380     virtual void putStreamBufs();
381     virtual int32_t initialize(cam_is_type_t isType,
382             uint8_t intent);
383     int32_t unmapOfflineBuffers(bool all);
384     virtual int32_t stop();
385     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
386                             QCamera3Stream *stream);
387     static void dataNotifyCB(mm_camera_super_buf_t *recvd_frame,
388                                        void* userdata);
389     int32_t addReprocStreamsFromSource(cam_pp_feature_config_t &pp_config,
390            const reprocess_config_t &src_config,
391            cam_is_type_t is_type,
392            QCamera3Channel *pMetaChannel);
393     QCamera3Stream *getStreamBySrcHandle(uint32_t srcHandle);
394     QCamera3Stream *getSrcStreamBySrcHandle(uint32_t srcHandle);
registerBuffer(buffer_handle_t *,cam_is_type_t,uint8_t)395     virtual int32_t registerBuffer(buffer_handle_t * /*buffer*/, cam_is_type_t /*isType*/,
396             uint8_t /*intent*/)
397             { return NO_ERROR; };
398 
399 public:
400     void *picChHandle;
401 private:
402     typedef struct {
403         QCamera3Stream *stream;
404         cam_mapping_buf_type type;
405         int index;
406     } OfflineBuffer;
407 
408     android::List<OfflineBuffer> mOfflineBuffers;
409     android::List<OfflineBuffer> mOfflineMetaBuffers;
410     int32_t mOfflineBuffersIndex;
411     int32_t mOfflineMetaIndex;
412     uint32_t mSrcStreamHandles[MAX_STREAM_NUM_IN_BUNDLE];
413     QCamera3Channel *m_pSrcChannel; // ptr to source channel for reprocess
414     QCamera3Channel *m_pMetaChannel;
415     QCamera3HeapMemory *mMemory;
416 };
417 
418 /* QCamera3SupportChannel is for HAL internal consumption only */
419 class QCamera3SupportChannel : public QCamera3Channel
420 {
421 public:
422     QCamera3SupportChannel(uint32_t cam_handle,
423                     mm_camera_ops_t *cam_ops,
424                     cam_padding_info_t *paddingInfo,
425                     uint32_t postprocess_mask,
426                     void *userData);
427     virtual ~QCamera3SupportChannel();
428 
429     virtual int32_t initialize(cam_is_type_t isType,
430             uint8_t intent);
431 
432     virtual int32_t request(buffer_handle_t *buffer, uint32_t frameNumber);
433     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
434                             QCamera3Stream *stream);
435 
436     virtual QCamera3Memory *getStreamBufs(uint32_t le);
437     virtual void putStreamBufs();
registerBuffer(buffer_handle_t *,cam_is_type_t,uint8_t)438     virtual int32_t registerBuffer(buffer_handle_t * /*buffer*/, cam_is_type_t /*isType*/,
439             uint8_t /*intent*/)
440             { return NO_ERROR; };
441 
442     static cam_dimension_t kDim;
443 private:
444     QCamera3HeapMemory *mMemory;
445 };
446 
447 }; // namespace qcamera
448 
449 #endif /* __QCAMERA_CHANNEL_H__ */
450