Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 ARM Limited. All rights reserved. |
| 3 | * |
| 4 | * Copyright (C) 2008 The Android Open Source Project |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * You may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <pthread.h> |
John Stultz | 21ad5be | 2018-03-20 20:35:07 -0700 | [diff] [blame] | 21 | #include <string.h> |
| 22 | //#include <sync/sync.h> |
| 23 | #include <android/sync.h> |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 24 | |
| 25 | #include <cutils/log.h> |
| 26 | #include <cutils/atomic.h> |
| 27 | #include <hardware/hardware.h> |
| 28 | #include <hardware/gralloc.h> |
| 29 | |
| 30 | #include "gralloc_priv.h" |
| 31 | #include "alloc_device.h" |
| 32 | #include "framebuffer_device.h" |
| 33 | |
| 34 | #if GRALLOC_ARM_UMP_MODULE |
| 35 | #include <ump/ump_ref_drv.h> |
| 36 | static int s_ump_is_open = 0; |
| 37 | #endif |
| 38 | |
| 39 | #if GRALLOC_ARM_DMA_BUF_MODULE |
| 40 | #include <linux/ion.h> |
| 41 | #include <ion/ion.h> |
| 42 | #include <sys/mman.h> |
| 43 | #endif |
| 44 | |
| 45 | static pthread_mutex_t s_map_lock = PTHREAD_MUTEX_INITIALIZER; |
| 46 | |
| 47 | static int gralloc_device_open(const hw_module_t *module, const char *name, hw_device_t **device) |
| 48 | { |
| 49 | int status = -EINVAL; |
| 50 | |
| 51 | if (!strncmp(name, GRALLOC_HARDWARE_GPU0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN)) |
| 52 | { |
| 53 | status = alloc_device_open(module, name, device); |
| 54 | } |
| 55 | else if (!strncmp(name, GRALLOC_HARDWARE_FB0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN)) |
| 56 | { |
| 57 | status = framebuffer_device_open(module, name, device); |
| 58 | } |
| 59 | |
| 60 | return status; |
| 61 | } |
| 62 | |
| 63 | static int gralloc_register_buffer(gralloc_module_t const *module, buffer_handle_t handle) |
| 64 | { |
| 65 | MALI_IGNORE(module); |
| 66 | |
| 67 | if (private_handle_t::validate(handle) < 0) |
| 68 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 69 | AERR("Registering invalid buffer %p, returning error", handle); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 70 | return -EINVAL; |
| 71 | } |
| 72 | |
| 73 | // if this handle was created in this process, then we keep it as is. |
| 74 | private_handle_t *hnd = (private_handle_t *)handle; |
| 75 | |
| 76 | int retval = -EINVAL; |
| 77 | |
| 78 | pthread_mutex_lock(&s_map_lock); |
| 79 | |
| 80 | #if GRALLOC_ARM_UMP_MODULE |
| 81 | |
| 82 | if (!s_ump_is_open) |
| 83 | { |
| 84 | ump_result res = ump_open(); // MJOLL-4012: UMP implementation needs a ump_close() for each ump_open |
| 85 | |
| 86 | if (res != UMP_OK) |
| 87 | { |
| 88 | pthread_mutex_unlock(&s_map_lock); |
| 89 | AERR("Failed to open UMP library with res=%d", res); |
| 90 | return retval; |
| 91 | } |
| 92 | |
| 93 | s_ump_is_open = 1; |
| 94 | } |
| 95 | |
| 96 | #endif |
| 97 | |
| 98 | hnd->pid = getpid(); |
| 99 | |
| 100 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) |
| 101 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 102 | AINF("Register buffer %p although it will be treated as a nop", handle); |
Chia-I Wu | fba69b5 | 2017-05-08 13:10:08 -0700 | [diff] [blame] | 103 | retval = 0; |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 104 | } |
| 105 | else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) |
| 106 | { |
| 107 | #if GRALLOC_ARM_UMP_MODULE |
| 108 | hnd->ump_mem_handle = (int)ump_handle_create_from_secure_id(hnd->ump_id); |
| 109 | |
| 110 | if (UMP_INVALID_MEMORY_HANDLE != (ump_handle)hnd->ump_mem_handle) |
| 111 | { |
| 112 | hnd->base = ump_mapped_pointer_get((ump_handle)hnd->ump_mem_handle); |
| 113 | |
| 114 | if (0 != hnd->base) |
| 115 | { |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 116 | hnd->writeOwner = 0; |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 117 | hnd->lockState &= ~(private_handle_t::LOCK_STATE_UNREGISTERED); |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 118 | |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 119 | pthread_mutex_unlock(&s_map_lock); |
| 120 | return 0; |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | AERR("Failed to map UMP handle 0x%x", hnd->ump_mem_handle); |
| 125 | } |
| 126 | |
| 127 | ump_reference_release((ump_handle)hnd->ump_mem_handle); |
| 128 | } |
| 129 | else |
| 130 | { |
| 131 | AERR("Failed to create UMP handle 0x%x", hnd->ump_mem_handle); |
| 132 | } |
| 133 | |
| 134 | #else |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 135 | AERR("Gralloc does not support UMP. Unable to register UMP memory for handle %p", hnd); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 136 | #endif |
| 137 | } |
| 138 | else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) |
| 139 | { |
| 140 | #if GRALLOC_ARM_DMA_BUF_MODULE |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 141 | unsigned char *mappedAddress; |
| 142 | size_t size = hnd->size; |
| 143 | hw_module_t *pmodule = NULL; |
| 144 | private_module_t *m = NULL; |
| 145 | |
| 146 | if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0) |
| 147 | { |
| 148 | m = reinterpret_cast<private_module_t *>(pmodule); |
| 149 | } |
| 150 | else |
| 151 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 152 | AERR("Could not get gralloc module for handle: %p", hnd); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 153 | retval = -errno; |
| 154 | goto cleanup; |
| 155 | } |
| 156 | |
| 157 | /* the test condition is set to m->ion_client <= 0 here, because: |
| 158 | * 1) module structure are initialized to 0 if no initial value is applied |
| 159 | * 2) a second user process should get a ion fd greater than 0. |
| 160 | */ |
| 161 | if (m->ion_client <= 0) |
| 162 | { |
| 163 | /* a second user process must obtain a client handle first via ion_open before it can obtain the shared ion buffer*/ |
| 164 | m->ion_client = ion_open(); |
| 165 | |
| 166 | if (m->ion_client < 0) |
| 167 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 168 | AERR("Could not open ion device for handle: %p", hnd); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 169 | retval = -errno; |
| 170 | goto cleanup; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | mappedAddress = (unsigned char *)mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, hnd->share_fd, 0); |
| 175 | |
| 176 | if (MAP_FAILED == mappedAddress) |
| 177 | { |
| 178 | AERR("mmap( share_fd:%d ) failed with %s", hnd->share_fd, strerror(errno)); |
| 179 | retval = -errno; |
| 180 | goto cleanup; |
| 181 | } |
| 182 | |
| 183 | hnd->base = mappedAddress + hnd->offset; |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 184 | hnd->lockState &= ~(private_handle_t::LOCK_STATE_UNREGISTERED); |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 185 | |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 186 | pthread_mutex_unlock(&s_map_lock); |
| 187 | return 0; |
| 188 | #endif |
| 189 | } |
| 190 | else |
| 191 | { |
| 192 | AERR("registering non-UMP buffer not supported. flags = %d", hnd->flags); |
| 193 | } |
| 194 | |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 195 | #if GRALLOC_ARM_DMA_BUF_MODULE |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 196 | cleanup: |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 197 | #endif |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 198 | pthread_mutex_unlock(&s_map_lock); |
| 199 | return retval; |
| 200 | } |
| 201 | |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 202 | static void unmap_buffer(private_handle_t *hnd) |
| 203 | { |
| 204 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP) |
| 205 | { |
| 206 | #if GRALLOC_ARM_UMP_MODULE |
| 207 | ump_mapped_pointer_release((ump_handle)hnd->ump_mem_handle); |
| 208 | ump_reference_release((ump_handle)hnd->ump_mem_handle); |
| 209 | hnd->ump_mem_handle = (int)UMP_INVALID_MEMORY_HANDLE; |
| 210 | #else |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 211 | AERR("Can't unregister UMP buffer for handle %p. Not supported", hnd); |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 212 | #endif |
| 213 | } |
| 214 | else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) |
| 215 | { |
| 216 | #if GRALLOC_ARM_DMA_BUF_MODULE |
| 217 | void *base = (void *)hnd->base; |
| 218 | size_t size = hnd->size; |
| 219 | |
| 220 | if (munmap(base, size) < 0) |
| 221 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 222 | AERR("Could not munmap base:%p size:%lu '%s'", base, (unsigned long)size, strerror(errno)); |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | #else |
| 226 | AERR("Can't unregister DMA_BUF buffer for hnd %p. Not supported", hnd); |
| 227 | #endif |
| 228 | |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | AERR("Unregistering unknown buffer is not supported. Flags = %d", hnd->flags); |
| 233 | } |
| 234 | |
| 235 | hnd->base = 0; |
| 236 | hnd->lockState = 0; |
| 237 | hnd->writeOwner = 0; |
| 238 | } |
| 239 | |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 240 | static int gralloc_unregister_buffer(gralloc_module_t const *module, buffer_handle_t handle) |
| 241 | { |
| 242 | MALI_IGNORE(module); |
| 243 | |
| 244 | if (private_handle_t::validate(handle) < 0) |
| 245 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 246 | AERR("unregistering invalid buffer %p, returning error", handle); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 247 | return -EINVAL; |
| 248 | } |
| 249 | |
| 250 | private_handle_t *hnd = (private_handle_t *)handle; |
| 251 | |
| 252 | AERR_IF(hnd->lockState & private_handle_t::LOCK_STATE_READ_MASK, "[unregister] handle %p still locked (state=%08x)", hnd, hnd->lockState); |
| 253 | |
| 254 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) |
| 255 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 256 | AERR("Can't unregister buffer %p as it is a framebuffer", handle); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 257 | } |
| 258 | else if (hnd->pid == getpid()) // never unmap buffers that were not registered in this process |
| 259 | { |
| 260 | pthread_mutex_lock(&s_map_lock); |
| 261 | |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 262 | hnd->lockState &= ~(private_handle_t::LOCK_STATE_MAPPED); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 263 | |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 264 | /* if handle is still locked, the unmapping would not happen until unlocked*/ |
| 265 | if (!(hnd->lockState & private_handle_t::LOCK_STATE_WRITE)) |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 266 | { |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 267 | unmap_buffer(hnd); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 268 | } |
| 269 | |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 270 | hnd->lockState |= private_handle_t::LOCK_STATE_UNREGISTERED; |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 271 | |
| 272 | pthread_mutex_unlock(&s_map_lock); |
| 273 | } |
| 274 | else |
| 275 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 276 | AERR("Trying to unregister buffer %p from process %d that was not created in current process: %d", hnd, hnd->pid, getpid()); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | static int gralloc_lock(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, void **vaddr) |
| 283 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 284 | |
| 285 | |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 286 | if (private_handle_t::validate(handle) < 0) |
| 287 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 288 | AERR("Locking invalid buffer %p, returning error", handle); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 289 | return -EINVAL; |
| 290 | } |
| 291 | |
| 292 | private_handle_t *hnd = (private_handle_t *)handle; |
| 293 | |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 294 | if (hnd->format == HAL_PIXEL_FORMAT_YCbCr_420_888) |
| 295 | { |
| 296 | AERR("Buffer with format HAL_PIXEL_FORMAT_YCbCr_*_888 must be locked by lock_ycbcr()"); |
| 297 | return -EINVAL; |
| 298 | } |
| 299 | |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 300 | pthread_mutex_lock(&s_map_lock); |
| 301 | |
| 302 | if (hnd->lockState & private_handle_t::LOCK_STATE_UNREGISTERED) |
| 303 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 304 | AERR("Locking on an unregistered buffer %p, returning error", hnd); |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 305 | pthread_mutex_unlock(&s_map_lock); |
| 306 | return -EINVAL; |
| 307 | } |
| 308 | |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 309 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP || hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) |
| 310 | { |
| 311 | hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK; |
| 312 | } |
| 313 | |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 314 | hnd->lockState |= private_handle_t::LOCK_STATE_WRITE; |
| 315 | |
| 316 | pthread_mutex_unlock(&s_map_lock); |
| 317 | |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 318 | if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) |
| 319 | { |
| 320 | *vaddr = (void *)hnd->base; |
| 321 | } |
| 322 | |
| 323 | MALI_IGNORE(module); |
| 324 | MALI_IGNORE(l); |
| 325 | MALI_IGNORE(t); |
| 326 | MALI_IGNORE(w); |
| 327 | MALI_IGNORE(h); |
| 328 | return 0; |
| 329 | } |
| 330 | |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 331 | static int gralloc_lock_ycbcr(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr) |
| 332 | { |
| 333 | int retval = 0; |
| 334 | int ystride, cstride; |
| 335 | |
| 336 | if (private_handle_t::validate(handle) < 0) |
| 337 | { |
| 338 | AERR("Locking invalid buffer %p, returning error", handle); |
| 339 | return -EINVAL; |
| 340 | } |
| 341 | |
| 342 | private_handle_t *hnd = (private_handle_t *)handle; |
| 343 | |
| 344 | pthread_mutex_lock(&s_map_lock); |
| 345 | |
| 346 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP || hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) |
| 347 | { |
| 348 | hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK; |
| 349 | } |
| 350 | |
| 351 | hnd->lockState |= private_handle_t::LOCK_STATE_WRITE; |
| 352 | |
| 353 | pthread_mutex_unlock(&s_map_lock); |
| 354 | |
| 355 | |
| 356 | if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) |
| 357 | { |
| 358 | switch (hnd->format) |
| 359 | { |
| 360 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 361 | ystride = cstride = GRALLOC_ALIGN(hnd->width, 16); |
| 362 | ycbcr->y = (void *)hnd->base; |
| 363 | ycbcr->cr = (void *)((unsigned char *)hnd->base + ystride * hnd->height); |
| 364 | ycbcr->cb = (void *)((unsigned char *)hnd->base + ystride * hnd->height + 1); |
| 365 | ycbcr->ystride = ystride; |
| 366 | ycbcr->cstride = cstride; |
| 367 | ycbcr->chroma_step = 2; |
| 368 | break; |
| 369 | |
| 370 | case HAL_PIXEL_FORMAT_YV12: |
| 371 | /* Here to keep consistency with YV12 alignment, define the ystride according to image height. */ |
| 372 | ystride = GRALLOC_ALIGN(hnd->width, (hnd->height % 8 == 0) ? GRALLOC_ALIGN_BASE_16 : |
| 373 | ((hnd->height % 4 == 0) ? GRALLOC_ALIGN_BASE_64 : GRALLOC_ALIGN_BASE_128)); |
| 374 | cstride = GRALLOC_ALIGN(ystride / 2, 16); |
| 375 | ycbcr->y = (void *)hnd->base; |
| 376 | /* the ystride calc is assuming the height can at least be divided by 2 */ |
| 377 | ycbcr->cr = (void *)((unsigned char *)hnd->base + ystride * GRALLOC_ALIGN(hnd->height, 2)); |
| 378 | ycbcr->cb = (void *)((unsigned char *)hnd->base + ystride * GRALLOC_ALIGN(hnd->height, 2) + cstride * hnd->height / 2); |
| 379 | ycbcr->ystride = ystride; |
| 380 | ycbcr->cstride = cstride; |
| 381 | ycbcr->chroma_step = 1; |
| 382 | break; |
| 383 | |
| 384 | #ifdef SUPPORT_LEGACY_FORMAT |
| 385 | |
| 386 | case HAL_PIXEL_FORMAT_YCbCr_420_SP: |
| 387 | ystride = cstride = GRALLOC_ALIGN(hnd->width, 16); |
| 388 | ycbcr->y = (void *)hnd->base; |
| 389 | ycbcr->cb = (void *)((unsigned char *)hnd->base + ystride * hnd->height); |
| 390 | ycbcr->cr = (void *)((unsigned char *)hnd->base + ystride * hnd->height + 1); |
| 391 | ycbcr->ystride = ystride; |
| 392 | ycbcr->cstride = cstride; |
| 393 | ycbcr->chroma_step = 2; |
| 394 | break; |
| 395 | #endif |
| 396 | |
| 397 | default: |
| 398 | AERR("Can not lock buffer, invalid format: 0x%x", hnd->format); |
| 399 | retval = -EINVAL; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | MALI_IGNORE(module); |
| 404 | MALI_IGNORE(l); |
| 405 | MALI_IGNORE(t); |
| 406 | MALI_IGNORE(w); |
| 407 | MALI_IGNORE(h); |
| 408 | return retval; |
| 409 | } |
| 410 | |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 411 | static int gralloc_unlock(gralloc_module_t const *module, buffer_handle_t handle) |
| 412 | { |
| 413 | MALI_IGNORE(module); |
| 414 | |
| 415 | if (private_handle_t::validate(handle) < 0) |
| 416 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 417 | AERR("Unlocking invalid buffer %p, returning error", handle); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 418 | return -EINVAL; |
| 419 | } |
| 420 | |
| 421 | private_handle_t *hnd = (private_handle_t *)handle; |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 422 | |
| 423 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_UMP && hnd->writeOwner) |
| 424 | { |
| 425 | #if GRALLOC_ARM_UMP_MODULE |
| 426 | ump_cpu_msync_now((ump_handle)hnd->ump_mem_handle, UMP_MSYNC_CLEAN_AND_INVALIDATE, (void *)hnd->base, hnd->size); |
| 427 | #else |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 428 | AERR("Buffer %p is UMP type but it is not supported", hnd); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 429 | #endif |
| 430 | } |
| 431 | else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION && hnd->writeOwner) |
| 432 | { |
| 433 | #if GRALLOC_ARM_DMA_BUF_MODULE |
| 434 | hw_module_t *pmodule = NULL; |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 435 | private_module_t *m = NULL; |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 436 | |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 437 | if (hw_get_module(GRALLOC_HARDWARE_MODULE_ID, (const hw_module_t **)&pmodule) == 0) |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 438 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 439 | m = reinterpret_cast<private_module_t *>(pmodule); |
| 440 | //ion_sync_fd(m->ion_client, hnd->share_fd); |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 441 | } |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 442 | else |
| 443 | { |
| 444 | AERR("Couldnot get gralloc module for handle %p\n", handle); |
| 445 | } |
| 446 | |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 447 | #endif |
| 448 | } |
| 449 | |
John Stultz | 3728745 | 2016-01-20 20:08:46 -0800 | [diff] [blame] | 450 | pthread_mutex_lock(&s_map_lock); |
| 451 | |
| 452 | hnd->lockState &= ~(private_handle_t::LOCK_STATE_WRITE); |
| 453 | |
| 454 | /* if the handle has already been unregistered, unmap it here*/ |
| 455 | if (hnd->lockState & private_handle_t::LOCK_STATE_UNREGISTERED) |
| 456 | { |
| 457 | unmap_buffer(hnd); |
| 458 | } |
| 459 | |
| 460 | pthread_mutex_unlock(&s_map_lock); |
| 461 | |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 462 | return 0; |
| 463 | } |
| 464 | |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 465 | #if defined(GRALLOC_MODULE_API_VERSION_0_3) |
| 466 | static int gralloc_lock_async (gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, void **vaddr, int fenceFD) |
| 467 | { |
| 468 | if (fenceFD >= 0) |
| 469 | { |
| 470 | sync_wait(fenceFD, -1); |
| 471 | close(fenceFD); |
| 472 | } |
| 473 | |
| 474 | return gralloc_lock(module, handle, usage, l, t, w, h, vaddr); |
| 475 | } |
| 476 | |
| 477 | static int gralloc_unlock_async(gralloc_module_t const *module, buffer_handle_t handle, int *fenceFD) |
| 478 | { |
| 479 | *fenceFD = -1; |
| 480 | |
| 481 | if (gralloc_unlock(module, handle) < 0) |
| 482 | { |
| 483 | return -EINVAL; |
| 484 | } |
| 485 | |
| 486 | return 0; |
| 487 | |
| 488 | } |
| 489 | |
| 490 | static int gralloc_lock_async_ycbcr(gralloc_module_t const *module, buffer_handle_t handle, int usage, int l, int t, int w, int h, struct android_ycbcr *ycbcr, int fenceFD) |
| 491 | { |
| 492 | if (fenceFD >= 0) |
| 493 | { |
| 494 | sync_wait(fenceFD, -1); |
| 495 | close(fenceFD); |
| 496 | } |
| 497 | |
| 498 | return gralloc_lock_ycbcr(module, handle, usage, l, t, w, h, ycbcr); |
| 499 | } |
| 500 | #endif |
| 501 | |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 502 | // There is one global instance of the module |
| 503 | |
| 504 | static struct hw_module_methods_t gralloc_module_methods = |
| 505 | { |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 506 | .open = gralloc_device_open |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 507 | }; |
| 508 | |
| 509 | private_module_t::private_module_t() |
| 510 | { |
| 511 | #define INIT_ZERO(obj) (memset(&(obj),0,sizeof((obj)))) |
| 512 | |
| 513 | base.common.tag = HARDWARE_MODULE_TAG; |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 514 | #if defined(GRALLOC_MODULE_API_VERSION_0_3) |
| 515 | base.common.version_major = GRALLOC_MODULE_API_VERSION_0_3; |
| 516 | #else |
| 517 | base.common.version_major = GRALLOC_MODULE_API_VERSION_0_2; |
| 518 | #endif |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 519 | base.common.version_minor = 0; |
| 520 | base.common.id = GRALLOC_HARDWARE_MODULE_ID; |
| 521 | base.common.name = "Graphics Memory Allocator Module"; |
| 522 | base.common.author = "ARM Ltd."; |
| 523 | base.common.methods = &gralloc_module_methods; |
| 524 | base.common.dso = NULL; |
| 525 | INIT_ZERO(base.common.reserved); |
| 526 | |
| 527 | base.registerBuffer = gralloc_register_buffer; |
| 528 | base.unregisterBuffer = gralloc_unregister_buffer; |
| 529 | base.lock = gralloc_lock; |
| 530 | base.unlock = gralloc_unlock; |
| 531 | base.perform = NULL; |
John Stultz | e5c5bb3 | 2018-03-20 18:16:49 -0700 | [diff] [blame] | 532 | base.lock_ycbcr = gralloc_lock_ycbcr; |
| 533 | #if defined(GRALLOC_MODULE_API_VERSION_0_3) |
| 534 | base.lockAsync = gralloc_lock_async; |
| 535 | base.unlockAsync = gralloc_unlock_async; |
| 536 | base.lockAsync_ycbcr = gralloc_lock_async_ycbcr; |
| 537 | #endif |
Vishal Bhoj | 78e9049 | 2015-12-07 01:36:32 +0530 | [diff] [blame] | 538 | INIT_ZERO(base.reserved_proc); |
| 539 | |
| 540 | framebuffer = NULL; |
| 541 | flags = 0; |
| 542 | numBuffers = 0; |
| 543 | bufferMask = 0; |
| 544 | pthread_mutex_init(&(lock), NULL); |
| 545 | currentBuffer = NULL; |
| 546 | INIT_ZERO(info); |
| 547 | INIT_ZERO(finfo); |
| 548 | xdpi = 0.0f; |
| 549 | ydpi = 0.0f; |
| 550 | fps = 0.0f; |
| 551 | |
| 552 | #undef INIT_ZERO |
| 553 | }; |
| 554 | |
| 555 | /* |
| 556 | * HAL_MODULE_INFO_SYM will be initialized using the default constructor |
| 557 | * implemented above |
| 558 | */ |
| 559 | struct private_module_t HAL_MODULE_INFO_SYM; |
| 560 | |