summaryrefslogtreecommitdiff
path: root/gst-libs/gst/cairo/gstcairobuffer.c
blob: 3f12311d017b278de6c71b7a88a5362ea47fa013 (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
247
/* 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 "gstcairobuffer.h"

#include "gstcairobuffer-private.h"
#include "gstcairoformat-private.h"
#ifdef HAVE_XLIB
#  include "gstcairoxbuffer.h"
#endif

G_DEFINE_TYPE (GstCairoBuffer, gst_cairo_buffer, GST_TYPE_BUFFER)

     static void gst_cairo_buffer_finalize (GstMiniObject * object)
{
  GstCairoBuffer *cbuffer = GST_CAIRO_BUFFER (object);

  gst_cairo_format_free (cbuffer->format);

  GST_MINI_OBJECT_CLASS (gst_cairo_buffer_parent_class)->finalize (object);
}

static void
gst_cairo_buffer_class_init (GstCairoBufferClass * klass)
{
  GstMiniObjectClass *mini_object_class = GST_MINI_OBJECT_CLASS (klass);

  mini_object_class->finalize = gst_cairo_buffer_finalize;
}

static void
gst_cairo_buffer_init (GstCairoBuffer * buffer)
{
}

/**
 * gst_cairo_buffer_is_fixed:
 * @buffer: a buffer
 *
 * Checks if a buffer is fixed. Non-fixed buffers can change the format in
 * use. See gst_cairo_buffer_set_format() for examples of where this is
 * relevant.
 *
 * Returns: %TRUE if the format of the buffer can still be changed
 **/
gboolean
gst_cairo_buffer_is_fixed (GstBuffer * buffer)
{
  g_return_val_if_fail (GST_IS_BUFFER (buffer), TRUE);

  return !GST_IS_CAIRO_BUFFER (buffer) || GST_CAIRO_BUFFER (buffer)->fixed;
}

/**
 * gst_cairo_buffer_set_format:
 * @buffer: a buffer that is not fixed
 * @format: the format to use on @buffer
 *
 * Sets the format to use for a non-fixed buffer. This function is supposed
 * to be used in buffer allocation functions (see 
 * gst_pad_set_bufferalloc_function()) to update the format used by the 
 * buffer. This allows forwarding allocations of cairo buffers through a
 * pipeline even when the caps change.
 **/
void
gst_cairo_buffer_set_format (GstBuffer * buffer, const GstCairoFormat * format)
{
  GstCairoBuffer *cbuffer;

  g_return_if_fail (!gst_cairo_buffer_is_fixed (buffer));
  g_return_if_fail (format != NULL);
  g_return_if_fail (gst_cairo_format_is_native (format));

  cbuffer = GST_CAIRO_BUFFER (buffer);
  gst_cairo_format_free (cbuffer->format);
  cbuffer->format = gst_cairo_format_copy (format);
  GST_BUFFER_SIZE (cbuffer) = 4 * gst_cairo_format_get_width (format)
      * gst_cairo_format_get_height (format);
}

static cairo_surface_t *
gst_cairo_buffer_get_surface (GstCairoBuffer * cbuffer,
    cairo_surface_t * similar)
{
  GstCairoBufferClass *klass;
  cairo_surface_t *surface;

  klass = GST_CAIRO_BUFFER_GET_CLASS (cbuffer);
  surface = klass->get_surface (GST_BUFFER (cbuffer), similar, cbuffer->format);
  /* we only set fixed afterwards, so the get_surface() function can use
   * gst_cairo_buffer_is_fixed() to check if it's the first time it was 
   * called.
   */
  cbuffer->fixed = TRUE;
  return surface;
}

/**
 * gst_cairo_create_surface:
 * @buffer: buffer to create surface from
 * @format: format of the @buffer
 *
 * Creates a cairo surface from the given @buffer. The @buffer must conform 
 * to @format. You may only modify the surface and use it with 
 * cairo_create() if the @buffer is writable. Using it as a source for 
 * drawing operations is fine in all cases.
 *
 * Returns: A new surface referencing the contents of @buffer. 
 *          Use cairo_surface_destroy() after use.
 **/
cairo_surface_t *
gst_cairo_create_surface (GstBuffer * buffer, const GstCairoFormat * format)
{
  return gst_cairo_create_similar_surface (buffer, NULL, format);
}

/**
 * gst_cairo_create_similar_surface:
 * @buffer: buffer to create surface from
 * @surface: surface the returned surface should be similar to or %NULL if
 *           no similarities are desired
 * @format: format of the @buffer
 *
 * Creates a cairo surface from the given @buffer that is as similar as
 * possible to the given @surface. The @buffer must conform to @format.
 * You may only modify the surface and use it with cairo_create() if the 
 * @buffer is writable. Using it as a source for drawing operations is fine
 * in all cases.
 *
 * Returns: A new surface referencing the contents of buffer.
 *          Use cairo_surface_destroy() after use.
 **/
cairo_surface_t *
gst_cairo_create_similar_surface (GstBuffer * buffer,
    cairo_surface_t * surface, const GstCairoFormat * format)
{
  cairo_surface_t *result;

  g_return_val_if_fail (GST_IS_BUFFER (buffer), NULL);
  g_return_val_if_fail (format != NULL, NULL);

  if (GST_IS_CAIRO_BUFFER (buffer)) {
    GstCairoBuffer *cbuffer = GST_CAIRO_BUFFER (buffer);
    result = gst_cairo_buffer_get_surface (cbuffer, surface);
  } else {
    static cairo_user_data_key_t buffer_free;
    guint8 *data = GST_BUFFER_DATA (buffer);
    result =
        cairo_image_surface_create_with_pixman_format (data,
        gst_cairo_format_get_pixman_format (format),
        format->width, format->height,
        gst_cairo_format_get_stride (format, 0),
        data + gst_cairo_format_get_offset (format, 1),
        gst_cairo_format_get_stride (format, 1),
        data + gst_cairo_format_get_offset (format, 2),
        gst_cairo_format_get_stride (format, 2),
        data + gst_cairo_format_get_offset (format, 3),
        gst_cairo_format_get_stride (format, 3));
    gst_buffer_ref (buffer);
    cairo_surface_set_user_data (result, &buffer_free, buffer,
        (cairo_destroy_func_t) gst_buffer_unref);
  }

  return result;
}

/**
 * gst_cairo_buffer_new:
 * @format: format to create a buffer for
 *
 * This is a convenience function for fallback code. Creates a buffer 
 * of the correct size for the given format.
 *
 * <note>This function does never create any accelerated buffer. It 
 * always creates an image surface.</note>
 *
 * Returns: a new buffer
 **/
GstBuffer *
gst_cairo_buffer_new (const GstCairoFormat * format)
{
  g_return_val_if_fail (format != NULL, NULL);

  return gst_buffer_new_and_alloc (gst_cairo_format_get_buffer_size (format));
}

/**
 * gst_cairo_buffer_new_similar:
 * @surface: surface to create a buffer for
 * @format: format to create a buffer for
 *
 * Tries to create a buffer best suited for the given @format and output 
 * on @surface. This function tries to achieve highest possible performance
 * when later using the surface returned from FIXME: what function?
 * for rendering the buffer.
 * 
 * Usually, a special buffer will be created that will do all drawing 
 * operations using the same acceleration method as @surface. However, if this
 * can not be achieved because things like the @format or compile-time options
 * do not allow it, a fallback path is taken and a regular buffer will be
 * created.
 *
 * Returns: a new buffer
 **/
GstBuffer *
gst_cairo_buffer_new_similar (cairo_surface_t * surface,
    const GstCairoFormat * format)
{
  G_GNUC_UNUSED GstBuffer *buffer;

  g_return_val_if_fail (surface != NULL, NULL);
  g_return_val_if_fail (format != NULL, NULL);

  switch (cairo_surface_get_type (surface)) {
    case CAIRO_SURFACE_TYPE_XLIB:
#ifdef HAVE_XLIB
      buffer = gst_cairo_x_buffer_new (surface, format);
      if (buffer)
        return buffer;
#endif
      break;
    default:
      break;
  }

  return gst_cairo_buffer_new (format);
}