From 3dbffb783f44752ec221a2ee7a94a21934d681a2 Mon Sep 17 00:00:00 2001
From: Wataru Mizuno <wmizuno@jp.adit-jv.com>
Date: Tue, 10 Apr 2018 12:22:07 +0900
Subject: [PATCH 5/5] compositor-drm: introduce drm_get_dmafd_from_view

This API enables to get dmafd from weston_view

Signed-off-by: Wataru Mizuno <wmizuno@jp.adit-jv.com>
---
 libweston/compositor-drm.c | 80 ++++++++++++++++++++++++++++++++++++++++++++++
 libweston/compositor-drm.h |  7 ++++
 2 files changed, 87 insertions(+)

diff --git a/libweston/compositor-drm.c b/libweston/compositor-drm.c
index 0b5b4c4..77f2ece 100644
--- a/libweston/compositor-drm.c
+++ b/libweston/compositor-drm.c
@@ -2430,6 +2430,85 @@ drm_output_set_seat(struct weston_output *base,
 }
 
 static int
+drm_get_dma_fd_from_view(struct weston_output *base,
+			   struct weston_view *ev)
+{
+	struct drm_backend *b = to_drm_backend(base->compositor);
+	struct weston_buffer *buffer = ev->surface->buffer_ref.buffer;
+	struct gbm_bo *bo;
+	struct drm_fb *current;
+	struct linux_dmabuf_buffer *dmabuf;
+	uint32_t format;
+	int fd, ret;
+
+	if(!buffer) {
+		weston_log("buffer is NULL\n");
+		return -1;
+	}
+
+	if(dmabuf = linux_dmabuf_buffer_get(buffer->resource)) {
+		struct gbm_import_fd_data gbm_dmabuf = {
+			.fd     = dmabuf->attributes.fd[0],
+			.width  = dmabuf->attributes.width,
+			.height = dmabuf->attributes.height,
+			.stride = dmabuf->attributes.stride[0],
+			.format = dmabuf->attributes.format
+		};
+
+		bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_FD,
+				   &gbm_dmabuf, GBM_BO_USE_SCANOUT);
+		if (!bo) {
+			weston_log("failed to get gbm_bo\n");
+			return -1;
+		}
+
+		current = zalloc(sizeof *current);
+		current->handle = gbm_bo_get_handle(bo).s32;
+		if (!current->handle) {
+			fprintf(stderr, "failed to get drm_handle\n");
+			return -1;
+		}
+	}
+	else if(ev->surface->buffer_ref.buffer->legacy_buffer) {
+		bo = gbm_bo_import(b->gbm, GBM_BO_IMPORT_WL_BUFFER,
+				   buffer->resource, GBM_BO_USE_SCANOUT);
+
+		if (!bo) {
+			weston_log("failed to get gbm_bo\n");
+			return -1;
+		}
+
+		format = gbm_bo_get_format(bo);
+		if (!format) {
+			weston_log("failed to get format\n");
+			gbm_bo_destroy(bo);
+			return -1;
+		}
+
+		current = drm_fb_get_from_bo(bo, b, format);
+		if (!current) {
+			weston_log("failed to get drm_fb\n");
+			gbm_bo_destroy(bo);
+			return -1;
+		}
+	}
+	else {
+		weston_log("Buffer is not supported\n");
+		return -1;
+	}
+
+	ret = drmPrimeHandleToFD(b->drm.fd, current->handle,
+				 DRM_CLOEXEC, &fd);
+	free(current);
+	if (ret) {
+		weston_log("failed to create prime fd for front buffer\n");
+		return -1;
+	}
+
+	return fd;
+}
+
+static int
 drm_output_enable(struct weston_output *base)
 {
 	struct drm_output *output = to_drm_output(base);
@@ -3199,6 +3278,7 @@ static const struct weston_drm_output_api api = {
 	drm_output_set_mode,
 	drm_output_set_gbm_format,
 	drm_output_set_seat,
+	drm_get_dma_fd_from_view,
 };
 
 static struct drm_backend *
diff --git a/libweston/compositor-drm.h b/libweston/compositor-drm.h
index 2e2995a..fe00bd5 100644
--- a/libweston/compositor-drm.h
+++ b/libweston/compositor-drm.h
@@ -78,6 +78,13 @@ struct weston_drm_output_api {
 	 */
 	void (*set_seat)(struct weston_output *output,
 			 const char *seat);
+
+	/** Get the dma fd from drm view.
+	 *
+         *  The dma fd is got from weston_view.
+	 *  Returns fd on success, -1 on failure.
+	 */
+	int (*get_dma_fd_from_view)(struct weston_output *output, struct weston_view *view);
 };
 
 static inline const struct weston_drm_output_api *
-- 
2.7.4

