Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
Naseer Ahmed | a163b73 | 2013-02-12 14:53:33 -0500 | [diff] [blame] | 3 | * Copyright (c) 2011-2013 The Linux Foundation. All rights reserved. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 24 | #include "gr.h" |
| 25 | #include "gpu.h" |
| 26 | #include "memalloc.h" |
| 27 | #include "alloc_controller.h" |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 28 | #include <qdMetaData.h> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 29 | |
| 30 | using namespace gralloc; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 31 | |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 32 | #define SZ_1M 0x100000 |
| 33 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 34 | gpu_context_t::gpu_context_t(const private_module_t* module, |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 35 | IAllocController* alloc_ctrl ) : |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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; |
Naseer Ahmed | 7421472 | 2013-02-09 08:11:36 -0500 | [diff] [blame] | 47 | #ifdef QCOM_BSP |
Ramkumar Radhakrishnan | 3d4c0ac | 2012-12-10 15:42:54 -0800 | [diff] [blame] | 48 | allocSize = gralloc_alloc_size; |
Naseer Ahmed | 7421472 | 2013-02-09 08:11:36 -0500 | [diff] [blame] | 49 | #endif |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 50 | free = gralloc_free; |
| 51 | |
| 52 | } |
| 53 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 54 | int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage, |
| 55 | buffer_handle_t* pHandle, int bufferType, |
| 56 | int format, int width, int height) |
| 57 | { |
| 58 | int err = 0; |
| 59 | int flags = 0; |
| 60 | size = roundUpToPageSize(size); |
| 61 | alloc_data data; |
| 62 | data.offset = 0; |
| 63 | data.fd = -1; |
| 64 | data.base = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 65 | if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) |
| 66 | data.align = 8192; |
| 67 | else |
| 68 | data.align = getpagesize(); |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 69 | |
| 70 | /* force 1MB alignment selectively for secure buffers, MDP5 onwards */ |
Naseer Ahmed | 8478672 | 2013-06-14 16:29:59 -0400 | [diff] [blame] | 71 | #ifdef MDSS_TARGET |
| 72 | if (usage & GRALLOC_USAGE_PROTECTED) { |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 73 | data.align = ALIGN(data.align, SZ_1M); |
| 74 | size = ALIGN(size, data.align); |
| 75 | } |
Naseer Ahmed | 8478672 | 2013-06-14 16:29:59 -0400 | [diff] [blame] | 76 | #endif |
| 77 | |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 78 | data.size = size; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 79 | data.pHandle = (unsigned int) pHandle; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 80 | err = mAllocCtrl->allocate(data, usage); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 81 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 82 | if (!err) { |
| 83 | /* allocate memory for enhancement data */ |
| 84 | alloc_data eData; |
| 85 | eData.fd = -1; |
| 86 | eData.base = 0; |
| 87 | eData.offset = 0; |
| 88 | eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 89 | eData.pHandle = data.pHandle; |
| 90 | eData.align = getpagesize(); |
| 91 | int eDataUsage = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP; |
| 92 | int eDataErr = mAllocCtrl->allocate(eData, eDataUsage); |
| 93 | ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s", |
| 94 | strerror(-eDataErr)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 95 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 96 | if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) { |
| 97 | flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY; |
| 98 | //The EXTERNAL_BLOCK flag is always an add-on |
| 99 | if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_BLOCK) { |
| 100 | flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK; |
| 101 | } |
| 102 | if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_CC) { |
| 103 | flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_CC; |
| 104 | } |
| 105 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 106 | |
Naseer Ahmed | fc940ef | 2013-04-29 15:47:47 -0400 | [diff] [blame] | 107 | if (bufferType == BUFFER_TYPE_VIDEO) { |
| 108 | if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { |
Naseer Ahmed | 8478672 | 2013-06-14 16:29:59 -0400 | [diff] [blame] | 109 | #ifndef MDSS_TARGET |
| 110 | flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR; |
| 111 | #else |
Naseer Ahmed | fc940ef | 2013-04-29 15:47:47 -0400 | [diff] [blame] | 112 | if (usage & (GRALLOC_USAGE_HW_TEXTURE | |
| 113 | GRALLOC_USAGE_HW_VIDEO_ENCODER)) |
| 114 | flags |= private_handle_t::PRIV_FLAGS_ITU_R_709; |
| 115 | else if (usage & GRALLOC_USAGE_HW_CAMERA_ZSL) |
| 116 | flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR; |
Naseer Ahmed | 8478672 | 2013-06-14 16:29:59 -0400 | [diff] [blame] | 117 | #endif |
Naseer Ahmed | fc940ef | 2013-04-29 15:47:47 -0400 | [diff] [blame] | 118 | } else { |
| 119 | flags |= private_handle_t::PRIV_FLAGS_ITU_R_601; |
| 120 | } |
| 121 | } |
| 122 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 123 | if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) { |
| 124 | flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER; |
| 125 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 126 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 127 | if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { |
| 128 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE; |
| 129 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 130 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 131 | if (usage & GRALLOC_USAGE_HW_CAMERA_READ) { |
| 132 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ; |
| 133 | } |
| 134 | |
Naseer Ahmed | e41ad9c | 2013-04-05 17:59:28 -0400 | [diff] [blame] | 135 | if (usage & GRALLOC_USAGE_HW_COMPOSER) { |
| 136 | flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER; |
| 137 | } |
| 138 | |
Shuzhen Wang | 46ca226 | 2013-04-10 23:02:22 -0700 | [diff] [blame] | 139 | if (usage & GRALLOC_USAGE_HW_TEXTURE) { |
| 140 | flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE; |
| 141 | } |
| 142 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 143 | flags |= data.allocType; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 144 | int eBaseAddr = int(eData.base) + eData.offset; |
| 145 | private_handle_t *hnd = new private_handle_t(data.fd, size, flags, |
| 146 | bufferType, format, width, height, eData.fd, eData.offset, |
| 147 | eBaseAddr); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 148 | |
| 149 | hnd->offset = data.offset; |
| 150 | hnd->base = int(data.base) + data.offset; |
Naseer Ahmed | a163b73 | 2013-02-12 14:53:33 -0500 | [diff] [blame] | 151 | hnd->gpuaddr = 0; |
| 152 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 153 | *pHandle = hnd; |
| 154 | } |
| 155 | |
| 156 | ALOGE_IF(err, "gralloc failed err=%s", strerror(-err)); |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 157 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 158 | return err; |
| 159 | } |
| 160 | |
| 161 | void gpu_context_t::getGrallocInformationFromFormat(int inputFormat, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 162 | int *bufferType) |
| 163 | { |
| 164 | *bufferType = BUFFER_TYPE_VIDEO; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 165 | |
Naseer Ahmed | 65f1656 | 2012-06-15 20:53:42 -0700 | [diff] [blame] | 166 | if (inputFormat < 0x7) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 167 | // RGB formats |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 168 | *bufferType = BUFFER_TYPE_UI; |
| 169 | } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) || |
| 170 | (inputFormat == HAL_PIXEL_FORMAT_RG_88)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 171 | *bufferType = BUFFER_TYPE_UI; |
| 172 | } |
| 173 | } |
| 174 | |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 175 | int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage, |
| 176 | buffer_handle_t* pHandle) |
| 177 | { |
| 178 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
| 179 | |
| 180 | // we don't support framebuffer allocations with graphics heap flags |
| 181 | if (usage & GRALLOC_HEAP_MASK) { |
| 182 | return -EINVAL; |
| 183 | } |
| 184 | |
| 185 | if (m->framebuffer == NULL) { |
| 186 | ALOGE("%s: Invalid framebuffer", __FUNCTION__); |
| 187 | return -EINVAL; |
| 188 | } |
| 189 | |
| 190 | const uint32_t bufferMask = m->bufferMask; |
| 191 | const uint32_t numBuffers = m->numBuffers; |
| 192 | size_t bufferSize = m->finfo.line_length * m->info.yres; |
| 193 | |
| 194 | //adreno needs FB size to be page aligned |
| 195 | bufferSize = roundUpToPageSize(bufferSize); |
| 196 | |
| 197 | if (numBuffers == 1) { |
| 198 | // If we have only one buffer, we never use page-flipping. Instead, |
| 199 | // we return a regular buffer which will be memcpy'ed to the main |
| 200 | // screen when post is called. |
| 201 | int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D; |
| 202 | return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI, |
| 203 | m->fbFormat, m->info.xres, m->info.yres); |
| 204 | } |
| 205 | |
| 206 | if (bufferMask >= ((1LU<<numBuffers)-1)) { |
| 207 | // We ran out of buffers. |
| 208 | return -ENOMEM; |
| 209 | } |
| 210 | |
| 211 | // create a "fake" handle for it |
| 212 | intptr_t vaddr = intptr_t(m->framebuffer->base); |
| 213 | private_handle_t* hnd = new private_handle_t( |
| 214 | dup(m->framebuffer->fd), bufferSize, |
| 215 | private_handle_t::PRIV_FLAGS_USES_PMEM | |
| 216 | private_handle_t::PRIV_FLAGS_FRAMEBUFFER, |
| 217 | BUFFER_TYPE_UI, m->fbFormat, m->info.xres, |
| 218 | m->info.yres); |
| 219 | |
| 220 | // find a free slot |
| 221 | for (uint32_t i=0 ; i<numBuffers ; i++) { |
| 222 | if ((bufferMask & (1LU<<i)) == 0) { |
| 223 | m->bufferMask |= (1LU<<i); |
| 224 | break; |
| 225 | } |
| 226 | vaddr += bufferSize; |
| 227 | } |
| 228 | hnd->base = vaddr; |
| 229 | hnd->offset = vaddr - intptr_t(m->framebuffer->base); |
| 230 | *pHandle = hnd; |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage, |
| 236 | buffer_handle_t* pHandle) |
| 237 | { |
| 238 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
| 239 | pthread_mutex_lock(&m->lock); |
| 240 | int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle); |
| 241 | pthread_mutex_unlock(&m->lock); |
| 242 | return err; |
| 243 | } |
| 244 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 245 | int gpu_context_t::alloc_impl(int w, int h, int format, int usage, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 246 | buffer_handle_t* pHandle, int* pStride, |
| 247 | size_t bufferSize) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 248 | if (!pHandle || !pStride) |
| 249 | return -EINVAL; |
| 250 | |
| 251 | size_t size; |
| 252 | int alignedw, alignedh; |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 253 | int grallocFormat = format; |
| 254 | int bufferType; |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 255 | |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 256 | //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on |
| 257 | //the usage bits, gralloc assigns a format. |
| 258 | if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
| 259 | if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) |
Shuzhen Wang | a11b6ba | 2013-04-21 14:23:05 -0700 | [diff] [blame] | 260 | grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12 |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 261 | else if(usage & GRALLOC_USAGE_HW_CAMERA_READ) |
| 262 | grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21 |
| 263 | else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) |
| 264 | grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21 |
| 265 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 266 | |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 267 | getGrallocInformationFromFormat(grallocFormat, &bufferType); |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 268 | size = getBufferSizeAndDimensions(w, h, grallocFormat, alignedw, alignedh); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 269 | |
| 270 | if ((ssize_t)size <= 0) |
| 271 | return -EINVAL; |
Ramkumar Radhakrishnan | 73f952a | 2013-03-05 14:14:24 -0800 | [diff] [blame] | 272 | size = (bufferSize >= size)? bufferSize : size; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 273 | |
| 274 | // All buffers marked as protected or for external |
| 275 | // display need to go to overlay |
| 276 | if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) || |
Sushil Chauhan | 7651a80 | 2013-01-08 16:08:09 -0800 | [diff] [blame] | 277 | (usage & GRALLOC_USAGE_PROTECTED)) { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 278 | bufferType = BUFFER_TYPE_VIDEO; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 279 | } |
Naseer Ahmed | 768619e | 2012-11-28 17:02:52 -0500 | [diff] [blame] | 280 | |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 281 | bool useFbMem = false; |
| 282 | char property[PROPERTY_VALUE_MAX]; |
| 283 | if((usage & GRALLOC_USAGE_HW_FB) && |
| 284 | (property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) && |
| 285 | (!strncmp(property, "1", PROPERTY_VALUE_MAX ) || |
| 286 | (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) { |
| 287 | useFbMem = true; |
| 288 | } |
| 289 | |
| 290 | int err = 0; |
| 291 | if(useFbMem) { |
| 292 | err = gralloc_alloc_framebuffer(size, usage, pHandle); |
| 293 | } else { |
| 294 | err = gralloc_alloc_buffer(size, usage, pHandle, bufferType, |
| 295 | grallocFormat, alignedw, alignedh); |
| 296 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 297 | |
| 298 | if (err < 0) { |
| 299 | return err; |
| 300 | } |
| 301 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 302 | *pStride = alignedw; |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | int gpu_context_t::free_impl(private_handle_t const* hnd) { |
| 307 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 308 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { |
| 309 | const size_t bufferSize = m->finfo.line_length * m->info.yres; |
| 310 | int index = (hnd->base - m->framebuffer->base) / bufferSize; |
| 311 | m->bufferMask &= ~(1<<index); |
| 312 | } else { |
Jeykumar Sankaran | c1f8682 | 2013-02-20 18:32:01 -0800 | [diff] [blame] | 313 | |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 314 | terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd)); |
| 315 | IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags); |
| 316 | int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size, |
| 317 | hnd->offset, hnd->fd); |
| 318 | if(err) |
| 319 | return err; |
| 320 | // free the metadata space |
| 321 | unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 322 | err = memalloc->free_buffer((void*)hnd->base_metadata, |
| 323 | (size_t) size, hnd->offset_metadata, |
| 324 | hnd->fd_metadata); |
| 325 | if (err) |
| 326 | return err; |
| 327 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 328 | delete hnd; |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 333 | int usage, buffer_handle_t* pHandle, |
| 334 | int* pStride) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 335 | { |
| 336 | if (!dev) { |
| 337 | return -EINVAL; |
| 338 | } |
| 339 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 340 | return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0); |
| 341 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 342 | int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h, |
| 343 | int format, int usage, |
| 344 | buffer_handle_t* pHandle, int* pStride, |
| 345 | int bufferSize) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 346 | { |
| 347 | if (!dev) { |
| 348 | return -EINVAL; |
| 349 | } |
| 350 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 351 | return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize); |
| 352 | } |
| 353 | |
| 354 | |
| 355 | int gpu_context_t::gralloc_free(alloc_device_t* dev, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 356 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 357 | { |
| 358 | if (private_handle_t::validate(handle) < 0) |
| 359 | return -EINVAL; |
| 360 | |
| 361 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle); |
| 362 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 363 | return gpu->free_impl(hnd); |
| 364 | } |
| 365 | |
| 366 | /*****************************************************************************/ |
| 367 | |
| 368 | int gpu_context_t::gralloc_close(struct hw_device_t *dev) |
| 369 | { |
| 370 | gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev); |
| 371 | if (ctx) { |
| 372 | /* TODO: keep a list of all buffer_handle_t created, and free them |
| 373 | * all here. |
| 374 | */ |
| 375 | delete ctx; |
| 376 | } |
| 377 | return 0; |
| 378 | } |
| 379 | |