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 #define GST_GL_MEMORY_VIDEO_FORMATS_STR \
54     "{ RGBA, BGRA, RGBx, BGRx, ARGB, ABGR, xRGB, xBGR, RGB, BGR, RGB16, BGR16, " \
55     "AYUV, VUYA, I420, YV12, NV12, NV21, YUY2, UYVY, Y41B, Y42B, Y444, " \
56     "GRAY8, GRAY16_LE, GRAY16_BE, ARGB64 }"
57 
58 /**
59  * GstGLMemory:
60  * @mem: the parent #GstGLBaseMemory object
61  * @tex_id: the GL texture id for this memory
62  * @tex_target: the GL texture target for this memory
63  * @tex_format: the texture type
64  * @info: the texture's #GstVideoInfo
65  * @valign: data alignment for system memory mapping
66  * @plane: data plane in @info
67  * @tex_scaling: GL shader scaling parameters for @valign and/or width/height
68  *
69  * Represents information about a GL texture
70  */
71 struct _GstGLMemory
72 {
73   GstGLBaseMemory           mem;
74 
75   guint                     tex_id;
76   GstGLTextureTarget        tex_target;
77   GstGLFormat               tex_format;
78   GstVideoInfo              info;
79   GstVideoAlignment         valign;
80   guint                     plane;
81   gfloat                    tex_scaling[2];
82 
83   /* <protected> */
84   gboolean                  texture_wrapped;
85   guint                     unpack_length;
86   guint                     tex_width;
87 
88   /* <private> */
89   gpointer                  _padding[GST_PADDING];
90 };
91 
92 
93 #define GST_TYPE_GL_VIDEO_ALLOCATION_PARAMS (gst_gl_video_allocation_params_get_type())
94 GST_GL_API
95 GType gst_gl_video_allocation_params_get_type (void);
96 
97 typedef struct _GstGLVideoAllocationParams GstGLVideoAllocationParams;
98 
99 /**
100  * GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_VIDEO:
101  *
102  * GL allocation flag indicating the allocation of 2D video frames
103  */
104 #define GST_GL_ALLOCATION_PARAMS_ALLOC_FLAG_VIDEO (1 << 3)
105 
106 /**
107  * GstGLVideoAllocationParams:
108  * @parent: the parent #GstGLAllocationParams structure
109  * @v_info: the #GstVideoInfo to allocate
110  * @plane: the video plane index to allocate
111  * @valign: the #GstVideoAlignment to align the system representation to (may be %NULL for the default)
112  * @target: the #GstGLTextureTarget to allocate
113  * @tex_format: the #GstGLFormat to allocate
114  */
115 struct _GstGLVideoAllocationParams
116 {
117   GstGLAllocationParams  parent;
118 
119   GstVideoInfo          *v_info;
120   guint                  plane;
121   GstVideoAlignment     *valign;
122   GstGLTextureTarget     target;
123   GstGLFormat            tex_format;
124 
125   /* <private> */
126   gpointer               _padding[GST_PADDING];
127 };
128 
129 GST_GL_API
130 gboolean        gst_gl_video_allocation_params_init_full        (GstGLVideoAllocationParams * params,
131                                                                  gsize struct_size,
132                                                                  guint alloc_flags,
133                                                                  GstGLAllocationParamsCopyFunc copy,
134                                                                  GstGLAllocationParamsFreeFunc free,
135                                                                  GstGLContext * context,
136                                                                  GstAllocationParams * alloc_params,
137                                                                  GstVideoInfo * v_info,
138                                                                  guint plane,
139                                                                  GstVideoAlignment * valign,
140                                                                  GstGLTextureTarget target,
141                                                                  GstGLFormat tex_format,
142                                                                  gpointer wrapped_data,
143                                                                  gpointer gl_handle,
144                                                                  gpointer user_data,
145                                                                  GDestroyNotify notify);
146 GST_GL_API
147 GstGLVideoAllocationParams * gst_gl_video_allocation_params_new (GstGLContext * context,
148                                                                  GstAllocationParams * alloc_params,
149                                                                  GstVideoInfo * v_info,
150                                                                  guint plane,
151                                                                  GstVideoAlignment * valign,
152                                                                  GstGLTextureTarget target,
153                                                                  GstGLFormat tex_format);
154 GST_GL_API
155 GstGLVideoAllocationParams * gst_gl_video_allocation_params_new_wrapped_data    (GstGLContext * context,
156                                                                                  GstAllocationParams * alloc_params,
157                                                                                  GstVideoInfo * v_info,
158                                                                                  guint plane,
159                                                                                  GstVideoAlignment * valign,
160                                                                                  GstGLTextureTarget target,
161                                                                                  GstGLFormat tex_format,
162                                                                                  gpointer wrapped_data,
163                                                                                  gpointer user_data,
164                                                                                  GDestroyNotify notify);
165 
166 GST_GL_API
167 GstGLVideoAllocationParams * gst_gl_video_allocation_params_new_wrapped_texture (GstGLContext * context,
168                                                                                  GstAllocationParams * alloc_params,
169                                                                                  GstVideoInfo * v_info,
170                                                                                  guint plane,
171                                                                                  GstVideoAlignment * valign,
172                                                                                  GstGLTextureTarget target,
173                                                                                  GstGLFormat tex_format,
174                                                                                  guint tex_id,
175                                                                                  gpointer user_data,
176                                                                                  GDestroyNotify notify);
177 
178 GST_GL_API
179 GstGLVideoAllocationParams * gst_gl_video_allocation_params_new_wrapped_gl_handle (GstGLContext * context,
180                                                                                  GstAllocationParams * alloc_params,
181                                                                                  GstVideoInfo * v_info,
182                                                                                  guint plane,
183                                                                                  GstVideoAlignment * valign,
184                                                                                  GstGLTextureTarget target,
185                                                                                  GstGLFormat tex_format,
186                                                                                  gpointer gl_handle,
187                                                                                  gpointer user_data,
188                                                                                  GDestroyNotify notify);
189 
190 /* subclass usage */
191 GST_GL_API
192 void            gst_gl_video_allocation_params_free_data    (GstGLVideoAllocationParams * params);
193 /* subclass usage */
194 GST_GL_API
195 void            gst_gl_video_allocation_params_copy_data    (GstGLVideoAllocationParams * src_vid,
196                                                              GstGLVideoAllocationParams * dest_vid);
197 
198 /**
199  * GstGLMemoryAllocator
200  *
201  * Opaque #GstGLMemoryAllocator struct
202  */
203 struct _GstGLMemoryAllocator
204 {
205   /* <private> */
206   GstGLBaseMemoryAllocator parent;
207 
208   gpointer _padding[GST_PADDING];
209 };
210 
211 /**
212  * GstGLMemoryAllocatorClass:
213  * @map: provide a custom map implementation
214  * @copy: provide a custom copy implementation
215  * @unmap: provide a custom unmap implementation
216  */
217 struct _GstGLMemoryAllocatorClass
218 {
219   /* <private> */
220   GstGLBaseMemoryAllocatorClass             parent_class;
221 
222   /* <public> */
223   GstGLBaseMemoryAllocatorMapFunction       map;
224   GstGLBaseMemoryAllocatorCopyFunction      copy;
225   GstGLBaseMemoryAllocatorUnmapFunction     unmap;
226 
227   /* <private> */
228   gpointer                                  _padding[GST_PADDING];
229 };
230 
231 #include <gst/gl/gstglbasememory.h>
232 
233 /**
234  * GST_GL_MEMORY_ALLOCATOR_NAME:
235  *
236  * The name of the GL memory allocator
237  */
238 #define GST_GL_MEMORY_ALLOCATOR_NAME   "GLMemory"
239 
240 GST_GL_API
241 void            gst_gl_memory_init_once (void);
242 GST_GL_API
243 gboolean        gst_is_gl_memory (GstMemory * mem);
244 
245 GST_GL_API
246 void            gst_gl_memory_init              (GstGLMemory * mem,
247                                                  GstAllocator * allocator,
248                                                  GstMemory * parent,
249                                                  GstGLContext * context,
250                                                  GstGLTextureTarget target,
251                                                  GstGLFormat tex_format,
252                                                  GstAllocationParams *params,
253                                                  GstVideoInfo * info,
254                                                  guint plane,
255                                                  GstVideoAlignment *valign,
256                                                  gpointer user_data,
257                                                  GDestroyNotify notify);
258 
259 GST_GL_API
260 gboolean        gst_gl_memory_copy_into         (GstGLMemory *gl_mem,
261                                                  guint tex_id,
262                                                  GstGLTextureTarget target,
263                                                  GstGLFormat tex_format,
264                                                  gint width,
265                                                  gint height);
266 GST_GL_API
267 gboolean        gst_gl_memory_copy_teximage     (GstGLMemory * src,
268                                                  guint tex_id,
269                                                  GstGLTextureTarget out_target,
270                                                  GstGLFormat out_tex_format,
271                                                  gint out_width,
272                                                  gint out_height);
273 
274 GST_GL_API
275 gboolean        gst_gl_memory_read_pixels       (GstGLMemory * gl_mem,
276                                                  gpointer read_pointer);
277 GST_GL_API
278 void            gst_gl_memory_texsubimage       (GstGLMemory * gl_mem,
279                                                  gpointer read_pointer);
280 
281 /* accessors */
282 GST_GL_API
283 gint                    gst_gl_memory_get_texture_width     (GstGLMemory * gl_mem);
284 GST_GL_API
285 gint                    gst_gl_memory_get_texture_height    (GstGLMemory * gl_mem);
286 GST_GL_API
287 GstGLFormat             gst_gl_memory_get_texture_format    (GstGLMemory * gl_mem);
288 GST_GL_API
289 GstGLTextureTarget      gst_gl_memory_get_texture_target    (GstGLMemory * gl_mem);
290 GST_GL_API
291 guint                   gst_gl_memory_get_texture_id        (GstGLMemory * gl_mem);
292 
293 GST_GL_API
294 gboolean                gst_gl_memory_setup_buffer          (GstGLMemoryAllocator * allocator,
295                                                              GstBuffer * buffer,
296                                                              GstGLVideoAllocationParams * params,
297                                                              GstGLFormat *tex_formats,
298                                                              gpointer *wrapped_data,
299                                                              gsize n_wrapped_pointers);
300 
301 GST_GL_API
302 GstGLMemoryAllocator *  gst_gl_memory_allocator_get_default (GstGLContext *context);
303 
304 G_END_DECLS
305 
306 #endif /* _GST_GL_MEMORY_H_ */
307