Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * Copyright (c) 2011-2012 Code Aurora Forum. All rights reserved. |
| 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 | |
| 24 | #include <genlock.h> |
| 25 | |
| 26 | #include "gr.h" |
| 27 | #include "gpu.h" |
| 28 | #include "memalloc.h" |
| 29 | #include "alloc_controller.h" |
| 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 | |
| 50 | int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 51 | buffer_handle_t* pHandle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 52 | { |
| 53 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
| 54 | |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 55 | // we don't support framebuffer allocations with graphics heap flags |
| 56 | if (usage & GRALLOC_HEAP_MASK) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 57 | return -EINVAL; |
| 58 | } |
| 59 | |
| 60 | if (m->framebuffer == NULL) { |
| 61 | ALOGE("%s: Invalid framebuffer", __FUNCTION__); |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | |
| 65 | const uint32_t bufferMask = m->bufferMask; |
| 66 | const uint32_t numBuffers = m->numBuffers; |
| 67 | size_t bufferSize = m->finfo.line_length * m->info.yres; |
| 68 | |
| 69 | //adreno needs FB size to be page aligned |
| 70 | bufferSize = roundUpToPageSize(bufferSize); |
| 71 | |
| 72 | if (numBuffers == 1) { |
| 73 | // If we have only one buffer, we never use page-flipping. Instead, |
| 74 | // we return a regular buffer which will be memcpy'ed to the main |
| 75 | // screen when post is called. |
| 76 | int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D; |
| 77 | return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI, |
| 78 | m->fbFormat, m->info.xres, m->info.yres); |
| 79 | } |
| 80 | |
| 81 | if (bufferMask >= ((1LU<<numBuffers)-1)) { |
| 82 | // We ran out of buffers. |
| 83 | return -ENOMEM; |
| 84 | } |
| 85 | |
Naseer Ahmed | d7f8427 | 2012-12-13 13:09:53 -0500 | [diff] [blame^] | 86 | // create a "fake" handle for it |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 87 | intptr_t vaddr = intptr_t(m->framebuffer->base); |
Naseer Ahmed | d7f8427 | 2012-12-13 13:09:53 -0500 | [diff] [blame^] | 88 | private_handle_t* hnd = new private_handle_t( |
| 89 | dup(m->framebuffer->fd), bufferSize, |
| 90 | private_handle_t::PRIV_FLAGS_USES_ION | |
| 91 | private_handle_t::PRIV_FLAGS_FRAMEBUFFER, |
| 92 | BUFFER_TYPE_UI, m->fbFormat, m->info.xres, |
| 93 | m->info.yres); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 94 | |
| 95 | // find a free slot |
| 96 | for (uint32_t i=0 ; i<numBuffers ; i++) { |
| 97 | if ((bufferMask & (1LU<<i)) == 0) { |
| 98 | m->bufferMask |= (1LU<<i); |
| 99 | break; |
| 100 | } |
| 101 | vaddr += bufferSize; |
| 102 | } |
| 103 | |
| 104 | hnd->base = vaddr; |
| 105 | hnd->offset = vaddr - intptr_t(m->framebuffer->base); |
| 106 | *pHandle = hnd; |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 112 | buffer_handle_t* pHandle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 113 | { |
| 114 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
| 115 | pthread_mutex_lock(&m->lock); |
| 116 | int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle); |
| 117 | pthread_mutex_unlock(&m->lock); |
| 118 | return err; |
| 119 | } |
| 120 | |
| 121 | int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage, |
| 122 | buffer_handle_t* pHandle, int bufferType, |
| 123 | int format, int width, int height) |
| 124 | { |
| 125 | int err = 0; |
| 126 | int flags = 0; |
| 127 | size = roundUpToPageSize(size); |
| 128 | alloc_data data; |
| 129 | data.offset = 0; |
| 130 | data.fd = -1; |
| 131 | data.base = 0; |
| 132 | data.size = size; |
| 133 | if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED) |
| 134 | data.align = 8192; |
| 135 | else |
| 136 | data.align = getpagesize(); |
| 137 | data.pHandle = (unsigned int) pHandle; |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 138 | err = mAllocCtrl->allocate(data, usage); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 139 | |
| 140 | if (usage & GRALLOC_USAGE_PRIVATE_UNSYNCHRONIZED) { |
| 141 | flags |= private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED; |
| 142 | } |
| 143 | |
Naseer Ahmed | 4c588a2 | 2012-07-31 19:12:17 -0700 | [diff] [blame] | 144 | if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_ONLY) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 145 | flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_ONLY; |
| 146 | //The EXTERNAL_BLOCK flag is always an add-on |
Naseer Ahmed | 4c588a2 | 2012-07-31 19:12:17 -0700 | [diff] [blame] | 147 | if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_BLOCK) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 148 | flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_BLOCK; |
Naseer Ahmed | 4c588a2 | 2012-07-31 19:12:17 -0700 | [diff] [blame] | 149 | }if (usage & GRALLOC_USAGE_PRIVATE_EXTERNAL_CC) { |
| 150 | flags |= private_handle_t::PRIV_FLAGS_EXTERNAL_CC; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 154 | if (usage & GRALLOC_USAGE_HW_VIDEO_ENCODER ) { |
| 155 | flags |= private_handle_t::PRIV_FLAGS_VIDEO_ENCODER; |
| 156 | } |
| 157 | |
| 158 | if (usage & GRALLOC_USAGE_HW_CAMERA_WRITE) { |
| 159 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_WRITE; |
| 160 | } |
| 161 | |
| 162 | if (usage & GRALLOC_USAGE_HW_CAMERA_READ) { |
| 163 | flags |= private_handle_t::PRIV_FLAGS_CAMERA_READ; |
| 164 | } |
| 165 | |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 166 | if (err == 0) { |
| 167 | flags |= data.allocType; |
| 168 | private_handle_t* hnd = new private_handle_t(data.fd, size, flags, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 169 | bufferType, format, width, |
| 170 | height); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 171 | |
| 172 | hnd->offset = data.offset; |
| 173 | hnd->base = int(data.base) + data.offset; |
| 174 | *pHandle = hnd; |
| 175 | } |
| 176 | |
| 177 | ALOGE_IF(err, "gralloc failed err=%s", strerror(-err)); |
| 178 | return err; |
| 179 | } |
| 180 | |
| 181 | void gpu_context_t::getGrallocInformationFromFormat(int inputFormat, |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 182 | int *bufferType) |
| 183 | { |
| 184 | *bufferType = BUFFER_TYPE_VIDEO; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 185 | |
Naseer Ahmed | 65f1656 | 2012-06-15 20:53:42 -0700 | [diff] [blame] | 186 | if (inputFormat < 0x7) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 187 | // RGB formats |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 188 | *bufferType = BUFFER_TYPE_UI; |
| 189 | } else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) || |
| 190 | (inputFormat == HAL_PIXEL_FORMAT_RG_88)) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 191 | *bufferType = BUFFER_TYPE_UI; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | 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] | 196 | buffer_handle_t* pHandle, int* pStride, |
| 197 | size_t bufferSize) { |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 198 | if (!pHandle || !pStride) |
| 199 | return -EINVAL; |
| 200 | |
| 201 | size_t size; |
| 202 | int alignedw, alignedh; |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 203 | int grallocFormat = format; |
| 204 | int bufferType; |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 205 | |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 206 | //If input format is HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED then based on |
| 207 | //the usage bits, gralloc assigns a format. |
| 208 | if(format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) { |
| 209 | if(usage & GRALLOC_USAGE_HW_VIDEO_ENCODER) |
| 210 | grallocFormat = HAL_PIXEL_FORMAT_YCbCr_420_SP; //NV12 |
| 211 | else if(usage & GRALLOC_USAGE_HW_CAMERA_READ) |
| 212 | grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21 |
| 213 | else if(usage & GRALLOC_USAGE_HW_CAMERA_WRITE) |
| 214 | grallocFormat = HAL_PIXEL_FORMAT_YCrCb_420_SP; //NV21 |
| 215 | } |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 216 | |
Saurabh Shah | aedf236 | 2012-09-05 11:30:59 -0700 | [diff] [blame] | 217 | getGrallocInformationFromFormat(grallocFormat, &bufferType); |
Naseer Ahmed | 5980250 | 2012-08-21 17:03:27 -0400 | [diff] [blame] | 218 | size = getBufferSizeAndDimensions(w, h, grallocFormat, alignedw, alignedh); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 219 | |
| 220 | if ((ssize_t)size <= 0) |
| 221 | return -EINVAL; |
| 222 | size = (bufferSize >= size)? bufferSize : size; |
| 223 | |
| 224 | // All buffers marked as protected or for external |
| 225 | // display need to go to overlay |
| 226 | if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) || |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 227 | (usage & GRALLOC_USAGE_PROTECTED) || |
| 228 | (usage & GRALLOC_USAGE_PRIVATE_CP_BUFFER)) { |
| 229 | bufferType = BUFFER_TYPE_VIDEO; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 230 | } |
Naseer Ahmed | 768619e | 2012-11-28 17:02:52 -0500 | [diff] [blame] | 231 | |
| 232 | int err = gralloc_alloc_buffer(size, usage, pHandle, bufferType, |
| 233 | grallocFormat, alignedw, alignedh); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 234 | |
| 235 | if (err < 0) { |
| 236 | return err; |
| 237 | } |
| 238 | |
| 239 | // Create a genlock lock for this buffer handle. |
| 240 | err = genlock_create_lock((native_handle_t*)(*pHandle)); |
| 241 | if (err) { |
| 242 | ALOGE("%s: genlock_create_lock failed", __FUNCTION__); |
| 243 | free_impl(reinterpret_cast<private_handle_t*>(pHandle)); |
| 244 | return err; |
| 245 | } |
| 246 | *pStride = alignedw; |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | int gpu_context_t::free_impl(private_handle_t const* hnd) { |
| 251 | private_module_t* m = reinterpret_cast<private_module_t*>(common.module); |
| 252 | if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) { |
| 253 | // free this buffer |
| 254 | const size_t bufferSize = m->finfo.line_length * m->info.yres; |
| 255 | int index = (hnd->base - m->framebuffer->base) / bufferSize; |
| 256 | m->bufferMask &= ~(1<<index); |
| 257 | } else { |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 258 | terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd)); |
Naseer Ahmed | 01d3fd3 | 2012-07-14 21:08:13 -0700 | [diff] [blame] | 259 | IMemAlloc* memalloc = mAllocCtrl->getAllocator(hnd->flags); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 260 | int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 261 | hnd->offset, hnd->fd); |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 262 | if(err) |
| 263 | return err; |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // Release the genlock |
| 267 | int err = genlock_release_lock((native_handle_t*)hnd); |
| 268 | if (err) { |
| 269 | ALOGE("%s: genlock_release_lock failed", __FUNCTION__); |
| 270 | } |
| 271 | |
| 272 | delete hnd; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | 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] | 277 | int usage, buffer_handle_t* pHandle, |
| 278 | int* pStride) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 279 | { |
| 280 | if (!dev) { |
| 281 | return -EINVAL; |
| 282 | } |
| 283 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 284 | return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0); |
| 285 | } |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 286 | int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h, |
| 287 | int format, int usage, |
| 288 | buffer_handle_t* pHandle, int* pStride, |
| 289 | int bufferSize) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 290 | { |
| 291 | if (!dev) { |
| 292 | return -EINVAL; |
| 293 | } |
| 294 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 295 | return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize); |
| 296 | } |
| 297 | |
| 298 | |
| 299 | int gpu_context_t::gralloc_free(alloc_device_t* dev, |
Naseer Ahmed | 29a2681 | 2012-06-14 00:56:20 -0700 | [diff] [blame] | 300 | buffer_handle_t handle) |
Iliyan Malchev | 202a77d | 2012-06-11 14:41:12 -0700 | [diff] [blame] | 301 | { |
| 302 | if (private_handle_t::validate(handle) < 0) |
| 303 | return -EINVAL; |
| 304 | |
| 305 | private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle); |
| 306 | gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev); |
| 307 | return gpu->free_impl(hnd); |
| 308 | } |
| 309 | |
| 310 | /*****************************************************************************/ |
| 311 | |
| 312 | int gpu_context_t::gralloc_close(struct hw_device_t *dev) |
| 313 | { |
| 314 | gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev); |
| 315 | if (ctx) { |
| 316 | /* TODO: keep a list of all buffer_handle_t created, and free them |
| 317 | * all here. |
| 318 | */ |
| 319 | delete ctx; |
| 320 | } |
| 321 | return 0; |
| 322 | } |
| 323 | |