blob: 641712c8cee1a1b341050a5a5387cf7cd24756d8 [file] [log] [blame]
Iliyan Malchev202a77d2012-06-11 14:41:12 -07001/*
2 * Copyright (C) 2010 The Android Open Source Project
Saurabh Shahc5b2b702016-10-24 17:16:01 -07003 * Copyright (c) 2011-2014,2017 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>
Saurabh Shah7d476ed2016-06-27 16:40:58 -070023#include <linux/msm_ion.h>
24#ifdef COMPILE_DRM
Saurabh Shah7d476ed2016-06-27 16:40:58 -070025#include <drm_master.h>
26#endif
27#include <qdMetaData.h>
28#include <qd_utils.h>
29
30#include <algorithm>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070031
Iliyan Malchev202a77d2012-06-11 14:41:12 -070032#include "gr.h"
33#include "gpu.h"
34#include "memalloc.h"
35#include "alloc_controller.h"
Saurabh Shah7d476ed2016-06-27 16:40:58 -070036
37#ifdef COMPILE_DRM
Saurabh Shaha228f122017-01-23 15:55:25 -080038using namespace drm_utils;
Saurabh Shah7d476ed2016-06-27 16:40:58 -070039#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -070040
41using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070042
43gpu_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;
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -070066 int alignedw = 0;
67 int alignedh = 0;
68
69 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
70 height,
71 format,
72 usage,
73 alignedw,
74 alignedh);
75
Iliyan Malchev202a77d2012-06-11 14:41:12 -070076 size = roundUpToPageSize(size);
77 alloc_data data;
78 data.offset = 0;
79 data.fd = -1;
80 data.base = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070081 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
82 data.align = 8192;
83 else
84 data.align = getpagesize();
Praveen Chavan4e931c12012-11-28 19:43:17 -080085
Naseer Ahmedde758fd2016-05-03 15:10:23 -040086 if (usage & GRALLOC_USAGE_PROTECTED) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -070087 if ((usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) ||
88 (usage & GRALLOC_USAGE_HW_CAMERA_MASK)) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -080089 /* The alignment here reflects qsee mmu V7L/V8L requirement */
90 data.align = SZ_2M;
91 } else {
92 data.align = SECURE_ALIGN;
93 }
Praveen Chavan4e931c12012-11-28 19:43:17 -080094 size = ALIGN(size, data.align);
95 }
Naseer Ahmed84786722013-06-14 16:29:59 -040096
Praveen Chavan4e931c12012-11-28 19:43:17 -080097 data.size = size;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -080098 data.pHandle = (uintptr_t) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -070099 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700100
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800101 if (!err) {
102 /* allocate memory for enhancement data */
103 alloc_data eData;
104 eData.fd = -1;
105 eData.base = 0;
106 eData.offset = 0;
107 eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
108 eData.pHandle = data.pHandle;
109 eData.align = getpagesize();
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500110 int eDataUsage = 0;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800111 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
112 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
113 strerror(-eDataErr));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700114
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800115 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
116 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800117 }
Saurabh Shahbddb9522015-09-30 13:31:55 -0700118
Arun Kumar K.R48a57552015-09-03 11:37:35 -0700119 if (usage & GRALLOC_USAGE_PRIVATE_INTERNAL_ONLY) {
120 flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
121 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400122
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
Sushil Chauhan7807d192015-08-13 10:10:48 -0700147 if (isUBwcEnabled(format, usage)) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800148 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
149 }
150
Ramkumar Radhakrishnan9d20b392013-11-15 19:32:47 -0800151 if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
152 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
153 }
154
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800155 if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER |
156 GRALLOC_USAGE_HW_CAMERA_WRITE |
157 GRALLOC_USAGE_HW_RENDER |
158 GRALLOC_USAGE_HW_FB)) {
159 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
160 }
161
Surajit Podder074314d2015-06-25 01:43:20 +0530162 if(usage & GRALLOC_USAGE_HW_COMPOSER) {
163 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
164 }
165
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800166 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,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700173 bufferType, format, alignedw, alignedh,
174 eData.fd, eData.offset, eBaseAddr, width, height);
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;
Pullakavi Srinivas46204e32016-07-22 12:44:46 +0530179 ColorSpace_t colorSpace = ITU_R_601;
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400180 setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);
Naseer Ahmeda163b732013-02-12 14:53:33 -0500181
Saurabh Shah7d476ed2016-06-27 16:40:58 -0700182#ifdef COMPILE_DRM
183 if (qdutils::getDriverType() == qdutils::DriverType::DRM &&
184 usage & GRALLOC_USAGE_HW_COMPOSER) {
185 DRMBuffer buf = {};
186 int ret = getPlaneStrideOffset(hnd, buf.stride, buf.offset,
187 &buf.num_planes);
188 if (ret < 0) {
189 ALOGE("%s failed", __FUNCTION__);
190 return ret;
191 }
192
193 buf.fd = hnd->fd;
194 buf.width = hnd->width;
195 buf.height = hnd->height;
196 getDRMFormat(hnd->format, flags, &buf.drm_format,
197 &buf.drm_format_modifier);
198
199 DRMMaster *master = nullptr;
200 ret = DRMMaster::GetInstance(&master);
201 if (ret < 0) {
202 ALOGE("%s Failed to acquire DRMMaster instance", __FUNCTION__);
203 return ret;
204 }
205
206 ret = master->CreateFbId(buf, &hnd->gem_handle, &hnd->fb_id);
207 if (ret < 0) {
208 ALOGE("%s: CreateFbId failed. width %d, height %d, " \
209 "format: %s, stride %u, error %d", __FUNCTION__,
210 buf.width, buf.height,
211 qdutils::GetHALPixelFormatString(hnd->format),
212 buf.stride[0], errno);
213 return ret;
214 }
215 }
216#endif
217
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700218 *pHandle = hnd;
219 }
220
221 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
222 return err;
223}
224
225void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700226 int *bufferType)
227{
228 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700229
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700230 if (isUncompressedRgbFormat(inputFormat) == TRUE) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700231 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700232 *bufferType = BUFFER_TYPE_UI;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700233 }
234}
235
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800236int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400237 buffer_handle_t* pHandle)
238{
239 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
240
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500241 // This allocation will only happen when gralloc is in fb mode
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400242
243 if (m->framebuffer == NULL) {
244 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
245 return -EINVAL;
246 }
247
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700248 const unsigned int bufferMask = m->bufferMask;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400249 const uint32_t numBuffers = m->numBuffers;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700250 unsigned int bufferSize = m->finfo.line_length * m->info.yres;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400251
252 //adreno needs FB size to be page aligned
253 bufferSize = roundUpToPageSize(bufferSize);
254
255 if (numBuffers == 1) {
256 // If we have only one buffer, we never use page-flipping. Instead,
257 // we return a regular buffer which will be memcpy'ed to the main
258 // screen when post is called.
259 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
260 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
261 m->fbFormat, m->info.xres, m->info.yres);
262 }
263
264 if (bufferMask >= ((1LU<<numBuffers)-1)) {
265 // We ran out of buffers.
266 return -ENOMEM;
267 }
268
269 // create a "fake" handle for it
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700270 uint64_t vaddr = uint64_t(m->framebuffer->base);
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700271 // As GPU needs ION FD, the private handle is created
272 // using ION fd and ION flags are set
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400273 private_handle_t* hnd = new private_handle_t(
274 dup(m->framebuffer->fd), bufferSize,
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700275 private_handle_t::PRIV_FLAGS_USES_ION |
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400276 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
277 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
278 m->info.yres);
279
280 // find a free slot
281 for (uint32_t i=0 ; i<numBuffers ; i++) {
282 if ((bufferMask & (1LU<<i)) == 0) {
Shalaj Jaina70b4352014-06-15 13:47:47 -0700283 m->bufferMask |= (uint32_t)(1LU<<i);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400284 break;
285 }
286 vaddr += bufferSize;
287 }
288 hnd->base = vaddr;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700289 hnd->offset = (unsigned int)(vaddr - m->framebuffer->base);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400290 *pHandle = hnd;
291 return 0;
292}
293
294
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800295int gpu_context_t::gralloc_alloc_framebuffer(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400296 buffer_handle_t* pHandle)
297{
298 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
299 pthread_mutex_lock(&m->lock);
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800300 int err = gralloc_alloc_framebuffer_locked(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400301 pthread_mutex_unlock(&m->lock);
302 return err;
303}
304
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700305int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700306 buffer_handle_t* pHandle, int* pStride,
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700307 unsigned int bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700308 if (!pHandle || !pStride)
309 return -EINVAL;
310
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700311 unsigned int size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700312 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400313 int grallocFormat = format;
314 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400315
Saurabh Shahaedf2362012-09-05 11:30:59 -0700316 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
317 //the usage bits, gralloc assigns a format.
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400318 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
319 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Sushil Chauhan32333292015-02-04 18:56:32 -0800320 if (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC)
321 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
Jeykumar Sankaran9bc1a782015-12-14 18:36:27 -0800322 else if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
323 if(MDPCapabilityInfo::getInstance().isWBUBWCSupportedByMDP() &&
Ramakant Singhf8d1c402017-01-17 19:01:21 +0530324 !IAllocController::getInstance()->isDisableUBWCForEncoder() &&
Jeykumar Sankaran9bc1a782015-12-14 18:36:27 -0800325 usage & GRALLOC_USAGE_HW_COMPOSER)
326 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
327 else
328 grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
329 } else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK)
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700330 == GRALLOC_USAGE_HW_CAMERA_ZSL)
331 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL
Saurabh Shahaedf2362012-09-05 11:30:59 -0700332 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
333 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
Ranjith Kagathi Ananda330b21d2015-08-17 16:41:08 -0700334 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
335 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Nagesh Subba Reddy81a519f2015-08-29 18:01:49 -0700336 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21
Ranjith Kagathi Ananda330b21d2015-08-17 16:41:08 -0700337 } else {
338 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; //NV12 preview
339 }
340 } else if(usage & GRALLOC_USAGE_HW_COMPOSER)
Manoj Kumar AVMcdb4fd52013-10-25 19:40:45 -0700341 //XXX: If we still haven't set a format, default to RGBA8888
342 grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888;
Nagesh Subba Reddy81a519f2015-08-29 18:01:49 -0700343 else if(format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
344 //If no other usage flags are detected, default the
345 //flexible YUV format to NV21_ZSL
346 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL;
347 }
Baldev Sahu5f673b42013-12-05 09:49:09 +0530348 }
349
Sushil Chauhan68c793a2015-04-16 16:31:22 -0700350 bool useFbMem = false;
351 char property[PROPERTY_VALUE_MAX];
352 char isUBWC[PROPERTY_VALUE_MAX];
353 if (usage & GRALLOC_USAGE_HW_FB) {
354 if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
355 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
356 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
357 useFbMem = true;
358 } else {
Sushil Chauhanc9853b52015-09-30 18:43:11 -0700359 usage &= ~GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
Sushil Chauhan68c793a2015-04-16 16:31:22 -0700360 if (property_get("debug.gralloc.enable_fb_ubwc", isUBWC, NULL) > 0){
361 if ((!strncmp(isUBWC, "1", PROPERTY_VALUE_MAX)) ||
362 (!strncasecmp(isUBWC, "true", PROPERTY_VALUE_MAX))) {
363 // Allocate UBWC aligned framebuffer
364 usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
365 }
366 }
367 }
368 }
369
Saurabh Shahaedf2362012-09-05 11:30:59 -0700370 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700371 size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw,
372 alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700373
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700374 if ((unsigned int)size <= 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700375 return -EINVAL;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800376 size = (bufferSize >= size)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700377
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400378 int err = 0;
379 if(useFbMem) {
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800380 err = gralloc_alloc_framebuffer(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400381 } else {
382 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700383 grallocFormat, w, h);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400384 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700385
386 if (err < 0) {
387 return err;
388 }
389
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700390 *pStride = alignedw;
391 return 0;
392}
393
394int gpu_context_t::free_impl(private_handle_t const* hnd) {
395 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400396 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700397 const unsigned int bufferSize = m->finfo.line_length * m->info.yres;
398 unsigned int index = (unsigned int) ((hnd->base - m->framebuffer->base)
399 / bufferSize);
Shalaj Jaina70b4352014-06-15 13:47:47 -0700400 m->bufferMask &= (uint32_t)~(1LU<<index);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400401 } else {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800402
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400403 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
404 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700405 int err = memalloc->free_buffer((void*)hnd->base, hnd->size,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400406 hnd->offset, hnd->fd);
407 if(err)
408 return err;
409 // free the metadata space
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700410 unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400411 err = memalloc->free_buffer((void*)hnd->base_metadata,
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800412 size, hnd->offset_metadata,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400413 hnd->fd_metadata);
414 if (err)
415 return err;
416 }
Saurabh Shah7d476ed2016-06-27 16:40:58 -0700417
418#ifdef COMPILE_DRM
419 if (hnd->fb_id) {
420 DRMMaster *master = nullptr;
421 int ret = DRMMaster::GetInstance(&master);
422 if (ret < 0) {
423 ALOGE("%s Failed to acquire DRMMaster instance", __FUNCTION__);
424 return ret;
425 }
426 ret = master->RemoveFbId(hnd->gem_handle, hnd->fb_id);
427 if (ret < 0) {
428 ALOGE("%s: Removing fb_id %d failed with error %d", __FUNCTION__,
429 hnd->fb_id, errno);
430 }
431 }
432#endif
433
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700434 delete hnd;
435 return 0;
436}
437
438int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700439 int usage, buffer_handle_t* pHandle,
440 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700441{
442 if (!dev) {
443 return -EINVAL;
444 }
445 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
446 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
447}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700448int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
449 int format, int usage,
450 buffer_handle_t* pHandle, int* pStride,
451 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700452{
453 if (!dev) {
454 return -EINVAL;
455 }
456 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
457 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
458}
459
460
461int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700462 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700463{
464 if (private_handle_t::validate(handle) < 0)
465 return -EINVAL;
466
467 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
468 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
469 return gpu->free_impl(hnd);
470}
471
472/*****************************************************************************/
473
474int gpu_context_t::gralloc_close(struct hw_device_t *dev)
475{
476 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
477 if (ctx) {
478 /* TODO: keep a list of all buffer_handle_t created, and free them
479 * all here.
480 */
481 delete ctx;
482 }
483 return 0;
484}
485