blob: 7c048907b074aaa43d0f19ac6acc4478cd6ba028 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
2* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
3*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of Code Aurora Forum, Inc. nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
29
30
31#ifndef OVERLAY_MEM_H
32#define OVERLAY_MEM_H
33
34#include <sys/mman.h>
35#include <fcntl.h>
36#include <alloc_controller.h>
37#include <memalloc.h>
38
39#include "gralloc_priv.h"
40#include "overlayUtils.h"
41
42namespace overlay {
43
44/*
45* Holds base address, offset and the fd
46* */
47class OvMem {
48public:
49 /* ctor init*/
50 explicit OvMem();
51
52 /* dtor DO NOT call close so it can be copied */
53 ~OvMem();
54
55 /* Use libgralloc to retrieve fd, base addr, alloc type */
56 bool open(uint32_t numbufs,
57 uint32_t bufSz, int flags = O_RDWR);
58
59 /* close fd. assign base address to invalid*/
60 bool close();
61
62 /* return underlying fd */
63 int getFD() const;
64
65 /* return true if fd is valid and base address is valid */
66 bool valid() const;
67
68 /* dump the state of the object */
69 void dump() const;
70
71 /* return underlying address */
72 void* addr() const;
73
74 /* return underlying offset */
75 uint32_t bufSz() const;
76
77 /* return number of bufs */
78 uint32_t numBufs() const ;
79
80private:
81 /* actual os fd */
82 int mFd;
83
84 /* points to base addr (mmap)*/
85 void* mBaseAddr;
86
87 /* allocated buffer type determined by gralloc (ashmem, ion, etc) */
88 int mAllocType;
89
90 /* holds buf size */
91 uint32_t mBufSz;
92
93 /* num of bufs */
94 uint32_t mNumBuffers;
95
96 /* gralloc alloc controller */
97 android::sp<gralloc::IAllocController> mAlloc;
98};
99
100//-------------------Inlines-----------------------------------
101
102using android::sp;
103using gralloc::IMemAlloc;
104using gralloc::alloc_data;
105
106inline OvMem::OvMem() {
107 mFd = -1;
108 mBaseAddr = MAP_FAILED;
109 mAllocType = 0;
110 mBufSz = 0;
111 mNumBuffers = 0;
112 mAlloc = gralloc::IAllocController::getInstance(false);
113}
114
115inline OvMem::~OvMem() { }
116
117inline bool OvMem::open(uint32_t numbufs,
118 uint32_t bufSz, int flags)
119{
120 alloc_data data;
121 //XXX: secure buffers and IOMMU heap
122 int allocFlags = GRALLOC_USAGE_PRIVATE_MM_HEAP |
123 GRALLOC_USAGE_PRIVATE_DO_NOT_MAP;
124 int err = 0;
125
126 OVASSERT(numbufs && bufSz, "numbufs=%d bufSz=%d", numbufs, bufSz);
127
128 mBufSz = bufSz;
129 mNumBuffers = numbufs;
130
131 data.base = 0;
132 data.fd = -1;
133 data.offset = 0;
134 data.size = mBufSz * mNumBuffers;
135 data.align = getpagesize();
136 data.uncached = true;
137
138 err = mAlloc->allocate(data, allocFlags, 0);
139 if (err != 0) {
140 ALOGE("OvMem: error allocating memory");
141 }
142
143 mFd = data.fd;
144 mBaseAddr = data.base;
145 mAllocType = data.allocType;
146
147 return true;
148}
149
150inline bool OvMem::close()
151{
152 int ret = 0;
153
154 if(!valid()) {
155 return true;
156 }
157
158 sp<IMemAlloc> memalloc = mAlloc->getAllocator(mAllocType);
159 ret = memalloc->free_buffer(mBaseAddr, mBufSz * mNumBuffers, 0, mFd);
160 if (ret != 0) {
161 ALOGE("OvMem: error freeing buffer");
162 }
163
164 mFd = -1;
165 mBaseAddr = MAP_FAILED;
166 mAllocType = 0;
167 mBufSz = 0;
168 mNumBuffers = 0;
169 return ret;
170}
171
172inline bool OvMem::valid() const
173{
174 return (mFd != -1) && (mBaseAddr != MAP_FAILED);
175}
176
177inline int OvMem::getFD() const
178{
179 return mFd;
180}
181
182inline void* OvMem::addr() const
183{
184 return mBaseAddr;
185}
186
187inline uint32_t OvMem::bufSz() const
188{
189 return mBufSz;
190}
191
192inline uint32_t OvMem::numBufs() const
193{
194 return mNumBuffers;
195}
196
197inline void OvMem::dump() const
198{
199 ALOGE("%s: fd=%d addr=%p type=%d bufsz=%u",
200 __FUNCTION__, mFd, mBaseAddr, mAllocType, mBufSz);
201}
202
203} // overlay
204
205#endif // OVERLAY_MEM_H