Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
Naseer Ahmed | 7a86b84 | 2016-05-03 15:25:02 -0400 | [diff] [blame] | 3 | * Copyright (c) 2011-2016, 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 | |
Naseer Ahmed | 7a86b84 | 2016-05-03 15:25:02 -0400 | [diff] [blame] | 58 | static int gralloc_map_metadata(buffer_handle_t handle) { |
| 59 | private_handle_t* hnd = (private_handle_t*)handle; |
| 60 | hnd->base_metadata = 0; |
| 61 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
| 62 | void *mappedAddress = MAP_FAILED; |
| 63 | unsigned int size = 0; |
| 64 | if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { |
| 65 | mappedAddress = MAP_FAILED; |
| 66 | size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 67 | int ret = memalloc->map_buffer(&mappedAddress, size, |
| 68 | hnd->offset_metadata, hnd->fd_metadata); |
| 69 | if(ret || mappedAddress == MAP_FAILED) { |
| 70 | ALOGE("Could not mmap metadata for handle %p, fd=%d (%s)", |
| 71 | hnd, hnd->fd_metadata, strerror(errno)); |
| 72 | return -errno; |
| 73 | } |
| 74 | hnd->base_metadata = uint64_t(mappedAddress) + hnd->offset_metadata; |
| 75 | } |
| 76 | return 0; |
| 77 | } |
| 78 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 79 | static int gralloc_map(gralloc_module_t const* module, |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 80 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 81 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 82 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 83 | if(!module) |
| 84 | return -EINVAL; |
| 85 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 86 | private_handle_t* hnd = (private_handle_t*)handle; |
Arun Kumar K.R | da2f69b | 2014-09-30 15:45:37 -0700 | [diff] [blame] | 87 | unsigned int size = 0; |
| 88 | int err = 0; |
| 89 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 90 | void *mappedAddress = MAP_FAILED; |
| 91 | hnd->base = 0; |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 92 | |
| 93 | // Dont map framebuffer and secure buffers |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 94 | if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) && |
| 95 | !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) { |
Arun Kumar K.R | da2f69b | 2014-09-30 15:45:37 -0700 | [diff] [blame] | 96 | size = hnd->size; |
| 97 | err = memalloc->map_buffer(&mappedAddress, size, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 98 | hnd->offset, hnd->fd); |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 99 | if(err || mappedAddress == MAP_FAILED) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 100 | ALOGE("Could not mmap handle %p, fd=%d (%s)", |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 101 | handle, hnd->fd, strerror(errno)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 102 | return -errno; |
| 103 | } |
| 104 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 105 | hnd->base = uint64_t(mappedAddress) + hnd->offset; |
Naseer Ahmed | 7a86b84 | 2016-05-03 15:25:02 -0400 | [diff] [blame] | 106 | } else { |
| 107 | // Cannot map secure buffers or framebuffers, but still need to map |
| 108 | // metadata for secure buffers. |
| 109 | // If mapping a secure buffers fails, the framework needs to get |
| 110 | // an error code. |
| 111 | err = -EINVAL; |
Arun Kumar K.R | da2f69b | 2014-09-30 15:45:37 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 114 | //Allow mapping of metadata for all buffers including secure ones, but not |
| 115 | //of framebuffer |
Naseer Ahmed | 7a86b84 | 2016-05-03 15:25:02 -0400 | [diff] [blame] | 116 | int metadata_err = gralloc_map_metadata(handle); |
| 117 | if (!err) { |
| 118 | err = metadata_err; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 119 | } |
Naseer Ahmed | 7a86b84 | 2016-05-03 15:25:02 -0400 | [diff] [blame] | 120 | return err; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static int gralloc_unmap(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 124 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 125 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 126 | ATRACE_CALL(); |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 127 | int err = -EINVAL; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 128 | if(!module) |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 129 | return err; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 130 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 131 | private_handle_t* hnd = (private_handle_t*)handle; |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 132 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
| 133 | if(!memalloc) |
| 134 | return err; |
| 135 | |
| 136 | if(hnd->base) { |
| 137 | err = memalloc->unmap_buffer((void*)hnd->base, hnd->size, hnd->offset); |
| 138 | if (err) { |
Naseer Ahmed | b8ecfbf | 2015-11-02 20:34:29 -0500 | [diff] [blame] | 139 | ALOGE("Could not unmap memory at address %p, %s", (void*) hnd->base, |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 140 | strerror(errno)); |
| 141 | return -errno; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 142 | } |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 143 | hnd->base = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 144 | } |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 145 | |
| 146 | if(hnd->base_metadata) { |
| 147 | unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 148 | err = memalloc->unmap_buffer((void*)hnd->base_metadata, |
| 149 | size, hnd->offset_metadata); |
| 150 | if (err) { |
| 151 | ALOGE("Could not unmap memory at address %p, %s", |
Naseer Ahmed | b8ecfbf | 2015-11-02 20:34:29 -0500 | [diff] [blame] | 152 | (void*) hnd->base_metadata, strerror(errno)); |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 153 | return -errno; |
| 154 | } |
| 155 | hnd->base_metadata = 0; |
| 156 | } |
| 157 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /*****************************************************************************/ |
| 162 | |
| 163 | static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER; |
| 164 | |
| 165 | /*****************************************************************************/ |
| 166 | |
| 167 | int gralloc_register_buffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 168 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 169 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 170 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 171 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 172 | return -EINVAL; |
Naseer Ahmed | 7a86b84 | 2016-05-03 15:25:02 -0400 | [diff] [blame] | 173 | // The base address received via IPC is invalid in this process |
| 174 | // Reset it to 0 here since it will be mapped in lock() |
| 175 | private_handle_t* hnd = (private_handle_t*)handle; |
| 176 | hnd->base = 0; |
| 177 | return gralloc_map_metadata(handle); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | int gralloc_unregister_buffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 181 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 182 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 183 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 184 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 185 | return -EINVAL; |
| 186 | |
| 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 | * NOTE: the framebuffer is handled differently and is never unmapped. |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 191 | * Also base and base_metadata are reset. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 192 | */ |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 193 | return gralloc_unmap(module, handle); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | int terminateBuffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 197 | private_handle_t* hnd) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 198 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 199 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 200 | if(!module) |
| 201 | return -EINVAL; |
| 202 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 203 | /* |
| 204 | * If the buffer has been mapped during a lock operation, it's time |
| 205 | * to un-map it. It's an error to be here with a locked buffer. |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 206 | * NOTE: the framebuffer is handled differently and is never unmapped. |
| 207 | * Also base and base_metadata are reset. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 208 | */ |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 209 | return gralloc_unmap(module, hnd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 212 | static int gralloc_map_and_invalidate (gralloc_module_t const* module, |
| 213 | buffer_handle_t handle, int usage) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 214 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 215 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 216 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 217 | return -EINVAL; |
| 218 | |
| 219 | int err = 0; |
| 220 | private_handle_t* hnd = (private_handle_t*)handle; |
| 221 | if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { |
| 222 | if (hnd->base == 0) { |
| 223 | // we need to map for real |
| 224 | pthread_mutex_t* const lock = &sMapLock; |
| 225 | pthread_mutex_lock(lock); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 226 | err = gralloc_map(module, handle); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 227 | pthread_mutex_unlock(lock); |
| 228 | } |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 229 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION and |
Saurabh Shah | 1adcafe | 2014-12-19 10:05:41 -0800 | [diff] [blame] | 230 | hnd->flags & private_handle_t::PRIV_FLAGS_CACHED) { |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 231 | //Invalidate if CPU reads in software and there are non-CPU |
| 232 | //writers. No need to do this for the metadata buffer as it is |
| 233 | //only read/written in software. |
Saurabh Shah | 1adcafe | 2014-12-19 10:05:41 -0800 | [diff] [blame] | 234 | if ((usage & GRALLOC_USAGE_SW_READ_MASK) and |
| 235 | (hnd->flags & private_handle_t::PRIV_FLAGS_NON_CPU_WRITER)) |
| 236 | { |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 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 | } |
| 242 | //Mark the buffer to be flushed after CPU write. |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 243 | if (usage & GRALLOC_USAGE_SW_WRITE_MASK) { |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 244 | hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 245 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 248 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 249 | return err; |
| 250 | } |
| 251 | |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 252 | int gralloc_lock(gralloc_module_t const* module, |
| 253 | buffer_handle_t handle, int usage, |
| 254 | int /*l*/, int /*t*/, int /*w*/, int /*h*/, |
| 255 | void** vaddr) |
| 256 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 257 | ATRACE_CALL(); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 258 | private_handle_t* hnd = (private_handle_t*)handle; |
| 259 | int err = gralloc_map_and_invalidate(module, handle, usage); |
| 260 | if(!err) |
| 261 | *vaddr = (void*)hnd->base; |
| 262 | return err; |
| 263 | } |
| 264 | |
| 265 | int gralloc_lock_ycbcr(gralloc_module_t const* module, |
| 266 | buffer_handle_t handle, int usage, |
| 267 | int /*l*/, int /*t*/, int /*w*/, int /*h*/, |
| 268 | struct android_ycbcr *ycbcr) |
| 269 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 270 | ATRACE_CALL(); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 271 | private_handle_t* hnd = (private_handle_t*)handle; |
| 272 | int err = gralloc_map_and_invalidate(module, handle, usage); |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 273 | if(!err) |
| 274 | err = getYUVPlaneInfo(hnd, ycbcr); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 275 | return err; |
| 276 | } |
| 277 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 278 | int gralloc_unlock(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 279 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 280 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 281 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 282 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 283 | return -EINVAL; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 284 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 285 | int err = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 286 | private_handle_t* hnd = (private_handle_t*)handle; |
| 287 | |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 288 | IMemAlloc* memalloc = getAllocator(hnd->flags); |
| 289 | if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) { |
| 290 | err = memalloc->clean_buffer((void*)hnd->base, |
| 291 | hnd->size, hnd->offset, hnd->fd, |
| 292 | CACHE_CLEAN); |
| 293 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 296 | return err; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /*****************************************************************************/ |
| 300 | |
| 301 | int gralloc_perform(struct gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 302 | int operation, ... ) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 303 | { |
| 304 | int res = -EINVAL; |
| 305 | va_list args; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 306 | if(!module) |
| 307 | return res; |
| 308 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 309 | va_start(args, operation); |
| 310 | switch (operation) { |
| 311 | case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: |
| 312 | { |
| 313 | int fd = va_arg(args, int); |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 314 | unsigned int size = va_arg(args, unsigned int); |
| 315 | unsigned int offset = va_arg(args, unsigned int); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 316 | void* base = va_arg(args, void*); |
| 317 | int width = va_arg(args, int); |
| 318 | int height = va_arg(args, int); |
| 319 | int format = va_arg(args, int); |
| 320 | |
| 321 | native_handle_t** handle = va_arg(args, native_handle_t**); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 322 | private_handle_t* hnd = (private_handle_t*)native_handle_create( |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 323 | private_handle_t::sNumFds, private_handle_t::sNumInts()); |
Ramakant Singh | 04b6b24 | 2015-03-23 15:01:12 +0530 | [diff] [blame] | 324 | if (hnd) { |
| 325 | hnd->magic = private_handle_t::sMagic; |
| 326 | hnd->fd = fd; |
| 327 | hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION; |
| 328 | hnd->size = size; |
| 329 | hnd->offset = offset; |
| 330 | hnd->base = uint64_t(base) + offset; |
| 331 | hnd->gpuaddr = 0; |
| 332 | hnd->width = width; |
| 333 | hnd->height = height; |
| 334 | hnd->format = format; |
| 335 | *handle = (native_handle_t *)hnd; |
| 336 | res = 0; |
| 337 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 338 | break; |
| 339 | |
| 340 | } |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 341 | case GRALLOC_MODULE_PERFORM_GET_STRIDE: |
| 342 | { |
| 343 | int width = va_arg(args, int); |
| 344 | int format = va_arg(args, int); |
| 345 | int *stride = va_arg(args, int *); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 346 | int alignedw = 0, alignedh = 0; |
| 347 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
Sushil Chauhan | 65e2630 | 2015-01-14 10:48:57 -0800 | [diff] [blame] | 348 | 0, format, 0, alignedw, alignedh); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 349 | *stride = alignedw; |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 350 | res = 0; |
| 351 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 352 | |
Naseer Ahmed | a978f95 | 2013-09-26 14:36:28 -0400 | [diff] [blame] | 353 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: |
| 354 | { |
Manoj Kumar AVM | 8e1aa18 | 2015-08-05 19:45:16 -0700 | [diff] [blame] | 355 | const private_handle_t* hnd = va_arg(args, private_handle_t*); |
Naseer Ahmed | a978f95 | 2013-09-26 14:36:28 -0400 | [diff] [blame] | 356 | int *stride = va_arg(args, int *); |
| 357 | if (private_handle_t::validate(hnd)) { |
| 358 | return res; |
| 359 | } |
Manoj Kumar AVM | 8e1aa18 | 2015-08-05 19:45:16 -0700 | [diff] [blame] | 360 | |
| 361 | int alignedw = 0, alignedh = 0; |
| 362 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(hnd, alignedw, alignedh); |
| 363 | *stride = alignedw; |
| 364 | |
Naseer Ahmed | a978f95 | 2013-09-26 14:36:28 -0400 | [diff] [blame] | 365 | res = 0; |
| 366 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 367 | |
Raj Kamal | 3d01d8d | 2014-03-04 05:25:33 +0530 | [diff] [blame] | 368 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: |
| 369 | { |
Manoj Kumar AVM | 8e1aa18 | 2015-08-05 19:45:16 -0700 | [diff] [blame] | 370 | const private_handle_t* hnd = va_arg(args, private_handle_t*); |
Raj Kamal | 3d01d8d | 2014-03-04 05:25:33 +0530 | [diff] [blame] | 371 | int *stride = va_arg(args, int *); |
| 372 | int *height = va_arg(args, int *); |
| 373 | if (private_handle_t::validate(hnd)) { |
| 374 | return res; |
| 375 | } |
Manoj Kumar AVM | 8e1aa18 | 2015-08-05 19:45:16 -0700 | [diff] [blame] | 376 | |
| 377 | int alignedw = 0, alignedh = 0; |
| 378 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(hnd, alignedw, alignedh); |
| 379 | *stride = alignedw; |
| 380 | *height = alignedh; |
| 381 | |
Raj Kamal | 3d01d8d | 2014-03-04 05:25:33 +0530 | [diff] [blame] | 382 | res = 0; |
| 383 | } break; |
| 384 | |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 385 | case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: |
| 386 | { |
| 387 | int width = va_arg(args, int); |
| 388 | int height = va_arg(args, int); |
| 389 | int format = va_arg(args, int); |
| 390 | int usage = va_arg(args, int); |
| 391 | int *alignedWidth = va_arg(args, int *); |
| 392 | int *alignedHeight = va_arg(args, int *); |
| 393 | int *tileEnabled = va_arg(args,int *); |
Sushil Chauhan | e61fac5 | 2015-04-30 17:14:37 -0700 | [diff] [blame] | 394 | *tileEnabled = isUBwcEnabled(format, usage) || |
| 395 | isMacroTileEnabled(format, usage); |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 396 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
Sushil Chauhan | 65e2630 | 2015-01-14 10:48:57 -0800 | [diff] [blame] | 397 | height, format, usage, *alignedWidth, *alignedHeight); |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 398 | res = 0; |
| 399 | } break; |
| 400 | |
Shuzhen Wang | c502c87 | 2014-01-28 16:10:42 -0800 | [diff] [blame] | 401 | case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: |
| 402 | { |
| 403 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 404 | int *color_space = va_arg(args, int *); |
| 405 | if (private_handle_t::validate(hnd)) { |
| 406 | return res; |
| 407 | } |
| 408 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 409 | if(metadata && metadata->operation & UPDATE_COLOR_SPACE) { |
| 410 | *color_space = metadata->colorSpace; |
| 411 | res = 0; |
| 412 | } |
| 413 | } break; |
Arun Kumar K.R | 563fbde | 2014-11-24 08:51:17 -0800 | [diff] [blame] | 414 | |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 415 | case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: |
| 416 | { |
| 417 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 418 | android_ycbcr* ycbcr = va_arg(args, struct android_ycbcr *); |
Naseer Ahmed | f5ff33a | 2014-04-30 15:30:39 -0400 | [diff] [blame] | 419 | if (!private_handle_t::validate(hnd)) { |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 420 | res = getYUVPlaneInfo(hnd, ycbcr); |
| 421 | } |
| 422 | } break; |
| 423 | |
Arun Kumar K.R | 563fbde | 2014-11-24 08:51:17 -0800 | [diff] [blame] | 424 | case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: |
| 425 | { |
| 426 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 427 | int *map_secure_buffer = va_arg(args, int *); |
| 428 | if (private_handle_t::validate(hnd)) { |
| 429 | return res; |
| 430 | } |
| 431 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 432 | if(metadata && metadata->operation & MAP_SECURE_BUFFER) { |
| 433 | *map_secure_buffer = metadata->mapSecureBuffer; |
| 434 | res = 0; |
| 435 | } else { |
| 436 | *map_secure_buffer = 0; |
| 437 | } |
| 438 | } break; |
| 439 | |
Sushil Chauhan | a9d4700 | 2015-02-18 14:55:03 -0800 | [diff] [blame] | 440 | case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: |
| 441 | { |
| 442 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 443 | int *flag = va_arg(args, int *); |
| 444 | if (private_handle_t::validate(hnd)) { |
| 445 | return res; |
| 446 | } |
| 447 | *flag = hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; |
Sushil Chauhan | e7acc3c | 2015-06-23 16:22:30 -0700 | [diff] [blame] | 448 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 449 | if (metadata && (metadata->operation & LINEAR_FORMAT)) { |
| 450 | *flag = 0; |
| 451 | } |
Sushil Chauhan | a9d4700 | 2015-02-18 14:55:03 -0800 | [diff] [blame] | 452 | res = 0; |
| 453 | } break; |
| 454 | |
Sushil Chauhan | 7dd3a43 | 2015-04-08 15:54:42 -0700 | [diff] [blame] | 455 | case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: |
| 456 | { |
| 457 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
Sushil Chauhan | c85b65b | 2015-04-30 11:05:36 -0700 | [diff] [blame] | 458 | void** rgb_data = va_arg(args, void**); |
Sushil Chauhan | 7dd3a43 | 2015-04-08 15:54:42 -0700 | [diff] [blame] | 459 | if (!private_handle_t::validate(hnd)) { |
| 460 | res = getRgbDataAddress(hnd, rgb_data); |
| 461 | } |
| 462 | } break; |
| 463 | |
Dileep Marchya | 1a7e1f1 | 2015-09-25 19:11:57 -0700 | [diff] [blame] | 464 | case GRALLOC_MODULE_PERFORM_GET_IGC: |
| 465 | { |
| 466 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 467 | uint32_t *igc = va_arg(args, uint32_t *); |
| 468 | if (!private_handle_t::validate(hnd) && igc) { |
| 469 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 470 | if (metadata && (metadata->operation & SET_IGC)) { |
| 471 | *igc = metadata->igc; |
| 472 | res = 0; |
| 473 | } |
| 474 | } |
| 475 | } break; |
| 476 | |
| 477 | case GRALLOC_MODULE_PERFORM_SET_IGC: |
Dileep Marchya | a212934 | 2016-01-05 18:15:57 -0800 | [diff] [blame] | 478 | res = 0; |
| 479 | break; |
Dileep Marchya | 1a7e1f1 | 2015-09-25 19:11:57 -0700 | [diff] [blame] | 480 | |
Saurabh Shah | 95f8368 | 2015-10-16 10:30:04 -0700 | [diff] [blame] | 481 | case GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE: |
| 482 | { |
| 483 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
Saurabh Shah | b8067a4 | 2015-11-06 16:52:02 -0800 | [diff] [blame] | 484 | uint32_t *enable = va_arg(args, uint32_t*); |
Sushil Chauhan | 7646621 | 2016-01-05 18:09:22 -0800 | [diff] [blame] | 485 | if (!private_handle_t::validate(hnd)) { |
| 486 | setMetaData(hnd, SET_SINGLE_BUFFER_MODE, enable); |
| 487 | res = 0; |
Saurabh Shah | 95f8368 | 2015-10-16 10:30:04 -0700 | [diff] [blame] | 488 | } |
Saurabh Shah | 95f8368 | 2015-10-16 10:30:04 -0700 | [diff] [blame] | 489 | } break; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 490 | default: |
| 491 | break; |
| 492 | } |
| 493 | va_end(args); |
| 494 | return res; |
| 495 | } |