Skip to content

Commit

Permalink
Remove unnecessary copying from paintCroppedPageOnPainter
Browse files Browse the repository at this point in the history
PagePainter::paintCroppedPageOnPainter had a few calls to
QPixmap::setDevicePixelRatio on pixmaps it does not own, which detaches
the pixmap, making a deep copy. It turns out these were all unnecessary.
It also copied scaled before drawing them onto the painter.

I've tested all the changes except for the annotation stamps, which I'm
just assuming works like the others.

Also ran clang-format.
  • Loading branch information
maximumsomething authored and osander1 committed Nov 30, 2022
1 parent 95846ea commit 521ea66
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions gui/pagepainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ void PagePainter::paintCroppedPageOnPainter(QPainter *destPainter,

if (p != nullptr) {
pixmap = *p;
pixmap.setDevicePixelRatio(dpr);
}

/** 1B - IF NO PIXMAP, DRAW EMPTY PAGE **/
Expand Down Expand Up @@ -255,7 +254,6 @@ void PagePainter::paintCroppedPageOnPainter(QPainter *destPainter,

if (!limitsInTile.isEmpty()) {
QPixmap *tilePixmap = tile.pixmap();
tilePixmap->setDevicePixelRatio(dpr);

if (tilePixmap->width() == dTileRect.width() && tilePixmap->height() == dTileRect.height()) {
destPainter->drawPixmap(limitsInTile.topLeft(), *tilePixmap, dLimitsInTile.translated(-dTileRect.topLeft()));
Expand All @@ -266,9 +264,7 @@ void PagePainter::paintCroppedPageOnPainter(QPainter *destPainter,
tIt++;
}
} else {
QPixmap scaledCroppedPixmap = pixmap.scaled(dScaledWidth, dScaledHeight).copy(dLimitsInPixmap);
scaledCroppedPixmap.setDevicePixelRatio(dpr);
destPainter->drawPixmap(limits.topLeft(), scaledCroppedPixmap, QRectF(0, 0, dLimits.width(), dLimits.height()));
destPainter->drawPixmap(limits, pixmap.scaled(dScaledWidth, dScaledHeight), dLimitsInPixmap);
}

// 4A.2. active painter is the one passed to this method
Expand All @@ -295,7 +291,6 @@ void PagePainter::paintCroppedPageOnPainter(QPainter *destPainter,

if (!limitsInTile.isEmpty()) {
QPixmap *tilePixmap = tile.pixmap();
tilePixmap->setDevicePixelRatio(dpr);

if (tilePixmap->width() == dTileRect.width() && tilePixmap->height() == dTileRect.height()) {
p.drawPixmap(limitsInTile.translated(-limits.topLeft()).topLeft(), *tilePixmap, dLimitsInTile.translated(-dTileRect.topLeft()));
Expand All @@ -310,9 +305,8 @@ void PagePainter::paintCroppedPageOnPainter(QPainter *destPainter,
}
} else {
// 4B.1. draw the page pixmap: normal or scaled
QPixmap scaledCroppedPixmap = pixmap.scaled(dScaledWidth, dScaledHeight).copy(dLimitsInPixmap);
scaledCroppedPixmap.setDevicePixelRatio(dpr);
p.drawPixmap(0, 0, scaledCroppedPixmap);

p.drawPixmap(QRectF(0, 0, limits.width(), limits.height()), pixmap.scaled(dScaledWidth, dScaledHeight), dLimitsInPixmap);
}

p.end();
Expand Down Expand Up @@ -599,13 +593,12 @@ void PagePainter::paintCroppedPageOnPainter(QPainter *destPainter,
QPixmap pixmap = Okular::AnnotationUtils::loadStamp(stamp->stampIconName(), qMax(annotBoundary.width(), annotBoundary.height()) * dpr);
if (!pixmap.isNull()) // should never happen but can happen on huge sizes
{
QPixmap scaledCroppedPixmap = pixmap.scaled(annotBoundary.width() * dpr, annotBoundary.height() * dpr).copy(dInnerRect.toAlignedRect());
scaledCroppedPixmap.setDevicePixelRatio(dpr);

// Draw pixmap with opacity:
mixedPainter->save();
mixedPainter->setOpacity(mixedPainter->opacity() * opacity / 255.0);
mixedPainter->drawPixmap(annotRect.topLeft(), scaledCroppedPixmap);

mixedPainter->drawPixmap(annotRect.topLeft(), pixmap.scaled(annotBoundary.width() * dpr, annotBoundary.height() * dpr), dInnerRect.toAlignedRect());

mixedPainter->restore();
}
}
Expand Down

0 comments on commit 521ea66

Please sign in to comment.