summaryrefslogtreecommitdiff
path: root/gst-libs/gst/gl/android/gstglwindow_android_egl.c
blob: 202aba08e23b374b5831e01293f64ee477f2fb8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * GStreamer
 * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
 * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
 * Copyright (C) 2013 Sebastian Dröge <slomo@circular-chaos.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/* TODO: - Window resize handling
 *       - Event handling input event handling
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>

#include <gst/gl/egl/gstglcontext_egl.h>
#include "gstglwindow_android_egl.h"

#define GST_CAT_DEFAULT gst_gl_window_debug

#define gst_gl_window_android_egl_parent_class parent_class
G_DEFINE_TYPE (GstGLWindowAndroidEGL, gst_gl_window_android_egl,
    GST_GL_TYPE_WINDOW);

static guintptr gst_gl_window_android_egl_get_display (GstGLWindow * window);
static guintptr gst_gl_window_android_egl_get_window_handle (GstGLWindow *
    window);
static void gst_gl_window_android_egl_set_window_handle (GstGLWindow * window,
    guintptr handle);
static void gst_gl_window_android_egl_draw (GstGLWindow * window);
static void gst_gl_window_android_egl_run (GstGLWindow * window);
static void gst_gl_window_android_egl_quit (GstGLWindow * window);
static void gst_gl_window_android_egl_send_message_async (GstGLWindow * window,
    GstGLWindowCB callback, gpointer data, GDestroyNotify destroy);
static gboolean gst_gl_window_android_egl_open (GstGLWindow * window,
    GError ** error);
static void gst_gl_window_android_egl_close (GstGLWindow * window);

static void
gst_gl_window_android_egl_class_init (GstGLWindowAndroidEGLClass * klass)
{
  GstGLWindowClass *window_class = (GstGLWindowClass *) klass;

  window_class->get_display =
      GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_get_display);
  window_class->get_window_handle =
      GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_get_window_handle);
  window_class->set_window_handle =
      GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_set_window_handle);
  window_class->draw_unlocked =
      GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_draw);
  window_class->draw = GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_draw);
  window_class->run = GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_run);
  window_class->quit = GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_quit);
  window_class->send_message_async =
      GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_send_message_async);
  window_class->open = GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_open);
  window_class->close = GST_DEBUG_FUNCPTR (gst_gl_window_android_egl_close);
}

static void
gst_gl_window_android_egl_init (GstGLWindowAndroidEGL * window)
{
}

/* Must be called in the gl thread */
GstGLWindowAndroidEGL *
gst_gl_window_android_egl_new (void)
{
  GstGLWindowAndroidEGL *window;

  GST_DEBUG ("creating Android EGL window");

  window = g_object_new (GST_GL_TYPE_WINDOW_ANDROID_EGL, NULL);

  return window;
}

static gboolean
gst_gl_window_android_egl_open (GstGLWindow * window, GError ** error)
{
  GstGLWindowAndroidEGL *window_egl;

  window_egl = GST_GL_WINDOW_ANDROID_EGL (window);

  window_egl->main_context = g_main_context_new ();
  window_egl->loop = g_main_loop_new (window_egl->main_context, FALSE);

  return TRUE;
}

static void
gst_gl_window_android_egl_close (GstGLWindow * window)
{
  GstGLWindowAndroidEGL *window_egl;

  window_egl = GST_GL_WINDOW_ANDROID_EGL (window);

  g_main_loop_unref (window_egl->loop);
  g_main_context_unref (window_egl->main_context);
}

static void
gst_gl_window_android_egl_run (GstGLWindow * window)
{
  GstGLWindowAndroidEGL *window_egl;

  window_egl = GST_GL_WINDOW_ANDROID_EGL (window);

  GST_LOG ("starting main loop");
  g_main_loop_run (window_egl->loop);
  GST_LOG ("exiting main loop");
}

static void
gst_gl_window_android_egl_quit (GstGLWindow * window)
{
  GstGLWindowAndroidEGL *window_egl;

  window_egl = GST_GL_WINDOW_ANDROID_EGL (window);

  GST_LOG ("sending quit");

  g_main_loop_quit (window_egl->loop);

  GST_LOG ("quit sent");
}

typedef struct _GstGLMessage
{
  GstGLWindowCB callback;
  gpointer data;
  GDestroyNotify destroy;
} GstGLMessage;

static gboolean
_run_message (GstGLMessage * message)
{
  if (message->callback)
    message->callback (message->data);

  if (message->destroy)
    message->destroy (message->data);

  g_slice_free (GstGLMessage, message);

  return FALSE;
}

static void
gst_gl_window_android_egl_send_message_async (GstGLWindow * window,
    GstGLWindowCB callback, gpointer data, GDestroyNotify destroy)
{
  GstGLWindowAndroidEGL *window_egl;
  GstGLMessage *message;

  window_egl = GST_GL_WINDOW_ANDROID_EGL (window);
  message = g_slice_new (GstGLMessage);

  message->callback = callback;
  message->data = data;
  message->destroy = destroy;

  g_main_context_invoke (window_egl->main_context, (GSourceFunc) _run_message,
      message);
}

static void
gst_gl_window_android_egl_set_window_handle (GstGLWindow * window,
    guintptr handle)
{
  GstGLWindowAndroidEGL *window_egl = GST_GL_WINDOW_ANDROID_EGL (window);

  window_egl->native_window = (EGLNativeWindowType) handle;
}

static guintptr
gst_gl_window_android_egl_get_window_handle (GstGLWindow * window)
{
  GstGLWindowAndroidEGL *window_egl = GST_GL_WINDOW_ANDROID_EGL (window);

  return (guintptr) window_egl->native_window;
}

static void
draw_cb (gpointer data)
{
  GstGLWindowAndroidEGL *window_egl = data;
  GstGLWindow *window = GST_GL_WINDOW (window_egl);
  GstGLContext *context = gst_gl_window_get_context (window);
  GstGLContextEGL *context_egl = GST_GL_CONTEXT_EGL (context);
  GstGLContextClass *context_class = GST_GL_CONTEXT_GET_CLASS (context);

  if (context_egl->egl_surface) {
    gint width, height;

    if (eglQuerySurface (context_egl->egl_display,
            context_egl->egl_surface, EGL_WIDTH, &width) &&
        eglQuerySurface (context_egl->egl_display,
            context_egl->egl_surface, EGL_HEIGHT, &height)
        && (width != window_egl->window_width
            || height != window_egl->window_height)) {
      window_egl->window_width = width;
      window_egl->window_height = height;

      if (window->resize)
        window->resize (window->resize_data, width, height);
    }
  }

  if (window->draw)
    window->draw (window->draw_data);

  context_class->swap_buffers (context);

  gst_object_unref (context);
}

static void
gst_gl_window_android_egl_draw (GstGLWindow * window)
{
  gst_gl_window_send_message (window, (GstGLWindowCB) draw_cb, window);
}

static guintptr
gst_gl_window_android_egl_get_display (GstGLWindow * window)
{
  return 0;
}