meta-agl-profile-telematics: recipes-core: systemd: change canbus systemd match regex
[AGL/meta-agl.git] / meta-agl-bsp / meta-ti / recipes-arago / gstreamer / gstreamer1.0-plugins-bad / 0003-gstkmssink-Add-support-for-KMS-based-sink.patch
1 From 44ba6f9839a410e981c9c941f099316ebfac2659 Mon Sep 17 00:00:00 2001
2 From: Pooja Prajod <a0132412@ti.com>
3 Date: Fri, 20 Jan 2017 16:18:22 +0530
4 Subject: [PATCH 3/5] gstkmssink: Add support for KMS based sink
5
6 The following features are enabled:
7 1. Add support for kmssink
8 2. Fix memory leak by using API's that do not hold
9    reference to GstMemory
10 3. Restrict the number of buffers that will be allocated
11    by kmssink bufferpool
12 4. Use Atomic mode setting instead of SetPlane
13 5. Store encoder and plane data as static data to enable
14    same process looping usecase
15 6. Handle usecase where display is disabled by default
16
17 Signed-off-by: Pooja Prajod <a0132412@ti.com>
18 ---
19  configure.ac               |  14 +
20  sys/Makefile.am            |  10 +-
21  sys/kms/Makefile.am        |  28 ++
22  sys/kms/gstdrmutils.c      | 347 +++++++++++++++++++++
23  sys/kms/gstdrmutils.h      |  50 +++
24  sys/kms/gstkmsbufferpriv.c | 121 ++++++++
25  sys/kms/gstkmsbufferpriv.h |  64 ++++
26  sys/kms/gstkmssink.c       | 740 +++++++++++++++++++++++++++++++++++++++++++++
27  sys/kms/gstkmssink.h       |  92 ++++++
28  9 files changed, 1464 insertions(+), 2 deletions(-)
29  create mode 100644 sys/kms/Makefile.am
30  create mode 100644 sys/kms/gstdrmutils.c
31  create mode 100644 sys/kms/gstdrmutils.h
32  create mode 100644 sys/kms/gstkmsbufferpriv.c
33  create mode 100644 sys/kms/gstkmsbufferpriv.h
34  create mode 100644 sys/kms/gstkmssink.c
35  create mode 100644 sys/kms/gstkmssink.h
36
37 diff --git a/configure.ac b/configure.ac
38 index e254605..9fdfbc7 100644
39 --- a/configure.ac
40 +++ b/configure.ac
41 @@ -2324,6 +2324,18 @@ AG_GST_CHECK_FEATURE(KATE, [Kate], kate, [
42    AC_SUBST(TIGER_LIBS)
43  ],,,[AM_CONDITIONAL(USE_TIGER, false)])
44  
45 +       
46 +       
47 +dnl *** kms ***
48 +translit(dnm, m, l) AM_CONDITIONAL(USE_KMS, true)
49 +AG_GST_CHECK_FEATURE(KMS, [kmssink], kms, [
50 +PKG_CHECK_MODULES([DRM], [libdrm libdrm_omap], HAVE_KMS=yes, HAVE_KMS=no)
51 +PKG_CHECK_MODULES(LIBDCE, [libdce >= 1.0.0], HAVE_KMS=yes, HAVE_KMS=no)
52 +AC_SUBST(DRM_CFLAGS)
53 +AC_SUBST(DRM_LIBS)
54 +])
55 +
56 +
57  dnl *** ladspa ***
58  translit(dnm, m, l) AM_CONDITIONAL(USE_LADSPA, true)
59  AG_GST_CHECK_FEATURE(LADSPA, [ladspa], ladspa, [
60 @@ -3383,6 +3395,7 @@ AM_CONDITIONAL(USE_GTK3_GL, false)
61  AM_CONDITIONAL(USE_HLS, false)
62  AM_CONDITIONAL(USE_KATE, false)
63  AM_CONDITIONAL(USE_TIGER, false)
64 +AM_CONDITIONAL(USE_KMS, false)
65  AM_CONDITIONAL(USE_LADSPA, false)
66  AM_CONDITIONAL(USE_LV2, false)
67  AM_CONDITIONAL(USE_LIBDE265, false)
68 @@ -3632,6 +3645,7 @@ sys/fbdev/Makefile
69  sys/linsys/Makefile
70  sys/nvenc/Makefile
71  sys/opensles/Makefile
72 +sys/kms/Makefile
73  sys/shm/Makefile
74  sys/tinyalsa/Makefile
75  sys/uvch264/Makefile
76 diff --git a/sys/Makefile.am b/sys/Makefile.am
77 index 32f79fb..325b4af 100644
78 --- a/sys/Makefile.am
79 +++ b/sys/Makefile.am
80 @@ -87,6 +87,12 @@ PVR_DIR=pvr2d
81  else
82  PVR_DIR=
83  endif
84 +       
85 +if USE_KMS
86 +KMS_DIR=kms
87 +else
88 +KMS_DIR=
89 +endif
90  
91  if USE_SHM
92  SHM_DIR=shm
93 @@ -148,10 +154,10 @@ else
94  TINYALSA_DIR=
95  endif
96  
97 -SUBDIRS = $(ACM_DIR) $(ANDROID_MEDIA_DIR) $(APPLE_MEDIA_DIR) $(AVC_DIR) $(BLUEZ_DIR) $(D3DVIDEOSINK_DIR) $(DECKLINK_DIR) $(DIRECTSOUND_DIR) $(WINKS_DIR) $(DVB_DIR) $(FBDEV_DIR) $(LINSYS_DIR) $(OPENSLES_DIR) $(PVR_DIR) $(SHM_DIR) $(UVCH264_DIR) $(VCD_DIR) $(VDPAU_DIR) $(WININET_DIR) $(WINSCREENCAP_DIR) $(WASAPI_DIR) $(NVENC_DIR) $(TINYALSA_DIR)
98 +SUBDIRS = $(ACM_DIR) $(ANDROID_MEDIA_DIR) $(APPLE_MEDIA_DIR) $(AVC_DIR) $(BLUEZ_DIR) $(D3DVIDEOSINK_DIR) $(DECKLINK_DIR) $(DIRECTSOUND_DIR) $(WINKS_DIR) $(DVB_DIR) $(FBDEV_DIR) $(LINSYS_DIR) $(OPENSLES_DIR) $(PVR_DIR) $(KMS_DIR) $(SHM_DIR) $(UVCH264_DIR) $(VCD_DIR) $(VDPAU_DIR) $(WININET_DIR) $(WINSCREENCAP_DIR) $(WASAPI_DIR) $(NVENC_DIR) $(TINYALSA_DIR)
99  
100  DIST_SUBDIRS = acmenc acmmp3dec androidmedia applemedia applemedia-nonpublic avc bluez d3dvideosink decklink directsound dvb linsys fbdev dshowdecwrapper dshowsrcwrapper dshowvideosink \
101 -               opensles pvr2d shm uvch264 vcd vdpau wasapi wininet winks winscreencap \
102 +               opensles pvr2d kms shm uvch264 vcd vdpau wasapi wininet winks winscreencap \
103                 nvenc tinyalsa
104  
105  include $(top_srcdir)/common/parallel-subdirs.mak
106 diff --git a/sys/kms/Makefile.am b/sys/kms/Makefile.am
107 new file mode 100644
108 index 0000000..6d56073
109 --- /dev/null
110 +++ b/sys/kms/Makefile.am
111 @@ -0,0 +1,28 @@
112 +plugin_LTLIBRARIES = libgstkmssink.la
113 +
114 +libgstkmssink_la_SOURCES = \
115 +       gstkmssink.c \
116 +       gstkmsbufferpriv.c \
117 +       gstdrmutils.c
118 +
119 +libgstkmssink_la_CFLAGS = \
120 +       $(GST_PLUGINS_BAD_CFLAGS) \
121 +       $(GST_PLUGINS_BASE_CFLAGS) \
122 +       $(GST_BASE_CFLAGS) \
123 +       $(LIBDCE_CFLAGS) \
124 +       $(GST_CFLAGS) \
125 +       $(DRM_CFLAGS)
126 +
127 +libgstkmssink_la_LIBADD = \
128 +       $(GST_PLUGINS_BASE_LIBS) \
129 +       $(GST_BASE_LIBS) \
130 +       $(GST_LIBS) \
131 +       $(LIBDCE_LIBS) \
132 +       $(DRM_LIBS) \
133 +       -lgstvideo-$(GST_API_VERSION) \
134 +       $(top_builddir)/gst-libs/gst/drm/libgstdrm-$(GST_API_VERSION).la
135 +
136 +libgstkmssink_la_LDFLAGS = $(GST_PLUGIN_LDFLAGS)
137 +libgstkmssink_la_LIBTOOLFLAGS = --tag=disable-static
138 +
139 +noinst_HEADERS = gstkmssink.h gstdrmutils.h gstkmsbufferpriv.h
140 diff --git a/sys/kms/gstdrmutils.c b/sys/kms/gstdrmutils.c
141 new file mode 100644
142 index 0000000..0e67a48
143 --- /dev/null
144 +++ b/sys/kms/gstdrmutils.c
145 @@ -0,0 +1,347 @@
146 +/* GStreamer
147 + *
148 + * Copyright (C) 2012 Texas Instruments
149 + * Copyright (C) 2012 Collabora Ltd
150 + *
151 + * Authors:
152 + *  Alessandro Decina <alessandro.decina@collabora.co.uk>
153 + *
154 + * This library is free software; you can redistribute it and/or
155 + * modify it under the terms of the GNU Library General Public
156 + * License as published by the Free Software Foundation; either
157 + * version 2 of the License, or (at your option) any later version.
158 + *
159 + * This library is distributed in the hope that it will be useful,
160 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
161 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
162 + * Library General Public License for more details.
163 + *
164 + * You should have received a copy of the GNU Library General Public
165 + * License along with this library; if not, write to the
166 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
167 + * Boston, MA 02111-1307, USA.
168 + */
169 +
170 +#include <gst/gst.h>
171 +#include "gstdrmutils.h"
172 +
173 +static int stored_enc = 0;
174 +static drmModeEncoder *enc;
175 +static struct plane_data *stored_plane;
176 +
177 +GST_DEBUG_CATEGORY_EXTERN (gst_debug_kms_sink);
178 +#define GST_CAT_DEFAULT gst_debug_kms_sink
179 +
180 +void
181 +gst_drm_connector_cleanup (int fd, struct connector *c)
182 +{
183 +  if (c->connector) {
184 +    drmModeFreeConnector (c->connector);
185 +    c->connector = NULL;
186 +  }
187 +
188 +  if (c->fb_id) {
189 +    drmModeRmFB (fd, c->fb_id);
190 +    c->fb_id = 0;
191 +  }
192 +  if (c->fb_bo) {
193 +    omap_bo_del (c->fb_bo);
194 +    c->fb_bo = NULL;
195 +  }
196 +}
197 +
198 +
199 +static gboolean
200 +gst_drm_connector_find_mode_and_plane_helper (int fd,
201 +    struct omap_device *dev, int width, int height,
202 +    drmModeRes * resources, drmModePlaneRes * plane_resources,
203 +    struct connector *c)
204 +{
205 +  int i, best_area = 0, ret;
206 +  struct drm_set_client_cap req;
207 +  unsigned int j;
208 +  int32_t crtc;
209 +
210 +  gst_drm_connector_cleanup (fd, c);
211 +
212 +  req.capability = DRM_CLIENT_CAP_ATOMIC;
213 +  req.value = 1;
214 +  ret = ioctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &req);
215 +  if(ret < 0) {
216 +    GST_DEBUG("drm set atomic cap failed");
217 +    goto fail;
218 +  }
219 +
220 +  /* First, find the connector & mode */
221 +  c->connector = drmModeGetConnector (fd, c->id);
222 +  if (!c->connector)
223 +    goto error_no_connector;
224 +
225 +  if (!c->connector->count_modes)
226 +    goto error_no_mode;
227 +
228 +  /* just look for the highest resolution: */
229 +  for (i = 0; i < c->connector->count_modes; i++) {
230 +    drmModeModeInfo *mode = &c->connector->modes[i];
231 +    int area = mode->hdisplay * mode->vdisplay;
232 +
233 +    if (area > best_area) {
234 +      c->mode = mode;
235 +      best_area = area;
236 +    }
237 +  }
238 +
239 +  if (c->mode == NULL) {
240 +    /* XXX: just pick the first available mode. Not sure this is correct... */
241 +    c->mode = &c->connector->modes[0];
242 +#if 0
243 +    goto error_no_mode;
244 +#endif
245 +  }
246 +
247 +  /* Now get the encoder */
248 +
249 +  if (stored_enc) {
250 +    c->encoder = enc;
251 +    c->connector->encoder_id = stored_enc;
252 +  } else {
253 +    c->encoder = drmModeGetEncoder (fd, c->connector->encoder_id);
254 +    enc = c->encoder;
255 +    stored_enc = c->connector->encoder_id;
256 +  }
257 +
258 +  if (!c->encoder) {
259 +    for (i = 0; i < c->connector->count_encoders; ++i) {
260 +       c->encoder = drmModeGetEncoder(fd, c->connector->encoders[i]);
261 +       if (!c->encoder) {
262 +          GST_DEBUG ("Cannot retrieve encoder %u:%u (%d): %m\n",
263 +              i, c->connector->encoders[i], errno);
264 +          continue;
265 +       }
266 +       /* iterate all global CRTCs */
267 +       for (j = 0; j < resources->count_crtcs; ++j) {
268 +          /* check whether this CRTC works with the encoder */
269 +          if (!(c->encoder->possible_crtcs & (1 << j)))
270 +             continue;
271 +          crtc = resources->crtcs[j];
272 +          break;
273 +       }
274 +      if (crtc >= 0) {
275 +         enc = c->encoder;
276 +         stored_enc = c->connector->encoder_id;
277 +         c->crtc = crtc;
278 +         goto found_encoder;
279 +      }
280 +    }
281 +  }
282 +
283 +found_encoder:
284 +
285 +  if (!c->encoder)
286 +    goto error_no_encoder;
287 +
288 +  if (c->crtc == -1)
289 +    c->crtc = c->encoder->crtc_id;
290 +
291 +  /* and figure out which crtc index it is: */
292 +  c->pipe = -1;
293 +  for (i = 0; i < resources->count_crtcs; i++) {
294 +    if (c->crtc == (int) resources->crtcs[i]) {
295 +      c->pipe = i;
296 +      break;
297 +    }
298 +  }
299 +
300 +  if (c->pipe == -1)
301 +    goto error_no_crtc;
302 +
303 +  if (stored_plane) {
304 +    c->pdata = stored_plane;
305 +  } else {
306 +
307 +    c->pdata = calloc(sizeof(struct plane_data), 1);
308 +    for (i = 0; i < plane_resources->count_planes; i++) {
309 +      drmModePlane *plane = drmModeGetPlane (fd, plane_resources->planes[i]);
310 +      int propc;
311 +      if (plane->possible_crtcs & (1 << c->pipe)) {
312 +        drmModeObjectPropertiesPtr props = drmModeObjectGetProperties(fd, plane_resources->planes[i], DRM_MODE_OBJECT_PLANE);
313 +        for(propc = 0; propc < props->count_props; propc++) {
314 +          drmModePropertyPtr prop = drmModeGetProperty(fd, props->props[propc]);
315 +          if(strcmp(prop->name, "FB_ID") == 0)
316 +            c->pdata[0].fb_id_property = props->props[propc];
317 +        }
318 +        c->pdata[0].plane = plane_resources->planes[i];
319 +        stored_plane = c->pdata;
320 +        break;
321 +      }
322 +    }
323 +    if (stored_plane == NULL)
324 +      goto error_no_plane;
325 +  }
326 +  c->fb_bo = omap_bo_new (dev, best_area * 2, OMAP_BO_WC);
327 +  if (c->fb_bo) {
328 +    uint32_t fourcc = DRM_FORMAT_RGB565;
329 +    uint32_t handles[4] = { omap_bo_handle (c->fb_bo) };
330 +    uint32_t pitches[4] = { c->mode->hdisplay * 2 };
331 +    uint32_t offsets[4] = { 0 };
332 +    ret = drmModeAddFB2 (fd, c->mode->hdisplay, c->mode->vdisplay,
333 +        fourcc, handles, pitches, offsets, &c->fb_id, 0);
334 +    if (ret) {
335 +      GST_DEBUG ("RGB565 AddFb2 failed");
336 +    }
337 +  }
338 +
339 +  /* now set the desired mode: */
340 +  ret = drmModeSetCrtc (fd, c->crtc, c->fb_id, 0, 0, &c->id, 1, c->mode);
341 +  if (ret) {
342 +    GST_DEBUG ("SetCrtc failed");
343 +  }
344 +
345 +  return TRUE;
346 +
347 +fail:
348 +  gst_drm_connector_cleanup (fd, c);
349 +
350 +  return FALSE;
351 +
352 +error_no_connector:
353 +  GST_DEBUG ("could not get connector %s", strerror (errno));
354 +  goto fail;
355 +
356 +error_no_mode:
357 +  GST_DEBUG ("could not find mode %dx%d (count_modes %d)",
358 +      width, height, c->connector->count_modes);
359 +  goto fail;
360 +
361 +error_no_encoder:
362 +  GST_DEBUG ("could not get encoder: %s", strerror (errno));
363 +  goto fail;
364 +
365 +error_no_crtc:
366 +  GST_DEBUG ("couldn't find a crtc");
367 +  goto fail;
368 +
369 +error_no_plane:
370 +  GST_DEBUG ("couldn't find a plane");
371 +  goto fail;
372 +}
373 +
374 +gboolean
375 +gst_drm_connector_find_mode_and_plane (int fd,
376 +    struct omap_device *dev, int width, int height,
377 +    drmModeRes * resources, drmModePlaneRes * plane_resources,
378 +    struct connector *c)
379 +{
380 +  int i;
381 +  gboolean found = FALSE;
382 +
383 +  /* First, find the connector & mode */
384 +  if (c->id == 0) {
385 +    /* Any connector */
386 +    GST_DEBUG ("Any connector, %d available", resources->count_connectors);
387 +    for (i = 0; i < resources->count_connectors; i++) {
388 +      GST_DEBUG ("  %d", resources->connectors[i]);
389 +    }
390 +    for (i = 0; i < resources->count_connectors; i++) {
391 +      GST_DEBUG ("Trying connector %d: %d", i, resources->connectors[i]);
392 +      c->id = resources->connectors[i];
393 +      if (gst_drm_connector_find_mode_and_plane_helper (fd, dev, width, height,
394 +              resources, plane_resources, c)) {
395 +        GST_DEBUG ("Found suitable connector");
396 +        found = TRUE;
397 +        break;
398 +      }
399 +      GST_DEBUG ("Connector not suitable");
400 +    }
401 +  } else {
402 +    /* A specific connector */
403 +    GST_DEBUG ("Connector %d", c->id);
404 +    found =
405 +        gst_drm_connector_find_mode_and_plane_helper (fd, dev, width, height,
406 +        resources, plane_resources, c);
407 +  }
408 +
409 +  return found;
410 +}
411 +
412 +/* table nicked off libdrm's modetest.c */
413 +/* *INDENT-OFF* */
414 +static const struct {
415 +  int type_id;
416 +  const char *type_name;
417 +} connector_type_names[] = {
418 +  { DRM_MODE_CONNECTOR_Unknown, "unknown" },
419 +  { DRM_MODE_CONNECTOR_VGA, "VGA" },
420 +  { DRM_MODE_CONNECTOR_DVII, "DVI-I" },
421 +  { DRM_MODE_CONNECTOR_DVID, "DVI-D" },
422 +  { DRM_MODE_CONNECTOR_DVIA, "DVI-A" },
423 +  { DRM_MODE_CONNECTOR_Composite, "composite" },
424 +  { DRM_MODE_CONNECTOR_SVIDEO, "s-video" },
425 +  { DRM_MODE_CONNECTOR_LVDS, "LVDS" },
426 +  { DRM_MODE_CONNECTOR_Component, "component" },
427 +  { DRM_MODE_CONNECTOR_9PinDIN, "9-pin-DIN" },
428 +  { DRM_MODE_CONNECTOR_DisplayPort, "displayport" },
429 +  { DRM_MODE_CONNECTOR_HDMIA, "HDMI-A" },
430 +  { DRM_MODE_CONNECTOR_HDMIB, "HDMI-B" },
431 +  { DRM_MODE_CONNECTOR_TV, "TV" },
432 +  { DRM_MODE_CONNECTOR_eDP, "embedded-displayport" },
433 +};
434 +/* *INDENT-ON* */
435 +
436 +gboolean
437 +gst_drm_connector_find_mode_and_plane_by_name (int fd,
438 +    struct omap_device * dev, int width, int height,
439 +    drmModeRes * resources, drmModePlaneRes * plane_resources,
440 +    struct connector * c, const char *name)
441 +{
442 +  int i, n;
443 +  char tmp[64];
444 +  const char *type_name;
445 +  int found[G_N_ELEMENTS (connector_type_names)] = { 0 };
446 +
447 +  /* Find connector from name */
448 +  for (i = 0; i < resources->count_connectors; i++) {
449 +    GST_DEBUG ("Trying connector %d: %d", i, resources->connectors[i]);
450 +    c->id = resources->connectors[i];
451 +    c->connector = drmModeGetConnector (fd, c->id);
452 +    if (!c->connector)
453 +      continue;
454 +
455 +    /* Find type name from this connector */
456 +    for (n = 0; n < G_N_ELEMENTS (connector_type_names); n++)
457 +      if (connector_type_names[n].type_id == c->connector->connector_type)
458 +        break;
459 +    if (n == G_N_ELEMENTS (connector_type_names))
460 +      continue;
461 +
462 +    type_name = connector_type_names[n].type_name;
463 +    GST_DEBUG ("Connector %d has type %s", i, type_name);
464 +    ++found[n];
465 +
466 +    drmModeFreeConnector (c->connector);
467 +    c->connector = NULL;
468 +
469 +    /* Try a few different matches, such as modetest and xrandr
470 +       output, and also a indexless one matching first found */
471 +    snprintf (tmp, sizeof (tmp), "%s-%u", type_name, found[n]);
472 +    if (!g_ascii_strcasecmp (tmp, name))
473 +      goto found;
474 +    snprintf (tmp, sizeof (tmp), "%s%u", type_name, found[n]);
475 +    if (!g_ascii_strcasecmp (tmp, name))
476 +      goto found;
477 +    if (!g_ascii_strcasecmp (name, type_name))
478 +      goto found;
479 +
480 +    continue;
481 +
482 +  found:
483 +    if (gst_drm_connector_find_mode_and_plane_helper (fd, dev, width, height,
484 +            resources, plane_resources, c)) {
485 +      GST_DEBUG ("Found suitable connector");
486 +      return TRUE;
487 +    }
488 +    GST_DEBUG ("Connector not suitable");
489 +  }
490 +
491 +  return FALSE;
492 +}
493 diff --git a/sys/kms/gstdrmutils.h b/sys/kms/gstdrmutils.h
494 new file mode 100644
495 index 0000000..ebc5fc6
496 --- /dev/null
497 +++ b/sys/kms/gstdrmutils.h
498 @@ -0,0 +1,50 @@
499 +#ifndef __GST_DRMUTILS_H__
500 +#define __GST_DRMUTILS_H__
501 +
502 +#include <fcntl.h>
503 +#include <xf86drm.h>
504 +#include <stdio.h>
505 +#include <stdint.h>
506 +#include <stdlib.h>
507 +#include <string.h>
508 +#include <errno.h>
509 +#include <unistd.h>
510 +#include <assert.h>
511 +#include <libdrm/drm.h>
512 +#include <libdrm/drm_mode.h>
513 +#include <xf86drmMode.h>
514 +#include <omap_drm.h>
515 +#include <omap_drmif.h>
516 +#include <drm_fourcc.h>
517 +#include <gst/gst.h>
518 +#include <sys/ioctl.h>
519 +
520 +struct plane_data {
521 +       int plane;
522 +       int fb_id_property;
523 +};
524 +
525 +struct connector {
526 +       uint32_t id;
527 +       char mode_str[64];
528 +       drmModeConnector *connector;
529 +       drmModeModeInfo *mode;
530 +       drmModeEncoder *encoder;
531 +       uint32_t fb_id;
532 +       struct omap_bo *fb_bo;
533 +       int crtc;
534 +       int pipe;
535 +        struct plane_data *pdata;
536 +};
537 +
538 +void gst_drm_connector_cleanup (int fd, struct connector * c);
539 +gboolean gst_drm_connector_find_mode_and_plane (int fd,
540 +    struct omap_device * dev, int width, int height,
541 +    drmModeRes * resources, drmModePlaneRes * plane_resources,
542 +    struct connector *c);
543 +gboolean gst_drm_connector_find_mode_and_plane_by_name (int fd,
544 +    struct omap_device *dev, int width, int height,
545 +    drmModeRes * resources, drmModePlaneRes * plane_resources,
546 +    struct connector *c, const char *name);
547 +
548 +#endif /* __GST_DRMUTILS_H__ */
549 diff --git a/sys/kms/gstkmsbufferpriv.c b/sys/kms/gstkmsbufferpriv.c
550 new file mode 100644
551 index 0000000..172a4c3
552 --- /dev/null
553 +++ b/sys/kms/gstkmsbufferpriv.c
554 @@ -0,0 +1,121 @@
555 +/*
556 + * GStreamer
557 + *
558 + * Copyright (C) 2012 Texas Instruments
559 + * Copyright (C) 2012 Collabora Ltd
560 + *
561 + * Authors:
562 + *  Alessandro Decina <alessandro.decina@collabora.co.uk>
563 + *  Rob Clark <rob.clark@linaro.org>
564 + *
565 + * This library is free software; you can redistribute it and/or
566 + * modify it under the terms of the GNU Lesser General Public
567 + * License as published by the Free Software Foundation
568 + * version 2.1 of the License.
569 + *
570 + * This library is distributed in the hope that it will be useful,
571 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
572 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
573 + * Lesser General Public License for more details.
574 + *
575 + * You should have received a copy of the GNU Lesser General Public
576 + * License along with this library; if not, write to the Free Software
577 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
578 + */
579 +
580 +#ifdef HAVE_CONFIG_H
581 +#include "config.h"
582 +#endif
583 +
584 +#include <stdint.h>
585 +#include <gst/gst.h>
586 +#include <gst/allocators/allocators.h>
587 +
588 +#include <omap_drm.h>
589 +#include <omap_drmif.h>
590 +#include <xf86drmMode.h>
591 +
592 +#include "gstkmssink.h"
593 +#include "gstkmsbufferpriv.h"
594 +
595 +static int
596 +create_fb (GstKMSBufferPriv * priv, GstKMSSink * sink)
597 +{
598 +  /* TODO get format, etc from caps.. and query device for
599 +   * supported formats, and make this all more flexible to
600 +   * cope with various formats:
601 +   */
602 +  uint32_t fourcc = GST_MAKE_FOURCC ('N', 'V', '1', '2');
603 +
604 +  uint32_t handles[4] = {
605 +    omap_bo_handle (priv->bo), omap_bo_handle (priv->bo),
606 +  };
607 +  uint32_t pitches[4] = {
608 +    GST_ROUND_UP_4 (sink->input_width), GST_ROUND_UP_4 (sink->input_width),
609 +  };
610 +  uint32_t offsets[4] = {
611 +    0, pitches[0] * sink->input_height
612 +  };
613 +
614 +  return drmModeAddFB2 (priv->fd, sink->input_width, sink->input_height,
615 +      fourcc, handles, pitches, offsets, &priv->fb_id, 0);
616 +}
617 +
618 +/**
619 + * gst_kms_buffer_priv:
620 + * @sink: a #GstKMSSink
621 + * @buf: a pointer to #GstBuffer
622 + *
623 + * Checks if the @buf has a GstMetaDmaBuf metadata set. If it doesn't we return a NULL
624 + * indicating its not a dmabuf buffer. We maintain a hashtable with dmabuf fd as key and 
625 + * the GstKMSBufferPriv structure as value
626 + *
627 + * Returns: the #GstKMSBufferPriv
628 + *
629 + * Since: 1.2.?
630 + */
631 +GstKMSBufferPriv *
632 +gst_kms_buffer_priv (GstKMSSink * sink, GstBuffer * buf)
633 +{
634 +    struct omap_bo *bo;
635 +    int fd;
636 +    int fd_copy;
637 +    GstKMSBufferPriv * priv;
638 +    GstMemory *mem;
639 +
640 +    /* if it isn't a dmabuf buffer that we can import, then there
641 +     * is nothing we can do with it:
642 +     */
643 +    mem = gst_buffer_peek_memory (buf, 0);
644 +    fd_copy = gst_fd_memory_get_fd (mem); 
645 +    if (fd_copy < 0) {
646 +      GST_DEBUG_OBJECT (sink, "not importing non dmabuf buffer");
647 +      return NULL;
648 +    }
649 +
650 +    /* lookup the hashtable with fd as key. If present return bo & buffer structure */
651 +    priv = g_hash_table_lookup (sink->kmsbufferpriv, (gpointer)fd_copy);
652 +    if(priv) {
653 +       return priv;
654 +     }
655 +
656 +    priv = g_malloc0 (sizeof (GstKMSBufferPriv));
657 +    bo = omap_bo_from_dmabuf (sink->dev, fd_copy);
658 +    fd = sink->fd;
659 +
660 +      priv->bo = bo;
661 +      priv->fd = fd;
662 +
663 +    if (create_fb (priv, sink)) {
664 +      GST_WARNING_OBJECT (sink, "could not create framebuffer: %s",
665 +          strerror (errno));
666 +      g_free(priv);
667 +      return NULL;
668 +    }
669 +
670 +    /* if fd not present, write to hash table fd and the corresponding priv. */
671 +    g_hash_table_insert(sink->kmsbufferpriv, (gpointer)fd_copy, priv); 
672 +   
673 +  
674 +  return priv;
675 +}
676 diff --git a/sys/kms/gstkmsbufferpriv.h b/sys/kms/gstkmsbufferpriv.h
677 new file mode 100644
678 index 0000000..a1070da
679 --- /dev/null
680 +++ b/sys/kms/gstkmsbufferpriv.h
681 @@ -0,0 +1,64 @@
682 +/*
683 + * GStreamer
684 + *
685 + * Copyright (C) 2012 Texas Instruments
686 + * Copyright (C) 2012 Collabora Ltd
687 + *
688 + * Authors:
689 + *  Alessandro Decina <alessandro.decina@collabora.co.uk>
690 + *  Rob Clark <rob.clark@linaro.org>
691 + *
692 + * This library is free software; you can redistribute it and/or
693 + * modify it under the terms of the GNU Lesser General Public
694 + * License as published by the Free Software Foundation
695 + * version 2.1 of the License.
696 + *
697 + * This library is distributed in the hope that it will be useful,
698 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
699 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
700 + * Lesser General Public License for more details.
701 + *
702 + * You should have received a copy of the GNU Lesser General Public
703 + * License along with this library; if not, write to the Free Software
704 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
705 + */
706 +
707 +#ifndef __GSTKMSBUFFERPRIV_H__
708 +#define __GSTKMSBUFFERPRIV_H__
709 +
710 +#include <stdint.h>
711 +#include <gst/gst.h>
712 +
713 +G_BEGIN_DECLS
714 +
715 +/*
716 + * per-buffer private data so kmssink can attach a drm_framebuffer
717 + * handle (fb_id) to a buffer, which gets deleted when the buffer
718 + * is finalized
719 + */
720 +
721 +#define GST_TYPE_KMS_BUFFER_PRIV      \
722 +  (gst_kms_buffer_priv_get_type ())
723 +#define GST_KMS_BUFFER_PRIV(obj)      \
724 +  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_KMS_BUFFER_PRIV, GstKMSBufferPriv))
725 +#define GST_IS_KMS_BUFFER_PRIV(obj)     \
726 +  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_KMS_BUFFER_PRIV))
727 +
728 +
729 +typedef struct
730 +{
731 +  struct omap_bo *bo;
732 +  int fd;
733 +  uint32_t fb_id;
734 +}GstKMSBufferPriv;
735 +
736 +
737 +GType gst_kms_buffer_priv_get_type (void);
738 +
739 +/* Returns a GstKMSBufferPriv, if it has a dmabuf fd metadata */
740 +GstKMSBufferPriv * gst_kms_buffer_priv (GstKMSSink *sink, GstBuffer * buf);
741 +
742 +G_END_DECLS
743 +
744 +
745 +#endif /* __GSTKMSBUFFERPRIV_H__ */
746 diff --git a/sys/kms/gstkmssink.c b/sys/kms/gstkmssink.c
747 new file mode 100644
748 index 0000000..17e6407
749 --- /dev/null
750 +++ b/sys/kms/gstkmssink.c
751 @@ -0,0 +1,740 @@
752 +/* GStreamer
753 + * Copyright (C) 2012 Texas Instruments
754 + * Copyright (C) 2012 Collabora Ltd
755 + *
756 + * Authors:
757 + *  Alessandro Decina <alessandro.decina@collabora.co.uk>
758 + *
759 + * This library is free software; you can redistribute it and/or
760 + * modify it under the terms of the GNU Library General Public
761 + * License as published by the Free Software Foundation; either
762 + * version 2 of the License, or (at your option) any later version.
763 + *
764 + * This library is distributed in the hope that it will be useful,
765 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
766 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
767 + * Library General Public License for more details.
768 + *
769 + * You should have received a copy of the GNU Library General Public
770 + * License along with this library; if not, write to the
771 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
772 + * Boston, MA 02111-1307, USA.
773 + *
774 + * Authors:
775 + *  Alessandro Decina <alessandro.decina@collabora.co.uk>
776 + */
777 +
778 +#ifdef HAVE_CONFIG_H
779 +#include "config.h"
780 +#endif
781 +
782 +#include "gstkmssink.h"
783 +#include "gstkmsbufferpriv.h"
784 +
785 +#include <libdce.h>
786 +#include <omap_drm.h>
787 +#include <omap_drmif.h>
788 +#include <xf86drmMode.h>
789 +
790 +static int drm_fd = -1;
791 +static struct omap_device *drm_dev;
792 +static int once =1;
793 +
794 +GST_DEBUG_CATEGORY (gst_debug_kms_sink);
795 +#define GST_CAT_DEFAULT gst_debug_kms_sink
796 +
797 +G_DEFINE_TYPE (GstKMSSink, gst_kms_sink, GST_TYPE_VIDEO_SINK);
798 +
799 +static void gst_kms_sink_reset (GstKMSSink * sink);
800 +
801 +static GstStaticPadTemplate gst_kms_sink_template_factory =
802 +GST_STATIC_PAD_TEMPLATE ("sink",
803 +    GST_PAD_SINK,
804 +    GST_PAD_ALWAYS,
805 +    GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE("NV12"))
806 +    );
807 +
808 +enum
809 +{
810 +  PROP_0,
811 +  PROP_PIXEL_ASPECT_RATIO,
812 +  PROP_FORCE_ASPECT_RATIO,
813 +  PROP_SCALE,
814 +  PROP_CONNECTOR,
815 +  PROP_CONNECTOR_NAME,
816 +};
817 +
818 +
819 +static inline void
820 +display_bufs_queue (GstKMSSink * sink, GstBuffer * buf)
821 +{
822 +  int i;
823 +  for (i = 0; i < (NUM_DISPLAY_BUFS - 1); i++)
824 +    gst_buffer_replace (&sink->display_bufs[i], sink->display_bufs[i + 1]);
825 +  gst_buffer_replace (&sink->display_bufs[i], buf);
826 +}
827 +
828 +static inline void
829 +display_bufs_free (GstKMSSink * sink)
830 +{
831 +  int i;
832 +  for (i = 0; i < NUM_DISPLAY_BUFS; i++)
833 +    gst_buffer_replace (&sink->display_bufs[i], NULL);
834 +}
835 +
836 +static gboolean
837 +gst_kms_sink_calculate_aspect_ratio (GstKMSSink * sink, gint width,
838 +    gint height, gint video_par_n, gint video_par_d)
839 +{
840 +  guint calculated_par_n;
841 +  guint calculated_par_d;
842 +
843 +  if (!gst_video_calculate_display_ratio (&calculated_par_n, &calculated_par_d,
844 +          width, height, video_par_n, video_par_d, 1, 1)) {
845 +    GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION, (NULL),
846 +        ("Error calculating the output display ratio of the video."));
847 +    return FALSE;
848 +  }
849 +  GST_DEBUG_OBJECT (sink,
850 +      "video width/height: %dx%d, calculated display ratio: %d/%d",
851 +      width, height, calculated_par_n, calculated_par_d);
852 +
853 +  /* now find a width x height that respects this display ratio.
854 +   * prefer those that have one of w/h the same as the incoming video
855 +   * using wd / hd = calculated_pad_n / calculated_par_d */
856 +
857 +  /* start with same height, because of interlaced video */
858 +  /* check hd / calculated_par_d is an integer scale factor, and scale wd with the PAR */
859 +  if (height % calculated_par_d == 0) {
860 +    GST_DEBUG_OBJECT (sink, "keeping video height");
861 +    GST_VIDEO_SINK_WIDTH (sink) = (guint)
862 +        gst_util_uint64_scale_int (height, calculated_par_n, calculated_par_d);
863 +    GST_VIDEO_SINK_HEIGHT (sink) = height;
864 +  } else if (width % calculated_par_n == 0) {
865 +    GST_DEBUG_OBJECT (sink, "keeping video width");
866 +    GST_VIDEO_SINK_WIDTH (sink) = width;
867 +    GST_VIDEO_SINK_HEIGHT (sink) = (guint)
868 +        gst_util_uint64_scale_int (width, calculated_par_d, calculated_par_n);
869 +  } else {
870 +    GST_DEBUG_OBJECT (sink, "approximating while keeping video height");
871 +    GST_VIDEO_SINK_WIDTH (sink) = (guint)
872 +        gst_util_uint64_scale_int (height, calculated_par_n, calculated_par_d);
873 +    GST_VIDEO_SINK_HEIGHT (sink) = height;
874 +  }
875 +  GST_DEBUG_OBJECT (sink, "scaling to %dx%d",
876 +      GST_VIDEO_SINK_WIDTH (sink), GST_VIDEO_SINK_HEIGHT (sink));
877 +
878 +  return TRUE;
879 +}
880 +
881 +static gboolean
882 +gst_kms_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
883 +{
884 +  GstKMSSink *sink;
885 +  gboolean ret = TRUE;
886 +  gint width, height;
887 +  gint fps_n, fps_d;
888 +  gint par_n, par_d;
889 +  GstVideoFormat format;
890 +  GstVideoInfo info;
891 +  GstStructure *conf;
892 +  GstStructure *s;
893 +  int size;
894 +
895 +  sink = GST_KMS_SINK (bsink);
896 +
897 +  ret = gst_video_info_from_caps (&info, caps);
898 +  format = GST_VIDEO_INFO_FORMAT(&info);
899 +  width = GST_VIDEO_INFO_WIDTH(&info);
900 +  height = GST_VIDEO_INFO_HEIGHT(&info);
901 +  fps_n = GST_VIDEO_INFO_FPS_N(&info);
902 +  fps_d = GST_VIDEO_INFO_FPS_D(&info);
903 +  par_n = GST_VIDEO_INFO_PAR_N(&info);
904 +  par_d = GST_VIDEO_INFO_PAR_D(&info);
905 +
906 +  if (!ret)
907 +    return FALSE;
908 +
909 +  if (width <= 0 || height <= 0) {
910 +    GST_ELEMENT_ERROR (sink, CORE, NEGOTIATION, (NULL),
911 +        ("Invalid image size."));
912 +    return FALSE;
913 +  }
914 +
915 +  sink->format = format;
916 +  sink->par_n = par_n;
917 +  sink->par_d = par_d;
918 +  sink->src_rect.x = sink->src_rect.y = 0;
919 +  sink->src_rect.w = width;
920 +  sink->src_rect.h = height;
921 +  sink->input_width = width;
922 +  sink->input_height = height;
923 +  size = info.size;
924 +
925 +  if (!sink->pool) {
926 +    GstAllocator *allocator;
927 +
928 +    allocator = gst_drm_allocator_get ();
929 +    sink->pool = gst_buffer_pool_new ();
930 +    conf = gst_buffer_pool_get_config (GST_BUFFER_POOL(sink->pool));
931 +    gst_buffer_pool_config_set_params (conf, caps, size, 0, 0);
932 +    gst_buffer_pool_config_set_allocator (conf, allocator, NULL);
933 +    gst_buffer_pool_set_config (GST_BUFFER_POOL(sink->pool), conf);
934 +    if (allocator)
935 +      gst_object_unref (allocator);
936 +  }
937 +
938 +  sink->conn.crtc = -1;
939 +  return TRUE;
940 +}
941 +
942 +static void
943 +gst_kms_sink_get_times (GstBaseSink * bsink, GstBuffer * buf,
944 +    GstClockTime * start, GstClockTime * end)
945 +{
946 +  GstKMSSink *sink;
947 +
948 +  sink = GST_KMS_SINK (bsink);
949 +
950 +  if (GST_BUFFER_PTS_IS_VALID (buf)) {
951 +    *start = GST_BUFFER_PTS (buf);
952 +    if (GST_BUFFER_DURATION_IS_VALID (buf)) {
953 +      *end = *start + GST_BUFFER_DURATION (buf);
954 +    } else {
955 +      if (sink->fps_n > 0) {
956 +        *end = *start +
957 +            gst_util_uint64_scale_int (GST_SECOND, sink->fps_d, sink->fps_n);
958 +      }
959 +    }
960 +  }
961 +}
962 +
963 +
964 +static void page_flip_handler(int fd, unsigned int frame,
965 +                  unsigned int sec, unsigned int usec, void *data)
966 +{
967 +        int *waiting_for_flip = data;
968 +        *waiting_for_flip = 0;
969 +}
970 +
971 +
972 +static GstFlowReturn
973 +gst_kms_sink_show_frame (GstVideoSink * vsink, GstBuffer * inbuf)
974 +{
975 +  GstKMSSink *sink = GST_KMS_SINK (vsink);
976 +  GstBuffer *buf = NULL;
977 +  GstKMSBufferPriv *priv;
978 +  GstFlowReturn flow_ret = GST_FLOW_OK;
979 +  int ret = 0;
980 +  gint width, height;
981 +  GstVideoRectangle *c = &sink->src_rect;
982 +  int waiting_for_flip = 1;
983 +
984 +  fd_set fds;
985 +  drmEventContext evctx = {
986 +                          .version = DRM_EVENT_CONTEXT_VERSION,
987 +                          .vblank_handler = 0,
988 +                          .page_flip_handler = page_flip_handler,
989 +                           };
990 +
991 +  g_mutex_lock (&sink->render_lock);
992 +  GstVideoCropMeta* crop = gst_buffer_get_video_crop_meta (inbuf);
993 +  if (crop){
994 +    c->y = crop->y;
995 +    c->x = crop->x;
996 +
997 +    if (crop->width >= 0) {
998 +      width = crop->width;
999 +    } else {
1000 +      width = sink->input_width;
1001 +    }
1002 +    if (crop->height >= 0){
1003 +      height = crop->height;
1004 +    } else {
1005 +      height = sink->input_height;
1006 +    }
1007 +  } else {
1008 +    width = sink->input_width;
1009 +    height = sink->input_height;
1010 +  }
1011 +
1012 +  c->w = width;
1013 +  c->h = height;
1014 +
1015 +
1016 +  if (!gst_kms_sink_calculate_aspect_ratio (sink, width, height,
1017 +              sink->par_n, sink->par_d))
1018 +    GST_DEBUG_OBJECT (sink, "calculate aspect ratio failed");
1019 +
1020 +
1021 +  GST_INFO_OBJECT (sink, "enter");
1022 +
1023 +  if (sink->conn.crtc == -1) {
1024 +    if (sink->conn_name) {
1025 +      if (!gst_drm_connector_find_mode_and_plane_by_name (sink->fd,
1026 +              sink->dev, sink->src_rect.w, sink->src_rect.h,
1027 +              sink->resources, sink->plane_resources, &sink->conn,
1028 +              sink->conn_name))
1029 +        goto connector_not_found;
1030 +    } else {
1031 +      sink->conn.id = sink->conn_id;
1032 +      if (!gst_drm_connector_find_mode_and_plane (sink->fd,
1033 +              sink->dev, sink->src_rect.w, sink->src_rect.h,
1034 +              sink->resources, sink->plane_resources, &sink->conn))
1035 +        goto connector_not_found;
1036 +    }
1037 +   once = 1;
1038 +  }
1039 +
1040 +  priv = gst_kms_buffer_priv (sink, inbuf);
1041 +
1042 +  if (priv) {
1043 +    buf = inbuf;
1044 +  } else {
1045 +    GST_LOG_OBJECT (sink, "not a KMS buffer, slow-path!");
1046 +    gst_buffer_pool_acquire_buffer (sink->pool, &buf, NULL);
1047 +    if (buf) {
1048 +      GST_BUFFER_PTS (buf) = GST_BUFFER_PTS (inbuf);
1049 +      GST_BUFFER_DURATION (buf) = GST_BUFFER_DURATION (inbuf);
1050 +      gst_buffer_copy_into (buf, inbuf, GST_BUFFER_COPY_DEEP, 0 ,-1);
1051 +      priv = gst_kms_buffer_priv (sink, buf);
1052 +    }
1053 +    if (!priv)
1054 +      goto add_fb2_failed;
1055 +  }
1056 +  
1057 + if (once) {
1058 +    once = 0;
1059 +    static  GstVideoRectangle dest = { 0 };
1060 +    dest.w = sink->conn.mode->hdisplay;
1061 +    dest.h = sink->conn.mode->vdisplay;
1062 +
1063 +    gst_video_sink_center_rect (sink->src_rect, dest, &sink->dst_rect,
1064 +        sink->scale);
1065 +    ret = drmModeSetPlane (sink->fd, sink->conn.pdata[0].plane,
1066 +        sink->conn.crtc, priv->fb_id, 0,
1067 +        sink->dst_rect.x, sink->dst_rect.y, sink->dst_rect.w, sink->dst_rect.h,
1068 +        sink->src_rect.x << 16, sink->src_rect.y << 16,
1069 +        sink->src_rect.w << 16, sink->src_rect.h << 16);
1070 +    if (ret)
1071 +      goto set_plane_failed;
1072 +  }
1073 +
1074 +  drmModeAtomicReqPtr m_req = drmModeAtomicAlloc();
1075 +
1076 +  drmModeAtomicAddProperty(m_req, sink->conn.pdata[0].plane,
1077 +                       sink->conn.pdata[0].fb_id_property,
1078 +                       priv->fb_id);
1079 +
1080 +  drmModeAtomicCommit(sink->fd, m_req, DRM_MODE_ATOMIC_TEST_ONLY, 0);
1081 +  drmModeAtomicCommit(sink->fd, m_req, DRM_MODE_PAGE_FLIP_EVENT | DRM_MODE_ATOMIC_NONBLOCK, &waiting_for_flip);
1082 +  drmModeAtomicFree(m_req);
1083 +
1084 +  while (waiting_for_flip) {
1085 +    FD_ZERO(&fds);
1086 +    FD_SET(sink->fd, &fds);
1087 +    int err;
1088 +    err = select(sink->fd + 1, &fds, NULL, NULL, NULL);
1089 +    if (err < 0) {
1090 +      GST_ERROR_OBJECT (sink,"select err: %s\n", strerror(errno));
1091 +      flow_ret = GST_FLOW_ERROR;
1092 +      goto out;
1093 +    }
1094 +    if (FD_ISSET(sink->fd, &fds)) {
1095 +      drmHandleEvent(sink->fd, &evctx);
1096 +    }
1097 +  }
1098 +
1099 +  display_bufs_queue (sink, buf);
1100 +
1101 +out:
1102 +  GST_INFO_OBJECT (sink, "exit");
1103 +  if (buf != inbuf)
1104 +    gst_buffer_unref (buf);
1105 +  g_mutex_unlock (&sink->render_lock);
1106 +  return flow_ret;
1107 +
1108 +add_fb2_failed:
1109 +  GST_ELEMENT_ERROR (sink, RESOURCE, FAILED,
1110 +      (NULL), ("drmModeAddFB2 failed: %s (%d)", strerror (errno), errno));
1111 +  flow_ret = GST_FLOW_ERROR;
1112 +  goto out;
1113 +
1114 +set_plane_failed:
1115 +  GST_ELEMENT_ERROR (sink, RESOURCE, FAILED,
1116 +      (NULL), ("drmModeSetPlane failed: %s (%d)", strerror (errno), errno));
1117 +  flow_ret = GST_FLOW_ERROR;
1118 +  goto out;
1119 +
1120 +connector_not_found:
1121 +  GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND,
1122 +      (NULL), ("connector not found", strerror (errno), errno));
1123 +  goto out;
1124 +}
1125 +
1126 +
1127 +static gboolean
1128 +gst_kms_sink_event (GstBaseSink * bsink, GstEvent * event)
1129 +{
1130 +  GstKMSSink *sink = GST_KMS_SINK (bsink);
1131 +
1132 +  switch (GST_EVENT_TYPE (event)) {
1133 +    default:
1134 +      break;
1135 +  }
1136 +  if (GST_BASE_SINK_CLASS (gst_kms_sink_parent_class)->event)
1137 +    return GST_BASE_SINK_CLASS (gst_kms_sink_parent_class)->event (bsink,
1138 +        event);
1139 +  else
1140 +    return TRUE;
1141 +}
1142 +
1143 +static void
1144 +gst_kms_sink_set_property (GObject * object, guint prop_id,
1145 +    const GValue * value, GParamSpec * pspec)
1146 +{
1147 +  GstKMSSink *sink;
1148 +
1149 +  g_return_if_fail (GST_IS_KMS_SINK (object));
1150 +
1151 +  sink = GST_KMS_SINK (object);
1152 +
1153 +  switch (prop_id) {
1154 +    case PROP_FORCE_ASPECT_RATIO:
1155 +      sink->keep_aspect = g_value_get_boolean (value);
1156 +      break;
1157 +    case PROP_SCALE:
1158 +      sink->scale = g_value_get_boolean (value);
1159 +      break;
1160 +    case PROP_CONNECTOR:
1161 +      sink->conn_id = g_value_get_uint (value);
1162 +      break;
1163 +    case PROP_CONNECTOR_NAME:
1164 +      g_free (sink->conn_name);
1165 +      sink->conn_name = g_strdup (g_value_get_string (value));
1166 +      break;
1167 +    case PROP_PIXEL_ASPECT_RATIO:
1168 +    {
1169 +      GValue *tmp;
1170 +
1171 +      tmp = g_new0 (GValue, 1);
1172 +      g_value_init (tmp, GST_TYPE_FRACTION);
1173 +
1174 +      if (!g_value_transform (value, tmp)) {
1175 +        GST_WARNING_OBJECT (sink, "Could not transform string to aspect ratio");
1176 +      } else {
1177 +        sink->par_n = gst_value_get_fraction_numerator (tmp);
1178 +        sink->par_d = gst_value_get_fraction_denominator (tmp);
1179 +        GST_DEBUG_OBJECT (sink, "set PAR to %d/%d", sink->par_n, sink->par_d);
1180 +      }
1181 +      g_free (tmp);
1182 +    }
1183 +      break;
1184 +    default:
1185 +      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1186 +      break;
1187 +  }
1188 +}
1189 +
1190 +static void
1191 +gst_kms_sink_get_property (GObject * object, guint prop_id,
1192 +    GValue * value, GParamSpec * pspec)
1193 +{
1194 +  GstKMSSink *sink;
1195 +
1196 +  g_return_if_fail (GST_IS_KMS_SINK (object));
1197 +
1198 +  sink = GST_KMS_SINK (object);
1199 +
1200 +  switch (prop_id) {
1201 +    case PROP_FORCE_ASPECT_RATIO:
1202 +      g_value_set_boolean (value, sink->keep_aspect);
1203 +      break;
1204 +    case PROP_SCALE:
1205 +      g_value_set_boolean (value, sink->scale);
1206 +      break;
1207 +    case PROP_CONNECTOR:
1208 +      g_value_set_uint (value, sink->conn.id);
1209 +      break;
1210 +    case PROP_PIXEL_ASPECT_RATIO:
1211 +    {
1212 +      char *v = g_strdup_printf ("%d/%d", sink->par_n, sink->par_d);
1213 +      g_value_take_string (value, v);
1214 +      break;
1215 +    }
1216 +    default:
1217 +      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1218 +      break;
1219 +  }
1220 +}
1221 +
1222 +static void
1223 +gst_kms_sink_reset (GstKMSSink * sink)
1224 +{
1225 +  GST_DEBUG_OBJECT (sink, "reset");
1226 +
1227 +  if (sink->fd != -1) {
1228 +    gst_drm_connector_cleanup (sink->fd, &sink->conn);
1229 +  }
1230 +  memset (&sink->conn, 0, sizeof (struct connector));
1231 +
1232 +  display_bufs_free (sink);
1233 +  
1234 + if (sink->pool) {
1235 +    gst_buffer_pool_set_active (GST_BUFFER_POOL(sink->pool), FALSE);
1236 +    gst_object_unref(sink->pool);
1237 +    sink->pool = NULL;
1238 +  }
1239 +
1240 +  if (sink->plane_resources) {
1241 +    drmModeFreePlaneResources (sink->plane_resources);
1242 +    sink->plane_resources = NULL;
1243 +  }
1244 +
1245 +  if (sink->resources) {
1246 +    drmModeFreeResources (sink->resources);
1247 +    sink->resources = NULL;
1248 +  }
1249 +
1250 +  sink->par_n = sink->par_d = 1;
1251 +  sink->src_rect.x = 0;
1252 +  sink->src_rect.y = 0;
1253 +  sink->src_rect.w = 0;
1254 +  sink->src_rect.h = 0;
1255 +  sink->input_width = 0;
1256 +  sink->input_height = 0;
1257 +  sink->format = GST_VIDEO_FORMAT_UNKNOWN;
1258 +
1259 +  memset (&sink->src_rect, 0, sizeof (GstVideoRectangle));
1260 +  memset (&sink->dst_rect, 0, sizeof (GstVideoRectangle));
1261 +}
1262 +
1263 +static gboolean
1264 +gst_kms_sink_start (GstBaseSink * bsink)
1265 +{
1266 +  GstKMSSink *sink;
1267 +
1268 +  sink = GST_KMS_SINK (bsink);
1269 +
1270 +  drm_dev = dce_init ();
1271 +  if (drm_dev == NULL)
1272 +    goto device_failed;
1273 +  else {
1274 +    sink->dev = drm_dev;
1275 +    sink->fd = dce_get_fd ();
1276 +    drm_fd = dce_get_fd ();
1277 +  }
1278 +
1279 +  sink->resources = drmModeGetResources (sink->fd);
1280 +  if (sink->resources == NULL)
1281 +    goto resources_failed;
1282 +
1283 +  sink->plane_resources = drmModeGetPlaneResources (sink->fd);
1284 +  if (sink->plane_resources == NULL)
1285 +    goto plane_resources_failed;
1286 +
1287 +  return TRUE;
1288 +
1289 +fail:
1290 +  gst_kms_sink_reset (sink);
1291 +  return FALSE;
1292 +
1293 +device_failed:
1294 +  GST_ELEMENT_ERROR (sink, RESOURCE, FAILED,
1295 +      (NULL), ("omap_device_new failed"));
1296 +  goto fail;
1297 +
1298 +resources_failed:
1299 +  GST_ELEMENT_ERROR (sink, RESOURCE, FAILED,
1300 +      (NULL), ("drmModeGetResources failed: %s (%d)", strerror (errno), errno));
1301 +  goto fail;
1302 +
1303 +plane_resources_failed:
1304 +  GST_ELEMENT_ERROR (sink, RESOURCE, FAILED,
1305 +      (NULL), ("drmModeGetPlaneResources failed: %s (%d)",
1306 +          strerror (errno), errno));
1307 +  goto fail;
1308 +}
1309 +
1310 +static gboolean
1311 +gst_kms_sink_stop (GstBaseSink * bsink)
1312 +{
1313 +  GstKMSSink *sink;
1314 +
1315 +  sink = GST_KMS_SINK (bsink);
1316 +  gst_kms_sink_reset (sink);
1317 +
1318 +  return TRUE;
1319 +}
1320 +
1321 +
1322 +static gboolean
1323 +gst_kms_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1324 +{
1325 +  GstKMSSink *sink;
1326 +  GstStructure *conf;
1327 +  GstCaps *caps;
1328 +  guint size;
1329 +  gboolean need_pool;
1330 +  GstStructure *s;
1331 +  int num_buffers = 0;
1332 +
1333 +
1334 +  sink = GST_KMS_SINK (bsink);
1335 +
1336 +  GST_DEBUG_OBJECT (sink, "begin");
1337 +
1338 +  gst_query_parse_allocation (query, &caps, &need_pool);
1339 +
1340 +  if (G_UNLIKELY (!caps)) {
1341 +    GST_WARNING_OBJECT (sink, "have no caps, doing fallback allocation");
1342 +    return FALSE;
1343 +  }
1344 +
1345 +  if (need_pool) {
1346 +    GstVideoInfo info;
1347 +  
1348 +    if (!gst_video_info_from_caps (&info, caps))
1349 +      goto invalid_caps;
1350 +
1351 +    GST_LOG_OBJECT (sink,
1352 +        "a bufferpool was requested with caps %" GST_PTR_FORMAT, caps);
1353 +
1354 +    /* We already have a pool after set_caps */
1355 +    if (sink->pool) {
1356 +      GstStructure *config;
1357 +      int min,max;
1358 +      config = gst_buffer_pool_get_config (sink->pool);
1359 +      gst_buffer_pool_config_get_params (config, NULL, &size, &min, &max);
1360 +      gst_structure_free (config);
1361 +    
1362 +      gst_query_add_allocation_pool (query, sink->pool, size, min, max);
1363 +      gst_query_add_allocation_param (query, gst_drm_allocator_get (), NULL);
1364 +      return TRUE;
1365 +   } else {
1366 +     GST_LOG_OBJECT (sink, "No bufferpool available");
1367 +     return FALSE;
1368 +   }
1369 +  }
1370
1371 +
1372 +invalid_caps:
1373 +  GST_DEBUG_OBJECT (sink, "invalid caps specified");
1374 +  return FALSE;
1375 +}
1376 +
1377 +static void
1378 +gst_kms_sink_finalize (GObject * object)
1379 +{
1380 +  GstKMSSink *sink;
1381 +
1382 +  sink = GST_KMS_SINK (object);
1383 +  g_mutex_clear (&sink->render_lock);
1384 +  g_free (sink->conn_name);
1385 +  if (sink->kmsbufferpriv){
1386 +    g_hash_table_destroy (sink->kmsbufferpriv);
1387 +    sink->kmsbufferpriv = NULL;
1388 +  gst_kms_sink_reset (sink);
1389 +}
1390 +
1391 +  G_OBJECT_CLASS (gst_kms_sink_parent_class)->finalize (object);
1392 +}
1393 +
1394 +static void
1395 +kmsbufferpriv_free_func (GstKMSBufferPriv *priv)
1396 +{
1397 +  drmModeRmFB (priv->fd, priv->fb_id);
1398 +  omap_bo_del (priv->bo);
1399 +  g_free(priv);
1400 +}
1401 +
1402 +
1403 +static void
1404 +gst_kms_sink_init (GstKMSSink * sink)
1405 +{
1406 +  sink->fd = -1;
1407 +  gst_kms_sink_reset (sink);
1408 +  sink->kmsbufferpriv = g_hash_table_new_full (g_direct_hash, g_direct_equal,
1409 +      NULL, (GDestroyNotify) kmsbufferpriv_free_func);
1410 +  g_mutex_init (&sink->render_lock);
1411 +}
1412 +
1413 +static void
1414 +gst_kms_sink_class_init (GstKMSSinkClass * klass)
1415 +{
1416 +  GObjectClass *gobject_class;
1417 +  GstElementClass *gstelement_class;
1418 +  GstBaseSinkClass *gstbasesink_class;
1419 +  GstVideoSinkClass *videosink_class;
1420 +
1421 +  gobject_class = (GObjectClass *) klass;
1422 +  gstelement_class = (GstElementClass *) klass;
1423 +  gstbasesink_class = (GstBaseSinkClass *) klass;
1424 +  videosink_class = (GstVideoSinkClass *) klass;
1425 +
1426 +  gobject_class->finalize = gst_kms_sink_finalize;
1427 +  gobject_class->set_property = gst_kms_sink_set_property;
1428 +  gobject_class->get_property = gst_kms_sink_get_property;
1429 +
1430 +  g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
1431 +      g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
1432 +          "When enabled, reverse caps negotiation (scaling) will respect "
1433 +          "original aspect ratio", FALSE,
1434 +          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1435 +  g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
1436 +      g_param_spec_string ("pixel-aspect-ratio", "Pixel Aspect Ratio",
1437 +          "The pixel aspect ratio of the device", "1/1",
1438 +          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1439 +  g_object_class_install_property (gobject_class, PROP_SCALE,
1440 +      g_param_spec_boolean ("scale", "Scale",
1441 +          "When true, scale to render fullscreen", FALSE,
1442 +          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1443 +  g_object_class_install_property (gobject_class, PROP_CONNECTOR,
1444 +      g_param_spec_uint ("connector", "Connector",
1445 +          "DRM connector id (0 for automatic selection)", 0, G_MAXUINT32, 0,
1446 +          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_CONSTRUCT));
1447 +  g_object_class_install_property (gobject_class, PROP_CONNECTOR_NAME,
1448 +      g_param_spec_string ("connector-name", "Connector name",
1449 +          "DRM connector name (alternative to the connector property, "
1450 +          "use $type$index, $type-$index, or $type)", "",
1451 +          G_PARAM_WRITABLE | G_PARAM_STATIC_STRINGS));
1452 +
1453 +  gst_element_class_set_details_simple (gstelement_class,
1454 +      "Video sink", "Sink/Video",
1455 +      "A video sink using the linux kernel mode setting API",
1456 +      "Alessandro Decina <alessandro.d@gmail.com>");
1457 +
1458 +  gst_element_class_add_pad_template (gstelement_class,
1459 +      gst_static_pad_template_get (&gst_kms_sink_template_factory));
1460 +
1461 +  gstbasesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_kms_sink_setcaps);
1462 +  gstbasesink_class->get_times = GST_DEBUG_FUNCPTR (gst_kms_sink_get_times);
1463 +  gstbasesink_class->event = GST_DEBUG_FUNCPTR (gst_kms_sink_event);
1464 +  gstbasesink_class->start = GST_DEBUG_FUNCPTR (gst_kms_sink_start);
1465 +  gstbasesink_class->stop = GST_DEBUG_FUNCPTR (gst_kms_sink_stop);
1466 +  gstbasesink_class->propose_allocation = GST_DEBUG_FUNCPTR (gst_kms_sink_propose_allocation);
1467 +
1468 +  /* disable preroll as it's called before GST_CROP_EVENT has been received, so
1469 +   * we end up configuring the wrong mode... (based on padded caps)
1470 +   */
1471 +  gstbasesink_class->preroll = NULL;
1472 +  videosink_class->show_frame = GST_DEBUG_FUNCPTR (gst_kms_sink_show_frame);
1473 +}
1474 +
1475 +static gboolean
1476 +plugin_init (GstPlugin * plugin)
1477 +{
1478 +  if (!gst_element_register (plugin, "kmssink",
1479 +          GST_RANK_PRIMARY + 1, GST_TYPE_KMS_SINK))
1480 +    return FALSE;
1481 +
1482 +  GST_DEBUG_CATEGORY_INIT (gst_debug_kms_sink, "kmssink", 0, "kmssink element");
1483 +
1484 +  return TRUE;
1485 +}
1486 +
1487 +GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
1488 +    GST_VERSION_MINOR,
1489 +    kms,
1490 +    "KMS video output element",
1491 +    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
1492 diff --git a/sys/kms/gstkmssink.h b/sys/kms/gstkmssink.h
1493 new file mode 100644
1494 index 0000000..9f76839
1495 --- /dev/null
1496 +++ b/sys/kms/gstkmssink.h
1497 @@ -0,0 +1,92 @@
1498 +/* GStreamer
1499 + *
1500 + * Copyright (C) 2012 Texas Instruments 
1501 + * Copyright (C) 2012 Collabora Ltd
1502 + *
1503 + * Authors:
1504 + *  Alessandro Decina <alessandro.decina@collabora.co.uk>
1505 + *
1506 + * This library is free software; you can redistribute it and/or
1507 + * modify it under the terms of the GNU Library General Public
1508 + * License as published by the Free Software Foundation; either
1509 + * version 2 of the License, or (at your option) any later version.
1510 + *
1511 + * This library is distributed in the hope that it will be useful,
1512 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1513 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1514 + * Library General Public License for more details.
1515 + *
1516 + * You should have received a copy of the GNU Library General Public
1517 + * License along with this library; if not, write to the
1518 + * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
1519 + * Boston, MA 02111-1307, USA.
1520 + */
1521 +
1522 +#ifndef __GST_KMS_SINK_H__
1523 +#define __GST_KMS_SINK_H__
1524 +
1525 +#include <gst/video/video.h>
1526 +#include <gst/video/gstvideosink.h>
1527 +#include <gst/drm/gstdrmallocator.h>
1528 +
1529 +#include <stdio.h>
1530 +#include <stdint.h>
1531 +#include <stdlib.h>
1532 +#include <string.h>
1533 +#include <errno.h>
1534 +#include <unistd.h>
1535 +#include <assert.h>
1536 +
1537 +#include "gstdrmutils.h"
1538 +
1539 +G_BEGIN_DECLS
1540 +#define GST_TYPE_KMS_SINK \
1541 +  (gst_kms_sink_get_type())
1542 +#define GST_KMS_SINK(obj) \
1543 +  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_KMS_SINK, GstKMSSink))
1544 +#define GST_KMS_SINK_CLASS(klass) \
1545 +  (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_KMS_SINK, GstKMSSinkClass))
1546 +#define GST_IS_KMS_SINK(obj) \
1547 +  (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_KMS_SINK))
1548 +#define GST_IS_KMS_SINK_CLASS(klass) \
1549 +  (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_KMS_SINK))
1550 +typedef struct _GstKMSSink GstKMSSink;
1551 +typedef struct _GstKMSSinkClass GstKMSSinkClass;
1552 +
1553 +#define NUM_DISPLAY_BUFS 1
1554 +
1555 +struct _GstKMSSink
1556 +{
1557 +  GstVideoSink videosink;
1558 +  gint input_width, input_height;
1559 +  GstVideoFormat format;
1560 +  gint par_n, par_d;
1561 +  gint fps_n, fps_d;
1562 +  gboolean keep_aspect;
1563 +  GstVideoRectangle src_rect;
1564 +  GstVideoRectangle dst_rect;
1565 +  int fd;
1566 +  struct omap_device *dev;
1567 +  drmModeRes *resources;
1568 +  drmModePlaneRes *plane_resources;
1569 +  struct connector conn;
1570 +  uint32_t conn_id;
1571 +  char *conn_name;
1572 +  drmModePlane *plane;
1573 +  GstBufferPool *pool;
1574 +  GHashTable *kmsbufferpriv;
1575 +  /* current displayed buffer and last displayed buffer: */
1576 +  GstBuffer *display_bufs[NUM_DISPLAY_BUFS];
1577 +  gboolean scale;
1578 +  GMutex render_lock;
1579 +};
1580 +
1581 +struct _GstKMSSinkClass
1582 +{
1583 +  GstVideoSinkClass parent_class;
1584 +};
1585 +
1586 +GType gst_kms_sink_get_type (void);
1587 +
1588 +G_END_DECLS
1589 +#endif /* __GST_KMS_SINK_H__ */
1590 -- 
1591 1.9.1
1592