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