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); |
| 262 | size_t ystride, cstride; |
| 263 | if(!err) { |
| 264 | //hnd->format holds our implementation defined format |
| 265 | //HAL_PIXEL_FORMAT_YCrCb_420_SP is the only one set right now. |
| 266 | switch (hnd->format) { |
| 267 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 268 | ystride = ALIGN(hnd->width, 16); |
| 269 | cstride = ALIGN(hnd->width, 16)/2; |
| 270 | ycbcr->y = (void*)hnd->base; |
| 271 | ycbcr->cr = (void*)(hnd->base + ystride * hnd->height); |
| 272 | ycbcr->cb = (void*)(hnd->base + ystride * hnd->height + 1); |
| 273 | ycbcr->ystride = ystride; |
| 274 | ycbcr->cstride = cstride; |
| 275 | ycbcr->chroma_step = 2; |
| 276 | memset(ycbcr->reserved, 0, sizeof(ycbcr->reserved)); |
| 277 | break; |
| 278 | default: |
| 279 | ALOGD("%s: Invalid format passed: 0x%x", __FUNCTION__, |
| 280 | hnd->format); |
| 281 | err = -EINVAL; |
| 282 | } |
| 283 | } |
| 284 | return err; |
| 285 | } |
| 286 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 287 | int gralloc_unlock(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 288 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 289 | { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 290 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 291 | return -EINVAL; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 292 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 293 | int err = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 294 | private_handle_t* hnd = (private_handle_t*)handle; |
| 295 | |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 296 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) { |
Naseer Ahmed | 69662ac | 2013-08-28 13:16:48 -0400 | [diff] [blame] | 297 | IMemAlloc* memalloc = getAllocator(hnd->flags); |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 298 | if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) { |
| 299 | err = memalloc->clean_buffer((void*)hnd->base, |
| 300 | hnd->size, hnd->offset, hnd->fd, |
| 301 | CACHE_CLEAN_AND_INVALIDATE); |
| 302 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 303 | } else if(hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) { |
| 304 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH; |
| 305 | } else { |
| 306 | //Probably a round about way to do this, but this avoids adding new |
| 307 | //flags |
| 308 | err = memalloc->clean_buffer((void*)hnd->base, |
| 309 | hnd->size, hnd->offset, hnd->fd, |
| 310 | CACHE_INVALIDATE); |
| 311 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 312 | } |
| 313 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 314 | return err; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /*****************************************************************************/ |
| 318 | |
| 319 | int gralloc_perform(struct gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 320 | int operation, ... ) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 321 | { |
| 322 | int res = -EINVAL; |
| 323 | va_list args; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 324 | if(!module) |
| 325 | return res; |
| 326 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 327 | va_start(args, operation); |
| 328 | switch (operation) { |
| 329 | case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: |
| 330 | { |
| 331 | int fd = va_arg(args, int); |
| 332 | size_t size = va_arg(args, size_t); |
| 333 | size_t offset = va_arg(args, size_t); |
| 334 | void* base = va_arg(args, void*); |
| 335 | int width = va_arg(args, int); |
| 336 | int height = va_arg(args, int); |
| 337 | int format = va_arg(args, int); |
| 338 | |
| 339 | native_handle_t** handle = va_arg(args, native_handle_t**); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 340 | private_handle_t* hnd = (private_handle_t*)native_handle_create( |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 341 | private_handle_t::sNumFds, private_handle_t::sNumInts); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 342 | hnd->magic = private_handle_t::sMagic; |
| 343 | hnd->fd = fd; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 344 | hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 345 | hnd->size = size; |
| 346 | hnd->offset = offset; |
| 347 | hnd->base = intptr_t(base) + offset; |
| 348 | hnd->gpuaddr = 0; |
| 349 | hnd->width = width; |
| 350 | hnd->height = height; |
| 351 | hnd->format = format; |
| 352 | *handle = (native_handle_t *)hnd; |
| 353 | res = 0; |
| 354 | break; |
| 355 | |
| 356 | } |
Naseer Ahmed | 7421472 | 2013-02-09 08:11:36 -0500 | [diff] [blame] | 357 | #ifdef QCOM_BSP |
Ramkumar Radhakrishnan | 935cb68 | 2012-11-07 16:43:38 -0800 | [diff] [blame] | 358 | case GRALLOC_MODULE_PERFORM_UPDATE_BUFFER_GEOMETRY: |
| 359 | { |
| 360 | int width = va_arg(args, int); |
| 361 | int height = va_arg(args, int); |
| 362 | int format = va_arg(args, int); |
| 363 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 364 | if (private_handle_t::validate(hnd)) { |
| 365 | return res; |
| 366 | } |
| 367 | hnd->width = width; |
| 368 | hnd->height = height; |
| 369 | hnd->format = format; |
| 370 | res = 0; |
| 371 | } |
| 372 | break; |
Naseer Ahmed | c0593ea | 2013-03-18 11:46:24 -0400 | [diff] [blame] | 373 | #endif |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 374 | case GRALLOC_MODULE_PERFORM_GET_STRIDE: |
| 375 | { |
| 376 | int width = va_arg(args, int); |
| 377 | int format = va_arg(args, int); |
| 378 | int *stride = va_arg(args, int *); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 379 | int alignedw = 0, alignedh = 0; |
| 380 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 381 | 0, format, false, alignedw, alignedh); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 382 | *stride = alignedw; |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 383 | res = 0; |
| 384 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 385 | |
Naseer Ahmed | a978f95 | 2013-09-26 14:36:28 -0400 | [diff] [blame] | 386 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: |
| 387 | { |
| 388 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 389 | int *stride = va_arg(args, int *); |
| 390 | if (private_handle_t::validate(hnd)) { |
| 391 | return res; |
| 392 | } |
| 393 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 394 | if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { |
| 395 | *stride = metadata->bufferDim.sliceWidth; |
| 396 | } else { |
| 397 | *stride = hnd->width; |
| 398 | } |
| 399 | res = 0; |
| 400 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 401 | |
Raj Kamal | 3d01d8d | 2014-03-04 05:25:33 +0530 | [diff] [blame] | 402 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: |
| 403 | { |
| 404 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 405 | int *stride = va_arg(args, int *); |
| 406 | int *height = va_arg(args, int *); |
| 407 | if (private_handle_t::validate(hnd)) { |
| 408 | return res; |
| 409 | } |
| 410 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 411 | if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { |
| 412 | *stride = metadata->bufferDim.sliceWidth; |
| 413 | *height = metadata->bufferDim.sliceHeight; |
| 414 | } else { |
| 415 | *stride = hnd->width; |
| 416 | *height = hnd->height; |
| 417 | } |
| 418 | res = 0; |
| 419 | } break; |
| 420 | |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 421 | case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: |
| 422 | { |
| 423 | int width = va_arg(args, int); |
| 424 | int height = va_arg(args, int); |
| 425 | int format = va_arg(args, int); |
| 426 | int usage = va_arg(args, int); |
| 427 | int *alignedWidth = va_arg(args, int *); |
| 428 | int *alignedHeight = va_arg(args, int *); |
| 429 | int *tileEnabled = va_arg(args,int *); |
| 430 | *tileEnabled = isMacroTileEnabled(format, usage); |
| 431 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
| 432 | height, format, *tileEnabled, *alignedWidth, |
| 433 | *alignedHeight); |
| 434 | res = 0; |
| 435 | } break; |
| 436 | |
Shuzhen Wang | c502c87 | 2014-01-28 16:10:42 -0800 | [diff] [blame] | 437 | case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: |
| 438 | { |
| 439 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 440 | int *color_space = va_arg(args, int *); |
| 441 | if (private_handle_t::validate(hnd)) { |
| 442 | return res; |
| 443 | } |
| 444 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 445 | if(metadata && metadata->operation & UPDATE_COLOR_SPACE) { |
| 446 | *color_space = metadata->colorSpace; |
| 447 | res = 0; |
| 448 | } |
| 449 | } break; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 450 | default: |
| 451 | break; |
| 452 | } |
| 453 | va_end(args); |
| 454 | return res; |
| 455 | } |