blob: 848c741ef7ea0bb1d9666713131e9bd15dfcbab0 [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
25#include <drm/drm_fourcc.h>
26#include <drm_master.h>
27#endif
28#include <qdMetaData.h>
29#include <qd_utils.h>
30
31#include <algorithm>
Iliyan Malchev202a77d2012-06-11 14:41:12 -070032
Iliyan Malchev202a77d2012-06-11 14:41:12 -070033#include "gr.h"
34#include "gpu.h"
35#include "memalloc.h"
36#include "alloc_controller.h"
Saurabh Shah7d476ed2016-06-27 16:40:58 -070037
38#ifdef COMPILE_DRM
39#ifndef DRM_FORMAT_MOD_QCOM_COMPRESSED
40#define DRM_FORMAT_MOD_QCOM_COMPRESSED fourcc_mod_code(QCOM, 1)
41#endif
42#endif
Iliyan Malchev202a77d2012-06-11 14:41:12 -070043
44using namespace gralloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -070045
Saurabh Shah7d476ed2016-06-27 16:40:58 -070046#ifdef COMPILE_DRM
47using namespace drm_utils;
48
49static int getPlaneStrideOffset(private_handle_t *hnd, uint32_t *stride,
50 uint32_t *offset, uint32_t *num_planes) {
51 struct android_ycbcr yuvInfo = {};
52 *num_planes = 1;
53
54 switch (hnd->format) {
55 case HAL_PIXEL_FORMAT_RGB_565:
56 case HAL_PIXEL_FORMAT_BGR_565:
57 case HAL_PIXEL_FORMAT_RGBA_5551:
58 case HAL_PIXEL_FORMAT_RGBA_4444:
59 stride[0] = hnd->width * 2;
60 break;
61 case HAL_PIXEL_FORMAT_RGB_888:
62 stride[0] = hnd->width * 3;
63 break;
64 case HAL_PIXEL_FORMAT_RGBA_8888:
65 case HAL_PIXEL_FORMAT_BGRA_8888:
66 case HAL_PIXEL_FORMAT_RGBX_8888:
67 case HAL_PIXEL_FORMAT_BGRX_8888:
68 case HAL_PIXEL_FORMAT_RGBA_1010102:
69 case HAL_PIXEL_FORMAT_ARGB_2101010:
70 case HAL_PIXEL_FORMAT_RGBX_1010102:
71 case HAL_PIXEL_FORMAT_XRGB_2101010:
72 case HAL_PIXEL_FORMAT_BGRA_1010102:
73 case HAL_PIXEL_FORMAT_ABGR_2101010:
74 case HAL_PIXEL_FORMAT_BGRX_1010102:
75 case HAL_PIXEL_FORMAT_XBGR_2101010:
76 stride[0] = hnd->width * 4;
77 break;
78 }
79
80 // Format is RGB
81 if (stride[0]) {
82 return 0;
83 }
84
85 (*num_planes)++;
86 int ret = getYUVPlaneInfo(hnd, &yuvInfo);
87 if (ret < 0) {
88 ALOGE("%s failed", __FUNCTION__);
89 return ret;
90 }
91
92 stride[0] = static_cast<uint32_t>(yuvInfo.ystride);
93 offset[0] = static_cast<uint32_t>(
94 reinterpret_cast<uint64_t>(yuvInfo.y) - hnd->base);
95 stride[1] = static_cast<uint32_t>(yuvInfo.cstride);
96 switch (hnd->format) {
97 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
98 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
99 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
100 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
101 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
102 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
103 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
104 offset[1] = static_cast<uint32_t>(
105 reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
106 break;
107 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
108 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
109 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
110 offset[1] = static_cast<uint32_t>(
111 reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
112 break;
113 case HAL_PIXEL_FORMAT_YV12:
114 offset[1] = static_cast<uint32_t>(
115 reinterpret_cast<uint64_t>(yuvInfo.cr) - hnd->base);
116 stride[2] = static_cast<uint32_t>(yuvInfo.cstride);
117 offset[2] = static_cast<uint32_t>(
118 reinterpret_cast<uint64_t>(yuvInfo.cb) - hnd->base);
119 (*num_planes)++;
120 break;
121 default:
122 ALOGW("%s: Unsupported format %s", __FUNCTION__,
123 qdutils::GetHALPixelFormatString(hnd->format));
124 }
125
126 if (hnd->flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
127 std::fill(offset, offset + 4, 0);
128 }
129
130 return 0;
131}
132
133static void getDRMFormat(int hal_format, int flags, uint32_t *drm_format,
134 uint64_t *drm_format_modifier) {
135
136 if (flags & private_handle_t::PRIV_FLAGS_UBWC_ALIGNED) {
137 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
138 }
139
140 switch (hal_format) {
141 case HAL_PIXEL_FORMAT_RGBA_8888:
142 *drm_format = DRM_FORMAT_RGBA8888;
143 break;
144 case HAL_PIXEL_FORMAT_RGBA_5551:
145 *drm_format = DRM_FORMAT_RGBA5551;
146 break;
147 case HAL_PIXEL_FORMAT_RGBA_4444:
148 *drm_format = DRM_FORMAT_RGBA4444;
149 break;
150 case HAL_PIXEL_FORMAT_BGRA_8888:
151 *drm_format = DRM_FORMAT_BGRA8888;
152 break;
153 case HAL_PIXEL_FORMAT_RGBX_8888:
154 *drm_format = DRM_FORMAT_RGBX8888;
155 break;
156 case HAL_PIXEL_FORMAT_BGRX_8888:
157 *drm_format = DRM_FORMAT_BGRX8888;
158 break;
159 case HAL_PIXEL_FORMAT_RGB_888:
160 *drm_format = DRM_FORMAT_RGB888;
161 break;
162 case HAL_PIXEL_FORMAT_RGB_565:
163 *drm_format = DRM_FORMAT_RGB565;
164 break;
165 case HAL_PIXEL_FORMAT_BGR_565:
166 *drm_format = DRM_FORMAT_BGR565;
167 break;
168 case HAL_PIXEL_FORMAT_RGBA_1010102:
169 *drm_format = DRM_FORMAT_RGBA1010102;
170 break;
171 case HAL_PIXEL_FORMAT_ARGB_2101010:
172 *drm_format = DRM_FORMAT_ARGB2101010;
173 break;
174 case HAL_PIXEL_FORMAT_RGBX_1010102:
175 *drm_format = DRM_FORMAT_RGBX1010102;
176 break;
177 case HAL_PIXEL_FORMAT_XRGB_2101010:
178 *drm_format = DRM_FORMAT_XRGB2101010;
179 break;
180 case HAL_PIXEL_FORMAT_BGRA_1010102:
181 *drm_format = DRM_FORMAT_BGRA1010102;
182 break;
183 case HAL_PIXEL_FORMAT_ABGR_2101010:
184 *drm_format = DRM_FORMAT_ABGR2101010;
185 break;
186 case HAL_PIXEL_FORMAT_BGRX_1010102:
187 *drm_format = DRM_FORMAT_BGRX1010102;
188 break;
189 case HAL_PIXEL_FORMAT_XBGR_2101010:
190 *drm_format = DRM_FORMAT_XBGR2101010;
191 break;
192 case HAL_PIXEL_FORMAT_YCbCr_420_SP:
193 *drm_format = DRM_FORMAT_NV12;
194 break;
195 case HAL_PIXEL_FORMAT_NV12_ENCODEABLE:
196 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS:
197 *drm_format = DRM_FORMAT_NV12;
198 break;
199 case HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC:
200 *drm_format = DRM_FORMAT_NV12;
201 *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED;
202 break;
203 case HAL_PIXEL_FORMAT_YCrCb_420_SP:
204 *drm_format = DRM_FORMAT_NV21;
205 break;
206 case HAL_PIXEL_FORMAT_YCrCb_420_SP_VENUS:
207 *drm_format = DRM_FORMAT_NV21;
208 break;
209 case HAL_PIXEL_FORMAT_YCbCr_420_P010:
210 // TODO *drm_format = DRM_FORMAT_P010;
211 break;
212 case HAL_PIXEL_FORMAT_YCbCr_420_TP10_UBWC:
213 // TODO *drm_format = DRM_FORMAT_P010;
214 // *drm_format_modifier = DRM_FORMAT_MOD_QCOM_COMPRESSED |
215 // DRM_FORMAT_MOD_QCOM_TIGHT;
216 break;
217 case HAL_PIXEL_FORMAT_YCbCr_422_SP:
218 *drm_format = DRM_FORMAT_NV16;
219 break;
220 case HAL_PIXEL_FORMAT_YCrCb_422_SP:
221 *drm_format = DRM_FORMAT_NV61;
222 break;
223 case HAL_PIXEL_FORMAT_YV12:
224 *drm_format = DRM_FORMAT_YVU420;
225 break;
226 default:
227 ALOGW("%s: Unsupported format %s", __FUNCTION__,
228 qdutils::GetHALPixelFormatString(hal_format));
229 }
230}
231#endif
232
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700233gpu_context_t::gpu_context_t(const private_module_t* module,
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700234 IAllocController* alloc_ctrl ) :
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700235 mAllocCtrl(alloc_ctrl)
236{
237 // Zero out the alloc_device_t
238 memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t));
239
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700240 // Initialize the procs
241 common.tag = HARDWARE_DEVICE_TAG;
242 common.version = 0;
243 common.module = const_cast<hw_module_t*>(&module->base.common);
244 common.close = gralloc_close;
245 alloc = gralloc_alloc;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700246 free = gralloc_free;
247
248}
249
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700250int gpu_context_t::gralloc_alloc_buffer(unsigned int size, int usage,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700251 buffer_handle_t* pHandle, int bufferType,
252 int format, int width, int height)
253{
254 int err = 0;
255 int flags = 0;
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700256 int alignedw = 0;
257 int alignedh = 0;
258
259 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
260 height,
261 format,
262 usage,
263 alignedw,
264 alignedh);
265
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700266 size = roundUpToPageSize(size);
267 alloc_data data;
268 data.offset = 0;
269 data.fd = -1;
270 data.base = 0;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700271 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
272 data.align = 8192;
273 else
274 data.align = getpagesize();
Praveen Chavan4e931c12012-11-28 19:43:17 -0800275
Naseer Ahmedde758fd2016-05-03 15:10:23 -0400276 if (usage & GRALLOC_USAGE_PROTECTED) {
Sushil Chauhandfe55a22016-09-12 15:48:29 -0700277 if ((usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) ||
278 (usage & GRALLOC_USAGE_HW_CAMERA_MASK)) {
Shalaj Jain1f9725a2015-03-04 17:53:49 -0800279 /* The alignment here reflects qsee mmu V7L/V8L requirement */
280 data.align = SZ_2M;
281 } else {
282 data.align = SECURE_ALIGN;
283 }
Praveen Chavan4e931c12012-11-28 19:43:17 -0800284 size = ALIGN(size, data.align);
285 }
Naseer Ahmed84786722013-06-14 16:29:59 -0400286
Praveen Chavan4e931c12012-11-28 19:43:17 -0800287 data.size = size;
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800288 data.pHandle = (uintptr_t) pHandle;
Naseer Ahmed01d3fd32012-07-14 21:08:13 -0700289 err = mAllocCtrl->allocate(data, usage);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700290
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800291 if (!err) {
292 /* allocate memory for enhancement data */
293 alloc_data eData;
294 eData.fd = -1;
295 eData.base = 0;
296 eData.offset = 0;
297 eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
298 eData.pHandle = data.pHandle;
299 eData.align = getpagesize();
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500300 int eDataUsage = 0;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800301 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
302 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
303 strerror(-eDataErr));
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700304
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800305 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
306 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800307 }
Saurabh Shahbddb9522015-09-30 13:31:55 -0700308
Arun Kumar K.R48a57552015-09-03 11:37:35 -0700309 if (usage & GRALLOC_USAGE_PRIVATE_INTERNAL_ONLY) {
310 flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
311 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400312
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800313 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
314 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
315 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400316
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800317 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
318 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
319 }
Naseer Ahmed59802502012-08-21 17:03:27 -0400320
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800321 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
322 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
323 }
324
Naseer Ahmede41ad9c2013-04-05 17:59:28 -0400325 if (usage & GRALLOC_USAGE_HW_COMPOSER) {
326 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
327 }
328
Shuzhen Wang46ca2262013-04-10 23:02:22 -0700329 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
330 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
331 }
332
Ramkumar Radhakrishnanba713382013-08-30 18:41:07 -0700333 if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
334 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
335 }
336
Sushil Chauhan7807d192015-08-13 10:10:48 -0700337 if (isUBwcEnabled(format, usage)) {
Sushil Chauhan65e26302015-01-14 10:48:57 -0800338 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
339 }
340
Ramkumar Radhakrishnan9d20b392013-11-15 19:32:47 -0800341 if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
342 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
343 }
344
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800345 if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER |
346 GRALLOC_USAGE_HW_CAMERA_WRITE |
347 GRALLOC_USAGE_HW_RENDER |
348 GRALLOC_USAGE_HW_FB)) {
349 flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER;
350 }
351
Surajit Podder074314d2015-06-25 01:43:20 +0530352 if(usage & GRALLOC_USAGE_HW_COMPOSER) {
353 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
354 }
355
Saurabh Shah1adcafe2014-12-19 10:05:41 -0800356 if(false == data.uncached) {
357 flags |= private_handle_t::PRIV_FLAGS_CACHED;
358 }
359
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700360 flags |= data.allocType;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700361 uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset;
Ramkumar Radhakrishnan47573e22012-11-07 11:36:41 -0800362 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700363 bufferType, format, alignedw, alignedh,
364 eData.fd, eData.offset, eBaseAddr, width, height);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700365
366 hnd->offset = data.offset;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700367 hnd->base = (uint64_t)(data.base) + data.offset;
Naseer Ahmeda163b732013-02-12 14:53:33 -0500368 hnd->gpuaddr = 0;
Pullakavi Srinivas46204e32016-07-22 12:44:46 +0530369 ColorSpace_t colorSpace = ITU_R_601;
Naseer Ahmed42e5dde2014-03-28 19:32:01 -0400370 setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);
Naseer Ahmeda163b732013-02-12 14:53:33 -0500371
Saurabh Shah7d476ed2016-06-27 16:40:58 -0700372#ifdef COMPILE_DRM
373 if (qdutils::getDriverType() == qdutils::DriverType::DRM &&
374 usage & GRALLOC_USAGE_HW_COMPOSER) {
375 DRMBuffer buf = {};
376 int ret = getPlaneStrideOffset(hnd, buf.stride, buf.offset,
377 &buf.num_planes);
378 if (ret < 0) {
379 ALOGE("%s failed", __FUNCTION__);
380 return ret;
381 }
382
383 buf.fd = hnd->fd;
384 buf.width = hnd->width;
385 buf.height = hnd->height;
386 getDRMFormat(hnd->format, flags, &buf.drm_format,
387 &buf.drm_format_modifier);
388
389 DRMMaster *master = nullptr;
390 ret = DRMMaster::GetInstance(&master);
391 if (ret < 0) {
392 ALOGE("%s Failed to acquire DRMMaster instance", __FUNCTION__);
393 return ret;
394 }
395
396 ret = master->CreateFbId(buf, &hnd->gem_handle, &hnd->fb_id);
397 if (ret < 0) {
398 ALOGE("%s: CreateFbId failed. width %d, height %d, " \
399 "format: %s, stride %u, error %d", __FUNCTION__,
400 buf.width, buf.height,
401 qdutils::GetHALPixelFormatString(hnd->format),
402 buf.stride[0], errno);
403 return ret;
404 }
405 }
406#endif
407
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700408 *pHandle = hnd;
409 }
410
411 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
412 return err;
413}
414
415void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700416 int *bufferType)
417{
418 *bufferType = BUFFER_TYPE_VIDEO;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700419
Naomi Luiscffc5bd2015-08-28 14:57:31 -0700420 if (isUncompressedRgbFormat(inputFormat) == TRUE) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700421 // RGB formats
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700422 *bufferType = BUFFER_TYPE_UI;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700423 }
424}
425
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800426int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400427 buffer_handle_t* pHandle)
428{
429 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
430
Naseer Ahmed8d0d72a2014-12-19 16:25:09 -0500431 // This allocation will only happen when gralloc is in fb mode
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400432
433 if (m->framebuffer == NULL) {
434 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
435 return -EINVAL;
436 }
437
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700438 const unsigned int bufferMask = m->bufferMask;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400439 const uint32_t numBuffers = m->numBuffers;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700440 unsigned int bufferSize = m->finfo.line_length * m->info.yres;
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400441
442 //adreno needs FB size to be page aligned
443 bufferSize = roundUpToPageSize(bufferSize);
444
445 if (numBuffers == 1) {
446 // If we have only one buffer, we never use page-flipping. Instead,
447 // we return a regular buffer which will be memcpy'ed to the main
448 // screen when post is called.
449 int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
450 return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
451 m->fbFormat, m->info.xres, m->info.yres);
452 }
453
454 if (bufferMask >= ((1LU<<numBuffers)-1)) {
455 // We ran out of buffers.
456 return -ENOMEM;
457 }
458
459 // create a "fake" handle for it
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700460 uint64_t vaddr = uint64_t(m->framebuffer->base);
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700461 // As GPU needs ION FD, the private handle is created
462 // using ION fd and ION flags are set
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400463 private_handle_t* hnd = new private_handle_t(
464 dup(m->framebuffer->fd), bufferSize,
Arun Kumar K.Rf0f853f2014-03-17 16:03:21 -0700465 private_handle_t::PRIV_FLAGS_USES_ION |
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400466 private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
467 BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
468 m->info.yres);
469
470 // find a free slot
471 for (uint32_t i=0 ; i<numBuffers ; i++) {
472 if ((bufferMask & (1LU<<i)) == 0) {
Shalaj Jaina70b4352014-06-15 13:47:47 -0700473 m->bufferMask |= (uint32_t)(1LU<<i);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400474 break;
475 }
476 vaddr += bufferSize;
477 }
478 hnd->base = vaddr;
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700479 hnd->offset = (unsigned int)(vaddr - m->framebuffer->base);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400480 *pHandle = hnd;
481 return 0;
482}
483
484
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800485int gpu_context_t::gralloc_alloc_framebuffer(int usage,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400486 buffer_handle_t* pHandle)
487{
488 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
489 pthread_mutex_lock(&m->lock);
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800490 int err = gralloc_alloc_framebuffer_locked(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400491 pthread_mutex_unlock(&m->lock);
492 return err;
493}
494
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700495int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700496 buffer_handle_t* pHandle, int* pStride,
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700497 unsigned int bufferSize) {
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700498 if (!pHandle || !pStride)
499 return -EINVAL;
500
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700501 unsigned int size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700502 int alignedw, alignedh;
Naseer Ahmed59802502012-08-21 17:03:27 -0400503 int grallocFormat = format;
504 int bufferType;
Naseer Ahmed59802502012-08-21 17:03:27 -0400505
Saurabh Shahaedf2362012-09-05 11:30:59 -0700506 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
507 //the usage bits, gralloc assigns a format.
Naseer Ahmed34d33bc2013-05-08 12:28:37 -0400508 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
509 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Sushil Chauhan32333292015-02-04 18:56:32 -0800510 if (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC)
511 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
Jeykumar Sankaran9bc1a782015-12-14 18:36:27 -0800512 else if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
513 if(MDPCapabilityInfo::getInstance().isWBUBWCSupportedByMDP() &&
Ramakant Singhf8d1c402017-01-17 19:01:21 +0530514 !IAllocController::getInstance()->isDisableUBWCForEncoder() &&
Jeykumar Sankaran9bc1a782015-12-14 18:36:27 -0800515 usage & GRALLOC_USAGE_HW_COMPOSER)
516 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
517 else
518 grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12
519 } else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK)
Ramkumar Radhakrishnanff511022013-07-23 16:12:08 -0700520 == GRALLOC_USAGE_HW_CAMERA_ZSL)
521 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL
Saurabh Shahaedf2362012-09-05 11:30:59 -0700522 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
523 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
Ranjith Kagathi Ananda330b21d2015-08-17 16:41:08 -0700524 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
525 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
Nagesh Subba Reddy81a519f2015-08-29 18:01:49 -0700526 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21
Ranjith Kagathi Ananda330b21d2015-08-17 16:41:08 -0700527 } else {
528 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; //NV12 preview
529 }
530 } else if(usage & GRALLOC_USAGE_HW_COMPOSER)
Manoj Kumar AVMcdb4fd52013-10-25 19:40:45 -0700531 //XXX: If we still haven't set a format, default to RGBA8888
532 grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888;
Nagesh Subba Reddy81a519f2015-08-29 18:01:49 -0700533 else if(format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
534 //If no other usage flags are detected, default the
535 //flexible YUV format to NV21_ZSL
536 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL;
537 }
Baldev Sahu5f673b42013-12-05 09:49:09 +0530538 }
539
Sushil Chauhan68c793a2015-04-16 16:31:22 -0700540 bool useFbMem = false;
541 char property[PROPERTY_VALUE_MAX];
542 char isUBWC[PROPERTY_VALUE_MAX];
543 if (usage & GRALLOC_USAGE_HW_FB) {
544 if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) &&
545 (!strncmp(property, "1", PROPERTY_VALUE_MAX ) ||
546 (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) {
547 useFbMem = true;
548 } else {
Sushil Chauhanc9853b52015-09-30 18:43:11 -0700549 usage &= ~GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
Sushil Chauhan68c793a2015-04-16 16:31:22 -0700550 if (property_get("debug.gralloc.enable_fb_ubwc", isUBWC, NULL) > 0){
551 if ((!strncmp(isUBWC, "1", PROPERTY_VALUE_MAX)) ||
552 (!strncasecmp(isUBWC, "true", PROPERTY_VALUE_MAX))) {
553 // Allocate UBWC aligned framebuffer
554 usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
555 }
556 }
557 }
558 }
559
Saurabh Shahaedf2362012-09-05 11:30:59 -0700560 getGrallocInformationFromFormat(grallocFormat, &bufferType);
Manoj Kumar AVM8a220812013-10-10 11:46:06 -0700561 size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw,
562 alignedh);
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700563
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700564 if ((unsigned int)size <= 0)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700565 return -EINVAL;
Ramkumar Radhakrishnan73f952a2013-03-05 14:14:24 -0800566 size = (bufferSize >= size)? bufferSize : size;
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700567
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400568 int err = 0;
569 if(useFbMem) {
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800570 err = gralloc_alloc_framebuffer(usage, pHandle);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400571 } else {
572 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
Ramkumar Radhakrishnanba55eac2016-08-26 22:33:48 -0700573 grallocFormat, w, h);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400574 }
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700575
576 if (err < 0) {
577 return err;
578 }
579
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700580 *pStride = alignedw;
581 return 0;
582}
583
584int gpu_context_t::free_impl(private_handle_t const* hnd) {
585 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400586 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700587 const unsigned int bufferSize = m->finfo.line_length * m->info.yres;
588 unsigned int index = (unsigned int) ((hnd->base - m->framebuffer->base)
589 / bufferSize);
Shalaj Jaina70b4352014-06-15 13:47:47 -0700590 m->bufferMask &= (uint32_t)~(1LU<<index);
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400591 } else {
Jeykumar Sankaranc1f86822013-02-20 18:32:01 -0800592
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400593 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
594 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700595 int err = memalloc->free_buffer((void*)hnd->base, hnd->size,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400596 hnd->offset, hnd->fd);
597 if(err)
598 return err;
599 // free the metadata space
Saurabh Shah8f0ea6f2014-05-19 16:48:53 -0700600 unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400601 err = memalloc->free_buffer((void*)hnd->base_metadata,
Arun Kumar K.R6c85f052014-01-21 21:47:41 -0800602 size, hnd->offset_metadata,
Naseer Ahmed47aa15e2013-04-10 21:27:09 -0400603 hnd->fd_metadata);
604 if (err)
605 return err;
606 }
Saurabh Shah7d476ed2016-06-27 16:40:58 -0700607
608#ifdef COMPILE_DRM
609 if (hnd->fb_id) {
610 DRMMaster *master = nullptr;
611 int ret = DRMMaster::GetInstance(&master);
612 if (ret < 0) {
613 ALOGE("%s Failed to acquire DRMMaster instance", __FUNCTION__);
614 return ret;
615 }
616 ret = master->RemoveFbId(hnd->gem_handle, hnd->fb_id);
617 if (ret < 0) {
618 ALOGE("%s: Removing fb_id %d failed with error %d", __FUNCTION__,
619 hnd->fb_id, errno);
620 }
621 }
622#endif
623
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700624 delete hnd;
625 return 0;
626}
627
628int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700629 int usage, buffer_handle_t* pHandle,
630 int* pStride)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700631{
632 if (!dev) {
633 return -EINVAL;
634 }
635 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
636 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
637}
Naseer Ahmed29a26812012-06-14 00:56:20 -0700638int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h,
639 int format, int usage,
640 buffer_handle_t* pHandle, int* pStride,
641 int bufferSize)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700642{
643 if (!dev) {
644 return -EINVAL;
645 }
646 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
647 return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
648}
649
650
651int gpu_context_t::gralloc_free(alloc_device_t* dev,
Naseer Ahmed29a26812012-06-14 00:56:20 -0700652 buffer_handle_t handle)
Iliyan Malchev202a77d2012-06-11 14:41:12 -0700653{
654 if (private_handle_t::validate(handle) < 0)
655 return -EINVAL;
656
657 private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
658 gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
659 return gpu->free_impl(hnd);
660}
661
662/*****************************************************************************/
663
664int gpu_context_t::gralloc_close(struct hw_device_t *dev)
665{
666 gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
667 if (ctx) {
668 /* TODO: keep a list of all buffer_handle_t created, and free them
669 * all here.
670 */
671 delete ctx;
672 }
673 return 0;
674}
675