1 /* Copyright (c) 2012-2015, 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 __QCAMERA_STREAM_H__
31 #define __QCAMERA_STREAM_H__
32 
33 #include <hardware/camera.h>
34 #include "QCameraCmdThread.h"
35 #include "QCameraMem.h"
36 #include "QCameraAllocator.h"
37 
38 extern "C" {
39 #include <mm_camera_interface.h>
40 }
41 
42 namespace qcamera {
43 
44 class QCameraStream;
45 typedef void (*stream_cb_routine)(mm_camera_super_buf_t *frame,
46                                   QCameraStream *stream,
47                                   void *userdata);
48 
49 class QCameraStream
50 {
51 public:
52     QCameraStream(QCameraAllocator &allocator,
53             uint32_t camHandle, uint32_t chId,
54             mm_camera_ops_t *camOps, cam_padding_info_t *paddingInfo,
55             bool deffered = false, cam_rotation_t online_rotation = ROTATE_0);
56     virtual ~QCameraStream();
57     virtual int32_t init(QCameraHeapMemory *streamInfoBuf,
58             QCameraHeapMemory *miscBuf,
59             uint8_t minStreamBufNum,
60             stream_cb_routine stream_cb,
61             void *userdata,
62             bool bDynallocBuf);
63     virtual int32_t processZoomDone(preview_stream_ops_t *previewWindow,
64                                     cam_crop_data_t &crop_info);
65     virtual int32_t bufDone(uint32_t index);
66     virtual int32_t bufDone(const void *opaque, bool isMetaData);
67     virtual int32_t processDataNotify(mm_camera_super_buf_t *bufs);
68     virtual int32_t start();
69     virtual int32_t stop();
70 
71     /* Used for deffered allocation of buffers */
72     virtual int32_t allocateBuffers();
73     virtual int32_t releaseBuffs();
74 
75     static void dataNotifyCB(mm_camera_super_buf_t *recvd_frame, void *userdata);
76     static void *dataProcRoutine(void *data);
77     static void *BufAllocRoutine(void *data);
getMyHandle()78     uint32_t getMyHandle() const {return mHandle;}
79     bool isTypeOf(cam_stream_type_t type);
80     bool isOrignalTypeOf(cam_stream_type_t type);
81     int32_t getFrameOffset(cam_frame_len_offset_t &offset);
82     int32_t getCropInfo(cam_rect_t &crop);
83     int32_t setCropInfo(cam_rect_t crop);
84     int32_t getFrameDimension(cam_dimension_t &dim);
85     int32_t getFormat(cam_format_t &fmt);
getStreamBufs()86     QCameraMemory *getStreamBufs() {return mStreamBufs;};
getStreamInfoBuf()87     QCameraHeapMemory *getStreamInfoBuf() {return mStreamInfoBuf;};
getMiscBuf()88     QCameraHeapMemory *getMiscBuf() {return mMiscBuf;};
89     uint32_t getMyServerID();
90     cam_stream_type_t getMyType();
91     cam_stream_type_t getMyOriginalType();
92     int32_t acquireStreamBufs();
93 
94     int32_t mapBuf(uint8_t buf_type, uint32_t buf_idx,
95             int32_t plane_idx, int fd, size_t size,
96             mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
97     int32_t unmapBuf(uint8_t buf_type, uint32_t buf_idx, int32_t plane_idx,
98             mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
99     int32_t setParameter(cam_stream_parm_buffer_t &param);
100     int32_t getParameter(cam_stream_parm_buffer_t &param);
101     int32_t syncRuntimeParams();
getOutputCrop()102     cam_stream_parm_buffer_t getOutputCrop() { return m_OutputCrop;};
getImgProp()103     cam_stream_parm_buffer_t getImgProp() { return m_ImgProp;};
104 
105     static void releaseFrameData(void *data, void *user_data);
106     int32_t configStream();
isDeffered()107     bool isDeffered() const { return mDefferedAllocation; }
108     void deleteStream();
109 
getBufferCount()110     uint8_t getBufferCount() { return mNumBufs; }
getChannelHandle()111     uint32_t getChannelHandle() { return mChannelHandle; }
112     int32_t getNumQueuedBuf();
113 
114     uint32_t mDumpFrame;
115     uint32_t mDumpMetaFrame;
116     uint32_t mDumpSkipCnt;
117 
118     void cond_wait();
119     void cond_signal();
120 
121 private:
122     uint32_t mCamHandle;
123     uint32_t mChannelHandle;
124     uint32_t mHandle; // stream handle from mm-camera-interface
125     mm_camera_ops_t *mCamOps;
126     cam_stream_info_t *mStreamInfo; // ptr to stream info buf
127     mm_camera_stream_mem_vtbl_t mMemVtbl;
128     uint8_t mNumBufs;
129     uint8_t mNumPlaneBufs;
130     uint8_t mNumBufsNeedAlloc;
131     uint8_t *mRegFlags;
132     stream_cb_routine mDataCB;
133     void *mUserData;
134 
135     QCameraQueue     mDataQ;
136     QCameraCmdThread mProcTh; // thread for dataCB
137 
138     QCameraHeapMemory *mStreamInfoBuf;
139     QCameraHeapMemory *mMiscBuf;
140     QCameraMemory *mStreamBufs;
141     QCameraMemory *mStreamBatchBufs;
142     QCameraAllocator &mAllocator;
143     mm_camera_buf_def_t *mBufDefs;
144     mm_camera_buf_def_t *mPlaneBufDefs;
145     cam_frame_len_offset_t mFrameLenOffset;
146     cam_padding_info_t mPaddingInfo;
147     cam_rect_t mCropInfo;
148     cam_rotation_t mOnlineRotation;
149     pthread_mutex_t mCropLock; // lock to protect crop info
150     pthread_mutex_t mParameterLock; // lock to sync access to parameters
151     bool mStreamBufsAcquired;
152     bool m_bActive; // if stream mProcTh is active
153     bool mDynBufAlloc; // allow buf allocation in 2 steps
154     pthread_t mBufAllocPid;
155     mm_camera_map_unmap_ops_tbl_t m_MemOpsTbl;
156     cam_stream_parm_buffer_t m_OutputCrop;
157     cam_stream_parm_buffer_t m_ImgProp;
158 
159     static int32_t get_bufs(
160                      cam_frame_len_offset_t *offset,
161                      uint8_t *num_bufs,
162                      uint8_t **initial_reg_flag,
163                      mm_camera_buf_def_t **bufs,
164                      mm_camera_map_unmap_ops_tbl_t *ops_tbl,
165                      void *user_data);
166 
167     static int32_t get_bufs_deffered(
168             cam_frame_len_offset_t *offset,
169             uint8_t *num_bufs,
170             uint8_t **initial_reg_flag,
171             mm_camera_buf_def_t **bufs,
172             mm_camera_map_unmap_ops_tbl_t *ops_tbl,
173             void *user_data);
174 
175     static int32_t put_bufs(
176                      mm_camera_map_unmap_ops_tbl_t *ops_tbl,
177                      void *user_data);
178 
179     static int32_t put_bufs_deffered(
180             mm_camera_map_unmap_ops_tbl_t *ops_tbl,
181             void *user_data);
182 
183     static int32_t invalidate_buf(uint32_t index, void *user_data);
184     static int32_t clean_invalidate_buf(uint32_t index, void *user_data);
185 
186     int32_t getBufs(cam_frame_len_offset_t *offset,
187                      uint8_t *num_bufs,
188                      uint8_t **initial_reg_flag,
189                      mm_camera_buf_def_t **bufs,
190                      mm_camera_map_unmap_ops_tbl_t *ops_tbl);
191     int32_t putBufs(mm_camera_map_unmap_ops_tbl_t *ops_tbl);
192 
193     /* Used for deffered allocation of buffers */
194     int32_t allocateBatchBufs(cam_frame_len_offset_t *offset,
195             uint8_t *num_bufs, uint8_t **initial_reg_flag,
196             mm_camera_buf_def_t **bufs, mm_camera_map_unmap_ops_tbl_t *ops_tbl);
197 
198     int32_t releaseBatchBufs(mm_camera_map_unmap_ops_tbl_t *ops_tbl);
199 
200     int32_t invalidateBuf(uint32_t index);
201     int32_t cleanInvalidateBuf(uint32_t index);
202     int32_t calcOffset(cam_stream_info_t *streamInfo);
203     int32_t unmapStreamInfoBuf();
204     int32_t releaseStreamInfoBuf();
205     int32_t releaseMiscBuf();
206     int32_t mapBuf(QCameraMemory *heapBuf, cam_mapping_buf_type bufType,
207             mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
208     int32_t unMapBuf(QCameraMemory *heapBuf, cam_mapping_buf_type bufType,
209             mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
210 
211     bool mDefferedAllocation;
212 
213     bool wait_for_cond;
214     pthread_mutex_t m_lock;
215     pthread_cond_t m_cond;
216 
217 };
218 
219 }; // namespace qcamera
220 
221 #endif /* __QCAMERA_STREAM_H__ */
222