Skip to content

Commit

Permalink
make Skia image cache size configurable
Browse files Browse the repository at this point in the history
As asked for in tdf#136244 comment #11. The default fits 4x 2000px
32bpp images, which is 64MiB, which is not that little, but then
4x 2000px is not that much either. So, yes, configurable ...

A good further improvement would be to make the cache grow more
if the memory is available and reduce the size on memory pressure
(https://lists.freedesktop.org/archives/libreoffice/2020-December/086404.html).

Change-Id: Ifa05025ab34630e456465ac8a96950463fd18b60
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/107468
Tested-by: Jenkins
Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
  • Loading branch information
Luboš Luňák committed Dec 10, 2020
1 parent 4f1a5c4 commit fae487b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
14 changes: 14 additions & 0 deletions officecfg/registry/schema/org/openoffice/Office/Common.xcs
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,20 @@
<value>10</value>
</prop>
</group>
<group oor:name="Skia">
<info>
<desc>Specifies a group of cache options related to Skia-based drawing.</desc>
</info>
<prop oor:name="ImageCacheSize" oor:type="xs:long" oor:nillable="false">
<info>
<desc>Specifies the maximum cache size in bytes for all images used by Skia-based
drawing code. Larger size may improve drawing performance when using
many large images in software rendering mode.</desc>
<label>Image Cache Size</label>
</info>
<value>64000000</value>
</prop>
</group>
</group>
<group oor:name="Path">
<!--OldLocation: soffice.ini -->
Expand Down
2 changes: 1 addition & 1 deletion vcl/inc/skia/utils.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ VCL_DLLPUBLIC void
void addCachedImage(const OString& key, sk_sp<SkImage> image);
sk_sp<SkImage> findCachedImage(const OString& key);
void removeCachedImage(sk_sp<SkImage> image);
constexpr int MAX_CACHE_SIZE = 4 * 2000 * 2000 * 4; // 4x 2000px 32bpp images, 64MiB
tools::Long maxImageCacheSize();

// SkSurfaceProps to be used by all Skia surfaces.
VCL_DLLPUBLIC const SkSurfaceProps* surfaceProps();
Expand Down
17 changes: 12 additions & 5 deletions vcl/skia/SkiaHelper.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -485,15 +485,15 @@ struct ImageCacheItem
{
OString key;
sk_sp<SkImage> image;
int size; // cost of the item
tools::Long size; // cost of the item
};
} //namespace

// LRU cache, last item is the least recently used. Hopefully there won't be that many items
// to require a hash/map. Using o3tl::lru_cache would be simpler, but it doesn't support
// calculating cost of each item.
static std::list<ImageCacheItem>* imageCache = nullptr;
static int imageCacheSize = 0; // sum of all ImageCacheItem.size
static tools::Long imageCacheSize = 0; // sum of all ImageCacheItem.size

void addCachedImage(const OString& key, sk_sp<SkImage> image)
{
Expand All @@ -502,12 +502,13 @@ void addCachedImage(const OString& key, sk_sp<SkImage> image)
return;
if (imageCache == nullptr)
imageCache = new std::list<ImageCacheItem>;
int size = image->width() * image->height()
* SkColorTypeBytesPerPixel(image->imageInfo().colorType());
tools::Long size = static_cast<tools::Long>(image->width()) * image->height()
* SkColorTypeBytesPerPixel(image->imageInfo().colorType());
imageCache->push_front({ key, image, size });
imageCacheSize += size;
SAL_INFO("vcl.skia.trace", "addcachedimage " << image << " :" << size << "/" << imageCacheSize);
while (imageCacheSize > MAX_CACHE_SIZE)
const tools::Long maxSize = maxImageCacheSize();
while (imageCacheSize > maxSize)
{
assert(!imageCache->empty());
imageCacheSize -= imageCache->back().size;
Expand Down Expand Up @@ -554,6 +555,12 @@ void removeCachedImage(sk_sp<SkImage> image)
}
}

tools::Long maxImageCacheSize()
{
// Defaults to 4x 2000px 32bpp images, 64MiB.
return officecfg::Office::Common::Cache::Skia::ImageCacheSize::get();
}

void cleanup()
{
delete sharedGrDirectContext;
Expand Down
2 changes: 1 addition & 1 deletion vcl/skia/gdiimpl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1574,7 +1574,7 @@ sk_sp<SkImage> SkiaSalGraphicsImpl::mergeCacheBitmaps(const SkiaSalBitmap& bitma
}
}
// Do not cache the result if it would take most of the cache and thus get evicted soon.
if (targetSize.Width() * targetSize.Height() * 4 > SkiaHelper::MAX_CACHE_SIZE * 0.7)
if (targetSize.Width() * targetSize.Height() * 4 > SkiaHelper::maxImageCacheSize() * 0.7)
return image;
OString key;
OStringBuffer keyBuf;
Expand Down

0 comments on commit fae487b

Please sign in to comment.