kwin/src/composite.h

259 lines
6.7 KiB
C
Raw Normal View History

2020-08-02 22:22:19 +00:00
/*
KWin - the KDE window manager
This file is part of the KDE project.
2020-08-02 22:22:19 +00:00
SPDX-FileCopyrightText: 2011 Arthur Arlt <a.arlt@stud.uni-heidelberg.de>
SPDX-FileCopyrightText: 2012 Martin Gräßlin <mgraesslin@kde.org>
2020-08-02 22:22:19 +00:00
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <kwinglobals.h>
#include <QObject>
#include <QTimer>
#include <QRegion>
namespace KWin
{
2020-11-19 08:52:29 +00:00
class AbstractOutput;
class CompositorSelectionOwner;
class CursorView;
class RenderBackend;
class RenderLayer;
2020-11-19 08:52:29 +00:00
class RenderLoop;
class Scene;
class Toplevel;
class X11Client;
class X11SyncManager;
class KWIN_EXPORT Compositor : public QObject
{
Q_OBJECT
public:
enum class State {
On = 0,
Off,
Starting,
Stopping
};
~Compositor() override;
static Compositor *self();
/**
* Schedules a new repaint if no repaint is currently scheduled.
*/
void scheduleRepaint();
/**
* Toggles compositing, that is if the Compositor is suspended it will be resumed
* and if the Compositor is active it will be suspended.
* Invoked by keybinding (shortcut default: Shift + Alt + F12).
*/
virtual void toggleCompositing() = 0;
/**
* Re-initializes the Compositor completely.
* Connected to the D-Bus signal org.kde.KWin /KWin reinitCompositing
*/
virtual void reinitialize();
/**
* Whether the Compositor is active. That is a Scene is present and the Compositor is
* not shutting down itself.
*/
bool isActive();
Scene *scene() const {
return m_scene;
}
RenderBackend *backend() const {
return m_backend;
}
/**
* @brief Static check to test whether the Compositor is available and active.
*
* @return bool @c true if there is a Compositor and it is active, @c false otherwise
*/
static bool compositing() {
return s_compositor != nullptr && s_compositor->isActive();
}
// for delayed supportproperty management of effects
void keepSupportProperty(xcb_atom_t atom);
void removeSupportProperty(xcb_atom_t atom);
Q_SIGNALS:
void compositingToggled(bool active);
void aboutToDestroy();
void aboutToToggleCompositing();
void sceneCreated();
protected:
explicit Compositor(QObject *parent = nullptr);
virtual void start() = 0;
virtual void stop();
/**
* @brief Prepares start.
* @return bool @c true if start should be continued and @c if not.
*/
bool setupStart();
/**
* Continues the startup after Scene And Workspace are created
*/
void startupWithWorkspace();
virtual void configChanged();
void destroyCompositorSelection();
static Compositor *s_compositor;
2020-11-19 08:52:29 +00:00
protected Q_SLOTS:
virtual void composite(RenderLoop *renderLoop);
2020-11-19 08:52:29 +00:00
private Q_SLOTS:
void handleFrameRequested(RenderLoop *renderLoop);
2020-11-19 08:52:29 +00:00
private:
void initializeX11();
void cleanupX11();
void releaseCompositorSelection();
void deleteUnusedSupportProperties();
bool attemptOpenGLCompositing();
bool attemptQPainterCompositing();
AbstractOutput *findOutput(RenderLoop *loop) const;
void addOutput(AbstractOutput *output);
void removeOutput(AbstractOutput *output);
void addSuperLayer(RenderLayer *layer);
void removeSuperLayer(RenderLayer *layer);
void prePaintPass(RenderLayer *layer);
void postPaintPass(RenderLayer *layer);
void preparePaintPass(RenderLayer *layer, QRegion *repaint);
void paintPass(RenderLayer *layer, const QRegion &repaint, const QRegion &repair, QRegion *surfaceDamage, QRegion *bufferDamage);
State m_state = State::Off;
CompositorSelectionOwner *m_selectionOwner = nullptr;
QTimer m_releaseSelectionTimer;
QList<xcb_atom_t> m_unusedSupportProperties;
QTimer m_unusedSupportPropertyTimer;
Scene *m_scene = nullptr;
CursorView *m_cursorView = nullptr;
RenderBackend *m_backend = nullptr;
QHash<RenderLoop *, RenderLayer *> m_superlayers;
};
class KWIN_EXPORT WaylandCompositor final : public Compositor
{
Q_OBJECT
public:
static WaylandCompositor *create(QObject *parent = nullptr);
~WaylandCompositor() override;
void toggleCompositing() override;
protected:
void start() override;
private:
explicit WaylandCompositor(QObject *parent);
};
class KWIN_EXPORT X11Compositor final : public Compositor
{
Q_OBJECT
public:
enum SuspendReason {
NoReasonSuspend = 0,
UserSuspend = 1 << 0,
BlockRuleSuspend = 1 << 1,
ScriptSuspend = 1 << 2,
AllReasonSuspend = 0xff
};
Q_DECLARE_FLAGS(SuspendReasons, SuspendReason)
Q_ENUM(SuspendReason)
Q_FLAG(SuspendReasons)
static X11Compositor *create(QObject *parent = nullptr);
~X11Compositor() override;
X11SyncManager *syncManager() const;
/**
* @brief Suspends the Compositor if it is currently active.
*
* Note: it is possible that the Compositor is not able to suspend. Use isActive to check
* whether the Compositor has been suspended.
*
* @return void
* @see resume
* @see isActive
2019-08-30 20:30:42 +00:00
*/
void suspend(SuspendReason reason);
/**
* @brief Resumes the Compositor if it is currently suspended.
*
* Note: it is possible that the Compositor cannot be resumed, that is there might be Clients
* blocking the usage of Compositing or the Scene might be broken. Use isActive to check
* whether the Compositor has been resumed. Also check isCompositingPossible and
* isOpenGLBroken.
*
* Note: The starting of the Compositor can require some time and is partially done threaded.
* After this method returns the setup may not have been completed.
*
* @return void
* @see suspend
* @see isActive
* @see isCompositingPossible
* @see isOpenGLBroken
2019-08-30 20:30:42 +00:00
*/
void resume(SuspendReason reason);
void toggleCompositing() override;
void reinitialize() override;
void configChanged() override;
/**
* Checks whether @p w is the Scene's overlay window.
*/
bool checkForOverlayWindow(WId w) const;
/**
* @returns Whether the Scene's Overlay X Window is visible.
2019-08-30 20:30:42 +00:00
*/
bool isOverlayWindowVisible() const;
void updateClientCompositeBlocking(X11Client *client = nullptr);
[x11] Fix crash during tear down Summary: Any call made to a virtual method in constructor/destructor of a base class won't go to a derived class because the base class may access uninitialized or destroyed resources. For example, let's consider the following two classes class Base { public: Base() { foo()->bar(); } virtual ~Base() { foo()->bar(); } virtual Foo* foo() const { return nullptr; } }; class Derived : public Base { public: Derived() : mFoo(new Foo) {} ~Derived() override { delete mFoo; } Foo* foo() const override { return mFoo; } private: Foo* mFoo; }; When an instance of Derived class is created, constructors will run in the following order: Base() Derived() It's not safe to dispatch foo() method call to Derived class because constructor of Derived hasn't initialized yet mFoo. Same story with destructors, they'll run in the following order: ~Derived() ~Base() It's not safe to dispatch foo() method call in the destructor of Base class to Derived class because mFoo was deleted. So, what does that weird C++ behavior has something to do with KWin? Well, recently Compositor class was split into two classes - WaylandCompositor, and X11Compositor. Some functionality from X11 doesn't make sense on Wayland. Therefore methods that implement that stuff were "purified," i.e. they became pure virtual methods. Unfortunately, when Compositor tears down it may call pure virtual methods on itself. Given that those calls cannot be dispatched to X11Compositor or WaylandCompositor, the only choice that C++ runtime has is to throw an exception. The fix for this very delicate problem is very simple - do not call virtual methods from constructors and the destructor. Avoid doing that if you can! This change moves Compositor::updateClientCompositeBlocking to X11Compositor so it longer has to be a virtual method. Also, it kind of doesn't make sense to keep it in base Compositor class because compositing can be blocked only on X11. BUG: 411049 Test Plan: KWin no longer crashes when running kwin_x11 --replace command. Reviewers: #kwin, romangg Reviewed By: #kwin, romangg Subscribers: anthonyfieroni, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D23098
2019-08-30 16:28:50 +00:00
static X11Compositor *self();
protected:
void start() override;
void stop() override;
void composite(RenderLoop *renderLoop) override;
private:
explicit X11Compositor(QObject *parent);
QScopedPointer<X11SyncManager> m_syncManager;
/**
* Whether the Compositor is currently suspended, 8 bits encoding the reason
2019-08-30 20:30:42 +00:00
*/
SuspendReasons m_suspended;
int m_framesToTestForSafety = 3;
};
}