1From d0308f183afba93079dc3fc09ee498aee36581c1 Mon Sep 17 00:00:00 2001
2From: Jeffy Chen <jeffy.chen@rock-chips.com>
3Date: Mon, 15 Jun 2020 10:03:01 +0800
4Subject: [PATCH 08/33] kmssink: Support setting plane zpos
5
6Set env KMSSINK_PLANE_ZPOS to specify plane zpos.
7Set env KMSSINK_PLANE_ON_TOP to set max zpos.
8Set env KMSSINK_PLANE_ON_BOTTOM to set min zpos.
9
10Default zpos is max.
11
12Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
13---
14 sys/kms/gstkmssink.c | 78 ++++++++++++++++++++++++++++++++++++++++++++
15 sys/kms/gstkmssink.h |  1 +
16 2 files changed, 79 insertions(+)
17
18diff --git a/sys/kms/gstkmssink.c b/sys/kms/gstkmssink.c
19index 0cb2bb6..b84657c 100644
20--- a/sys/kms/gstkmssink.c
21+++ b/sys/kms/gstkmssink.c
22@@ -722,6 +722,76 @@ gst_kms_sink_update_plane_properties (GstKMSSink * self)
23   gst_kms_sink_update_properties (&iter, self->plane_props);
24 }
25
26+static void
27+gst_kms_sink_configure_plane_zpos (GstKMSSink * self, gboolean restore)
28+{
29+  drmModeObjectPropertiesPtr props = NULL;
30+  drmModePropertyPtr prop = NULL;
31+  drmModeResPtr res = NULL;
32+  guint64 min, max, zpos;
33+  const gchar *buf;
34+  gint i;
35+
36+  if (self->plane_id <= 0)
37+    return;
38+
39+  if (drmSetClientCap (self->fd, DRM_CLIENT_CAP_ATOMIC, 1))
40+    return;
41+
42+  res = drmModeGetResources (self->fd);
43+  if (!res)
44+    return;
45+
46+  props = drmModeObjectGetProperties (self->fd, self->plane_id,
47+      DRM_MODE_OBJECT_PLANE);
48+  if (!props)
49+    goto out;
50+
51+  for (i = 0; i < props->count_props; i++) {
52+    prop = drmModeGetProperty (self->fd, props->props[i]);
53+    if (prop && !strcmp (prop->name, "ZPOS"))
54+      break;
55+    drmModeFreeProperty (prop);
56+    prop = NULL;
57+  }
58+
59+  if (!prop)
60+    goto out;
61+
62+  min = prop->values[0];
63+  max = prop->values[1];
64+
65+  if (restore) {
66+    if (self->saved_zpos < 0)
67+      goto out;
68+
69+    zpos = self->saved_zpos;
70+  } else {
71+    zpos = min + 1;
72+
73+    buf = g_getenv ("KMSSINK_PLANE_ZPOS");
74+    if (buf)
75+      zpos = atoi (buf);
76+    else if (g_getenv ("KMSSINK_PLANE_ON_TOP"))
77+      zpos = max;
78+    else if (g_getenv ("KMSSINK_PLANE_ON_BOTTOM"))
79+      zpos = min;
80+  }
81+
82+  GST_INFO_OBJECT (self, "set plane zpos = %lu (%lu~%lu)", zpos, min, max);
83+
84+  if (self->saved_zpos < 0)
85+    self->saved_zpos = props->prop_values[i];
86+
87+  drmModeObjectSetProperty (self->fd, self->plane_id,
88+      DRM_MODE_OBJECT_PLANE, props->props[i], zpos);
89+
90+out:
91+  drmModeFreeProperty (prop);
92+  drmModeFreeObjectProperties (props);
93+  drmModeFreeResources (res);
94+}
95+
96 static gboolean
97 gst_kms_sink_start (GstBaseSink * bsink)
98 {
99@@ -802,6 +872,8 @@ retry_find_plane:
100   self->crtc_id = crtc->crtc_id;
101   self->plane_id = plane->plane_id;
102
103+  gst_kms_sink_configure_plane_zpos (self, FALSE);
104+
105   GST_INFO_OBJECT (self, "connector id = %d / crtc id = %d / plane id = %d",
106       self->conn_id, self->crtc_id, self->plane_id);
107
108@@ -936,6 +1008,11 @@ gst_kms_sink_stop (GstBaseSink * bsink)
109   if (self->allocator)
110     gst_kms_allocator_clear_cache (self->allocator);
111
112+  if (self->saved_zpos >= 0) {
113+    gst_kms_sink_configure_plane_zpos (self, TRUE);
114+    self->saved_zpos = -1;
115+  }
116+
117   gst_buffer_replace (&self->last_buffer, NULL);
118   gst_caps_replace (&self->allowed_caps, NULL);
119   gst_object_replace ((GstObject **) & self->pool, NULL);
120@@ -1931,6 +2008,7 @@ gst_kms_sink_init (GstKMSSink * sink)
121   sink->fd = -1;
122   sink->conn_id = -1;
123   sink->plane_id = -1;
124+  sink->saved_zpos = -1;
125   sink->can_scale = TRUE;
126   gst_poll_fd_init (&sink->pollfd);
127   sink->poll = gst_poll_new (TRUE);
128diff --git a/sys/kms/gstkmssink.h b/sys/kms/gstkmssink.h
129index 0fccfa2..45a9c08 100644
130--- a/sys/kms/gstkmssink.h
131+++ b/sys/kms/gstkmssink.h
132@@ -53,6 +53,7 @@ struct _GstKMSSink {
133   gint crtc_id;
134   gint plane_id;
135   guint pipe;
136+  guint saved_zpos;
137
138   /* crtc data */
139   guint16 hdisplay, vdisplay;
140--
1412.20.1
142
143