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