wayland: Move surface texture ownership to MetaWaylandSurface

Prior to this commit, MetaWaylandSurface held a reference to
MetaWaylandBuffer, who owned the texture drawn by the surface. When
switching buffer, the texture change with it.

This is problematic when dealing with SHM buffer damage management, as
when having one texture per buffer, damaged regions uploaded to one,
will not follow along to the next one attached. It also wasted GPU
memory as there would be one texture per buffer, instead of one one
texture per surface.

Instead, move the texture ownership to MetaWaylandSurface, and have the
SHM buffer damage management update the surface texture. This ensures
damage is processed properly, and that we won't end up with stale
texture content when doing partial texture uploads. If the same SHM
buffer is attached to multiple surfaces, each surface will get their own
copy, and damage is tracked and uploaded separately.

Non-SHM types of buffers still has their own texture reference, as the
texture is just a representation of the GPU memory associated with the
buffer. When such a buffer is attached to a surface, instead the surface
just gets a reference to that texture, instead of a separately allocated
one.

Fixes: https://gitlab.gnome.org/GNOME/mutter/issues/199
This commit is contained in:
Jonas Ådahl
2019-02-08 11:46:48 +01:00
parent d2f1edd6c6
commit 252e64a0ea
9 changed files with 152 additions and 87 deletions

View File

@ -139,7 +139,7 @@ meta_wayland_buffer_realize (MetaWaylandBuffer *buffer)
buffer->egl_stream.stream = stream;
buffer->type = META_WAYLAND_BUFFER_TYPE_EGL_STREAM;
buffer->texture = COGL_TEXTURE (texture);
buffer->egl_stream.texture = COGL_TEXTURE (texture);
buffer->is_y_inverted = meta_wayland_egl_stream_is_y_inverted (stream);
return TRUE;
@ -196,8 +196,10 @@ shm_buffer_get_cogl_pixel_format (struct wl_shm_buffer *shm_buffer,
}
static gboolean
shm_buffer_attach (MetaWaylandBuffer *buffer,
GError **error)
shm_buffer_attach (MetaWaylandBuffer *buffer,
CoglTexture **texture,
gboolean *changed_texture,
GError **error)
{
MetaBackend *backend = meta_get_backend ();
ClutterBackend *clutter_backend = meta_backend_get_clutter_backend (backend);
@ -207,48 +209,60 @@ shm_buffer_attach (MetaWaylandBuffer *buffer,
CoglPixelFormat format;
CoglTextureComponents components;
CoglBitmap *bitmap;
CoglTexture *texture;
if (buffer->texture)
return TRUE;
CoglTexture2D *texture_2d;
shm_buffer = wl_shm_buffer_get (buffer->resource);
stride = wl_shm_buffer_get_stride (shm_buffer);
width = wl_shm_buffer_get_width (shm_buffer);
height = wl_shm_buffer_get_height (shm_buffer);
shm_buffer_get_cogl_pixel_format (shm_buffer, &format, &components);
if (*texture &&
cogl_texture_get_width (*texture) == width &&
cogl_texture_get_height (*texture) == height &&
cogl_texture_get_components (*texture) == components &&
_cogl_texture_get_format (*texture) == format)
{
buffer->is_y_inverted = TRUE;
*changed_texture = FALSE;
return TRUE;
}
cogl_clear_object (texture);
wl_shm_buffer_begin_access (shm_buffer);
shm_buffer_get_cogl_pixel_format (shm_buffer, &format, &components);
bitmap = cogl_bitmap_new_for_data (cogl_context,
width, height,
format,
stride,
wl_shm_buffer_get_data (shm_buffer));
texture = COGL_TEXTURE (cogl_texture_2d_new_from_bitmap (bitmap));
cogl_texture_set_components (COGL_TEXTURE (texture), components);
texture_2d = cogl_texture_2d_new_from_bitmap (bitmap);
cogl_texture_set_components (COGL_TEXTURE (texture_2d), components);
cogl_object_unref (bitmap);
if (!cogl_texture_allocate (COGL_TEXTURE (texture), error))
g_clear_pointer (&texture, cogl_object_unref);
if (!cogl_texture_allocate (COGL_TEXTURE (texture_2d), error))
g_clear_pointer (&texture_2d, cogl_object_unref);
wl_shm_buffer_end_access (shm_buffer);
buffer->texture = texture;
buffer->is_y_inverted = TRUE;
if (!buffer->texture)
if (!texture_2d)
return FALSE;
*texture = COGL_TEXTURE (texture_2d);
*changed_texture = TRUE;
buffer->is_y_inverted = TRUE;
return TRUE;
}
static gboolean
egl_image_buffer_attach (MetaWaylandBuffer *buffer,
GError **error)
egl_image_buffer_attach (MetaWaylandBuffer *buffer,
CoglTexture **texture,
gboolean *changed_texture,
GError **error)
{
MetaBackend *backend = meta_get_backend ();
MetaEgl *egl = meta_backend_get_egl (backend);
@ -258,10 +272,15 @@ egl_image_buffer_attach (MetaWaylandBuffer *buffer,
int format, width, height, y_inverted;
CoglPixelFormat cogl_format;
EGLImageKHR egl_image;
CoglTexture2D *texture;
CoglTexture2D *texture_2d;
if (buffer->texture)
return TRUE;
if (buffer->egl_image.texture)
{
*changed_texture = *texture != buffer->egl_image.texture;
cogl_clear_object (texture);
*texture = cogl_object_ref (buffer->egl_image.texture);
return TRUE;
}
if (!meta_egl_query_wayland_buffer (egl, egl_display, buffer->resource,
EGL_TEXTURE_FORMAT, &format,
@ -307,26 +326,32 @@ egl_image_buffer_attach (MetaWaylandBuffer *buffer,
if (egl_image == EGL_NO_IMAGE_KHR)
return FALSE;
texture = cogl_egl_texture_2d_new_from_image (cogl_context,
width, height,
cogl_format,
egl_image,
error);
texture_2d = cogl_egl_texture_2d_new_from_image (cogl_context,
width, height,
cogl_format,
egl_image,
error);
meta_egl_destroy_image (egl, egl_display, egl_image, NULL);
if (!texture)
if (!texture_2d)
return FALSE;
buffer->texture = COGL_TEXTURE (texture);
buffer->egl_image.texture = COGL_TEXTURE (texture_2d);
buffer->is_y_inverted = !!y_inverted;
cogl_clear_object (texture);
*texture = cogl_object_ref (buffer->egl_image.texture);
*changed_texture = TRUE;
return TRUE;
}
#ifdef HAVE_WAYLAND_EGLSTREAM
static gboolean
egl_stream_buffer_attach (MetaWaylandBuffer *buffer,
CoglTexture **texture,
gboolean *changed_texture,
GError **error)
{
MetaWaylandEglStream *stream = buffer->egl_stream.stream;
@ -336,13 +361,38 @@ egl_stream_buffer_attach (MetaWaylandBuffer *buffer,
if (!meta_wayland_egl_stream_attach (stream, error))
return FALSE;
*changed_texture = *texture != buffer->egl_stream.texture;
cogl_clear_object (texture);
*texture = cogl_object_ref (buffer->egl_stream.texture);
return TRUE;
}
#endif /* HAVE_WAYLAND_EGLSTREAM */
/**
* meta_wayland_buffer_attach:
* @buffer: a pointer to a #MetaWaylandBuffer
* @texture: (inout) (transfer full): a #CoglTexture representing the surface
* content
* @error: return location for error or %NULL
*
* This function should be passed a pointer to the texture used to draw the
* surface content. The texture will either be replaced by a new texture, or
* stay the same, in which case, it may later be updated with new content when
* processing damage. The new texture might be newly created, or it may be a
* reference to an already existing one.
*
* If replaced, the old texture will have its reference count decreased by one,
* potentially freeing it. When a new texture is set, the caller (i.e. the
* surface) will be the owner of one reference count. It must free it, either
* using g_object_unref() or have it updated again using
* meta_wayland_buffer_attach(), which also might free it, as described above.
*/
gboolean
meta_wayland_buffer_attach (MetaWaylandBuffer *buffer,
GError **error)
meta_wayland_buffer_attach (MetaWaylandBuffer *buffer,
CoglTexture **texture,
gboolean *changed_texture,
GError **error)
{
g_return_val_if_fail (buffer->resource, FALSE);
@ -358,15 +408,18 @@ meta_wayland_buffer_attach (MetaWaylandBuffer *buffer,
switch (buffer->type)
{
case META_WAYLAND_BUFFER_TYPE_SHM:
return shm_buffer_attach (buffer, error);
return shm_buffer_attach (buffer, texture, changed_texture, error);
case META_WAYLAND_BUFFER_TYPE_EGL_IMAGE:
return egl_image_buffer_attach (buffer, error);
return egl_image_buffer_attach (buffer, texture, changed_texture, error);
#ifdef HAVE_WAYLAND_EGLSTREAM
case META_WAYLAND_BUFFER_TYPE_EGL_STREAM:
return egl_stream_buffer_attach (buffer, error);
return egl_stream_buffer_attach (buffer, texture, changed_texture, error);
#endif
case META_WAYLAND_BUFFER_TYPE_DMA_BUF:
return meta_wayland_dma_buf_buffer_attach (buffer, error);
return meta_wayland_dma_buf_buffer_attach (buffer,
texture,
changed_texture,
error);
case META_WAYLAND_BUFFER_TYPE_UNKNOWN:
g_assert_not_reached ();
return FALSE;
@ -376,12 +429,6 @@ meta_wayland_buffer_attach (MetaWaylandBuffer *buffer,
return FALSE;
}
CoglTexture *
meta_wayland_buffer_get_texture (MetaWaylandBuffer *buffer)
{
return buffer->texture;
}
CoglSnippet *
meta_wayland_buffer_create_snippet (MetaWaylandBuffer *buffer)
{
@ -403,6 +450,7 @@ meta_wayland_buffer_is_y_inverted (MetaWaylandBuffer *buffer)
static gboolean
process_shm_buffer_damage (MetaWaylandBuffer *buffer,
CoglTexture *texture,
cairo_region_t *region,
GError **error)
{
@ -427,7 +475,7 @@ process_shm_buffer_damage (MetaWaylandBuffer *buffer,
bpp = _cogl_pixel_format_get_bytes_per_pixel (format);
cairo_region_get_rectangle (region, i, &rect);
if (!_cogl_texture_set_region (buffer->texture,
if (!_cogl_texture_set_region (texture,
rect.width, rect.height,
format,
stride,
@ -448,6 +496,7 @@ process_shm_buffer_damage (MetaWaylandBuffer *buffer,
void
meta_wayland_buffer_process_damage (MetaWaylandBuffer *buffer,
CoglTexture *texture,
cairo_region_t *region)
{
gboolean res = FALSE;
@ -458,7 +507,7 @@ meta_wayland_buffer_process_damage (MetaWaylandBuffer *buffer,
switch (buffer->type)
{
case META_WAYLAND_BUFFER_TYPE_SHM:
res = process_shm_buffer_damage (buffer, region, &error);
res = process_shm_buffer_damage (buffer, texture, region, &error);
break;
case META_WAYLAND_BUFFER_TYPE_EGL_IMAGE:
#ifdef HAVE_WAYLAND_EGLSTREAM
@ -487,10 +536,12 @@ meta_wayland_buffer_finalize (GObject *object)
{
MetaWaylandBuffer *buffer = META_WAYLAND_BUFFER (object);
g_clear_pointer (&buffer->texture, cogl_object_unref);
g_clear_pointer (&buffer->egl_image.texture, cogl_object_unref);
#ifdef HAVE_WAYLAND_EGLSTREAM
g_clear_pointer (&buffer->egl_stream.texture, cogl_object_unref);
g_clear_object (&buffer->egl_stream.stream);
#endif
g_clear_pointer (&buffer->dma_buf.texture, cogl_object_unref);
g_clear_object (&buffer->dma_buf.dma_buf);
G_OBJECT_CLASS (meta_wayland_buffer_parent_class)->finalize (object);