13#include <QtWaylandCompositor/qwaylandcompositor.h>
14#include <QtWaylandCompositor/qwaylandseat.h>
15#include <QtWaylandCompositor/qwaylandbufferref.h>
16#if QT_CONFIG(draganddrop)
17#include <QtWaylandCompositor/QWaylandDrag>
19#include <QtWaylandCompositor/private/qwlclientbufferintegration_p.h>
20#include <QtWaylandCompositor/private/qwaylandsurface_p.h>
23# include <QtOpenGL/QOpenGLTexture>
24# include <QtGui/QOpenGLFunctions>
27#include <QtGui/QKeyEvent>
28#include <QtGui/QGuiApplication>
29#include <QtGui/QScreen>
31#include <QtQuick/QSGSimpleTextureNode>
32#include <QtQuick/QQuickWindow>
33#include <QtQuick/qsgtexture.h>
35#include <QtCore/QFile>
36#include <QtCore/qendian.h>
37#include <QtCore/QMutexLocker>
38#include <QtCore/QMutex>
40#include <wayland-server-core.h>
44#include <QtGui/private/qshaderdescription_p.h>
47#ifndef GL_TEXTURE_EXTERNAL_OES
48#define GL_TEXTURE_EXTERNAL_OES 0x8D65
55 const char *
const vertexShaderSourceFile;
56 const char *
const fragmentShaderSourceFile;
58 bool canProvideTexture;
59 QSGMaterial::Flags materialFlags;
60 QSGMaterialType materialType;
63 {
"",
"", 0,
false, {}, {} },
67 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
68 ":/qt-project.org/wayland/compositor/shaders/surface_rgbx.frag.qsb",
70 QSGMaterial::Blending,
76 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
77 ":/qt-project.org/wayland/compositor/shaders/surface_rgba.frag.qsb",
79 QSGMaterial::Blending,
85 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
86 ":/qt-project.org/wayland/compositor/shaders/surface_oes_external.frag",
88 QSGMaterial::Blending,
94 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
95 ":/qt-project.org/wayland/compositor/shaders/surface_y_u_v.frag.qsb",
97 QSGMaterial::Blending,
103 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
104 ":/qt-project.org/wayland/compositor/shaders/surface_y_uv.frag.qsb",
106 QSGMaterial::Blending,
112 ":/qt-project.org/wayland/compositor/shaders/surface.vert.qsb",
113 ":/qt-project.org/wayland/compositor/shaders/surface_y_xuxv.frag.qsb",
115 QSGMaterial::Blending,
120QWaylandBufferMaterialShader::QWaylandBufferMaterialShader(QWaylandBufferRef::BufferFormatEgl format)
123 setShaderFileName(VertexStage, QString::fromLatin1(bufferTypes[format].vertexShaderSourceFile));
124 auto fragmentShaderSourceFile = QString::fromLatin1(bufferTypes[format].fragmentShaderSourceFile);
126 if (format == QWaylandBufferRef::BufferFormatEgl_EXTERNAL_OES)
127 setupExternalOESShader(fragmentShaderSourceFile);
129 setShaderFileName(FragmentStage, fragmentShaderSourceFile);
132void QWaylandBufferMaterialShader::setupExternalOESShader(
const QString &shaderFilename)
135 QFile shaderFile(shaderFilename);
136 if (!shaderFile.open(QIODevice::ReadOnly)) {
137 qCWarning(qLcWaylandCompositor) <<
"Cannot find external OES shader file:" << shaderFilename;
140 QByteArray FS = shaderFile.readAll();
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 =
147 "#extension GL_OES_EGL_image_external : require\n";
148 QByteArray fsGLES = FS_GLES_PREAMBLE + FS;
149 QByteArray fsGL = FS_GL_PREAMBLE + FS;
151 QShaderDescription desc;
152 QShaderDescriptionPrivate *descData = QShaderDescriptionPrivate::get(&desc);
154 QShaderDescription::InOutVariable texCoordInput;
155 texCoordInput.name =
"v_texcoord";
156 texCoordInput.type = QShaderDescription::Vec2;
157 texCoordInput.location = 0;
159 descData->inVars = { texCoordInput };
161 QShaderDescription::InOutVariable fragColorOutput;
162 fragColorOutput.name =
"gl_FragColor";
163 fragColorOutput.type = QShaderDescription::Vec4;
164 fragColorOutput.location = 0;
166 descData->outVars = { fragColorOutput };
168 QShaderDescription::BlockVariable matrixBlockVar;
169 matrixBlockVar.name =
"qt_Matrix";
170 matrixBlockVar.type = QShaderDescription::Mat4;
171 matrixBlockVar.offset = 0;
172 matrixBlockVar.size = 64;
174 QShaderDescription::BlockVariable opacityBlockVar;
175 opacityBlockVar.name =
"qt_Opacity";
176 opacityBlockVar.type = QShaderDescription::Float;
177 opacityBlockVar.offset = 64;
178 opacityBlockVar.size = 4;
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 };
187 descData->uniformBlocks = { ubufStruct };
189 QShaderDescription::InOutVariable samplerTex0;
190 samplerTex0.name =
"tex0";
191 samplerTex0.type = QShaderDescription::SamplerExternalOES;
192 samplerTex0.binding = 1;
194 descData->combinedImageSamplers = { samplerTex0 };
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));
202 setShader(FragmentStage, shaderPack);
204 Q_UNUSED(shaderFilename);
208bool QWaylandBufferMaterialShader::updateUniformData(RenderState &state, QSGMaterial *, QSGMaterial *)
210 bool changed =
false;
211 QByteArray *buf = state.uniformData();
212 Q_ASSERT(buf->size() >= 68);
214 if (state.isMatrixDirty()) {
215 const QMatrix4x4 m = state.combinedMatrix();
216 memcpy(buf->data(), m.constData(), 64);
220 if (state.isOpacityDirty()) {
221 const float opacity = state.opacity();
222 memcpy(buf->data() + 64, &opacity, 4);
229void QWaylandBufferMaterialShader::updateSampledImage(RenderState &state,
int binding, QSGTexture **texture,
230 QSGMaterial *newMaterial, QSGMaterial *)
234 QWaylandBufferMaterial *material =
static_cast<QWaylandBufferMaterial *>(newMaterial);
237 *texture = material->m_scenegraphTextures.at(0);
240 *texture = material->m_scenegraphTextures.at(1);
243 *texture = material->m_scenegraphTextures.at(2);
253 (*texture)->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
256QWaylandBufferMaterial::QWaylandBufferMaterial(QWaylandBufferRef::BufferFormatEgl format)
259 setFlag(bufferTypes[m_format].materialFlags);
262QWaylandBufferMaterial::~QWaylandBufferMaterial()
264 qDeleteAll(m_scenegraphTextures);
267void QWaylandBufferMaterial::setTextureForPlane(
int plane,
268 QOpenGLTexture *texture,
269 QSGTexture *scenegraphTexture)
271 if (plane < 0 || plane >= bufferTypes[m_format].planeCount) {
272 qWarning(
"plane index is out of range");
277 setTextureParameters(texture->target());
279 ensureTextures(plane - 1);
281 if (m_textures.size() <= plane) {
282 m_textures << texture;
283 m_scenegraphTextures << scenegraphTexture;
285 delete m_scenegraphTextures[plane];
287 m_textures[plane] = texture;
288 m_scenegraphTextures[plane] = scenegraphTexture;
292void QWaylandBufferMaterial::bind()
294 ensureTextures(bufferTypes[m_format].planeCount);
296 switch (m_textures.size()) {
299 m_textures[2]->bind(2);
303 m_textures[1]->bind(1);
307 m_textures[0]->bind(0);
311QSGMaterialType *QWaylandBufferMaterial::type()
const
313 return const_cast<QSGMaterialType *>(&bufferTypes[m_format].materialType);
316QSGMaterialShader *QWaylandBufferMaterial::createShader(QSGRendererInterface::RenderMode renderMode)
const
318 Q_UNUSED(renderMode);
319 return new QWaylandBufferMaterialShader(m_format);
323void QWaylandBufferMaterial::setTextureParameters(GLenum target)
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);
333void QWaylandBufferMaterial::ensureTextures(
int count)
335 for (
int plane = m_textures.size(); plane < count; plane++) {
336 m_textures <<
nullptr;
337 m_scenegraphTextures <<
nullptr;
341void QWaylandBufferMaterial::setBufferRef(QWaylandQuickItem *surfaceItem,
const QWaylandBufferRef &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(),
357 scenegraphTexture = QNativeInterface::QSGOpenGLTexture::fromNative(texture->textureId(),
358 surfaceItem->window(),
362 scenegraphTexture->setFiltering(surfaceItem->smooth() ? QSGTexture::Linear : QSGTexture::Nearest);
363 setTextureForPlane(plane, texture, scenegraphTexture);
371QMutex *QWaylandQuickItemPrivate::mutex =
nullptr;
380 switch (image.format()) {
381 case QImage::Format_RGB32:
382 alphaMask = 0xff000000u;
384 case QImage::Format_RGBX8888:
385 alphaMask = qFromLittleEndian<quint32>(0xff000000u);
391 QImage result(image.size(), image.format());
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;
421 if (m_compositorDestroyed.loadAcquire() && m_ref.hasBuffer())
422 m_ref.buffer()->setDestroyed();
427 void setBufferRef(QWaylandQuickItem *surfaceItem,
const QWaylandBufferRef &buffer)
429 Q_ASSERT(QThread::currentThread() == thread());
433 if (m_ref.hasBuffer()) {
434 if (buffer.isSharedMemory()) {
435 m_sgTex = surfaceItem->window()->createTextureFromImage(withValidAlpha(buffer.image()));
438 QQuickWindow::CreateTextureOptions opt;
439 QWaylandQuickSurface *surface = qobject_cast<QWaylandQuickSurface *>(surfaceItem->surface());
440 if (surface && surface->useTextureAlpha() && !surface->isOpaque()) {
441 opt |= QQuickWindow::TextureHasAlphaChannel;
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);
449 qCWarning(qLcWaylandCompositor) <<
"Without OpenGL support only shared memory textures are supported";
453 emit textureChanged();
459 m_sgTex->setFiltering(m_smooth ? QSGTexture::Linear : QSGTexture::Nearest);
467 m_compositorDestroyed.storeRelease(1);
472 bool m_smooth =
false;
473 QSGTexture *m_sgTex =
nullptr;
475 QAtomicInt m_compositorDestroyed;
476 QWaylandBufferRef m_ref;
481#if QT_CONFIG(draganddrop)
482 Q_Q(QWaylandQuickItem);
483 QWaylandQuickOutput *currentOutput = qobject_cast<QWaylandQuickOutput *>(q->view()->output());
485 QWaylandQuickItem *targetItem = qobject_cast<QWaylandQuickItem *>(currentOutput->pickClickableItem(q->mapToScene(globalPosition)));
486 QWaylandSurface *targetSurface = targetItem ? targetItem->surface() :
nullptr;
488 QPointF position = q->mapToItem(targetItem, globalPosition);
489 QPointF surfacePosition = targetItem->mapToSurface(position);
490 seat->drag()->dragMove(targetSurface, surfacePosition);
494 Q_UNUSED(globalPosition);
500#if QT_CONFIG(draganddrop)
502 seat->drag()->drop();
509
510
511
512
513
514
515
516
517
518
521
522
523
524
525
526
527
528
529
532
533
534QWaylandQuickItem::QWaylandQuickItem(QQuickItem *parent)
535 : QWaylandQuickItem(*
new QWaylandQuickItemPrivate(), parent)
540
541
542QWaylandQuickItem::QWaylandQuickItem(QWaylandQuickItemPrivate &dd, QQuickItem *parent)
543 : QQuickItem(dd, parent)
546 connect(
this, &QQuickItem::activeFocusChanged,
this, &QWaylandQuickItem::updateFocus);
550
551
552QWaylandQuickItem::~QWaylandQuickItem()
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);
559 disconnect(d->texProviderConnection);
560 d->provider->deleteLater();
565
566
567
568
571
572
573
574
575QWaylandCompositor *QWaylandQuickItem::compositor()
const
577 Q_D(
const QWaylandQuickItem);
578 return d->view->surface() ? d->view->surface()->compositor() :
nullptr;
582
583
584QWaylandView *QWaylandQuickItem::view()
const
586 Q_D(
const QWaylandQuickItem);
587 return d->view.data();
591
592
593
594
597
598
599
600
602QWaylandSurface *QWaylandQuickItem::surface()
const
604 Q_D(
const QWaylandQuickItem);
605 return d->view->surface();
608void QWaylandQuickItem::setSurface(QWaylandSurface *surface)
610 Q_D(QWaylandQuickItem);
611 QWaylandSurface *oldSurf = d->view->surface();
612 if (oldSurf == surface)
615 QWaylandCompositor *oldComp = oldSurf ? oldSurf->compositor() :
nullptr;
616 QWaylandCompositor *newComp = surface ? surface->compositor() :
nullptr;
617 if (oldComp != newComp)
618 emit compositorChanged();
620 d->view->setSurface(surface);
627
628
629
630
631
632
633
636
637
638
639
640
641
642
643QWaylandSurface::Origin QWaylandQuickItem::origin()
const
645 Q_D(
const QWaylandQuickItem);
649bool QWaylandQuickItem::isTextureProvider()
const
651 Q_D(
const QWaylandQuickItem);
652 return QQuickItem::isTextureProvider() || d->provider;
656
657
658QSGTextureProvider *QWaylandQuickItem::textureProvider()
const
660 Q_D(
const QWaylandQuickItem);
662 if (QQuickItem::isTextureProvider())
663 return QQuickItem::textureProvider();
669
670
671void QWaylandQuickItem::mousePressEvent(QMouseEvent *event)
673 Q_D(QWaylandQuickItem);
674 if (!d->shouldSendInputEvents()) {
679 if (!inputRegionContains(event->position())) {
684 QWaylandSeat *seat = compositor()->seatFor(event);
689 seat->sendMouseMoveEvent(d->view.data(), mapToSurface(event->position()), event->scenePosition());
690 seat->sendMousePressEvent(event->button());
691 d->hoverPos = event->position();
695
696
697void QWaylandQuickItem::mouseMoveEvent(QMouseEvent *event)
699 Q_D(QWaylandQuickItem);
700 if (d->shouldSendInputEvents()) {
701 QWaylandSeat *seat = compositor()->seatFor(event);
703 d->handleDragUpdate(seat, event->position());
705 seat->sendMouseMoveEvent(d->view.data(), mapToSurface(event->position()), event->scenePosition());
706 d->hoverPos = event->position();
709 emit mouseMove(event->scenePosition());
715
716
717void QWaylandQuickItem::mouseReleaseEvent(QMouseEvent *event)
719 Q_D(QWaylandQuickItem);
720 if (d->shouldSendInputEvents()) {
721 QWaylandSeat *seat = compositor()->seatFor(event);
723 d->handleDragEnded(seat);
725 seat->sendMouseReleaseEvent(event->button());
733
734
735void QWaylandQuickItem::hoverEnterEvent(QHoverEvent *event)
737 Q_D(QWaylandQuickItem);
738 if (!inputRegionContains(event->position())) {
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();
752
753
754void QWaylandQuickItem::hoverMoveEvent(QHoverEvent *event)
756 Q_D(QWaylandQuickItem);
758 if (!inputRegionContains(event->position())) {
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();
775
776
777void QWaylandQuickItem::hoverLeaveEvent(QHoverEvent *event)
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);
791#if QT_CONFIG(wheelevent)
793
794
795void QWaylandQuickItem::wheelEvent(QWheelEvent *event)
797 Q_D(QWaylandQuickItem);
798 if (d->shouldSendInputEvents()) {
799 if (!inputRegionContains(event->position())) {
804 QWaylandSeat *seat = compositor()->seatFor(event);
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());
817
818
819void QWaylandQuickItem::keyPressEvent(QKeyEvent *event)
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);
827 qWarning() <<
"Unable to set keyboard focus, cannot send key press event";
834
835
836void QWaylandQuickItem::keyReleaseEvent(QKeyEvent *event)
838 Q_D(QWaylandQuickItem);
839 if (d->shouldSendInputEvents()) {
840 QWaylandSeat *seat = compositor()->seatFor(event);
841 seat->sendFullKeyEvent(event);
848
849
850void QWaylandQuickItem::touchEvent(QTouchEvent *event)
852 Q_D(QWaylandQuickItem);
853 if (d->shouldSendInputEvents() && d->touchEventsEnabled) {
854 QWaylandSeat *seat = compositor()->seatFor(event);
857 const QList<QTouchEvent::TouchPoint> &points = event->points();
858 if (!points.isEmpty())
859 pointPos = points.at(0).position();
861 if (event->type() == QEvent::TouchBegin && !inputRegionContains(pointPos)) {
866 if (event->type() == QEvent::TouchUpdate && d->isDragging)
867 d->handleDragUpdate(seat, pointPos);
870 if (seat->mouseFocus() != d->view.data()) {
871 seat->sendMouseMoveEvent(d->view.data(), pointPos, mapToScene(pointPos));
873 seat->sendFullTouchEvent(surface(), event);
875 if (event->type() == QEvent::TouchBegin) {
876 d->touchingSeats.append(seat);
877 }
else if (event->type() == QEvent::TouchEnd || event->type() == QEvent::TouchCancel) {
879 d->handleDragEnded(seat);
880 d->touchingSeats.removeOne(seat);
883 if (event->type() == QEvent::TouchBegin && d->focusOnClick)
890void QWaylandQuickItem::touchUngrabEvent()
892 Q_D(QWaylandQuickItem);
894 if (d->shouldSendInputEvents()) {
895 for (
auto seat : std::as_const(d->touchingSeats))
896 seat->sendTouchCancelEvent(surface()->client());
899 d->touchingSeats.clear();
904
905
906void QWaylandQuickItem::inputMethodEvent(QInputMethodEvent *event)
908 Q_D(QWaylandQuickItem);
909 if (d->shouldSendInputEvents()) {
910 d->oldSurface->inputMethodControl()->inputMethodEvent(event);
918
919
920void QWaylandQuickItem::surfaceChangedEvent(QWaylandSurface *newSurface, QWaylandSurface *oldSurface)
922 Q_UNUSED(newSurface);
923 Q_UNUSED(oldSurface);
926void QWaylandQuickItem::handleSubsurfaceAdded(QWaylandSurface *childSurface)
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);
939 bool success = QMetaObject::invokeMethod(d->subsurfaceHandler,
"handleSubsurfaceAdded", Q_ARG(QWaylandSurface *, childSurface));
941 success = QMetaObject::invokeMethod(d->subsurfaceHandler,
"handleSubsurfaceAdded",
942 Q_ARG(QVariant, QVariant::fromValue(childSurface)));
945 qWarning(
"QWaylandQuickItem: subsurfaceHandler does not implement handleSubsurfaceAdded()");
949void QWaylandQuickItem::handlePlaceAbove(QWaylandSurface *referenceSurface)
951 Q_D(QWaylandQuickItem);
952 auto *parent = qobject_cast<QWaylandQuickItem*>(parentItem());
956 if (parent->surface() == referenceSurface) {
957 d->placeAboveParent();
958 }
else if (
auto *sibling = d->findSibling(referenceSurface)) {
959 d->placeAboveSibling(sibling);
961 qWarning() <<
"Couldn't find QWaylandQuickItem for surface" << referenceSurface
962 <<
"when handling wl_subsurface.place_above";
966void QWaylandQuickItem::handlePlaceBelow(QWaylandSurface *referenceSurface)
968 Q_D(QWaylandQuickItem);
969 QWaylandQuickItem *parent = qobject_cast<QWaylandQuickItem*>(parentItem());
973 if (parent->surface() == referenceSurface) {
974 d->placeBelowParent();
975 }
else if (
auto *sibling = d->findSibling(referenceSurface)) {
976 d->placeBelowSibling(sibling);
978 qWarning() <<
"Couldn't find QWaylandQuickItem for surface" << referenceSurface
979 <<
"when handling wl_subsurface.place_below";
983void QWaylandQuickItem::updateFocus()
985 Q_D(
const QWaylandQuickItem);
986 if (hasActiveFocus() && compositor())
987 compositor()->defaultSeat()->setKeyboardFocus(d->view->surface());
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027QObject *QWaylandQuickItem::subsurfaceHandler()
const
1029 Q_D(
const QWaylandQuickItem);
1030 return d->subsurfaceHandler.data();
1033void QWaylandQuickItem::setSubsurfaceHandler(QObject *handler)
1035 Q_D(QWaylandQuickItem);
1036 if (d->subsurfaceHandler.data() != handler) {
1037 d->subsurfaceHandler = handler;
1038 emit subsurfaceHandlerChanged();
1043
1044
1045
1046
1048
1049
1050
1051
1052QWaylandOutput *QWaylandQuickItem::output()
const
1054 Q_D(
const QWaylandQuickItem);
1055 return d->view->output();
1058void QWaylandQuickItem::setOutput(QWaylandOutput *output)
1060 Q_D(QWaylandQuickItem);
1061 d->view->setOutput(output);
1065
1066
1067
1068
1069
1070
1071
1073
1074
1075
1076
1077
1078
1079
1080bool QWaylandQuickItem::isBufferLocked()
const
1082 Q_D(
const QWaylandQuickItem);
1083 return d->view->isBufferLocked();
1086void QWaylandQuickItem::setBufferLocked(
bool locked)
1088 Q_D(QWaylandQuickItem);
1089 d->view->setBufferLocked(locked);
1093
1094
1095
1096
1097
1098
1099
1100bool QWaylandQuickItem::allowDiscardFrontBuffer()
const
1102 Q_D(
const QWaylandQuickItem);
1103 return d->view->allowDiscardFrontBuffer();
1106void QWaylandQuickItem::setAllowDiscardFrontBuffer(
bool discard)
1108 Q_D(QWaylandQuickItem);
1109 d->view->setAllowDiscardFrontBuffer(discard);
1113
1114
1115
1116
1119
1120
1121
1122
1123void QWaylandQuickItem::setPrimary()
1125 Q_D(QWaylandQuickItem);
1126 d->view->setPrimary();
1130
1131
1132void QWaylandQuickItem::handleSurfaceChanged()
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);
1148 disconnect(d->oldSurface->inputMethodControl(), &QWaylandInputMethodControl::updateInputMethod,
this, &QWaylandQuickItem::updateInputMethod);
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);
1164 connect(newSurface->inputMethodControl(), &QWaylandInputMethodControl::updateInputMethod,
this, &QWaylandQuickItem::updateInputMethod);
1167 if (newSurface->origin() != d->origin) {
1168 d->origin = newSurface->origin();
1169 emit originChanged();
1172 QWaylandOutput *output = newSurface->compositor()->outputFor(window());
1173 d->view->setOutput(output);
1175 for (
const auto &subsurface : std::as_const(QWaylandSurfacePrivate::get(newSurface)->subsurfaceChildren)) {
1176 if (!subsurface.isNull())
1177 handleSubsurfaceAdded(subsurface.data());
1182 surfaceChangedEvent(d->view->surface(), d->oldSurface);
1183 d->oldSurface = d->view->surface();
1185 updateInputMethod(Qt::ImQueryInput);
1190
1191
1192
1193void QWaylandQuickItem::takeFocus(QWaylandSeat *device)
1197 if (!surface() || !surface()->client())
1200 QWaylandSeat *target = device;
1202 target = compositor()->defaultSeat();
1204 target->setKeyboardFocus(surface());
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);
1212 textInput->setFocus(surface());
1215 if (surface()->client()->textInputProtocols().testFlag(QWaylandClient::TextInputProtocol::TextInputV3)) {
1216 QWaylandTextInputV3 *textInputV3 = QWaylandTextInputV3::findIn(target);
1218 textInputV3->setFocus(surface());
1221 if (surface()->client()->textInputProtocols().testFlag(QWaylandClient::TextInputProtocol::QtTextInputMethodV1)) {
1222 QWaylandQtTextInputMethod *textInputMethod = QWaylandQtTextInputMethod::findIn(target);
1223 if (textInputMethod)
1224 textInputMethod->setFocus(surface());
1229
1230
1231void QWaylandQuickItem::handleBufferLockedChanged()
1233 Q_D(QWaylandQuickItem);
1236 if (!d->view->isBufferLocked()) {
1242#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
1243void QWaylandQuickItem::surfaceMappedChanged()
1250
1251
1252void QWaylandQuickItem::parentChanged(QWaylandSurface *newParent, QWaylandSurface *oldParent)
1254 Q_UNUSED(oldParent);
1257 setPaintEnabled(
true);
1265
1266
1267void QWaylandQuickItem::updateSize()
1269 Q_D(QWaylandQuickItem);
1272 if (isBufferLocked()) {
1273 qWarning() <<
"No update on item size as the buffer is currently locked";
1279 size = surface()->destinationSize() * d->scaleFactor();
1281 setImplicitSize(size.width(), size.height());
1285
1286
1287
1288
1289
1290
1291
1294
1295
1296
1297
1298
1299
1300
1301bool QWaylandQuickItem::focusOnClick()
const
1303 Q_D(
const QWaylandQuickItem);
1304 return d->focusOnClick;
1307void QWaylandQuickItem::setFocusOnClick(
bool focus)
1309 Q_D(QWaylandQuickItem);
1310 if (d->focusOnClick == focus)
1313 d->focusOnClick = focus;
1314 emit focusOnClickChanged();
1318
1319
1320
1321bool QWaylandQuickItem::inputRegionContains(
const QPointF &localPosition)
const
1323 if (QWaylandSurface *s = surface())
1324 return s->inputRegionContains(mapToSurface(localPosition));
1329
1330
1331
1332
1333
1334
1337
1338
1339
1340
1341QPointF QWaylandQuickItem::mapToSurface(
const QPointF &point)
const
1343 Q_D(
const QWaylandQuickItem);
1344 if (!surface() || surface()->destinationSize().isEmpty())
1345 return point / d->scaleFactor();
1347 qreal xScale = width() / surface()->destinationSize().width();
1348 qreal yScale = height() / surface()->destinationSize().height();
1350 return QPointF(point.x() / xScale, point.y() / yScale);
1354
1355
1356
1357
1358
1359
1362
1363
1364
1365
1366
1367QPointF QWaylandQuickItem::mapFromSurface(
const QPointF &point)
const
1369 Q_D(
const QWaylandQuickItem);
1370 if (!surface() || surface()->destinationSize().isEmpty())
1371 return point * d->scaleFactor();
1373 qreal xScale = width() / surface()->destinationSize().width();
1374 qreal yScale = height() / surface()->destinationSize().height();
1376 return QPointF(point.x() * xScale, point.y() * yScale);
1380QVariant QWaylandQuickItem::inputMethodQuery(Qt::InputMethodQuery query)
const
1382 return inputMethodQuery(query, QVariant());
1385QVariant QWaylandQuickItem::inputMethodQuery(Qt::InputMethodQuery query, QVariant argument)
const
1387 Q_D(
const QWaylandQuickItem);
1389 if (query == Qt::ImEnabled)
1390 return QVariant((flags() & ItemAcceptsInputMethod) != 0);
1393 return d->oldSurface->inputMethodControl()->inputMethodQuery(query, argument);
1400
1401
1402
1403
1404
1405
1406
1409
1410
1411
1412
1413
1414
1415bool QWaylandQuickItem::isPaintEnabled()
const
1417 Q_D(
const QWaylandQuickItem);
1418 return d->paintEnabled;
1421void QWaylandQuickItem::setPaintEnabled(
bool enabled)
1423 Q_D(QWaylandQuickItem);
1425 if (enabled != d->paintEnabled) {
1426 d->paintEnabled = enabled;
1427 emit paintEnabledChanged();
1434
1435
1436
1437
1438
1441
1442
1443
1444
1445
1446bool QWaylandQuickItem::touchEventsEnabled()
const
1448 Q_D(
const QWaylandQuickItem);
1449 return d->touchEventsEnabled;
1452void QWaylandQuickItem::updateBuffer(
bool hasBuffer)
1454 Q_D(QWaylandQuickItem);
1455 Q_UNUSED(hasBuffer);
1456 if (d->origin != surface()->origin()) {
1457 d->origin = surface()->origin();
1458 emit originChanged();
1462void QWaylandQuickItem::updateWindow()
1464 Q_D(QWaylandQuickItem);
1466 QQuickWindow *newWindow = window();
1467 if (newWindow == d->connectedWindow)
1470 if (d->connectedWindow) {
1471 disconnect(d->connectedWindow, &QQuickWindow::beforeSynchronizing,
this, &QWaylandQuickItem::beforeSync);
1472 disconnect(d->connectedWindow, &QQuickWindow::screenChanged,
this, &QWaylandQuickItem::updateSize);
1475 d->connectedWindow = newWindow;
1477 if (d->connectedWindow) {
1478 connect(d->connectedWindow, &QQuickWindow::beforeSynchronizing,
this, &QWaylandQuickItem::beforeSync, Qt::DirectConnection);
1479 connect(d->connectedWindow, &QQuickWindow::screenChanged,
this, &QWaylandQuickItem::updateSize);
1482 QWaylandOutput *output = compositor()->outputFor(d->connectedWindow);
1484 d->view->setOutput(output);
1491void QWaylandQuickItem::updateOutput()
1493 Q_D(QWaylandQuickItem);
1494 if (d->view->output() == d->connectedOutput)
1497 if (d->connectedOutput)
1498 disconnect(d->connectedOutput, &QWaylandOutput::scaleFactorChanged,
this, &QWaylandQuickItem::updateSize);
1500 d->connectedOutput = d->view->output();
1502 if (d->connectedOutput)
1503 connect(d->connectedOutput, &QWaylandOutput::scaleFactorChanged,
this, &QWaylandQuickItem::updateSize);
1508void QWaylandQuickItem::beforeSync()
1510 Q_D(QWaylandQuickItem);
1511 if (d->view->advance()) {
1512 d->newTexture =
true;
1518void QWaylandQuickItem::updateInputMethod(Qt::InputMethodQueries queries)
1520 Q_D(QWaylandQuickItem);
1522 setFlag(QQuickItem::ItemAcceptsInputMethod,
1523 d->oldSurface ? d->oldSurface->inputMethodControl()->enabled() :
false);
1524 QQuickItem::updateInputMethod(queries | Qt::ImEnabled);
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1554QSGNode *QWaylandQuickItem::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *data)
1556 Q_D(QWaylandQuickItem);
1557 d->lastMatrix = data->transformNode->combinedMatrix();
1558 const bool bufferHasContent = d->view->currentBuffer().hasContent();
1560 if (d->view->isBufferLocked() && d->paintEnabled)
1563 if (!bufferHasContent || !d->paintEnabled || !surface()) {
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());
1573 if (ref.isSharedMemory()
1574#if QT_CONFIG(opengl)
1575 || bufferTypes[ref.bufferFormatEgl()].canProvideTexture
1578#if QT_CONFIG(opengl)
1579 if (oldNode && !d->paintByProvider) {
1584 d->paintByProvider =
true;
1587 QSGSimpleTextureNode *node =
static_cast<QSGSimpleTextureNode *>(oldNode);
1590 node =
new QSGSimpleTextureNode();
1592 node->setFiltering(QSGTexture::Linear);
1593 d->newTexture =
true;
1597 d->provider =
new QWaylandSurfaceTextureProvider();
1598 if (
auto *c = compositor()) {
1599 d->texProviderConnection =
1600 QObject::connect(c, &QObject::destroyed,
this, [d = d] {
1602 d->provider->reportCompositorDestroyed();
1603 d->provider =
nullptr;
1605 QObject::disconnect(d->texProviderConnection);
1610 if (d->newTexture) {
1611 d->newTexture =
false;
1612 d->provider->setBufferRef(
this, ref);
1613 node->setTexture(d->provider->texture());
1616 d->provider->setSmooth(smooth());
1617 node->setRect(rect);
1619 qreal scale = surface()->bufferScale();
1620 QRectF source = surface()->sourceGeometry();
1621 node->setSourceRect(QRectF(source.topLeft() * scale, source.size() * scale));
1626#if QT_CONFIG(opengl)
1627 Q_ASSERT(!d->provider);
1629 if (oldNode && d->paintByProvider) {
1634 d->paintByProvider =
false;
1636 QSGGeometryNode *node =
static_cast<QSGGeometryNode *>(oldNode);
1639 node =
new QSGGeometryNode;
1640 d->newTexture =
true;
1643 QSGGeometry *geometry = node->geometry();
1644 QWaylandBufferMaterial *material =
static_cast<QWaylandBufferMaterial *>(node->material());
1647 geometry =
new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D(), 4);
1650 material =
new QWaylandBufferMaterial(ref.bufferFormatEgl());
1652 if (d->newTexture) {
1653 d->newTexture =
false;
1654 material->setBufferRef(
this, ref);
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);
1667 QSGGeometry::updateTexturedRectGeometry(geometry, rect, normalizedCoordinates);
1669 node->setGeometry(geometry);
1670 node->setFlag(QSGNode::OwnsGeometry,
true);
1672 node->setMaterial(material);
1673 node->setFlag(QSGNode::OwnsMaterial,
true);
1677 qCWarning(qLcWaylandCompositor) <<
"Without OpenGL support only shared memory textures are supported";
1682void QWaylandQuickItem::setTouchEventsEnabled(
bool enabled)
1684 Q_D(QWaylandQuickItem);
1685 if (d->touchEventsEnabled != enabled) {
1686 d->touchEventsEnabled = enabled;
1687 emit touchEventsEnabledChanged();
1692
1693
1694
1695
1696
1697
1698
1701
1702
1703
1704
1705
1706
1707
1708bool QWaylandQuickItem::inputEventsEnabled()
const
1710 Q_D(
const QWaylandQuickItem);
1711 return d->inputEventsEnabled;
1714void QWaylandQuickItem::setInputEventsEnabled(
bool enabled)
1716 Q_D(QWaylandQuickItem);
1717 if (d->inputEventsEnabled != enabled) {
1720 d->setInputEventsEnabled(enabled);
1721 emit inputEventsEnabledChanged();
1725void QWaylandQuickItem::lower()
1727 Q_D(QWaylandQuickItem);
1733 Q_Q(QWaylandQuickItem);
1734 QQuickItem *parent = q->parentItem();
1736 QQuickItem *bottom = parent->childItems().constFirst();
1738 q->stackBefore(bottom);
1741void QWaylandQuickItem::raise()
1743 Q_D(QWaylandQuickItem);
1749 Q_Q(QWaylandQuickItem);
1750 QQuickItem *parent = q->parentItem();
1752 QQuickItem *top = parent->childItems().constLast();
1757void QWaylandQuickItem::sendMouseMoveEvent(
const QPointF &position, QWaylandSeat *seat)
1759 if (seat ==
nullptr)
1760 seat = compositor()->defaultSeat();
1763 qWarning() <<
"No seat, can't send mouse event";
1767 seat->sendMouseMoveEvent(view(), position);
1771
1772
1773
1774
1775void QWaylandQuickItem::handleSubsurfacePosition(
const QPoint &pos)
1777 Q_D(QWaylandQuickItem);
1778 QQuickItem::setPosition(pos * d->scaleFactor());
1781#if QT_CONFIG(draganddrop)
1782void QWaylandQuickItem::handleDragStarted(QWaylandDrag *drag)
1784 Q_D(QWaylandQuickItem);
1785 Q_ASSERT(drag->origin() == surface());
1786 drag->seat()->setMouseFocus(
nullptr);
1787 d->isDragging =
true;
1793 qreal f = view->output() ? view->output()->scaleFactor() : 1;
1794#if !defined(Q_OS_MACOS)
1796 f /= window->devicePixelRatio();
1803 Q_Q(
const QWaylandQuickItem);
1804 auto *parent = q->parentItem();
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)
1819 Q_Q(QWaylandQuickItem);
1820 q->stackAfter(sibling);
1821 q->setZ(sibling->z());
1822 belowParent = sibling->d_func()->belowParent;
1827 Q_Q(QWaylandQuickItem);
1828 q->stackBefore(sibling);
1829 q->setZ(sibling->z());
1830 belowParent = sibling->d_func()->belowParent;
1836 Q_Q(QWaylandQuickItem);
1837 const auto siblings = q->parentItem()->childItems();
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;
1851 if (!foundSibling && siblings.last() != q)
1852 q->stackAfter(siblings.last());
1854 q->setZ(q->parentItem()->z());
1861 Q_Q(QWaylandQuickItem);
1862 const auto siblings = q->parentItem()->childItems();
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;
1876 if (!foundSibling && siblings.first() != q)
1877 q->stackBefore(siblings.first());
1879 q->setZ(q->parentItem()->z() - 1.0);
1885#include "moc_qwaylandquickitem.cpp"
qreal scaleFactor() const
QWaylandSurfaceTextureProvider()
QSGTexture * texture() const override
Returns a pointer to the texture object.
void setBufferRef(QWaylandQuickItem *surfaceItem, const QWaylandBufferRef &buffer)
void setSmooth(bool smooth)
~QWaylandSurfaceTextureProvider() override
void reportCompositorDestroyed()
static QImage withValidAlpha(const QImage &image)