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" |
| 41 | |
| 42 | namespace overlay { |
| 43 | |
| 44 | /* |
| 45 | * Holds base address, offset and the fd |
| 46 | * */ |
| 47 | class OvMem { |
| 48 | public: |
| 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 Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 57 | uint32_t bufSz, bool isSecure); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 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 | |
| 80 | private: |
| 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 Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 97 | gralloc::IAllocController* mAlloc; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | //-------------------Inlines----------------------------------- |
| 101 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 102 | using gralloc::IMemAlloc; |
| 103 | using gralloc::alloc_data; |
| 104 | |
| 105 | inline OvMem::OvMem() { |
| 106 | mFd = -1; |
| 107 | mBaseAddr = MAP_FAILED; |
| 108 | mAllocType = 0; |
| 109 | mBufSz = 0; |
| 110 | mNumBuffers = 0; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 111 | mAlloc = gralloc::IAllocController::getInstance(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | inline OvMem::~OvMem() { } |
| 115 | |
| 116 | inline bool OvMem::open(uint32_t numbufs, |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 117 | uint32_t bufSz, bool isSecure) |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 118 | { |
| 119 | alloc_data data; |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 120 | int allocFlags = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP; |
| 121 | if(isSecure) { |
Sushil Chauhan | faae042 | 2012-10-23 23:48:04 -0700 | [diff] [blame] | 122 | allocFlags = GRALLOC_USAGE_PRIVATE_MM_HEAP; |
Sushil Chauhan | 7651a80 | 2013-01-08 16:08:09 -0800 | [diff] [blame] | 123 | allocFlags |= GRALLOC_USAGE_PROTECTED; |
Sushil Chauhan | faae042 | 2012-10-23 23:48:04 -0700 | [diff] [blame] | 124 | allocFlags |= GRALLOC_USAGE_PRIVATE_UNCACHED; |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 125 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 126 | |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 127 | int err = 0; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 128 | OVASSERT(numbufs && bufSz, "numbufs=%d bufSz=%d", numbufs, bufSz); |
| 129 | |
| 130 | mBufSz = bufSz; |
| 131 | mNumBuffers = numbufs; |
| 132 | |
| 133 | data.base = 0; |
| 134 | data.fd = -1; |
| 135 | data.offset = 0; |
| 136 | data.size = mBufSz * mNumBuffers; |
| 137 | data.align = getpagesize(); |
| 138 | data.uncached = true; |
| 139 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 140 | err = mAlloc->allocate(data, allocFlags); |
Naseer Ahmed | c72e7dc | 2012-07-31 19:12:51 -0700 | [diff] [blame] | 141 | //see if we can fallback to other heap |
| 142 | //we can try MM_HEAP once if it's not secure playback |
| 143 | if (err != 0 && !isSecure) { |
| 144 | allocFlags |= GRALLOC_USAGE_PRIVATE_MM_HEAP; |
| 145 | err = mAlloc->allocate(data, allocFlags); |
| 146 | if (err != 0) { |
| 147 | ALOGE(" could not allocate from fallback heap"); |
| 148 | return false; |
| 149 | } |
| 150 | } else if (err != 0) { |
| 151 | ALOGE("OvMem: error allocating memory can not fall back"); |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 152 | return false; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Naseer Ahmed | c72e7dc | 2012-07-31 19:12:51 -0700 | [diff] [blame] | 155 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 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); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 172 | ret = memalloc->free_buffer(mBaseAddr, mBufSz * mNumBuffers, 0, mFd); |
| 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; |
| 182 | mNumBuffers = 0; |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 183 | return true; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | inline bool OvMem::valid() const |
| 187 | { |
| 188 | return (mFd != -1) && (mBaseAddr != MAP_FAILED); |
| 189 | } |
| 190 | |
| 191 | inline int OvMem::getFD() const |
| 192 | { |
| 193 | return mFd; |
| 194 | } |
| 195 | |
| 196 | inline void* OvMem::addr() const |
| 197 | { |
| 198 | return mBaseAddr; |
| 199 | } |
| 200 | |
| 201 | inline uint32_t OvMem::bufSz() const |
| 202 | { |
| 203 | return mBufSz; |
| 204 | } |
| 205 | |
| 206 | inline uint32_t OvMem::numBufs() const |
| 207 | { |
| 208 | return mNumBuffers; |
| 209 | } |
| 210 | |
| 211 | inline void OvMem::dump() const |
| 212 | { |
Naseer Ahmed | f48aef6 | 2012-07-20 09:05:53 -0700 | [diff] [blame] | 213 | ALOGE("== Dump OvMem start =="); |
| 214 | ALOGE("fd=%d addr=%p type=%d bufsz=%u", mFd, mBaseAddr, mAllocType, mBufSz); |
| 215 | ALOGE("== Dump OvMem end =="); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | } // overlay |
| 219 | |
| 220 | #endif // OVERLAY_MEM_H |