Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
Naseer Ahmed | debd5ce | 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 | debd5ce | 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 | debd5ce | 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 = -EACCES; |
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 | debd5ce | 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 | debd5ce | 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 | f8d5f82 | 2016-08-02 11:28:13 -0400 | [diff] [blame] | 173 | |
Naseer Ahmed | debd5ce | 2016-05-03 15:25:02 -0400 | [diff] [blame] | 174 | int err = gralloc_map(module, handle); |
| 175 | /* Do not fail register_buffer for secure buffers*/ |
| 176 | if (err == -EACCES) |
| 177 | err = 0; |
| 178 | return err; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | int gralloc_unregister_buffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 182 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 183 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 184 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 185 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 186 | return -EINVAL; |
| 187 | |
| 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 | * NOTE: the framebuffer is handled differently and is never unmapped. |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 192 | * Also base and base_metadata are reset. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 193 | */ |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 194 | return gralloc_unmap(module, handle); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | int terminateBuffer(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 198 | private_handle_t* hnd) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 199 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 200 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 201 | if(!module) |
| 202 | return -EINVAL; |
| 203 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 204 | /* |
| 205 | * If the buffer has been mapped during a lock operation, it's time |
| 206 | * 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] | 207 | * NOTE: the framebuffer is handled differently and is never unmapped. |
| 208 | * Also base and base_metadata are reset. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 209 | */ |
Saurabh Shah | dbe41c5 | 2015-04-23 11:50:50 -0700 | [diff] [blame] | 210 | return gralloc_unmap(module, hnd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 213 | static int gralloc_map_and_invalidate (gralloc_module_t const* module, |
| 214 | buffer_handle_t handle, int usage) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 215 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 216 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 217 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 218 | return -EINVAL; |
| 219 | |
| 220 | int err = 0; |
| 221 | private_handle_t* hnd = (private_handle_t*)handle; |
| 222 | if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { |
| 223 | if (hnd->base == 0) { |
| 224 | // we need to map for real |
| 225 | pthread_mutex_t* const lock = &sMapLock; |
| 226 | pthread_mutex_lock(lock); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 227 | err = gralloc_map(module, handle); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 228 | pthread_mutex_unlock(lock); |
| 229 | } |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 230 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION and |
Saurabh Shah | 1adcafe | 2014-12-19 10:05:41 -0800 | [diff] [blame] | 231 | hnd->flags & private_handle_t::PRIV_FLAGS_CACHED) { |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 232 | //Invalidate if CPU reads in software and there are non-CPU |
| 233 | //writers. No need to do this for the metadata buffer as it is |
| 234 | //only read/written in software. |
Saurabh Shah | 1adcafe | 2014-12-19 10:05:41 -0800 | [diff] [blame] | 235 | if ((usage & GRALLOC_USAGE_SW_READ_MASK) and |
| 236 | (hnd->flags & private_handle_t::PRIV_FLAGS_NON_CPU_WRITER)) |
| 237 | { |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 238 | IMemAlloc* memalloc = getAllocator(hnd->flags) ; |
| 239 | err = memalloc->clean_buffer((void*)hnd->base, |
| 240 | hnd->size, hnd->offset, hnd->fd, |
| 241 | CACHE_INVALIDATE); |
| 242 | } |
| 243 | //Mark the buffer to be flushed after CPU write. |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 244 | if (usage & GRALLOC_USAGE_SW_WRITE_MASK) { |
Naseer Ahmed | ec14798 | 2013-06-14 17:06:46 -0400 | [diff] [blame] | 245 | hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
| 246 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 247 | } |
| 248 | } |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 249 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 250 | return err; |
| 251 | } |
| 252 | |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 253 | int gralloc_lock(gralloc_module_t const* module, |
| 254 | buffer_handle_t handle, int usage, |
| 255 | int /*l*/, int /*t*/, int /*w*/, int /*h*/, |
| 256 | void** vaddr) |
| 257 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 258 | ATRACE_CALL(); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 259 | private_handle_t* hnd = (private_handle_t*)handle; |
| 260 | int err = gralloc_map_and_invalidate(module, handle, usage); |
| 261 | if(!err) |
| 262 | *vaddr = (void*)hnd->base; |
| 263 | return err; |
| 264 | } |
| 265 | |
| 266 | int gralloc_lock_ycbcr(gralloc_module_t const* module, |
| 267 | buffer_handle_t handle, int usage, |
| 268 | int /*l*/, int /*t*/, int /*w*/, int /*h*/, |
| 269 | struct android_ycbcr *ycbcr) |
| 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); |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 274 | if(!err) |
| 275 | err = getYUVPlaneInfo(hnd, ycbcr); |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 276 | return err; |
| 277 | } |
| 278 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 279 | int gralloc_unlock(gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 280 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 281 | { |
Saurabh Shah | d4749de | 2014-09-10 18:04:31 -0700 | [diff] [blame] | 282 | ATRACE_CALL(); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 283 | if (!module || private_handle_t::validate(handle) < 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 284 | return -EINVAL; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 285 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 286 | int err = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 287 | private_handle_t* hnd = (private_handle_t*)handle; |
| 288 | |
Saurabh Shah | 9ff53a9 | 2014-09-17 16:40:44 -0700 | [diff] [blame] | 289 | IMemAlloc* memalloc = getAllocator(hnd->flags); |
| 290 | if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) { |
| 291 | err = memalloc->clean_buffer((void*)hnd->base, |
| 292 | hnd->size, hnd->offset, hnd->fd, |
| 293 | CACHE_CLEAN); |
| 294 | hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 295 | } |
| 296 | |
Naseer Ahmed | aeab91f | 2013-03-20 21:01:19 -0400 | [diff] [blame] | 297 | return err; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /*****************************************************************************/ |
| 301 | |
| 302 | int gralloc_perform(struct gralloc_module_t const* module, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 303 | int operation, ... ) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 304 | { |
| 305 | int res = -EINVAL; |
| 306 | va_list args; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 307 | if(!module) |
| 308 | return res; |
| 309 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 310 | va_start(args, operation); |
| 311 | switch (operation) { |
| 312 | case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: |
| 313 | { |
| 314 | int fd = va_arg(args, int); |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 315 | unsigned int size = va_arg(args, unsigned int); |
| 316 | unsigned int offset = va_arg(args, unsigned int); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 317 | void* base = va_arg(args, void*); |
| 318 | int width = va_arg(args, int); |
| 319 | int height = va_arg(args, int); |
| 320 | int format = va_arg(args, int); |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 321 | int alignedw = 0, alignedh = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 322 | |
| 323 | native_handle_t** handle = va_arg(args, native_handle_t**); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 324 | private_handle_t* hnd = (private_handle_t*)native_handle_create( |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 325 | private_handle_t::sNumFds, private_handle_t::sNumInts()); |
Ramakant Singh | 04b6b24 | 2015-03-23 15:01:12 +0530 | [diff] [blame] | 326 | if (hnd) { |
| 327 | hnd->magic = private_handle_t::sMagic; |
| 328 | hnd->fd = fd; |
| 329 | hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION; |
| 330 | hnd->size = size; |
| 331 | hnd->offset = offset; |
| 332 | hnd->base = uint64_t(base) + offset; |
| 333 | hnd->gpuaddr = 0; |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 334 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
| 335 | height, format, 0, alignedw, alignedh); |
| 336 | hnd->width = alignedw; |
| 337 | hnd->height = alignedh; |
| 338 | hnd->unaligned_width = width; |
| 339 | hnd->unaligned_height = height; |
Ramakant Singh | 04b6b24 | 2015-03-23 15:01:12 +0530 | [diff] [blame] | 340 | hnd->format = format; |
| 341 | *handle = (native_handle_t *)hnd; |
| 342 | res = 0; |
| 343 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 344 | break; |
| 345 | |
| 346 | } |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 347 | case GRALLOC_MODULE_PERFORM_GET_STRIDE: |
| 348 | { |
| 349 | int width = va_arg(args, int); |
| 350 | int format = va_arg(args, int); |
| 351 | int *stride = va_arg(args, int *); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 352 | int alignedw = 0, alignedh = 0; |
| 353 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
Sushil Chauhan | 65e2630 | 2015-01-14 10:48:57 -0800 | [diff] [blame] | 354 | 0, format, 0, alignedw, alignedh); |
Ramkumar Radhakrishnan | 473f408 | 2013-11-04 14:29:18 -0800 | [diff] [blame] | 355 | *stride = alignedw; |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 356 | res = 0; |
| 357 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 358 | |
Naseer Ahmed | a978f95 | 2013-09-26 14:36:28 -0400 | [diff] [blame] | 359 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: |
| 360 | { |
Manoj Kumar AVM | 8e1aa18 | 2015-08-05 19:45:16 -0700 | [diff] [blame] | 361 | const private_handle_t* hnd = va_arg(args, private_handle_t*); |
Naseer Ahmed | a978f95 | 2013-09-26 14:36:28 -0400 | [diff] [blame] | 362 | int *stride = va_arg(args, int *); |
| 363 | if (private_handle_t::validate(hnd)) { |
| 364 | return res; |
| 365 | } |
Manoj Kumar AVM | 8e1aa18 | 2015-08-05 19:45:16 -0700 | [diff] [blame] | 366 | |
| 367 | int alignedw = 0, alignedh = 0; |
| 368 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(hnd, alignedw, alignedh); |
| 369 | *stride = alignedw; |
| 370 | |
Naseer Ahmed | a978f95 | 2013-09-26 14:36:28 -0400 | [diff] [blame] | 371 | res = 0; |
| 372 | } break; |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 373 | |
Raj Kamal | 3d01d8d | 2014-03-04 05:25:33 +0530 | [diff] [blame] | 374 | case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: |
| 375 | { |
Manoj Kumar AVM | 8e1aa18 | 2015-08-05 19:45:16 -0700 | [diff] [blame] | 376 | const private_handle_t* hnd = va_arg(args, private_handle_t*); |
Raj Kamal | 3d01d8d | 2014-03-04 05:25:33 +0530 | [diff] [blame] | 377 | int *stride = va_arg(args, int *); |
| 378 | int *height = va_arg(args, int *); |
| 379 | if (private_handle_t::validate(hnd)) { |
| 380 | return res; |
| 381 | } |
Manoj Kumar AVM | 8e1aa18 | 2015-08-05 19:45:16 -0700 | [diff] [blame] | 382 | |
| 383 | int alignedw = 0, alignedh = 0; |
| 384 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(hnd, alignedw, alignedh); |
| 385 | *stride = alignedw; |
| 386 | *height = alignedh; |
| 387 | |
Raj Kamal | 3d01d8d | 2014-03-04 05:25:33 +0530 | [diff] [blame] | 388 | res = 0; |
| 389 | } break; |
| 390 | |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 391 | case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: |
| 392 | { |
| 393 | int width = va_arg(args, int); |
| 394 | int height = va_arg(args, int); |
| 395 | int format = va_arg(args, int); |
| 396 | int usage = va_arg(args, int); |
| 397 | int *alignedWidth = va_arg(args, int *); |
| 398 | int *alignedHeight = va_arg(args, int *); |
| 399 | int *tileEnabled = va_arg(args,int *); |
Sushil Chauhan | e61fac5 | 2015-04-30 17:14:37 -0700 | [diff] [blame] | 400 | *tileEnabled = isUBwcEnabled(format, usage) || |
| 401 | isMacroTileEnabled(format, usage); |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 402 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
Sushil Chauhan | 65e2630 | 2015-01-14 10:48:57 -0800 | [diff] [blame] | 403 | height, format, usage, *alignedWidth, *alignedHeight); |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 404 | res = 0; |
| 405 | } break; |
| 406 | |
Shuzhen Wang | c502c87 | 2014-01-28 16:10:42 -0800 | [diff] [blame] | 407 | case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: |
| 408 | { |
| 409 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 410 | int *color_space = va_arg(args, int *); |
| 411 | if (private_handle_t::validate(hnd)) { |
| 412 | return res; |
| 413 | } |
| 414 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
Arun Kumar K.R | b2771bf | 2016-10-03 21:38:23 -0700 | [diff] [blame^] | 415 | if (!metadata) { |
| 416 | break; |
| 417 | #ifdef USE_COLOR_METADATA |
| 418 | } else if (metadata->operation & COLOR_METADATA) { |
| 419 | ColorMetaData *colorMetadata = &metadata->color; |
| 420 | res = 0; |
| 421 | switch (colorMetadata->colorPrimaries) { |
| 422 | case ColorPrimaries_BT709_5: |
| 423 | *color_space = HAL_CSC_ITU_R_709; |
| 424 | break; |
| 425 | case ColorPrimaries_BT601_6_525: |
| 426 | *color_space = ((colorMetadata->range) ? |
| 427 | HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601); |
| 428 | break; |
| 429 | case ColorPrimaries_BT2020: |
| 430 | *color_space = (colorMetadata->range) ? |
| 431 | HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020; |
| 432 | break; |
| 433 | default: |
| 434 | res = -EINVAL; |
| 435 | break; |
| 436 | } |
| 437 | #endif |
| 438 | } else if(metadata->operation & UPDATE_COLOR_SPACE) { |
Shuzhen Wang | c502c87 | 2014-01-28 16:10:42 -0800 | [diff] [blame] | 439 | *color_space = metadata->colorSpace; |
| 440 | res = 0; |
| 441 | } |
| 442 | } break; |
Arun Kumar K.R | 563fbde | 2014-11-24 08:51:17 -0800 | [diff] [blame] | 443 | |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 444 | case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: |
| 445 | { |
| 446 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 447 | android_ycbcr* ycbcr = va_arg(args, struct android_ycbcr *); |
Naseer Ahmed | f5ff33a | 2014-04-30 15:30:39 -0400 | [diff] [blame] | 448 | if (!private_handle_t::validate(hnd)) { |
Naseer Ahmed | b29fdfd | 2014-04-08 20:23:47 -0400 | [diff] [blame] | 449 | res = getYUVPlaneInfo(hnd, ycbcr); |
| 450 | } |
| 451 | } break; |
| 452 | |
Arun Kumar K.R | 563fbde | 2014-11-24 08:51:17 -0800 | [diff] [blame] | 453 | case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: |
| 454 | { |
| 455 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 456 | int *map_secure_buffer = va_arg(args, int *); |
| 457 | if (private_handle_t::validate(hnd)) { |
| 458 | return res; |
| 459 | } |
| 460 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 461 | if(metadata && metadata->operation & MAP_SECURE_BUFFER) { |
| 462 | *map_secure_buffer = metadata->mapSecureBuffer; |
| 463 | res = 0; |
| 464 | } else { |
| 465 | *map_secure_buffer = 0; |
| 466 | } |
| 467 | } break; |
| 468 | |
Sushil Chauhan | a9d4700 | 2015-02-18 14:55:03 -0800 | [diff] [blame] | 469 | case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: |
| 470 | { |
| 471 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 472 | int *flag = va_arg(args, int *); |
| 473 | if (private_handle_t::validate(hnd)) { |
| 474 | return res; |
| 475 | } |
| 476 | *flag = hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; |
Sushil Chauhan | e7acc3c | 2015-06-23 16:22:30 -0700 | [diff] [blame] | 477 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 478 | if (metadata && (metadata->operation & LINEAR_FORMAT)) { |
| 479 | *flag = 0; |
| 480 | } |
Sushil Chauhan | a9d4700 | 2015-02-18 14:55:03 -0800 | [diff] [blame] | 481 | res = 0; |
| 482 | } break; |
| 483 | |
Sushil Chauhan | 7dd3a43 | 2015-04-08 15:54:42 -0700 | [diff] [blame] | 484 | case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: |
| 485 | { |
| 486 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
Sushil Chauhan | c85b65b | 2015-04-30 11:05:36 -0700 | [diff] [blame] | 487 | void** rgb_data = va_arg(args, void**); |
Sushil Chauhan | 7dd3a43 | 2015-04-08 15:54:42 -0700 | [diff] [blame] | 488 | if (!private_handle_t::validate(hnd)) { |
| 489 | res = getRgbDataAddress(hnd, rgb_data); |
| 490 | } |
| 491 | } break; |
| 492 | |
Dileep Marchya | 1a7e1f1 | 2015-09-25 19:11:57 -0700 | [diff] [blame] | 493 | case GRALLOC_MODULE_PERFORM_GET_IGC: |
| 494 | { |
| 495 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
| 496 | uint32_t *igc = va_arg(args, uint32_t *); |
| 497 | if (!private_handle_t::validate(hnd) && igc) { |
| 498 | MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; |
| 499 | if (metadata && (metadata->operation & SET_IGC)) { |
| 500 | *igc = metadata->igc; |
| 501 | res = 0; |
| 502 | } |
| 503 | } |
| 504 | } break; |
| 505 | |
| 506 | case GRALLOC_MODULE_PERFORM_SET_IGC: |
Dileep Marchya | a212934 | 2016-01-05 18:15:57 -0800 | [diff] [blame] | 507 | res = 0; |
| 508 | break; |
Dileep Marchya | 1a7e1f1 | 2015-09-25 19:11:57 -0700 | [diff] [blame] | 509 | |
Saurabh Shah | 95f8368 | 2015-10-16 10:30:04 -0700 | [diff] [blame] | 510 | case GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE: |
| 511 | { |
| 512 | private_handle_t* hnd = va_arg(args, private_handle_t*); |
Saurabh Shah | b8067a4 | 2015-11-06 16:52:02 -0800 | [diff] [blame] | 513 | uint32_t *enable = va_arg(args, uint32_t*); |
Sushil Chauhan | 7646621 | 2016-01-05 18:09:22 -0800 | [diff] [blame] | 514 | if (!private_handle_t::validate(hnd)) { |
| 515 | setMetaData(hnd, SET_SINGLE_BUFFER_MODE, enable); |
| 516 | res = 0; |
Saurabh Shah | 95f8368 | 2015-10-16 10:30:04 -0700 | [diff] [blame] | 517 | } |
Saurabh Shah | 95f8368 | 2015-10-16 10:30:04 -0700 | [diff] [blame] | 518 | } break; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 519 | default: |
| 520 | break; |
| 521 | } |
| 522 | va_end(args); |
| 523 | return res; |
| 524 | } |