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 | |
Naseer Ahmed | 9eb5e09 | 2014-09-25 13:24:44 -0400 | [diff] [blame] | 18 | #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 19 | #include <limits.h> |
| 20 | #include <errno.h> |
| 21 | #include <pthread.h> |
| 22 | #include <unistd.h> |
| 23 | #include <string.h> |
| 24 | #include <stdarg.h> |
| 25 | |
| 26 | #include <sys/mman.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <sys/ioctl.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 30 | |
| 31 | #include <cutils/log.h> |
| 32 | #include <cutils/atomic.h> |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 33 | #include <utils/Trace.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 | |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 44 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 61 | ATRACE_CALL(); |
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)) { |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 69 | unsigned int 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 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 80 | hnd->base = uint64_t(mappedAddress) + hnd->offset; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 81 | mappedAddress = MAP_FAILED; |
| 82 | size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 83 | err = memalloc->map_buffer(&mappedAddress, size, |
| 84 | hnd->offset_metadata, hnd->fd_metadata); |
| 85 | if(err || mappedAddress == MAP_FAILED) { |
| 86 | ALOGE("Could not mmap handle %p, fd=%d (%s)", |
| 87 | handle, hnd->fd_metadata, strerror(errno)); |
| 88 | hnd->base_metadata = 0; |
| 89 | return -errno; |
| 90 | } |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 91 | hnd->base_metadata = uint64_t(mappedAddress) + hnd->offset_metadata; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 92 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | static int gralloc_unmap(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 97 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 98 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 99 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 100 | if(!module) |
| 101 | return -EINVAL; |
| 102 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 103 | private_handle_t* hnd = (private_handle_t*)handle; |
| 104 | if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { |
| 105 | int err = -EINVAL; |
| 106 | void* base = (void*)hnd->base; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 107 | unsigned int size = hnd->size; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 108 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 109 | if(memalloc != NULL) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 110 | err = memalloc->unmap_buffer(base, size, hnd->offset); |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 111 | if (err) { |
| 112 | ALOGE("Could not unmap memory at address %p", base); |
| 113 | } |
| 114 | base = (void*)hnd->base_metadata; |
| 115 | size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 116 | err = memalloc->unmap_buffer(base, size, hnd->offset_metadata); |
| 117 | if (err) { |
| 118 | ALOGE("Could not unmap memory at address %p", base); |
| 119 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 122 | /* need to initialize the pointer to NULL otherwise unmapping for that |
| 123 | * buffer happens twice which leads to crash */ |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 124 | hnd->base = 0; |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 125 | hnd->base_metadata = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /*****************************************************************************/ |
| 130 | |
| 131 | static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER; |
| 132 | |
| 133 | /*****************************************************************************/ |
| 134 | |
| 135 | int gralloc_register_buffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 136 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 137 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 138 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 139 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 140 | return -EINVAL; |
| 141 | |
| 142 | // In this implementation, we don't need to do anything here |
| 143 | |
| 144 | /* NOTE: we need to initialize the buffer as not mapped/not locked |
| 145 | * because it shouldn't when this function is called the first time |
| 146 | * in a new process. Ideally these flags shouldn't be part of the |
| 147 | * handle, but instead maintained in the kernel or at least |
| 148 | * out-of-line |
| 149 | */ |
| 150 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 151 | private_handle_t* hnd = (private_handle_t*)handle; |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 152 | hnd->base = 0; |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 153 | hnd->base_metadata = 0; |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 154 | int err = gralloc_map(module, handle); |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 155 | if (err) { |
| 156 | ALOGE("%s: gralloc_map failed", __FUNCTION__); |
| 157 | return err; |
| 158 | } |
| 159 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | int gralloc_unregister_buffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 164 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 165 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 166 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 167 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 168 | return -EINVAL; |
| 169 | |
| 170 | /* |
| 171 | * If the buffer has been mapped during a lock operation, it's time |
| 172 | * to un-map it. It's an error to be here with a locked buffer. |
| 173 | * NOTE: the framebuffer is handled differently and is never unmapped. |
| 174 | */ |
| 175 | |
| 176 | private_handle_t* hnd = (private_handle_t*)handle; |
| 177 | |
Kinjal Bhavsar | 139e162 | 2012-09-10 12:35:11 -0700 | [diff] [blame] | 178 | if (hnd->base != 0) { |
| 179 | gralloc_unmap(module, handle); |
| 180 | } |
| 181 | hnd->base = 0; |
Ramkumar Radhakrishnan | 7bf31c3 | 2013-01-28 22:51:51 -0800 | [diff] [blame] | 182 | hnd->base_metadata = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | int terminateBuffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 187 | private_handle_t* hnd) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 188 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 189 | ATRACE_CALL(); |
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 | |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 215 | static int gralloc_map_and_invalidate (gralloc_module_t const* module, |
| 216 | buffer_handle_t handle, int usage) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 217 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 218 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 219 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 220 | return -EINVAL; |
| 221 | |
| 222 | int err = 0; |
| 223 | private_handle_t* hnd = (private_handle_t*)handle; |
| 224 | if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { |
| 225 | if (hnd->base == 0) { |
| 226 | // we need to map for real |
| 227 | pthread_mutex_t* const lock = &sMapLock; |
| 228 | pthread_mutex_lock(lock); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 229 | err = gralloc_map(module, handle); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 230 | pthread_mutex_unlock(lock); |
| 231 | } |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 232 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION and |
| 233 | not useUncached(usage)) { |
| 234 | bool nonCPUWriters = usage & ( |
| 235 | GRALLOC_USAGE_HW_RENDER | |
| 236 | GRALLOC_USAGE_HW_FB | |
| 237 | GRALLOC_USAGE_HW_VIDEO_ENCODER | |
| 238 | GRALLOC_USAGE_HW_CAMERA_WRITE); |
| 239 | |
| 240 | //Invalidate if CPU reads in software and there are non-CPU |
| 241 | //writers. No need to do this for the metadata buffer as it is |
| 242 | //only read/written in software. |
| 243 | //Corner case: If we reach here with a READ_RARELY, then there must |
| 244 | //be a WRITE_OFTEN that caused caching to be used. |
| 245 | if ((usage & GRALLOC_USAGE_SW_READ_MASK) and nonCPUWriters) { |
| 246 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
| 247 | err = memalloc->clean_buffer((void*)hnd->base, |
| 248 | hnd->size, hnd->offset, hnd->fd, |
| 249 | CACHE_INVALIDATE); |
| 250 | } |
| 251 | //Mark the buffer to be flushed after CPU write. |
| 252 | //Corner case: If we reach here with a WRITE_RARELY, then there |
| 253 | //must be a READ_OFTEN that caused caching to be used. |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 254 | if (usage & GRALLOC_USAGE_SW_WRITE_MASK) { |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 255 | hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 256 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 259 | |
| 260 | if(useUncached(usage)) |
| 261 | hnd->flags |= private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH; |
| 262 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 263 | return err; |
| 264 | } |
| 265 | |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 266 | int gralloc_lock(gralloc_module_t const* module, |
| 267 | buffer_handle_t handle, int usage, |
| 268 | int /*l*/, int /*t*/, int /*w*/, int /*h*/, |
| 269 | void** vaddr) |
| 270 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 271 | ATRACE_CALL(); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 272 | private_handle_t* hnd = (private_handle_t*)handle; |
| 273 | int err = gralloc_map_and_invalidate(module, handle, usage); |
| 274 | if(!err) |
| 275 | *vaddr = (void*)hnd->base; |
| 276 | return err; |
| 277 | } |
| 278 | |
| 279 | int gralloc_lock_ycbcr(gralloc_module_t const* module, |
| 280 | buffer_handle_t handle, int usage, |
| 281 | int /*l*/, int /*t*/, int /*w*/, int /*h*/, |
| 282 | struct android_ycbcr *ycbcr) |
| 283 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 284 | ATRACE_CALL(); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 285 | private_handle_t* hnd = (private_handle_t*)handle; |
| 286 | int err = gralloc_map_and_invalidate(module, handle, usage); |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 287 | if(!err) |
| 288 | err = getYUVPlaneInfo(hnd, ycbcr); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 289 | return err; |
| 290 | } |
| 291 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 292 | int gralloc_unlock(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 293 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 294 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 295 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 296 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 297 | return -EINVAL; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 298 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 299 | int err = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 300 | private_handle_t* hnd = (private_handle_t*)handle; |
| 301 | |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 302 | IMemAlloc* memalloc = getAllocator(hnd->flags); |
| 303 | if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) { |
| 304 | err = memalloc->clean_buffer((void*)hnd->base, |
| 305 | hnd->size, hnd->offset, hnd->fd, |
| 306 | CACHE_CLEAN); |
| 307 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 310 | if(hnd->flags & private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH) |
| 311 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_DO_NOT_FLUSH; |
| 312 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 313 | return err; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /*****************************************************************************/ |
| 317 | |
| 318 | int gralloc_perform(struct gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 319 | int operation, ... ) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 320 | { |
| 321 | int res = -EINVAL; |
| 322 | va_list args; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 323 | if(!module) |
| 324 | return res; |
| 325 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 326 | va_start(args, operation); |
| 327 | switch (operation) { |
| 328 | case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: |
| 329 | { |
| 330 | int fd = va_arg(args, int); |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 331 | unsigned int size = va_arg(args, unsigned int); |
| 332 | unsigned int offset = va_arg(args, unsigned int); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 333 | void* base = va_arg(args, void*); |
| 334 | int width = va_arg(args, int); |
| 335 | int height = va_arg(args, int); |
| 336 | int format = va_arg(args, int); |
| 337 | |
| 338 | native_handle_t** handle = va_arg(args, native_handle_t**); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 339 | private_handle_t* hnd = (private_handle_t*)native_handle_create( |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 340 | private_handle_t::sNumFds, private_handle_t::sNumInts()); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 341 | hnd->magic = private_handle_t::sMagic; |
| 342 | hnd->fd = fd; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 343 | hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 344 | hnd->size = size; |
| 345 | hnd->offset = offset; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 346 | hnd->base = uint64_t(base) + offset; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 347 | hnd->gpuaddr = 0; |
| 348 | hnd->width = width; |
| 349 | hnd->height = height; |
| 350 | hnd->format = format; |
| 351 | *handle = (native_handle_t *)hnd; |
| 352 | res = 0; |
| 353 | break; |
| 354 | |
| 355 | } |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 356 | case GRALLOC_MODULE_PERFORM_GET_STRIDE: |
| 357 | { |
| 358 | int width = va_arg(args, int); |
| 359 | int format = va_arg(args, int); |
| 360 | int *stride = va_arg(args, int *); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 361 | int alignedw = 0, alignedh = 0; |
| 362 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 363 | 0, format, false, alignedw, alignedh); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 364 | *stride = alignedw; |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 365 | res = 0; |
| 366 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 367 | |
Naseer Ahmed | a978f95 | 2013-09-26 14:36:28 -0400 | [diff] [blame] | 368 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: |
| 369 | { |
| 370 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 371 | int *stride = va_arg(args, int *); |
| 372 | if (private_handle_t::validate(hnd)) { |
| 373 | return res; |
| 374 | } |
| 375 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 376 | if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { |
| 377 | *stride = metadata->bufferDim.sliceWidth; |
| 378 | } else { |
| 379 | *stride = hnd->width; |
| 380 | } |
| 381 | res = 0; |
| 382 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 383 | |
Raj Kamal | 3d01d8d | 2014-03-04 05:25:33 +0530 | [diff] [blame] | 384 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: |
| 385 | { |
| 386 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 387 | int *stride = va_arg(args, int *); |
| 388 | int *height = va_arg(args, int *); |
| 389 | if (private_handle_t::validate(hnd)) { |
| 390 | return res; |
| 391 | } |
| 392 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 393 | if(metadata && metadata->operation & UPDATE_BUFFER_GEOMETRY) { |
| 394 | *stride = metadata->bufferDim.sliceWidth; |
| 395 | *height = metadata->bufferDim.sliceHeight; |
| 396 | } else { |
| 397 | *stride = hnd->width; |
| 398 | *height = hnd->height; |
| 399 | } |
| 400 | res = 0; |
| 401 | } break; |
| 402 | |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 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; |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 432 | case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: |
| 433 | { |
| 434 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 435 | android_ycbcr* ycbcr = va_arg(args, struct android_ycbcr *); |
Naseer Ahmed | f5ff33a | 2014-04-30 15:30:39 -0400 | [diff] [blame] | 436 | if (!private_handle_t::validate(hnd)) { |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 437 | res = getYUVPlaneInfo(hnd, ycbcr); |
| 438 | } |
| 439 | } break; |
| 440 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 441 | default: |
| 442 | break; |
| 443 | } |
| 444 | va_end(args); |
| 445 | return res; |
| 446 | } |