graphics: rewrite libgralloc1-adapter
Rewrite libgralloc1-adapter to be based on Gralloc1On0Adapter.
Previously, the adapter targeted maximum portability and maximum
performance. The rewritten adapter targets ease of use instead.
This also fixes a bug in the adapter's GRALLOC1_FUNCTION_RELEASE. The
function does not imply native_handle_close/native_handle_delete. As a
result, IMapper and IComposer are also fixed to close/delete handles.
Test: builds and boots
Change-Id: I5c071453dc950583087ae07967bca2c22866c538
diff --git a/graphics/mapper/2.0/default/Android.bp b/graphics/mapper/2.0/default/Android.bp
index 02ed877..c3d2281 100644
--- a/graphics/mapper/2.0/default/Android.bp
+++ b/graphics/mapper/2.0/default/Android.bp
@@ -19,6 +19,7 @@
srcs: ["GrallocMapper.cpp"],
cppflags: ["-Wall", "-Wextra"],
shared_libs: [
+ "android.hardware.graphics.allocator@2.0",
"android.hardware.graphics.mapper@2.0",
"libbase",
"libcutils",
diff --git a/graphics/mapper/2.0/default/GrallocMapper.cpp b/graphics/mapper/2.0/default/GrallocMapper.cpp
index cd9db38..3b6460a 100644
--- a/graphics/mapper/2.0/default/GrallocMapper.cpp
+++ b/graphics/mapper/2.0/default/GrallocMapper.cpp
@@ -17,7 +17,9 @@
#include "GrallocMapper.h"
+#include <mutex>
#include <vector>
+#include <unordered_map>
#include <string.h>
@@ -100,6 +102,9 @@
GRALLOC1_PFN_LOCK_FLEX lockFlex;
GRALLOC1_PFN_UNLOCK unlock;
} mDispatch;
+
+ std::mutex mMutex;
+ std::unordered_map<buffer_handle_t, size_t> mBufferReferenceCounts;
};
GrallocMapperHal::GrallocMapperHal(const hw_module_t* module)
@@ -201,12 +206,34 @@
Return<Error> GrallocMapperHal::retain(const hidl_handle& bufferHandle)
{
int32_t err = mDispatch.retain(mDevice, bufferHandle);
+ if (err == GRALLOC1_ERROR_NONE) {
+ auto nativeHandle = bufferHandle.getNativeHandle();
+ std::lock_guard<std::mutex> lock(mMutex);
+
+ ++mBufferReferenceCounts[nativeHandle];
+ }
return static_cast<Error>(err);
}
Return<Error> GrallocMapperHal::release(const hidl_handle& bufferHandle)
{
int32_t err = mDispatch.release(mDevice, bufferHandle);
+ if (err == GRALLOC1_ERROR_NONE) {
+ auto nativeHandle = bufferHandle.getNativeHandle();
+ std::lock_guard<std::mutex> lock(mMutex);
+
+ auto iter = mBufferReferenceCounts.find(bufferHandle);
+ if (iter == mBufferReferenceCounts.end()) {
+ // this should never happen
+ err = GRALLOC1_ERROR_BAD_HANDLE;
+ } else if (--iter->second == 0) {
+ native_handle_close(nativeHandle);
+ native_handle_delete(const_cast<native_handle_t*>(nativeHandle));
+
+ mBufferReferenceCounts.erase(iter);
+ }
+ }
+
return static_cast<Error>(err);
}