From 1473f46cbc82aa6f0ba744cc896a36923823d55b Mon Sep 17 00:00:00 2001 From: Mathias Agopian Date: Fri, 10 Apr 2009 14:24:30 -0700 Subject: Integrate from //sandbox/mathias/donut/...@145728 SurfaceFlinger rework for new EGL driver model support. --- libs/surfaceflinger/BufferAllocator.cpp | 162 ++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 libs/surfaceflinger/BufferAllocator.cpp (limited to 'libs/surfaceflinger/BufferAllocator.cpp') diff --git a/libs/surfaceflinger/BufferAllocator.cpp b/libs/surfaceflinger/BufferAllocator.cpp new file mode 100644 index 0000000..ac32985 --- /dev/null +++ b/libs/surfaceflinger/BufferAllocator.cpp @@ -0,0 +1,162 @@ +/* +** +** 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 +#include +#include +#include +#include + +#include + +#include "BufferAllocator.h" + +// FIXME: ANDROID_GRALLOC_DEBUG must only be used with *our* gralloc +#define ANDROID_GRALLOC_DEBUG 1 + + +namespace android { +// --------------------------------------------------------------------------- + +Mutex BufferAllocator::sLock; +KeyedVector 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& 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 ; ialloc(mAllocDev, + w, h, format, usage, handle, stride); + 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& 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); + } + + return err; +} + +status_t BufferAllocator::free(buffer_handle_t handle) +{ + Mutex::Autolock _l(mLock); + +#if ANDROID_GRALLOC_DEBUG + if (handle->data[2]) { + CallStack s; + s.update(); + s.dump(""); + } +#endif + + status_t err = mAllocDev->free(mAllocDev, handle); + LOGW_IF(err, "free(...) failed %d (%s)", err, strerror(-err)); + + if (err == NO_ERROR) { + Mutex::Autolock _l(sLock); + KeyedVector& list(sAllocList); + list.removeItem(handle); + } + + return err; +} + +status_t BufferAllocator::map(buffer_handle_t handle, void** addr) +{ + Mutex::Autolock _l(mLock); + status_t err = BufferMapper::get().map(handle, addr); + if (err == NO_ERROR) { + Mutex::Autolock _l(sLock); + KeyedVector& list(sAllocList); + ssize_t idx = list.indexOfKey(handle); + if (idx >= 0) + list.editValueAt(idx).vaddr = addr; + } + + return err; +} + +status_t BufferAllocator::unmap(buffer_handle_t handle) +{ + Mutex::Autolock _l(mLock); + gralloc_module_t* mod = (gralloc_module_t*)mAllocDev->common.module; + status_t err = mod->unmap(mod, handle); + LOGW_IF(err, "unmap(...) failed %d (%s)", err, strerror(-err)); + + if (err == NO_ERROR) { + Mutex::Autolock _l(sLock); + KeyedVector& list(sAllocList); + ssize_t idx = list.indexOfKey(handle); + if (idx >= 0) + list.editValueAt(idx).vaddr = 0; + } + + return err; +} + + +// --------------------------------------------------------------------------- +}; // namespace android -- cgit v1.1