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