texture-cache: preserve aspect ratio of loaded pixbufs

st_texture_cache_load_file_async takes optional width and height hints
to specify how much space on-screen the loaded image will take up. One
or both of the width and height can be negative.

gdk-pixbuf uses this information to scale down the image to an
appropriate size, when necessary, to save memory and prevent
aliasing.

gdk-pixbuf never uses this information to scale up the image, though;
that would just waste memory. Instead, the width and height hints are
used to set the geometry of the clutter content, so the texture may
get upscaled on-screen.

If both the passed in width and height are negative, it means to assume
the image will fit at native size, and gdk-pixbuf shouldn't downscale
the image.

If just one dimension is negative, it means the image should be
downscaled by gdk-pixbuf proportionally to fit the given (positive)
dimension.

If neither dimension is negative, it means the image should be
downscaled by gdk-pixbuf proportionally to fit whichever dimension keeps
the whole image within the bounds of its clutter content.

In any case, if the passed in width and height are bigger than the
dimensions of the loaded image, then those passed in values won't be used
by gdk-pixbuf, since it has no downscaling to do.

The problem is, the code that converts a pixbuf to a clutter content
fails to handle the case where the passed in dimensions aren't used by
gdk-pixbuf.  It assumes at least one of the pixbuf dimensions matches the
dimensions passed to st_texture_cache_load_file_async.  That won't be the
case if the pixbuf code had no downscaling to do.

This commit fixes that problem by deriving the unknown clutter content
dimension from the known clutter content dimension and the aspect ratio
of the image.
This commit is contained in:
Ray Strode 2019-04-30 14:58:26 -04:00
parent 50b7739076
commit f581ade9a3

View File

@ -495,16 +495,34 @@ pixbuf_to_st_content_image (GdkPixbuf *pixbuf,
{ {
ClutterContent *image; ClutterContent *image;
g_autoptr(GError) error = NULL; g_autoptr(GError) error = NULL;
int pixbuf_width, pixbuf_height;
if (width < 0) pixbuf_width = gdk_pixbuf_get_width (pixbuf);
width = ceilf (gdk_pixbuf_get_width (pixbuf) / resource_scale); pixbuf_height = gdk_pixbuf_get_height (pixbuf);
else
width *= paint_scale;
if (width >= 0 || height >= 0)
{
if (height < 0) if (height < 0)
height = ceilf (gdk_pixbuf_get_height (pixbuf) / resource_scale); {
else float aspect_scale = (float) pixbuf_height / pixbuf_width;
height = ceilf (width * aspect_scale);
}
else if (width < 0)
{
float aspect_scale = (float) pixbuf_width / pixbuf_height;
width = ceilf (height * aspect_scale);
}
width *= paint_scale;
height *= paint_scale; height *= paint_scale;
}
else
{
width = ceilf (pixbuf_width / resource_scale);
height = ceilf (pixbuf_height / resource_scale);
}
image = st_image_content_new_with_preferred_size (width, height); image = st_image_content_new_with_preferred_size (width, height);
clutter_image_set_data (CLUTTER_IMAGE (image), clutter_image_set_data (CLUTTER_IMAGE (image),