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 __QCAMERA3HWI_MEM_H__
31 #define __QCAMERA3HWI_MEM_H__
32 
33 // System dependencies
34 #include <linux/msm_ion.h>
35 #include <utils/Mutex.h>
36 
37 // Camera dependencies
38 #include "hardware/camera3.h"
39 
40 extern "C" {
41 #include "mm_camera_interface.h"
42 }
43 
44 using namespace android;
45 
46 namespace qcamera {
47 
48 // Base class for all memory types. Abstract.
49 class QCamera3Memory {
50 
51 public:
cleanCache(uint32_t index)52     int cleanCache(uint32_t index)
53     {
54         return cacheOps(index, ION_IOC_CLEAN_CACHES);
55     }
invalidateCache(uint32_t index)56     int invalidateCache(uint32_t index)
57     {
58         return cacheOps(index, ION_IOC_INV_CACHES);
59     }
cleanInvalidateCache(uint32_t index)60     int cleanInvalidateCache(uint32_t index)
61     {
62         return cacheOps(index, ION_IOC_CLEAN_INV_CACHES);
63     }
64     int getFd(uint32_t index);
65     ssize_t getSize(uint32_t index);
66     uint32_t getCnt();
67 
68     virtual int cacheOps(uint32_t index, unsigned int cmd) = 0;
69     virtual int getMatchBufIndex(void *object) = 0;
70     virtual void *getPtr(uint32_t index) = 0;
71 
72     virtual int32_t markFrameNumber(uint32_t index, uint32_t frameNumber) = 0;
73     virtual int32_t getFrameNumber(uint32_t index) = 0;
74     virtual int32_t getBufferIndex(uint32_t frameNumber) = 0;
75     virtual int32_t getOldestFrameNumber(uint32_t &index) = 0;
76 
77     QCamera3Memory();
78     virtual ~QCamera3Memory();
79 
80     int32_t getBufDef(const cam_frame_len_offset_t &offset,
81             mm_camera_buf_def_t &bufDef, uint32_t index, bool virtualAddr);
82 
83 protected:
84     struct QCamera3MemInfo {
85         int fd;
86         ion_user_handle_t handle;
87         size_t size;
88     };
89 
90     int cacheOpsInternal(uint32_t index, unsigned int cmd, void *vaddr);
91     virtual void *getPtrLocked(uint32_t index) = 0;
92 
93     uint32_t mBufferCount;
94     struct QCamera3MemInfo mMemInfo[MM_CAMERA_MAX_NUM_FRAMES];
95     void *mPtr[MM_CAMERA_MAX_NUM_FRAMES];
96     int32_t mCurrentFrameNumbers[MM_CAMERA_MAX_NUM_FRAMES];
97     Mutex mLock;
98     int main_ion_fd = -1;
99 };
100 
101 // Internal heap memory is used for memories used internally
102 // They are allocated from /dev/ion. Examples are: capabilities,
103 // parameters, metadata, and internal YUV data for jpeg encoding.
104 class QCamera3HeapMemory : public QCamera3Memory {
105 public:
106     QCamera3HeapMemory(uint32_t maxCnt);
107     virtual ~QCamera3HeapMemory();
108 
109     int allocate(size_t size);
110     int allocateOne(size_t size, bool isCached = true);
111     void deallocate();
112 
113     virtual int cacheOps(uint32_t index, unsigned int cmd);
114     virtual int getMatchBufIndex(void *object);
115     virtual void *getPtr(uint32_t index);
116 
117     virtual int32_t markFrameNumber(uint32_t index, uint32_t frameNumber);
118     virtual int32_t getFrameNumber(uint32_t index);
119     virtual int32_t getBufferIndex(uint32_t frameNumber);
120     virtual int32_t getOldestFrameNumber(uint32_t &index);
121 
122 protected:
123     virtual void *getPtrLocked(uint32_t index);
124 private:
125     int allocOneBuffer(struct QCamera3MemInfo &memInfo,
126             unsigned int heap_id, size_t size, bool isCached = true);
127     void deallocOneBuffer(struct QCamera3MemInfo &memInfo);
128     uint32_t mMaxCnt;
129 };
130 
131 // Gralloc Memory shared with frameworks
132 class QCamera3GrallocMemory : public QCamera3Memory {
133 public:
134     QCamera3GrallocMemory(uint32_t startIdx);
135     virtual ~QCamera3GrallocMemory();
136 
137     int registerBuffer(buffer_handle_t *buffer, cam_stream_type_t type);
138     int32_t unregisterBuffer(size_t idx);
139     void unregisterBuffers();
140     virtual int cacheOps(uint32_t index, unsigned int cmd);
141     virtual int getMatchBufIndex(void *object);
142     virtual void *getPtr(uint32_t index);
143 
144     virtual int32_t markFrameNumber(uint32_t index, uint32_t frameNumber);
145     virtual int32_t getFrameNumber(uint32_t index);
146     virtual int32_t getBufferIndex(uint32_t frameNumber);
147     virtual int32_t getOldestFrameNumber(uint32_t &index);
148 
149     void *getBufferHandle(uint32_t index);
150 protected:
151     virtual void *getPtrLocked(uint32_t index);
152 private:
153     int32_t unregisterBufferLocked(size_t idx);
154     int32_t getFreeIndexLocked();
155     buffer_handle_t *mBufferHandle[MM_CAMERA_MAX_NUM_FRAMES];
156     struct private_handle_t *mPrivateHandle[MM_CAMERA_MAX_NUM_FRAMES];
157 
158     uint32_t mStartIdx;
159 };
160 };
161 #endif
162