From eb4c130ccf05799fe7c20a4737f21dd63c0dbed5 Mon Sep 17 00:00:00 2001 From: koron393 Date: Tue, 7 May 2019 02:49:25 +0900 Subject: shinano-common: libshim_camera: Update GraphicBuffer sources to Pie * Copy from Pie framework sources. Change-Id: I903eb0c3c80a752d4a8da44dec47e348c7dd23fc Signed-off-by: Nikhil Punathil --- libshims/ui/GraphicBuffer.cpp | 356 +++++++++++++++++++++++++++++++----------- 1 file changed, 267 insertions(+), 89 deletions(-) (limited to 'libshims/ui/GraphicBuffer.cpp') diff --git a/libshims/ui/GraphicBuffer.cpp b/libshims/ui/GraphicBuffer.cpp index dad41b4..7670ac6 100644 --- a/libshims/ui/GraphicBuffer.cpp +++ b/libshims/ui/GraphicBuffer.cpp @@ -16,17 +16,16 @@ #define LOG_TAG "GraphicBuffer" -#include -#include -#include +#include -#include -#include +#include -#include +#include + +#include +#include #include #include -#include namespace android { @@ -34,58 +33,65 @@ namespace android { // Buffer and implementation of ANativeWindowBuffer // =========================================================================== +static uint64_t getUniqueId() { + static volatile int32_t nextId = 0; + uint64_t id = static_cast(getpid()) << 32; + id |= static_cast(android_atomic_inc(&nextId)); + return id; +} + +sp GraphicBuffer::from(ANativeWindowBuffer* anwb) { + return static_cast(anwb); +} + GraphicBuffer::GraphicBuffer() : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()), - mInitCheck(NO_ERROR) + mInitCheck(NO_ERROR), mId(getUniqueId()), mGenerationNumber(0) { width = height = stride = format = + usage_deprecated = 0; usage = 0; + layerCount = 0; handle = NULL; } +// deprecated GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, - PixelFormat inFormat, uint32_t inUsage) - : BASE(), mOwner(ownData), mBufferMapper(GraphicBufferMapper::get()), - mInitCheck(NO_ERROR) + PixelFormat inFormat, uint32_t inUsage, std::string requestorName) + : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast(inUsage), requestorName) { - width = - height = - stride = - format = - usage = 0; - handle = NULL; - mInitCheck = initSize(inWidth, inHeight, inFormat, inUsage); } GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, - PixelFormat inFormat, uint32_t inUsage, uint32_t inStride, - native_handle_t* inHandle, bool keepOwnership) - : BASE(), mOwner(keepOwnership ? ownHandle : ownNone), - mBufferMapper(GraphicBufferMapper::get()), - mInitCheck(NO_ERROR) + PixelFormat inFormat, uint32_t inLayerCount, uint64_t usage, std::string requestorName) + : GraphicBuffer() { - width = static_cast(inWidth); - height = static_cast(inHeight); - stride = static_cast(inStride); - format = inFormat; - usage = static_cast(inUsage); - handle = inHandle; -} - -GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership) - : BASE(), mOwner(keepOwnership ? ownHandle : ownNone), - mBufferMapper(GraphicBufferMapper::get()), - mInitCheck(NO_ERROR), mWrappedBuffer(buffer) + mInitCheck = initWithSize(inWidth, inHeight, inFormat, inLayerCount, + usage, std::move(requestorName)); +} + +// deprecated +GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, + PixelFormat inFormat, uint32_t inLayerCount, uint32_t inUsage, + uint32_t inStride, native_handle_t* inHandle, bool keepOwnership) + : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE, + inWidth, inHeight, inFormat, inLayerCount, static_cast(inUsage), + inStride) +{ +} + +GraphicBuffer::GraphicBuffer(const native_handle_t* handle, + HandleWrapMethod method, uint32_t width, uint32_t height, + PixelFormat format, uint32_t layerCount, + uint64_t usage, + uint32_t stride) + : GraphicBuffer() { - width = buffer->width; - height = buffer->height; - stride = buffer->stride; - format = buffer->format; - usage = buffer->usage; - handle = buffer->handle; + mInitCheck = initWithHandle(handle, method, width, height, format, + layerCount, usage, stride); } GraphicBuffer::~GraphicBuffer() @@ -98,15 +104,12 @@ GraphicBuffer::~GraphicBuffer() void GraphicBuffer::free_handle() { if (mOwner == ownHandle) { - mBufferMapper.unregisterBuffer(handle); - native_handle_close(handle); - native_handle_delete(const_cast(handle)); + mBufferMapper.freeBuffer(handle); } else if (mOwner == ownData) { GraphicBufferAllocator& allocator(GraphicBufferAllocator::get()); allocator.free(handle); } - - mWrappedBuffer = 0; + handle = NULL; } status_t GraphicBuffer::initCheck() const { @@ -126,7 +129,7 @@ ANativeWindowBuffer* GraphicBuffer::getNativeBuffer() const } status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight, - PixelFormat inFormat, uint32_t inUsage) + PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage) { if (mOwner != ownData) return INVALID_OPERATION; @@ -135,7 +138,8 @@ status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight, static_cast(inWidth) == width && static_cast(inHeight) == height && inFormat == format && - static_cast(inUsage) == usage) + inLayerCount == layerCount && + inUsage == usage) return NO_ERROR; if (handle) { @@ -143,26 +147,83 @@ status_t GraphicBuffer::reallocate(uint32_t inWidth, uint32_t inHeight, allocator.free(handle); handle = 0; } - return initSize(inWidth, inHeight, inFormat, inUsage); + return initWithSize(inWidth, inHeight, inFormat, inLayerCount, inUsage, "[Reallocation]"); +} + +bool GraphicBuffer::needsReallocation(uint32_t inWidth, uint32_t inHeight, + PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage) +{ + if (static_cast(inWidth) != width) return true; + if (static_cast(inHeight) != height) return true; + if (inFormat != format) return true; + if (inLayerCount != layerCount) return true; + if ((usage & inUsage) != inUsage) return true; + return false; } -status_t GraphicBuffer::initSize(uint32_t inWidth, uint32_t inHeight, - PixelFormat inFormat, uint32_t inUsage) +status_t GraphicBuffer::initWithSize(uint32_t inWidth, uint32_t inHeight, + PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage, + std::string requestorName) { GraphicBufferAllocator& allocator = GraphicBufferAllocator::get(); - int outStride = 0; - status_t err = allocator.alloc(inWidth, inHeight, inFormat, inUsage, - &handle, &outStride); + uint32_t outStride = 0; + status_t err = allocator.allocate(inWidth, inHeight, inFormat, inLayerCount, + inUsage, &handle, &outStride, mId, + std::move(requestorName)); if (err == NO_ERROR) { + mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts); + width = static_cast(inWidth); height = static_cast(inHeight); format = inFormat; - usage = static_cast(inUsage); + layerCount = inLayerCount; + usage = inUsage; + usage_deprecated = int(usage); stride = static_cast(outStride); } return err; } +status_t GraphicBuffer::initWithHandle(const native_handle_t* handle, + HandleWrapMethod method, uint32_t width, uint32_t height, + PixelFormat format, uint32_t layerCount, uint64_t usage, + uint32_t stride) +{ + ANativeWindowBuffer::width = static_cast(width); + ANativeWindowBuffer::height = static_cast(height); + ANativeWindowBuffer::stride = static_cast(stride); + ANativeWindowBuffer::format = format; + ANativeWindowBuffer::usage = usage; + ANativeWindowBuffer::usage_deprecated = int(usage); + + ANativeWindowBuffer::layerCount = layerCount; + + mOwner = (method == WRAP_HANDLE) ? ownNone : ownHandle; + + if (method == TAKE_UNREGISTERED_HANDLE || method == CLONE_HANDLE) { + buffer_handle_t importedHandle; + status_t err = mBufferMapper.importBuffer(handle, width, height, + layerCount, format, usage, stride, &importedHandle); + if (err != NO_ERROR) { + initWithHandle(nullptr, WRAP_HANDLE, 0, 0, 0, 0, 0, 0); + + return err; + } + + if (method == TAKE_UNREGISTERED_HANDLE) { + native_handle_close(handle); + native_handle_delete(const_cast(handle)); + } + + handle = importedHandle; + mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts); + } + + ANativeWindowBuffer::handle = handle; + + return NO_ERROR; +} + status_t GraphicBuffer::lock(uint32_t inUsage, void** vaddr) { const Rect lockBounds(width, height); @@ -210,12 +271,68 @@ status_t GraphicBuffer::unlock() return res; } +status_t GraphicBuffer::lockAsync(uint32_t inUsage, void** vaddr, int fenceFd) +{ + const Rect lockBounds(width, height); + status_t res = lockAsync(inUsage, lockBounds, vaddr, fenceFd); + return res; +} + +status_t GraphicBuffer::lockAsync(uint32_t inUsage, const Rect& rect, + void** vaddr, int fenceFd) +{ + return lockAsync(inUsage, inUsage, rect, vaddr, fenceFd); +} + +status_t GraphicBuffer::lockAsync(uint64_t inProducerUsage, + uint64_t inConsumerUsage, const Rect& rect, void** vaddr, int fenceFd) +{ + if (rect.left < 0 || rect.right > width || + rect.top < 0 || rect.bottom > height) { + ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", + rect.left, rect.top, rect.right, rect.bottom, + width, height); + return BAD_VALUE; + } + status_t res = getBufferMapper().lockAsync(handle, inProducerUsage, + inConsumerUsage, rect, vaddr, fenceFd); + return res; +} + +status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, android_ycbcr* ycbcr, + int fenceFd) +{ + const Rect lockBounds(width, height); + status_t res = lockAsyncYCbCr(inUsage, lockBounds, ycbcr, fenceFd); + return res; +} + +status_t GraphicBuffer::lockAsyncYCbCr(uint32_t inUsage, const Rect& rect, + android_ycbcr* ycbcr, int fenceFd) +{ + if (rect.left < 0 || rect.right > width || + rect.top < 0 || rect.bottom > height) { + ALOGE("locking pixels (%d,%d,%d,%d) outside of buffer (w=%d, h=%d)", + rect.left, rect.top, rect.right, rect.bottom, + width, height); + return BAD_VALUE; + } + status_t res = getBufferMapper().lockAsyncYCbCr(handle, inUsage, rect, ycbcr, fenceFd); + return res; +} + +status_t GraphicBuffer::unlockAsync(int *fenceFd) +{ + status_t res = getBufferMapper().unlockAsync(handle, fenceFd); + return res; +} + size_t GraphicBuffer::getFlattenedSize() const { - return static_cast(8 + (handle ? handle->numInts : 0)) * sizeof(int); + return static_cast(13 + (handle ? mTransportNumInts : 0)) * sizeof(int); } size_t GraphicBuffer::getFdCount() const { - return static_cast(handle ? handle->numFds : 0); + return static_cast(handle ? mTransportNumFds : 0); } status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const { @@ -225,30 +342,34 @@ status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& size_t fdCountNeeded = GraphicBuffer::getFdCount(); if (count < fdCountNeeded) return NO_MEMORY; - int* buf = static_cast(buffer); - buf[0] = 'GBFR'; + int32_t* buf = static_cast(buffer); + buf[0] = 'GB01'; buf[1] = width; buf[2] = height; buf[3] = stride; buf[4] = format; - buf[5] = usage; - buf[6] = 0; - buf[7] = 0; + buf[5] = static_cast(layerCount); + buf[6] = int(usage); // low 32-bits + buf[7] = static_cast(mId >> 32); + buf[8] = static_cast(mId & 0xFFFFFFFFull); + buf[9] = static_cast(mGenerationNumber); + buf[10] = 0; + buf[11] = 0; + buf[12] = int(usage >> 32); // high 32-bits if (handle) { - buf[6] = handle->numFds; - buf[7] = handle->numInts; - memcpy(fds, handle->data, - static_cast(handle->numFds) * sizeof(int)); - memcpy(&buf[8], handle->data + handle->numFds, - static_cast(handle->numInts) * sizeof(int)); + buf[10] = int32_t(mTransportNumFds); + buf[11] = int32_t(mTransportNumInts); + memcpy(fds, handle->data, static_cast(mTransportNumFds) * sizeof(int)); + memcpy(buf + 13, handle->data + handle->numFds, + static_cast(mTransportNumInts) * sizeof(int)); } - buffer = static_cast(static_cast(buffer) + sizeNeeded); + buffer = static_cast(static_cast(buffer) + sizeNeeded); size -= sizeNeeded; if (handle) { - fds += handle->numFds; - count -= static_cast(handle->numFds); + fds += mTransportNumFds; + count -= static_cast(mTransportNumFds); } return NO_ERROR; @@ -256,28 +377,44 @@ status_t GraphicBuffer::flatten(void*& buffer, size_t& size, int*& fds, size_t& status_t GraphicBuffer::unflatten( void const*& buffer, size_t& size, int const*& fds, size_t& count) { - if (size < 8 * sizeof(int)) return NO_MEMORY; + if (size < 12 * sizeof(int)) { + android_errorWriteLog(0x534e4554, "114223584"); + return NO_MEMORY; + } int const* buf = static_cast(buffer); - if (buf[0] != 'GBFR') return BAD_TYPE; - const size_t numFds = static_cast(buf[6]); - const size_t numInts = static_cast(buf[7]); + // NOTE: it turns out that some media code generates a flattened GraphicBuffer manually!!!!! + // see H2BGraphicBufferProducer.cpp + uint32_t flattenWordCount = 0; + if (buf[0] == 'GB01') { + // new version with 64-bits usage bits + flattenWordCount = 13; + } else if (buf[0] == 'GBFR') { + // old version, when usage bits were 32-bits + flattenWordCount = 12; + } else { + return BAD_TYPE; + } + + const size_t numFds = static_cast(buf[10]); + const size_t numInts = static_cast(buf[11]); // Limit the maxNumber to be relatively small. The number of fds or ints // should not come close to this number, and the number itself was simply // chosen to be high enough to not cause issues and low enough to prevent // overflow problems. const size_t maxNumber = 4096; - if (numFds >= maxNumber || numInts >= (maxNumber - 10)) { - width = height = stride = format = usage = 0; + if (numFds >= maxNumber || numInts >= (maxNumber - flattenWordCount)) { + width = height = stride = format = usage_deprecated = 0; + layerCount = 0; + usage = 0; handle = NULL; - ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", - numFds, numInts); + ALOGE("unflatten: numFds or numInts is too large: %zd, %zd", numFds, numInts); return BAD_VALUE; } - const size_t sizeNeeded = (8 + numInts) * sizeof(int); + const size_t sizeNeeded = (flattenWordCount + numInts) * sizeof(int); if (size < sizeNeeded) return NO_MEMORY; size_t fdCountNeeded = numFds; @@ -293,37 +430,60 @@ status_t GraphicBuffer::unflatten( height = buf[2]; stride = buf[3]; format = buf[4]; - usage = buf[5]; + layerCount = static_cast(buf[5]); + usage_deprecated = buf[6]; + if (flattenWordCount == 13) { + usage = (uint64_t(buf[12]) << 32) | uint32_t(buf[6]); + } else { + usage = uint64_t(usage_deprecated); + } native_handle* h = native_handle_create( static_cast(numFds), static_cast(numInts)); if (!h) { - width = height = stride = format = usage = 0; + width = height = stride = format = usage_deprecated = 0; + layerCount = 0; + usage = 0; handle = NULL; ALOGE("unflatten: native_handle_create failed"); return NO_MEMORY; } memcpy(h->data, fds, numFds * sizeof(int)); - memcpy(h->data + numFds, &buf[8], numInts * sizeof(int)); + memcpy(h->data + numFds, buf + flattenWordCount, numInts * sizeof(int)); handle = h; } else { - width = height = stride = format = usage = 0; + width = height = stride = format = usage_deprecated = 0; + layerCount = 0; + usage = 0; handle = NULL; } + mId = static_cast(buf[7]) << 32; + mId |= static_cast(buf[8]); + + mGenerationNumber = static_cast(buf[9]); + mOwner = ownHandle; if (handle != 0) { - status_t err = mBufferMapper.registerBuffer(handle); + buffer_handle_t importedHandle; + status_t err = mBufferMapper.importBuffer(handle, uint32_t(width), uint32_t(height), + uint32_t(layerCount), format, usage, uint32_t(stride), &importedHandle); if (err != NO_ERROR) { - width = height = stride = format = usage = 0; + width = height = stride = format = usage_deprecated = 0; + layerCount = 0; + usage = 0; handle = NULL; - ALOGE("unflatten: registerBuffer failed: %s (%d)", - strerror(-err), err); + ALOGE("unflatten: registerBuffer failed: %s (%d)", strerror(-err), err); return err; } + + native_handle_close(handle); + native_handle_delete(const_cast(handle)); + handle = importedHandle; + mBufferMapper.getTransportSize(handle, &mTransportNumFds, &mTransportNumInts); } - buffer = static_cast(static_cast(buffer) + sizeNeeded); + buffer = static_cast(static_cast(buffer) + sizeNeeded); size -= sizeNeeded; fds += numFds; count -= numFds; @@ -331,6 +491,24 @@ status_t GraphicBuffer::unflatten( return NO_ERROR; } +bool GraphicBuffer::isDetachedBuffer() const { + return mDetachedBufferHandle && mDetachedBufferHandle->isValid(); +} + +status_t GraphicBuffer::setDetachedBufferHandle(std::unique_ptr channel) { + if (isDetachedBuffer()) { + ALOGW("setDetachedBuffer: there is already a BufferHub channel associated with this " + "GraphicBuffer. Replacing the old one."); + } + + mDetachedBufferHandle = std::move(channel); + return NO_ERROR; +} + +std::unique_ptr GraphicBuffer::takeDetachedBufferHandle() { + return std::move(mDetachedBufferHandle); +} + // --------------------------------------------------------------------------- }; // namespace android -- cgit v1.2.3