summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Nienhüser <nienhueser@kde.org>2016-10-22 12:47:24 (GMT)
committerFriedrich W. H. Kossebau <kossebau@kde.org>2016-11-05 16:47:11 (GMT)
commit5213c02e8779309aee1d7575d751c010d283ff09 (patch)
treec4d255f4bbc74c1c07d8673bf7de9c837cecfbda
parentf2ef75f7dccedeb5b519273b0760b0850c553c9c (diff)
Use a pixmap cache for texture caching
- Reduces memory consumption drastically, e.g. from 4.6 GB RAM down to 0.25 GB on tile level 11 around Karlsruhe - Textures have to be generated only once for each texture anymore, not once for each graphics item - Use a QPixmap in the cache instead of an QImage
-rw-r--r--src/lib/marble/geodata/graphicsitem/AbstractGeoPolygonGraphicsItem.cpp43
-rw-r--r--src/lib/marble/geodata/graphicsitem/AbstractGeoPolygonGraphicsItem.h7
2 files changed, 29 insertions, 21 deletions
diff --git a/src/lib/marble/geodata/graphicsitem/AbstractGeoPolygonGraphicsItem.cpp b/src/lib/marble/geodata/graphicsitem/AbstractGeoPolygonGraphicsItem.cpp
index b78cc96..19dd1a3 100644
--- a/src/lib/marble/geodata/graphicsitem/AbstractGeoPolygonGraphicsItem.cpp
+++ b/src/lib/marble/geodata/graphicsitem/AbstractGeoPolygonGraphicsItem.cpp
@@ -27,6 +27,8 @@
namespace Marble
{
+QPixmapCache AbstractGeoPolygonGraphicsItem::m_textureCache = QPixmapCache();
+
AbstractGeoPolygonGraphicsItem::AbstractGeoPolygonGraphicsItem(const GeoDataPlacemark *placemark, const GeoDataPolygon *polygon) :
GeoGraphicsItem(placemark),
m_polygon(polygon),
@@ -103,27 +105,11 @@ QPen AbstractGeoPolygonGraphicsItem::configurePainter(GeoPainter *painter, const
const QColor paintedColor = polyStyle.paintedColor();
if (painter->brush().color() != paintedColor ||
painter->brush().style() != polyStyle.brushStyle()) {
- const QImage textureImage = polyStyle.textureImage();
- if (!textureImage.isNull()) {
+ if (!polyStyle.texturePath().isEmpty() || !polyStyle.textureImage().isNull()) {
GeoDataCoordinates coords = latLonAltBox().center();
qreal x, y;
viewport->screenCoordinates(coords, x, y);
- const QString texturePath = polyStyle.texturePath();
- if (m_cachedTexturePath != texturePath ||
- m_cachedTextureColor != paintedColor) {
- if (textureImage.hasAlphaChannel()) {
- m_cachedTexture = QImage ( textureImage.size(), QImage::Format_ARGB32_Premultiplied );
- m_cachedTexture.fill(paintedColor);
- QPainter imagePainter(&m_cachedTexture );
- imagePainter.drawImage(0, 0, textureImage);
- }
- else {
- m_cachedTexture = textureImage;
- }
- m_cachedTexturePath = texturePath;
- m_cachedTextureColor = paintedColor;
- }
- QBrush brush(m_cachedTexture);
+ QBrush brush(texture(polyStyle.texturePath(), polyStyle.textureImage(), paintedColor));
painter->setBrush(brush);
painter->setBrushOrigin(QPoint(x,y));
}
@@ -151,4 +137,25 @@ int AbstractGeoPolygonGraphicsItem::extractElevation(const GeoDataPlacemark &pla
return elevation;
}
+QPixmap AbstractGeoPolygonGraphicsItem::texture(const QString &texturePath, const QImage &textureImage, const QColor &color)
+{
+ QString const key = QString("%1/%2").arg(color.rgba()).arg(texturePath);
+ QPixmap texture;
+ if (!m_textureCache.find(key, texture)) {
+ if (textureImage.hasAlphaChannel()) {
+ QImage image = QImage (textureImage.size(), QImage::Format_ARGB32_Premultiplied);
+ image.fill(color);
+ QPainter imagePainter(&image);
+ imagePainter.drawImage(0, 0, textureImage);
+ imagePainter.end();
+ texture = QPixmap::fromImage(image);
+ }
+ else {
+ texture = QPixmap::fromImage(textureImage);
+ }
+ m_textureCache.insert(key, texture);
+ }
+ return texture;
+}
+
}
diff --git a/src/lib/marble/geodata/graphicsitem/AbstractGeoPolygonGraphicsItem.h b/src/lib/marble/geodata/graphicsitem/AbstractGeoPolygonGraphicsItem.h
index b0c02e9..0e008ca 100644
--- a/src/lib/marble/geodata/graphicsitem/AbstractGeoPolygonGraphicsItem.h
+++ b/src/lib/marble/geodata/graphicsitem/AbstractGeoPolygonGraphicsItem.h
@@ -16,6 +16,7 @@
#include <QImage>
#include <QColor>
+#include <QPixmapCache>
namespace Marble
{
@@ -44,11 +45,11 @@ protected:
static int extractElevation(const GeoDataPlacemark &placemark);
private:
+ QPixmap texture(const QString &path, const QImage &textureImage, const QColor &color);
+
const GeoDataPolygon *const m_polygon;
const GeoDataLinearRing *const m_ring;
- QString m_cachedTexturePath;
- QColor m_cachedTextureColor;
- QImage m_cachedTexture;
+ static QPixmapCache m_textureCache;
};
}