Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
Duy Truong | 73d36df | 2013-02-09 20:33:23 -0800 | [diff] [blame] | 2 | * Copyright (c) 2011-2012, The Linux Foundation. All rights reserved. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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. |
Duy Truong | 73d36df | 2013-02-09 20:33:23 -0800 | [diff] [blame] | 13 | * * Neither the name of The Linux Foundation nor the names of its |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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> |
Naomi Luis | 01f5c8e | 2013-02-11 12:46:24 -0800 | [diff] [blame^] | 32 | #include <dlfcn.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 33 | #include "gralloc_priv.h" |
| 34 | #include "alloc_controller.h" |
| 35 | #include "memalloc.h" |
| 36 | #include "ionalloc.h" |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 37 | #include "gr.h" |
Naseer Ahmed | a87da60 | 2012-07-01 23:54:19 -0700 | [diff] [blame] | 38 | #include "comptype.h" |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 39 | |
Sushil Chauhan | c6bd6d9 | 2012-12-12 12:33:01 -0800 | [diff] [blame] | 40 | #ifdef VENUS_COLOR_FORMAT |
| 41 | #include <media/msm_media_info.h> |
| 42 | #else |
| 43 | #define VENUS_Y_STRIDE(args...) 0 |
| 44 | #define VENUS_Y_SCANLINES(args...) 0 |
| 45 | #define VENUS_BUFFER_SIZE(args...) 0 |
| 46 | #endif |
| 47 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 48 | using namespace gralloc; |
Naseer Ahmed | a87da60 | 2012-07-01 23:54:19 -0700 | [diff] [blame] | 49 | using namespace qdutils; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 50 | |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 51 | ANDROID_SINGLETON_STATIC_INSTANCE(AdrenoMemInfo); |
| 52 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 53 | //Common functions |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 54 | static bool canFallback(int usage, bool triedSystem) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 55 | { |
| 56 | // Fallback to system heap when alloc fails unless |
| 57 | // 1. Composition type is MDP |
| 58 | // 2. Alloc from system heap was already tried |
| 59 | // 3. The heap type is requsted explicitly |
| 60 | // 4. The heap type is protected |
| 61 | // 5. The buffer is meant for external display only |
| 62 | |
Naseer Ahmed | a87da60 | 2012-07-01 23:54:19 -0700 | [diff] [blame] | 63 | if(QCCompositionType::getInstance().getCompositionType() & |
| 64 | COMPOSITION_TYPE_MDP) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 65 | return false; |
| 66 | if(triedSystem) |
| 67 | return false; |
Sushil Chauhan | 7651a80 | 2013-01-08 16:08:09 -0800 | [diff] [blame] | 68 | if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PROTECTED)) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 69 | return false; |
Naseer Ahmed | 4c588a2 | 2012-07-31 19:12:17 -0700 | [diff] [blame] | 70 | if(usage & (GRALLOC_HEAP_MASK | GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY)) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 71 | return false; |
| 72 | //Return true by default |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | static bool useUncached(int usage) |
| 77 | { |
| 78 | // System heaps cannot be uncached |
| 79 | if(usage & (GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP | |
| 80 | GRALLOC_USAGE_PRIVATE_IOMMU_HEAP)) |
| 81 | return false; |
| 82 | if (usage & GRALLOC_USAGE_PRIVATE_UNCACHED) |
| 83 | return true; |
| 84 | return false; |
| 85 | } |
| 86 | |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 87 | //-------------- AdrenoMemInfo-----------------------// |
Naomi Luis | 01f5c8e | 2013-02-11 12:46:24 -0800 | [diff] [blame^] | 88 | AdrenoMemInfo::AdrenoMemInfo() |
| 89 | { |
| 90 | libadreno_utils = ::dlopen("libadreno_utils.so", RTLD_NOW); |
| 91 | if (libadreno_utils) { |
| 92 | *(void **)&LINK_adreno_compute_padding = ::dlsym(libadreno_utils, |
| 93 | "compute_surface_padding"); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | AdrenoMemInfo::~AdrenoMemInfo() |
| 98 | { |
| 99 | if (libadreno_utils) { |
| 100 | ::dlclose(libadreno_utils); |
| 101 | } |
| 102 | } |
| 103 | |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 104 | int AdrenoMemInfo::getStride(int width, int format) |
| 105 | { |
| 106 | int stride = ALIGN(width, 32); |
Naomi Luis | 01f5c8e | 2013-02-11 12:46:24 -0800 | [diff] [blame^] | 107 | // Currently surface padding is only computed for RGB* surfaces. |
| 108 | if (format < 0x7) { |
| 109 | int bpp = 4; |
| 110 | switch(format) |
| 111 | { |
| 112 | case HAL_PIXEL_FORMAT_RGB_888: |
| 113 | bpp = 3; |
| 114 | break; |
| 115 | case HAL_PIXEL_FORMAT_RGB_565: |
| 116 | case HAL_PIXEL_FORMAT_RGBA_5551: |
| 117 | case HAL_PIXEL_FORMAT_RGBA_4444: |
| 118 | bpp = 2; |
| 119 | break; |
| 120 | default: break; |
| 121 | } |
| 122 | if ((libadreno_utils) && (LINK_adreno_compute_padding)) { |
| 123 | int surface_tile_height = 1; // Linear surface |
| 124 | int raster_mode = 1; // Adreno TW raster mode. |
| 125 | int padding_threshold = 512; // Threshold for padding surfaces. |
| 126 | // the function below expects the width to be a multiple of |
| 127 | // 32 pixels, hence we pass stride instead of width. |
| 128 | stride = LINK_adreno_compute_padding(stride, bpp, |
| 129 | surface_tile_height, raster_mode, |
| 130 | padding_threshold); |
| 131 | } |
| 132 | } else { |
| 133 | switch (format) |
| 134 | { |
| 135 | case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: |
| 136 | stride = ALIGN(width, 32); |
| 137 | break; |
| 138 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: |
| 139 | stride = ALIGN(width, 128); |
| 140 | break; |
| 141 | case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: |
| 142 | case HAL_PIXEL_FORMAT_YCbCr_420_SP: |
| 143 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 144 | case HAL_PIXEL_FORMAT_YV12: |
| 145 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 146 | case HAL_PIXEL_FORMAT_YCrCb_422_SP: |
| 147 | stride = ALIGN(width, 16); |
| 148 | break; |
| 149 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: |
| 150 | stride = VENUS_Y_STRIDE(COLOR_FMT_NV12, width); |
| 151 | break; |
| 152 | default: break; |
| 153 | } |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 154 | } |
| 155 | return stride; |
| 156 | } |
| 157 | |
| 158 | //-------------- IAllocController-----------------------// |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 159 | IAllocController* IAllocController::sController = NULL; |
| 160 | IAllocController* IAllocController::getInstance(void) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 161 | { |
| 162 | if(sController == NULL) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 163 | sController = new IonController(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 164 | } |
| 165 | return sController; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | //-------------- IonController-----------------------// |
| 170 | IonController::IonController() |
| 171 | { |
| 172 | mIonAlloc = new IonAlloc(); |
| 173 | } |
| 174 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 175 | int IonController::allocate(alloc_data& data, int usage) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 176 | { |
| 177 | int ionFlags = 0; |
| 178 | int ret; |
| 179 | bool noncontig = false; |
| 180 | |
| 181 | data.uncached = useUncached(usage); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 182 | data.allocType = 0; |
| 183 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 184 | if(usage & GRALLOC_USAGE_PRIVATE_UI_CONTIG_HEAP) |
| 185 | ionFlags |= ION_HEAP(ION_SF_HEAP_ID); |
| 186 | |
| 187 | if(usage & GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP) { |
| 188 | ionFlags |= ION_HEAP(ION_SYSTEM_HEAP_ID); |
| 189 | noncontig = true; |
| 190 | } |
| 191 | |
Naseer Ahmed | 4b9784e | 2013-02-07 13:55:43 -0500 | [diff] [blame] | 192 | if(usage & GRALLOC_USAGE_PRIVATE_IOMMU_HEAP) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 193 | ionFlags |= ION_HEAP(ION_IOMMU_HEAP_ID); |
Naseer Ahmed | 4b9784e | 2013-02-07 13:55:43 -0500 | [diff] [blame] | 194 | noncontig = true; |
| 195 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 196 | |
| 197 | if(usage & GRALLOC_USAGE_PRIVATE_MM_HEAP) |
| 198 | ionFlags |= ION_HEAP(ION_CP_MM_HEAP_ID); |
| 199 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 200 | if(usage & GRALLOC_USAGE_PRIVATE_CAMERA_HEAP) |
| 201 | ionFlags |= ION_HEAP(ION_CAMERA_HEAP_ID); |
| 202 | |
Naseer Ahmed | 4b9784e | 2013-02-07 13:55:43 -0500 | [diff] [blame] | 203 | if(usage & GRALLOC_USAGE_PROTECTED && !noncontig) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 204 | ionFlags |= ION_SECURE; |
| 205 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 206 | // if no flags are set, default to |
| 207 | // SF + IOMMU heaps, so that bypass can work |
| 208 | // we can fall back to system heap if |
| 209 | // we run out. |
| 210 | if(!ionFlags) |
| 211 | ionFlags = ION_HEAP(ION_SF_HEAP_ID) | ION_HEAP(ION_IOMMU_HEAP_ID); |
| 212 | |
| 213 | data.flags = ionFlags; |
| 214 | ret = mIonAlloc->alloc_buffer(data); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 215 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 216 | // Fallback |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 217 | if(ret < 0 && canFallback(usage, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 218 | (ionFlags & ION_SYSTEM_HEAP_ID))) |
| 219 | { |
| 220 | ALOGW("Falling back to system heap"); |
| 221 | data.flags = ION_HEAP(ION_SYSTEM_HEAP_ID); |
| 222 | noncontig = true; |
| 223 | ret = mIonAlloc->alloc_buffer(data); |
| 224 | } |
| 225 | |
| 226 | if(ret >= 0 ) { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 227 | data.allocType |= private_handle_t::PRIV_FLAGS_USES_ION; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 228 | if(noncontig) |
| 229 | data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM; |
| 230 | if(ionFlags & ION_SECURE) |
| 231 | data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER; |
| 232 | } |
| 233 | |
| 234 | return ret; |
| 235 | } |
| 236 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 237 | IMemAlloc* IonController::getAllocator(int flags) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 238 | { |
Naseer Ahmed | b16edac | 2012-07-15 23:56:21 -0700 | [diff] [blame] | 239 | IMemAlloc* memalloc = NULL; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 240 | if (flags & private_handle_t::PRIV_FLAGS_USES_ION) { |
| 241 | memalloc = mIonAlloc; |
| 242 | } else { |
| 243 | ALOGE("%s: Invalid flags passed: 0x%x", __FUNCTION__, flags); |
| 244 | } |
| 245 | |
| 246 | return memalloc; |
| 247 | } |
| 248 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 249 | size_t getBufferSizeAndDimensions(int width, int height, int format, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 250 | int& alignedw, int &alignedh) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 251 | { |
| 252 | size_t size; |
| 253 | |
Naomi Luis | a44100c | 2013-02-08 12:42:03 -0800 | [diff] [blame] | 254 | alignedw = AdrenoMemInfo::getInstance().getStride(width, format); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 255 | alignedh = ALIGN(height, 32); |
| 256 | switch (format) { |
| 257 | case HAL_PIXEL_FORMAT_RGBA_8888: |
| 258 | case HAL_PIXEL_FORMAT_RGBX_8888: |
| 259 | case HAL_PIXEL_FORMAT_BGRA_8888: |
| 260 | size = alignedw * alignedh * 4; |
| 261 | break; |
| 262 | case HAL_PIXEL_FORMAT_RGB_888: |
| 263 | size = alignedw * alignedh * 3; |
| 264 | break; |
| 265 | case HAL_PIXEL_FORMAT_RGB_565: |
| 266 | case HAL_PIXEL_FORMAT_RGBA_5551: |
| 267 | case HAL_PIXEL_FORMAT_RGBA_4444: |
| 268 | size = alignedw * alignedh * 2; |
| 269 | break; |
| 270 | |
| 271 | // adreno formats |
| 272 | case HAL_PIXEL_FORMAT_YCrCb_420_SP_ADRENO: // NV21 |
| 273 | size = ALIGN(alignedw*alignedh, 4096); |
| 274 | size += ALIGN(2 * ALIGN(width/2, 32) * ALIGN(height/2, 32), 4096); |
| 275 | break; |
| 276 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED: // NV12 |
| 277 | // The chroma plane is subsampled, |
| 278 | // but the pitch in bytes is unchanged |
| 279 | // The GPU needs 4K alignment, but the video decoder needs 8K |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 280 | size = ALIGN( alignedw * alignedh, 8192); |
| 281 | size += ALIGN( alignedw * ALIGN(height/2, 32), 8192); |
| 282 | break; |
| 283 | case HAL_PIXEL_FORMAT_NV12_ENCODEABLE: |
| 284 | case HAL_PIXEL_FORMAT_YCbCr_420_SP: |
| 285 | case HAL_PIXEL_FORMAT_YCrCb_420_SP: |
| 286 | case HAL_PIXEL_FORMAT_YV12: |
| 287 | if ((format == HAL_PIXEL_FORMAT_YV12) && ((width&1) || (height&1))) { |
| 288 | ALOGE("w or h is odd for the YV12 format"); |
| 289 | return -EINVAL; |
| 290 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 291 | alignedh = height; |
| 292 | if (HAL_PIXEL_FORMAT_NV12_ENCODEABLE == format) { |
| 293 | // The encoder requires a 2K aligned chroma offset. |
| 294 | size = ALIGN(alignedw*alignedh, 2048) + |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 295 | (ALIGN(alignedw/2, 16) * (alignedh/2))*2; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 296 | } else { |
| 297 | size = alignedw*alignedh + |
| 298 | (ALIGN(alignedw/2, 16) * (alignedh/2))*2; |
| 299 | } |
| 300 | size = ALIGN(size, 4096); |
| 301 | break; |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 302 | case HAL_PIXEL_FORMAT_YCbCr_422_SP: |
| 303 | case HAL_PIXEL_FORMAT_YCrCb_422_SP: |
| 304 | if(width & 1) { |
| 305 | ALOGE("width is odd for the YUV422_SP format"); |
| 306 | return -EINVAL; |
| 307 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 308 | alignedh = height; |
| 309 | size = ALIGN(alignedw * alignedh * 2, 4096); |
| 310 | break; |
Sushil Chauhan | c5e6148 | 2012-08-22 17:13:32 -0700 | [diff] [blame] | 311 | case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS: |
Sushil Chauhan | 73dcce4 | 2012-11-15 14:25:19 -0800 | [diff] [blame] | 312 | alignedh = VENUS_Y_SCANLINES(COLOR_FMT_NV12, height); |
Sushil Chauhan | e8a0179 | 2012-11-01 16:25:45 -0700 | [diff] [blame] | 313 | size = VENUS_BUFFER_SIZE(COLOR_FMT_NV12, width, height); |
Sushil Chauhan | c5e6148 | 2012-08-22 17:13:32 -0700 | [diff] [blame] | 314 | break; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 315 | default: |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 316 | ALOGE("unrecognized pixel format: 0x%x", format); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 317 | return -EINVAL; |
| 318 | } |
| 319 | |
| 320 | return size; |
| 321 | } |
| 322 | |
| 323 | // Allocate buffer from width, height and format into a |
| 324 | // private_handle_t. It is the responsibility of the caller |
| 325 | // to free the buffer using the free_buffer function |
| 326 | int alloc_buffer(private_handle_t **pHnd, int w, int h, int format, int usage) |
| 327 | { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 328 | alloc_data data; |
| 329 | int alignedw, alignedh; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 330 | gralloc::IAllocController* sAlloc = |
| 331 | gralloc::IAllocController::getInstance(); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 332 | data.base = 0; |
| 333 | data.fd = -1; |
| 334 | data.offset = 0; |
| 335 | data.size = getBufferSizeAndDimensions(w, h, format, alignedw, alignedh); |
| 336 | data.align = getpagesize(); |
| 337 | data.uncached = useUncached(usage); |
| 338 | int allocFlags = usage; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 339 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 340 | int err = sAlloc->allocate(data, allocFlags); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 341 | if (0 != err) { |
| 342 | ALOGE("%s: allocate failed", __FUNCTION__); |
| 343 | return -ENOMEM; |
| 344 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 345 | |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 346 | private_handle_t* hnd = new private_handle_t(data.fd, data.size, |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 347 | data.allocType, 0, format, |
| 348 | alignedw, alignedh); |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 349 | hnd->base = (int) data.base; |
| 350 | hnd->offset = data.offset; |
| 351 | hnd->gpuaddr = 0; |
| 352 | *pHnd = hnd; |
| 353 | return 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | void free_buffer(private_handle_t *hnd) |
| 357 | { |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 358 | gralloc::IAllocController* sAlloc = |
| 359 | gralloc::IAllocController::getInstance(); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 360 | if (hnd && hnd->fd > 0) { |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 361 | IMemAlloc* memalloc = sAlloc->getAllocator(hnd->flags); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 362 | memalloc->free_buffer((void*)hnd->base, hnd->size, hnd->offset, hnd->fd); |
| 363 | } |
| 364 | if(hnd) |
| 365 | delete hnd; |
| 366 | |
| 367 | } |