blob: 8bae92645b85fd778fb03ad990db8756adc4638f [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Arun Kumar K.R6c85f052014-01-21 21:47:41 -08003 * Copyright (c) 2011-2014 The Linux Foundation. All rights reserved.
Iliyan Malchev202a77d2012-06-11 14:41:12 -07004 *
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 Malchev202a77d2012-06-11 14:41:12 -070024#include "gr.h"
25#include "gpu.h"
26#include "memalloc.h"
27#include "alloc_controller.h"
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080028#include <qdMetaData.h>
Shalaj Jain1f9725a2015-03-04 17:53:49 -080029#include <linux/msm_ion.h>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070030
31using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070032
33gpu_context_t::gpu_context_t(const private_module_t* module,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070034 IAllocController* alloc_ctrl ) :
Iliyan Malchev202a77d2012-06-11 14:41:12 -070035 mAllocCtrl(alloc_ctrl)
36{
37 // Zero out the alloc_device_t
38 memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t));
39
Iliyan Malchev202a77d2012-06-11 14:41:12 -070040 // Initialize the procs
41 common.tag = HARDWARE_DEVICE_TAG;
42 common.version = 0;
43 common.module = const_cast<hw_module_t*>(&module->base.common);
44 common.close = gralloc_close;
45 alloc = gralloc_alloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070046 free = gralloc_free;
47
48}
49
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -070050int gpu_context_t::gralloc_alloc_buffer(unsigned int size, int usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -070051 buffer_handle_t* pHandle, int bufferType,
52 int format, int width, int height)
53{
54 int err = 0;
55 int flags = 0;
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -070056 int alignedw = 0;
57 int alignedh = 0;
58
59 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
60 height,
61 format,
62 usage,
63 alignedw,
64 alignedh);
65
Iliyan Malchev202a77d2012-06-11 14:41:12 -070066 size = roundUpToPageSize(size);
67 alloc_data data;
68 data.offset = 0;
69 data.fd = -1;
70 data.base = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070071 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
72 data.align = 8192;
73 else
74 data.align = getpagesize();
Praveen Chavan4e931c12012-11-28 19:43:17 -080075
Naseer Ahmedde758fd2016-05-03 15:10:23 -040076 if (usage & GRALLOC_USAGE_PROTECTED) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -080077 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
78 /* The alignment here reflects qsee mmu V7L/V8L requirement */
79 data.align = SZ_2M;
80 } else {
81 data.align = SECURE_ALIGN;
82 }
Praveen Chavan4e931c12012-11-28 19:43:17 -080083 size = ALIGN(size, data.align);
84 }
Naseer Ahmed84786722013-06-14 16:29:59 -040085
Praveen Chavan4e931c12012-11-28 19:43:17 -080086 data.size = size;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -080087 data.pHandle = (uintptr_t) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070088 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070089
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080090 if (!err) {
91 /* allocate memory for enhancement data */
92 alloc_data eData;
93 eData.fd = -1;
94 eData.base = 0;
95 eData.offset = 0;
96 eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
97 eData.pHandle = data.pHandle;
98 eData.align = getpagesize();
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -050099 int eDataUsage = 0;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800100 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
101 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
102 strerror(-eDataErr));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700103
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800104 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
105 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800106 }
Saurabh Shahbddb9522015-09-30 13:31:55 -0700107
Arun Kumar K.R48a57552015-09-03 11:37:35 -0700108 if (usage & GRALLOC_USAGE_PRIVATE_INTERNAL_ONLY) {
109 flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
110 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400111
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800112 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
113 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
114 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400115
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800116 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
117 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
118 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400119
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800120 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
121 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
122 }
123
Naseer Ahmede41ad9c2013-04-05 17:59:28 -0400124 if (usage & GRALLOC_USAGE_HW_COMPOSER) {
125 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
126 }
127
Shuzhen Wang46ca2262013-04-10 23:02:22 -0700128 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
129 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
130 }
131
Ramkumar Radhakrishnanba713382013-08-30 18:41:07 -0700132 if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
133 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
134 }
135
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700136 if(isMacroTileEnabled(format, usage)) {
137 flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED;
138 }
139
Sushil Chauhan7807d192015-08-13 10:10:48 -0700140 if (isUBwcEnabled(format, usage)) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800141 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
142 }
143
Ramkumar Radhakrishnan9d20b392013-11-15 19:32:47 -0800144 if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
145 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
146 }
147
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800148 if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER |
149 GRALLOC_USAGE_HW_CAMERA_WRITE |
150 GRALLOC_USAGE_HW_RENDER |
151 GRALLOC_USAGE_HW_FB)) {
152 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
153 }
154
Surajit Podder074314d2015-06-25 01:43:20 +0530155 if(usage & GRALLOC_USAGE_HW_COMPOSER) {
156 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
157 }
158
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800159 if(false == data.uncached) {
160 flags |= private_handle_t::PRIV_FLAGS_CACHED;
161 }
162
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700163 flags |= data.allocType;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700164 uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800165 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700166 bufferType, format, alignedw, alignedh,
167 eData.fd, eData.offset, eBaseAddr, width, height);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700168
169 hnd->offset = data.offset;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700170 hnd->base = (uint64_t)(data.base) + data.offset;
Naseer Ahmeda163b732013-02-12 14:53:33 -0500171 hnd->gpuaddr = 0;
Pullakavi Srinivas46204e32016-07-22 12:44:46 +0530172 ColorSpace_t colorSpace = ITU_R_601;
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400173 setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);
Naseer Ahmeda163b732013-02-12 14:53:33 -0500174
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700175 *pHandle = hnd;
176 }
177
178 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800179
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700180 return err;
181}
182
183void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700184 int *bufferType)
185{
186 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700187
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700188 if (isUncompressedRgbFormat(inputFormat) == TRUE) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700189 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700190 *bufferType = BUFFER_TYPE_UI;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700191 }
192}
193
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800194int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400195 buffer_handle_t* pHandle)
196{
197 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
198
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500199 // This allocation will only happen when gralloc is in fb mode
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400200
201 if (m->framebuffer == NULL) {
202 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
203 return -EINVAL;
204 }
205
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700206 const unsigned int bufferMask = m->bufferMask;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400207 const uint32_t numBuffers = m->numBuffers;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700208 unsigned int bufferSize = m->finfo.line_length * m->info.yres;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400209
210 //adreno needs FB size to be page aligned
211 bufferSize = roundUpToPageSize(bufferSize);
212
213 if (numBuffers == 1) {
214 // If we have only one buffer, we never use page-flipping. Instead,
215 // we return a regular buffer which will be memcpy'ed to the main
216 // screen when post is called.
217 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
218 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
219 m->fbFormat, m->info.xres, m->info.yres);
220 }
221
222 if (bufferMask >= ((1LU<<numBuffers)-1)) {
223 // We ran out of buffers.
224 return -ENOMEM;
225 }
226
227 // create a "fake" handle for it
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700228 uint64_t vaddr = uint64_t(m->framebuffer->base);
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700229 // As GPU needs ION FD, the private handle is created
230 // using ION fd and ION flags are set
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400231 private_handle_t* hnd = new private_handle_t(
232 dup(m->framebuffer->fd), bufferSize,
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700233 private_handle_t::PRIV_FLAGS_USES_ION |
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400234 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
235 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
236 m->info.yres);
237
238 // find a free slot
239 for (uint32_t i=0 ; i<numBuffers ; i++) {
240 if ((bufferMask & (1LU<<i)) == 0) {
Shalaj Jaina70b4352014-06-15 13:47:47 -0700241 m->bufferMask |= (uint32_t)(1LU<<i);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400242 break;
243 }
244 vaddr += bufferSize;
245 }
246 hnd->base = vaddr;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700247 hnd->offset = (unsigned int)(vaddr - m->framebuffer->base);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400248 *pHandle = hnd;
249 return 0;
250}
251
252
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800253int gpu_context_t::gralloc_alloc_framebuffer(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400254 buffer_handle_t* pHandle)
255{
256 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
257 pthread_mutex_lock(&m->lock);
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800258 int err = gralloc_alloc_framebuffer_locked(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400259 pthread_mutex_unlock(&m->lock);
260 return err;
261}
262
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700263int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700264 buffer_handle_t* pHandle, int* pStride,
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700265 unsigned int bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700266 if (!pHandle || !pStride)
267 return -EINVAL;
268
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700269 unsigned int size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700270 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400271 int grallocFormat = format;
272 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400273
Saurabh Shahaedf2362012-09-05 11:30:59 -0700274 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
275 //the usage bits, gralloc assigns a format.
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400276 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
277 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Sushil Chauhan32333292015-02-04 18:56:32 -0800278 if (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC)
279 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
Jeykumar Sankaran9bc1a782015-12-14 18:36:27 -0800280 else if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
281 if(MDPCapabilityInfo::getInstance().isWBUBWCSupportedByMDP() &&
282 usage & GRALLOC_USAGE_HW_COMPOSER)
283 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
284 else
285 grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
286 } else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK)
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700287 == GRALLOC_USAGE_HW_CAMERA_ZSL)
288 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL
Saurabh Shahaedf2362012-09-05 11:30:59 -0700289 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
290 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
Ranjith Kagathi Ananda330b21d2015-08-17 16:41:08 -0700291 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
292 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Nagesh Subba Reddy81a519f2015-08-29 18:01:49 -0700293 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21
Ranjith Kagathi Ananda330b21d2015-08-17 16:41:08 -0700294 } else {
295 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; //NV12 preview
296 }
297 } else if(usage & GRALLOC_USAGE_HW_COMPOSER)
Manoj Kumar AVMcdb4fd52013-10-25 19:40:45 -0700298 //XXX: If we still haven't set a format, default to RGBA8888
299 grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888;
Nagesh Subba Reddy81a519f2015-08-29 18:01:49 -0700300 else if(format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
301 //If no other usage flags are detected, default the
302 //flexible YUV format to NV21_ZSL
303 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL;
304 }
Baldev Sahu5f673b42013-12-05 09:49:09 +0530305 }
306
Sushil Chauhan68c793a2015-04-16 16:31:22 -0700307 bool useFbMem = false;
308 char property[PROPERTY_VALUE_MAX];
309 char isUBWC[PROPERTY_VALUE_MAX];
310 if (usage & GRALLOC_USAGE_HW_FB) {
311 if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
312 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
313 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
314 useFbMem = true;
315 } else {
Sushil Chauhanc9853b52015-09-30 18:43:11 -0700316 usage &= ~GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
Sushil Chauhan68c793a2015-04-16 16:31:22 -0700317 if (property_get("debug.gralloc.enable_fb_ubwc", isUBWC, NULL) > 0){
318 if ((!strncmp(isUBWC, "1", PROPERTY_VALUE_MAX)) ||
319 (!strncasecmp(isUBWC, "true", PROPERTY_VALUE_MAX))) {
320 // Allocate UBWC aligned framebuffer
321 usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
322 }
323 }
324 }
325 }
326
Saurabh Shahaedf2362012-09-05 11:30:59 -0700327 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700328 size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw,
329 alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700330
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700331 if ((unsigned int)size <= 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700332 return -EINVAL;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800333 size = (bufferSize >= size)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700334
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400335 int err = 0;
336 if(useFbMem) {
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800337 err = gralloc_alloc_framebuffer(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400338 } else {
339 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700340 grallocFormat, w, h);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400341 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700342
343 if (err < 0) {
344 return err;
345 }
346
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700347 *pStride = alignedw;
348 return 0;
349}
350
351int gpu_context_t::free_impl(private_handle_t const* hnd) {
352 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400353 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700354 const unsigned int bufferSize = m->finfo.line_length * m->info.yres;
355 unsigned int index = (unsigned int) ((hnd->base - m->framebuffer->base)
356 / bufferSize);
Shalaj Jaina70b4352014-06-15 13:47:47 -0700357 m->bufferMask &= (uint32_t)~(1LU<<index);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400358 } else {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800359
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400360 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
361 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700362 int err = memalloc->free_buffer((void*)hnd->base, hnd->size,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400363 hnd->offset, hnd->fd);
364 if(err)
365 return err;
366 // free the metadata space
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700367 unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400368 err = memalloc->free_buffer((void*)hnd->base_metadata,
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800369 size, hnd->offset_metadata,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400370 hnd->fd_metadata);
371 if (err)
372 return err;
373 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700374 delete hnd;
375 return 0;
376}
377
378int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700379 int usage, buffer_handle_t* pHandle,
380 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700381{
382 if (!dev) {
383 return -EINVAL;
384 }
385 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
386 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
387}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700388int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
389 int format, int usage,
390 buffer_handle_t* pHandle, int* pStride,
391 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700392{
393 if (!dev) {
394 return -EINVAL;
395 }
396 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
397 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
398}
399
400
401int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700402 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700403{
404 if (private_handle_t::validate(handle) < 0)
405 return -EINVAL;
406
407 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
408 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
409 return gpu->free_impl(hnd);
410}
411
412/*****************************************************************************/
413
414int gpu_context_t::gralloc_close(struct hw_device_t *dev)
415{
416 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
417 if (ctx) {
418 /* TODO: keep a list of all buffer_handle_t created, and free them
419 * all here.
420 */
421 delete ctx;
422 }
423 return 0;
424}
425