Debian Codegraph @

plugins/platforms/wayland/waylandcolormanagement/KisWaylandSurfaceColorManager.cpp

raw symbols SCIP
1 /*
2 * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7 #include "KisWaylandSurfaceColorManager.h"
8
9 #include <QWindow>
10 #include <QPromise>
11 #include <QFuture>
12 #include <QPlatformSurfaceEvent>
13
14 #include <qpa/qplatformnativeinterface.h>
15 #include <qpa/qplatformwindow_p.h>
16
17 #include "KisWaylandAPISurface.h"
18 #include "KisWaylandAPIColorManager.h"
19 #include "KisWaylandAPIImageDescription.h"
20 #include "KisWaylandAPIImageDescriptionCreatorParams.h"
21
22 #include <kis_debug.h>
23
24 namespace {
25 template <typename T>
26 QFuture<std::decay_t<T>> makeReadyQFuture(T &&value) {
27 QPromise<std::decay_t<T>> promise;
28 promise.start();
29 promise.addResult(std::forward<T>(value));
30 promise.finish();
31 return promise.future();
32 }
33 }
34
35 Q_GLOBAL_STATIC(std::weak_ptr<KisWaylandAPIColorManager>, s_waylandManager)
36
37 std::shared_ptr<KisWaylandAPIColorManager> KisWaylandSurfaceColorManager::getOrCreateGlobalWaylandManager()
38 {
39 std::shared_ptr<KisWaylandAPIColorManager> result;
40
41 if (s_waylandManager.exists()) {
42 result = s_waylandManager->lock();
43 }
44
45 /**
46 * A weak pointer in `s_waylandManager` may have become released,
47 * so it may be null, even though the static itself is non-null.
48 */
49 if (!result) {
50 result.reset(new KisWaylandAPIColorManager());
51 *s_waylandManager = result;
52 }
53
54 return result;
55 }
56
57 KisWaylandSurfaceColorManager::KisWaylandSurfaceColorManager(QWindow *window, QObject *parent)
58 : KisSurfaceColorManagerInterface(window, parent)
59 {
60 m_waylandManager = getOrCreateGlobalWaylandManager();
61 connect(m_waylandManager.get(), &KisWaylandAPIColorManager::sigReadyChanged,
62 this, &KisWaylandSurfaceColorManager::reinitialize);
63
64 /**
65 * If we have reused an existing wayland manager, then it will be ready
66 */
67 if (m_waylandManager->isReady()) {
68 reinitialize();
69 }
70 }
71
72 KisWaylandSurfaceColorManager::~KisWaylandSurfaceColorManager()
73 {
74 }
75
76 void KisWaylandSurfaceColorManager::setReadyImpl(bool value)
77 {
78 if (value == m_isReady) return;
79
80 m_isReady = value;
81 Q_EMIT sigReadyChanged(m_isReady);
82 }
83
84 class PlatformWindowDetectionEventFilter : public QObject
85 {
86 Q_OBJECT
87 public:
88 PlatformWindowDetectionEventFilter(QObject *watched, QObject *parent = nullptr)
89 : QObject(parent)
90 , m_watched(watched)
91 {
92 }
93
94 Q_SIGNALS:
95 void sigPlatformWindowCreated();
96 void sigPlatformWindowDestroyed();
97
98 private:
99 bool eventFilter(QObject *watched, QEvent *event) override {
100 if (watched != m_watched) return false;
101
102 if (event->type() == QEvent::PlatformSurface) {
103 QPlatformSurfaceEvent *pevent = static_cast<QPlatformSurfaceEvent*>(event);
104 if (pevent->surfaceEventType() == QPlatformSurfaceEvent::SurfaceCreated) {
105 Q_EMIT sigPlatformWindowCreated();
106 } else if (pevent->surfaceEventType() == QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed) {
107 Q_EMIT sigPlatformWindowDestroyed();
108 }
109 }
110
111 return false;
112 }
113
114 QObject *m_watched = nullptr;
115 };
116
117
118 void KisWaylandSurfaceColorManager::reinitialize()
119 {
120 if (!m_waylandManager->isReady()) {
121 auto newState = tryDeinitialize(std::nullopt);
122 KIS_SAFE_ASSERT_RECOVER_NOOP(newState == WaylandSurfaceState::Disconnected);
123 setReadyImpl(false);
124 return;
125 }
126
127 if (m_currentState > WaylandSurfaceState::Disconnected) {
128 qWarning() << "WARNING: KisWaylandSurfaceColorManager::reinitialize(): received unbalanced connectionActive(true) signal!";
129 qWarning() << " " << ppVar(m_currentState);
130 m_currentState = tryDeinitialize(std::nullopt);
131 KIS_SAFE_ASSERT_RECOVER_RETURN(m_currentState == WaylandSurfaceState::Connected);
132 }
133
134 m_currentState = tryInitilize();
135 KIS_SAFE_ASSERT_RECOVER_NOOP(m_currentState < WaylandSurfaceState::PreferredDescriptionReceived);
136 }
137
138 bool KisWaylandSurfaceColorManager::isReady() const
139 {
140 return m_isReady;
141 }
142
143 bool KisWaylandSurfaceColorManager::supportsSurfaceDescription(const KisSurfaceColorimetry::SurfaceDescription &desc)
144 {
145 using feature = QtWayland::wp_color_manager_v1::feature;
146 using namespace KisColorimetryUtils;
147
148 if (!m_waylandManager->isFeatureSupported(feature::feature_parametric)) {
149 qWarning() << "KisWaylandSurfaceColorManager: feature_parametric is not supported";
150 return false;
151 }
152
153 if (!m_waylandManager->isFeatureSupported(feature::feature_set_primaries) &&
154 std::holds_alternative<Colorimetry>(desc.colorSpace.primaries)) {
155
156 qWarning() << "KisWaylandSurfaceColorManager: feature_set_primaries is not supported, even though requested";
157 return false;
158 }
159
160 if (!m_waylandManager->isFeatureSupported(feature::feature_set_tf_power) &&
161 std::holds_alternative<uint32_t>(desc.colorSpace.transferFunction)) {
162
163 qWarning() << "KisWaylandSurfaceColorManager: feature_set_tf_power is not supported, even though requested";
164 return false;
165 }
166
167 if (!m_waylandManager->isFeatureSupported(feature::feature_set_luminances) &&
168 desc.colorSpace.luminance) {
169
170 qWarning() << "KisWaylandSurfaceColorManager: feature_set_luminances is not supported, even though requested";
171 return false;
172 }
173
174 if (!m_waylandManager->isFeatureSupported(feature::feature_set_mastering_display_primaries) &&
175 desc.masteringInfo) {
176
177 qWarning() << "KisWaylandSurfaceColorManager: feature_set_mastering_display_primaries is not supported, even though requested";
178 return false;
179 }
180
181 if (!m_waylandManager->isFeatureSupported(feature::feature_extended_target_volume) &&
182 desc.masteringInfo && desc.colorSpace.luminance) {
183 if (desc.masteringInfo->luminance.minLuminance < desc.colorSpace.luminance->minLuminance
184 || desc.masteringInfo->luminance.maxLuminance > desc.colorSpace.luminance->maxLuminance) {
185
186 qWarning() << "KisWaylandSurfaceColorManager: feature_set_mastering_display_primaries is not supported, even though requested";
187 return false;
188 }
189 }
190
191 if (std::holds_alternative<KisSurfaceColorimetry::NamedPrimaries>(desc.colorSpace.primaries)) {
192 auto waylandPrimaries = primariesKritaToWayland(std::get<KisSurfaceColorimetry::NamedPrimaries>(desc.colorSpace.primaries));
193
194 if (!m_waylandManager->isPrimariesNamedSupported(waylandPrimaries))
195 return false;
196 }
197
198 if (std::holds_alternative<KisSurfaceColorimetry::NamedTransferFunction>(desc.colorSpace.transferFunction)) {
199 auto waylandTransferFunction = transferFunctionKritaToWayland(std::get<KisSurfaceColorimetry::NamedTransferFunction>(desc.colorSpace.transferFunction));
200
201 /**
202 * For some obscure reason Wayland compositors implemented transfer_function_srgb
203 * as gamma-2.2 transfer function, which caused a lot of confusion. Hence the enum
204 * is going to be deprecated in the upcoming version of the protocol.
205 *
206 * https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/442
207 */
208 if (waylandTransferFunction == QtWayland::wp_color_manager_v1::transfer_function_srgb ||
209 waylandTransferFunction == QtWayland::wp_color_manager_v1::transfer_function_ext_srgb) {
210
211 return false;
212 }
213
214 if (!m_waylandManager->isTransferFunctionNamedSupported(waylandTransferFunction))
215 return false;
216 }
217
218 return true;
219 }
220
221 bool KisWaylandSurfaceColorManager::supportsRenderIntent(const KisSurfaceColorimetry::RenderIntent &intent)
222 {
223 auto waylandIntent = renderIntentKritaToWayland(intent);
224 return m_waylandManager->isIntentSupported(waylandIntent);
225 }
226
227 #define USE_KWIN_BUG_WORKAROUND
228
229 #ifdef USE_KWIN_BUG_WORKAROUND
230 #include "wayland-wayland-client-protocol.h"
231 #endif
232
233 QFuture<bool> KisWaylandSurfaceColorManager::setSurfaceDescription(const KisSurfaceColorimetry::SurfaceDescription &desc, KisSurfaceColorimetry::RenderIntent intent)
234 {
235 if (!m_isReady) {
236 qWarning() << "ERROR: KisWaylandSurfaceColorManager::setSurfaceDescription: the manager is not ready";
237 return makeReadyQFuture(false);
238 }
239
240 using KisSurfaceColorimetry::WaylandSurfaceDescription;
241
242 auto waylandDescription = WaylandSurfaceDescription::fromSurfaceDescription(desc);
243 auto waylandIntent = renderIntentKritaToWayland(intent);
244
245 if (!supportsSurfaceDescription(desc)) {
246 qWarning() << "ERROR: KisWaylandSurfaceColorManager::setSurfaceDescription: unsupported surface description";
247 return makeReadyQFuture(false);
248 }
249
250 if (!m_waylandManager->isIntentSupported(waylandIntent)) {
251 qWarning() << "ERROR: KisWaylandSurfaceColorManager::setSurfaceDescription: unsupported rendering intent";
252 return makeReadyQFuture(false);
253 }
254
255 KisWaylandAPIImageDescriptionCreatorParams creator(m_waylandManager.get());
256 std::shared_ptr<KisWaylandAPIImageDescriptionNoInfo> descriptionObject =
257 creator.createImageDescription(waylandDescription);
258
259 QPromise<bool> imageDescriptionPromise;
260 auto future = imageDescriptionPromise.future();
261
262 connect(descriptionObject.get(), &KisWaylandAPIImageDescription::sigDescriptionConstructed,
263 this,
264 [promise = std::move(imageDescriptionPromise), descriptionObject] (bool success) mutable {
265 promise.start();
266 promise.addResult(success);
267 promise.finish();
268 });
269
270 QFuture<bool> result =
271 future.then([this, desc, descriptionObject, waylandIntent, intent] (QFuture<bool> future) {
272 if (!future.result()) return false;
273
274 if (!m_surface) {
275 qWarning() << "WARNING: the surface has been destroyed while its format was being set!";
276 return false;
277 }
278
279 #ifdef USE_KWIN_BUG_WORKAROUND
280 if (qEnvironmentVariableIsSet("KRITA_ENABLE_KWIN_INTENT_WORKAROUND")) {
281 // WARNING: KWin <= 6.4.4 doesn't handle intent changes properly
282 if (m_currentDescription && m_currentDescription == desc &&
283 m_renderingIntent && m_renderingIntent != intent) {
284
285 m_surface->unset_image_description();
286 auto waylandWindow = m_window->nativeInterface<QNativeInterface::Private::QWaylandWindow>();
287 ::wl_surface_commit(waylandWindow->surface());
288 }
289 }
290 #endif
291
292 m_surface->set_image_description(descriptionObject->object(), waylandIntent);
293 m_window->requestUpdate();
294 m_currentDescription = desc;
295 m_renderingIntent = intent;
296 return true;
297 });
298
299 return result;
300 }
301
302 void KisWaylandSurfaceColorManager::unsetSurfaceDescription()
303 {
304 if (!m_isReady) return;
305
306 m_surface->unset_image_description();
307 m_currentDescription = std::nullopt;
308 m_renderingIntent = std::nullopt;
309 }
310
311 std::optional<KisSurfaceColorimetry::SurfaceDescription> KisWaylandSurfaceColorManager::surfaceDescription() const
312 {
313 return m_currentDescription;
314 }
315
316 std::optional<KisSurfaceColorimetry::RenderIntent> KisWaylandSurfaceColorManager::renderingIntent() const
317 {
318 return m_renderingIntent;
319 }
320
321 std::optional<KisSurfaceColorimetry::SurfaceDescription> KisWaylandSurfaceColorManager::preferredSurfaceDescription() const
322 {
323 return m_preferredDescription;
324 }
325
326 void KisWaylandSurfaceColorManager::slotPreferredChanged()
327 {
328 KIS_SAFE_ASSERT_RECOVER_RETURN(m_currentState >= WaylandSurfaceState::APIFeedbackCreated);
329
330 auto newState = tryInitilize();
331
332 KIS_SAFE_ASSERT_RECOVER_RETURN(newState == WaylandSurfaceState::PreferredDescriptionReceived);
333
334 if (newState == m_currentState) {
335 Q_EMIT sigPreferredSurfaceDescriptionChanged(*m_preferredDescription);
336 } else {
337 m_currentState = newState;
338 setReadyImpl(true);
339 }
340 }
341
342 void KisWaylandSurfaceColorManager::slotPlatformWindowCreated()
343 {
344 if (m_currentState > WaylandSurfaceState::Connected) {
345 qWarning() << "WARNING: KisWaylandSurfaceColorManager::slotPlatformWindowCreated(): received unbalanced window created signal!";
346 qWarning() << " " << ppVar(m_currentState);
347 m_currentState = tryDeinitialize(WaylandSurfaceState::Connected);
348 KIS_SAFE_ASSERT_RECOVER_RETURN(m_currentState == WaylandSurfaceState::Connected);
349 setReadyImpl(false);
350 }
351
352 auto newState = tryInitilize();
353
354 if (newState < WaylandSurfaceState::WaylandWindowCreated) {
355 qWarning() << "WARNING: KisWaylandSurfaceColorManager::slotPlatformWindowCreated(): failed to reach WaylandWindowCreated state";
356 }
357
358 m_currentState = newState;
359 }
360
361 void KisWaylandSurfaceColorManager::slotPlatformWindowDestroyed()
362 {
363 auto newState = tryDeinitialize(WaylandSurfaceState::Connected);
364 KIS_SAFE_ASSERT_RECOVER_NOOP(newState <= WaylandSurfaceState::Connected);
365 m_currentState = newState;
366 setReadyImpl(false);
367 }
368
369 void KisWaylandSurfaceColorManager::slotWaylandSurfaceCreated()
370 {
371 if (m_currentState > WaylandSurfaceState::WaylandWindowCreated) {
372 qWarning() << "WARNING: KisWaylandSurfaceColorManager::slotWaylandSurfaceCreated(): received unbalanced surface created signal!";
373 qWarning() << " " << ppVar(m_currentState);
374 m_currentState = tryDeinitialize(WaylandSurfaceState::WaylandWindowCreated);
375 KIS_SAFE_ASSERT_RECOVER_RETURN(m_currentState == WaylandSurfaceState::WaylandWindowCreated);
376 setReadyImpl(false);
377 }
378
379 auto newState = tryInitilize();
380
381 if (newState < WaylandSurfaceState::WaylandSurfaceCreated) {
382 qWarning() << "WARNING: KisWaylandSurfaceColorManager::slotWaylandSurfaceCreated(): failed to reach WaylandSurfaceCreated state";
383 }
384 m_currentState = newState;
385 }
386
387 void KisWaylandSurfaceColorManager::slotWaylandSurfaceDestroyed()
388 {
389 auto newState = tryDeinitialize(WaylandSurfaceState::WaylandWindowCreated);
390 KIS_SAFE_ASSERT_RECOVER_NOOP(newState <= WaylandSurfaceState::WaylandWindowCreated);
391 m_currentState = newState;
392 setReadyImpl(false);
393 }
394
395 KisWaylandSurfaceColorManager::WaylandSurfaceState
396 KisWaylandSurfaceColorManager::tryInitilize()
397 {
398 /**
399 * Initialization of the managed happens sequentially by transitioning through
400 * a set of states. Some states may be transitioned asynchronously, e.g. when
401 * Qt creates a platform windows or when it creates or recreates a surface.
402 */
403
404 WaylandSurfaceState currentState = WaylandSurfaceState::Disconnected;
405
406 if (m_waylandManager->isReady()) {
407 currentState = WaylandSurfaceState::Connected;
408 }
409
410 if (currentState == WaylandSurfaceState::Connected) {
411 if (!m_platformWindowStateDetector) {
412 auto *filter = new PlatformWindowDetectionEventFilter(m_window, this);
413 connect(filter,
414 &PlatformWindowDetectionEventFilter::sigPlatformWindowCreated,
415 this,
416 &KisWaylandSurfaceColorManager::slotPlatformWindowCreated);
417 connect(filter,
418 &PlatformWindowDetectionEventFilter::sigPlatformWindowDestroyed,
419 this,
420 &KisWaylandSurfaceColorManager::slotPlatformWindowDestroyed);
421 m_window->installEventFilter(filter);
422 m_platformWindowStateDetector = filter;
423 }
424
425 auto waylandWindow = m_window->nativeInterface<QNativeInterface::Private::QWaylandWindow>();
426 if (waylandWindow) {
427 currentState = WaylandSurfaceState::WaylandWindowCreated;
428 }
429 }
430
431 if (currentState == WaylandSurfaceState::WaylandWindowCreated) {
432 auto waylandWindow = m_window->nativeInterface<QNativeInterface::Private::QWaylandWindow>();
433
434 if (!m_surfaceCreatedConnection) {
435 m_surfaceCreatedConnection = connect(waylandWindow,
436 &QNativeInterface::Private::QWaylandWindow::surfaceCreated,
437 this,
438 &KisWaylandSurfaceColorManager::slotWaylandSurfaceCreated);
439 }
440
441 if (!m_surfaceDestroyedConnection) {
442 m_surfaceDestroyedConnection = connect(waylandWindow,
443 &QNativeInterface::Private::QWaylandWindow::surfaceDestroyed,
444 this,
445 &KisWaylandSurfaceColorManager::slotWaylandSurfaceDestroyed);
446 }
447
448 if (waylandWindow->surface()) {
449 currentState = WaylandSurfaceState::WaylandSurfaceCreated;
450 }
451 }
452
453 if (currentState == WaylandSurfaceState::WaylandSurfaceCreated) {
454 auto waylandWindow = m_window->nativeInterface<QNativeInterface::Private::QWaylandWindow>();
455
456 if (!m_surface) {
457 auto feedback = std::make_unique<KisWaylandAPISurfaceFeedback>(
458 m_waylandManager->get_surface_feedback(waylandWindow->surface()));
459 connect(feedback.get(),
460 &KisWaylandAPISurfaceFeedback::preferredChanged,
461 this,
462 &KisWaylandSurfaceColorManager::slotPreferredChanged);
463 m_surface = std::make_unique<KisWaylandAPISurface>(m_waylandManager->get_surface(waylandWindow->surface()),
464 std::move(feedback));
465 }
466
467 currentState = WaylandSurfaceState::APIFeedbackCreated;
468 }
469
470 if (currentState == WaylandSurfaceState::APIFeedbackCreated) {
471 if (m_surface->m_feedback->m_preferred->info.isReady()) {
472 auto waylandDesc = m_surface->m_feedback->m_preferred->info.m_data;
473 m_preferredDescription = waylandDesc.toSurfaceDescription();
474
475 currentState = WaylandSurfaceState::PreferredDescriptionReceived;
476 }
477 }
478
479 return currentState;
480 }
481
482 KisWaylandSurfaceColorManager::WaylandSurfaceState
483 KisWaylandSurfaceColorManager::tryDeinitialize(std::optional<KisWaylandSurfaceColorManager::WaylandSurfaceState> targetState)
484 {
485 WaylandSurfaceState currentState = WaylandSurfaceState::PreferredDescriptionReceived;
486
487 if (!targetState || *targetState < currentState) {
488 m_currentDescription = std::nullopt;
489 m_renderingIntent = std::nullopt;
490 m_preferredDescription = std::nullopt;
491
492 currentState = WaylandSurfaceState::APIFeedbackCreated;
493 }
494
495 auto waylandWindow = m_window->nativeInterface<QNativeInterface::Private::QWaylandWindow>();
496
497 if (!waylandWindow || !waylandWindow->surface() ||
498 (targetState && *targetState < currentState)) {
499
500 m_surface.reset();
501 currentState = WaylandSurfaceState::WaylandSurfaceCreated;
502 }
503
504 if (!waylandWindow || !waylandWindow->surface() ||
505 (targetState && *targetState < currentState)) {
506
507 currentState = WaylandSurfaceState::WaylandWindowCreated;
508 }
509
510 if (!waylandWindow || (targetState && *targetState < currentState)) {
511 if (m_surfaceCreatedConnection) {
512 disconnect(m_surfaceCreatedConnection);
513 }
514 if (m_surfaceDestroyedConnection) {
515 disconnect(m_surfaceDestroyedConnection);
516 }
517 currentState = WaylandSurfaceState::Connected;
518 }
519
520 if (!m_waylandManager->isReady() || (targetState && *targetState < currentState)) {
521 m_window->removeEventFilter(m_platformWindowStateDetector);
522 m_platformWindowStateDetector->deleteLater();
523 m_platformWindowStateDetector = nullptr;
524
525 currentState = WaylandSurfaceState::Disconnected;
526 }
527
528 return currentState;
529 }
530
531 #include <moc_KisWaylandSurfaceColorManager.cpp>
532 #include <KisWaylandSurfaceColorManager.moc>