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 | |
| 132 | // if this handle was created in this process, then we keep it as is. |
| 133 | private_handle_t* hnd = (private_handle_t*)handle; |
| 134 | if (hnd->pid != getpid()) { |
| 135 | hnd->base = 0; |
| 136 | void *vaddr; |
| 137 | int err = gralloc_map(module, handle, &vaddr); |
| 138 | if (err) { |
| 139 | ALOGE("%s: gralloc_map failed", __FUNCTION__); |
| 140 | return err; |
| 141 | } |
| 142 | |
| 143 | // Reset the genlock private fd flag in the handle |
| 144 | hnd->genlockPrivFd = -1; |
| 145 | |
| 146 | // Check if there is a valid lock attached to the handle. |
| 147 | if (-1 == hnd->genlockHandle) { |
| 148 | ALOGE("%s: the lock is invalid.", __FUNCTION__); |
| 149 | gralloc_unmap(module, handle); |
| 150 | hnd->base = 0; |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | // Attach the genlock handle |
| 155 | if (GENLOCK_NO_ERROR != genlock_attach_lock((native_handle_t *)handle)) { |
| 156 | ALOGE("%s: genlock_attach_lock failed", __FUNCTION__); |
| 157 | gralloc_unmap(module, handle); |
| 158 | hnd->base = 0; |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | } |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | int gralloc_unregister_buffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 166 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 167 | { |
| 168 | if (private_handle_t::validate(handle) < 0) |
| 169 | return -EINVAL; |
| 170 | |
| 171 | /* |
| 172 | * If the buffer has been mapped during a lock operation, it's time |
| 173 | * to un-map it. It's an error to be here with a locked buffer. |
| 174 | * NOTE: the framebuffer is handled differently and is never unmapped. |
| 175 | */ |
| 176 | |
| 177 | private_handle_t* hnd = (private_handle_t*)handle; |
| 178 | |
| 179 | // never unmap buffers that were created in this process |
| 180 | if (hnd->pid != getpid()) { |
| 181 | if (hnd->base != 0) { |
| 182 | gralloc_unmap(module, handle); |
| 183 | } |
| 184 | hnd->base = 0; |
| 185 | // Release the genlock |
| 186 | if (-1 != hnd->genlockHandle) { |
| 187 | return genlock_release_lock((native_handle_t *)handle); |
| 188 | } else { |
| 189 | ALOGE("%s: there was no genlock attached to this buffer", __FUNCTION__); |
| 190 | return -EINVAL; |
| 191 | } |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | int terminateBuffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 197 | private_handle_t* hnd) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 198 | { |
| 199 | /* |
| 200 | * If the buffer has been mapped during a lock operation, it's time |
| 201 | * to un-map it. It's an error to be here with a locked buffer. |
| 202 | */ |
| 203 | |
| 204 | if (hnd->base != 0) { |
| 205 | // this buffer was mapped, unmap it now |
| 206 | if (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM | |
| 207 | private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP | |
| 208 | private_handle_t::PRIV_FLAGS_USES_ASHMEM | |
| 209 | private_handle_t::PRIV_FLAGS_USES_ION)) { |
| 210 | if (hnd->pid != getpid()) { |
| 211 | // ... unless it's a "master" pmem buffer, that is a buffer |
| 212 | // mapped in the process it's been allocated. |
| 213 | // (see gralloc_alloc_buffer()) |
| 214 | gralloc_unmap(module, hnd); |
| 215 | } |
| 216 | } else { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 217 | ALOGE("terminateBuffer: unmapping a non pmem/ashmem buffer flags = 0x%x", |
| 218 | hnd->flags); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 219 | gralloc_unmap(module, hnd); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | int gralloc_lock(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 227 | buffer_handle_t handle, int usage, |
| 228 | int l, int t, int w, int h, |
| 229 | void** vaddr) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 230 | { |
| 231 | if (private_handle_t::validate(handle) < 0) |
| 232 | return -EINVAL; |
| 233 | |
| 234 | int err = 0; |
| 235 | private_handle_t* hnd = (private_handle_t*)handle; |
| 236 | if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { |
| 237 | if (hnd->base == 0) { |
| 238 | // we need to map for real |
| 239 | pthread_mutex_t* const lock = &sMapLock; |
| 240 | pthread_mutex_lock(lock); |
| 241 | err = gralloc_map(module, handle, vaddr); |
| 242 | pthread_mutex_unlock(lock); |
| 243 | } |
| 244 | *vaddr = (void*)hnd->base; |
| 245 | |
| 246 | // Lock the buffer for read/write operation as specified. Write lock |
| 247 | // has a higher priority over read lock. |
| 248 | int lockType = 0; |
| 249 | if (usage & GRALLOC_USAGE_SW_WRITE_MASK) { |
| 250 | lockType = GENLOCK_WRITE_LOCK; |
| 251 | } else if (usage & GRALLOC_USAGE_SW_READ_MASK) { |
| 252 | lockType = GENLOCK_READ_LOCK; |
| 253 | } |
| 254 | |
| 255 | int timeout = GENLOCK_MAX_TIMEOUT; |
| 256 | if (GENLOCK_NO_ERROR != genlock_lock_buffer((native_handle_t *)handle, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 257 | (genlock_lock_type)lockType, |
| 258 | timeout)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 259 | ALOGE("%s: genlock_lock_buffer (lockType=0x%x) failed", __FUNCTION__, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 260 | lockType); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 261 | return -EINVAL; |
| 262 | } else { |
| 263 | // Mark this buffer as locked for SW read/write operation. |
| 264 | hnd->flags |= private_handle_t::PRIV_FLAGS_SW_LOCK; |
| 265 | } |
| 266 | |
| 267 | if ((usage & GRALLOC_USAGE_SW_WRITE_MASK) && |
| 268 | !(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { |
| 269 | // Mark the buffer to be flushed after cpu read/write |
| 270 | hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 271 | } |
| 272 | } |
| 273 | return err; |
| 274 | } |
| 275 | |
| 276 | int gralloc_unlock(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 277 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 278 | { |
| 279 | if (private_handle_t::validate(handle) < 0) |
| 280 | return -EINVAL; |
| 281 | |
| 282 | private_handle_t* hnd = (private_handle_t*)handle; |
| 283 | |
| 284 | if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) { |
| 285 | int err; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 286 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 287 | err = memalloc->clean_buffer((void*)hnd->base, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 288 | hnd->size, hnd->offset, hnd->fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 289 | 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] | 290 | hnd, hnd->offset, hnd->size, hnd->flags, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 291 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 292 | } |
| 293 | |
| 294 | if ((hnd->flags & private_handle_t::PRIV_FLAGS_SW_LOCK)) { |
| 295 | // Unlock the buffer. |
| 296 | if (GENLOCK_NO_ERROR != genlock_unlock_buffer((native_handle_t *)handle)) { |
| 297 | ALOGE("%s: genlock_unlock_buffer failed", __FUNCTION__); |
| 298 | return -EINVAL; |
| 299 | } else |
| 300 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_SW_LOCK; |
| 301 | } |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | /*****************************************************************************/ |
| 306 | |
| 307 | int gralloc_perform(struct gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 308 | int operation, ... ) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 309 | { |
| 310 | int res = -EINVAL; |
| 311 | va_list args; |
| 312 | va_start(args, operation); |
| 313 | switch (operation) { |
| 314 | case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: |
| 315 | { |
| 316 | int fd = va_arg(args, int); |
| 317 | size_t size = va_arg(args, size_t); |
| 318 | size_t offset = va_arg(args, size_t); |
| 319 | void* base = va_arg(args, void*); |
| 320 | int width = va_arg(args, int); |
| 321 | int height = va_arg(args, int); |
| 322 | int format = va_arg(args, int); |
| 323 | |
| 324 | native_handle_t** handle = va_arg(args, native_handle_t**); |
| 325 | int memoryFlags = va_arg(args, int); |
| 326 | private_handle_t* hnd = (private_handle_t*)native_handle_create( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 327 | private_handle_t::sNumFds, private_handle_t::sNumInts); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 328 | hnd->magic = private_handle_t::sMagic; |
| 329 | hnd->fd = fd; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 330 | hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 331 | hnd->size = size; |
| 332 | hnd->offset = offset; |
| 333 | hnd->base = intptr_t(base) + offset; |
| 334 | hnd->gpuaddr = 0; |
| 335 | hnd->width = width; |
| 336 | hnd->height = height; |
| 337 | hnd->format = format; |
| 338 | *handle = (native_handle_t *)hnd; |
| 339 | res = 0; |
| 340 | break; |
| 341 | |
| 342 | } |
| 343 | default: |
| 344 | break; |
| 345 | } |
| 346 | va_end(args); |
| 347 | return res; |
| 348 | } |