From 84cd15d69345eea274c4d03f6c77b5fd50ac626a Mon Sep 17 00:00:00 2001 From: Sean Callanan Date: Fri, 23 Jul 2021 14:08:08 -0700 Subject: Replace refcounting functions with buffer free libGralloc4Wrapper currently exposes reference retain and free functions which call into Gralloc's internal reference-counting mechanisms. These actually aren't necessary: the only thing that clients need is a function that undoes the effect of importBuffer() on a locally-created buffer. Removed ::retain and ::release, and instead added freeImportedHandle(), which calls freeBuffer(), unregistering the buffer, and then also manually unmaps the buffer's handles (which is necessary because freeBuffer() doesn't normally do that). Bug: 193573573 Test: many photos with rear and front camera Change-Id: Iba339d35279512d75da01388c294e6c2191ede6a --- gralloc4/src/Android.bp | 1 + .../include/gralloc4/gralloc_vendor_interface.h | 3 +- gralloc4/src/libGralloc4Wrapper/wrapper.cpp | 54 ++++++++++++++++------ 3 files changed, 43 insertions(+), 15 deletions(-) (limited to 'gralloc4/src') diff --git a/gralloc4/src/Android.bp b/gralloc4/src/Android.bp index 2a4a5e3..a3a254d 100644 --- a/gralloc4/src/Android.bp +++ b/gralloc4/src/Android.bp @@ -67,6 +67,7 @@ cc_library_shared { "libnativewindow", "libion_google", "android.hardware.graphics.common@1.2", + "android.hardware.graphics.mapper@4.0", "libdmabufheap", "libgralloctypes", "libdrm", diff --git a/gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h b/gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h index 45054c8..cbd98d9 100644 --- a/gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h +++ b/gralloc4/src/libGralloc4Wrapper/include/gralloc4/gralloc_vendor_interface.h @@ -43,8 +43,7 @@ void setFormat(Descriptor &descriptor, int format); buffer_handle_t createNativeHandle(const Descriptor &descriptor); -android::hardware::graphics::mapper::V4_0::Error retain(buffer_handle_t handle); -android::hardware::graphics::mapper::V4_0::Error release(buffer_handle_t handle); +int freeImportedHandle(void *handle); } // namespace android::hardware::graphics::allocator::priv diff --git a/gralloc4/src/libGralloc4Wrapper/wrapper.cpp b/gralloc4/src/libGralloc4Wrapper/wrapper.cpp index 07374ad..c9d3381 100644 --- a/gralloc4/src/libGralloc4Wrapper/wrapper.cpp +++ b/gralloc4/src/libGralloc4Wrapper/wrapper.cpp @@ -188,22 +188,50 @@ buffer_handle_t createNativeHandle(const Descriptor &descriptor) { return tmp_buffer; } -android::hardware::graphics::mapper::V4_0::Error retain(buffer_handle_t handle) { - int err = mali_gralloc_reference_retain(handle); - if (err == -EINVAL) { - return android::hardware::graphics::mapper::V4_0::Error::BAD_BUFFER; - } else if (err) { - return android::hardware::graphics::mapper::V4_0::Error::NO_RESOURCES; +int freeImportedHandle(void *handle) +{ + using android::hardware::graphics::mapper::V4_0::IMapper; + using android::hardware::graphics::mapper::V4_0::Error; + + const private_handle_t *hnd = static_cast(handle); + + struct UnmapWork { void *base; size_t alloc_size; }; + std::vector work(hnd->fd_count); + + for (size_t i = 0; i < hnd->fd_count; ++i) + { + work[i].base = reinterpret_cast(hnd->bases[i]); + work[i].alloc_size = hnd->alloc_sizes[i]; + } + + static android::sp mapper = IMapper::getService(); + if (!mapper) + { + ALOGE("libGralloc4Wrapper: %s failed to get a mapper", __func__); + return -1; } - return android::hardware::graphics::mapper::V4_0::Error::NONE; -} -android::hardware::graphics::mapper::V4_0::Error release(buffer_handle_t handle) { - int err = mali_gralloc_reference_release(handle, true); - if (err) { - return android::hardware::graphics::mapper::V4_0::Error::BAD_BUFFER; + if (mapper->freeBuffer(handle) != Error::NONE) + { + ALOGE("libGralloc4Wrapper: %s couldn't freeBuffer(%p\n", __func__, handle); + return -1; } - return android::hardware::graphics::mapper::V4_0::Error::NONE; + + { + bool unmapFailed = false; + for (const UnmapWork &w : work) + { + if (!w.base) { continue; } + if (int ret = munmap(w.base, w.alloc_size); ret) + { + ALOGE("libGralloc4Wrapper: %s couldn't unmap address %p (size %zu): %s", __func__, w.base, w.alloc_size, strerror(ret)); + unmapFailed = true; + } + } + if (unmapFailed) { return -1; } + } + + return 0; } } // namespace android::hardware::graphics::allocator::priv -- cgit v1.2.3