Simplify QColorSpacePrivate initialization

QColorVector and QColorMatrix are default-constructed following the
Qt philosophy of types always being well-defined. The corner-case where
we need uninitialized versions of these types for performance reasons
is handled explicitly.

Change-Id: I629334d1ffc63563ec9fd1298c623946e0799d1d
Reviewed-by: Paul Olav Tvete <paul.tvete@qt.io>
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
Tor Arne Vestbø 2019-09-04 12:50:37 +02:00
parent f567129bb5
commit d1486e2982
4 changed files with 20 additions and 22 deletions

View File

@ -62,17 +62,16 @@ class QColorVector
{
public:
QColorVector() = default;
Q_DECL_CONSTEXPR QColorVector(float x, float y, float z) : x(x), y(y), z(z), _unused(0.0f) { }
Q_DECL_CONSTEXPR QColorVector(float x, float y, float z) : x(x), y(y), z(z) { }
explicit Q_DECL_CONSTEXPR QColorVector(const QPointF &chr) // from XY chromaticity
: x(chr.x() / chr.y())
, y(1.0f)
, z((1.0 - chr.x() - chr.y()) / chr.y())
, _unused(0.0f)
{ }
float x; // X, x or red
float y; // Y, y or green
float z; // Z, Y or blue
float _unused;
float x = 0.0f; // X, x or red
float y = 0.0f; // Y, y or green
float z = 0.0f; // Z, Y or blue
float _unused = 0.0f;
friend inline bool operator==(const QColorVector &v1, const QColorVector &v2);
friend inline bool operator!=(const QColorVector &v1, const QColorVector &v2);
@ -81,7 +80,6 @@ public:
return !x && !y && !z;
}
static Q_DECL_CONSTEXPR QColorVector null() { return QColorVector(0.0f, 0.0f, 0.0f); }
static bool isValidChromaticity(const QPointF &chr)
{
if (chr.x() < qreal(0.0) || chr.x() > qreal(1.0))
@ -187,10 +185,6 @@ public:
{ r.z, g.z, b.z } };
}
static QColorMatrix null()
{
return { QColorVector::null(), QColorVector::null(), QColorVector::null() };
}
static QColorMatrix identity()
{
return { { 1.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 1.0f } };

View File

@ -146,17 +146,11 @@ QColorMatrix QColorSpacePrimaries::toXyzMatrix() const
}
QColorSpacePrivate::QColorSpacePrivate()
: primaries(QColorSpace::Primaries::Custom)
, transferFunction(QColorSpace::TransferFunction::Custom)
, gamma(0.0f)
, whitePoint(QColorVector::null())
, toXyz(QColorMatrix::null())
{
}
QColorSpacePrivate::QColorSpacePrivate(QColorSpace::NamedColorSpace namedColorSpace)
: namedColorSpace(namedColorSpace)
, gamma(0.0f)
{
switch (namedColorSpace) {
case QColorSpace::SRgb:
@ -282,7 +276,7 @@ void QColorSpacePrivate::initialize()
void QColorSpacePrivate::setToXyzMatrix()
{
if (primaries == QColorSpace::Primaries::Custom) {
toXyz = QColorMatrix::null();
toXyz = QColorMatrix();
whitePoint = QColorVector::D50();
return;
}

View File

@ -124,9 +124,9 @@ public:
static constexpr QColorSpace::NamedColorSpace Unknown = QColorSpace::NamedColorSpace(0);
QColorSpace::NamedColorSpace namedColorSpace = Unknown;
QColorSpace::Primaries primaries;
QColorSpace::TransferFunction transferFunction;
float gamma;
QColorSpace::Primaries primaries = QColorSpace::Primaries::Custom;
QColorSpace::TransferFunction transferFunction = QColorSpace::TransferFunction::Custom;
float gamma = 0.0f;
QColorVector whitePoint;
QColorTrc trc[3];

View File

@ -612,6 +612,15 @@ static void storeOpaque(QRgba64 *dst, const QRgba64 *src, const QColorVector *bu
static constexpr qsizetype WorkBlockSize = 256;
template <typename T, int Count = 1>
class QUninitialized
{
public:
operator T*() { return reinterpret_cast<T *>(this); }
private:
alignas(T) char data[sizeof(T) * Count];
};
template<typename T>
void QColorTransformPrivate::apply(T *dst, const T *src, qsizetype count, TransformFlags flags) const
{
@ -623,7 +632,8 @@ void QColorTransformPrivate::apply(T *dst, const T *src, qsizetype count, Transf
bool doApplyMatrix = (colorMatrix != QColorMatrix::identity());
QColorVector buffer[WorkBlockSize];
QUninitialized<QColorVector, WorkBlockSize> buffer;
qsizetype i = 0;
while (i < count) {
const qsizetype len = qMin(count - i, WorkBlockSize);