alloc_controller.cpp revision 2d9ebfd924dfc0e86b7b6fda9ec7a538a1e91955
1/* 2 * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. 3 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * * Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * * Redistributions in binary form must reproduce the above 10 * copyright notice, this list of conditions and the following 11 * disclaimer in the documentation and/or other materials provided 12 * with the distribution. 13 * * Neither the name of Code Aurora Forum, Inc. nor the names of its 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include <cutils/log.h> 31#include <fcntl.h> 32#include "gralloc_priv.h" 33#include "alloc_controller.h" 34#include "memalloc.h" 35#include "ionalloc.h" 36#include "gr.h" 37#include "comptype.h" 38 39#ifdef VENUS_COLOR_FORMAT 40#include <media/msm_media_info.h> 41#else 42#define VENUS_Y_STRIDE(args...) 0 43#define VENUS_Y_SCANLINES(args...) 0 44#define VENUS_BUFFER_SIZE(args...) 0 45#endif 46 47using namespace gralloc; 48using namespace qdutils; 49 50//Common functions 51static bool canFallback(int usage, bool triedSystem) 52{ 53 // Fallback to system heap when alloc fails unless 54 // 1. Composition type is MDP 55 // 2. Alloc from system heap was already tried 56 // 3. The heap type is requsted explicitly 57 // 4. The heap type is protected 58 // 5. The buffer is meant for external display only 59 60 if(QCCompositionType::getInstance().getCompositionType() & 61 COMPOSITION_TYPE_MDP) 62 return false; 63 if(triedSystem) 64 return false; 65 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED | 66 GRALLOC_USAGE_PRIVATE_CP_BUFFER)) 67 return false; 68 if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY)) 69 return false; 70 //Return true by default 71 return true; 72} 73 74static bool useUncached(int usage) 75{ 76 // System heaps cannot be uncached 77 if(usage & (GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP | 78 GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)) 79 return false; 80 if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED) 81 return true; 82 return false; 83} 84 85IAllocController* IAllocController::sController = NULL; 86IAllocController* IAllocController::getInstance(void) 87{ 88 if(sController == NULL) { 89 sController = new IonController(); 90 } 91 return sController; 92} 93 94 95//-------------- IonController-----------------------// 96IonController::IonController() 97{ 98 mIonAlloc = new IonAlloc(); 99} 100 101int IonController::allocate(alloc_data& data, int usage) 102{ 103 int ionFlags = 0; 104 int ret; 105 bool noncontig = false; 106 107 data.uncached = useUncached(usage); 108 data.allocType = 0; 109 110 if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP) 111 ionFlags |= ION_HEAP(ION_SF_HEAP_ID); 112 113 if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP) { 114 ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID); 115 noncontig = true; 116 } 117 118 if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP) 119 ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID); 120 121 if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) 122 ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID); 123 124 if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP) 125 ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID); 126 127 if(usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER) 128 ionFlags |= ION_SECURE; 129 130 // if no flags are set, default to 131 // SF + IOMMU heaps, so that bypass can work 132 // we can fall back to system heap if 133 // we run out. 134 if(!ionFlags) 135 ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID); 136 137 data.flags = ionFlags; 138 ret = mIonAlloc->alloc_buffer(data); 139 140 // Fallback 141 if(ret < 0 && canFallback(usage, 142 (ionFlags & ION_SYSTEM_HEAP_ID))) 143 { 144 ALOGW("Falling back to system heap"); 145 data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID); 146 noncontig = true; 147 ret = mIonAlloc->alloc_buffer(data); 148 } 149 150 if(ret >= 0 ) { 151 data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION; 152 if(noncontig) 153 data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM; 154 if(ionFlags & ION_SECURE) 155 data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER; 156 } 157 158 return ret; 159} 160 161IMemAlloc* IonController::getAllocator(int flags) 162{ 163 IMemAlloc* memalloc = NULL; 164 if (flags & private_handle_t::PRIV_FLAGS_USES_ION) { 165 memalloc = mIonAlloc; 166 } else { 167 ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags); 168 } 169 170 return memalloc; 171} 172 173size_t getBufferSizeAndDimensions(int width, int height, int format, 174 int& alignedw, int &alignedh) 175{ 176 size_t size; 177 178 alignedw = ALIGN(width, 32); 179 alignedh = ALIGN(height, 32); 180 switch (format) { 181 case HAL_PIXEL_FORMAT_RGBA_8888: 182 case HAL_PIXEL_FORMAT_RGBX_8888: 183 case HAL_PIXEL_FORMAT_BGRA_8888: 184 size = alignedw * alignedh * 4; 185 break; 186 case HAL_PIXEL_FORMAT_RGB_888: 187 size = alignedw * alignedh * 3; 188 break; 189 case HAL_PIXEL_FORMAT_RGB_565: 190 case HAL_PIXEL_FORMAT_RGBA_5551: 191 case HAL_PIXEL_FORMAT_RGBA_4444: 192 size = alignedw * alignedh * 2; 193 break; 194 195 // adreno formats 196 case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21 197 size = ALIGN(alignedw*alignedh, 4096); 198 size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096); 199 break; 200 case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12 201 // The chroma plane is subsampled, 202 // but the pitch in bytes is unchanged 203 // The GPU needs 4K alignment, but the video decoder needs 8K 204 alignedw = ALIGN(width, 128); 205 size = ALIGN( alignedw * alignedh, 8192); 206 size += ALIGN( alignedw * ALIGN(height/2, 32), 8192); 207 break; 208 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: 209 case HAL_PIXEL_FORMAT_YCbCr_420_SP: 210 case HAL_PIXEL_FORMAT_YCrCb_420_SP: 211 case HAL_PIXEL_FORMAT_YV12: 212 if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) { 213 ALOGE("w or h is odd for the YV12 format"); 214 return -EINVAL; 215 } 216 alignedw = ALIGN(width, 16); 217 alignedh = height; 218 if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) { 219 // The encoder requires a 2K aligned chroma offset. 220 size = ALIGN(alignedw*alignedh, 2048) + 221 (ALIGN(alignedw/2, 16) * (alignedh/2))*2; 222 } else { 223 size = alignedw*alignedh + 224 (ALIGN(alignedw/2, 16) * (alignedh/2))*2; 225 } 226 size = ALIGN(size, 4096); 227 break; 228 case HAL_PIXEL_FORMAT_YCbCr_422_SP: 229 case HAL_PIXEL_FORMAT_YCrCb_422_SP: 230 if(width & 1) { 231 ALOGE("width is odd for the YUV422_SP format"); 232 return -EINVAL; 233 } 234 alignedw = ALIGN(width, 16); 235 alignedh = height; 236 size = ALIGN(alignedw * alignedh * 2, 4096); 237 break; 238 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: 239 size = alignedw*alignedh + 240 (ALIGN(alignedw/2, 32) * (alignedh/2))*2; 241 size = ALIGN(size, 4096); 242 break; 243 default: 244 ALOGE("unrecognized pixel format: 0x%x", format); 245 return -EINVAL; 246 } 247 248 return size; 249} 250 251// Allocate buffer from width, height and format into a 252// private_handle_t. It is the responsibility of the caller 253// to free the buffer using the free_buffer function 254int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage) 255{ 256 alloc_data data; 257 int alignedw, alignedh; 258 gralloc::IAllocController* sAlloc = 259 gralloc::IAllocController::getInstance(); 260 data.base = 0; 261 data.fd = -1; 262 data.offset = 0; 263 data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh); 264 data.align = getpagesize(); 265 data.uncached = useUncached(usage); 266 int allocFlags = usage; 267 268 int err = sAlloc->allocate(data, allocFlags); 269 if (0 != err) { 270 ALOGE("%s: allocate failed", __FUNCTION__); 271 return -ENOMEM; 272 } 273 274 private_handle_t* hnd = new private_handle_t(data.fd, data.size, 275 data.allocType, 0, format, 276 alignedw, alignedh); 277 hnd->base = (int) data.base; 278 hnd->offset = data.offset; 279 hnd->gpuaddr = 0; 280 *pHnd = hnd; 281 return 0; 282} 283 284void free_buffer(private_handle_t *hnd) 285{ 286 gralloc::IAllocController* sAlloc = 287 gralloc::IAllocController::getInstance(); 288 if (hnd && hnd->fd > 0) { 289 IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags); 290 memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd); 291 } 292 if(hnd) 293 delete hnd; 294 295} 296