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(int index)49     int cleanCache(int index) {return cacheOps(index, ION_IOC_CLEAN_CACHES);}
invalidateCache(int index)50     int invalidateCache(int index) {return cacheOps(index, ION_IOC_INV_CACHES);}
cleanInvalidateCache(int index)51     int cleanInvalidateCache(int index) {return cacheOps(index, ION_IOC_CLEAN_INV_CACHES);}
52     int getFd(int index);
53     int getSize(int index);
54     int getCnt();
55 
56     virtual int cacheOps(int index, unsigned int cmd) = 0;
57     virtual int getRegFlags(uint8_t *regFlags) = 0;
58     virtual int getMatchBufIndex(void *object) = 0;
59     virtual void *getPtr(int index) = 0;
60 
61     QCamera3Memory();
62     virtual ~QCamera3Memory();
63 
64     int32_t getBufDef(const cam_frame_len_offset_t &offset,
65                 mm_camera_buf_def_t &bufDef, int index);
66 
67 protected:
68     struct QCamera3MemInfo {
69         int fd;
70         int main_ion_fd;
71         ion_user_handle_t handle;
72         uint32_t size;
73     };
74 
75     int cacheOpsInternal(int index, unsigned int cmd, void *vaddr);
76     virtual void *getPtrLocked(int index) = 0;
77 
78     int mBufferCount;
79     struct QCamera3MemInfo mMemInfo[MM_CAMERA_MAX_NUM_FRAMES];
80     void *mPtr[MM_CAMERA_MAX_NUM_FRAMES];
81     Mutex mLock;
82 };
83 
84 // Internal heap memory is used for memories used internally
85 // They are allocated from /dev/ion. Examples are: capabilities,
86 // parameters, metadata, and internal YUV data for jpeg encoding.
87 class QCamera3HeapMemory : public QCamera3Memory {
88 public:
89     QCamera3HeapMemory();
90     virtual ~QCamera3HeapMemory();
91 
92     int allocate(int count, int size, bool queueAll);
93     void deallocate();
94 
95     virtual int cacheOps(int index, unsigned int cmd);
96     virtual int getRegFlags(uint8_t *regFlags);
97     virtual int getMatchBufIndex(void *object);
98     virtual void *getPtr(int index);
99 protected:
100     virtual void *getPtrLocked(int index);
101 private:
102     int alloc(int count, int size, int heap_id);
103     void dealloc();
104 
105     int allocOneBuffer(struct QCamera3MemInfo &memInfo, int heap_id, int size);
106     void deallocOneBuffer(struct QCamera3MemInfo &memInfo);
107     bool mQueueAll;
108 };
109 
110 // Gralloc Memory shared with frameworks
111 class QCamera3GrallocMemory : public QCamera3Memory {
112 public:
113     QCamera3GrallocMemory();
114     virtual ~QCamera3GrallocMemory();
115 
116     int registerBuffer(buffer_handle_t *buffer, cam_stream_type_t type);
117     int32_t unregisterBuffer(size_t idx);
118     void unregisterBuffers();
119     virtual int cacheOps(int index, unsigned int cmd);
120     virtual int getRegFlags(uint8_t *regFlags);
121     virtual int getMatchBufIndex(void *object);
122     virtual void *getPtr(int index);
123     int32_t markFrameNumber(int index, uint32_t frameNumber);
124     int32_t getFrameNumber(int index);
125     void *getBufferHandle(int index);
126 protected:
127     virtual void *getPtrLocked(int index);
128 private:
129     int32_t unregisterBufferLocked(size_t idx);
130     int32_t getFreeIndexLocked();
131     buffer_handle_t *mBufferHandle[MM_CAMERA_MAX_NUM_FRAMES];
132     struct private_handle_t *mPrivateHandle[MM_CAMERA_MAX_NUM_FRAMES];
133     uint32_t mCurrentFrameNumbers[MM_CAMERA_MAX_NUM_FRAMES];
134 };
135 
136 };
137 #endif
138