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 __QCAMERA3_STREAM_H__
31#define __QCAMERA3_STREAM_H__
32
33// System dependencies
34#include <utils/Mutex.h>
35
36// Camera dependencies
37#include "QCamera3Mem.h"
38#include "QCamera3StreamMem.h"
39#include "QCameraCmdThread.h"
40#include "QCameraQueue.h"
41
42extern "C" {
43#include "mm_camera_interface.h"
44}
45
46namespace qcamera {
47
48class QCamera3Channel;
49class QCamera3Stream;
50
51typedef void (*hal3_stream_cb_routine)(mm_camera_super_buf_t *frame,
52                                  QCamera3Stream *stream,
53                                  void *userdata);
54
55class QCamera3Stream
56{
57public:
58    QCamera3Stream(uint32_t camHandle,
59                  uint32_t chId,
60                  mm_camera_ops_t *camOps,
61                  cam_padding_info_t *paddingInfo,
62                  QCamera3Channel *channel,
63                  bool mapStreamBuffers);
64    virtual ~QCamera3Stream();
65    virtual int32_t init(cam_stream_type_t streamType,
66                         cam_format_t streamFormat,
67                         cam_dimension_t streamDim,
68                         cam_rotation_t streamRotation,
69                         cam_stream_reproc_config_t* reprocess_config,
70                         uint8_t minStreamBufNum,
71                         cam_feature_mask_t postprocess_mask,
72                         cam_is_type_t is_type,
73                         uint32_t batchSize,
74                         hal3_stream_cb_routine stream_cb,
75                         void *userdata);
76    virtual int32_t bufDone(uint32_t index);
77    virtual int32_t cancelBuffer(uint32_t index);
78    virtual int32_t bufRelease(int32_t index);
79    virtual int32_t processDataNotify(mm_camera_super_buf_t *bufs);
80    virtual int32_t start();
81    virtual int32_t stop();
82    virtual int32_t queueBatchBuf();
83
84    static void dataNotifyCB(mm_camera_super_buf_t *recvd_frame, void *userdata);
85    static void *dataProcRoutine(void *data);
86    uint32_t getMyHandle() const {return mHandle;}
87    cam_stream_type_t getMyType() const;
88    int32_t getFrameOffset(cam_frame_len_offset_t &offset);
89    int32_t getFrameDimension(cam_dimension_t &dim);
90    int32_t getFormat(cam_format_t &fmt);
91    QCamera3StreamMem *getStreamBufs() {return mStreamBufs;};
92    uint32_t getMyServerID();
93
94    int32_t mapBuf(uint8_t buf_type, uint32_t buf_idx,
95            int32_t plane_idx, int fd, void *buffer, size_t size);
96    int32_t unmapBuf(uint8_t buf_type, uint32_t buf_idx, int32_t plane_idx);
97    int32_t setParameter(cam_stream_parm_buffer_t &param);
98    cam_stream_info_t* getStreamInfo() const {return mStreamInfo; };
99
100    static void releaseFrameData(void *data, void *user_data);
101    int32_t timeoutFrame(int32_t bufIdx);
102
103private:
104    uint32_t mCamHandle;
105    uint32_t mChannelHandle;
106    uint32_t mHandle; // stream handle from mm-camera-interface
107    mm_camera_ops_t *mCamOps;
108    cam_stream_info_t *mStreamInfo; // ptr to stream info buf
109    mm_camera_stream_mem_vtbl_t mMemVtbl;
110    mm_camera_map_unmap_ops_tbl_t *mMemOps;
111    uint8_t mNumBufs;
112    hal3_stream_cb_routine mDataCB;
113    void *mUserData;
114
115    QCameraQueue     mDataQ;
116
117    List<int32_t> mTimeoutFrameQ;
118    Mutex mTimeoutFrameQLock;
119
120    QCameraCmdThread mProcTh; // thread for dataCB
121
122    QCamera3HeapMemory *mStreamInfoBuf;
123    QCamera3StreamMem *mStreamBufs;
124    mm_camera_buf_def_t *mBufDefs;
125    cam_frame_len_offset_t mFrameLenOffset;
126    cam_padding_info_t mPaddingInfo;
127    QCamera3Channel *mChannel;
128    Mutex mLock;    //Lock controlling access to 'mBufDefs'
129
130    uint32_t mBatchSize; // 0: No batch, non-0: Number of imaage bufs in a batch
131    uint8_t mNumBatchBufs; //Number of batch buffers which can hold image bufs
132    QCamera3HeapMemory *mStreamBatchBufs; //Pointer to batch buffers memory
133    mm_camera_buf_def_t *mBatchBufDefs; //Pointer to array of batch bufDefs
134    mm_camera_buf_def_t *mCurrentBatchBufDef; //batch buffer in progress during
135                                              //aggregation
136    uint32_t    mBufsStaged; //Number of image buffers aggregated into
137                             //currentBatchBufDef
138    QCameraQueue mFreeBatchBufQ; //Buffer queue containing empty batch buffers
139    uint8_t mNRMode; // Initial noise reduction mode
140
141    bool mMapStreamBuffers; // Whether to mmap every stream buffers
142
143    static int32_t get_bufs(
144                     cam_frame_len_offset_t *offset,
145                     uint8_t *num_bufs,
146                     uint8_t **initial_reg_flag,
147                     mm_camera_buf_def_t **bufs,
148                     mm_camera_map_unmap_ops_tbl_t *ops_tbl,
149                     void *user_data);
150    static int32_t put_bufs(
151                     mm_camera_map_unmap_ops_tbl_t *ops_tbl,
152                     void *user_data);
153    static int32_t invalidate_buf(uint32_t index, void *user_data);
154    static int32_t clean_invalidate_buf(uint32_t index, void *user_data);
155    static int32_t clean_buf(uint32_t index, void *user_data);
156
157    int32_t getBufs(cam_frame_len_offset_t *offset,
158                     uint8_t *num_bufs,
159                     uint8_t **initial_reg_flag,
160                     mm_camera_buf_def_t **bufs,
161                     mm_camera_map_unmap_ops_tbl_t *ops_tbl);
162    int32_t putBufs(mm_camera_map_unmap_ops_tbl_t *ops_tbl);
163    int32_t invalidateBuf(uint32_t index);
164    int32_t cleanInvalidateBuf(uint32_t index);
165    int32_t cleanBuf(uint32_t index);
166    int32_t getBatchBufs(
167            uint8_t *num_bufs, uint8_t **initial_reg_flag,
168            mm_camera_buf_def_t **bufs,
169            mm_camera_map_unmap_ops_tbl_t *ops_tbl);
170    int32_t putBatchBufs(mm_camera_map_unmap_ops_tbl_t *ops_tbl);
171    int32_t getBatchBufDef(mm_camera_buf_def_t& batchBufDef,
172            int32_t index);
173    int32_t aggregateBufToBatch(mm_camera_buf_def_t& bufDef);
174    int32_t aggregateStartingBufs(const uint8_t *initial_reg_flag);
175    int32_t handleBatchBuffer(mm_camera_super_buf_t *superBuf);
176    int32_t bufDoneLocked(uint32_t index);
177
178    static const char* mStreamNames[CAM_STREAM_TYPE_MAX];
179    void flushFreeBatchBufQ();
180};
181
182}; // namespace qcamera
183
184#endif /* __QCAMERA3_STREAM_H__ */
185