gtk: Fix race between queue_draw and destroy
[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   /* Pending queued idles callback */
74   guint draw_id;
75   guint resize_id;
76 };
77
78 static void
79 gtk_gst_widget_get_preferred_width (GtkWidget * widget, gint * min,
80     gint * natural)
81 {
82   GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
83   gint video_width = gst_widget->priv->display_width;
84
85   if (!gst_widget->priv->negotiated)
86     video_width = 10;
87
88   if (min)
89     *min = 1;
90   if (natural)
91     *natural = video_width;
92 }
93
94 static void
95 gtk_gst_widget_get_preferred_height (GtkWidget * widget, gint * min,
96     gint * natural)
97 {
98   GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
99   gint video_height = gst_widget->priv->display_height;
100
101   if (!gst_widget->priv->negotiated)
102     video_height = 10;
103
104   if (min)
105     *min = 1;
106   if (natural)
107     *natural = video_height;
108 }
109
110 static gboolean
111 gtk_gst_widget_draw (GtkWidget * widget, cairo_t * cr)
112 {
113   GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
114   guint widget_width, widget_height;
115   cairo_surface_t *surface;
116   GstVideoFrame frame;
117
118   widget_width = gtk_widget_get_allocated_width (widget);
119   widget_height = gtk_widget_get_allocated_height (widget);
120
121   g_mutex_lock (&gst_widget->priv->lock);
122
123   /* failed to map the video frame */
124   if (gst_widget->priv->negotiated && gst_widget->priv->buffer
125       && gst_video_frame_map (&frame, &gst_widget->priv->v_info,
126           gst_widget->priv->buffer, GST_MAP_READ)) {
127     gdouble scale_x = (gdouble) widget_width / gst_widget->priv->display_width;
128     gdouble scale_y =
129         (gdouble) widget_height / gst_widget->priv->display_height;
130     GstVideoRectangle result;
131     cairo_format_t format;
132
133     gst_widget->priv->v_info = frame.info;
134     if (frame.info.finfo->format == GST_VIDEO_FORMAT_ARGB ||
135         frame.info.finfo->format == GST_VIDEO_FORMAT_BGRA) {
136       format = CAIRO_FORMAT_ARGB32;
137     } else {
138       format = CAIRO_FORMAT_RGB24;
139     }
140
141     surface = cairo_image_surface_create_for_data (frame.data[0],
142         format, frame.info.width, frame.info.height, frame.info.stride[0]);
143
144     if (gst_widget->priv->force_aspect_ratio) {
145       GstVideoRectangle src, dst;
146
147       src.x = 0;
148       src.y = 0;
149       src.w = gst_widget->priv->display_width;
150       src.h = gst_widget->priv->display_height;
151
152       dst.x = 0;
153       dst.y = 0;
154       dst.w = widget_width;
155       dst.h = widget_height;
156
157       gst_video_sink_center_rect (src, dst, &result, TRUE);
158
159       scale_x = scale_y = MIN (scale_x, scale_y);
160     } else {
161       result.x = 0;
162       result.y = 0;
163       result.w = widget_width;
164       result.h = widget_height;
165     }
166
167     if (gst_widget->priv->ignore_alpha) {
168       GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
169
170       gdk_cairo_set_source_rgba (cr, &color);
171       if (result.x > 0) {
172         cairo_rectangle (cr, 0, 0, result.x, widget_height);
173         cairo_fill (cr);
174       }
175       if (result.y > 0) {
176         cairo_rectangle (cr, 0, 0, widget_width, result.y);
177         cairo_fill (cr);
178       }
179       if (result.w < widget_width) {
180         cairo_rectangle (cr, result.x + result.w, 0, widget_width - result.w,
181             widget_height);
182         cairo_fill (cr);
183       }
184       if (result.h < widget_height) {
185         cairo_rectangle (cr, 0, result.y + result.h, widget_width,
186             widget_height - result.h);
187         cairo_fill (cr);
188       }
189     }
190
191     scale_x *=
192         (gdouble) gst_widget->priv->display_width / (gdouble) frame.info.width;
193     scale_y *=
194         (gdouble) gst_widget->priv->display_height /
195         (gdouble) frame.info.height;
196
197     cairo_translate (cr, result.x, result.y);
198     cairo_scale (cr, scale_x, scale_y);
199     cairo_rectangle (cr, 0, 0, result.w, result.h);
200     cairo_set_source_surface (cr, surface, 0, 0);
201     cairo_paint (cr);
202
203     cairo_surface_destroy (surface);
204
205     gst_video_frame_unmap (&frame);
206   } else {
207     GdkRGBA color;
208
209     if (gst_widget->priv->ignore_alpha) {
210       color.red = color.blue = color.green = 0.0;
211       color.alpha = 1.0;
212     } else {
213       gtk_style_context_get_color (gtk_widget_get_style_context (widget),
214           GTK_STATE_FLAG_NORMAL, &color);
215     }
216     gdk_cairo_set_source_rgba (cr, &color);
217     cairo_rectangle (cr, 0, 0, widget_width, widget_height);
218     cairo_fill (cr);
219   }
220
221   g_mutex_unlock (&gst_widget->priv->lock);
222   return FALSE;
223 }
224
225 static void
226 gtk_gst_widget_finalize (GObject * object)
227 {
228   GtkGstWidget *widget = GTK_GST_WIDGET_CAST (object);
229
230   gst_buffer_replace (&widget->priv->buffer, NULL);
231   g_mutex_clear (&widget->priv->lock);
232
233   if (widget->priv->draw_id)
234     g_source_remove (widget->priv->draw_id);
235
236   if (widget->priv->resize_id)
237     g_source_remove (widget->priv->resize_id);
238
239   G_OBJECT_CLASS (gtk_gst_widget_parent_class)->finalize (object);
240 }
241
242 static void
243 gtk_gst_widget_set_property (GObject * object, guint prop_id,
244     const GValue * value, GParamSpec * pspec)
245 {
246   GtkGstWidget *gtk_widget = GTK_GST_WIDGET (object);
247
248   switch (prop_id) {
249     case PROP_FORCE_ASPECT_RATIO:
250       gtk_widget->priv->force_aspect_ratio = g_value_get_boolean (value);
251       break;
252     case PROP_PIXEL_ASPECT_RATIO:
253       gtk_widget->priv->par_n = gst_value_get_fraction_numerator (value);
254       gtk_widget->priv->par_d = gst_value_get_fraction_denominator (value);
255       break;
256     case PROP_IGNORE_ALPHA:
257       gtk_widget->priv->ignore_alpha = g_value_get_boolean (value);
258       break;
259     default:
260       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261       break;
262   }
263 }
264
265 static void
266 gtk_gst_widget_get_property (GObject * object, guint prop_id, GValue * value,
267     GParamSpec * pspec)
268 {
269   GtkGstWidget *gtk_widget = GTK_GST_WIDGET (object);
270
271   switch (prop_id) {
272     case PROP_FORCE_ASPECT_RATIO:
273       g_value_set_boolean (value, gtk_widget->priv->force_aspect_ratio);
274       break;
275     case PROP_PIXEL_ASPECT_RATIO:
276       gst_value_set_fraction (value, gtk_widget->priv->par_n,
277           gtk_widget->priv->par_d);
278       break;
279     case PROP_IGNORE_ALPHA:
280       g_value_set_boolean (value, gtk_widget->priv->ignore_alpha);
281       break;
282     default:
283       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284       break;
285   }
286 }
287
288 static void
289 gtk_gst_widget_class_init (GtkGstWidgetClass * klass)
290 {
291   GObjectClass *gobject_klass = (GObjectClass *) klass;
292   GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
293
294   g_type_class_add_private (klass, sizeof (GtkGstWidgetPrivate));
295
296   gobject_klass->set_property = gtk_gst_widget_set_property;
297   gobject_klass->get_property = gtk_gst_widget_get_property;
298   gobject_klass->finalize = gtk_gst_widget_finalize;
299
300   g_object_class_install_property (gobject_klass, PROP_FORCE_ASPECT_RATIO,
301       g_param_spec_boolean ("force-aspect-ratio",
302           "Force aspect ratio",
303           "When enabled, scaling will respect original aspect ratio",
304           DEFAULT_FORCE_ASPECT_RATIO,
305           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
306
307   g_object_class_install_property (gobject_klass, PROP_PIXEL_ASPECT_RATIO,
308       gst_param_spec_fraction ("pixel-aspect-ratio", "Pixel Aspect Ratio",
309           "The pixel aspect ratio of the device", DEFAULT_PAR_N, DEFAULT_PAR_D,
310           G_MAXINT, 1, 1, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
311
312   g_object_class_install_property (gobject_klass, PROP_IGNORE_ALPHA,
313       g_param_spec_boolean ("ignore-alpha", "Ignore Alpha",
314           "When enabled, alpha will be ignored and converted to black",
315           DEFAULT_IGNORE_ALPHA, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
316
317   widget_klass->draw = gtk_gst_widget_draw;
318   widget_klass->get_preferred_width = gtk_gst_widget_get_preferred_width;
319   widget_klass->get_preferred_height = gtk_gst_widget_get_preferred_height;
320 }
321
322 static void
323 gtk_gst_widget_init (GtkGstWidget * widget)
324 {
325   widget->priv = GTK_GST_WIDGET_GET_PRIVATE (widget);
326
327   widget->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
328   widget->priv->par_n = DEFAULT_PAR_N;
329   widget->priv->par_d = DEFAULT_PAR_D;
330   widget->priv->ignore_alpha = DEFAULT_IGNORE_ALPHA;
331
332   g_mutex_init (&widget->priv->lock);
333 }
334
335 GtkWidget *
336 gtk_gst_widget_new (void)
337 {
338   return (GtkWidget *) g_object_new (GTK_TYPE_GST_WIDGET, NULL);
339 }
340
341 static gboolean
342 _queue_draw (GtkGstWidget * widget)
343 {
344   g_mutex_lock (&widget->priv->lock);
345   widget->priv->draw_id = 0;
346   g_mutex_unlock (&widget->priv->lock);
347
348   gtk_widget_queue_draw (GTK_WIDGET (widget));
349
350   return G_SOURCE_REMOVE;
351 }
352
353 void
354 gtk_gst_widget_set_buffer (GtkGstWidget * widget, GstBuffer * buffer)
355 {
356   g_return_if_fail (GTK_IS_GST_WIDGET (widget));
357   g_return_if_fail (buffer == NULL || widget->priv->negotiated);
358
359   g_mutex_lock (&widget->priv->lock);
360
361   gst_buffer_replace (&widget->priv->buffer, buffer);
362
363   if (!widget->priv->draw_id) {
364     widget->priv->draw_id = g_idle_add_full (G_PRIORITY_DEFAULT,
365         (GSourceFunc) _queue_draw, widget, NULL);
366   }
367
368   g_mutex_unlock (&widget->priv->lock);
369 }
370
371 static gboolean
372 _queue_resize (GtkGstWidget * widget)
373 {
374   g_mutex_lock (&widget->priv->lock);
375   widget->priv->resize_id = 0;
376   g_mutex_unlock (&widget->priv->lock);
377
378   gtk_widget_queue_resize (GTK_WIDGET (widget));
379
380   return G_SOURCE_REMOVE;
381 }
382
383 static gboolean
384 _calculate_par (GtkGstWidget * widget, GstVideoInfo * info)
385 {
386   gboolean ok;
387   gint width, height;
388   gint par_n, par_d;
389   gint display_par_n, display_par_d;
390   guint display_ratio_num, display_ratio_den;
391
392   width = GST_VIDEO_INFO_WIDTH (info);
393   height = GST_VIDEO_INFO_HEIGHT (info);
394
395   par_n = GST_VIDEO_INFO_PAR_N (info);
396   par_d = GST_VIDEO_INFO_PAR_D (info);
397
398   if (!par_n)
399     par_n = 1;
400
401   /* get display's PAR */
402   if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
403     display_par_n = widget->priv->par_n;
404     display_par_d = widget->priv->par_d;
405   } else {
406     display_par_n = 1;
407     display_par_d = 1;
408   }
409
410   ok = gst_video_calculate_display_ratio (&display_ratio_num,
411       &display_ratio_den, width, height, par_n, par_d, display_par_n,
412       display_par_d);
413
414   if (!ok)
415     return FALSE;
416
417   GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
418
419   if (height % display_ratio_den == 0) {
420     GST_DEBUG ("keeping video height");
421     widget->priv->display_width = (guint)
422         gst_util_uint64_scale_int (height, display_ratio_num,
423         display_ratio_den);
424     widget->priv->display_height = height;
425   } else if (width % display_ratio_num == 0) {
426     GST_DEBUG ("keeping video width");
427     widget->priv->display_width = width;
428     widget->priv->display_height = (guint)
429         gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
430   } else {
431     GST_DEBUG ("approximating while keeping video height");
432     widget->priv->display_width = (guint)
433         gst_util_uint64_scale_int (height, display_ratio_num,
434         display_ratio_den);
435     widget->priv->display_height = height;
436   }
437   GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
438       widget->priv->display_height);
439
440   return TRUE;
441 }
442
443 gboolean
444 gtk_gst_widget_set_caps (GtkGstWidget * widget, GstCaps * caps)
445 {
446   GstVideoInfo v_info;
447
448   g_return_val_if_fail (GTK_IS_GST_WIDGET (widget), FALSE);
449   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
450   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
451
452   if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
453     return TRUE;
454
455   if (!gst_video_info_from_caps (&v_info, caps))
456     return FALSE;
457
458   /* FIXME: support other formats */
459 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
460   g_return_val_if_fail (GST_VIDEO_INFO_FORMAT (&v_info) ==
461       GST_VIDEO_FORMAT_BGRA || GST_VIDEO_INFO_FORMAT (&v_info) ==
462       GST_VIDEO_FORMAT_BGRx, FALSE);
463 #else
464   g_return_val_if_fail (GST_VIDEO_INFO_FORMAT (&v_info) ==
465       GST_VIDEO_FORMAT_ARGB || GST_VIDEO_INFO_FORMAT (&v_info) ==
466       GST_VIDEO_FORMAT_xRGB, FALSE);
467 #endif
468
469   g_mutex_lock (&widget->priv->lock);
470
471   if (!_calculate_par (widget, &v_info)) {
472     g_mutex_unlock (&widget->priv->lock);
473     return FALSE;
474   }
475
476   gst_buffer_replace (&widget->priv->buffer, NULL);
477   gst_caps_replace (&widget->priv->caps, caps);
478   widget->priv->v_info = v_info;
479   widget->priv->negotiated = TRUE;
480
481   if (!widget->priv->resize_id) {
482     widget->priv->resize_id = g_idle_add_full (G_PRIORITY_DEFAULT,
483         (GSourceFunc) _queue_resize, widget, NULL);
484   }
485
486   g_mutex_unlock (&widget->priv->lock);
487
488   return TRUE;
489 }