summaryrefslogtreecommitdiff
path: root/gst-libs/gst/cairo/gstcairopad.c
blob: a6c757497d381584d49331ba078515073d086027 (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
/* GStreamer
 * Copyright (C) 2009 Benjamin Otte <otte@gnome.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., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

#include "gstcairopad.h"

#include "gstcairobuffer.h"
#include "gstcairoformat-private.h"

/**
 * gst_cairo_pad_template_new:
 * @name: name of the template
 * @dir: direction of template
 * @presence: presence of pads created from this template
 *
 * This is a convenience macro for creating pad templates for use in 
 * elements that use gst-plugins-cairo. It is recommended to use this
 * function when creating pad templates in your elements and not use
 * GstStaticPadTemplate<!-- -->s, because then the element will 
 * automatically benefit from new formats added to gst-plugins-cairo.
 *
 * Returns: a new pad template accepting all formats that 
 *          gst-plugins-cairo supports.
 **/

/**
 * GstCairoRestrictCapsFunction:
 * @caps: caps to restrict
 * @data: data passed to gst_cairo_pad_alloc_buffer_full()
 *
 * Function used when fixating caps in gst_cairo_pad_alloc_buffer_full().
 * The function is supposed to return a possibly reduced #GstCaps that
 * should be used for caps negotiation. If the restricted caps would be
 * empty, this function should return %NULL.
 *
 * Returns: %NULL or a subset of the given caps.
 */

/**
 * gst_cairo_pad_alloc_buffer:
 * @pad: a source pad
 * @buffer: pointer to set the allocated buffer on
 *
 * Tries to allocate a new buffer on @pad for use with gstcairo. The @pad
 * must accept a subset of caps returned by gst_cairo_caps_any(). If @pad
 * is not yet negotiated, this function will attempt to do so and return
 * #GST_FLOW_NOT_NEGOTIATED if that fails.
 *
 * Returns: #GST_FLOW_OK on success
 **/
GstFlowReturn
gst_cairo_pad_alloc_buffer (GstPad * pad, GstBuffer ** buffer)
{
  return gst_cairo_pad_alloc_buffer_full (pad, NULL, NULL, TRUE, buffer);
}

/**
 * gst_cairo_pad_alloc_buffer_full:
 * @pad: a source pad
 * @restrict_func: function that restricts allowed caps or %NULL
 * @restrict_data: data to pass to @restrict_func
 * @set_caps: %TRUE to set caps on @pad if the pad doesn't have caps yet.
 * @buffer: location to store newly allocated buffer
 *
 * This is the function you should use to allocate buffers in your element.
 * Always use this function instead of gst_pad_alloc_buffer() as this 
 * function will use special features of cairo bufers that allow allocating
 * accelerated buffers even when different caps are in use by the pad(s).
 * See gst_cairo_buffer_is_fixed() for a discussion of this feature.
 * The function will use the caps of @pad to allocate the new buffer.
 *
 * Returns: a result code indicating success of the operation. Any result
 *          code other than GST_FLOW_OK is an error and buf should not be
 *          used. An error can occur if the pad is not connected or when the
 *          downstream peer elements cannot provide an acceptable buffer.
 *          MT safe.
 **/
GstFlowReturn
gst_cairo_pad_alloc_buffer_full (GstPad * pad,
    GstCairoRestrictCapsFunction restrict_func, gpointer restrict_data,
    gboolean set_caps, GstBuffer ** buffer)
{
  GstCairoFormat *format;
  GstCaps *caps;
  GstFlowReturn ret;

  g_return_val_if_fail (GST_IS_PAD (pad), GST_FLOW_ERROR);
  g_return_val_if_fail (GST_PAD_IS_SRC (pad), GST_FLOW_ERROR);
  g_return_val_if_fail (buffer != NULL, GST_FLOW_ERROR);

  caps = GST_PAD_CAPS (pad);
  if (G_LIKELY (caps)) {
    format = gst_cairo_format_new (caps);
    ret = gst_pad_alloc_buffer_and_set_caps (pad, GST_BUFFER_OFFSET_NONE,
        gst_cairo_format_get_buffer_size (format), caps, buffer);
    gst_cairo_format_free (format);
    return ret;
  } else {
    caps = gst_pad_get_allowed_caps (pad);

    if (caps && restrict_func)
      caps = restrict_func (caps, restrict_data);

    if (caps == NULL)
      return GST_FLOW_NOT_NEGOTIATED;

    if (!gst_caps_is_fixed (caps)) {
      caps = gst_caps_make_writable (caps);
      gst_pad_fixate_caps (pad, caps);
      gst_caps_truncate (caps);
      if (set_caps)
        gst_pad_set_caps (pad, caps);
    }

    format = gst_cairo_format_new (caps);
    ret = gst_pad_alloc_buffer_and_set_caps (pad,
        GST_BUFFER_OFFSET_NONE, gst_cairo_format_get_buffer_size (format),
        caps, buffer);
    gst_caps_unref (caps);
  }
  if (*buffer == NULL) {
    *buffer = gst_cairo_buffer_new (format);
    gst_buffer_set_caps (*buffer, caps);
  }
  gst_cairo_format_free (format);
  return ret;
}