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