[hwcomposer] Rework the vsync code

This changes how we synchronize through vsync. We use a mutex and a
wait condition to synchronize the threads. When presenting the frame
our main gui thread blocks and will be woken up by the vsync event
(or a timeout of max 1 frame time slot). In order to minimize the
blocked time we use the blocksForRetrace functionality from the GLX
compositor.

Given this change we no longer need to tell the compositor that we
are swapping the frame, it's blocked anyway. Also we don't need the
failsafe QTimer anymore.

With this change applied on a Nexus 5 it's succeeding the "Martin
tortures phone test". It doesn't tear anymore and has a smooth
experience.

I'm rather disappointed by the fact that we need to block in order
to get vsync. This means Android/hwcomposer is as bad as GLX. So
much for the "Android stack is so awesome", in fact it's not. Anybody
thinking it's awesome should compare to DRM/KMS and especially atomic
modesetting. Yes it's possible to present frames without tearing and
without having to block the rendering thread.

Reviewed-By: Marco Martin and Bhushan Shah
icc-effect-5.14.5
Martin Gräßlin 2015-10-20 13:16:05 +02:00
parent d24411b9ac
commit 93b5e13308
3 changed files with 22 additions and 35 deletions

View File

@ -36,6 +36,7 @@ EglHwcomposerBackend::EglHwcomposerBackend(HwcomposerBackend *backend)
// EGL is always direct rendering
setIsDirectRendering(true);
setSyncsToVBlank(true);
setBlocksForRetrace(true);
}
EglHwcomposerBackend::~EglHwcomposerBackend()

View File

@ -28,8 +28,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <KWayland/Server/display.h>
#include <KWayland/Server/output_interface.h>
#include <KWayland/Server/seat_interface.h>
// Qt
#include <QTimer>
// hybris/android
#include <hardware/hardware.h>
#include <hardware/hwcomposer.h>
@ -43,12 +41,8 @@ namespace KWin
HwcomposerBackend::HwcomposerBackend(QObject *parent)
: AbstractBackend(parent)
, m_vsyncFailSafeTimer(new QTimer(this))
{
handleOutputs();
m_vsyncFailSafeTimer->setSingleShot(true);
m_vsyncFailSafeTimer->setInterval(1000);
connect(m_vsyncFailSafeTimer, &QTimer::timeout, this, &HwcomposerBackend::vsync);
}
HwcomposerBackend::~HwcomposerBackend()
@ -127,9 +121,7 @@ void HwcomposerBackend::init()
if (disp != 0) {
return;
}
QMetaObject::invokeMethod(dynamic_cast<HwcomposerBackend*>(waylandServer()->backend()),
"vsync",
Qt::QueuedConnection);
dynamic_cast<HwcomposerBackend*>(waylandServer()->backend())->wakeVSync();
};
procs->hotplug = [] (const struct hwc_procs* procs, int disp, int connected) {
Q_UNUSED(procs)
@ -148,6 +140,9 @@ void HwcomposerBackend::init()
}
m_displaySize = output->pixelSize();
m_refreshRate = output->refreshRate();
if (m_refreshRate != 0) {
m_vsyncInterval = 1000000/m_refreshRate;
}
qCDebug(KWIN_HWCOMPOSER) << "Display size:" << m_displaySize;
qCDebug(KWIN_HWCOMPOSER) << "Refresh rate:" << m_refreshRate;
@ -201,27 +196,18 @@ OpenGLBackend *HwcomposerBackend::createOpenGLBackend()
return new EglHwcomposerBackend(this);
}
void HwcomposerBackend::present()
void HwcomposerBackend::waitVSync()
{
if (m_pageFlipPending) {
return;
}
m_pageFlipPending = true;
if (Compositor::self()) {
m_vsyncFailSafeTimer->start();
Compositor::self()->aboutToSwapBuffers();
}
m_vsyncMutex.lock();
m_vsyncWaitCondition.wait(&m_vsyncMutex, m_vsyncInterval);
m_vsyncMutex.unlock();
}
void HwcomposerBackend::vsync()
void HwcomposerBackend::wakeVSync()
{
m_vsyncFailSafeTimer->stop();
if (m_pageFlipPending) {
m_pageFlipPending = false;
if (Compositor::self()) {
Compositor::self()->bufferSwapComplete();
}
}
m_vsyncMutex.lock();
m_vsyncWaitCondition.wakeAll();
m_vsyncMutex.unlock();
}
static void initLayer(hwc_layer_1_t *layer, const hwc_rect_t &rect)
@ -279,7 +265,7 @@ HwcomposerWindow::~HwcomposerWindow()
void HwcomposerWindow::present(HWComposerNativeWindowBuffer *buffer)
{
m_backend->present();
m_backend->waitVSync();
hwc_composer_device_1_t *device = m_backend->device();
auto fblayer = &m_list[0]->hwLayers[1];

View File

@ -21,6 +21,9 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#define KWIN_HWCOMPOSER_BACKEND_H
#include "abstract_backend.h"
#include <QMutex>
#include <QWaitCondition>
// libhybris
#include <hwcomposer_window.h>
// needed as hwcomposer_window.h includes EGL which on non-arm includes Xlib
@ -32,8 +35,6 @@ typedef struct hwc_composer_device_1 hwc_composer_device_1_t;
class HWComposerNativeWindowBuffer;
class QTimer;
namespace KWin
{
@ -61,14 +62,12 @@ public:
hwc_composer_device_1_t *device() const {
return m_device;
}
void present();
int refreshRate() const {
return m_refreshRate;
}
void enableVSync(bool enable);
public Q_SLOTS:
void vsync();
void waitVSync();
void wakeVSync();
private Q_SLOTS:
void toggleBlankOutput();
@ -77,10 +76,11 @@ private:
QSize m_displaySize;
hwc_composer_device_1_t *m_device = nullptr;
bool m_outputBlank = true;
bool m_pageFlipPending = false;
int m_refreshRate = 60000;
QTimer *m_vsyncFailSafeTimer;
int m_vsyncInterval = 16;
bool m_hasVsync = false;
QMutex m_vsyncMutex;
QWaitCondition m_vsyncWaitCondition;
};
class HwcomposerWindow : public HWComposerNativeWindow