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