From fd201d3c1df49d4af95d2c48709b5a98b6e6d8e9 Mon Sep 17 00:00:00 2001 From: Nikhil Punathil Date: Mon, 17 Jun 2019 21:14:34 -0700 Subject: shinano-common: bring in libshim_camera from msm8974-common we need updated sources that break rhine compatibility for working cam Change-Id: I274c91efc797b0304a2074baf4b908766d321356 Signed-off-by: Nikhil Punathil --- libshims/ui/GraphicBufferAllocator.cpp | 159 +++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 libshims/ui/GraphicBufferAllocator.cpp (limited to 'libshims/ui/GraphicBufferAllocator.cpp') diff --git a/libshims/ui/GraphicBufferAllocator.cpp b/libshims/ui/GraphicBufferAllocator.cpp new file mode 100644 index 0000000..4f93fb6 --- /dev/null +++ b/libshims/ui/GraphicBufferAllocator.cpp @@ -0,0 +1,159 @@ +/* +** +** 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. +*/ + +#define LOG_TAG "GraphicBufferAllocator" +#define ATRACE_TAG ATRACE_TAG_GRAPHICS + +#include + +#include +#include +#include + +#include + +namespace android { +// --------------------------------------------------------------------------- + +ANDROID_SINGLETON_STATIC_INSTANCE( GraphicBufferAllocator ) + +Mutex GraphicBufferAllocator::sLock; +KeyedVector GraphicBufferAllocator::sAllocList; + +GraphicBufferAllocator::GraphicBufferAllocator() + : mAllocDev(0) +{ + hw_module_t const* module; + int err = hw_get_module(GRALLOC_HARDWARE_MODULE_ID, &module); + ALOGE_IF(err, "FATAL: can't find the %s module", GRALLOC_HARDWARE_MODULE_ID); + if (err == 0) { + gralloc_open(module, &mAllocDev); + } +} + +GraphicBufferAllocator::~GraphicBufferAllocator() +{ + gralloc_close(mAllocDev); +} + +void GraphicBufferAllocator::dump(String8& result) const +{ + Mutex::Autolock _l(sLock); + KeyedVector& list(sAllocList); + size_t total = 0; + const size_t SIZE = 4096; + char buffer[SIZE]; + snprintf(buffer, SIZE, "Allocated buffers:\n"); + result.append(buffer); + const size_t c = list.size(); + for (size_t i=0 ; icommon.version >= 1 && mAllocDev->dump) { + mAllocDev->dump(mAllocDev, buffer, SIZE); + result.append(buffer); + } +} + +void GraphicBufferAllocator::dumpToSystemLog() +{ + String8 s; + GraphicBufferAllocator::getInstance().dump(s); + ALOGD("%s", s.string()); +} + +status_t GraphicBufferAllocator::alloc(uint32_t width, uint32_t height, + 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 (!width || !height) + width = height = 1; + + // we have a h/w allocator and h/w buffer is requested + status_t err; + + // Filter out any usage bits that should not be passed to the gralloc module + usage &= GRALLOC_USAGE_ALLOC_MASK; + + int outStride = 0; + err = mAllocDev->alloc(mAllocDev, static_cast(width), + static_cast(height), format, static_cast(usage), handle, + &outStride); + *stride = static_cast(outStride); + + ALOGW_IF(err, "alloc(%u, %u, %d, %08x, ...) failed %d (%s)", + width, height, format, usage, err, strerror(-err)); + + if (err == NO_ERROR) { + Mutex::Autolock _l(sLock); + KeyedVector& 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.width = width; + rec.height = height; + rec.stride = *stride; + rec.format = format; + rec.usage = usage; + rec.size = static_cast(height * (*stride) * bpp); + list.add(*handle, rec); + } + + return err; +} + +status_t GraphicBufferAllocator::free(buffer_handle_t handle) +{ + ATRACE_CALL(); + status_t err; + + err = mAllocDev->free(mAllocDev, handle); + + ALOGW_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; +} + +// --------------------------------------------------------------------------- +}; // namespace android -- cgit v1.2.3