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