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