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