| 1 |
// Copyright (C) 2024 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QtGui> |
| 5 |
#include <qpa/qplatformscreen.h> |
| 6 |
|
| 7 |
QString debugDescription(const QColorSpace &colorSpace) |
| 8 |
{ |
| 9 |
QString str; |
| 10 |
QDebug dbg(&str); |
| 11 |
dbg << colorSpace; |
| 12 |
return str; |
| 13 |
} |
| 14 |
|
| 15 |
// The color space of our assets that we need to color match from. |
| 16 |
// In real world apps this could be different for each QImage etc. |
| 17 |
static const QColorSpace assetColorSpace = QColorSpace::AdobeRgb; |
| 18 |
|
| 19 |
class TestWindow : public QRasterWindow |
| 20 |
{ |
| 21 |
public: |
| 22 |
using QRasterWindow::QRasterWindow; |
| 23 |
TestWindow() |
| 24 |
{ |
| 25 |
resize(900, 300); |
| 26 |
setFlag(Qt::NoDropShadowWindowHint); |
| 27 |
|
| 28 |
// Moving a window to a different screen may not result |
| 29 |
// in an automatic expose/paint event, as the window's |
| 30 |
// content might still be valid on the new screen. In |
| 31 |
// our case we know it's not, as we need to update the |
| 32 |
// screen information we reflect in the paintEvent below. |
| 33 |
QObject::connect(this, &QWindow::screenChanged, |
| 34 |
this, qOverload<>(&QRasterWindow::update)); |
| 35 |
} |
| 36 |
|
| 37 |
bool colorManaged = false; |
| 38 |
|
| 39 |
protected: |
| 40 |
void paintEvent(QPaintEvent *) override |
| 41 |
{ |
| 42 |
QPainter painter(this); |
| 43 |
|
| 44 |
auto requestedColorSpace = requestedFormat().colorSpace(); |
| 45 |
auto windowColorSpace = format().colorSpace(); |
| 46 |
auto screenColorSpace = screen()->handle()->colorSpace(); |
| 47 |
|
| 48 |
auto targetColorSpace = windowColorSpace; |
| 49 |
if (!targetColorSpace.isValid()) { |
| 50 |
qWarning("Window does not report color space! Using screen color space as target"); |
| 51 |
targetColorSpace = screenColorSpace; |
| 52 |
} |
| 53 |
auto colorTransform = assetColorSpace.transformationToColorSpace(targetColorSpace); |
| 54 |
|
| 55 |
if (!colorManaged && requestedColorSpace.isValid() && !colorTransform.isIdentity()) { |
| 56 |
qWarning() << "Requested" << requestedColorSpace << "but got" |
| 57 |
<< targetColorSpace << "and not prepared to do color matching"; |
| 58 |
} |
| 59 |
|
| 60 |
QColor colors[] = { |
| 61 |
QColor(Qt::red), |
| 62 |
QColor(Qt::green), |
| 63 |
QColor(Qt::blue), |
| 64 |
QColor(Qt::cyan), |
| 65 |
QColor(Qt::magenta), |
| 66 |
QColor(Qt::yellow) |
| 67 |
}; |
| 68 |
qreal colorWidth = width() / qreal(std::size(colors)); |
| 69 |
for (size_t i = 0; i < std::size(colors); ++i) { |
| 70 |
QColor color = colors[i]; |
| 71 |
if (colorManaged) |
| 72 |
color = colorTransform.map(color); |
| 73 |
|
| 74 |
painter.fillRect(QRectF(colorWidth * i, 0, colorWidth, height()), color); |
| 75 |
} |
| 76 |
|
| 77 |
QRect rect(0, 0, width(), height()); |
| 78 |
painter.fillRect(rect.adjusted(20, 100, -20, -100), Qt::white); |
| 79 |
painter.drawText(rect, Qt::AlignCenter, |
| 80 |
QString("Assets: %1\nRequested: %2\nWindow: %3\nScreen: %4").arg( |
| 81 |
debugDescription(assetColorSpace) |
| 82 |
).arg( |
| 83 |
debugDescription(windowColorSpace) |
| 84 |
).arg( |
| 85 |
debugDescription(windowColorSpace) |
| 86 |
).arg( |
| 87 |
debugDescription(screenColorSpace) |
| 88 |
)); |
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
int main(int argc, char *argv[]) |
| 93 |
{ |
| 94 |
QGuiApplication app(argc, argv); |
| 95 |
|
| 96 |
TestWindow defaultColorSpaceWindow; |
| 97 |
defaultColorSpaceWindow.setTitle(QString( |
| 98 |
"Color-space unaware (pass-through, assuming surface/screen is %1)").arg( |
| 99 |
assetColorSpace.description())); |
| 100 |
defaultColorSpaceWindow.show(); |
| 101 |
|
| 102 |
TestWindow colorSpaceAwareWindow; |
| 103 |
colorSpaceAwareWindow.setTitle(QString( |
| 104 |
"Color-space aware (match %1 to surface)").arg(assetColorSpace.description())); |
| 105 |
colorSpaceAwareWindow.colorManaged = true; |
| 106 |
colorSpaceAwareWindow.show(); |
| 107 |
|
| 108 |
TestWindow explicitColorSpaceWindow; |
| 109 |
explicitColorSpaceWindow.setTitle(QString( |
| 110 |
"Explicit %1 surface").arg(assetColorSpace.description())); |
| 111 |
auto format = explicitColorSpaceWindow.format(); |
| 112 |
format.setColorSpace(assetColorSpace); |
| 113 |
explicitColorSpaceWindow.setFormat(format); |
| 114 |
explicitColorSpaceWindow.show(); |
| 115 |
|
| 116 |
return app.exec(); |
| 117 |
} |