blob: c743741abf6b9ef8b3a9bccd45c73106b0af8491 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
Naseer Ahmedf48aef62012-07-20 09:05:53 -07002* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
Naseer Ahmed29a26812012-06-14 00:56:20 -07003*
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,
Naseer Ahmedf48aef62012-07-20 09:05:53 -070057 uint32_t bufSz, bool isSecure);
Naseer Ahmed29a26812012-06-14 00:56:20 -070058
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 */
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070097 gralloc::IAllocController* mAlloc;
Naseer Ahmed29a26812012-06-14 00:56:20 -070098};
99
100//-------------------Inlines-----------------------------------
101
Naseer Ahmed29a26812012-06-14 00:56:20 -0700102using gralloc::IMemAlloc;
103using gralloc::alloc_data;
104
105inline OvMem::OvMem() {
106 mFd = -1;
107 mBaseAddr = MAP_FAILED;
108 mAllocType = 0;
109 mBufSz = 0;
110 mNumBuffers = 0;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700111 mAlloc = gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700112}
113
114inline OvMem::~OvMem() { }
115
116inline bool OvMem::open(uint32_t numbufs,
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700117 uint32_t bufSz, bool isSecure)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700118{
119 alloc_data data;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700120 int allocFlags = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
121 if(isSecure) {
122 allocFlags |= GRALLOC_USAGE_PRIVATE_MM_HEAP;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700123 allocFlags |= GRALLOC_USAGE_PRIVATE_CP_BUFFER;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700124 }
Naseer Ahmed29a26812012-06-14 00:56:20 -0700125
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700126 int err = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700127 OVASSERT(numbufs && bufSz, "numbufs=%d bufSz=%d", numbufs, bufSz);
128
129 mBufSz = bufSz;
130 mNumBuffers = numbufs;
131
132 data.base = 0;
133 data.fd = -1;
134 data.offset = 0;
135 data.size = mBufSz * mNumBuffers;
136 data.align = getpagesize();
137 data.uncached = true;
138
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700139 err = mAlloc->allocate(data, allocFlags);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700140 if (err != 0) {
141 ALOGE("OvMem: error allocating memory");
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700142 return false;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700143 }
144
145 mFd = data.fd;
146 mBaseAddr = data.base;
147 mAllocType = data.allocType;
148
149 return true;
150}
151
152inline bool OvMem::close()
153{
154 int ret = 0;
155
156 if(!valid()) {
157 return true;
158 }
159
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700160 IMemAlloc* memalloc = mAlloc->getAllocator(mAllocType);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700161 ret = memalloc->free_buffer(mBaseAddr, mBufSz * mNumBuffers, 0, mFd);
162 if (ret != 0) {
163 ALOGE("OvMem: error freeing buffer");
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700164 return false;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700165 }
166
167 mFd = -1;
168 mBaseAddr = MAP_FAILED;
169 mAllocType = 0;
170 mBufSz = 0;
171 mNumBuffers = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700172 return true;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700173}
174
175inline bool OvMem::valid() const
176{
177 return (mFd != -1) && (mBaseAddr != MAP_FAILED);
178}
179
180inline int OvMem::getFD() const
181{
182 return mFd;
183}
184
185inline void* OvMem::addr() const
186{
187 return mBaseAddr;
188}
189
190inline uint32_t OvMem::bufSz() const
191{
192 return mBufSz;
193}
194
195inline uint32_t OvMem::numBufs() const
196{
197 return mNumBuffers;
198}
199
200inline void OvMem::dump() const
201{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700202 ALOGE("== Dump OvMem start ==");
203 ALOGE("fd=%d addr=%p type=%d bufsz=%u", mFd, mBaseAddr, mAllocType, mBufSz);
204 ALOGE("== Dump OvMem end ==");
Naseer Ahmed29a26812012-06-14 00:56:20 -0700205}
206
207} // overlay
208
209#endif // OVERLAY_MEM_H