File indexing completed on 2024-05-19 05:32:44

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 "core/graphicsbuffer.h"
0010 #include "surface.h"
0011 // Qt
0012 #include <QHash>
0013 #include <QList>
0014 #include <QPointer>
0015 // Wayland
0016 #include "qwayland-server-wayland.h"
0017 // C++
0018 #include <deque>
0019 #include <optional>
0020 
0021 namespace KWin
0022 {
0023 class IdleInhibitorV1Interface;
0024 class ViewportInterface;
0025 class ContentTypeV1Interface;
0026 class TearingControlV1Interface;
0027 class FractionalScaleV1Interface;
0028 class FrogColorManagementSurfaceV1;
0029 class PresentationTimeFeedback;
0030 class XXColorSurfaceV2;
0031 
0032 struct SurfaceState
0033 {
0034     SurfaceState();
0035     SurfaceState(const SurfaceState &cpy) = delete;
0036     ~SurfaceState();
0037     SurfaceState &operator=(SurfaceState &&mv) = default;
0038 
0039     void mergeInto(SurfaceState *target);
0040 
0041     quint32 serial = 0;
0042 
0043     QRegion damage = QRegion();
0044     QRegion bufferDamage = QRegion();
0045     QRegion opaque = QRegion();
0046     QRegion input = infiniteRegion();
0047     bool inputIsSet = false;
0048     bool opaqueIsSet = false;
0049     bool bufferIsSet = false;
0050     bool shadowIsSet = false;
0051     bool blurIsSet = false;
0052     bool contrastIsSet = false;
0053     bool slideIsSet = false;
0054     bool subsurfaceOrderChanged = false;
0055     bool subsurfacePositionChanged = false;
0056     bool bufferScaleIsSet = false;
0057     bool bufferTransformIsSet = false;
0058     bool contentTypeIsSet = false;
0059     bool presentationModeHintIsSet = false;
0060     bool colorDescriptionIsSet = false;
0061     qint32 bufferScale = 1;
0062     OutputTransform bufferTransform = OutputTransform::Normal;
0063     wl_list frameCallbacks;
0064     QPoint offset = QPoint();
0065     QPointer<GraphicsBuffer> buffer;
0066     QPointer<ShadowInterface> shadow;
0067     QPointer<BlurInterface> blur;
0068     QPointer<ContrastInterface> contrast;
0069     QPointer<SlideInterface> slide;
0070     ContentType contentType = ContentType::None;
0071     PresentationModeHint presentationHint = PresentationModeHint::VSync;
0072     ColorDescription colorDescription = ColorDescription::sRGB;
0073     std::unique_ptr<PresentationTimeFeedback> presentationFeedback;
0074 
0075     struct
0076     {
0077         // Subsurfaces are stored in two lists. The below list contains subsurfaces that
0078         // are below their parent surface; the above list contains subsurfaces that are
0079         // placed above the parent surface.
0080         QList<SubSurfaceInterface *> below;
0081         QList<SubSurfaceInterface *> above;
0082 
0083         // Subsurface position is here becase it is a part of the parent surface's state.
0084         QHash<SubSurfaceInterface *, QPoint> position;
0085     } subsurface;
0086 
0087     struct
0088     {
0089         QRectF sourceGeometry = QRectF();
0090         QSize destinationSize = QSize();
0091         bool sourceGeometryIsSet = false;
0092         bool destinationSizeIsSet = false;
0093     } viewport;
0094 };
0095 
0096 class SurfaceInterfacePrivate : public QtWaylandServer::wl_surface
0097 {
0098 public:
0099     static SurfaceInterfacePrivate *get(SurfaceInterface *surface)
0100     {
0101         return surface->d.get();
0102     }
0103 
0104     explicit SurfaceInterfacePrivate(SurfaceInterface *q);
0105 
0106     void addChild(SubSurfaceInterface *subsurface);
0107     void removeChild(SubSurfaceInterface *subsurface);
0108     bool raiseChild(SubSurfaceInterface *subsurface, SurfaceInterface *anchor);
0109     bool lowerChild(SubSurfaceInterface *subsurface, SurfaceInterface *anchor);
0110     void setShadow(const QPointer<ShadowInterface> &shadow);
0111     void setBlur(const QPointer<BlurInterface> &blur);
0112     void setContrast(const QPointer<ContrastInterface> &contrast);
0113     void setSlide(const QPointer<SlideInterface> &slide);
0114     void installPointerConstraint(LockedPointerV1Interface *lock);
0115     void installPointerConstraint(ConfinedPointerV1Interface *confinement);
0116     void installIdleInhibitor(IdleInhibitorV1Interface *inhibitor);
0117 
0118     QRectF computeBufferSourceBox() const;
0119     void applyState(SurfaceState *next);
0120 
0121     bool computeEffectiveMapped() const;
0122     void updateEffectiveMapped();
0123 
0124     /**
0125      * Returns true if this surface (not including subsurfaces) contains a given point
0126      * @param position in surface-local co-ordiantes
0127      */
0128     bool contains(const QPointF &position) const;
0129     bool inputContains(const QPointF &position) const;
0130     QRegion mapToBuffer(const QRegion &region) const;
0131 
0132     CompositorInterface *compositor;
0133     SurfaceInterface *q;
0134     SurfaceRole *role = nullptr;
0135     std::unique_ptr<SurfaceState> current;
0136     std::unique_ptr<SurfaceState> pending;
0137     QSize bufferSize = QSize(0, 0);
0138     QRectF bufferSourceBox;
0139     QSizeF surfaceSize = QSizeF(0, 0);
0140 
0141     QRegion inputRegion;
0142     QRegion opaqueRegion;
0143     GraphicsBufferRef bufferRef;
0144     QRegion bufferDamage;
0145     bool mapped = false;
0146     qreal scaleOverride = 1.;
0147     qreal pendingScaleOverride = 1.;
0148 
0149     Transaction *firstTransaction = nullptr;
0150     Transaction *lastTransaction = nullptr;
0151 
0152     QList<OutputInterface *> outputs;
0153     QPointer<OutputInterface> primaryOutput;
0154     std::optional<qreal> preferredBufferScale;
0155     std::optional<OutputTransform> preferredBufferTransform;
0156     std::optional<ColorDescription> preferredColorDescription;
0157 
0158     LockedPointerV1Interface *lockedPointer = nullptr;
0159     ConfinedPointerV1Interface *confinedPointer = nullptr;
0160     QHash<OutputInterface *, QMetaObject::Connection> outputDestroyedConnections;
0161     QHash<OutputInterface *, QMetaObject::Connection> outputBoundConnections;
0162 
0163     QList<IdleInhibitorV1Interface *> idleInhibitors;
0164     ViewportInterface *viewportExtension = nullptr;
0165     std::unique_ptr<LinuxDmaBufV1Feedback> dmabufFeedbackV1;
0166     QPointer<ContentTypeV1Interface> contentTypeInterface;
0167     FractionalScaleV1Interface *fractionalScaleExtension = nullptr;
0168     ClientConnection *client = nullptr;
0169     TearingControlV1Interface *tearing = nullptr;
0170     FrogColorManagementSurfaceV1 *frogColorManagement = nullptr;
0171     XXColorSurfaceV2 *xxColorSurface = nullptr;
0172 
0173     struct
0174     {
0175         SubSurfaceInterface *handle = nullptr;
0176         std::unique_ptr<Transaction> transaction;
0177     } subsurface;
0178 
0179     std::vector<std::unique_ptr<PresentationTimeFeedback>> pendingPresentationFeedbacks;
0180 
0181 protected:
0182     void surface_destroy_resource(Resource *resource) override;
0183     void surface_destroy(Resource *resource) override;
0184     void surface_attach(Resource *resource, struct ::wl_resource *buffer, int32_t x, int32_t y) override;
0185     void surface_damage(Resource *resource, int32_t x, int32_t y, int32_t width, int32_t height) override;
0186     void surface_frame(Resource *resource, uint32_t callback) override;
0187     void surface_set_opaque_region(Resource *resource, struct ::wl_resource *region) override;
0188     void surface_set_input_region(Resource *resource, struct ::wl_resource *region) override;
0189     void surface_commit(Resource *resource) override;
0190     void surface_set_buffer_transform(Resource *resource, int32_t transform) override;
0191     void surface_set_buffer_scale(Resource *resource, int32_t scale) override;
0192     void surface_damage_buffer(Resource *resource, int32_t x, int32_t y, int32_t width, int32_t height) override;
0193     void surface_offset(Resource *resource, int32_t x, int32_t y) override;
0194 
0195 private:
0196     QMetaObject::Connection constrainsOneShotConnection;
0197     QMetaObject::Connection constrainsUnboundConnection;
0198 };
0199 
0200 } // namespace KWin