1/* 2 * Copyright (C) 2008 The Android Open Source Project 3 * Copyright (c) 2011-2017, The Linux Foundation. All rights reserved. 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 18#define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL) 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> 30 31#include <cutils/log.h> 32#include <cutils/atomic.h> 33#include <utils/Trace.h> 34 35#include <hardware/hardware.h> 36#include <hardware/gralloc.h> 37 38#include "gralloc_priv.h" 39#include "gr.h" 40#include "alloc_controller.h" 41#include "memalloc.h" 42#include <qdMetaData.h> 43 44 45using namespace gralloc; 46/*****************************************************************************/ 47 48// Return the type of allocator - 49// these are used for mapping/unmapping 50static IMemAlloc* getAllocator(int flags) 51{ 52 IMemAlloc* memalloc; 53 IAllocController* alloc_ctrl = IAllocController::getInstance(); 54 memalloc = alloc_ctrl->getAllocator(flags); 55 return memalloc; 56} 57 58static int gralloc_map_metadata(buffer_handle_t handle) { 59 private_handle_t* hnd = (private_handle_t*)handle; 60 hnd->base_metadata = 0; 61 IMemAlloc* memalloc = getAllocator(hnd->flags) ; 62 void *mappedAddress = MAP_FAILED; 63 unsigned int size = 0; 64 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { 65 mappedAddress = MAP_FAILED; 66 size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); 67 int ret = memalloc->map_buffer(&mappedAddress, size, 68 hnd->offset_metadata, hnd->fd_metadata); 69 if(ret || mappedAddress == MAP_FAILED) { 70 ALOGE("Could not mmap metadata for handle %p, fd=%d (%s)", 71 hnd, hnd->fd_metadata, strerror(errno)); 72 return -errno; 73 } 74 hnd->base_metadata = uint64_t(mappedAddress); 75 } 76 return 0; 77} 78 79static int gralloc_map(gralloc_module_t const* module, 80 buffer_handle_t handle) 81{ 82 ATRACE_CALL(); 83 if(!module) 84 return -EINVAL; 85 86 private_handle_t* hnd = (private_handle_t*)handle; 87 unsigned int size = 0; 88 int err = 0; 89 IMemAlloc* memalloc = getAllocator(hnd->flags) ; 90 void *mappedAddress = MAP_FAILED; 91 hnd->base = 0; 92 93 // Dont map framebuffer and secure buffers 94 if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) && 95 !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) { 96 size = hnd->size; 97 err = memalloc->map_buffer(&mappedAddress, size, 98 hnd->offset, hnd->fd); 99 if(err || mappedAddress == MAP_FAILED) { 100 ALOGE("Could not mmap handle %p, fd=%d (%s)", 101 handle, hnd->fd, strerror(errno)); 102 return -errno; 103 } 104 105 hnd->base = uint64_t(mappedAddress); 106 } else { 107 // Cannot map secure buffers or framebuffers, but still need to map 108 // metadata for secure buffers. 109 // If mapping a secure buffers fails, the framework needs to get 110 // an error code. 111 err = -EACCES; 112 } 113 114 //Allow mapping of metadata for all buffers including secure ones, but not 115 //of framebuffer 116 int metadata_err = gralloc_map_metadata(handle); 117 if (!err) { 118 err = metadata_err; 119 } 120 return err; 121} 122 123static int gralloc_unmap(gralloc_module_t const* module, 124 buffer_handle_t handle) 125{ 126 ATRACE_CALL(); 127 int err = -EINVAL; 128 if(!module) 129 return err; 130 131 private_handle_t* hnd = (private_handle_t*)handle; 132 IMemAlloc* memalloc = getAllocator(hnd->flags) ; 133 if(!memalloc) 134 return err; 135 136 if(hnd->base) { 137 err = memalloc->unmap_buffer((void*)hnd->base, hnd->size, hnd->offset); 138 if (err) { 139 ALOGE("Could not unmap memory at address %p, %s", (void*) hnd->base, 140 strerror(errno)); 141 return -errno; 142 } 143 hnd->base = 0; 144 } 145 146 if(hnd->base_metadata) { 147 unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); 148 err = memalloc->unmap_buffer((void*)hnd->base_metadata, 149 size, hnd->offset_metadata); 150 if (err) { 151 ALOGE("Could not unmap memory at address %p, %s", 152 (void*) hnd->base_metadata, strerror(errno)); 153 return -errno; 154 } 155 hnd->base_metadata = 0; 156 } 157 158 return 0; 159} 160 161/*****************************************************************************/ 162 163static pthread_mutex_t sMapLock = PTHREAD_MUTEX_INITIALIZER; 164 165/*****************************************************************************/ 166 167int gralloc_register_buffer(gralloc_module_t const* module, 168 buffer_handle_t handle) 169{ 170 ATRACE_CALL(); 171 if (!module || private_handle_t::validate(handle) < 0) 172 return -EINVAL; 173 174 int err = gralloc_map(module, handle); 175 /* Do not fail register_buffer for secure buffers*/ 176 if (err == -EACCES) 177 err = 0; 178 return err; 179} 180 181int gralloc_unregister_buffer(gralloc_module_t const* module, 182 buffer_handle_t handle) 183{ 184 ATRACE_CALL(); 185 if (!module || private_handle_t::validate(handle) < 0) 186 return -EINVAL; 187 188 /* 189 * If the buffer has been mapped during a lock operation, it's time 190 * to un-map it. It's an error to be here with a locked buffer. 191 * NOTE: the framebuffer is handled differently and is never unmapped. 192 * Also base and base_metadata are reset. 193 */ 194 return gralloc_unmap(module, handle); 195} 196 197int terminateBuffer(gralloc_module_t const* module, 198 private_handle_t* hnd) 199{ 200 ATRACE_CALL(); 201 if(!module) 202 return -EINVAL; 203 204 /* 205 * If the buffer has been mapped during a lock operation, it's time 206 * to un-map it. It's an error to be here with a locked buffer. 207 * NOTE: the framebuffer is handled differently and is never unmapped. 208 * Also base and base_metadata are reset. 209 */ 210 return gralloc_unmap(module, hnd); 211} 212 213static int gralloc_map_and_invalidate (gralloc_module_t const* module, 214 buffer_handle_t handle, int usage) 215{ 216 ATRACE_CALL(); 217 if (!module || private_handle_t::validate(handle) < 0) 218 return -EINVAL; 219 220 int err = 0; 221 private_handle_t* hnd = (private_handle_t*)handle; 222 if (usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { 223 if (hnd->base == 0) { 224 // we need to map for real 225 pthread_mutex_t* const lock = &sMapLock; 226 pthread_mutex_lock(lock); 227 err = gralloc_map(module, handle); 228 pthread_mutex_unlock(lock); 229 } 230 if (hnd->flags & private_handle_t::PRIV_FLAGS_USES_ION and 231 hnd->flags & private_handle_t::PRIV_FLAGS_CACHED) { 232 //Invalidate if CPU reads in software and there are non-CPU 233 //writers. No need to do this for the metadata buffer as it is 234 //only read/written in software. 235 if ((usage & GRALLOC_USAGE_SW_READ_MASK) and 236 (hnd->flags & private_handle_t::PRIV_FLAGS_NON_CPU_WRITER)) 237 { 238 IMemAlloc* memalloc = getAllocator(hnd->flags) ; 239 err = memalloc->clean_buffer((void*)hnd->base, 240 hnd->size, hnd->offset, hnd->fd, 241 CACHE_INVALIDATE); 242 } 243 //Mark the buffer to be flushed after CPU write. 244 if (usage & GRALLOC_USAGE_SW_WRITE_MASK) { 245 hnd->flags |= private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; 246 } 247 } 248 } 249 250 return err; 251} 252 253int gralloc_lock(gralloc_module_t const* module, 254 buffer_handle_t handle, int usage, 255 int /*l*/, int /*t*/, int /*w*/, int /*h*/, 256 void** vaddr) 257{ 258 ATRACE_CALL(); 259 private_handle_t* hnd = (private_handle_t*)handle; 260 int err = gralloc_map_and_invalidate(module, handle, usage); 261 if(!err) 262 *vaddr = (void*)hnd->base; 263 return err; 264} 265 266int gralloc_lock_ycbcr(gralloc_module_t const* module, 267 buffer_handle_t handle, int usage, 268 int /*l*/, int /*t*/, int /*w*/, int /*h*/, 269 struct android_ycbcr *ycbcr) 270{ 271 ATRACE_CALL(); 272 private_handle_t* hnd = (private_handle_t*)handle; 273 int err = gralloc_map_and_invalidate(module, handle, usage); 274 if(!err) 275 err = getYUVPlaneInfo(hnd, ycbcr); 276 return err; 277} 278 279int gralloc_unlock(gralloc_module_t const* module, 280 buffer_handle_t handle) 281{ 282 ATRACE_CALL(); 283 if (!module || private_handle_t::validate(handle) < 0) 284 return -EINVAL; 285 286 int err = 0; 287 private_handle_t* hnd = (private_handle_t*)handle; 288 289 IMemAlloc* memalloc = getAllocator(hnd->flags); 290 if (hnd->flags & private_handle_t::PRIV_FLAGS_NEEDS_FLUSH) { 291 err = memalloc->clean_buffer((void*)hnd->base, 292 hnd->size, hnd->offset, hnd->fd, 293 CACHE_CLEAN); 294 hnd->flags &= ~private_handle_t::PRIV_FLAGS_NEEDS_FLUSH; 295 } 296 297 return err; 298} 299 300/*****************************************************************************/ 301 302int gralloc_perform(struct gralloc_module_t const* module, 303 int operation, ... ) 304{ 305 int res = -EINVAL; 306 va_list args; 307 if(!module) 308 return res; 309 310 va_start(args, operation); 311 switch (operation) { 312 case GRALLOC_MODULE_PERFORM_CREATE_HANDLE_FROM_BUFFER: 313 { 314 int fd = va_arg(args, int); 315 unsigned int size = va_arg(args, unsigned int); 316 unsigned int offset = va_arg(args, unsigned int); 317 void* base = va_arg(args, void*); 318 int width = va_arg(args, int); 319 int height = va_arg(args, int); 320 int format = va_arg(args, int); 321 int alignedw = 0, alignedh = 0; 322 323 native_handle_t** handle = va_arg(args, native_handle_t**); 324 private_handle_t* hnd = (private_handle_t*)native_handle_create( 325 private_handle_t::sNumFds, private_handle_t::sNumInts()); 326 if (hnd) { 327 hnd->magic = private_handle_t::sMagic; 328 hnd->fd = fd; 329 hnd->flags = private_handle_t::PRIV_FLAGS_USES_ION; 330 hnd->size = size; 331 hnd->offset = offset; 332 hnd->base = uint64_t(base) + offset; 333 hnd->gpuaddr = 0; 334 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, 335 height, format, 0, alignedw, alignedh); 336 hnd->width = alignedw; 337 hnd->height = alignedh; 338 hnd->unaligned_width = width; 339 hnd->unaligned_height = height; 340 hnd->format = format; 341 *handle = (native_handle_t *)hnd; 342 res = 0; 343 } 344 break; 345 346 } 347 case GRALLOC_MODULE_PERFORM_GET_STRIDE: 348 { 349 int width = va_arg(args, int); 350 int format = va_arg(args, int); 351 int *stride = va_arg(args, int *); 352 int alignedw = 0, alignedh = 0; 353 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, 354 0, format, 0, alignedw, alignedh); 355 *stride = alignedw; 356 res = 0; 357 } break; 358 359 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_FROM_HANDLE: 360 { 361 const private_handle_t* hnd = va_arg(args, private_handle_t*); 362 int *stride = va_arg(args, int *); 363 if (private_handle_t::validate(hnd)) { 364 return res; 365 } 366 367 int alignedw = 0, alignedh = 0; 368 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(hnd, alignedw, alignedh); 369 *stride = alignedw; 370 371 res = 0; 372 } break; 373 374 case GRALLOC_MODULE_PERFORM_GET_CUSTOM_STRIDE_AND_HEIGHT_FROM_HANDLE: 375 { 376 const private_handle_t* hnd = va_arg(args, private_handle_t*); 377 int *stride = va_arg(args, int *); 378 int *height = va_arg(args, int *); 379 if (private_handle_t::validate(hnd)) { 380 return res; 381 } 382 383 int alignedw = 0, alignedh = 0; 384 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(hnd, alignedw, alignedh); 385 *stride = alignedw; 386 *height = alignedh; 387 388 res = 0; 389 } break; 390 391 case GRALLOC_MODULE_PERFORM_GET_ATTRIBUTES: 392 { 393 int width = va_arg(args, int); 394 int height = va_arg(args, int); 395 int format = va_arg(args, int); 396 int usage = va_arg(args, int); 397 int *alignedWidth = va_arg(args, int *); 398 int *alignedHeight = va_arg(args, int *); 399 int *tileEnabled = va_arg(args,int *); 400 *tileEnabled = isUBwcEnabled(format, usage); 401 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, 402 height, format, usage, *alignedWidth, *alignedHeight); 403 res = 0; 404 } break; 405 406 case GRALLOC_MODULE_PERFORM_GET_COLOR_SPACE_FROM_HANDLE: 407 { 408 private_handle_t* hnd = va_arg(args, private_handle_t*); 409 int *color_space = va_arg(args, int *); 410 if (private_handle_t::validate(hnd)) { 411 return res; 412 } 413 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; 414 if (!metadata) { 415 break; 416#ifdef USE_COLOR_METADATA 417 } else if (metadata->operation & COLOR_METADATA) { 418 ColorMetaData *colorMetadata = &metadata->color; 419 res = 0; 420 switch (colorMetadata->colorPrimaries) { 421 case ColorPrimaries_BT709_5: 422 *color_space = HAL_CSC_ITU_R_709; 423 break; 424 case ColorPrimaries_BT601_6_525: 425 *color_space = ((colorMetadata->range) ? 426 HAL_CSC_ITU_R_601_FR : HAL_CSC_ITU_R_601); 427 break; 428 case ColorPrimaries_BT2020: 429 *color_space = (colorMetadata->range) ? 430 HAL_CSC_ITU_R_2020_FR : HAL_CSC_ITU_R_2020; 431 break; 432 default: 433 res = -EINVAL; 434 break; 435 } 436#endif 437 } else if(metadata->operation & UPDATE_COLOR_SPACE) { 438 *color_space = metadata->colorSpace; 439 res = 0; 440 } 441 } break; 442 443 case GRALLOC_MODULE_PERFORM_GET_YUV_PLANE_INFO: 444 { 445 private_handle_t* hnd = va_arg(args, private_handle_t*); 446 android_ycbcr* ycbcr = va_arg(args, struct android_ycbcr *); 447 if (!private_handle_t::validate(hnd)) { 448 res = getYUVPlaneInfo(hnd, ycbcr); 449 } 450 } break; 451 452 case GRALLOC_MODULE_PERFORM_GET_MAP_SECURE_BUFFER_INFO: 453 { 454 private_handle_t* hnd = va_arg(args, private_handle_t*); 455 int *map_secure_buffer = va_arg(args, int *); 456 if (private_handle_t::validate(hnd)) { 457 return res; 458 } 459 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; 460 if(metadata && metadata->operation & MAP_SECURE_BUFFER) { 461 *map_secure_buffer = metadata->mapSecureBuffer; 462 res = 0; 463 } else { 464 *map_secure_buffer = 0; 465 } 466 } break; 467 468 case GRALLOC_MODULE_PERFORM_GET_UBWC_FLAG: 469 { 470 private_handle_t* hnd = va_arg(args, private_handle_t*); 471 int *flag = va_arg(args, int *); 472 if (private_handle_t::validate(hnd)) { 473 return res; 474 } 475 *flag = hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; 476 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; 477 if (metadata && (metadata->operation & LINEAR_FORMAT)) { 478 *flag = 0; 479 } 480 res = 0; 481 } break; 482 483 case GRALLOC_MODULE_PERFORM_GET_RGB_DATA_ADDRESS: 484 { 485 private_handle_t* hnd = va_arg(args, private_handle_t*); 486 void** rgb_data = va_arg(args, void**); 487 if (!private_handle_t::validate(hnd)) { 488 res = getRgbDataAddress(hnd, rgb_data); 489 } 490 } break; 491 492 case GRALLOC_MODULE_PERFORM_GET_IGC: 493 { 494 private_handle_t* hnd = va_arg(args, private_handle_t*); 495 uint32_t *igc = va_arg(args, uint32_t *); 496 if (!private_handle_t::validate(hnd) && igc) { 497 MetaData_t *metadata = (MetaData_t *)hnd->base_metadata; 498 if (metadata && (metadata->operation & SET_IGC)) { 499 *igc = metadata->igc; 500 res = 0; 501 } 502 } 503 } break; 504 505 case GRALLOC_MODULE_PERFORM_SET_IGC: 506 res = 0; 507 break; 508 509 case GRALLOC_MODULE_PERFORM_SET_SINGLE_BUFFER_MODE: 510 { 511 private_handle_t* hnd = va_arg(args, private_handle_t*); 512 uint32_t *enable = va_arg(args, uint32_t*); 513 if (!private_handle_t::validate(hnd)) { 514 setMetaData(hnd, SET_SINGLE_BUFFER_MODE, enable); 515 res = 0; 516 } 517 } break; 518 default: 519 break; 520 } 521 va_end(args); 522 return res; 523} 524