blob: a6f787448a031a5322677798eac3e3b1ce2b6033 [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
Shalaj Jain1f9725a2015-03-04 17:53:49 -080033#define SZ_2M 0x200000
Praveen Chavan4e931c12012-11-28 19:43:17 -080034#define SZ_1M 0x100000
Shalaj Jain1f9725a2015-03-04 17:53:49 -080035#define SZ_4K 0x1000
36
37#ifdef ION_FLAG_CP_PIXEL
38#define SECURE_ALIGN SZ_4K
39#else
40#define SECURE_ALIGN SZ_1M
41#endif
Praveen Chavan4e931c12012-11-28 19:43:17 -080042
Iliyan Malchev202a77d2012-06-11 14:41:12 -070043gpu_context_t::gpu_context_t(const private_module_t* module,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070044 IAllocController* alloc_ctrl ) :
Iliyan Malchev202a77d2012-06-11 14:41:12 -070045 mAllocCtrl(alloc_ctrl)
46{
47 // Zero out the alloc_device_t
48 memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t));
49
Iliyan Malchev202a77d2012-06-11 14:41:12 -070050 // Initialize the procs
51 common.tag = HARDWARE_DEVICE_TAG;
52 common.version = 0;
53 common.module = const_cast<hw_module_t*>(&module->base.common);
54 common.close = gralloc_close;
55 alloc = gralloc_alloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070056 free = gralloc_free;
57
58}
59
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -070060int gpu_context_t::gralloc_alloc_buffer(unsigned int size, int usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -070061 buffer_handle_t* pHandle, int bufferType,
62 int format, int width, int height)
63{
64 int err = 0;
65 int flags = 0;
66 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
Praveena Pachipulusu95e16d42014-06-03 18:23:16 +053076 if ((usage & GRALLOC_USAGE_PROTECTED) &&
77 (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -080078 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
79 /* 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 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400108
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400109 ColorSpace_t colorSpace = ITU_R_601;
110 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601;
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400111 if (bufferType == BUFFER_TYPE_VIDEO) {
112 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
Naseer Ahmed22fa2d32013-08-14 12:59:24 -0400113 // Per the camera spec ITU 709 format should be set only for
114 // video encoding.
115 // It should be set to ITU 601 full range format for any other
116 // camera buffer
117 //
118 if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400119 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400120 flags |= private_handle_t::PRIV_FLAGS_ITU_R_709;
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400121 colorSpace = ITU_R_709;
122 } else {
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400123 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR;
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400124 colorSpace = ITU_R_601_FR;
125 }
Naseer Ahmed22fa2d32013-08-14 12:59:24 -0400126 }
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400127 }
128 }
129
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800130 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
131 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
132 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400133
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800134 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
135 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
136 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400137
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800138 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
139 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
140 }
141
Naseer Ahmede41ad9c2013-04-05 17:59:28 -0400142 if (usage & GRALLOC_USAGE_HW_COMPOSER) {
143 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
144 }
145
Shuzhen Wang46ca2262013-04-10 23:02:22 -0700146 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
147 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
148 }
149
Ramkumar Radhakrishnanba713382013-08-30 18:41:07 -0700150 if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
151 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
152 }
153
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700154 if(isMacroTileEnabled(format, usage)) {
155 flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED;
156 }
157
Sushil Chauhan81594f62015-01-26 16:00:51 -0800158 if (AdrenoMemInfo::getInstance().isUBWCSupportedByGPU(format) &&
159 isUBwcEnabled(format, usage)) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800160 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
161 }
162
Ramkumar Radhakrishnan9d20b392013-11-15 19:32:47 -0800163 if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
164 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
165 }
166
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800167 if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER |
168 GRALLOC_USAGE_HW_CAMERA_WRITE |
169 GRALLOC_USAGE_HW_RENDER |
170 GRALLOC_USAGE_HW_FB)) {
171 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
172 }
173
174 if(false == data.uncached) {
175 flags |= private_handle_t::PRIV_FLAGS_CACHED;
176 }
177
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700178 flags |= data.allocType;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700179 uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800180 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
181 bufferType, format, width, height, eData.fd, eData.offset,
182 eBaseAddr);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700183
184 hnd->offset = data.offset;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700185 hnd->base = (uint64_t)(data.base) + data.offset;
Naseer Ahmeda163b732013-02-12 14:53:33 -0500186 hnd->gpuaddr = 0;
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400187 setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);
Naseer Ahmeda163b732013-02-12 14:53:33 -0500188
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700189 *pHandle = hnd;
190 }
191
192 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800193
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700194 return err;
195}
196
197void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700198 int *bufferType)
199{
200 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700201
Jesse Hallfbe96d22013-09-20 01:39:43 -0700202 if (inputFormat <= HAL_PIXEL_FORMAT_sRGB_X_8888) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700203 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700204 *bufferType = BUFFER_TYPE_UI;
205 } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
206 (inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700207 *bufferType = BUFFER_TYPE_UI;
208 }
209}
210
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800211int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400212 buffer_handle_t* pHandle)
213{
214 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
215
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500216 // This allocation will only happen when gralloc is in fb mode
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400217
218 if (m->framebuffer == NULL) {
219 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
220 return -EINVAL;
221 }
222
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700223 const unsigned int bufferMask = m->bufferMask;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400224 const uint32_t numBuffers = m->numBuffers;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700225 unsigned int bufferSize = m->finfo.line_length * m->info.yres;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400226
227 //adreno needs FB size to be page aligned
228 bufferSize = roundUpToPageSize(bufferSize);
229
230 if (numBuffers == 1) {
231 // If we have only one buffer, we never use page-flipping. Instead,
232 // we return a regular buffer which will be memcpy'ed to the main
233 // screen when post is called.
234 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
235 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
236 m->fbFormat, m->info.xres, m->info.yres);
237 }
238
239 if (bufferMask >= ((1LU<<numBuffers)-1)) {
240 // We ran out of buffers.
241 return -ENOMEM;
242 }
243
244 // create a "fake" handle for it
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700245 uint64_t vaddr = uint64_t(m->framebuffer->base);
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700246 // As GPU needs ION FD, the private handle is created
247 // using ION fd and ION flags are set
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400248 private_handle_t* hnd = new private_handle_t(
249 dup(m->framebuffer->fd), bufferSize,
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700250 private_handle_t::PRIV_FLAGS_USES_ION |
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400251 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
252 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
253 m->info.yres);
254
255 // find a free slot
256 for (uint32_t i=0 ; i<numBuffers ; i++) {
257 if ((bufferMask & (1LU<<i)) == 0) {
Shalaj Jaina70b4352014-06-15 13:47:47 -0700258 m->bufferMask |= (uint32_t)(1LU<<i);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400259 break;
260 }
261 vaddr += bufferSize;
262 }
263 hnd->base = vaddr;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700264 hnd->offset = (unsigned int)(vaddr - m->framebuffer->base);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400265 *pHandle = hnd;
266 return 0;
267}
268
269
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800270int gpu_context_t::gralloc_alloc_framebuffer(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400271 buffer_handle_t* pHandle)
272{
273 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
274 pthread_mutex_lock(&m->lock);
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800275 int err = gralloc_alloc_framebuffer_locked(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400276 pthread_mutex_unlock(&m->lock);
277 return err;
278}
279
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700280int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700281 buffer_handle_t* pHandle, int* pStride,
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700282 unsigned int bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700283 if (!pHandle || !pStride)
284 return -EINVAL;
285
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700286 unsigned int size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700287 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400288 int grallocFormat = format;
289 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400290
Saurabh Shahaedf2362012-09-05 11:30:59 -0700291 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
292 //the usage bits, gralloc assigns a format.
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400293 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
294 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Sushil Chauhan32333292015-02-04 18:56:32 -0800295 if (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC)
296 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
297 else if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
Naseer Ahmedf2feca92013-08-16 14:48:59 -0400298 grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700299 else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK)
300 == GRALLOC_USAGE_HW_CAMERA_ZSL)
301 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL
Saurabh Shahaedf2362012-09-05 11:30:59 -0700302 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
303 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
304 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE)
305 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
Manoj Kumar AVMcdb4fd52013-10-25 19:40:45 -0700306 else if(usage & GRALLOC_USAGE_HW_COMPOSER)
307 //XXX: If we still haven't set a format, default to RGBA8888
308 grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888;
Baldev Sahu5f673b42013-12-05 09:49:09 +0530309 }
310
Saurabh Shahaedf2362012-09-05 11:30:59 -0700311 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700312 size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw,
313 alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700314
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700315 if ((unsigned int)size <= 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700316 return -EINVAL;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800317 size = (bufferSize >= size)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700318
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400319 bool useFbMem = false;
320 char property[PROPERTY_VALUE_MAX];
321 if((usage & GRALLOC_USAGE_HW_FB) &&
322 (property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
323 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
324 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
325 useFbMem = true;
326 }
327
328 int err = 0;
329 if(useFbMem) {
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800330 err = gralloc_alloc_framebuffer(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400331 } else {
332 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
333 grallocFormat, alignedw, alignedh);
334 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700335
336 if (err < 0) {
337 return err;
338 }
339
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700340 *pStride = alignedw;
341 return 0;
342}
343
344int gpu_context_t::free_impl(private_handle_t const* hnd) {
345 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400346 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700347 const unsigned int bufferSize = m->finfo.line_length * m->info.yres;
348 unsigned int index = (unsigned int) ((hnd->base - m->framebuffer->base)
349 / bufferSize);
Shalaj Jaina70b4352014-06-15 13:47:47 -0700350 m->bufferMask &= (uint32_t)~(1LU<<index);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400351 } else {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800352
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400353 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
354 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700355 int err = memalloc->free_buffer((void*)hnd->base, hnd->size,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400356 hnd->offset, hnd->fd);
357 if(err)
358 return err;
359 // free the metadata space
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700360 unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400361 err = memalloc->free_buffer((void*)hnd->base_metadata,
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800362 size, hnd->offset_metadata,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400363 hnd->fd_metadata);
364 if (err)
365 return err;
366 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700367 delete hnd;
368 return 0;
369}
370
371int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700372 int usage, buffer_handle_t* pHandle,
373 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700374{
375 if (!dev) {
376 return -EINVAL;
377 }
378 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
379 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
380}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700381int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
382 int format, int usage,
383 buffer_handle_t* pHandle, int* pStride,
384 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700385{
386 if (!dev) {
387 return -EINVAL;
388 }
389 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
390 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
391}
392
393
394int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700395 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700396{
397 if (private_handle_t::validate(handle) < 0)
398 return -EINVAL;
399
400 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
401 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
402 return gpu->free_impl(hnd);
403}
404
405/*****************************************************************************/
406
407int gpu_context_t::gralloc_close(struct hw_device_t *dev)
408{
409 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
410 if (ctx) {
411 /* TODO: keep a list of all buffer_handle_t created, and free them
412 * all here.
413 */
414 delete ctx;
415 }
416 return 0;
417}
418