blob: a0d7d8d38ef476c0591e46f493478513b0d6740c [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;
56 size = roundUpToPageSize(size);
57 alloc_data data;
58 data.offset = 0;
59 data.fd = -1;
60 data.base = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070061 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
62 data.align = 8192;
63 else
64 data.align = getpagesize();
Praveen Chavan4e931c12012-11-28 19:43:17 -080065
Praveena Pachipulusu95e16d42014-06-03 18:23:16 +053066 if ((usage & GRALLOC_USAGE_PROTECTED) &&
67 (usage & GRALLOC_USAGE_PRIVATE_MM_HEAP)) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -080068 if (usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
69 /* The alignment here reflects qsee mmu V7L/V8L requirement */
70 data.align = SZ_2M;
71 } else {
72 data.align = SECURE_ALIGN;
73 }
Praveen Chavan4e931c12012-11-28 19:43:17 -080074 size = ALIGN(size, data.align);
75 }
Naseer Ahmed84786722013-06-14 16:29:59 -040076
Praveen Chavan4e931c12012-11-28 19:43:17 -080077 data.size = size;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -080078 data.pHandle = (uintptr_t) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070079 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -070080
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080081 if (!err) {
82 /* allocate memory for enhancement data */
83 alloc_data eData;
84 eData.fd = -1;
85 eData.base = 0;
86 eData.offset = 0;
87 eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
88 eData.pHandle = data.pHandle;
89 eData.align = getpagesize();
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -050090 int eDataUsage = 0;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080091 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
92 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
93 strerror(-eDataErr));
Iliyan Malchev202a77d2012-06-11 14:41:12 -070094
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080095 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
96 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -080097 }
Arun Kumar K.R48a57552015-09-03 11:37:35 -070098 if (usage & GRALLOC_USAGE_PRIVATE_INTERNAL_ONLY) {
99 flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
100 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400101
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400102 ColorSpace_t colorSpace = ITU_R_601;
103 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601;
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400104 if (bufferType == BUFFER_TYPE_VIDEO) {
105 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
Naseer Ahmed22fa2d32013-08-14 12:59:24 -0400106 // Per the camera spec ITU 709 format should be set only for
107 // video encoding.
108 // It should be set to ITU 601 full range format for any other
109 // camera buffer
110 //
111 if (usage & GRALLOC_USAGE_HW_CAMERA_MASK) {
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400112 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400113 flags |= private_handle_t::PRIV_FLAGS_ITU_R_709;
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400114 colorSpace = ITU_R_709;
115 } else {
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400116 flags |= private_handle_t::PRIV_FLAGS_ITU_R_601_FR;
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400117 colorSpace = ITU_R_601_FR;
118 }
Naseer Ahmed22fa2d32013-08-14 12:59:24 -0400119 }
Naseer Ahmedfc940ef2013-04-29 15:47:47 -0400120 }
121 }
122
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800123 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
124 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
125 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400126
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800127 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
128 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
129 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400130
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800131 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
132 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
133 }
134
Naseer Ahmede41ad9c2013-04-05 17:59:28 -0400135 if (usage & GRALLOC_USAGE_HW_COMPOSER) {
136 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
137 }
138
Shuzhen Wang46ca2262013-04-10 23:02:22 -0700139 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
140 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
141 }
142
Ramkumar Radhakrishnanba713382013-08-30 18:41:07 -0700143 if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
144 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
145 }
146
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700147 if(isMacroTileEnabled(format, usage)) {
148 flags |= private_handle_t::PRIV_FLAGS_TILE_RENDERED;
149 }
150
Sushil Chauhan7807d192015-08-13 10:10:48 -0700151 if (isUBwcEnabled(format, usage)) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800152 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
153 }
154
Ramkumar Radhakrishnan9d20b392013-11-15 19:32:47 -0800155 if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
156 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
157 }
158
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800159 if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER |
160 GRALLOC_USAGE_HW_CAMERA_WRITE |
161 GRALLOC_USAGE_HW_RENDER |
162 GRALLOC_USAGE_HW_FB)) {
163 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
164 }
165
166 if(false == data.uncached) {
167 flags |= private_handle_t::PRIV_FLAGS_CACHED;
168 }
169
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700170 flags |= data.allocType;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700171 uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800172 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
173 bufferType, format, width, height, eData.fd, eData.offset,
174 eBaseAddr);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700175
176 hnd->offset = data.offset;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700177 hnd->base = (uint64_t)(data.base) + data.offset;
Naseer Ahmeda163b732013-02-12 14:53:33 -0500178 hnd->gpuaddr = 0;
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400179 setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);
Naseer Ahmeda163b732013-02-12 14:53:33 -0500180
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700181 *pHandle = hnd;
182 }
183
184 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800185
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700186 return err;
187}
188
189void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700190 int *bufferType)
191{
192 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700193
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700194 if (isUncompressedRgbFormat(inputFormat) == TRUE) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700195 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700196 *bufferType = BUFFER_TYPE_UI;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700197 }
198}
199
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800200int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400201 buffer_handle_t* pHandle)
202{
203 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
204
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500205 // This allocation will only happen when gralloc is in fb mode
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400206
207 if (m->framebuffer == NULL) {
208 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
209 return -EINVAL;
210 }
211
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700212 const unsigned int bufferMask = m->bufferMask;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400213 const uint32_t numBuffers = m->numBuffers;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700214 unsigned int bufferSize = m->finfo.line_length * m->info.yres;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400215
216 //adreno needs FB size to be page aligned
217 bufferSize = roundUpToPageSize(bufferSize);
218
219 if (numBuffers == 1) {
220 // If we have only one buffer, we never use page-flipping. Instead,
221 // we return a regular buffer which will be memcpy'ed to the main
222 // screen when post is called.
223 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
224 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
225 m->fbFormat, m->info.xres, m->info.yres);
226 }
227
228 if (bufferMask >= ((1LU<<numBuffers)-1)) {
229 // We ran out of buffers.
230 return -ENOMEM;
231 }
232
233 // create a "fake" handle for it
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700234 uint64_t vaddr = uint64_t(m->framebuffer->base);
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700235 // As GPU needs ION FD, the private handle is created
236 // using ION fd and ION flags are set
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400237 private_handle_t* hnd = new private_handle_t(
238 dup(m->framebuffer->fd), bufferSize,
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700239 private_handle_t::PRIV_FLAGS_USES_ION |
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400240 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
241 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
242 m->info.yres);
243
244 // find a free slot
245 for (uint32_t i=0 ; i<numBuffers ; i++) {
246 if ((bufferMask & (1LU<<i)) == 0) {
Shalaj Jaina70b4352014-06-15 13:47:47 -0700247 m->bufferMask |= (uint32_t)(1LU<<i);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400248 break;
249 }
250 vaddr += bufferSize;
251 }
252 hnd->base = vaddr;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700253 hnd->offset = (unsigned int)(vaddr - m->framebuffer->base);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400254 *pHandle = hnd;
255 return 0;
256}
257
258
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800259int gpu_context_t::gralloc_alloc_framebuffer(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400260 buffer_handle_t* pHandle)
261{
262 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
263 pthread_mutex_lock(&m->lock);
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800264 int err = gralloc_alloc_framebuffer_locked(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400265 pthread_mutex_unlock(&m->lock);
266 return err;
267}
268
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700269int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700270 buffer_handle_t* pHandle, int* pStride,
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700271 unsigned int bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700272 if (!pHandle || !pStride)
273 return -EINVAL;
274
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700275 unsigned int size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700276 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400277 int grallocFormat = format;
278 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400279
Saurabh Shahaedf2362012-09-05 11:30:59 -0700280 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
281 //the usage bits, gralloc assigns a format.
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400282 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
283 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Sushil Chauhan32333292015-02-04 18:56:32 -0800284 if (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC)
285 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
286 else if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER)
Naseer Ahmedf2feca92013-08-16 14:48:59 -0400287 grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700288 else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK)
289 == GRALLOC_USAGE_HW_CAMERA_ZSL)
290 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL
Saurabh Shahaedf2362012-09-05 11:30:59 -0700291 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
292 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
Ranjith Kagathi Ananda330b21d2015-08-17 16:41:08 -0700293 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
294 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Nagesh Subba Reddy81a519f2015-08-29 18:01:49 -0700295 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21
Ranjith Kagathi Ananda330b21d2015-08-17 16:41:08 -0700296 } else {
297 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; //NV12 preview
298 }
299 } else if(usage & GRALLOC_USAGE_HW_COMPOSER)
Manoj Kumar AVMcdb4fd52013-10-25 19:40:45 -0700300 //XXX: If we still haven't set a format, default to RGBA8888
301 grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888;
Nagesh Subba Reddy81a519f2015-08-29 18:01:49 -0700302 else if(format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
303 //If no other usage flags are detected, default the
304 //flexible YUV format to NV21_ZSL
305 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL;
306 }
Baldev Sahu5f673b42013-12-05 09:49:09 +0530307 }
308
Sushil Chauhan68c793a2015-04-16 16:31:22 -0700309 bool useFbMem = false;
310 char property[PROPERTY_VALUE_MAX];
311 char isUBWC[PROPERTY_VALUE_MAX];
312 if (usage & GRALLOC_USAGE_HW_FB) {
313 if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
314 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
315 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
316 useFbMem = true;
317 } else {
Sushil Chauhanc9853b52015-09-30 18:43:11 -0700318 usage &= ~GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
Sushil Chauhan68c793a2015-04-16 16:31:22 -0700319 if (property_get("debug.gralloc.enable_fb_ubwc", isUBWC, NULL) > 0){
320 if ((!strncmp(isUBWC, "1", PROPERTY_VALUE_MAX)) ||
321 (!strncasecmp(isUBWC, "true", PROPERTY_VALUE_MAX))) {
322 // Allocate UBWC aligned framebuffer
323 usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
324 }
325 }
326 }
327 }
328
Saurabh Shahaedf2362012-09-05 11:30:59 -0700329 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700330 size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw,
331 alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700332
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700333 if ((unsigned int)size <= 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700334 return -EINVAL;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800335 size = (bufferSize >= size)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700336
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400337 int err = 0;
338 if(useFbMem) {
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800339 err = gralloc_alloc_framebuffer(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400340 } else {
341 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
342 grallocFormat, alignedw, alignedh);
343 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700344
345 if (err < 0) {
346 return err;
347 }
348
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700349 *pStride = alignedw;
350 return 0;
351}
352
353int gpu_context_t::free_impl(private_handle_t const* hnd) {
354 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400355 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700356 const unsigned int bufferSize = m->finfo.line_length * m->info.yres;
357 unsigned int index = (unsigned int) ((hnd->base - m->framebuffer->base)
358 / bufferSize);
Shalaj Jaina70b4352014-06-15 13:47:47 -0700359 m->bufferMask &= (uint32_t)~(1LU<<index);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400360 } else {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800361
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400362 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
363 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700364 int err = memalloc->free_buffer((void*)hnd->base, hnd->size,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400365 hnd->offset, hnd->fd);
366 if(err)
367 return err;
368 // free the metadata space
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700369 unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400370 err = memalloc->free_buffer((void*)hnd->base_metadata,
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800371 size, hnd->offset_metadata,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400372 hnd->fd_metadata);
373 if (err)
374 return err;
375 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700376 delete hnd;
377 return 0;
378}
379
380int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700381 int usage, buffer_handle_t* pHandle,
382 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700383{
384 if (!dev) {
385 return -EINVAL;
386 }
387 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
388 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
389}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700390int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
391 int format, int usage,
392 buffer_handle_t* pHandle, int* pStride,
393 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700394{
395 if (!dev) {
396 return -EINVAL;
397 }
398 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
399 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
400}
401
402
403int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700404 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700405{
406 if (private_handle_t::validate(handle) < 0)
407 return -EINVAL;
408
409 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
410 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
411 return gpu->free_impl(hnd);
412}
413
414/*****************************************************************************/
415
416int gpu_context_t::gralloc_close(struct hw_device_t *dev)
417{
418 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
419 if (ctx) {
420 /* TODO: keep a list of all buffer_handle_t created, and free them
421 * all here.
422 */
423 delete ctx;
424 }
425 return 0;
426}
427