gstglwindow.c 26.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * GStreamer
 * Copyright (C) 2012 Matthew Waters <ystreet00@gmail.com>
 *
 * 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.
 */

21 22 23 24 25 26 27 28 29 30
/**
 * SECTION:gstglwindow
 * @short_description: window/surface abstraction
 * @title: GstGLWindow
 * @see_also: #GstGLContext, #GstGLDisplay
 *
 * GstGLWindow represents a window that elements can render into.  A window can
 * either be a user visible window (onscreen) or hidden (offscreen).
 */

31
#ifdef HAVE_CONFIG_H
32 33 34
# include "config.h"
#endif

35
#include <gmodule.h>
36
#include <stdio.h>
37

38
#include "gl.h"
39
#include "gstglwindow.h"
40
#include "gstglwindow_private.h"
41

42 43
/* FIXME make this work with windowless contexts */

44
#if GST_GL_HAVE_WINDOW_X11
45
#include "x11/gstglwindow_x11.h"
46
#endif
47
#if GST_GL_HAVE_WINDOW_WIN32
48
#include "win32/gstglwindow_win32.h"
49
#endif
50
#if GST_GL_HAVE_WINDOW_COCOA
51
#include "cocoa/gstglwindow_cocoa.h"
52
#endif
53
#if GST_GL_HAVE_WINDOW_WAYLAND
54 55
#include "wayland/gstglwindow_wayland_egl.h"
#endif
56 57 58
#if GST_GL_HAVE_WINDOW_ANDROID
#include "android/gstglwindow_android_egl.h"
#endif
59 60 61
#if GST_GL_HAVE_WINDOW_EAGL
#include "eagl/gstglwindow_eagl.h"
#endif
62 63 64
#if GST_GL_HAVE_WINDOW_VIV_FB
#include "viv-fb/gstglwindow_viv_fb_egl.h"
#endif
65 66 67
#if GST_GL_HAVE_WINDOW_GBM
#include "gbm/gstglwindow_gbm_egl.h"
#endif
68 69 70
#if GST_GL_HAVE_WINDOW_DISPMANX
#include "dispmanx/gstglwindow_dispmanx_egl.h"
#endif
71

72 73 74 75 76
#define USING_OPENGL(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 1, 0))
#define USING_OPENGL3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 1))
#define USING_GLES(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES, 1, 0))
#define USING_GLES2(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 2, 0))
#define USING_GLES3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0))
77

78
#define GST_CAT_DEFAULT gst_gl_window_debug
79
GST_DEBUG_CATEGORY (GST_CAT_DEFAULT);
80 81

#define gst_gl_window_parent_class parent_class
82
G_DEFINE_ABSTRACT_TYPE (GstGLWindow, gst_gl_window, GST_TYPE_OBJECT);
83

84
#define GST_GL_WINDOW_GET_PRIVATE(o) \
85
  (G_TYPE_INSTANCE_GET_PRIVATE((o), GST_TYPE_GL_WINDOW, GstGLWindowPrivate))
86

87 88 89
static void gst_gl_window_default_draw (GstGLWindow * window);
static void gst_gl_window_default_run (GstGLWindow * window);
static void gst_gl_window_default_quit (GstGLWindow * window);
90 91
static void gst_gl_window_default_send_message (GstGLWindow * window,
    GstGLWindowCB callback, gpointer data);
92 93
static void gst_gl_window_default_send_message_async (GstGLWindow * window,
    GstGLWindowCB callback, gpointer data, GDestroyNotify destroy);
94

95 96
struct _GstGLWindowPrivate
{
97
  GMainLoop *loop;
98

99 100 101
  guint surface_width;
  guint surface_height;

102
  gboolean alive;
103

104 105
  GMutex sync_message_lock;
  GCond sync_message_cond;
106 107 108 109
};

static void gst_gl_window_finalize (GObject * object);

110 111 112 113 114 115 116 117 118 119 120 121
typedef struct _GstGLDummyWindow
{
  GstGLWindow parent;

  guintptr handle;
} GstGLDummyWindow;

typedef struct _GstGLDummyWindowCass
{
  GstGLWindowClass parent;
} GstGLDummyWindowClass;

122
static GstGLDummyWindow *gst_gl_dummy_window_new (void);
123

124 125 126 127 128 129 130 131 132 133
enum
{
  SIGNAL_0,
  EVENT_MOUSE_SIGNAL,
  EVENT_KEY_SIGNAL,
  LAST_SIGNAL
};

static guint gst_gl_window_signals[LAST_SIGNAL] = { 0 };

134 135 136 137 138 139
GQuark
gst_gl_window_error_quark (void)
{
  return g_quark_from_static_string ("gst-gl-window-error-quark");
}

140 141 142 143 144 145 146 147 148 149 150
static gboolean
gst_gl_window_default_open (GstGLWindow * window, GError ** error)
{
  return TRUE;
}

static void
gst_gl_window_default_close (GstGLWindow * window)
{
}

151 152 153 154 155 156 157 158 159 160 161 162
static void
_init_debug (void)
{
  static volatile gsize _init = 0;

  if (g_once_init_enter (&_init)) {
    GST_DEBUG_CATEGORY_INIT (gst_gl_window_debug, "glwindow", 0,
        "glwindow element");
    g_once_init_leave (&_init, 1);
  }
}

163 164 165
static void
gst_gl_window_init (GstGLWindow * window)
{
166 167
  GstGLWindowPrivate *priv = GST_GL_WINDOW_GET_PRIVATE (window);
  window->priv = priv;
168

169
  g_mutex_init (&window->lock);
170 171
  window->is_drawing = FALSE;

172
  g_weak_ref_init (&window->context_ref, NULL);
173

174 175 176
  g_mutex_init (&window->priv->sync_message_lock);
  g_cond_init (&window->priv->sync_message_cond);

177 178
  window->main_context = g_main_context_new ();
  priv->loop = g_main_loop_new (window->main_context, FALSE);
179 180 181 182 183
}

static void
gst_gl_window_class_init (GstGLWindowClass * klass)
{
184 185
  g_type_class_add_private (klass, sizeof (GstGLWindowPrivate));

186 187 188 189 190
  klass->open = GST_DEBUG_FUNCPTR (gst_gl_window_default_open);
  klass->close = GST_DEBUG_FUNCPTR (gst_gl_window_default_close);
  klass->run = GST_DEBUG_FUNCPTR (gst_gl_window_default_run);
  klass->quit = GST_DEBUG_FUNCPTR (gst_gl_window_default_quit);
  klass->draw = GST_DEBUG_FUNCPTR (gst_gl_window_default_draw);
191
  klass->send_message = GST_DEBUG_FUNCPTR (gst_gl_window_default_send_message);
192 193
  klass->send_message_async =
      GST_DEBUG_FUNCPTR (gst_gl_window_default_send_message_async);
194

195
  G_OBJECT_CLASS (klass)->finalize = gst_gl_window_finalize;
196 197 198 199 200 201 202 203 204 205 206

  /**
   * GstGLWindow::mouse-event:
   * @object: the #GstGLWindow
   * @id: the name of the event
   * @button: the id of the button
   * @x: the x coordinate of the mouse event
   * @y: the y coordinate of the mouse event
   *
   * Will be emitted when a mouse event is received by the GstGLwindow.
   *
207
   * Since: 1.6
208 209 210 211 212 213 214 215 216 217 218 219 220 221
   */
  gst_gl_window_signals[EVENT_MOUSE_SIGNAL] =
      g_signal_new ("mouse-event", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
      G_TYPE_NONE, 4, G_TYPE_STRING, G_TYPE_INT, G_TYPE_DOUBLE, G_TYPE_DOUBLE);

  /**
   * GstGLWindow::key-event:
   * @object: the #GstGLWindow
   * @id: the name of the event
   * @key: the id of the key pressed
   *
   * Will be emitted when a key event is received by the GstGLwindow.
   *
222
   * Since: 1.6
223 224 225 226 227
   */
  gst_gl_window_signals[EVENT_KEY_SIGNAL] =
      g_signal_new ("key-event", G_TYPE_FROM_CLASS (klass),
      G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
      G_TYPE_NONE, 2, G_TYPE_STRING, G_TYPE_STRING);
228 229

  _init_debug ();
230 231
}

232 233 234 235 236
/**
 * gst_gl_window_new:
 * @display: a #GstGLDisplay
 *
 * Returns: (transfer full): a new #GstGLWindow using @display's connection
237 238
 *
 * Since: 1.4
239
 */
240
GstGLWindow *
241
gst_gl_window_new (GstGLDisplay * display)
242 243 244
{
  GstGLWindow *window = NULL;
  const gchar *user_choice;
245

246 247
  g_return_val_if_fail (display != NULL, NULL);

248
  _init_debug ();
249 250

  user_choice = g_getenv ("GST_GL_WINDOW");
251 252
  GST_INFO ("creating a window, user choice:%s", user_choice);

253 254
#if GST_GL_HAVE_WINDOW_COCOA
  if (!window && (!user_choice || g_strstr_len (user_choice, 5, "cocoa")))
255
    window = GST_GL_WINDOW (gst_gl_window_cocoa_new (display));
256
#endif
257
#if GST_GL_HAVE_WINDOW_X11
258
  if (!window && (!user_choice || g_strstr_len (user_choice, 3, "x11")))
259
    window = GST_GL_WINDOW (gst_gl_window_x11_new (display));
260
#endif
261
#if GST_GL_HAVE_WINDOW_WIN32
262
  if (!window && (!user_choice || g_strstr_len (user_choice, 5, "win32")))
263
    window = GST_GL_WINDOW (gst_gl_window_win32_new (display));
264
#endif
265
#if GST_GL_HAVE_WINDOW_WAYLAND
266
  if (!window && (!user_choice || g_strstr_len (user_choice, 7, "wayland")))
267
    window = GST_GL_WINDOW (gst_gl_window_wayland_egl_new (display));
268
#endif
269 270
#if GST_GL_HAVE_WINDOW_DISPMANX
  if (!window && (!user_choice || g_strstr_len (user_choice, 8, "dispmanx")))
271
    window = GST_GL_WINDOW (gst_gl_window_dispmanx_egl_new (display));
272
#endif
273 274
#if GST_GL_HAVE_WINDOW_ANDROID
  if (!window && (!user_choice || g_strstr_len (user_choice, 7, "android")))
275
    window = GST_GL_WINDOW (gst_gl_window_android_egl_new (display));
276 277
#endif
#if GST_GL_HAVE_WINDOW_EAGL
278
  if (!window && (!user_choice || g_strstr_len (user_choice, 4, "eagl")))
279
    window = GST_GL_WINDOW (gst_gl_window_eagl_new (display));
280
#endif
281
#if GST_GL_HAVE_WINDOW_VIV_FB
282
  if (!window && (!user_choice || g_strstr_len (user_choice, 6, "viv-fb")))
283 284
    window = GST_GL_WINDOW (gst_gl_window_viv_fb_egl_new (display));
#endif
285 286 287 288
#if GST_GL_HAVE_WINDOW_GBM
  if (!window && (!user_choice || g_strstr_len (user_choice, 3, "gbm")))
    window = GST_GL_WINDOW (gst_gl_window_gbm_egl_new (display));
#endif
289

290
  if (!window) {
291
    /* subclass returned a NULL window */
292 293
    GST_WARNING ("Could not create window. user specified %s, creating dummy"
        " window", user_choice ? user_choice : "(null)");
294

295
    window = GST_GL_WINDOW (gst_gl_dummy_window_new ());
296 297
  }

298
  window->display = gst_object_ref (display);
299

300 301 302
  return window;
}

303 304 305 306
static void
gst_gl_window_finalize (GObject * object)
{
  GstGLWindow *window = GST_GL_WINDOW (object);
307
  GstGLWindowPrivate *priv = window->priv;
308

309 310 311
  if (priv->loop)
    g_main_loop_unref (priv->loop);

312 313 314
  if (window->main_context)
    g_main_context_unref (window->main_context);
  window->main_context = NULL;
315

316
  g_weak_ref_clear (&window->context_ref);
317

318
  g_mutex_clear (&window->lock);
319 320
  g_mutex_clear (&window->priv->sync_message_lock);
  g_cond_clear (&window->priv->sync_message_cond);
321
  gst_object_unref (window->display);
322

323 324 325
  G_OBJECT_CLASS (gst_gl_window_parent_class)->finalize (object);
}

326 327 328 329 330 331 332 333 334 335 336
typedef struct _GstSetWindowHandleCb
{
  GstGLWindow *window;
  guintptr handle;
} GstSetWindowHandleCb;

static void
_set_window_handle_cb (GstSetWindowHandleCb * data)
{
  GstGLContext *context = gst_gl_window_get_context (data->window);
  GstGLWindowClass *window_class = GST_GL_WINDOW_GET_CLASS (data->window);
337 338 339 340 341 342 343 344 345 346 347
  GThread *thread = NULL;

  /* deactivate if necessary */
  if (context) {
    thread = gst_gl_context_get_thread (context);
    if (thread) {
      /* This is only thread safe iff the context thread == g_thread_self() */
      g_assert (thread == g_thread_self ());
      gst_gl_context_activate (context, FALSE);
    }
  }
348 349

  window_class->set_window_handle (data->window, data->handle);
Edward Hervey's avatar
Edward Hervey committed
350

351 352 353 354 355 356 357 358
  /* reactivate */
  if (context && thread)
    gst_gl_context_activate (context, TRUE);

  if (context)
    gst_object_unref (context);
  if (thread)
    g_thread_unref (thread);
359 360 361 362 363 364 365 366 367
}

static void
_free_swh_cb (GstSetWindowHandleCb * data)
{
  gst_object_unref (data->window);
  g_slice_free (GstSetWindowHandleCb, data);
}

368 369 370 371 372 373 374
/**
 * gst_gl_window_set_window_handle:
 * @window: a #GstGLWindow
 * @handle: handle to the window
 *
 * Sets the window that this @window should render into.  Some implementations
 * require this to be called with a valid handle before drawing can commence.
375 376
 *
 * Since: 1.4
377
 */
378 379 380 381
void
gst_gl_window_set_window_handle (GstGLWindow * window, guintptr handle)
{
  GstGLWindowClass *window_class;
382
  GstSetWindowHandleCb *data;
383

384
  g_return_if_fail (GST_IS_GL_WINDOW (window));
385 386 387 388
  g_return_if_fail (handle != 0);
  window_class = GST_GL_WINDOW_GET_CLASS (window);
  g_return_if_fail (window_class->set_window_handle != NULL);

389 390 391 392 393 394 395 396 397 398
  data = g_slice_new (GstSetWindowHandleCb);
  data->window = gst_object_ref (window);
  data->handle = handle;

  /* FIXME: Move to a message which deactivates, calls implementation, activates */
  gst_gl_window_send_message_async (window,
      (GstGLWindowCB) _set_window_handle_cb, data,
      (GDestroyNotify) _free_swh_cb);

  /* window_class->set_window_handle (window, handle); */
399 400
}

401 402 403 404 405 406
static void
draw_cb (gpointer data)
{
  GstGLWindow *window = GST_GL_WINDOW (data);
  GstGLContext *context = gst_gl_window_get_context (window);

407 408 409 410 411 412 413
  if (window->queue_resize) {
    guint width, height;

    gst_gl_window_get_surface_dimensions (window, &width, &height);
    gst_gl_window_resize (window, width, height);
  }

414 415 416
  if (window->draw)
    window->draw (window->draw_data);

417
  gst_gl_context_swap_buffers (context);
418 419 420 421 422 423 424 425 426 427

  gst_object_unref (context);
}

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

428 429 430 431 432
/**
 * gst_gl_window_draw:
 * @window: a #GstGLWindow
 *
 * Redraw the window contents.  Implementations should invoke the draw callback.
433 434
 *
 * Since: 1.4
435
 */
436
void
437
gst_gl_window_draw (GstGLWindow * window)
438 439 440
{
  GstGLWindowClass *window_class;

441
  g_return_if_fail (GST_IS_GL_WINDOW (window));
442 443 444
  window_class = GST_GL_WINDOW_GET_CLASS (window);
  g_return_if_fail (window_class->draw != NULL);

445 446 447 448 449
  /* avoid to overload the drawer */
  if (window->is_drawing) {
    return;
  }

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
  window_class->draw (window);
}

/**
 * gst_gl_window_set_preferred_size:
 * @window: a #GstGLWindow
 * @width: new preferred width
 * @height: new preferred height
 *
 * Set the preferred width and height of the window.  Implementations are free
 * to ignore this information.
 *
 * Since: 1.6
 */
void
gst_gl_window_set_preferred_size (GstGLWindow * window, gint width, gint height)
{
  GstGLWindowClass *window_class;

469
  g_return_if_fail (GST_IS_GL_WINDOW (window));
470 471 472 473
  window_class = GST_GL_WINDOW_GET_CLASS (window);

  if (window_class->set_preferred_size)
    window_class->set_preferred_size (window, width, height);
474 475
}

476 477 478 479 480 481 482 483 484 485 486 487 488
/**
 * gst_gl_window_show:
 * @window: a #GstGLWindow
 *
 * Present the window to the screen.
 *
 * Since: 1.6
 */
void
gst_gl_window_show (GstGLWindow * window)
{
  GstGLWindowClass *window_class;

489
  g_return_if_fail (GST_IS_GL_WINDOW (window));
490 491 492 493 494 495
  window_class = GST_GL_WINDOW_GET_CLASS (window);

  if (window_class->show)
    window_class->show (window);
}

496 497 498
static void
gst_gl_window_default_run (GstGLWindow * window)
{
499 500
  GstGLWindowPrivate *priv = window->priv;

501
  g_main_context_push_thread_default (window->main_context);
502 503 504

  g_main_loop_run (priv->loop);

505
  g_main_context_pop_thread_default (window->main_context);
506 507
}

508 509 510 511 512
/**
 * gst_gl_window_run:
 * @window: a #GstGLWindow
 *
 * Start the execution of the runloop.
513 514
 *
 * Since: 1.4
515
 */
516 517 518 519 520
void
gst_gl_window_run (GstGLWindow * window)
{
  GstGLWindowClass *window_class;

521
  g_return_if_fail (GST_IS_GL_WINDOW (window));
522 523 524
  window_class = GST_GL_WINDOW_GET_CLASS (window);
  g_return_if_fail (window_class->run != NULL);

525
  window->priv->alive = TRUE;
526 527 528
  window_class->run (window);
}

529 530 531 532 533 534
static void
gst_gl_window_default_quit (GstGLWindow * window)
{
  g_main_loop_quit (window->priv->loop);
}

535 536 537 538 539
/**
 * gst_gl_window_quit:
 * @window: a #GstGLWindow
 *
 * Quit the runloop's execution.
540 541
 *
 * Since: 1.4
542
 */
543
void
544
gst_gl_window_quit (GstGLWindow * window)
545 546 547
{
  GstGLWindowClass *window_class;

548
  g_return_if_fail (GST_IS_GL_WINDOW (window));
549 550 551 552 553
  window_class = GST_GL_WINDOW_GET_CLASS (window);
  g_return_if_fail (window_class->quit != NULL);

  GST_GL_WINDOW_LOCK (window);

554 555
  window->priv->alive = FALSE;

556
  window_class->quit (window);
557

558 559
  GST_INFO ("quit sent to gl window loop");

560
  GST_GL_WINDOW_UNLOCK (window);
561 562
}

563 564
typedef struct _GstGLSyncMessage
{
565
  GstGLWindow *window;
566 567 568 569 570 571 572 573 574 575 576 577 578
  gboolean fired;

  GstGLWindowCB callback;
  gpointer data;
} GstGLSyncMessage;

static void
_run_message_sync (GstGLSyncMessage * message)
{

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

579
  g_mutex_lock (&message->window->priv->sync_message_lock);
580
  message->fired = TRUE;
581 582
  g_cond_broadcast (&message->window->priv->sync_message_cond);
  g_mutex_unlock (&message->window->priv->sync_message_lock);
583 584 585 586 587 588 589 590
}

void
gst_gl_window_default_send_message (GstGLWindow * window,
    GstGLWindowCB callback, gpointer data)
{
  GstGLSyncMessage message;

591
  message.window = window;
592 593 594 595 596 597 598
  message.callback = callback;
  message.data = data;
  message.fired = FALSE;

  gst_gl_window_send_message_async (window, (GstGLWindowCB) _run_message_sync,
      &message, NULL);

599
  g_mutex_lock (&window->priv->sync_message_lock);
600 601 602

  /* block until opengl calls have been executed in the gl thread */
  while (!message.fired)
603 604 605
    g_cond_wait (&window->priv->sync_message_cond,
        &window->priv->sync_message_lock);
  g_mutex_unlock (&window->priv->sync_message_lock);
606 607
}

608 609 610 611 612 613 614 615
/**
 * gst_gl_window_send_message:
 * @window: a #GstGLWindow
 * @callback: (scope async): function to invoke
 * @data: (closure): data to invoke @callback with
 *
 * Invoke @callback with data on the window thread.  @callback is guarenteed to
 * have executed when this function returns.
616 617
 *
 * Since: 1.4
618
 */
619 620 621 622 623 624
void
gst_gl_window_send_message (GstGLWindow * window, GstGLWindowCB callback,
    gpointer data)
{
  GstGLWindowClass *window_class;

625
  g_return_if_fail (GST_IS_GL_WINDOW (window));
626 627
  g_return_if_fail (callback != NULL);
  window_class = GST_GL_WINDOW_GET_CLASS (window);
628
  g_return_if_fail (window_class->send_message != NULL);
629 630 631 632

  window_class->send_message (window, callback, data);
}

633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
typedef struct _GstGLAsyncMessage
{
  GstGLWindowCB callback;
  gpointer data;
  GDestroyNotify destroy;
} GstGLAsyncMessage;

static gboolean
_run_message_async (GstGLAsyncMessage * message)
{
  if (message->callback)
    message->callback (message->data);

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

  g_slice_free (GstGLAsyncMessage, message);

  return FALSE;
}

static void
gst_gl_window_default_send_message_async (GstGLWindow * window,
    GstGLWindowCB callback, gpointer data, GDestroyNotify destroy)
{
  GstGLAsyncMessage *message = g_slice_new (GstGLAsyncMessage);

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

664
  g_main_context_invoke (window->main_context, (GSourceFunc) _run_message_async,
665 666 667
      message);
}

668 669 670 671 672
/**
 * gst_gl_window_send_message_async:
 * @window: a #GstGLWindow
 * @callback: (scope async): function to invoke
 * @data: (closure): data to invoke @callback with
673
 * @destroy: called when @data is not needed anymore
674 675 676
 *
 * Invoke @callback with @data on the window thread.  The callback may not
 * have been executed when this function returns.
677 678
 *
 * Since: 1.4
679
 */
680 681 682 683 684 685
void
gst_gl_window_send_message_async (GstGLWindow * window, GstGLWindowCB callback,
    gpointer data, GDestroyNotify destroy)
{
  GstGLWindowClass *window_class;

686
  g_return_if_fail (GST_IS_GL_WINDOW (window));
687 688 689 690 691 692 693
  g_return_if_fail (callback != NULL);
  window_class = GST_GL_WINDOW_GET_CLASS (window);
  g_return_if_fail (window_class->send_message_async != NULL);

  window_class->send_message_async (window, callback, data, destroy);
}

694 695 696 697 698
/**
 * gst_gl_window_set_draw_callback:
 * @window: a #GstGLWindow
 * @callback: (scope notified): function to invoke
 * @data: (closure): data to invoke @callback with
699
 * @destroy_notify: called when @data is not needed any more
700 701
 *
 * Sets the draw callback called everytime gst_gl_window_draw() is called
702 703
 *
 * Since: 1.4
704
 */
705 706
void
gst_gl_window_set_draw_callback (GstGLWindow * window, GstGLWindowCB callback,
707
    gpointer data, GDestroyNotify destroy_notify)
708
{
709
  g_return_if_fail (GST_IS_GL_WINDOW (window));
710 711 712

  GST_GL_WINDOW_LOCK (window);

713 714 715
  if (window->draw_notify)
    window->draw_notify (window->draw_data);

716 717
  window->draw = callback;
  window->draw_data = data;
718
  window->draw_notify = destroy_notify;
719 720 721 722

  GST_GL_WINDOW_UNLOCK (window);
}

723 724 725 726 727
/**
 * gst_gl_window_set_resize_callback:
 * @window: a #GstGLWindow
 * @callback: (scope notified): function to invoke
 * @data: (closure): data to invoke @callback with
728
 * @destroy_notify: called when @data is not needed any more
729 730
 *
 * Sets the resize callback called everytime a resize of the window occurs.
731 732
 *
 * Since: 1.4
733
 */
734 735
void
gst_gl_window_set_resize_callback (GstGLWindow * window,
736
    GstGLWindowResizeCB callback, gpointer data, GDestroyNotify destroy_notify)
737
{
738
  g_return_if_fail (GST_IS_GL_WINDOW (window));
739 740 741

  GST_GL_WINDOW_LOCK (window);

742 743 744
  if (window->resize_notify)
    window->resize_notify (window->resize_data);

745 746
  window->resize = callback;
  window->resize_data = data;
747
  window->resize_notify = destroy_notify;
748 749 750 751

  GST_GL_WINDOW_UNLOCK (window);
}

752 753 754 755 756
/**
 * gst_gl_window_set_close_callback:
 * @window: a #GstGLWindow
 * @callback: (scope notified): function to invoke
 * @data: (closure): data to invoke @callback with
757
 * @destroy_notify: called when @data is not needed any more
758 759
 *
 * Sets the callback called when the window is about to close.
760 761
 *
 * Since: 1.4
762
 */
763 764
void
gst_gl_window_set_close_callback (GstGLWindow * window, GstGLWindowCB callback,
765
    gpointer data, GDestroyNotify destroy_notify)
766
{
767
  g_return_if_fail (GST_IS_GL_WINDOW (window));
768 769 770

  GST_GL_WINDOW_LOCK (window);

771 772 773
  if (window->close_notify)
    window->close_notify (window->close_data);

774 775
  window->close = callback;
  window->close_data = data;
776
  window->close_notify = destroy_notify;
777 778 779

  GST_GL_WINDOW_UNLOCK (window);
}
780

781 782 783 784 785
/**
 * gst_gl_window_get_display:
 * @window: a #GstGLWindow
 *
 * Returns: the windowing system display handle for this @window
786 787
 *
 * Since: 1.4
788
 */
789 790
guintptr
gst_gl_window_get_display (GstGLWindow * window)
791 792 793
{
  GstGLWindowClass *window_class;

794
  g_return_val_if_fail (GST_IS_GL_WINDOW (window), 0);
795
  window_class = GST_GL_WINDOW_GET_CLASS (window);
796 797
  g_return_val_if_fail (window_class->get_display != NULL, 0);

798
  return window_class->get_display (window);
799
}
800

801 802 803 804 805
/**
 * gst_gl_window_get_window_handle:
 * @window: a #GstGLWindow
 *
 * Returns: the window handle we are currently rendering into
806 807
 *
 * Since: 1.4
808
 */
809 810
guintptr
gst_gl_window_get_window_handle (GstGLWindow * window)
811 812 813
{
  GstGLWindowClass *window_class;

814
  g_return_val_if_fail (GST_IS_GL_WINDOW (window), 0);
815
  window_class = GST_GL_WINDOW_GET_CLASS (window);
816
  g_return_val_if_fail (window_class->get_window_handle != NULL, 0);
817

818
  return window_class->get_window_handle (window);
819 820
}

821 822 823 824 825
/**
 * gst_gl_window_get_context:
 * @window: a #GstGLWindow
 *
 * Returns: (transfer full): the #GstGLContext associated with this @window
826 827
 *
 * Since: 1.4
828
 */
829 830
GstGLContext *
gst_gl_window_get_context (GstGLWindow * window)
831
{
832
  g_return_val_if_fail (GST_IS_GL_WINDOW (window), NULL);
833

834
  return (GstGLContext *) g_weak_ref_get (&window->context_ref);
835
}
836

837 838 839 840 841
/**
 * gst_gl_window_get_surface_dimensions:
 * @window: a #GstGLWindow
 * @width: (out): resulting surface width
 * @height: (out): resulting surface height
842 843
 *
 * Since: 1.6
844 845 846 847 848
 */
void
gst_gl_window_get_surface_dimensions (GstGLWindow * window, guint * width,
    guint * height)
{
849 850 851 852
  if (width)
    *width = window->priv->surface_width;
  if (height)
    *height = window->priv->surface_height;
853 854
}

855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870
void
gst_gl_window_send_key_event (GstGLWindow * window, const char *event_type,
    const char *key_str)
{
  g_signal_emit (window, gst_gl_window_signals[EVENT_KEY_SIGNAL], 0,
      event_type, key_str);
}

void
gst_gl_window_send_mouse_event (GstGLWindow * window, const char *event_type,
    int button, double posx, double posy)
{
  g_signal_emit (window, gst_gl_window_signals[EVENT_MOUSE_SIGNAL], 0,
      event_type, button, posx, posy);
}

871 872 873 874 875
/**
 * gst_gl_window_handle_events:
 * @window: a #GstGLWindow
 * @handle_events: a #gboolean indicating if events should be handled or not.
 *
876 877 878 879 880
 * Tell a @window that it should handle events from the window system. These
 * events are forwarded upstream as navigation events. In some window systems
 * events are not propagated in the window hierarchy if a client is listening
 * for them. This method allows you to disable events handling completely
 * from the @window.
881 882 883 884 885 886
 */
void
gst_gl_window_handle_events (GstGLWindow * window, gboolean handle_events)
{
  GstGLWindowClass *window_class;

887
  g_return_if_fail (GST_IS_GL_WINDOW (window));
888 889
  window_class = GST_GL_WINDOW_GET_CLASS (window);

890 891
  if (window_class->handle_events)
    window_class->handle_events (window, handle_events);
892
}
893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913

/**
 * gst_gl_window_set_render_rectangle:
 * @window: a #GstGLWindow
 * @x: x position
 * @y: y position
 * @width: width
 * @height: height
 *
 * Tell a @window that it should render into a specific region of the window
 * according to the #GstVideoOverlay interface.
 *
 * Returns: whether the specified region could be set
 */
gboolean
gst_gl_window_set_render_rectangle (GstGLWindow * window, gint x, gint y,
    gint width, gint height)
{
  GstGLWindowClass *window_class;
  gboolean ret = FALSE;

914
  g_return_val_if_fail (GST_IS_GL_WINDOW (window), FALSE);
915 916
  window_class = GST_GL_WINDOW_GET_CLASS (window);

917 918 919 920 921 922 923
  /* When x/y is smaller then reset the render rectangle */
  if (x < 0 || y < 0) {
    x = y = 0;
    width = window->priv->surface_width;
    height = window->priv->surface_height;
  }

924 925 926 927 928 929 930 931
  if (x < 0 || y < 0 || width <= 0 || height <= 0)
    return FALSE;

  if (window_class->set_render_rectangle)
    ret = window_class->set_render_rectangle (window, x, y, width, height);

  return ret;
}
932 933 934 935 936 937

void
gst_gl_window_queue_resize (GstGLWindow * window)
{
  GstGLWindowClass *window_class;

938
  g_return_if_fail (GST_IS_GL_WINDOW (window));
939 940 941 942 943 944 945
  window_class = GST_GL_WINDOW_GET_CLASS (window);

  window->queue_resize = TRUE;
  if (window_class->queue_resize)
    window_class->queue_resize (window);
}

946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
struct resize_data
{
  GstGLWindow *window;
  guint width, height;
};

static void
_on_resize (gpointer data)
{
  struct resize_data *resize = data;

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

961 962 963
void
gst_gl_window_resize (GstGLWindow * window, guint width, guint height)
{
964
  g_return_if_fail (GST_IS_GL_WINDOW (window));
965

966 967 968 969 970 971 972 973 974
  if (window->resize) {
    struct resize_data resize = { 0, };

    resize.window = window;
    resize.width = width;
    resize.height = height;

    gst_gl_window_send_message (window, (GstGLWindowCB) _on_resize, &resize);
  }
975 976 977

  window->priv->surface_width = width;
  window->priv->surface_height = height;
978 979

  window->queue_resize = FALSE;
980
}
981

982
static GType gst_gl_dummy_window_get_type (void);
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026

G_DEFINE_TYPE (GstGLDummyWindow, gst_gl_dummy_window, GST_TYPE_GL_WINDOW);

static void
gst_gl_dummy_window_set_window_handle (GstGLWindow * window, guintptr handle)
{
  GstGLDummyWindow *dummy = (GstGLDummyWindow *) window;

  dummy->handle = handle;
}

static guintptr
gst_gl_dummy_window_get_window_handle (GstGLWindow * window)
{
  GstGLDummyWindow *dummy = (GstGLDummyWindow *) window;

  return (guintptr) dummy->handle;
}

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

static void
gst_gl_dummy_window_class_init (GstGLDummyWindowClass * klass)
{
  GstGLWindowClass *window_class = (GstGLWindowClass *) klass;

  window_class->get_display =
      GST_DEBUG_FUNCPTR (gst_gl_dummy_window_get_display);
  window_class->get_window_handle =
      GST_DEBUG_FUNCPTR (gst_gl_dummy_window_get_window_handle);
  window_class->set_window_handle =
      GST_DEBUG_FUNCPTR (gst_gl_dummy_window_set_window_handle);
}

static void
gst_gl_dummy_window_init (GstGLDummyWindow * dummy)
{
  dummy->handle = 0;
}

1027
static GstGLDummyWindow *
1028 1029
gst_gl_dummy_window_new (void)
{
1030 1031 1032 1033 1034 1035
  GstGLDummyWindow *window;

  window = g_object_new (gst_gl_dummy_window_get_type (), NULL);
  gst_object_ref_sink (window);

  return window;
1036
}