kwin/composite.h
Vlad Zahorodnii b8a70e62d5 Introduce RenderLoop
At the moment, our frame scheduling infrastructure is still heavily
based on Xinerama-style rendering. Specifically, we assume that painting
is driven by a single timer, etc.

This change introduces a new type - RenderLoop. Its main purpose is to
drive compositing on a specific output, or in case of X11, on the
overlay window.

With RenderLoop, compositing is synchronized to vblank events. It
exposes the last and the next estimated presentation timestamp. The
expected presentation timestamp can be used by effects to ensure that
animations are synchronized with the upcoming vblank event.

On Wayland, every outputs has its own render loop. On X11, per screen
rendering is not possible, therefore the platform exposes the render
loop for the overlay window. Ideally, the Scene has to expose the
RenderLoop, but as the first step towards better compositing scheduling
it's good as is for the time being.

The RenderLoop tries to minimize the latency by delaying compositing as
close as possible to the next vblank event. One tricky thing about it is
that if compositing is too close to the next vblank event, animations
may become a little bit choppy. However, increasing the latency reduces
the choppiness.

Given that, there is no any "silver bullet" solution for the choppiness
issue, a new option has been added in the Compositing KCM to specify the
amount of latency. By default, it's "Medium," but if a user is not
satisfied with the upstream default, they can tweak it.
2021-01-06 16:59:29 +00:00

240 lines
6.2 KiB
C++

/*
KWin - the KDE window manager
This file is part of the KDE project.
SPDX-FileCopyrightText: 2011 Arthur Arlt <a.arlt@stud.uni-heidelberg.de>
SPDX-FileCopyrightText: 2012 Martin Gräßlin <mgraesslin@kde.org>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <kwinglobals.h>
#include <QObject>
#include <QTimer>
#include <QRegion>
namespace KWin
{
class AbstractOutput;
class CompositorSelectionOwner;
class RenderLoop;
class Scene;
class X11Client;
class KWIN_EXPORT Compositor : public QObject
{
Q_OBJECT
public:
enum class State {
On = 0,
Off,
Starting,
Stopping
};
~Compositor() override;
static Compositor *self();
// when adding repaints caused by a window, you probably want to use
// either Toplevel::addRepaint() or Toplevel::addWorkspaceRepaint()
void addRepaint(const QRect& r);
void addRepaint(const QRegion& r);
void addRepaint(int x, int y, int w, int h);
void addRepaintFull();
/**
* 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;
}
/**
* @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;
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;
protected Q_SLOTS:
virtual void handleFrameRequested(RenderLoop *renderLoop);
private Q_SLOTS:
void handleOutputEnabled(AbstractOutput *output);
void handleOutputDisabled(AbstractOutput *output);
private:
void initializeX11();
void cleanupX11();
void releaseCompositorSelection();
void deleteUnusedSupportProperties();
int screenForRenderLoop(RenderLoop *renderLoop) const;
void registerRenderLoop(RenderLoop *renderLoop, AbstractOutput *output);
void unregisterRenderLoop(RenderLoop *renderLoop);
State m_state;
CompositorSelectionOwner *m_selectionOwner;
QTimer m_releaseSelectionTimer;
QList<xcb_atom_t> m_unusedSupportProperties;
QTimer m_unusedSupportPropertyTimer;
Scene *m_scene;
int m_framesToTestForSafety = 3;
QMap<RenderLoop *, AbstractOutput *> m_renderLoops;
};
class KWIN_EXPORT WaylandCompositor : public Compositor
{
Q_OBJECT
public:
static WaylandCompositor *create(QObject *parent = nullptr);
void toggleCompositing() override;
protected:
void start() override;
private:
explicit WaylandCompositor(QObject *parent);
};
class KWIN_EXPORT X11Compositor : 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);
/**
* @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
*/
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
*/
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.
*/
bool isOverlayWindowVisible() const;
void updateClientCompositeBlocking(X11Client *client = nullptr);
static X11Compositor *self();
protected:
void start() override;
void handleFrameRequested(RenderLoop *renderLoop) override;
private:
explicit X11Compositor(QObject *parent);
/**
* Whether the Compositor is currently suspended, 8 bits encoding the reason
*/
SuspendReasons m_suspended;
};
}