Skip to content

Commit 7d33eb4

Browse files
committed
fix: Reduce composition rendering time and playback cpu load by about 50% 💨
1 parent 743df33 commit 7d33eb4

1 file changed

Lines changed: 13 additions & 10 deletions

File tree

src/Glyph.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,19 @@ void Glyph::renderColored(QPainter* painter, const QColor& color) {
3434
// Render the svg to a transparent image and then color it
3535
// We don't know if the painter passed in draws on a transparent surface so we need to take this detour
3636

37-
this->svgImage.fill(Qt::GlobalColor::transparent); // Make sure the image is transparent or the coloring won't work properly
38-
39-
// Draw the svg onto the image
40-
QRectF savedAlignedBounds{this->scaledAlignedBounds};
41-
this->scaledAlignedBounds = this->svgImage.rect().toRectF();
42-
QPainter svgPainter(&this->svgImage);
43-
MySvgRenderer::render(&svgPainter);
44-
this->scaledAlignedBounds = savedAlignedBounds;
37+
// Copy svgImage that already has the svg prerendered with a transparent background for coloring
38+
QImage copiedSvgImage = this->svgImage.copy();
39+
QPainter svgPainter(&copiedSvgImage);
4540

4641
// Color the svg with the requested color
4742
svgPainter.setCompositionMode(QPainter::CompositionMode::CompositionMode_SourceAtop);
48-
svgPainter.fillRect(this->svgImage.rect(), color);
43+
svgPainter.fillRect(copiedSvgImage.rect(), color);
4944

5045
// Stop painting on the image and draw the image to the original painter
5146
svgPainter.end();
5247
painter->save();
5348
painter->setRenderHints(QPainter::RenderHint::Antialiasing | QPainter::RenderHint::SmoothPixmapTransform);
54-
painter->drawImage(this->scaledAlignedBounds, svgImage);
49+
painter->drawImage(this->scaledAlignedBounds, copiedSvgImage);
5550
painter->restore();
5651
}
5752

@@ -63,4 +58,12 @@ void Glyph::calcBounds(const QRect& drawingArea, qreal scale) {
6358
// Also make sure that we have an valid image of at least size 1x1. We get a QPainter error spam otherwise
6459
// We double the scale to make the rendered SVG look good after rendering the image to the passed in painter
6560
this->svgImage = QImage(this->scaledAlignedBounds.size().toSize().expandedTo(QSize(1, 1)) * 2, QImage::Format::Format_ARGB32_Premultiplied);
61+
62+
// Pre render svg to the image to save on processing. Svg rendering is expensive...
63+
this->svgImage.fill(Qt::GlobalColor::transparent); // Make sure the image is transparent or the coloring won't work properly
64+
QRectF savedAlignedBounds{this->scaledAlignedBounds};
65+
this->scaledAlignedBounds = this->svgImage.rect().toRectF();
66+
QPainter svgPainter(&this->svgImage);
67+
MySvgRenderer::render(&svgPainter);
68+
this->scaledAlignedBounds = savedAlignedBounds;
6669
}

0 commit comments

Comments
 (0)