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 
39 extern "C" {
40 #include <mm_camera_interface.h>
41 }
42 
43 using namespace android;
44 
45 #define F_NUMBER_DECIMAL_PRECISION  100
46 namespace qcamera {
47 
48 typedef void (*channel_cb_routine)(mm_camera_super_buf_t *metadata,
49                                 camera3_stream_buffer_t *buffer,
50                                 uint32_t frame_number, void *userdata);
51 
52 class QCamera3Channel
53 {
54 public:
55     QCamera3Channel(uint32_t cam_handle,
56                    mm_camera_ops_t *cam_ops,
57                    channel_cb_routine cb_routine,
58                    cam_padding_info_t *paddingInfo,
59                    void *userData);
60     QCamera3Channel();
61     virtual ~QCamera3Channel();
62 
63     int32_t addStream(cam_stream_type_t streamType,
64                               cam_format_t streamFormat,
65                               cam_dimension_t streamDim,
66                               uint8_t minStreamBufnum);
67     int32_t start();
68     int32_t stop();
69     int32_t bufDone(mm_camera_super_buf_t *recvd_frame);
70 
71     uint32_t getStreamTypeMask();
72 
73     virtual int32_t registerBuffers(uint32_t num_buffers,
74                         buffer_handle_t **buffers) = 0;
75     virtual int32_t initialize() = 0;
request(buffer_handle_t *,uint32_t)76     virtual int32_t request(buffer_handle_t * /*buffer*/,
77                 uint32_t /*frameNumber*/){ return 0;};
request(buffer_handle_t *,uint32_t,jpeg_settings_t *,mm_camera_buf_def_t *,QCamera3Channel *)78     virtual int32_t request(buffer_handle_t * /*buffer*/,
79                 uint32_t /*frameNumber*/,
80                 jpeg_settings_t* /*mJpegSettings*/,
81                 mm_camera_buf_def_t* /*pInputBuffer*/,
82                 QCamera3Channel* /*pInputChannel*/){ return 0;};
83     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
84                             QCamera3Stream *stream) = 0;
85 
86     virtual QCamera3Memory *getStreamBufs(uint32_t len) = 0;
87     virtual void putStreamBufs() = 0;
88 
89     QCamera3Stream *getStreamByHandle(uint32_t streamHandle);
getMyHandle()90     uint32_t getMyHandle() const {return m_handle;};
getNumOfStreams()91     uint8_t getNumOfStreams() const {return m_numStreams;};
92     QCamera3Stream *getStreamByIndex(uint8_t index);
93 
94     static void streamCbRoutine(mm_camera_super_buf_t *super_frame,
95                 QCamera3Stream *stream, void *userdata);
96     void *mUserData;
97     cam_padding_info_t *mPaddingInfo;
98     QCamera3Stream *mStreams[MAX_STREAM_NUM_IN_BUNDLE];
99     uint8_t m_numStreams;
100 protected:
101 
102    virtual int32_t init(mm_camera_channel_attr_t *attr,
103                          mm_camera_buf_notify_t dataCB);
104     int32_t allocateStreamInfoBuf(camera3_stream_t *stream);
105 
106     uint32_t m_camHandle;
107     mm_camera_ops_t *m_camOps;
108     bool m_bIsActive;
109 
110     uint32_t m_handle;
111 
112 
113     mm_camera_buf_notify_t mDataCB;
114 
115 
116     QCamera3HeapMemory *mStreamInfoBuf;
117     channel_cb_routine mChannelCB;
118     //cam_padding_info_t *mPaddingInfo;
119 };
120 
121 /* QCamera3RegularChannel is used to handle all streams that are directly
122  * generated by hardware and given to frameworks without any postprocessing at HAL.
123  * Examples are: all IMPLEMENTATION_DEFINED streams, CPU_READ streams. */
124 class QCamera3RegularChannel : public QCamera3Channel
125 {
126 public:
127     QCamera3RegularChannel(uint32_t cam_handle,
128                     mm_camera_ops_t *cam_ops,
129                     channel_cb_routine cb_routine,
130                     cam_padding_info_t *paddingInfo,
131                     void *userData,
132                     camera3_stream_t *stream);
133     QCamera3RegularChannel(uint32_t cam_handle,
134                     mm_camera_ops_t *cam_ops,
135                     channel_cb_routine cb_routine,
136                     cam_padding_info_t *paddingInfo,
137                     void *userData,
138                     camera3_stream_t *stream,
139                     uint32_t width, uint32_t height);
140     virtual ~QCamera3RegularChannel();
141 
142     virtual int32_t initialize();
143     virtual int32_t request(buffer_handle_t *buffer, uint32_t frameNumber);
144     virtual int32_t registerBuffers(uint32_t num_buffers,
145                                 buffer_handle_t **buffers);
146     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
147                                             QCamera3Stream *stream);
148 
149     virtual QCamera3Memory *getStreamBufs(uint32_t le);
150     virtual void putStreamBufs();
151     mm_camera_buf_def_t* getInternalFormatBuffer(buffer_handle_t* buffer);
152 
153 public:
154     static int kMaxBuffers;
155 private:
156     camera3_stream_t *mCamera3Stream;
157     uint32_t mNumBufs;
158     buffer_handle_t **mCamera3Buffers;
159 
160     QCamera3GrallocMemory *mMemory;
161     // width and height of internal stream may be different than what's
162     // specified in camera3_stream_t. For example: ZSL stream size is
163     // always the active region size, but internally we use the JPEG
164     // size.
165     uint32_t mWidth, mHeight;
166 };
167 
168 /* QCamera3MetadataChannel is for metadata stream generated by camera daemon. */
169 class QCamera3MetadataChannel : public QCamera3Channel
170 {
171 public:
172     QCamera3MetadataChannel(uint32_t cam_handle,
173                     mm_camera_ops_t *cam_ops,
174                     channel_cb_routine cb_routine,
175                     cam_padding_info_t *paddingInfo,
176                     void *userData);
177     virtual ~QCamera3MetadataChannel();
178 
179     virtual int32_t initialize();
180 
181     virtual int32_t request(buffer_handle_t *buffer, uint32_t frameNumber);
182     virtual int32_t registerBuffers(uint32_t num_buffers,
183                 buffer_handle_t **buffers);
184     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
185                             QCamera3Stream *stream);
186 
187     virtual QCamera3Memory *getStreamBufs(uint32_t le);
188     virtual void putStreamBufs();
189 
190 private:
191     QCamera3HeapMemory *mMemory;
192 };
193 
194 /* QCamera3PicChannel is for JPEG stream, which contains a YUV stream generated
195  * by the hardware, and encoded to a JPEG stream */
196 class QCamera3PicChannel : public QCamera3Channel
197 {
198 public:
199     QCamera3PicChannel(uint32_t cam_handle,
200             mm_camera_ops_t *cam_ops,
201             channel_cb_routine cb_routine,
202             cam_padding_info_t *paddingInfo,
203             void *userData,
204             camera3_stream_t *stream);
205     ~QCamera3PicChannel();
206 
207     virtual int32_t initialize();
208 
209     virtual int32_t request(buffer_handle_t *buffer,
210             uint32_t frameNumber, jpeg_settings_t* mJpegSettings,
211             mm_camera_buf_def_t* pInputBuffer,QCamera3Channel* pInputChannel);
212     virtual int32_t registerBuffers(uint32_t num_buffers,
213             buffer_handle_t **buffers);
214     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
215             QCamera3Stream *stream);
216 
217     virtual QCamera3Memory *getStreamBufs(uint32_t le);
218     virtual void putStreamBufs();
isWNREnabled()219     bool isWNREnabled() {return m_bWNROn;};
220     bool needOnlineRotation();
221     void getThumbnailSize(cam_dimension_t &dim);
222     int getJpegQuality();
223     int getJpegRotation();
224     bool isRawSnapshot();
225     QCamera3Exif *getExifData();
226     static void jpegEvtHandle(jpeg_job_status_t status,
227             uint32_t /*client_hdl*/,
228             uint32_t jobId,
229             mm_jpeg_output_t *p_output,
230             void *userdata);
231     static void dataNotifyCB(mm_camera_super_buf_t *recvd_frame,
232             void *userdata);
233     void queueMetadata(mm_camera_super_buf_t *metadata_buf);
234 
235 public:
236     static int kMaxBuffers;
237     QCamera3PostProcessor m_postprocessor; // post processor
238 private:
239     camera3_stream_t *mCamera3Stream;
240     uint32_t mNumBufs;
241     buffer_handle_t **mCamera3Buffers;
242     jpeg_settings_t* mJpegSettings;
243     int32_t mCurrentBufIndex;
244     bool m_bWNROn;
245 
246 
247     QCamera3GrallocMemory *mMemory;
248     QCamera3HeapMemory *mYuvMemory;
249 };
250 
251 // reprocess channel class
252 class QCamera3ReprocessChannel : public QCamera3Channel
253 {
254 public:
255     QCamera3ReprocessChannel(uint32_t cam_handle,
256                             mm_camera_ops_t *cam_ops,
257                             channel_cb_routine cb_routine,
258                             cam_padding_info_t *paddingInfo,
259                             void *userData, void *ch_hdl);
260     QCamera3ReprocessChannel();
261     virtual ~QCamera3ReprocessChannel();
262     // online reprocess
263     int32_t doReprocess(mm_camera_super_buf_t *frame,
264                         mm_camera_super_buf_t *meta_frame);
265     // offline reprocess
266     int32_t doReprocess(int buf_fd, uint32_t buf_length, int32_t &ret_val,
267                         mm_camera_super_buf_t *meta_buf);
268     virtual int32_t registerBuffers(uint32_t num_buffers, buffer_handle_t **buffers);
269     virtual QCamera3Memory *getStreamBufs(uint32_t len);
270     virtual void putStreamBufs();
271     virtual int32_t initialize();
272     virtual void streamCbRoutine(mm_camera_super_buf_t *super_frame,
273                             QCamera3Stream *stream);
274     static void dataNotifyCB(mm_camera_super_buf_t *recvd_frame,
275                                        void* userdata);
276     int32_t addReprocStreamsFromSource(cam_pp_feature_config_t &config,
277                                        QCamera3Channel *pSrcChannel,
278                                        QCamera3Channel *pMetaChannel);
279     QCamera3Stream *getStreamBySourceHandle(uint32_t srcHandle);
280 public:
281     void *picChHandle;
282 private:
283     uint32_t mSrcStreamHandles[MAX_STREAM_NUM_IN_BUNDLE];
284     QCamera3Channel *m_pSrcChannel; // ptr to source channel for reprocess
285     QCamera3Channel *m_pMetaChannel;
286     mm_camera_super_buf_t *m_metaFrame;
287     QCamera3HeapMemory *mMemory;
288 };
289 
290 }; // namespace qcamera
291 
292 #endif /* __QCAMERA_CHANNEL_H__ */
293