File indexing completed on 2024-05-19 16:35:29

0001 /*
0002     SPDX-FileCopyrightText: 2014 Martin Gräßlin <mgraesslin@kde.org>
0003     SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 #pragma once
0008 
0009 #include "surface_interface.h"
0010 #include "utils.h"
0011 // Qt
0012 #include <QHash>
0013 #include <QVector>
0014 // Wayland
0015 #include "qwayland-server-wayland.h"
0016 
0017 namespace KWaylandServer
0018 {
0019 class IdleInhibitorV1Interface;
0020 class SurfaceRole;
0021 class ViewportInterface;
0022 class ContentTypeV1Interface;
0023 class TearingControlV1Interface;
0024 class FractionalScaleV1Interface;
0025 
0026 struct SurfaceState
0027 {
0028     void mergeInto(SurfaceState *target);
0029 
0030     QRegion damage = QRegion();
0031     QRegion bufferDamage = QRegion();
0032     QRegion opaque = QRegion();
0033     QRegion input = infiniteRegion();
0034     bool inputIsSet = false;
0035     bool opaqueIsSet = false;
0036     bool bufferIsSet = false;
0037     bool shadowIsSet = false;
0038     bool blurIsSet = false;
0039     bool contrastIsSet = false;
0040     bool slideIsSet = false;
0041     bool childrenChanged = false;
0042     bool bufferScaleIsSet = false;
0043     bool bufferTransformIsSet = false;
0044     bool contentTypeIsSet = false;
0045     bool tearingIsSet = false;
0046     qint32 bufferScale = 1;
0047     KWin::Output::Transform bufferTransform = KWin::Output::Transform::Normal;
0048     wl_list frameCallbacks;
0049     QPoint offset = QPoint();
0050     QPointer<ClientBuffer> buffer;
0051     QPointer<ShadowInterface> shadow;
0052     QPointer<BlurInterface> blur;
0053     QPointer<ContrastInterface> contrast;
0054     QPointer<SlideInterface> slide;
0055     KWin::ContentType contentType = KWin::ContentType::None;
0056     PresentationHint presentationHint = PresentationHint::VSync;
0057 
0058     // Subsurfaces are stored in two lists. The below list contains subsurfaces that
0059     // are below their parent surface; the above list contains subsurfaces that are
0060     // placed above the parent surface.
0061     QList<SubSurfaceInterface *> below;
0062     QList<SubSurfaceInterface *> above;
0063 
0064     struct
0065     {
0066         QRectF sourceGeometry = QRectF();
0067         QSize destinationSize = QSize();
0068         bool sourceGeometryIsSet = false;
0069         bool destinationSizeIsSet = false;
0070     } viewport;
0071 };
0072 
0073 class SurfaceInterfacePrivate : public QtWaylandServer::wl_surface
0074 {
0075 public:
0076     static SurfaceInterfacePrivate *get(SurfaceInterface *surface)
0077     {
0078         return surface->d.get();
0079     }
0080 
0081     explicit SurfaceInterfacePrivate(SurfaceInterface *q);
0082     ~SurfaceInterfacePrivate() override;
0083 
0084     void addChild(SubSurfaceInterface *subsurface);
0085     void removeChild(SubSurfaceInterface *subsurface);
0086     bool raiseChild(SubSurfaceInterface *subsurface, SurfaceInterface *anchor);
0087     bool lowerChild(SubSurfaceInterface *subsurface, SurfaceInterface *anchor);
0088     void setShadow(const QPointer<ShadowInterface> &shadow);
0089     void setBlur(const QPointer<BlurInterface> &blur);
0090     void setContrast(const QPointer<ContrastInterface> &contrast);
0091     void setSlide(const QPointer<SlideInterface> &slide);
0092     void installPointerConstraint(LockedPointerV1Interface *lock);
0093     void installPointerConstraint(ConfinedPointerV1Interface *confinement);
0094     void installIdleInhibitor(IdleInhibitorV1Interface *inhibitor);
0095 
0096     void commitToCache();
0097     void commitFromCache();
0098 
0099     void commitSubSurface();
0100     QMatrix4x4 buildSurfaceToBufferMatrix();
0101     void applyState(SurfaceState *next);
0102 
0103     bool computeEffectiveMapped() const;
0104     void updateEffectiveMapped();
0105 
0106     /**
0107      * Returns true if this surface (not including subsurfaces) contains a given point
0108      * @param position in surface-local co-ordiantes
0109      */
0110     bool contains(const QPointF &position) const;
0111     bool inputContains(const QPointF &position) const;
0112 
0113     CompositorInterface *compositor;
0114     SurfaceInterface *q;
0115     SurfaceRole *role = nullptr;
0116     SurfaceState current;
0117     SurfaceState pending;
0118     SurfaceState cached;
0119     SubSurfaceInterface *subSurface = nullptr;
0120     QMatrix4x4 surfaceToBufferMatrix;
0121     QMatrix4x4 bufferToSurfaceMatrix;
0122     QSize bufferSize = QSize(0, 0);
0123     QSizeF implicitSurfaceSize = QSizeF(0, 0);
0124     QSizeF surfaceSize = QSizeF(0, 0);
0125 
0126     QRegion inputRegion;
0127     QRegion opaqueRegion;
0128     ClientBuffer *bufferRef = nullptr;
0129     bool mapped = false;
0130     bool hasCacheState = false;
0131     qreal scaleOverride = 1.;
0132     qreal pendingScaleOverride = 1.;
0133 
0134     QVector<OutputInterface *> outputs;
0135     qreal preferredScale = 1.0;
0136 
0137     LockedPointerV1Interface *lockedPointer = nullptr;
0138     ConfinedPointerV1Interface *confinedPointer = nullptr;
0139     QHash<OutputInterface *, QMetaObject::Connection> outputDestroyedConnections;
0140     QHash<OutputInterface *, QMetaObject::Connection> outputBoundConnections;
0141 
0142     QVector<IdleInhibitorV1Interface *> idleInhibitors;
0143     ViewportInterface *viewportExtension = nullptr;
0144     std::unique_ptr<LinuxDmaBufV1Feedback> dmabufFeedbackV1;
0145     QPointer<ContentTypeV1Interface> contentTypeInterface;
0146     FractionalScaleV1Interface *fractionalScaleExtension = nullptr;
0147     ClientConnection *client = nullptr;
0148     TearingControlV1Interface *tearing = nullptr;
0149 
0150 protected:
0151     void surface_destroy_resource(Resource *resource) override;
0152     void surface_destroy(Resource *resource) override;
0153     void surface_attach(Resource *resource, struct ::wl_resource *buffer, int32_t x, int32_t y) override;
0154     void surface_damage(Resource *resource, int32_t x, int32_t y, int32_t width, int32_t height) override;
0155     void surface_frame(Resource *resource, uint32_t callback) override;
0156     void surface_set_opaque_region(Resource *resource, struct ::wl_resource *region) override;
0157     void surface_set_input_region(Resource *resource, struct ::wl_resource *region) override;
0158     void surface_commit(Resource *resource) override;
0159     void surface_set_buffer_transform(Resource *resource, int32_t transform) override;
0160     void surface_set_buffer_scale(Resource *resource, int32_t scale) override;
0161     void surface_damage_buffer(Resource *resource, int32_t x, int32_t y, int32_t width, int32_t height) override;
0162     void surface_offset(Resource *resource, int32_t x, int32_t y) override;
0163 
0164 private:
0165     QMetaObject::Connection constrainsOneShotConnection;
0166     QMetaObject::Connection constrainsUnboundConnection;
0167 };
0168 
0169 } // namespace KWaylandServer