Commit 40b0296d authored by David Edmundson's avatar David Edmundson
Browse files

[libkwineffects] Introduce API to easily show a QtQuick scene in an effect

Summary:
EffectQuickView/Scene is a convenient class to render a QtQuick
scenegraph into an effect.

Current methods (such as present windows) involve creating an underlying
platform window which is expensive, causes a headache to filter out
again in the rest of the code, and only works as an overlay.

The new class exposes things more natively to an effect where we don't
mess with real windows, we can perform the painting anywhere in the view
and we don't have issues with hiding/closing.

QtQuick has both software and hardware accelerated modes, and kwin also
has 3 render backends. Every combination is supported.

* When used in OpenGL mode for both, we render into an FBO export the
texture ID then it's up to the effect to render that into a scene.

* When using software QtQuick rendering we blit into an image, upload
that into a KWinGLTexture which serves as an abstraction layer and
render that into the scene.

* When using GL for QtQuick and XRender/QPainter in kwin everything is
rendered into the internal FBO, blit and exported as an image.

* When using software rendering for both an image gets passed directly.

Mouse and keyboard events can be forwarded, only if the effect
intercepts them.

The class is meant to be generic enough that we can remove all the
QtQuick code from Aurorae.

The intention is also to replace EffectFrameImpl using this backend and
we can kill all of the EffectFrame code throughout the scenes.

The close button in present windows will also be ported to this,
simplifiying that code base.

Classes that handle the rendering and handling QML are intentionally
split so that in the future we can have a declarative effects API create
overlays from within the same context. Similar to how one can
instantiate windows from a typical QML scene.

Notes:
I don't like how I pass the kwin GL context from the backends into the
effect, but I need something that works with the library separation. It
also currently has wayland problem if I create a QOpenGLContext before
the QPA is set up with a scene - but I don't have anything better?

I know for the EffectFrame we need an API to push things through the
effects stack to handle blur/invert etc. Will deal with that when we
port the EffectFrame.

Test Plan: Used in an effect

Reviewers: #kwin, zzag

Reviewed By: #kwin, zzag

Subscribers: zzag, kwin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D24215
parent a161c508
......@@ -278,6 +278,9 @@ public:
KSharedConfigPtr config() const override;
KSharedConfigPtr inputConfig() const override;
void renderEffectQuickView(KWin::EffectQuickView *quickView) const override {
Q_UNUSED(quickView);
}
private:
bool m_animationsSuported = true;
......
......@@ -46,9 +46,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "window_property_notify_x11_filter.h"
#include "workspace.h"
#include "kwinglutils.h"
#include "kwineffectquickview.h"
#include <QDebug>
#include <QDesktopWidget>
#include <Plasma/Theme>
......@@ -1702,6 +1702,14 @@ Effect *EffectsHandlerImpl::findEffect(const QString &name) const
return (*it).second;
}
void EffectsHandlerImpl::renderEffectQuickView(EffectQuickView *w) const
{
if (!w->isVisible()) {
return;
}
scene()->paintEffectQuickView(w);
}
//****************************************
// EffectWindowImpl
//****************************************
......
......@@ -273,6 +273,8 @@ public:
*/
Effect *findEffect(const QString &name) const;
void renderEffectQuickView(EffectQuickView *effectQuickView) const override;
public Q_SLOTS:
void slotCurrentTabAboutToChange(EffectWindow* from, EffectWindow* to);
void slotTabAdded(EffectWindow* from, EffectWindow* to);
......
......@@ -40,6 +40,7 @@ install(TARGETS kwinxrenderutils EXPORT kdeworkspaceLibraryTargets ${INSTALL_TAR
set(kwin_EFFECTSLIB_SRCS
anidata.cpp
kwinanimationeffect.cpp
kwineffectquickview.cpp
kwineffects.cpp
logging.cpp
)
......@@ -47,12 +48,14 @@ set(kwin_EFFECTSLIB_SRCS
set(kwineffects_QT_LIBS
Qt5::DBus
Qt5::Widgets
Qt5::Quick
)
set(kwineffects_KDE_LIBS
KF5::ConfigCore
KF5::CoreAddons
KF5::WindowSystem
KF5::Declarative
)
set(kwineffects_XCB_LIBS
......@@ -66,6 +69,7 @@ target_link_libraries(kwineffects
${kwineffects_QT_LIBS}
${kwineffects_KDE_LIBS}
${kwineffects_XCB_LIBS}
kwinglutils
)
if (KWIN_HAVE_XRENDER_COMPOSITING)
target_link_libraries(kwineffects PRIVATE kwinxrenderutils XCB::XFIXES)
......@@ -112,6 +116,7 @@ install(FILES
${CMAKE_CURRENT_BINARY_DIR}/kwinglutils_export.h
${CMAKE_CURRENT_BINARY_DIR}/kwinxrenderutils_export.h
kwinanimationeffect.h
kwineffectquickview.h
kwineffects.h
kwinglobals.h
kwinglplatform.h
......
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2019 David Edmundson <davidedmundson@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#include "kwineffectquickview.h"
#include "kwinglutils.h"
#include "kwineffects.h"
#include "logging_p.h"
#include <QQmlEngine>
#include <QQuickItem>
#include <QQmlContext>
#include <QQmlComponent>
#include <QQuickView>
#include <QQuickRenderControl>
#include <QUrl>
#include <QOffscreenSurface>
#include <QOpenGLContext>
#include <QOpenGLFramebufferObject>
#include <QTimer>
#include <KDeclarative/QmlObjectSharedEngine>
using namespace KWin;
static std::unique_ptr<QOpenGLContext> s_shareContext;
class Q_DECL_HIDDEN EffectQuickView::Private
{
public:
QQuickWindow *m_view;
QQuickRenderControl *m_renderControl;
QScopedPointer<QOpenGLContext> m_glcontext;
QScopedPointer<QOffscreenSurface> m_offscreenSurface;
QScopedPointer<QOpenGLFramebufferObject> m_fbo;
QImage m_image;
QScopedPointer<GLTexture> m_textureExport;
// if we should capture a QImage after rendering into our BO.
// Used for either software QtQuick rendering and nonGL kwin rendering
bool m_useBlit = false;
bool m_visible = true;
void releaseResources();
};
class Q_DECL_HIDDEN EffectQuickScene::Private
{
public:
KDeclarative::QmlObjectSharedEngine *qmlObject = nullptr;
};
EffectQuickView::EffectQuickView(QObject *parent)
: EffectQuickView(parent, effects->isOpenGLCompositing() ? ExportMode::Texture : ExportMode::Image)
{
}
EffectQuickView::EffectQuickView(QObject *parent, ExportMode exportMode)
: QObject(parent)
, d(new EffectQuickView::Private)
{
d->m_renderControl = new QQuickRenderControl(this);
d->m_view = new QQuickWindow(d->m_renderControl);
d->m_view->setFlags(Qt::FramelessWindowHint);
d->m_view->setColor(Qt::transparent);
if (exportMode == ExportMode::Image) {
d->m_useBlit = true;
}
const bool usingGl = d->m_view->rendererInterface()->graphicsApi() == QSGRendererInterface::OpenGL;
if (!usingGl) {
qCDebug(LIBKWINEFFECTS) << "QtQuick Software rendering mode detected";
d->m_useBlit = true;
d->m_renderControl->initialize(nullptr);
} else {
QSurfaceFormat format;
format.setOption(QSurfaceFormat::ResetNotification);
format.setDepthBufferSize(16);
format.setStencilBufferSize(8);
d->m_glcontext.reset(new QOpenGLContext);
d->m_glcontext->setShareContext(s_shareContext.get());
d->m_glcontext->setFormat(format);
d->m_glcontext->create();
// and the offscreen surface
d->m_offscreenSurface.reset(new QOffscreenSurface);
d->m_offscreenSurface->setFormat(d->m_glcontext->format());
d->m_offscreenSurface->create();
d->m_glcontext->makeCurrent(d->m_offscreenSurface.data());
d->m_renderControl->initialize(d->m_glcontext.data());
d->m_glcontext->doneCurrent();
if (!d->m_glcontext->shareContext()) {
qCDebug(LIBKWINEFFECTS) << "Failed to create a shared context, falling back to raster rendering";
qCDebug(LIBKWINEFFECTS) << "Extra debug:";
qCDebug(LIBKWINEFFECTS) << "our context:" << d->m_glcontext.data();
qCDebug(LIBKWINEFFECTS) << "share context:" << s_shareContext.get();
// still render via GL, but blit for presentation
d->m_useBlit = true;
}
}
auto updateSize = [this]() { contentItem()->setSize(d->m_view->size()); };
updateSize();
connect(d->m_view, &QWindow::widthChanged, this, updateSize);
connect(d->m_view, &QWindow::heightChanged, this, updateSize);
QTimer *t = new QTimer(this);
t->setSingleShot(true);
t->setInterval(10);
connect(t, &QTimer::timeout, this, &EffectQuickView::update);
connect(d->m_renderControl, &QQuickRenderControl::renderRequested, t, [t]() { t->start(); });
connect(d->m_renderControl, &QQuickRenderControl::sceneChanged, t, [t]() { t->start(); });
}
EffectQuickView::~EffectQuickView()
{
if (d->m_glcontext) {
d->m_glcontext->makeCurrent(d->m_offscreenSurface.data());
d->m_renderControl->invalidate();
d->m_glcontext->doneCurrent();
}
}
void EffectQuickView::update()
{
if (!d->m_visible) {
return;
}
if (d->m_view->size().isEmpty()) {
return;
}
bool usingGl = d->m_glcontext;
if (usingGl) {
if (!d->m_glcontext->makeCurrent(d->m_offscreenSurface.data())) {
// probably a context loss event, kwin is about to reset all the effects anyway
return;
}
if (d->m_fbo.isNull() || d->m_fbo->size() != d->m_view->size()) {
d->m_textureExport.reset(nullptr);
d->m_fbo.reset(new QOpenGLFramebufferObject(d->m_view->size(), QOpenGLFramebufferObject::CombinedDepthStencil));
if (!d->m_fbo->isValid()) {
d->m_fbo.reset();
d->m_glcontext->doneCurrent();
return;
}
}
d->m_view->setRenderTarget(d->m_fbo.data());
}
d->m_renderControl->polishItems();
d->m_renderControl->sync();
d->m_renderControl->render();
if (usingGl) {
d->m_view->resetOpenGLState();
}
if (d->m_useBlit) {
d->m_image = d->m_renderControl->grab();
}
if (usingGl) {
QOpenGLFramebufferObject::bindDefault();
d->m_glcontext->doneCurrent();
}
emit repaintNeeded();
}
void EffectQuickView::forwardMouseEvent(QEvent *e)
{
if (!d->m_visible) {
return;
}
QMouseEvent *me = static_cast<QMouseEvent *>(e);
const QPoint widgetPos = d->m_view->mapFromGlobal(me->pos());
QMouseEvent cloneEvent(me->type(), widgetPos, me->pos(), me->button(), me->buttons(), me->modifiers());
QCoreApplication::sendEvent(d->m_view, &cloneEvent);
e->setAccepted(cloneEvent.isAccepted());
}
void EffectQuickView::forwardKeyEvent(QKeyEvent *keyEvent)
{
if (!d->m_visible) {
return;
}
QCoreApplication::sendEvent(d->m_view, keyEvent);
}
void EffectQuickView::setShareContext(std::unique_ptr<QOpenGLContext> context)
{
s_shareContext = std::move(context);
}
QRect EffectQuickView::geometry() const
{
return d->m_view->geometry();
}
QQuickItem *EffectQuickView::contentItem() const
{
return d->m_view->contentItem();
}
void EffectQuickView::setVisible(bool visible)
{
if (d->m_visible == visible) {
return;
}
d->m_visible = visible;
if (visible){
d->m_renderControl->renderRequested();
} else {
// deferred to not change GL context
QTimer::singleShot(0, this, [this]() {
d->releaseResources();
});
}
}
bool EffectQuickView::isVisible() const
{
return d->m_visible;
}
void EffectQuickView::show()
{
setVisible(true);
}
void EffectQuickView::hide()
{
setVisible(false);
}
GLTexture *EffectQuickView::bufferAsTexture()
{
if (d->m_useBlit) {
if (d->m_image.isNull()) {
return nullptr;
}
d->m_textureExport.reset(new GLTexture(d->m_image));
} else {
if (!d->m_fbo) {
return nullptr;
}
if (!d->m_textureExport) {
d->m_textureExport.reset(new GLTexture(d->m_fbo->texture(), d->m_fbo->format().internalTextureFormat(), d->m_fbo->size()));
}
}
return d->m_textureExport.data();
}
QImage EffectQuickView::bufferAsImage() const
{
return d->m_image;
}
QSize EffectQuickView::size() const
{
return d->m_view->geometry().size();
}
void EffectQuickView::setGeometry(const QRect &rect)
{
const QRect oldGeometry = d->m_view->geometry();
d->m_view->setGeometry(rect);
emit geometryChanged(oldGeometry, rect);
}
void EffectQuickView::Private::releaseResources()
{
if (m_glcontext) {
m_glcontext->makeCurrent(m_offscreenSurface.data());
m_view->releaseResources();
m_glcontext->doneCurrent();
} else {
m_view->releaseResources();
}
}
EffectQuickScene::EffectQuickScene(QObject *parent)
: EffectQuickView(parent)
, d(new EffectQuickScene::Private)
{
}
EffectQuickScene::EffectQuickScene(QObject *parent, EffectQuickView::ExportMode exportMode)
: EffectQuickView(parent, exportMode)
, d(new EffectQuickScene::Private)
{
}
EffectQuickScene::~EffectQuickScene()
{
}
void EffectQuickScene::setSource(const QUrl &source)
{
if (!d->qmlObject) {
d->qmlObject = new KDeclarative::QmlObjectSharedEngine(this);
}
d->qmlObject->setSource(source);
QQuickItem *item = rootItem();
if (!item) {
qCDebug(LIBKWINEFFECTS) << "Could not load effect quick view" << source;
return;
}
item->setParentItem(contentItem());
auto updateSize = [item, this]() { item->setSize(contentItem()->size()); };
updateSize();
connect(contentItem(), &QQuickItem::widthChanged, item, updateSize);
connect(contentItem(), &QQuickItem::heightChanged, item, updateSize);
}
QQmlContext *EffectQuickScene::rootContext() const
{
return d->qmlObject->rootContext();
}
QQuickItem *EffectQuickScene::rootItem() const
{
return qobject_cast<QQuickItem *>(d->qmlObject->rootObject());
}
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2019 David Edmundson <davidedmundson@kde.org>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
#pragma once
#include <QObject>
#include <QUrl>
#include <QRect>
#include <kwineffects_export.h>
#include "kwineffects.h"
#include <memory>
class QKeyEvent;
class QMouseEvent;
class QOpenGLContext;
class QMouseEvent;
class QKeyEvent;
class QQmlContext;
class QQuickItem;
namespace KWin
{
class GLTexture;
class EffectQuickView;
/**
* @brief The KwinQuickView class provides a convenient API for exporting
* QtQuick scenes as buffers that can be composited in any other fashion.
*
* Contents can be fetched as a GL Texture or as a QImage
* If data is to be fetched as an image, it should be specified upfront as
* blitting is performed when we update our FBO to keep kwin's render loop
* as fast as possible.
*/
class KWINEFFECTS_EXPORT EffectQuickView : public QObject
{
Q_OBJECT
public:
static void setShareContext(std::unique_ptr<QOpenGLContext> context);
enum class ExportMode {
/** The contents will be available as a texture in the shared contexts. Image will be blank*/
Texture,
/** The contents will be blit during the update into a QImage buffer. */
Image
};
/**
* Construct a new KWinQuickView
* Export mode will be determined by the current effectsHandler
*/
EffectQuickView(QObject *parent);
/**
* Construct a new KWinQuickView explicitly stating an export mode
*/
EffectQuickView(QObject *parent, ExportMode exportMode);
/**
* Note that this may change the current GL Context
*/
~EffectQuickView();
QSize size() const;
/**
* The geometry of the current view
* This may be out of sync with the current buffer size if an update is pending
*/
void setGeometry(const QRect &rect);
QRect geometry() const;
/**
* Render the current scene graph into the FBO.
* This is typically done automatically when the scene changes
* albeit deffered by a timer
*
* It can be manually invoked to update the contents immediately.
* Note this will change the GL context
*/
void update();
/** The invisble root item of the window*/
QQuickItem *contentItem() const;
/**
* @brief Marks the window as visible/invisible
* This can be used to release resources used by the window
* The default is true.
*/
void setVisible(bool visible);
bool isVisible() const;
void show();
void hide();
/**
* Returns the current output of the scene graph
* @note The render context must valid at the time of calling
*/
GLTexture *bufferAsTexture();
/**
* Returns the current output of the scene graph
*/
QImage bufferAsImage() const;
/**
* Inject any mouse event into the QQuickWindow.
* Local co-ordinates are transformed
* If it is handled the event will be accepted
*/
void forwardMouseEvent(QEvent *mouseEvent);
/**
* Inject a key event into the window.
* If it is handled the event will be accepted
*/
void forwardKeyEvent(QKeyEvent *keyEvent);
Q_SIGNALS:
/**
* The frame buffer has changed, contents need re-rendering on screen
*/
void repaintNeeded();
void geometryChanged(const QRect &oldGeometry, const QRect &newGeometry);
private:
class Private;
QScopedPointer<Private> d;
};
/**
* The KWinQuickScene class extends KWinQuickView
* adding QML support. This will represent a context
* powered by an engine
*/
class KWINEFFECTS_EXPORT EffectQuickScene : public EffectQuickView
{
public:
EffectQuickScene(QObject *parent);
EffectQuickScene(QObject *parent, ExportMode exportMode);
~EffectQuickScene();
QQmlContext *rootContext() const;
/** top level item in the given source*/
QQuickItem *rootItem() const