Blame ext/gl/gstglfilterapp.c

Packit 971217
/* 
Packit 971217
 * GStreamer
Packit 971217
 * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
/**
Packit 971217
 * SECTION:element-glfilterapp
Packit 971217
 * @title: glfilterapp
Packit 971217
 *
Packit 971217
 * The resize and redraw callbacks can be set from a client code.
Packit 971217
 *
Packit 971217
 * ## CLient callbacks
Packit 971217
 *
Packit 971217
 * The graphic scene can be written from a client code through the
Packit 971217
 * two glfilterapp properties.
Packit 971217
 *
Packit 971217
 * ## Examples
Packit 971217
 * see gst-plugins-gl/tests/examples/generic/recordgraphic
Packit 971217
 *
Packit 971217
 */
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include "gstglfilterapp.h"
Packit 971217
Packit 971217
#define GST_CAT_DEFAULT gst_gl_filter_app_debug
Packit 971217
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
Packit 971217
Packit 971217
enum
Packit 971217
{
Packit 971217
  SIGNAL_0,
Packit 971217
  CLIENT_DRAW_SIGNAL,
Packit 971217
  LAST_SIGNAL
Packit 971217
};
Packit 971217
Packit 971217
static guint gst_gl_filter_app_signals[LAST_SIGNAL] = { 0 };
Packit 971217
Packit 971217
#define DEBUG_INIT \
Packit 971217
  GST_DEBUG_CATEGORY_INIT (gst_gl_filter_app_debug, "glfilterapp", 0, "glfilterapp element");
Packit 971217
Packit 971217
#define gst_gl_filter_app_parent_class parent_class
Packit 971217
G_DEFINE_TYPE_WITH_CODE (GstGLFilterApp, gst_gl_filter_app,
Packit 971217
    GST_TYPE_GL_FILTER, DEBUG_INIT);
Packit 971217
Packit 971217
static void gst_gl_filter_app_set_property (GObject * object, guint prop_id,
Packit 971217
    const GValue * value, GParamSpec * pspec);
Packit 971217
static void gst_gl_filter_app_get_property (GObject * object, guint prop_id,
Packit 971217
    GValue * value, GParamSpec * pspec);
Packit 971217
Packit 971217
static gboolean gst_gl_filter_app_set_caps (GstGLFilter * filter,
Packit 971217
    GstCaps * incaps, GstCaps * outcaps);
Packit 971217
static gboolean gst_gl_filter_app_filter_texture (GstGLFilter * filter,
Packit 971217
    GstGLMemory * in_tex, GstGLMemory * out_tex);
Packit 971217
Packit 971217
static gboolean gst_gl_filter_app_gl_start (GstGLBaseFilter * base_filter);
Packit 971217
static void gst_gl_filter_app_gl_stop (GstGLBaseFilter * base_filter);
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_filter_app_class_init (GstGLFilterAppClass * klass)
Packit 971217
{
Packit 971217
  GObjectClass *gobject_class;
Packit 971217
  GstElementClass *element_class;
Packit 971217
Packit 971217
  gobject_class = (GObjectClass *) klass;
Packit 971217
  element_class = GST_ELEMENT_CLASS (klass);
Packit 971217
Packit 971217
  gst_gl_filter_add_rgba_pad_templates (GST_GL_FILTER_CLASS (klass));
Packit 971217
Packit 971217
  gobject_class->set_property = gst_gl_filter_app_set_property;
Packit 971217
  gobject_class->get_property = gst_gl_filter_app_get_property;
Packit 971217
Packit 971217
  GST_GL_BASE_FILTER_CLASS (klass)->gl_start = gst_gl_filter_app_gl_start;
Packit 971217
  GST_GL_BASE_FILTER_CLASS (klass)->gl_stop = gst_gl_filter_app_gl_stop;
Packit 971217
Packit 971217
  GST_GL_FILTER_CLASS (klass)->set_caps = gst_gl_filter_app_set_caps;
Packit 971217
  GST_GL_FILTER_CLASS (klass)->filter_texture =
Packit 971217
      gst_gl_filter_app_filter_texture;
Packit 971217
Packit 971217
  /**
Packit 971217
   * GstGLFilterApp::client-draw:
Packit 971217
   * @object: the #GstGLImageSink
Packit 971217
   * @texture: the #guint id of the texture.
Packit 971217
   * @width: the #guint width of the texture.
Packit 971217
   * @height: the #guint height of the texture.
Packit 971217
   *
Packit 971217
   * Will be emitted before to draw the texture.  The client should
Packit 971217
   * redraw the surface/contents with the @texture, @width and @height.
Packit 971217
   *
Packit 971217
   * Returns: whether the texture was redrawn by the signal.  If not, a
Packit 971217
   *          default redraw will occur.
Packit 971217
   */
Packit 971217
  gst_gl_filter_app_signals[CLIENT_DRAW_SIGNAL] =
Packit 971217
      g_signal_new ("client-draw", G_TYPE_FROM_CLASS (klass),
Packit 971217
      G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
Packit 971217
      G_TYPE_BOOLEAN, 3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
Packit 971217
Packit 971217
  gst_element_class_set_metadata (element_class,
Packit 971217
      "OpenGL application filter", "Filter/Effect",
Packit 971217
      "Use client callbacks to define the scene",
Packit 971217
      "Julien Isorce <julien.isorce@gmail.com>");
Packit 971217
Packit 971217
  GST_GL_BASE_FILTER_CLASS (klass)->supported_gl_api =
Packit 971217
      GST_GL_API_OPENGL | GST_GL_API_GLES2 | GST_GL_API_OPENGL3;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_filter_app_init (GstGLFilterApp * filter)
Packit 971217
{
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_filter_app_set_property (GObject * object, guint prop_id,
Packit 971217
    const GValue * value, GParamSpec * pspec)
Packit 971217
{
Packit 971217
  switch (prop_id) {
Packit 971217
    default:
Packit 971217
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 971217
      break;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_filter_app_get_property (GObject * object, guint prop_id,
Packit 971217
    GValue * value, GParamSpec * pspec)
Packit 971217
{
Packit 971217
  switch (prop_id) {
Packit 971217
    default:
Packit 971217
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Packit 971217
      break;
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_gl_filter_app_gl_start (GstGLBaseFilter * base_filter)
Packit 971217
{
Packit 971217
  GstGLFilter *filter = GST_GL_FILTER (base_filter);
Packit 971217
  GError *error = NULL;
Packit 971217
Packit 971217
  if (!(filter->default_shader =
Packit 971217
          gst_gl_shader_new_default (base_filter->context, &error))) {
Packit 971217
    GST_ELEMENT_ERROR (filter, RESOURCE, NOT_FOUND, ("%s",
Packit 971217
            "Failed to create the default shader"), ("%s", error->message));
Packit 971217
    return FALSE;
Packit 971217
  }
Packit 971217
Packit 971217
  return GST_GL_BASE_FILTER_CLASS (parent_class)->gl_start (base_filter);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_gl_filter_app_gl_stop (GstGLBaseFilter * base_filter)
Packit 971217
{
Packit 971217
  GstGLFilter *filter = GST_GL_FILTER (base_filter);
Packit 971217
Packit 971217
  if (filter->default_shader)
Packit 971217
    gst_object_unref (filter->default_shader);
Packit 971217
  filter->default_shader = NULL;
Packit 971217
Packit 971217
  GST_GL_BASE_FILTER_CLASS (parent_class)->gl_stop (base_filter);
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_gl_filter_app_set_caps (GstGLFilter * filter, GstCaps * incaps,
Packit 971217
    GstCaps * outcaps)
Packit 971217
{
Packit 971217
  //GstGLFilterApp* app_filter = GST_GL_FILTER_APP(filter);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
struct glcb2
Packit 971217
{
Packit 971217
  GstGLFilterApp *app;
Packit 971217
  GstGLMemory *in_tex;
Packit 971217
  GstGLMemory *out_tex;
Packit 971217
};
Packit 971217
Packit 971217
static gboolean
Packit 971217
_emit_draw_signal (gpointer data)
Packit 971217
{
Packit 971217
  struct glcb2 *cb = data;
Packit 971217
  gboolean drawn;
Packit 971217
Packit 971217
  g_signal_emit (cb->app, gst_gl_filter_app_signals[CLIENT_DRAW_SIGNAL], 0,
Packit 971217
      cb->in_tex->tex_id, gst_gl_memory_get_texture_width (cb->out_tex),
Packit 971217
      gst_gl_memory_get_texture_height (cb->out_tex), &drawn);
Packit 971217
Packit 971217
  return !drawn;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_gl_filter_app_filter_texture (GstGLFilter * filter, GstGLMemory * in_tex,
Packit 971217
    GstGLMemory * out_tex)
Packit 971217
{
Packit 971217
  GstGLFilterApp *app_filter = GST_GL_FILTER_APP (filter);
Packit 971217
  gboolean default_draw;
Packit 971217
  struct glcb2 cb;
Packit 971217
Packit 971217
  cb.app = app_filter;
Packit 971217
  cb.in_tex = in_tex;
Packit 971217
  cb.out_tex = out_tex;
Packit 971217
Packit 971217
  default_draw =
Packit 971217
      gst_gl_framebuffer_draw_to_texture (filter->fbo,
Packit 971217
      out_tex, _emit_draw_signal, &cb;;
Packit 971217
Packit 971217
  if (default_draw) {
Packit 971217
    gst_gl_filter_render_to_target_with_shader (filter, in_tex, out_tex,
Packit 971217
        filter->default_shader);
Packit 971217
  }
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}