Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qwaylandquickitem.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
13#include <QtWaylandCompositor/qwaylandcompositor.h>
14#include <QtWaylandCompositor/qwaylandseat.h>
15#include <QtWaylandCompositor/qwaylandbufferref.h>
16#if QT_CONFIG(draganddrop)
17#include <QtWaylandCompositor/QWaylandDrag>
18#endif
19#include <QtWaylandCompositor/private/qwlclientbufferintegration_p.h>
20#include <QtWaylandCompositor/private/qwaylandsurface_p.h>
21
22#if QT_CONFIG(opengl)
23# include <QtOpenGL/QOpenGLTexture>
24# include <QtGui/QOpenGLFunctions>
25#endif
26
27#include <QtGui/QKeyEvent>
28#include <QtGui/QGuiApplication>
29#include <QtGui/QScreen>
30
31#include <QtQuick/QSGSimpleTextureNode>
32#include <QtQuick/QQuickWindow>
33#include <QtQuick/qsgtexture.h>
34
35#include <QtCore/QFile>
36#include <QtCore/qendian.h>
37#include <QtCore/QMutexLocker>
38#include <QtCore/QMutex>
39
40#include <wayland-server-core.h>
41#include <QThread>
42
43#if QT_CONFIG(opengl)
44#include <QtGui/private/qshaderdescription_p.h>
45#endif
46
47#ifndef GL_TEXTURE_EXTERNAL_OES
48#define GL_TEXTURE_EXTERNAL_OES 0x8D65
49#endif
50
51QT_BEGIN_NAMESPACE
52
53#if QT_CONFIG(opengl)
54static const struct {
55 const char * const vertexShaderSourceFile;
56 const char * const fragmentShaderSourceFile;
57 int planeCount;
58 bool canProvideTexture;
59 QSGMaterial::Flags materialFlags;
60 QSGMaterialType materialType;
61} bufferTypes[] = {
62 // BufferFormatEgl_Null
63 { "", "", 0, false, {}, {} },
64
65 // BufferFormatEgl_RGB (GL_TEXTURE_2D)
66 {
67 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
68 ":/qt-project.org/wayland/compositor/shaders/surface_rgbx.frag.qsb",
69 1, true,
70 QSGMaterial::Blending,
71 {}
72 },
73
74 // BufferFormatEgl_RGBA (GL_TEXTURE_2D)
75 {
76 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
77 ":/qt-project.org/wayland/compositor/shaders/surface_rgba.frag.qsb",
78 1, true,
79 QSGMaterial::Blending,
80 {}
81 },
82
83 // BufferFormatEgl_EXTERNAL_OES (GL_TEXTURE_EXTERNAL_OES)
84 {
85 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
86 ":/qt-project.org/wayland/compositor/shaders/surface_oes_external.frag",
87 1, false,
88 QSGMaterial::Blending,
89 {}
90 },
91
92 // BufferFormatEgl_Y_U_V (GL_TEXTURE_2D)
93 {
94 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
95 ":/qt-project.org/wayland/compositor/shaders/surface_y_u_v.frag.qsb",
96 3, false,
97 QSGMaterial::Blending,
98 {}
99 },
100
101 // BufferFormatEgl_Y_UV (GL_TEXTURE_2D)
102 {
103 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
104 ":/qt-project.org/wayland/compositor/shaders/surface_y_uv.frag.qsb",
105 2, false,
106 QSGMaterial::Blending,
107 {}
108 },
109
110 // BufferFormatEgl_Y_XUXV (GL_TEXTURE_2D)
111 {
112 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
113 ":/qt-project.org/wayland/compositor/shaders/surface_y_xuxv.frag.qsb",
114 2, false,
115 QSGMaterial::Blending,
116 {}
117 }
118};
119
120QWaylandBufferMaterialShader::QWaylandBufferMaterialShader(QWaylandBufferRef::BufferFormatEgl format)
121{
122 Q_UNUSED(format);
123 setShaderFileName(VertexStage, QString::fromLatin1(bufferTypes[format].vertexShaderSourceFile));
124 auto fragmentShaderSourceFile = QString::fromLatin1(bufferTypes[format].fragmentShaderSourceFile);
125
126 if (format == QWaylandBufferRef::BufferFormatEgl_EXTERNAL_OES)
127 setupExternalOESShader(fragmentShaderSourceFile);
128 else
129 setShaderFileName(FragmentStage, fragmentShaderSourceFile);
130}
131
132void QWaylandBufferMaterialShader::setupExternalOESShader(const QString &shaderFilename)
133{
134#if QT_CONFIG(opengl)
135 QFile shaderFile(shaderFilename);
136 if (!shaderFile.open(QIODevice::ReadOnly)) {
137 qCWarning(qLcWaylandCompositor) << "Cannot find external OES shader file:" << shaderFilename;
138 return;
139 }
140 QByteArray FS = shaderFile.readAll();
141
142 static const char *FS_GLES_PREAMBLE =
143 "#extension GL_OES_EGL_image_external : require\n"
144 "precision highp float;\n";
145 static const char *FS_GL_PREAMBLE =
146 "#version 120\n"
147 "#extension GL_OES_EGL_image_external : require\n";
148 QByteArray fsGLES = FS_GLES_PREAMBLE + FS;
149 QByteArray fsGL = FS_GL_PREAMBLE + FS;
150
151 QShaderDescription desc;
152 QShaderDescriptionPrivate *descData = QShaderDescriptionPrivate::get(&desc);
153
154 QShaderDescription::InOutVariable texCoordInput;
155 texCoordInput.name = "v_texcoord";
156 texCoordInput.type = QShaderDescription::Vec2;
157 texCoordInput.location = 0;
158
159 descData->inVars = { texCoordInput };
160
161 QShaderDescription::InOutVariable fragColorOutput;
162 fragColorOutput.name = "gl_FragColor";
163 fragColorOutput.type = QShaderDescription::Vec4;
164 fragColorOutput.location = 0;
165
166 descData->outVars = { fragColorOutput };
167
168 QShaderDescription::BlockVariable matrixBlockVar;
169 matrixBlockVar.name = "qt_Matrix";
170 matrixBlockVar.type = QShaderDescription::Mat4;
171 matrixBlockVar.offset = 0;
172 matrixBlockVar.size = 64;
173
174 QShaderDescription::BlockVariable opacityBlockVar;
175 opacityBlockVar.name = "qt_Opacity";
176 opacityBlockVar.type = QShaderDescription::Float;
177 opacityBlockVar.offset = 64;
178 opacityBlockVar.size = 4;
179
180 QShaderDescription::UniformBlock ubufStruct;
181 ubufStruct.blockName = "buf";
182 ubufStruct.structName = "ubuf";
183 ubufStruct.size = 64 + 4;
184 ubufStruct.binding = 0;
185 ubufStruct.members = { matrixBlockVar, opacityBlockVar };
186
187 descData->uniformBlocks = { ubufStruct };
188
189 QShaderDescription::InOutVariable samplerTex0;
190 samplerTex0.name = "tex0";
191 samplerTex0.type = QShaderDescription::SamplerExternalOES;
192 samplerTex0.binding = 1;
193
194 descData->combinedImageSamplers = { samplerTex0 };
195
196 QShader shaderPack;
197 shaderPack.setStage(QShader::FragmentStage);
198 shaderPack.setDescription(desc);
199 shaderPack.setShader(QShaderKey(QShader::GlslShader, QShaderVersion(100, QShaderVersion::GlslEs)), QShaderCode(fsGLES));
200 shaderPack.setShader(QShaderKey(QShader::GlslShader, QShaderVersion(120)), QShaderCode(fsGL));
201
202 setShader(FragmentStage, shaderPack);
203#else
204 Q_UNUSED(shaderFilename);
205#endif
206}
207
208bool QWaylandBufferMaterialShader::updateUniformData(RenderState &state, QSGMaterial *, QSGMaterial *)
209{
210 bool changed = false;
211 QByteArray *buf = state.uniformData();
212 Q_ASSERT(buf->size() >= 68);
213
214 if (state.isMatrixDirty()) {
215 const QMatrix4x4 m = state.combinedMatrix();
216 memcpy(buf->data(), m.constData(), 64);
217 changed = true;
218 }
219
220 if (state.isOpacityDirty()) {
221 const float opacity = state.opacity();
222 memcpy(buf->data() + 64, &opacity, 4);
223 changed = true;
224 }
225
226 return changed;
227}
228
229void QWaylandBufferMaterialShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
230 QSGMaterial *newMaterial, QSGMaterial *)
231{
232 Q_UNUSED(state);
233
234 QWaylandBufferMaterial *material = static_cast<QWaylandBufferMaterial *>(newMaterial);
235 switch (binding) {
236 case 1:
237 *texture = material->m_scenegraphTextures.at(0);
238 break;
239 case 2:
240 *texture = material->m_scenegraphTextures.at(1);
241 break;
242 case 3:
243 *texture = material->m_scenegraphTextures.at(2);
244 break;
245 default:
246 return;
247 }
248
249 // This is for the shared memory case, and is a no-op for others,
250 // this is where the upload from the QImage happens when not yet done.
251 // ### or is this too late? (if buffer.image() disappears in the meantime then this is the wrong...)
252 if (*texture)
253 (*texture)->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
254}
255
256QWaylandBufferMaterial::QWaylandBufferMaterial(QWaylandBufferRef::BufferFormatEgl format)
257 : m_format(format)
258{
259 setFlag(bufferTypes[m_format].materialFlags);
260}
261
262QWaylandBufferMaterial::~QWaylandBufferMaterial()
263{
264 qDeleteAll(m_scenegraphTextures);
265}
266
267void QWaylandBufferMaterial::setTextureForPlane(int plane,
268 QOpenGLTexture *texture,
269 QSGTexture *scenegraphTexture)
270{
271 if (plane < 0 || plane >= bufferTypes[m_format].planeCount) {
272 qWarning("plane index is out of range");
273 return;
274 }
275
276 texture->bind();
277 setTextureParameters(texture->target());
278
279 ensureTextures(plane - 1);
280
281 if (m_textures.size() <= plane) {
282 m_textures << texture;
283 m_scenegraphTextures << scenegraphTexture;
284 } else {
285 delete m_scenegraphTextures[plane];
286
287 m_textures[plane] = texture;
288 m_scenegraphTextures[plane] = scenegraphTexture;
289 }
290}
291
292void QWaylandBufferMaterial::bind()
293{
294 ensureTextures(bufferTypes[m_format].planeCount);
295
296 switch (m_textures.size()) {
297 case 3:
298 if (m_textures[2])
299 m_textures[2]->bind(2);
300 Q_FALLTHROUGH();
301 case 2:
302 if (m_textures[1])
303 m_textures[1]->bind(1);
304 Q_FALLTHROUGH();
305 case 1:
306 if (m_textures[0])
307 m_textures[0]->bind(0);
308 }
309}
310
311QSGMaterialType *QWaylandBufferMaterial::type() const
312{
313 return const_cast<QSGMaterialType *>(&bufferTypes[m_format].materialType);
314}
315
316QSGMaterialShader *QWaylandBufferMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
317{
318 Q_UNUSED(renderMode);
319 return new QWaylandBufferMaterialShader(m_format);
320}
321
322
323void QWaylandBufferMaterial::setTextureParameters(GLenum target)
324{
325 QOpenGLFunctions *gl = QOpenGLContext::currentContext()->functions();
326 gl->glTexParameteri(target, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
327 gl->glTexParameteri(target, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
328 gl->glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
329 gl->glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
330}
331
332//TODO move this into a separate centralized texture management class
333void QWaylandBufferMaterial::ensureTextures(int count)
334{
335 for (int plane = m_textures.size(); plane < count; plane++) {
336 m_textures << nullptr;
337 m_scenegraphTextures << nullptr;
338 }
339}
340
341void QWaylandBufferMaterial::setBufferRef(QWaylandQuickItem *surfaceItem, const QWaylandBufferRef &ref)
342{
343 m_bufferRef = ref;
344 for (int plane = 0; plane < bufferTypes[ref.bufferFormatEgl()].planeCount; plane++) {
345 if (auto texture = ref.toOpenGLTexture(plane)) {
346 QQuickWindow::CreateTextureOptions opt;
347 QWaylandQuickSurface *waylandSurface = qobject_cast<QWaylandQuickSurface *>(surfaceItem->surface());
348 if (waylandSurface != nullptr && waylandSurface->useTextureAlpha() && !waylandSurface->isOpaque())
349 opt |= QQuickWindow::TextureHasAlphaChannel;
350 QSGTexture *scenegraphTexture;
351 if (ref.bufferFormatEgl() == QWaylandBufferRef::BufferFormatEgl_EXTERNAL_OES) {
352 scenegraphTexture = QNativeInterface::QSGOpenGLTexture::fromNativeExternalOES(texture->textureId(),
353 surfaceItem->window(),
354 ref.size(),
355 opt);
356 } else {
357 scenegraphTexture = QNativeInterface::QSGOpenGLTexture::fromNative(texture->textureId(),
358 surfaceItem->window(),
359 ref.size(),
360 opt);
361 }
362 scenegraphTexture->setFiltering(surfaceItem->smooth() ? QSGTexture::Linear : QSGTexture::Nearest);
363 setTextureForPlane(plane, texture, scenegraphTexture);
364 }
365 }
366
367 bind();
368}
369#endif // QT_CONFIG(opengl)
370
371QMutex *QWaylandQuickItemPrivate::mutex = nullptr;
372
373// The X byte of WL_SHM_FORMAT_XRGB8888/XBGR8888 is undefined, but QImage's
374// Format_RGB32/Format_RGBX8888 (which they map to) requires 0xff, and the
375// scene graph uploads them as-is. Since the image wraps client memory, make
376// a copy with the alpha byte set.
377static QImage withValidAlpha(const QImage &image)
378{
379 quint32 alphaMask;
380 switch (image.format()) {
381 case QImage::Format_RGB32:
382 alphaMask = 0xff000000u;
383 break;
384 case QImage::Format_RGBX8888:
385 alphaMask = qFromLittleEndian<quint32>(0xff000000u);
386 break;
387 default:
388 return image;
389 }
390
391 QImage result(image.size(), image.format());
392 if (result.isNull())
393 return image;
394 for (int y = 0; y < image.height(); ++y) {
395 const auto *src = reinterpret_cast<const quint32 *>(image.constScanLine(y));
396 auto *dst = reinterpret_cast<quint32 *>(result.scanLine(y));
397 for (int x = 0; x < image.width(); ++x)
398 dst[x] = src[x] | alphaMask;
399 }
400 return result;
401}
402
404{
405public:
409
411 {
412 // Note: If the ClientBuffer is deleted from the render thread, it happens on a deferred delete
413 // which is processed during the sync phase. Or it may be deleted from the main thread. In
414 // either case, we know the compositor is not shutting down simultaneously. Therefore, we can
415 // safely rely on the value of m_compositorDestroyed to stay the same for the duration of this
416 // destructor.
417 //
418 // Calling setDestroyed() on the buffer will cause it to skip its final sendRelease()
419 // on destruction, which would otherwise cause a crash when the compositor has been
420 // destroyed.
421 if (m_compositorDestroyed.loadAcquire() && m_ref.hasBuffer())
422 m_ref.buffer()->setDestroyed();
423
424 delete m_sgTex;
425 }
426
427 void setBufferRef(QWaylandQuickItem *surfaceItem, const QWaylandBufferRef &buffer)
428 {
429 Q_ASSERT(QThread::currentThread() == thread());
430 m_ref = buffer;
431 delete m_sgTex;
432 m_sgTex = nullptr;
433 if (m_ref.hasBuffer()) {
434 if (buffer.isSharedMemory()) {
435 m_sgTex = surfaceItem->window()->createTextureFromImage(withValidAlpha(buffer.image()));
436 } else {
437#if QT_CONFIG(opengl)
438 QQuickWindow::CreateTextureOptions opt;
439 QWaylandQuickSurface *surface = qobject_cast<QWaylandQuickSurface *>(surfaceItem->surface());
440 if (surface && surface->useTextureAlpha() && !surface->isOpaque()) {
441 opt |= QQuickWindow::TextureHasAlphaChannel;
442 }
443
444 auto texture = buffer.toOpenGLTexture();
445 GLuint textureId = texture->textureId();
446 auto size = surface->bufferSize();
447 m_sgTex = QNativeInterface::QSGOpenGLTexture::fromNative(textureId, surfaceItem->window(), size, opt);
448#else
449 qCWarning(qLcWaylandCompositor) << "Without OpenGL support only shared memory textures are supported";
450#endif
451 }
452 }
453 emit textureChanged();
454 }
455
456 QSGTexture *texture() const override
457 {
458 if (m_sgTex)
459 m_sgTex->setFiltering(m_smooth ? QSGTexture::Linear : QSGTexture::Nearest);
460 return m_sgTex;
461 }
462
463 void setSmooth(bool smooth) { m_smooth = smooth; }
464
466 {
467 m_compositorDestroyed.storeRelease(1);
468 deleteLater();
469 }
470
471private:
472 bool m_smooth = false;
473 QSGTexture *m_sgTex = nullptr;
474
475 QAtomicInt m_compositorDestroyed;
476 QWaylandBufferRef m_ref;
477};
478
479void QWaylandQuickItemPrivate::handleDragUpdate(QWaylandSeat *seat, const QPointF &globalPosition)
480{
481#if QT_CONFIG(draganddrop)
482 Q_Q(QWaylandQuickItem);
483 QWaylandQuickOutput *currentOutput = qobject_cast<QWaylandQuickOutput *>(q->view()->output());
484 //TODO: also check if dragging onto other outputs
485 QWaylandQuickItem *targetItem = qobject_cast<QWaylandQuickItem *>(currentOutput->pickClickableItem(q->mapToScene(globalPosition)));
486 QWaylandSurface *targetSurface = targetItem ? targetItem->surface() : nullptr;
487 if (targetSurface) {
488 QPointF position = q->mapToItem(targetItem, globalPosition);
489 QPointF surfacePosition = targetItem->mapToSurface(position);
490 seat->drag()->dragMove(targetSurface, surfacePosition);
491 }
492#else
493 Q_UNUSED(seat);
494 Q_UNUSED(globalPosition);
495#endif // QT_CONFIG(draganddrop)
496}
497
498void QWaylandQuickItemPrivate::handleDragEnded(QWaylandSeat *seat)
499{
500#if QT_CONFIG(draganddrop)
501 isDragging = false;
502 seat->drag()->drop();
503#else
504 Q_UNUSED(seat);
505#endif // QT_CONFIG(draganddrop)
506}
507
508/*!
509 * \qmltype WaylandQuickItem
510 * \nativetype QWaylandQuickItem
511 * \inqmlmodule QtWayland.Compositor
512 * \since 5.8
513 * \brief The WaylandQuickItem type provides a Qt Quick item which represents the surface of a Wayland client.
514 *
515 * Qt Quick-based Wayland compositors can use this type to display a client's
516 * contents on an output device. It passes user input to the
517 * client.
518 */
519
520/*!
521 * \class QWaylandQuickItem
522 * \inmodule QtWaylandCompositor
523 * \since 5.8
524 * \brief The QWaylandQuickItem class provides a Qt Quick item which represents the surface of a Wayland client.
525 *
526 * When writing a QWaylandCompositor in Qt Quick, this class can be used to display a
527 * client's contents on an output device and will pass user input to the
528 * client.
529 */
530
531/*!
532 * Constructs a QWaylandQuickItem with the given \a parent.
533 */
534QWaylandQuickItem::QWaylandQuickItem(QQuickItem *parent)
535 : QWaylandQuickItem(*new QWaylandQuickItemPrivate(), parent)
536{
537}
538
539/*!
540 * \internal
541 */
542QWaylandQuickItem::QWaylandQuickItem(QWaylandQuickItemPrivate &dd, QQuickItem *parent)
543 : QQuickItem(dd, parent)
544{
545 d_func()->init();
546 connect(this, &QQuickItem::activeFocusChanged, this, &QWaylandQuickItem::updateFocus);
547}
548
549/*!
550 * Destroy the QWaylandQuickItem.
551 */
552QWaylandQuickItem::~QWaylandQuickItem()
553{
554 Q_D(QWaylandQuickItem);
555 disconnect(this, &QQuickItem::windowChanged, this, &QWaylandQuickItem::updateWindow);
556 disconnect(this, &QQuickItem::activeFocusChanged, this, &QWaylandQuickItem::updateFocus);
557 QMutexLocker locker(d->mutex);
558 if (d->provider) {
559 disconnect(d->texProviderConnection);
560 d->provider->deleteLater();
561 }
562}
563
564/*!
565 * \qmlproperty WaylandCompositor QtWayland.Compositor::WaylandQuickItem::compositor
566 *
567 * This property holds the compositor for the surface rendered by this WaylandQuickItem.
568 */
569
570/*!
571 * \property QWaylandQuickItem::compositor
572 *
573 * This property holds the compositor for the surface rendered by this QWaylandQuickItem.
574 */
575QWaylandCompositor *QWaylandQuickItem::compositor() const
576{
577 Q_D(const QWaylandQuickItem);
578 return d->view->surface() ? d->view->surface()->compositor() : nullptr;
579}
580
581/*!
582 * Returns the view rendered by this QWaylandQuickItem.
583 */
584QWaylandView *QWaylandQuickItem::view() const
585{
586 Q_D(const QWaylandQuickItem);
587 return d->view.data();
588}
589
590/*!
591 * \qmlproperty WaylandSurface QtWayland.Compositor::WaylandQuickItem::surface
592 *
593 * This property holds the surface rendered by this WaylandQuickItem.
594 */
595
596/*!
597 * \property QWaylandQuickItem::surface
598 *
599 * This property holds the surface rendered by this QWaylandQuickItem.
600 */
601
602QWaylandSurface *QWaylandQuickItem::surface() const
603{
604 Q_D(const QWaylandQuickItem);
605 return d->view->surface();
606}
607
608void QWaylandQuickItem::setSurface(QWaylandSurface *surface)
609{
610 Q_D(QWaylandQuickItem);
611 QWaylandSurface *oldSurf = d->view->surface();
612 if (oldSurf == surface)
613 return;
614
615 QWaylandCompositor *oldComp = oldSurf ? oldSurf->compositor() : nullptr;
616 QWaylandCompositor *newComp = surface ? surface->compositor() : nullptr;
617 if (oldComp != newComp)
618 emit compositorChanged();
619
620 d->view->setSurface(surface);
621
622 updateFocus();
623 update();
624}
625
626/*!
627 * \qmlproperty enum QtWayland.Compositor::WaylandQuickItem::origin
628 *
629 * This property reflects the origin of the surface's current buffer, that is,
630 * which corner of the buffer corresponds to coordinate (0, 0).
631 *
632 * \sa WaylandSurface::origin
633 */
634
635/*!
636 * \property QWaylandQuickItem::origin
637 *
638 * This property reflects the origin of the surface's current buffer, that is,
639 * which corner of the buffer corresponds to coordinate (0, 0).
640 *
641 * \sa QWaylandSurface::origin
642 */
643QWaylandSurface::Origin QWaylandQuickItem::origin() const
644{
645 Q_D(const QWaylandQuickItem);
646 return d->origin;
647}
648
649bool QWaylandQuickItem::isTextureProvider() const
650{
651 Q_D(const QWaylandQuickItem);
652 return QQuickItem::isTextureProvider() || d->provider;
653}
654
655/*!
656 * Returns the texture provider of this QWaylandQuickItem.
657 */
658QSGTextureProvider *QWaylandQuickItem::textureProvider() const
659{
660 Q_D(const QWaylandQuickItem);
661
662 if (QQuickItem::isTextureProvider())
663 return QQuickItem::textureProvider();
664
665 return d->provider;
666}
667
668/*!
669 * \internal
670 */
671void QWaylandQuickItem::mousePressEvent(QMouseEvent *event)
672{
673 Q_D(QWaylandQuickItem);
674 if (!d->shouldSendInputEvents()) {
675 event->ignore();
676 return;
677 }
678
679 if (!inputRegionContains(event->position())) {
680 event->ignore();
681 return;
682 }
683
684 QWaylandSeat *seat = compositor()->seatFor(event);
685
686 if (d->focusOnClick)
687 takeFocus(seat);
688
689 seat->sendMouseMoveEvent(d->view.data(), mapToSurface(event->position()), event->scenePosition());
690 seat->sendMousePressEvent(event->button());
691 d->hoverPos = event->position();
692}
693
694/*!
695 * \internal
696 */
697void QWaylandQuickItem::mouseMoveEvent(QMouseEvent *event)
698{
699 Q_D(QWaylandQuickItem);
700 if (d->shouldSendInputEvents()) {
701 QWaylandSeat *seat = compositor()->seatFor(event);
702 if (d->isDragging) {
703 d->handleDragUpdate(seat, event->position());
704 } else {
705 seat->sendMouseMoveEvent(d->view.data(), mapToSurface(event->position()), event->scenePosition());
706 d->hoverPos = event->position();
707 }
708 } else {
709 emit mouseMove(event->scenePosition());
710 event->ignore();
711 }
712}
713
714/*!
715 * \internal
716 */
717void QWaylandQuickItem::mouseReleaseEvent(QMouseEvent *event)
718{
719 Q_D(QWaylandQuickItem);
720 if (d->shouldSendInputEvents()) {
721 QWaylandSeat *seat = compositor()->seatFor(event);
722 if (d->isDragging)
723 d->handleDragEnded(seat);
724 else
725 seat->sendMouseReleaseEvent(event->button());
726 } else {
727 emit mouseRelease();
728 event->ignore();
729 }
730}
731
732/*!
733 * \internal
734 */
735void QWaylandQuickItem::hoverEnterEvent(QHoverEvent *event)
736{
737 Q_D(QWaylandQuickItem);
738 if (!inputRegionContains(event->position())) {
739 event->ignore();
740 return;
741 }
742 if (d->shouldSendInputEvents()) {
743 QWaylandSeat *seat = compositor()->seatFor(event);
744 seat->sendMouseMoveEvent(d->view.data(), event->position(), mapToScene(event->position()));
745 d->hoverPos = event->position();
746 } else {
747 event->ignore();
748 }
749}
750
751/*!
752 * \internal
753 */
754void QWaylandQuickItem::hoverMoveEvent(QHoverEvent *event)
755{
756 Q_D(QWaylandQuickItem);
757 if (surface()) {
758 if (!inputRegionContains(event->position())) {
759 event->ignore();
760 return;
761 }
762 }
763 if (d->shouldSendInputEvents()) {
764 QWaylandSeat *seat = compositor()->seatFor(event);
765 if (event->position() != d->hoverPos) {
766 seat->sendMouseMoveEvent(d->view.data(), mapToSurface(event->position()), mapToScene(event->position()));
767 d->hoverPos = event->position();
768 }
769 } else {
770 event->ignore();
771 }
772}
773
774/*!
775 * \internal
776 */
777void QWaylandQuickItem::hoverLeaveEvent(QHoverEvent *event)
778{
779 Q_D(QWaylandQuickItem);
780 if (d->shouldSendInputEvents()) {
781 QWaylandSeat *seat = compositor()->seatFor(event);
782 if (d->view.data() == seat->mouseFocus()) {
783 seat->setMouseFocus(nullptr);
784 return;
785 }
786 }
787
788 event->ignore();
789}
790
791#if QT_CONFIG(wheelevent)
792/*!
793 * \internal
794 */
795void QWaylandQuickItem::wheelEvent(QWheelEvent *event)
796{
797 Q_D(QWaylandQuickItem);
798 if (d->shouldSendInputEvents()) {
799 if (!inputRegionContains(event->position())) {
800 event->ignore();
801 return;
802 }
803
804 QWaylandSeat *seat = compositor()->seatFor(event);
805 // TODO: fix this to send a single event, when diagonal scrolling is supported
806 if (event->angleDelta().x() != 0)
807 seat->sendMouseWheelEvent(Qt::Horizontal, event->angleDelta().x());
808 if (event->angleDelta().y() != 0)
809 seat->sendMouseWheelEvent(Qt::Vertical, event->angleDelta().y());
810 } else {
811 event->ignore();
812 }
813}
814#endif
815
816/*!
817 * \internal
818 */
819void QWaylandQuickItem::keyPressEvent(QKeyEvent *event)
820{
821 Q_D(QWaylandQuickItem);
822 if (d->shouldSendInputEvents()) {
823 QWaylandSeat *seat = compositor()->seatFor(event);
824 if (seat->setKeyboardFocus(d->view->surface()))
825 seat->sendFullKeyEvent(event);
826 else
827 qWarning() << "Unable to set keyboard focus, cannot send key press event";
828 } else {
829 event->ignore();
830 }
831}
832
833/*!
834 * \internal
835 */
836void QWaylandQuickItem::keyReleaseEvent(QKeyEvent *event)
837{
838 Q_D(QWaylandQuickItem);
839 if (d->shouldSendInputEvents()) {
840 QWaylandSeat *seat = compositor()->seatFor(event);
841 seat->sendFullKeyEvent(event);
842 } else {
843 event->ignore();
844 }
845}
846
847/*!
848 * \internal
849 */
850void QWaylandQuickItem::touchEvent(QTouchEvent *event)
851{
852 Q_D(QWaylandQuickItem);
853 if (d->shouldSendInputEvents() && d->touchEventsEnabled) {
854 QWaylandSeat *seat = compositor()->seatFor(event);
855
856 QPointF pointPos;
857 const QList<QTouchEvent::TouchPoint> &points = event->points();
858 if (!points.isEmpty())
859 pointPos = points.at(0).position();
860
861 if (event->type() == QEvent::TouchBegin && !inputRegionContains(pointPos)) {
862 event->ignore();
863 return;
864 }
865
866 if (event->type() == QEvent::TouchUpdate && d->isDragging)
867 d->handleDragUpdate(seat, pointPos);
868
869 event->accept();
870 if (seat->mouseFocus() != d->view.data()) {
871 seat->sendMouseMoveEvent(d->view.data(), pointPos, mapToScene(pointPos));
872 }
873 seat->sendFullTouchEvent(surface(), event);
874
875 if (event->type() == QEvent::TouchBegin) {
876 d->touchingSeats.append(seat);
877 } else if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) {
878 if (d->isDragging)
879 d->handleDragEnded(seat);
880 d->touchingSeats.removeOne(seat);
881 }
882
883 if (event->type() == QEvent::TouchBegin && d->focusOnClick)
884 takeFocus(seat);
885 } else {
886 event->ignore();
887 }
888}
889
890void QWaylandQuickItem::touchUngrabEvent()
891{
892 Q_D(QWaylandQuickItem);
893
894 if (d->shouldSendInputEvents()) {
895 for (auto seat : std::as_const(d->touchingSeats))
896 seat->sendTouchCancelEvent(surface()->client());
897 }
898
899 d->touchingSeats.clear();
900}
901
902#if QT_CONFIG(im)
903/*!
904 * \internal
905 */
906void QWaylandQuickItem::inputMethodEvent(QInputMethodEvent *event)
907{
908 Q_D(QWaylandQuickItem);
909 if (d->shouldSendInputEvents()) {
910 d->oldSurface->inputMethodControl()->inputMethodEvent(event);
911 } else {
912 event->ignore();
913 }
914}
915#endif
916
917/*!
918 * \internal
919 */
920void QWaylandQuickItem::surfaceChangedEvent(QWaylandSurface *newSurface, QWaylandSurface *oldSurface)
921{
922 Q_UNUSED(newSurface);
923 Q_UNUSED(oldSurface);
924}
925
926void QWaylandQuickItem::handleSubsurfaceAdded(QWaylandSurface *childSurface)
927{
928 Q_D(QWaylandQuickItem);
929 if (d->subsurfaceHandler.isNull()) {
930 QWaylandQuickItem *childItem = new QWaylandQuickItem;
931 childItem->setSurface(childSurface);
932 childItem->setVisible(true);
933 childItem->setParentItem(this);
934 childItem->setParent(this);
935 childItem->setPosition(QWaylandSurfacePrivate::get(childSurface)->subsurfacePosition() * d->scaleFactor());
936 connect(childSurface, &QWaylandSurface::subsurfacePositionChanged, childItem, &QWaylandQuickItem::handleSubsurfacePosition);
937 connect(childSurface, &QWaylandSurface::destroyed, childItem, &QObject::deleteLater);
938 } else {
939 bool success = QMetaObject::invokeMethod(d->subsurfaceHandler, "handleSubsurfaceAdded", Q_ARG(QWaylandSurface *, childSurface));
940 if (!success)
941 success = QMetaObject::invokeMethod(d->subsurfaceHandler, "handleSubsurfaceAdded",
942 Q_ARG(QVariant, QVariant::fromValue(childSurface)));
943
944 if (!success)
945 qWarning("QWaylandQuickItem: subsurfaceHandler does not implement handleSubsurfaceAdded()");
946 }
947}
948
949void QWaylandQuickItem::handlePlaceAbove(QWaylandSurface *referenceSurface)
950{
951 Q_D(QWaylandQuickItem);
952 auto *parent = qobject_cast<QWaylandQuickItem*>(parentItem());
953 if (!parent)
954 return;
955
956 if (parent->surface() == referenceSurface) {
957 d->placeAboveParent();
958 } else if (auto *sibling = d->findSibling(referenceSurface)) {
959 d->placeAboveSibling(sibling);
960 } else {
961 qWarning() << "Couldn't find QWaylandQuickItem for surface" << referenceSurface
962 << "when handling wl_subsurface.place_above";
963 }
964}
965
966void QWaylandQuickItem::handlePlaceBelow(QWaylandSurface *referenceSurface)
967{
968 Q_D(QWaylandQuickItem);
969 QWaylandQuickItem *parent = qobject_cast<QWaylandQuickItem*>(parentItem());
970 if (!parent)
971 return;
972
973 if (parent->surface() == referenceSurface) {
974 d->placeBelowParent();
975 } else if (auto *sibling = d->findSibling(referenceSurface)) {
976 d->placeBelowSibling(sibling);
977 } else {
978 qWarning() << "Couldn't find QWaylandQuickItem for surface" << referenceSurface
979 << "when handling wl_subsurface.place_below";
980 }
981}
982
983void QWaylandQuickItem::updateFocus()
984{
985 Q_D(const QWaylandQuickItem);
986 if (hasActiveFocus() && compositor())
987 compositor()->defaultSeat()->setKeyboardFocus(d->view->surface());
988}
989
990/*!
991 \qmlproperty object QtWayland.Compositor::WaylandQuickItem::subsurfaceHandler
992
993 This property provides a way to override the default subsurface behavior.
994
995 By default, Qt will create a new SurfaceItem as a child of this item, and maintain the correct position.
996
997 To override the default, assign a handler object to this property. The handler should implement
998 a handleSubsurfaceAdded(WaylandSurface) function.
999
1000 \code
1001 ShellSurfaceItem {
1002 subsurfaceHandler: QtObject {
1003 function handleSubsurfaceAdded(child) {
1004 // create custom surface item, and connect the subsurfacePositionChanged signal
1005 }
1006 }
1007 }
1008 \endcode
1009
1010 The default value of this property is \c null.
1011 */
1012
1013/*!
1014 * \property QWaylandQuickItem::subsurfaceHandler
1015 *
1016 * This property provides a way to override the default subsurface behavior.
1017 *
1018 * By default, Qt will create a new QWaylandQuickItem as a child of this item
1019 * and maintain the correct position.
1020 *
1021 * To override the default behavior, assign a handler object to this property.
1022 * The handler must implement a \c handleSubsurfaceAdded(QWaylandSurface*)
1023 * method.
1024 *
1025 * The default value of this property is \nullptr.
1026 */
1027QObject *QWaylandQuickItem::subsurfaceHandler() const
1028{
1029 Q_D(const QWaylandQuickItem);
1030 return d->subsurfaceHandler.data();
1031}
1032
1033void QWaylandQuickItem::setSubsurfaceHandler(QObject *handler)
1034{
1035 Q_D(QWaylandQuickItem);
1036 if (d->subsurfaceHandler.data() != handler) {
1037 d->subsurfaceHandler = handler;
1038 emit subsurfaceHandlerChanged();
1039 }
1040}
1041
1042/*!
1043 * \qmlproperty WaylandOutput QtWayland.Compositor::WaylandQuickItem::output
1044 *
1045 * This property holds the output on which this item is displayed.
1046 */
1047/*!
1048 * \property QWaylandQuickItem::output
1049 *
1050 * This property holds the output on which this item is displayed.
1051 */
1052QWaylandOutput *QWaylandQuickItem::output() const
1053{
1054 Q_D(const QWaylandQuickItem);
1055 return d->view->output();
1056}
1057
1058void QWaylandQuickItem::setOutput(QWaylandOutput *output)
1059{
1060 Q_D(QWaylandQuickItem);
1061 d->view->setOutput(output);
1062}
1063
1064/*!
1065 * \qmlproperty bool QtWayland.Compositor::WaylandQuickItem::bufferLocked
1066 *
1067 * This property holds whether the item's buffer is currently locked. As long as
1068 * the buffer is locked, it will not be released and returned to the client.
1069 *
1070 * The default is false.
1071 */
1072/*!
1073 * \property QWaylandQuickItem::bufferLocked
1074 *
1075 * This property holds whether the item's buffer is currently locked. As long as
1076 * the buffer is locked, it will not be released and returned to the client.
1077 *
1078 * The default is false.
1079 */
1080bool QWaylandQuickItem::isBufferLocked() const
1081{
1082 Q_D(const QWaylandQuickItem);
1083 return d->view->isBufferLocked();
1084}
1085
1086void QWaylandQuickItem::setBufferLocked(bool locked)
1087{
1088 Q_D(QWaylandQuickItem);
1089 d->view->setBufferLocked(locked);
1090}
1091
1092/*!
1093 * \property QWaylandQuickItem::allowDiscardFrontBuffer
1094 *
1095 * By default, the item locks the current buffer until a new buffer is available
1096 * and updatePaintNode() is called. Set this property to true to allow Qt to release the buffer
1097 * immediately when the throttling view is no longer using it. This is useful for items that have
1098 * slow update intervals.
1099 */
1100bool QWaylandQuickItem::allowDiscardFrontBuffer() const
1101{
1102 Q_D(const QWaylandQuickItem);
1103 return d->view->allowDiscardFrontBuffer();
1104}
1105
1106void QWaylandQuickItem::setAllowDiscardFrontBuffer(bool discard)
1107{
1108 Q_D(QWaylandQuickItem);
1109 d->view->setAllowDiscardFrontBuffer(discard);
1110}
1111
1112/*!
1113 * \qmlmethod void WaylandQuickItem::setPrimary()
1114 *
1115 * Makes this WaylandQuickItem the primary view for the surface.
1116 */
1117
1118/*!
1119 * Makes this QWaylandQuickItem's view the primary view for the surface.
1120 *
1121 * \sa QWaylandSurface::primaryView
1122 */
1123void QWaylandQuickItem::setPrimary()
1124{
1125 Q_D(QWaylandQuickItem);
1126 d->view->setPrimary();
1127}
1128
1129/*!
1130 * \internal
1131 */
1132void QWaylandQuickItem::handleSurfaceChanged()
1133{
1134 Q_D(QWaylandQuickItem);
1135 if (d->oldSurface) {
1136 disconnect(d->oldSurface.data(), &QWaylandSurface::parentChanged, this, &QWaylandQuickItem::parentChanged);
1137 disconnect(d->oldSurface.data(), &QWaylandSurface::destinationSizeChanged, this, &QWaylandQuickItem::updateSize);
1138 disconnect(d->oldSurface.data(), &QWaylandSurface::bufferScaleChanged, this, &QWaylandQuickItem::updateSize);
1139 disconnect(d->oldSurface.data(), &QWaylandSurface::configure, this, &QWaylandQuickItem::updateBuffer);
1140 disconnect(d->oldSurface.data(), &QWaylandSurface::redraw, this, &QQuickItem::update);
1141 disconnect(d->oldSurface.data(), &QWaylandSurface::childAdded, this, &QWaylandQuickItem::handleSubsurfaceAdded);
1142 disconnect(d->oldSurface.data(), &QWaylandSurface::subsurfacePlaceAbove, this, &QWaylandQuickItem::handlePlaceAbove);
1143 disconnect(d->oldSurface.data(), &QWaylandSurface::subsurfacePlaceBelow, this, &QWaylandQuickItem::handlePlaceBelow);
1144#if QT_CONFIG(draganddrop)
1145 disconnect(d->oldSurface.data(), &QWaylandSurface::dragStarted, this, &QWaylandQuickItem::handleDragStarted);
1146#endif
1147#if QT_CONFIG(im)
1148 disconnect(d->oldSurface->inputMethodControl(), &QWaylandInputMethodControl::updateInputMethod, this, &QWaylandQuickItem::updateInputMethod);
1149#endif
1150 }
1151 if (QWaylandSurface *newSurface = d->view->surface()) {
1152 connect(newSurface, &QWaylandSurface::parentChanged, this, &QWaylandQuickItem::parentChanged);
1153 connect(newSurface, &QWaylandSurface::destinationSizeChanged, this, &QWaylandQuickItem::updateSize);
1154 connect(newSurface, &QWaylandSurface::bufferScaleChanged, this, &QWaylandQuickItem::updateSize);
1155 connect(newSurface, &QWaylandSurface::configure, this, &QWaylandQuickItem::updateBuffer);
1156 connect(newSurface, &QWaylandSurface::redraw, this, &QQuickItem::update);
1157 connect(newSurface, &QWaylandSurface::childAdded, this, &QWaylandQuickItem::handleSubsurfaceAdded);
1158 connect(newSurface, &QWaylandSurface::subsurfacePlaceAbove, this, &QWaylandQuickItem::handlePlaceAbove);
1159 connect(newSurface, &QWaylandSurface::subsurfacePlaceBelow, this, &QWaylandQuickItem::handlePlaceBelow);
1160#if QT_CONFIG(draganddrop)
1161 connect(newSurface, &QWaylandSurface::dragStarted, this, &QWaylandQuickItem::handleDragStarted);
1162#endif
1163#if QT_CONFIG(im)
1164 connect(newSurface->inputMethodControl(), &QWaylandInputMethodControl::updateInputMethod, this, &QWaylandQuickItem::updateInputMethod);
1165#endif
1166
1167 if (newSurface->origin() != d->origin) {
1168 d->origin = newSurface->origin();
1169 emit originChanged();
1170 }
1171 if (window()) {
1172 QWaylandOutput *output = newSurface->compositor()->outputFor(window());
1173 d->view->setOutput(output);
1174 }
1175 for (const auto &subsurface : std::as_const(QWaylandSurfacePrivate::get(newSurface)->subsurfaceChildren)) {
1176 if (!subsurface.isNull())
1177 handleSubsurfaceAdded(subsurface.data());
1178 }
1179
1180 updateSize();
1181 }
1182 surfaceChangedEvent(d->view->surface(), d->oldSurface);
1183 d->oldSurface = d->view->surface();
1184#if QT_CONFIG(im)
1185 updateInputMethod(Qt::ImQueryInput);
1186#endif
1187}
1188
1189/*!
1190 * Calling this function causes the item to take the focus of the
1191 * input \a device.
1192 */
1193void QWaylandQuickItem::takeFocus(QWaylandSeat *device)
1194{
1195 forceActiveFocus();
1196
1197 if (!surface() || !surface()->client())
1198 return;
1199
1200 QWaylandSeat *target = device;
1201 if (!target) {
1202 target = compositor()->defaultSeat();
1203 }
1204 target->setKeyboardFocus(surface());
1205
1206 qCDebug(qLcWaylandCompositorInputMethods) << Q_FUNC_INFO << " surface:" << surface()
1207 << ", client:" << surface()->client()
1208 << ", textinputprotocol:" << (int)(surface()->client()->textInputProtocols());
1209 if (surface()->client()->textInputProtocols().testFlag(QWaylandClient::TextInputProtocol::TextInputV2)) {
1210 QWaylandTextInput *textInput = QWaylandTextInput::findIn(target);
1211 if (textInput)
1212 textInput->setFocus(surface());
1213 }
1214
1215 if (surface()->client()->textInputProtocols().testFlag(QWaylandClient::TextInputProtocol::TextInputV3)) {
1216 QWaylandTextInputV3 *textInputV3 = QWaylandTextInputV3::findIn(target);
1217 if (textInputV3)
1218 textInputV3->setFocus(surface());
1219 }
1220
1221 if (surface()->client()->textInputProtocols().testFlag(QWaylandClient::TextInputProtocol::QtTextInputMethodV1)) {
1222 QWaylandQtTextInputMethod *textInputMethod = QWaylandQtTextInputMethod::findIn(target);
1223 if (textInputMethod)
1224 textInputMethod->setFocus(surface());
1225 }
1226}
1227
1228/*!
1229 * \internal
1230 */
1231void QWaylandQuickItem::handleBufferLockedChanged()
1232{
1233 Q_D(QWaylandQuickItem);
1234
1235 // Apply the latest surface size
1236 if (!d->view->isBufferLocked()) {
1237 updateSize();
1238 update();
1239 }
1240}
1241
1242#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
1243void QWaylandQuickItem::surfaceMappedChanged()
1244{
1245 update();
1246}
1247#endif
1248
1249/*!
1250 * \internal
1251 */
1252void QWaylandQuickItem::parentChanged(QWaylandSurface *newParent, QWaylandSurface *oldParent)
1253{
1254 Q_UNUSED(oldParent);
1255
1256 if (newParent) {
1257 setPaintEnabled(true);
1258 setVisible(true);
1259 setOpacity(1);
1260 setEnabled(true);
1261 }
1262}
1263
1264/*!
1265 * \internal
1266 */
1267void QWaylandQuickItem::updateSize()
1268{
1269 Q_D(QWaylandQuickItem);
1270
1271 // No resize if buffer is locked
1272 if (isBufferLocked()) {
1273 qWarning() << "No update on item size as the buffer is currently locked";
1274 return;
1275 }
1276
1277 QSize size(0, 0);
1278 if (surface())
1279 size = surface()->destinationSize() * d->scaleFactor();
1280
1281 setImplicitSize(size.width(), size.height());
1282}
1283
1284/*!
1285 * \qmlproperty bool QtWayland.Compositor::WaylandQuickItem::focusOnClick
1286 *
1287 * This property specifies whether the WaylandQuickItem should take focus when
1288 * it is clicked or touched.
1289 *
1290 * The default is \c true.
1291 */
1292
1293/*!
1294 * \property QWaylandQuickItem::focusOnClick
1295 *
1296 * This property specifies whether the QWaylandQuickItem should take focus when
1297 * it is clicked or touched.
1298 *
1299 * The default is \c true.
1300 */
1301bool QWaylandQuickItem::focusOnClick() const
1302{
1303 Q_D(const QWaylandQuickItem);
1304 return d->focusOnClick;
1305}
1306
1307void QWaylandQuickItem::setFocusOnClick(bool focus)
1308{
1309 Q_D(QWaylandQuickItem);
1310 if (d->focusOnClick == focus)
1311 return;
1312
1313 d->focusOnClick = focus;
1314 emit focusOnClickChanged();
1315}
1316
1317/*!
1318 * Returns \c true if the input region of this item's surface contains the
1319 * position given by \a localPosition.
1320 */
1321bool QWaylandQuickItem::inputRegionContains(const QPointF &localPosition) const
1322{
1323 if (QWaylandSurface *s = surface())
1324 return s->inputRegionContains(mapToSurface(localPosition));
1325 return false;
1326}
1327
1328/*!
1329 * \qmlmethod point WaylandQuickItem::mapToSurface(point point)
1330 *
1331 * Maps the given \a point in this item's coordinate system to the equivalent
1332 * point within the Wayland surface's coordinate system, and returns the mapped
1333 * coordinate.
1334 */
1335
1336/*!
1337 * Maps the given \a point in this item's coordinate system to the equivalent
1338 * point within the Wayland surface's coordinate system, and returns the mapped
1339 * coordinate.
1340 */
1341QPointF QWaylandQuickItem::mapToSurface(const QPointF &point) const
1342{
1343 Q_D(const QWaylandQuickItem);
1344 if (!surface() || surface()->destinationSize().isEmpty())
1345 return point / d->scaleFactor();
1346
1347 qreal xScale = width() / surface()->destinationSize().width();
1348 qreal yScale = height() / surface()->destinationSize().height();
1349
1350 return QPointF(point.x() / xScale, point.y() / yScale);
1351}
1352
1353/*!
1354 * \qmlmethod point WaylandQuickItem::mapFromSurface(point point)
1355 * \since 5.13
1356 *
1357 * Maps the given \a point in the Wayland surfaces's coordinate system to the equivalent
1358 * point within this item's coordinate system, and returns the mapped coordinate.
1359 */
1360
1361/*!
1362 * Maps the given \a point in the Wayland surfaces's coordinate system to the equivalent
1363 * point within this item's coordinate system, and returns the mapped coordinate.
1364 *
1365 * \since 5.13
1366 */
1367QPointF QWaylandQuickItem::mapFromSurface(const QPointF &point) const
1368{
1369 Q_D(const QWaylandQuickItem);
1370 if (!surface() || surface()->destinationSize().isEmpty())
1371 return point * d->scaleFactor();
1372
1373 qreal xScale = width() / surface()->destinationSize().width();
1374 qreal yScale = height() / surface()->destinationSize().height();
1375
1376 return QPointF(point.x() * xScale, point.y() * yScale);
1377}
1378
1379#if QT_CONFIG(im)
1380QVariant QWaylandQuickItem::inputMethodQuery(Qt::InputMethodQuery query) const
1381{
1382 return inputMethodQuery(query, QVariant());
1383}
1384
1385QVariant QWaylandQuickItem::inputMethodQuery(Qt::InputMethodQuery query, QVariant argument) const
1386{
1387 Q_D(const QWaylandQuickItem);
1388
1389 if (query == Qt::ImEnabled)
1390 return QVariant((flags() & ItemAcceptsInputMethod) != 0);
1391
1392 if (d->oldSurface)
1393 return d->oldSurface->inputMethodControl()->inputMethodQuery(query, argument);
1394
1395 return QVariant();
1396}
1397#endif
1398
1399/*!
1400 \qmlproperty bool QtWayland.Compositor::WaylandQuickItem::paintEnabled
1401 \default true
1402
1403 Setting this property to \c false will stop rendering the item. As opposed
1404 to hiding the item by setting \l{Item::visible}{visible} to \c false, it
1405 will not prevent mouse or keyboard input from reaching the item.
1406*/
1407
1408/*!
1409 \property QWaylandQuickItem::paintEnabled
1410
1411 Setting this property to \c false will stop rendering the item. As opposed
1412 to hiding the item by setting \l{Item::visible}{visible} to \c false, it
1413 will not prevent mouse or keyboard input from reaching the item.
1414*/
1415bool QWaylandQuickItem::isPaintEnabled() const
1416{
1417 Q_D(const QWaylandQuickItem);
1418 return d->paintEnabled;
1419}
1420
1421void QWaylandQuickItem::setPaintEnabled(bool enabled)
1422{
1423 Q_D(QWaylandQuickItem);
1424
1425 if (enabled != d->paintEnabled) {
1426 d->paintEnabled = enabled;
1427 emit paintEnabledChanged();
1428 }
1429
1430 update();
1431}
1432
1433/*!
1434 \qmlproperty bool QtWayland.Compositor::WaylandQuickItem::touchEventsEnabled
1435
1436 This property holds \c true if touch events are forwarded to the client
1437 surface, \c false otherwise.
1438*/
1439
1440/*!
1441 \property QWaylandQuickItem::touchEventsEnabled
1442
1443 This property holds \c true if touch events are forwarded to the client
1444 surface, \c false otherwise.
1445*/
1446bool QWaylandQuickItem::touchEventsEnabled() const
1447{
1448 Q_D(const QWaylandQuickItem);
1449 return d->touchEventsEnabled;
1450}
1451
1452void QWaylandQuickItem::updateBuffer(bool hasBuffer)
1453{
1454 Q_D(QWaylandQuickItem);
1455 Q_UNUSED(hasBuffer);
1456 if (d->origin != surface()->origin()) {
1457 d->origin = surface()->origin();
1458 emit originChanged();
1459 }
1460}
1461
1462void QWaylandQuickItem::updateWindow()
1463{
1464 Q_D(QWaylandQuickItem);
1465
1466 QQuickWindow *newWindow = window();
1467 if (newWindow == d->connectedWindow)
1468 return;
1469
1470 if (d->connectedWindow) {
1471 disconnect(d->connectedWindow, &QQuickWindow::beforeSynchronizing, this, &QWaylandQuickItem::beforeSync);
1472 disconnect(d->connectedWindow, &QQuickWindow::screenChanged, this, &QWaylandQuickItem::updateSize);
1473 }
1474
1475 d->connectedWindow = newWindow;
1476
1477 if (d->connectedWindow) {
1478 connect(d->connectedWindow, &QQuickWindow::beforeSynchronizing, this, &QWaylandQuickItem::beforeSync, Qt::DirectConnection);
1479 connect(d->connectedWindow, &QQuickWindow::screenChanged, this, &QWaylandQuickItem::updateSize); // new screen may have new dpr
1480
1481 if (compositor()) {
1482 QWaylandOutput *output = compositor()->outputFor(d->connectedWindow);
1483 Q_ASSERT(output);
1484 d->view->setOutput(output);
1485 }
1486
1487 updateSize(); // because scaleFactor depends on devicePixelRatio, which may be different for the new window
1488 }
1489}
1490
1491void QWaylandQuickItem::updateOutput()
1492{
1493 Q_D(QWaylandQuickItem);
1494 if (d->view->output() == d->connectedOutput)
1495 return;
1496
1497 if (d->connectedOutput)
1498 disconnect(d->connectedOutput, &QWaylandOutput::scaleFactorChanged, this, &QWaylandQuickItem::updateSize);
1499
1500 d->connectedOutput = d->view->output();
1501
1502 if (d->connectedOutput)
1503 connect(d->connectedOutput, &QWaylandOutput::scaleFactorChanged, this, &QWaylandQuickItem::updateSize);
1504
1505 updateSize();
1506}
1507
1508void QWaylandQuickItem::beforeSync()
1509{
1510 Q_D(QWaylandQuickItem);
1511 if (d->view->advance()) {
1512 d->newTexture = true;
1513 update();
1514 }
1515}
1516
1517#if QT_CONFIG(im)
1518void QWaylandQuickItem::updateInputMethod(Qt::InputMethodQueries queries)
1519{
1520 Q_D(QWaylandQuickItem);
1521
1522 setFlag(QQuickItem::ItemAcceptsInputMethod,
1523 d->oldSurface ? d->oldSurface->inputMethodControl()->enabled() : false);
1524 QQuickItem::updateInputMethod(queries | Qt::ImEnabled);
1525}
1526#endif
1527
1528/*!
1529 * \qmlsignal void QtWayland.Compositor::WaylandQuickItem::surfaceDestroyed()
1530 *
1531 * This signal is emitted when the client has destroyed the \c wl_surface associated
1532 * with the WaylandQuickItem. The handler for this signal is expected to either destroy the
1533 * WaylandQuickItem immediately or start a close animation and then destroy the Item.
1534 *
1535 * If an animation is started, bufferLocked should be set to ensure the item keeps its content
1536 * until the animation finishes
1537 *
1538 * \sa bufferLocked
1539 */
1540
1541/*!
1542 * \fn void QWaylandQuickItem::surfaceDestroyed()
1543 *
1544 * This signal is emitted when the client has destroyed the \c wl_surface associated
1545 * with the QWaylandQuickItem. The handler for this signal is expected to either destroy the
1546 * QWaylandQuickItem immediately or start a close animation and then destroy the Item.
1547 *
1548 * If an animation is started, bufferLocked should be set to ensure the item keeps its content
1549 * until the animation finishes
1550 *
1551 * \sa QWaylandQuickItem::bufferLocked
1552 */
1553
1554QSGNode *QWaylandQuickItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data)
1555{
1556 Q_D(QWaylandQuickItem);
1557 d->lastMatrix = data->transformNode->combinedMatrix();
1558 const bool bufferHasContent = d->view->currentBuffer().hasContent();
1559
1560 if (d->view->isBufferLocked() && d->paintEnabled)
1561 return oldNode;
1562
1563 if (!bufferHasContent || !d->paintEnabled || !surface()) {
1564 delete oldNode;
1565 return nullptr;
1566 }
1567
1568 QWaylandBufferRef ref = d->view->currentBuffer();
1569 const bool invertY = ref.origin() == QWaylandSurface::OriginBottomLeft;
1570 const QRectF rect = invertY ? QRectF(0, height(), width(), -height())
1571 : QRectF(0, 0, width(), height());
1572
1573 if (ref.isSharedMemory()
1574#if QT_CONFIG(opengl)
1575 || bufferTypes[ref.bufferFormatEgl()].canProvideTexture
1576#endif
1577 ) {
1578#if QT_CONFIG(opengl)
1579 if (oldNode && !d->paintByProvider) {
1580 // Need to re-create a node
1581 delete oldNode;
1582 oldNode = nullptr;
1583 }
1584 d->paintByProvider = true;
1585#endif
1586 // This case could covered by the more general path below, but this is more efficient (especially when using ShaderEffect items).
1587 QSGSimpleTextureNode *node = static_cast<QSGSimpleTextureNode *>(oldNode);
1588
1589 if (!node) {
1590 node = new QSGSimpleTextureNode();
1591 if (smooth())
1592 node->setFiltering(QSGTexture::Linear);
1593 d->newTexture = true;
1594 }
1595
1596 if (!d->provider) {
1597 d->provider = new QWaylandSurfaceTextureProvider();
1598 if (auto *c = compositor()) {
1599 d->texProviderConnection =
1600 QObject::connect(c, &QObject::destroyed, this, [d = d] {
1601 if (d->provider) {
1602 d->provider->reportCompositorDestroyed();
1603 d->provider = nullptr;
1604 }
1605 QObject::disconnect(d->texProviderConnection);
1606 });
1607 }
1608 }
1609
1610 if (d->newTexture) {
1611 d->newTexture = false;
1612 d->provider->setBufferRef(this, ref);
1613 node->setTexture(d->provider->texture());
1614 }
1615
1616 d->provider->setSmooth(smooth());
1617 node->setRect(rect);
1618
1619 qreal scale = surface()->bufferScale();
1620 QRectF source = surface()->sourceGeometry();
1621 node->setSourceRect(QRectF(source.topLeft() * scale, source.size() * scale));
1622
1623 return node;
1624 }
1625
1626#if QT_CONFIG(opengl)
1627 Q_ASSERT(!d->provider);
1628
1629 if (oldNode && d->paintByProvider) {
1630 // Need to re-create a node
1631 delete oldNode;
1632 oldNode = nullptr;
1633 }
1634 d->paintByProvider = false;
1635
1636 QSGGeometryNode *node = static_cast<QSGGeometryNode *>(oldNode);
1637
1638 if (!node) {
1639 node = new QSGGeometryNode;
1640 d->newTexture = true;
1641 }
1642
1643 QSGGeometry *geometry = node->geometry();
1644 QWaylandBufferMaterial *material = static_cast<QWaylandBufferMaterial *>(node->material());
1645
1646 if (!geometry)
1647 geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4);
1648
1649 if (!material)
1650 material = new QWaylandBufferMaterial(ref.bufferFormatEgl());
1651
1652 if (d->newTexture) {
1653 d->newTexture = false;
1654 material->setBufferRef(this, ref);
1655 }
1656
1657 const QSize surfaceSize = ref.size() / surface()->bufferScale();
1658 const QRectF sourceGeometry = surface()->sourceGeometry();
1659 const QRectF normalizedCoordinates =
1660 sourceGeometry.isValid()
1661 ? QRectF(sourceGeometry.x() / surfaceSize.width(),
1662 sourceGeometry.y() / surfaceSize.height(),
1663 sourceGeometry.width() / surfaceSize.width(),
1664 sourceGeometry.height() / surfaceSize.height())
1665 : QRectF(0, 0, 1, 1);
1666
1667 QSGGeometry::updateTexturedRectGeometry(geometry, rect, normalizedCoordinates);
1668
1669 node->setGeometry(geometry);
1670 node->setFlag(QSGNode::OwnsGeometry, true);
1671
1672 node->setMaterial(material);
1673 node->setFlag(QSGNode::OwnsMaterial, true);
1674
1675 return node;
1676#else
1677 qCWarning(qLcWaylandCompositor) << "Without OpenGL support only shared memory textures are supported";
1678 return nullptr;
1679#endif // QT_CONFIG(opengl)
1680}
1681
1682void QWaylandQuickItem::setTouchEventsEnabled(bool enabled)
1683{
1684 Q_D(QWaylandQuickItem);
1685 if (d->touchEventsEnabled != enabled) {
1686 d->touchEventsEnabled = enabled;
1687 emit touchEventsEnabledChanged();
1688 }
1689}
1690
1691/*!
1692 * \qmlproperty bool QtWayland.Compositor::WaylandQuickItem::inputEventsEnabled
1693 *
1694 * This property holds whether input events are enabled for this item.
1695 *
1696 * When \c true, the item will process and handle input events such as keyboard
1697 * and mouse events; when \c false, input events are ignored.
1698 */
1699
1700/*!
1701 * \property QWaylandQuickItem::inputEventsEnabled
1702 *
1703 * This property holds whether input events are enabled for this item.
1704 *
1705 * When \c true, the item will process and handle input events such as keyboard
1706 * and mouse events; when \c false, input events are ignored.
1707 */
1708bool QWaylandQuickItem::inputEventsEnabled() const
1709{
1710 Q_D(const QWaylandQuickItem);
1711 return d->inputEventsEnabled;
1712}
1713
1714void QWaylandQuickItem::setInputEventsEnabled(bool enabled)
1715{
1716 Q_D(QWaylandQuickItem);
1717 if (d->inputEventsEnabled != enabled) {
1718 if (enabled)
1719 setEnabled(true);
1720 d->setInputEventsEnabled(enabled);
1721 emit inputEventsEnabledChanged();
1722 }
1723}
1724
1725void QWaylandQuickItem::lower()
1726{
1727 Q_D(QWaylandQuickItem);
1728 d->lower();
1729}
1730
1732{
1733 Q_Q(QWaylandQuickItem);
1734 QQuickItem *parent = q->parentItem();
1735 Q_ASSERT(parent);
1736 QQuickItem *bottom = parent->childItems().constFirst();
1737 if (q != bottom)
1738 q->stackBefore(bottom);
1739}
1740
1741void QWaylandQuickItem::raise()
1742{
1743 Q_D(QWaylandQuickItem);
1744 d->raise();
1745}
1746
1748{
1749 Q_Q(QWaylandQuickItem);
1750 QQuickItem *parent = q->parentItem();
1751 Q_ASSERT(parent);
1752 QQuickItem *top = parent->childItems().constLast();
1753 if (q != top)
1754 q->stackAfter(top);
1755}
1756
1757void QWaylandQuickItem::sendMouseMoveEvent(const QPointF &position, QWaylandSeat *seat)
1758{
1759 if (seat == nullptr)
1760 seat = compositor()->defaultSeat();
1761
1762 if (!seat) {
1763 qWarning() << "No seat, can't send mouse event";
1764 return;
1765 }
1766
1767 seat->sendMouseMoveEvent(view(), position);
1768}
1769
1770/*!
1771 * \internal
1772 *
1773 * Sets the position of this item relative to the parent item.
1774 */
1775void QWaylandQuickItem::handleSubsurfacePosition(const QPoint &pos)
1776{
1777 Q_D(QWaylandQuickItem);
1778 QQuickItem::setPosition(pos * d->scaleFactor());
1779}
1780
1781#if QT_CONFIG(draganddrop)
1782void QWaylandQuickItem::handleDragStarted(QWaylandDrag *drag)
1783{
1784 Q_D(QWaylandQuickItem);
1785 Q_ASSERT(drag->origin() == surface());
1786 drag->seat()->setMouseFocus(nullptr);
1787 d->isDragging = true;
1788}
1789#endif
1790
1792{
1793 qreal f = view->output() ? view->output()->scaleFactor() : 1;
1794#if !defined(Q_OS_MACOS)
1795 if (window)
1796 f /= window->devicePixelRatio();
1797#endif
1798 return f;
1799}
1800
1801QWaylandQuickItem *QWaylandQuickItemPrivate::findSibling(QWaylandSurface *surface) const
1802{
1803 Q_Q(const QWaylandQuickItem);
1804 auto *parent = q->parentItem();
1805 if (!parent)
1806 return nullptr;
1807
1808 const auto siblings = q->parentItem()->childItems();
1809 for (auto *sibling : siblings) {
1810 auto *waylandItem = qobject_cast<QWaylandQuickItem *>(sibling);
1811 if (waylandItem && waylandItem->surface() == surface)
1812 return waylandItem;
1813 }
1814 return nullptr;
1815}
1816
1817void QWaylandQuickItemPrivate::placeAboveSibling(QWaylandQuickItem *sibling)
1818{
1819 Q_Q(QWaylandQuickItem);
1820 q->stackAfter(sibling);
1821 q->setZ(sibling->z());
1822 belowParent = sibling->d_func()->belowParent;
1823}
1824
1825void QWaylandQuickItemPrivate::placeBelowSibling(QWaylandQuickItem *sibling)
1826{
1827 Q_Q(QWaylandQuickItem);
1828 q->stackBefore(sibling);
1829 q->setZ(sibling->z());
1830 belowParent = sibling->d_func()->belowParent;
1831}
1832
1833//### does not handle changes in z value if parent is a subsurface
1835{
1836 Q_Q(QWaylandQuickItem);
1837 const auto siblings = q->parentItem()->childItems();
1838
1839 // Stack below first (bottom) sibling above parent
1840 bool foundSibling = false;
1841 for (auto it = siblings.cbegin(); it != siblings.cend(); ++it) {
1842 QWaylandQuickItem *sibling = qobject_cast<QWaylandQuickItem*>(*it);
1843 if (sibling && !sibling->d_func()->belowParent) {
1844 q->stackBefore(sibling);
1845 foundSibling = true;
1846 break;
1847 }
1848 }
1849
1850 // No other subsurfaces above parent
1851 if (!foundSibling && siblings.last() != q)
1852 q->stackAfter(siblings.last());
1853
1854 q->setZ(q->parentItem()->z());
1855 belowParent = false;
1856}
1857
1858//### does not handle changes in z value if parent is a subsurface
1860{
1861 Q_Q(QWaylandQuickItem);
1862 const auto siblings = q->parentItem()->childItems();
1863
1864 // Stack above last (top) sibling below parent
1865 bool foundSibling = false;
1866 for (auto it = siblings.crbegin(); it != siblings.crend(); ++it) {
1867 QWaylandQuickItem *sibling = qobject_cast<QWaylandQuickItem*>(*it);
1868 if (sibling && sibling->d_func()->belowParent) {
1869 q->stackAfter(sibling);
1870 foundSibling = true;
1871 break;
1872 }
1873 }
1874
1875 // No other subsurfaces below parent
1876 if (!foundSibling && siblings.first() != q)
1877 q->stackBefore(siblings.first());
1878
1879 q->setZ(q->parentItem()->z() - 1.0);
1880 belowParent = true;
1881}
1882
1883QT_END_NAMESPACE
1884
1885#include "moc_qwaylandquickitem.cpp"
QSGTexture * texture() const override
Returns a pointer to the texture object.
void setBufferRef(QWaylandQuickItem *surfaceItem, const QWaylandBufferRef &buffer)
static QImage withValidAlpha(const QImage &image)