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