gtksink: Add support for xRGB/BGRx
[platform/upstream/gst-plugins-good.git] / ext / gtk / gtkgstwidget.c
1 /*
2  * GStreamer
3  * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26
27 #include "gtkgstwidget.h"
28 #include <gst/video/video.h>
29
30 /**
31  * SECTION:gtkgstwidget
32  * @short_description: a #GtkWidget that renders GStreamer video #GstBuffers
33  * @see_also: #GtkDrawingArea, #GstBuffer
34  *
35  * #GtkGstWidget is an #GtkWidget that renders GStreamer video buffers.
36  */
37
38 G_DEFINE_TYPE (GtkGstWidget, gtk_gst_widget, GTK_TYPE_DRAWING_AREA);
39
40 #define GTK_GST_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
41     GTK_TYPE_GST_WIDGET, GtkGstWidgetPrivate))
42
43 #define DEFAULT_FORCE_ASPECT_RATIO  TRUE
44 #define DEFAULT_PAR_N               0
45 #define DEFAULT_PAR_D               1
46 #define DEFAULT_IGNORE_ALPHA        TRUE
47
48 enum
49 {
50   PROP_0,
51   PROP_FORCE_ASPECT_RATIO,
52   PROP_PIXEL_ASPECT_RATIO,
53   PROP_IGNORE_ALPHA,
54 };
55
56 struct _GtkGstWidgetPrivate
57 {
58   GMutex lock;
59
60   /* properties */
61   gboolean force_aspect_ratio;
62   gint par_n, par_d;
63   gboolean ignore_alpha;
64
65   gint display_width;
66   gint display_height;
67
68   gboolean negotiated;
69   GstBuffer *buffer;
70   GstCaps *caps;
71   GstVideoInfo v_info;
72 };
73
74 static void
75 gtk_gst_widget_get_preferred_width (GtkWidget * widget, gint * min,
76     gint * natural)
77 {
78   GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
79   gint video_width = GST_VIDEO_INFO_WIDTH (&gst_widget->priv->v_info);
80
81   if (!gst_widget->priv->negotiated)
82     video_width = 10;
83
84   if (min)
85     *min = 1;
86   if (natural)
87     *natural = video_width;
88 }
89
90 static void
91 gtk_gst_widget_get_preferred_height (GtkWidget * widget, gint * min,
92     gint * natural)
93 {
94   GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
95   gint video_height = GST_VIDEO_INFO_HEIGHT (&gst_widget->priv->v_info);
96
97   if (!gst_widget->priv->negotiated)
98     video_height = 10;
99
100   if (min)
101     *min = 1;
102   if (natural)
103     *natural = video_height;
104 }
105
106 static gboolean
107 gtk_gst_widget_draw (GtkWidget * widget, cairo_t * cr)
108 {
109   GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
110   guint widget_width, widget_height;
111   cairo_surface_t *surface;
112   GstVideoFrame frame;
113
114   widget_width = gtk_widget_get_allocated_width (widget);
115   widget_height = gtk_widget_get_allocated_height (widget);
116
117   g_mutex_lock (&gst_widget->priv->lock);
118
119   /* failed to map the video frame */
120   if (gst_widget->priv->negotiated && gst_widget->priv->buffer
121       && gst_video_frame_map (&frame, &gst_widget->priv->v_info,
122           gst_widget->priv->buffer, GST_MAP_READ)) {
123     gdouble scale_x = (gdouble) widget_width / gst_widget->priv->display_width;
124     gdouble scale_y =
125         (gdouble) widget_height / gst_widget->priv->display_height;
126     GstVideoRectangle result;
127     cairo_format_t format;
128
129     gst_widget->priv->v_info = frame.info;
130     if (frame.info.finfo->format == GST_VIDEO_FORMAT_ARGB ||
131         frame.info.finfo->format == GST_VIDEO_FORMAT_BGRA) {
132       format = CAIRO_FORMAT_ARGB32;
133     } else {
134       format = CAIRO_FORMAT_RGB24;
135     }
136
137     surface = cairo_image_surface_create_for_data (frame.data[0],
138         format, frame.info.width, frame.info.height, frame.info.stride[0]);
139
140     if (gst_widget->priv->force_aspect_ratio) {
141       GstVideoRectangle src, dst;
142
143       src.x = 0;
144       src.y = 0;
145       src.w = gst_widget->priv->display_width;
146       src.h = gst_widget->priv->display_height;
147
148       dst.x = 0;
149       dst.y = 0;
150       dst.w = widget_width;
151       dst.h = widget_height;
152
153       gst_video_sink_center_rect (src, dst, &result, TRUE);
154
155       scale_x = scale_y = MIN (scale_x, scale_y);
156     } else {
157       result.x = 0;
158       result.y = 0;
159       result.w = widget_width;
160       result.h = widget_height;
161     }
162
163     if (gst_widget->priv->ignore_alpha) {
164       GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
165
166       gdk_cairo_set_source_rgba (cr, &color);
167       if (result.x > 0) {
168         cairo_rectangle (cr, 0, 0, result.x, widget_height);
169         cairo_fill (cr);
170       }
171       if (result.y > 0) {
172         cairo_rectangle (cr, 0, 0, widget_width, result.y);
173         cairo_fill (cr);
174       }
175       if (result.w < widget_width) {
176         cairo_rectangle (cr, result.x + result.w, 0, widget_width - result.w,
177             widget_height);
178         cairo_fill (cr);
179       }
180       if (result.h < widget_height) {
181         cairo_rectangle (cr, 0, result.y + result.h, widget_width,
182             widget_height - result.h);
183         cairo_fill (cr);
184       }
185     }
186
187     scale_x *=
188         (gdouble) gst_widget->priv->display_width / (gdouble) frame.info.width;
189     scale_y *=
190         (gdouble) gst_widget->priv->display_height /
191         (gdouble) frame.info.height;
192
193     cairo_translate (cr, result.x, result.y);
194     cairo_scale (cr, scale_x, scale_y);
195     cairo_rectangle (cr, 0, 0, result.w, result.h);
196     cairo_set_source_surface (cr, surface, 0, 0);
197     cairo_paint (cr);
198
199     cairo_surface_destroy (surface);
200
201     gst_video_frame_unmap (&frame);
202   } else {
203     GdkRGBA color;
204
205     if (gst_widget->priv->ignore_alpha) {
206       color.red = color.blue = color.green = 0.0;
207       color.alpha = 1.0;
208     } else {
209       gtk_style_context_get_color (gtk_widget_get_style_context (widget),
210           GTK_STATE_FLAG_NORMAL, &color);
211     }
212     gdk_cairo_set_source_rgba (cr, &color);
213     cairo_rectangle (cr, 0, 0, widget_width, widget_height);
214     cairo_fill (cr);
215   }
216
217   g_mutex_unlock (&gst_widget->priv->lock);
218   return FALSE;
219 }
220
221 static void
222 gtk_gst_widget_finalize (GObject * object)
223 {
224   GtkGstWidget *widget = GTK_GST_WIDGET_CAST (object);
225
226   gst_buffer_replace (&widget->priv->buffer, NULL);
227   g_mutex_clear (&widget->priv->lock);
228
229   G_OBJECT_CLASS (gtk_gst_widget_parent_class)->finalize (object);
230 }
231
232 static void
233 gtk_gst_widget_set_property (GObject * object, guint prop_id,
234     const GValue * value, GParamSpec * pspec)
235 {
236   GtkGstWidget *gtk_widget = GTK_GST_WIDGET (object);
237
238   switch (prop_id) {
239     case PROP_FORCE_ASPECT_RATIO:
240       gtk_widget->priv->force_aspect_ratio = g_value_get_boolean (value);
241       break;
242     case PROP_PIXEL_ASPECT_RATIO:
243       gtk_widget->priv->par_n = gst_value_get_fraction_numerator (value);
244       gtk_widget->priv->par_d = gst_value_get_fraction_denominator (value);
245       break;
246     case PROP_IGNORE_ALPHA:
247       gtk_widget->priv->ignore_alpha = g_value_get_boolean (value);
248       break;
249     default:
250       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
251       break;
252   }
253 }
254
255 static void
256 gtk_gst_widget_get_property (GObject * object, guint prop_id, GValue * value,
257     GParamSpec * pspec)
258 {
259   GtkGstWidget *gtk_widget = GTK_GST_WIDGET (object);
260
261   switch (prop_id) {
262     case PROP_FORCE_ASPECT_RATIO:
263       g_value_set_boolean (value, gtk_widget->priv->force_aspect_ratio);
264       break;
265     case PROP_PIXEL_ASPECT_RATIO:
266       gst_value_set_fraction (value, gtk_widget->priv->par_n,
267           gtk_widget->priv->par_d);
268     case PROP_IGNORE_ALPHA:
269       g_value_set_boolean (value, gtk_widget->priv->ignore_alpha);
270       break;
271     default:
272       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
273       break;
274   }
275 }
276
277 static void
278 gtk_gst_widget_class_init (GtkGstWidgetClass * klass)
279 {
280   GObjectClass *gobject_klass = (GObjectClass *) klass;
281   GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
282
283   g_type_class_add_private (klass, sizeof (GtkGstWidgetPrivate));
284
285   gobject_klass->set_property = gtk_gst_widget_set_property;
286   gobject_klass->get_property = gtk_gst_widget_get_property;
287   gobject_klass->finalize = gtk_gst_widget_finalize;
288
289   g_object_class_install_property (gobject_klass, PROP_FORCE_ASPECT_RATIO,
290       g_param_spec_boolean ("force-aspect-ratio",
291           "Force aspect ratio",
292           "When enabled, scaling will respect original aspect ratio",
293           DEFAULT_FORCE_ASPECT_RATIO,
294           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295
296   g_object_class_install_property (gobject_klass, PROP_PIXEL_ASPECT_RATIO,
297       gst_param_spec_fraction ("pixel-aspect-ratio", "Pixel Aspect Ratio",
298           "The pixel aspect ratio of the device", DEFAULT_PAR_N, DEFAULT_PAR_D,
299           G_MAXINT, 1, 1, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300
301   g_object_class_install_property (gobject_klass, PROP_IGNORE_ALPHA,
302       g_param_spec_boolean ("ignore-alpha", "Ignore Alpha",
303           "When enabled, alpha will be ignored and converted to black",
304           DEFAULT_IGNORE_ALPHA, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
305
306   widget_klass->draw = gtk_gst_widget_draw;
307   widget_klass->get_preferred_width = gtk_gst_widget_get_preferred_width;
308   widget_klass->get_preferred_height = gtk_gst_widget_get_preferred_height;
309 }
310
311 static void
312 gtk_gst_widget_init (GtkGstWidget * widget)
313 {
314   widget->priv = GTK_GST_WIDGET_GET_PRIVATE (widget);
315
316   widget->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
317   widget->priv->par_n = DEFAULT_PAR_N;
318   widget->priv->par_d = DEFAULT_PAR_D;
319   widget->priv->ignore_alpha = DEFAULT_IGNORE_ALPHA;
320
321   g_mutex_init (&widget->priv->lock);
322 }
323
324 GtkWidget *
325 gtk_gst_widget_new (void)
326 {
327   return (GtkWidget *) g_object_new (GTK_TYPE_GST_WIDGET, NULL);
328 }
329
330 static gboolean
331 _queue_draw (GtkGstWidget * widget)
332 {
333   gtk_widget_queue_draw (GTK_WIDGET (widget));
334
335   return G_SOURCE_REMOVE;
336 }
337
338 void
339 gtk_gst_widget_set_buffer (GtkGstWidget * widget, GstBuffer * buffer)
340 {
341   GMainContext *main_context = g_main_context_default ();
342
343   g_return_if_fail (GTK_IS_GST_WIDGET (widget));
344   g_return_if_fail (widget->priv->negotiated);
345
346   g_mutex_lock (&widget->priv->lock);
347
348   gst_buffer_replace (&widget->priv->buffer, buffer);
349
350   g_mutex_unlock (&widget->priv->lock);
351
352   g_main_context_invoke (main_context, (GSourceFunc) _queue_draw, widget);
353 }
354
355 static gboolean
356 _queue_resize (GtkGstWidget * widget)
357 {
358   gtk_widget_queue_resize (GTK_WIDGET (widget));
359
360   return G_SOURCE_REMOVE;
361 }
362
363 static gboolean
364 _calculate_par (GtkGstWidget * widget, GstVideoInfo * info)
365 {
366   gboolean ok;
367   gint width, height;
368   gint par_n, par_d;
369   gint display_par_n, display_par_d;
370   guint display_ratio_num, display_ratio_den;
371
372   width = GST_VIDEO_INFO_WIDTH (info);
373   height = GST_VIDEO_INFO_HEIGHT (info);
374
375   par_n = GST_VIDEO_INFO_PAR_N (info);
376   par_d = GST_VIDEO_INFO_PAR_D (info);
377
378   if (!par_n)
379     par_n = 1;
380
381   /* get display's PAR */
382   if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
383     display_par_n = widget->priv->par_n;
384     display_par_d = widget->priv->par_d;
385   } else {
386     display_par_n = 1;
387     display_par_d = 1;
388   }
389
390   ok = gst_video_calculate_display_ratio (&display_ratio_num,
391       &display_ratio_den, width, height, par_n, par_d, display_par_n,
392       display_par_d);
393
394   if (!ok)
395     return FALSE;
396
397   GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
398
399   if (height % display_ratio_den == 0) {
400     GST_DEBUG ("keeping video height");
401     widget->priv->display_width = (guint)
402         gst_util_uint64_scale_int (height, display_ratio_num,
403         display_ratio_den);
404     widget->priv->display_height = height;
405   } else if (width % display_ratio_num == 0) {
406     GST_DEBUG ("keeping video width");
407     widget->priv->display_width = width;
408     widget->priv->display_height = (guint)
409         gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
410   } else {
411     GST_DEBUG ("approximating while keeping video height");
412     widget->priv->display_width = (guint)
413         gst_util_uint64_scale_int (height, display_ratio_num,
414         display_ratio_den);
415     widget->priv->display_height = height;
416   }
417   GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
418       widget->priv->display_height);
419
420   return TRUE;
421 }
422
423 gboolean
424 gtk_gst_widget_set_caps (GtkGstWidget * widget, GstCaps * caps)
425 {
426   GMainContext *main_context = g_main_context_default ();
427   GstVideoInfo v_info;
428
429   g_return_val_if_fail (GTK_IS_GST_WIDGET (widget), FALSE);
430   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
431   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
432
433   if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
434     return TRUE;
435
436   if (!gst_video_info_from_caps (&v_info, caps))
437     return FALSE;
438
439   /* FIXME: support other formats */
440 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
441   g_return_val_if_fail (GST_VIDEO_INFO_FORMAT (&v_info) ==
442       GST_VIDEO_FORMAT_BGRA || GST_VIDEO_INFO_FORMAT (&v_info) ==
443       GST_VIDEO_FORMAT_BGRx, FALSE);
444 #else
445   g_return_val_if_fail (GST_VIDEO_INFO_FORMAT (&v_info) ==
446       GST_VIDEO_FORMAT_ARGB || GST_VIDEO_INFO_FORMAT (&v_info) ==
447       GST_VIDEO_FORMAT_xRGB, FALSE);
448 #endif
449
450   g_mutex_lock (&widget->priv->lock);
451
452   if (!_calculate_par (widget, &v_info)) {
453     g_mutex_unlock (&widget->priv->lock);
454     return FALSE;
455   }
456
457   gst_caps_replace (&widget->priv->caps, caps);
458   widget->priv->v_info = v_info;
459   widget->priv->negotiated = TRUE;
460
461   g_mutex_unlock (&widget->priv->lock);
462
463   gtk_widget_queue_resize (GTK_WIDGET (widget));
464
465   g_main_context_invoke (main_context, (GSourceFunc) _queue_resize, widget);
466
467   return TRUE;
468 }