QWaylandShmBuffer: use a memfd if we can for a simple memory buffer

which has a file descriptor we can also use to share with

Change-Id: I8d96dea9955d4c749b99fffd14cd61628c616b30
Reviewed-by: Johan Helsing <johan.helsing@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Thiago Macieira 2017-07-01 19:08:46 -07:00
parent 70044654d3
commit 32daa1a5b9

View File

@ -55,6 +55,13 @@
#include <unistd.h>
#include <sys/mman.h>
#ifdef Q_OS_LINUX
# include <sys/syscall.h>
// from linux/memfd.h:
# define MFD_CLOEXEC 0x0001U
#endif
QT_BEGIN_NAMESPACE
namespace QtWaylandClient {
@ -71,15 +78,29 @@ QWaylandShmBuffer::QWaylandShmBuffer(QWaylandDisplay *display,
{
int stride = size.width() * 4;
int alloc = stride * size.height();
int fd;
int fd = -1;
QTemporaryFile tmp(QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) +
QLatin1String("/wayland-shm-XXXXXX"));
if (!tmp.open() || !tmp.resize(alloc)) {
qWarning("QWaylandShmBuffer: failed: %s", qUtf8Printable(tmp.errorString()));
#ifdef SYS_memfd_create
fd = syscall(SYS_memfd_create, "wayland-shm", MFD_CLOEXEC);
#endif
QScopedPointer<QFile> filePointer;
if (fd == -1) {
auto tmpFile = new QTemporaryFile (QStandardPaths::writableLocation(QStandardPaths::RuntimeLocation) +
QLatin1String("/wayland-shm-XXXXXX"));
tmpFile->open();
filePointer.reset(tmpFile);
} else {
auto file = new QFile;
file->open(fd, QIODevice::ReadWrite | QIODevice::Unbuffered, QFile::AutoCloseHandle);
filePointer.reset(file);
}
if (!filePointer->isOpen() || !filePointer->resize(alloc)) {
qWarning("QWaylandShmBuffer: failed: %s", qUtf8Printable(filePointer->errorString()));
return;
}
fd = tmp.handle();
fd = filePointer->handle();
// map ourselves: QFile::map() will unmap when the object is destroyed,
// but we want this mapping to persist (unmapping in destructor)