blob: 5e0db6f3d9ac4fb968799fdc3f384a63df2113c8 [file] [log] [blame]
Naseer Ahmed29a26812012-06-14 00:56:20 -07001/*
Duy Truong73d36df2013-02-09 20:33:23 -08002* Copyright (c) 2011, The Linux Foundation. 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.
Duy Truong73d36df2013-02-09 20:33:23 -080013* * Neither the name of The Linux Foundation nor the names of its
Naseer Ahmed29a26812012-06-14 00:56:20 -070014* 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"
Tatenda Chipeperekwa1c412122013-10-17 11:29:41 -070041#define SIZE_1M 0x00100000
Naseer Ahmed29a26812012-06-14 00:56:20 -070042
43namespace overlay {
44
45/*
46* Holds base address, offset and the fd
47* */
48class OvMem {
49public:
50 /* ctor init*/
51 explicit OvMem();
52
53 /* dtor DO NOT call close so it can be copied */
54 ~OvMem();
55
56 /* Use libgralloc to retrieve fd, base addr, alloc type */
57 bool open(uint32_t numbufs,
Naseer Ahmedf48aef62012-07-20 09:05:53 -070058 uint32_t bufSz, bool isSecure);
Naseer Ahmed29a26812012-06-14 00:56:20 -070059
60 /* close fd. assign base address to invalid*/
61 bool close();
62
63 /* return underlying fd */
64 int getFD() const;
65
66 /* return true if fd is valid and base address is valid */
67 bool valid() const;
68
69 /* dump the state of the object */
70 void dump() const;
71
72 /* return underlying address */
73 void* addr() const;
74
75 /* return underlying offset */
76 uint32_t bufSz() const;
77
78 /* return number of bufs */
79 uint32_t numBufs() const ;
80
81private:
82 /* actual os fd */
83 int mFd;
84
85 /* points to base addr (mmap)*/
86 void* mBaseAddr;
87
88 /* allocated buffer type determined by gralloc (ashmem, ion, etc) */
89 int mAllocType;
90
91 /* holds buf size */
92 uint32_t mBufSz;
93
94 /* num of bufs */
95 uint32_t mNumBuffers;
96
97 /* gralloc alloc controller */
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070098 gralloc::IAllocController* mAlloc;
Naseer Ahmed29a26812012-06-14 00:56:20 -070099};
100
101//-------------------Inlines-----------------------------------
102
Naseer Ahmed29a26812012-06-14 00:56:20 -0700103using 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;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700112 mAlloc = gralloc::IAllocController::getInstance();
Naseer Ahmed29a26812012-06-14 00:56:20 -0700113}
114
115inline OvMem::~OvMem() { }
116
117inline bool OvMem::open(uint32_t numbufs,
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700118 uint32_t bufSz, bool isSecure)
Naseer Ahmed29a26812012-06-14 00:56:20 -0700119{
120 alloc_data data;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700121 int allocFlags = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700122 int err = 0;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700123 OVASSERT(numbufs && bufSz, "numbufs=%d bufSz=%d", numbufs, bufSz);
124
Tatenda Chipeperekwa1c412122013-10-17 11:29:41 -0700125 if(isSecure) {
126 allocFlags = GRALLOC_USAGE_PRIVATE_MM_HEAP;
127 allocFlags |= GRALLOC_USAGE_PROTECTED;
128 mBufSz = utils::align(bufSz, SIZE_1M);
129 data.align = SIZE_1M;
130 } else {
131 mBufSz = bufSz;
132 data.align = getpagesize();
133 }
134
135 // Allocate uncached rotator buffers
136 allocFlags |= GRALLOC_USAGE_PRIVATE_UNCACHED;
137
Naseer Ahmed29a26812012-06-14 00:56:20 -0700138 mNumBuffers = numbufs;
139
140 data.base = 0;
141 data.fd = -1;
142 data.offset = 0;
143 data.size = mBufSz * mNumBuffers;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700144 data.uncached = true;
145
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700146 err = mAlloc->allocate(data, allocFlags);
Naseer Ahmedc5e6fb02013-03-07 13:42:20 -0500147 if (err != 0) {
148 ALOGE("OvMem: Error allocating memory");
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700149 return false;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700150 }
151
152 mFd = data.fd;
153 mBaseAddr = data.base;
154 mAllocType = data.allocType;
155
156 return true;
157}
158
159inline bool OvMem::close()
160{
161 int ret = 0;
162
163 if(!valid()) {
164 return true;
165 }
166
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700167 IMemAlloc* memalloc = mAlloc->getAllocator(mAllocType);
Naseer Ahmed29a26812012-06-14 00:56:20 -0700168 ret = memalloc->free_buffer(mBaseAddr, mBufSz * mNumBuffers, 0, mFd);
169 if (ret != 0) {
170 ALOGE("OvMem: error freeing buffer");
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700171 return false;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700172 }
173
174 mFd = -1;
175 mBaseAddr = MAP_FAILED;
176 mAllocType = 0;
177 mBufSz = 0;
178 mNumBuffers = 0;
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700179 return true;
Naseer Ahmed29a26812012-06-14 00:56:20 -0700180}
181
182inline bool OvMem::valid() const
183{
184 return (mFd != -1) && (mBaseAddr != MAP_FAILED);
185}
186
187inline int OvMem::getFD() const
188{
189 return mFd;
190}
191
192inline void* OvMem::addr() const
193{
194 return mBaseAddr;
195}
196
197inline uint32_t OvMem::bufSz() const
198{
199 return mBufSz;
200}
201
202inline uint32_t OvMem::numBufs() const
203{
204 return mNumBuffers;
205}
206
207inline void OvMem::dump() const
208{
Naseer Ahmedf48aef62012-07-20 09:05:53 -0700209 ALOGE("== Dump OvMem start ==");
210 ALOGE("fd=%d addr=%p type=%d bufsz=%u", mFd, mBaseAddr, mAllocType, mBufSz);
211 ALOGE("== Dump OvMem end ==");
Naseer Ahmed29a26812012-06-14 00:56:20 -0700212}
213
214} // overlay
215
216#endif // OVERLAY_MEM_H