1 /* Copyright (c) 2012,2014 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,
54                   uint32_t chId,
55                   mm_camera_ops_t *camOps,
56                   cam_padding_info_t *paddingInfo,
57                   bool deffered = false);
58     virtual ~QCameraStream();
59     virtual int32_t init(QCameraHeapMemory *streamInfoBuf,
60                          uint8_t minStreamBufNum,
61                          stream_cb_routine stream_cb,
62                          void *userdata,
63                          bool bDynallocBuf);
64     virtual int32_t processZoomDone(preview_stream_ops_t *previewWindow,
65                                     cam_crop_data_t &crop_info);
66     virtual int32_t bufDone(int index);
67     virtual int32_t bufDone(const void *opaque, bool isMetaData);
68     virtual int32_t processDataNotify(mm_camera_super_buf_t *bufs);
69     virtual int32_t start();
70     virtual int32_t stop();
71 
72     /* Used for deffered allocation of buffers */
73     virtual int32_t allocateBuffers();
74     virtual int32_t releaseBuffs();
75 
76     static void dataNotifyCB(mm_camera_super_buf_t *recvd_frame, void *userdata);
77     static void *dataProcRoutine(void *data);
78     static void *BufAllocRoutine(void *data);
getMyHandle()79     uint32_t getMyHandle() const {return mHandle;}
80     bool isTypeOf(cam_stream_type_t type);
81     bool isOrignalTypeOf(cam_stream_type_t type);
82     int32_t getFrameOffset(cam_frame_len_offset_t &offset);
83     int32_t getCropInfo(cam_rect_t &crop);
84     int32_t setCropInfo(cam_rect_t crop);
85     int32_t getFrameDimension(cam_dimension_t &dim);
86     int32_t getFormat(cam_format_t &fmt);
getStreamBufs()87     QCameraMemory *getStreamBufs() {return mStreamBufs;};
88     uint32_t getMyServerID();
89     cam_stream_type_t getMyType();
90     int32_t acquireStreamBufs();
91 
92     int32_t mapBuf(uint8_t buf_type, uint32_t buf_idx,
93                    int32_t plane_idx, int fd, uint32_t size);
94     int32_t unmapBuf(uint8_t buf_type, uint32_t buf_idx, int32_t plane_idx);
95     int32_t setParameter(cam_stream_parm_buffer_t &param);
96     int32_t getParameter(cam_stream_parm_buffer_t &param);
97     int32_t syncRuntimeParams();
getOutputCrop()98     cam_stream_parm_buffer_t getOutputCrop() { return m_OutputCrop;};
getImgProp()99     cam_stream_parm_buffer_t getImgProp() { return m_ImgProp;};
100 
101     static void releaseFrameData(void *data, void *user_data);
102     int32_t configStream();
isDeffered()103     bool isDeffered() const { return mDefferedAllocation; }
104     void deleteStream();
105 
106     int mDumpFrame;
107     int mDumpMetaFrame;
108     int mDumpSkipCnt;
109 
110     void cond_wait();
111     void cond_signal();
112 
113 private:
114     uint32_t mCamHandle;
115     uint32_t mChannelHandle;
116     uint32_t mHandle; // stream handle from mm-camera-interface
117     mm_camera_ops_t *mCamOps;
118     cam_stream_info_t *mStreamInfo; // ptr to stream info buf
119     mm_camera_stream_mem_vtbl_t mMemVtbl;
120     uint8_t mNumBufs;
121     uint8_t mNumBufsNeedAlloc;
122     uint8_t *mRegFlags;
123     stream_cb_routine mDataCB;
124     void *mUserData;
125 
126     QCameraQueue     mDataQ;
127     QCameraCmdThread mProcTh; // thread for dataCB
128 
129     QCameraHeapMemory *mStreamInfoBuf;
130     QCameraMemory *mStreamBufs;
131     QCameraAllocator &mAllocator;
132     mm_camera_buf_def_t *mBufDefs;
133     cam_frame_len_offset_t mFrameLenOffset;
134     cam_padding_info_t mPaddingInfo;
135     cam_rect_t mCropInfo;
136     pthread_mutex_t mCropLock; // lock to protect crop info
137     pthread_mutex_t mParameterLock; // lock to sync access to parameters
138     bool mStreamBufsAcquired;
139     bool m_bActive; // if stream mProcTh is active
140     bool mDynBufAlloc; // allow buf allocation in 2 steps
141     pthread_t mBufAllocPid;
142     mm_camera_map_unmap_ops_tbl_t m_MemOpsTbl;
143     cam_stream_parm_buffer_t m_OutputCrop;
144     cam_stream_parm_buffer_t m_ImgProp;
145 
146     static int32_t get_bufs(
147                      cam_frame_len_offset_t *offset,
148                      uint8_t *num_bufs,
149                      uint8_t **initial_reg_flag,
150                      mm_camera_buf_def_t **bufs,
151                      mm_camera_map_unmap_ops_tbl_t *ops_tbl,
152                      void *user_data);
153 
154     static int32_t get_bufs_deffered(
155             cam_frame_len_offset_t *offset,
156             uint8_t *num_bufs,
157             uint8_t **initial_reg_flag,
158             mm_camera_buf_def_t **bufs,
159             mm_camera_map_unmap_ops_tbl_t *ops_tbl,
160             void *user_data);
161 
162     static int32_t put_bufs(
163                      mm_camera_map_unmap_ops_tbl_t *ops_tbl,
164                      void *user_data);
165 
166     static int32_t put_bufs_deffered(
167             mm_camera_map_unmap_ops_tbl_t *ops_tbl,
168             void *user_data);
169 
170     static int32_t invalidate_buf(int index, void *user_data);
171     static int32_t clean_invalidate_buf(int index, void *user_data);
172 
173     int32_t getBufs(cam_frame_len_offset_t *offset,
174                      uint8_t *num_bufs,
175                      uint8_t **initial_reg_flag,
176                      mm_camera_buf_def_t **bufs,
177                      mm_camera_map_unmap_ops_tbl_t *ops_tbl);
178     int32_t putBufs(mm_camera_map_unmap_ops_tbl_t *ops_tbl);
179     int32_t invalidateBuf(int index);
180     int32_t cleanInvalidateBuf(int index);
181     int32_t calcOffset(cam_stream_info_t *streamInfo);
182     int32_t unmapStreamInfoBuf();
183     int32_t releaseStreamInfoBuf();
184     bool mDefferedAllocation;
185 
186     bool wait_for_cond;
187     pthread_mutex_t m_lock;
188     pthread_cond_t m_cond;
189 
190 };
191 
192 }; // namespace qcamera
193 
194 #endif /* __QCAMERA_STREAM_H__ */
195