John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [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> |
| 21 | #include <inttypes.h> |
| 22 | |
| 23 | #include <cutils/log.h> |
| 24 | #include <cutils/atomic.h> |
| 25 | #include <hardware/hardware.h> |
| 26 | #include <hardware/gralloc.h> |
| 27 | |
| 28 | #include "gralloc_priv.h" |
| 29 | #include "gralloc_helper.h" |
| 30 | #include "alloc_device.h" |
| 31 | #include "framebuffer_device.h" |
| 32 | |
| 33 | #include "gralloc_module_allocator_specific.h" |
| 34 | #include "gralloc_buffer_priv.h" |
| 35 | |
| 36 | #include "mali_gralloc_formats.h" |
| 37 | |
| 38 | static pthread_mutex_t s_map_lock = PTHREAD_MUTEX_INITIALIZER; |
| 39 | |
| 40 | static int gralloc_device_open(const hw_module_t* module, const char* name, hw_device_t** device) |
| 41 | { |
| 42 | int status = -EINVAL; |
| 43 | |
| 44 | if (!strncmp(name, GRALLOC_HARDWARE_GPU0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN)) |
| 45 | { |
| 46 | status = alloc_device_open(module, name, device); |
| 47 | } |
| 48 | else if (!strncmp(name, GRALLOC_HARDWARE_FB0, MALI_GRALLOC_HARDWARE_MAX_STR_LEN)) |
| 49 | { |
| 50 | status = framebuffer_device_open(module, name, device); |
| 51 | } |
| 52 | |
| 53 | return status; |
| 54 | } |
| 55 | |
| 56 | static int gralloc_register_buffer(gralloc_module_t const* module, buffer_handle_t handle) |
| 57 | { |
| 58 | GRALLOC_UNUSED(module); |
| 59 | |
| 60 | if (private_handle_t::validate(handle) < 0) |
| 61 | { |
| 62 | AERR("Registering invalid buffer %p, returning error", handle); |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | |
| 66 | // if this handle was created in this process, then we keep it as is. |
| 67 | private_handle_t* hnd = (private_handle_t*)handle; |
| 68 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 69 | int retval = -EINVAL; |
| 70 | |
| 71 | pthread_mutex_lock(&s_map_lock); |
| 72 | |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 73 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) |
| 74 | { |
| 75 | AERR( "Can't register buffer %p as it is a framebuffer", handle ); |
| 76 | } |
| 77 | else if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) |
| 78 | { |
| 79 | retval = gralloc_backend_register(hnd); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | AERR("unknown buffer flags not supported. flags = %d", hnd->flags ); |
| 84 | } |
| 85 | |
| 86 | pthread_mutex_unlock(&s_map_lock); |
| 87 | return retval; |
| 88 | } |
| 89 | |
| 90 | static int gralloc_unregister_buffer(gralloc_module_t const* module, buffer_handle_t handle) |
| 91 | { |
| 92 | GRALLOC_UNUSED(module); |
| 93 | |
| 94 | if (private_handle_t::validate(handle) < 0) |
| 95 | { |
| 96 | AERR("unregistering invalid buffer %p, returning error", handle); |
| 97 | return -EINVAL; |
| 98 | } |
| 99 | |
| 100 | private_handle_t* hnd = (private_handle_t*)handle; |
| 101 | |
| 102 | AERR_IF(hnd->lockState & private_handle_t::LOCK_STATE_READ_MASK, "[unregister] handle %p still locked (state=%08x)", hnd, hnd->lockState); |
| 103 | |
| 104 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) |
| 105 | { |
| 106 | AERR( "Can't unregister buffer %p as it is a framebuffer", handle ); |
| 107 | } |
Chia-I Wu | a3db107 | 2017-05-08 12:55:28 -0700 | [diff] [blame^] | 108 | else |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 109 | { |
| 110 | pthread_mutex_lock(&s_map_lock); |
| 111 | |
| 112 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) |
| 113 | { |
| 114 | gralloc_backend_unregister(hnd); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | AERR("Unregistering unknown buffer is not supported. Flags = %d", hnd->flags); |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Close shared attribute region file descriptor. It might seem strange to "free" |
| 123 | * this here since this can happen in a client process, but free here is nothing |
| 124 | * but unmapping and closing the duplicated file descriptor. The original ashmem |
| 125 | * fd instance is still open until alloc_device_free() is called. Even sharing |
| 126 | * of gralloc buffers within the same process should have fds dup:ed. |
| 127 | */ |
| 128 | gralloc_buffer_attr_free( hnd ); |
| 129 | |
| 130 | hnd->base = 0; |
| 131 | hnd->lockState = 0; |
| 132 | hnd->writeOwner = 0; |
| 133 | |
| 134 | pthread_mutex_unlock(&s_map_lock); |
| 135 | } |
John Stultz | 16100f6 | 2017-05-03 11:12:18 -0700 | [diff] [blame] | 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | 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) |
| 141 | { |
| 142 | GRALLOC_UNUSED(module); |
| 143 | GRALLOC_UNUSED(l); |
| 144 | GRALLOC_UNUSED(t); |
| 145 | GRALLOC_UNUSED(w); |
| 146 | GRALLOC_UNUSED(h); |
| 147 | |
| 148 | if (private_handle_t::validate(handle) < 0) |
| 149 | { |
| 150 | AERR("Locking invalid buffer %p, returning error", handle ); |
| 151 | return -EINVAL; |
| 152 | } |
| 153 | |
| 154 | private_handle_t* hnd = (private_handle_t*)handle; |
| 155 | |
| 156 | if (hnd->req_format == HAL_PIXEL_FORMAT_YCbCr_420_888) |
| 157 | { |
| 158 | AERR("Buffers with format YCbCr_420_888 must be locked using (*lock_ycbcr)" ); |
| 159 | return -EINVAL; |
| 160 | } |
| 161 | |
| 162 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) |
| 163 | { |
| 164 | hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK; |
| 165 | } |
| 166 | if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) |
| 167 | { |
| 168 | *vaddr = (void*)hnd->base; |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static int gralloc_lock_ycbcr(gralloc_module_t const* module, buffer_handle_t handle, int usage, |
| 174 | int l, int t, int w, int h, |
| 175 | android_ycbcr *ycbcr) |
| 176 | { |
| 177 | GRALLOC_UNUSED(module); |
| 178 | GRALLOC_UNUSED(l); |
| 179 | GRALLOC_UNUSED(t); |
| 180 | GRALLOC_UNUSED(w); |
| 181 | GRALLOC_UNUSED(h); |
| 182 | |
| 183 | if (private_handle_t::validate(handle) < 0) |
| 184 | { |
| 185 | AERR("Locking invalid buffer %p, returning error", handle ); |
| 186 | return -EINVAL; |
| 187 | } |
| 188 | |
| 189 | private_handle_t* hnd = (private_handle_t*)handle; |
| 190 | |
| 191 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION) |
| 192 | { |
| 193 | hnd->writeOwner = usage & GRALLOC_USAGE_SW_WRITE_MASK; |
| 194 | } |
| 195 | if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK) && |
| 196 | !(hnd->internal_format & MALI_GRALLOC_INTFMT_EXT_MASK)) |
| 197 | { |
| 198 | char* base = (char*)hnd->base; |
| 199 | int y_stride = hnd->byte_stride; |
| 200 | /* Ensure height is aligned for subsampled chroma before calculating buffer parameters */ |
| 201 | int adjusted_height = GRALLOC_ALIGN(hnd->height, 2); |
| 202 | int y_size = y_stride * adjusted_height; |
| 203 | |
| 204 | int u_offset = 0; |
| 205 | int v_offset = 0; |
| 206 | int c_stride = 0; |
| 207 | int step = 0; |
| 208 | |
| 209 | uint64_t base_format = hnd->internal_format & MALI_GRALLOC_INTFMT_FMT_MASK; |
| 210 | |
| 211 | switch (base_format) |
| 212 | { |
| 213 | case MALI_GRALLOC_FORMAT_INTERNAL_NV12: |
| 214 | c_stride = y_stride; |
| 215 | /* Y plane, UV plane */ |
| 216 | u_offset = y_size; |
| 217 | v_offset = y_size + 1; |
| 218 | step = 2; |
| 219 | break; |
| 220 | |
| 221 | case MALI_GRALLOC_FORMAT_INTERNAL_NV21: |
| 222 | c_stride = y_stride; |
| 223 | /* Y plane, UV plane */ |
| 224 | v_offset = y_size; |
| 225 | u_offset = y_size + 1; |
| 226 | step = 2; |
| 227 | break; |
| 228 | |
| 229 | case MALI_GRALLOC_FORMAT_INTERNAL_YV12: |
| 230 | { |
| 231 | int c_size; |
| 232 | |
| 233 | /* Stride alignment set to 16 as the SW access flags were set */ |
| 234 | c_stride = GRALLOC_ALIGN(hnd->byte_stride / 2, 16); |
| 235 | c_size = c_stride * (adjusted_height / 2); |
| 236 | /* Y plane, V plane, U plane */ |
| 237 | v_offset = y_size; |
| 238 | u_offset = y_size + c_size; |
| 239 | step = 1; |
| 240 | break; |
| 241 | } |
| 242 | |
| 243 | default: |
| 244 | AERR("Can't lock buffer %p: wrong format %" PRIx64, hnd, hnd->internal_format); |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
| 248 | ycbcr->y = base; |
| 249 | ycbcr->cb = base + u_offset; |
| 250 | ycbcr->cr = base + v_offset; |
| 251 | ycbcr->ystride = y_stride; |
| 252 | ycbcr->cstride = c_stride; |
| 253 | ycbcr->chroma_step = step; |
| 254 | } |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | static int gralloc_unlock(gralloc_module_t const* module, buffer_handle_t handle) |
| 259 | { |
| 260 | GRALLOC_UNUSED(module); |
| 261 | |
| 262 | if (private_handle_t::validate(handle) < 0) |
| 263 | { |
| 264 | AERR( "Unlocking invalid buffer %p, returning error", handle ); |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
| 268 | private_handle_t* hnd = (private_handle_t*)handle; |
| 269 | |
| 270 | if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION && hnd->writeOwner) |
| 271 | { |
| 272 | gralloc_backend_sync(hnd); |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | // There is one global instance of the module |
| 279 | |
| 280 | static struct hw_module_methods_t gralloc_module_methods = |
| 281 | { |
| 282 | gralloc_device_open |
| 283 | }; |
| 284 | |
| 285 | private_module_t::private_module_t() |
| 286 | { |
| 287 | #define INIT_ZERO(obj) (memset(&(obj),0,sizeof((obj)))) |
| 288 | |
| 289 | base.common.tag = HARDWARE_MODULE_TAG; |
| 290 | base.common.version_major = 1; |
| 291 | base.common.version_minor = 0; |
| 292 | base.common.id = GRALLOC_HARDWARE_MODULE_ID; |
| 293 | base.common.name = "Graphics Memory Allocator Module"; |
| 294 | base.common.author = "ARM Ltd."; |
| 295 | base.common.methods = &gralloc_module_methods; |
| 296 | base.common.dso = NULL; |
| 297 | INIT_ZERO(base.common.reserved); |
| 298 | |
| 299 | base.registerBuffer = gralloc_register_buffer; |
| 300 | base.unregisterBuffer = gralloc_unregister_buffer; |
| 301 | base.lock = gralloc_lock; |
| 302 | base.lock_ycbcr = gralloc_lock_ycbcr; |
| 303 | base.unlock = gralloc_unlock; |
| 304 | base.perform = NULL; |
| 305 | INIT_ZERO(base.reserved_proc); |
| 306 | |
| 307 | framebuffer = NULL; |
| 308 | flags = 0; |
| 309 | numBuffers = 0; |
| 310 | bufferMask = 0; |
| 311 | pthread_mutex_init(&(lock), NULL); |
| 312 | currentBuffer = NULL; |
| 313 | INIT_ZERO(info); |
| 314 | INIT_ZERO(finfo); |
| 315 | xdpi = 0.0f; |
| 316 | ydpi = 0.0f; |
| 317 | fps = 0.0f; |
| 318 | swapInterval = 1; |
| 319 | |
| 320 | #undef INIT_ZERO |
| 321 | }; |
| 322 | |
| 323 | /* |
| 324 | * HAL_MODULE_INFO_SYM will be initialized using the default constructor |
| 325 | * implemented above |
| 326 | */ |
| 327 | struct private_module_t HAL_MODULE_INFO_SYM; |
| 328 | |