drm_hwcomposer: Cache FB and gem in gralloc buffer

Move drm fb object creation to the importer.

In NV importer, use GRALLOC_MODULE_PERFORM_SET_IMPORTER_PRIVATE
and GRALLOC_MODULE_PERFORM_GET_IMPORTER_PRIVATE to cache the hwc_drm_bo
in the gralloc buffer.  This avoids the need to recreate fb objects
again each frame, and also greatly simplifies managing the lifetime of
the gem handles.

The drm_gralloc importer does not support caching at this point.  In an
attempt to keep it somewhat working, add hwc_import_bo_release function
that hwc calls when it's ok to rm fb.  If hwc_import_bo_release returns
true, then hwc will clean up gem handles as it did before.

We should consider doing a follow-up patch that adds fb/gem caching to
drm_gralloc importer also.  Then some of the code that is kept in this
patch for backwards compatibility can be removed.

Change-Id: I92857dcaddf8cea219ebc041e16ef76087a1b696
Reviewed-on: https://chrome-internal-review.googlesource.com/200895
Reviewed-by: Stéphane Marchesin <marcheu@google.com>
Commit-Queue: Stéphane Marchesin <marcheu@google.com>
Tested-by: Stéphane Marchesin <marcheu@google.com>
diff --git a/hwcomposer_import_drm_gralloc.cpp b/hwcomposer_import_drm_gralloc.cpp
index e2e2e94..b724c83 100644
--- a/hwcomposer_import_drm_gralloc.cpp
+++ b/hwcomposer_import_drm_gralloc.cpp
@@ -85,7 +85,7 @@
 	}
 }
 
-int hwc_create_bo_from_import(int fd, hwc_import_context *ctx,
+int hwc_import_bo_create(int fd, hwc_import_context *ctx,
 			buffer_handle_t handle, struct hwc_drm_bo *bo)
 {
 	gralloc_drm_handle_t *gr_handle = gralloc_drm_handle(handle);
@@ -116,5 +116,26 @@
 	bo->gem_handles[0] = gem_handle;
 	bo->offsets[0] = 0;
 
-	return 0;
+	ret = drmModeAddFB2(fd, bo->width, bo->height, bo->format,
+			bo->gem_handles, bo->pitches, bo->offsets,
+			&bo->fb_id, 0);
+	if (ret) {
+		ALOGE("could not create drm fb %d", ret);
+		return ret;
+	}
+
+	return ret;
+}
+
+bool hwc_import_bo_release(int fd, hwc_import_context *ctx,
+			struct hwc_drm_bo *bo)
+{
+	(void)ctx;
+
+	if (bo->fb_id)
+		if (drmModeRmFB(fd, bo->fb_id))
+			ALOGE("Failed to rm fb");
+
+	/* hwc may close the gem handles now. */
+	return true;
 }