Guidance on Optimizing Skia Image Rendering

68 views
Skip to first unread message

L2000

unread,
Jul 24, 2025, 6:57:02 AMJul 24
to skia-discuss
Hi,
I’m currently working on rendering images using Skia and have observed a performance-quality trade-off depending on the SkSamplingOptions used with canvas->drawImageRect().
I construct the image using:
image = SkImages::RasterFromPixmap(pixmap, nullptr, nullptr);
And draw it like this:
canvas->drawImageRect(image, imageRect, SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear));
While this produces high-quality results — especially for downscaled images — it introduces noticeable performance overhead.
On the other hand, drawing the image using the default SkSamplingOptions():
canvas->drawImageRect(image, imageRect, SkSamplingOptions());
is significantly faster, but leads to very poor image quality, especially when downscaling.
What I Tried :
To balance performance and quality, I’ve implemented conditional sampling based on the image size and destination imageRect:
auto samplingOptions = SkSamplingOptions();
if (imgWidth > imageRect.width() && imgHeight > imageRect.height()) {
// Downscaling
samplingOptions = SkSamplingOptions(SkFilterMode::kLinear, SkMipmapMode::kLinear);
} else if (imgWidth < imageRect.width() || imgHeight < imageRect.height()) {
// Upscaling
samplingOptions = SkSamplingOptions(SkFilterMode::kLinear);
}
// No sampling for exact size match
canvas->drawImageRect(image, imageRect, samplingOptions);

This helps improve performance slightly while preserving decent quality in most scenarios.
Are there any better practices or optimizations you’d recommend for:
Improving rendering speed while maintaining image quality?
Choosing the most appropriate SkSamplingOptions dynamically?
Possibly using mipmaps more efficiently (or alternatives)?
Any insights would be greatly appreciated.

Thanks

Michael Ludwig

unread,
Jul 28, 2025, 11:10:38 AMJul 28
to skia-discuss
Those are reasonable heuristics to use for performance. Cubic filtering can give higher quality than linear filtering when upscaling, but it is going to be more expensive.  You could experiment with SkMipmapMode::kNearest for downscaling, as I believe that will be faster on the CPU backend while still producing better results than linear or nearest for downscaling.  But realistically, CPU image filtering performance is quite sensitive to the sampling options you use.  GPU rendering will avoid most of that cost.
Reply all
Reply to author
Forward
0 new messages