[v4l2videodecoder] Support TBM for output buffer
[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  * @title: GtkGstWidget
33  * @short_description: a #GtkWidget that renders GStreamer video #GstBuffers
34  * @see_also: #GtkDrawingArea, #GstBuffer
35  *
36  * #GtkGstWidget is an #GtkWidget that renders GStreamer video buffers.
37  */
38
39 G_DEFINE_TYPE (GtkGstWidget, gtk_gst_widget, GTK_TYPE_DRAWING_AREA);
40
41 static gboolean
42 gtk_gst_widget_draw (GtkWidget * widget, cairo_t * cr)
43 {
44   GtkGstBaseWidget *gst_widget = (GtkGstBaseWidget *) widget;
45   guint widget_width, widget_height;
46   cairo_surface_t *surface;
47   GstVideoFrame frame;
48
49   widget_width = gtk_widget_get_allocated_width (widget);
50   widget_height = gtk_widget_get_allocated_height (widget);
51
52   GTK_GST_BASE_WIDGET_LOCK (gst_widget);
53
54   /* There is not much to optimize in term of redisplay, so simply swap the
55    * pending_buffer with the active buffer */
56   if (gst_widget->pending_buffer) {
57     if (gst_widget->buffer)
58       gst_buffer_unref (gst_widget->buffer);
59     gst_widget->buffer = gst_widget->pending_buffer;
60     gst_widget->pending_buffer = NULL;
61   }
62
63   /* failed to map the video frame */
64   if (gst_widget->negotiated && gst_widget->buffer
65       && gst_video_frame_map (&frame, &gst_widget->v_info,
66           gst_widget->buffer, GST_MAP_READ)) {
67     gdouble scale_x = (gdouble) widget_width / gst_widget->display_width;
68     gdouble scale_y = (gdouble) widget_height / gst_widget->display_height;
69     GstVideoRectangle result;
70     cairo_format_t format;
71
72     gst_widget->v_info = frame.info;
73     if (frame.info.finfo->format == GST_VIDEO_FORMAT_ARGB ||
74         frame.info.finfo->format == GST_VIDEO_FORMAT_BGRA) {
75       format = CAIRO_FORMAT_ARGB32;
76     } else {
77       format = CAIRO_FORMAT_RGB24;
78     }
79
80     surface = cairo_image_surface_create_for_data (frame.data[0],
81         format, frame.info.width, frame.info.height, frame.info.stride[0]);
82
83     if (gst_widget->force_aspect_ratio) {
84       GstVideoRectangle src, dst;
85
86       src.x = 0;
87       src.y = 0;
88       src.w = gst_widget->display_width;
89       src.h = gst_widget->display_height;
90
91       dst.x = 0;
92       dst.y = 0;
93       dst.w = widget_width;
94       dst.h = widget_height;
95
96       gst_video_sink_center_rect (src, dst, &result, TRUE);
97
98       scale_x = scale_y = MIN (scale_x, scale_y);
99     } else {
100       result.x = 0;
101       result.y = 0;
102       result.w = widget_width;
103       result.h = widget_height;
104     }
105
106     if (gst_widget->ignore_alpha) {
107       GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
108
109       gdk_cairo_set_source_rgba (cr, &color);
110       if (result.x > 0) {
111         cairo_rectangle (cr, 0, 0, result.x, widget_height);
112         cairo_fill (cr);
113       }
114       if (result.y > 0) {
115         cairo_rectangle (cr, 0, 0, widget_width, result.y);
116         cairo_fill (cr);
117       }
118       if (result.w < widget_width) {
119         cairo_rectangle (cr, result.x + result.w, 0, widget_width - result.w,
120             widget_height);
121         cairo_fill (cr);
122       }
123       if (result.h < widget_height) {
124         cairo_rectangle (cr, 0, result.y + result.h, widget_width,
125             widget_height - result.h);
126         cairo_fill (cr);
127       }
128     }
129
130     scale_x *= (gdouble) gst_widget->display_width / (gdouble) frame.info.width;
131     scale_y *=
132         (gdouble) gst_widget->display_height / (gdouble) frame.info.height;
133
134     cairo_translate (cr, result.x, result.y);
135     cairo_scale (cr, scale_x, scale_y);
136     cairo_rectangle (cr, 0, 0, result.w, result.h);
137     cairo_set_source_surface (cr, surface, 0, 0);
138     cairo_paint (cr);
139
140     cairo_surface_destroy (surface);
141
142     gst_video_frame_unmap (&frame);
143   } else {
144     GdkRGBA color;
145
146     if (gst_widget->ignore_alpha) {
147       color.red = color.blue = color.green = 0.0;
148       color.alpha = 1.0;
149     } else {
150       gtk_style_context_get_color (gtk_widget_get_style_context (widget),
151           GTK_STATE_FLAG_NORMAL, &color);
152     }
153     gdk_cairo_set_source_rgba (cr, &color);
154     cairo_rectangle (cr, 0, 0, widget_width, widget_height);
155     cairo_fill (cr);
156   }
157
158   GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
159   return FALSE;
160 }
161
162 static void
163 gtk_gst_widget_finalize (GObject * object)
164 {
165   gtk_gst_base_widget_finalize (object);
166
167   G_OBJECT_CLASS (gtk_gst_widget_parent_class)->finalize (object);
168 }
169
170 static void
171 gtk_gst_widget_class_init (GtkGstWidgetClass * klass)
172 {
173   GObjectClass *gobject_klass = (GObjectClass *) klass;
174   GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
175
176   gtk_gst_base_widget_class_init (GTK_GST_BASE_WIDGET_CLASS (klass));
177   gobject_klass->finalize = gtk_gst_widget_finalize;
178   widget_klass->draw = gtk_gst_widget_draw;
179 }
180
181 static void
182 gtk_gst_widget_init (GtkGstWidget * widget)
183 {
184   gtk_gst_base_widget_init (GTK_GST_BASE_WIDGET (widget));
185 }
186
187 GtkWidget *
188 gtk_gst_widget_new (void)
189 {
190   return (GtkWidget *) g_object_new (GTK_TYPE_GST_WIDGET, NULL);
191 }