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
39extern "C" {
40#include "mm_camera_interface.h"
41}
42
43namespace qcamera {
44
45class QCameraStream;
46typedef 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
54class QCameraStream
55{
56public:
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);
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);
94    QCameraMemory *getStreamBufs() {return mStreamBufs;};
95    QCameraHeapMemory *getStreamInfoBuf() {return mStreamInfoBuf;};
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();
113    cam_stream_parm_buffer_t getOutputCrop() { return m_OutputCrop;};
114    cam_stream_parm_buffer_t getImgProp() { return m_ImgProp;};
115
116    static void releaseFrameData(void *data, void *user_data);
117    int32_t configStream();
118    bool isDeffered() const { return mDefferedAllocation; }
119    bool isSyncCBEnabled() {return mSyncCBEnabled;};
120    void deleteStream();
121
122    uint8_t getBufferCount() { return mNumBufs; }
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    int32_t handleCacheOps(mm_camera_buf_def_t* buf);
152
153private:
154    uint32_t mCamHandle;
155    uint32_t mChannelHandle;
156    uint32_t mHandle; // stream handle from mm-camera-interface
157    mm_camera_ops_t *mCamOps;
158    cam_stream_info_t *mStreamInfo; // ptr to stream info buf
159    mm_camera_stream_mem_vtbl_t mMemVtbl;
160    uint8_t mNumBufs;
161    uint8_t mNumPlaneBufs;
162    uint8_t mNumBufsNeedAlloc;
163    uint8_t *mRegFlags;
164    stream_cb_routine mDataCB;
165    stream_cb_routine mSYNCDataCB;
166    void *mUserData;
167
168    QCameraQueue     mDataQ;
169    QCameraCmdThread mProcTh; // thread for dataCB
170
171    QCameraHeapMemory *mStreamInfoBuf;
172    QCameraHeapMemory *mMiscBuf;
173    QCameraMemory *mStreamBufs;
174    QCameraMemory *mStreamBatchBufs;
175    QCameraAllocator &mAllocator;
176    mm_camera_buf_def_t *mBufDefs;
177    mm_camera_buf_def_t *mPlaneBufDefs;
178    cam_frame_len_offset_t mFrameLenOffset;
179    cam_padding_info_t mPaddingInfo;
180    cam_rect_t mCropInfo;
181    cam_rotation_t mOnlineRotation;
182    pthread_mutex_t mCropLock; // lock to protect crop info
183    pthread_mutex_t mParameterLock; // lock to sync access to parameters
184    bool mStreamBufsAcquired;
185    bool m_bActive; // if stream mProcTh is active
186    bool mDynBufAlloc; // allow buf allocation in 2 steps
187    pthread_t mBufAllocPid;
188    mm_camera_map_unmap_ops_tbl_t m_MemOpsTbl;
189    cam_stream_parm_buffer_t m_OutputCrop;
190    cam_stream_parm_buffer_t m_ImgProp;
191
192    static int32_t get_bufs(
193                     cam_frame_len_offset_t *offset,
194                     uint8_t *num_bufs,
195                     uint8_t **initial_reg_flag,
196                     mm_camera_buf_def_t **bufs,
197                     mm_camera_map_unmap_ops_tbl_t *ops_tbl,
198                     void *user_data);
199
200    static int32_t get_bufs_deffered(
201            cam_frame_len_offset_t *offset,
202            uint8_t *num_bufs,
203            uint8_t **initial_reg_flag,
204            mm_camera_buf_def_t **bufs,
205            mm_camera_map_unmap_ops_tbl_t *ops_tbl,
206            void *user_data);
207
208    static int32_t put_bufs(
209                     mm_camera_map_unmap_ops_tbl_t *ops_tbl,
210                     void *user_data);
211
212    static int32_t put_bufs_deffered(
213            mm_camera_map_unmap_ops_tbl_t *ops_tbl,
214            void *user_data);
215
216    static int32_t set_config_ops(
217            mm_camera_map_unmap_ops_tbl_t *ops_tbl,
218            void *user_data);
219
220    static int32_t invalidate_buf(uint32_t index, void *user_data);
221    static int32_t clean_invalidate_buf(uint32_t index, void *user_data);
222    static int32_t clean_buf(uint32_t index, void *user_data);
223
224    static int32_t backgroundAllocate(void* data);
225    static int32_t backgroundMap(void* data);
226
227    int32_t getBufs(cam_frame_len_offset_t *offset,
228                     uint8_t *num_bufs,
229                     uint8_t **initial_reg_flag,
230                     mm_camera_buf_def_t **bufs,
231                     mm_camera_map_unmap_ops_tbl_t *ops_tbl);
232    int32_t getBufsDeferred(cam_frame_len_offset_t *offset,
233            uint8_t *num_bufs,
234            uint8_t **initial_reg_flag,
235            mm_camera_buf_def_t **bufs,
236            mm_camera_map_unmap_ops_tbl_t *ops_tbl);
237    int32_t putBufs(mm_camera_map_unmap_ops_tbl_t *ops_tbl);
238    int32_t putBufsDeffered();
239
240    /* Used for deffered allocation of buffers */
241    int32_t allocateBatchBufs(cam_frame_len_offset_t *offset,
242            uint8_t *num_bufs, uint8_t **initial_reg_flag,
243            mm_camera_buf_def_t **bufs, mm_camera_map_unmap_ops_tbl_t *ops_tbl);
244
245    int32_t releaseBatchBufs(mm_camera_map_unmap_ops_tbl_t *ops_tbl);
246
247    int32_t invalidateBuf(uint32_t index);
248    int32_t cleanInvalidateBuf(uint32_t index);
249    int32_t cleanBuf(uint32_t index);
250    int32_t calcOffset(cam_stream_info_t *streamInfo);
251    int32_t unmapStreamInfoBuf();
252    int32_t releaseStreamInfoBuf();
253    int32_t releaseMiscBuf();
254    int32_t mapBufs(QCameraMemory *heapBuf, cam_mapping_buf_type bufType,
255            mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
256    int32_t unMapBuf(QCameraMemory *heapBuf, cam_mapping_buf_type bufType,
257            mm_camera_map_unmap_ops_tbl_t *ops_tbl = NULL);
258
259    bool mDefferedAllocation;
260
261    bool wait_for_cond;
262    pthread_mutex_t m_lock;
263    pthread_cond_t m_cond;
264
265    BackgroundTask mAllocTask;
266    uint32_t mAllocTaskId;
267    BackgroundTask mMapTask;
268    uint32_t mMapTaskId;
269
270    bool mSyncCBEnabled;
271};
272
273}; // namespace qcamera
274
275#endif /* __QCAMERA_STREAM_H__ */
276