texture-cache: Replace ClutterTexture by ClutterImage

ClutterTexture is a deprecated class that is simultaneously
an actor, and the content of the actor. Clutter's new model
is to separate painting (via ClutterContent) from actors.

Currently, StTextureCache relies on ClutterTexture to store
the loaded textures. This not only does not match the latest
practices of Clutter, but also generates various compile-time
warnings.

Port StTextureCache to store ClutterImages instead of storing
ClutterTextures. ClutterImage exposes the internal CoglTexture,
so no helpers are needed to match the current StTextureCache
API. Aspect ratio was dropped, but from my testing, it doesn't
change anything.
This commit is contained in:
Georges Basile Stavracas Neto 2019-01-24 13:42:14 -02:00
parent 8bb9eb0fc9
commit deec0bf255
No known key found for this signature in database
GPG Key ID: 886C17EE170D1385
2 changed files with 108 additions and 106 deletions

View File

@ -309,11 +309,11 @@ st_icon_update_shadow_pipeline (StIcon *icon)
} }
static void static void
on_pixbuf_changed (ClutterTexture *texture, on_content_changed (ClutterActor *actor,
StIcon *icon) GParamSpec *pspec,
StIcon *icon)
{ {
st_icon_clear_shadow_pipeline (icon); st_icon_clear_shadow_pipeline (icon);
clutter_actor_queue_redraw (CLUTTER_ACTOR (icon));
} }
static void static void
@ -340,9 +340,8 @@ st_icon_finish_update (StIcon *icon)
st_icon_clear_shadow_pipeline (icon); st_icon_clear_shadow_pipeline (icon);
/* "pixbuf-change" is actually a misnomer for "texture-changed" */ g_signal_connect_object (priv->icon_texture, "notify::content",
g_signal_connect_object (priv->icon_texture, "pixbuf-change", G_CALLBACK (on_content_changed), icon, 0);
G_CALLBACK (on_pixbuf_changed), icon, 0);
} }
} }

View File

@ -36,7 +36,7 @@ struct _StTextureCachePrivate
GtkIconTheme *icon_theme; GtkIconTheme *icon_theme;
/* Things that were loaded with a cache policy != NONE */ /* Things that were loaded with a cache policy != NONE */
GHashTable *keyed_cache; /* char * -> CoglTexture* */ GHashTable *keyed_cache; /* char * -> ClutterImage* */
GHashTable *keyed_surface_cache; /* char * -> cairo_surface_t* */ GHashTable *keyed_surface_cache; /* char * -> cairo_surface_t* */
/* Presently this is used to de-duplicate requests for GIcons and async URIs. */ /* Presently this is used to de-duplicate requests for GIcons and async URIs. */
@ -64,20 +64,23 @@ G_DEFINE_TYPE(StTextureCache, st_texture_cache, G_TYPE_OBJECT);
* pipeline for an empty texture is full opacity white, which we * pipeline for an empty texture is full opacity white, which we
* definitely don't want. Skip that by setting 0 opacity. * definitely don't want. Skip that by setting 0 opacity.
*/ */
static ClutterTexture * static ClutterActor *
create_default_texture (void) create_invisible_actor (void)
{ {
ClutterTexture * texture = CLUTTER_TEXTURE (clutter_texture_new ()); return g_object_new (CLUTTER_TYPE_ACTOR,
g_object_set (texture, "keep-aspect-ratio", TRUE, "opacity", 0, NULL); "opacity", 0,
return texture; NULL);
} }
/* Reverse the opacity we added while loading */ /* Reverse the opacity we added while loading */
static void static void
set_texture_cogl_texture (ClutterTexture *clutter_texture, CoglTexture *cogl_texture) set_content_from_image (ClutterActor *actor,
ClutterContent *image)
{ {
clutter_texture_set_cogl_texture (clutter_texture, cogl_texture); g_assert (image && CLUTTER_IS_IMAGE (image));
g_object_set (clutter_texture, "opacity", 255, NULL);
clutter_actor_set_content (actor, image);
clutter_actor_set_opacity (actor, 255);
} }
static void static void
@ -145,7 +148,7 @@ st_texture_cache_init (StTextureCache *self)
G_CALLBACK (on_icon_theme_changed), self); G_CALLBACK (on_icon_theme_changed), self);
self->priv->keyed_cache = g_hash_table_new_full (g_str_hash, g_str_equal, self->priv->keyed_cache = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, cogl_object_unref); g_free, g_object_unref);
self->priv->keyed_surface_cache = g_hash_table_new_full (g_str_hash, self->priv->keyed_surface_cache = g_hash_table_new_full (g_str_hash,
g_str_equal, g_str_equal,
g_free, g_free,
@ -270,7 +273,7 @@ typedef struct {
guint width; guint width;
guint height; guint height;
guint scale; guint scale;
GSList *textures; GSList *actors;
GtkIconInfo *icon_info; GtkIconInfo *icon_info;
StIconColors *colors; StIconColors *colors;
@ -294,8 +297,8 @@ texture_load_data_free (gpointer p)
if (data->key) if (data->key)
g_free (data->key); g_free (data->key);
if (data->textures) if (data->actors)
g_slist_free_full (data->textures, (GDestroyNotify) g_object_unref); g_slist_free_full (data->actors, (GDestroyNotify) g_object_unref);
g_free (data); g_free (data);
} }
@ -464,29 +467,29 @@ load_pixbuf_async_finish (StTextureCache *cache, GAsyncResult *result, GError **
return g_task_propagate_pointer (G_TASK (result), error); return g_task_propagate_pointer (G_TASK (result), error);
} }
static CoglTexture * static ClutterContent *
pixbuf_to_cogl_texture (GdkPixbuf *pixbuf) pixbuf_to_clutter_image (GdkPixbuf *pixbuf)
{ {
ClutterBackend *backend = clutter_get_default_backend (); ClutterContent *image;
CoglContext *ctx = clutter_backend_get_cogl_context (backend); g_autoptr(GError) error = NULL;
CoglError *error = NULL;
CoglTexture2D *texture;
texture = cogl_texture_2d_new_from_data (ctx, image = clutter_image_new ();
gdk_pixbuf_get_width (pixbuf), clutter_image_set_data (CLUTTER_IMAGE (image),
gdk_pixbuf_get_height (pixbuf), gdk_pixbuf_get_pixels (pixbuf),
gdk_pixbuf_get_has_alpha (pixbuf) ? COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888, gdk_pixbuf_get_has_alpha (pixbuf) ?
gdk_pixbuf_get_rowstride (pixbuf), COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888,
gdk_pixbuf_get_pixels (pixbuf), gdk_pixbuf_get_width (pixbuf),
&error); gdk_pixbuf_get_height (pixbuf),
gdk_pixbuf_get_rowstride (pixbuf),
&error);
if (error) if (error)
{ {
g_warning ("Failed to allocate texture: %s", error->message); g_warning ("Failed to allocate texture: %s", error->message);
cogl_error_free (error); g_clear_object (&image);
} }
return texture ? COGL_TEXTURE (texture) : NULL; return image;
} }
static cairo_surface_t * static cairo_surface_t *
@ -514,9 +517,9 @@ static void
finish_texture_load (AsyncTextureLoadData *data, finish_texture_load (AsyncTextureLoadData *data,
GdkPixbuf *pixbuf) GdkPixbuf *pixbuf)
{ {
g_autoptr(ClutterContent) image = NULL;
GSList *iter; GSList *iter;
StTextureCache *cache; StTextureCache *cache;
CoglTexture *texdata = NULL;
cache = data->cache; cache = data->cache;
@ -525,8 +528,8 @@ finish_texture_load (AsyncTextureLoadData *data,
if (pixbuf == NULL) if (pixbuf == NULL)
goto out; goto out;
texdata = pixbuf_to_cogl_texture (pixbuf); image = pixbuf_to_clutter_image (pixbuf);
if (!texdata) if (!image)
goto out; goto out;
if (data->policy != ST_TEXTURE_CACHE_POLICY_NONE) if (data->policy != ST_TEXTURE_CACHE_POLICY_NONE)
@ -536,22 +539,18 @@ finish_texture_load (AsyncTextureLoadData *data,
if (!g_hash_table_lookup_extended (cache->priv->keyed_cache, data->key, if (!g_hash_table_lookup_extended (cache->priv->keyed_cache, data->key,
&orig_key, &value)) &orig_key, &value))
{ {
cogl_object_ref (texdata);
g_hash_table_insert (cache->priv->keyed_cache, g_strdup (data->key), g_hash_table_insert (cache->priv->keyed_cache, g_strdup (data->key),
texdata); g_object_ref (image));
} }
} }
for (iter = data->textures; iter; iter = iter->next) for (iter = data->actors; iter; iter = iter->next)
{ {
ClutterTexture *texture = iter->data; ClutterActor *actor = iter->data;
set_texture_cogl_texture (texture, texdata); set_content_from_image (actor, image);
} }
out: out:
if (texdata)
cogl_object_unref (texdata);
texture_load_data_free (data); texture_load_data_free (data);
} }
@ -630,7 +629,7 @@ load_texture_async (StTextureCache *cache,
typedef struct { typedef struct {
StTextureCache *cache; StTextureCache *cache;
ClutterTexture *texture; ClutterActor *actor;
GObject *source; GObject *source;
guint notify_signal_id; guint notify_signal_id;
gboolean weakref_active; gboolean weakref_active;
@ -641,9 +640,6 @@ st_texture_cache_reset_texture (StTextureCachePropertyBind *bind,
const char *propname) const char *propname)
{ {
cairo_surface_t *surface; cairo_surface_t *surface;
CoglTexture *texdata;
ClutterBackend *backend = clutter_get_default_backend ();
CoglContext *ctx = clutter_backend_get_cogl_context (backend);
g_object_get (bind->source, propname, &surface, NULL); g_object_get (bind->source, propname, &surface, NULL);
@ -652,32 +648,39 @@ st_texture_cache_reset_texture (StTextureCachePropertyBind *bind,
(cairo_image_surface_get_format (surface) == CAIRO_FORMAT_ARGB32 || (cairo_image_surface_get_format (surface) == CAIRO_FORMAT_ARGB32 ||
cairo_image_surface_get_format (surface) == CAIRO_FORMAT_RGB24)) cairo_image_surface_get_format (surface) == CAIRO_FORMAT_RGB24))
{ {
CoglError *error = NULL; ClutterContent *image;
GError *error = NULL;
texdata = COGL_TEXTURE (cogl_texture_2d_new_from_data (ctx, image = clutter_actor_get_content (bind->actor);
cairo_image_surface_get_width (surface), if (!image || !CLUTTER_IS_IMAGE (image))
cairo_image_surface_get_height (surface), image = clutter_image_new ();
cairo_image_surface_get_format (surface) == CAIRO_FORMAT_ARGB32 ? else
COGL_PIXEL_FORMAT_BGRA_8888 : COGL_PIXEL_FORMAT_BGR_888, g_object_ref (image);
cairo_image_surface_get_stride (surface),
cairo_image_surface_get_data (surface),
&error));
if (texdata) clutter_image_set_data (CLUTTER_IMAGE (image),
cairo_image_surface_get_data (surface),
cairo_image_surface_get_format (surface) == CAIRO_FORMAT_ARGB32 ?
COGL_PIXEL_FORMAT_BGRA_8888 : COGL_PIXEL_FORMAT_BGR_888,
cairo_image_surface_get_width (surface),
cairo_image_surface_get_height (surface),
cairo_image_surface_get_stride (surface),
&error);
if (image)
{ {
clutter_texture_set_cogl_texture (bind->texture, texdata); clutter_actor_set_content (bind->actor, image);
cogl_object_unref (texdata); g_object_unref (image);
} }
else if (error) else if (error)
{ {
g_warning ("Failed to allocate texture: %s", error->message); g_warning ("Failed to allocate texture: %s", error->message);
cogl_error_free (error); g_error_free (error);
} }
clutter_actor_set_opacity (CLUTTER_ACTOR (bind->texture), 255); clutter_actor_set_opacity (bind->actor, 255);
} }
else else
clutter_actor_set_opacity (CLUTTER_ACTOR (bind->texture), 0); clutter_actor_set_opacity (bind->actor, 0);
} }
static void static void
@ -703,7 +706,7 @@ st_texture_cache_free_bind (gpointer data)
{ {
StTextureCachePropertyBind *bind = data; StTextureCachePropertyBind *bind = data;
if (bind->weakref_active) if (bind->weakref_active)
g_object_weak_unref (G_OBJECT(bind->texture), st_texture_cache_bind_weak_notify, bind); g_object_weak_unref (G_OBJECT (bind->actor), st_texture_cache_bind_weak_notify, bind);
g_free (bind); g_free (bind);
} }
@ -727,17 +730,17 @@ st_texture_cache_bind_cairo_surface_property (StTextureCache *cache,
GObject *object, GObject *object,
const char *property_name) const char *property_name)
{ {
ClutterTexture *texture; ClutterActor *actor;
gchar *notify_key; gchar *notify_key;
StTextureCachePropertyBind *bind; StTextureCachePropertyBind *bind;
texture = CLUTTER_TEXTURE (clutter_texture_new ()); actor = clutter_actor_new ();
bind = g_new0 (StTextureCachePropertyBind, 1); bind = g_new0 (StTextureCachePropertyBind, 1);
bind->cache = cache; bind->cache = cache;
bind->texture = texture; bind->actor = actor;
bind->source = object; bind->source = object;
g_object_weak_ref (G_OBJECT (texture), st_texture_cache_bind_weak_notify, bind); g_object_weak_ref (G_OBJECT (actor), st_texture_cache_bind_weak_notify, bind);
bind->weakref_active = TRUE; bind->weakref_active = TRUE;
st_texture_cache_reset_texture (bind, property_name); st_texture_cache_reset_texture (bind, property_name);
@ -747,7 +750,7 @@ st_texture_cache_bind_cairo_surface_property (StTextureCache *cache,
bind, (GClosureNotify)st_texture_cache_free_bind, 0); bind, (GClosureNotify)st_texture_cache_free_bind, 0);
g_free (notify_key); g_free (notify_key);
return CLUTTER_ACTOR(texture); return actor;
} }
/** /**
@ -802,25 +805,25 @@ st_texture_cache_load (StTextureCache *cache,
* is already a request pending, append it to that request to avoid loading * is already a request pending, append it to that request to avoid loading
* the data multiple times. * the data multiple times.
* *
* Returns: %TRUE iff there is already a request pending * Returns: %TRUE if there is already a request pending
*/ */
static gboolean static gboolean
ensure_request (StTextureCache *cache, ensure_request (StTextureCache *cache,
const char *key, const char *key,
StTextureCachePolicy policy, StTextureCachePolicy policy,
AsyncTextureLoadData **request, AsyncTextureLoadData **request,
ClutterActor *texture) ClutterActor *actor)
{ {
CoglTexture *texdata; ClutterContent *image;
AsyncTextureLoadData *pending; AsyncTextureLoadData *pending;
gboolean had_pending; gboolean had_pending;
texdata = g_hash_table_lookup (cache->priv->keyed_cache, key); image = g_hash_table_lookup (cache->priv->keyed_cache, key);
if (texdata != NULL) if (image != NULL)
{ {
/* We had this cached already, just set the texture and we're done. */ /* We had this cached already, just set the texture and we're done. */
set_texture_cogl_texture (CLUTTER_TEXTURE (texture), texdata); set_content_from_image (actor, image);
return TRUE; return TRUE;
} }
@ -838,7 +841,7 @@ ensure_request (StTextureCache *cache,
*request = pending; *request = pending;
/* Regardless of whether there was a pending request, prepend our texture here. */ /* Regardless of whether there was a pending request, prepend our texture here. */
(*request)->textures = g_slist_prepend ((*request)->textures, g_object_ref (texture)); (*request)->actors = g_slist_prepend ((*request)->actors, g_object_ref (actor));
return had_pending; return had_pending;
} }
@ -866,7 +869,7 @@ st_texture_cache_load_gicon (StTextureCache *cache,
gint scale) gint scale)
{ {
AsyncTextureLoadData *request; AsyncTextureLoadData *request;
ClutterActor *texture; ClutterActor *actor;
char *gicon_string; char *gicon_string;
char *key; char *key;
GtkIconTheme *theme; GtkIconTheme *theme;
@ -925,10 +928,10 @@ st_texture_cache_load_gicon (StTextureCache *cache,
} }
g_free (gicon_string); g_free (gicon_string);
texture = (ClutterActor *) create_default_texture (); actor = create_invisible_actor ();
clutter_actor_set_size (texture, size * scale, size * scale); clutter_actor_set_size (actor, size * scale, size * scale);
if (ensure_request (cache, key, policy, &request, texture)) if (ensure_request (cache, key, policy, &request, actor))
{ {
/* If there's an outstanding request, we've just added ourselves to it */ /* If there's an outstanding request, we've just added ourselves to it */
g_object_unref (info); g_object_unref (info);
@ -950,27 +953,24 @@ st_texture_cache_load_gicon (StTextureCache *cache,
load_texture_async (cache, request); load_texture_async (cache, request);
} }
return CLUTTER_ACTOR (texture); return actor;
} }
static ClutterActor * static ClutterActor *
load_from_pixbuf (GdkPixbuf *pixbuf) load_from_pixbuf (GdkPixbuf *pixbuf)
{ {
ClutterTexture *texture; g_autoptr(ClutterContent) image = NULL;
CoglTexture *texdata; ClutterActor *actor;
int width = gdk_pixbuf_get_width (pixbuf); int width = gdk_pixbuf_get_width (pixbuf);
int height = gdk_pixbuf_get_height (pixbuf); int height = gdk_pixbuf_get_height (pixbuf);
texture = create_default_texture (); image = pixbuf_to_clutter_image (pixbuf);
clutter_actor_set_size (CLUTTER_ACTOR (texture), width, height); actor = clutter_actor_new ();
clutter_actor_set_size (actor, width, height);
clutter_actor_set_content (actor, image);
texdata = pixbuf_to_cogl_texture (pixbuf); return actor;
set_texture_cogl_texture (texture, texdata);
cogl_object_unref (texdata);
return CLUTTER_ACTOR (texture);
} }
static void static void
@ -1218,7 +1218,7 @@ st_texture_cache_load_file_async (StTextureCache *cache,
int available_height, int available_height,
int scale) int scale)
{ {
ClutterActor *texture; ClutterActor *actor;
AsyncTextureLoadData *request; AsyncTextureLoadData *request;
StTextureCachePolicy policy; StTextureCachePolicy policy;
gchar *key; gchar *key;
@ -1227,9 +1227,9 @@ st_texture_cache_load_file_async (StTextureCache *cache,
policy = ST_TEXTURE_CACHE_POLICY_NONE; /* XXX */ policy = ST_TEXTURE_CACHE_POLICY_NONE; /* XXX */
texture = (ClutterActor *) create_default_texture (); actor = create_invisible_actor ();
if (ensure_request (cache, key, policy, &request, texture)) if (ensure_request (cache, key, policy, &request, actor))
{ {
/* If there's an outstanding request, we've just added ourselves to it */ /* If there's an outstanding request, we've just added ourselves to it */
g_free (key); g_free (key);
@ -1252,7 +1252,7 @@ st_texture_cache_load_file_async (StTextureCache *cache,
ensure_monitor_for_file (cache, file); ensure_monitor_for_file (cache, file);
return CLUTTER_ACTOR (texture); return actor;
} }
static CoglTexture * static CoglTexture *
@ -1264,34 +1264,37 @@ st_texture_cache_load_file_sync_to_cogl_texture (StTextureCache *cache,
int scale, int scale,
GError **error) GError **error)
{ {
ClutterContent *image;
CoglTexture *texdata; CoglTexture *texdata;
GdkPixbuf *pixbuf; GdkPixbuf *pixbuf;
char *key; char *key;
key = g_strdup_printf (CACHE_PREFIX_FILE "%u", g_file_hash (file)); key = g_strdup_printf (CACHE_PREFIX_FILE "%u", g_file_hash (file));
texdata = g_hash_table_lookup (cache->priv->keyed_cache, key); texdata = NULL;
image = g_hash_table_lookup (cache->priv->keyed_cache, key);
if (texdata == NULL) if (image == NULL)
{ {
pixbuf = impl_load_pixbuf_file (file, available_width, available_height, scale, error); pixbuf = impl_load_pixbuf_file (file, available_width, available_height, scale, error);
if (!pixbuf) if (!pixbuf)
goto out; goto out;
texdata = pixbuf_to_cogl_texture (pixbuf); image = pixbuf_to_clutter_image (pixbuf);
g_object_unref (pixbuf); g_object_unref (pixbuf);
if (!texdata) if (!image)
goto out; goto out;
if (policy == ST_TEXTURE_CACHE_POLICY_FOREVER) if (policy == ST_TEXTURE_CACHE_POLICY_FOREVER)
{ g_hash_table_insert (cache->priv->keyed_cache, g_strdup (key), image);
cogl_object_ref (texdata);
g_hash_table_insert (cache->priv->keyed_cache, g_strdup (key), texdata);
}
} }
else
cogl_object_ref (texdata); /* Because the texture is loaded synchronously, we won't call
* clutter_image_set_data(), so it's safe to use the texture
* of ClutterImage here. */
texdata = clutter_image_get_texture (CLUTTER_IMAGE (image));
cogl_object_ref (texdata);
ensure_monitor_for_file (cache, file); ensure_monitor_for_file (cache, file);