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