drm_hwcomposer: Use prime to import gem handles

Change-Id: Id5d5f9a61f092f33130fed4d8b8d00fcaf401dea
Signed-off-by: Sean Paul <seanpaul@chromium.org>
diff --git a/hwcomposer.cpp b/hwcomposer.cpp
index 2ed2073..912bb74 100644
--- a/hwcomposer.cpp
+++ b/hwcomposer.cpp
@@ -166,20 +166,6 @@
 	return ret;
 }
 
-/*
- * TODO: This hack allows us to use the importer's fd to drm to add and remove
- * framebuffers. The reason it exists is because gralloc doesn't export its
- * bo's, so we have to use its file descriptor to drm for some operations. Once
- * gralloc behaves, we can remove this.
- */
-static int hwc_get_fd_for_bo(struct hwc_context_t *ctx, struct hwc_drm_bo *bo)
-{
-	if (bo->importer_fd >= 0)
-		return bo->importer_fd;
-
-	return ctx->fd;
-}
-
 static bool hwc_mode_is_equal(drmModeModeInfoPtr a, drmModeModeInfoPtr b)
 {
 	return a->clock == b->clock &&
@@ -290,9 +276,10 @@
 static int hwc_wait_and_set(struct hwc_drm_display *hd,
 			struct hwc_drm_bo *buf)
 {
-	int ret;
+	struct drm_gem_close args;
+	int ret, i;
 
-	ret = drmModeAddFB2(hwc_get_fd_for_bo(hd->ctx, buf), buf->width,
+	ret = drmModeAddFB2(hd->ctx->fd, buf->width,
 		buf->height, buf->format, buf->gem_handles, buf->pitches,
 		buf->offsets, &buf->fb_id, 0);
 	if (ret) {
@@ -315,13 +302,20 @@
 	}
 
 	if (hd->front.fb_id) {
-		ret = drmModeRmFB(hwc_get_fd_for_bo(hd->ctx, &hd->front),
-				hd->front.fb_id);
+		ret = drmModeRmFB(hd->ctx->fd, hd->front.fb_id);
 		if (ret) {
 			ALOGE("Failed to rm fb from front %d", ret);
 			return ret;
 		}
 	}
+
+	memset(&args, 0, sizeof(args));
+	for (i = 0; i < ARRAY_SIZE(hd->front.gem_handles); i++) {
+		if (!hd->front.gem_handles[i])
+			continue;
+		args.handle = hd->front.gem_handles[i];
+		drmIoctl(hd->ctx->fd, DRM_IOCTL_GEM_CLOSE, &args);
+	}
 	hd->front = *buf;
 
 	return ret;
@@ -1129,11 +1123,7 @@
 
 	ret = hwc_get_display_configs(&hd->ctx->device, hd->display, &config,
 		&num_configs);
-	if (ret) {
-		ALOGE("Failed to get display configs d=%d ret=%d", hd->display,
-			ret);
-	}
-	if (!num_configs)
+	if (ret || !num_configs)
 		return 0;
 
 	ret = hwc_set_active_config(&hd->ctx->device, hd->display, 0);
diff --git a/hwcomposer_import_drm_gralloc.cpp b/hwcomposer_import_drm_gralloc.cpp
index 1e54f70..e2e2e94 100644
--- a/hwcomposer_import_drm_gralloc.cpp
+++ b/hwcomposer_import_drm_gralloc.cpp
@@ -85,13 +85,16 @@
 	}
 }
 
-int hwc_create_bo_from_import(int /* fd */, hwc_import_context *ctx,
+int hwc_create_bo_from_import(int fd, hwc_import_context *ctx,
 			buffer_handle_t handle, struct hwc_drm_bo *bo)
 {
-	gralloc_drm_t *drm = ctx->gralloc_module->drm;
-	gralloc_drm_handle_t *gr_handle;
+	gralloc_drm_handle_t *gr_handle = gralloc_drm_handle(handle);
 	struct gralloc_drm_bo_t *gralloc_bo;
-	gr_handle = (gralloc_drm_handle_t *)handle;
+	uint32_t gem_handle;
+	int ret;
+
+	if (!gr_handle)
+		return -EINVAL;
 
 	gralloc_bo = gr_handle->data;
 	if (!gralloc_bo) {
@@ -99,12 +102,18 @@
 		return -EINVAL;
 	}
 
-	bo->importer_fd = drm->fd;
+	ret = drmPrimeFDToHandle(fd, gr_handle->prime_fd, &gem_handle);
+	if (ret) {
+		ALOGE("failed to import prime fd %d ret=%d",
+			gr_handle->prime_fd, ret);
+		return ret;
+	}
+
 	bo->width = gr_handle->width;
 	bo->height = gr_handle->height;
 	bo->format = hwc_convert_hal_format_to_drm_format(gr_handle->format);
 	bo->pitches[0] = gr_handle->stride;
-	bo->gem_handles[0] = gralloc_bo->fb_handle;
+	bo->gem_handles[0] = gem_handle;
 	bo->offsets[0] = 0;
 
 	return 0;