am 879cc4e7: drm_gralloc: Use render node

* commit '879cc4e7c6da5ecf631d65ddd98d12bccd5817f5':
  drm_gralloc: Use render node
diff --git a/Android.mk b/Android.mk
index f4d4483..ee53679 100644
--- a/Android.mk
+++ b/Android.mk
@@ -93,6 +93,7 @@
 	liblog \
 	libcutils \
 	libhardware_legacy \
+	libutils
 
 ifneq ($(filter $(intel_drivers), $(DRM_GPU_DRIVERS)),)
 LOCAL_SRC_FILES += gralloc_drm_intel.c
@@ -154,11 +155,12 @@
 
 LOCAL_C_INCLUDES := \
 	external/libdrm \
-	external/libdrm/include/drm \
+	external/libdrm/include/drm
 
 LOCAL_SHARED_LIBRARIES := \
 	libgralloc_drm \
 	liblog \
+	libutils
 
 # for glFlush/glFinish
 LOCAL_SHARED_LIBRARIES += \
diff --git a/gralloc.cpp b/gralloc.cpp
index 3c377c6..521d6cb 100644
--- a/gralloc.cpp
+++ b/gralloc.cpp
@@ -69,30 +69,6 @@
 			err = 0;
 		}
 		break;
-	/* should we remove this and next ops, and make it transparent? */
-	case GRALLOC_MODULE_PERFORM_GET_DRM_MAGIC:
-		{
-			int32_t *magic = va_arg(args, int32_t *);
-			err = gralloc_drm_get_magic(dmod->drm, magic);
-		}
-		break;
-	case GRALLOC_MODULE_PERFORM_AUTH_DRM_MAGIC:
-		{
-			int32_t magic = va_arg(args, int32_t);
-			err = gralloc_drm_auth_magic(dmod->drm, magic);
-		}
-		break;
-	case GRALLOC_MODULE_PERFORM_ENTER_VT:
-		{
-			err = gralloc_drm_set_master(dmod->drm);
-		}
-		break;
-	case GRALLOC_MODULE_PERFORM_LEAVE_VT:
-		{
-			gralloc_drm_drop_master(dmod->drm);
-			err = 0;
-		}
-		break;
 	default:
 		err = -EINVAL;
 		break;
@@ -150,8 +126,10 @@
 
 static int drm_mod_close_gpu0(struct hw_device_t *dev)
 {
+	struct drm_module_t *dmod = (struct drm_module_t *)dev->module;
 	struct alloc_device_t *alloc = (struct alloc_device_t *) dev;
 
+	gralloc_drm_destroy(dmod->drm);
 	delete alloc;
 
 	return 0;
diff --git a/gralloc_drm.cpp b/gralloc_drm.cpp
index 8826f73..ebff717 100644
--- a/gralloc_drm.cpp
+++ b/gralloc_drm.cpp
@@ -25,6 +25,7 @@
 
 #include <cutils/log.h>
 #include <cutils/atomic.h>
+#include <cutils/properties.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <sys/types.h>
@@ -36,8 +37,6 @@
 
 #define unlikely(x) __builtin_expect(!!(x), 0)
 
-#define GRALLOC_DRM_DEVICE "/dev/dri/card0"
-
 static int32_t gralloc_drm_pid = 0;
 
 /*
@@ -101,6 +100,7 @@
  */
 struct gralloc_drm_t *gralloc_drm_create(void)
 {
+	char path[PROPERTY_VALUE_MAX];
 	struct gralloc_drm_t *drm;
 	int err;
 
@@ -108,9 +108,10 @@
 	if (!drm)
 		return NULL;
 
-	drm->fd = open(GRALLOC_DRM_DEVICE, O_RDWR);
+	property_get("gralloc.drm.device", path, "/dev/dri/renderD128");
+	drm->fd = open(path, O_RDWR);
 	if (drm->fd < 0) {
-		ALOGE("failed to open %s", GRALLOC_DRM_DEVICE);
+		ALOGE("failed to open %s", path);
 		return NULL;
 	}
 
@@ -144,42 +145,6 @@
 }
 
 /*
- * Get the magic for authentication.
- */
-int gralloc_drm_get_magic(struct gralloc_drm_t *drm, int32_t *magic)
-{
-	return drmGetMagic(drm->fd, (drm_magic_t *) magic);
-}
-
-/*
- * Authenticate a magic.
- */
-int gralloc_drm_auth_magic(struct gralloc_drm_t *drm, int32_t magic)
-{
-	return drmAuthMagic(drm->fd, (drm_magic_t) magic);
-}
-
-/*
- * Set as the master of a DRM device.
- */
-int gralloc_drm_set_master(struct gralloc_drm_t *drm)
-{
-	ALOGD("set master");
-	drmSetMaster(drm->fd);
-	drm->first_post = 1;
-
-	return 0;
-}
-
-/*
- * Drop from the master of a DRM device.
- */
-void gralloc_drm_drop_master(struct gralloc_drm_t *drm)
-{
-	drmDropMaster(drm->fd);
-}
-
-/*
  * Validate a buffer handle and return the associated bo.
  */
 static struct gralloc_drm_bo_t *validate_handle(buffer_handle_t _handle,
@@ -191,6 +156,7 @@
 		return NULL;
 
 	/* the buffer handle is passed to a new process */
+	ALOGE("data_owner=%d gralloc_pid=%d data=%p\n", handle->data_owner, gralloc_drm_get_pid(), handle->data);
 	if (unlikely(handle->data_owner != gralloc_drm_pid)) {
 		struct gralloc_drm_bo_t *bo;
 
@@ -198,8 +164,10 @@
 		if (!drm)
 			return NULL;
 
+		ALOGE("handle: name=%d pfd=%d\n", handle->name,
+			handle->prime_fd);
 		/* create the struct gralloc_drm_bo_t locally */
-		if (handle->name)
+		if (handle->name || handle->prime_fd >= 0)
 			bo = drm->drv->alloc(drm->drv, handle);
 		else /* an invalid handle */
 			bo = NULL;
@@ -263,6 +231,7 @@
 	handle->height = height;
 	handle->format = format;
 	handle->usage = usage;
+	handle->prime_fd = -1;
 
 	return handle;
 }
@@ -347,12 +316,6 @@
 	return &bo->handle->base;
 }
 
-int gralloc_drm_get_gem_handle(buffer_handle_t _handle)
-{
-	struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
-	return (handle) ? handle->name : 0;
-}
-
 /*
  * Query YUV component offsets for a buffer handle
  */
diff --git a/gralloc_drm.h b/gralloc_drm.h
index 1b464cf..3ad818f 100644
--- a/gralloc_drm.h
+++ b/gralloc_drm.h
@@ -38,20 +38,12 @@
 
 enum {
 	GRALLOC_MODULE_PERFORM_GET_DRM_FD                = 0x80000002,
-	GRALLOC_MODULE_PERFORM_GET_DRM_MAGIC             = 0x80000003,
-	GRALLOC_MODULE_PERFORM_AUTH_DRM_MAGIC            = 0x80000004,
-	GRALLOC_MODULE_PERFORM_ENTER_VT                  = 0x80000005,
-	GRALLOC_MODULE_PERFORM_LEAVE_VT                  = 0x80000006,
 };
 
 struct gralloc_drm_t *gralloc_drm_create(void);
 void gralloc_drm_destroy(struct gralloc_drm_t *drm);
 
 int gralloc_drm_get_fd(struct gralloc_drm_t *drm);
-int gralloc_drm_get_magic(struct gralloc_drm_t *drm, int32_t *magic);
-int gralloc_drm_auth_magic(struct gralloc_drm_t *drm, int32_t magic);
-int gralloc_drm_set_master(struct gralloc_drm_t *drm);
-void gralloc_drm_drop_master(struct gralloc_drm_t *drm);
 
 static inline int gralloc_drm_get_bpp(int format)
 {
diff --git a/gralloc_drm_handle.h b/gralloc_drm_handle.h
index 6bf2d2f..313628f 100644
--- a/gralloc_drm_handle.h
+++ b/gralloc_drm_handle.h
@@ -36,9 +36,10 @@
 struct gralloc_drm_handle_t {
 	native_handle_t base;
 
-#define GRALLOC_DRM_HANDLE_MAGIC 0x12345678
-#define GRALLOC_DRM_HANDLE_NUM_INTS 10
-#define GRALLOC_DRM_HANDLE_NUM_FDS 0
+	/* file descriptors */
+	int prime_fd;
+
+	/* integers */
 	int magic;
 
 	int width;
@@ -49,9 +50,17 @@
 	int name;   /* the name of the bo */
 	int stride; /* the stride in bytes */
 
-	int data_owner; /* owner of data (for validation) */
 	struct gralloc_drm_bo_t *data; /* pointer to struct gralloc_drm_bo_t */
+
+	// FIXME: the attributes below should be out-of-line
+	uint64_t unknown __attribute__((aligned(8)));
+	int data_owner; /* owner of data (for validation) */
 };
+#define GRALLOC_DRM_HANDLE_MAGIC 0x12345678
+#define GRALLOC_DRM_HANDLE_NUM_FDS 1
+#define GRALLOC_DRM_HANDLE_NUM_INTS (						\
+	((sizeof(struct gralloc_drm_handle_t) - sizeof(native_handle_t))/sizeof(int))	\
+	 - GRALLOC_DRM_HANDLE_NUM_FDS)
 
 static inline struct gralloc_drm_handle_t *gralloc_drm_handle(buffer_handle_t _handle)
 {
@@ -61,8 +70,12 @@
 	if (handle && (handle->base.version != sizeof(handle->base) ||
 	               handle->base.numInts != GRALLOC_DRM_HANDLE_NUM_INTS ||
 	               handle->base.numFds != GRALLOC_DRM_HANDLE_NUM_FDS ||
-	               handle->magic != GRALLOC_DRM_HANDLE_MAGIC))
+	               handle->magic != GRALLOC_DRM_HANDLE_MAGIC)) {
+		ALOGE("invalid handle: version=%d, numInts=%d, numFds=%d, magic=%x",
+			handle->base.version, handle->base.numInts,
+			handle->base.numFds, handle->magic);
 		handle = NULL;
+	}
 
 	return handle;
 }