From 0fcca6600c7d74ec13986d3e9e795f4a7c8afe59 Mon Sep 17 00:00:00 2001
From: Hiroyuki Ishii <ishii.hiroyuki002@jp.panasonic.com>
Date: Tue, 27 Dec 2022 16:43:46 +0900
Subject: [PATCH 16/17] Imprement drm-lease support

Basic support to utilize drm-lease device via drm-lease-manager.

Known issue:
- --angle option does not work correctly with drm-lease enabled

Signed-off-by: Hiroyuki Ishii <ishii.hiroyuki002@jp.panasonic.com>
---
 Makefile.am         |  5 +++++
 configure.ac        |  9 +++++++++
 psplash-drm-lease.h | 10 ++++++++++
 psplash-drm.c       | 44 +++++++++++++++++++++++++++++++++++++++-----
 psplash.c           | 12 ++++++++++++
 5 files changed, 75 insertions(+), 5 deletions(-)
 create mode 100644 psplash-drm-lease.h

diff --git a/Makefile.am b/Makefile.am
index c3d4f03..3657889 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -19,6 +19,11 @@ psplash_CPPFLAGS += $(LIBDRM_CFLAGS) -DENABLE_DRM
 psplash_LDFLAGS += $(LIBDRM_LIBS)
 endif
 
+if ENABLE_DRM_LEASE
+psplash_CPPFLAGS += $(LIBDRM_LEASE_CFLAGS) -DENABLE_DRM_LEASE
+psplash_LDFLAGS += $(LIBDRM_LEASE_LIBS)
+endif
+
 if HAVE_SYSTEMD
 psplash_CPPFLAGS += $(SYSTEMD_CFLAGS) -DHAVE_SYSTEMD
 psplash_LDFLAGS += $(SYSTEMD_LIBS)
diff --git a/configure.ac b/configure.ac
index 2e5c4f5..1a752e3 100644
--- a/configure.ac
+++ b/configure.ac
@@ -21,6 +21,15 @@ AS_IF([test "x$enable_drm" = "xyes"], [
 
 AM_CONDITIONAL([ENABLE_DRM], [test "x$enable_drm" = "xyes"])
 
+AC_ARG_ENABLE(drm-lease,
+    AS_HELP_STRING([--enable-drm-lease], [enable drm-lease (default is 'no')]))
+
+AS_IF([test "x$enable_drm_lease" = "xyes"], [
+    PKG_CHECK_MODULES(LIBDRM_LEASE, libdlmclient)
+])
+
+AM_CONDITIONAL([ENABLE_DRM_LEASE], [test "x$enable_drm_lease" = "xyes"])
+
 AC_ARG_WITH([systemd], AS_HELP_STRING([--with-systemd], [Build with systemd
 	     support]))
 
diff --git a/psplash-drm-lease.h b/psplash-drm-lease.h
new file mode 100644
index 0000000..46289c3
--- /dev/null
+++ b/psplash-drm-lease.h
@@ -0,0 +1,10 @@
+#ifndef _HAVE_PSPLASH_DRM_LEASE_H
+#define _HAVE_PSPLASH_DRM_LEASE_H
+#ifdef ENABLE_DRM_LEASE
+
+#include <dlmclient.h>
+
+void drm_set_lease_name(char *);
+
+#endif // ENABLE_DRM_LEASE
+#endif // _HAVE_PSPLASH_DRM_LEASE_H
diff --git a/psplash-drm.c b/psplash-drm.c
index fcb7507..a5aff90 100644
--- a/psplash-drm.c
+++ b/psplash-drm.c
@@ -39,6 +39,9 @@
 #include <xf86drm.h>
 #include <xf86drmMode.h>
 #include "psplash-drm.h"
+#ifdef ENABLE_DRM_LEASE
+#include "psplash-drm-lease.h"
+#endif
 
 #define MIN(a,b) ((a) < (b) ? (a) : (b))
 
@@ -53,6 +56,10 @@ static int modeset_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
 static int modeset_open(int *out, const char *node);
 static int modeset_prepare(int fd);
 
+#ifdef ENABLE_DRM_LEASE
+char *drm_lease_name;
+#endif
+
 /*
  * When the linux kernel detects a graphics-card on your machine, it loads the
  * correct device driver (located in kernel-tree at ./drivers/gpu/drm/<xy>) and
@@ -662,6 +669,7 @@ PSplashDRM* psplash_drm_new(int angle, int dev_id)
 		perror("malloc");
 		goto error;
 	}
+	drm->fd = 0;
 	drm->canvas.priv = drm;
 	drm->canvas.flip = psplash_drm_flip;
 
@@ -670,12 +678,30 @@ PSplashDRM* psplash_drm_new(int angle, int dev_id)
 		card[13] = dev_id + 48;
 	}
 
-	fprintf(stderr, "using card '%s'\n", card);
+#ifdef ENABLE_DRM_LEASE
+	if (drm_lease_name) {
+		fprintf(stderr, "using drm lease '%s'\n", drm_lease_name);
+		struct dlm_lease *lease = dlm_get_lease(drm_lease_name);
+		if (lease) {
+			drm->fd = dlm_lease_fd(lease);
+			if (!drm->fd)
+				dlm_release_lease(lease);
+		}
+		if (!drm->fd) {
+			fprintf(stderr, "Could not get DRM lease %s\n", drm_lease_name);
+			goto error;
+		}
+	}
+#endif
 
-	/* open the DRM device */
-	ret = modeset_open(&drm->fd, card);
-	if (ret)
-		goto error;
+	if (!drm->fd) {
+		fprintf(stderr, "using card '%s'\n", card);
+
+		/* open the DRM device */
+		ret = modeset_open(&drm->fd, card);
+		if (ret)
+			goto error;
+	}
 
 	/* prepare all connectors and CRTCs */
 	ret = modeset_prepare(drm->fd);
@@ -758,6 +784,14 @@ void psplash_drm_destroy(PSplashDRM *drm)
 	free(drm);
 }
 
+#ifdef ENABLE_DRM_LEASE
+void drm_set_lease_name (char *name) {
+	if (!name)
+		return;
+	drm_lease_name = strdup(name);
+}
+#endif
+
 /*
  * I hope this was a short but easy overview of the DRM modesetting API. The DRM
  * API offers much more capabilities including:
diff --git a/psplash.c b/psplash.c
index ebf8d7a..4aa650d 100644
--- a/psplash.c
+++ b/psplash.c
@@ -15,6 +15,9 @@
 #ifdef ENABLE_DRM
 #include "psplash-drm.h"
 #endif
+#ifdef ENABLE_DRM_LEASE
+#include "psplash-drm-lease.h"
+#endif
 #include "psplash-config.h"
 #include "psplash-colors.h"
 #include "psplash-poky-img.h"
@@ -267,6 +270,15 @@ main (int argc, char** argv)
         continue;
     }
 #endif
+#ifdef ENABLE_DRM_LEASE
+    if (!strcmp(argv[i],"--drm-lease"))
+      {
+        if (++i >= argc) goto fail;
+        drm_set_lease_name(argv[i]);
+        use_drm = 1;
+        continue;
+      }
+#endif
 
     fail:
       fprintf(stderr,
-- 
2.25.1