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