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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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>
|
|
|
|
|
|
|
|
#include "BufferAllocator.h"
|
|
|
|
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2009-04-17 21:15:18 +00:00
|
|
|
ANDROID_SINGLETON_STATIC_INSTANCE( BufferAllocator )
|
2009-04-16 01:34:24 +00:00
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
Mutex BufferAllocator::sLock;
|
|
|
|
KeyedVector<buffer_handle_t, BufferAllocator::alloc_rec_t> BufferAllocator::sAllocList;
|
|
|
|
|
|
|
|
BufferAllocator::BufferAllocator()
|
|
|
|
: mAllocDev(0)
|
|
|
|
{
|
|
|
|
hw_module_t const* module;
|
|
|
|
int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module);
|
|
|
|
LOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID);
|
|
|
|
if (err == 0) {
|
|
|
|
gralloc_open(module, &mAllocDev);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferAllocator::~BufferAllocator()
|
|
|
|
{
|
|
|
|
gralloc_close(mAllocDev);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BufferAllocator::dump(String8& result) const
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(sLock);
|
|
|
|
KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
|
|
|
|
size_t total = 0;
|
|
|
|
const size_t SIZE = 512;
|
|
|
|
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));
|
2009-05-04 21:17:04 +00:00
|
|
|
snprintf(buffer, SIZE, "%10p: %7.2f KiB | %4u x %4u | %2d | 0x%08x\n",
|
|
|
|
list.keyAt(i), rec.size/1024.0f,
|
2009-04-10 21:24:30 +00:00
|
|
|
rec.w, rec.h, rec.format, rec.usage);
|
|
|
|
result.append(buffer);
|
|
|
|
total += rec.size;
|
|
|
|
}
|
|
|
|
snprintf(buffer, SIZE, "Total allocated: %.2f KB\n", total/1024.0f);
|
|
|
|
result.append(buffer);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
static inline uint32_t clamp(uint32_t c) {
|
|
|
|
return c>0 ? c : 1;
|
|
|
|
}
|
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
status_t BufferAllocator::alloc(uint32_t w, uint32_t h, PixelFormat format,
|
|
|
|
int usage, buffer_handle_t* handle, int32_t* stride)
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
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
|
|
|
|
|
|
|
// make sure to not allocate a 0 x 0 buffer
|
|
|
|
w = clamp(w);
|
|
|
|
h = clamp(h);
|
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
// we have a h/w allocator and h/w buffer is requested
|
|
|
|
status_t err = mAllocDev->alloc(mAllocDev,
|
|
|
|
w, h, format, usage, handle, stride);
|
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
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
LOGW_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);
|
|
|
|
alloc_rec_t rec;
|
|
|
|
rec.w = w;
|
|
|
|
rec.h = h;
|
|
|
|
rec.format = format;
|
|
|
|
rec.usage = usage;
|
|
|
|
rec.vaddr = 0;
|
|
|
|
rec.size = h * stride[0] * bytesPerPixel(format);
|
|
|
|
list.add(*handle, rec);
|
2009-09-28 01:44:09 +00:00
|
|
|
} else {
|
|
|
|
String8 s;
|
|
|
|
dump(s);
|
|
|
|
LOGD("%s", s.string());
|
2009-04-10 21:24:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t BufferAllocator::free(buffer_handle_t handle)
|
|
|
|
{
|
|
|
|
Mutex::Autolock _l(mLock);
|
|
|
|
|
2009-04-16 01:34:24 +00:00
|
|
|
status_t err = mAllocDev->free(mAllocDev, handle);
|
|
|
|
LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err));
|
|
|
|
|
2009-04-10 21:24:30 +00:00
|
|
|
if (err == NO_ERROR) {
|
|
|
|
Mutex::Autolock _l(sLock);
|
|
|
|
KeyedVector<buffer_handle_t, alloc_rec_t>& list(sAllocList);
|
|
|
|
list.removeItem(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
}; // namespace android
|