Skip to content

Commit

Permalink
QPen/QBrush: de-inline compare helper functions
Browse files Browse the repository at this point in the history
These functions expose implementation details of their respective
class, so, seeing as the classes are pimpl'ed and their op==s are
out-of-line, too, these functions ought to be out-of-line, too.

De-inline them, by calling a private out-of-line helper function. This
way, they can remain unexported and "real" hidden friends (= with an
inline definition).

As drive-by:

- in QBrush/QColor: check the properties in the order of cheapness
  (style(), color(), transform()), and don't copy QTransform just to
  check isIdentity() (access the member directly).

- in QPen/QColor: avoid the QBrush copy (access the member
  directly). This also retroactively endorses the noexcept on this
  function.

- in QPen/PenStyle: amend the comment that says it's allocating with a
  `// ### optimize`

Amends f018686.

Found in API-review.

Pick-to: 6.9
Change-Id: Ibfd43b1f2200ef030d6739dad1bf026cc190606b
Reviewed-by: Tatiana Borisova <tatiana.borisova@qt.io>
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
  • Loading branch information
marcmutz committed Jan 30, 2025
1 parent c05982f commit d769ca4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
27 changes: 27 additions & 0 deletions src/gui/painting/qbrush.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,33 @@ bool QBrush::operator==(const QBrush &b) const
}
}

/*!
\internal
*/
bool QBrush::doCompareEqualColor(QColor rhs) const noexcept
{
return style() == Qt::SolidPattern && color() == rhs && d->transform.isIdentity();
}

/*!
\internal
*/
bool QBrush::doCompareEqualStyle(Qt::BrushStyle rhs) const noexcept
{
switch (rhs) {
case Qt::NoBrush:
case Qt::TexturePattern:
case Qt::LinearGradientPattern:
case Qt::RadialGradientPattern:
case Qt::ConicalGradientPattern:
// A brush constructed only from one of those styles will end up
// using NoBrush (see qbrush_check_type)
return style() == Qt::NoBrush;
default:
return style() == rhs && color() == QColor(0, 0, 0);
}
}

#ifndef QT_NO_DEBUG_STREAM
/*!
\internal
Expand Down
18 changes: 4 additions & 14 deletions src/gui/painting/qbrush.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,18 @@ class Q_GUI_EXPORT QBrush
friend class QPainter;
friend bool Q_GUI_EXPORT qHasPixmapTexture(const QBrush& brush);

bool doCompareEqualColor(QColor rhs) const noexcept;
friend bool comparesEqual(const QBrush &lhs, QColor rhs) noexcept
{
return lhs.color() == rhs && lhs.style() == Qt::SolidPattern
&& lhs.transform().isIdentity();
return lhs.doCompareEqualColor(rhs);
}
Q_DECLARE_EQUALITY_COMPARABLE(QBrush, QColor)
Q_DECLARE_EQUALITY_COMPARABLE(QBrush, Qt::GlobalColor)

bool doCompareEqualStyle(Qt::BrushStyle rhs) const noexcept;
friend bool comparesEqual(const QBrush &lhs, Qt::BrushStyle rhs) noexcept
{
switch (rhs) {
case Qt::NoBrush:
case Qt::TexturePattern:
case Qt::LinearGradientPattern:
case Qt::RadialGradientPattern:
case Qt::ConicalGradientPattern:
// A brush constructed only from one of those styles will end up
// using NoBrush (see qbrush_check_type)
return lhs.style() == Qt::NoBrush;
default:
return lhs.style() == rhs && lhs.color() == QColor(0, 0, 0);
}
return lhs.doCompareEqualStyle(rhs);
}
Q_DECLARE_EQUALITY_COMPARABLE(QBrush, Qt::BrushStyle)

Expand Down
17 changes: 17 additions & 0 deletions src/gui/painting/qpen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,23 @@ bool QPen::operator==(const QPen &p) const
&& p.d->cosmetic == d->cosmetic);
}

/*!
\internal
*/
bool QPen::doCompareEqualColor(QColor rhs) const noexcept
{
return d->brush == rhs && isSolidDefaultLine();
}

/*!
\internal
*/
bool QPen::doCompareEqualStyle(Qt::PenStyle rhs) const
{
if (rhs == Qt::NoPen)
return style() == Qt::NoPen;
return *this == QPen(rhs); // ### optimize (allocates)
}

/*!
\fn bool QPen::isDetached()
Expand Down
8 changes: 4 additions & 4 deletions src/gui/painting/qpen.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ class Q_GUI_EXPORT QPen

bool isSolidDefaultLine() const noexcept;

bool doCompareEqualColor(QColor rhs) const noexcept;
friend bool comparesEqual(const QPen &lhs, QColor rhs) noexcept
{
return lhs.brush() == rhs && lhs.isSolidDefaultLine();
return lhs.doCompareEqualColor(rhs);
}
Q_DECLARE_EQUALITY_COMPARABLE(QPen, QColor)

bool doCompareEqualStyle(Qt::PenStyle rhs) const;
friend bool comparesEqual(const QPen &lhs, Qt::PenStyle rhs)
{
if (rhs == Qt::NoPen)
return lhs.style() == Qt::NoPen;
return lhs == QPen(rhs); // allocates
return lhs.doCompareEqualStyle(rhs);
}
Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QPen, Qt::PenStyle)

Expand Down

0 comments on commit d769ca4

Please sign in to comment.