1 /*
2  * GStreamer
3  * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
4  * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
5  * Copyright (C) 2013 Sebastian Dröge <slomo@circular-chaos.org>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 /* TODO: - Window resize handling
24  *       - Event handling input event handling
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 
31 #include <gst/gst.h>
32 
33 #include <gst/gl/egl/gstglcontext_egl.h>
34 #include "gstglwindow_android_egl.h"
35 #include "../gstglwindow_private.h"
36 
37 #define GST_CAT_DEFAULT gst_gl_window_debug
38 
39 #define gst_gl_window_android_egl_parent_class parent_class
40 G_DEFINE_TYPE (GstGLWindowAndroidEGL, gst_gl_window_android_egl,
41     GST_TYPE_GL_WINDOW);
42 
43 static guintptr gst_gl_window_android_egl_get_display (GstGLWindow * window);
44 static guintptr gst_gl_window_android_egl_get_window_handle (GstGLWindow *
45     window);
46 static void gst_gl_window_android_egl_set_window_handle (GstGLWindow * window,
47     guintptr handle);
48 static void gst_gl_window_android_egl_draw (GstGLWindow * window);
49 
50 static void
gst_gl_window_android_egl_class_init(GstGLWindowAndroidEGLClass * klass)51 gst_gl_window_android_egl_class_init (GstGLWindowAndroidEGLClass * klass)
52 {
53   GstGLWindowClass *window_class = (GstGLWindowClass *) klass;
54 
55   window_class->get_display =
56       GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_get_display);
57   window_class->get_window_handle =
58       GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_get_window_handle);
59   window_class->set_window_handle =
60       GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_set_window_handle);
61   window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_draw);
62 }
63 
64 static void
gst_gl_window_android_egl_init(GstGLWindowAndroidEGL * window)65 gst_gl_window_android_egl_init (GstGLWindowAndroidEGL * window)
66 {
67 }
68 
69 /* Must be called in the gl thread */
70 GstGLWindowAndroidEGL *
gst_gl_window_android_egl_new(GstGLDisplay * display)71 gst_gl_window_android_egl_new (GstGLDisplay * display)
72 {
73   GstGLWindowAndroidEGL *window;
74 
75   if ((gst_gl_display_get_handle_type (display) & GST_GL_DISPLAY_TYPE_EGL) == 0)
76     /* we require an egl display to create android windows */
77     return NULL;
78 
79   GST_DEBUG ("creating Android EGL window");
80 
81   window = g_object_new (GST_TYPE_GL_WINDOW_ANDROID_EGL, NULL);
82   gst_object_ref_sink (window);
83 
84   return window;
85 }
86 
87 static void
gst_gl_window_android_egl_set_window_handle(GstGLWindow * window,guintptr handle)88 gst_gl_window_android_egl_set_window_handle (GstGLWindow * window,
89     guintptr handle)
90 {
91   GstGLWindowAndroidEGL *window_egl = GST_GL_WINDOW_ANDROID_EGL (window);
92 
93   window_egl->native_window = (EGLNativeWindowType) handle;
94 }
95 
96 static guintptr
gst_gl_window_android_egl_get_window_handle(GstGLWindow * window)97 gst_gl_window_android_egl_get_window_handle (GstGLWindow * window)
98 {
99   GstGLWindowAndroidEGL *window_egl = GST_GL_WINDOW_ANDROID_EGL (window);
100 
101   return (guintptr) window_egl->native_window;
102 }
103 
104 static void
draw_cb(gpointer data)105 draw_cb (gpointer data)
106 {
107   GstGLWindowAndroidEGL *window_egl = data;
108   GstGLWindow *window = GST_GL_WINDOW (window_egl);
109   GstGLContext *context = gst_gl_window_get_context (window);
110   GstGLContextEGL *context_egl = GST_GL_CONTEXT_EGL (context);
111 
112   if (context_egl->egl_surface) {
113     gint width, height;
114     guint window_width, window_height;
115 
116     gst_gl_window_get_surface_dimensions (window, &window_width,
117         &window_height);
118     if (eglQuerySurface (context_egl->egl_display, context_egl->egl_surface,
119             EGL_WIDTH, &width)
120         && eglQuerySurface (context_egl->egl_display, context_egl->egl_surface,
121             EGL_HEIGHT, &height)
122         && (window->queue_resize || width != window_egl->window_width
123             || height != window_egl->window_height)) {
124       gst_gl_window_resize (window, width, height);
125     }
126   }
127 
128   if (window->draw)
129     window->draw (window->draw_data);
130 
131   gst_gl_context_swap_buffers (context);
132 
133   gst_object_unref (context);
134 }
135 
136 static void
gst_gl_window_android_egl_draw(GstGLWindow * window)137 gst_gl_window_android_egl_draw (GstGLWindow * window)
138 {
139   gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
140 }
141 
142 static guintptr
gst_gl_window_android_egl_get_display(GstGLWindow * window)143 gst_gl_window_android_egl_get_display (GstGLWindow * window)
144 {
145   return 0;
146 }
147