2010-04-22 00:33:32 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2010 The Android Open Source Project
|
2011-11-16 21:41:37 +00:00
|
|
|
* Copyright (c) 2011 Code Aurora Forum. All rights reserved.
|
2010-04-22 00:33:32 +00:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2011-07-05 22:48:18 +00:00
|
|
|
#include <cutils/properties.h>
|
2011-11-16 21:41:37 +00:00
|
|
|
#include <sys/mman.h>
|
2011-07-05 22:48:18 +00:00
|
|
|
|
2011-11-20 08:12:03 +00:00
|
|
|
#include <genlock.h>
|
|
|
|
|
2010-04-22 00:33:32 +00:00
|
|
|
#include "gr.h"
|
|
|
|
#include "gpu.h"
|
2011-11-16 21:41:37 +00:00
|
|
|
#include "memalloc.h"
|
|
|
|
#include "alloc_controller.h"
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-16 21:41:37 +00:00
|
|
|
using namespace gralloc;
|
|
|
|
using android::sp;
|
|
|
|
|
|
|
|
gpu_context_t::gpu_context_t(const private_module_t* module,
|
|
|
|
sp<IAllocController> alloc_ctrl ) :
|
|
|
|
mAllocCtrl(alloc_ctrl)
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
|
|
|
// Zero out the alloc_device_t
|
|
|
|
memset(static_cast<alloc_device_t*>(this), 0, sizeof(alloc_device_t));
|
|
|
|
|
2011-07-05 22:48:18 +00:00
|
|
|
char property[PROPERTY_VALUE_MAX];
|
|
|
|
if (property_get("debug.sf.hw", property, NULL) > 0) {
|
|
|
|
if(atoi(property) == 0) {
|
|
|
|
//debug.sf.hw = 0
|
|
|
|
compositionType = CPU_COMPOSITION;
|
|
|
|
} else { //debug.sf.hw = 1
|
|
|
|
// Get the composition type
|
|
|
|
property_get("debug.composition.type", property, NULL);
|
|
|
|
if (property == NULL) {
|
|
|
|
compositionType = GPU_COMPOSITION;
|
|
|
|
} else if ((strncmp(property, "mdp", 3)) == 0) {
|
|
|
|
compositionType = MDP_COMPOSITION;
|
|
|
|
} else if ((strncmp(property, "c2d", 3)) == 0) {
|
|
|
|
compositionType = C2D_COMPOSITION;
|
|
|
|
} else {
|
|
|
|
compositionType = GPU_COMPOSITION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { //debug.sf.hw is not set. Use cpu composition
|
|
|
|
compositionType = CPU_COMPOSITION;
|
|
|
|
}
|
|
|
|
|
2010-04-22 00:33:32 +00:00
|
|
|
// Initialize the procs
|
|
|
|
common.tag = HARDWARE_DEVICE_TAG;
|
|
|
|
common.version = 0;
|
|
|
|
common.module = const_cast<hw_module_t*>(&module->base.common);
|
|
|
|
common.close = gralloc_close;
|
|
|
|
alloc = gralloc_alloc;
|
2011-11-19 09:52:20 +00:00
|
|
|
#if 0
|
2011-03-16 20:32:07 +00:00
|
|
|
allocSize = gralloc_alloc_size;
|
2011-11-19 09:52:20 +00:00
|
|
|
#endif
|
2010-04-22 00:33:32 +00:00
|
|
|
free = gralloc_free;
|
2011-11-16 21:41:37 +00:00
|
|
|
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int gpu_context_t::gralloc_alloc_framebuffer_locked(size_t size, int usage,
|
|
|
|
buffer_handle_t* pHandle)
|
|
|
|
{
|
|
|
|
private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
|
|
|
|
|
|
|
|
// we don't support allocations with both the FB and PMEM_ADSP flags
|
2011-11-03 01:29:14 +00:00
|
|
|
if (usage & GRALLOC_USAGE_PRIVATE_ADSP_HEAP) {
|
2010-04-22 00:33:32 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m->framebuffer == NULL) {
|
2011-11-16 21:41:37 +00:00
|
|
|
LOGE("%s: Invalid framebuffer", __FUNCTION__);
|
|
|
|
return -EINVAL;
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const uint32_t bufferMask = m->bufferMask;
|
|
|
|
const uint32_t numBuffers = m->numBuffers;
|
|
|
|
const size_t bufferSize = m->finfo.line_length * m->info.yres;
|
|
|
|
if (numBuffers == 1) {
|
|
|
|
// If we have only one buffer, we never use page-flipping. Instead,
|
|
|
|
// we return a regular buffer which will be memcpy'ed to the main
|
|
|
|
// screen when post is called.
|
|
|
|
int newUsage = (usage & ~GRALLOC_USAGE_HW_FB) | GRALLOC_USAGE_HW_2D;
|
2011-11-16 21:41:37 +00:00
|
|
|
return gralloc_alloc_buffer(bufferSize, newUsage, pHandle, BUFFER_TYPE_UI,
|
|
|
|
m->fbFormat, m->info.xres, m->info.yres);
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bufferMask >= ((1LU<<numBuffers)-1)) {
|
|
|
|
// We ran out of buffers.
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a "fake" handles for it
|
2011-11-16 21:41:37 +00:00
|
|
|
// Set the PMEM flag as well, since adreno
|
|
|
|
// treats the FB memory as pmem
|
2010-04-22 00:33:32 +00:00
|
|
|
intptr_t vaddr = intptr_t(m->framebuffer->base);
|
2011-03-08 01:31:51 +00:00
|
|
|
private_handle_t* hnd = new private_handle_t(dup(m->framebuffer->fd), bufferSize,
|
2010-04-22 00:33:32 +00:00
|
|
|
private_handle_t::PRIV_FLAGS_USES_PMEM |
|
2011-03-08 01:35:59 +00:00
|
|
|
private_handle_t::PRIV_FLAGS_FRAMEBUFFER,
|
2011-11-16 21:41:37 +00:00
|
|
|
BUFFER_TYPE_UI, m->fbFormat, m->info.xres,
|
|
|
|
m->info.yres);
|
2010-04-22 00:33:32 +00:00
|
|
|
|
|
|
|
// find a free slot
|
|
|
|
for (uint32_t i=0 ; i<numBuffers ; i++) {
|
|
|
|
if ((bufferMask & (1LU<<i)) == 0) {
|
|
|
|
m->bufferMask |= (1LU<<i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
vaddr += bufferSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
hnd->base = vaddr;
|
|
|
|
hnd->offset = vaddr - intptr_t(m->framebuffer->base);
|
|
|
|
*pHandle = hnd;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int gpu_context_t::gralloc_alloc_framebuffer(size_t size, int usage,
|
|
|
|
buffer_handle_t* pHandle)
|
|
|
|
{
|
|
|
|
private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
|
|
|
|
pthread_mutex_lock(&m->lock);
|
|
|
|
int err = gralloc_alloc_framebuffer_locked(size, usage, pHandle);
|
|
|
|
pthread_mutex_unlock(&m->lock);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-11-16 21:41:37 +00:00
|
|
|
int gpu_context_t::gralloc_alloc_buffer(size_t size, int usage,
|
|
|
|
buffer_handle_t* pHandle, int bufferType,
|
|
|
|
int format, int width, int height)
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
|
|
|
int err = 0;
|
|
|
|
int flags = 0;
|
|
|
|
size = roundUpToPageSize(size);
|
2011-11-16 21:41:37 +00:00
|
|
|
alloc_data data;
|
|
|
|
data.offset = 0;
|
|
|
|
data.fd = -1;
|
|
|
|
data.base = 0;
|
|
|
|
data.size = size;
|
|
|
|
if(format == HAL_PIXEL_FORMAT_YCbCr_420_SP_TILED)
|
|
|
|
data.align = 8192;
|
|
|
|
else
|
|
|
|
data.align = getpagesize();
|
|
|
|
data.pHandle = (unsigned int) pHandle;
|
|
|
|
err = mAllocCtrl->allocate(data, usage, compositionType);
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2012-01-19 18:23:16 +00:00
|
|
|
if (usage & GRALLOC_USAGE_PRIVATE_UNSYNCHRONIZED) {
|
|
|
|
flags |= private_handle_t::PRIV_FLAGS_UNSYNCHRONIZED;
|
|
|
|
}
|
|
|
|
|
2010-04-22 00:33:32 +00:00
|
|
|
if (err == 0) {
|
2011-11-16 21:41:37 +00:00
|
|
|
flags |= data.allocType;
|
|
|
|
private_handle_t* hnd = new private_handle_t(data.fd, size, flags,
|
|
|
|
bufferType, format, width, height);
|
|
|
|
|
|
|
|
hnd->offset = data.offset;
|
|
|
|
hnd->base = int(data.base) + data.offset;
|
2010-04-22 00:33:32 +00:00
|
|
|
*pHandle = hnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOGE_IF(err, "gralloc failed err=%s", strerror(-err));
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-11-16 21:41:37 +00:00
|
|
|
void gpu_context_t::getGrallocInformationFromFormat(int inputFormat,
|
|
|
|
int *colorFormat,
|
|
|
|
int *bufferType)
|
2011-03-08 01:35:59 +00:00
|
|
|
{
|
|
|
|
*bufferType = BUFFER_TYPE_VIDEO;
|
|
|
|
*colorFormat = inputFormat;
|
|
|
|
|
2011-05-12 20:07:20 +00:00
|
|
|
if (inputFormat == HAL_PIXEL_FORMAT_YV12) {
|
2011-06-28 18:58:56 +00:00
|
|
|
*bufferType = BUFFER_TYPE_VIDEO;
|
2011-05-12 20:07:20 +00:00
|
|
|
} else if (inputFormat & S3D_FORMAT_MASK) {
|
|
|
|
// S3D format
|
|
|
|
*colorFormat = COLOR_FORMAT(inputFormat);
|
|
|
|
} else if (inputFormat & INTERLACE_MASK) {
|
|
|
|
// Interlaced
|
|
|
|
*colorFormat = inputFormat ^ HAL_PIXEL_FORMAT_INTERLACE;
|
|
|
|
} else if (inputFormat < 0x7) {
|
|
|
|
// RGB formats
|
|
|
|
*colorFormat = inputFormat;
|
|
|
|
*bufferType = BUFFER_TYPE_UI;
|
|
|
|
} else if ((inputFormat == HAL_PIXEL_FORMAT_R_8) ||
|
|
|
|
(inputFormat == HAL_PIXEL_FORMAT_RG_88)) {
|
|
|
|
*colorFormat = inputFormat;
|
|
|
|
*bufferType = BUFFER_TYPE_UI;
|
2011-03-08 01:35:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-22 00:33:32 +00:00
|
|
|
int gpu_context_t::alloc_impl(int w, int h, int format, int usage,
|
2011-11-16 21:41:37 +00:00
|
|
|
buffer_handle_t* pHandle, int* pStride, size_t bufferSize) {
|
2010-04-22 00:33:32 +00:00
|
|
|
if (!pHandle || !pStride)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2012-01-30 09:20:08 +00:00
|
|
|
size_t size;
|
|
|
|
int alignedw, alignedh;
|
2011-05-12 20:07:20 +00:00
|
|
|
int colorFormat, bufferType;
|
|
|
|
getGrallocInformationFromFormat(format, &colorFormat, &bufferType);
|
2012-01-30 09:20:08 +00:00
|
|
|
size = getBufferSizeAndDimensions(w, h, colorFormat, alignedw, alignedh);
|
2010-04-22 00:33:32 +00:00
|
|
|
|
|
|
|
if ((ssize_t)size <= 0)
|
|
|
|
return -EINVAL;
|
2011-03-16 20:32:07 +00:00
|
|
|
size = (bufferSize >= size)? bufferSize : size;
|
|
|
|
|
2011-06-28 20:35:14 +00:00
|
|
|
// All buffers marked as protected or for external
|
|
|
|
// display need to go to overlay
|
|
|
|
if ((usage & GRALLOC_USAGE_EXTERNAL_DISP) ||
|
|
|
|
(usage & GRALLOC_USAGE_PROTECTED)) {
|
|
|
|
bufferType = BUFFER_TYPE_VIDEO;
|
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
int err;
|
|
|
|
if (usage & GRALLOC_USAGE_HW_FB) {
|
|
|
|
err = gralloc_alloc_framebuffer(size, usage, pHandle);
|
|
|
|
} else {
|
2011-11-16 21:41:37 +00:00
|
|
|
err = gralloc_alloc_buffer(size, usage, pHandle, bufferType,
|
|
|
|
format, alignedw, alignedh);
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err < 0) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-11-20 08:12:03 +00:00
|
|
|
// Create a genlock lock for this buffer handle.
|
|
|
|
err = genlock_create_lock((native_handle_t*)(*pHandle));
|
|
|
|
if (err) {
|
|
|
|
LOGE("%s: genlock_create_lock failed", __FUNCTION__);
|
|
|
|
free_impl(reinterpret_cast<private_handle_t*>(pHandle));
|
|
|
|
return err;
|
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
*pStride = alignedw;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gpu_context_t::free_impl(private_handle_t const* hnd) {
|
|
|
|
private_module_t* m = reinterpret_cast<private_module_t*>(common.module);
|
|
|
|
if (hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) {
|
|
|
|
// free this buffer
|
|
|
|
const size_t bufferSize = m->finfo.line_length * m->info.yres;
|
|
|
|
int index = (hnd->base - m->framebuffer->base) / bufferSize;
|
2011-11-16 21:41:37 +00:00
|
|
|
m->bufferMask &= ~(1<<index);
|
2011-03-08 01:46:28 +00:00
|
|
|
} else {
|
2011-11-16 21:41:37 +00:00
|
|
|
sp<IMemAlloc> memalloc = mAllocCtrl->getAllocator(hnd->flags);
|
|
|
|
int err = memalloc->free_buffer((void*)hnd->base, (size_t) hnd->size,
|
|
|
|
hnd->offset, hnd->fd);
|
|
|
|
if(err)
|
|
|
|
return err;
|
2011-11-19 17:55:49 +00:00
|
|
|
terminateBuffer(&m->base, const_cast<private_handle_t*>(hnd));
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
2011-11-20 08:12:03 +00:00
|
|
|
|
|
|
|
// Release the genlock
|
|
|
|
int err = genlock_release_lock((native_handle_t*)hnd);
|
|
|
|
if (err) {
|
|
|
|
LOGE("%s: genlock_release_lock failed", __FUNCTION__);
|
|
|
|
}
|
|
|
|
|
2011-11-16 21:41:37 +00:00
|
|
|
delete hnd;
|
2010-04-22 00:33:32 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int gpu_context_t::gralloc_alloc(alloc_device_t* dev, int w, int h, int format,
|
|
|
|
int usage, buffer_handle_t* pHandle, int* pStride)
|
|
|
|
{
|
|
|
|
if (!dev) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
|
2011-03-16 20:32:07 +00:00
|
|
|
return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, 0);
|
|
|
|
}
|
|
|
|
int gpu_context_t::gralloc_alloc_size(alloc_device_t* dev, int w, int h, int format,
|
|
|
|
int usage, buffer_handle_t* pHandle, int* pStride, int bufferSize)
|
|
|
|
{
|
|
|
|
if (!dev) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
|
|
|
|
return gpu->alloc_impl(w, h, format, usage, pHandle, pStride, bufferSize);
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
2011-11-16 21:41:37 +00:00
|
|
|
|
2010-04-22 00:33:32 +00:00
|
|
|
int gpu_context_t::gralloc_free(alloc_device_t* dev,
|
2011-11-16 21:41:37 +00:00
|
|
|
buffer_handle_t handle)
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
|
|
|
if (private_handle_t::validate(handle) < 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
private_handle_t const* hnd = reinterpret_cast<private_handle_t const*>(handle);
|
|
|
|
gpu_context_t* gpu = reinterpret_cast<gpu_context_t*>(dev);
|
|
|
|
return gpu->free_impl(hnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
int gpu_context_t::gralloc_close(struct hw_device_t *dev)
|
|
|
|
{
|
|
|
|
gpu_context_t* ctx = reinterpret_cast<gpu_context_t*>(dev);
|
|
|
|
if (ctx) {
|
|
|
|
/* TODO: keep a list of all buffer_handle_t created, and free them
|
|
|
|
* all here.
|
|
|
|
*/
|
|
|
|
delete ctx;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|