| 1 |
/* GStreamer Wayland Library |
| 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 weak ref data). |
| 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 |
#ifdef HAVE_CONFIG_H |
| 80 |
#include <config.h> |
| 81 |
#endif |
| 82 |
|
| 83 |
#include "gstwlbuffer.h" |
| 84 |
|
| 85 |
#define GST_CAT_DEFAULT gst_wl_buffer_debug |
| 86 |
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); |
| 87 |
|
| 88 |
typedef struct _GstWlBufferPrivate |
| 89 |
{ |
| 90 |
struct wl_buffer *wlbuffer; |
| 91 |
GstBuffer *current_gstbuffer; |
| 92 |
GstMemory *gstmem; |
| 93 |
|
| 94 |
GstWlDisplay *display; |
| 95 |
|
| 96 |
gboolean used_by_compositor; |
| 97 |
} GstWlBufferPrivate; |
| 98 |
|
| 99 |
G_DEFINE_TYPE_WITH_CODE (GstWlBuffer, gst_wl_buffer, G_TYPE_OBJECT, |
| 100 |
G_ADD_PRIVATE (GstWlBuffer) |
| 101 |
GST_DEBUG_CATEGORY_INIT (gst_wl_buffer_debug, |
| 102 |
"wlbuffer", 0, "wlbuffer library"); |
| 103 |
); |
| 104 |
|
| 105 |
static void |
| 106 |
gst_wl_buffer_dispose (GObject * gobject) |
| 107 |
{ |
| 108 |
GstWlBuffer *self = GST_WL_BUFFER (gobject); |
| 109 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self); |
| 110 |
|
| 111 |
GST_TRACE_OBJECT (self, "dispose"); |
| 112 |
|
| 113 |
/* if the display is shutting down and we are trying to dipose |
| 114 |
* the GstWlBuffer from another thread, unregister_buffer() will |
| 115 |
* block and in the end the display will increase the refcount |
| 116 |
* of this GstWlBuffer, so it will not be finalized */ |
| 117 |
if (priv->display) { |
| 118 |
gst_wl_display_unregister_buffer (priv->display, priv->gstmem); |
| 119 |
} |
| 120 |
|
| 121 |
G_OBJECT_CLASS (gst_wl_buffer_parent_class)->dispose (gobject); |
| 122 |
} |
| 123 |
|
| 124 |
static void |
| 125 |
gst_wl_buffer_finalize (GObject * gobject) |
| 126 |
{ |
| 127 |
GstWlBuffer *self = GST_WL_BUFFER (gobject); |
| 128 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self); |
| 129 |
|
| 130 |
GST_TRACE_OBJECT (self, "finalize"); |
| 131 |
|
| 132 |
if (priv->wlbuffer) |
| 133 |
wl_buffer_destroy (priv->wlbuffer); |
| 134 |
|
| 135 |
G_OBJECT_CLASS (gst_wl_buffer_parent_class)->finalize (gobject); |
| 136 |
} |
| 137 |
|
| 138 |
static void |
| 139 |
gst_wl_buffer_class_init (GstWlBufferClass * klass) |
| 140 |
{ |
| 141 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 142 |
|
| 143 |
object_class->dispose = gst_wl_buffer_dispose; |
| 144 |
object_class->finalize = gst_wl_buffer_finalize; |
| 145 |
} |
| 146 |
|
| 147 |
static void |
| 148 |
gst_wl_buffer_init (GstWlBuffer * self) |
| 149 |
{ |
| 150 |
} |
| 151 |
|
| 152 |
static void |
| 153 |
buffer_release (void *data, struct wl_buffer *wl_buffer) |
| 154 |
{ |
| 155 |
GstWlBuffer *self = data; |
| 156 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self); |
| 157 |
GstBuffer *buf = priv->current_gstbuffer; |
| 158 |
|
| 159 |
GST_LOG_OBJECT (self, "wl_buffer::release (GstBuffer: %p)", buf); |
| 160 |
|
| 161 |
priv->used_by_compositor = FALSE; |
| 162 |
priv->current_gstbuffer = NULL; |
| 163 |
|
| 164 |
/* unref should be last, because it may end up destroying the GstWlBuffer */ |
| 165 |
gst_buffer_unref (buf); |
| 166 |
} |
| 167 |
|
| 168 |
static const struct wl_buffer_listener buffer_listener = { |
| 169 |
buffer_release |
| 170 |
}; |
| 171 |
|
| 172 |
static void |
| 173 |
gstmemory_disposed (GstWlBuffer * self) |
| 174 |
{ |
| 175 |
#ifndef G_DISABLE_ASSERT |
| 176 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self); |
| 177 |
g_assert (!priv->used_by_compositor); |
| 178 |
#endif |
| 179 |
|
| 180 |
GST_TRACE_OBJECT (self, "owning GstMemory was finalized"); |
| 181 |
|
| 182 |
/* this will normally destroy the GstWlBuffer, unless the display is |
| 183 |
* finalizing and it has taken an additional reference to it */ |
| 184 |
g_object_unref (self); |
| 185 |
} |
| 186 |
|
| 187 |
GstWlBuffer * |
| 188 |
gst_buffer_add_wl_buffer (GstBuffer * gstbuffer, struct wl_buffer *wlbuffer, |
| 189 |
GstWlDisplay * display) |
| 190 |
{ |
| 191 |
GstWlBuffer *self; |
| 192 |
GstWlBufferPrivate *priv; |
| 193 |
|
| 194 |
self = g_object_new (GST_TYPE_WL_BUFFER, NULL); |
| 195 |
priv = gst_wl_buffer_get_instance_private (self); |
| 196 |
priv->current_gstbuffer = gstbuffer; |
| 197 |
priv->wlbuffer = wlbuffer; |
| 198 |
priv->display = display; |
| 199 |
priv->gstmem = gst_buffer_peek_memory (gstbuffer, 0); |
| 200 |
|
| 201 |
gst_wl_display_register_buffer (priv->display, priv->gstmem, self); |
| 202 |
|
| 203 |
wl_buffer_add_listener (priv->wlbuffer, &buffer_listener, self); |
| 204 |
|
| 205 |
gst_mini_object_weak_ref (GST_MINI_OBJECT (priv->gstmem), |
| 206 |
(GstMiniObjectNotify) gstmemory_disposed, self); |
| 207 |
|
| 208 |
|
| 209 |
return self; |
| 210 |
} |
| 211 |
|
| 212 |
GstWlBuffer * |
| 213 |
gst_buffer_get_wl_buffer (GstWlDisplay * display, GstBuffer * gstbuffer) |
| 214 |
{ |
| 215 |
GstMemory *mem0; |
| 216 |
GstWlBuffer *wlbuf; |
| 217 |
|
| 218 |
if (!gstbuffer) |
| 219 |
return NULL; |
| 220 |
|
| 221 |
mem0 = gst_buffer_peek_memory (gstbuffer, 0); |
| 222 |
|
| 223 |
wlbuf = gst_wl_display_lookup_buffer (display, mem0); |
| 224 |
if (wlbuf) { |
| 225 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (wlbuf); |
| 226 |
|
| 227 |
priv->current_gstbuffer = gstbuffer; |
| 228 |
} |
| 229 |
|
| 230 |
return wlbuf; |
| 231 |
} |
| 232 |
|
| 233 |
void |
| 234 |
gst_wl_buffer_force_release_and_unref (GstBuffer * buf, GstWlBuffer * self) |
| 235 |
{ |
| 236 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self); |
| 237 |
|
| 238 |
/* Force a buffer release. |
| 239 |
* At this point, the GstWlDisplay has killed its event loop, |
| 240 |
* so we don't need to worry about buffer_release() being called |
| 241 |
* at the same time from the event loop thread */ |
| 242 |
if (priv->used_by_compositor) { |
| 243 |
GST_DEBUG_OBJECT (self, "forcing wl_buffer::release (GstBuffer: %p)", |
| 244 |
priv->current_gstbuffer); |
| 245 |
priv->used_by_compositor = FALSE; |
| 246 |
gst_buffer_unref (priv->current_gstbuffer); |
| 247 |
} |
| 248 |
|
| 249 |
/* Finalize this GstWlBuffer early. |
| 250 |
* This method has been called as a result of the display shutting down, |
| 251 |
* so we need to stop using any wayland resources and disconnect from |
| 252 |
* the display. The GstWlBuffer stays alive, though, to avoid race |
| 253 |
* conditions with the GstBuffer being destroyed from another thread. |
| 254 |
* The last reference is either owned by the GstBuffer or by us and |
| 255 |
* it will be released at the end of this function. */ |
| 256 |
GST_TRACE_OBJECT (self, "finalizing early"); |
| 257 |
wl_buffer_destroy (priv->wlbuffer); |
| 258 |
priv->wlbuffer = NULL; |
| 259 |
priv->display = NULL; |
| 260 |
priv->current_gstbuffer = NULL; |
| 261 |
|
| 262 |
/* remove the reference that the caller (GstWlDisplay) owns */ |
| 263 |
g_object_unref (self); |
| 264 |
} |
| 265 |
|
| 266 |
void |
| 267 |
gst_wl_buffer_attach (GstWlBuffer * self, struct wl_surface *surface) |
| 268 |
{ |
| 269 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self); |
| 270 |
|
| 271 |
if (priv->used_by_compositor) { |
| 272 |
GST_DEBUG_OBJECT (self, "buffer used by compositor %p", |
| 273 |
priv->current_gstbuffer); |
| 274 |
return; |
| 275 |
} |
| 276 |
|
| 277 |
wl_surface_attach (surface, priv->wlbuffer, 0, 0); |
| 278 |
|
| 279 |
/* Add a reference to the buffer. This represents the fact that |
| 280 |
* the compositor is using the buffer and it should not return |
| 281 |
* back to the pool and be re-used until the compositor releases it. */ |
| 282 |
gst_buffer_ref (priv->current_gstbuffer); |
| 283 |
priv->used_by_compositor = TRUE; |
| 284 |
} |
| 285 |
|
| 286 |
void |
| 287 |
gst_wl_buffer_ref_gst_buffer (GstWlBuffer * self) |
| 288 |
{ |
| 289 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self); |
| 290 |
gst_buffer_ref (priv->current_gstbuffer); |
| 291 |
} |
| 292 |
|
| 293 |
void |
| 294 |
gst_wl_buffer_unref_buffer (GstWlBuffer * self) |
| 295 |
{ |
| 296 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self); |
| 297 |
gst_buffer_unref (priv->current_gstbuffer); |
| 298 |
} |
| 299 |
|
| 300 |
GstWlDisplay * |
| 301 |
gst_wl_buffer_get_display (GstWlBuffer * self) |
| 302 |
{ |
| 303 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self); |
| 304 |
|
| 305 |
return priv->display; |
| 306 |
} |
| 307 |
|
| 308 |
GstVideoMeta * |
| 309 |
gst_wl_buffer_get_video_meta (GstWlBuffer * self) |
| 310 |
{ |
| 311 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self); |
| 312 |
return gst_buffer_get_video_meta (priv->current_gstbuffer); |
| 313 |
} |
| 314 |
|
| 315 |
GstVideoCropMeta * |
| 316 |
gst_wl_buffer_get_video_crop_meta (GstWlBuffer * self) |
| 317 |
{ |
| 318 |
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self); |
| 319 |
return gst_buffer_get_video_crop_meta (priv->current_gstbuffer); |
| 320 |
} |