Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 1 | /* |
Duy Truong | 73d36df | 2013-02-09 20:33:23 -0800 | [diff] [blame] | 2 | * Copyright (c) 2011, The Linux Foundation. All rights reserved. |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 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. |
Duy Truong | 73d36df | 2013-02-09 20:33:23 -0800 | [diff] [blame] | 13 | * * Neither the name of The Linux Foundation nor the names of its |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 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" |
Tatenda Chipeperekwa | 1c41212 | 2013-10-17 11:29:41 -0700 | [diff] [blame] | 41 | #define SIZE_1M 0x00100000 |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 42 | |
| 43 | namespace overlay { |
| 44 | |
| 45 | /* |
| 46 | * Holds base address, offset and the fd |
| 47 | * */ |
| 48 | class OvMem { |
| 49 | public: |
| 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 Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 58 | uint32_t bufSz, bool isSecure); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 59 | |
| 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 | |
| 81 | private: |
| 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 | |
Tatenda Chipeperekwa | 59e467e | 2013-11-01 10:24:56 -0700 | [diff] [blame] | 91 | /* holds buf size sent down by the client */ |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 92 | uint32_t mBufSz; |
| 93 | |
| 94 | /* num of bufs */ |
| 95 | uint32_t mNumBuffers; |
| 96 | |
| 97 | /* gralloc alloc controller */ |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 98 | gralloc::IAllocController* mAlloc; |
Tatenda Chipeperekwa | 59e467e | 2013-11-01 10:24:56 -0700 | [diff] [blame] | 99 | |
| 100 | /*Holds the aligned buffer size used for actual allocation*/ |
| 101 | uint32_t mBufSzAligned; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | //-------------------Inlines----------------------------------- |
| 105 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 106 | using gralloc::IMemAlloc; |
| 107 | using gralloc::alloc_data; |
| 108 | |
| 109 | inline OvMem::OvMem() { |
| 110 | mFd = -1; |
| 111 | mBaseAddr = MAP_FAILED; |
| 112 | mAllocType = 0; |
| 113 | mBufSz = 0; |
| 114 | mNumBuffers = 0; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 115 | mAlloc = gralloc::IAllocController::getInstance(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | inline OvMem::~OvMem() { } |
| 119 | |
| 120 | inline bool OvMem::open(uint32_t numbufs, |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 121 | uint32_t bufSz, bool isSecure) |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 122 | { |
| 123 | alloc_data data; |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 124 | int allocFlags = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP; |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 125 | int err = 0; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 126 | OVASSERT(numbufs && bufSz, "numbufs=%d bufSz=%d", numbufs, bufSz); |
Tatenda Chipeperekwa | 59e467e | 2013-11-01 10:24:56 -0700 | [diff] [blame] | 127 | mBufSz = bufSz; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 128 | |
Tatenda Chipeperekwa | 1c41212 | 2013-10-17 11:29:41 -0700 | [diff] [blame] | 129 | if(isSecure) { |
| 130 | allocFlags = GRALLOC_USAGE_PRIVATE_MM_HEAP; |
| 131 | allocFlags |= GRALLOC_USAGE_PROTECTED; |
Tatenda Chipeperekwa | 59e467e | 2013-11-01 10:24:56 -0700 | [diff] [blame] | 132 | mBufSzAligned = utils::align(bufSz, SIZE_1M); |
Tatenda Chipeperekwa | 1c41212 | 2013-10-17 11:29:41 -0700 | [diff] [blame] | 133 | data.align = SIZE_1M; |
| 134 | } else { |
Tatenda Chipeperekwa | 59e467e | 2013-11-01 10:24:56 -0700 | [diff] [blame] | 135 | mBufSzAligned = bufSz; |
Tatenda Chipeperekwa | 1c41212 | 2013-10-17 11:29:41 -0700 | [diff] [blame] | 136 | data.align = getpagesize(); |
| 137 | } |
| 138 | |
| 139 | // Allocate uncached rotator buffers |
| 140 | allocFlags |= GRALLOC_USAGE_PRIVATE_UNCACHED; |
| 141 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 142 | mNumBuffers = numbufs; |
| 143 | |
| 144 | data.base = 0; |
| 145 | data.fd = -1; |
| 146 | data.offset = 0; |
Tatenda Chipeperekwa | 59e467e | 2013-11-01 10:24:56 -0700 | [diff] [blame] | 147 | data.size = mBufSzAligned * mNumBuffers; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 148 | data.uncached = true; |
| 149 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 150 | err = mAlloc->allocate(data, allocFlags); |
Naseer Ahmed | c5e6fb0 | 2013-03-07 13:42:20 -0500 | [diff] [blame] | 151 | if (err != 0) { |
| 152 | ALOGE("OvMem: Error allocating memory"); |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 153 | return false; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | mFd = data.fd; |
| 157 | mBaseAddr = data.base; |
| 158 | mAllocType = data.allocType; |
| 159 | |
| 160 | return true; |
| 161 | } |
| 162 | |
| 163 | inline bool OvMem::close() |
| 164 | { |
| 165 | int ret = 0; |
| 166 | |
| 167 | if(!valid()) { |
| 168 | return true; |
| 169 | } |
| 170 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 171 | IMemAlloc* memalloc = mAlloc->getAllocator(mAllocType); |
Tatenda Chipeperekwa | 59e467e | 2013-11-01 10:24:56 -0700 | [diff] [blame] | 172 | ret = memalloc->free_buffer(mBaseAddr, mBufSzAligned * mNumBuffers, 0, mFd); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 173 | if (ret != 0) { |
| 174 | ALOGE("OvMem: error freeing buffer"); |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 175 | return false; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | mFd = -1; |
| 179 | mBaseAddr = MAP_FAILED; |
| 180 | mAllocType = 0; |
| 181 | mBufSz = 0; |
Tatenda Chipeperekwa | 59e467e | 2013-11-01 10:24:56 -0700 | [diff] [blame] | 182 | mBufSzAligned = 0; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 183 | mNumBuffers = 0; |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 184 | return true; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | inline bool OvMem::valid() const |
| 188 | { |
| 189 | return (mFd != -1) && (mBaseAddr != MAP_FAILED); |
| 190 | } |
| 191 | |
| 192 | inline int OvMem::getFD() const |
| 193 | { |
| 194 | return mFd; |
| 195 | } |
| 196 | |
| 197 | inline void* OvMem::addr() const |
| 198 | { |
| 199 | return mBaseAddr; |
| 200 | } |
| 201 | |
| 202 | inline uint32_t OvMem::bufSz() const |
| 203 | { |
| 204 | return mBufSz; |
| 205 | } |
| 206 | |
| 207 | inline uint32_t OvMem::numBufs() const |
| 208 | { |
| 209 | return mNumBuffers; |
| 210 | } |
| 211 | |
| 212 | inline void OvMem::dump() const |
| 213 | { |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 214 | ALOGE("== Dump OvMem start =="); |
Tatenda Chipeperekwa | 59e467e | 2013-11-01 10:24:56 -0700 | [diff] [blame] | 215 | ALOGE("fd=%d addr=%p type=%d bufsz=%u AlignedBufSz=%u", |
| 216 | mFd, mBaseAddr, mAllocType, mBufSz, mBufSzAligned); |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 217 | ALOGE("== Dump OvMem end =="); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | } // overlay |
| 221 | |
| 222 | #endif // OVERLAY_MEM_H |