From 498ce9d5488a9095c99e0cc7ed3e1957032ba81f Mon Sep 17 00:00:00 2001 From: "Aaron J. Seigo" Date: Tue, 29 May 2007 07:56:21 +0000 Subject: [PATCH] attempt to share renderers. this should make quite a difference, one would hope. needs more testing. svn path=/trunk/KDE/kdebase/workspace/plasma/lib/; revision=669390 --- svg.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/svg.cpp b/svg.cpp index d796f5d32..5f410e1fc 100644 --- a/svg.cpp +++ b/svg.cpp @@ -22,14 +22,40 @@ #include #include #include +#include #include +#include #include + #include "theme.h" namespace Plasma { +class SharedSvgRenderer : public KSvgRenderer, public QSharedData +{ + public: + typedef KSharedPtr Ptr; + + SharedSvgRenderer(QObject *parent = 0) + : KSvgRenderer(parent) + {} + + SharedSvgRenderer(const QString &filename, QObject *parent = 0) + : KSvgRenderer(filename, parent) + {} + + SharedSvgRenderer(const QByteArray &contents, QObject *parent = 0) + : KSvgRenderer(contents, parent) + {} + + ~SharedSvgRenderer() + { + kDebug() << "leaving this world for a better one." << endl; + } +}; + class Svg::Private { public: @@ -47,7 +73,12 @@ class Svg::Private ~Private() { - delete renderer; + if (renderer.count() == 2) { + // this and the cache reference it; and boy is this not thread safe ;) + renderers.erase(renderers.find(themePath)); + } + + renderer = 0; } void removeFromCache() @@ -112,7 +143,15 @@ class Svg::Private //TODO: connect the renderer's repaintNeeded to the Plasma::Svg signal // take into consideration for cache, e.g. don't cache if svg is animated - renderer = new KSvgRenderer(path); + QHash::const_iterator it = renderers.find(path); + + if (it != renderers.end()) { + kDebug() << "gots us an existing one!" << endl; + renderer = it.value(); + } else { + renderer = new SharedSvgRenderer(path); + renderers[path] = renderer; + } } QSize elementSize( const QString& elementId ) @@ -127,8 +166,8 @@ class Svg::Private return elementSize.toSize(); } - //TODO: share renderers between Svg objects with identical themePath - KSvgRenderer* renderer; + static QHash renderers; + SharedSvgRenderer::Ptr renderer; QString themePath; QString path; QString id; @@ -136,6 +175,8 @@ class Svg::Private bool themed; }; +QHash Svg::Private:: renderers; + Svg::Svg( const QString& imagePath, QObject* parent ) : QObject( parent ), d( new Private( imagePath ) ) @@ -199,7 +240,7 @@ void Svg::themeChanged() { d->removeFromCache(); d->path.clear(); - delete d->renderer; + //delete d->renderer; we're a KSharedPtr d->renderer = 0; emit repaintNeeded(); }