gralloc: drop prebuilt libvmmem dependency and use dlopen instead

Change-Id: I05b106fe10a77dd73126d9809c7c0c94759cd49c
diff --git a/gralloc/Android.bp b/gralloc/Android.bp
index fe68772..26e35f9 100644
--- a/gralloc/Android.bp
+++ b/gralloc/Android.bp
@@ -63,7 +63,6 @@
         "libhidlbase",
         "libion",
         "libdmabufheap",
-        "libvmmem",
         "android.hardware.graphics.mapper@2.1",
         "android.hardware.graphics.mapper@3.0",
         "android.hardware.graphics.mapper@4.0",
diff --git a/gralloc/gr_dma_mgr.cpp b/gralloc/gr_dma_mgr.cpp
index 41ce6ef..7fe336d 100644
--- a/gralloc/gr_dma_mgr.cpp
+++ b/gralloc/gr_dma_mgr.cpp
@@ -56,6 +56,23 @@
 
 DmaManager *DmaManager::dma_manager_ = NULL;
 
+DmaManager::DmaManager() {
+  libvmmemPointer = dlopen("libvmmem.so", RTLD_LAZY);
+
+  if (libvmmemPointer) {
+    createVmMem = reinterpret_cast<std::unique_ptr<VmMem> (*)()>(dlsym(libvmmemPointer,
+                                                                 "CreateVmMem"));
+    const char* dlsym_error = dlerror();
+    if (dlsym_error) {
+      ALOGE("Cannot load symbol CreateVmMem: %s", dlsym_error);
+      return;
+    }
+  } else {
+    ALOGE("Could not load libvmmem: %s", dlerror());
+    return;
+  }
+}
+
 DmaManager *DmaManager::GetInstance() {
   if (!dma_manager_) {
     dma_manager_ = new DmaManager();
@@ -216,7 +233,7 @@
 
 int DmaManager::SecureMemPerms(AllocData *data) {
   int ret = 0;
-  std::unique_ptr<VmMem> vmmem = VmMem::CreateVmMem();
+  std::unique_ptr<VmMem> vmmem = createVmMem();
   if (!vmmem) {
     return -ENOMEM;
   }
diff --git a/gralloc/gr_dma_mgr.h b/gralloc/gr_dma_mgr.h
index e2b30d2..14636ed 100644
--- a/gralloc/gr_dma_mgr.h
+++ b/gralloc/gr_dma_mgr.h
@@ -71,7 +71,7 @@
   static DmaManager *GetInstance();
 
  private:
-  DmaManager() {}
+  DmaManager();
   int UnmapBuffer(void *base, unsigned int size, unsigned int offset);
   void GetVMPermission(BufferPermission perm, std::bitset<kVmPermissionMax> *vm_perm);
   void InitMemUtils();
@@ -86,6 +86,9 @@
   void *mem_utils_lib_ = {};
   CreateMemBufInterface CreateMemBuf_ = nullptr;
   DestroyMemBufInterface DestroyMemBuf_ = nullptr;
+
+  void* libvmmemPointer;
+  std::unique_ptr<VmMem> (*createVmMem)();
 };
 
 }  // namespace gralloc