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 | #include <linux/android_pmem.h> |
| 38 | |
| 39 | #include "gralloc_priv.h" |
| 40 | #include "gr.h" |
| 41 | #include "alloc_controller.h" |
| 42 | #include "memalloc.h" |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 43 | #include <qdMetaData.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 44 | |
| 45 | using namespace gralloc; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 46 | /*****************************************************************************/ |
| 47 | |
| 48 | // Return the type of allocator - |
| 49 | // these are used for mapping/unmapping |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 50 | static IMemAlloc* getAllocator(int flags) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 51 | { |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 52 | IMemAlloc* memalloc; |
| 53 | IAllocController* alloc_ctrl = IAllocController::getInstance(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 54 | memalloc = alloc_ctrl->getAllocator(flags); |
| 55 | return memalloc; |
| 56 | } |
| 57 | |
| 58 | static int gralloc_map(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 59 | buffer_handle_t handle, |
| 60 | void** vaddr) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 61 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame^] | 62 | if(!module) |
| 63 | return -EINVAL; |
| 64 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 65 | private_handle_t* hnd = (private_handle_t*)handle; |
| 66 | void *mappedAddress; |
| 67 | if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) && |
| 68 | !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) { |
| 69 | size_t size = hnd->size; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 70 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 71 | int err = memalloc->map_buffer(&mappedAddress, size, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 72 | hnd->offset, hnd->fd); |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 73 | if(err || mappedAddress == MAP_FAILED) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 74 | ALOGE("Could not mmap handle %p, fd=%d (%s)", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 75 | handle, hnd->fd, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 76 | hnd->base = 0; |
| 77 | return -errno; |
| 78 | } |
| 79 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 80 | hnd->base = intptr_t(mappedAddress) + hnd->offset; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 81 | //LOGD("gralloc_map() succeeded fd=%d, off=%d, size=%d, vaddr=%p", |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 82 | // hnd->fd, hnd->offset, hnd->size, mappedAddress); |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 83 | mappedAddress = MAP_FAILED; |
| 84 | size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 85 | err = memalloc->map_buffer(&mappedAddress, size, |
| 86 | hnd->offset_metadata, hnd->fd_metadata); |
| 87 | if(err || mappedAddress == MAP_FAILED) { |
| 88 | ALOGE("Could not mmap handle %p, fd=%d (%s)", |
| 89 | handle, hnd->fd_metadata, strerror(errno)); |
| 90 | hnd->base_metadata = 0; |
| 91 | return -errno; |
| 92 | } |
| 93 | hnd->base_metadata = intptr_t(mappedAddress) + hnd->offset_metadata; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 94 | } |
| 95 | *vaddr = (void*)hnd->base; |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int gralloc_unmap(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 100 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 101 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame^] | 102 | if(!module) |
| 103 | return -EINVAL; |
| 104 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 105 | private_handle_t* hnd = (private_handle_t*)handle; |
| 106 | if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { |
| 107 | int err = -EINVAL; |
| 108 | void* base = (void*)hnd->base; |
| 109 | size_t size = hnd->size; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 110 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 111 | if(memalloc != NULL) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 112 | err = memalloc->unmap_buffer(base, size, hnd->offset); |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 113 | if (err) { |
| 114 | ALOGE("Could not unmap memory at address %p", base); |
| 115 | } |
| 116 | base = (void*)hnd->base_metadata; |
| 117 | size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 118 | err = memalloc->unmap_buffer(base, size, hnd->offset_metadata); |
| 119 | if (err) { |
| 120 | ALOGE("Could not unmap memory at address %p", base); |
| 121 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 122 | } |
| 123 | } |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 124 | /* need to initialize the pointer to NULL otherwise unmapping for that |
| 125 | * buffer happens twice which leads to crash */ |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 126 | hnd->base = 0; |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 127 | hnd->base_metadata = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /*****************************************************************************/ |
| 132 | |
| 133 | static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER; |
| 134 | |
| 135 | /*****************************************************************************/ |
| 136 | |
| 137 | int gralloc_register_buffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 138 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 139 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame^] | 140 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 141 | return -EINVAL; |
| 142 | |
| 143 | // In this implementation, we don't need to do anything here |
| 144 | |
| 145 | /* NOTE: we need to initialize the buffer as not mapped/not locked |
| 146 | * because it shouldn't when this function is called the first time |
| 147 | * in a new process. Ideally these flags shouldn't be part of the |
| 148 | * handle, but instead maintained in the kernel or at least |
| 149 | * out-of-line |
| 150 | */ |
| 151 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 152 | private_handle_t* hnd = (private_handle_t*)handle; |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 153 | hnd->base = 0; |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 154 | hnd->base_metadata = 0; |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 155 | void *vaddr; |
| 156 | int err = gralloc_map(module, handle, &vaddr); |
| 157 | if (err) { |
| 158 | ALOGE("%s: gralloc_map failed", __FUNCTION__); |
| 159 | return err; |
| 160 | } |
| 161 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame^] | 168 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 | |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 179 | if (hnd->base != 0) { |
| 180 | gralloc_unmap(module, handle); |
| 181 | } |
| 182 | hnd->base = 0; |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 183 | hnd->base_metadata = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | int terminateBuffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 188 | private_handle_t* hnd) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 189 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame^] | 190 | if(!module) |
| 191 | return -EINVAL; |
| 192 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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, |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame^] | 217 | int /*l*/, int /*t*/, int /*w*/, int /*h*/, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 218 | void** vaddr) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 219 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame^] | 220 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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; |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 234 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) { |
| 235 | //Invalidate if reading in software. No need to do this for the |
| 236 | //metadata buffer as it is only read/written in software. |
| 237 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
| 238 | err = memalloc->clean_buffer((void*)hnd->base, |
| 239 | hnd->size, hnd->offset, hnd->fd, |
| 240 | CACHE_INVALIDATE); |
| 241 | if (usage & GRALLOC_USAGE_SW_WRITE_MASK) { |
| 242 | // Mark the buffer to be flushed after cpu read/write |
| 243 | hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 244 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 245 | } |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 246 | } else { |
| 247 | hnd->flags |= private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 248 | } |
| 249 | return err; |
| 250 | } |
| 251 | |
| 252 | int gralloc_unlock(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 253 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 254 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame^] | 255 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 256 | return -EINVAL; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame^] | 257 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 258 | int err = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 259 | private_handle_t* hnd = (private_handle_t*)handle; |
| 260 | |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 261 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) { |
Naseer Ahmed | 69662ac | 2013-08-28 13:16:48 -0400 | [diff] [blame] | 262 | IMemAlloc* memalloc = getAllocator(hnd->flags); |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 263 | if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) { |
| 264 | err = memalloc->clean_buffer((void*)hnd->base, |
| 265 | hnd->size, hnd->offset, hnd->fd, |
| 266 | CACHE_CLEAN_AND_INVALIDATE); |
| 267 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 268 | } else if(hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) { |
| 269 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH; |
| 270 | } else { |
| 271 | //Probably a round about way to do this, but this avoids adding new |
| 272 | //flags |
| 273 | err = memalloc->clean_buffer((void*)hnd->base, |
| 274 | hnd->size, hnd->offset, hnd->fd, |
| 275 | CACHE_INVALIDATE); |
| 276 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 279 | return err; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /*****************************************************************************/ |
| 283 | |
| 284 | int gralloc_perform(struct gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 285 | int operation, ... ) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 286 | { |
| 287 | int res = -EINVAL; |
| 288 | va_list args; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame^] | 289 | if(!module) |
| 290 | return res; |
| 291 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 292 | va_start(args, operation); |
| 293 | switch (operation) { |
| 294 | case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: |
| 295 | { |
| 296 | int fd = va_arg(args, int); |
| 297 | size_t size = va_arg(args, size_t); |
| 298 | size_t offset = va_arg(args, size_t); |
| 299 | void* base = va_arg(args, void*); |
| 300 | int width = va_arg(args, int); |
| 301 | int height = va_arg(args, int); |
| 302 | int format = va_arg(args, int); |
| 303 | |
| 304 | native_handle_t** handle = va_arg(args, native_handle_t**); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 305 | private_handle_t* hnd = (private_handle_t*)native_handle_create( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 306 | private_handle_t::sNumFds, private_handle_t::sNumInts); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 307 | hnd->magic = private_handle_t::sMagic; |
| 308 | hnd->fd = fd; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 309 | hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 310 | hnd->size = size; |
| 311 | hnd->offset = offset; |
| 312 | hnd->base = intptr_t(base) + offset; |
| 313 | hnd->gpuaddr = 0; |
| 314 | hnd->width = width; |
| 315 | hnd->height = height; |
| 316 | hnd->format = format; |
| 317 | *handle = (native_handle_t *)hnd; |
| 318 | res = 0; |
| 319 | break; |
| 320 | |
| 321 | } |
Naseer Ahmed | 7421472 | 2013-02-09 08:11:36 -0500 | [diff] [blame] | 322 | #ifdef QCOM_BSP |
Ramkumar Radhakrishnan | 935cb68 | 2012-11-07 16:43:38 -0800 | [diff] [blame] | 323 | case GRALLOC_MODULE_PERFORM_UPDATE_BUFFER_GEOMETRY: |
| 324 | { |
| 325 | int width = va_arg(args, int); |
| 326 | int height = va_arg(args, int); |
| 327 | int format = va_arg(args, int); |
| 328 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 329 | if (private_handle_t::validate(hnd)) { |
| 330 | return res; |
| 331 | } |
| 332 | hnd->width = width; |
| 333 | hnd->height = height; |
| 334 | hnd->format = format; |
| 335 | res = 0; |
| 336 | } |
| 337 | break; |
Naseer Ahmed | c0593ea | 2013-03-18 11:46:24 -0400 | [diff] [blame] | 338 | #endif |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 339 | case GRALLOC_MODULE_PERFORM_GET_STRIDE: |
| 340 | { |
| 341 | int width = va_arg(args, int); |
| 342 | int format = va_arg(args, int); |
| 343 | int *stride = va_arg(args, int *); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 344 | int alignedw = 0, alignedh = 0; |
| 345 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 346 | 0, format, false, alignedw, alignedh); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 347 | *stride = alignedw; |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 348 | res = 0; |
| 349 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 350 | |
Naseer Ahmed | a978f95 | 2013-09-26 14:36:28 -0400 | [diff] [blame] | 351 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: |
| 352 | { |
| 353 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 354 | int *stride = va_arg(args, int *); |
| 355 | if (private_handle_t::validate(hnd)) { |
| 356 | return res; |
| 357 | } |
| 358 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 359 | if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { |
| 360 | *stride = metadata->bufferDim.sliceWidth; |
| 361 | } else { |
| 362 | *stride = hnd->width; |
| 363 | } |
| 364 | res = 0; |
| 365 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 366 | |
| 367 | case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: |
| 368 | { |
| 369 | int width = va_arg(args, int); |
| 370 | int height = va_arg(args, int); |
| 371 | int format = va_arg(args, int); |
| 372 | int usage = va_arg(args, int); |
| 373 | int *alignedWidth = va_arg(args, int *); |
| 374 | int *alignedHeight = va_arg(args, int *); |
| 375 | int *tileEnabled = va_arg(args,int *); |
| 376 | *tileEnabled = isMacroTileEnabled(format, usage); |
| 377 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
| 378 | height, format, *tileEnabled, *alignedWidth, |
| 379 | *alignedHeight); |
| 380 | res = 0; |
| 381 | } break; |
| 382 | |
Shuzhen Wang | c502c87 | 2014-01-28 16:10:42 -0800 | [diff] [blame] | 383 | case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: |
| 384 | { |
| 385 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 386 | int *color_space = 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_COLOR_SPACE) { |
| 392 | *color_space = metadata->colorSpace; |
| 393 | res = 0; |
| 394 | } |
| 395 | } break; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 396 | default: |
| 397 | break; |
| 398 | } |
| 399 | va_end(args); |
| 400 | return res; |
| 401 | } |