Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
Naseer Ahmed | a163b73 | 2013-02-12 14:53:33 -0500 | [diff] [blame] | 3 | * Copyright (c) 2011-2013 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> |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 29 | #include "mdp_version.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 | |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 33 | #define SZ_1M 0x100000 |
| 34 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 35 | gpu_context_t::gpu_context_t(const private_module_t* module, |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 36 | IAllocController* alloc_ctrl ) : |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 37 | mAllocCtrl(alloc_ctrl) |
| 38 | { |
| 39 | // Zero out the alloc_device_t |
| 40 | memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t)); |
| 41 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 42 | // Initialize the procs |
| 43 | common.tag = HARDWARE_DEVICE_TAG; |
| 44 | common.version = 0; |
| 45 | common.module = const_cast<hw_module_t*>(&module->base.common); |
| 46 | common.close = gralloc_close; |
| 47 | alloc = gralloc_alloc; |
Naseer Ahmed | 7421472 | 2013-02-09 08:11:36 -0500 | [diff] [blame] | 48 | #ifdef QCOM_BSP |
Ramkumar Radhakrishnan | 3d4c0ac | 2012-12-10 15:42:54 -0800 | [diff] [blame] | 49 | allocSize = gralloc_alloc_size; |
Naseer Ahmed | 7421472 | 2013-02-09 08:11:36 -0500 | [diff] [blame] | 50 | #endif |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 51 | free = gralloc_free; |
| 52 | |
| 53 | } |
| 54 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 55 | int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage, |
| 56 | buffer_handle_t* pHandle, int bufferType, |
| 57 | int format, int width, int height) |
| 58 | { |
| 59 | int err = 0; |
| 60 | int flags = 0; |
| 61 | size = roundUpToPageSize(size); |
| 62 | alloc_data data; |
| 63 | data.offset = 0; |
| 64 | data.fd = -1; |
| 65 | data.base = 0; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 66 | if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) |
| 67 | data.align = 8192; |
| 68 | else |
| 69 | data.align = getpagesize(); |
Praveen Chavan | 4e931c1 | 2012-11-28 19:43:17 -0800 | [diff] [blame] | 70 | |
| 71 | /* force 1MB alignment selectively for secure buffers, MDP5 onwards */ |
| 72 | if ((qdutils::MDPVersion::getInstance().getMDPVersion() >= \ |
| 73 | qdutils::MDSS_V5) && (usage & GRALLOC_USAGE_PROTECTED)) { |
| 74 | data.align = ALIGN(data.align, SZ_1M); |
| 75 | size = ALIGN(size, data.align); |
| 76 | } |
| 77 | data.size = size; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 78 | data.pHandle = (unsigned int) 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(); |
| 90 | int eDataUsage = GRALLOC_USAGE_PRIVATE_SYSTEM_HEAP; |
| 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; |
| 97 | //The EXTERNAL_BLOCK flag is always an add-on |
| 98 | if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_BLOCK) { |
| 99 | flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK; |
| 100 | } |
| 101 | if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_CC) { |
| 102 | flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_CC; |
| 103 | } |
| 104 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 105 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 106 | if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) { |
| 107 | flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER; |
| 108 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 109 | |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 110 | if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { |
| 111 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE; |
| 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_CAMERA_READ) { |
| 115 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ; |
| 116 | } |
| 117 | |
Naseer Ahmed | e41ad9c | 2013-04-05 17:59:28 -0400 | [diff] [blame] | 118 | if (usage & GRALLOC_USAGE_HW_COMPOSER) { |
| 119 | flags |= private_handle_t::PRIV_FLAGS_HW_COMPOSER; |
| 120 | } |
| 121 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 122 | flags |= data.allocType; |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 123 | int eBaseAddr = int(eData.base) + eData.offset; |
| 124 | private_handle_t *hnd = new private_handle_t(data.fd, size, flags, |
| 125 | bufferType, format, width, height, eData.fd, eData.offset, |
| 126 | eBaseAddr); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 127 | |
| 128 | hnd->offset = data.offset; |
| 129 | hnd->base = int(data.base) + data.offset; |
Naseer Ahmed | a163b73 | 2013-02-12 14:53:33 -0500 | [diff] [blame] | 130 | hnd->gpuaddr = 0; |
| 131 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 132 | *pHandle = hnd; |
| 133 | } |
| 134 | |
| 135 | ALOGE_IF(err, "gralloc failed err=%s", strerror(-err)); |
Ramkumar Radhakrishnan | 47573e2 | 2012-11-07 11:36:41 -0800 | [diff] [blame] | 136 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 137 | return err; |
| 138 | } |
| 139 | |
| 140 | void gpu_context_t::getGrallocInformationFromFormat(int inputFormat, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 141 | int *bufferType) |
| 142 | { |
| 143 | *bufferType = BUFFER_TYPE_VIDEO; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 144 | |
Naseer Ahmed | 65f1656 | 2012-06-15 20:53:42 -0700 | [diff] [blame] | 145 | if (inputFormat < 0x7) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 146 | // RGB formats |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 147 | *bufferType = BUFFER_TYPE_UI; |
| 148 | } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) || |
| 149 | (inputFormat == HAL_PIXEL_FORMAT_RG_88)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 150 | *bufferType = BUFFER_TYPE_UI; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | 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] | 155 | buffer_handle_t* pHandle, int* pStride, |
| 156 | size_t bufferSize) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 157 | if (!pHandle || !pStride) |
| 158 | return -EINVAL; |
| 159 | |
| 160 | size_t size; |
| 161 | int alignedw, alignedh; |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 162 | int grallocFormat = format; |
| 163 | int bufferType; |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 164 | |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 165 | //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on |
| 166 | //the usage bits, gralloc assigns a format. |
| 167 | if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
| 168 | if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) |
Shuzhen Wang | a11b6ba | 2013-04-21 14:23:05 -0700 | [diff] [blame^] | 169 | grallocFormat = HAL_PIXEL_FORMAT_NV12_ENCODEABLE; //NV12 |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 170 | else if(usage & GRALLOC_USAGE_HW_CAMERA_READ) |
| 171 | grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21 |
| 172 | else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) |
| 173 | grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21 |
| 174 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 175 | |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 176 | getGrallocInformationFromFormat(grallocFormat, &bufferType); |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 177 | size = getBufferSizeAndDimensions(w, h, grallocFormat, alignedw, alignedh); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 178 | |
| 179 | if ((ssize_t)size <= 0) |
| 180 | return -EINVAL; |
Ramkumar Radhakrishnan | 73f952a | 2013-03-05 14:14:24 -0800 | [diff] [blame] | 181 | size = (bufferSize >= size)? bufferSize : size; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 182 | |
| 183 | // All buffers marked as protected or for external |
| 184 | // display need to go to overlay |
| 185 | if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) || |
Sushil Chauhan | 7651a80 | 2013-01-08 16:08:09 -0800 | [diff] [blame] | 186 | (usage & GRALLOC_USAGE_PROTECTED)) { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 187 | bufferType = BUFFER_TYPE_VIDEO; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 188 | } |
Naseer Ahmed | 768619e | 2012-11-28 17:02:52 -0500 | [diff] [blame] | 189 | |
| 190 | int err = gralloc_alloc_buffer(size, usage, pHandle, bufferType, |
| 191 | grallocFormat, alignedw, alignedh); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 192 | |
| 193 | if (err < 0) { |
| 194 | return err; |
| 195 | } |
| 196 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 197 | *pStride = alignedw; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | int gpu_context_t::free_impl(private_handle_t const* hnd) { |
| 202 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
Jeykumar Sankaran | c1f8682 | 2013-02-20 18:32:01 -0800 | [diff] [blame] | 203 | |
| 204 | terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd)); |
| 205 | IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags); |
| 206 | int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size, |
| 207 | hnd->offset, hnd->fd); |
| 208 | if(err) |
| 209 | return err; |
| 210 | // free the metadata space |
| 211 | unsigned long size = ROUND_UP_PAGESIZE(sizeof(MetaData_t)); |
| 212 | err = memalloc->free_buffer((void*)hnd->base_metadata, |
| 213 | (size_t) size, hnd->offset_metadata, |
| 214 | hnd->fd_metadata); |
| 215 | if (err) |
| 216 | return err; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 217 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 218 | delete hnd; |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | 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] | 223 | int usage, buffer_handle_t* pHandle, |
| 224 | int* pStride) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 225 | { |
| 226 | if (!dev) { |
| 227 | return -EINVAL; |
| 228 | } |
| 229 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 230 | return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0); |
| 231 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 232 | int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h, |
| 233 | int format, int usage, |
| 234 | buffer_handle_t* pHandle, int* pStride, |
| 235 | int bufferSize) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 236 | { |
| 237 | if (!dev) { |
| 238 | return -EINVAL; |
| 239 | } |
| 240 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 241 | return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | int gpu_context_t::gralloc_free(alloc_device_t* dev, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 246 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 247 | { |
| 248 | if (private_handle_t::validate(handle) < 0) |
| 249 | return -EINVAL; |
| 250 | |
| 251 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle); |
| 252 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 253 | return gpu->free_impl(hnd); |
| 254 | } |
| 255 | |
| 256 | /*****************************************************************************/ |
| 257 | |
| 258 | int gpu_context_t::gralloc_close(struct hw_device_t *dev) |
| 259 | { |
| 260 | gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev); |
| 261 | if (ctx) { |
| 262 | /* TODO: keep a list of all buffer_handle_t created, and free them |
| 263 | * all here. |
| 264 | */ |
| 265 | delete ctx; |
| 266 | } |
| 267 | return 0; |
| 268 | } |
| 269 | |