proper caching, and only caching when we need to

svn path=/trunk/KDE/kdelibs/; revision=1029074
This commit is contained in:
Aaron J. Seigo 2009-09-28 21:54:00 +00:00
parent 79a826e17c
commit 326a6a4b18
2 changed files with 84 additions and 67 deletions

View File

@ -389,18 +389,80 @@ void FrameSvgPrivate::generateBackground(FrameData *frame)
return; return;
} }
QSizeF size = frameSize(frame); const QString id = cacheId(frame, prefix);
const QString id = QString::fromLatin1("%5_%4_%3_%2_%1_").
arg(frame->enabledBorders).arg(size.width()).arg(size.height()).arg(prefix).arg(q->imagePath());
Theme *theme = Theme::defaultTheme(); Theme *theme = Theme::defaultTheme();
bool frameCached = !frame->cachedBackground.isNull();
bool overlayCached = false;
const bool overlayAvailable = !prefix.startsWith("mask-") && q->hasElement(prefix + "overlay");
QPixmap overlay;
if (q->isUsingRenderingCache()) { if (q->isUsingRenderingCache()) {
if (theme->findInCache(id, frame->cachedBackground) && !frame->cachedBackground.isNull()) { frameCached = theme->findInCache(id, frame->cachedBackground) && !frame->cachedBackground.isNull();
return;
if (overlayAvailable) {
overlayCached = theme->findInCache("overlay_" + id, overlay) && !overlay.isNull();
} }
} }
if (!frameCached) {
generateFrameBackground(frame);
}
//Overlays
QSize overlaySize;
if (overlayAvailable && !overlayCached) {
QPoint pos = QPoint(0, 0);
overlaySize = q->elementSize(prefix+"overlay");
//Random pos, stretched and tiled are mutually exclusive
if (q->hasElement(prefix + "hint-overlay-random-pos")) {
pos = overlayPos;
//Stretched or Tiled?
} else if (q->hasElement(prefix + "hint-overlay-stretch")) {
overlaySize = frameSize(frame).toSize();
} else {
if (q->hasElement(prefix + "hint-overlay-tile-horizontal")) {
overlaySize.setWidth(frameSize(frame).width());
}
if (q->hasElement(prefix + "hint-overlay-tile-vertical")) {
overlaySize.setHeight(frameSize(frame).height());
}
}
overlay = q->alphaMask();
QPainter overlayPainter(&overlay);
overlayPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
//Tiling?
if (q->hasElement(prefix+"hint-overlay-tile-horizontal") ||
q->hasElement(prefix+"hint-overlay-tile-vertical")) {
QSize s = q->size();
q->resize(q->elementSize(prefix+"overlay"));
overlayPainter.drawTiledPixmap(QRect(QPoint(0,0), overlaySize), q->pixmap(prefix+"overlay"));
q->resize(s);
} else {
q->paint(&overlayPainter, QRect(overlayPos, overlaySize), prefix+"overlay");
}
overlayPainter.end();
}
if (!frameCached) {
cacheFrame(prefix, frame->cachedBackground, overlayCached ? overlay : QPixmap());
}
if (!overlay.isNull()) {
QPainter p(&frame->cachedBackground);
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
p.drawPixmap(overlayPos, overlay, QRect(overlayPos, overlaySize));
}
}
void FrameSvgPrivate::generateFrameBackground(FrameData *frame)
{
//kDebug() << "generating background"; //kDebug() << "generating background";
const QSizeF size = frameSize(frame);
const int topWidth = q->elementSize(prefix + "top").width(); const int topWidth = q->elementSize(prefix + "top").width();
const int leftHeight = q->elementSize(prefix + "left").height(); const int leftHeight = q->elementSize(prefix + "left").height();
const int topOffset = 0; const int topOffset = 0;
@ -556,59 +618,17 @@ void FrameSvgPrivate::generateBackground(FrameData *frame)
} }
} }
//Overlays
QSize overlaySize;
QPixmap overlay;
bool overlayAlreadyCached = false;
if (!prefix.startsWith("mask-") && q->hasElement(prefix + "overlay") &&
!(overlayAlreadyCached = theme->findInCache("overlay_" + id, overlay)) &&
overlay.isNull()) {
QPoint pos = QPoint(0, 0);
overlaySize = q->elementSize(prefix+"overlay");
//Random pos, stretched and tiled are mutually exclusive
if (q->hasElement(prefix + "hint-overlay-random-pos")) {
pos = overlayPos;
//Stretched or Tiled?
} else if (q->hasElement(prefix + "hint-overlay-stretch")) {
overlaySize = frameSize(frame).toSize();
} else {
if (q->hasElement(prefix + "hint-overlay-tile-horizontal")) {
overlaySize.setWidth(frameSize(frame).width());
}
if (q->hasElement(prefix + "hint-overlay-tile-vertical")) {
overlaySize.setHeight(frameSize(frame).height());
}
} }
overlay = q->alphaMask(); QString FrameSvgPrivate::cacheId(FrameData *frame, const QString &prefixToSave) const
QPainter overlayPainter(&overlay); {
overlayPainter.setCompositionMode(QPainter::CompositionMode_SourceIn); const QSizeF size = frameSize(frame);
//Tiling? return QString::fromLatin1("%5_%4_%3_%2_%1_").arg(frame->enabledBorders)
if (q->hasElement(prefix+"hint-overlay-tile-horizontal") || .arg(size.width())
q->hasElement(prefix+"hint-overlay-tile-vertical")) { .arg(size.height())
.arg(prefixToSave)
QSize s = q->size(); .arg(q->imagePath());
q->resize(q->elementSize(prefix+"overlay"));
overlayPainter.drawTiledPixmap(QRect(QPoint(0,0), overlaySize), q->pixmap(prefix+"overlay"));
q->resize(s);
} else {
q->paint(&overlayPainter, QRect(overlayPos, overlaySize), prefix+"overlay");
} }
overlayPainter.end();
}
cacheFrame(prefix, frame->cachedBackground, overlay);
if (!overlay.isNull()) {
p.setCompositionMode(QPainter::CompositionMode_SourceOver);
p.drawPixmap(overlayPos, overlay, QRect(overlayPos, overlaySize));
}
}
void FrameSvgPrivate::cacheFrame(const QString &prefixToSave, const QPixmap &background, const QPixmap &overlay) void FrameSvgPrivate::cacheFrame(const QString &prefixToSave, const QPixmap &background, const QPixmap &overlay)
{ {
@ -623,9 +643,7 @@ void FrameSvgPrivate::cacheFrame(const QString &prefixToSave, const QPixmap &bac
return; return;
} }
const QSizeF size = frameSize(frame); const QString id = cacheId(frame, prefixToSave);
QString id = QString::fromLatin1("%7_%6_%5_%4_%3_%2_%1_").
arg(overlayPos.y()).arg(overlayPos.x()).arg(frame->enabledBorders).arg(size.width()).arg(size.height()).arg(prefixToSave).arg(q->imagePath());
//kDebug()<<"Saving to cache frame"<<id; //kDebug()<<"Saving to cache frame"<<id;
@ -633,14 +651,11 @@ void FrameSvgPrivate::cacheFrame(const QString &prefixToSave, const QPixmap &bac
if (!overlay.isNull()) { if (!overlay.isNull()) {
//insert overlay //insert overlay
id = QString::fromLatin1("overlay_%7_%6_%5_%4_%3_%2_%1_"). Theme::defaultTheme()->insertIntoCache("overlay_" + id, overlay, QString::number((qint64)q, 16) + prefixToSave + "overlay");
arg(overlayPos.y()).arg(overlayPos.x()).arg(frame->enabledBorders).arg(size.width()).arg(size.height()).arg(prefixToSave).arg(q->imagePath());
Theme::defaultTheme()->insertIntoCache(id, overlay, QString::number((qint64)q, 16) + prefixToSave + "overlay");
} }
} }
void FrameSvgPrivate::updateSizes() void FrameSvgPrivate::updateSizes() const
{ {
//kDebug() << "!!!!!!!!!!!!!!!!!!!!!! updating sizes" << prefix; //kDebug() << "!!!!!!!!!!!!!!!!!!!!!! updating sizes" << prefix;
FrameData *frame = frames[prefix]; FrameData *frame = frames[prefix];
@ -718,7 +733,7 @@ void FrameSvgPrivate::updateAndSignalSizes()
emit q->repaintNeeded(); emit q->repaintNeeded();
} }
QSizeF FrameSvgPrivate::frameSize(FrameData *frame) QSizeF FrameSvgPrivate::frameSize(FrameData *frame) const
{ {
if (!frame->frameSize.isValid()) { if (!frame->frameSize.isValid()) {
updateSizes(); updateSizes();

View File

@ -107,11 +107,13 @@ public:
} }
void generateBackground(FrameData *frame); void generateBackground(FrameData *frame);
void generateFrameBackground(FrameData *frame);
QString cacheId(FrameData *frame, const QString &prefixToUse) const;
void cacheFrame(const QString &prefixToSave, const QPixmap &background, const QPixmap &overlay); void cacheFrame(const QString &prefixToSave, const QPixmap &background, const QPixmap &overlay);
void updateSizes(); void updateSizes() const;
void updateNeeded(); void updateNeeded();
void updateAndSignalSizes(); void updateAndSignalSizes();
QSizeF frameSize(FrameData *frame); QSizeF frameSize(FrameData *frame) const;
Location location; Location location;
QString prefix; QString prefix;