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