1 /* Copyright (c) 2012-2016, The Linux Foundation. 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 __QCAMERA_STREAM_H__
31 #define __QCAMERA_STREAM_H__
32 
33 // Camera dependencies
34 #include "camera.h"
35 #include "QCameraCmdThread.h"
36 #include "QCameraMem.h"
37 #include "QCameraAllocator.h"
38 
39 extern "C" {
40 #include "mm_camera_interface.h"
41 }
42 
43 namespace qcamera {
44 
45 class QCameraStream;
46 typedef void (*stream_cb_routine)(mm_camera_super_buf_t *frame,
47                                   QCameraStream *stream,
48                                   void *userdata);
49 
50 #define CAMERA_MAX_CONSUMER_BATCH_BUFFER_SIZE   16
51 #define CAMERA_MIN_VIDEO_BATCH_BUFFERS          3
52 
53 
54 class QCameraStream
55 {
56 public:
57     QCameraStream(QCameraAllocator &allocator,
58             uint32_t camHandle, uint32_t chId,
59             mm_camera_ops_t *camOps, cam_padding_info_t *paddingInfo,
60             bool deffered = false, cam_rotation_t online_rotation = ROTATE_0);
61     virtual ~QCameraStream();
62     virtual int32_t init(QCameraHeapMemory *streamInfoBuf,
63             QCameraHeapMemory *miscBuf,
64             uint8_t minStreamBufNum,
65             stream_cb_routine stream_cb,
66             void *userdata,
67             bool bDynallocBuf);
68     virtual int32_t processZoomDone(preview_stream_ops_t *previewWindow,
69                                     cam_crop_data_t &crop_info);
70     virtual int32_t bufDone(uint32_t index);
71     virtual int32_t bufDone(const void *opaque, bool isMetaData);
72     virtual int32_t processDataNotify(mm_camera_super_buf_t *bufs);
73     virtual int32_t start();
74     virtual int32_t stop();
75 
76     /* Used for deffered allocation of buffers */
77     virtual int32_t allocateBuffers();
78     virtual int32_t mapBuffers();
79     virtual int32_t releaseBuffs();
80 
81     static void dataNotifyCB(mm_camera_super_buf_t *recvd_frame, void *userdata);
82     static void dataNotifySYNCCB(mm_camera_super_buf_t *recvd_frame,
83             void *userdata);
84     static void *dataProcRoutine(void *data);
85     static void *BufAllocRoutine(void *data);
getMyHandle()86     uint32_t getMyHandle() const {return mHandle;}
87     bool isTypeOf(cam_stream_type_t type);
88     bool isOrignalTypeOf(cam_stream_type_t type);
89     int32_t getFrameOffset(cam_frame_len_offset_t &offset);
90     int32_t getCropInfo(cam_rect_t &crop);
91     int32_t setCropInfo(cam_rect_t crop);
92     int32_t getFrameDimension(cam_dimension_t &dim);
93     int32_t getFormat(cam_format_t &fmt);
getStreamBufs()94     QCameraMemory *getStreamBufs() {return mStreamBufs;};
getStreamInfoBuf()95     QCameraHeapMemory *getStreamInfoBuf() {return mStreamInfoBuf;};
getMiscBuf()96     QCameraHeapMemory *getMiscBuf() {return mMiscBuf;};
97     uint32_t getMyServerID();
98     cam_stream_type_t getMyType();
99     cam_stream_type_t getMyOriginalType();
100     int32_t acquireStreamBufs();
101 
102     int32_t mapBuf(uint8_t buf_type, uint32_t buf_idx,
103             int32_t plane_idx, int fd, void *buffer, size_t size,
104             mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
105     int32_t mapBufs(cam_buf_map_type_list bufMapList,
106             mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
107     int32_t mapNewBuffer(uint32_t index);
108     int32_t unmapBuf(uint8_t buf_type, uint32_t buf_idx, int32_t plane_idx,
109             mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
110     int32_t setParameter(cam_stream_parm_buffer_t &param);
111     int32_t getParameter(cam_stream_parm_buffer_t &param);
112     int32_t syncRuntimeParams();
getOutputCrop()113     cam_stream_parm_buffer_t getOutputCrop() { return m_OutputCrop;};
getImgProp()114     cam_stream_parm_buffer_t getImgProp() { return m_ImgProp;};
115 
116     static void releaseFrameData(void *data, void *user_data);
117     int32_t configStream();
isDeffered()118     bool isDeffered() const { return mDefferedAllocation; }
isSyncCBEnabled()119     bool isSyncCBEnabled() {return mSyncCBEnabled;};
120     void deleteStream();
121 
getBufferCount()122     uint8_t getBufferCount() { return mNumBufs; }
getChannelHandle()123     uint32_t getChannelHandle() { return mChannelHandle; }
124     int32_t getNumQueuedBuf();
125 
126     uint32_t mDumpFrame;
127     uint32_t mDumpMetaFrame;
128     uint32_t mDumpSkipCnt;
129 
130     void cond_wait();
131     void cond_signal(bool forceExit = false);
132 
133     int32_t setSyncDataCB(stream_cb_routine data_cb);
134     //Stream time stamp. We need this for preview stream to update display
135     nsecs_t mStreamTimestamp;
136 
137     //Frame Buffer will be stored here in case framework batch mode.
138     camera_memory_t *mCurMetaMemory; // Current metadata buffer ptr
139     int8_t mCurBufIndex;             // Buffer count filled in current metadata
140     int8_t mCurMetaIndex;            // Active metadata buffer index
141 
142     nsecs_t mFirstTimeStamp;         // Timestamp of first frame in Metadata.
143 
144     // Buffer storage structure.
145     typedef struct {
146         bool consumerOwned; // Metadata is with Consumer if TRUE
147         uint8_t numBuffers; // Num of buffer need to released
148         uint8_t buf_index[CAMERA_MAX_CONSUMER_BATCH_BUFFER_SIZE];
149     } MetaMemory;
150     MetaMemory mStreamMetaMemory[CAMERA_MIN_VIDEO_BATCH_BUFFERS];
151 
152 private:
153     uint32_t mCamHandle;
154     uint32_t mChannelHandle;
155     uint32_t mHandle; // stream handle from mm-camera-interface
156     mm_camera_ops_t *mCamOps;
157     cam_stream_info_t *mStreamInfo; // ptr to stream info buf
158     mm_camera_stream_mem_vtbl_t mMemVtbl;
159     uint8_t mNumBufs;
160     uint8_t mNumPlaneBufs;
161     uint8_t mNumBufsNeedAlloc;
162     uint8_t *mRegFlags;
163     stream_cb_routine mDataCB;
164     stream_cb_routine mSYNCDataCB;
165     void *mUserData;
166 
167     QCameraQueue     mDataQ;
168     QCameraCmdThread mProcTh; // thread for dataCB
169 
170     QCameraHeapMemory *mStreamInfoBuf;
171     QCameraHeapMemory *mMiscBuf;
172     QCameraMemory *mStreamBufs;
173     QCameraMemory *mStreamBatchBufs;
174     QCameraAllocator &mAllocator;
175     mm_camera_buf_def_t *mBufDefs;
176     mm_camera_buf_def_t *mPlaneBufDefs;
177     cam_frame_len_offset_t mFrameLenOffset;
178     cam_padding_info_t mPaddingInfo;
179     cam_rect_t mCropInfo;
180     cam_rotation_t mOnlineRotation;
181     pthread_mutex_t mCropLock; // lock to protect crop info
182     pthread_mutex_t mParameterLock; // lock to sync access to parameters
183     bool mStreamBufsAcquired;
184     bool m_bActive; // if stream mProcTh is active
185     bool mDynBufAlloc; // allow buf allocation in 2 steps
186     pthread_t mBufAllocPid;
187     mm_camera_map_unmap_ops_tbl_t m_MemOpsTbl;
188     cam_stream_parm_buffer_t m_OutputCrop;
189     cam_stream_parm_buffer_t m_ImgProp;
190 
191     static int32_t get_bufs(
192                      cam_frame_len_offset_t *offset,
193                      uint8_t *num_bufs,
194                      uint8_t **initial_reg_flag,
195                      mm_camera_buf_def_t **bufs,
196                      mm_camera_map_unmap_ops_tbl_t *ops_tbl,
197                      void *user_data);
198 
199     static int32_t get_bufs_deffered(
200             cam_frame_len_offset_t *offset,
201             uint8_t *num_bufs,
202             uint8_t **initial_reg_flag,
203             mm_camera_buf_def_t **bufs,
204             mm_camera_map_unmap_ops_tbl_t *ops_tbl,
205             void *user_data);
206 
207     static int32_t put_bufs(
208                      mm_camera_map_unmap_ops_tbl_t *ops_tbl,
209                      void *user_data);
210 
211     static int32_t put_bufs_deffered(
212             mm_camera_map_unmap_ops_tbl_t *ops_tbl,
213             void *user_data);
214 
215     static int32_t set_config_ops(
216             mm_camera_map_unmap_ops_tbl_t *ops_tbl,
217             void *user_data);
218 
219     static int32_t invalidate_buf(uint32_t index, void *user_data);
220     static int32_t clean_invalidate_buf(uint32_t index, void *user_data);
221 
222     static int32_t backgroundAllocate(void* data);
223     static int32_t backgroundMap(void* data);
224 
225     int32_t getBufs(cam_frame_len_offset_t *offset,
226                      uint8_t *num_bufs,
227                      uint8_t **initial_reg_flag,
228                      mm_camera_buf_def_t **bufs,
229                      mm_camera_map_unmap_ops_tbl_t *ops_tbl);
230     int32_t getBufsDeferred(cam_frame_len_offset_t *offset,
231             uint8_t *num_bufs,
232             uint8_t **initial_reg_flag,
233             mm_camera_buf_def_t **bufs,
234             mm_camera_map_unmap_ops_tbl_t *ops_tbl);
235     int32_t putBufs(mm_camera_map_unmap_ops_tbl_t *ops_tbl);
236     int32_t putBufsDeffered();
237 
238     /* Used for deffered allocation of buffers */
239     int32_t allocateBatchBufs(cam_frame_len_offset_t *offset,
240             uint8_t *num_bufs, uint8_t **initial_reg_flag,
241             mm_camera_buf_def_t **bufs, mm_camera_map_unmap_ops_tbl_t *ops_tbl);
242 
243     int32_t releaseBatchBufs(mm_camera_map_unmap_ops_tbl_t *ops_tbl);
244 
245     int32_t invalidateBuf(uint32_t index);
246     int32_t cleanInvalidateBuf(uint32_t index);
247     int32_t calcOffset(cam_stream_info_t *streamInfo);
248     int32_t unmapStreamInfoBuf();
249     int32_t releaseStreamInfoBuf();
250     int32_t releaseMiscBuf();
251     int32_t mapBufs(QCameraMemory *heapBuf, cam_mapping_buf_type bufType,
252             mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
253     int32_t unMapBuf(QCameraMemory *heapBuf, cam_mapping_buf_type bufType,
254             mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
255 
256     bool mDefferedAllocation;
257 
258     bool wait_for_cond;
259     pthread_mutex_t m_lock;
260     pthread_cond_t m_cond;
261 
262     BackgroundTask mAllocTask;
263     uint32_t mAllocTaskId;
264     BackgroundTask mMapTask;
265     uint32_t mMapTaskId;
266 
267     bool mSyncCBEnabled;
268 };
269 
270 }; // namespace qcamera
271 
272 #endif /* __QCAMERA_STREAM_H__ */
273