2022-01-04 14:20:41 +00:00
|
|
|
/* GStreamer Wayland Library
|
2014-06-20 11:47:57 +00:00
|
|
|
*
|
|
|
|
* Copyright (C) 2014 Collabora Ltd.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free
|
|
|
|
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* GstWlBuffer wraps wl_buffer and provides a mechanism for preventing
|
|
|
|
* buffers from being re-used while the compositor is using them. This
|
|
|
|
* is achieved by adding a reference to the GstBuffer as soon as its
|
|
|
|
* associated wl_buffer is sent to the compositor and by removing this
|
|
|
|
* reference as soon as the compositor sends a wl_buffer::release message.
|
|
|
|
*
|
|
|
|
* This mechanism is a bit complicated, though, because it adds cyclic
|
|
|
|
* references that can be dangerous. The reference cycles looks like:
|
|
|
|
*
|
|
|
|
* ----------------
|
2014-06-23 14:27:01 +00:00
|
|
|
* | GstWlDisplay | ---------------------------->
|
|
|
|
* ---------------- |
|
|
|
|
* |
|
|
|
|
* V
|
|
|
|
* ----------------- ------------- ---------------
|
|
|
|
* | GstBufferPool | --> | GstBuffer | ==> | GstWlBuffer |
|
|
|
|
* | | <-- | | <-- | |
|
|
|
|
* ----------------- ------------- ---------------
|
2014-06-20 11:47:57 +00:00
|
|
|
*
|
|
|
|
* A GstBufferPool normally holds references to its GstBuffers and each buffer
|
2020-07-08 10:06:38 +00:00
|
|
|
* holds a reference to a GstWlBuffer (saved in the GstMiniObject weak ref data).
|
2014-06-20 11:47:57 +00:00
|
|
|
* When a GstBuffer is in use, it holds a reference back to the pool and the
|
|
|
|
* pool doesn't hold a reference to the GstBuffer. When the GstBuffer is unrefed
|
|
|
|
* externally, it returns back to the pool and the pool holds again a reference
|
|
|
|
* to the buffer.
|
|
|
|
*
|
|
|
|
* Now when the compositor is using a buffer, the GstWlBuffer also holds a ref
|
|
|
|
* to the GstBuffer, which prevents it from returning to the pool. When the
|
|
|
|
* last GstWlBuffer receives a release event and unrefs the last GstBuffer,
|
|
|
|
* the GstBufferPool will be able to stop and if no-one is holding a strong
|
2014-06-23 14:27:01 +00:00
|
|
|
* ref to it, it will be destroyed. This will destroy the pool's GstBuffers and
|
|
|
|
* also the GstWlBuffers. This will all happen in the same context of the last
|
2014-06-20 11:47:57 +00:00
|
|
|
* gst_buffer_unref, which will be called from the buffer_release() callback.
|
|
|
|
*
|
2014-06-23 14:27:01 +00:00
|
|
|
* The problem here lies in the fact that buffer_release() will be called
|
|
|
|
* from the event loop thread of GstWlDisplay, so it's as if the display
|
|
|
|
* holds a reference to the GstWlBuffer, but without having an actual reference.
|
|
|
|
* When we kill the display, there is no way for the GstWlBuffer, the associated
|
|
|
|
* GstBuffer and the GstBufferPool to get destroyed, so we are going to leak a
|
2019-09-02 19:08:44 +00:00
|
|
|
* fair amount of memory.
|
2014-06-20 11:47:57 +00:00
|
|
|
*
|
2014-07-02 10:29:55 +00:00
|
|
|
* Normally, this rarely happens, because the compositor releases buffers
|
|
|
|
* almost immediately and when waylandsink stops, they are already released.
|
2014-06-20 11:47:57 +00:00
|
|
|
*
|
|
|
|
* However, we want to be absolutely certain, so a solution is introduced
|
2014-06-23 14:27:01 +00:00
|
|
|
* by registering all the GstWlBuffers with the display and explicitly
|
2014-07-02 10:29:55 +00:00
|
|
|
* releasing all the buffer references as soon as the display is destroyed.
|
|
|
|
*
|
|
|
|
* When the GstWlDisplay is finalized, it takes a reference to all the
|
|
|
|
* registered GstWlBuffers and then calls gst_wl_buffer_force_release_and_unref,
|
|
|
|
* which releases the potential reference to the GstBuffer, destroys the
|
|
|
|
* underlying wl_buffer and removes the reference that GstWlDisplay is holding.
|
|
|
|
* At that point, either the GstBuffer is alive somewhere and still holds a ref
|
|
|
|
* to the GstWlBuffer, which it will release when it gets destroyed, or the
|
|
|
|
* GstBuffer was destroyed in the meantime and the GstWlBuffer gets destroyed
|
|
|
|
* as soon as we remove the reference that GstWlDisplay holds.
|
2014-06-20 11:47:57 +00:00
|
|
|
*/
|
|
|
|
|
2022-01-04 14:20:41 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2014-06-20 11:47:57 +00:00
|
|
|
|
2022-01-04 14:20:41 +00:00
|
|
|
#include "gstwlbuffer.h"
|
2014-06-20 11:47:57 +00:00
|
|
|
|
2022-01-04 14:20:41 +00:00
|
|
|
#define GST_CAT_DEFAULT gst_wl_buffer_debug
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
|
|
|
|
|
|
|
|
typedef struct _GstWlBufferPrivate
|
|
|
|
{
|
|
|
|
struct wl_buffer *wlbuffer;
|
|
|
|
GstBuffer *current_gstbuffer;
|
|
|
|
GstMemory *gstmem;
|
|
|
|
|
|
|
|
GstWlDisplay *display;
|
|
|
|
|
|
|
|
gboolean used_by_compositor;
|
|
|
|
} GstWlBufferPrivate;
|
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (GstWlBuffer, gst_wl_buffer, G_TYPE_OBJECT,
|
|
|
|
G_ADD_PRIVATE (GstWlBuffer)
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_wl_buffer_debug,
|
|
|
|
"wlbuffer", 0, "wlbuffer library");
|
|
|
|
);
|
2014-06-20 11:47:57 +00:00
|
|
|
|
|
|
|
static void
|
2014-07-02 10:29:55 +00:00
|
|
|
gst_wl_buffer_dispose (GObject * gobject)
|
2014-06-20 11:47:57 +00:00
|
|
|
{
|
|
|
|
GstWlBuffer *self = GST_WL_BUFFER (gobject);
|
2022-01-04 14:20:41 +00:00
|
|
|
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self);
|
2014-06-20 11:47:57 +00:00
|
|
|
|
2014-07-02 10:29:55 +00:00
|
|
|
GST_TRACE_OBJECT (self, "dispose");
|
|
|
|
|
|
|
|
/* if the display is shutting down and we are trying to dipose
|
|
|
|
* the GstWlBuffer from another thread, unregister_buffer() will
|
|
|
|
* block and in the end the display will increase the refcount
|
|
|
|
* of this GstWlBuffer, so it will not be finalized */
|
2022-01-04 14:20:41 +00:00
|
|
|
if (priv->display) {
|
|
|
|
gst_wl_display_unregister_buffer (priv->display, priv->gstmem);
|
2020-07-15 04:30:48 +00:00
|
|
|
}
|
2014-07-02 10:29:55 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_wl_buffer_parent_class)->dispose (gobject);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wl_buffer_finalize (GObject * gobject)
|
|
|
|
{
|
|
|
|
GstWlBuffer *self = GST_WL_BUFFER (gobject);
|
2022-01-04 14:20:41 +00:00
|
|
|
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self);
|
2014-07-02 10:29:55 +00:00
|
|
|
|
|
|
|
GST_TRACE_OBJECT (self, "finalize");
|
|
|
|
|
2022-01-04 14:20:41 +00:00
|
|
|
if (priv->wlbuffer)
|
|
|
|
wl_buffer_destroy (priv->wlbuffer);
|
2014-06-20 11:47:57 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gst_wl_buffer_parent_class)->finalize (gobject);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wl_buffer_class_init (GstWlBufferClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = (GObjectClass *) klass;
|
|
|
|
|
2014-07-02 10:29:55 +00:00
|
|
|
object_class->dispose = gst_wl_buffer_dispose;
|
2014-06-20 11:47:57 +00:00
|
|
|
object_class->finalize = gst_wl_buffer_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_wl_buffer_init (GstWlBuffer * self)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
buffer_release (void *data, struct wl_buffer *wl_buffer)
|
|
|
|
{
|
|
|
|
GstWlBuffer *self = data;
|
2022-01-04 14:20:41 +00:00
|
|
|
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self);
|
|
|
|
GstBuffer *buf = priv->current_gstbuffer;
|
2014-06-20 11:47:57 +00:00
|
|
|
|
2020-07-22 07:32:37 +00:00
|
|
|
GST_LOG_OBJECT (self, "wl_buffer::release (GstBuffer: %p)", buf);
|
2014-06-20 11:47:57 +00:00
|
|
|
|
2022-01-04 14:20:41 +00:00
|
|
|
priv->used_by_compositor = FALSE;
|
|
|
|
priv->current_gstbuffer = NULL;
|
2014-06-20 11:47:57 +00:00
|
|
|
|
|
|
|
/* unref should be last, because it may end up destroying the GstWlBuffer */
|
2020-07-22 07:32:37 +00:00
|
|
|
gst_buffer_unref (buf);
|
2014-06-20 11:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct wl_buffer_listener buffer_listener = {
|
|
|
|
buffer_release
|
|
|
|
};
|
|
|
|
|
2014-07-02 10:29:55 +00:00
|
|
|
static void
|
2020-07-15 04:30:48 +00:00
|
|
|
gstmemory_disposed (GstWlBuffer * self)
|
2014-07-02 10:29:55 +00:00
|
|
|
{
|
2022-01-04 14:20:41 +00:00
|
|
|
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self);
|
|
|
|
|
|
|
|
g_assert (!priv->used_by_compositor);
|
2014-07-02 10:29:55 +00:00
|
|
|
|
2020-07-15 04:30:48 +00:00
|
|
|
GST_TRACE_OBJECT (self, "owning GstMemory was finalized");
|
2014-07-02 10:29:55 +00:00
|
|
|
|
|
|
|
/* this will normally destroy the GstWlBuffer, unless the display is
|
|
|
|
* finalizing and it has taken an additional reference to it */
|
|
|
|
g_object_unref (self);
|
|
|
|
}
|
|
|
|
|
2014-07-01 08:43:20 +00:00
|
|
|
GstWlBuffer *
|
2014-06-20 11:47:57 +00:00
|
|
|
gst_buffer_add_wl_buffer (GstBuffer * gstbuffer, struct wl_buffer *wlbuffer,
|
|
|
|
GstWlDisplay * display)
|
|
|
|
{
|
|
|
|
GstWlBuffer *self;
|
2022-01-04 14:20:41 +00:00
|
|
|
GstWlBufferPrivate *priv;
|
2014-06-20 11:47:57 +00:00
|
|
|
|
|
|
|
self = g_object_new (GST_TYPE_WL_BUFFER, NULL);
|
2022-01-04 14:20:41 +00:00
|
|
|
priv = gst_wl_buffer_get_instance_private (self);
|
|
|
|
priv->current_gstbuffer = gstbuffer;
|
|
|
|
priv->wlbuffer = wlbuffer;
|
|
|
|
priv->display = display;
|
|
|
|
priv->gstmem = gst_buffer_peek_memory (gstbuffer, 0);
|
2014-06-20 11:47:57 +00:00
|
|
|
|
2022-01-04 14:20:41 +00:00
|
|
|
gst_wl_display_register_buffer (priv->display, priv->gstmem, self);
|
2014-06-20 11:47:57 +00:00
|
|
|
|
2022-01-04 14:20:41 +00:00
|
|
|
wl_buffer_add_listener (priv->wlbuffer, &buffer_listener, self);
|
2014-06-20 11:47:57 +00:00
|
|
|
|
2022-01-04 14:20:41 +00:00
|
|
|
gst_mini_object_weak_ref (GST_MINI_OBJECT (priv->gstmem),
|
2020-07-15 04:30:48 +00:00
|
|
|
(GstMiniObjectNotify) gstmemory_disposed, self);
|
2020-07-08 10:06:38 +00:00
|
|
|
|
2014-07-01 08:43:20 +00:00
|
|
|
|
|
|
|
return self;
|
2014-06-20 11:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstWlBuffer *
|
2020-07-08 10:06:38 +00:00
|
|
|
gst_buffer_get_wl_buffer (GstWlDisplay * display, GstBuffer * gstbuffer)
|
2014-06-20 11:47:57 +00:00
|
|
|
{
|
2021-09-16 21:12:58 +00:00
|
|
|
GstMemory *mem0;
|
|
|
|
GstWlBuffer *wlbuf;
|
|
|
|
|
|
|
|
if (!gstbuffer)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
mem0 = gst_buffer_peek_memory (gstbuffer, 0);
|
|
|
|
|
|
|
|
wlbuf = gst_wl_display_lookup_buffer (display, mem0);
|
2022-01-04 14:20:41 +00:00
|
|
|
if (wlbuf) {
|
|
|
|
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (wlbuf);
|
|
|
|
|
|
|
|
priv->current_gstbuffer = gstbuffer;
|
|
|
|
}
|
2021-09-16 21:12:58 +00:00
|
|
|
|
2020-07-22 07:32:37 +00:00
|
|
|
return wlbuf;
|
2014-06-20 11:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-07-08 10:06:38 +00:00
|
|
|
gst_wl_buffer_force_release_and_unref (GstBuffer * buf, GstWlBuffer * self)
|
2014-06-20 11:47:57 +00:00
|
|
|
{
|
2022-01-04 14:20:41 +00:00
|
|
|
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self);
|
|
|
|
|
2014-07-02 10:29:55 +00:00
|
|
|
/* Force a buffer release.
|
|
|
|
* At this point, the GstWlDisplay has killed its event loop,
|
2014-06-20 11:47:57 +00:00
|
|
|
* so we don't need to worry about buffer_release() being called
|
|
|
|
* at the same time from the event loop thread */
|
2022-01-04 14:20:41 +00:00
|
|
|
if (priv->used_by_compositor) {
|
2014-06-20 11:47:57 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "forcing wl_buffer::release (GstBuffer: %p)",
|
2022-01-04 14:20:41 +00:00
|
|
|
priv->current_gstbuffer);
|
|
|
|
priv->used_by_compositor = FALSE;
|
|
|
|
gst_buffer_unref (priv->current_gstbuffer);
|
2014-06-20 11:47:57 +00:00
|
|
|
}
|
|
|
|
|
2014-07-02 10:29:55 +00:00
|
|
|
/* Finalize this GstWlBuffer early.
|
|
|
|
* This method has been called as a result of the display shutting down,
|
|
|
|
* so we need to stop using any wayland resources and disconnect from
|
|
|
|
* the display. The GstWlBuffer stays alive, though, to avoid race
|
|
|
|
* conditions with the GstBuffer being destroyed from another thread.
|
|
|
|
* The last reference is either owned by the GstBuffer or by us and
|
|
|
|
* it will be released at the end of this function. */
|
|
|
|
GST_TRACE_OBJECT (self, "finalizing early");
|
2022-01-04 14:20:41 +00:00
|
|
|
wl_buffer_destroy (priv->wlbuffer);
|
|
|
|
priv->wlbuffer = NULL;
|
|
|
|
priv->display = NULL;
|
|
|
|
priv->current_gstbuffer = NULL;
|
2014-07-02 10:29:55 +00:00
|
|
|
|
|
|
|
/* remove the reference that the caller (GstWlDisplay) owns */
|
2014-06-20 11:47:57 +00:00
|
|
|
g_object_unref (self);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-07-01 08:43:20 +00:00
|
|
|
gst_wl_buffer_attach (GstWlBuffer * self, struct wl_surface *surface)
|
2014-06-20 11:47:57 +00:00
|
|
|
{
|
2022-01-04 14:20:41 +00:00
|
|
|
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self);
|
|
|
|
|
|
|
|
if (priv->used_by_compositor) {
|
2020-07-22 07:32:37 +00:00
|
|
|
GST_DEBUG_OBJECT (self, "buffer used by compositor %p",
|
2022-01-04 14:20:41 +00:00
|
|
|
priv->current_gstbuffer);
|
2018-10-18 05:35:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-06-20 11:47:57 +00:00
|
|
|
|
2022-01-04 14:20:41 +00:00
|
|
|
wl_surface_attach (surface, priv->wlbuffer, 0, 0);
|
2014-06-20 11:47:57 +00:00
|
|
|
|
|
|
|
/* Add a reference to the buffer. This represents the fact that
|
|
|
|
* the compositor is using the buffer and it should not return
|
|
|
|
* back to the pool and be re-used until the compositor releases it. */
|
2022-01-04 14:20:41 +00:00
|
|
|
gst_buffer_ref (priv->current_gstbuffer);
|
|
|
|
priv->used_by_compositor = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GstWlDisplay *
|
|
|
|
gst_wl_buffer_get_display (GstWlBuffer * self)
|
|
|
|
{
|
|
|
|
GstWlBufferPrivate *priv = gst_wl_buffer_get_instance_private (self);
|
|
|
|
|
|
|
|
return priv->display;
|
2014-06-20 11:47:57 +00:00
|
|
|
}
|