Skip to content
This repository has been archived by the owner on Jul 23, 2020. It is now read-only.

Commit

Permalink
QGtkWindow: Improve the tick callback
Browse files Browse the repository at this point in the history
* Don't unconditionally install a tick callback, these are only used for
  the QtQuick (or other requestUpdate) users, not the widgets case
* Remove the tick callback once window updates have ceased for a while,
  and reinstall it later.

This significantly decreases the amount of unnecessary wakeups on
otherwise idle gtkplatform clients.

For instance, telegram-desktop while idle (measured using powertop):
    before: 60-80 wakeups per second (consuming 160-220 mW of power)
    after: ~0.5-2 wakeups per second (consuming 0.8-2.5 mW of power)
  • Loading branch information
rburchell committed Apr 27, 2018
1 parent def7d52 commit 5e0ab4b
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/platform-plugin/qgtkwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ void QGtkWindow::create(Qt::WindowType windowType)
g_signal_connect(m_window.get(), "size-allocate", G_CALLBACK(size_allocate_cb), this);
g_signal_connect(m_window.get(), "delete-event", G_CALLBACK(delete_cb), this);
g_signal_connect(m_window.get(), "window-state-event", G_CALLBACK(window_state_event_cb), this);
m_tick_callback = gtk_widget_add_tick_callback(m_window.get(), QGtkWindow::windowTickCallback, this, NULL);

GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add(GTK_CONTAINER(m_window.get()), vbox);
Expand Down
4 changes: 3 additions & 1 deletion src/platform-plugin/qgtkwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ QT_BEGIN_NAMESPACE

class QTouchDevice;

class QGtkWindow : public QPlatformWindow
class QGtkWindow : public QObject, public QPlatformWindow
{
public:
QGtkWindow(QWindow *window);
Expand Down Expand Up @@ -172,6 +172,8 @@ class QGtkWindow : public QPlatformWindow
double m_initialZoom = 0;
bool m_initialRotateSet = false;
double m_initialRotate = 0;
bool m_hasTickCallback = false;
QTimer *m_cancelTickTimer = nullptr;
};

class QGtkCourierObject : public QObject
Expand Down
31 changes: 31 additions & 0 deletions src/platform-plugin/qgtkwindow_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include <QtCore/qloggingcategory.h>
#include <QtCore/qthread.h>
#include <QtCore/qtimer.h>
#include <QtGui/private/qwindow_p.h>

#include "CSystrace.h"
Expand Down Expand Up @@ -122,13 +123,43 @@ void QGtkWindow::onWindowTickCallback()
m_wantsUpdate = false;
TRACE_EVENT_ASYNC_END0("gfx", "QGtkWindow::requestUpdate", this);
QWindowPrivate::get(window())->deliverUpdateRequest();
} else {
if (m_cancelTickTimer) {
return;
}
qCDebug(lcWindowRender) << "Preparing to remove tick callback" << this;
// Stop delivering ticks if update requests end for a long time.
m_cancelTickTimer = new QTimer(this);
m_cancelTickTimer->setInterval(1000);
m_cancelTickTimer->setSingleShot(true);
m_cancelTickTimer->start();
connect(m_cancelTickTimer, &QTimer::timeout, this, [=]() {
if (!m_wantsUpdate) {
qCDebug(lcWindowRender) << "Removing tick callback" << this;
gtk_widget_remove_tick_callback(m_window.get(), m_tick_callback);
m_tick_callback = 0;
m_hasTickCallback = false;
m_cancelTickTimer->deleteLater();
m_cancelTickTimer = nullptr;
}
});
}
}

void QGtkWindow::requestUpdate()
{
TRACE_EVENT_ASYNC_BEGIN0("gfx", "QGtkWindow::requestUpdate", this);
m_wantsUpdate = true;
if (!m_hasTickCallback) {
qCDebug(lcWindowRender) << "Installing tick callback" << this;
m_hasTickCallback = true;
m_tick_callback = gtk_widget_add_tick_callback(m_window.get(), QGtkWindow::windowTickCallback, this, NULL);
}
if (m_cancelTickTimer) {
m_cancelTickTimer->deleteLater();
m_cancelTickTimer = nullptr;
qCDebug(lcWindowRender) << "Cancelling remove of tick callback" << this;
}
}

QGtkCourierObject::QGtkCourierObject(QObject *parent)
Expand Down

0 comments on commit 5e0ab4b

Please sign in to comment.