• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "gtkgstglwidget.h"
28 #include "gstgtkutils.h"
29 #include <gst/gl/gstglfuncs.h>
30 #include <gst/video/video.h>
31 
32 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
33 #include <gdk/gdkx.h>
34 #include <gst/gl/x11/gstgldisplay_x11.h>
35 #endif
36 
37 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)
38 #include <gdk/gdkwayland.h>
39 #include <gst/gl/wayland/gstgldisplay_wayland.h>
40 #endif
41 
42 /**
43  * SECTION:gtkgstglwidget
44  * @title: GtkGstGlWidget
45  * @short_description: a #GtkGLArea that renders GStreamer video #GstBuffers
46  * @see_also: #GtkGLArea, #GstBuffer
47  *
48  * #GtkGstGLWidget is an #GtkWidget that renders GStreamer video buffers.
49  */
50 
51 #define GST_CAT_DEFAULT gtk_gst_gl_widget_debug
52 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
53 
54 struct _GtkGstGLWidgetPrivate
55 {
56   gboolean initted;
57   GstGLDisplay *display;
58   GdkGLContext *gdk_context;
59   GstGLContext *other_context;
60   GstGLContext *context;
61   GstGLUpload *upload;
62   GstGLShader *shader;
63   GLuint vao;
64   GLuint vertex_buffer;
65   GLint attr_position;
66   GLint attr_texture;
67   GLuint current_tex;
68   GstGLOverlayCompositor *overlay_compositor;
69   GstVideoOrientationMethod rotate_method;
70   GstVideoOrientationMethod current_rotate_method;
71   const gfloat *transform_matrix;
72 };
73 
74 static const GLfloat vertices[] = {
75   1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
76   -1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
77   -1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
78   1.0f, -1.0f, 0.0f, 1.0f, 1.0f
79 };
80 
81 G_DEFINE_TYPE_WITH_CODE (GtkGstGLWidget, gtk_gst_gl_widget, GTK_TYPE_GL_AREA,
82     G_ADD_PRIVATE (GtkGstGLWidget)
83     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "gtkgstglwidget", 0,
84         "Gtk Gst GL Widget");
85     );
86 
87 static void
gtk_gst_gl_widget_bind_buffer(GtkGstGLWidget * gst_widget)88 gtk_gst_gl_widget_bind_buffer (GtkGstGLWidget * gst_widget)
89 {
90   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
91   const GstGLFuncs *gl = priv->context->gl_vtable;
92 
93   gl->BindBuffer (GL_ARRAY_BUFFER, priv->vertex_buffer);
94 
95   /* Load the vertex position */
96   gl->VertexAttribPointer (priv->attr_position, 3, GL_FLOAT, GL_FALSE,
97       5 * sizeof (GLfloat), (void *) 0);
98 
99   /* Load the texture coordinate */
100   gl->VertexAttribPointer (priv->attr_texture, 2, GL_FLOAT, GL_FALSE,
101       5 * sizeof (GLfloat), (void *) (3 * sizeof (GLfloat)));
102 
103   gl->EnableVertexAttribArray (priv->attr_position);
104   gl->EnableVertexAttribArray (priv->attr_texture);
105 }
106 
107 static void
gtk_gst_gl_widget_unbind_buffer(GtkGstGLWidget * gst_widget)108 gtk_gst_gl_widget_unbind_buffer (GtkGstGLWidget * gst_widget)
109 {
110   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
111   const GstGLFuncs *gl = priv->context->gl_vtable;
112 
113   gl->BindBuffer (GL_ARRAY_BUFFER, 0);
114 
115   gl->DisableVertexAttribArray (priv->attr_position);
116   gl->DisableVertexAttribArray (priv->attr_texture);
117 }
118 
119 static void
gtk_gst_gl_widget_init_redisplay(GtkGstGLWidget * gst_widget)120 gtk_gst_gl_widget_init_redisplay (GtkGstGLWidget * gst_widget)
121 {
122   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
123   const GstGLFuncs *gl = priv->context->gl_vtable;
124   GError *error = NULL;
125   GstGLSLStage *frag_stage, *vert_stage;
126 
127   vert_stage = gst_glsl_stage_new_with_string (priv->context,
128       GL_VERTEX_SHADER, GST_GLSL_VERSION_NONE,
129       GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY,
130       gst_gl_shader_string_vertex_mat4_vertex_transform);
131   frag_stage = gst_glsl_stage_new_default_fragment (priv->context);
132 
133   gst_gl_insert_debug_marker (priv->other_context, "initializing redisplay");
134   if (!(priv->shader =
135           gst_gl_shader_new_link_with_stages (priv->context, &error, vert_stage,
136               frag_stage, NULL))) {
137     GST_ERROR ("Failed to initialize shader: %s", error->message);
138     return;
139   }
140 
141   priv->attr_position =
142       gst_gl_shader_get_attribute_location (priv->shader, "a_position");
143   priv->attr_texture =
144       gst_gl_shader_get_attribute_location (priv->shader, "a_texcoord");
145 
146   if (gl->GenVertexArrays) {
147     gl->GenVertexArrays (1, &priv->vao);
148     gl->BindVertexArray (priv->vao);
149   }
150 
151   gl->GenBuffers (1, &priv->vertex_buffer);
152   gl->BindBuffer (GL_ARRAY_BUFFER, priv->vertex_buffer);
153   gl->BufferData (GL_ARRAY_BUFFER, 4 * 5 * sizeof (GLfloat), vertices,
154       GL_STATIC_DRAW);
155 
156   if (gl->GenVertexArrays) {
157     gtk_gst_gl_widget_bind_buffer (gst_widget);
158     gl->BindVertexArray (0);
159   }
160 
161   gl->BindBuffer (GL_ARRAY_BUFFER, 0);
162 
163   priv->overlay_compositor =
164       gst_gl_overlay_compositor_new (priv->other_context);
165 
166   priv->initted = TRUE;
167 }
168 
169 /* rotate 90 */
170 static const gfloat clockwise_matrix[] = {
171   0.0f, -1.0f, 0.0f, 0.0f,
172   1.0f, 0.0f, 0.0f, 0.0f,
173   0.0f, 0.0f, 1.0f, 0.0f,
174   0.0f, 0.0f, 0.0f, 1.0f,
175 };
176 
177 /* rotate 180 */
178 static const gfloat clockwise_180_matrix[] = {
179   -1.0f, 0.0f, 0.0f, 0.0f,
180   0.0f, -1.0f, 0.0f, 0.0f,
181   0.0f, 0.0f, 1.0f, 0.0f,
182   0.0f, 0.0f, 0.0f, 1.0f,
183 };
184 
185 /* rotate 270 */
186 static const gfloat counterclockwise_matrix[] = {
187   0.0f, 1.0f, 0.0f, 0.0f,
188   -1.0f, 0.0f, 0.0f, 0.0f,
189   0.0f, 0.0f, 1.0f, 0.0f,
190   0.0f, 0.0f, 0.0f, 1.0f,
191 };
192 
193 /* horizontal-flip */
194 static const gfloat horizontal_flip_matrix[] = {
195   -1.0f, 0.0f, 0.0f, 0.0f,
196   0.0f, 1.0f, 0.0f, 0.0f,
197   0.0f, 0.0f, 1.0f, 0.0f,
198   0.0f, 0.0f, 0.0f, 1.0f,
199 };
200 
201 /* vertical-flip */
202 static const gfloat vertical_flip_matrix[] = {
203   1.0f, 0.0f, 0.0f, 0.0f,
204   0.0f, -1.0f, 0.0f, 0.0f,
205   0.0f, 0.0f, 1.0f, 0.0f,
206   0.0f, 0.0f, 0.0f, 1.0f,
207 };
208 
209 /* upper-left-diagonal */
210 static const gfloat upper_left_matrix[] = {
211   0.0f, 1.0f, 0.0f, 0.0f,
212   1.0f, 0.0f, 0.0f, 0.0f,
213   0.0f, 0.0f, 1.0f, 0.0f,
214   0.0f, 0.0f, 0.0f, 1.0f,
215 };
216 
217 /* upper-right-diagonal */
218 static const gfloat upper_right_matrix[] = {
219   0.0f, -1.0f, 0.0f, 0.0f,
220   -1.0f, 0.0f, 0.0f, 0.0f,
221   0.0f, 0.0f, 1.0f, 0.0f,
222   0.0f, 0.0f, 0.0f, 1.0f,
223 };
224 
225 static void
_redraw_texture(GtkGstGLWidget * gst_widget,guint tex)226 _redraw_texture (GtkGstGLWidget * gst_widget, guint tex)
227 {
228   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
229   const GstGLFuncs *gl = priv->context->gl_vtable;
230   const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
231   GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (gst_widget);
232   GtkWidget *widget = GTK_WIDGET (gst_widget);
233 
234 
235   if (gst_widget->base.force_aspect_ratio) {
236     GstVideoRectangle src, dst, result;
237     gint video_width, video_height, widget_scale;
238 
239     gl->ClearColor (0.0, 0.0, 0.0, 0.0);
240     gl->Clear (GL_COLOR_BUFFER_BIT);
241 
242     widget_scale = gtk_widget_get_scale_factor (widget);
243 
244     if (priv->current_rotate_method == GST_VIDEO_ORIENTATION_90R
245         || priv->current_rotate_method == GST_VIDEO_ORIENTATION_90L
246         || priv->current_rotate_method == GST_VIDEO_ORIENTATION_UL_LR
247         || priv->current_rotate_method == GST_VIDEO_ORIENTATION_UR_LL) {
248       video_width = base_widget->display_height;
249       video_height = base_widget->display_width;
250     } else {
251       video_width = base_widget->display_width;
252       video_height = base_widget->display_height;
253     }
254 
255     src.x = 0;
256     src.y = 0;
257     src.w = video_width;
258     src.h = video_height;
259 
260     dst.x = 0;
261     dst.y = 0;
262     dst.w = gtk_widget_get_allocated_width (widget) * widget_scale;
263     dst.h = gtk_widget_get_allocated_height (widget) * widget_scale;
264 
265     gst_video_sink_center_rect (src, dst, &result, TRUE);
266 
267     GST_LOG ("Center src %dx%d into dst %dx%d result -> %dx%d",
268         src.w, src.h, dst.w, dst.h, result.w, result.h);
269 
270     gl->Viewport (result.x, result.y, result.w, result.h);
271   }
272 
273   gst_gl_shader_use (priv->shader);
274 
275   if (gl->BindVertexArray)
276     gl->BindVertexArray (priv->vao);
277   gtk_gst_gl_widget_bind_buffer (gst_widget);
278 
279   gl->ActiveTexture (GL_TEXTURE0);
280   gl->BindTexture (GL_TEXTURE_2D, tex);
281   gst_gl_shader_set_uniform_1i (priv->shader, "tex", 0);
282 
283   {
284     GstVideoAffineTransformationMeta *af_meta;
285     gfloat matrix[16];
286 
287     af_meta =
288         gst_buffer_get_video_affine_transformation_meta (base_widget->buffer);
289 
290     if (priv->transform_matrix) {
291       gfloat tmp[16];
292 
293       gst_gl_get_affine_transformation_meta_as_ndc (af_meta, tmp);
294       gst_gl_multiply_matrix4 (tmp, priv->transform_matrix, matrix);
295     } else {
296       gst_gl_get_affine_transformation_meta_as_ndc (af_meta, matrix);
297     }
298 
299     gst_gl_shader_set_uniform_matrix_4fv (priv->shader,
300         "u_transformation", 1, FALSE, matrix);
301   }
302 
303   gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
304 
305   if (gl->BindVertexArray)
306     gl->BindVertexArray (0);
307   else
308     gtk_gst_gl_widget_unbind_buffer (gst_widget);
309 
310   gl->BindTexture (GL_TEXTURE_2D, 0);
311 }
312 
313 static inline void
_draw_black(GstGLContext * context)314 _draw_black (GstGLContext * context)
315 {
316   const GstGLFuncs *gl = context->gl_vtable;
317 
318   gst_gl_insert_debug_marker (context, "no buffer.  rendering black");
319   gl->ClearColor (0.0, 0.0, 0.0, 0.0);
320   gl->Clear (GL_COLOR_BUFFER_BIT);
321 }
322 
323 static gboolean
gtk_gst_gl_widget_render(GtkGLArea * widget,GdkGLContext * context)324 gtk_gst_gl_widget_render (GtkGLArea * widget, GdkGLContext * context)
325 {
326   GtkGstGLWidgetPrivate *priv = GTK_GST_GL_WIDGET (widget)->priv;
327   GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (widget);
328 
329   GTK_GST_BASE_WIDGET_LOCK (widget);
330 
331   if (!priv->context || !priv->other_context)
332     goto done;
333 
334   gst_gl_context_activate (priv->other_context, TRUE);
335 
336   if (!priv->initted)
337     gtk_gst_gl_widget_init_redisplay (GTK_GST_GL_WIDGET (widget));
338 
339   if (!priv->initted || !base_widget->negotiated) {
340     _draw_black (priv->other_context);
341     goto done;
342   }
343 
344   /* Upload latest buffer */
345   if (base_widget->pending_buffer) {
346     GstBuffer *buffer = base_widget->pending_buffer;
347     GstVideoFrame gl_frame;
348     GstGLSyncMeta *sync_meta;
349 
350     if (!gst_video_frame_map (&gl_frame, &base_widget->v_info, buffer,
351             GST_MAP_READ | GST_MAP_GL)) {
352       _draw_black (priv->other_context);
353       goto done;
354     }
355 
356     priv->current_tex = *(guint *) gl_frame.data[0];
357     gst_gl_insert_debug_marker (priv->other_context, "redrawing texture %u",
358         priv->current_tex);
359 
360     gst_gl_overlay_compositor_upload_overlays (priv->overlay_compositor,
361         buffer);
362 
363     sync_meta = gst_buffer_get_gl_sync_meta (buffer);
364     if (sync_meta) {
365       /* XXX: the set_sync() seems to be needed for resizing */
366       gst_gl_sync_meta_set_sync_point (sync_meta, priv->context);
367       gst_gl_sync_meta_wait (sync_meta, priv->other_context);
368     }
369 
370     gst_video_frame_unmap (&gl_frame);
371 
372     if (base_widget->buffer)
373       gst_buffer_unref (base_widget->buffer);
374 
375     /* Keep the buffer to ensure current_tex stay valid */
376     base_widget->buffer = buffer;
377     base_widget->pending_buffer = NULL;
378   }
379 
380   GST_DEBUG ("rendering buffer %p with gdk context %p",
381       base_widget->buffer, context);
382 
383   _redraw_texture (GTK_GST_GL_WIDGET (widget), priv->current_tex);
384   gst_gl_overlay_compositor_draw_overlays (priv->overlay_compositor);
385 
386   gst_gl_insert_debug_marker (priv->other_context, "texture %u redrawn",
387       priv->current_tex);
388 
389 done:
390   if (priv->other_context)
391     gst_gl_context_activate (priv->other_context, FALSE);
392 
393   GTK_GST_BASE_WIDGET_UNLOCK (widget);
394   return FALSE;
395 }
396 
397 static void
_reset_gl(GtkGstGLWidget * gst_widget)398 _reset_gl (GtkGstGLWidget * gst_widget)
399 {
400   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
401   const GstGLFuncs *gl = priv->other_context->gl_vtable;
402 
403   if (!priv->gdk_context)
404     priv->gdk_context = gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
405 
406   if (priv->gdk_context == NULL)
407     return;
408 
409   gdk_gl_context_make_current (priv->gdk_context);
410   gst_gl_context_activate (priv->other_context, TRUE);
411 
412   if (priv->vao) {
413     gl->DeleteVertexArrays (1, &priv->vao);
414     priv->vao = 0;
415   }
416 
417   if (priv->vertex_buffer) {
418     gl->DeleteBuffers (1, &priv->vertex_buffer);
419     priv->vertex_buffer = 0;
420   }
421 
422   if (priv->upload) {
423     gst_object_unref (priv->upload);
424     priv->upload = NULL;
425   }
426 
427   if (priv->shader) {
428     gst_object_unref (priv->shader);
429     priv->shader = NULL;
430   }
431 
432   if (priv->overlay_compositor)
433     gst_object_unref (priv->overlay_compositor);
434 
435   gst_gl_context_activate (priv->other_context, FALSE);
436 
437   gst_object_unref (priv->other_context);
438   priv->other_context = NULL;
439 
440   gdk_gl_context_clear_current ();
441 
442   g_object_unref (priv->gdk_context);
443   priv->gdk_context = NULL;
444 }
445 
446 static void
gtk_gst_gl_widget_finalize(GObject * object)447 gtk_gst_gl_widget_finalize (GObject * object)
448 {
449   GtkGstGLWidgetPrivate *priv = GTK_GST_GL_WIDGET (object)->priv;
450   GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (object);
451 
452   if (priv->other_context)
453     gst_gtk_invoke_on_main ((GThreadFunc) _reset_gl, base_widget);
454 
455   if (priv->context)
456     gst_object_unref (priv->context);
457 
458   if (priv->display)
459     gst_object_unref (priv->display);
460 
461   gtk_gst_base_widget_finalize (object);
462   G_OBJECT_CLASS (gtk_gst_gl_widget_parent_class)->finalize (object);
463 }
464 
465 static void
gtk_gst_gl_widget_class_init(GtkGstGLWidgetClass * klass)466 gtk_gst_gl_widget_class_init (GtkGstGLWidgetClass * klass)
467 {
468   GObjectClass *gobject_klass = (GObjectClass *) klass;
469   GtkGLAreaClass *gl_widget_klass = (GtkGLAreaClass *) klass;
470 
471   gtk_gst_base_widget_class_init (GTK_GST_BASE_WIDGET_CLASS (klass));
472 
473   gobject_klass->finalize = gtk_gst_gl_widget_finalize;
474   gl_widget_klass->render = gtk_gst_gl_widget_render;
475 }
476 
477 static void
gtk_gst_gl_widget_init(GtkGstGLWidget * gst_widget)478 gtk_gst_gl_widget_init (GtkGstGLWidget * gst_widget)
479 {
480   GtkGstBaseWidget *base_widget = GTK_GST_BASE_WIDGET (gst_widget);
481   GdkDisplay *display;
482   GtkGstGLWidgetPrivate *priv;
483 
484   gtk_gst_base_widget_init (base_widget);
485 
486   gst_widget->priv = priv = gtk_gst_gl_widget_get_instance_private (gst_widget);
487 
488   display = gdk_display_get_default ();
489 
490 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
491   if (GDK_IS_X11_DISPLAY (display)) {
492     priv->display = (GstGLDisplay *)
493         gst_gl_display_x11_new_with_display (gdk_x11_display_get_xdisplay
494         (display));
495   }
496 #endif
497 #if GST_GL_HAVE_WINDOW_WAYLAND && defined (GDK_WINDOWING_WAYLAND)
498   if (GDK_IS_WAYLAND_DISPLAY (display)) {
499     struct wl_display *wayland_display =
500         gdk_wayland_display_get_wl_display (display);
501     priv->display = (GstGLDisplay *)
502         gst_gl_display_wayland_new_with_display (wayland_display);
503   }
504 #endif
505 
506   (void) display;
507 
508   if (!priv->display)
509     priv->display = gst_gl_display_new ();
510 
511   GST_INFO ("Created %" GST_PTR_FORMAT, priv->display);
512 
513   gtk_gl_area_set_has_alpha (GTK_GL_AREA (gst_widget),
514       !base_widget->ignore_alpha);
515 }
516 
517 static void
_get_gl_context(GtkGstGLWidget * gst_widget)518 _get_gl_context (GtkGstGLWidget * gst_widget)
519 {
520   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
521   GstGLPlatform platform = GST_GL_PLATFORM_NONE;
522   GstGLAPI gl_api = GST_GL_API_NONE;
523   guintptr gl_handle = 0;
524 
525   gtk_widget_realize (GTK_WIDGET (gst_widget));
526 
527   if (priv->other_context)
528     gst_object_unref (priv->other_context);
529   priv->other_context = NULL;
530 
531   if (priv->gdk_context)
532     g_object_unref (priv->gdk_context);
533 
534   priv->gdk_context = gtk_gl_area_get_context (GTK_GL_AREA (gst_widget));
535   if (priv->gdk_context == NULL) {
536     GError *error = gtk_gl_area_get_error (GTK_GL_AREA (gst_widget));
537 
538     GST_ERROR_OBJECT (gst_widget, "Error creating GdkGLContext : %s",
539         error ? error->message : "No error set by Gdk");
540     return;
541   }
542 
543   g_object_ref (priv->gdk_context);
544 
545   gdk_gl_context_make_current (priv->gdk_context);
546 
547 #if GST_GL_HAVE_WINDOW_X11 && defined (GDK_WINDOWING_X11)
548   if (GST_IS_GL_DISPLAY_X11 (priv->display)) {
549 #if GST_GL_HAVE_PLATFORM_GLX
550     if (!gl_handle) {
551       platform = GST_GL_PLATFORM_GLX;
552       gl_handle = gst_gl_context_get_current_gl_context (platform);
553     }
554 #endif
555 
556 #if GST_GL_HAVE_PLATFORM_EGL
557     if (!gl_handle) {
558       platform = GST_GL_PLATFORM_EGL;
559       gl_handle = gst_gl_context_get_current_gl_context (platform);
560     }
561 #endif
562 
563     if (gl_handle) {
564       gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
565       priv->other_context =
566           gst_gl_context_new_wrapped (priv->display, gl_handle,
567           platform, gl_api);
568     }
569   }
570 #endif
571 #if GST_GL_HAVE_WINDOW_WAYLAND && GST_GL_HAVE_PLATFORM_EGL && defined (GDK_WINDOWING_WAYLAND)
572   if (GST_IS_GL_DISPLAY_WAYLAND (priv->display)) {
573     platform = GST_GL_PLATFORM_EGL;
574     gl_api = gst_gl_context_get_current_gl_api (platform, NULL, NULL);
575     gl_handle = gst_gl_context_get_current_gl_context (platform);
576     if (gl_handle)
577       priv->other_context =
578           gst_gl_context_new_wrapped (priv->display, gl_handle,
579           platform, gl_api);
580   }
581 #endif
582 
583   (void) platform;
584   (void) gl_api;
585   (void) gl_handle;
586 
587   if (priv->other_context) {
588     GError *error = NULL;
589 
590     GST_INFO ("Retrieved Gdk OpenGL context %" GST_PTR_FORMAT,
591         priv->other_context);
592     gst_gl_context_activate (priv->other_context, TRUE);
593     if (!gst_gl_context_fill_info (priv->other_context, &error)) {
594       GST_ERROR ("failed to retrieve gdk context info: %s", error->message);
595       g_clear_error (&error);
596       g_object_unref (priv->other_context);
597       priv->other_context = NULL;
598     } else {
599       gst_gl_context_activate (priv->other_context, FALSE);
600     }
601   } else {
602     GST_WARNING ("Could not retrieve Gdk OpenGL context");
603   }
604 }
605 
606 GtkWidget *
gtk_gst_gl_widget_new(void)607 gtk_gst_gl_widget_new (void)
608 {
609   return (GtkWidget *) g_object_new (GTK_TYPE_GST_GL_WIDGET, NULL);
610 }
611 
612 gboolean
gtk_gst_gl_widget_init_winsys(GtkGstGLWidget * gst_widget)613 gtk_gst_gl_widget_init_winsys (GtkGstGLWidget * gst_widget)
614 {
615   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
616   GError *error = NULL;
617 
618   g_return_val_if_fail (GTK_IS_GST_GL_WIDGET (gst_widget), FALSE);
619   g_return_val_if_fail (priv->display != NULL, FALSE);
620 
621   GTK_GST_BASE_WIDGET_LOCK (gst_widget);
622 
623   if (priv->display && priv->gdk_context && priv->other_context) {
624     GST_TRACE ("have already initialized contexts");
625     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
626     return TRUE;
627   }
628 
629   if (!priv->other_context) {
630     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
631     gst_gtk_invoke_on_main ((GThreadFunc) _get_gl_context, gst_widget);
632     GTK_GST_BASE_WIDGET_LOCK (gst_widget);
633   }
634 
635   if (!GST_IS_GL_CONTEXT (priv->other_context)) {
636     GST_FIXME ("Could not retrieve Gdk OpenGL context");
637     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
638     return FALSE;
639   }
640 
641   GST_OBJECT_LOCK (priv->display);
642   if (!gst_gl_display_create_context (priv->display, priv->other_context,
643           &priv->context, &error)) {
644     GST_WARNING ("Could not create OpenGL context: %s",
645         error ? error->message : "Unknown");
646     g_clear_error (&error);
647     GST_OBJECT_UNLOCK (priv->display);
648     GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
649     return FALSE;
650   }
651   gst_gl_display_add_context (priv->display, priv->context);
652   GST_OBJECT_UNLOCK (priv->display);
653 
654   GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
655   return TRUE;
656 }
657 
658 GstGLContext *
gtk_gst_gl_widget_get_gtk_context(GtkGstGLWidget * gst_widget)659 gtk_gst_gl_widget_get_gtk_context (GtkGstGLWidget * gst_widget)
660 {
661   if (!gst_widget->priv->other_context)
662     return NULL;
663 
664   return gst_object_ref (gst_widget->priv->other_context);
665 }
666 
667 GstGLContext *
gtk_gst_gl_widget_get_context(GtkGstGLWidget * gst_widget)668 gtk_gst_gl_widget_get_context (GtkGstGLWidget * gst_widget)
669 {
670   if (!gst_widget->priv->context)
671     return NULL;
672 
673   return gst_object_ref (gst_widget->priv->context);
674 }
675 
676 GstGLDisplay *
gtk_gst_gl_widget_get_display(GtkGstGLWidget * gst_widget)677 gtk_gst_gl_widget_get_display (GtkGstGLWidget * gst_widget)
678 {
679   if (!gst_widget->priv->display)
680     return NULL;
681 
682   return gst_object_ref (gst_widget->priv->display);
683 }
684 
685 void
gtk_gst_gl_widget_set_rotate_method(GtkGstGLWidget * gst_widget,GstVideoOrientationMethod method,gboolean from_tag)686 gtk_gst_gl_widget_set_rotate_method (GtkGstGLWidget * gst_widget,
687     GstVideoOrientationMethod method, gboolean from_tag)
688 {
689   GstVideoOrientationMethod tag_method = GST_VIDEO_ORIENTATION_AUTO;
690   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
691 
692   if (method == GST_VIDEO_ORIENTATION_CUSTOM) {
693     GST_WARNING_OBJECT (gst_widget, "unsupported custom orientation");
694     return;
695   } else if (method == GST_VIDEO_ORIENTATION_AUTO && from_tag) {
696     GST_WARNING_OBJECT (gst_widget, "auto orientation cannot come from a tag");
697     return;
698   }
699 
700   GTK_GST_BASE_WIDGET_LOCK (gst_widget);
701   if (from_tag)
702     tag_method = method;
703   else
704     priv->rotate_method = method;
705 
706   if (priv->rotate_method == GST_VIDEO_ORIENTATION_AUTO)
707     method = tag_method;
708   else
709     method = priv->rotate_method;
710 
711   /* We can't apply an AUTO orientation if we don't have an
712    * orientation coming from a tag, so reset to identity */
713   if (method != priv->current_rotate_method &&
714       method == GST_VIDEO_ORIENTATION_AUTO)
715     method = GST_VIDEO_ORIENTATION_IDENTITY;
716 
717   if (method != priv->current_rotate_method) {
718     GST_DEBUG ("Changing method from %d to %d",
719         priv->current_rotate_method, method);
720 
721     switch (method) {
722       case GST_VIDEO_ORIENTATION_IDENTITY:
723         priv->transform_matrix = NULL;
724         break;
725       case GST_VIDEO_ORIENTATION_90R:
726         priv->transform_matrix = clockwise_matrix;
727         break;
728       case GST_VIDEO_ORIENTATION_180:
729         priv->transform_matrix = clockwise_180_matrix;
730         break;
731       case GST_VIDEO_ORIENTATION_90L:
732         priv->transform_matrix = counterclockwise_matrix;
733         break;
734       case GST_VIDEO_ORIENTATION_HORIZ:
735         priv->transform_matrix = horizontal_flip_matrix;
736         break;
737       case GST_VIDEO_ORIENTATION_VERT:
738         priv->transform_matrix = vertical_flip_matrix;
739         break;
740       case GST_VIDEO_ORIENTATION_UL_LR:
741         priv->transform_matrix = upper_left_matrix;
742         break;
743       case GST_VIDEO_ORIENTATION_UR_LL:
744         priv->transform_matrix = upper_right_matrix;
745         break;
746       default:
747         g_assert_not_reached ();
748         break;
749     }
750 
751     priv->current_rotate_method = method;
752   }
753   GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
754 
755   gtk_gst_base_widget_queue_draw (GTK_GST_BASE_WIDGET (gst_widget));
756 }
757 
758 GstVideoOrientationMethod
gtk_gst_gl_widget_get_rotate_method(GtkGstGLWidget * gst_widget)759 gtk_gst_gl_widget_get_rotate_method (GtkGstGLWidget * gst_widget)
760 {
761   GtkGstGLWidgetPrivate *priv = gst_widget->priv;
762   GstVideoOrientationMethod method;
763 
764   GTK_GST_BASE_WIDGET_LOCK (gst_widget);
765   method = priv->current_rotate_method;
766   GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
767 
768   return method;
769 }
770