Refactor how per screen rendering is handled

In order to allow per screen rendering, we need the Compositor to be
able to drive rendering on each screen. Currently, it's not possible
because Scene::paint() paints all screen.

With this change, the Compositor will be able to ask the Scene to paint
only a screen with the specific id.
This commit is contained in:
Vlad Zahorodnii 2020-11-09 16:10:03 +02:00
parent d2f3372749
commit 755dd81e49
18 changed files with 83 additions and 131 deletions

View file

@ -686,7 +686,15 @@ void Compositor::performCompositing()
if (m_framesToTestForSafety > 0 && (m_scene->compositingType() & OpenGLCompositing)) {
kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PreFrame);
}
m_timeSinceLastVBlank = m_scene->paint(repaints, windows);
m_renderTimer.start();
if (m_scene->isPerScreenRenderingEnabled()) {
for (int screenId = 0; screenId < screens()->count(); ++screenId) {
m_scene->paint(screenId, repaints, windows);
}
} else {
m_scene->paint(-1, repaints, windows);
}
m_timeSinceLastVBlank = m_renderTimer.elapsed();
if (m_framesToTestForSafety > 0) {
if (m_scene->compositingType() & OpenGLCompositing) {
kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PostFrame);

View file

@ -157,6 +157,7 @@ private:
bool m_composeAtSwapCompletion;
int m_framesToTestForSafety = 3;
QElapsedTimer m_renderTimer;
QElapsedTimer m_monotonicClock;
};

View file

@ -10,7 +10,6 @@
#ifndef KWIN_SCENE_OPENGL_BACKEND_H
#define KWIN_SCENE_OPENGL_BACKEND_H
#include <QElapsedTimer>
#include <QRegion>
#include <kwin_export.h>
@ -47,13 +46,7 @@ public:
virtual ~OpenGLBackend();
virtual void init() = 0;
/**
* @return Time passes since start of rendering current frame.
* @see startRenderTimer
*/
qint64 renderTime() {
return m_renderTimer.nsecsElapsed();
}
virtual void screenGeometryChanged(const QSize &size) = 0;
virtual SceneOpenGLTexturePrivate *createBackendTexture(SceneOpenGLTexture *texture) = 0;
@ -288,14 +281,6 @@ protected:
void setLastDamage(const QRegion &damage) {
m_lastDamage = damage;
}
/**
* @brief Starts the timer for how long it takes to render the frame.
*
* @see renderTime
*/
void startRenderTimer() {
m_renderTimer.start();
}
/**
* Sets the platform-specific @p extensions.
@ -348,11 +333,6 @@ private:
* @brief The damage history for the past 10 frames.
*/
QList<QRegion> m_damageHistory;
/**
* @brief Timer to measure how long a frame renders.
*/
QElapsedTimer m_renderTimer;
QList<QByteArray> m_extensions;
};

View file

@ -513,7 +513,6 @@ SceneOpenGLTexturePrivate *EglGbmBackend::createBackendTexture(SceneOpenGLTextur
QRegion EglGbmBackend::prepareRenderingFrame()
{
startRenderTimer();
return QRegion();
}

View file

@ -477,7 +477,6 @@ SceneOpenGLTexturePrivate *EglStreamBackend::createBackendTexture(SceneOpenGLTex
QRegion EglStreamBackend::prepareRenderingFrame()
{
startRenderTimer();
return QRegion();
}

View file

@ -145,7 +145,6 @@ QRegion EglHwcomposerBackend::prepareRenderingFrame()
present();
// TODO: buffer age?
startRenderTimer();
// triggers always a full repaint
return QRegion(QRect(QPoint(0, 0), m_backend->size()));
}

View file

@ -167,7 +167,6 @@ QRegion EglGbmBackend::prepareRenderingFrame()
if (!lastDamage().isEmpty()) {
present();
}
startRenderTimer();
if (!GLRenderTarget::isRenderTargetBound()) {
GLRenderTarget::pushRenderTarget(m_fbo);
}

View file

@ -366,7 +366,6 @@ SceneOpenGLTexturePrivate *EglWaylandBackend::createBackendTexture(SceneOpenGLTe
QRegion EglWaylandBackend::prepareRenderingFrame()
{
eglWaitNative(EGL_CORE_NATIVE_ENGINE);
startRenderTimer();
m_swapping = false;
return QRegion();
}

View file

@ -402,7 +402,6 @@ QRegion EglOnXBackend::prepareRenderingFrame()
if (supportsBufferAge())
repaint = accumulatedDamageHistory(m_bufferAge);
startRenderTimer();
eglWaitNative(EGL_CORE_NATIVE_ENGINE);
return repaint;

View file

@ -799,7 +799,6 @@ QRegion GlxBackend::prepareRenderingFrame()
if (supportsBufferAge())
repaint = accumulatedDamageHistory(m_bufferAge);
startRenderTimer();
glXWaitX();
return repaint;

View file

@ -62,7 +62,6 @@ void EglX11Backend::present()
QRegion EglX11Backend::prepareRenderingFrame()
{
startRenderTimer();
return QRegion();
}

View file

@ -535,6 +535,8 @@ void SceneOpenGL::handleGraphicsReset(GLenum status)
QMetaObject::invokeMethod(this, "resetCompositing", Qt::QueuedConnection);
KNotification::event(QStringLiteral("graphicsreset"), i18n("Desktop effects were restarted due to a graphics reset"));
m_resetOccurred = true;
}
@ -620,105 +622,84 @@ void SceneOpenGL::aboutToStartPainting(int screenId, const QRegion &damage)
m_backend->aboutToStartPainting(screenId, damage);
}
qint64 SceneOpenGL::paint(const QRegion &damage, const QList<Toplevel *> &toplevels)
void SceneOpenGL::paint(int screenId, const QRegion &damage, const QList<Toplevel *> &toplevels)
{
if (m_resetOccurred) {
return; // A graphics reset has occurred, do nothing.
}
painted_screen = screenId;
// actually paint the frame, flushed with the NEXT frame
createStackingOrder(toplevels);
// After this call, updateRegion will contain the damaged region in the
// back buffer. This is the region that needs to be posted to repair
// the front buffer. It doesn't include the additional damage returned
// by prepareRenderingFrame(). validRegion is the region that has been
// repainted, and may be larger than updateRegion.
QRegion updateRegion, validRegion;
if (m_backend->perScreenRendering()) {
// trigger start render timer
m_backend->prepareRenderingFrame();
for (int i = 0; i < screens()->count(); ++i) {
painted_screen = i;
const QRect &geo = screens()->geometry(i);
const qreal scaling = screens()->scale(i);
QRegion update;
QRegion valid;
// prepare rendering makes context current on the output
QRegion repaint = m_backend->prepareRenderingForScreen(i);
GLVertexBuffer::setVirtualScreenGeometry(geo);
GLRenderTarget::setVirtualScreenGeometry(geo);
GLVertexBuffer::setVirtualScreenScale(scaling);
GLRenderTarget::setVirtualScreenScale(scaling);
QRegion update;
QRegion valid;
QRegion repaint;
QRect geo;
qreal scaling;
const GLenum status = glGetGraphicsResetStatus();
if (status != GL_NO_ERROR) {
handleGraphicsReset(status);
return 0;
}
int mask = 0;
updateProjectionMatrix();
paintScreen(&mask, damage.intersected(geo), repaint, &update, &valid, projectionMatrix(), geo, scaling); // call generic implementation
paintCursor(valid);
GLVertexBuffer::streamingBuffer()->endOfFrame();
m_backend->endRenderingFrameForScreen(i, valid, update);
GLVertexBuffer::streamingBuffer()->framePosted();
}
// prepare rendering makes context current on the output
if (screenId != -1) {
repaint = m_backend->prepareRenderingForScreen(screenId);
geo = screens()->geometry(screenId);
scaling = screens()->scale(screenId);
} else {
painted_screen = -1;
m_backend->makeCurrent();
QRegion repaint = m_backend->prepareRenderingFrame();
repaint = m_backend->prepareRenderingFrame();
geo = screens()->geometry();
scaling = 1;
}
const GLenum status = glGetGraphicsResetStatus();
if (status != GL_NO_ERROR) {
handleGraphicsReset(status);
return 0;
}
GLVertexBuffer::setVirtualScreenGeometry(screens()->geometry());
GLRenderTarget::setVirtualScreenGeometry(screens()->geometry());
GLVertexBuffer::setVirtualScreenScale(1);
GLRenderTarget::setVirtualScreenScale(1);
GLVertexBuffer::setVirtualScreenGeometry(geo);
GLRenderTarget::setVirtualScreenGeometry(geo);
GLVertexBuffer::setVirtualScreenScale(scaling);
GLRenderTarget::setVirtualScreenScale(scaling);
const GLenum status = glGetGraphicsResetStatus();
if (status != GL_NO_ERROR) {
handleGraphicsReset(status);
} else {
int mask = 0;
updateProjectionMatrix();
paintScreen(&mask, damage, repaint, &updateRegion, &validRegion, projectionMatrix()); // call generic implementation
if (!GLPlatform::instance()->isGLES()) {
paintScreen(&mask, damage.intersected(geo), repaint, &update, &valid, projectionMatrix(), geo, scaling); // call generic implementation
paintCursor(valid);
if (!GLPlatform::instance()->isGLES() && screenId == -1) {
const QSize &screenSize = screens()->size();
const QRegion displayRegion(0, 0, screenSize.width(), screenSize.height());
// copy dirty parts from front to backbuffer
if (!m_backend->supportsBufferAge() &&
options->glPreferBufferSwap() == Options::CopyFrontBuffer &&
validRegion != displayRegion) {
valid != displayRegion) {
glReadBuffer(GL_FRONT);
m_backend->copyPixels(displayRegion - validRegion);
m_backend->copyPixels(displayRegion - valid);
glReadBuffer(GL_BACK);
validRegion = displayRegion;
valid = displayRegion;
}
}
GLVertexBuffer::streamingBuffer()->endOfFrame();
m_backend->endRenderingFrame(validRegion, updateRegion);
GLVertexBuffer::streamingBuffer()->framePosted();
}
if (m_currentFence) {
if (!m_syncManager->updateFences()) {
qCDebug(KWIN_OPENGL) << "Aborting explicit synchronization with the X command stream.";
qCDebug(KWIN_OPENGL) << "Future frames will be rendered unsynchronized.";
delete m_syncManager;
m_syncManager = nullptr;
if (screenId != -1) {
m_backend->endRenderingFrameForScreen(screenId, valid, update);
} else {
m_backend->endRenderingFrame(valid, update);
}
GLVertexBuffer::streamingBuffer()->framePosted();
if (m_currentFence) {
if (!m_syncManager->updateFences()) {
qCDebug(KWIN_OPENGL) << "Aborting explicit synchronization with the X command stream.";
qCDebug(KWIN_OPENGL) << "Future frames will be rendered unsynchronized.";
delete m_syncManager;
m_syncManager = nullptr;
}
m_currentFence = nullptr;
}
m_currentFence = nullptr;
}
// do cleanup
clearStackingOrder();
return m_backend->renderTime();
}
QMatrix4x4 SceneOpenGL::transformation(int mask, const ScreenPaintData &data) const

View file

@ -35,7 +35,7 @@ public:
~SceneOpenGL() override;
bool initFailed() const override;
bool hasPendingFlush() const override;
qint64 paint(const QRegion &damage, const QList<Toplevel *> &windows) override;
void paint(int screenId, const QRegion &damage, const QList<Toplevel *> &windows) override;
Scene::EffectFrame *createEffectFrame(EffectFrameImpl *frame) override;
Shadow *createShadow(Toplevel *toplevel) override;
void screenGeometryChanged(const QSize &size) override;
@ -95,6 +95,7 @@ private:
bool viewportLimitsMatched(const QSize &size) const;
private:
bool m_resetOccurred = false;
bool m_debug;
OpenGLBackend *m_backend;
SyncManager *m_syncManager;

View file

@ -81,30 +81,25 @@ void SceneQPainter::paintGenericScreen(int mask, const ScreenPaintData &data)
m_painter->restore();
}
qint64 SceneQPainter::paint(const QRegion &_damage, const QList<Toplevel *> &toplevels)
void SceneQPainter::paint(int screenId, const QRegion &_damage, const QList<Toplevel *> &toplevels)
{
Q_ASSERT(m_backend->perScreenRendering());
QElapsedTimer renderTimer;
renderTimer.start();
painted_screen = screenId;
createStackingOrder(toplevels);
QRegion damage = _damage;
for (int i = 0; i < screens()->count(); ++i) {
int mask = 0;
int mask = 0;
painted_screen = i;
m_backend->prepareRenderingFrame(i);
const bool needsFullRepaint = m_backend->needsFullRepaint(i);
if (needsFullRepaint) {
mask |= Scene::PAINT_SCREEN_BACKGROUND_FIRST;
damage = screens()->geometry();
}
const QRect geometry = screens()->geometry(i);
QImage *buffer = m_backend->bufferForScreen(i);
if (!buffer || buffer->isNull()) {
continue;
}
m_backend->prepareRenderingFrame(screenId);
const bool needsFullRepaint = m_backend->needsFullRepaint(screenId);
if (needsFullRepaint) {
mask |= Scene::PAINT_SCREEN_BACKGROUND_FIRST;
damage = screens()->geometry(screenId);
}
const QRect geometry = screens()->geometry(screenId);
QImage *buffer = m_backend->bufferForScreen(screenId);
if (buffer && !buffer->isNull()) {
m_painter->begin(buffer);
m_painter->setWindow(geometry);
@ -113,13 +108,11 @@ qint64 SceneQPainter::paint(const QRegion &_damage, const QList<Toplevel *> &top
paintCursor(updateRegion);
m_painter->end();
m_backend->present(i, mask, updateRegion);
m_backend->present(screenId, mask, updateRegion);
}
// do cleanup
clearStackingOrder();
return renderTimer.nsecsElapsed();
}
void SceneQPainter::paintBackground(const QRegion &region)

View file

@ -25,7 +25,7 @@ public:
~SceneQPainter() override;
bool usesOverlayWindow() const override;
OverlayWindow* overlayWindow() const override;
qint64 paint(const QRegion &damage, const QList<Toplevel *> &windows) override;
void paint(int screenId, const QRegion &damage, const QList<Toplevel *> &windows) override;
void paintGenericScreen(int mask, const ScreenPaintData &data) override;
CompositingType compositingType() const override;
bool initFailed() const override;

View file

@ -238,11 +238,9 @@ bool SceneXrender::initFailed() const
}
// the entry point for painting
qint64 SceneXrender::paint(const QRegion &damage, const QList<Toplevel *> &toplevels)
void SceneXrender::paint(int screenId, const QRegion &damage, const QList<Toplevel *> &toplevels)
{
painted_screen = -1;
QElapsedTimer renderTimer;
renderTimer.start();
painted_screen = screenId;
createStackingOrder(toplevels);
@ -255,8 +253,6 @@ qint64 SceneXrender::paint(const QRegion &damage, const QList<Toplevel *> &tople
m_backend->present(mask, updateRegion);
// do cleanup
clearStackingOrder();
return renderTimer.nsecsElapsed();
}
void SceneXrender::paintGenericScreen(int mask, const ScreenPaintData &data)

View file

@ -143,7 +143,7 @@ public:
CompositingType compositingType() const override {
return XRenderCompositing;
}
qint64 paint(const QRegion &damage, const QList<Toplevel *> &windows) override;
void paint(int screenId, const QRegion &damage, const QList<Toplevel *> &windows) override;
Scene::EffectFrame *createEffectFrame(EffectFrameImpl *frame) override;
Shadow *createShadow(Toplevel *toplevel) override;
void screenGeometryChanged(const QSize &size) override;

View file

@ -65,7 +65,7 @@ public:
// The entry point for the main part of the painting pass.
// returns the time since the last vblank signal - if there's one
// ie. "what of this frame is lost to painting"
virtual qint64 paint(const QRegion &damage, const QList<Toplevel *> &windows) = 0;
virtual void paint(int screenId, const QRegion &damage, const QList<Toplevel *> &windows) = 0;
/**
* Adds the Toplevel to the Scene.