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