gst: don't use volatile to mean atomic
[platform/upstream/gstreamer.git] / sys / applemedia / iosurfaceglmemory.c
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 "iosurfaceglmemory.h"
26
27 GST_DEBUG_CATEGORY_STATIC (GST_CAT_IO_SURFACE_GL_MEMORY);
28 #define GST_CAT_DEFAULT GST_CAT_IO_SURFACE_GL_MEMORY
29
30 G_DEFINE_TYPE (GstIOSurfaceGLMemoryAllocator,
31     gst_io_surface_gl_memory_allocator, GST_TYPE_GL_MEMORY_ALLOCATOR);
32
33 typedef struct
34 {
35   GstIOSurfaceGLMemory *memory;
36   IOSurfaceRef surface;
37 } ContextThreadData;
38
39 static void _io_surface_gl_memory_set_surface (GstIOSurfaceGLMemory * memory,
40     IOSurfaceRef surface);
41
42 static GstAllocator *_io_surface_gl_memory_allocator;
43
44 static gboolean
45 _io_surface_gl_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
63 _io_surface_gl_memory_destroy (GstGLBaseMemory * gl_mem)
64 {
65   GST_GL_BASE_MEMORY_ALLOCATOR_CLASS
66       (gst_io_surface_gl_memory_allocator_parent_class)->destroy (gl_mem);
67   _io_surface_gl_memory_set_surface ((GstIOSurfaceGLMemory *) gl_mem, NULL);
68 }
69
70 static gpointer
71 _io_surface_gl_memory_allocator_map (GstGLBaseMemory * bmem,
72     GstMapInfo * info, gsize size)
73 {
74   GstGLMemory *gl_mem = (GstGLMemory *) bmem;
75   GstIOSurfaceGLMemory *mem = (GstIOSurfaceGLMemory *) 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
92 _io_surface_gl_memory_allocator_unmap (GstGLBaseMemory * bmem,
93     GstMapInfo * info)
94 {
95   GstGLMemory *gl_mem = (GstGLMemory *) bmem;
96   GstIOSurfaceGLMemory *mem = (GstIOSurfaceGLMemory *) gl_mem;
97
98   GST_LOG ("unmapping surface %p flags %d gl? %d",
99       mem->surface, info->flags, ((info->flags & GST_MAP_GL) != 0));
100
101   if (!(info->flags & GST_MAP_GL)) {
102     IOSurfaceUnlock (mem->surface, kIOSurfaceLockReadOnly, NULL);
103   }
104 }
105
106 static GstMemory *
107 _mem_alloc (GstAllocator * allocator, gsize size, GstAllocationParams * params)
108 {
109   g_warning ("use gst_io_surface_gl_memory_wrapped () to allocate from this "
110       "IOSurface allocator");
111
112   return NULL;
113 }
114
115 static void
116     gst_io_surface_gl_memory_allocator_class_init
117     (GstIOSurfaceGLMemoryAllocatorClass * klass)
118 {
119   GstAllocatorClass *allocator_class = (GstAllocatorClass *) klass;
120   GstGLBaseMemoryAllocatorClass *gl_base_allocator_class =
121       (GstGLBaseMemoryAllocatorClass *) klass;
122
123   allocator_class->alloc = _mem_alloc;
124
125   gl_base_allocator_class->create = _io_surface_gl_memory_create;
126   gl_base_allocator_class->destroy = _io_surface_gl_memory_destroy;
127   gl_base_allocator_class->map = _io_surface_gl_memory_allocator_map;
128   gl_base_allocator_class->unmap = _io_surface_gl_memory_allocator_unmap;
129 }
130
131 static void
132 gst_io_surface_gl_memory_allocator_init (GstIOSurfaceGLMemoryAllocator *
133     allocator)
134 {
135   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
136
137   alloc->mem_type = GST_IO_SURFACE_GL_MEMORY_ALLOCATOR_NAME;
138   GST_OBJECT_FLAG_SET (allocator, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
139 }
140
141 void
142 gst_ios_surface_gl_memory_init (void)
143 {
144   static gsize _init = 0;
145
146   if (g_once_init_enter (&_init)) {
147     GST_DEBUG_CATEGORY_INIT (GST_CAT_IO_SURFACE_GL_MEMORY, "iosurfacegl", 0,
148         "IOSurface Buffer");
149
150     _io_surface_gl_memory_allocator =
151         g_object_new (GST_TYPE_IO_SURFACE_GL_MEMORY_ALLOCATOR, NULL);
152     gst_object_ref_sink (_io_surface_gl_memory_allocator);
153
154     gst_allocator_register (GST_IO_SURFACE_GL_MEMORY_ALLOCATOR_NAME,
155         gst_object_ref (_io_surface_gl_memory_allocator));
156     g_once_init_leave (&_init, 1);
157   }
158 }
159
160 gboolean
161 gst_is_io_surface_gl_memory (GstMemory * mem)
162 {
163   return mem != NULL && mem->allocator != NULL &&
164       g_type_is_a (G_OBJECT_TYPE (mem->allocator),
165       GST_TYPE_IO_SURFACE_GL_MEMORY_ALLOCATOR);
166 }
167
168 static GstIOSurfaceGLMemory *
169 _io_surface_gl_memory_new (GstGLContext * context,
170     IOSurfaceRef surface,
171     GstGLTextureTarget target,
172     GstGLFormat tex_format,
173     GstVideoInfo * info,
174     guint plane,
175     GstVideoAlignment * valign, gpointer user_data, GDestroyNotify notify)
176 {
177   GstIOSurfaceGLMemory *mem;
178
179   g_return_val_if_fail (target == GST_GL_TEXTURE_TARGET_RECTANGLE, NULL);
180
181   mem = g_new0 (GstIOSurfaceGLMemory, 1);
182   gst_gl_memory_init (&mem->gl_mem, _io_surface_gl_memory_allocator, NULL,
183       context, target, tex_format, NULL, info, plane, valign, user_data,
184       notify);
185
186   GST_MINI_OBJECT_FLAG_SET (mem, GST_MEMORY_FLAG_READONLY);
187
188   mem->surface = NULL;
189   gst_io_surface_gl_memory_set_surface (mem, surface);
190
191   return mem;
192 }
193
194 GstIOSurfaceGLMemory *
195 gst_io_surface_gl_memory_wrapped (GstGLContext * context,
196     IOSurfaceRef surface,
197     GstGLTextureTarget target,
198     GstGLFormat tex_format,
199     GstVideoInfo * info,
200     guint plane,
201     GstVideoAlignment * valign, gpointer user_data, GDestroyNotify notify)
202 {
203   return _io_surface_gl_memory_new (context, surface, target, tex_format, info,
204       plane, valign, user_data, notify);
205 }
206
207 static void
208 _io_surface_gl_memory_set_surface (GstIOSurfaceGLMemory * memory,
209     IOSurfaceRef surface)
210 {
211   GstGLMemory *gl_mem = (GstGLMemory *) memory;
212   GstGLContext *context = ((GstGLBaseMemory *) gl_mem)->context;
213   GstGLFuncs *gl = context->gl_vtable;
214
215   if (memory->surface)
216     IOSurfaceDecrementUseCount (memory->surface);
217   memory->surface = surface;
218   if (surface) {
219     GLuint tex_id, tex_target, texifmt, texfmt;
220     guint plane;
221     CGLError cglError;
222
223     plane = gl_mem->plane;
224     tex_id = gl_mem->tex_id;
225     tex_target = gst_gl_texture_target_to_gl (gl_mem->tex_target);
226     texifmt = gst_gl_format_from_video_info (context, &gl_mem->info, plane);
227     texfmt =
228         gst_gl_sized_gl_format_from_gl_format_type (context, texifmt,
229         GL_UNSIGNED_BYTE);
230     gl->BindTexture (tex_target, tex_id);
231     cglError = CGLTexImageIOSurface2D ((CGLContextObj)
232         gst_gl_context_get_gl_context (context), tex_target, texifmt,
233         IOSurfaceGetWidthOfPlane (surface, plane),
234         IOSurfaceGetHeightOfPlane (surface, plane), texifmt, GL_UNSIGNED_BYTE,
235         surface, plane);
236     gl->BindTexture (tex_target, 0);
237     IOSurfaceIncrementUseCount (surface);
238     GST_DEBUG ("bound surface %p to texture %u: %d", surface, tex_id, cglError);
239   }
240 }
241
242 static void
243 _do_set_surface (GstGLContext * context, ContextThreadData * data)
244 {
245   _io_surface_gl_memory_set_surface (data->memory, data->surface);
246 }
247
248 void
249 gst_io_surface_gl_memory_set_surface (GstIOSurfaceGLMemory * memory,
250     IOSurfaceRef surface)
251 {
252   GstGLContext *context;
253   ContextThreadData data = { memory, surface };
254
255   g_return_if_fail (gst_is_io_surface_gl_memory ((GstMemory *) memory));
256
257   context = memory->gl_mem.mem.context;
258   gst_gl_context_thread_add (context,
259       (GstGLContextThreadFunc) _do_set_surface, &data);
260 }