Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 31 | #include <fcntl.h> |
| 32 | #include "gralloc_priv.h" |
| 33 | #include "alloc_controller.h" |
| 34 | #include "memalloc.h" |
| 35 | #include "ionalloc.h" |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 36 | #include "gr.h" |
Naseer Ahmed | a87da60 | 2012-07-01 23:54:19 -0700 | [diff] [blame] | 37 | #include "comptype.h" |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 38 | |
Sushil Chauhan | c6bd6d9 | 2012-12-12 12:33:01 -0800 | [diff] [blame] | 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 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 47 | using namespace gralloc; |
Naseer Ahmed | a87da60 | 2012-07-01 23:54:19 -0700 | [diff] [blame] | 48 | using namespace qdutils; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 49 | |
| 50 | //Common functions |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 51 | static bool canFallback(int usage, bool triedSystem) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 | |
Naseer Ahmed | a87da60 | 2012-07-01 23:54:19 -0700 | [diff] [blame] | 60 | if(QCCompositionType::getInstance().getCompositionType() & |
| 61 | COMPOSITION_TYPE_MDP) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 62 | return false; |
| 63 | if(triedSystem) |
| 64 | return false; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 65 | if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED | |
| 66 | GRALLOC_USAGE_PRIVATE_CP_BUFFER)) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 67 | return false; |
Naseer Ahmed | 4c588a2 | 2012-07-31 19:12:17 -0700 | [diff] [blame] | 68 | if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY)) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 69 | return false; |
| 70 | //Return true by default |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | static 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 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 85 | IAllocController* IAllocController::sController = NULL; |
| 86 | IAllocController* IAllocController::getInstance(void) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 87 | { |
| 88 | if(sController == NULL) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 89 | sController = new IonController(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 90 | } |
| 91 | return sController; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | //-------------- IonController-----------------------// |
| 96 | IonController::IonController() |
| 97 | { |
| 98 | mIonAlloc = new IonAlloc(); |
| 99 | } |
| 100 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 101 | int IonController::allocate(alloc_data& data, int usage) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 102 | { |
| 103 | int ionFlags = 0; |
| 104 | int ret; |
| 105 | bool noncontig = false; |
| 106 | |
| 107 | data.uncached = useUncached(usage); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 108 | data.allocType = 0; |
| 109 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 124 | if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP) |
| 125 | ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID); |
| 126 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 127 | if(usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 128 | ionFlags |= ION_SECURE; |
| 129 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 139 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 140 | // Fallback |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 141 | if(ret < 0 && canFallback(usage, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 ) { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 151 | data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 161 | IMemAlloc* IonController::getAllocator(int flags) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 162 | { |
Naseer Ahmed | b16edac | 2012-07-15 23:56:21 -0700 | [diff] [blame] | 163 | IMemAlloc* memalloc = NULL; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 173 | size_t getBufferSizeAndDimensions(int width, int height, int format, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 174 | int& alignedw, int &alignedh) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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) + |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 221 | (ALIGN(alignedw/2, 16) * (alignedh/2))*2; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 222 | } else { |
| 223 | size = alignedw*alignedh + |
| 224 | (ALIGN(alignedw/2, 16) * (alignedh/2))*2; |
| 225 | } |
| 226 | size = ALIGN(size, 4096); |
| 227 | break; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 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; |
Sushil Chauhan | c5e6148 | 2012-08-22 17:13:32 -0700 | [diff] [blame] | 238 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: |
Sushil Chauhan | e8a0179 | 2012-11-01 16:25:45 -0700 | [diff] [blame^] | 239 | size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height); |
Sushil Chauhan | c5e6148 | 2012-08-22 17:13:32 -0700 | [diff] [blame] | 240 | break; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 241 | default: |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 242 | ALOGE("unrecognized pixel format: 0x%x", format); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 243 | return -EINVAL; |
| 244 | } |
| 245 | |
| 246 | return size; |
| 247 | } |
| 248 | |
| 249 | // Allocate buffer from width, height and format into a |
| 250 | // private_handle_t. It is the responsibility of the caller |
| 251 | // to free the buffer using the free_buffer function |
| 252 | int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage) |
| 253 | { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 254 | alloc_data data; |
| 255 | int alignedw, alignedh; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 256 | gralloc::IAllocController* sAlloc = |
| 257 | gralloc::IAllocController::getInstance(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 258 | data.base = 0; |
| 259 | data.fd = -1; |
| 260 | data.offset = 0; |
| 261 | data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh); |
| 262 | data.align = getpagesize(); |
| 263 | data.uncached = useUncached(usage); |
| 264 | int allocFlags = usage; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 265 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 266 | int err = sAlloc->allocate(data, allocFlags); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 267 | if (0 != err) { |
| 268 | ALOGE("%s: allocate failed", __FUNCTION__); |
| 269 | return -ENOMEM; |
| 270 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 271 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 272 | private_handle_t* hnd = new private_handle_t(data.fd, data.size, |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 273 | data.allocType, 0, format, |
| 274 | alignedw, alignedh); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 275 | hnd->base = (int) data.base; |
| 276 | hnd->offset = data.offset; |
| 277 | hnd->gpuaddr = 0; |
| 278 | *pHnd = hnd; |
| 279 | return 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void free_buffer(private_handle_t *hnd) |
| 283 | { |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 284 | gralloc::IAllocController* sAlloc = |
| 285 | gralloc::IAllocController::getInstance(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 286 | if (hnd && hnd->fd > 0) { |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 287 | IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 288 | memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd); |
| 289 | } |
| 290 | if(hnd) |
| 291 | delete hnd; |
| 292 | |
| 293 | } |