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             stream_cb_routine stream_cb,
65             void *userdata,
66             bool bDynallocBuf);
67     virtual int32_t processZoomDone(preview_stream_ops_t *previewWindow,
68                                     cam_crop_data_t &crop_info);
69     virtual int32_t bufDone(uint32_t index);
70     virtual int32_t bufDone(const void *opaque, bool isMetaData);
71     virtual int32_t bufDone(mm_camera_super_buf_t *super_buf);
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     int32_t setBundleInfo();
135     int32_t switchStreamCb(uint32_t camMaster);
136     int32_t processCameraControl(uint32_t camState);
isDualStream()137     bool isDualStream(){return mDualStream;};
138     bool needCbSwitch();
139     bool needFrameSync();
getMyCamType()140     uint32_t getMyCamType() { return mCamType; }
141     //Stream time stamp. We need this for preview stream to update display
142     nsecs_t mStreamTimestamp;
143 
144     //Frame Buffer will be stored here in case framework batch mode.
145     camera_memory_t *mCurMetaMemory; // Current metadata buffer ptr
146     int8_t mCurBufIndex;             // Buffer count filled in current metadata
147     int8_t mCurMetaIndex;            // Active metadata buffer index
148 
149     nsecs_t mFirstTimeStamp;         // Timestamp of first frame in Metadata.
150 
151     // Buffer storage structure.
152     typedef struct {
153         bool consumerOwned; // Metadata is with Consumer if TRUE
154         uint8_t numBuffers; // Num of buffer need to released
155         uint8_t buf_index[CAMERA_MAX_CONSUMER_BATCH_BUFFER_SIZE];
156     } MetaMemory;
157     MetaMemory mStreamMetaMemory[CAMERA_MIN_VIDEO_BATCH_BUFFERS];
158     int32_t handleCacheOps(mm_camera_buf_def_t* buf);
159 
160 private:
161     uint32_t mCamHandle;
162     uint32_t mChannelHandle;
163     uint32_t mHandle; // stream handle from mm-camera-interface
164     uint32_t mActiveCameras;
165     uint32_t mMasterCamera;
166     mm_camera_ops_t *mCamOps;
167     cam_stream_info_t *mStreamInfo; // ptr to stream info buf
168     mm_camera_stream_mem_vtbl_t mMemVtbl;
169     uint8_t mNumBufs;
170     uint8_t mNumPlaneBufs;
171     uint8_t mNumBufsNeedAlloc;
172     uint8_t *mRegFlags;
173     stream_cb_routine mDataCB;
174     stream_cb_routine mSYNCDataCB;
175     void *mUserData;
176 
177     QCameraQueue     mDataQ;
178     QCameraCmdThread mProcTh; // thread for dataCB
179 
180     QCameraHeapMemory *mStreamInfoBuf;
181     QCameraHeapMemory *mMiscBuf;
182     QCameraMemory *mStreamBufs;
183     QCameraMemory *mStreamBatchBufs;
184     QCameraAllocator &mAllocator;
185     mm_camera_buf_def_t *mBufDefs;
186     mm_camera_buf_def_t *mPlaneBufDefs;
187     cam_frame_len_offset_t mFrameLenOffset;
188     cam_padding_info_t mPaddingInfo;
189     cam_rect_t mCropInfo;
190     cam_rotation_t mOnlineRotation;
191     pthread_mutex_t mCropLock; // lock to protect crop info
192     pthread_mutex_t mParameterLock; // lock to sync access to parameters
193     bool mStreamBufsAcquired;
194     bool m_bActive; // if stream mProcTh is active
195     bool mDynBufAlloc; // allow buf allocation in 2 steps
196     pthread_t mBufAllocPid;
197     mm_camera_map_unmap_ops_tbl_t m_MemOpsTbl;
198     cam_stream_parm_buffer_t m_OutputCrop;
199     cam_stream_parm_buffer_t m_ImgProp;
200 
201     static int32_t get_bufs(
202                      cam_frame_len_offset_t *offset,
203                      uint8_t *num_bufs,
204                      uint8_t **initial_reg_flag,
205                      mm_camera_buf_def_t **bufs,
206                      mm_camera_map_unmap_ops_tbl_t *ops_tbl,
207                      void *user_data);
208 
209     static int32_t get_bufs_deffered(
210             cam_frame_len_offset_t *offset,
211             uint8_t *num_bufs,
212             uint8_t **initial_reg_flag,
213             mm_camera_buf_def_t **bufs,
214             mm_camera_map_unmap_ops_tbl_t *ops_tbl,
215             void *user_data);
216 
217     static int32_t put_bufs(
218                      mm_camera_map_unmap_ops_tbl_t *ops_tbl,
219                      void *user_data);
220 
221     static int32_t put_bufs_deffered(
222             mm_camera_map_unmap_ops_tbl_t *ops_tbl,
223             void *user_data);
224 
225     static int32_t set_config_ops(
226             mm_camera_map_unmap_ops_tbl_t *ops_tbl,
227             void *user_data);
228 
229     static int32_t invalidate_buf(uint32_t index, void *user_data);
230     static int32_t clean_invalidate_buf(uint32_t index, void *user_data);
231     static int32_t clean_buf(uint32_t index, void *user_data);
232 
233     static int32_t backgroundAllocate(void* data);
234     static int32_t backgroundMap(void* data);
235 
236     int32_t getBufs(cam_frame_len_offset_t *offset,
237                      uint8_t *num_bufs,
238                      uint8_t **initial_reg_flag,
239                      mm_camera_buf_def_t **bufs,
240                      mm_camera_map_unmap_ops_tbl_t *ops_tbl);
241     int32_t getBufsDeferred(cam_frame_len_offset_t *offset,
242             uint8_t *num_bufs,
243             uint8_t **initial_reg_flag,
244             mm_camera_buf_def_t **bufs,
245             mm_camera_map_unmap_ops_tbl_t *ops_tbl);
246     int32_t putBufs(mm_camera_map_unmap_ops_tbl_t *ops_tbl);
247     int32_t putBufsDeffered();
248 
249     /* Used for deffered allocation of buffers */
250     int32_t allocateBatchBufs(cam_frame_len_offset_t *offset,
251             uint8_t *num_bufs, uint8_t **initial_reg_flag,
252             mm_camera_buf_def_t **bufs, mm_camera_map_unmap_ops_tbl_t *ops_tbl);
253 
254     int32_t releaseBatchBufs(mm_camera_map_unmap_ops_tbl_t *ops_tbl);
255 
256     int32_t invalidateBuf(uint32_t index);
257     int32_t cleanInvalidateBuf(uint32_t index);
258     int32_t cleanBuf(uint32_t index);
259     int32_t calcOffset(cam_stream_info_t *streamInfo);
260     int32_t unmapStreamInfoBuf();
261     int32_t releaseStreamInfoBuf();
262     int32_t releaseMiscBuf();
263     int32_t mapBufs(QCameraMemory *heapBuf, cam_mapping_buf_type bufType,
264             mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
265     int32_t unMapBuf(QCameraMemory *heapBuf, cam_mapping_buf_type bufType,
266             mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
267 
268     bool mDefferedAllocation;
269 
270     bool wait_for_cond;
271     pthread_mutex_t m_lock;
272     pthread_cond_t m_cond;
273 
274     BackgroundTask mAllocTask;
275     uint32_t mAllocTaskId;
276     BackgroundTask mMapTask;
277     uint32_t mMapTaskId;
278     bool mSyncCBEnabled;
279     bool mDualStream;
280     uint32_t mCamType;
281 };
282 
283 }; // namespace qcamera
284 
285 #endif /* __QCAMERA_STREAM_H__ */
286