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