2009-04-10 21:24:30 +00:00
|
|
|
/*
|
|
|
|
**
|
|
|
|
** Copyright 2009, The Android Open Source Project
|
|
|
|
**
|
|
|
|
** 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.
|
|
|
|
*/
|
|
|
|
|
2010-04-15 21:57:39 +00:00
|
|
|
#define LOG_TAG "GraphicBufferAllocator"
|
2012-03-01 04:43:29 +00:00
|
|
|
#define ATRACE_TAG ATRACE_TAG_GRAPHICS
|
2010-04-15 21:57:39 +00:00
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
#include <cutils/log.h>
|
2009-04-16 01:34:24 +00:00
|
|
|
|
|
|
|
#include <utils/Singleton.h>
|
2009-04-10 21:24:30 +00:00
|
|
|
#include <utils/String8.h>
|
2012-03-01 04:43:29 +00:00
|
|
|
#include <utils/Trace.h>
|
2009-04-10 21:24:30 +00:00
|
|
|
|
2009-10-06 00:07:12 +00:00
|
|
|
#include <ui/GraphicBufferAllocator.h>
|
2009-04-10 21:24:30 +00:00
|
|
|
|
|
|
|
namespace android {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2009-10-06 00:07:12 +00:00
|
|
|
ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator )
|
2009-04-16 01:34:24 +00:00
|
|
|
|
2009-10-06 00:07:12 +00:00
|
|
|
Mutex GraphicBufferAllocator::sLock;
|
2009-10-06 01:19:57 +00:00
|
|
|
KeyedVector<buffer_handle_t,
|
|
|
|
GraphicBufferAllocator::alloc_rec_t> GraphicBufferAllocator::sAllocList;
|
2009-04-10 21:24:30 +00:00
|
|
|
|
2009-10-06 00:07:12 +00:00
|
|
|
GraphicBufferAllocator::GraphicBufferAllocator()
|
2009-04-10 21:24:30 +00:00
|
|
|
: mAllocDev(0)
|
|
|
|
{
|
|
|
|
hw_module_t const* module;
|
|
|
|
int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
|
2009-04-10 21:24:30 +00:00
|
|
|
if (err == 0) {
|
|
|
|
gralloc_open(module, &mAllocDev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-06 00:07:12 +00:00
|
|
|
GraphicBufferAllocator::~GraphicBufferAllocator()
|
2009-04-10 21:24:30 +00:00
|
|
|
{
|
|
|
|
gralloc_close(mAllocDev);
|
|
|
|
}
|
|
|
|
|
2009-10-06 00:07:12 +00:00
|
|
|
void GraphicBufferAllocator::dump(String8& result) const
|
2009-04-10 21:24:30 +00:00
|
|
|
{
|
|
|
|
Mutex::Autolock _l(sLock);
|
|
|
|
KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
|
|
|
|
size_t total = 0;
|
2010-12-02 00:38:01 +00:00
|
|
|
const size_t SIZE = 4096;
|
2009-04-10 21:24:30 +00:00
|
|
|
char buffer[SIZE];
|
|
|
|
snprintf(buffer, SIZE, "Allocated buffers:\n");
|
|
|
|
result.append(buffer);
|
|
|
|
const size_t c = list.size();
|
|
|
|
for (size_t i=0 ; i<c ; i++) {
|
|
|
|
const alloc_rec_t& rec(list.valueAt(i));
|
2011-07-29 23:35:41 +00:00
|
|
|
if (rec.size) {
|
|
|
|
snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u (%4u) x %4u | %8X | 0x%08x\n",
|
|
|
|
list.keyAt(i), rec.size/1024.0f,
|
|
|
|
rec.w, rec.s, rec.h, rec.format, rec.usage);
|
|
|
|
} else {
|
|
|
|
snprintf(buffer, SIZE, "%10p: unknown | %4u (%4u) x %4u | %8X | 0x%08x\n",
|
|
|
|
list.keyAt(i),
|
|
|
|
rec.w, rec.s, rec.h, rec.format, rec.usage);
|
|
|
|
}
|
2009-04-10 21:24:30 +00:00
|
|
|
result.append(buffer);
|
|
|
|
total += rec.size;
|
|
|
|
}
|
2011-07-29 23:35:41 +00:00
|
|
|
snprintf(buffer, SIZE, "Total allocated (estimate): %.2f KB\n", total/1024.0f);
|
2009-04-10 21:24:30 +00:00
|
|
|
result.append(buffer);
|
2010-12-02 00:38:01 +00:00
|
|
|
if (mAllocDev->common.version >= 1 && mAllocDev->dump) {
|
|
|
|
mAllocDev->dump(mAllocDev, buffer, SIZE);
|
|
|
|
result.append(buffer);
|
|
|
|
}
|
2009-04-10 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
2010-12-04 01:33:09 +00:00
|
|
|
void GraphicBufferAllocator::dumpToSystemLog()
|
|
|
|
{
|
|
|
|
String8 s;
|
|
|
|
GraphicBufferAllocator::getInstance().dump(s);
|
2011-12-20 16:23:08 +00:00
|
|
|
ALOGD("%s", s.string());
|
2010-12-04 01:33:09 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 01:06:44 +00:00
|
|
|
class BufferLiberatorThread : public Thread {
|
|
|
|
public:
|
fix [2068105] implement queueBuffer/lockBuffer/dequeueBuffer properly
Rewrote SurfaceFlinger's buffer management from the ground-up.
The design now support an arbitrary number of buffers per surface, however the current implementation is limited to four. Currently only 2 buffers are used in practice.
The main new feature is to be able to dequeue all buffers at once (very important when there are only two).
A client can dequeue all buffers until there are none available, it can lock all buffers except the last one that is used for composition. The client will block then, until a new buffer is enqueued.
The current implementation requires that buffers are locked in the same order they are dequeued and enqueued in the same order they are locked. Only one buffer can be locked at a time.
eg. Allowed sequence: DQ, DQ, LOCK, Q, LOCK, Q
eg. Forbidden sequence: DQ, DQ, LOCK, LOCK, Q, Q
2009-09-07 23:32:45 +00:00
|
|
|
|
2012-12-11 01:06:44 +00:00
|
|
|
static void queueCaptiveBuffer(buffer_handle_t handle) {
|
|
|
|
size_t queueSize;
|
|
|
|
{
|
|
|
|
Mutex::Autolock lock(sMutex);
|
|
|
|
if (sThread == NULL) {
|
|
|
|
sThread = new BufferLiberatorThread;
|
|
|
|
sThread->run("BufferLiberator");
|
|
|
|
}
|
fix [2068105] implement queueBuffer/lockBuffer/dequeueBuffer properly
Rewrote SurfaceFlinger's buffer management from the ground-up.
The design now support an arbitrary number of buffers per surface, however the current implementation is limited to four. Currently only 2 buffers are used in practice.
The main new feature is to be able to dequeue all buffers at once (very important when there are only two).
A client can dequeue all buffers until there are none available, it can lock all buffers except the last one that is used for composition. The client will block then, until a new buffer is enqueued.
The current implementation requires that buffers are locked in the same order they are dequeued and enqueued in the same order they are locked. Only one buffer can be locked at a time.
eg. Allowed sequence: DQ, DQ, LOCK, Q, LOCK, Q
eg. Forbidden sequence: DQ, DQ, LOCK, LOCK, Q, Q
2009-09-07 23:32:45 +00:00
|
|
|
|
2012-12-11 01:06:44 +00:00
|
|
|
sThread->mQueue.push_back(handle);
|
|
|
|
sThread->mQueuedCondition.signal();
|
|
|
|
queueSize = sThread->mQueue.size();
|
2011-07-29 23:35:41 +00:00
|
|
|
}
|
2009-04-10 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 01:06:44 +00:00
|
|
|
static void waitForLiberation() {
|
|
|
|
Mutex::Autolock lock(sMutex);
|
2009-04-10 21:24:30 +00:00
|
|
|
|
2012-12-11 01:06:44 +00:00
|
|
|
waitForLiberationLocked();
|
|
|
|
}
|
2012-12-07 08:38:36 +00:00
|
|
|
|
2012-12-11 01:06:44 +00:00
|
|
|
static void maybeWaitForLiberation() {
|
|
|
|
Mutex::Autolock lock(sMutex);
|
|
|
|
if (sThread != NULL) {
|
|
|
|
if (sThread->mQueue.size() > 8) {
|
|
|
|
waitForLiberationLocked();
|
|
|
|
}
|
2012-12-07 08:38:36 +00:00
|
|
|
}
|
|
|
|
}
|
2010-12-09 00:40:01 +00:00
|
|
|
|
2012-12-07 08:38:36 +00:00
|
|
|
private:
|
2009-10-06 01:19:57 +00:00
|
|
|
|
2012-12-07 08:38:36 +00:00
|
|
|
BufferLiberatorThread() {}
|
|
|
|
|
|
|
|
virtual bool threadLoop() {
|
|
|
|
buffer_handle_t handle;
|
2012-12-11 01:06:44 +00:00
|
|
|
{ // Scope for mutex
|
|
|
|
Mutex::Autolock lock(sMutex);
|
2012-12-07 08:38:36 +00:00
|
|
|
while (mQueue.isEmpty()) {
|
2012-12-11 01:06:44 +00:00
|
|
|
mQueuedCondition.wait(sMutex);
|
2012-12-07 08:38:36 +00:00
|
|
|
}
|
|
|
|
handle = mQueue[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t err;
|
|
|
|
GraphicBufferAllocator& gba(GraphicBufferAllocator::get());
|
|
|
|
{ // Scope for tracing
|
|
|
|
ATRACE_NAME("gralloc::free");
|
|
|
|
err = gba.mAllocDev->free(gba.mAllocDev, handle);
|
|
|
|
}
|
|
|
|
ALOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
|
|
|
|
|
|
|
|
if (err == NO_ERROR) {
|
|
|
|
Mutex::Autolock _l(GraphicBufferAllocator::sLock);
|
|
|
|
KeyedVector<buffer_handle_t, GraphicBufferAllocator::alloc_rec_t>&
|
|
|
|
list(GraphicBufferAllocator::sAllocList);
|
|
|
|
list.removeItem(handle);
|
|
|
|
}
|
|
|
|
|
2012-12-11 01:06:44 +00:00
|
|
|
{ // Scope for mutex
|
|
|
|
Mutex::Autolock lock(sMutex);
|
|
|
|
mQueue.removeAt(0);
|
|
|
|
mFreedCondition.broadcast();
|
|
|
|
}
|
|
|
|
|
2012-12-07 08:38:36 +00:00
|
|
|
return true;
|
2009-04-10 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
2012-12-11 01:06:44 +00:00
|
|
|
static void waitForLiberationLocked() {
|
|
|
|
if (sThread == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsecs_t timeout = 500 * 1000 * 1000;
|
|
|
|
nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC);
|
|
|
|
nsecs_t timeToStop = now + timeout;
|
|
|
|
while (!sThread->mQueue.isEmpty() && now < timeToStop) {
|
|
|
|
sThread->mFreedCondition.waitRelative(sMutex, timeToStop - now);
|
|
|
|
now = systemTime(SYSTEM_TIME_MONOTONIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sThread->mQueue.isEmpty()) {
|
|
|
|
ALOGW("waitForLiberationLocked timed out");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Mutex sMutex;
|
|
|
|
static sp<BufferLiberatorThread> sThread;
|
2012-12-07 08:38:36 +00:00
|
|
|
Vector<buffer_handle_t> mQueue;
|
2012-12-11 01:06:44 +00:00
|
|
|
Condition mQueuedCondition;
|
|
|
|
Condition mFreedCondition;
|
2012-12-07 08:38:36 +00:00
|
|
|
};
|
|
|
|
|
2012-12-11 01:06:44 +00:00
|
|
|
Mutex BufferLiberatorThread::sMutex;
|
|
|
|
sp<BufferLiberatorThread> BufferLiberatorThread::sThread;
|
|
|
|
|
|
|
|
status_t GraphicBufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format,
|
|
|
|
int usage, buffer_handle_t* handle, int32_t* stride)
|
|
|
|
{
|
|
|
|
ATRACE_CALL();
|
|
|
|
// make sure to not allocate a N x 0 or 0 x N buffer, since this is
|
|
|
|
// allowed from an API stand-point allocate a 1x1 buffer instead.
|
|
|
|
if (!w || !h)
|
|
|
|
w = h = 1;
|
|
|
|
|
|
|
|
// we have a h/w allocator and h/w buffer is requested
|
|
|
|
status_t err;
|
|
|
|
|
|
|
|
// If too many async frees are queued up then wait for some of them to
|
|
|
|
// complete before attempting to allocate more memory. This is exercised
|
|
|
|
// by the android.opengl.cts.GLSurfaceViewTest CTS test.
|
|
|
|
BufferLiberatorThread::maybeWaitForLiberation();
|
|
|
|
|
|
|
|
err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride);
|
|
|
|
|
|
|
|
if (err != NO_ERROR) {
|
|
|
|
ALOGW("WOW! gralloc alloc failed, waiting for pending frees!");
|
|
|
|
BufferLiberatorThread::waitForLiberation();
|
|
|
|
err = mAllocDev->alloc(mAllocDev, w, h, format, usage, handle, stride);
|
|
|
|
}
|
|
|
|
|
|
|
|
ALOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)",
|
|
|
|
w, h, format, usage, err, strerror(-err));
|
|
|
|
|
|
|
|
if (err == NO_ERROR) {
|
|
|
|
Mutex::Autolock _l(sLock);
|
|
|
|
KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
|
|
|
|
int bpp = bytesPerPixel(format);
|
|
|
|
if (bpp < 0) {
|
|
|
|
// probably a HAL custom format. in any case, we don't know
|
|
|
|
// what its pixel size is.
|
|
|
|
bpp = 0;
|
|
|
|
}
|
|
|
|
alloc_rec_t rec;
|
|
|
|
rec.w = w;
|
|
|
|
rec.h = h;
|
|
|
|
rec.s = *stride;
|
|
|
|
rec.format = format;
|
|
|
|
rec.usage = usage;
|
|
|
|
rec.size = h * stride[0] * bpp;
|
|
|
|
list.add(*handle, rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-07 08:38:36 +00:00
|
|
|
status_t GraphicBufferAllocator::free(buffer_handle_t handle)
|
|
|
|
{
|
|
|
|
BufferLiberatorThread::queueCaptiveBuffer(handle);
|
|
|
|
return NO_ERROR;
|
2009-04-10 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|