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