libdrmutils: Add support for RMFB2

Add support for RMFB2 if available. Original RMFB bypasses refcounting
in kernel and removes planes, crtcs etc associated with the FB Id.
RMFB2 has proper refcounting and allows buffer removal to be
independent of drawing thread in client

Change-Id: I4f2c9dc509844467f39be067697a46d1beeeaff7
CRs-fixed: 1114808
diff --git a/libdrmutils/Android.mk b/libdrmutils/Android.mk
index 8d9205d..15102e4 100644
--- a/libdrmutils/Android.mk
+++ b/libdrmutils/Android.mk
@@ -3,10 +3,10 @@
 
 LOCAL_MODULE                  := libdrmutils
 LOCAL_MODULE_TAGS             := optional
-LOCAL_C_INCLUDES              := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include \
-                                 external/libdrm
+LOCAL_C_INCLUDES              := external/libdrm \
+                                 $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr/include
 LOCAL_SHARED_LIBRARIES        := libdrm libdl
-LOCAL_CFLAGS                  := -DLOG_TAG=\"DRMUTILS\" -Wall -std=c++11 -Werror
+LOCAL_CFLAGS                  := -DLOG_TAG=\"DRMUTILS\" -Wall -std=c++11 -Werror -fno-operator-names
 LOCAL_CLANG                   := true
 LOCAL_ADDITIONAL_DEPENDENCIES := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ/usr
 LOCAL_SRC_FILES               := drm_master.cpp drm_res_mgr.cpp drm_lib_loader.cpp
diff --git a/libdrmutils/drm_master.cpp b/libdrmutils/drm_master.cpp
index 46ea24e..9b3c48e 100644
--- a/libdrmutils/drm_master.cpp
+++ b/libdrmutils/drm_master.cpp
@@ -27,14 +27,16 @@
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
-#include <drm/drm_fourcc.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <xf86drm.h>
 #include <xf86drmMode.h>
-
+// Intentionally included after xf86 headers so that they in-turn include libdrm version of drm.h
+// that doesn't use keyword "virtual" for a variable name. Not doing so leads to the kernel version
+// of drm.h being included causing compilation to fail
+#include <drm/msm_drm.h>
 #include <algorithm>
 #include <iterator>
 
@@ -119,18 +121,24 @@
 }
 
 int DRMMaster::RemoveFbId(uint32_t gem_handle, uint32_t fb_id) {
-  int ret = drmModeRmFB(dev_fd_, fb_id);
-  if (ret) {
-    DRM_LOGE("drmModeRmFB failed with error %d", ret);
-  }
-
   struct drm_gem_close gem_close = {};
   gem_close.handle = gem_handle;
-  ret = drmIoctl(dev_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close);
+  int ret = drmIoctl(dev_fd_, DRM_IOCTL_GEM_CLOSE, &gem_close);
   if (ret) {
-    DRM_LOGE("drmIoctl::DRM_IOCTL_GEM_CLOSE failed with error %d", ret);
+    DRM_LOGE("drmIoctl::DRM_IOCTL_GEM_CLOSE failed with error %d", errno);
   }
 
+#ifdef DRM_IOCTL_MSM_RMFB2
+  ret = drmIoctl(dev_fd_, DRM_IOCTL_MSM_RMFB2, &fb_id);
+  if (ret) {
+    DRM_LOGE("drmIoctl::DRM_IOCTL_MSM_RMFB2 failed for fb_id %d with error %d", fb_id, errno);
+  }
+#else
+  ret = drmModeRmFB(dev_fd_, fb_id);
+  if (ret) {
+    DRM_LOGE("drmModeRmFB failed for fb_id %d with error %d", fb_id, ret);
+  }
+#endif
   return ret;
 }