• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gstkmssink.h"
61 #include "gstkmsutils.h"
62 #include "gstkmsbufferpool.h"
63 #include "gstkmsallocator.h"
64 
65 #define GST_PLUGIN_NAME "kmssink"
66 #define GST_PLUGIN_DESC "Video sink using the Linux kernel mode setting API"
67 
68 GST_DEBUG_CATEGORY_STATIC (gst_kms_sink_debug);
69 GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE);
70 #define GST_CAT_DEFAULT gst_kms_sink_debug
71 
72 static GstFlowReturn gst_kms_sink_show_frame (GstVideoSink * vsink,
73     GstBuffer * buf);
74 static void gst_kms_sink_video_overlay_init (GstVideoOverlayInterface * iface);
75 static void gst_kms_sink_drain (GstKMSSink * self);
76 
77 #define parent_class gst_kms_sink_parent_class
78 G_DEFINE_TYPE_WITH_CODE (GstKMSSink, gst_kms_sink, GST_TYPE_VIDEO_SINK,
79     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, GST_PLUGIN_NAME, 0,
80         GST_PLUGIN_DESC);
81     GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE");
82     G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
83         gst_kms_sink_video_overlay_init));
84 GST_ELEMENT_REGISTER_DEFINE (kmssink, GST_PLUGIN_NAME, GST_RANK_SECONDARY,
85     GST_TYPE_KMS_SINK);
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", "tegra",
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 void
ensure_kms_allocator(GstKMSSink * self)421 ensure_kms_allocator (GstKMSSink * self)
422 {
423   if (self->allocator)
424     return;
425   self->allocator = gst_kms_allocator_new (self->fd);
426 }
427 
428 static gboolean
configure_mode_setting(GstKMSSink * self,GstVideoInfo * vinfo)429 configure_mode_setting (GstKMSSink * self, GstVideoInfo * vinfo)
430 {
431   gboolean ret;
432   drmModeConnector *conn;
433   int err;
434   gint i;
435   drmModeModeInfo *mode;
436   guint32 fb_id;
437   GstKMSMemory *kmsmem;
438 
439   ret = FALSE;
440   conn = NULL;
441   mode = NULL;
442   kmsmem = NULL;
443 
444   if (self->conn_id < 0)
445     goto bail;
446 
447   GST_INFO_OBJECT (self, "configuring mode setting");
448 
449   ensure_kms_allocator (self);
450   kmsmem = (GstKMSMemory *) gst_kms_allocator_bo_alloc (self->allocator, vinfo);
451   if (!kmsmem)
452     goto bo_failed;
453   fb_id = kmsmem->fb_id;
454 
455   conn = drmModeGetConnector (self->fd, self->conn_id);
456   if (!conn)
457     goto connector_failed;
458 
459   for (i = 0; i < conn->count_modes; i++) {
460     if (conn->modes[i].vdisplay == GST_VIDEO_INFO_HEIGHT (vinfo) &&
461         conn->modes[i].hdisplay == GST_VIDEO_INFO_WIDTH (vinfo)) {
462       mode = &conn->modes[i];
463       break;
464     }
465   }
466   if (!mode)
467     goto mode_failed;
468 
469   err = drmModeSetCrtc (self->fd, self->crtc_id, fb_id, 0, 0,
470       (uint32_t *) & self->conn_id, 1, mode);
471   if (err)
472     goto modesetting_failed;
473 
474   g_clear_pointer (&self->tmp_kmsmem, gst_memory_unref);
475   self->tmp_kmsmem = (GstMemory *) kmsmem;
476 
477   ret = TRUE;
478 
479 bail:
480   if (conn)
481     drmModeFreeConnector (conn);
482 
483   return ret;
484 
485   /* ERRORS */
486 bo_failed:
487   {
488     GST_ERROR_OBJECT (self,
489         "failed to allocate buffer object for mode setting");
490     goto bail;
491   }
492 connector_failed:
493   {
494     GST_ERROR_OBJECT (self, "Could not find a valid monitor connector");
495     goto bail;
496   }
497 mode_failed:
498   {
499     GST_ERROR_OBJECT (self, "cannot find appropriate mode");
500     goto bail;
501   }
502 modesetting_failed:
503   {
504     GST_ERROR_OBJECT (self, "Failed to set mode: %s", g_strerror (errno));
505     goto bail;
506   }
507 }
508 
509 static gboolean
ensure_allowed_caps(GstKMSSink * self,drmModeConnector * conn,drmModePlane * plane,drmModeRes * res)510 ensure_allowed_caps (GstKMSSink * self, drmModeConnector * conn,
511     drmModePlane * plane, drmModeRes * res)
512 {
513   GstCaps *out_caps, *tmp_caps, *caps;
514   int i, j;
515   GstVideoFormat fmt;
516   const gchar *format;
517   drmModeModeInfo *mode;
518   gint count_modes;
519 
520   if (self->allowed_caps)
521     return TRUE;
522 
523   out_caps = gst_caps_new_empty ();
524   if (!out_caps)
525     return FALSE;
526 
527   if (conn && self->modesetting_enabled)
528     count_modes = conn->count_modes;
529   else
530     count_modes = 1;
531 
532   for (i = 0; i < count_modes; i++) {
533     tmp_caps = gst_caps_new_empty ();
534     if (!tmp_caps)
535       return FALSE;
536 
537     mode = NULL;
538     if (conn && self->modesetting_enabled)
539       mode = &conn->modes[i];
540 
541     for (j = 0; j < plane->count_formats; j++) {
542       fmt = gst_video_format_from_drm (plane->formats[j]);
543       if (fmt == GST_VIDEO_FORMAT_UNKNOWN) {
544         GST_INFO_OBJECT (self, "ignoring format %" GST_FOURCC_FORMAT,
545             GST_FOURCC_ARGS (plane->formats[j]));
546         continue;
547       }
548 
549       format = gst_video_format_to_string (fmt);
550 
551       if (mode) {
552         caps = gst_caps_new_simple ("video/x-raw",
553             "format", G_TYPE_STRING, format,
554             "width", G_TYPE_INT, mode->hdisplay,
555             "height", G_TYPE_INT, mode->vdisplay,
556             "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
557       } else {
558         caps = gst_caps_new_simple ("video/x-raw",
559             "format", G_TYPE_STRING, format,
560             "width", GST_TYPE_INT_RANGE, res->min_width, res->max_width,
561             "height", GST_TYPE_INT_RANGE, res->min_height, res->max_height,
562             "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
563       }
564       if (!caps)
565         continue;
566 
567       tmp_caps = gst_caps_merge (tmp_caps, caps);
568     }
569 
570     out_caps = gst_caps_merge (out_caps, gst_caps_simplify (tmp_caps));
571   }
572 
573   if (gst_caps_is_empty (out_caps)) {
574     GST_DEBUG_OBJECT (self, "allowed caps is empty");
575     gst_caps_unref (out_caps);
576     return FALSE;
577   }
578 
579   self->allowed_caps = gst_caps_simplify (out_caps);
580 
581   GST_DEBUG_OBJECT (self, "allowed caps = %" GST_PTR_FORMAT,
582       self->allowed_caps);
583 
584   return TRUE;
585 }
586 
587 static gboolean
set_drm_property(gint fd,guint32 object,guint32 object_type,drmModeObjectPropertiesPtr properties,const gchar * prop_name,guint64 value)588 set_drm_property (gint fd, guint32 object, guint32 object_type,
589     drmModeObjectPropertiesPtr properties, const gchar * prop_name,
590     guint64 value)
591 {
592   guint i;
593   gboolean ret = FALSE;
594 
595   for (i = 0; i < properties->count_props && !ret; i++) {
596     drmModePropertyPtr property;
597 
598     property = drmModeGetProperty (fd, properties->props[i]);
599 
600     /* GstStructure parser limits the set of supported character, so we
601      * replace the invalid characters with '-'. In DRM, this is generally
602      * replacing spaces into '-'. */
603     g_strcanon (property->name, G_CSET_a_2_z G_CSET_A_2_Z G_CSET_DIGITS "_",
604         '-');
605 
606     GST_LOG ("found property %s (looking for %s)", property->name, prop_name);
607 
608     if (!strcmp (property->name, prop_name)) {
609       drmModeObjectSetProperty (fd, object, object_type,
610           property->prop_id, value);
611       ret = TRUE;
612     }
613     drmModeFreeProperty (property);
614   }
615 
616   return ret;
617 }
618 
619 typedef struct
620 {
621   GstKMSSink *self;
622   drmModeObjectPropertiesPtr properties;
623   guint obj_id;
624   guint obj_type;
625   const gchar *obj_type_str;
626 } SetPropsIter;
627 
628 static gboolean
set_obj_prop(GQuark field_id,const GValue * value,gpointer user_data)629 set_obj_prop (GQuark field_id, const GValue * value, gpointer user_data)
630 {
631   SetPropsIter *iter = user_data;
632   GstKMSSink *self = iter->self;
633   const gchar *name;
634   guint64 v;
635 
636   name = g_quark_to_string (field_id);
637 
638   if (G_VALUE_HOLDS (value, G_TYPE_INT))
639     v = g_value_get_int (value);
640   else if (G_VALUE_HOLDS (value, G_TYPE_UINT))
641     v = g_value_get_uint (value);
642   else if (G_VALUE_HOLDS (value, G_TYPE_INT64))
643     v = g_value_get_int64 (value);
644   else if (G_VALUE_HOLDS (value, G_TYPE_UINT64))
645     v = g_value_get_uint64 (value);
646   else {
647     GST_WARNING_OBJECT (self,
648         "'uint64' value expected for control '%s'.", name);
649     return TRUE;
650   }
651 
652   if (set_drm_property (self->fd, iter->obj_id, iter->obj_type,
653           iter->properties, name, v)) {
654     GST_DEBUG_OBJECT (self,
655         "Set %s property '%s' to %" G_GUINT64_FORMAT,
656         iter->obj_type_str, name, v);
657   } else {
658     GST_WARNING_OBJECT (self,
659         "Failed to set %s property '%s' to %" G_GUINT64_FORMAT,
660         iter->obj_type_str, name, v);
661   }
662 
663   return TRUE;
664 }
665 
666 static void
gst_kms_sink_update_properties(SetPropsIter * iter,GstStructure * props)667 gst_kms_sink_update_properties (SetPropsIter * iter, GstStructure * props)
668 {
669   GstKMSSink *self = iter->self;
670 
671   iter->properties = drmModeObjectGetProperties (self->fd, iter->obj_id,
672       iter->obj_type);
673 
674   gst_structure_foreach (props, set_obj_prop, iter);
675 
676   drmModeFreeObjectProperties (iter->properties);
677 }
678 
679 static void
gst_kms_sink_update_connector_properties(GstKMSSink * self)680 gst_kms_sink_update_connector_properties (GstKMSSink * self)
681 {
682   SetPropsIter iter;
683 
684   if (!self->connector_props)
685     return;
686 
687   iter.self = self;
688   iter.obj_id = self->conn_id;
689   iter.obj_type = DRM_MODE_OBJECT_CONNECTOR;
690   iter.obj_type_str = "connector";
691 
692   gst_kms_sink_update_properties (&iter, self->connector_props);
693 }
694 
695 static void
gst_kms_sink_update_plane_properties(GstKMSSink * self)696 gst_kms_sink_update_plane_properties (GstKMSSink * self)
697 {
698   SetPropsIter iter;
699 
700   if (!self->plane_props)
701     return;
702 
703   iter.self = self;
704   iter.obj_id = self->plane_id;
705   iter.obj_type = DRM_MODE_OBJECT_PLANE;
706   iter.obj_type_str = "plane";
707 
708   gst_kms_sink_update_properties (&iter, self->plane_props);
709 }
710 
711 static gboolean
gst_kms_sink_start(GstBaseSink * bsink)712 gst_kms_sink_start (GstBaseSink * bsink)
713 {
714   GstKMSSink *self;
715   drmModeRes *res;
716   drmModeConnector *conn;
717   drmModeCrtc *crtc;
718   drmModePlaneRes *pres;
719   drmModePlane *plane;
720   gboolean universal_planes;
721   gboolean ret;
722 
723   self = GST_KMS_SINK (bsink);
724   universal_planes = FALSE;
725   ret = FALSE;
726   res = NULL;
727   conn = NULL;
728   crtc = NULL;
729   pres = NULL;
730   plane = NULL;
731 
732   if (self->devname || self->bus_id)
733     self->fd = drmOpen (self->devname, self->bus_id);
734   else
735     self->fd = kms_open (&self->devname);
736   if (self->fd < 0)
737     goto open_failed;
738 
739   log_drm_version (self);
740   if (!get_drm_caps (self))
741     goto bail;
742 
743   res = drmModeGetResources (self->fd);
744   if (!res)
745     goto resources_failed;
746 
747   if (self->conn_id == -1)
748     conn = find_main_monitor (self->fd, res);
749   else
750     conn = drmModeGetConnector (self->fd, self->conn_id);
751   if (!conn)
752     goto connector_failed;
753 
754   crtc = find_crtc_for_connector (self->fd, res, conn, &self->pipe);
755   if (!crtc)
756     goto crtc_failed;
757 
758   if (!crtc->mode_valid || self->modesetting_enabled) {
759     GST_DEBUG_OBJECT (self, "enabling modesetting");
760     self->modesetting_enabled = TRUE;
761     universal_planes = TRUE;
762   }
763 
764   if (crtc->mode_valid && self->modesetting_enabled && self->restore_crtc) {
765     self->saved_crtc = (drmModeCrtc *) crtc;
766   }
767 
768 retry_find_plane:
769   if (universal_planes &&
770       drmSetClientCap (self->fd, DRM_CLIENT_CAP_UNIVERSAL_PLANES, 1))
771     goto set_cap_failed;
772 
773   pres = drmModeGetPlaneResources (self->fd);
774   if (!pres)
775     goto plane_resources_failed;
776 
777   if (self->plane_id == -1)
778     plane = find_plane_for_crtc (self->fd, res, pres, crtc->crtc_id);
779   else
780     plane = drmModeGetPlane (self->fd, self->plane_id);
781   if (!plane)
782     goto plane_failed;
783 
784   if (!ensure_allowed_caps (self, conn, plane, res))
785     goto allowed_caps_failed;
786 
787   self->conn_id = conn->connector_id;
788   self->crtc_id = crtc->crtc_id;
789   self->plane_id = plane->plane_id;
790 
791   GST_INFO_OBJECT (self, "connector id = %d / crtc id = %d / plane id = %d",
792       self->conn_id, self->crtc_id, self->plane_id);
793 
794   GST_OBJECT_LOCK (self);
795   self->hdisplay = crtc->mode.hdisplay;
796   self->vdisplay = crtc->mode.vdisplay;
797 
798   if (self->render_rect.w == 0 || self->render_rect.h == 0) {
799     self->render_rect.x = 0;
800     self->render_rect.y = 0;
801     self->render_rect.w = self->hdisplay;
802     self->render_rect.h = self->vdisplay;
803   }
804 
805   self->pending_rect = self->render_rect;
806   GST_OBJECT_UNLOCK (self);
807 
808   self->buffer_id = crtc->buffer_id;
809 
810   self->mm_width = conn->mmWidth;
811   self->mm_height = conn->mmHeight;
812 
813   GST_INFO_OBJECT (self, "display size: pixels = %dx%d / millimeters = %dx%d",
814       self->hdisplay, self->vdisplay, self->mm_width, self->mm_height);
815 
816   self->pollfd.fd = self->fd;
817   gst_poll_add_fd (self->poll, &self->pollfd);
818   gst_poll_fd_ctl_read (self->poll, &self->pollfd, TRUE);
819 
820   g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_WIDTH]);
821   g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_HEIGHT]);
822 
823   gst_kms_sink_update_connector_properties (self);
824   gst_kms_sink_update_plane_properties (self);
825 
826   ret = TRUE;
827 
828 bail:
829   if (plane)
830     drmModeFreePlane (plane);
831   if (pres)
832     drmModeFreePlaneResources (pres);
833   if (crtc != self->saved_crtc)
834     drmModeFreeCrtc (crtc);
835   if (conn)
836     drmModeFreeConnector (conn);
837   if (res)
838     drmModeFreeResources (res);
839 
840   if (!ret && self->fd >= 0) {
841     drmClose (self->fd);
842     self->fd = -1;
843   }
844 
845   return ret;
846 
847   /* ERRORS */
848 open_failed:
849   {
850     GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE,
851         ("Could not open DRM module %s", GST_STR_NULL (self->devname)),
852         ("reason: %s (%d)", g_strerror (errno), errno));
853     return FALSE;
854   }
855 
856 resources_failed:
857   {
858     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
859         ("drmModeGetResources failed"),
860         ("reason: %s (%d)", g_strerror (errno), errno));
861     goto bail;
862   }
863 
864 connector_failed:
865   {
866     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
867         ("Could not find a valid monitor connector"), (NULL));
868     goto bail;
869   }
870 
871 crtc_failed:
872   {
873     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
874         ("Could not find a crtc for connector"), (NULL));
875     goto bail;
876   }
877 
878 set_cap_failed:
879   {
880     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
881         ("Could not set universal planes capability bit"), (NULL));
882     goto bail;
883   }
884 
885 plane_resources_failed:
886   {
887     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
888         ("drmModeGetPlaneResources failed"),
889         ("reason: %s (%d)", g_strerror (errno), errno));
890     goto bail;
891   }
892 
893 plane_failed:
894   {
895     if (universal_planes) {
896       GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
897           ("Could not find a plane for crtc"), (NULL));
898       goto bail;
899     } else {
900       universal_planes = TRUE;
901       goto retry_find_plane;
902     }
903   }
904 
905 allowed_caps_failed:
906   {
907     GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
908         ("Could not get allowed GstCaps of device"),
909         ("driver does not provide mode settings configuration"));
910     goto bail;
911   }
912 }
913 
914 static gboolean
gst_kms_sink_stop(GstBaseSink * bsink)915 gst_kms_sink_stop (GstBaseSink * bsink)
916 {
917   GstKMSSink *self;
918   int err;
919 
920   self = GST_KMS_SINK (bsink);
921 
922   if (self->allocator)
923     gst_kms_allocator_clear_cache (self->allocator);
924 
925   gst_buffer_replace (&self->last_buffer, NULL);
926   gst_caps_replace (&self->allowed_caps, NULL);
927   gst_object_replace ((GstObject **) & self->pool, NULL);
928   gst_object_replace ((GstObject **) & self->allocator, NULL);
929 
930   gst_poll_remove_fd (self->poll, &self->pollfd);
931   gst_poll_restart (self->poll);
932   gst_poll_fd_init (&self->pollfd);
933 
934   if (self->saved_crtc) {
935     drmModeCrtc *crtc = (drmModeCrtc *) self->saved_crtc;
936 
937     err = drmModeSetCrtc (self->fd, crtc->crtc_id, crtc->buffer_id, crtc->x,
938         crtc->y, (uint32_t *) & self->conn_id, 1, &crtc->mode);
939     if (err)
940       GST_ERROR_OBJECT (self, "Failed to restore previous CRTC mode: %s",
941           g_strerror (errno));
942 
943     drmModeFreeCrtc (crtc);
944     self->saved_crtc = NULL;
945   }
946 
947   if (self->fd >= 0) {
948     drmClose (self->fd);
949     self->fd = -1;
950   }
951 
952   GST_OBJECT_LOCK (bsink);
953   self->hdisplay = 0;
954   self->vdisplay = 0;
955   self->pending_rect.x = 0;
956   self->pending_rect.y = 0;
957   self->pending_rect.w = 0;
958   self->pending_rect.h = 0;
959   self->render_rect = self->pending_rect;
960   GST_OBJECT_UNLOCK (bsink);
961 
962   g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_WIDTH]);
963   g_object_notify_by_pspec (G_OBJECT (self), g_properties[PROP_DISPLAY_HEIGHT]);
964 
965   return TRUE;
966 }
967 
968 static GstCaps *
gst_kms_sink_get_allowed_caps(GstKMSSink * self)969 gst_kms_sink_get_allowed_caps (GstKMSSink * self)
970 {
971   if (!self->allowed_caps)
972     return NULL;                /* base class will return the template caps */
973   return gst_caps_ref (self->allowed_caps);
974 }
975 
976 static GstCaps *
gst_kms_sink_get_caps(GstBaseSink * bsink,GstCaps * filter)977 gst_kms_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
978 {
979   GstKMSSink *self;
980   GstCaps *caps, *out_caps;
981   GstStructure *s;
982   guint dpy_par_n, dpy_par_d;
983 
984   self = GST_KMS_SINK (bsink);
985 
986   caps = gst_kms_sink_get_allowed_caps (self);
987   if (!caps)
988     return NULL;
989 
990   GST_OBJECT_LOCK (self);
991 
992   if (!self->can_scale) {
993     out_caps = gst_caps_new_empty ();
994     gst_video_calculate_device_ratio (self->hdisplay, self->vdisplay,
995         self->mm_width, self->mm_height, &dpy_par_n, &dpy_par_d);
996 
997     s = gst_structure_copy (gst_caps_get_structure (caps, 0));
998     gst_structure_set (s, "width", G_TYPE_INT, self->pending_rect.w,
999         "height", G_TYPE_INT, self->pending_rect.h,
1000         "pixel-aspect-ratio", GST_TYPE_FRACTION, dpy_par_n, dpy_par_d, NULL);
1001 
1002     gst_caps_append_structure (out_caps, s);
1003 
1004     out_caps = gst_caps_merge (out_caps, caps);
1005     caps = NULL;
1006 
1007     /* enforce our display aspect ratio */
1008     gst_caps_set_simple (out_caps, "pixel-aspect-ratio", GST_TYPE_FRACTION,
1009         dpy_par_n, dpy_par_d, NULL);
1010   } else {
1011     out_caps = gst_caps_make_writable (caps);
1012     caps = NULL;
1013   }
1014 
1015   GST_OBJECT_UNLOCK (self);
1016 
1017   GST_DEBUG_OBJECT (self, "Proposing caps %" GST_PTR_FORMAT, out_caps);
1018 
1019   if (filter) {
1020     caps = out_caps;
1021     out_caps = gst_caps_intersect_full (caps, filter, GST_CAPS_INTERSECT_FIRST);
1022     gst_caps_unref (caps);
1023   }
1024 
1025   return out_caps;
1026 }
1027 
1028 static GstBufferPool *
gst_kms_sink_create_pool(GstKMSSink * self,GstCaps * caps,gsize size,gint min)1029 gst_kms_sink_create_pool (GstKMSSink * self, GstCaps * caps, gsize size,
1030     gint min)
1031 {
1032   GstBufferPool *pool;
1033   GstStructure *config;
1034 
1035   pool = gst_kms_buffer_pool_new ();
1036   if (!pool)
1037     goto pool_failed;
1038 
1039   config = gst_buffer_pool_get_config (pool);
1040   gst_buffer_pool_config_set_params (config, caps, size, min, 0);
1041   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
1042 
1043   ensure_kms_allocator (self);
1044   gst_buffer_pool_config_set_allocator (config, self->allocator, NULL);
1045 
1046   if (!gst_buffer_pool_set_config (pool, config))
1047     goto config_failed;
1048 
1049   return pool;
1050 
1051   /* ERRORS */
1052 pool_failed:
1053   {
1054     GST_ERROR_OBJECT (self, "failed to create buffer pool");
1055     return NULL;
1056   }
1057 config_failed:
1058   {
1059     GST_ERROR_OBJECT (self, "failed to set config");
1060     gst_object_unref (pool);
1061     return NULL;
1062   }
1063 }
1064 
1065 static gboolean
gst_kms_sink_calculate_display_ratio(GstKMSSink * self,GstVideoInfo * vinfo,gint * scaled_width,gint * scaled_height)1066 gst_kms_sink_calculate_display_ratio (GstKMSSink * self, GstVideoInfo * vinfo,
1067     gint * scaled_width, gint * scaled_height)
1068 {
1069   guint dar_n, dar_d;
1070   guint video_width, video_height;
1071   guint video_par_n, video_par_d;
1072   guint dpy_par_n, dpy_par_d;
1073 
1074   video_width = GST_VIDEO_INFO_WIDTH (vinfo);
1075   video_height = GST_VIDEO_INFO_HEIGHT (vinfo);
1076   video_par_n = GST_VIDEO_INFO_PAR_N (vinfo);
1077   video_par_d = GST_VIDEO_INFO_PAR_D (vinfo);
1078 
1079   if (self->can_scale) {
1080     gst_video_calculate_device_ratio (self->hdisplay, self->vdisplay,
1081         self->mm_width, self->mm_height, &dpy_par_n, &dpy_par_d);
1082   } else {
1083     *scaled_width = video_width;
1084     *scaled_height = video_height;
1085     goto out;
1086   }
1087 
1088   if (!gst_video_calculate_display_ratio (&dar_n, &dar_d, video_width,
1089           video_height, video_par_n, video_par_d, dpy_par_n, dpy_par_d))
1090     return FALSE;
1091 
1092   GST_DEBUG_OBJECT (self, "video calculated display ratio: %d/%d", dar_n,
1093       dar_d);
1094 
1095   /* now find a width x height that respects this display ratio.
1096    * prefer those that have one of w/h the same as the incoming video
1097    * using wd / hd = dar_n / dar_d */
1098 
1099   /* start with same height, because of interlaced video */
1100   /* check hd / dar_d is an integer scale factor, and scale wd with the PAR */
1101   if (video_height % dar_d == 0) {
1102     GST_DEBUG_OBJECT (self, "keeping video height");
1103     *scaled_width = (guint)
1104         gst_util_uint64_scale_int (video_height, dar_n, dar_d);
1105     *scaled_height = video_height;
1106   } else if (video_width % dar_n == 0) {
1107     GST_DEBUG_OBJECT (self, "keeping video width");
1108     *scaled_width = video_width;
1109     *scaled_height = (guint)
1110         gst_util_uint64_scale_int (video_width, dar_d, dar_n);
1111   } else {
1112     GST_DEBUG_OBJECT (self, "approximating while keeping video height");
1113     *scaled_width = (guint)
1114         gst_util_uint64_scale_int (video_height, dar_n, dar_d);
1115     *scaled_height = video_height;
1116   }
1117 
1118 out:
1119   GST_DEBUG_OBJECT (self, "scaling to %dx%d", *scaled_width, *scaled_height);
1120 
1121   return TRUE;
1122 }
1123 
1124 static gboolean
gst_kms_sink_set_caps(GstBaseSink * bsink,GstCaps * caps)1125 gst_kms_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
1126 {
1127   GstKMSSink *self;
1128   GstVideoInfo vinfo;
1129 
1130   self = GST_KMS_SINK (bsink);
1131 
1132   if (!gst_video_info_from_caps (&vinfo, caps))
1133     goto invalid_format;
1134   self->vinfo = vinfo;
1135 
1136   if (!gst_kms_sink_calculate_display_ratio (self, &vinfo,
1137           &GST_VIDEO_SINK_WIDTH (self), &GST_VIDEO_SINK_HEIGHT (self)))
1138     goto no_disp_ratio;
1139 
1140   if (GST_VIDEO_SINK_WIDTH (self) <= 0 || GST_VIDEO_SINK_HEIGHT (self) <= 0)
1141     goto invalid_size;
1142 
1143   /* discard dumb buffer pool */
1144   if (self->pool) {
1145     gst_buffer_pool_set_active (self->pool, FALSE);
1146     gst_object_unref (self->pool);
1147     self->pool = NULL;
1148   }
1149 
1150   if (self->modesetting_enabled && !configure_mode_setting (self, &vinfo))
1151     goto modesetting_failed;
1152 
1153   GST_OBJECT_LOCK (self);
1154   if (self->reconfigure) {
1155     self->reconfigure = FALSE;
1156     self->render_rect = self->pending_rect;
1157   }
1158   GST_OBJECT_UNLOCK (self);
1159 
1160   GST_DEBUG_OBJECT (self, "negotiated caps = %" GST_PTR_FORMAT, caps);
1161 
1162   return TRUE;
1163 
1164   /* ERRORS */
1165 invalid_format:
1166   {
1167     GST_ERROR_OBJECT (self, "caps invalid");
1168     return FALSE;
1169   }
1170 
1171 invalid_size:
1172   {
1173     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1174         ("Invalid image size."));
1175     return FALSE;
1176   }
1177 
1178 no_disp_ratio:
1179   {
1180     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1181         ("Error calculating the output display ratio of the video."));
1182     return FALSE;
1183   }
1184 
1185 modesetting_failed:
1186   {
1187     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1188         ("failed to configure video mode"));
1189     return FALSE;
1190   }
1191 
1192 }
1193 
1194 static gboolean
gst_kms_sink_propose_allocation(GstBaseSink * bsink,GstQuery * query)1195 gst_kms_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1196 {
1197   GstKMSSink *self;
1198   GstCaps *caps;
1199   gboolean need_pool;
1200   GstVideoInfo vinfo;
1201   GstBufferPool *pool;
1202   gsize size;
1203 
1204   self = GST_KMS_SINK (bsink);
1205 
1206   GST_DEBUG_OBJECT (self, "propose allocation");
1207 
1208   gst_query_parse_allocation (query, &caps, &need_pool);
1209   if (!caps)
1210     goto no_caps;
1211   if (!gst_video_info_from_caps (&vinfo, caps))
1212     goto invalid_caps;
1213 
1214   size = GST_VIDEO_INFO_SIZE (&vinfo);
1215 
1216   pool = NULL;
1217   if (need_pool) {
1218     pool = gst_kms_sink_create_pool (self, caps, size, 0);
1219     if (!pool)
1220       goto no_pool;
1221 
1222     /* Only export for pool used upstream */
1223     if (self->has_prime_export) {
1224       GstStructure *config = gst_buffer_pool_get_config (pool);
1225       gst_buffer_pool_config_add_option (config,
1226           GST_BUFFER_POOL_OPTION_KMS_PRIME_EXPORT);
1227       gst_buffer_pool_set_config (pool, config);
1228     }
1229   }
1230 
1231   /* we need at least 2 buffer because we hold on to the last one */
1232   gst_query_add_allocation_pool (query, pool, size, 2, 0);
1233   if (pool)
1234     gst_object_unref (pool);
1235 
1236   gst_query_add_allocation_meta (query, GST_VIDEO_META_API_TYPE, NULL);
1237   gst_query_add_allocation_meta (query, GST_VIDEO_CROP_META_API_TYPE, NULL);
1238 
1239   return TRUE;
1240 
1241   /* ERRORS */
1242 no_caps:
1243   {
1244     GST_DEBUG_OBJECT (bsink, "no caps specified");
1245     return FALSE;
1246   }
1247 invalid_caps:
1248   {
1249     GST_DEBUG_OBJECT (bsink, "invalid caps specified");
1250     return FALSE;
1251   }
1252 no_pool:
1253   {
1254     /* Already warned in create_pool */
1255     return FALSE;
1256   }
1257 }
1258 
1259 static void
sync_handler(gint fd,guint frame,guint sec,guint usec,gpointer data)1260 sync_handler (gint fd, guint frame, guint sec, guint usec, gpointer data)
1261 {
1262   gboolean *waiting;
1263 
1264   waiting = data;
1265   *waiting = FALSE;
1266 }
1267 
1268 static gboolean
gst_kms_sink_sync(GstKMSSink * self)1269 gst_kms_sink_sync (GstKMSSink * self)
1270 {
1271   gint ret;
1272   gboolean waiting;
1273   drmEventContext evctxt = {
1274     .version = DRM_EVENT_CONTEXT_VERSION,
1275     .page_flip_handler = sync_handler,
1276     .vblank_handler = sync_handler,
1277   };
1278   drmVBlank vbl = {
1279     .request = {
1280           .type = DRM_VBLANK_RELATIVE | DRM_VBLANK_EVENT,
1281           .sequence = 1,
1282           .signal = (gulong) & waiting,
1283         },
1284   };
1285 
1286   if (self->pipe == 1)
1287     vbl.request.type |= DRM_VBLANK_SECONDARY;
1288   else if (self->pipe > 1)
1289     vbl.request.type |= self->pipe << DRM_VBLANK_HIGH_CRTC_SHIFT;
1290 
1291   waiting = TRUE;
1292   if (!self->has_async_page_flip && !self->modesetting_enabled) {
1293     ret = drmWaitVBlank (self->fd, &vbl);
1294     if (ret)
1295       goto vblank_failed;
1296   } else {
1297     ret = drmModePageFlip (self->fd, self->crtc_id, self->buffer_id,
1298         DRM_MODE_PAGE_FLIP_EVENT, &waiting);
1299     if (ret)
1300       goto pageflip_failed;
1301   }
1302 
1303   while (waiting) {
1304     do {
1305       ret = gst_poll_wait (self->poll, 3 * GST_SECOND);
1306     } while (ret == -1 && (errno == EAGAIN || errno == EINTR));
1307 
1308     ret = drmHandleEvent (self->fd, &evctxt);
1309     if (ret)
1310       goto event_failed;
1311   }
1312 
1313   return TRUE;
1314 
1315   /* ERRORS */
1316 vblank_failed:
1317   {
1318     GST_WARNING_OBJECT (self, "drmWaitVBlank failed: %s (%d)",
1319         g_strerror (errno), errno);
1320     return FALSE;
1321   }
1322 pageflip_failed:
1323   {
1324     GST_WARNING_OBJECT (self, "drmModePageFlip failed: %s (%d)",
1325         g_strerror (errno), errno);
1326     return FALSE;
1327   }
1328 event_failed:
1329   {
1330     GST_ERROR_OBJECT (self, "drmHandleEvent failed: %s (%d)",
1331         g_strerror (errno), errno);
1332     return FALSE;
1333   }
1334 }
1335 
1336 static gboolean
gst_kms_sink_import_dmabuf(GstKMSSink * self,GstBuffer * inbuf,GstBuffer ** outbuf)1337 gst_kms_sink_import_dmabuf (GstKMSSink * self, GstBuffer * inbuf,
1338     GstBuffer ** outbuf)
1339 {
1340   gint prime_fds[GST_VIDEO_MAX_PLANES] = { 0, };
1341   GstVideoMeta *meta;
1342   guint i, n_mem, n_planes;
1343   GstKMSMemory *kmsmem;
1344   guint mems_idx[GST_VIDEO_MAX_PLANES];
1345   gsize mems_skip[GST_VIDEO_MAX_PLANES];
1346   GstMemory *mems[GST_VIDEO_MAX_PLANES];
1347 
1348   if (!self->has_prime_import)
1349     return FALSE;
1350 
1351   /* This will eliminate most non-dmabuf out there */
1352   if (!gst_is_dmabuf_memory (gst_buffer_peek_memory (inbuf, 0)))
1353     return FALSE;
1354 
1355   n_planes = GST_VIDEO_INFO_N_PLANES (&self->vinfo);
1356   n_mem = gst_buffer_n_memory (inbuf);
1357   meta = gst_buffer_get_video_meta (inbuf);
1358 
1359   GST_TRACE_OBJECT (self, "Found a dmabuf with %u planes and %u memories",
1360       n_planes, n_mem);
1361 
1362   /* We cannot have multiple dmabuf per plane */
1363   if (n_mem > n_planes)
1364     return FALSE;
1365   g_assert (n_planes != 0);
1366 
1367   /* Update video info based on video meta */
1368   if (meta) {
1369     GST_VIDEO_INFO_WIDTH (&self->vinfo) = meta->width;
1370     GST_VIDEO_INFO_HEIGHT (&self->vinfo) = meta->height;
1371 
1372     for (i = 0; i < meta->n_planes; i++) {
1373       GST_VIDEO_INFO_PLANE_OFFSET (&self->vinfo, i) = meta->offset[i];
1374       GST_VIDEO_INFO_PLANE_STRIDE (&self->vinfo, i) = meta->stride[i];
1375     }
1376   }
1377 
1378   /* Find and validate all memories */
1379   for (i = 0; i < n_planes; i++) {
1380     guint length;
1381 
1382     if (!gst_buffer_find_memory (inbuf,
1383             GST_VIDEO_INFO_PLANE_OFFSET (&self->vinfo, i), 1,
1384             &mems_idx[i], &length, &mems_skip[i]))
1385       return FALSE;
1386 
1387     mems[i] = gst_buffer_peek_memory (inbuf, mems_idx[i]);
1388 
1389     /* adjust for memory offset, in case data does not
1390      * start from byte 0 in the dmabuf fd */
1391     mems_skip[i] += mems[i]->offset;
1392 
1393     /* And all memory found must be dmabuf */
1394     if (!gst_is_dmabuf_memory (mems[i]))
1395       return FALSE;
1396   }
1397 
1398   ensure_kms_allocator (self);
1399 
1400   kmsmem = (GstKMSMemory *) gst_kms_allocator_get_cached (mems[0]);
1401   if (kmsmem) {
1402     GST_LOG_OBJECT (self, "found KMS mem %p in DMABuf mem %p with fb id = %d",
1403         kmsmem, mems[0], kmsmem->fb_id);
1404     goto wrap_mem;
1405   }
1406 
1407   for (i = 0; i < n_planes; i++)
1408     prime_fds[i] = gst_dmabuf_memory_get_fd (mems[i]);
1409 
1410   GST_LOG_OBJECT (self, "found these prime ids: %d, %d, %d, %d", prime_fds[0],
1411       prime_fds[1], prime_fds[2], prime_fds[3]);
1412 
1413   kmsmem = gst_kms_allocator_dmabuf_import (self->allocator,
1414       prime_fds, n_planes, mems_skip, &self->vinfo);
1415   if (!kmsmem)
1416     return FALSE;
1417 
1418   GST_LOG_OBJECT (self, "setting KMS mem %p to DMABuf mem %p with fb id = %d",
1419       kmsmem, mems[0], kmsmem->fb_id);
1420   gst_kms_allocator_cache (self->allocator, mems[0], GST_MEMORY_CAST (kmsmem));
1421 
1422 wrap_mem:
1423   *outbuf = gst_buffer_new ();
1424   if (!*outbuf)
1425     return FALSE;
1426   gst_buffer_append_memory (*outbuf, gst_memory_ref (GST_MEMORY_CAST (kmsmem)));
1427   gst_buffer_add_parent_buffer_meta (*outbuf, inbuf);
1428 
1429   return TRUE;
1430 }
1431 
1432 static gboolean
ensure_internal_pool(GstKMSSink * self,GstVideoInfo * in_vinfo,GstBuffer * inbuf)1433 ensure_internal_pool (GstKMSSink * self, GstVideoInfo * in_vinfo,
1434     GstBuffer * inbuf)
1435 {
1436   GstBufferPool *pool;
1437   GstVideoInfo vinfo = *in_vinfo;
1438   GstVideoMeta *vmeta;
1439   GstCaps *caps;
1440 
1441   if (self->pool)
1442     return TRUE;
1443 
1444   /* When cropping, the caps matches the cropped rectangle width/height, but
1445    * we can retrieve the padded width/height from the VideoMeta (which is kept
1446    * intact when adding crop meta */
1447   if ((vmeta = gst_buffer_get_video_meta (inbuf))) {
1448     vinfo.width = vmeta->width;
1449     vinfo.height = vmeta->height;
1450   }
1451 
1452   caps = gst_video_info_to_caps (&vinfo);
1453   pool = gst_kms_sink_create_pool (self, caps, gst_buffer_get_size (inbuf), 2);
1454   gst_caps_unref (caps);
1455 
1456   if (!pool)
1457     return FALSE;
1458 
1459   if (!gst_buffer_pool_set_active (pool, TRUE))
1460     goto activate_pool_failed;
1461 
1462   self->pool = pool;
1463   return TRUE;
1464 
1465 activate_pool_failed:
1466   {
1467     GST_ELEMENT_ERROR (self, STREAM, FAILED, ("failed to activate buffer pool"),
1468         ("failed to activate buffer pool"));
1469     gst_object_unref (pool);
1470     return FALSE;
1471   }
1472 
1473 }
1474 
1475 static GstBuffer *
gst_kms_sink_copy_to_dumb_buffer(GstKMSSink * self,GstVideoInfo * vinfo,GstBuffer * inbuf)1476 gst_kms_sink_copy_to_dumb_buffer (GstKMSSink * self, GstVideoInfo * vinfo,
1477     GstBuffer * inbuf)
1478 {
1479   GstFlowReturn ret;
1480   GstVideoFrame inframe, outframe;
1481   gboolean success;
1482   GstBuffer *buf = NULL;
1483 
1484   if (!ensure_internal_pool (self, vinfo, inbuf))
1485     goto bail;
1486 
1487   ret = gst_buffer_pool_acquire_buffer (self->pool, &buf, NULL);
1488   if (ret != GST_FLOW_OK)
1489     goto create_buffer_failed;
1490 
1491   if (!gst_video_frame_map (&inframe, vinfo, inbuf, GST_MAP_READ))
1492     goto error_map_src_buffer;
1493 
1494   if (!gst_video_frame_map (&outframe, vinfo, buf, GST_MAP_WRITE))
1495     goto error_map_dst_buffer;
1496 
1497   success = gst_video_frame_copy (&outframe, &inframe);
1498   gst_video_frame_unmap (&outframe);
1499   gst_video_frame_unmap (&inframe);
1500   if (!success)
1501     goto error_copy_buffer;
1502 
1503   return buf;
1504 
1505 bail:
1506   {
1507     if (buf)
1508       gst_buffer_unref (buf);
1509     return NULL;
1510   }
1511 
1512   /* ERRORS */
1513 create_buffer_failed:
1514   {
1515     GST_ELEMENT_ERROR (self, STREAM, FAILED, ("allocation failed"),
1516         ("failed to create buffer"));
1517     return NULL;
1518   }
1519 error_copy_buffer:
1520   {
1521     GST_WARNING_OBJECT (self, "failed to upload buffer");
1522     goto bail;
1523   }
1524 error_map_dst_buffer:
1525   {
1526     gst_video_frame_unmap (&inframe);
1527     /* fall-through */
1528   }
1529 error_map_src_buffer:
1530   {
1531     GST_WARNING_OBJECT (self, "failed to map buffer");
1532     goto bail;
1533   }
1534 }
1535 
1536 static GstBuffer *
gst_kms_sink_get_input_buffer(GstKMSSink * self,GstBuffer * inbuf)1537 gst_kms_sink_get_input_buffer (GstKMSSink * self, GstBuffer * inbuf)
1538 {
1539   GstMemory *mem;
1540   GstBuffer *buf = NULL;
1541 
1542   mem = gst_buffer_peek_memory (inbuf, 0);
1543   if (!mem)
1544     return NULL;
1545 
1546   if (gst_is_kms_memory (mem))
1547     return gst_buffer_ref (inbuf);
1548 
1549   if (gst_kms_sink_import_dmabuf (self, inbuf, &buf))
1550     goto done;
1551 
1552   GST_CAT_INFO_OBJECT (CAT_PERFORMANCE, self, "frame copy");
1553   buf = gst_kms_sink_copy_to_dumb_buffer (self, &self->vinfo, inbuf);
1554 
1555 done:
1556   /* Copy all the non-memory related metas, this way CropMeta will be
1557    * available upon GstVideoOverlay::expose calls. */
1558   if (buf)
1559     gst_buffer_copy_into (buf, inbuf, GST_BUFFER_COPY_METADATA, 0, -1);
1560 
1561   return buf;
1562 }
1563 
1564 static GstFlowReturn
gst_kms_sink_show_frame(GstVideoSink * vsink,GstBuffer * buf)1565 gst_kms_sink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
1566 {
1567   gint ret;
1568   GstBuffer *buffer = NULL;
1569   guint32 fb_id;
1570   GstKMSSink *self;
1571   GstVideoInfo *vinfo;
1572   GstVideoCropMeta *crop;
1573   GstVideoRectangle src = { 0, };
1574   gint video_width, video_height;
1575   GstVideoRectangle dst = { 0, };
1576   GstVideoRectangle result;
1577   GstFlowReturn res;
1578 
1579   self = GST_KMS_SINK (vsink);
1580 
1581   res = GST_FLOW_ERROR;
1582 
1583   if (buf) {
1584     buffer = gst_kms_sink_get_input_buffer (self, buf);
1585     vinfo = &self->vinfo;
1586     video_width = src.w = GST_VIDEO_SINK_WIDTH (self);
1587     video_height = src.h = GST_VIDEO_SINK_HEIGHT (self);
1588   } else if (self->last_buffer) {
1589     buffer = gst_buffer_ref (self->last_buffer);
1590     vinfo = &self->last_vinfo;
1591     video_width = src.w = self->last_width;
1592     video_height = src.h = self->last_height;
1593   }
1594 
1595   /* Make sure buf is not used accidentally */
1596   buf = NULL;
1597 
1598   if (!buffer)
1599     return GST_FLOW_ERROR;
1600   fb_id = gst_kms_memory_get_fb_id (gst_buffer_peek_memory (buffer, 0));
1601   if (fb_id == 0)
1602     goto buffer_invalid;
1603 
1604   GST_TRACE_OBJECT (self, "displaying fb %d", fb_id);
1605 
1606   GST_OBJECT_LOCK (self);
1607   if (self->modesetting_enabled) {
1608     self->buffer_id = fb_id;
1609     goto sync_frame;
1610   }
1611 
1612   if ((crop = gst_buffer_get_video_crop_meta (buffer))) {
1613     GstVideoInfo cropped_vinfo = *vinfo;
1614 
1615     cropped_vinfo.width = crop->width;
1616     cropped_vinfo.height = crop->height;
1617 
1618     if (!gst_kms_sink_calculate_display_ratio (self, &cropped_vinfo, &src.w,
1619             &src.h))
1620       goto no_disp_ratio;
1621 
1622     src.x = crop->x;
1623     src.y = crop->y;
1624   }
1625 
1626   dst.w = self->render_rect.w;
1627   dst.h = self->render_rect.h;
1628 
1629 retry_set_plane:
1630   gst_video_sink_center_rect (src, dst, &result, self->can_scale);
1631 
1632   result.x += self->render_rect.x;
1633   result.y += self->render_rect.y;
1634 
1635   if (crop) {
1636     src.w = crop->width;
1637     src.h = crop->height;
1638   } else {
1639     src.w = video_width;
1640     src.h = video_height;
1641   }
1642 
1643   /* handle out of screen case */
1644   if ((result.x + result.w) > self->hdisplay)
1645     result.w = self->hdisplay - result.x;
1646 
1647   if ((result.y + result.h) > self->vdisplay)
1648     result.h = self->vdisplay - result.y;
1649 
1650   if (result.w <= 0 || result.h <= 0) {
1651     GST_WARNING_OBJECT (self, "video is out of display range");
1652     goto sync_frame;
1653   }
1654 
1655   /* to make sure it can be show when driver don't support scale */
1656   if (!self->can_scale) {
1657     src.w = result.w;
1658     src.h = result.h;
1659   }
1660 
1661   GST_TRACE_OBJECT (self,
1662       "drmModeSetPlane at (%i,%i) %ix%i sourcing at (%i,%i) %ix%i",
1663       result.x, result.y, result.w, result.h, src.x, src.y, src.w, src.h);
1664 
1665   ret = drmModeSetPlane (self->fd, self->plane_id, self->crtc_id, fb_id, 0,
1666       result.x, result.y, result.w, result.h,
1667       /* source/cropping coordinates are given in Q16 */
1668       src.x << 16, src.y << 16, src.w << 16, src.h << 16);
1669   if (ret) {
1670     if (self->can_scale) {
1671       self->can_scale = FALSE;
1672       goto retry_set_plane;
1673     }
1674     goto set_plane_failed;
1675   }
1676 
1677 sync_frame:
1678   /* Wait for the previous frame to complete redraw */
1679   if (!gst_kms_sink_sync (self)) {
1680     GST_OBJECT_UNLOCK (self);
1681     goto bail;
1682   }
1683 
1684   /* Save the rendered buffer and its metadata in case a redraw is needed */
1685   if (buffer != self->last_buffer) {
1686     gst_buffer_replace (&self->last_buffer, buffer);
1687     self->last_width = GST_VIDEO_SINK_WIDTH (self);
1688     self->last_height = GST_VIDEO_SINK_HEIGHT (self);
1689     self->last_vinfo = self->vinfo;
1690   }
1691   g_clear_pointer (&self->tmp_kmsmem, gst_memory_unref);
1692 
1693   GST_OBJECT_UNLOCK (self);
1694   res = GST_FLOW_OK;
1695 
1696 bail:
1697   gst_buffer_unref (buffer);
1698   return res;
1699 
1700   /* ERRORS */
1701 buffer_invalid:
1702   {
1703     GST_ERROR_OBJECT (self, "invalid buffer: it doesn't have a fb id");
1704     goto bail;
1705   }
1706 set_plane_failed:
1707   {
1708     GST_OBJECT_UNLOCK (self);
1709     GST_DEBUG_OBJECT (self, "result = { %d, %d, %d, %d} / "
1710         "src = { %d, %d, %d %d } / dst = { %d, %d, %d %d }", result.x, result.y,
1711         result.w, result.h, src.x, src.y, src.w, src.h, dst.x, dst.y, dst.w,
1712         dst.h);
1713     GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
1714         (NULL), ("drmModeSetPlane failed: %s (%d)", g_strerror (errno), errno));
1715     goto bail;
1716   }
1717 no_disp_ratio:
1718   {
1719     GST_OBJECT_UNLOCK (self);
1720     GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (NULL),
1721         ("Error calculating the output display ratio of the video."));
1722     goto bail;
1723   }
1724 }
1725 
1726 static void
gst_kms_sink_drain(GstKMSSink * self)1727 gst_kms_sink_drain (GstKMSSink * self)
1728 {
1729   GstParentBufferMeta *parent_meta;
1730 
1731   if (!self->last_buffer)
1732     return;
1733 
1734   /* We only need to return the last_buffer if it depends on upstream buffer.
1735    * In this case, the last_buffer will have a GstParentBufferMeta set. */
1736   parent_meta = gst_buffer_get_parent_buffer_meta (self->last_buffer);
1737   if (parent_meta) {
1738     GstBuffer *dumb_buf, *last_buf;
1739 
1740     /* If this was imported from our dumb buffer pool we can safely skip the
1741      * drain */
1742     if (parent_meta->buffer->pool &&
1743         GST_IS_KMS_BUFFER_POOL (parent_meta->buffer->pool))
1744       return;
1745 
1746     GST_DEBUG_OBJECT (self, "draining");
1747 
1748     dumb_buf = gst_kms_sink_copy_to_dumb_buffer (self, &self->last_vinfo,
1749         parent_meta->buffer);
1750     last_buf = self->last_buffer;
1751     self->last_buffer = dumb_buf;
1752 
1753     gst_kms_allocator_clear_cache (self->allocator);
1754     gst_kms_sink_show_frame (GST_VIDEO_SINK (self), NULL);
1755     gst_buffer_unref (last_buf);
1756   }
1757 }
1758 
1759 static gboolean
gst_kms_sink_query(GstBaseSink * bsink,GstQuery * query)1760 gst_kms_sink_query (GstBaseSink * bsink, GstQuery * query)
1761 {
1762   GstKMSSink *self = GST_KMS_SINK (bsink);
1763 
1764   switch (GST_QUERY_TYPE (query)) {
1765     case GST_QUERY_ALLOCATION:
1766     case GST_QUERY_DRAIN:
1767     {
1768       gst_kms_sink_drain (self);
1769       break;
1770     }
1771     default:
1772       break;
1773   }
1774 
1775   return GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
1776 }
1777 
1778 static void
gst_kms_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1779 gst_kms_sink_set_property (GObject * object, guint prop_id,
1780     const GValue * value, GParamSpec * pspec)
1781 {
1782   GstKMSSink *sink;
1783 
1784   sink = GST_KMS_SINK (object);
1785 
1786   switch (prop_id) {
1787     case PROP_DRIVER_NAME:
1788       g_free (sink->devname);
1789       sink->devname = g_value_dup_string (value);
1790       break;
1791     case PROP_BUS_ID:
1792       g_free (sink->bus_id);
1793       sink->bus_id = g_value_dup_string (value);
1794       break;
1795     case PROP_CONNECTOR_ID:
1796       sink->conn_id = g_value_get_int (value);
1797       break;
1798     case PROP_PLANE_ID:
1799       sink->plane_id = g_value_get_int (value);
1800       break;
1801     case PROP_FORCE_MODESETTING:
1802       sink->modesetting_enabled = g_value_get_boolean (value);
1803       break;
1804     case PROP_RESTORE_CRTC:
1805       sink->restore_crtc = g_value_get_boolean (value);
1806       break;
1807     case PROP_CAN_SCALE:
1808       sink->can_scale = g_value_get_boolean (value);
1809       break;
1810     case PROP_CONNECTOR_PROPS:{
1811       const GstStructure *s = gst_value_get_structure (value);
1812 
1813       g_clear_pointer (&sink->connector_props, gst_structure_free);
1814 
1815       if (s)
1816         sink->connector_props = gst_structure_copy (s);
1817 
1818       break;
1819     }
1820     case PROP_PLANE_PROPS:{
1821       const GstStructure *s = gst_value_get_structure (value);
1822 
1823       g_clear_pointer (&sink->plane_props, gst_structure_free);
1824 
1825       if (s)
1826         sink->plane_props = gst_structure_copy (s);
1827 
1828       break;
1829     }
1830     default:
1831       if (!gst_video_overlay_set_property (object, PROP_N, prop_id, value))
1832         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1833       break;
1834   }
1835 }
1836 
1837 static void
gst_kms_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1838 gst_kms_sink_get_property (GObject * object, guint prop_id,
1839     GValue * value, GParamSpec * pspec)
1840 {
1841   GstKMSSink *sink;
1842 
1843   sink = GST_KMS_SINK (object);
1844 
1845   switch (prop_id) {
1846     case PROP_DRIVER_NAME:
1847       g_value_set_string (value, sink->devname);
1848       break;
1849     case PROP_BUS_ID:
1850       g_value_set_string (value, sink->bus_id);
1851       break;
1852     case PROP_CONNECTOR_ID:
1853       g_value_set_int (value, sink->conn_id);
1854       break;
1855     case PROP_PLANE_ID:
1856       g_value_set_int (value, sink->plane_id);
1857       break;
1858     case PROP_FORCE_MODESETTING:
1859       g_value_set_boolean (value, sink->modesetting_enabled);
1860       break;
1861     case PROP_RESTORE_CRTC:
1862       g_value_set_boolean (value, sink->restore_crtc);
1863       break;
1864     case PROP_CAN_SCALE:
1865       g_value_set_boolean (value, sink->can_scale);
1866       break;
1867     case PROP_DISPLAY_WIDTH:
1868       GST_OBJECT_LOCK (sink);
1869       g_value_set_int (value, sink->hdisplay);
1870       GST_OBJECT_UNLOCK (sink);
1871       break;
1872     case PROP_DISPLAY_HEIGHT:
1873       GST_OBJECT_LOCK (sink);
1874       g_value_set_int (value, sink->vdisplay);
1875       GST_OBJECT_UNLOCK (sink);
1876       break;
1877     case PROP_CONNECTOR_PROPS:
1878       gst_value_set_structure (value, sink->connector_props);
1879       break;
1880     case PROP_PLANE_PROPS:
1881       gst_value_set_structure (value, sink->plane_props);
1882       break;
1883     default:
1884       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1885       break;
1886   }
1887 }
1888 
1889 static void
gst_kms_sink_finalize(GObject * object)1890 gst_kms_sink_finalize (GObject * object)
1891 {
1892   GstKMSSink *sink;
1893 
1894   sink = GST_KMS_SINK (object);
1895   g_clear_pointer (&sink->devname, g_free);
1896   g_clear_pointer (&sink->bus_id, g_free);
1897   gst_poll_free (sink->poll);
1898   g_clear_pointer (&sink->connector_props, gst_structure_free);
1899   g_clear_pointer (&sink->plane_props, gst_structure_free);
1900   g_clear_pointer (&sink->tmp_kmsmem, gst_memory_unref);
1901 
1902   G_OBJECT_CLASS (parent_class)->finalize (object);
1903 }
1904 
1905 static void
gst_kms_sink_init(GstKMSSink * sink)1906 gst_kms_sink_init (GstKMSSink * sink)
1907 {
1908   sink->fd = -1;
1909   sink->conn_id = -1;
1910   sink->plane_id = -1;
1911   sink->can_scale = TRUE;
1912   gst_poll_fd_init (&sink->pollfd);
1913   sink->poll = gst_poll_new (TRUE);
1914   gst_video_info_init (&sink->vinfo);
1915 }
1916 
1917 static void
gst_kms_sink_class_init(GstKMSSinkClass * klass)1918 gst_kms_sink_class_init (GstKMSSinkClass * klass)
1919 {
1920   GObjectClass *gobject_class;
1921   GstElementClass *element_class;
1922   GstBaseSinkClass *basesink_class;
1923   GstVideoSinkClass *videosink_class;
1924   GstCaps *caps;
1925 
1926   gobject_class = G_OBJECT_CLASS (klass);
1927   element_class = GST_ELEMENT_CLASS (klass);
1928   basesink_class = GST_BASE_SINK_CLASS (klass);
1929   videosink_class = GST_VIDEO_SINK_CLASS (klass);
1930 
1931   gst_element_class_set_static_metadata (element_class, "KMS video sink",
1932       "Sink/Video", GST_PLUGIN_DESC, "Víctor Jáquez <vjaquez@igalia.com>");
1933 
1934   caps = gst_kms_sink_caps_template_fill ();
1935   gst_element_class_add_pad_template (element_class,
1936       gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, caps));
1937   gst_caps_unref (caps);
1938 
1939   basesink_class->start = GST_DEBUG_FUNCPTR (gst_kms_sink_start);
1940   basesink_class->stop = GST_DEBUG_FUNCPTR (gst_kms_sink_stop);
1941   basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_kms_sink_set_caps);
1942   basesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_kms_sink_get_caps);
1943   basesink_class->propose_allocation = gst_kms_sink_propose_allocation;
1944   basesink_class->query = gst_kms_sink_query;
1945 
1946   videosink_class->show_frame = gst_kms_sink_show_frame;
1947 
1948   gobject_class->finalize = gst_kms_sink_finalize;
1949   gobject_class->set_property = gst_kms_sink_set_property;
1950   gobject_class->get_property = gst_kms_sink_get_property;
1951 
1952   /**
1953    * kmssink:driver-name:
1954    *
1955    * If you have a system with multiple GPUs, you can choose which GPU
1956    * to use setting the DRM device driver name. Otherwise, the first
1957    * one from an internal list is used.
1958    */
1959   g_properties[PROP_DRIVER_NAME] = g_param_spec_string ("driver-name",
1960       "device name", "DRM device driver name", NULL,
1961       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1962 
1963   /**
1964    * kmssink:bus-id:
1965    *
1966    * If you have a system with multiple displays for the same driver-name,
1967    * you can choose which display to use by setting the DRM bus ID. Otherwise,
1968    * the driver decides which one.
1969    */
1970   g_properties[PROP_BUS_ID] = g_param_spec_string ("bus-id",
1971       "Bus ID", "DRM bus ID", NULL,
1972       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1973 
1974   /**
1975    * kmssink:connector-id:
1976    *
1977    * A GPU has several output connectors, for example: LVDS, VGA,
1978    * HDMI, etc. By default the first LVDS is tried, then the first
1979    * eDP, and at the end, the first connected one.
1980    */
1981   g_properties[PROP_CONNECTOR_ID] = g_param_spec_int ("connector-id",
1982       "Connector ID", "DRM connector id", -1, G_MAXINT32, -1,
1983       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1984 
1985    /**
1986    * kmssink:plane-id:
1987    *
1988    * There could be several planes associated with a CRTC.
1989    * By default the first plane that's possible to use with a given
1990    * CRTC is tried.
1991    */
1992   g_properties[PROP_PLANE_ID] = g_param_spec_int ("plane-id",
1993       "Plane ID", "DRM plane id", -1, G_MAXINT32, -1,
1994       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
1995 
1996   /**
1997    * kmssink:force-modesetting:
1998    *
1999    * If the output connector is already active, the sink automatically uses an
2000    * overlay plane. Enforce mode setting in the kms sink and output to the
2001    * base plane to override the automatic behavior.
2002    */
2003   g_properties[PROP_FORCE_MODESETTING] =
2004       g_param_spec_boolean ("force-modesetting", "Force modesetting",
2005       "When enabled, the sink try to configure the display mode", FALSE,
2006       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
2007 
2008   /**
2009    * kmssink:restore-crtc:
2010    *
2011    * Restore previous CRTC setting if new CRTC mode was set forcefully.
2012    * By default this is enabled if user set CRTC with a new mode on an already
2013    * active CRTC wich was having a valid mode.
2014    */
2015   g_properties[PROP_RESTORE_CRTC] =
2016       g_param_spec_boolean ("restore-crtc", "Restore CRTC mode",
2017       "When enabled and CRTC was set with a new mode, previous CRTC mode will"
2018       "be restored when going to NULL state.", TRUE,
2019       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
2020 
2021   /**
2022    * kmssink:can-scale:
2023    *
2024    * User can tell kmssink if the driver can support scale.
2025    */
2026   g_properties[PROP_CAN_SCALE] =
2027       g_param_spec_boolean ("can-scale", "can scale",
2028       "User can tell kmssink if the driver can support scale", TRUE,
2029       G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT);
2030 
2031   /**
2032    * kmssink:display-width
2033    *
2034    * Actual width of the display. This is read only and only available in
2035    * PAUSED and PLAYING state. It's meant to be used with
2036    * gst_video_overlay_set_render_rectangle() function.
2037    */
2038   g_properties[PROP_DISPLAY_WIDTH] =
2039       g_param_spec_int ("display-width", "Display Width",
2040       "Width of the display surface in pixels", 0, G_MAXINT, 0,
2041       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
2042 
2043   /**
2044    * kmssink:display-height
2045    *
2046    * Actual height of the display. This is read only and only available in
2047    * PAUSED and PLAYING state. It's meant to be used with
2048    * gst_video_overlay_set_render_rectangle() function.
2049    */
2050   g_properties[PROP_DISPLAY_HEIGHT] =
2051       g_param_spec_int ("display-height", "Display Height",
2052       "Height of the display surface in pixels", 0, G_MAXINT, 0,
2053       G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
2054 
2055   /**
2056    * kmssink:connector-properties:
2057    *
2058    * Additional properties for the connector. Keys are strings and values
2059    * unsigned 64 bits integers.
2060    *
2061    * Since: 1.16
2062    */
2063   g_properties[PROP_CONNECTOR_PROPS] =
2064       g_param_spec_boxed ("connector-properties", "Connector Properties",
2065       "Additional properties for the connector",
2066       GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2067 
2068   /**
2069    * kmssink:plane-properties:
2070    *
2071    * Additional properties for the plane. Keys are strings and values
2072    * unsigned 64 bits integers.
2073    *
2074    * Since: 1.16
2075    */
2076   g_properties[PROP_PLANE_PROPS] =
2077       g_param_spec_boxed ("plane-properties", "Connector Plane",
2078       "Additional properties for the plane",
2079       GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
2080 
2081   g_object_class_install_properties (gobject_class, PROP_N, g_properties);
2082 
2083   gst_video_overlay_install_properties (gobject_class, PROP_N);
2084 }
2085 
2086 static gboolean
plugin_init(GstPlugin * plugin)2087 plugin_init (GstPlugin * plugin)
2088 {
2089   return GST_ELEMENT_REGISTER (kmssink, plugin);
2090 }
2091 
2092 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR, GST_VERSION_MINOR, kms,
2093     GST_PLUGIN_DESC, plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME,
2094     GST_PACKAGE_ORIGIN)
2095