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> |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 24 | #include <qdMetaData.h> |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 25 | #include <algorithm> |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 26 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 27 | #include "gr.h" |
| 28 | #include "gpu.h" |
| 29 | #include "memalloc.h" |
| 30 | #include "alloc_controller.h" |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 31 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 32 | using namespace gralloc; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 33 | |
| 34 | gpu_context_t::gpu_context_t(const private_module_t* module, |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 35 | IAllocController* alloc_ctrl ) : |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 36 | mAllocCtrl(alloc_ctrl) |
| 37 | { |
| 38 | // Zero out the alloc_device_t |
| 39 | memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t)); |
| 40 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 41 | // Initialize the procs |
| 42 | common.tag = HARDWARE_DEVICE_TAG; |
| 43 | common.version = 0; |
| 44 | common.module = const_cast<hw_module_t*>(&module->base.common); |
| 45 | common.close = gralloc_close; |
| 46 | alloc = gralloc_alloc; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 47 | free = gralloc_free; |
| 48 | |
| 49 | } |
| 50 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 51 | int gpu_context_t::gralloc_alloc_buffer(unsigned int size, int usage, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 52 | buffer_handle_t* pHandle, int bufferType, |
| 53 | int format, int width, int height) |
| 54 | { |
| 55 | int err = 0; |
| 56 | int flags = 0; |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 57 | int alignedw = 0; |
| 58 | int alignedh = 0; |
| 59 | |
| 60 | AdrenoMemInfo::getInstance().getAlignedWidthAndHeight(width, |
| 61 | height, |
| 62 | format, |
| 63 | usage, |
| 64 | alignedw, |
| 65 | alignedh); |
| 66 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 67 | size = roundUpToPageSize(size); |
| 68 | alloc_data data; |
| 69 | data.offset = 0; |
| 70 | data.fd = -1; |
| 71 | data.base = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 72 | if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) |
| 73 | data.align = 8192; |
| 74 | else |
| 75 | data.align = getpagesize(); |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 76 | |
Naseer Ahmed | de758fd | 2016-05-03 15:10:23 -0400 | [diff] [blame] | 77 | if (usage & GRALLOC_USAGE_PROTECTED) { |
Sushil Chauhan | dfe55a2 | 2016-09-12 15:48:29 -0700 | [diff] [blame] | 78 | if ((usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) || |
| 79 | (usage & GRALLOC_USAGE_HW_CAMERA_MASK)) { |
Shalaj Jain | 1f9725a | 2015-03-04 17:53:49 -0800 | [diff] [blame] | 80 | /* The alignment here reflects qsee mmu V7L/V8L requirement */ |
| 81 | data.align = SZ_2M; |
| 82 | } else { |
| 83 | data.align = SECURE_ALIGN; |
| 84 | } |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 85 | size = ALIGN(size, data.align); |
| 86 | } |
Naseer Ahmed | 8478672 | 2013-06-14 16:29:59 -0400 | [diff] [blame] | 87 | |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 88 | data.size = size; |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 89 | data.pHandle = (uintptr_t) pHandle; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 90 | err = mAllocCtrl->allocate(data, usage); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 91 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 92 | if (!err) { |
| 93 | /* allocate memory for enhancement data */ |
| 94 | alloc_data eData; |
| 95 | eData.fd = -1; |
| 96 | eData.base = 0; |
| 97 | eData.offset = 0; |
| 98 | eData.size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 99 | eData.pHandle = data.pHandle; |
| 100 | eData.align = getpagesize(); |
Naseer Ahmed | 8d0d72a | 2014-12-19 16:25:09 -0500 | [diff] [blame] | 101 | int eDataUsage = 0; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 102 | int eDataErr = mAllocCtrl->allocate(eData, eDataUsage); |
| 103 | ALOGE_IF(eDataErr, "gralloc failed for eDataErr=%s", |
| 104 | strerror(-eDataErr)); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 105 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 106 | if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) { |
| 107 | flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 108 | } |
Saurabh Shah | bddb952 | 2015-09-30 13:31:55 -0700 | [diff] [blame] | 109 | |
Arun Kumar K.R | 48a5755 | 2015-09-03 11:37:35 -0700 | [diff] [blame] | 110 | if (usage & GRALLOC_USAGE_PRIVATE_INTERNAL_ONLY) { |
| 111 | flags |= private_handle_t::PRIV_FLAGS_INTERNAL_ONLY; |
| 112 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 113 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 114 | if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) { |
| 115 | flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER; |
| 116 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 117 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 118 | if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { |
| 119 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE; |
| 120 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 121 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 122 | if (usage & GRALLOC_USAGE_HW_CAMERA_READ) { |
| 123 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ; |
| 124 | } |
| 125 | |
Naseer Ahmed | e41ad9c | 2013-04-05 17:59:28 -0400 | [diff] [blame] | 126 | if (usage & GRALLOC_USAGE_HW_COMPOSER) { |
| 127 | flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER; |
| 128 | } |
| 129 | |
Shuzhen Wang | 46ca226 | 2013-04-10 23:02:22 -0700 | [diff] [blame] | 130 | if (usage & GRALLOC_USAGE_HW_TEXTURE) { |
| 131 | flags |= private_handle_t::PRIV_FLAGS_HW_TEXTURE; |
| 132 | } |
| 133 | |
Ramkumar Radhakrishnan | ba71338 | 2013-08-30 18:41:07 -0700 | [diff] [blame] | 134 | if(usage & GRALLOC_USAGE_PRIVATE_SECURE_DISPLAY) { |
| 135 | flags |= private_handle_t::PRIV_FLAGS_SECURE_DISPLAY; |
| 136 | } |
| 137 | |
Sushil Chauhan | 7807d19 | 2015-08-13 10:10:48 -0700 | [diff] [blame] | 138 | if (isUBwcEnabled(format, usage)) { |
Sushil Chauhan | 65e2630 | 2015-01-14 10:48:57 -0800 | [diff] [blame] | 139 | flags |= private_handle_t::PRIV_FLAGS_UBWC_ALIGNED; |
| 140 | } |
| 141 | |
Ramkumar Radhakrishnan | 9d20b39 | 2013-11-15 19:32:47 -0800 | [diff] [blame] | 142 | if(usage & (GRALLOC_USAGE_SW_READ_MASK | GRALLOC_USAGE_SW_WRITE_MASK)) { |
| 143 | flags |= private_handle_t::PRIV_FLAGS_CPU_RENDERED; |
| 144 | } |
| 145 | |
Saurabh Shah | 1adcafe | 2014-12-19 10:05:41 -0800 | [diff] [blame] | 146 | if (usage & (GRALLOC_USAGE_HW_VIDEO_ENCODER | |
| 147 | GRALLOC_USAGE_HW_CAMERA_WRITE | |
| 148 | GRALLOC_USAGE_HW_RENDER | |
| 149 | GRALLOC_USAGE_HW_FB)) { |
| 150 | flags |= private_handle_t::PRIV_FLAGS_NON_CPU_WRITER; |
| 151 | } |
| 152 | |
Surajit Podder | 074314d | 2015-06-25 01:43:20 +0530 | [diff] [blame] | 153 | if(usage & GRALLOC_USAGE_HW_COMPOSER) { |
| 154 | flags |= private_handle_t::PRIV_FLAGS_DISP_CONSUMER; |
| 155 | } |
| 156 | |
Saurabh Shah | 1adcafe | 2014-12-19 10:05:41 -0800 | [diff] [blame] | 157 | if(false == data.uncached) { |
| 158 | flags |= private_handle_t::PRIV_FLAGS_CACHED; |
| 159 | } |
| 160 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 161 | flags |= data.allocType; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 162 | uint64_t eBaseAddr = (uint64_t)(eData.base) + eData.offset; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 163 | private_handle_t *hnd = new private_handle_t(data.fd, size, flags, |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 164 | bufferType, format, alignedw, alignedh, |
| 165 | eData.fd, eData.offset, eBaseAddr, width, height); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 166 | |
| 167 | hnd->offset = data.offset; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 168 | hnd->base = (uint64_t)(data.base) + data.offset; |
Naseer Ahmed | a163b73 | 2013-02-12 14:53:33 -0500 | [diff] [blame] | 169 | hnd->gpuaddr = 0; |
Pullakavi Srinivas | 46204e3 | 2016-07-22 12:44:46 +0530 | [diff] [blame] | 170 | ColorSpace_t colorSpace = ITU_R_601; |
Naseer Ahmed | 42e5dde | 2014-03-28 19:32:01 -0400 | [diff] [blame] | 171 | setMetaData(hnd, UPDATE_COLOR_SPACE, (void*) &colorSpace); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 172 | *pHandle = hnd; |
| 173 | } |
| 174 | |
| 175 | ALOGE_IF(err, "gralloc failed err=%s", strerror(-err)); |
| 176 | return err; |
| 177 | } |
| 178 | |
| 179 | void gpu_context_t::getGrallocInformationFromFormat(int inputFormat, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 180 | int *bufferType) |
| 181 | { |
| 182 | *bufferType = BUFFER_TYPE_VIDEO; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 183 | |
Naomi Luis | cffc5bd | 2015-08-28 14:57:31 -0700 | [diff] [blame] | 184 | if (isUncompressedRgbFormat(inputFormat) == TRUE) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 185 | // RGB formats |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 186 | *bufferType = BUFFER_TYPE_UI; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 190 | int gpu_context_t::gralloc_alloc_framebuffer_locked(int usage, |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 191 | buffer_handle_t* pHandle) |
| 192 | { |
| 193 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
| 194 | |
Naseer Ahmed | 8d0d72a | 2014-12-19 16:25:09 -0500 | [diff] [blame] | 195 | // This allocation will only happen when gralloc is in fb mode |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 196 | |
| 197 | if (m->framebuffer == NULL) { |
| 198 | ALOGE("%s: Invalid framebuffer", __FUNCTION__); |
| 199 | return -EINVAL; |
| 200 | } |
| 201 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 202 | const unsigned int bufferMask = m->bufferMask; |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 203 | const uint32_t numBuffers = m->numBuffers; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 204 | unsigned int bufferSize = m->finfo.line_length * m->info.yres; |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 205 | |
| 206 | //adreno needs FB size to be page aligned |
| 207 | bufferSize = roundUpToPageSize(bufferSize); |
| 208 | |
| 209 | if (numBuffers == 1) { |
| 210 | // If we have only one buffer, we never use page-flipping. Instead, |
| 211 | // we return a regular buffer which will be memcpy'ed to the main |
| 212 | // screen when post is called. |
| 213 | int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D; |
| 214 | return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI, |
| 215 | m->fbFormat, m->info.xres, m->info.yres); |
| 216 | } |
| 217 | |
| 218 | if (bufferMask >= ((1LU<<numBuffers)-1)) { |
| 219 | // We ran out of buffers. |
| 220 | return -ENOMEM; |
| 221 | } |
| 222 | |
| 223 | // create a "fake" handle for it |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 224 | uint64_t vaddr = uint64_t(m->framebuffer->base); |
Arun Kumar K.R | f0f853f | 2014-03-17 16:03:21 -0700 | [diff] [blame] | 225 | // As GPU needs ION FD, the private handle is created |
| 226 | // using ION fd and ION flags are set |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 227 | private_handle_t* hnd = new private_handle_t( |
| 228 | dup(m->framebuffer->fd), bufferSize, |
Arun Kumar K.R | f0f853f | 2014-03-17 16:03:21 -0700 | [diff] [blame] | 229 | private_handle_t::PRIV_FLAGS_USES_ION | |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 230 | private_handle_t::PRIV_FLAGS_FRAMEBUFFER, |
| 231 | BUFFER_TYPE_UI, m->fbFormat, m->info.xres, |
| 232 | m->info.yres); |
| 233 | |
| 234 | // find a free slot |
| 235 | for (uint32_t i=0 ; i<numBuffers ; i++) { |
| 236 | if ((bufferMask & (1LU<<i)) == 0) { |
Shalaj Jain | a70b435 | 2014-06-15 13:47:47 -0700 | [diff] [blame] | 237 | m->bufferMask |= (uint32_t)(1LU<<i); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 238 | break; |
| 239 | } |
| 240 | vaddr += bufferSize; |
| 241 | } |
| 242 | hnd->base = vaddr; |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 243 | hnd->offset = (unsigned int)(vaddr - m->framebuffer->base); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 244 | *pHandle = hnd; |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 249 | int gpu_context_t::gralloc_alloc_framebuffer(int usage, |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 250 | buffer_handle_t* pHandle) |
| 251 | { |
| 252 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
| 253 | pthread_mutex_lock(&m->lock); |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 254 | int err = gralloc_alloc_framebuffer_locked(usage, pHandle); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 255 | pthread_mutex_unlock(&m->lock); |
| 256 | return err; |
| 257 | } |
| 258 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 259 | 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] | 260 | buffer_handle_t* pHandle, int* pStride, |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 261 | unsigned int bufferSize) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 262 | if (!pHandle || !pStride) |
| 263 | return -EINVAL; |
| 264 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 265 | unsigned int size; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 266 | int alignedw, alignedh; |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 267 | int grallocFormat = format; |
| 268 | int bufferType; |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 269 | |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 270 | //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on |
| 271 | //the usage bits, gralloc assigns a format. |
Naseer Ahmed | 34d33bc | 2013-05-08 12:28:37 -0400 | [diff] [blame] | 272 | if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED || |
| 273 | format == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
Sushil Chauhan | 3233329 | 2015-02-04 18:56:32 -0800 | [diff] [blame] | 274 | if (usage & GRALLOC_USAGE_PRIVATE_ALLOC_UBWC) |
| 275 | grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC; |
Jeykumar Sankaran | 9bc1a78 | 2015-12-14 18:36:27 -0800 | [diff] [blame] | 276 | else if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) { |
| 277 | if(MDPCapabilityInfo::getInstance().isWBUBWCSupportedByMDP() && |
Ramakant Singh | f8d1c40 | 2017-01-17 19:01:21 +0530 | [diff] [blame] | 278 | !IAllocController::getInstance()->isDisableUBWCForEncoder() && |
Jeykumar Sankaran | 9bc1a78 | 2015-12-14 18:36:27 -0800 | [diff] [blame] | 279 | usage & GRALLOC_USAGE_HW_COMPOSER) |
| 280 | grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS_UBWC; |
| 281 | else |
| 282 | grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12 |
| 283 | } else if((usage & GRALLOC_USAGE_HW_CAMERA_MASK) |
Ramkumar Radhakrishnan | ff51102 | 2013-07-23 16:12:08 -0700 | [diff] [blame] | 284 | == GRALLOC_USAGE_HW_CAMERA_ZSL) |
| 285 | grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 ZSL |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 286 | else if(usage & GRALLOC_USAGE_HW_CAMERA_READ) |
| 287 | grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21 |
Ranjith Kagathi Ananda | 330b21d | 2015-08-17 16:41:08 -0700 | [diff] [blame] | 288 | else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { |
| 289 | if (format == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
Nagesh Subba Reddy | 81a519f | 2015-08-29 18:01:49 -0700 | [diff] [blame] | 290 | grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; //NV21 |
Ranjith Kagathi Ananda | 330b21d | 2015-08-17 16:41:08 -0700 | [diff] [blame] | 291 | } else { |
| 292 | grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP_VENUS; //NV12 preview |
| 293 | } |
| 294 | } else if(usage & GRALLOC_USAGE_HW_COMPOSER) |
Manoj Kumar AVM | cdb4fd5 | 2013-10-25 19:40:45 -0700 | [diff] [blame] | 295 | //XXX: If we still haven't set a format, default to RGBA8888 |
| 296 | grallocFormat = HAL_PIXEL_FORMAT_RGBA_8888; |
Nagesh Subba Reddy | 81a519f | 2015-08-29 18:01:49 -0700 | [diff] [blame] | 297 | else if(format == HAL_PIXEL_FORMAT_YCbCr_420_888) { |
| 298 | //If no other usage flags are detected, default the |
| 299 | //flexible YUV format to NV21_ZSL |
| 300 | grallocFormat = HAL_PIXEL_FORMAT_NV21_ZSL; |
| 301 | } |
Baldev Sahu | 5f673b4 | 2013-12-05 09:49:09 +0530 | [diff] [blame] | 302 | } |
| 303 | |
Sushil Chauhan | 68c793a | 2015-04-16 16:31:22 -0700 | [diff] [blame] | 304 | bool useFbMem = false; |
| 305 | char property[PROPERTY_VALUE_MAX]; |
| 306 | char isUBWC[PROPERTY_VALUE_MAX]; |
| 307 | if (usage & GRALLOC_USAGE_HW_FB) { |
| 308 | if ((property_get("debug.gralloc.map_fb_memory", property, NULL) > 0) && |
| 309 | (!strncmp(property, "1", PROPERTY_VALUE_MAX ) || |
| 310 | (!strncasecmp(property,"true", PROPERTY_VALUE_MAX )))) { |
| 311 | useFbMem = true; |
| 312 | } else { |
Sushil Chauhan | c9853b5 | 2015-09-30 18:43:11 -0700 | [diff] [blame] | 313 | usage &= ~GRALLOC_USAGE_PRIVATE_ALLOC_UBWC; |
Sushil Chauhan | 68c793a | 2015-04-16 16:31:22 -0700 | [diff] [blame] | 314 | if (property_get("debug.gralloc.enable_fb_ubwc", isUBWC, NULL) > 0){ |
| 315 | if ((!strncmp(isUBWC, "1", PROPERTY_VALUE_MAX)) || |
| 316 | (!strncasecmp(isUBWC, "true", PROPERTY_VALUE_MAX))) { |
| 317 | // Allocate UBWC aligned framebuffer |
| 318 | usage |= GRALLOC_USAGE_PRIVATE_ALLOC_UBWC; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 324 | getGrallocInformationFromFormat(grallocFormat, &bufferType); |
Manoj Kumar AVM | 8a22081 | 2013-10-10 11:46:06 -0700 | [diff] [blame] | 325 | size = getBufferSizeAndDimensions(w, h, grallocFormat, usage, alignedw, |
| 326 | alignedh); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 327 | |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 328 | if ((unsigned int)size <= 0) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 329 | return -EINVAL; |
Ramkumar Radhakrishnan | 73f952a | 2013-03-05 14:14:24 -0800 | [diff] [blame] | 330 | size = (bufferSize >= size)? bufferSize : size; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 331 | |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 332 | int err = 0; |
| 333 | if(useFbMem) { |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 334 | err = gralloc_alloc_framebuffer(usage, pHandle); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 335 | } else { |
| 336 | err = gralloc_alloc_buffer(size, usage, pHandle, bufferType, |
Ramkumar Radhakrishnan | ba55eac | 2016-08-26 22:33:48 -0700 | [diff] [blame] | 337 | grallocFormat, w, h); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 338 | } |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 339 | |
| 340 | if (err < 0) { |
| 341 | return err; |
| 342 | } |
| 343 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 344 | *pStride = alignedw; |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | int gpu_context_t::free_impl(private_handle_t const* hnd) { |
| 349 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 350 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 351 | const unsigned int bufferSize = m->finfo.line_length * m->info.yres; |
| 352 | unsigned int index = (unsigned int) ((hnd->base - m->framebuffer->base) |
| 353 | / bufferSize); |
Shalaj Jain | a70b435 | 2014-06-15 13:47:47 -0700 | [diff] [blame] | 354 | m->bufferMask &= (uint32_t)~(1LU<<index); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 355 | } else { |
Jeykumar Sankaran | c1f8682 | 2013-02-20 18:32:01 -0800 | [diff] [blame] | 356 | |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 357 | terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd)); |
| 358 | IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags); |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 359 | int err = memalloc->free_buffer((void*)hnd->base, hnd->size, |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 360 | hnd->offset, hnd->fd); |
| 361 | if(err) |
| 362 | return err; |
| 363 | // free the metadata space |
Saurabh Shah | 8f0ea6f | 2014-05-19 16:48:53 -0700 | [diff] [blame] | 364 | unsigned int size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 365 | err = memalloc->free_buffer((void*)hnd->base_metadata, |
Arun Kumar K.R | 6c85f05 | 2014-01-21 21:47:41 -0800 | [diff] [blame] | 366 | size, hnd->offset_metadata, |
Naseer Ahmed | 47aa15e | 2013-04-10 21:27:09 -0400 | [diff] [blame] | 367 | hnd->fd_metadata); |
| 368 | if (err) |
| 369 | return err; |
| 370 | } |
Saurabh Shah | 7d476ed | 2016-06-27 16:40:58 -0700 | [diff] [blame] | 371 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 372 | delete hnd; |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | 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] | 377 | int usage, buffer_handle_t* pHandle, |
| 378 | int* pStride) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 379 | { |
| 380 | if (!dev) { |
| 381 | return -EINVAL; |
| 382 | } |
| 383 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 384 | return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0); |
| 385 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 386 | int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h, |
| 387 | int format, int usage, |
| 388 | buffer_handle_t* pHandle, int* pStride, |
| 389 | int bufferSize) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 390 | { |
| 391 | if (!dev) { |
| 392 | return -EINVAL; |
| 393 | } |
| 394 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 395 | return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize); |
| 396 | } |
| 397 | |
| 398 | |
| 399 | int gpu_context_t::gralloc_free(alloc_device_t* dev, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 400 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 401 | { |
| 402 | if (private_handle_t::validate(handle) < 0) |
| 403 | return -EINVAL; |
| 404 | |
| 405 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle); |
| 406 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 407 | return gpu->free_impl(hnd); |
| 408 | } |
| 409 | |
| 410 | /*****************************************************************************/ |
| 411 | |
| 412 | int gpu_context_t::gralloc_close(struct hw_device_t *dev) |
| 413 | { |
| 414 | gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev); |
| 415 | if (ctx) { |
| 416 | /* TODO: keep a list of all buffer_handle_t created, and free them |
| 417 | * all here. |
| 418 | */ |
| 419 | delete ctx; |
| 420 | } |
| 421 | return 0; |
| 422 | } |
| 423 | |