From de5416ca15c27aa1b204e540a5510f54b67cb3f6 Mon Sep 17 00:00:00 2001 From: koron393 Date: Mon, 16 Sep 2019 01:32:35 +0900 Subject: shinano-common: libshim_camera: Add pre N method to GraphicBuffer * Add Marshmallow method to GraphicBuffer and Mapper. * Apply module loading changes to Allocator. Signed-off-by: koron393 Change-Id: I906fa4f123afd9b9e1b62f6e53c1a2f753a027c3 --- libshims/include/ui/GraphicBuffer.h | 18 +++++++++++++ libshims/include/ui/GraphicBufferAllocator.h | 4 ++- libshims/include/ui/GraphicBufferMapper.h | 3 +++ libshims/ui/GraphicBuffer.cpp | 31 +++++++++++++++++++++ libshims/ui/GraphicBufferAllocator.cpp | 40 +++++++++++++++------------- libshims/ui/GraphicBufferMapper.cpp | 7 +++++ 6 files changed, 83 insertions(+), 20 deletions(-) diff --git a/libshims/include/ui/GraphicBuffer.h b/libshims/include/ui/GraphicBuffer.h index bc820b0..fbf0af0 100644 --- a/libshims/include/ui/GraphicBuffer.h +++ b/libshims/include/ui/GraphicBuffer.h @@ -35,6 +35,7 @@ #include #include +struct ANativeWindowBuffer; namespace android { @@ -140,6 +141,18 @@ public: 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(const native_handle_t* inHandle, HandleWrapMethod method, uint32_t inWidth, + uint32_t inHeight, PixelFormat inFormat, uint32_t inUsage, uint32_t inStride); + + GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, uint32_t inUsage, + uint32_t inStride, native_handle_t* inHandle, bool keepOwnership); + + GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, uint32_t inUsage); + + // create a buffer from an existing ANativeWindowBuffer + GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership); + GraphicBuffer(uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, uint32_t inUsage, std::string requestorName = ""); @@ -255,6 +268,10 @@ private: GraphicBufferMapper& mBufferMapper; ssize_t mInitCheck; + // If we're wrapping another buffer then this reference will make sure it + // doesn't get freed. + sp mWrappedBuffer; + // numbers of fds/ints in native_handle_t to flatten uint32_t mTransportNumFds; uint32_t mTransportNumInts; @@ -285,6 +302,7 @@ private: // and informs SurfaceFlinger that it should drop its strong pointer reference to the buffer. std::vector> mDeathCallbacks; + }; }; // namespace android diff --git a/libshims/include/ui/GraphicBufferAllocator.h b/libshims/include/ui/GraphicBufferAllocator.h index 25d4512..9804bea 100644 --- a/libshims/include/ui/GraphicBufferAllocator.h +++ b/libshims/include/ui/GraphicBufferAllocator.h @@ -32,6 +32,8 @@ #include #include +#include + namespace android { class GrallocAllocator; @@ -73,7 +75,7 @@ private: GraphicBufferAllocator(); ~GraphicBufferAllocator(); - GraphicBufferMapper& mMapper; + alloc_device_t *mAllocDev; std::unique_ptr mAllocator; }; diff --git a/libshims/include/ui/GraphicBufferMapper.h b/libshims/include/ui/GraphicBufferMapper.h index 2461454..45f615a 100644 --- a/libshims/include/ui/GraphicBufferMapper.h +++ b/libshims/include/ui/GraphicBufferMapper.h @@ -63,6 +63,9 @@ public: status_t lock(buffer_handle_t handle, uint32_t usage, const Rect& bounds, void** vaddr, int32_t* outBytesPerPixel = nullptr, int32_t* outBytesPerStride = nullptr); + status_t lock(buffer_handle_t handle, + uint32_t usage, const Rect& bounds, void** vaddr); + status_t lockYCbCr(buffer_handle_t handle, uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr); diff --git a/libshims/ui/GraphicBuffer.cpp b/libshims/ui/GraphicBuffer.cpp index 325d176..d70701d 100644 --- a/libshims/ui/GraphicBuffer.cpp +++ b/libshims/ui/GraphicBuffer.cpp @@ -99,6 +99,37 @@ GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, { } +// deprecated (android-6.0) +GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, + PixelFormat inFormat, uint32_t inUsage) + : GraphicBuffer(inWidth, inHeight, inFormat, 1, static_cast(inUsage), "") +{ +} + +// deprecated (android-6.0) +GraphicBuffer::GraphicBuffer(uint32_t inWidth, uint32_t inHeight, + PixelFormat inFormat, uint32_t inUsage, uint32_t inStride, + native_handle_t* inHandle, bool keepOwnership) + : GraphicBuffer(inHandle, keepOwnership ? TAKE_HANDLE : WRAP_HANDLE, + inWidth, inHeight, inFormat, 1, static_cast(inUsage), inStride) +{ +} + +// deprecated (android-6.0) +GraphicBuffer::GraphicBuffer(ANativeWindowBuffer* buffer, bool keepOwnership) + : BASE(), mOwner(keepOwnership ? ownHandle : ownNone), + mBufferMapper(GraphicBufferMapper::get()), + mInitCheck(NO_ERROR), mWrappedBuffer(buffer), mId(getUniqueId()), + mGenerationNumber(0) +{ + width = buffer->width; + height = buffer->height; + stride = buffer->stride; + format = buffer->format; + usage = buffer->usage; + handle = buffer->handle; +} + GraphicBuffer::GraphicBuffer(const native_handle_t* inHandle, HandleWrapMethod method, uint32_t inWidth, uint32_t inHeight, PixelFormat inFormat, uint32_t inLayerCount, uint64_t inUsage, uint32_t inStride) diff --git a/libshims/ui/GraphicBufferAllocator.cpp b/libshims/ui/GraphicBufferAllocator.cpp index 0861a1f..9543fd7 100644 --- a/libshims/ui/GraphicBufferAllocator.cpp +++ b/libshims/ui/GraphicBufferAllocator.cpp @@ -45,20 +45,21 @@ Mutex GraphicBufferAllocator::sLock; KeyedVector GraphicBufferAllocator::sAllocList; -GraphicBufferAllocator::GraphicBufferAllocator() : mMapper(GraphicBufferMapper::getInstance()) { - mAllocator = std::make_unique( - reinterpret_cast(mMapper.getGrallocMapper())); - if (!mAllocator->isLoaded()) { - mAllocator = std::make_unique( - reinterpret_cast(mMapper.getGrallocMapper())); - } - - if (!mAllocator->isLoaded()) { - LOG_ALWAYS_FATAL("gralloc-allocator is missing"); +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() {} +GraphicBufferAllocator::~GraphicBufferAllocator() +{ + gralloc_close(mAllocDev); +} size_t GraphicBufferAllocator::getTotalSize() const { Mutex::Autolock _l(sLock); @@ -150,17 +151,18 @@ status_t GraphicBufferAllocator::allocate(uint32_t width, uint32_t height, status_t GraphicBufferAllocator::free(buffer_handle_t handle) { - ATRACE_CALL(); + status_t err; - // We allocated a buffer from the allocator and imported it into the - // mapper to get the handle. We just need to free the handle now. - mMapper.freeBuffer(handle); + err = mAllocDev->free(mAllocDev, handle); - Mutex::Autolock _l(sLock); - KeyedVector& list(sAllocList); - list.removeItem(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 NO_ERROR; + return err; } // --------------------------------------------------------------------------- diff --git a/libshims/ui/GraphicBufferMapper.cpp b/libshims/ui/GraphicBufferMapper.cpp index 25b7247..1efad90 100644 --- a/libshims/ui/GraphicBufferMapper.cpp +++ b/libshims/ui/GraphicBufferMapper.cpp @@ -111,6 +111,13 @@ status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, const return lockAsync(handle, usage, bounds, vaddr, -1, outBytesPerPixel, outBytesPerStride); } +// for old devices +status_t GraphicBufferMapper::lock(buffer_handle_t handle, uint32_t usage, + const Rect& bounds, void** vaddr) +{ + return lock(handle, usage, bounds, vaddr, 0, 0); +} + status_t GraphicBufferMapper::lockYCbCr(buffer_handle_t handle, uint32_t usage, const Rect& bounds, android_ycbcr *ycbcr) { -- cgit v1.2.3