Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 3 | * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <limits.h> |
| 19 | #include <errno.h> |
| 20 | #include <pthread.h> |
| 21 | #include <unistd.h> |
| 22 | #include <string.h> |
| 23 | #include <stdarg.h> |
| 24 | |
| 25 | #include <sys/mman.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/ioctl.h> |
| 29 | #include <linux/ashmem.h> |
| 30 | |
| 31 | #include <cutils/log.h> |
| 32 | #include <cutils/atomic.h> |
| 33 | #include <cutils/ashmem.h> |
| 34 | |
| 35 | #include <hardware/hardware.h> |
| 36 | #include <hardware/gralloc.h> |
| 37 | #include <genlock.h> |
| 38 | |
| 39 | #include <linux/android_pmem.h> |
| 40 | |
| 41 | #include "gralloc_priv.h" |
| 42 | #include "gr.h" |
| 43 | #include "alloc_controller.h" |
| 44 | #include "memalloc.h" |
| 45 | |
| 46 | using namespace gralloc; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 47 | /*****************************************************************************/ |
| 48 | |
| 49 | // Return the type of allocator - |
| 50 | // these are used for mapping/unmapping |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 51 | static IMemAlloc* getAllocator(int flags) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 52 | { |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 53 | IMemAlloc* memalloc; |
| 54 | IAllocController* alloc_ctrl = IAllocController::getInstance(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 55 | memalloc = alloc_ctrl->getAllocator(flags); |
| 56 | return memalloc; |
| 57 | } |
| 58 | |
| 59 | static int gralloc_map(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 60 | buffer_handle_t handle, |
| 61 | void** vaddr) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 62 | { |
| 63 | private_handle_t* hnd = (private_handle_t*)handle; |
| 64 | void *mappedAddress; |
| 65 | if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) && |
| 66 | !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) { |
| 67 | size_t size = hnd->size; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 68 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 69 | int err = memalloc->map_buffer(&mappedAddress, size, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 70 | hnd->offset, hnd->fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 71 | if(err) { |
| 72 | ALOGE("Could not mmap handle %p, fd=%d (%s)", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 73 | handle, hnd->fd, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 74 | hnd->base = 0; |
| 75 | return -errno; |
| 76 | } |
| 77 | |
| 78 | if (mappedAddress == MAP_FAILED) { |
| 79 | ALOGE("Could not mmap handle %p, fd=%d (%s)", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 80 | handle, hnd->fd, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 81 | hnd->base = 0; |
| 82 | return -errno; |
| 83 | } |
| 84 | hnd->base = intptr_t(mappedAddress) + hnd->offset; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 85 | //LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p", |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 86 | // hnd->fd, hnd->offset, hnd->size, mappedAddress); |
| 87 | } |
| 88 | *vaddr = (void*)hnd->base; |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int gralloc_unmap(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 93 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 94 | { |
| 95 | private_handle_t* hnd = (private_handle_t*)handle; |
| 96 | if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { |
| 97 | int err = -EINVAL; |
| 98 | void* base = (void*)hnd->base; |
| 99 | size_t size = hnd->size; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 100 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 101 | if(memalloc != NULL) |
| 102 | err = memalloc->unmap_buffer(base, size, hnd->offset); |
| 103 | if (err) { |
| 104 | ALOGE("Could not unmap memory at address %p", base); |
| 105 | } |
| 106 | } |
| 107 | hnd->base = 0; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | /*****************************************************************************/ |
| 112 | |
| 113 | static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER; |
| 114 | |
| 115 | /*****************************************************************************/ |
| 116 | |
| 117 | int gralloc_register_buffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 118 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 119 | { |
| 120 | if (private_handle_t::validate(handle) < 0) |
| 121 | return -EINVAL; |
| 122 | |
| 123 | // In this implementation, we don't need to do anything here |
| 124 | |
| 125 | /* NOTE: we need to initialize the buffer as not mapped/not locked |
| 126 | * because it shouldn't when this function is called the first time |
| 127 | * in a new process. Ideally these flags shouldn't be part of the |
| 128 | * handle, but instead maintained in the kernel or at least |
| 129 | * out-of-line |
| 130 | */ |
| 131 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 132 | private_handle_t* hnd = (private_handle_t*)handle; |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 133 | hnd->base = 0; |
| 134 | void *vaddr; |
| 135 | int err = gralloc_map(module, handle, &vaddr); |
| 136 | if (err) { |
| 137 | ALOGE("%s: gralloc_map failed", __FUNCTION__); |
| 138 | return err; |
| 139 | } |
| 140 | |
| 141 | // Reset the genlock private fd flag in the handle |
| 142 | hnd->genlockPrivFd = -1; |
| 143 | |
| 144 | // Check if there is a valid lock attached to the handle. |
| 145 | if (-1 == hnd->genlockHandle) { |
| 146 | ALOGE("%s: the lock is invalid.", __FUNCTION__); |
| 147 | gralloc_unmap(module, handle); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 148 | hnd->base = 0; |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 149 | return -EINVAL; |
| 150 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 151 | |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 152 | // Attach the genlock handle |
| 153 | if (GENLOCK_NO_ERROR != genlock_attach_lock((native_handle_t *)handle)) { |
| 154 | ALOGE("%s: genlock_attach_lock failed", __FUNCTION__); |
| 155 | gralloc_unmap(module, handle); |
| 156 | hnd->base = 0; |
| 157 | return -EINVAL; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | int gralloc_unregister_buffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 163 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 164 | { |
| 165 | if (private_handle_t::validate(handle) < 0) |
| 166 | return -EINVAL; |
| 167 | |
| 168 | /* |
| 169 | * If the buffer has been mapped during a lock operation, it's time |
| 170 | * to un-map it. It's an error to be here with a locked buffer. |
| 171 | * NOTE: the framebuffer is handled differently and is never unmapped. |
| 172 | */ |
| 173 | |
| 174 | private_handle_t* hnd = (private_handle_t*)handle; |
| 175 | |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 176 | if (hnd->base != 0) { |
| 177 | gralloc_unmap(module, handle); |
| 178 | } |
| 179 | hnd->base = 0; |
| 180 | // Release the genlock |
| 181 | if (-1 != hnd->genlockHandle) { |
| 182 | return genlock_release_lock((native_handle_t *)handle); |
| 183 | } else { |
| 184 | ALOGE("%s: there was no genlock attached to this buffer", __FUNCTION__); |
| 185 | return -EINVAL; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 186 | } |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | int terminateBuffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 191 | private_handle_t* hnd) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 192 | { |
| 193 | /* |
| 194 | * If the buffer has been mapped during a lock operation, it's time |
| 195 | * to un-map it. It's an error to be here with a locked buffer. |
| 196 | */ |
| 197 | |
| 198 | if (hnd->base != 0) { |
| 199 | // this buffer was mapped, unmap it now |
| 200 | if (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM | |
| 201 | private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP | |
| 202 | private_handle_t::PRIV_FLAGS_USES_ASHMEM | |
| 203 | private_handle_t::PRIV_FLAGS_USES_ION)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 204 | gralloc_unmap(module, hnd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 205 | } else { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 206 | ALOGE("terminateBuffer: unmapping a non pmem/ashmem buffer flags = 0x%x", |
| 207 | hnd->flags); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 208 | gralloc_unmap(module, hnd); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | int gralloc_lock(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 216 | buffer_handle_t handle, int usage, |
| 217 | int l, int t, int w, int h, |
| 218 | void** vaddr) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 219 | { |
| 220 | if (private_handle_t::validate(handle) < 0) |
| 221 | return -EINVAL; |
| 222 | |
| 223 | int err = 0; |
| 224 | private_handle_t* hnd = (private_handle_t*)handle; |
| 225 | if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { |
| 226 | if (hnd->base == 0) { |
| 227 | // we need to map for real |
| 228 | pthread_mutex_t* const lock = &sMapLock; |
| 229 | pthread_mutex_lock(lock); |
| 230 | err = gralloc_map(module, handle, vaddr); |
| 231 | pthread_mutex_unlock(lock); |
| 232 | } |
| 233 | *vaddr = (void*)hnd->base; |
| 234 | |
| 235 | // Lock the buffer for read/write operation as specified. Write lock |
| 236 | // has a higher priority over read lock. |
| 237 | int lockType = 0; |
| 238 | if (usage & GRALLOC_USAGE_SW_WRITE_MASK) { |
| 239 | lockType = GENLOCK_WRITE_LOCK; |
| 240 | } else if (usage & GRALLOC_USAGE_SW_READ_MASK) { |
| 241 | lockType = GENLOCK_READ_LOCK; |
| 242 | } |
| 243 | |
| 244 | int timeout = GENLOCK_MAX_TIMEOUT; |
| 245 | if (GENLOCK_NO_ERROR != genlock_lock_buffer((native_handle_t *)handle, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 246 | (genlock_lock_type)lockType, |
| 247 | timeout)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 248 | ALOGE("%s: genlock_lock_buffer (lockType=0x%x) failed", __FUNCTION__, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 249 | lockType); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 250 | return -EINVAL; |
| 251 | } else { |
| 252 | // Mark this buffer as locked for SW read/write operation. |
| 253 | hnd->flags |= private_handle_t::PRIV_FLAGS_SW_LOCK; |
| 254 | } |
| 255 | |
| 256 | if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) && |
| 257 | !(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { |
| 258 | // Mark the buffer to be flushed after cpu read/write |
| 259 | hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 260 | } |
| 261 | } |
| 262 | return err; |
| 263 | } |
| 264 | |
| 265 | int gralloc_unlock(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 266 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 267 | { |
| 268 | if (private_handle_t::validate(handle) < 0) |
| 269 | return -EINVAL; |
| 270 | |
| 271 | private_handle_t* hnd = (private_handle_t*)handle; |
| 272 | |
| 273 | if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) { |
| 274 | int err; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 275 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 276 | err = memalloc->clean_buffer((void*)hnd->base, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 277 | hnd->size, hnd->offset, hnd->fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 278 | ALOGE_IF(err < 0, "cannot flush handle %p (offs=%x len=%x, flags = 0x%x) err=%s\n", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 279 | hnd, hnd->offset, hnd->size, hnd->flags, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 280 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 281 | } |
| 282 | |
| 283 | if ((hnd->flags & private_handle_t::PRIV_FLAGS_SW_LOCK)) { |
| 284 | // Unlock the buffer. |
| 285 | if (GENLOCK_NO_ERROR != genlock_unlock_buffer((native_handle_t *)handle)) { |
| 286 | ALOGE("%s: genlock_unlock_buffer failed", __FUNCTION__); |
| 287 | return -EINVAL; |
| 288 | } else |
| 289 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_SW_LOCK; |
| 290 | } |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | /*****************************************************************************/ |
| 295 | |
| 296 | int gralloc_perform(struct gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 297 | int operation, ... ) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 298 | { |
| 299 | int res = -EINVAL; |
| 300 | va_list args; |
| 301 | va_start(args, operation); |
| 302 | switch (operation) { |
| 303 | case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: |
| 304 | { |
| 305 | int fd = va_arg(args, int); |
| 306 | size_t size = va_arg(args, size_t); |
| 307 | size_t offset = va_arg(args, size_t); |
| 308 | void* base = va_arg(args, void*); |
| 309 | int width = va_arg(args, int); |
| 310 | int height = va_arg(args, int); |
| 311 | int format = va_arg(args, int); |
| 312 | |
| 313 | native_handle_t** handle = va_arg(args, native_handle_t**); |
| 314 | int memoryFlags = va_arg(args, int); |
| 315 | private_handle_t* hnd = (private_handle_t*)native_handle_create( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 316 | private_handle_t::sNumFds, private_handle_t::sNumInts); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 317 | hnd->magic = private_handle_t::sMagic; |
| 318 | hnd->fd = fd; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 319 | hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 320 | hnd->size = size; |
| 321 | hnd->offset = offset; |
| 322 | hnd->base = intptr_t(base) + offset; |
| 323 | hnd->gpuaddr = 0; |
| 324 | hnd->width = width; |
| 325 | hnd->height = height; |
| 326 | hnd->format = format; |
| 327 | *handle = (native_handle_t *)hnd; |
| 328 | res = 0; |
| 329 | break; |
| 330 | |
| 331 | } |
| 332 | default: |
| 333 | break; |
| 334 | } |
| 335 | va_end(args); |
| 336 | return res; |
| 337 | } |