Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
Shuzhen Wang | c502c87 | 2014-01-28 16:10:42 -0800 | [diff] [blame] | 3 | * Copyright (c) 2011-2014, The Linux Foundation. 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> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 37 | |
| 38 | #include "gralloc_priv.h" |
| 39 | #include "gr.h" |
| 40 | #include "alloc_controller.h" |
| 41 | #include "memalloc.h" |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 42 | #include <qdMetaData.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 43 | |
| 44 | using namespace gralloc; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 45 | /*****************************************************************************/ |
| 46 | |
| 47 | // Return the type of allocator - |
| 48 | // these are used for mapping/unmapping |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 49 | static IMemAlloc* getAllocator(int flags) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 50 | { |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 51 | IMemAlloc* memalloc; |
| 52 | IAllocController* alloc_ctrl = IAllocController::getInstance(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 53 | memalloc = alloc_ctrl->getAllocator(flags); |
| 54 | return memalloc; |
| 55 | } |
| 56 | |
| 57 | static int gralloc_map(gralloc_module_t const* module, |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 58 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 59 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 60 | if(!module) |
| 61 | return -EINVAL; |
| 62 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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); |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 71 | if(err || mappedAddress == MAP_FAILED) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 78 | hnd->base = intptr_t(mappedAddress) + hnd->offset; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 79 | mappedAddress = MAP_FAILED; |
| 80 | size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 81 | err = memalloc->map_buffer(&mappedAddress, size, |
| 82 | hnd->offset_metadata, hnd->fd_metadata); |
| 83 | if(err || mappedAddress == MAP_FAILED) { |
| 84 | ALOGE("Could not mmap handle %p, fd=%d (%s)", |
| 85 | handle, hnd->fd_metadata, strerror(errno)); |
| 86 | hnd->base_metadata = 0; |
| 87 | return -errno; |
| 88 | } |
| 89 | hnd->base_metadata = intptr_t(mappedAddress) + hnd->offset_metadata; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 90 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | static int gralloc_unmap(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 95 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 96 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 97 | if(!module) |
| 98 | return -EINVAL; |
| 99 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 100 | private_handle_t* hnd = (private_handle_t*)handle; |
| 101 | if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { |
| 102 | int err = -EINVAL; |
| 103 | void* base = (void*)hnd->base; |
| 104 | size_t size = hnd->size; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 105 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 106 | if(memalloc != NULL) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 107 | err = memalloc->unmap_buffer(base, size, hnd->offset); |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 108 | if (err) { |
| 109 | ALOGE("Could not unmap memory at address %p", base); |
| 110 | } |
| 111 | base = (void*)hnd->base_metadata; |
| 112 | size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 113 | err = memalloc->unmap_buffer(base, size, hnd->offset_metadata); |
| 114 | if (err) { |
| 115 | ALOGE("Could not unmap memory at address %p", base); |
| 116 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 119 | /* need to initialize the pointer to NULL otherwise unmapping for that |
| 120 | * buffer happens twice which leads to crash */ |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 121 | hnd->base = 0; |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 122 | hnd->base_metadata = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | /*****************************************************************************/ |
| 127 | |
| 128 | static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER; |
| 129 | |
| 130 | /*****************************************************************************/ |
| 131 | |
| 132 | int gralloc_register_buffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 133 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 134 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 135 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 136 | return -EINVAL; |
| 137 | |
| 138 | // In this implementation, we don't need to do anything here |
| 139 | |
| 140 | /* NOTE: we need to initialize the buffer as not mapped/not locked |
| 141 | * because it shouldn't when this function is called the first time |
| 142 | * in a new process. Ideally these flags shouldn't be part of the |
| 143 | * handle, but instead maintained in the kernel or at least |
| 144 | * out-of-line |
| 145 | */ |
| 146 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 147 | private_handle_t* hnd = (private_handle_t*)handle; |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 148 | hnd->base = 0; |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 149 | hnd->base_metadata = 0; |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 150 | int err = gralloc_map(module, handle); |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 151 | if (err) { |
| 152 | ALOGE("%s: gralloc_map failed", __FUNCTION__); |
| 153 | return err; |
| 154 | } |
| 155 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | int gralloc_unregister_buffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 160 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 161 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 162 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 163 | return -EINVAL; |
| 164 | |
| 165 | /* |
| 166 | * If the buffer has been mapped during a lock operation, it's time |
| 167 | * to un-map it. It's an error to be here with a locked buffer. |
| 168 | * NOTE: the framebuffer is handled differently and is never unmapped. |
| 169 | */ |
| 170 | |
| 171 | private_handle_t* hnd = (private_handle_t*)handle; |
| 172 | |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 173 | if (hnd->base != 0) { |
| 174 | gralloc_unmap(module, handle); |
| 175 | } |
| 176 | hnd->base = 0; |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 177 | hnd->base_metadata = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | int terminateBuffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 182 | private_handle_t* hnd) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 183 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 184 | if(!module) |
| 185 | return -EINVAL; |
| 186 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 187 | /* |
| 188 | * If the buffer has been mapped during a lock operation, it's time |
| 189 | * to un-map it. It's an error to be here with a locked buffer. |
| 190 | */ |
| 191 | |
| 192 | if (hnd->base != 0) { |
| 193 | // this buffer was mapped, unmap it now |
| 194 | if (hnd->flags & (private_handle_t::PRIV_FLAGS_USES_PMEM | |
| 195 | private_handle_t::PRIV_FLAGS_USES_PMEM_ADSP | |
| 196 | private_handle_t::PRIV_FLAGS_USES_ASHMEM | |
| 197 | private_handle_t::PRIV_FLAGS_USES_ION)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 198 | gralloc_unmap(module, hnd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 199 | } else { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 200 | ALOGE("terminateBuffer: unmapping a non pmem/ashmem buffer flags = 0x%x", |
| 201 | hnd->flags); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 202 | gralloc_unmap(module, hnd); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 209 | static int gralloc_map_and_invalidate (gralloc_module_t const* module, |
| 210 | buffer_handle_t handle, int usage) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 211 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 212 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 213 | return -EINVAL; |
| 214 | |
| 215 | int err = 0; |
| 216 | private_handle_t* hnd = (private_handle_t*)handle; |
| 217 | if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { |
| 218 | if (hnd->base == 0) { |
| 219 | // we need to map for real |
| 220 | pthread_mutex_t* const lock = &sMapLock; |
| 221 | pthread_mutex_lock(lock); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 222 | err = gralloc_map(module, handle); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 223 | pthread_mutex_unlock(lock); |
| 224 | } |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 225 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) { |
| 226 | //Invalidate if reading in software. No need to do this for the |
| 227 | //metadata buffer as it is only read/written in software. |
| 228 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
| 229 | err = memalloc->clean_buffer((void*)hnd->base, |
| 230 | hnd->size, hnd->offset, hnd->fd, |
| 231 | CACHE_INVALIDATE); |
| 232 | if (usage & GRALLOC_USAGE_SW_WRITE_MASK) { |
| 233 | // Mark the buffer to be flushed after cpu read/write |
| 234 | hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 235 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 236 | } |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 237 | } else { |
| 238 | hnd->flags |= private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 239 | } |
| 240 | return err; |
| 241 | } |
| 242 | |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 243 | int gralloc_lock(gralloc_module_t const* module, |
| 244 | buffer_handle_t handle, int usage, |
| 245 | int /*l*/, int /*t*/, int /*w*/, int /*h*/, |
| 246 | void** vaddr) |
| 247 | { |
| 248 | private_handle_t* hnd = (private_handle_t*)handle; |
| 249 | int err = gralloc_map_and_invalidate(module, handle, usage); |
| 250 | if(!err) |
| 251 | *vaddr = (void*)hnd->base; |
| 252 | return err; |
| 253 | } |
| 254 | |
| 255 | int gralloc_lock_ycbcr(gralloc_module_t const* module, |
| 256 | buffer_handle_t handle, int usage, |
| 257 | int /*l*/, int /*t*/, int /*w*/, int /*h*/, |
| 258 | struct android_ycbcr *ycbcr) |
| 259 | { |
| 260 | private_handle_t* hnd = (private_handle_t*)handle; |
| 261 | int err = gralloc_map_and_invalidate(module, handle, usage); |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 262 | if(!err) |
| 263 | err = getYUVPlaneInfo(hnd, ycbcr); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 264 | return err; |
| 265 | } |
| 266 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 267 | int gralloc_unlock(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 268 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 269 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 270 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 271 | return -EINVAL; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 272 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 273 | int err = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 274 | private_handle_t* hnd = (private_handle_t*)handle; |
| 275 | |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 276 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) { |
Naseer Ahmed | 69662ac | 2013-08-28 13:16:48 -0400 | [diff] [blame] | 277 | IMemAlloc* memalloc = getAllocator(hnd->flags); |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 278 | if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) { |
| 279 | err = memalloc->clean_buffer((void*)hnd->base, |
| 280 | hnd->size, hnd->offset, hnd->fd, |
| 281 | CACHE_CLEAN_AND_INVALIDATE); |
| 282 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 283 | } else if(hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) { |
| 284 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH; |
| 285 | } else { |
| 286 | //Probably a round about way to do this, but this avoids adding new |
| 287 | //flags |
| 288 | err = memalloc->clean_buffer((void*)hnd->base, |
| 289 | hnd->size, hnd->offset, hnd->fd, |
| 290 | CACHE_INVALIDATE); |
| 291 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 294 | return err; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /*****************************************************************************/ |
| 298 | |
| 299 | int gralloc_perform(struct gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 300 | int operation, ... ) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 301 | { |
| 302 | int res = -EINVAL; |
| 303 | va_list args; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 304 | if(!module) |
| 305 | return res; |
| 306 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 307 | va_start(args, operation); |
| 308 | switch (operation) { |
| 309 | case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: |
| 310 | { |
| 311 | int fd = va_arg(args, int); |
| 312 | size_t size = va_arg(args, size_t); |
| 313 | size_t offset = va_arg(args, size_t); |
| 314 | void* base = va_arg(args, void*); |
| 315 | int width = va_arg(args, int); |
| 316 | int height = va_arg(args, int); |
| 317 | int format = va_arg(args, int); |
| 318 | |
| 319 | native_handle_t** handle = va_arg(args, native_handle_t**); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 320 | private_handle_t* hnd = (private_handle_t*)native_handle_create( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 321 | private_handle_t::sNumFds, private_handle_t::sNumInts); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 322 | hnd->magic = private_handle_t::sMagic; |
| 323 | hnd->fd = fd; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 324 | hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 325 | hnd->size = size; |
| 326 | hnd->offset = offset; |
| 327 | hnd->base = intptr_t(base) + offset; |
| 328 | hnd->gpuaddr = 0; |
| 329 | hnd->width = width; |
| 330 | hnd->height = height; |
| 331 | hnd->format = format; |
| 332 | *handle = (native_handle_t *)hnd; |
| 333 | res = 0; |
| 334 | break; |
| 335 | |
| 336 | } |
Naseer Ahmed | 7421472 | 2013-02-09 08:11:36 -0500 | [diff] [blame] | 337 | #ifdef QCOM_BSP |
Ramkumar Radhakrishnan | 935cb68 | 2012-11-07 16:43:38 -0800 | [diff] [blame] | 338 | case GRALLOC_MODULE_PERFORM_UPDATE_BUFFER_GEOMETRY: |
| 339 | { |
| 340 | int width = va_arg(args, int); |
| 341 | int height = va_arg(args, int); |
| 342 | int format = va_arg(args, int); |
| 343 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 344 | if (private_handle_t::validate(hnd)) { |
| 345 | return res; |
| 346 | } |
| 347 | hnd->width = width; |
| 348 | hnd->height = height; |
| 349 | hnd->format = format; |
| 350 | res = 0; |
| 351 | } |
| 352 | break; |
Naseer Ahmed | c0593ea | 2013-03-18 11:46:24 -0400 | [diff] [blame] | 353 | #endif |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 354 | case GRALLOC_MODULE_PERFORM_GET_STRIDE: |
| 355 | { |
| 356 | int width = va_arg(args, int); |
| 357 | int format = va_arg(args, int); |
| 358 | int *stride = va_arg(args, int *); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 359 | int alignedw = 0, alignedh = 0; |
| 360 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 361 | 0, format, false, alignedw, alignedh); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 362 | *stride = alignedw; |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 363 | res = 0; |
| 364 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 365 | |
Naseer Ahmed | a978f95 | 2013-09-26 14:36:28 -0400 | [diff] [blame] | 366 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: |
| 367 | { |
| 368 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 369 | int *stride = va_arg(args, int *); |
| 370 | if (private_handle_t::validate(hnd)) { |
| 371 | return res; |
| 372 | } |
| 373 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 374 | if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { |
| 375 | *stride = metadata->bufferDim.sliceWidth; |
| 376 | } else { |
| 377 | *stride = hnd->width; |
| 378 | } |
| 379 | res = 0; |
| 380 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 381 | |
Raj Kamal | 3d01d8d | 2014-03-04 05:25:33 +0530 | [diff] [blame] | 382 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: |
| 383 | { |
| 384 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 385 | int *stride = va_arg(args, int *); |
| 386 | int *height = va_arg(args, int *); |
| 387 | if (private_handle_t::validate(hnd)) { |
| 388 | return res; |
| 389 | } |
| 390 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 391 | if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { |
| 392 | *stride = metadata->bufferDim.sliceWidth; |
| 393 | *height = metadata->bufferDim.sliceHeight; |
| 394 | } else { |
| 395 | *stride = hnd->width; |
| 396 | *height = hnd->height; |
| 397 | } |
| 398 | res = 0; |
| 399 | } break; |
| 400 | |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 401 | case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: |
| 402 | { |
| 403 | int width = va_arg(args, int); |
| 404 | int height = va_arg(args, int); |
| 405 | int format = va_arg(args, int); |
| 406 | int usage = va_arg(args, int); |
| 407 | int *alignedWidth = va_arg(args, int *); |
| 408 | int *alignedHeight = va_arg(args, int *); |
| 409 | int *tileEnabled = va_arg(args,int *); |
| 410 | *tileEnabled = isMacroTileEnabled(format, usage); |
| 411 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
| 412 | height, format, *tileEnabled, *alignedWidth, |
| 413 | *alignedHeight); |
| 414 | res = 0; |
| 415 | } break; |
| 416 | |
Shuzhen Wang | c502c87 | 2014-01-28 16:10:42 -0800 | [diff] [blame] | 417 | case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: |
| 418 | { |
| 419 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 420 | int *color_space = va_arg(args, int *); |
| 421 | if (private_handle_t::validate(hnd)) { |
| 422 | return res; |
| 423 | } |
| 424 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 425 | if(metadata && metadata->operation & UPDATE_COLOR_SPACE) { |
| 426 | *color_space = metadata->colorSpace; |
| 427 | res = 0; |
| 428 | } |
| 429 | } break; |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 430 | case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: |
| 431 | { |
| 432 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 433 | android_ycbcr* ycbcr = va_arg(args, struct android_ycbcr *); |
Naseer Ahmed | f5ff33a | 2014-04-30 15:30:39 -0400 | [diff] [blame] | 434 | if (!private_handle_t::validate(hnd)) { |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 435 | res = getYUVPlaneInfo(hnd, ycbcr); |
| 436 | } |
| 437 | } break; |
| 438 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 439 | default: |
| 440 | break; |
| 441 | } |
| 442 | va_end(args); |
| 443 | return res; |
| 444 | } |