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 __QCAMERA2HWI_MEM_H__
31 #define __QCAMERA2HWI_MEM_H__
32 
33 // System dependencies
34 #include <linux/msm_ion.h>
35 #include <utils/Mutex.h>
36 #include <utils/List.h>
37 
38 // Display dependencies
39 #include "qdMetaData.h"
40 
41 // Camera dependencies
42 #include "camera.h"
43 
44 extern "C" {
45 #include "mm_camera_interface.h"
46 }
47 
48 namespace qcamera {
49 
50 class QCameraMemoryPool;
51 
52 //OFFSET, SIZE, USAGE, TIMESTAMP, FORMAT
53 #define VIDEO_METADATA_NUM_INTS          5
54 
55 enum QCameraMemType {
56     QCAMERA_MEM_TYPE_DEFAULT      = 0,
57     QCAMERA_MEM_TYPE_SECURE       = 1,
58     QCAMERA_MEM_TYPE_BATCH        = (1 << 1),
59     QCAMERA_MEM_TYPE_COMPRESSED   = (1 << 2),
60 };
61 
62 // Base class for all memory types. Abstract.
63 class QCameraMemory {
64 
65 public:
cleanCache(uint32_t index)66     int cleanCache(uint32_t index)
67     {
68         return cacheOps(index, ION_IOC_CLEAN_CACHES);
69     }
invalidateCache(uint32_t index)70     int invalidateCache(uint32_t index)
71     {
72         return cacheOps(index, ION_IOC_INV_CACHES);
73     }
cleanInvalidateCache(uint32_t index)74     int cleanInvalidateCache(uint32_t index)
75     {
76         return cacheOps(index, ION_IOC_CLEAN_INV_CACHES);
77     }
78     int getFd(uint32_t index) const;
79     ssize_t getSize(uint32_t index) const;
80     uint8_t getCnt() const;
81     virtual uint8_t getMappable() const;
82     virtual uint8_t checkIfAllBuffersMapped() const;
83 
84     virtual int allocate(uint8_t count, size_t size, uint32_t is_secure) = 0;
85     virtual void deallocate() = 0;
86     virtual int allocateMore(uint8_t count, size_t size) = 0;
87     virtual int cacheOps(uint32_t index, unsigned int cmd) = 0;
88     virtual int getRegFlags(uint8_t *regFlags) const = 0;
89     virtual camera_memory_t *getMemory(uint32_t index,
90             bool metadata) const = 0;
91     virtual int getMatchBufIndex(const void *opaque, bool metadata) const = 0;
92     virtual void *getPtr(uint32_t index) const= 0;
93 
94     QCameraMemory(bool cached,
95                   QCameraMemoryPool *pool = NULL,
96                   cam_stream_type_t streamType = CAM_STREAM_TYPE_DEFAULT,
97                   QCameraMemType buf_Type = QCAMERA_MEM_TYPE_DEFAULT);
98     virtual ~QCameraMemory();
99     virtual void reset();
100 
101     void getBufDef(const cam_frame_len_offset_t &offset,
102             mm_camera_buf_def_t &bufDef, uint32_t index) const;
103 
104     int32_t getUserBufDef(const cam_stream_user_buf_info_t &buf_info,
105             mm_camera_buf_def_t &bufDef, uint32_t index,
106             const cam_frame_len_offset_t &plane_offset,
107             mm_camera_buf_def_t *planebufDef, QCameraMemory *bufs) const;
108 
109 protected:
110 
111     friend class QCameraMemoryPool;
112 
113     struct QCameraMemInfo {
114         int fd;
115         int main_ion_fd;
116         ion_user_handle_t handle;
117         size_t size;
118         bool cached;
119         unsigned int heap_id;
120     };
121 
122     int alloc(int count, size_t size, unsigned int heap_id,
123             uint32_t is_secure);
124     void dealloc();
125     static int allocOneBuffer(struct QCameraMemInfo &memInfo,
126             unsigned int heap_id, size_t size, bool cached, uint32_t is_secure);
127     static void deallocOneBuffer(struct QCameraMemInfo &memInfo);
128     int cacheOpsInternal(uint32_t index, unsigned int cmd, void *vaddr);
129 
130     bool m_bCached;
131     uint8_t mBufferCount;
132     struct QCameraMemInfo mMemInfo[MM_CAMERA_MAX_NUM_FRAMES];
133     QCameraMemoryPool *mMemoryPool;
134     cam_stream_type_t mStreamType;
135     QCameraMemType mBufType;
136 };
137 
138 class QCameraMemoryPool {
139 
140 public:
141 
142     QCameraMemoryPool();
143     virtual ~QCameraMemoryPool();
144 
145     int allocateBuffer(struct QCameraMemory::QCameraMemInfo &memInfo,
146             unsigned int heap_id, size_t size, bool cached,
147             cam_stream_type_t streamType, uint32_t is_secure);
148     void releaseBuffer(struct QCameraMemory::QCameraMemInfo &memInfo,
149             cam_stream_type_t streamType);
150     void clear();
151 
152 protected:
153 
154     int findBufferLocked(struct QCameraMemory::QCameraMemInfo &memInfo,
155             unsigned int heap_id, size_t size, bool cached,
156             cam_stream_type_t streamType);
157 
158     android::List<QCameraMemory::QCameraMemInfo> mPools[CAM_STREAM_TYPE_MAX];
159     pthread_mutex_t mLock;
160 };
161 
162 // Internal heap memory is used for memories used internally
163 // They are allocated from /dev/ion.
164 class QCameraHeapMemory : public QCameraMemory {
165 public:
166     QCameraHeapMemory(bool cached);
167     virtual ~QCameraHeapMemory();
168 
169     virtual int allocate(uint8_t count, size_t size, uint32_t is_secure);
170     virtual int allocateMore(uint8_t count, size_t size);
171     virtual void deallocate();
172     virtual int cacheOps(uint32_t index, unsigned int cmd);
173     virtual int getRegFlags(uint8_t *regFlags) const;
174     virtual camera_memory_t *getMemory(uint32_t index, bool metadata) const;
175     virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
176     virtual void *getPtr(uint32_t index) const;
177 
178 private:
179     void *mPtr[MM_CAMERA_MAX_NUM_FRAMES];
180 };
181 
182 class QCameraMetadataStreamMemory : public QCameraHeapMemory {
183 public:
184     QCameraMetadataStreamMemory(bool cached);
185     virtual ~QCameraMetadataStreamMemory();
186 
187     virtual int getRegFlags(uint8_t *regFlags) const;
188 };
189 
190 // Externel heap memory is used for memories shared with
191 // framework. They are allocated from /dev/ion or gralloc.
192 class QCameraStreamMemory : public QCameraMemory {
193 public:
194     QCameraStreamMemory(camera_request_memory getMemory,
195                         bool cached,
196                         QCameraMemoryPool *pool = NULL,
197                         cam_stream_type_t streamType = CAM_STREAM_TYPE_DEFAULT,
198                         cam_stream_buf_type buf_Type = CAM_STREAM_BUF_TYPE_MPLANE);
199     virtual ~QCameraStreamMemory();
200 
201     virtual int allocate(uint8_t count, size_t size, uint32_t is_secure);
202     virtual int allocateMore(uint8_t count, size_t size);
203     virtual void deallocate();
204     virtual int cacheOps(uint32_t index, unsigned int cmd);
205     virtual int getRegFlags(uint8_t *regFlags) const;
206     virtual camera_memory_t *getMemory(uint32_t index, bool metadata) const;
207     virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
208     virtual void *getPtr(uint32_t index) const;
209 
210 protected:
211     camera_request_memory mGetMemory;
212     camera_memory_t *mCameraMemory[MM_CAMERA_MAX_NUM_FRAMES];
213 };
214 
215 // Externel heap memory is used for memories shared with
216 // framework. They are allocated from /dev/ion or gralloc.
217 class QCameraVideoMemory : public QCameraStreamMemory {
218 public:
219     QCameraVideoMemory(camera_request_memory getMemory, bool cached,
220             QCameraMemType bufType = QCAMERA_MEM_TYPE_DEFAULT);
221     virtual ~QCameraVideoMemory();
222 
223     virtual int allocate(uint8_t count, size_t size, uint32_t is_secure);
224     virtual int allocateMore(uint8_t count, size_t size);
225     virtual void deallocate();
226     virtual camera_memory_t *getMemory(uint32_t index, bool metadata) const;
227     virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
228     int allocateMeta(uint8_t buf_cnt, int numFDs, int numInts);
229     void deallocateMeta();
230     void setVideoInfo(int usage, cam_format_t format);
getUsage()231     int getUsage(){return mUsage;};
getFormat()232     int getFormat(){return mFormat;};
233     int convCamtoOMXFormat(cam_format_t format);
234 private:
235     camera_memory_t *mMetadata[MM_CAMERA_MAX_NUM_FRAMES];
236     uint8_t mMetaBufCount;
237     int mUsage, mFormat;
238 };
239 
240 
241 // Gralloc Memory is acquired from preview window
242 class QCameraGrallocMemory : public QCameraMemory {
243     enum {
244         BUFFER_NOT_OWNED,
245         BUFFER_OWNED,
246     };
247 public:
248     QCameraGrallocMemory(camera_request_memory getMemory);
249     void setNativeWindow(preview_stream_ops_t *anw);
250     virtual ~QCameraGrallocMemory();
251 
252     virtual int allocate(uint8_t count, size_t size, uint32_t is_secure);
253     virtual int allocateMore(uint8_t count, size_t size);
254     virtual void deallocate();
255     virtual int cacheOps(uint32_t index, unsigned int cmd);
256     virtual int getRegFlags(uint8_t *regFlags) const;
257     virtual camera_memory_t *getMemory(uint32_t index, bool metadata) const;
258     virtual int getMatchBufIndex(const void *opaque, bool metadata) const;
259     virtual void *getPtr(uint32_t index) const;
260     virtual void setMappable(uint8_t mappable);
261     virtual uint8_t getMappable() const;
262     virtual uint8_t checkIfAllBuffersMapped() const;
263 
264     void setWindowInfo(preview_stream_ops_t *window, int width, int height,
265         int stride, int scanline, int format, int maxFPS, int usage = 0);
266     // Enqueue/display buffer[index] onto the native window,
267     // and dequeue one buffer from it.
268     // Returns the buffer index of the dequeued buffer.
269     int displayBuffer(uint32_t index);
270     void setMaxFPS(int maxFPS);
271     int32_t enqueueBuffer(uint32_t index, nsecs_t timeStamp = 0);
272     int32_t dequeueBuffer();
isBufOwnedByCamera(uint32_t index)273     inline bool isBufOwnedByCamera(uint32_t index){return mLocalFlag[index] == BUFFER_OWNED;};
274 
275 private:
276     buffer_handle_t *mBufferHandle[MM_CAMERA_MAX_NUM_FRAMES];
277     int mLocalFlag[MM_CAMERA_MAX_NUM_FRAMES];
278     struct private_handle_t *mPrivateHandle[MM_CAMERA_MAX_NUM_FRAMES];
279     preview_stream_ops_t *mWindow;
280     int mWidth, mHeight, mFormat, mStride, mScanline, mUsage, mMaxFPS;
281     camera_request_memory mGetMemory;
282     camera_memory_t *mCameraMemory[MM_CAMERA_MAX_NUM_FRAMES];
283     int mMinUndequeuedBuffers;
284     enum ColorSpace_t mColorSpace;
285     uint8_t mMappableBuffers;
286     pthread_mutex_t mLock;
287     uint8_t mEnqueuedBuffers;
288 };
289 
290 }; // namespace qcamera
291 
292 #endif /* __QCAMERA2HWI_MEM_H__ */
293