Skip to content

Commit

Permalink
Add support for forced VSYNC using the EGLFS platform plugin.
Browse files Browse the repository at this point in the history
Before a buffer swap the new QEglFSHooks::waitForVSync method is
called which looks at QT_QPA_EGLFS_FORCEVSYNC and - if that is set
and non-null - calls ioctl with the FBIO_WAITFORVSYNC request on
the framebuffer device.

This is required on some embedded platforms where the driver does not
support VSYNC yet the Kernel provides a generic implementation.

I tested this using QML_RENDER_TIMING=1 which proofs that the frame
rate for an example of mine drops from >125fps to a straight ~60fps
with a few frames that take ~33ms (i.e. 30fps) as expected for VSYNC.

To prevent excessive open/close calls on the frame buffer device
per frame, the file descriptor is now cached. To keep the QEglFSHooks
interface as clean as possible this is done via a global static in
qeglfshooks_stub.cpp and initialized and freed in platformInit and
platformDestroy.

Change-Id: I4d31b227c65ff22aa089db0fbc62c89a59cbb6c7
Reviewed-by: Sean Harmer <[email protected]>
Reviewed-by: Samuel Rødal <[email protected]>
  • Loading branch information
milianw authored and The Qt Project committed Feb 22, 2013
1 parent 25c9005 commit 4f14b42
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
1 change: 1 addition & 0 deletions src/plugins/platforms/eglfs/qeglfscontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void QEglFSContext::swapBuffers(QPlatformSurface *surface)
cursor->paintOnScreen();
}

hooks->waitForVSync();
QEGLPlatformContext::swapBuffers(surface);
}

Expand Down
1 change: 1 addition & 0 deletions src/plugins/platforms/eglfs/qeglfshooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class QEglFSHooks
virtual bool hasCapability(QPlatformIntegration::Capability cap) const;
virtual QEglFSCursor *createCursor(QEglFSScreen *screen) const;
virtual bool filterConfig(EGLDisplay display, EGLConfig config) const;
virtual void waitForVSync() const;

virtual const char *fbDeviceName() const;
};
Expand Down
50 changes: 28 additions & 22 deletions src/plugins/platforms/eglfs/qeglfshooks_stub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,14 @@
#include <sys/ioctl.h>

#include <private/qmath_p.h>
#include <private/qcore_unix_p.h>

QT_BEGIN_NAMESPACE

// file descriptor for the frame buffer
// this is a global static to keep the QEglFSHooks interface as clean as possible
static int framebuffer = -1;

const char *QEglFSHooks::fbDeviceName() const
{
return "/dev/fb0";
Expand All @@ -58,10 +63,17 @@ const char *QEglFSHooks::fbDeviceName() const
void QEglFSHooks::platformInit()
{
Q_UNUSED(hooks);

framebuffer = qt_safe_open(fbDeviceName(), O_RDONLY);

if (framebuffer == -1)
qWarning("EGLFS: Failed to open %s", fbDeviceName());
}

void QEglFSHooks::platformDestroy()
{
if (framebuffer != -1)
close(framebuffer);
}

EGLNativeDisplayType QEglFSHooks::platformDisplay() const
Expand All @@ -86,22 +98,16 @@ QSizeF QEglFSHooks::physicalScreenSize() const
}

struct fb_var_screeninfo vinfo;
int fd = open(fbDeviceName(), O_RDONLY);

int w = -1;
int h = -1;

if (fd != -1) {
if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
if (framebuffer != -1) {
if (ioctl(framebuffer, FBIOGET_VSCREENINFO, &vinfo) == -1) {
qWarning("EGLFS: Could not query variable screen info.");
} else {
w = vinfo.width;
h = vinfo.height;
}

close(fd);
} else {
qWarning("EGLFS: Failed to open %s to detect physical screen size.", fbDeviceName());
}

const int defaultPhysicalDpi = 100;
Expand Down Expand Up @@ -140,22 +146,17 @@ QSize QEglFSHooks::screenSize() const
}

struct fb_var_screeninfo vinfo;
int fd = open(fbDeviceName(), O_RDONLY);

int xres = -1;
int yres = -1;

if (fd != -1) {
if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) == -1) {
if (framebuffer != -1) {
if (ioctl(framebuffer, FBIOGET_VSCREENINFO, &vinfo) == -1) {
qWarning("EGLFS: Could not query variable screen info.");
} else {
xres = vinfo.xres;
yres = vinfo.yres;
}

close(fd);
} else {
qWarning("EGLFS: Failed to open %s to detect screen resolution.", fbDeviceName());
}

const int defaultWidth = 800;
Expand Down Expand Up @@ -185,17 +186,12 @@ int QEglFSHooks::screenDepth() const

if (depth == 0) {
struct fb_var_screeninfo vinfo;
int fd = open(fbDeviceName(), O_RDONLY);

if (fd != -1) {
if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) == -1)
if (framebuffer != -1) {
if (ioctl(framebuffer, FBIOGET_VSCREENINFO, &vinfo) == -1)
qWarning("EGLFS: Could not query variable screen info.");
else
depth = vinfo.bits_per_pixel;

close(fd);
} else {
qWarning("EGLFS: Failed to open %s to detect screen depth.", fbDeviceName());
}

const int defaultDepth = 32;
Expand Down Expand Up @@ -250,6 +246,16 @@ QEglFSCursor *QEglFSHooks::createCursor(QEglFSScreen *screen) const
return 0;
}

void QEglFSHooks::waitForVSync() const
{
static const bool forceSync = qgetenv("QT_QPA_EGLFS_FORCEVSYNC").toInt();
if (forceSync && framebuffer != -1) {
int arg = 0;
if (ioctl(framebuffer, FBIO_WAITFORVSYNC, &arg) == -1)
qWarning("Could not wait for vsync.");
}
}

#ifndef EGLFS_PLATFORM_HOOKS
QEglFSHooks stubHooks;
#endif
Expand Down

0 comments on commit 4f14b42

Please sign in to comment.