1 /* GStreamer
2  *
3  * Copyright (C) 2016 Igalia
4  *
5  * Authors:
6  *  Víctor Manuel Jáquez Leal <vjaquez@igalia.com>
7  *  Javier Martin <javiermartin@by.com.es>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this library; if not, write to the
21  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22  * Boston, MA 02110-1301, USA.
23  *
24  */
25 
26 /**
27  * SECTION:element-kmssink
28  * @title: kmssink
29  * @short_description: A KMS/DRM based video sink
30  *
31  * kmssink is a simple video sink that renders video frames directly
32  * in a plane of a DRM device.
33  *
34  * In advance usage, the behaviour of kmssink can be change using the
35  * supported properties. Note that plane and connectors IDs and properties can
36  * be enumerated using the modetest command line tool.
37  *
38  * ## Example launch line
39  * |[
40  * gst-launch-1.0 videotestsrc ! kmssink
41  * gst-launch-1.0 videotestsrc ! kmssink plane-properties=s,rotation=4
42  * ]|
43  *
44  */
45 
46 #ifdef HAVE_CONFIG_H
47 #include "config.h"
48 #endif
49 
50 #include <gst/video/video.h>
51 #include <gst/video/videooverlay.h>
52 #include <gst/allocators/gstdmabuf.h>
53 
54 #include <drm.h>
55 #include <xf86drm.h>
56 #include <xf86drmMode.h>
57 #include <drm_fourcc.h>
58 #include <string.h>
59 
60 #include <string.h>
61 
62 #include "gstkmssink.h"
63 #include "gstkmsutils.h"
64 #include "gstkmsbufferpool.h"
65 #include "gstkmsallocator.h"
66 
67 #define GST_PLUGIN_NAME "kmssink"
68 #define GST_PLUGIN_DESC "Video sink using the Linux kernel mode setting API"
69 
70 GST_DEBUG_CATEGORY_STATIC (gst_kms_sink_debug);
71 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
72 #define GST_CAT_DEFAULT gst_kms_sink_debug
73 
74 static GstFlowReturn gst_kms_sink_show_frame (GstVideoSink * vsink,
75     GstBuffer * buf);
76 static void gst_kms_sink_video_overlay_init (GstVideoOverlayInterface * iface);
77 static void gst_kms_sink_drain (GstKMSSink * self);
78 
79 #define parent_class gst_kms_sink_parent_class
80 G_DEFINE_TYPE_WITH_CODE (GstKMSSink, gst_kms_sink, GST_TYPE_VIDEO_SINK,
81     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, GST_PLUGIN_NAME, 0,
82         GST_PLUGIN_DESC);
83     GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
84     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
85         gst_kms_sink_video_overlay_init));
86 
87 enum
88 {
89   PROP_DRIVER_NAME = 1,
90   PROP_BUS_ID,
91   PROP_CONNECTOR_ID,
92   PROP_PLANE_ID,
93   PROP_FORCE_MODESETTING,
94   PROP_RESTORE_CRTC,
95   PROP_CAN_SCALE,
96   PROP_DISPLAY_WIDTH,
97   PROP_DISPLAY_HEIGHT,
98   PROP_CONNECTOR_PROPS,
99   PROP_PLANE_PROPS,
100   PROP_N,
101 };
102 
103 static GParamSpec *g_properties[PROP_N] = { NULL, };
104 
105 static void
gst_kms_sink_set_render_rectangle(GstVideoOverlay * overlay,gint x,gint y,gint width,gint height)106 gst_kms_sink_set_render_rectangle (GstVideoOverlay * overlay,
107     gint x, gint y, gint width, gint height)
108 {
109   GstKMSSink *self = GST_KMS_SINK (overlay);
110 
111   GST_DEBUG_OBJECT (self, "Setting render rectangle to (%d,%d) %dx%d", x, y,
112       width, height);
113 
114   GST_OBJECT_LOCK (self);
115 
116   if (width == -1 && height == -1) {
117     x = 0;
118     y = 0;
119     width = self->hdisplay;
120     height = self->vdisplay;
121   }
122 
123   if (width <= 0 || height <= 0)
124     goto done;
125 
126   self->pending_rect.x = x;
127   self->pending_rect.y = y;
128   self->pending_rect.w = width;
129   self->pending_rect.h = height;
130 
131   if (self->can_scale ||
132       (self->render_rect.w == width && self->render_rect.h == height)) {
133     self->render_rect = self->pending_rect;
134   } else {
135     self->reconfigure = TRUE;
136     GST_DEBUG_OBJECT (self, "Waiting for new caps to apply render rectangle");
137   }
138 
139 done:
140   GST_OBJECT_UNLOCK (self);
141 }
142 
143 static void
gst_kms_sink_expose(GstVideoOverlay * overlay)144 gst_kms_sink_expose (GstVideoOverlay * overlay)
145 {
146   GstKMSSink *self = GST_KMS_SINK (overlay);
147 
148   GST_DEBUG_OBJECT (overlay, "Expose called by application");
149 
150   if (!self->can_scale) {
151     GST_OBJECT_LOCK (self);
152     if (self->reconfigure) {
153       GST_OBJECT_UNLOCK (self);
154       GST_DEBUG_OBJECT (overlay, "Sending a reconfigure event");
155       gst_pad_push_event (GST_BASE_SINK_PAD (self),
156           gst_event_new_reconfigure ());
157     } else {
158       GST_DEBUG_OBJECT (overlay, "Applying new render rectangle");
159       /* size of the rectangle does not change, only the (x,y) position changes */
160       self->render_rect = self->pending_rect;
161       GST_OBJECT_UNLOCK (self);
162     }
163   }
164 
165   gst_kms_sink_show_frame (GST_VIDEO_SINK (self), NULL);
166 }
167 
168 static void
gst_kms_sink_video_overlay_init(GstVideoOverlayInterface * iface)169 gst_kms_sink_video_overlay_init (GstVideoOverlayInterface * iface)
170 {
171   iface->expose = gst_kms_sink_expose;
172   iface->set_render_rectangle = gst_kms_sink_set_render_rectangle;
173 }
174 
175 static int
kms_open(gchar ** driver)176 kms_open (gchar ** driver)
177 {
178   static const char *drivers[] = { "i915", "radeon", "nouveau", "vmwgfx",
179     "exynos", "amdgpu", "imx-drm", "rockchip", "atmel-hlcdc", "msm",
180     "xlnx", "vc4", "meson", "sun4i-drm", "mxsfb-drm",
181     "xilinx_drm",               /* DEPRECATED. Replaced by xlnx */
182   };
183   int i, fd = -1;
184 
185   for (i = 0; i < G_N_ELEMENTS (drivers); i++) {
186     fd = drmOpen (drivers[i], NULL);
187     if (fd >= 0) {
188       if (driver)
189         *driver = g_strdup (drivers[i]);
190       break;
191     }
192   }
193 
194   return fd;
195 }
196 
197 static drmModePlane *
find_plane_for_crtc(int fd,drmModeRes * res,drmModePlaneRes * pres,int crtc_id)198 find_plane_for_crtc (int fd, drmModeRes * res, drmModePlaneRes * pres,
199     int crtc_id)
200 {
201   drmModePlane *plane;
202   int i, pipe;
203 
204   plane = NULL;
205   pipe = -1;
206   for (i = 0; i < res->count_crtcs; i++) {
207     if (crtc_id == res->crtcs[i]) {
208       pipe = i;
209       break;
210     }
211   }
212 
213   if (pipe == -1)
214     return NULL;
215 
216   for (i = 0; i < pres->count_planes; i++) {
217     plane = drmModeGetPlane (fd, pres->planes[i]);
218     if (plane->possible_crtcs & (1 << pipe))
219       return plane;
220     drmModeFreePlane (plane);
221   }
222 
223   return NULL;
224 }
225 
226 static drmModeCrtc *
find_crtc_for_connector(int fd,drmModeRes * res,drmModeConnector * conn,guint * pipe)227 find_crtc_for_connector (int fd, drmModeRes * res, drmModeConnector * conn,
228     guint * pipe)
229 {
230   int i;
231   int crtc_id;
232   drmModeEncoder *enc;
233   drmModeCrtc *crtc;
234   guint32 crtcs_for_connector = 0;
235 
236   crtc_id = -1;
237   for (i = 0; i < res->count_encoders; i++) {
238     enc = drmModeGetEncoder (fd, res->encoders[i]);
239     if (enc) {
240       if (enc->encoder_id == conn->encoder_id) {
241         crtc_id = enc->crtc_id;
242         drmModeFreeEncoder (enc);
243         break;
244       }
245       drmModeFreeEncoder (enc);
246     }
247   }
248 
249   /* If no active crtc was found, pick the first possible crtc */
250   if (crtc_id == -1) {
251     for (i = 0; i < conn->count_encoders; i++) {
252       enc = drmModeGetEncoder (fd, conn->encoders[i]);
253       crtcs_for_connector |= enc->possible_crtcs;
254       drmModeFreeEncoder (enc);
255     }
256 
257     if (crtcs_for_connector != 0)
258       crtc_id = res->crtcs[ffs (crtcs_for_connector) - 1];
259   }
260 
261   if (crtc_id == -1)
262     return NULL;
263 
264   for (i = 0; i < res->count_crtcs; i++) {
265     crtc = drmModeGetCrtc (fd, res->crtcs[i]);
266     if (crtc) {
267       if (crtc_id == crtc->crtc_id) {
268         if (pipe)
269           *pipe = i;
270         return crtc;
271       }
272       drmModeFreeCrtc (crtc);
273     }
274   }
275 
276   return NULL;
277 }
278 
279 static gboolean
connector_is_used(int fd,drmModeRes * res,drmModeConnector * conn)280 connector_is_used (int fd, drmModeRes * res, drmModeConnector * conn)
281 {
282   gboolean result;
283   drmModeCrtc *crtc;
284 
285   result = FALSE;
286   crtc = find_crtc_for_connector (fd, res, conn, NULL);
287   if (crtc) {
288     result = crtc->buffer_id != 0;
289     drmModeFreeCrtc (crtc);
290   }
291 
292   return result;
293 }
294 
295 static drmModeConnector *
find_used_connector_by_type(int fd,drmModeRes * res,int type)296 find_used_connector_by_type (int fd, drmModeRes * res, int type)
297 {
298   int i;
299   drmModeConnector *conn;
300 
301   conn = NULL;
302   for (i = 0; i < res->count_connectors; i++) {
303     conn = drmModeGetConnector (fd, res->connectors[i]);
304     if (conn) {
305       if ((conn->connector_type == type) && connector_is_used (fd, res, conn))
306         return conn;
307       drmModeFreeConnector (conn);
308     }
309   }
310 
311   return NULL;
312 }
313 
314 static drmModeConnector *
find_first_used_connector(int fd,drmModeRes * res)315 find_first_used_connector (int fd, drmModeRes * res)
316 {
317   int i;
318   drmModeConnector *conn;
319 
320   conn = NULL;
321   for (i = 0; i < res->count_connectors; i++) {
322     conn = drmModeGetConnector (fd, res->connectors[i]);
323     if (conn) {
324       if (connector_is_used (fd, res, conn))
325         return conn;
326       drmModeFreeConnector (conn);
327     }
328   }
329 
330   return NULL;
331 }
332 
333 static drmModeConnector *
find_main_monitor(int fd,drmModeRes * res)334 find_main_monitor (int fd, drmModeRes * res)
335 {
336   /* Find the LVDS and eDP connectors: those are the main screens. */
337   static const int priority[] = { DRM_MODE_CONNECTOR_LVDS,
338     DRM_MODE_CONNECTOR_eDP
339   };
340   int i;
341   drmModeConnector *conn;
342 
343   conn = NULL;
344   for (i = 0; !conn && i < G_N_ELEMENTS (priority); i++)
345     conn = find_used_connector_by_type (fd, res, priority[i]);
346 
347   /* if we didn't find a connector, grab the first one in use */
348   if (!conn)
349     conn = find_first_used_connector (fd, res);
350 
351   /* if no connector is used, grab the first one */
352   if (!conn)
353     conn = drmModeGetConnector (fd, res->connectors[0]);
354 
355   return conn;
356 }
357 
358 static void
log_drm_version(GstKMSSink * self)359 log_drm_version (GstKMSSink * self)
360 {
361 #ifndef GST_DISABLE_GST_DEBUG
362   drmVersion *v;
363 
364   v = drmGetVersion (self->fd);
365   if (v) {
366     GST_INFO_OBJECT (self, "DRM v%d.%d.%d [%s — %s — %s]", v->version_major,
367         v->version_minor, v->version_patchlevel, GST_STR_NULL (v->name),
368         GST_STR_NULL (v->desc), GST_STR_NULL (v->date));
369     drmFreeVersion (v);
370   } else {
371     GST_WARNING_OBJECT (self, "could not get driver information: %s",
372         GST_STR_NULL (self->devname));
373   }
374 #endif
375   return;
376 }
377 
378 static gboolean
get_drm_caps(GstKMSSink * self)379 get_drm_caps (GstKMSSink * self)
380 {
381   gint ret;
382   guint64 has_dumb_buffer;
383   guint64 has_prime;
384   guint64 has_async_page_flip;
385 
386   has_dumb_buffer = 0;
387   ret = drmGetCap (self->fd, DRM_CAP_DUMB_BUFFER, &has_dumb_buffer);
388   if (ret)
389     GST_WARNING_OBJECT (self, "could not get dumb buffer capability");
390   if (has_dumb_buffer == 0) {
391     GST_ERROR_OBJECT (self, "driver cannot handle dumb buffers");
392     return FALSE;
393   }
394 
395   has_prime = 0;
396   ret = drmGetCap (self->fd, DRM_CAP_PRIME, &has_prime);
397   if (ret)
398     GST_WARNING_OBJECT (self, "could not get prime capability");
399   else {
400     self->has_prime_import = (gboolean) (has_prime & DRM_PRIME_CAP_IMPORT);
401     self->has_prime_export = (gboolean) (has_prime & DRM_PRIME_CAP_EXPORT);
402   }
403 
404   has_async_page_flip = 0;
405   ret = drmGetCap (self->fd, DRM_CAP_ASYNC_PAGE_FLIP, &has_async_page_flip);
406   if (ret)
407     GST_WARNING_OBJECT (self, "could not get async page flip capability");
408   else
409     self->has_async_page_flip = (gboolean) has_async_page_flip;
410 
411   GST_INFO_OBJECT (self,
412       "prime import (%s) / prime export (%s) / async page flip (%s)",
413       self->has_prime_import ? "✓" : "✗",
414       self->has_prime_export ? "✓" : "✗",
415       self->has_async_page_flip ? "✓" : "✗");
416 
417   return TRUE;
418 }
419 
420 static gboolean
configure_mode_setting(GstKMSSink * self,GstVideoInfo * vinfo)421 configure_mode_setting (GstKMSSink * self, GstVideoInfo * vinfo)
422 {
423   gboolean ret;
424   drmModeConnector *conn;
425   int err;
426   gint i;
427   drmModeModeInfo *mode;
428   guint32 fb_id;
429   GstKMSMemory *kmsmem;
430 
431   ret = FALSE;
432   conn = NULL;
433   mode = NULL;
434   kmsmem = NULL;
435 
436   if (self->conn_id < 0)
437     goto bail;
438 
439   GST_INFO_OBJECT (self, "configuring mode setting");
440 
441   kmsmem = (GstKMSMemory *) gst_kms_allocator_bo_alloc (self->allocator, vinfo);
442   if (!kmsmem)
443     goto bo_failed;
444   fb_id = kmsmem->fb_id;
445 
446   conn = drmModeGetConnector (self->fd, self->conn_id);
447   if (!conn)
448     goto connector_failed;
449 
450   for (i = 0; i < conn->count_modes; i++) {
451     if (conn->modes[i].vdisplay == GST_VIDEO_INFO_HEIGHT (vinfo) &&
452         conn->modes[i].hdisplay == GST_VIDEO_INFO_WIDTH (vinfo)) {
453       mode = &conn->modes[i];
454       break;
455     }
456   }
457   if (!mode)
458     goto mode_failed;
459 
460   err = drmModeSetCrtc (self->fd, self->crtc_id, fb_id, 0, 0,
461       (uint32_t *) & self->conn_id, 1, mode);
462   if (err)
463     goto modesetting_failed;
464 
465   g_clear_pointer (&self->tmp_kmsmem, gst_memory_unref);
466   self->tmp_kmsmem = (GstMemory *) kmsmem;
467 
468   ret = TRUE;
469 
470 bail:
471   if (conn)
472     drmModeFreeConnector (conn);
473 
474   return ret;
475 
476   /* ERRORS */
477 bo_failed:
478   {
479     GST_ERROR_OBJECT (self,
480         "failed to allocate buffer object for mode setting");
481     goto bail;
482   }
483 connector_failed:
484   {
485     GST_ERROR_OBJECT (self, "Could not find a valid monitor connector");
486     goto bail;
487   }
488 mode_failed:
489   {
490     GST_ERROR_OBJECT (self, "cannot find appropriate mode");
491     goto bail;
492   }
493 modesetting_failed:
494   {
495     GST_ERROR_OBJECT (self, "Failed to set mode: %s", strerror (errno));
496     goto bail;
497   }
498 }
499 
500 static gboolean
ensure_allowed_caps(GstKMSSink * self,drmModeConnector * conn,drmModePlane * plane,drmModeRes * res)501 ensure_allowed_caps (GstKMSSink * self, drmModeConnector * conn,
502     drmModePlane * plane, drmModeRes * res)
503 {
504   GstCaps *out_caps, *tmp_caps, *caps;
505   int i, j;
506   GstVideoFormat fmt;
507   const gchar *format;
508   drmModeModeInfo *mode;
509   gint count_modes;
510 
511   if (self->allowed_caps)
512     return TRUE;
513 
514   out_caps = gst_caps_new_empty ();
515   if (!out_caps)
516     return FALSE;
517 
518   if (conn && self->modesetting_enabled)
519     count_modes = conn->count_modes;
520   else
521     count_modes = 1;
522 
523   for (i = 0; i < count_modes; i++) {
524     tmp_caps = gst_caps_new_empty ();
525     if (!tmp_caps)
526       return FALSE;
527 
528     mode = NULL;
529     if (conn && self->modesetting_enabled)
530       mode = &conn->modes[i];
531 
532     for (j = 0; j < plane->count_formats; j++) {
533       fmt = gst_video_format_from_drm (plane->formats[j]);
534       if (fmt == GST_VIDEO_FORMAT_UNKNOWN) {
535         GST_INFO_OBJECT (self, "ignoring format %" GST_FOURCC_FORMAT,
536             GST_FOURCC_ARGS (plane->formats[j]));
537         continue;
538       }
539 
540       format = gst_video_format_to_string (fmt);
541 
542       if (mode) {
543         caps = gst_caps_new_simple ("video/x-raw",
544             "format", G_TYPE_STRING, format,
545             "width", G_TYPE_INT, mode->hdisplay,
546             "height", G_TYPE_INT, mode->vdisplay,
547             "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
548       } else {
549         caps = gst_caps_new_simple ("video/x-raw",
550             "format", G_TYPE_STRING, format,
551             "width", GST_TYPE_INT_RANGE, res->min_width, res->max_width,
552             "height", GST_TYPE_INT_RANGE, res->min_height, res->max_height,
553             "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
554       }
555       if (!caps)
556         continue;
557 
558       tmp_caps = gst_caps_merge (tmp_caps, caps);
559     }
560 
561     out_caps = gst_caps_merge (out_caps, gst_caps_simplify (tmp_caps));
562   }
563 
564   self->allowed_caps = gst_caps_simplify (out_caps);
565 
566   GST_DEBUG_OBJECT (self, "allowed caps = %" GST_PTR_FORMAT,
567       self->allowed_caps);
568 
569   return (self->allowed_caps && !gst_caps_is_empty (self->allowed_caps));
570 }
571 
572 static gboolean
set_drm_property(gint fd,guint32 object,guint32 object_type,drmModeObjectPropertiesPtr properties,const gchar * prop_name,guint64 value)573 set_drm_property (gint fd, guint32 object, guint32 object_type,
574     drmModeObjectPropertiesPtr properties, const gchar * prop_name,
575     guint64 value)
576 {
577   guint i;
578   gboolean ret = FALSE;
579 
580   for (i = 0; i < properties->count_props && !ret; i++) {
581     drmModePropertyPtr property;
582 
583     property = drmModeGetProperty (fd, properties->props[i]);
584 
585     /* GstStructure parser limits the set of supported character, so we
586      * replace the invalid characters with '-'. In DRM, this is generally
587      * replacing spaces into '-'. */
588     g_strcanon (property->name, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "_",
589         '-');
590 
591     GST_LOG ("found property %s (looking for %s)", property->name, prop_name);
592 
593     if (!strcmp (property->name, prop_name)) {
594       drmModeObjectSetProperty (fd, object, object_type,
595           property->prop_id, value);
596       ret = TRUE;
597     }
598     drmModeFreeProperty (property);
599   }
600 
601   return ret;
602 }
603 
604 typedef struct
605 {
606   GstKMSSink *self;
607   drmModeObjectPropertiesPtr properties;
608   guint obj_id;
609   guint obj_type;
610   const gchar *obj_type_str;
611 } SetPropsIter;
612 
613 static gboolean
set_obj_prop(GQuark field_id,const GValue * value,gpointer user_data)614 set_obj_prop (GQuark field_id, const GValue * value, gpointer user_data)
615 {
616   SetPropsIter *iter = user_data;
617   GstKMSSink *self = iter->self;
618   const gchar *name;
619   guint64 v;
620 
621   name = g_quark_to_string (field_id);
622 
623   if (G_VALUE_HOLDS (value, G_TYPE_INT))
624     v = g_value_get_int (value);
625   else if (G_VALUE_HOLDS (value, G_TYPE_UINT))
626     v = g_value_get_uint (value);
627   else if (G_VALUE_HOLDS (value, G_TYPE_INT64))
628     v = g_value_get_int64 (value);
629   else if (G_VALUE_HOLDS (value, G_TYPE_UINT64))
630     v = g_value_get_uint64 (value);
631   else {
632     GST_WARNING_OBJECT (self,
633         "'uint64' value expected for control '%s'.", name);
634     return TRUE;
635   }
636 
637   if (set_drm_property (self->fd, iter->obj_id, iter->obj_type,
638           iter->properties, name, v)) {
639     GST_DEBUG_OBJECT (self,
640         "Set %s property '%s' to %" G_GUINT64_FORMAT,
641         iter->obj_type_str, name, v);
642   } else {
643     GST_WARNING_OBJECT (self,
644         "Failed to set %s property '%s' to %" G_GUINT64_FORMAT,
645         iter->obj_type_str, name, v);
646   }
647 
648   return TRUE;
649 }
650 
651 static void
gst_kms_sink_update_properties(SetPropsIter * iter,GstStructure * props)652 gst_kms_sink_update_properties (SetPropsIter * iter, GstStructure * props)
653 {
654   GstKMSSink *self = iter->self;
655 
656   iter->properties = drmModeObjectGetProperties (self->fd, iter->obj_id,
657       iter->obj_type);
658 
659   gst_structure_foreach (props, set_obj_prop, iter);
660 
661   drmModeFreeObjectProperties (iter->properties);
662 }
663 
664 static void
gst_kms_sink_update_connector_properties(GstKMSSink * self)665 gst_kms_sink_update_connector_properties (GstKMSSink * self)
666 {
667   SetPropsIter iter;
668 
669   if (!self->connector_props)
670     return;
671 
672   iter.self = self;
673   iter.obj_id = self->conn_id;
674   iter.obj_type = DRM_MODE_OBJECT_CONNECTOR;
675   iter.obj_type_str = "connector";
676 
677   gst_kms_sink_update_properties (&iter, self->connector_props);
678 }
679 
680 static void
gst_kms_sink_update_plane_properties(GstKMSSink * self)681 gst_kms_sink_update_plane_properties (GstKMSSink * self)
682 {
683   SetPropsIter iter;
684 
685   if (!self->plane_props)
686     return;
687 
688   iter.self = self;
689   iter.obj_id = self->plane_id;
690   iter.obj_type = DRM_MODE_OBJECT_PLANE;
691   iter.obj_type_str = "plane";
692 
693   gst_kms_sink_update_properties (&iter, self->plane_props);
694 }
695 
696 static gboolean
gst_kms_sink_start(GstBaseSink * bsink)697 gst_kms_sink_start (GstBaseSink * bsink)
698 {
699   GstKMSSink *self;
700   drmModeRes *res;
701   drmModeConnector *conn;
702   drmModeCrtc *crtc;
703   drmModePlaneRes *pres;
704   drmModePlane *plane;
705   gboolean universal_planes;
706   gboolean ret;
707 
708   self = GST_KMS_SINK (bsink);
709   universal_planes = FALSE;
710   ret = FALSE;
711   res = NULL;
712   conn = NULL;
713   crtc = NULL;
714   pres = NULL;
715   plane = NULL;
716 
717   if (self->devname || self->bus_id)
718     self->fd = drmOpen (self->devname, self->bus_id);
719   else
720     self->fd = kms_open (&self->devname);
721   if (self->fd < 0)
722     goto open_failed;
723 
724   log_drm_version (self);
725   if (!get_drm_caps (self))
726     goto bail;
727 
728   res = drmModeGetResources (self->fd);
729   if (!res)
730     goto resources_failed;
731 
732   if (self->conn_id == -1)
733     conn = find_main_monitor (self->fd, res);
734   else
735     conn = drmModeGetConnector (self->fd, self->conn_id);
736   if (!conn)
737     goto connector_failed;
738 
739   crtc = find_crtc_for_connector (self->fd, res, conn, &self->pipe);
740   if (!crtc)
741     goto crtc_failed;
742 
743   if (!crtc->mode_valid || self->modesetting_enabled) {
744     GST_DEBUG_OBJECT (self, "enabling modesetting");
745     self->modesetting_enabled = TRUE;
746     universal_planes = TRUE;
747   }
748 
749   if (crtc->mode_valid && self->modesetting_enabled && self->restore_crtc) {
750     self->saved_crtc = (drmModeCrtc *) crtc;
751   }
752 
753 retry_find_plane:
754   if (universal_planes &&
755       drmSetClientCap (self->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1))
756     goto set_cap_failed;
757 
758   pres = drmModeGetPlaneResources (self->fd);
759   if (!pres)
760     goto plane_resources_failed;
761 
762   if (self->plane_id == -1)
763     plane = find_plane_for_crtc (self->fd, res, pres, crtc->crtc_id);
764   else
765     plane = drmModeGetPlane (self->fd, self->plane_id);
766   if (!plane)
767     goto plane_failed;
768 
769   if (!ensure_allowed_caps (self, conn, plane, res))
770     goto allowed_caps_failed;
771 
772   self->conn_id = conn->connector_id;
773   self->crtc_id = crtc->crtc_id;
774   self->plane_id = plane->plane_id;
775 
776   GST_INFO_OBJECT (self, "connector id = %d / crtc id = %d / plane id = %d",
777       self->conn_id, self->crtc_id, self->plane_id);
778 
779   GST_OBJECT_LOCK (self);
780   self->hdisplay = crtc->mode.hdisplay;
781   self->vdisplay = crtc->mode.vdisplay;
782 
783   if (self->render_rect.w == 0 || self->render_rect.h == 0) {
784     self->render_rect.x = 0;
785     self->render_rect.y = 0;
786     self->render_rect.w = self->hdisplay;
787     self->render_rect.h = self->vdisplay;
788   }
789 
790   self->pending_rect = self->render_rect;
791   GST_OBJECT_UNLOCK (self);
792 
793   self->buffer_id = crtc->buffer_id;
794 
795   self->mm_width = conn->mmWidth;
796   self->mm_height = conn->mmHeight;
797 
798   GST_INFO_OBJECT (self, "display size: pixels = %dx%d / millimeters = %dx%d",
799       self->hdisplay, self->vdisplay, self->mm_width, self->mm_height);
800 
801   self->pollfd.fd = self->fd;
802   gst_poll_add_fd (self->poll, &self->pollfd);
803   gst_poll_fd_ctl_read (self->poll, &self->pollfd, TRUE);
804 
805   g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_WIDTH]);
806   g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_HEIGHT]);
807 
808   gst_kms_sink_update_connector_properties (self);
809   gst_kms_sink_update_plane_properties (self);
810 
811   ret = TRUE;
812 
813 bail:
814   if (plane)
815     drmModeFreePlane (plane);
816   if (pres)
817     drmModeFreePlaneResources (pres);
818   if (crtc != self->saved_crtc)
819     drmModeFreeCrtc (crtc);
820   if (conn)
821     drmModeFreeConnector (conn);
822   if (res)
823     drmModeFreeResources (res);
824 
825   if (!ret && self->fd >= 0) {
826     drmClose (self->fd);
827     self->fd = -1;
828   }
829 
830   return ret;
831 
832   /* ERRORS */
833 open_failed:
834   {
835     GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE,
836         ("Could not open DRM module %s", GST_STR_NULL (self->devname)),
837         ("reason: %s (%d)", strerror (errno), errno));
838     return FALSE;
839   }
840 
841 resources_failed:
842   {
843     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
844         ("drmModeGetResources failed"),
845         ("reason: %s (%d)", strerror (errno), errno));
846     goto bail;
847   }
848 
849 connector_failed:
850   {
851     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
852         ("Could not find a valid monitor connector"), (NULL));
853     goto bail;
854   }
855 
856 crtc_failed:
857   {
858     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
859         ("Could not find a crtc for connector"), (NULL));
860     goto bail;
861   }
862 
863 set_cap_failed:
864   {
865     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
866         ("Could not set universal planes capability bit"), (NULL));
867     goto bail;
868   }
869 
870 plane_resources_failed:
871   {
872     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
873         ("drmModeGetPlaneResources failed"),
874         ("reason: %s (%d)", strerror (errno), errno));
875     goto bail;
876   }
877 
878 plane_failed:
879   {
880     if (universal_planes) {
881       GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
882           ("Could not find a plane for crtc"), (NULL));
883       goto bail;
884     } else {
885       universal_planes = TRUE;
886       goto retry_find_plane;
887     }
888   }
889 
890 allowed_caps_failed:
891   {
892     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
893         ("Could not get allowed GstCaps of device"),
894         ("driver does not provide mode settings configuration"));
895     goto bail;
896   }
897 }
898 
899 static gboolean
gst_kms_sink_stop(GstBaseSink * bsink)900 gst_kms_sink_stop (GstBaseSink * bsink)
901 {
902   GstKMSSink *self;
903   int err;
904 
905   self = GST_KMS_SINK (bsink);
906 
907   if (self->allocator)
908     gst_kms_allocator_clear_cache (self->allocator);
909 
910   gst_buffer_replace (&self->last_buffer, NULL);
911   gst_caps_replace (&self->allowed_caps, NULL);
912   gst_object_replace ((GstObject **) & self->pool, NULL);
913   gst_object_replace ((GstObject **) & self->allocator, NULL);
914 
915   gst_poll_remove_fd (self->poll, &self->pollfd);
916   gst_poll_restart (self->poll);
917   gst_poll_fd_init (&self->pollfd);
918 
919   if (self->saved_crtc) {
920     drmModeCrtc *crtc = (drmModeCrtc *) self->saved_crtc;
921 
922     err = drmModeSetCrtc (self->fd, crtc->crtc_id, crtc->buffer_id, crtc->x,
923         crtc->y, (uint32_t *) & self->conn_id, 1, &crtc->mode);
924     if (err)
925       GST_ERROR_OBJECT (self, "Failed to restore previous CRTC mode: %s",
926           g_strerror (errno));
927 
928     drmModeFreeCrtc (crtc);
929     self->saved_crtc = NULL;
930   }
931 
932   if (self->fd >= 0) {
933     drmClose (self->fd);
934     self->fd = -1;
935   }
936 
937   GST_OBJECT_LOCK (bsink);
938   self->hdisplay = 0;
939   self->vdisplay = 0;
940   self->pending_rect.x = 0;
941   self->pending_rect.y = 0;
942   self->pending_rect.w = 0;
943   self->pending_rect.h = 0;
944   self->render_rect = self->pending_rect;
945   GST_OBJECT_UNLOCK (bsink);
946 
947   g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_WIDTH]);
948   g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_HEIGHT]);
949 
950   return TRUE;
951 }
952 
953 static GstCaps *
gst_kms_sink_get_allowed_caps(GstKMSSink * self)954 gst_kms_sink_get_allowed_caps (GstKMSSink * self)
955 {
956   if (!self->allowed_caps)
957     return NULL;                /* base class will return the template caps */
958   return gst_caps_ref (self->allowed_caps);
959 }
960 
961 static GstCaps *
gst_kms_sink_get_caps(GstBaseSink * bsink,GstCaps * filter)962 gst_kms_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
963 {
964   GstKMSSink *self;
965   GstCaps *caps, *out_caps;
966   GstStructure *s;
967   guint dpy_par_n, dpy_par_d;
968 
969   self = GST_KMS_SINK (bsink);
970 
971   caps = gst_kms_sink_get_allowed_caps (self);
972   if (!caps)
973     return NULL;
974 
975   GST_OBJECT_LOCK (self);
976 
977   if (!self->can_scale) {
978     out_caps = gst_caps_new_empty ();
979     gst_video_calculate_device_ratio (self->hdisplay, self->vdisplay,
980         self->mm_width, self->mm_height, &dpy_par_n, &dpy_par_d);
981 
982     s = gst_structure_copy (gst_caps_get_structure (caps, 0));
983     gst_structure_set (s, "width", G_TYPE_INT, self->pending_rect.w,
984         "height", G_TYPE_INT, self->pending_rect.h,
985         "pixel-aspect-ratio", GST_TYPE_FRACTION, dpy_par_n, dpy_par_d, NULL);
986 
987     gst_caps_append_structure (out_caps, s);
988 
989     out_caps = gst_caps_merge (out_caps, caps);
990     caps = NULL;
991 
992     /* enforce our display aspect ratio */
993     gst_caps_set_simple (out_caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
994         dpy_par_n, dpy_par_d, NULL);
995   } else {
996     out_caps = gst_caps_make_writable (caps);
997     caps = NULL;
998   }
999 
1000   GST_OBJECT_UNLOCK (self);
1001 
1002   GST_DEBUG_OBJECT (self, "Proposing caps %" GST_PTR_FORMAT, out_caps);
1003 
1004   if (filter) {
1005     caps = out_caps;
1006     out_caps = gst_caps_intersect_full (caps, filter, GST_CAPS_INTERSECT_FIRST);
1007     gst_caps_unref (caps);
1008   }
1009 
1010   return out_caps;
1011 }
1012 
1013 static void
ensure_kms_allocator(GstKMSSink * self)1014 ensure_kms_allocator (GstKMSSink * self)
1015 {
1016   if (self->allocator)
1017     return;
1018   self->allocator = gst_kms_allocator_new (self->fd);
1019 }
1020 
1021 static GstBufferPool *
gst_kms_sink_create_pool(GstKMSSink * self,GstCaps * caps,gsize size,gint min)1022 gst_kms_sink_create_pool (GstKMSSink * self, GstCaps * caps, gsize size,
1023     gint min)
1024 {
1025   GstBufferPool *pool;
1026   GstStructure *config;
1027 
1028   pool = gst_kms_buffer_pool_new ();
1029   if (!pool)
1030     goto pool_failed;
1031 
1032   config = gst_buffer_pool_get_config (pool);
1033   gst_buffer_pool_config_set_params (config, caps, size, min, 0);
1034   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
1035 
1036   ensure_kms_allocator (self);
1037   gst_buffer_pool_config_set_allocator (config, self->allocator, NULL);
1038 
1039   if (!gst_buffer_pool_set_config (pool, config))
1040     goto config_failed;
1041 
1042   return pool;
1043 
1044   /* ERRORS */
1045 pool_failed:
1046   {
1047     GST_ERROR_OBJECT (self, "failed to create buffer pool");
1048     return NULL;
1049   }
1050 config_failed:
1051   {
1052     GST_ERROR_OBJECT (self, "failed to set config");
1053     gst_object_unref (pool);
1054     return NULL;
1055   }
1056 }
1057 
1058 static gboolean
gst_kms_sink_calculate_display_ratio(GstKMSSink * self,GstVideoInfo * vinfo)1059 gst_kms_sink_calculate_display_ratio (GstKMSSink * self, GstVideoInfo * vinfo)
1060 {
1061   guint dar_n, dar_d;
1062   guint video_width, video_height;
1063   guint video_par_n, video_par_d;
1064   guint dpy_par_n, dpy_par_d;
1065 
1066   video_width = GST_VIDEO_INFO_WIDTH (vinfo);
1067   video_height = GST_VIDEO_INFO_HEIGHT (vinfo);
1068   video_par_n = GST_VIDEO_INFO_PAR_N (vinfo);
1069   video_par_d = GST_VIDEO_INFO_PAR_D (vinfo);
1070 
1071   if (self->can_scale) {
1072     gst_video_calculate_device_ratio (self->hdisplay, self->vdisplay,
1073         self->mm_width, self->mm_height, &dpy_par_n, &dpy_par_d);
1074   } else {
1075     GST_VIDEO_SINK_WIDTH (self) = video_width;
1076     GST_VIDEO_SINK_HEIGHT (self) = video_height;
1077     goto out;
1078   }
1079 
1080   if (!gst_video_calculate_display_ratio (&dar_n, &dar_d, video_width,
1081           video_height, video_par_n, video_par_d, dpy_par_n, dpy_par_d))
1082     return FALSE;
1083 
1084   GST_DEBUG_OBJECT (self, "video calculated display ratio: %d/%d", dar_n,
1085       dar_d);
1086 
1087   /* now find a width x height that respects this display ratio.
1088    * prefer those that have one of w/h the same as the incoming video
1089    * using wd / hd = dar_n / dar_d */
1090 
1091   /* start with same height, because of interlaced video */
1092   /* check hd / dar_d is an integer scale factor, and scale wd with the PAR */
1093   if (video_height % dar_d == 0) {
1094     GST_DEBUG_OBJECT (self, "keeping video height");
1095     GST_VIDEO_SINK_WIDTH (self) = (guint)
1096         gst_util_uint64_scale_int (video_height, dar_n, dar_d);
1097     GST_VIDEO_SINK_HEIGHT (self) = video_height;
1098   } else if (video_width % dar_n == 0) {
1099     GST_DEBUG_OBJECT (self, "keeping video width");
1100     GST_VIDEO_SINK_WIDTH (self) = video_width;
1101     GST_VIDEO_SINK_HEIGHT (self) = (guint)
1102         gst_util_uint64_scale_int (video_width, dar_d, dar_n);
1103   } else {
1104     GST_DEBUG_OBJECT (self, "approximating while keeping video height");
1105     GST_VIDEO_SINK_WIDTH (self) = (guint)
1106         gst_util_uint64_scale_int (video_height, dar_n, dar_d);
1107     GST_VIDEO_SINK_HEIGHT (self) = video_height;
1108   }
1109 
1110 out:
1111   GST_DEBUG_OBJECT (self, "scaling to %dx%d", GST_VIDEO_SINK_WIDTH (self),
1112       GST_VIDEO_SINK_HEIGHT (self));
1113 
1114   return TRUE;
1115 }
1116 
1117 static gboolean
gst_kms_sink_set_caps(GstBaseSink * bsink,GstCaps * caps)1118 gst_kms_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
1119 {
1120   GstKMSSink *self;
1121   GstVideoInfo vinfo;
1122   GstBufferPool *newpool, *oldpool;
1123 
1124   self = GST_KMS_SINK (bsink);
1125 
1126   /* We are going to change the internal buffer pool, which means it will no
1127    * longer be compatbile with the last_buffer size. Drain now, as we won't be
1128    * able to do that later on. */
1129   gst_kms_sink_drain (self);
1130 
1131   if (!gst_video_info_from_caps (&vinfo, caps))
1132     goto invalid_format;
1133 
1134   if (!gst_kms_sink_calculate_display_ratio (self, &vinfo))
1135     goto no_disp_ratio;
1136 
1137   if (GST_VIDEO_SINK_WIDTH (self) <= 0 || GST_VIDEO_SINK_HEIGHT (self) <= 0)
1138     goto invalid_size;
1139 
1140   /* create a new pool for the new configuration */
1141   newpool = gst_kms_sink_create_pool (self, caps, GST_VIDEO_INFO_SIZE (&vinfo),
1142       2);
1143   if (!newpool)
1144     goto no_pool;
1145 
1146   /* we don't activate the internal pool yet as it may not be needed */
1147   oldpool = self->pool;
1148   self->pool = newpool;
1149 
1150   if (oldpool) {
1151     gst_buffer_pool_set_active (oldpool, FALSE);
1152     gst_object_unref (oldpool);
1153   }
1154 
1155   if (self->modesetting_enabled && !configure_mode_setting (self, &vinfo))
1156     goto modesetting_failed;
1157 
1158   self->vinfo = vinfo;
1159 
1160   GST_OBJECT_LOCK (self);
1161   if (self->reconfigure) {
1162     self->reconfigure = FALSE;
1163     self->render_rect = self->pending_rect;
1164   }
1165   GST_OBJECT_UNLOCK (self);
1166 
1167   GST_DEBUG_OBJECT (self, "negotiated caps = %" GST_PTR_FORMAT, caps);
1168 
1169   return TRUE;
1170 
1171   /* ERRORS */
1172 invalid_format:
1173   {
1174     GST_ERROR_OBJECT (self, "caps invalid");
1175     return FALSE;
1176   }
1177 
1178 invalid_size:
1179   {
1180     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1181         ("Invalid image size."));
1182     return FALSE;
1183   }
1184 
1185 no_disp_ratio:
1186   {
1187     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1188         ("Error calculating the output display ratio of the video."));
1189     return FALSE;
1190   }
1191 no_pool:
1192   {
1193     /* Already warned in create_pool */
1194     return FALSE;
1195   }
1196 
1197 modesetting_failed:
1198   {
1199     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1200         ("failed to configure video mode"));
1201     return FALSE;
1202   }
1203 
1204 }
1205 
1206 static gboolean
gst_kms_sink_propose_allocation(GstBaseSink * bsink,GstQuery * query)1207 gst_kms_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1208 {
1209   GstKMSSink *self;
1210   GstCaps *caps;
1211   gboolean need_pool;
1212   GstVideoInfo vinfo;
1213   GstBufferPool *pool;
1214   gsize size;
1215 
1216   self = GST_KMS_SINK (bsink);
1217 
1218   gst_query_parse_allocation (query, &caps, &need_pool);
1219   if (!caps)
1220     goto no_caps;
1221   if (!gst_video_info_from_caps (&vinfo, caps))
1222     goto invalid_caps;
1223 
1224   size = GST_VIDEO_INFO_SIZE (&vinfo);
1225 
1226   pool = NULL;
1227   if (need_pool) {
1228     pool = gst_kms_sink_create_pool (self, caps, size, 0);
1229     if (!pool)
1230       goto no_pool;
1231 
1232     /* Only export for pool used upstream */
1233     if (self->has_prime_export) {
1234       GstStructure *config = gst_buffer_pool_get_config (pool);
1235       gst_buffer_pool_config_add_option (config,
1236           GST_BUFFER_POOL_OPTION_KMS_PRIME_EXPORT);
1237       gst_buffer_pool_set_config (pool, config);
1238     }
1239   }
1240 
1241   /* we need at least 2 buffer because we hold on to the last one */
1242   gst_query_add_allocation_pool (query, pool, size, 2, 0);
1243   if (pool)
1244     gst_object_unref (pool);
1245 
1246   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1247   gst_query_add_allocation_meta (query, GST_VIDEO_CROP_META_API_TYPE, NULL);
1248 
1249   return TRUE;
1250 
1251   /* ERRORS */
1252 no_caps:
1253   {
1254     GST_DEBUG_OBJECT (bsink, "no caps specified");
1255     return FALSE;
1256   }
1257 invalid_caps:
1258   {
1259     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
1260     return FALSE;
1261   }
1262 no_pool:
1263   {
1264     /* Already warned in create_pool */
1265     return FALSE;
1266   }
1267 }
1268 
1269 static void
sync_handler(gint fd,guint frame,guint sec,guint usec,gpointer data)1270 sync_handler (gint fd, guint frame, guint sec, guint usec, gpointer data)
1271 {
1272   gboolean *waiting;
1273 
1274   waiting = data;
1275   *waiting = FALSE;
1276 }
1277 
1278 static gboolean
gst_kms_sink_sync(GstKMSSink * self)1279 gst_kms_sink_sync (GstKMSSink * self)
1280 {
1281   gint ret;
1282   gboolean waiting;
1283   drmEventContext evctxt = {
1284     .version = DRM_EVENT_CONTEXT_VERSION,
1285     .page_flip_handler = sync_handler,
1286     .vblank_handler = sync_handler,
1287   };
1288   drmVBlank vbl = {
1289     .request = {
1290           .type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1291           .sequence = 1,
1292           .signal = (gulong) & waiting,
1293         },
1294   };
1295 
1296   if (self->pipe == 1)
1297     vbl.request.type |= DRM_VBLANK_SECONDARY;
1298   else if (self->pipe > 1)
1299     vbl.request.type |= self->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT;
1300 
1301   waiting = TRUE;
1302   if (!self->has_async_page_flip && !self->modesetting_enabled) {
1303     ret = drmWaitVBlank (self->fd, &vbl);
1304     if (ret)
1305       goto vblank_failed;
1306   } else {
1307     ret = drmModePageFlip (self->fd, self->crtc_id, self->buffer_id,
1308         DRM_MODE_PAGE_FLIP_EVENT, &waiting);
1309     if (ret)
1310       goto pageflip_failed;
1311   }
1312 
1313   while (waiting) {
1314     do {
1315       ret = gst_poll_wait (self->poll, 3 * GST_SECOND);
1316     } while (ret == -1 && (errno == EAGAIN || errno == EINTR));
1317 
1318     ret = drmHandleEvent (self->fd, &evctxt);
1319     if (ret)
1320       goto event_failed;
1321   }
1322 
1323   return TRUE;
1324 
1325   /* ERRORS */
1326 vblank_failed:
1327   {
1328     GST_WARNING_OBJECT (self, "drmWaitVBlank failed: %s (%d)", strerror (-ret),
1329         ret);
1330     return FALSE;
1331   }
1332 pageflip_failed:
1333   {
1334     GST_WARNING_OBJECT (self, "drmModePageFlip failed: %s (%d)",
1335         strerror (-ret), ret);
1336     return FALSE;
1337   }
1338 event_failed:
1339   {
1340     GST_ERROR_OBJECT (self, "drmHandleEvent failed: %s (%d)", strerror (-ret),
1341         ret);
1342     return FALSE;
1343   }
1344 }
1345 
1346 static gboolean
gst_kms_sink_import_dmabuf(GstKMSSink * self,GstBuffer * inbuf,GstBuffer ** outbuf)1347 gst_kms_sink_import_dmabuf (GstKMSSink * self, GstBuffer * inbuf,
1348     GstBuffer ** outbuf)
1349 {
1350   gint prime_fds[GST_VIDEO_MAX_PLANES] = { 0, };
1351   GstVideoMeta *meta;
1352   guint i, n_mem, n_planes;
1353   GstKMSMemory *kmsmem;
1354   guint mems_idx[GST_VIDEO_MAX_PLANES];
1355   gsize mems_skip[GST_VIDEO_MAX_PLANES];
1356   GstMemory *mems[GST_VIDEO_MAX_PLANES];
1357 
1358   if (!self->has_prime_import)
1359     return FALSE;
1360 
1361   /* This will eliminate most non-dmabuf out there */
1362   if (!gst_is_dmabuf_memory (gst_buffer_peek_memory (inbuf, 0)))
1363     return FALSE;
1364 
1365   n_planes = GST_VIDEO_INFO_N_PLANES (&self->vinfo);
1366   n_mem = gst_buffer_n_memory (inbuf);
1367   meta = gst_buffer_get_video_meta (inbuf);
1368 
1369   GST_TRACE_OBJECT (self, "Found a dmabuf with %u planes and %u memories",
1370       n_planes, n_mem);
1371 
1372   /* We cannot have multiple dmabuf per plane */
1373   if (n_mem > n_planes)
1374     return FALSE;
1375   g_assert (n_planes != 0);
1376 
1377   /* Update video info based on video meta */
1378   if (meta) {
1379     GST_VIDEO_INFO_WIDTH (&self->vinfo) = meta->width;
1380     GST_VIDEO_INFO_HEIGHT (&self->vinfo) = meta->height;
1381 
1382     for (i = 0; i < meta->n_planes; i++) {
1383       GST_VIDEO_INFO_PLANE_OFFSET (&self->vinfo, i) = meta->offset[i];
1384       GST_VIDEO_INFO_PLANE_STRIDE (&self->vinfo, i) = meta->stride[i];
1385     }
1386   }
1387 
1388   /* Find and validate all memories */
1389   for (i = 0; i < n_planes; i++) {
1390     guint length;
1391 
1392     if (!gst_buffer_find_memory (inbuf,
1393             GST_VIDEO_INFO_PLANE_OFFSET (&self->vinfo, i), 1,
1394             &mems_idx[i], &length, &mems_skip[i]))
1395       return FALSE;
1396 
1397     mems[i] = gst_buffer_peek_memory (inbuf, mems_idx[i]);
1398 
1399     /* adjust for memory offset, in case data does not
1400      * start from byte 0 in the dmabuf fd */
1401     mems_skip[i] += mems[i]->offset;
1402 
1403     /* And all memory found must be dmabuf */
1404     if (!gst_is_dmabuf_memory (mems[i]))
1405       return FALSE;
1406   }
1407 
1408   kmsmem = (GstKMSMemory *) gst_kms_allocator_get_cached (mems[0]);
1409   if (kmsmem) {
1410     GST_LOG_OBJECT (self, "found KMS mem %p in DMABuf mem %p with fb id = %d",
1411         kmsmem, mems[0], kmsmem->fb_id);
1412     goto wrap_mem;
1413   }
1414 
1415   for (i = 0; i < n_planes; i++)
1416     prime_fds[i] = gst_dmabuf_memory_get_fd (mems[i]);
1417 
1418   GST_LOG_OBJECT (self, "found these prime ids: %d, %d, %d, %d", prime_fds[0],
1419       prime_fds[1], prime_fds[2], prime_fds[3]);
1420 
1421   kmsmem = gst_kms_allocator_dmabuf_import (self->allocator,
1422       prime_fds, n_planes, mems_skip, &self->vinfo);
1423   if (!kmsmem)
1424     return FALSE;
1425 
1426   GST_LOG_OBJECT (self, "setting KMS mem %p to DMABuf mem %p with fb id = %d",
1427       kmsmem, mems[0], kmsmem->fb_id);
1428   gst_kms_allocator_cache (self->allocator, mems[0], GST_MEMORY_CAST (kmsmem));
1429 
1430 wrap_mem:
1431   *outbuf = gst_buffer_new ();
1432   if (!*outbuf)
1433     return FALSE;
1434   gst_buffer_append_memory (*outbuf, gst_memory_ref (GST_MEMORY_CAST (kmsmem)));
1435   gst_buffer_add_parent_buffer_meta (*outbuf, inbuf);
1436 
1437   return TRUE;
1438 }
1439 
1440 static GstBuffer *
gst_kms_sink_copy_to_dumb_buffer(GstKMSSink * self,GstBuffer * inbuf)1441 gst_kms_sink_copy_to_dumb_buffer (GstKMSSink * self, GstBuffer * inbuf)
1442 {
1443   GstFlowReturn ret;
1444   GstVideoFrame inframe, outframe;
1445   gboolean success;
1446   GstBuffer *buf = NULL;
1447 
1448   if (!gst_buffer_pool_set_active (self->pool, TRUE))
1449     goto activate_pool_failed;
1450 
1451   ret = gst_buffer_pool_acquire_buffer (self->pool, &buf, NULL);
1452   if (ret != GST_FLOW_OK)
1453     goto create_buffer_failed;
1454 
1455   if (!gst_video_frame_map (&inframe, &self->vinfo, inbuf, GST_MAP_READ))
1456     goto error_map_src_buffer;
1457 
1458   if (!gst_video_frame_map (&outframe, &self->vinfo, buf, GST_MAP_WRITE))
1459     goto error_map_dst_buffer;
1460 
1461   success = gst_video_frame_copy (&outframe, &inframe);
1462   gst_video_frame_unmap (&outframe);
1463   gst_video_frame_unmap (&inframe);
1464   if (!success)
1465     goto error_copy_buffer;
1466 
1467   return buf;
1468 
1469 bail:
1470   {
1471     if (buf)
1472       gst_buffer_unref (buf);
1473     return NULL;
1474   }
1475 
1476   /* ERRORS */
1477 activate_pool_failed:
1478   {
1479     GST_ELEMENT_ERROR (self, STREAM, FAILED, ("failed to activate buffer pool"),
1480         ("failed to activate buffer pool"));
1481     return NULL;
1482   }
1483 create_buffer_failed:
1484   {
1485     GST_ELEMENT_ERROR (self, STREAM, FAILED, ("allocation failed"),
1486         ("failed to create buffer"));
1487     return NULL;
1488   }
1489 error_copy_buffer:
1490   {
1491     GST_WARNING_OBJECT (self, "failed to upload buffer");
1492     goto bail;
1493   }
1494 error_map_dst_buffer:
1495   {
1496     gst_video_frame_unmap (&inframe);
1497     /* fall-through */
1498   }
1499 error_map_src_buffer:
1500   {
1501     GST_WARNING_OBJECT (self, "failed to map buffer");
1502     goto bail;
1503   }
1504 }
1505 
1506 static GstBuffer *
gst_kms_sink_get_input_buffer(GstKMSSink * self,GstBuffer * inbuf)1507 gst_kms_sink_get_input_buffer (GstKMSSink * self, GstBuffer * inbuf)
1508 {
1509   GstMemory *mem;
1510   GstBuffer *buf = NULL;
1511 
1512   mem = gst_buffer_peek_memory (inbuf, 0);
1513   if (!mem)
1514     return NULL;
1515 
1516   if (gst_is_kms_memory (mem))
1517     return gst_buffer_ref (inbuf);
1518 
1519   if (gst_kms_sink_import_dmabuf (self, inbuf, &buf))
1520     goto done;
1521 
1522   GST_CAT_INFO_OBJECT (CAT_PERFORMANCE, self, "frame copy");
1523   buf = gst_kms_sink_copy_to_dumb_buffer (self, inbuf);
1524 
1525 done:
1526   /* Copy all the non-memory related metas, this way CropMeta will be
1527    * available upon GstVideoOverlay::expose calls. */
1528   if (buf)
1529     gst_buffer_copy_into (buf, inbuf, GST_BUFFER_COPY_METADATA, 0, -1);
1530 
1531   return buf;
1532 }
1533 
1534 static GstFlowReturn
gst_kms_sink_show_frame(GstVideoSink * vsink,GstBuffer * buf)1535 gst_kms_sink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
1536 {
1537   gint ret;
1538   GstBuffer *buffer = NULL;
1539   guint32 fb_id;
1540   GstKMSSink *self;
1541   GstVideoCropMeta *crop;
1542   GstVideoRectangle src = { 0, };
1543   GstVideoRectangle dst = { 0, };
1544   GstVideoRectangle result;
1545   GstFlowReturn res;
1546 
1547   self = GST_KMS_SINK (vsink);
1548 
1549   res = GST_FLOW_ERROR;
1550 
1551   if (buf)
1552     buffer = gst_kms_sink_get_input_buffer (self, buf);
1553   else if (self->last_buffer)
1554     buffer = gst_buffer_ref (self->last_buffer);
1555 
1556   /* Make sure buf is not used accidentally */
1557   buf = NULL;
1558 
1559   if (!buffer)
1560     return GST_FLOW_ERROR;
1561   fb_id = gst_kms_memory_get_fb_id (gst_buffer_peek_memory (buffer, 0));
1562   if (fb_id == 0)
1563     goto buffer_invalid;
1564 
1565   GST_TRACE_OBJECT (self, "displaying fb %d", fb_id);
1566 
1567   GST_OBJECT_LOCK (self);
1568   if (self->modesetting_enabled) {
1569     self->buffer_id = fb_id;
1570     goto sync_frame;
1571   }
1572 
1573   if ((crop = gst_buffer_get_video_crop_meta (buffer))) {
1574     GstVideoInfo vinfo = self->vinfo;
1575     vinfo.width = crop->width;
1576     vinfo.height = crop->height;
1577 
1578     if (!gst_kms_sink_calculate_display_ratio (self, &vinfo))
1579       goto no_disp_ratio;
1580 
1581     src.x = crop->x;
1582     src.y = crop->y;
1583   }
1584 
1585   src.w = GST_VIDEO_SINK_WIDTH (self);
1586   src.h = GST_VIDEO_SINK_HEIGHT (self);
1587 
1588   dst.w = self->render_rect.w;
1589   dst.h = self->render_rect.h;
1590 
1591 retry_set_plane:
1592   gst_video_sink_center_rect (src, dst, &result, self->can_scale);
1593 
1594   result.x += self->render_rect.x;
1595   result.y += self->render_rect.y;
1596 
1597   if (crop) {
1598     src.w = crop->width;
1599     src.h = crop->height;
1600   } else {
1601     src.w = GST_VIDEO_INFO_WIDTH (&self->vinfo);
1602     src.h = GST_VIDEO_INFO_HEIGHT (&self->vinfo);
1603   }
1604 
1605   /* handle out of screen case */
1606   if ((result.x + result.w) > self->hdisplay)
1607     result.w = self->hdisplay - result.x;
1608 
1609   if ((result.y + result.h) > self->vdisplay)
1610     result.h = self->vdisplay - result.y;
1611 
1612   if (result.w <= 0 || result.h <= 0) {
1613     GST_WARNING_OBJECT (self, "video is out of display range");
1614     goto sync_frame;
1615   }
1616 
1617   /* to make sure it can be show when driver don't support scale */
1618   if (!self->can_scale) {
1619     src.w = result.w;
1620     src.h = result.h;
1621   }
1622 
1623   GST_TRACE_OBJECT (self,
1624       "drmModeSetPlane at (%i,%i) %ix%i sourcing at (%i,%i) %ix%i",
1625       result.x, result.y, result.w, result.h, src.x, src.y, src.w, src.h);
1626 
1627   ret = drmModeSetPlane (self->fd, self->plane_id, self->crtc_id, fb_id, 0,
1628       result.x, result.y, result.w, result.h,
1629       /* source/cropping coordinates are given in Q16 */
1630       src.x << 16, src.y << 16, src.w << 16, src.h << 16);
1631   if (ret) {
1632     if (self->can_scale) {
1633       self->can_scale = FALSE;
1634       goto retry_set_plane;
1635     }
1636     goto set_plane_failed;
1637   }
1638 
1639 sync_frame:
1640   /* Wait for the previous frame to complete redraw */
1641   if (!gst_kms_sink_sync (self)) {
1642     GST_OBJECT_UNLOCK (self);
1643     goto bail;
1644   }
1645 
1646   if (buffer != self->last_buffer)
1647     gst_buffer_replace (&self->last_buffer, buffer);
1648   g_clear_pointer (&self->tmp_kmsmem, gst_memory_unref);
1649 
1650   GST_OBJECT_UNLOCK (self);
1651   res = GST_FLOW_OK;
1652 
1653 bail:
1654   gst_buffer_unref (buffer);
1655   return res;
1656 
1657   /* ERRORS */
1658 buffer_invalid:
1659   {
1660     GST_ERROR_OBJECT (self, "invalid buffer: it doesn't have a fb id");
1661     goto bail;
1662   }
1663 set_plane_failed:
1664   {
1665     GST_OBJECT_UNLOCK (self);
1666     GST_DEBUG_OBJECT (self, "result = { %d, %d, %d, %d} / "
1667         "src = { %d, %d, %d %d } / dst = { %d, %d, %d %d }", result.x, result.y,
1668         result.w, result.h, src.x, src.y, src.w, src.h, dst.x, dst.y, dst.w,
1669         dst.h);
1670     GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
1671         (NULL), ("drmModeSetPlane failed: %s (%d)", strerror (-ret), ret));
1672     goto bail;
1673   }
1674 no_disp_ratio:
1675   {
1676     GST_OBJECT_UNLOCK (self);
1677     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1678         ("Error calculating the output display ratio of the video."));
1679     goto bail;
1680   }
1681 }
1682 
1683 static void
gst_kms_sink_drain(GstKMSSink * self)1684 gst_kms_sink_drain (GstKMSSink * self)
1685 {
1686   GstParentBufferMeta *parent_meta;
1687 
1688   GST_DEBUG_OBJECT (self, "draining");
1689 
1690   if (!self->last_buffer)
1691     return;
1692 
1693   /* We only need to return the last_buffer if it depends on upstream buffer.
1694    * In this case, the last_buffer will have a GstParentBufferMeta set. */
1695   parent_meta = gst_buffer_get_parent_buffer_meta (self->last_buffer);
1696   if (parent_meta) {
1697     GstBuffer *dumb_buf;
1698     dumb_buf = gst_kms_sink_copy_to_dumb_buffer (self, parent_meta->buffer);
1699     gst_kms_allocator_clear_cache (self->allocator);
1700     gst_kms_sink_show_frame (GST_VIDEO_SINK (self), dumb_buf);
1701     gst_buffer_unref (dumb_buf);
1702   }
1703 }
1704 
1705 static gboolean
gst_kms_sink_query(GstBaseSink * bsink,GstQuery * query)1706 gst_kms_sink_query (GstBaseSink * bsink, GstQuery * query)
1707 {
1708   GstKMSSink *self = GST_KMS_SINK (bsink);
1709 
1710   switch (GST_QUERY_TYPE (query)) {
1711     case GST_QUERY_ALLOCATION:
1712     case GST_QUERY_DRAIN:
1713     {
1714       gst_kms_sink_drain (self);
1715       break;
1716     }
1717     default:
1718       break;
1719   }
1720 
1721   return GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
1722 }
1723 
1724 static void
gst_kms_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1725 gst_kms_sink_set_property (GObject * object, guint prop_id,
1726     const GValue * value, GParamSpec * pspec)
1727 {
1728   GstKMSSink *sink;
1729 
1730   sink = GST_KMS_SINK (object);
1731 
1732   switch (prop_id) {
1733     case PROP_DRIVER_NAME:
1734       g_free (sink->devname);
1735       sink->devname = g_value_dup_string (value);
1736       break;
1737     case PROP_BUS_ID:
1738       g_free (sink->bus_id);
1739       sink->bus_id = g_value_dup_string (value);
1740       break;
1741     case PROP_CONNECTOR_ID:
1742       sink->conn_id = g_value_get_int (value);
1743       break;
1744     case PROP_PLANE_ID:
1745       sink->plane_id = g_value_get_int (value);
1746       break;
1747     case PROP_FORCE_MODESETTING:
1748       sink->modesetting_enabled = g_value_get_boolean (value);
1749       break;
1750     case PROP_RESTORE_CRTC:
1751       sink->restore_crtc = g_value_get_boolean (value);
1752       break;
1753     case PROP_CAN_SCALE:
1754       sink->can_scale = g_value_get_boolean (value);
1755       break;
1756     case PROP_CONNECTOR_PROPS:{
1757       const GstStructure *s = gst_value_get_structure (value);
1758 
1759       g_clear_pointer (&sink->connector_props, gst_structure_free);
1760 
1761       if (s)
1762         sink->connector_props = gst_structure_copy (s);
1763 
1764       break;
1765     }
1766     case PROP_PLANE_PROPS:{
1767       const GstStructure *s = gst_value_get_structure (value);
1768 
1769       g_clear_pointer (&sink->plane_props, gst_structure_free);
1770 
1771       if (s)
1772         sink->plane_props = gst_structure_copy (s);
1773 
1774       break;
1775     }
1776     default:
1777       if (!gst_video_overlay_set_property (object, PROP_N, prop_id, value))
1778         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1779       break;
1780   }
1781 }
1782 
1783 static void
gst_kms_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1784 gst_kms_sink_get_property (GObject * object, guint prop_id,
1785     GValue * value, GParamSpec * pspec)
1786 {
1787   GstKMSSink *sink;
1788 
1789   sink = GST_KMS_SINK (object);
1790 
1791   switch (prop_id) {
1792     case PROP_DRIVER_NAME:
1793       g_value_set_string (value, sink->devname);
1794       break;
1795     case PROP_BUS_ID:
1796       g_value_set_string (value, sink->bus_id);
1797       break;
1798     case PROP_CONNECTOR_ID:
1799       g_value_set_int (value, sink->conn_id);
1800       break;
1801     case PROP_PLANE_ID:
1802       g_value_set_int (value, sink->plane_id);
1803       break;
1804     case PROP_FORCE_MODESETTING:
1805       g_value_set_boolean (value, sink->modesetting_enabled);
1806       break;
1807     case PROP_RESTORE_CRTC:
1808       g_value_set_boolean (value, sink->restore_crtc);
1809       break;
1810     case PROP_CAN_SCALE:
1811       g_value_set_boolean (value, sink->can_scale);
1812       break;
1813     case PROP_DISPLAY_WIDTH:
1814       GST_OBJECT_LOCK (sink);
1815       g_value_set_int (value, sink->hdisplay);
1816       GST_OBJECT_UNLOCK (sink);
1817       break;
1818     case PROP_DISPLAY_HEIGHT:
1819       GST_OBJECT_LOCK (sink);
1820       g_value_set_int (value, sink->vdisplay);
1821       GST_OBJECT_UNLOCK (sink);
1822       break;
1823     case PROP_CONNECTOR_PROPS:
1824       gst_value_set_structure (value, sink->connector_props);
1825       break;
1826     case PROP_PLANE_PROPS:
1827       gst_value_set_structure (value, sink->plane_props);
1828       break;
1829     default:
1830       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1831       break;
1832   }
1833 }
1834 
1835 static void
gst_kms_sink_finalize(GObject * object)1836 gst_kms_sink_finalize (GObject * object)
1837 {
1838   GstKMSSink *sink;
1839 
1840   sink = GST_KMS_SINK (object);
1841   g_clear_pointer (&sink->devname, g_free);
1842   g_clear_pointer (&sink->bus_id, g_free);
1843   gst_poll_free (sink->poll);
1844   g_clear_pointer (&sink->connector_props, gst_structure_free);
1845   g_clear_pointer (&sink->plane_props, gst_structure_free);
1846   g_clear_pointer (&sink->tmp_kmsmem, gst_memory_unref);
1847 
1848   G_OBJECT_CLASS (parent_class)->finalize (object);
1849 }
1850 
1851 static void
gst_kms_sink_init(GstKMSSink * sink)1852 gst_kms_sink_init (GstKMSSink * sink)
1853 {
1854   sink->fd = -1;
1855   sink->conn_id = -1;
1856   sink->plane_id = -1;
1857   sink->can_scale = TRUE;
1858   gst_poll_fd_init (&sink->pollfd);
1859   sink->poll = gst_poll_new (TRUE);
1860   gst_video_info_init (&sink->vinfo);
1861 }
1862 
1863 static void
gst_kms_sink_class_init(GstKMSSinkClass * klass)1864 gst_kms_sink_class_init (GstKMSSinkClass * klass)
1865 {
1866   GObjectClass *gobject_class;
1867   GstElementClass *element_class;
1868   GstBaseSinkClass *basesink_class;
1869   GstVideoSinkClass *videosink_class;
1870   GstCaps *caps;
1871 
1872   gobject_class = G_OBJECT_CLASS (klass);
1873   element_class = GST_ELEMENT_CLASS (klass);
1874   basesink_class = GST_BASE_SINK_CLASS (klass);
1875   videosink_class = GST_VIDEO_SINK_CLASS (klass);
1876 
1877   gst_element_class_set_static_metadata (element_class, "KMS video sink",
1878       "Sink/Video", GST_PLUGIN_DESC, "Víctor Jáquez <vjaquez@igalia.com>");
1879 
1880   caps = gst_kms_sink_caps_template_fill ();
1881   gst_element_class_add_pad_template (element_class,
1882       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
1883   gst_caps_unref (caps);
1884 
1885   basesink_class->start = GST_DEBUG_FUNCPTR (gst_kms_sink_start);
1886   basesink_class->stop = GST_DEBUG_FUNCPTR (gst_kms_sink_stop);
1887   basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_kms_sink_set_caps);
1888   basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_kms_sink_get_caps);
1889   basesink_class->propose_allocation = gst_kms_sink_propose_allocation;
1890   basesink_class->query = gst_kms_sink_query;
1891 
1892   videosink_class->show_frame = gst_kms_sink_show_frame;
1893 
1894   gobject_class->finalize = gst_kms_sink_finalize;
1895   gobject_class->set_property = gst_kms_sink_set_property;
1896   gobject_class->get_property = gst_kms_sink_get_property;
1897 
1898   /**
1899    * kmssink:driver-name:
1900    *
1901    * If you have a system with multiple GPUs, you can choose which GPU
1902    * to use setting the DRM device driver name. Otherwise, the first
1903    * one from an internal list is used.
1904    */
1905   g_properties[PROP_DRIVER_NAME] = g_param_spec_string ("driver-name",
1906       "device name", "DRM device driver name", NULL,
1907       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1908 
1909   /**
1910    * kmssink:bus-id:
1911    *
1912    * If you have a system with multiple displays for the same driver-name,
1913    * you can choose which display to use by setting the DRM bus ID. Otherwise,
1914    * the driver decides which one.
1915    */
1916   g_properties[PROP_BUS_ID] = g_param_spec_string ("bus-id",
1917       "Bus ID", "DRM bus ID", NULL,
1918       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1919 
1920   /**
1921    * kmssink:connector-id:
1922    *
1923    * A GPU has several output connectors, for example: LVDS, VGA,
1924    * HDMI, etc. By default the first LVDS is tried, then the first
1925    * eDP, and at the end, the first connected one.
1926    */
1927   g_properties[PROP_CONNECTOR_ID] = g_param_spec_int ("connector-id",
1928       "Connector ID", "DRM connector id", -1, G_MAXINT32, -1,
1929       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1930 
1931    /**
1932    * kmssink:plane-id:
1933    *
1934    * There could be several planes associated with a CRTC.
1935    * By default the first plane that's possible to use with a given
1936    * CRTC is tried.
1937    */
1938   g_properties[PROP_PLANE_ID] = g_param_spec_int ("plane-id",
1939       "Plane ID", "DRM plane id", -1, G_MAXINT32, -1,
1940       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1941 
1942   /**
1943    * kmssink:force-modesetting:
1944    *
1945    * If the output connector is already active, the sink automatically uses an
1946    * overlay plane. Enforce mode setting in the kms sink and output to the
1947    * base plane to override the automatic behavior.
1948    */
1949   g_properties[PROP_FORCE_MODESETTING] =
1950       g_param_spec_boolean ("force-modesetting", "Force modesetting",
1951       "When enabled, the sink try to configure the display mode", FALSE,
1952       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1953 
1954   /**
1955    * kmssink:restore-crtc:
1956    *
1957    * Restore previous CRTC setting if new CRTC mode was set forcefully.
1958    * By default this is enabled if user set CRTC with a new mode on an already
1959    * active CRTC wich was having a valid mode.
1960    */
1961   g_properties[PROP_RESTORE_CRTC] =
1962       g_param_spec_boolean ("restore-crtc", "Restore CRTC mode",
1963       "When enabled and CRTC was set with a new mode, previous CRTC mode will"
1964       "be restored when going to NULL state.", TRUE,
1965       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1966 
1967   /**
1968    * kmssink:can-scale:
1969    *
1970    * User can tell kmssink if the driver can support scale.
1971    */
1972   g_properties[PROP_CAN_SCALE] =
1973       g_param_spec_boolean ("can-scale", "can scale",
1974       "User can tell kmssink if the driver can support scale", TRUE,
1975       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1976 
1977   /**
1978    * kmssink:display-width
1979    *
1980    * Actual width of the display. This is read only and only available in
1981    * PAUSED and PLAYING state. It's meant to be used with
1982    * gst_video_overlay_set_render_rectangle() function.
1983    */
1984   g_properties[PROP_DISPLAY_WIDTH] =
1985       g_param_spec_int ("display-width", "Display Width",
1986       "Width of the display surface in pixels", 0, G_MAXINT, 0,
1987       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
1988 
1989   /**
1990    * kmssink:display-height
1991    *
1992    * Actual height of the display. This is read only and only available in
1993    * PAUSED and PLAYING state. It's meant to be used with
1994    * gst_video_overlay_set_render_rectangle() function.
1995    */
1996   g_properties[PROP_DISPLAY_HEIGHT] =
1997       g_param_spec_int ("display-height", "Display Height",
1998       "Height of the display surface in pixels", 0, G_MAXINT, 0,
1999       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
2000 
2001   /**
2002    * kmssink:connector-properties:
2003    *
2004    * Additional properties for the connector. Keys are strings and values
2005    * unsigned 64 bits integers.
2006    *
2007    * Since: 1.16
2008    */
2009   g_properties[PROP_CONNECTOR_PROPS] =
2010       g_param_spec_boxed ("connector-properties", "Connector Properties",
2011       "Additionnal properties for the connector",
2012       GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2013 
2014   /**
2015    * kmssink:plane-properties:
2016    *
2017    * Additional properties for the plane. Keys are strings and values
2018    * unsigned 64 bits integers.
2019    *
2020    * Since: 1.16
2021    */
2022   g_properties[PROP_PLANE_PROPS] =
2023       g_param_spec_boxed ("plane-properties", "Connector Plane",
2024       "Additionnal properties for the plane",
2025       GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2026 
2027   g_object_class_install_properties (gobject_class, PROP_N, g_properties);
2028 
2029   gst_video_overlay_install_properties (gobject_class, PROP_N);
2030 }
2031 
2032 static gboolean
plugin_init(GstPlugin * plugin)2033 plugin_init (GstPlugin * plugin)
2034 {
2035   if (!gst_element_register (plugin, GST_PLUGIN_NAME, GST_RANK_SECONDARY,
2036           GST_TYPE_KMS_SINK))
2037     return FALSE;
2038 
2039   return TRUE;
2040 }
2041 
2042 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, kms,
2043     GST_PLUGIN_DESC, plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
2044     GST_PACKAGE_ORIGIN)
2045