1 /*
2 * GStreamer
3 * Copyright (C) 2015 Alessandro Decina <twi@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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "iosurfacememory.h"
26
27 GST_DEBUG_CATEGORY_STATIC (GST_CAT_IO_SURFACE_MEMORY);
28 #define GST_CAT_DEFAULT GST_CAT_IO_SURFACE_MEMORY
29
30 G_DEFINE_TYPE (GstIOSurfaceMemoryAllocator, gst_io_surface_memory_allocator,
31 GST_TYPE_GL_MEMORY_ALLOCATOR);
32
33 typedef struct
34 {
35 GstIOSurfaceMemory *memory;
36 IOSurfaceRef surface;
37 } ContextThreadData;
38
39 static void _io_surface_memory_set_surface (GstIOSurfaceMemory * memory,
40 IOSurfaceRef surface);
41
42 static GstAllocator *_io_surface_memory_allocator;
43
44 static gboolean
_io_surface_memory_create(GstGLBaseMemory * bmem,GError ** error)45 _io_surface_memory_create (GstGLBaseMemory * bmem, GError ** error)
46 {
47 GstGLMemory *gl_mem = (GstGLMemory *) bmem;
48 GstGLContext *context = gl_mem->mem.context;
49 const GstGLFuncs *gl = context->gl_vtable;
50 GLuint target;
51
52 target = gst_gl_texture_target_to_gl (gl_mem->tex_target);
53 gl->GenTextures (1, &gl_mem->tex_id);
54 gl->BindTexture (target, gl_mem->tex_id);
55 gl->BindTexture (target, 0);
56
57 GST_LOG ("generated texture id:%d", gl_mem->tex_id);
58
59 return TRUE;
60 }
61
62 static void
_io_surface_memory_destroy(GstGLBaseMemory * gl_mem)63 _io_surface_memory_destroy (GstGLBaseMemory * gl_mem)
64 {
65 GST_GL_BASE_MEMORY_ALLOCATOR_CLASS
66 (gst_io_surface_memory_allocator_parent_class)->destroy (gl_mem);
67 _io_surface_memory_set_surface ((GstIOSurfaceMemory *) gl_mem, NULL);
68 }
69
70 static gpointer
_io_surface_memory_allocator_map(GstGLBaseMemory * bmem,GstMapInfo * info,gsize size)71 _io_surface_memory_allocator_map (GstGLBaseMemory * bmem,
72 GstMapInfo * info, gsize size)
73 {
74 GstGLMemory *gl_mem = (GstGLMemory *) bmem;
75 GstIOSurfaceMemory *mem = (GstIOSurfaceMemory *) gl_mem;
76
77 GST_LOG ("mapping surface %p flags %d gl? %d",
78 mem->surface, info->flags, ((info->flags & GST_MAP_GL) != 0));
79
80 if (info->flags & GST_MAP_GL) {
81 return &gl_mem->tex_id;
82 } else if (!(info->flags & GST_MAP_WRITE)) {
83 IOSurfaceLock (mem->surface, kIOSurfaceLockReadOnly, NULL);
84 return IOSurfaceGetBaseAddressOfPlane (mem->surface, gl_mem->plane);
85 } else {
86 GST_ERROR ("couldn't map IOSurface %p flags %d", mem->surface, info->flags);
87 return NULL;
88 }
89 }
90
91 static void
_io_surface_memory_allocator_unmap(GstGLBaseMemory * bmem,GstMapInfo * info)92 _io_surface_memory_allocator_unmap (GstGLBaseMemory * bmem, GstMapInfo * info)
93 {
94 GstGLMemory *gl_mem = (GstGLMemory *) bmem;
95 GstIOSurfaceMemory *mem = (GstIOSurfaceMemory *) gl_mem;
96
97 GST_LOG ("unmapping surface %p flags %d gl? %d",
98 mem->surface, info->flags, ((info->flags & GST_MAP_GL) != 0));
99
100 if (!(info->flags & GST_MAP_GL)) {
101 IOSurfaceUnlock (mem->surface, kIOSurfaceLockReadOnly, NULL);
102 }
103 }
104
105 static GstMemory *
_mem_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params)106 _mem_alloc (GstAllocator * allocator, gsize size, GstAllocationParams * params)
107 {
108 g_warning ("use gst_io_surface_memory_wrapped () to allocate from this "
109 "IOSurface allocator");
110
111 return NULL;
112 }
113
114 static void
gst_io_surface_memory_allocator_class_init(GstIOSurfaceMemoryAllocatorClass * klass)115 gst_io_surface_memory_allocator_class_init (GstIOSurfaceMemoryAllocatorClass *
116 klass)
117 {
118 GstAllocatorClass *allocator_class = (GstAllocatorClass *) klass;
119 GstGLBaseMemoryAllocatorClass *gl_base_allocator_class =
120 (GstGLBaseMemoryAllocatorClass *) klass;
121
122 allocator_class->alloc = _mem_alloc;
123
124 gl_base_allocator_class->create = _io_surface_memory_create;
125 gl_base_allocator_class->destroy = _io_surface_memory_destroy;
126 gl_base_allocator_class->map = _io_surface_memory_allocator_map;
127 gl_base_allocator_class->unmap = _io_surface_memory_allocator_unmap;
128 }
129
130 static void
gst_io_surface_memory_allocator_init(GstIOSurfaceMemoryAllocator * allocator)131 gst_io_surface_memory_allocator_init (GstIOSurfaceMemoryAllocator * allocator)
132 {
133 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
134
135 alloc->mem_type = GST_IO_SURFACE_MEMORY_ALLOCATOR_NAME;
136 GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
137 }
138
139 void
gst_ios_surface_memory_init(void)140 gst_ios_surface_memory_init (void)
141 {
142 static volatile gsize _init = 0;
143
144 if (g_once_init_enter (&_init)) {
145 GST_DEBUG_CATEGORY_INIT (GST_CAT_IO_SURFACE_MEMORY, "iosurface", 0,
146 "IOSurface Buffer");
147
148 _io_surface_memory_allocator =
149 g_object_new (GST_TYPE_IO_SURFACE_MEMORY_ALLOCATOR, NULL);
150 gst_object_ref_sink (_io_surface_memory_allocator);
151
152 gst_allocator_register (GST_IO_SURFACE_MEMORY_ALLOCATOR_NAME,
153 gst_object_ref (_io_surface_memory_allocator));
154 g_once_init_leave (&_init, 1);
155 }
156 }
157
158 gboolean
gst_is_io_surface_memory(GstMemory * mem)159 gst_is_io_surface_memory (GstMemory * mem)
160 {
161 return mem != NULL && mem->allocator != NULL &&
162 g_type_is_a (G_OBJECT_TYPE (mem->allocator),
163 GST_TYPE_IO_SURFACE_MEMORY_ALLOCATOR);
164 }
165
166 static GstIOSurfaceMemory *
_io_surface_memory_new(GstGLContext * context,IOSurfaceRef surface,GstGLTextureTarget target,GstGLFormat tex_format,GstVideoInfo * info,guint plane,GstVideoAlignment * valign,gpointer user_data,GDestroyNotify notify)167 _io_surface_memory_new (GstGLContext * context,
168 IOSurfaceRef surface,
169 GstGLTextureTarget target,
170 GstGLFormat tex_format,
171 GstVideoInfo * info,
172 guint plane,
173 GstVideoAlignment * valign, gpointer user_data, GDestroyNotify notify)
174 {
175 GstIOSurfaceMemory *mem;
176
177 g_return_val_if_fail (target == GST_GL_TEXTURE_TARGET_RECTANGLE, NULL);
178
179 mem = g_new0 (GstIOSurfaceMemory, 1);
180 gst_gl_memory_init (&mem->gl_mem, _io_surface_memory_allocator, NULL, context,
181 target, tex_format, NULL, info, plane, valign, user_data, notify);
182
183 GST_MINI_OBJECT_FLAG_SET (mem, GST_MEMORY_FLAG_READONLY);
184
185 mem->surface = NULL;
186 gst_io_surface_memory_set_surface (mem, surface);
187
188 return mem;
189 }
190
191 GstIOSurfaceMemory *
gst_io_surface_memory_wrapped(GstGLContext * context,IOSurfaceRef surface,GstGLTextureTarget target,GstGLFormat tex_format,GstVideoInfo * info,guint plane,GstVideoAlignment * valign,gpointer user_data,GDestroyNotify notify)192 gst_io_surface_memory_wrapped (GstGLContext * context,
193 IOSurfaceRef surface,
194 GstGLTextureTarget target,
195 GstGLFormat tex_format,
196 GstVideoInfo * info,
197 guint plane,
198 GstVideoAlignment * valign, gpointer user_data, GDestroyNotify notify)
199 {
200 return _io_surface_memory_new (context, surface, target, tex_format, info,
201 plane, valign, user_data, notify);
202 }
203
204 static void
_io_surface_memory_set_surface(GstIOSurfaceMemory * memory,IOSurfaceRef surface)205 _io_surface_memory_set_surface (GstIOSurfaceMemory * memory,
206 IOSurfaceRef surface)
207 {
208 GstGLMemory *gl_mem = (GstGLMemory *) memory;
209 GstGLContext *context = ((GstGLBaseMemory *) gl_mem)->context;
210 GstGLFuncs *gl = context->gl_vtable;
211
212 if (memory->surface)
213 IOSurfaceDecrementUseCount (memory->surface);
214 memory->surface = surface;
215 if (surface) {
216 GLuint tex_id, tex_target, texifmt, texfmt;
217 guint plane;
218 CGLError cglError;
219
220 plane = gl_mem->plane;
221 tex_id = gl_mem->tex_id;
222 tex_target = gst_gl_texture_target_to_gl (gl_mem->tex_target);
223 texifmt = gst_gl_format_from_video_info (context, &gl_mem->info, plane);
224 texfmt =
225 gst_gl_sized_gl_format_from_gl_format_type (context, texifmt,
226 GL_UNSIGNED_BYTE);
227 gl->BindTexture (tex_target, tex_id);
228 cglError = CGLTexImageIOSurface2D ((CGLContextObj)
229 gst_gl_context_get_gl_context (context), tex_target, texifmt,
230 IOSurfaceGetWidthOfPlane (surface, plane),
231 IOSurfaceGetHeightOfPlane (surface, plane), texifmt, GL_UNSIGNED_BYTE,
232 surface, plane);
233 gl->BindTexture (tex_target, 0);
234 IOSurfaceIncrementUseCount (surface);
235 GST_DEBUG ("bound surface %p to texture %u: %d", surface, tex_id, cglError);
236 }
237 }
238
239 static void
_do_set_surface(GstGLContext * context,ContextThreadData * data)240 _do_set_surface (GstGLContext * context, ContextThreadData * data)
241 {
242 _io_surface_memory_set_surface (data->memory, data->surface);
243 }
244
245 void
gst_io_surface_memory_set_surface(GstIOSurfaceMemory * memory,IOSurfaceRef surface)246 gst_io_surface_memory_set_surface (GstIOSurfaceMemory * memory,
247 IOSurfaceRef surface)
248 {
249 GstGLContext *context;
250 ContextThreadData data = { memory, surface };
251
252 g_return_if_fail (gst_is_io_surface_memory ((GstMemory *) memory));
253
254 context = memory->gl_mem.mem.context;
255 gst_gl_context_thread_add (context,
256 (GstGLContextThreadFunc) _do_set_surface, &data);
257 }
258