1/* 2 * Copyright (C) 2010 The Android Open Source Project 3 * Copyright (c) 2011-2014 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#include <atomic> 19 20#include <limits.h> 21#include <unistd.h> 22#include <fcntl.h> 23#include <cutils/properties.h> 24#include <sys/mman.h> 25 26#include "gr.h" 27#include "gpu.h" 28#include "memalloc.h" 29#include "alloc_controller.h" 30#include <qdMetaData.h> 31 32using namespace gralloc; 33 34#define SZ_1M 0x100000 35 36static uint64_t next_backing_store_id() 37{ 38 static std::atomic<uint64_t> next_id(1); 39 return next_id++; 40} 41 42gpu_context_t::gpu_context_t(const private_module_t* module, 43 IAllocController* alloc_ctrl ) : 44 mAllocCtrl(alloc_ctrl) 45{ 46 // Zero out the alloc_device_t 47 memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t)); 48 49 // Initialize the procs 50 common.tag = HARDWARE_DEVICE_TAG; 51 common.version = 0; 52 common.module = const_cast<hw_module_t*>(&module->base.common); 53 common.close = gralloc_close; 54 alloc = gralloc_alloc; 55 free = gralloc_free; 56 57} 58 59int gpu_context_t::gralloc_alloc_buffer(unsigned int size, int usage, 60 buffer_handle_t* pHandle, int bufferType, 61 int format, int width, int height) 62{ 63 int err = 0; 64 int flags = 0; 65 size = roundUpToPageSize(size); 66 alloc_data data; 67 data.offset = 0; 68 data.fd = -1; 69 data.base = 0; 70 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) 71 data.align = 8192; 72 else 73 data.align = getpagesize(); 74 75 /* force 1MB alignment selectively for secure buffers, MDP5 onwards */ 76#ifdef MDSS_TARGET 77 if (usage & GRALLOC_USAGE_PROTECTED) { 78 data.align = ALIGN((int) data.align, SZ_1M); 79 size = ALIGN(size, data.align); 80 } 81#endif 82 83 data.size = size; 84 data.pHandle = (uintptr_t) pHandle; 85 err = mAllocCtrl->allocate(data, usage); 86 87 if (!err) { 88 /* allocate memory for enhancement data */ 89 alloc_data eData; 90 eData.fd = -1; 91 eData.base = 0; 92 eData.offset = 0; 93 eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); 94 eData.pHandle = data.pHandle; 95 eData.align = getpagesize(); 96 int eDataUsage = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP; 97 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage); 98 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s", 99 strerror(-eDataErr)); 100 101 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) { 102 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY; 103 } 104 105 ColorSpace_t colorSpace = ITU_R_601; 106 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601; 107 if (bufferType == BUFFER_TYPE_VIDEO) { 108 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { 109#ifndef MDSS_TARGET 110 colorSpace = ITU_R_601_FR; 111 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR; 112#else 113 // Per the camera spec ITU 709 format should be set only for 114 // video encoding. 115 // It should be set to ITU 601 full range format for any other 116 // camera buffer 117 // 118 if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) { 119 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { 120 flags |= private_handle_t::PRIV_FLAGS_ITU_R_709; 121 colorSpace = ITU_R_709; 122 } else { 123 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR; 124 colorSpace = ITU_R_601_FR; 125 } 126 } 127#endif 128 } 129 } 130 131 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) { 132 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER; 133 } 134 135 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { 136 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE; 137 } 138 139 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) { 140 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ; 141 } 142 143 if (usage & GRALLOC_USAGE_HW_COMPOSER) { 144 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER; 145 } 146 147 if (usage & GRALLOC_USAGE_HW_TEXTURE) { 148 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE; 149 } 150 151 if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) { 152 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY; 153 } 154 155 if(isMacroTileEnabled(format, usage)) { 156 flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED; 157 } 158 159 if (isUBwcEnabled(format, usage)) { 160 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; 161 } 162 163 if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { 164 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED; 165 } 166 167 if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER | 168 GRALLOC_USAGE_HW_CAMERA_WRITE | 169 GRALLOC_USAGE_HW_RENDER | 170 GRALLOC_USAGE_HW_FB)) { 171 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER; 172 } 173 174 if(false == data.uncached) { 175 flags |= private_handle_t::PRIV_FLAGS_CACHED; 176 } 177 178 flags |= data.allocType; 179 uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset; 180 private_handle_t *hnd = new private_handle_t(data.fd, size, flags, 181 bufferType, format, width, height, eData.fd, eData.offset, 182 eBaseAddr); 183 184 hnd->offset = data.offset; 185 hnd->base = (uint64_t)(data.base) + data.offset; 186 hnd->gpuaddr = 0; 187 setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace); 188 189 *pHandle = hnd; 190 } 191 192 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err)); 193 194 return err; 195} 196 197void gpu_context_t::getGrallocInformationFromFormat(int inputFormat, 198 int *bufferType) 199{ 200 *bufferType = BUFFER_TYPE_VIDEO; 201 202 if (isUncompressedRgbFormat(inputFormat) == TRUE) { 203 // RGB formats 204 *bufferType = BUFFER_TYPE_UI; 205 } 206} 207 208int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage, 209 buffer_handle_t* pHandle) 210{ 211 private_module_t* m = reinterpret_cast<private_module_t*>(common.module); 212 213 // we don't support framebuffer allocations with graphics heap flags 214 if (usage & GRALLOC_HEAP_MASK) { 215 return -EINVAL; 216 } 217 218 if (m->framebuffer == NULL) { 219 ALOGE("%s: Invalid framebuffer", __FUNCTION__); 220 return -EINVAL; 221 } 222 223 const unsigned int bufferMask = m->bufferMask; 224 const uint32_t numBuffers = m->numBuffers; 225 unsigned int bufferSize = m->finfo.line_length * m->info.yres; 226 227 //adreno needs FB size to be page aligned 228 bufferSize = roundUpToPageSize(bufferSize); 229 230 if (numBuffers == 1) { 231 // If we have only one buffer, we never use page-flipping. Instead, 232 // we return a regular buffer which will be memcpy'ed to the main 233 // screen when post is called. 234 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D; 235 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI, 236 m->fbFormat, m->info.xres, m->info.yres); 237 } 238 239 if (bufferMask >= ((1LU<<numBuffers)-1)) { 240 // We ran out of buffers. 241 return -ENOMEM; 242 } 243 244 // create a "fake" handle for it 245 uint64_t vaddr = uint64_t(m->framebuffer->base); 246 // As GPU needs ION FD, the private handle is created 247 // using ION fd and ION flags are set 248 private_handle_t* hnd = new private_handle_t( 249 dup(m->framebuffer->fd), bufferSize, 250 private_handle_t::PRIV_FLAGS_USES_ION | 251 private_handle_t::PRIV_FLAGS_FRAMEBUFFER, 252 BUFFER_TYPE_UI, m->fbFormat, m->info.xres, 253 m->info.yres); 254 255 // find a free slot 256 for (uint32_t i=0 ; i<numBuffers ; i++) { 257 if ((bufferMask & (1LU<<i)) == 0) { 258 m->bufferMask |= (uint32_t)(1LU<<i); 259 break; 260 } 261 vaddr += bufferSize; 262 } 263 hnd->base = vaddr; 264 hnd->offset = (unsigned int)(vaddr - m->framebuffer->base); 265 *pHandle = hnd; 266 return 0; 267} 268 269 270int gpu_context_t::gralloc_alloc_framebuffer(int usage, 271 buffer_handle_t* pHandle) 272{ 273 private_module_t* m = reinterpret_cast<private_module_t*>(common.module); 274 pthread_mutex_lock(&m->lock); 275 int err = gralloc_alloc_framebuffer_locked(usage, pHandle); 276 pthread_mutex_unlock(&m->lock); 277 return err; 278} 279 280int gpu_context_t::alloc_impl(int w, int h, int format, int usage, 281 buffer_handle_t* pHandle, int* pStride, 282 unsigned int bufferSize) { 283 if (!pHandle || !pStride) 284 return -EINVAL; 285 286 unsigned int size; 287 int alignedw, alignedh; 288 int grallocFormat = format; 289 int bufferType; 290 291 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on 292 //the usage bits, gralloc assigns a format. 293 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED || 294 format == HAL_PIXEL_FORMAT_YCbCr_420_888) { 295 if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) 296 grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12 297 else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK) 298 == GRALLOC_USAGE_HW_CAMERA_ZSL) 299 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL 300 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ) 301 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21 302 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { 303 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) { 304 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 305 } else { 306 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; //NV12 preview 307 } 308 } else if(usage & GRALLOC_USAGE_HW_COMPOSER) 309 //XXX: If we still haven't set a format, default to RGBA8888 310 grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888; 311 else if(format == HAL_PIXEL_FORMAT_YCbCr_420_888) 312 //If no other usage flags are detected, default the 313 //flexible YUV format to NV21_ZSL 314 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; 315 } 316 317 getGrallocInformationFromFormat(grallocFormat, &bufferType); 318 size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw, 319 alignedh); 320 321 if ((unsigned int)size <= 0) 322 return -EINVAL; 323 size = (bufferSize >= size)? bufferSize : size; 324 325 bool useFbMem = false; 326 char property[PROPERTY_VALUE_MAX]; 327 if((usage & GRALLOC_USAGE_HW_FB) && 328 (property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) && 329 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) || 330 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) { 331 useFbMem = true; 332 } 333 334 int err = 0; 335 if(useFbMem) { 336 err = gralloc_alloc_framebuffer(usage, pHandle); 337 } else { 338 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType, 339 grallocFormat, alignedw, alignedh); 340 } 341 342 if (err < 0) { 343 return err; 344 } 345 346 auto hnd = (private_handle_t*) *pHandle; 347 hnd->backing_store = next_backing_store_id(); 348 hnd->original_width = w; 349 hnd->original_format = format; 350 351 *pStride = alignedw; 352 return 0; 353} 354 355int gpu_context_t::free_impl(private_handle_t const* hnd) { 356 private_module_t* m = reinterpret_cast<private_module_t*>(common.module); 357 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { 358 const unsigned int bufferSize = m->finfo.line_length * m->info.yres; 359 unsigned int index = (unsigned int) ((hnd->base - m->framebuffer->base) 360 / bufferSize); 361 m->bufferMask &= (uint32_t)~(1LU<<index); 362 } else { 363 364 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd)); 365 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags); 366 int err = memalloc->free_buffer((void*)hnd->base, hnd->size, 367 hnd->offset, hnd->fd); 368 if(err) 369 return err; 370 // free the metadata space 371 unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); 372 err = memalloc->free_buffer((void*)hnd->base_metadata, 373 size, hnd->offset_metadata, 374 hnd->fd_metadata); 375 if (err) 376 return err; 377 } 378 delete hnd; 379 return 0; 380} 381 382int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format, 383 int usage, buffer_handle_t* pHandle, 384 int* pStride) 385{ 386 if (!dev) { 387 return -EINVAL; 388 } 389 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); 390 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0); 391} 392int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h, 393 int format, int usage, 394 buffer_handle_t* pHandle, int* pStride, 395 int bufferSize) 396{ 397 if (!dev) { 398 return -EINVAL; 399 } 400 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); 401 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize); 402} 403 404 405int gpu_context_t::gralloc_free(alloc_device_t* dev, 406 buffer_handle_t handle) 407{ 408 if (private_handle_t::validate(handle) < 0) 409 return -EINVAL; 410 411 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle); 412 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); 413 return gpu->free_impl(hnd); 414} 415 416/*****************************************************************************/ 417 418int gpu_context_t::gralloc_close(struct hw_device_t *dev) 419{ 420 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev); 421 if (ctx) { 422 /* TODO: keep a list of all buffer_handle_t created, and free them 423 * all here. 424 */ 425 delete ctx; 426 } 427 return 0; 428} 429 430