From 6783df71a7936ec12686c2f731ace1f1291cc660 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 28 Sep 2021 18:39:53 +0100 Subject: [PATCH] Cache the last returned bitmap in wxBitmapBundleImplSVG This seems to be enough to avoid inefficiencies and doesn't consume as many resources as caching all bitmaps ever generated. --- src/generic/bmpsvg.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/generic/bmpsvg.cpp b/src/generic/bmpsvg.cpp index 7f5198b082..912817a37b 100644 --- a/src/generic/bmpsvg.cpp +++ b/src/generic/bmpsvg.cpp @@ -80,11 +80,22 @@ public: virtual wxBitmap GetBitmap(const wxSize size) wxOVERRIDE; private: + wxBitmap DoRasterize(const wxSize size); + NSVGimage* const m_svgImage; NSVGrasterizer* const m_svgRasterizer; const wxSize m_sizeDef; + // Cache the last used bitmap (may be invalid if not used yet). + // + // Note that we cache only the last bitmap and not all the bitmaps ever + // requested from GetBitmap() for the different sizes because there would + // be no way to clear such cache and its growth could be unbounded, + // resulting in too many bitmap objects being used in an application using + // SVG for all of its icons. + wxBitmap m_cachedBitmap; + wxDECLARE_NO_COPY_CLASS(wxBitmapBundleImplSVG); }; @@ -101,8 +112,16 @@ wxSize wxBitmapBundleImplSVG::GetDefaultSize() const wxBitmap wxBitmapBundleImplSVG::GetBitmap(const wxSize size) { - // TODO: Cache. + if ( !m_cachedBitmap.IsOk() || m_cachedBitmap.GetSize() != size ) + { + m_cachedBitmap = DoRasterize(size); + } + return m_cachedBitmap; +} + +wxBitmap wxBitmapBundleImplSVG::DoRasterize(const wxSize size) +{ wxVector buffer(size.x*size.y*4); nsvgRasterize (