Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
Saurabh Shah | c5b2b70 | 2016-10-24 17:16:01 -0700 | [diff] [blame] | 3 | * Copyright (c) 2011-2014,2017 The Linux Foundation. All rights reserved. |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 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> |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 23 | #include <linux/msm_ion.h> |
| 24 | #ifdef COMPILE_DRM |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 25 | #include <drm_master.h> |
| 26 | #endif |
| 27 | #include <qdMetaData.h> |
| 28 | #include <qd_utils.h> |
| 29 | |
| 30 | #include <algorithm> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 31 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 32 | #include "gr.h" |
| 33 | #include "gpu.h" |
| 34 | #include "memalloc.h" |
| 35 | #include "alloc_controller.h" |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 36 | |
| 37 | #ifdef COMPILE_DRM |
Saurabh Shah | a228f12 | 2017-01-23 15:55:25 -0800 | [diff] [blame] | 38 | using namespace drm_utils; |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 39 | #endif |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 40 | |
| 41 | using namespace gralloc; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 42 | |
| 43 | gpu_context_t::gpu_context_t(const private_module_t* module, |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 44 | IAllocController* alloc_ctrl ) : |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 45 | 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 Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 50 | // 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 Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 56 | free = gralloc_free; |
| 57 | |
| 58 | } |
| 59 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 60 | int gpu_context_t::gralloc_alloc_buffer(unsigned int size, int usage, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 61 | buffer_handle_t* pHandle, int bufferType, |
| 62 | int format, int width, int height) |
| 63 | { |
| 64 | int err = 0; |
| 65 | int flags = 0; |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 66 | 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 Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 76 | size = roundUpToPageSize(size); |
| 77 | alloc_data data; |
| 78 | data.offset = 0; |
| 79 | data.fd = -1; |
| 80 | data.base = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 81 | if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) |
| 82 | data.align = 8192; |
| 83 | else |
| 84 | data.align = getpagesize(); |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 85 | |
Naseer Ahmed | de758fd | 2016-05-03 15:10:23 -0400 | [diff] [blame] | 86 | if (usage & GRALLOC_USAGE_PROTECTED) { |
Sushil Chauhan | dfe55a2 | 2016-09-12 15:48:29 -0700 | [diff] [blame] | 87 | if ((usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) || |
| 88 | (usage & GRALLOC_USAGE_HW_CAMERA_MASK)) { |
Shalaj Jain | 1f9725a | 2015-03-04 17:53:49 -0800 | [diff] [blame] | 89 | /* The alignment here reflects qsee mmu V7L/V8L requirement */ |
| 90 | data.align = SZ_2M; |
| 91 | } else { |
| 92 | data.align = SECURE_ALIGN; |
| 93 | } |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 94 | size = ALIGN(size, data.align); |
| 95 | } |
Naseer Ahmed | 8478672 | 2013-06-14 16:29:59 -0400 | [diff] [blame] | 96 | |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 97 | data.size = size; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 98 | data.pHandle = (uintptr_t) pHandle; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 99 | err = mAllocCtrl->allocate(data, usage); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 100 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 101 | 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 Ahmed | 8d0d72a | 2014-12-19 16:25:09 -0500 | [diff] [blame] | 110 | int eDataUsage = 0; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 111 | int eDataErr = mAllocCtrl->allocate(eData, eDataUsage); |
| 112 | ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s", |
| 113 | strerror(-eDataErr)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 114 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 115 | if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) { |
| 116 | flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 117 | } |
Saurabh Shah | bddb952 | 2015-09-30 13:31:55 -0700 | [diff] [blame] | 118 | |
Arun Kumar K.R | 48a5755 | 2015-09-03 11:37:35 -0700 | [diff] [blame] | 119 | if (usage & GRALLOC_USAGE_PRIVATE_INTERNAL_ONLY) { |
| 120 | flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY; |
| 121 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 122 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 123 | if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) { |
| 124 | flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER; |
| 125 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 126 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 127 | if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { |
| 128 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE; |
| 129 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 130 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 131 | if (usage & GRALLOC_USAGE_HW_CAMERA_READ) { |
| 132 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ; |
| 133 | } |
| 134 | |
Naseer Ahmed | e41ad9c | 2013-04-05 17:59:28 -0400 | [diff] [blame] | 135 | if (usage & GRALLOC_USAGE_HW_COMPOSER) { |
| 136 | flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER; |
| 137 | } |
| 138 | |
Shuzhen Wang | 46ca226 | 2013-04-10 23:02:22 -0700 | [diff] [blame] | 139 | if (usage & GRALLOC_USAGE_HW_TEXTURE) { |
| 140 | flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE; |
| 141 | } |
| 142 | |
Ramkumar Radhakrishnan | ba71338 | 2013-08-30 18:41:07 -0700 | [diff] [blame] | 143 | if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) { |
| 144 | flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY; |
| 145 | } |
| 146 | |
Sushil Chauhan | 7807d19 | 2015-08-13 10:10:48 -0700 | [diff] [blame] | 147 | if (isUBwcEnabled(format, usage)) { |
Sushil Chauhan | 65e2630 | 2015-01-14 10:48:57 -0800 | [diff] [blame] | 148 | flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; |
| 149 | } |
| 150 | |
Ramkumar Radhakrishnan | 9d20b39 | 2013-11-15 19:32:47 -0800 | [diff] [blame] | 151 | if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { |
| 152 | flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED; |
| 153 | } |
| 154 | |
Saurabh Shah | 1adcafe | 2014-12-19 10:05:41 -0800 | [diff] [blame] | 155 | 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 Podder | 074314d | 2015-06-25 01:43:20 +0530 | [diff] [blame] | 162 | if(usage & GRALLOC_USAGE_HW_COMPOSER) { |
| 163 | flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER; |
| 164 | } |
| 165 | |
Saurabh Shah | 1adcafe | 2014-12-19 10:05:41 -0800 | [diff] [blame] | 166 | if(false == data.uncached) { |
| 167 | flags |= private_handle_t::PRIV_FLAGS_CACHED; |
| 168 | } |
| 169 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 170 | flags |= data.allocType; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 171 | uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 172 | private_handle_t *hnd = new private_handle_t(data.fd, size, flags, |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 173 | bufferType, format, alignedw, alignedh, |
| 174 | eData.fd, eData.offset, eBaseAddr, width, height); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 175 | |
| 176 | hnd->offset = data.offset; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 177 | hnd->base = (uint64_t)(data.base) + data.offset; |
Naseer Ahmed | a163b73 | 2013-02-12 14:53:33 -0500 | [diff] [blame] | 178 | hnd->gpuaddr = 0; |
Pullakavi Srinivas | 46204e3 | 2016-07-22 12:44:46 +0530 | [diff] [blame] | 179 | ColorSpace_t colorSpace = ITU_R_601; |
Naseer Ahmed | 42e5dde | 2014-03-28 19:32:01 -0400 | [diff] [blame] | 180 | setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace); |
Naseer Ahmed | a163b73 | 2013-02-12 14:53:33 -0500 | [diff] [blame] | 181 | |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 182 | #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 Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 218 | *pHandle = hnd; |
| 219 | } |
| 220 | |
| 221 | ALOGE_IF(err, "gralloc failed err=%s", strerror(-err)); |
| 222 | return err; |
| 223 | } |
| 224 | |
| 225 | void gpu_context_t::getGrallocInformationFromFormat(int inputFormat, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 226 | int *bufferType) |
| 227 | { |
| 228 | *bufferType = BUFFER_TYPE_VIDEO; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 229 | |
Naomi Luis | cffc5bd | 2015-08-28 14:57:31 -0700 | [diff] [blame] | 230 | if (isUncompressedRgbFormat(inputFormat) == TRUE) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 231 | // RGB formats |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 232 | *bufferType = BUFFER_TYPE_UI; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 236 | int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage, |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 237 | buffer_handle_t* pHandle) |
| 238 | { |
| 239 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
| 240 | |
Naseer Ahmed | 8d0d72a | 2014-12-19 16:25:09 -0500 | [diff] [blame] | 241 | // This allocation will only happen when gralloc is in fb mode |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 242 | |
| 243 | if (m->framebuffer == NULL) { |
| 244 | ALOGE("%s: Invalid framebuffer", __FUNCTION__); |
| 245 | return -EINVAL; |
| 246 | } |
| 247 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 248 | const unsigned int bufferMask = m->bufferMask; |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 249 | const uint32_t numBuffers = m->numBuffers; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 250 | unsigned int bufferSize = m->finfo.line_length * m->info.yres; |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 251 | |
| 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 Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 270 | uint64_t vaddr = uint64_t(m->framebuffer->base); |
Arun Kumar K.R | f0f853f | 2014-03-17 16:03:21 -0700 | [diff] [blame] | 271 | // As GPU needs ION FD, the private handle is created |
| 272 | // using ION fd and ION flags are set |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 273 | private_handle_t* hnd = new private_handle_t( |
| 274 | dup(m->framebuffer->fd), bufferSize, |
Arun Kumar K.R | f0f853f | 2014-03-17 16:03:21 -0700 | [diff] [blame] | 275 | private_handle_t::PRIV_FLAGS_USES_ION | |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 276 | 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 Jain | a70b435 | 2014-06-15 13:47:47 -0700 | [diff] [blame] | 283 | m->bufferMask |= (uint32_t)(1LU<<i); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | vaddr += bufferSize; |
| 287 | } |
| 288 | hnd->base = vaddr; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 289 | hnd->offset = (unsigned int)(vaddr - m->framebuffer->base); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 290 | *pHandle = hnd; |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 295 | int gpu_context_t::gralloc_alloc_framebuffer(int usage, |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 296 | 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.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 300 | int err = gralloc_alloc_framebuffer_locked(usage, pHandle); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 301 | pthread_mutex_unlock(&m->lock); |
| 302 | return err; |
| 303 | } |
| 304 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 305 | int gpu_context_t::alloc_impl(int w, int h, int format, int usage, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 306 | buffer_handle_t* pHandle, int* pStride, |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 307 | unsigned int bufferSize) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 308 | if (!pHandle || !pStride) |
| 309 | return -EINVAL; |
| 310 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 311 | unsigned int size; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 312 | int alignedw, alignedh; |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 313 | int grallocFormat = format; |
| 314 | int bufferType; |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 315 | |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 316 | //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on |
| 317 | //the usage bits, gralloc assigns a format. |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 318 | if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED || |
| 319 | format == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
Sushil Chauhan | 3233329 | 2015-02-04 18:56:32 -0800 | [diff] [blame] | 320 | if (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) |
| 321 | grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC; |
Jeykumar Sankaran | 9bc1a78 | 2015-12-14 18:36:27 -0800 | [diff] [blame] | 322 | else if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { |
| 323 | if(MDPCapabilityInfo::getInstance().isWBUBWCSupportedByMDP() && |
Ramakant Singh | f8d1c40 | 2017-01-17 19:01:21 +0530 | [diff] [blame] | 324 | !IAllocController::getInstance()->isDisableUBWCForEncoder() && |
Jeykumar Sankaran | 9bc1a78 | 2015-12-14 18:36:27 -0800 | [diff] [blame] | 325 | 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 Radhakrishnan | ff51102 | 2013-07-23 16:12:08 -0700 | [diff] [blame] | 330 | == GRALLOC_USAGE_HW_CAMERA_ZSL) |
| 331 | grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 332 | else if(usage & GRALLOC_USAGE_HW_CAMERA_READ) |
| 333 | grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21 |
Ranjith Kagathi Ananda | 330b21d | 2015-08-17 16:41:08 -0700 | [diff] [blame] | 334 | else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { |
| 335 | if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
Nagesh Subba Reddy | 81a519f | 2015-08-29 18:01:49 -0700 | [diff] [blame] | 336 | grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 |
Ranjith Kagathi Ananda | 330b21d | 2015-08-17 16:41:08 -0700 | [diff] [blame] | 337 | } else { |
| 338 | grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; //NV12 preview |
| 339 | } |
| 340 | } else if(usage & GRALLOC_USAGE_HW_COMPOSER) |
Manoj Kumar AVM | cdb4fd5 | 2013-10-25 19:40:45 -0700 | [diff] [blame] | 341 | //XXX: If we still haven't set a format, default to RGBA8888 |
| 342 | grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888; |
Nagesh Subba Reddy | 81a519f | 2015-08-29 18:01:49 -0700 | [diff] [blame] | 343 | 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 Sahu | 5f673b4 | 2013-12-05 09:49:09 +0530 | [diff] [blame] | 348 | } |
| 349 | |
Sushil Chauhan | 68c793a | 2015-04-16 16:31:22 -0700 | [diff] [blame] | 350 | 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 Chauhan | c9853b5 | 2015-09-30 18:43:11 -0700 | [diff] [blame] | 359 | usage &= ~GRALLOC_USAGE_PRIVATE_ALLOC_UBWC; |
Sushil Chauhan | 68c793a | 2015-04-16 16:31:22 -0700 | [diff] [blame] | 360 | 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 Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 370 | getGrallocInformationFromFormat(grallocFormat, &bufferType); |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 371 | size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw, |
| 372 | alignedh); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 373 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 374 | if ((unsigned int)size <= 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 375 | return -EINVAL; |
Ramkumar Radhakrishnan | 73f952a | 2013-03-05 14:14:24 -0800 | [diff] [blame] | 376 | size = (bufferSize >= size)? bufferSize : size; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 377 | |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 378 | int err = 0; |
| 379 | if(useFbMem) { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 380 | err = gralloc_alloc_framebuffer(usage, pHandle); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 381 | } else { |
| 382 | err = gralloc_alloc_buffer(size, usage, pHandle, bufferType, |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 383 | grallocFormat, w, h); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 384 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 385 | |
| 386 | if (err < 0) { |
| 387 | return err; |
| 388 | } |
| 389 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 390 | *pStride = alignedw; |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | int gpu_context_t::free_impl(private_handle_t const* hnd) { |
| 395 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 396 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 397 | 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 Jain | a70b435 | 2014-06-15 13:47:47 -0700 | [diff] [blame] | 400 | m->bufferMask &= (uint32_t)~(1LU<<index); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 401 | } else { |
Jeykumar Sankaran | c1f8682 | 2013-02-20 18:32:01 -0800 | [diff] [blame] | 402 | |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 403 | terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd)); |
| 404 | IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags); |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 405 | int err = memalloc->free_buffer((void*)hnd->base, hnd->size, |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 406 | hnd->offset, hnd->fd); |
| 407 | if(err) |
| 408 | return err; |
| 409 | // free the metadata space |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 410 | unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 411 | err = memalloc->free_buffer((void*)hnd->base_metadata, |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 412 | size, hnd->offset_metadata, |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 413 | hnd->fd_metadata); |
| 414 | if (err) |
| 415 | return err; |
| 416 | } |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 417 | |
| 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 Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 434 | delete hnd; |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 439 | int usage, buffer_handle_t* pHandle, |
| 440 | int* pStride) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 441 | { |
| 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 Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 448 | int 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 Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 452 | { |
| 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 | |
| 461 | int gpu_context_t::gralloc_free(alloc_device_t* dev, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 462 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 463 | { |
| 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 | |
| 474 | int 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 | |