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