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.
2020-11-19 08:52:29 +00:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: 2020 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
|
|
|
|
|
|
|
|
SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "sgivideosyncvsyncmonitor.h"
|
2021-01-28 12:03:24 +00:00
|
|
|
#include "glxconvenience.h"
|
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.
2020-11-19 08:52:29 +00:00
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
#include <QX11Info>
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
SGIVideoSyncVsyncMonitor *SGIVideoSyncVsyncMonitor::create(QObject *parent)
|
|
|
|
{
|
|
|
|
const char *extensions = glXQueryExtensionsString(QX11Info::display(),
|
|
|
|
QX11Info::appScreen());
|
|
|
|
if (!strstr(extensions, "GLX_SGI_video_sync")) {
|
|
|
|
return nullptr; // GLX_SGI_video_sync is unsupported.
|
|
|
|
}
|
|
|
|
|
|
|
|
SGIVideoSyncVsyncMonitor *monitor = new SGIVideoSyncVsyncMonitor(parent);
|
|
|
|
if (monitor->isValid()) {
|
|
|
|
return monitor;
|
|
|
|
}
|
|
|
|
delete monitor;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
SGIVideoSyncVsyncMonitorHelper::SGIVideoSyncVsyncMonitorHelper(QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
{
|
|
|
|
// Establish a new X11 connection to avoid locking up the main X11 connection.
|
|
|
|
m_display = XOpenDisplay(DisplayString(QX11Info::display()));
|
|
|
|
if (!m_display) {
|
|
|
|
qCDebug(KWIN_X11STANDALONE) << "Failed to establish vsync monitor X11 connection";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-28 12:03:24 +00:00
|
|
|
Window rootWindow = DefaultRootWindow(m_display);
|
|
|
|
|
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.
2020-11-19 08:52:29 +00:00
|
|
|
const int attribs[] = {
|
|
|
|
GLX_RENDER_TYPE, GLX_RGBA_BIT,
|
|
|
|
GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2021-01-28 12:03:24 +00:00
|
|
|
GLXFBConfig config = chooseGlxFbConfig(m_display, attribs);
|
|
|
|
if (!config) {
|
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.
2020-11-19 08:52:29 +00:00
|
|
|
qCDebug(KWIN_X11STANDALONE) << "Couldn't find any suitable FBConfig for vsync monitor";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-28 12:03:24 +00:00
|
|
|
XVisualInfo *visualInfo = glXGetVisualFromFBConfig(m_display, config);
|
|
|
|
if (!visualInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Visual *visual = visualInfo->visual;
|
|
|
|
const int depth = visualInfo->depth;
|
|
|
|
XFree(visualInfo);
|
|
|
|
|
|
|
|
Colormap colormap = XCreateColormap(m_display, rootWindow, visual, AllocNone);
|
|
|
|
XSetWindowAttributes attributes;
|
|
|
|
attributes.colormap = colormap;
|
|
|
|
|
|
|
|
m_dummyWindow = XCreateWindow(m_display, rootWindow, 0, 0, 1, 1, 0, depth,
|
|
|
|
InputOutput, visual, CWColormap, &attributes);
|
|
|
|
XFreeColormap(m_display, colormap);
|
|
|
|
if (!m_dummyWindow) {
|
|
|
|
qCDebug(KWIN_X11STANDALONE) << "Failed to create a dummy window for vsync monitor";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_drawable = glXCreateWindow(m_display, config, m_dummyWindow, nullptr);
|
|
|
|
if (!m_drawable) {
|
|
|
|
qCDebug(KWIN_X11STANDALONE) << "Failed to create GLXWindow for dummy window";
|
|
|
|
return;
|
|
|
|
}
|
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.
2020-11-19 08:52:29 +00:00
|
|
|
|
|
|
|
m_localContext = glXCreateNewContext(m_display, config, GLX_RGBA_TYPE, 0, true);
|
|
|
|
if (!m_localContext) {
|
|
|
|
qCDebug(KWIN_X11STANDALONE) << "Failed to create opengl context for vsync monitor";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SGIVideoSyncVsyncMonitorHelper::~SGIVideoSyncVsyncMonitorHelper()
|
|
|
|
{
|
|
|
|
if (m_localContext) {
|
|
|
|
glXDestroyContext(m_display, m_localContext);
|
|
|
|
}
|
2021-01-28 12:03:24 +00:00
|
|
|
if (m_drawable) {
|
|
|
|
glXDestroyWindow(m_display, m_drawable);
|
|
|
|
}
|
|
|
|
if (m_dummyWindow) {
|
|
|
|
XDestroyWindow(m_display, m_dummyWindow);
|
|
|
|
}
|
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.
2020-11-19 08:52:29 +00:00
|
|
|
if (m_display) {
|
|
|
|
XCloseDisplay(m_display);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SGIVideoSyncVsyncMonitorHelper::isValid() const
|
|
|
|
{
|
|
|
|
return m_display && m_localContext && m_drawable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SGIVideoSyncVsyncMonitorHelper::poll()
|
|
|
|
{
|
|
|
|
if (!glXMakeCurrent(m_display, m_drawable, m_localContext)) {
|
|
|
|
qCDebug(KWIN_X11STANDALONE) << "Failed to make vsync monitor OpenGL context current";
|
|
|
|
emit errorOccurred();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint count;
|
|
|
|
|
|
|
|
glXGetVideoSyncSGI(&count);
|
|
|
|
glXWaitVideoSyncSGI(2, (count + 1) % 2, &count);
|
|
|
|
|
|
|
|
// Using monotonic clock is inaccurate, but it's still a pretty good estimate.
|
|
|
|
emit vblankOccurred(std::chrono::steady_clock::now().time_since_epoch());
|
|
|
|
}
|
|
|
|
|
|
|
|
SGIVideoSyncVsyncMonitor::SGIVideoSyncVsyncMonitor(QObject *parent)
|
|
|
|
: VsyncMonitor(parent)
|
|
|
|
, m_thread(new QThread)
|
|
|
|
, m_helper(new SGIVideoSyncVsyncMonitorHelper)
|
|
|
|
{
|
|
|
|
m_helper->moveToThread(m_thread);
|
|
|
|
|
|
|
|
connect(m_helper, &SGIVideoSyncVsyncMonitorHelper::errorOccurred,
|
|
|
|
this, &SGIVideoSyncVsyncMonitor::errorOccurred);
|
|
|
|
connect(m_helper, &SGIVideoSyncVsyncMonitorHelper::vblankOccurred,
|
|
|
|
this, &SGIVideoSyncVsyncMonitor::vblankOccurred);
|
|
|
|
|
|
|
|
m_thread->setObjectName(QStringLiteral("vsync event monitor"));
|
|
|
|
m_thread->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
SGIVideoSyncVsyncMonitor::~SGIVideoSyncVsyncMonitor()
|
|
|
|
{
|
|
|
|
m_thread->quit();
|
|
|
|
m_thread->wait();
|
|
|
|
|
|
|
|
delete m_helper;
|
|
|
|
delete m_thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SGIVideoSyncVsyncMonitor::isValid() const
|
|
|
|
{
|
|
|
|
return m_helper->isValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SGIVideoSyncVsyncMonitor::arm()
|
|
|
|
{
|
|
|
|
QMetaObject::invokeMethod(m_helper, &SGIVideoSyncVsyncMonitorHelper::poll);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace KWin
|