blob: 545903a560c48affbe88578b0b729d79fe515191 [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) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -070077 if ((usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) ||
78 (usage & GRALLOC_USAGE_HW_CAMERA_MASK)) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -080079 /* The alignment here reflects qsee mmu V7L/V8L requirement */
80 data.align = SZ_2M;
81 } else {
82 data.align = SECURE_ALIGN;
83 }
Praveen Chavan4e931c12012-11-28 19:43:17 -080084 size = ALIGN(size, data.align);
85 }
Naseer Ahmed84786722013-06-14 16:29:59 -040086
Praveen Chavan4e931c12012-11-28 19:43:17 -080087 data.size = size;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -080088 data.pHandle = (uintptr_t) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070089 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070090
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080091 if (!err) {
92 /* allocate memory for enhancement data */
93 alloc_data eData;
94 eData.fd = -1;
95 eData.base = 0;
96 eData.offset = 0;
97 eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
98 eData.pHandle = data.pHandle;
99 eData.align = getpagesize();
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500100 int eDataUsage = 0;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800101 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
102 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
103 strerror(-eDataErr));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700104
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800105 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
106 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800107 }
Saurabh Shahbddb9522015-09-30 13:31:55 -0700108
Arun Kumar K.R48a57552015-09-03 11:37:35 -0700109 if (usage & GRALLOC_USAGE_PRIVATE_INTERNAL_ONLY) {
110 flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
111 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400112
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800113 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
114 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
115 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400116
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800117 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
118 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
119 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400120
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800121 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
122 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
123 }
124
Naseer Ahmede41ad9c2013-04-05 17:59:28 -0400125 if (usage & GRALLOC_USAGE_HW_COMPOSER) {
126 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
127 }
128
Shuzhen Wang46ca2262013-04-10 23:02:22 -0700129 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
130 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
131 }
132
Ramkumar Radhakrishnanba713382013-08-30 18:41:07 -0700133 if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
134 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
135 }
136
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700137 if(isMacroTileEnabled(format, usage)) {
138 flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED;
139 }
140
Sushil Chauhan7807d192015-08-13 10:10:48 -0700141 if (isUBwcEnabled(format, usage)) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800142 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
143 }
144
Ramkumar Radhakrishnan9d20b392013-11-15 19:32:47 -0800145 if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
146 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
147 }
148
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800149 if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER |
150 GRALLOC_USAGE_HW_CAMERA_WRITE |
151 GRALLOC_USAGE_HW_RENDER |
152 GRALLOC_USAGE_HW_FB)) {
153 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
154 }
155
Surajit Podder074314d2015-06-25 01:43:20 +0530156 if(usage & GRALLOC_USAGE_HW_COMPOSER) {
157 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
158 }
159
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800160 if(false == data.uncached) {
161 flags |= private_handle_t::PRIV_FLAGS_CACHED;
162 }
163
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700164 flags |= data.allocType;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700165 uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800166 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700167 bufferType, format, alignedw, alignedh,
168 eData.fd, eData.offset, eBaseAddr, width, height);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700169
170 hnd->offset = data.offset;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700171 hnd->base = (uint64_t)(data.base) + data.offset;
Naseer Ahmeda163b732013-02-12 14:53:33 -0500172 hnd->gpuaddr = 0;
Pullakavi Srinivas46204e32016-07-22 12:44:46 +0530173 ColorSpace_t colorSpace = ITU_R_601;
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400174 setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);
Naseer Ahmeda163b732013-02-12 14:53:33 -0500175
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700176 *pHandle = hnd;
177 }
178
179 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800180
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700181 return err;
182}
183
184void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700185 int *bufferType)
186{
187 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700188
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700189 if (isUncompressedRgbFormat(inputFormat) == TRUE) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700190 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700191 *bufferType = BUFFER_TYPE_UI;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700192 }
193}
194
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800195int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400196 buffer_handle_t* pHandle)
197{
198 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
199
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500200 // This allocation will only happen when gralloc is in fb mode
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400201
202 if (m->framebuffer == NULL) {
203 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
204 return -EINVAL;
205 }
206
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700207 const unsigned int bufferMask = m->bufferMask;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400208 const uint32_t numBuffers = m->numBuffers;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700209 unsigned int bufferSize = m->finfo.line_length * m->info.yres;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400210
211 //adreno needs FB size to be page aligned
212 bufferSize = roundUpToPageSize(bufferSize);
213
214 if (numBuffers == 1) {
215 // If we have only one buffer, we never use page-flipping. Instead,
216 // we return a regular buffer which will be memcpy'ed to the main
217 // screen when post is called.
218 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
219 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
220 m->fbFormat, m->info.xres, m->info.yres);
221 }
222
223 if (bufferMask >= ((1LU<<numBuffers)-1)) {
224 // We ran out of buffers.
225 return -ENOMEM;
226 }
227
228 // create a "fake" handle for it
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700229 uint64_t vaddr = uint64_t(m->framebuffer->base);
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700230 // As GPU needs ION FD, the private handle is created
231 // using ION fd and ION flags are set
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400232 private_handle_t* hnd = new private_handle_t(
233 dup(m->framebuffer->fd), bufferSize,
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700234 private_handle_t::PRIV_FLAGS_USES_ION |
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400235 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
236 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
237 m->info.yres);
238
239 // find a free slot
240 for (uint32_t i=0 ; i<numBuffers ; i++) {
241 if ((bufferMask & (1LU<<i)) == 0) {
Shalaj Jaina70b4352014-06-15 13:47:47 -0700242 m->bufferMask |= (uint32_t)(1LU<<i);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400243 break;
244 }
245 vaddr += bufferSize;
246 }
247 hnd->base = vaddr;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700248 hnd->offset = (unsigned int)(vaddr - m->framebuffer->base);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400249 *pHandle = hnd;
250 return 0;
251}
252
253
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800254int gpu_context_t::gralloc_alloc_framebuffer(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400255 buffer_handle_t* pHandle)
256{
257 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
258 pthread_mutex_lock(&m->lock);
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800259 int err = gralloc_alloc_framebuffer_locked(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400260 pthread_mutex_unlock(&m->lock);
261 return err;
262}
263
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700264int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700265 buffer_handle_t* pHandle, int* pStride,
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700266 unsigned int bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700267 if (!pHandle || !pStride)
268 return -EINVAL;
269
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700270 unsigned int size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700271 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400272 int grallocFormat = format;
273 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400274
Saurabh Shahaedf2362012-09-05 11:30:59 -0700275 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
276 //the usage bits, gralloc assigns a format.
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400277 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
278 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Sushil Chauhan32333292015-02-04 18:56:32 -0800279 if (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC)
280 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
Jeykumar Sankaran9bc1a782015-12-14 18:36:27 -0800281 else if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
282 if(MDPCapabilityInfo::getInstance().isWBUBWCSupportedByMDP() &&
283 usage & GRALLOC_USAGE_HW_COMPOSER)
284 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
285 else
286 grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
287 } else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK)
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700288 == GRALLOC_USAGE_HW_CAMERA_ZSL)
289 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL
Saurabh Shahaedf2362012-09-05 11:30:59 -0700290 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
291 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
Ranjith Kagathi Ananda330b21d2015-08-17 16:41:08 -0700292 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
293 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Nagesh Subba Reddy81a519f2015-08-29 18:01:49 -0700294 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21
Ranjith Kagathi Ananda330b21d2015-08-17 16:41:08 -0700295 } else {
296 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; //NV12 preview
297 }
298 } else if(usage & GRALLOC_USAGE_HW_COMPOSER)
Manoj Kumar AVMcdb4fd52013-10-25 19:40:45 -0700299 //XXX: If we still haven't set a format, default to RGBA8888
300 grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888;
Nagesh Subba Reddy81a519f2015-08-29 18:01:49 -0700301 else if(format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
302 //If no other usage flags are detected, default the
303 //flexible YUV format to NV21_ZSL
304 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL;
305 }
Baldev Sahu5f673b42013-12-05 09:49:09 +0530306 }
307
Sushil Chauhan68c793a2015-04-16 16:31:22 -0700308 bool useFbMem = false;
309 char property[PROPERTY_VALUE_MAX];
310 char isUBWC[PROPERTY_VALUE_MAX];
311 if (usage & GRALLOC_USAGE_HW_FB) {
312 if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
313 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
314 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
315 useFbMem = true;
316 } else {
Sushil Chauhanc9853b52015-09-30 18:43:11 -0700317 usage &= ~GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
Sushil Chauhan68c793a2015-04-16 16:31:22 -0700318 if (property_get("debug.gralloc.enable_fb_ubwc", isUBWC, NULL) > 0){
319 if ((!strncmp(isUBWC, "1", PROPERTY_VALUE_MAX)) ||
320 (!strncasecmp(isUBWC, "true", PROPERTY_VALUE_MAX))) {
321 // Allocate UBWC aligned framebuffer
322 usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
323 }
324 }
325 }
326 }
327
Saurabh Shahaedf2362012-09-05 11:30:59 -0700328 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700329 size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw,
330 alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700331
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700332 if ((unsigned int)size <= 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700333 return -EINVAL;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800334 size = (bufferSize >= size)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700335
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400336 int err = 0;
337 if(useFbMem) {
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800338 err = gralloc_alloc_framebuffer(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400339 } else {
340 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700341 grallocFormat, w, h);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400342 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700343
344 if (err < 0) {
345 return err;
346 }
347
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700348 *pStride = alignedw;
349 return 0;
350}
351
352int gpu_context_t::free_impl(private_handle_t const* hnd) {
353 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400354 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700355 const unsigned int bufferSize = m->finfo.line_length * m->info.yres;
356 unsigned int index = (unsigned int) ((hnd->base - m->framebuffer->base)
357 / bufferSize);
Shalaj Jaina70b4352014-06-15 13:47:47 -0700358 m->bufferMask &= (uint32_t)~(1LU<<index);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400359 } else {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800360
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400361 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
362 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700363 int err = memalloc->free_buffer((void*)hnd->base, hnd->size,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400364 hnd->offset, hnd->fd);
365 if(err)
366 return err;
367 // free the metadata space
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700368 unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400369 err = memalloc->free_buffer((void*)hnd->base_metadata,
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800370 size, hnd->offset_metadata,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400371 hnd->fd_metadata);
372 if (err)
373 return err;
374 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700375 delete hnd;
376 return 0;
377}
378
379int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700380 int usage, buffer_handle_t* pHandle,
381 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700382{
383 if (!dev) {
384 return -EINVAL;
385 }
386 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
387 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
388}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700389int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
390 int format, int usage,
391 buffer_handle_t* pHandle, int* pStride,
392 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700393{
394 if (!dev) {
395 return -EINVAL;
396 }
397 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
398 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
399}
400
401
402int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700403 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700404{
405 if (private_handle_t::validate(handle) < 0)
406 return -EINVAL;
407
408 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
409 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
410 return gpu->free_impl(hnd);
411}
412
413/*****************************************************************************/
414
415int gpu_context_t::gralloc_close(struct hw_device_t *dev)
416{
417 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
418 if (ctx) {
419 /* TODO: keep a list of all buffer_handle_t created, and free them
420 * all here.
421 */
422 delete ctx;
423 }
424 return 0;
425}
426