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 "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 
76     QCamera3Memory();
77     virtual ~QCamera3Memory();
78 
79     int32_t getBufDef(const cam_frame_len_offset_t &offset,
80             mm_camera_buf_def_t &bufDef, uint32_t index);
81 
82 protected:
83     struct QCamera3MemInfo {
84         int fd;
85         int main_ion_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 };
99 
100 // Internal heap memory is used for memories used internally
101 // They are allocated from /dev/ion. Examples are: capabilities,
102 // parameters, metadata, and internal YUV data for jpeg encoding.
103 class QCamera3HeapMemory : public QCamera3Memory {
104 public:
105     QCamera3HeapMemory(uint32_t maxCnt);
106     virtual ~QCamera3HeapMemory();
107 
108     int allocate(size_t size);
109     int allocateOne(size_t size);
110     void deallocate();
111 
112     virtual int cacheOps(uint32_t index, unsigned int cmd);
113     virtual int getMatchBufIndex(void *object);
114     virtual void *getPtr(uint32_t index);
115 
116     virtual int32_t markFrameNumber(uint32_t index, uint32_t frameNumber);
117     virtual int32_t getFrameNumber(uint32_t index);
118     virtual int32_t getBufferIndex(uint32_t frameNumber);
119 
120 protected:
121     virtual void *getPtrLocked(uint32_t index);
122 private:
123     int allocOneBuffer(struct QCamera3MemInfo &memInfo,
124             unsigned int heap_id, size_t size);
125     void deallocOneBuffer(struct QCamera3MemInfo &memInfo);
126     uint32_t mMaxCnt;
127 };
128 
129 // Gralloc Memory shared with frameworks
130 class QCamera3GrallocMemory : public QCamera3Memory {
131 public:
132     QCamera3GrallocMemory(uint32_t startIdx);
133     virtual ~QCamera3GrallocMemory();
134 
135     int registerBuffer(buffer_handle_t *buffer, cam_stream_type_t type);
136     int32_t unregisterBuffer(size_t idx);
137     void unregisterBuffers();
138     virtual int cacheOps(uint32_t index, unsigned int cmd);
139     virtual int getMatchBufIndex(void *object);
140     virtual void *getPtr(uint32_t index);
141 
142     virtual int32_t markFrameNumber(uint32_t index, uint32_t frameNumber);
143     virtual int32_t getFrameNumber(uint32_t index);
144     virtual int32_t getBufferIndex(uint32_t frameNumber);
145 
146     void *getBufferHandle(uint32_t index);
147 protected:
148     virtual void *getPtrLocked(uint32_t index);
149 private:
150     int32_t unregisterBufferLocked(size_t idx);
151     int32_t getFreeIndexLocked();
152     buffer_handle_t *mBufferHandle[MM_CAMERA_MAX_NUM_FRAMES];
153     struct private_handle_t *mPrivateHandle[MM_CAMERA_MAX_NUM_FRAMES];
154 
155     uint32_t mStartIdx;
156 };
157 };
158 #endif
159