documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / ext / wayland / wlbuffer.c
1 /* GStreamer Wayland video sink
2  *
3  * Copyright (C) 2014 Collabora Ltd.
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 Free
17  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301 USA.
19  */
20
21 /* GstWlBuffer wraps wl_buffer and provides a mechanism for preventing
22  * buffers from being re-used while the compositor is using them. This
23  * is achieved by adding a reference to the GstBuffer as soon as its
24  * associated wl_buffer is sent to the compositor and by removing this
25  * reference as soon as the compositor sends a wl_buffer::release message.
26  *
27  * This mechanism is a bit complicated, though, because it adds cyclic
28  * references that can be dangerous. The reference cycles looks like:
29  *
30  *   ----------------
31  *   | GstWlDisplay | ---------------------------->
32  *   ----------------                              |
33  *                                                 |
34  *                                                 V
35  *   -----------------     -------------     ---------------
36  *   | GstBufferPool | --> | GstBuffer | ==> | GstWlBuffer |
37  *   |               | <-- |           | <-- |             |
38  *   -----------------     -------------     ---------------
39  *
40  * A GstBufferPool normally holds references to its GstBuffers and each buffer
41  * holds a reference to a GstWlBuffer (saved in the GstMiniObject qdata).
42  * When a GstBuffer is in use, it holds a reference back to the pool and the
43  * pool doesn't hold a reference to the GstBuffer. When the GstBuffer is unrefed
44  * externally, it returns back to the pool and the pool holds again a reference
45  * to the buffer.
46  *
47  * Now when the compositor is using a buffer, the GstWlBuffer also holds a ref
48  * to the GstBuffer, which prevents it from returning to the pool. When the
49  * last GstWlBuffer receives a release event and unrefs the last GstBuffer,
50  * the GstBufferPool will be able to stop and if no-one is holding a strong
51  * ref to it, it will be destroyed. This will destroy the pool's GstBuffers and
52  * also the GstWlBuffers. This will all happen in the same context of the last
53  * gst_buffer_unref, which will be called from the buffer_release() callback.
54  *
55  * The problem here lies in the fact that buffer_release() will be called
56  * from the event loop thread of GstWlDisplay, so it's as if the display
57  * holds a reference to the GstWlBuffer, but without having an actual reference.
58  * When we kill the display, there is no way for the GstWlBuffer, the associated
59  * GstBuffer and the GstBufferPool to get destroyed, so we are going to leak a
60  * fair amount of memory.
61  *
62  * Normally, this rarely happens, because the compositor releases buffers
63  * almost immediately and when waylandsink stops, they are already released.
64  *
65  * However, we want to be absolutely certain, so a solution is introduced
66  * by registering all the GstWlBuffers with the display and explicitly
67  * releasing all the buffer references as soon as the display is destroyed.
68  *
69  * When the GstWlDisplay is finalized, it takes a reference to all the
70  * registered GstWlBuffers and then calls gst_wl_buffer_force_release_and_unref,
71  * which releases the potential reference to the GstBuffer, destroys the
72  * underlying wl_buffer and removes the reference that GstWlDisplay is holding.
73  * At that point, either the GstBuffer is alive somewhere and still holds a ref
74  * to the GstWlBuffer, which it will release when it gets destroyed, or the
75  * GstBuffer was destroyed in the meantime and the GstWlBuffer gets destroyed
76  * as soon as we remove the reference that GstWlDisplay holds.
77  */
78
79 #include "wlbuffer.h"
80
81 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
82 #define GST_CAT_DEFAULT gstwayland_debug
83
84 G_DEFINE_TYPE (GstWlBuffer, gst_wl_buffer, G_TYPE_OBJECT);
85
86 static G_DEFINE_QUARK (GstWlBufferQDataQuark, gst_wl_buffer_qdata);
87
88 static void
89 gst_wl_buffer_dispose (GObject * gobject)
90 {
91   GstWlBuffer *self = GST_WL_BUFFER (gobject);
92
93   GST_TRACE_OBJECT (self, "dispose");
94
95   /* if the display is shutting down and we are trying to dipose
96    * the GstWlBuffer from another thread, unregister_buffer() will
97    * block and in the end the display will increase the refcount
98    * of this GstWlBuffer, so it will not be finalized */
99   if (self->display)
100     gst_wl_display_unregister_buffer (self->display, self);
101
102   G_OBJECT_CLASS (gst_wl_buffer_parent_class)->dispose (gobject);
103 }
104
105 static void
106 gst_wl_buffer_finalize (GObject * gobject)
107 {
108   GstWlBuffer *self = GST_WL_BUFFER (gobject);
109
110   GST_TRACE_OBJECT (self, "finalize");
111
112   if (self->wlbuffer)
113     wl_buffer_destroy (self->wlbuffer);
114
115   G_OBJECT_CLASS (gst_wl_buffer_parent_class)->finalize (gobject);
116 }
117
118 static void
119 gst_wl_buffer_class_init (GstWlBufferClass * klass)
120 {
121   GObjectClass *object_class = (GObjectClass *) klass;
122
123   object_class->dispose = gst_wl_buffer_dispose;
124   object_class->finalize = gst_wl_buffer_finalize;
125 }
126
127 static void
128 gst_wl_buffer_init (GstWlBuffer * self)
129 {
130 }
131
132 static void
133 buffer_release (void *data, struct wl_buffer *wl_buffer)
134 {
135   GstWlBuffer *self = data;
136
137   GST_LOG_OBJECT (self, "wl_buffer::release (GstBuffer: %p)", self->gstbuffer);
138
139   self->used_by_compositor = FALSE;
140
141   /* unref should be last, because it may end up destroying the GstWlBuffer */
142   gst_buffer_unref (self->gstbuffer);
143 }
144
145 static const struct wl_buffer_listener buffer_listener = {
146   buffer_release
147 };
148
149 static void
150 gstbuffer_disposed (GstWlBuffer * self)
151 {
152   g_assert (!self->used_by_compositor);
153   self->gstbuffer = NULL;
154
155   GST_TRACE_OBJECT (self, "owning GstBuffer was finalized");
156
157   /* this will normally destroy the GstWlBuffer, unless the display is
158    * finalizing and it has taken an additional reference to it */
159   g_object_unref (self);
160 }
161
162 GstWlBuffer *
163 gst_buffer_add_wl_buffer (GstBuffer * gstbuffer, struct wl_buffer *wlbuffer,
164     GstWlDisplay * display)
165 {
166   GstWlBuffer *self;
167
168   self = g_object_new (GST_TYPE_WL_BUFFER, NULL);
169   self->gstbuffer = gstbuffer;
170   self->wlbuffer = wlbuffer;
171   self->display = display;
172
173   gst_wl_display_register_buffer (self->display, self);
174
175   wl_buffer_add_listener (self->wlbuffer, &buffer_listener, self);
176
177   gst_mini_object_set_qdata ((GstMiniObject *) gstbuffer,
178       gst_wl_buffer_qdata_quark (), self, (GDestroyNotify) gstbuffer_disposed);
179
180   return self;
181 }
182
183 GstWlBuffer *
184 gst_buffer_get_wl_buffer (GstBuffer * gstbuffer)
185 {
186   return gst_mini_object_get_qdata ((GstMiniObject *) gstbuffer,
187       gst_wl_buffer_qdata_quark ());
188 }
189
190 void
191 gst_wl_buffer_force_release_and_unref (GstWlBuffer * self)
192 {
193   /* Force a buffer release.
194    * At this point, the GstWlDisplay has killed its event loop,
195    * so we don't need to worry about buffer_release() being called
196    * at the same time from the event loop thread */
197   if (self->used_by_compositor) {
198     GST_DEBUG_OBJECT (self, "forcing wl_buffer::release (GstBuffer: %p)",
199         self->gstbuffer);
200     self->used_by_compositor = FALSE;
201     gst_buffer_unref (self->gstbuffer);
202   }
203
204   /* Finalize this GstWlBuffer early.
205    * This method has been called as a result of the display shutting down,
206    * so we need to stop using any wayland resources and disconnect from
207    * the display. The GstWlBuffer stays alive, though, to avoid race
208    * conditions with the GstBuffer being destroyed from another thread.
209    * The last reference is either owned by the GstBuffer or by us and
210    * it will be released at the end of this function. */
211   GST_TRACE_OBJECT (self, "finalizing early");
212   wl_buffer_destroy (self->wlbuffer);
213   self->wlbuffer = NULL;
214   self->display = NULL;
215
216   /* remove the reference that the caller (GstWlDisplay) owns */
217   g_object_unref (self);
218 }
219
220 void
221 gst_wl_buffer_attach (GstWlBuffer * self, struct wl_surface *surface)
222 {
223   if (self->used_by_compositor) {
224     GST_DEBUG_OBJECT (self, "buffer used by compositor %p", self->gstbuffer);
225     return;
226   }
227
228   wl_surface_attach (surface, self->wlbuffer, 0, 0);
229
230   /* Add a reference to the buffer. This represents the fact that
231    * the compositor is using the buffer and it should not return
232    * back to the pool and be re-used until the compositor releases it. */
233   gst_buffer_ref (self->gstbuffer);
234   self->used_by_compositor = TRUE;
235 }