broadway: Reuse surfaces passed to server

This way we don't have to reopen all the time for pure updates,
and we can immediately unlink the shm file to avoid "leaking" them
on improper shutdown.
This commit is contained in:
Alexander Larsson 2013-03-29 09:50:59 +01:00
parent c6baa9bc25
commit e9b8b5c1f0

View File

@ -99,6 +99,9 @@ struct BroadwayWindow {
gint32 transient_for;
cairo_surface_t *last_surface;
char *cached_surface_name;
cairo_surface_t *cached_surface;
};
static void broadway_server_resync_windows (BroadwayServer *server);
@ -1416,6 +1419,12 @@ broadway_server_destroy_window (BroadwayServer *server,
server->toplevels = g_list_remove (server->toplevels, window);
g_hash_table_remove (server->id_ht,
GINT_TO_POINTER (id));
if (window->cached_surface_name != NULL)
g_free (window->cached_surface_name);
if (window->cached_surface != NULL)
cairo_surface_destroy (window->cached_surface);
g_free (window);
}
}
@ -1833,12 +1842,22 @@ broadway_server_open_surface (BroadwayServer *server,
int width,
int height)
{
BroadwayWindow *window;
ShmSurfaceData *data;
cairo_surface_t *surface;
gsize size;
void *ptr;
int fd;
window = g_hash_table_lookup (server->id_ht,
GINT_TO_POINTER (id));
if (window == NULL)
return NULL;
if (window->cached_surface_name != NULL &&
strcmp (name, window->cached_surface_name) == 0)
return cairo_surface_reference (window->cached_surface);
size = width * height * sizeof (guint32);
fd = shm_open(name, O_RDONLY, 0600);
@ -1851,6 +1870,8 @@ broadway_server_open_surface (BroadwayServer *server,
ptr = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0);
(void) close(fd);
shm_unlink (name);
if (ptr == NULL)
return NULL;
@ -1868,6 +1889,14 @@ broadway_server_open_surface (BroadwayServer *server,
cairo_surface_set_user_data (surface, &shm_cairo_key,
data, shm_data_unmap);
if (window->cached_surface_name != NULL)
g_free (window->cached_surface_name);
window->cached_surface_name = g_strdup (name);
if (window->cached_surface != NULL)
cairo_surface_destroy (window->cached_surface);
window->cached_surface = cairo_surface_reference (surface);
return surface;
}