From b97f4d17b4cf627f7cb35477a6aab99b52a87acc Mon Sep 17 00:00:00 2001 From: Kalyan Kondapally Date: Mon, 12 Jun 2017 11:29:43 -0700 Subject: [PATCH] Ensure we close all the gem handles. We create gem handles in our side which need to be closed when releasing buffer. This was not an issue on Linux as minigbm takes care of it. Jira: None. Test: shmem doesn't grow when switching between apps on Android. Signed-off-by: Kalyan Kondapally --- os/android/gralloc1bufferhandler.cpp | 2 +- os/android/grallocbufferhandler.cpp | 2 +- os/android/platformdefines.h | 1 + os/android/utils_android.h | 17 +++++++++++++++-- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/os/android/gralloc1bufferhandler.cpp b/os/android/gralloc1bufferhandler.cpp index f5ec05a..7d7576a 100644 --- a/os/android/gralloc1bufferhandler.cpp +++ b/os/android/gralloc1bufferhandler.cpp @@ -83,7 +83,7 @@ bool Gralloc1BufferHandler::CreateBuffer(uint32_t w, uint32_t h, int format, } bool Gralloc1BufferHandler::ReleaseBuffer(HWCNativeHandle handle) { - if (!ReleaseGraphicsBuffer(handle)) + if (!ReleaseGraphicsBuffer(handle, fd_)) return false; gralloc1_device_t *gralloc1_dvc = diff --git a/os/android/grallocbufferhandler.cpp b/os/android/grallocbufferhandler.cpp index 9b5c048..1f9e051 100644 --- a/os/android/grallocbufferhandler.cpp +++ b/os/android/grallocbufferhandler.cpp @@ -76,7 +76,7 @@ bool GrallocBufferHandler::CreateBuffer(uint32_t w, uint32_t h, int format, } bool GrallocBufferHandler::ReleaseBuffer(HWCNativeHandle handle) { - return ReleaseGraphicsBuffer(handle); + return ReleaseGraphicsBuffer(handle, fd_); } void GrallocBufferHandler::DestroyHandle(HWCNativeHandle handle) { diff --git a/os/android/platformdefines.h b/os/android/platformdefines.h index 864296a..c13bb16 100644 --- a/os/android/platformdefines.h +++ b/os/android/platformdefines.h @@ -43,6 +43,7 @@ struct gralloc_handle { buffer_handle_t handle_ = NULL; android::sp buffer_ = NULL; native_handle_t* imported_handle_ = NULL; + uint32_t gem_handle_ = 0; bool hwc_buffer_ = false; }; diff --git a/os/android/utils_android.h b/os/android/utils_android.h index 917faca..c5c8135 100644 --- a/os/android/utils_android.h +++ b/os/android/utils_android.h @@ -121,7 +121,7 @@ static bool CreateGraphicsBuffer(uint32_t w, uint32_t h, int /*format*/, return true; } -static bool ReleaseGraphicsBuffer(HWCNativeHandle handle) { +static bool ReleaseGraphicsBuffer(HWCNativeHandle handle, int fd) { if (!handle) return false; @@ -129,6 +129,17 @@ static bool ReleaseGraphicsBuffer(HWCNativeHandle handle) { handle->buffer_.clear(); } + if (handle->gem_handle_ > 0) { + struct drm_gem_close gem_close; + memset(&gem_close, 0, sizeof(gem_close)); + gem_close.handle = handle->gem_handle_; + int ret = drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &gem_close); + if (ret) + ETRACE("Failed to close gem handle %d", ret); + } + + handle->gem_handle_ = 0; + return true; } @@ -141,7 +152,7 @@ static bool ImportGraphicsBuffer(HWCNativeHandle handle, HwcBuffer *bo, bo->height = gr_handle->height; bo->prime_fd = gr_handle->fds[0]; - uint32_t id; + uint32_t id = 0; if (drmPrimeFDToHandle(fd, bo->prime_fd, &id)) { ETRACE("drmPrimeFDToHandle failed."); return false; @@ -163,6 +174,8 @@ static bool ImportGraphicsBuffer(HWCNativeHandle handle, HwcBuffer *bo, bo->usage |= hwcomposer::kLayerNormal; } + handle->gem_handle_ = id; + return true; } #endif -- 2.11.0