QPaintEngineRaster: port from QSharedPointer to std::shared_ptr

Compared to std::shared_ptr, QSharedPointer requires 2x the atomic
operations per copy, and does not support QSharedPointer<void>.

Port to std::shared_ptr, and drop the Pinnable kludge.

Add an optimistic std::move() when we insert into QMultiHash.

Pick-to: 6.5
Change-Id: I2ab004b7e8fa36d9e777cd787ffded4076d2880f
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
This commit is contained in:
Marc Mutz 2022-12-21 15:57:59 +01:00
parent 7b9f4aa0fc
commit 5ba2590388
2 changed files with 8 additions and 12 deletions

View File

@ -29,7 +29,7 @@
#include "private/qrasterdefs_p.h" #include "private/qrasterdefs_p.h"
#include <private/qsimd_p.h> #include <private/qsimd_p.h>
#include <QtCore/qsharedpointer.h> #include <memory>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
@ -330,11 +330,7 @@ struct QSpanData
QGradientData gradient; QGradientData gradient;
QTextureData texture; QTextureData texture;
}; };
class Pinnable { std::shared_ptr<const void> cachedGradient;
protected:
~Pinnable() {}
}; // QSharedPointer<const void> is not supported
QSharedPointer<const Pinnable> cachedGradient;
void init(QRasterBuffer *rb, const QRasterPaintEngine *pe); void init(QRasterBuffer *rb, const QRasterPaintEngine *pe);

View File

@ -4206,7 +4206,7 @@ static void qt_span_clip(int count, const QT_FT_Span *spans, void *userData)
class QGradientCache class QGradientCache
{ {
public: public:
struct CacheInfo : QSpanData::Pinnable struct CacheInfo
{ {
inline CacheInfo(QGradientStops s, int op, QGradient::InterpolationMode mode) : inline CacheInfo(QGradientStops s, int op, QGradient::InterpolationMode mode) :
stops(std::move(s)), opacity(op), interpolationMode(mode) {} stops(std::move(s)), opacity(op), interpolationMode(mode) {}
@ -4217,9 +4217,9 @@ public:
QGradient::InterpolationMode interpolationMode; QGradient::InterpolationMode interpolationMode;
}; };
typedef QMultiHash<quint64, QSharedPointer<const CacheInfo>> QGradientColorTableHash; using QGradientColorTableHash = QMultiHash<quint64, std::shared_ptr<const CacheInfo>>;
inline QSharedPointer<const CacheInfo> getBuffer(const QGradient &gradient, int opacity) { std::shared_ptr<const CacheInfo> getBuffer(const QGradient &gradient, int opacity) {
quint64 hash_val = 0; quint64 hash_val = 0;
const QGradientStops stops = gradient.stops(); const QGradientStops stops = gradient.stops();
@ -4249,16 +4249,16 @@ protected:
inline void generateGradientColorTable(const QGradient& g, inline void generateGradientColorTable(const QGradient& g,
QRgba64 *colorTable, QRgba64 *colorTable,
int size, int opacity) const; int size, int opacity) const;
QSharedPointer<const CacheInfo> addCacheElement(quint64 hash_val, const QGradient &gradient, int opacity) { std::shared_ptr<const CacheInfo> addCacheElement(quint64 hash_val, const QGradient &gradient, int opacity) {
if (cache.size() == maxCacheSize()) { if (cache.size() == maxCacheSize()) {
// may remove more than 1, but OK // may remove more than 1, but OK
cache.erase(std::next(cache.begin(), QRandomGenerator::global()->bounded(maxCacheSize()))); cache.erase(std::next(cache.begin(), QRandomGenerator::global()->bounded(maxCacheSize())));
} }
auto cache_entry = QSharedPointer<CacheInfo>::create(gradient.stops(), opacity, gradient.interpolationMode()); auto cache_entry = std::make_shared<CacheInfo>(gradient.stops(), opacity, gradient.interpolationMode());
generateGradientColorTable(gradient, cache_entry->buffer64, paletteSize(), opacity); generateGradientColorTable(gradient, cache_entry->buffer64, paletteSize(), opacity);
for (int i = 0; i < GRADIENT_STOPTABLE_SIZE; ++i) for (int i = 0; i < GRADIENT_STOPTABLE_SIZE; ++i)
cache_entry->buffer32[i] = cache_entry->buffer64[i].toArgb32(); cache_entry->buffer32[i] = cache_entry->buffer64[i].toArgb32();
return cache.insert(hash_val, cache_entry).value(); return cache.insert(hash_val, std::move(cache_entry)).value();
} }
QGradientColorTableHash cache; QGradientColorTableHash cache;