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