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 | |
| 39 | using namespace gralloc; |
Naseer Ahmed | a87da60 | 2012-07-01 23:54:19 -0700 | [diff] [blame] | 40 | using namespace qdutils; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 41 | |
| 42 | //Common functions |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 43 | static bool canFallback(int usage, bool triedSystem) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 44 | { |
| 45 | // Fallback to system heap when alloc fails unless |
| 46 | // 1. Composition type is MDP |
| 47 | // 2. Alloc from system heap was already tried |
| 48 | // 3. The heap type is requsted explicitly |
| 49 | // 4. The heap type is protected |
| 50 | // 5. The buffer is meant for external display only |
| 51 | |
Naseer Ahmed | a87da60 | 2012-07-01 23:54:19 -0700 | [diff] [blame] | 52 | if(QCCompositionType::getInstance().getCompositionType() & |
| 53 | COMPOSITION_TYPE_MDP) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 54 | return false; |
| 55 | if(triedSystem) |
| 56 | return false; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 57 | if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED | |
| 58 | GRALLOC_USAGE_PRIVATE_CP_BUFFER)) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 59 | return false; |
| 60 | if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_EXTERNAL_ONLY)) |
| 61 | return false; |
| 62 | //Return true by default |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | static bool useUncached(int usage) |
| 67 | { |
| 68 | // System heaps cannot be uncached |
| 69 | if(usage & (GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP | |
| 70 | GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)) |
| 71 | return false; |
| 72 | if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED) |
| 73 | return true; |
| 74 | return false; |
| 75 | } |
| 76 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 77 | IAllocController* IAllocController::sController = NULL; |
| 78 | IAllocController* IAllocController::getInstance(void) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 79 | { |
| 80 | if(sController == NULL) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 81 | sController = new IonController(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 82 | } |
| 83 | return sController; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | //-------------- IonController-----------------------// |
| 88 | IonController::IonController() |
| 89 | { |
| 90 | mIonAlloc = new IonAlloc(); |
| 91 | } |
| 92 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 93 | int IonController::allocate(alloc_data& data, int usage) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 94 | { |
| 95 | int ionFlags = 0; |
| 96 | int ret; |
| 97 | bool noncontig = false; |
| 98 | |
| 99 | data.uncached = useUncached(usage); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 100 | data.allocType = 0; |
| 101 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 102 | if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP) |
| 103 | ionFlags |= ION_HEAP(ION_SF_HEAP_ID); |
| 104 | |
| 105 | if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP) { |
| 106 | ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID); |
| 107 | noncontig = true; |
| 108 | } |
| 109 | |
| 110 | if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP) |
| 111 | ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID); |
| 112 | |
| 113 | if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) |
| 114 | ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID); |
| 115 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 116 | if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP) |
| 117 | ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID); |
| 118 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 119 | if(usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 120 | ionFlags |= ION_SECURE; |
| 121 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 122 | // if no flags are set, default to |
| 123 | // SF + IOMMU heaps, so that bypass can work |
| 124 | // we can fall back to system heap if |
| 125 | // we run out. |
| 126 | if(!ionFlags) |
| 127 | ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID); |
| 128 | |
| 129 | data.flags = ionFlags; |
| 130 | ret = mIonAlloc->alloc_buffer(data); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 131 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 132 | // Fallback |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 133 | if(ret < 0 && canFallback(usage, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 134 | (ionFlags & ION_SYSTEM_HEAP_ID))) |
| 135 | { |
| 136 | ALOGW("Falling back to system heap"); |
| 137 | data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID); |
| 138 | noncontig = true; |
| 139 | ret = mIonAlloc->alloc_buffer(data); |
| 140 | } |
| 141 | |
| 142 | if(ret >= 0 ) { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 143 | data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 144 | if(noncontig) |
| 145 | data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM; |
| 146 | if(ionFlags & ION_SECURE) |
| 147 | data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER; |
| 148 | } |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 153 | IMemAlloc* IonController::getAllocator(int flags) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 154 | { |
Naseer Ahmed | b16edac | 2012-07-15 23:56:21 -0700 | [diff] [blame^] | 155 | IMemAlloc* memalloc = NULL; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 156 | if (flags & private_handle_t::PRIV_FLAGS_USES_ION) { |
| 157 | memalloc = mIonAlloc; |
| 158 | } else { |
| 159 | ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags); |
| 160 | } |
| 161 | |
| 162 | return memalloc; |
| 163 | } |
| 164 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 165 | size_t getBufferSizeAndDimensions(int width, int height, int format, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 166 | int& alignedw, int &alignedh) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 167 | { |
| 168 | size_t size; |
| 169 | |
| 170 | alignedw = ALIGN(width, 32); |
| 171 | alignedh = ALIGN(height, 32); |
| 172 | switch (format) { |
| 173 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 174 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 175 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 176 | size = alignedw * alignedh * 4; |
| 177 | break; |
| 178 | case HAL_PIXEL_FORMAT_RGB_888: |
| 179 | size = alignedw * alignedh * 3; |
| 180 | break; |
| 181 | case HAL_PIXEL_FORMAT_RGB_565: |
| 182 | case HAL_PIXEL_FORMAT_RGBA_5551: |
| 183 | case HAL_PIXEL_FORMAT_RGBA_4444: |
| 184 | size = alignedw * alignedh * 2; |
| 185 | break; |
| 186 | |
| 187 | // adreno formats |
| 188 | case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21 |
| 189 | size = ALIGN(alignedw*alignedh, 4096); |
| 190 | size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096); |
| 191 | break; |
| 192 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12 |
| 193 | // The chroma plane is subsampled, |
| 194 | // but the pitch in bytes is unchanged |
| 195 | // The GPU needs 4K alignment, but the video decoder needs 8K |
| 196 | alignedw = ALIGN(width, 128); |
| 197 | size = ALIGN( alignedw * alignedh, 8192); |
| 198 | size += ALIGN( alignedw * ALIGN(height/2, 32), 8192); |
| 199 | break; |
| 200 | case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: |
| 201 | case HAL_PIXEL_FORMAT_YCbCr_420_SP: |
| 202 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 203 | case HAL_PIXEL_FORMAT_YV12: |
| 204 | if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) { |
| 205 | ALOGE("w or h is odd for the YV12 format"); |
| 206 | return -EINVAL; |
| 207 | } |
| 208 | alignedw = ALIGN(width, 16); |
| 209 | alignedh = height; |
| 210 | if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) { |
| 211 | // The encoder requires a 2K aligned chroma offset. |
| 212 | size = ALIGN(alignedw*alignedh, 2048) + |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 213 | (ALIGN(alignedw/2, 16) * (alignedh/2))*2; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 214 | } else { |
| 215 | size = alignedw*alignedh + |
| 216 | (ALIGN(alignedw/2, 16) * (alignedh/2))*2; |
| 217 | } |
| 218 | size = ALIGN(size, 4096); |
| 219 | break; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 220 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 221 | case HAL_PIXEL_FORMAT_YCrCb_422_SP: |
| 222 | if(width & 1) { |
| 223 | ALOGE("width is odd for the YUV422_SP format"); |
| 224 | return -EINVAL; |
| 225 | } |
| 226 | alignedw = ALIGN(width, 16); |
| 227 | alignedh = height; |
| 228 | size = ALIGN(alignedw * alignedh * 2, 4096); |
| 229 | break; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 230 | default: |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 231 | ALOGE("unrecognized pixel format: 0x%x", format); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 232 | return -EINVAL; |
| 233 | } |
| 234 | |
| 235 | return size; |
| 236 | } |
| 237 | |
| 238 | // Allocate buffer from width, height and format into a |
| 239 | // private_handle_t. It is the responsibility of the caller |
| 240 | // to free the buffer using the free_buffer function |
| 241 | int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage) |
| 242 | { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 243 | alloc_data data; |
| 244 | int alignedw, alignedh; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 245 | gralloc::IAllocController* sAlloc = |
| 246 | gralloc::IAllocController::getInstance(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 247 | data.base = 0; |
| 248 | data.fd = -1; |
| 249 | data.offset = 0; |
| 250 | data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh); |
| 251 | data.align = getpagesize(); |
| 252 | data.uncached = useUncached(usage); |
| 253 | int allocFlags = usage; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 254 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 255 | int err = sAlloc->allocate(data, allocFlags); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 256 | if (0 != err) { |
| 257 | ALOGE("%s: allocate failed", __FUNCTION__); |
| 258 | return -ENOMEM; |
| 259 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 260 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 261 | private_handle_t* hnd = new private_handle_t(data.fd, data.size, |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 262 | data.allocType, 0, format, |
| 263 | alignedw, alignedh); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 264 | hnd->base = (int) data.base; |
| 265 | hnd->offset = data.offset; |
| 266 | hnd->gpuaddr = 0; |
| 267 | *pHnd = hnd; |
| 268 | return 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void free_buffer(private_handle_t *hnd) |
| 272 | { |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 273 | gralloc::IAllocController* sAlloc = |
| 274 | gralloc::IAllocController::getInstance(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 275 | if (hnd && hnd->fd > 0) { |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 276 | IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 277 | memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd); |
| 278 | } |
| 279 | if(hnd) |
| 280 | delete hnd; |
| 281 | |
| 282 | } |