1 /*
2 * Copyright (C) 2010 The Android Open Source Project
3 * Copyright (c) 2011-2014,2017 The Linux Foundation. All rights reserved.
4 *
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 #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>
32
33 #include "gr.h"
34 #include "gpu.h"
35 #include "memalloc.h"
36 #include "alloc_controller.h"
37
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
43
44 using namespace gralloc;
45
46 #ifdef COMPILE_DRM
47 using namespace drm_utils;
48
getPlaneStrideOffset(private_handle_t * hnd,uint32_t * stride,uint32_t * offset,uint32_t * num_planes)49 static 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
getDRMFormat(int hal_format,int flags,uint32_t * drm_format,uint64_t * drm_format_modifier)133 static 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
gpu_context_t(const private_module_t * module,IAllocController * alloc_ctrl)233 gpu_context_t::gpu_context_t(const private_module_t* module,
234 IAllocController* alloc_ctrl ) :
235 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
240 // 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;
246 free = gralloc_free;
247
248 }
249
gralloc_alloc_buffer(unsigned int size,int usage,buffer_handle_t * pHandle,int bufferType,int format,int width,int height)250 int gpu_context_t::gralloc_alloc_buffer(unsigned int size, int usage,
251 buffer_handle_t* pHandle, int bufferType,
252 int format, int width, int height)
253 {
254 int err = 0;
255 int flags = 0;
256 int alignedw = 0;
257 int alignedh = 0;
258
259 AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width,
260 height,
261 format,
262 usage,
263 alignedw,
264 alignedh);
265
266 size = roundUpToPageSize(size);
267 alloc_data data;
268 data.offset = 0;
269 data.fd = -1;
270 data.base = 0;
271 if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
272 data.align = 8192;
273 else
274 data.align = getpagesize();
275
276 if (usage & GRALLOC_USAGE_PROTECTED) {
277 if ((usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) ||
278 (usage & GRALLOC_USAGE_HW_CAMERA_MASK)) {
279 /* The alignment here reflects qsee mmu V7L/V8L requirement */
280 data.align = SZ_2M;
281 } else {
282 data.align = SECURE_ALIGN;
283 }
284 size = ALIGN(size, data.align);
285 }
286
287 data.size = size;
288 data.pHandle = (uintptr_t) pHandle;
289 err = mAllocCtrl->allocate(data, usage);
290
291 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();
300 int eDataUsage = 0;
301 int eDataErr = mAllocCtrl->allocate(eData, eDataUsage);
302 ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s",
303 strerror(-eDataErr));
304
305 if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) {
306 flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY;
307 }
308
309 if (usage & GRALLOC_USAGE_PRIVATE_INTERNAL_ONLY) {
310 flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY;
311 }
312
313 if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) {
314 flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER;
315 }
316
317 if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
318 flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE;
319 }
320
321 if (usage & GRALLOC_USAGE_HW_CAMERA_READ) {
322 flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ;
323 }
324
325 if (usage & GRALLOC_USAGE_HW_COMPOSER) {
326 flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER;
327 }
328
329 if (usage & GRALLOC_USAGE_HW_TEXTURE) {
330 flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE;
331 }
332
333 if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) {
334 flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY;
335 }
336
337 if (isUBwcEnabled(format, usage)) {
338 flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED;
339 }
340
341 if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) {
342 flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED;
343 }
344
345 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
352 if(usage & GRALLOC_USAGE_HW_COMPOSER) {
353 flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER;
354 }
355
356 if(false == data.uncached) {
357 flags |= private_handle_t::PRIV_FLAGS_CACHED;
358 }
359
360 flags |= data.allocType;
361 uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset;
362 private_handle_t *hnd = new private_handle_t(data.fd, size, flags,
363 bufferType, format, alignedw, alignedh,
364 eData.fd, eData.offset, eBaseAddr, width, height);
365
366 hnd->offset = data.offset;
367 hnd->base = (uint64_t)(data.base) + data.offset;
368 hnd->gpuaddr = 0;
369 ColorSpace_t colorSpace = ITU_R_601;
370 setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace);
371
372 #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
408 *pHandle = hnd;
409 }
410
411 ALOGE_IF(err, "gralloc failed err=%s", strerror(-err));
412 return err;
413 }
414
getGrallocInformationFromFormat(int inputFormat,int * bufferType)415 void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
416 int *bufferType)
417 {
418 *bufferType = BUFFER_TYPE_VIDEO;
419
420 if (isUncompressedRgbFormat(inputFormat) == TRUE) {
421 // RGB formats
422 *bufferType = BUFFER_TYPE_UI;
423 }
424 }
425
gralloc_alloc_framebuffer_locked(int usage,buffer_handle_t * pHandle)426 int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage,
427 buffer_handle_t* pHandle)
428 {
429 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
430
431 // This allocation will only happen when gralloc is in fb mode
432
433 if (m->framebuffer == NULL) {
434 ALOGE("%s: Invalid framebuffer", __FUNCTION__);
435 return -EINVAL;
436 }
437
438 const unsigned int bufferMask = m->bufferMask;
439 const uint32_t numBuffers = m->numBuffers;
440 unsigned int bufferSize = m->finfo.line_length * m->info.yres;
441
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
460 uint64_t vaddr = uint64_t(m->framebuffer->base);
461 // As GPU needs ION FD, the private handle is created
462 // using ION fd and ION flags are set
463 private_handle_t* hnd = new private_handle_t(
464 dup(m->framebuffer->fd), bufferSize,
465 private_handle_t::PRIV_FLAGS_USES_ION |
466 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) {
473 m->bufferMask |= (uint32_t)(1LU<<i);
474 break;
475 }
476 vaddr += bufferSize;
477 }
478 hnd->base = vaddr;
479 hnd->offset = (unsigned int)(vaddr - m->framebuffer->base);
480 *pHandle = hnd;
481 return 0;
482 }
483
484
gralloc_alloc_framebuffer(int usage,buffer_handle_t * pHandle)485 int gpu_context_t::gralloc_alloc_framebuffer(int usage,
486 buffer_handle_t* pHandle)
487 {
488 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
489 pthread_mutex_lock(&m->lock);
490 int err = gralloc_alloc_framebuffer_locked(usage, pHandle);
491 pthread_mutex_unlock(&m->lock);
492 return err;
493 }
494
alloc_impl(int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride,unsigned int bufferSize)495 int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
496 buffer_handle_t* pHandle, int* pStride,
497 unsigned int bufferSize) {
498 if (!pHandle || !pStride)
499 return -EINVAL;
500
501 unsigned int size;
502 int alignedw, alignedh;
503 int grallocFormat = format;
504 int bufferType;
505
506 //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on
507 //the usage bits, gralloc assigns a format.
508 if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED ||
509 format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
510 if (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC)
511 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC;
512 else if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) {
513 if(MDPCapabilityInfo::getInstance().isWBUBWCSupportedByMDP() &&
514 !IAllocController::getInstance()->isDisableUBWCForEncoder() &&
515 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)
520 == GRALLOC_USAGE_HW_CAMERA_ZSL)
521 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL
522 else if(usage & GRALLOC_USAGE_HW_CAMERA_READ)
523 grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21
524 else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) {
525 if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) {
526 grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21
527 } else {
528 grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; //NV12 preview
529 }
530 } else if(usage & GRALLOC_USAGE_HW_COMPOSER)
531 //XXX: If we still haven't set a format, default to RGBA8888
532 grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888;
533 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 }
538 }
539
540 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 {
549 usage &= ~GRALLOC_USAGE_PRIVATE_ALLOC_UBWC;
550 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
560 getGrallocInformationFromFormat(grallocFormat, &bufferType);
561 size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw,
562 alignedh);
563
564 if ((unsigned int)size <= 0)
565 return -EINVAL;
566 size = (bufferSize >= size)? bufferSize : size;
567
568 int err = 0;
569 if(useFbMem) {
570 err = gralloc_alloc_framebuffer(usage, pHandle);
571 } else {
572 err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
573 grallocFormat, w, h);
574 }
575
576 if (err < 0) {
577 return err;
578 }
579
580 *pStride = alignedw;
581 return 0;
582 }
583
free_impl(private_handle_t const * hnd)584 int gpu_context_t::free_impl(private_handle_t const* hnd) {
585 private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
586 if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
587 const unsigned int bufferSize = m->finfo.line_length * m->info.yres;
588 unsigned int index = (unsigned int) ((hnd->base - m->framebuffer->base)
589 / bufferSize);
590 m->bufferMask &= (uint32_t)~(1LU<<index);
591 } else {
592
593 terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
594 IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags);
595 int err = memalloc->free_buffer((void*)hnd->base, hnd->size,
596 hnd->offset, hnd->fd);
597 if(err)
598 return err;
599 // free the metadata space
600 unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t));
601 err = memalloc->free_buffer((void*)hnd->base_metadata,
602 size, hnd->offset_metadata,
603 hnd->fd_metadata);
604 if (err)
605 return err;
606 }
607
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
624 delete hnd;
625 return 0;
626 }
627
gralloc_alloc(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride)628 int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
629 int usage, buffer_handle_t* pHandle,
630 int* pStride)
631 {
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 }
gralloc_alloc_size(alloc_device_t * dev,int w,int h,int format,int usage,buffer_handle_t * pHandle,int * pStride,int bufferSize)638 int 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)
642 {
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
gralloc_free(alloc_device_t * dev,buffer_handle_t handle)651 int gpu_context_t::gralloc_free(alloc_device_t* dev,
652 buffer_handle_t handle)
653 {
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
gralloc_close(struct hw_device_t * dev)664 int 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
676