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