| 1 | /* |
| 2 | * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "KisWaylandOutputColorInfo.h" |
| 8 | |
| 9 | #include <QScreen> |
| 10 | #include <QTimer> |
| 11 | #include <QPointer> |
| 12 | |
| 13 | #include <qpa/qplatformnativeinterface.h> |
| 14 | |
| 15 | #include "KisWaylandAPIColorManager.h" |
| 16 | #include "KisWaylandAPIImageDescription.h" |
| 17 | #include "KisWaylandAPIOutput.h" |
| 18 | |
| 19 | #include "KisWaylandSurfaceColorManager.h" |
| 20 | |
| 21 | #include <QtAssert> |
| 22 | #include <kis_debug.h> |
| 23 | |
| 24 | KisWaylandOutputColorInfo::KisWaylandOutputColorInfo(QObject *parent) |
| 25 | : KisOutputColorInfoInterface(parent) |
| 26 | { |
| 27 | m_waylandManager = KisWaylandSurfaceColorManager::getOrCreateGlobalWaylandManager(); |
| 28 | connect(m_waylandManager.get(), &KisWaylandAPIColorManager::sigReadyChanged, |
| 29 | this, &KisWaylandOutputColorInfo::reinitialize); |
| 30 | reinitialize(); |
| 31 | |
| 32 | connect(qApp, &QGuiApplication::screenAdded, this, &KisWaylandOutputColorInfo::slotScreenAdded); |
| 33 | connect(qApp, &QGuiApplication::screenRemoved, this, &KisWaylandOutputColorInfo::slotScreenRemoved); |
| 34 | } |
| 35 | |
| 36 | KisWaylandOutputColorInfo::~KisWaylandOutputColorInfo() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | void KisWaylandOutputColorInfo::setReadyImpl(bool value) |
| 41 | { |
| 42 | if (value == m_isReady) return; |
| 43 | |
| 44 | m_isReady = value; |
| 45 | Q_EMIT sigReadyChanged(m_isReady); |
| 46 | } |
| 47 | |
| 48 | bool KisWaylandOutputColorInfo::checkIfAllReady() const |
| 49 | { |
| 50 | if (m_waylandOutputs.empty()) return false; |
| 51 | |
| 52 | return std::find_if(m_waylandOutputs.begin(), m_waylandOutputs.end(), |
| 53 | [] (const auto &it) { |
| 54 | return !it.second->m_imageDescription->info.isReady(); |
| 55 | } |
| 56 | ) == m_waylandOutputs.end(); |
| 57 | } |
| 58 | |
| 59 | void KisWaylandOutputColorInfo::reinitialize() |
| 60 | { |
| 61 | m_waylandOutputs.clear(); |
| 62 | |
| 63 | if (!m_waylandManager->isReady()) { |
| 64 | setReadyImpl(false); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | for (QScreen *screen : qApp->screens()) { |
| 69 | initScreenConnection(screen); |
| 70 | } |
| 71 | |
| 72 | setReadyImpl(checkIfAllReady()); |
| 73 | } |
| 74 | |
| 75 | void KisWaylandOutputColorInfo::initScreenConnection(QScreen *screen) |
| 76 | { |
| 77 | auto waylandScreen = screen->nativeInterface<QNativeInterface::QWaylandScreen>(); |
| 78 | |
| 79 | // the screen may have no wayland screen if it is a fake placeholder screen |
| 80 | // that was created by Qt while compositor restart |
| 81 | if (!waylandScreen) return; |
| 82 | |
| 83 | ::wl_output *output = waylandScreen->output(); |
| 84 | ::wp_color_management_output_v1 *cmoutput = m_waylandManager->get_output(output); |
| 85 | Q_ASSERT(cmoutput); |
| 86 | |
| 87 | auto outputObject = std::make_unique<KisWaylandAPIOutput>(cmoutput); |
| 88 | connect(outputObject.get(), &KisWaylandAPIOutput::outputImageDescriptionChanged, outputObject.get(), [this, screen, outputPtr = outputObject.get()]() { |
| 89 | auto desc = outputPtr->m_imageDescription->info.m_data; |
| 90 | Q_EMIT sigOutputDescriptionChanged(screen, desc.toSurfaceDescription()); |
| 91 | |
| 92 | setReadyImpl(checkIfAllReady()); |
| 93 | }); |
| 94 | |
| 95 | m_waylandOutputs.insert_or_assign(screen, std::move(outputObject)); |
| 96 | } |
| 97 | |
| 98 | void KisWaylandOutputColorInfo::slotScreenAdded(QScreen *screen) |
| 99 | { |
| 100 | if (!m_waylandManager->isActive()) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | initScreenConnection(screen); |
| 105 | |
| 106 | setReadyImpl(checkIfAllReady()); |
| 107 | } |
| 108 | |
| 109 | void KisWaylandOutputColorInfo::slotScreenRemoved(QScreen *screen) |
| 110 | { |
| 111 | if (!m_waylandManager->isActive()) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | m_waylandOutputs.erase(screen); |
| 116 | |
| 117 | setReadyImpl(checkIfAllReady()); |
| 118 | } |
| 119 | |
| 120 | bool KisWaylandOutputColorInfo::isReady() const |
| 121 | { |
| 122 | return m_isReady; |
| 123 | } |
| 124 | |
| 125 | std::optional<KisSurfaceColorimetry::SurfaceDescription> KisWaylandOutputColorInfo::outputDescription(const QScreen *screen) const |
| 126 | { |
| 127 | auto it = m_waylandOutputs.find(screen); |
| 128 | if (it == m_waylandOutputs.end()) return std::nullopt; |
| 129 | |
| 130 | const auto &info = it->second->m_imageDescription->info; |
| 131 | |
| 132 | if (!info.isReady()) { |
| 133 | return std::nullopt; |
| 134 | } |
| 135 | |
| 136 | return info.m_data.toSurfaceDescription(); |
| 137 | } |
| 138 | |
| 139 | #include <moc_KisWaylandOutputColorInfo.cpp> |