kwin/src/composite.cpp

885 lines
26 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: 2006 Lubos Lunak <l.lunak@kde.org>
2020-08-02 22:22:19 +00:00
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "composite.h"
2020-11-19 08:52:29 +00:00
#include "abstract_output.h"
#include "dbusinterface.h"
#include "x11client.h"
#include "decorations/decoratedclient.h"
#include "deleted.h"
#include "effects.h"
#include "ftrace.h"
#include "internal_client.h"
#include "overlaywindow.h"
#include "platform.h"
2020-11-19 08:52:29 +00:00
#include "renderloop.h"
#include "scene.h"
#include "screens.h"
#include "shadow.h"
#include "surfaceitem_x11.h"
#include "unmanaged.h"
#include "useractions.h"
#include "utils.h"
#include "wayland_server.h"
#include "workspace.h"
#include "xcbutils.h"
#include <kwingltexture.h>
#include <KWaylandServer/surface_interface.h>
#include <KGlobalAccel>
#include <KLocalizedString>
Move SceneXRender into a plugin Summary: First step for loading the compositor Scenes through plugins. The general idea is that we currently needlessly pull in all the Scenes although only one will be used. E.g. on X11 we pull in QPainter, although they are not compatible. On Wayland we pull in XRender although they are not compatible. Furthermore our current Scene creation strategy is not really fault tolerant and can create situations where we don't get a compositor. E.g on fbdev backend the default settings won't work as it does not support OpenGL. Long term I want to tackle those conceptional problems together: we try to load all plugins supported by the current platform till we have a scene which works. Thus on Wayland we don't end up in a situation where we don't have a working compositor because the configuration is bad. To make this possible the switch statement in the Scene needs to go and needs to be replaced by a for loop iterating over all the available scenes on the platform. If we go there it makes sense to replace it directly with a plugin based approach. So this is a change which tackles the problem by first introducing the plugin loading. The xrender based scene (as it's the most simple one) is moved into a plugin. It is first tried to find a scene plugin and only if there is none the existing code is used. Test Plan: Tested all scenes Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D7232
2017-08-10 16:13:42 +00:00
#include <KPluginLoader>
#include <KPluginMetaData>
#include <KNotification>
#include <KSelectionOwner>
#include <QDateTime>
#include <QFutureWatcher>
#include <QMenu>
#include <QOpenGLContext>
#include <QQuickWindow>
#include <QtConcurrentRun>
#include <QTextStream>
#include <QTimerEvent>
2012-12-11 20:21:35 +00:00
#include <xcb/composite.h>
#include <xcb/damage.h>
#include <cstdio>
Q_DECLARE_METATYPE(KWin::X11Compositor::SuspendReason)
namespace KWin
{
// See main.cpp:
extern int screen_number;
extern bool is_multihead;
Compositor *Compositor::s_compositor = nullptr;
Compositor *Compositor::self()
{
return s_compositor;
}
WaylandCompositor *WaylandCompositor::create(QObject *parent)
{
Q_ASSERT(!s_compositor);
auto *compositor = new WaylandCompositor(parent);
s_compositor = compositor;
return compositor;
}
X11Compositor *X11Compositor::create(QObject *parent)
{
Q_ASSERT(!s_compositor);
auto *compositor = new X11Compositor(parent);
s_compositor = compositor;
return compositor;
}
class CompositorSelectionOwner : public KSelectionOwner
{
Q_OBJECT
public:
CompositorSelectionOwner(const char *selection)
: KSelectionOwner(selection, connection(), rootWindow())
, m_owning(false)
{
connect (this, &CompositorSelectionOwner::lostOwnership,
this, [this]() { m_owning = false; });
}
bool owning() const {
return m_owning;
}
void setOwning(bool own) {
m_owning = own;
}
private:
bool m_owning;
};
Compositor::Compositor(QObject* workspace)
: QObject(workspace)
, m_state(State::Off)
, m_selectionOwner(nullptr)
, m_scene(nullptr)
{
connect(options, &Options::configChanged, this, &Compositor::configChanged);
connect(options, &Options::animationSpeedChanged, this, &Compositor::configChanged);
// 2 sec which should be enough to restart the compositor.
static const int compositorLostMessageDelay = 2000;
m_releaseSelectionTimer.setSingleShot(true);
m_releaseSelectionTimer.setInterval(compositorLostMessageDelay);
connect(&m_releaseSelectionTimer, &QTimer::timeout,
this, &Compositor::releaseCompositorSelection);
m_unusedSupportPropertyTimer.setInterval(compositorLostMessageDelay);
m_unusedSupportPropertyTimer.setSingleShot(true);
connect(&m_unusedSupportPropertyTimer, &QTimer::timeout,
this, &Compositor::deleteUnusedSupportProperties);
// Delay the call to start by one event cycle.
// The ctor of this class is invoked from the Workspace ctor, that means before
// Workspace is completely constructed, so calling Workspace::self() would result
// in undefined behavior. This is fixed by using a delayed invocation.
if (kwinApp()->platform()->isReady()) {
QTimer::singleShot(0, this, &Compositor::start);
}
connect(kwinApp()->platform(), &Platform::readyChanged, this,
[this] (bool ready) {
if (ready) {
start();
} else {
stop();
}
}, Qt::QueuedConnection
);
// register DBus
new CompositorDBusInterface(this);
FTraceLogger::create();
}
Compositor::~Compositor()
{
Q_EMIT aboutToDestroy();
stop();
deleteUnusedSupportProperties();
destroyCompositorSelection();
s_compositor = nullptr;
}
bool Compositor::setupStart()
2011-01-30 14:34:42 +00:00
{
if (kwinApp()->isTerminating()) {
// Don't start while KWin is terminating. An event to restart might be lingering
// in the event queue due to graphics reset.
return false;
}
if (m_state != State::Off) {
return false;
2011-01-30 14:34:42 +00:00
}
m_state = State::Starting;
options->reloadCompositingSettings(true);
initializeX11();
// There might still be a deleted around, needs to be cleared before
// creating the scene (BUG 333275).
if (Workspace::self()) {
while (!Workspace::self()->deletedList().isEmpty()) {
Workspace::self()->deletedList().first()->discard();
}
}
Q_EMIT aboutToToggleCompositing();
auto supportedCompositors = kwinApp()->platform()->supportedCompositors();
const auto userConfigIt = std::find(supportedCompositors.begin(), supportedCompositors.end(),
options->compositingMode());
if (userConfigIt != supportedCompositors.end()) {
supportedCompositors.erase(userConfigIt);
supportedCompositors.prepend(options->compositingMode());
} else {
qCWarning(KWIN_CORE)
<< "Configured compositor not supported by Platform. Falling back to defaults";
}
Move SceneXRender into a plugin Summary: First step for loading the compositor Scenes through plugins. The general idea is that we currently needlessly pull in all the Scenes although only one will be used. E.g. on X11 we pull in QPainter, although they are not compatible. On Wayland we pull in XRender although they are not compatible. Furthermore our current Scene creation strategy is not really fault tolerant and can create situations where we don't get a compositor. E.g on fbdev backend the default settings won't work as it does not support OpenGL. Long term I want to tackle those conceptional problems together: we try to load all plugins supported by the current platform till we have a scene which works. Thus on Wayland we don't end up in a situation where we don't have a working compositor because the configuration is bad. To make this possible the switch statement in the Scene needs to go and needs to be replaced by a for loop iterating over all the available scenes on the platform. If we go there it makes sense to replace it directly with a plugin based approach. So this is a change which tackles the problem by first introducing the plugin loading. The xrender based scene (as it's the most simple one) is moved into a plugin. It is first tried to find a scene plugin and only if there is none the existing code is used. Test Plan: Tested all scenes Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D7232
2017-08-10 16:13:42 +00:00
const auto availablePlugins = KPluginLoader::findPlugins(QStringLiteral("org.kde.kwin.scenes"));
2011-01-30 14:34:42 +00:00
for (const KPluginMetaData &pluginMetaData : availablePlugins) {
qCDebug(KWIN_CORE) << "Available scene plugin:" << pluginMetaData.fileName();
}
for (auto type : qAsConst(supportedCompositors)) {
switch (type) {
case OpenGLCompositing:
case OpenGL2Compositing:
qCDebug(KWIN_CORE) << "Attempting to load the OpenGL scene";
break;
case QPainterCompositing:
qCDebug(KWIN_CORE) << "Attempting to load the QPainter scene";
break;
case NoCompositing:
Q_UNREACHABLE();
}
const auto pluginIt = std::find_if(availablePlugins.begin(), availablePlugins.end(),
[type] (const auto &plugin) {
const auto &metaData = plugin.rawData();
auto it = metaData.find(QStringLiteral("CompositingType"));
if (it != metaData.end()) {
if ((*it).toInt() == int{type}) {
return true;
}
}
return false;
});
if (pluginIt != availablePlugins.end()) {
std::unique_ptr<SceneFactory>
factory{ qobject_cast<SceneFactory*>(pluginIt->instantiate()) };
if (factory) {
m_scene = factory->create(this);
if (m_scene) {
if (!m_scene->initFailed()) {
qCDebug(KWIN_CORE) << "Instantiated compositing plugin:"
<< pluginIt->name();
break;
} else {
delete m_scene;
m_scene = nullptr;
}
Move SceneXRender into a plugin Summary: First step for loading the compositor Scenes through plugins. The general idea is that we currently needlessly pull in all the Scenes although only one will be used. E.g. on X11 we pull in QPainter, although they are not compatible. On Wayland we pull in XRender although they are not compatible. Furthermore our current Scene creation strategy is not really fault tolerant and can create situations where we don't get a compositor. E.g on fbdev backend the default settings won't work as it does not support OpenGL. Long term I want to tackle those conceptional problems together: we try to load all plugins supported by the current platform till we have a scene which works. Thus on Wayland we don't end up in a situation where we don't have a working compositor because the configuration is bad. To make this possible the switch statement in the Scene needs to go and needs to be replaced by a for loop iterating over all the available scenes on the platform. If we go there it makes sense to replace it directly with a plugin based approach. So this is a change which tackles the problem by first introducing the plugin loading. The xrender based scene (as it's the most simple one) is moved into a plugin. It is first tried to find a scene plugin and only if there is none the existing code is used. Test Plan: Tested all scenes Reviewers: #kwin, #plasma Subscribers: plasma-devel, kwin Tags: #kwin Differential Revision: https://phabricator.kde.org/D7232
2017-08-10 16:13:42 +00:00
}
}
}
}
if (m_scene == nullptr || m_scene->initFailed()) {
qCCritical(KWIN_CORE) << "Failed to initialize compositing, compositing disabled";
m_state = State::Off;
delete m_scene;
m_scene = nullptr;
if (m_selectionOwner) {
m_selectionOwner->setOwning(false);
m_selectionOwner->release();
}
if (!supportedCompositors.contains(NoCompositing)) {
qCCritical(KWIN_CORE) << "The used windowing system requires compositing";
qCCritical(KWIN_CORE) << "We are going to quit KWin now as it is broken";
qApp->quit();
}
return false;
2011-01-30 14:34:42 +00:00
}
CompositingType compositingType = m_scene->compositingType();
if (compositingType & OpenGLCompositing) {
// Override for OpenGl sub-type OpenGL2Compositing.
compositingType = OpenGLCompositing;
}
kwinApp()->platform()->setSelectedCompositor(compositingType);
if (!Workspace::self() && m_scene && m_scene->compositingType() == QPainterCompositing) {
// Force Software QtQuick on first startup with QPainter.
QQuickWindow::setSceneGraphBackend(QSGRendererInterface::Software);
}
connect(m_scene, &Scene::resetCompositing, this, &Compositor::reinitialize);
Q_EMIT sceneCreated();
return true;
}
void Compositor::initializeX11()
{
xcb_connection_t *connection = kwinApp()->x11Connection();
if (!connection) {
return;
}
if (!m_selectionOwner) {
char selection_name[ 100 ];
sprintf(selection_name, "_NET_WM_CM_S%d", Application::x11ScreenNumber());
m_selectionOwner = new CompositorSelectionOwner(selection_name);
connect(m_selectionOwner, &CompositorSelectionOwner::lostOwnership,
this, &Compositor::stop);
}
if (!m_selectionOwner->owning()) {
// Force claim ownership.
m_selectionOwner->claim(true);
m_selectionOwner->setOwning(true);
}
xcb_composite_redirect_subwindows(connection, kwinApp()->x11RootWindow(),
XCB_COMPOSITE_REDIRECT_MANUAL);
}
void Compositor::cleanupX11()
{
delete m_selectionOwner;
m_selectionOwner = nullptr;
}
void Compositor::startupWithWorkspace()
{
connect(kwinApp(), &Application::x11ConnectionChanged,
this, &Compositor::initializeX11, Qt::UniqueConnection);
connect(kwinApp(), &Application::x11ConnectionAboutToBeDestroyed,
this, &Compositor::cleanupX11, Qt::UniqueConnection);
initializeX11();
2017-06-21 19:10:12 +00:00
Workspace::self()->markXStackingOrderAsDirty();
Q_ASSERT(m_scene);
2020-11-19 08:52:29 +00:00
const Platform *platform = kwinApp()->platform();
if (platform->isPerScreenRenderingEnabled()) {
const QVector<AbstractOutput *> outputs = platform->enabledOutputs();
for (AbstractOutput *output : outputs) {
registerRenderLoop(output->renderLoop(), output);
}
connect(platform, &Platform::outputEnabled,
this, &Compositor::handleOutputEnabled);
connect(platform, &Platform::outputDisabled,
this, &Compositor::handleOutputDisabled);
} else {
2020-11-19 08:52:29 +00:00
registerRenderLoop(platform->renderLoop(), nullptr);
}
// Sets also the 'effects' pointer.
kwinApp()->platform()->createEffectsHandler(this, m_scene);
connect(Workspace::self(), &Workspace::deletedRemoved, m_scene, &Scene::removeToplevel);
connect(effects, &EffectsHandler::screenGeometryChanged, this, &Compositor::addRepaintFull);
for (X11Client *c : Workspace::self()->clientList()) {
c->setupCompositing();
c->updateShadow();
}
for (Unmanaged *c : Workspace::self()->unmanagedList()) {
c->setupCompositing();
c->updateShadow();
}
for (InternalClient *client : workspace()->internalClients()) {
client->setupCompositing();
client->updateShadow();
}
if (auto *server = waylandServer()) {
const auto clients = server->clients();
for (AbstractClient *c : clients) {
c->setupCompositing();
c->updateShadow();
}
}
m_state = State::On;
Q_EMIT compositingToggled(true);
if (m_releaseSelectionTimer.isActive()) {
m_releaseSelectionTimer.stop();
}
// Render at least once.
addRepaintFull();
2020-11-19 08:52:29 +00:00
}
void Compositor::registerRenderLoop(RenderLoop *renderLoop, AbstractOutput *output)
{
Q_ASSERT(!m_renderLoops.contains(renderLoop));
m_renderLoops.insert(renderLoop, output);
connect(renderLoop, &RenderLoop::frameRequested, this, &Compositor::handleFrameRequested);
}
void Compositor::unregisterRenderLoop(RenderLoop *renderLoop)
{
Q_ASSERT(m_renderLoops.contains(renderLoop));
m_renderLoops.remove(renderLoop);
disconnect(renderLoop, &RenderLoop::frameRequested, this, &Compositor::handleFrameRequested);
}
void Compositor::handleOutputEnabled(AbstractOutput *output)
{
registerRenderLoop(output->renderLoop(), output);
}
void Compositor::handleOutputDisabled(AbstractOutput *output)
{
unregisterRenderLoop(output->renderLoop());
}
int Compositor::screenForRenderLoop(RenderLoop *renderLoop) const
{
Q_ASSERT(m_renderLoops.contains(renderLoop));
AbstractOutput *output = m_renderLoops.value(renderLoop);
if (!output) {
return -1;
}
return kwinApp()->platform()->enabledOutputs().indexOf(output);
2011-01-30 14:34:42 +00:00
}
void Compositor::scheduleRepaint()
{
2020-11-19 08:52:29 +00:00
for (auto it = m_renderLoops.constBegin(); it != m_renderLoops.constEnd(); ++it) {
it.key()->scheduleRepaint();
}
}
void Compositor::stop()
2011-01-30 14:34:42 +00:00
{
if (m_state == State::Off || m_state == State::Stopping) {
return;
}
m_state = State::Stopping;
Q_EMIT aboutToToggleCompositing();
m_releaseSelectionTimer.start();
// Some effects might need access to effect windows when they are about to
// be destroyed, for example to unreference deleted windows, so we have to
// make sure that effect windows outlive effects.
delete effects;
effects = nullptr;
if (Workspace::self()) {
for (X11Client *c : Workspace::self()->clientList()) {
m_scene->removeToplevel(c);
}
for (Unmanaged *c : Workspace::self()->unmanagedList()) {
m_scene->removeToplevel(c);
}
for (InternalClient *client : workspace()->internalClients()) {
m_scene->removeToplevel(client);
}
for (X11Client *c : Workspace::self()->clientList()) {
c->finishCompositing();
}
for (Unmanaged *c : Workspace::self()->unmanagedList()) {
c->finishCompositing();
}
for (InternalClient *client : workspace()->internalClients()) {
client->finishCompositing();
}
if (auto *con = kwinApp()->x11Connection()) {
xcb_composite_unredirect_subwindows(con, kwinApp()->x11RootWindow(),
XCB_COMPOSITE_REDIRECT_MANUAL);
}
while (!workspace()->deletedList().isEmpty()) {
workspace()->deletedList().first()->discard();
}
}
if (waylandServer()) {
for (AbstractClient *c : waylandServer()->clients()) {
m_scene->removeToplevel(c);
}
for (AbstractClient *c : waylandServer()->clients()) {
c->finishCompositing();
}
}
2020-11-19 08:52:29 +00:00
while (!m_renderLoops.isEmpty()) {
unregisterRenderLoop(m_renderLoops.firstKey());
}
disconnect(kwinApp()->platform(), &Platform::outputEnabled,
this, &Compositor::handleOutputEnabled);
disconnect(kwinApp()->platform(), &Platform::outputDisabled,
this, &Compositor::handleOutputDisabled);
delete m_scene;
m_scene = nullptr;
m_state = State::Off;
Q_EMIT compositingToggled(false);
2011-01-30 14:34:42 +00:00
}
void Compositor::destroyCompositorSelection()
{
delete m_selectionOwner;
m_selectionOwner = nullptr;
}
void Compositor::releaseCompositorSelection()
{
switch (m_state) {
case State::On:
// We are compositing at the moment. Don't release.
break;
case State::Off:
if (m_selectionOwner) {
qCDebug(KWIN_CORE) << "Releasing compositor selection";
m_selectionOwner->setOwning(false);
m_selectionOwner->release();
}
break;
case State::Starting:
case State::Stopping:
// Still starting or shutting down the compositor. Starting might fail
// or after stopping a restart might follow. So test again later on.
m_releaseSelectionTimer.start();
break;
}
}
void Compositor::keepSupportProperty(xcb_atom_t atom)
{
m_unusedSupportProperties.removeAll(atom);
}
void Compositor::removeSupportProperty(xcb_atom_t atom)
{
m_unusedSupportProperties << atom;
m_unusedSupportPropertyTimer.start();
}
void Compositor::deleteUnusedSupportProperties()
{
if (m_state == State::Starting || m_state == State::Stopping) {
// Currently still maybe restarting the compositor.
m_unusedSupportPropertyTimer.start();
return;
}
if (auto *con = kwinApp()->x11Connection()) {
for (const xcb_atom_t &atom : qAsConst(m_unusedSupportProperties)) {
// remove property from root window
xcb_delete_property(con, kwinApp()->x11RootWindow(), atom);
}
m_unusedSupportProperties.clear();
}
}
void Compositor::configChanged()
{
reinitialize();
addRepaintFull();
}
void Compositor::reinitialize()
{
// Reparse config. Config options will be reloaded by start()
kwinApp()->config()->reparseConfiguration();
// Restart compositing
stop();
start();
if (effects) { // start() may fail
effects->reconfigure();
}
}
void Compositor::addRepaint(int x, int y, int width, int height)
2011-01-30 14:34:42 +00:00
{
addRepaint(QRegion(x, y, width, height));
2011-01-30 14:34:42 +00:00
}
void Compositor::addRepaint(const QRect &rect)
2011-01-30 14:34:42 +00:00
{
addRepaint(QRegion(rect));
2011-01-30 14:34:42 +00:00
}
void Compositor::addRepaint(const QRegion &region)
2011-01-30 14:34:42 +00:00
{
if (m_scene) {
m_scene->addRepaint(region);
}
2011-01-30 14:34:42 +00:00
}
void Compositor::addRepaintFull()
2011-01-30 14:34:42 +00:00
{
addRepaint(screens()->geometry());
2011-01-30 14:34:42 +00:00
}
2020-11-19 08:52:29 +00:00
void Compositor::handleFrameRequested(RenderLoop *renderLoop)
2011-01-30 14:34:42 +00:00
{
composite(renderLoop);
}
void Compositor::composite(RenderLoop *renderLoop)
{
2020-11-19 08:52:29 +00:00
const int screenId = screenForRenderLoop(renderLoop);
fTraceDuration("Paint (", screens()->name(screenId), ")");
// Create a list of all windows in the stacking order
QList<Toplevel *> windows = Workspace::self()->xStackingOrder();
// Move elevated windows to the top of the stacking order
for (EffectWindow *c : static_cast<EffectsHandlerImpl *>(effects)->elevatedWindows()) {
Toplevel *t = static_cast<EffectWindowImpl *>(c)->window();
windows.removeAll(t);
windows.append(t);
}
// Skip windows that are not yet ready for being painted and if screen is locked skip windows
// that are neither lockscreen nor inputmethod windows.
//
// TODO? This cannot be used so carelessly - needs protections against broken clients, the
// window should not get focus before it's displayed, handle unredirected windows properly and
// so on.
for (Toplevel *win : windows) {
if (!win->readyForPainting()) {
windows.removeAll(win);
}
if (waylandServer() && waylandServer()->isScreenLocked()) {
if(!win->isLockScreen() && !win->isInputMethod()) {
windows.removeAll(win);
}
}
}
2020-11-19 08:52:29 +00:00
const QRegion repaints = m_scene->repaints(screenId);
m_scene->resetRepaints(screenId);
m_scene->paint(screenId, repaints, windows, renderLoop);
if (waylandServer()) {
2020-11-19 08:52:29 +00:00
const std::chrono::milliseconds frameTime =
std::chrono::duration_cast<std::chrono::milliseconds>(renderLoop->lastPresentationTimestamp());
for (Toplevel *window : qAsConst(windows)) {
if (!window->readyForPainting()) {
continue;
}
if (waylandServer()->isScreenLocked() &&
!(window->isLockScreen() || window->isInputMethod())) {
continue;
}
if (!window->isOnScreen(screenId)) {
continue;
}
if (auto surface = window->surface()) {
surface->frameRendered(frameTime.count());
}
}
if (!kwinApp()->platform()->isCursorHidden()) {
Cursors::self()->currentCursor()->markAsRendered();
}
}
2011-01-30 14:34:42 +00:00
}
bool Compositor::isActive()
2011-01-30 14:34:42 +00:00
{
return m_state == State::On;
2011-01-30 14:34:42 +00:00
}
WaylandCompositor::WaylandCompositor(QObject *parent)
: Compositor(parent)
{
connect(kwinApp(), &Application::x11ConnectionAboutToBeDestroyed,
this, &WaylandCompositor::destroyCompositorSelection);
}
void WaylandCompositor::toggleCompositing()
{
// For the shortcut. Not possible on Wayland because we always composite.
}
void WaylandCompositor::start()
{
if (!Compositor::setupStart()) {
// Internal setup failed, abort.
return;
}
if (Workspace::self()) {
startupWithWorkspace();
} else {
connect(kwinApp(), &Application::workspaceCreated,
this, &WaylandCompositor::startupWithWorkspace);
}
}
X11Compositor::X11Compositor(QObject *parent)
: Compositor(parent)
, m_suspended(options->isUseCompositing() ? NoReasonSuspend : UserSuspend)
{
if (qEnvironmentVariableIsSet("KWIN_MAX_FRAMES_TESTED")) {
m_framesToTestForSafety = qEnvironmentVariableIntValue("KWIN_MAX_FRAMES_TESTED");
}
}
void X11Compositor::toggleCompositing()
{
if (m_suspended) {
// Direct user call; clear all bits.
resume(AllReasonSuspend);
} else {
// But only set the user one (sufficient to suspend).
suspend(UserSuspend);
}
}
void X11Compositor::reinitialize()
{
// Resume compositing if suspended.
m_suspended = NoReasonSuspend;
Compositor::reinitialize();
}
void X11Compositor::configChanged()
{
if (m_suspended) {
stop();
return;
}
Compositor::configChanged();
}
void X11Compositor::suspend(X11Compositor::SuspendReason reason)
{
Q_ASSERT(reason != NoReasonSuspend);
m_suspended |= reason;
if (reason & ScriptSuspend) {
// When disabled show a shortcut how the user can get back compositing.
const auto shortcuts = KGlobalAccel::self()->shortcut(
workspace()->findChild<QAction*>(QStringLiteral("Suspend Compositing")));
if (!shortcuts.isEmpty()) {
// Display notification only if there is the shortcut.
const QString message =
i18n("Desktop effects have been suspended by another application.<br/>"
"You can resume using the '%1' shortcut.",
shortcuts.first().toString(QKeySequence::NativeText));
KNotification::event(QStringLiteral("compositingsuspendeddbus"), message);
}
}
stop();
}
void X11Compositor::resume(X11Compositor::SuspendReason reason)
{
Q_ASSERT(reason != NoReasonSuspend);
m_suspended &= ~reason;
start();
}
void X11Compositor::start()
{
if (m_suspended) {
QStringList reasons;
if (m_suspended & UserSuspend) {
reasons << QStringLiteral("Disabled by User");
}
if (m_suspended & BlockRuleSuspend) {
reasons << QStringLiteral("Disabled by Window");
}
if (m_suspended & ScriptSuspend) {
reasons << QStringLiteral("Disabled by Script");
}
qCDebug(KWIN_CORE) << "Compositing is suspended, reason:" << reasons;
return;
} else if (!kwinApp()->platform()->compositingPossible()) {
qCCritical(KWIN_CORE) << "Compositing is not possible";
return;
}
if (!Compositor::setupStart()) {
// Internal setup failed, abort.
return;
}
startupWithWorkspace();
}
void X11Compositor::composite(RenderLoop *renderLoop)
{
if (scene()->overlayWindow() && !isOverlayWindowVisible()) {
// Return since nothing is visible.
return;
}
QList<Toplevel *> windows = Workspace::self()->xStackingOrder();
QList<SurfaceItemX11 *> dirtyItems;
// Reset the damage state of each window and fetch the damage region
// without waiting for a reply
for (Toplevel *window : qAsConst(windows)) {
SurfaceItemX11 *surfaceItem = static_cast<SurfaceItemX11 *>(window->surfaceItem());
if (surfaceItem->fetchDamage()) {
dirtyItems.append(surfaceItem);
}
}
if (dirtyItems.count() > 0) {
scene()->triggerFence();
xcb_flush(kwinApp()->x11Connection());
}
// Get the replies
for (SurfaceItemX11 *item : qAsConst(dirtyItems)) {
item->waitForDamage();
}
if (m_framesToTestForSafety > 0 && (scene()->compositingType() & OpenGLCompositing)) {
kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PreFrame);
}
Compositor::composite(renderLoop);
if (m_framesToTestForSafety > 0) {
if (scene()->compositingType() & OpenGLCompositing) {
kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PostFrame);
}
m_framesToTestForSafety--;
if (m_framesToTestForSafety == 0 && (scene()->compositingType() & OpenGLCompositing)) {
kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PostLastGuardedFrame);
}
}
}
bool X11Compositor::checkForOverlayWindow(WId w) const
{
if (!scene()) {
// No scene, so it cannot be the overlay window.
return false;
}
if (!scene()->overlayWindow()) {
// No overlay window, it cannot be the overlay.
return false;
}
// Compare the window ID's.
return w == scene()->overlayWindow()->window();
}
bool X11Compositor::isOverlayWindowVisible() const
{
if (!scene()) {
return false;
}
if (!scene()->overlayWindow()) {
return false;
}
return scene()->overlayWindow()->isVisible();
}
void X11Compositor::updateClientCompositeBlocking(X11Client *c)
{
if (c) {
if (c->isBlockingCompositing()) {
// Do NOT attempt to call suspend(true) from within the eventchain!
if (!(m_suspended & BlockRuleSuspend))
QMetaObject::invokeMethod(this, [this]() {
suspend(BlockRuleSuspend);
}, Qt::QueuedConnection);
}
}
else if (m_suspended & BlockRuleSuspend) {
// If !c we just check if we can resume in case a blocking client was lost.
bool shouldResume = true;
for (auto it = Workspace::self()->clientList().constBegin();
it != Workspace::self()->clientList().constEnd(); ++it) {
if ((*it)->isBlockingCompositing()) {
shouldResume = false;
break;
}
}
if (shouldResume) {
// Do NOT attempt to call suspend(false) from within the eventchain!
QMetaObject::invokeMethod(this, [this]() {
resume(BlockRuleSuspend);
}, Qt::QueuedConnection);
}
}
}
[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
X11Compositor *X11Compositor::self()
{
return qobject_cast<X11Compositor *>(Compositor::self());
}
}
// included for CompositorSelectionOwner
#include "composite.moc"