3c9f42be323c1b965e8b74feb9d3e5d01612d268
[platform/upstream/gst-plugins-base.git] / gst-libs / gst / gl / gstglmemory.h
1 /*
2  * GStreamer
3  * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20
21 #ifndef _GST_GL_MEMORY_H_
22 #define _GST_GL_MEMORY_H_
23
24 #include <gst/gl/gstglbasememory.h>
25 #include <gst/gl/gstglformat.h>
26
27 G_BEGIN_DECLS
28
29 #define GST_TYPE_GL_MEMORY_ALLOCATOR (gst_gl_memory_allocator_get_type())
30 GST_GL_API
31 GType gst_gl_memory_allocator_get_type(void);
32
33 #define GST_IS_GL_MEMORY_ALLOCATOR(obj)              (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_GL_MEMORY_ALLOCATOR))
34 #define GST_IS_GL_MEMORY_ALLOCATOR_CLASS(klass)      (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_GL_MEMORY_ALLOCATOR))
35 #define GST_GL_MEMORY_ALLOCATOR_GET_CLASS(obj)       (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_GL_MEMORY_ALLOCATOR, GstGLMemoryAllocatorClass))
36 #define GST_GL_MEMORY_ALLOCATOR(obj)                 (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_GL_MEMORY_ALLOCATOR, GstGLMemoryAllocator))
37 #define GST_GL_MEMORY_ALLOCATOR_CLASS(klass)         (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_GL_MEMORY_ALLOCATOR, GstGLMemoryAllocatorClass))
38 #define GST_GL_MEMORY_ALLOCATOR_CAST(obj)            ((GstGLMemoryAllocator *)(obj))
39
40 #define GST_GL_MEMORY_CAST(obj) ((GstGLMemory *) obj)
41
42 /**
43  * GST_CAPS_FEATURE_MEMORY_GL_MEMORY:
44  *
45  * Name of the caps feature for indicating the use of #GstGLMemory
46  */
47 #define GST_CAPS_FEATURE_MEMORY_GL_MEMORY "memory:GLMemory"
48 /**
49  * GST_GL_MEMORY_VIDEO_FORMATS_STR:
50  *
51  * List of video formats that are supported by #GstGLMemory
52  */
53 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
54 #define MEMORY_VIDEO_EXT_FORMATS ", BGR10A2_LE, RGB10A2_LE, P010_10LE, P016_LE"
55 #else
56 #define MEMORY_VIDEO_EXT_FORMATS ", P010_10BE, P016_BE"
57 #endif
58
59 #define GST_GL_MEMORY_VIDEO_FORMATS_STR \
60     "{ RGBA, BGRA, RGBx, BGRx, ARGB, ABGR, xRGB, xBGR, RGB, BGR, RGB16, BGR16, " \
61     "AYUV, VUYA, Y410, I420, YV12, NV12, NV21, YUY2, UYVY, Y41B, Y42B, Y444, " \
62     "GRAY8, GRAY16_LE, GRAY16_BE, ARGB64" MEMORY_VIDEO_EXT_FORMATS "}"
63
64 /**
65  * GstGLMemory:
66  * @mem: the parent #GstGLBaseMemory object
67  * @tex_id: the GL texture id for this memory
68  * @tex_target: the GL texture target for this memory
69  * @tex_format: the texture type
70  * @info: the texture's #GstVideoInfo
71  * @valign: data alignment for system memory mapping
72  * @plane: data plane in @info
73  * @tex_scaling: GL shader scaling parameters for @valign and/or width/height
74  *
75  * Represents information about a GL texture
76  */
77 struct _GstGLMemory
78 {
79   GstGLBaseMemory           mem;
80
81   guint                     tex_id;
82   GstGLTextureTarget        tex_target;
83   GstGLFormat               tex_format;
84   GstVideoInfo              info;
85   GstVideoAlignment         valign;
86   guint                     plane;
87   gfloat                    tex_scaling[2];
88
89   /*< protected >*/
90   gboolean                  texture_wrapped;
91   guint                     unpack_length;
92   guint                     tex_width;
93
94   /*< private >*/
95   gpointer                  _padding[GST_PADDING];
96 };
97
98
99 #define GST_TYPE_GL_VIDEO_ALLOCATION_PARAMS (gst_gl_video_allocation_params_get_type())
100 GST_GL_API
101 GType gst_gl_video_allocation_params_get_type (void);
102
103 typedef struct _GstGLVideoAllocationParams GstGLVideoAllocationParams;
104
105 /**
106  * GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_VIDEO:
107  *
108  * GL allocation flag indicating the allocation of 2D video frames
109  */
110 #define GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_VIDEO (1 << 3)
111
112 /**
113  * GstGLVideoAllocationParams:
114  * @parent: the parent #GstGLAllocationParams structure
115  * @v_info: the #GstVideoInfo to allocate
116  * @plane: the video plane index to allocate
117  * @valign: the #GstVideoAlignment to align the system representation to (may be %NULL for the default)
118  * @target: the #GstGLTextureTarget to allocate
119  * @tex_format: the #GstGLFormat to allocate
120  */
121 struct _GstGLVideoAllocationParams
122 {
123   GstGLAllocationParams  parent;
124
125   GstVideoInfo          *v_info;
126   guint                  plane;
127   GstVideoAlignment     *valign;
128   GstGLTextureTarget     target;
129   GstGLFormat            tex_format;
130
131   /*< private >*/
132   gpointer               _padding[GST_PADDING];
133 };
134
135 GST_GL_API
136 gboolean        gst_gl_video_allocation_params_init_full        (GstGLVideoAllocationParams * params,
137                                                                  gsize struct_size,
138                                                                  guint alloc_flags,
139                                                                  GstGLAllocationParamsCopyFunc copy,
140                                                                  GstGLAllocationParamsFreeFunc free,
141                                                                  GstGLContext * context,
142                                                                  GstAllocationParams * alloc_params,
143                                                                  GstVideoInfo * v_info,
144                                                                  guint plane,
145                                                                  GstVideoAlignment * valign,
146                                                                  GstGLTextureTarget target,
147                                                                  GstGLFormat tex_format,
148                                                                  gpointer wrapped_data,
149                                                                  gpointer gl_handle,
150                                                                  gpointer user_data,
151                                                                  GDestroyNotify notify);
152 GST_GL_API
153 GstGLVideoAllocationParams * gst_gl_video_allocation_params_new (GstGLContext * context,
154                                                                  GstAllocationParams * alloc_params,
155                                                                  GstVideoInfo * v_info,
156                                                                  guint plane,
157                                                                  GstVideoAlignment * valign,
158                                                                  GstGLTextureTarget target,
159                                                                  GstGLFormat tex_format);
160 GST_GL_API
161 GstGLVideoAllocationParams * gst_gl_video_allocation_params_new_wrapped_data    (GstGLContext * context,
162                                                                                  GstAllocationParams * alloc_params,
163                                                                                  GstVideoInfo * v_info,
164                                                                                  guint plane,
165                                                                                  GstVideoAlignment * valign,
166                                                                                  GstGLTextureTarget target,
167                                                                                  GstGLFormat tex_format,
168                                                                                  gpointer wrapped_data,
169                                                                                  gpointer user_data,
170                                                                                  GDestroyNotify notify);
171
172 GST_GL_API
173 GstGLVideoAllocationParams * gst_gl_video_allocation_params_new_wrapped_texture (GstGLContext * context,
174                                                                                  GstAllocationParams * alloc_params,
175                                                                                  GstVideoInfo * v_info,
176                                                                                  guint plane,
177                                                                                  GstVideoAlignment * valign,
178                                                                                  GstGLTextureTarget target,
179                                                                                  GstGLFormat tex_format,
180                                                                                  guint tex_id,
181                                                                                  gpointer user_data,
182                                                                                  GDestroyNotify notify);
183
184 GST_GL_API
185 GstGLVideoAllocationParams * gst_gl_video_allocation_params_new_wrapped_gl_handle (GstGLContext * context,
186                                                                                  GstAllocationParams * alloc_params,
187                                                                                  GstVideoInfo * v_info,
188                                                                                  guint plane,
189                                                                                  GstVideoAlignment * valign,
190                                                                                  GstGLTextureTarget target,
191                                                                                  GstGLFormat tex_format,
192                                                                                  gpointer gl_handle,
193                                                                                  gpointer user_data,
194                                                                                  GDestroyNotify notify);
195
196 /* subclass usage */
197 GST_GL_API
198 void            gst_gl_video_allocation_params_free_data    (GstGLVideoAllocationParams * params);
199 /* subclass usage */
200 GST_GL_API
201 void            gst_gl_video_allocation_params_copy_data    (GstGLVideoAllocationParams * src_vid,
202                                                              GstGLVideoAllocationParams * dest_vid);
203
204 /**
205  * GstGLMemoryAllocator
206  *
207  * Opaque #GstGLMemoryAllocator struct
208  */
209 struct _GstGLMemoryAllocator
210 {
211   /*< private >*/
212   GstGLBaseMemoryAllocator parent;
213
214   gpointer _padding[GST_PADDING];
215 };
216
217 /**
218  * GstGLMemoryAllocatorClass:
219  * @map: provide a custom map implementation
220  * @copy: provide a custom copy implementation
221  * @unmap: provide a custom unmap implementation
222  */
223 struct _GstGLMemoryAllocatorClass
224 {
225   /*< private >*/
226   GstGLBaseMemoryAllocatorClass             parent_class;
227
228   /*< public >*/
229   GstGLBaseMemoryAllocatorMapFunction       map;
230   GstGLBaseMemoryAllocatorCopyFunction      copy;
231   GstGLBaseMemoryAllocatorUnmapFunction     unmap;
232
233   /*< private >*/
234   gpointer                                  _padding[GST_PADDING];
235 };
236
237 #include <gst/gl/gstglbasememory.h>
238
239 /**
240  * GST_GL_MEMORY_ALLOCATOR_NAME:
241  *
242  * The name of the GL memory allocator
243  */
244 #define GST_GL_MEMORY_ALLOCATOR_NAME   "GLMemory"
245
246 GST_GL_API
247 void            gst_gl_memory_init_once (void);
248 GST_GL_API
249 gboolean        gst_is_gl_memory (GstMemory * mem);
250
251 GST_GL_API
252 void            gst_gl_memory_init              (GstGLMemory * mem,
253                                                  GstAllocator * allocator,
254                                                  GstMemory * parent,
255                                                  GstGLContext * context,
256                                                  GstGLTextureTarget target,
257                                                  GstGLFormat tex_format,
258                                                  GstAllocationParams *params,
259                                                  GstVideoInfo * info,
260                                                  guint plane,
261                                                  GstVideoAlignment *valign,
262                                                  gpointer user_data,
263                                                  GDestroyNotify notify);
264
265 GST_GL_API
266 gboolean        gst_gl_memory_copy_into         (GstGLMemory *gl_mem,
267                                                  guint tex_id,
268                                                  GstGLTextureTarget target,
269                                                  GstGLFormat tex_format,
270                                                  gint width,
271                                                  gint height);
272 GST_GL_API
273 gboolean        gst_gl_memory_copy_teximage     (GstGLMemory * src,
274                                                  guint tex_id,
275                                                  GstGLTextureTarget out_target,
276                                                  GstGLFormat out_tex_format,
277                                                  gint out_width,
278                                                  gint out_height);
279
280 GST_GL_API
281 gboolean        gst_gl_memory_read_pixels       (GstGLMemory * gl_mem,
282                                                  gpointer read_pointer);
283 GST_GL_API
284 void            gst_gl_memory_texsubimage       (GstGLMemory * gl_mem,
285                                                  gpointer read_pointer);
286
287 /* accessors */
288 GST_GL_API
289 gint                    gst_gl_memory_get_texture_width     (GstGLMemory * gl_mem);
290 GST_GL_API
291 gint                    gst_gl_memory_get_texture_height    (GstGLMemory * gl_mem);
292 GST_GL_API
293 GstGLFormat             gst_gl_memory_get_texture_format    (GstGLMemory * gl_mem);
294 GST_GL_API
295 GstGLTextureTarget      gst_gl_memory_get_texture_target    (GstGLMemory * gl_mem);
296 GST_GL_API
297 guint                   gst_gl_memory_get_texture_id        (GstGLMemory * gl_mem);
298
299 GST_GL_API
300 gboolean                gst_gl_memory_setup_buffer          (GstGLMemoryAllocator * allocator,
301                                                              GstBuffer * buffer,
302                                                              GstGLVideoAllocationParams * params,
303                                                              GstGLFormat *tex_formats,
304                                                              gpointer *wrapped_data,
305                                                              gsize n_wrapped_pointers);
306
307 GST_GL_API
308 GstGLMemoryAllocator *  gst_gl_memory_allocator_get_default (GstGLContext *context);
309
310 G_END_DECLS
311
312 #endif /* _GST_GL_MEMORY_H_ */