revise gralloc_drm bo interface

Make it more intuitive to use.
diff --git a/gralloc.c b/gralloc.c
index 0cf63e5..fec57be 100644
--- a/gralloc.c
+++ b/gralloc.c
@@ -121,41 +121,22 @@
 	if (err)
 		return err;
 
-	return (gralloc_drm_bo_register(dmod->drm, handle, 1)) ? 0 : -EINVAL;
+	return gralloc_drm_handle_register(handle, dmod->drm);
 }
 
 static int drm_mod_unregister_buffer(const gralloc_module_t *mod,
 		buffer_handle_t handle)
 {
-	struct drm_module_t *dmod = (struct drm_module_t *) mod;
-	struct gralloc_drm_bo_t *bo;
-	int err;
-
-	err = drm_init(dmod, 0);
-	if (err)
-		return err;
-
-	bo = gralloc_drm_bo_validate(dmod->drm, handle);
-	if (!bo)
-		return -EINVAL;
-
-	gralloc_drm_bo_unregister(bo);
-
-	return 0;
+	return gralloc_drm_handle_unregister(handle);
 }
 
 static int drm_mod_lock(const gralloc_module_t *mod, buffer_handle_t handle,
 		int usage, int x, int y, int w, int h, void **ptr)
 {
-	struct drm_module_t *dmod = (struct drm_module_t *) mod;
 	struct gralloc_drm_bo_t *bo;
 	int err;
 
-	err = drm_init(dmod, 0);
-	if (err)
-		return err;
-
-	bo = gralloc_drm_bo_validate(dmod->drm, handle);
+	bo = gralloc_drm_bo_from_handle(handle);
 	if (!bo)
 		return -EINVAL;
 
@@ -167,7 +148,7 @@
 	struct drm_module_t *dmod = (struct drm_module_t *) mod;
 	struct gralloc_drm_bo_t *bo;
 
-	bo = gralloc_drm_bo_validate(dmod->drm, handle);
+	bo = gralloc_drm_bo_from_handle(handle);
 	if (!bo)
 		return -EINVAL;
 
@@ -190,7 +171,7 @@
 	struct drm_module_t *dmod = (struct drm_module_t *) dev->common.module;
 	struct gralloc_drm_bo_t *bo;
 
-	bo = gralloc_drm_bo_validate(dmod->drm, handle);
+	bo = gralloc_drm_bo_from_handle(handle);
 	if (!bo)
 		return -EINVAL;
 
@@ -282,7 +263,7 @@
 	struct drm_module_t *dmod = (struct drm_module_t *) fb->common.module;
 	struct gralloc_drm_bo_t *bo;
 
-	bo = gralloc_drm_bo_validate(dmod->drm, handle);
+	bo = gralloc_drm_bo_from_handle(handle);
 	if (!bo)
 		return -EINVAL;
 
diff --git a/gralloc_drm.c b/gralloc_drm.c
index d67561f..57021ac 100644
--- a/gralloc_drm.c
+++ b/gralloc_drm.c
@@ -180,6 +180,68 @@
 }
 
 /*
+ * Validate a buffer handle and return the associated bo.
+ */
+static struct gralloc_drm_bo_t *validate_handle(buffer_handle_t _handle,
+		struct gralloc_drm_t *drm)
+{
+	struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
+
+	if (!handle)
+		return NULL;
+
+	/* the buffer handle is passed to a new process */
+	if (unlikely(handle->data_owner != gralloc_drm_pid)) {
+		struct gralloc_drm_bo_t *bo;
+
+		/* check only */
+		if (!drm)
+			return NULL;
+
+		/* create the struct gralloc_drm_bo_t locally */
+		if (handle->name)
+			bo = drm->drv->alloc(drm->drv, handle);
+		else /* an invalid handle */
+			bo = NULL;
+		if (bo) {
+			bo->drm = drm;
+			bo->imported = 1;
+			bo->handle = handle;
+		}
+
+		handle->data_owner = gralloc_drm_get_pid();
+		handle->data = (int) bo;
+	}
+
+	return (struct gralloc_drm_bo_t *) handle->data;
+}
+
+/*
+ * Register a buffer handle.
+ */
+int gralloc_drm_handle_register(buffer_handle_t handle, struct gralloc_drm_t *drm)
+{
+	return (validate_handle(handle, drm)) ? 0 : -EINVAL;
+}
+
+/*
+ * Unregister a buffer handle.  It is no-op for handles created locally.
+ */
+int gralloc_drm_handle_unregister(buffer_handle_t handle)
+{
+	struct gralloc_drm_bo_t *bo;
+
+	bo = validate_handle(handle, NULL);
+	if (!bo)
+		return -EINVAL;
+
+	if (bo->imported)
+		gralloc_drm_bo_destroy(bo);
+
+	return 0;
+}
+
+/*
  * Create a buffer handle.
  */
 static struct gralloc_drm_handle_t *create_bo_handle(int width,
@@ -252,48 +314,21 @@
 }
 
 /*
- * Register a buffer handle and return the associated bo.
+ * Return the bo of a registered handle.
  */
-struct gralloc_drm_bo_t *gralloc_drm_bo_register(struct gralloc_drm_t *drm,
-		buffer_handle_t _handle, int create)
+struct gralloc_drm_bo_t *gralloc_drm_bo_from_handle(buffer_handle_t handle)
 {
-	struct gralloc_drm_handle_t *handle = gralloc_drm_handle(_handle);
-
-	if (!handle)
-		return NULL;
-
-	/* the buffer handle is passed to a new process */
-	if (unlikely(handle->data_owner != gralloc_drm_pid)) {
-		struct gralloc_drm_bo_t *bo;
-
-		if (!create)
-			return NULL;
-
-		/* create the struct gralloc_drm_bo_t locally */
-		if (handle->name)
-			bo = drm->drv->alloc(drm->drv, handle);
-		else /* an invalid handle */
-			bo = NULL;
-		if (bo) {
-			bo->drm = drm;
-			bo->imported = 1;
-			bo->handle = handle;
-		}
-
-		handle->data_owner = gralloc_drm_get_pid();
-		handle->data = (int) bo;
-	}
-
-	return (struct gralloc_drm_bo_t *) handle->data;
+	return validate_handle(handle, NULL);
 }
 
 /*
- * Unregister a bo.  It is no-op for bo created locally.
+ * Get the buffer handle and stride of a bo.
  */
-void gralloc_drm_bo_unregister(struct gralloc_drm_bo_t *bo)
+buffer_handle_t gralloc_drm_bo_get_handle(struct gralloc_drm_bo_t *bo, int *stride)
 {
-	if (bo->imported)
-		gralloc_drm_bo_destroy(bo);
+	if (stride)
+		*stride = bo->handle->stride;
+	return &bo->handle->base;
 }
 
 /*
@@ -352,13 +387,3 @@
 	if (!bo->lock_count)
 		bo->locked_for = 0;
 }
-
-/*
- * Get the buffer handle and stride of a bo.
- */
-buffer_handle_t gralloc_drm_bo_get_handle(struct gralloc_drm_bo_t *bo, int *stride)
-{
-	if (stride)
-		*stride = bo->handle->stride;
-	return &bo->handle->base;
-}
diff --git a/gralloc_drm.h b/gralloc_drm.h
index 6d3aa1f..b9c0de8 100644
--- a/gralloc_drm.h
+++ b/gralloc_drm.h
@@ -71,20 +71,17 @@
 	return bpp;
 }
 
+int gralloc_drm_handle_register(buffer_handle_t handle, struct gralloc_drm_t *drm);
+int gralloc_drm_handle_unregister(buffer_handle_t handle);
+
 struct gralloc_drm_bo_t *gralloc_drm_bo_create(struct gralloc_drm_t *drm, int width, int height, int format, int usage);
 void gralloc_drm_bo_destroy(struct gralloc_drm_bo_t *bo);
 
-struct gralloc_drm_bo_t *gralloc_drm_bo_register(struct gralloc_drm_t *drm, buffer_handle_t handle, int create);
-void gralloc_drm_bo_unregister(struct gralloc_drm_bo_t *bo);
-
-static inline struct gralloc_drm_bo_t *gralloc_drm_bo_validate(struct gralloc_drm_t *drm, buffer_handle_t handle)
-{
-	return gralloc_drm_bo_register(drm, handle, 0);
-}
+struct gralloc_drm_bo_t *gralloc_drm_bo_from_handle(buffer_handle_t handle);
+buffer_handle_t gralloc_drm_bo_get_handle(struct gralloc_drm_bo_t *bo, int *stride);
 
 int gralloc_drm_bo_lock(struct gralloc_drm_bo_t *bo, int x, int y, int w, int h, int enable_write, void **addr);
 void gralloc_drm_bo_unlock(struct gralloc_drm_bo_t *bo);
-buffer_handle_t gralloc_drm_bo_get_handle(struct gralloc_drm_bo_t *bo, int *stride);
 
 int gralloc_drm_bo_need_fb(const struct gralloc_drm_bo_t *bo);
 int gralloc_drm_bo_add_fb(struct gralloc_drm_bo_t *bo);