2015-10-08 14:12:09 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
|
|
|
Copyright (C) 2015 Martin Gräßlin <mgraesslin@kde.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************/
|
|
|
|
#include "egl_gbm_backend.h"
|
|
|
|
// kwin
|
|
|
|
#include "composite.h"
|
|
|
|
#include "virtual_backend.h"
|
|
|
|
#include "options.h"
|
|
|
|
#include "screens.h"
|
2016-07-19 08:51:09 +00:00
|
|
|
#include "udev.h"
|
|
|
|
#include <logging.h>
|
2015-10-08 14:12:09 +00:00
|
|
|
// kwin libs
|
|
|
|
#include <kwinglplatform.h>
|
2017-09-08 20:30:18 +00:00
|
|
|
#include <kwinglutils.h>
|
2015-10-08 14:12:09 +00:00
|
|
|
// Qt
|
|
|
|
#include <QOpenGLContext>
|
2016-07-19 08:51:09 +00:00
|
|
|
// system
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#if HAVE_GBM
|
|
|
|
#include <gbm.h>
|
|
|
|
#endif
|
2015-10-08 14:12:09 +00:00
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
EglGbmBackend::EglGbmBackend(VirtualBackend *b)
|
2017-09-03 08:26:20 +00:00
|
|
|
: AbstractEglBackend()
|
2015-10-08 14:12:09 +00:00
|
|
|
, m_backend(b)
|
|
|
|
{
|
|
|
|
// Egl is always direct rendering
|
|
|
|
setIsDirectRendering(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
EglGbmBackend::~EglGbmBackend()
|
|
|
|
{
|
|
|
|
while (GLRenderTarget::isRenderTargetBound()) {
|
|
|
|
GLRenderTarget::popRenderTarget();
|
|
|
|
}
|
|
|
|
delete m_fbo;
|
|
|
|
delete m_backBuffer;
|
|
|
|
cleanup();
|
2016-07-19 08:51:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::initGbmDevice()
|
|
|
|
{
|
2016-08-19 13:40:39 +00:00
|
|
|
if (m_backend->drmFd() != -1) {
|
2016-07-19 08:51:09 +00:00
|
|
|
// already initialized
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QScopedPointer<Udev> udev(new Udev);
|
2017-09-29 19:20:12 +00:00
|
|
|
UdevDevice::Ptr device = udev->virtualGpu();
|
2016-07-19 08:51:09 +00:00
|
|
|
if (!device) {
|
2017-09-29 19:20:12 +00:00
|
|
|
// if we don't have a virtual (vgem) device, try to find a render node
|
|
|
|
qCDebug(KWIN_VIRTUAL) << "No vgem device, looking for a render node";
|
|
|
|
device = udev->renderNode();
|
2016-07-19 08:51:09 +00:00
|
|
|
}
|
|
|
|
if (!device) {
|
|
|
|
qCDebug(KWIN_VIRTUAL) << "Neither a render node, nor a vgem device found";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qCDebug(KWIN_VIRTUAL) << "Found a device: " << device->devNode();
|
2016-08-19 13:40:39 +00:00
|
|
|
int fd = open(device->devNode(), O_RDWR | O_CLOEXEC);
|
|
|
|
if (fd == -1) {
|
2016-07-19 08:51:09 +00:00
|
|
|
qCWarning(KWIN_VIRTUAL) << "Failed to open: " << device->devNode();
|
|
|
|
return;
|
|
|
|
}
|
2016-08-19 13:40:39 +00:00
|
|
|
m_backend->setDrmFd(fd);
|
2016-07-19 08:51:09 +00:00
|
|
|
#if HAVE_GBM
|
2016-08-19 13:40:39 +00:00
|
|
|
auto gbmDevice = gbm_create_device(fd);
|
|
|
|
if (!gbmDevice) {
|
2016-07-19 08:51:09 +00:00
|
|
|
qCWarning(KWIN_VIRTUAL) << "Failed to open gbm device";
|
|
|
|
}
|
2016-08-19 13:40:39 +00:00
|
|
|
m_backend->setGbmDevice(gbmDevice);
|
2016-07-19 08:51:09 +00:00
|
|
|
#endif
|
2015-10-08 14:12:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EglGbmBackend::initializeEgl()
|
|
|
|
{
|
|
|
|
initClientExtensions();
|
2016-07-18 08:27:56 +00:00
|
|
|
EGLDisplay display = m_backend->sceneEglDisplay();
|
2015-10-08 14:12:09 +00:00
|
|
|
|
|
|
|
// Use eglGetPlatformDisplayEXT() to get the display pointer
|
|
|
|
// if the implementation supports it.
|
2016-07-18 08:27:56 +00:00
|
|
|
if (display == EGL_NO_DISPLAY) {
|
check for EGL_KHR_platform_gbm extension as well
Summary:
some drivers, like Mali have EGL_KHR_platform_gbm
but not EGL_MESA_platform_gbm
Test Plan: pending a test on rock64 board
Reviewers: #kwin, #plasma, davidedmundson, graesslin
Reviewed By: #kwin, #plasma, graesslin
Subscribers: graesslin, garg, davidedmundson, plasma-devel, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D10346
2018-02-15 10:41:10 +00:00
|
|
|
const bool hasMesaGBM = hasClientExtension(QByteArrayLiteral("EGL_MESA_platform_gbm"));
|
|
|
|
const bool hasKHRGBM = hasClientExtension(QByteArrayLiteral("EGL_KHR_platform_gbm"));
|
|
|
|
const GLenum platform = hasMesaGBM ? EGL_PLATFORM_GBM_MESA : EGL_PLATFORM_GBM_KHR;
|
|
|
|
|
2016-07-18 08:27:56 +00:00
|
|
|
if (!hasClientExtension(QByteArrayLiteral("EGL_EXT_platform_base")) ||
|
check for EGL_KHR_platform_gbm extension as well
Summary:
some drivers, like Mali have EGL_KHR_platform_gbm
but not EGL_MESA_platform_gbm
Test Plan: pending a test on rock64 board
Reviewers: #kwin, #plasma, davidedmundson, graesslin
Reviewed By: #kwin, #plasma, graesslin
Subscribers: graesslin, garg, davidedmundson, plasma-devel, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D10346
2018-02-15 10:41:10 +00:00
|
|
|
(!hasMesaGBM && !hasKHRGBM)) {
|
|
|
|
setFailed("missing one or more extensions between EGL_EXT_platform_base, EGL_MESA_platform_gbm, EGL_KHR_platform_gbm");
|
2016-07-18 08:27:56 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-10-08 14:12:09 +00:00
|
|
|
|
2016-07-19 08:51:09 +00:00
|
|
|
#if HAVE_GBM
|
|
|
|
initGbmDevice();
|
2016-08-19 13:40:39 +00:00
|
|
|
if (auto device = m_backend->gbmDevice()) {
|
check for EGL_KHR_platform_gbm extension as well
Summary:
some drivers, like Mali have EGL_KHR_platform_gbm
but not EGL_MESA_platform_gbm
Test Plan: pending a test on rock64 board
Reviewers: #kwin, #plasma, davidedmundson, graesslin
Reviewed By: #kwin, #plasma, graesslin
Subscribers: graesslin, garg, davidedmundson, plasma-devel, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D10346
2018-02-15 10:41:10 +00:00
|
|
|
display = eglGetPlatformDisplayEXT(platform, device, nullptr);
|
2016-07-19 08:51:09 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (display == EGL_NO_DISPLAY) {
|
|
|
|
qCWarning(KWIN_VIRTUAL) << "Failed to create EGLDisplay through GBM device, trying with default device";
|
check for EGL_KHR_platform_gbm extension as well
Summary:
some drivers, like Mali have EGL_KHR_platform_gbm
but not EGL_MESA_platform_gbm
Test Plan: pending a test on rock64 board
Reviewers: #kwin, #plasma, davidedmundson, graesslin
Reviewed By: #kwin, #plasma, graesslin
Subscribers: graesslin, garg, davidedmundson, plasma-devel, kwin
Tags: #kwin
Differential Revision: https://phabricator.kde.org/D10346
2018-02-15 10:41:10 +00:00
|
|
|
display = eglGetPlatformDisplayEXT(platform, EGL_DEFAULT_DISPLAY, nullptr);
|
2016-07-19 08:51:09 +00:00
|
|
|
}
|
2016-07-18 08:27:56 +00:00
|
|
|
}
|
2015-10-08 14:12:09 +00:00
|
|
|
|
|
|
|
if (display == EGL_NO_DISPLAY)
|
|
|
|
return false;
|
|
|
|
setEglDisplay(display);
|
|
|
|
return initEglAPI();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::init()
|
|
|
|
{
|
2015-11-25 12:09:28 +00:00
|
|
|
if (!initializeEgl()) {
|
|
|
|
setFailed("Could not initialize egl");
|
|
|
|
return;
|
|
|
|
}
|
2015-10-08 14:12:09 +00:00
|
|
|
if (!initRenderingContext()) {
|
|
|
|
setFailed("Could not initialize rendering context");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
initKWinGL();
|
|
|
|
|
|
|
|
m_backBuffer = new GLTexture(GL_RGB8, screens()->size().width(), screens()->size().height());
|
|
|
|
m_fbo = new GLRenderTarget(*m_backBuffer);
|
|
|
|
if (!m_fbo->valid()) {
|
|
|
|
setFailed("Could not create framebuffer object");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
GLRenderTarget::pushRenderTarget(m_fbo);
|
|
|
|
if (!m_fbo->isRenderTargetBound()) {
|
|
|
|
setFailed("Failed to bind framebuffer object");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (checkGLError("Init")) {
|
|
|
|
setFailed("Error during init of EglGbmBackend");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setSupportsBufferAge(false);
|
|
|
|
initWayland();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EglGbmBackend::initRenderingContext()
|
|
|
|
{
|
|
|
|
initBufferConfigs();
|
|
|
|
|
|
|
|
const char* eglExtensionsCString = eglQueryString(eglDisplay(), EGL_EXTENSIONS);
|
|
|
|
const QList<QByteArray> extensions = QByteArray::fromRawData(eglExtensionsCString, qstrlen(eglExtensionsCString)).split(' ');
|
|
|
|
if (!extensions.contains(QByteArrayLiteral("EGL_KHR_surfaceless_context"))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-13 07:29:49 +00:00
|
|
|
if (!createContext()) {
|
|
|
|
return false;
|
2015-10-08 14:12:09 +00:00
|
|
|
}
|
|
|
|
setSurfaceLessContext(true);
|
|
|
|
|
|
|
|
return makeCurrent();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EglGbmBackend::initBufferConfigs()
|
|
|
|
{
|
|
|
|
const EGLint config_attribs[] = {
|
|
|
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
|
|
|
EGL_RED_SIZE, 1,
|
|
|
|
EGL_GREEN_SIZE, 1,
|
|
|
|
EGL_BLUE_SIZE, 1,
|
|
|
|
EGL_ALPHA_SIZE, 0,
|
2015-10-30 11:56:03 +00:00
|
|
|
EGL_RENDERABLE_TYPE, isOpenGLES() ? EGL_OPENGL_ES2_BIT : EGL_OPENGL_BIT,
|
2015-10-08 14:12:09 +00:00
|
|
|
EGL_CONFIG_CAVEAT, EGL_NONE,
|
|
|
|
EGL_NONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint count;
|
|
|
|
EGLConfig configs[1024];
|
|
|
|
if (eglChooseConfig(eglDisplay(), config_attribs, configs, 1, &count) == EGL_FALSE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (count != 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
setConfig(configs[0]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::present()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::screenGeometryChanged(const QSize &size)
|
|
|
|
{
|
|
|
|
Q_UNUSED(size)
|
|
|
|
// TODO, create new buffer?
|
|
|
|
}
|
|
|
|
|
2017-09-08 20:30:18 +00:00
|
|
|
SceneOpenGLTexturePrivate *EglGbmBackend::createBackendTexture(SceneOpenGLTexture *texture)
|
2015-10-08 14:12:09 +00:00
|
|
|
{
|
|
|
|
return new EglGbmTexture(texture, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion EglGbmBackend::prepareRenderingFrame()
|
|
|
|
{
|
|
|
|
startRenderTimer();
|
|
|
|
if (!GLRenderTarget::isRenderTargetBound()) {
|
|
|
|
GLRenderTarget::pushRenderTarget(m_fbo);
|
|
|
|
}
|
|
|
|
return QRegion(0, 0, screens()->size().width(), screens()->size().height());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void convertFromGLImage(QImage &img, int w, int h)
|
|
|
|
{
|
|
|
|
// from QtOpenGL/qgl.cpp
|
|
|
|
// Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies)
|
|
|
|
// see http://qt.gitorious.org/qt/qt/blobs/master/src/opengl/qgl.cpp
|
|
|
|
if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
|
|
|
|
// OpenGL gives RGBA; Qt wants ARGB
|
|
|
|
uint *p = (uint*)img.bits();
|
|
|
|
uint *end = p + w * h;
|
|
|
|
while (p < end) {
|
|
|
|
uint a = *p << 24;
|
|
|
|
*p = (*p >> 8) | a;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// OpenGL gives ABGR (i.e. RGBA backwards); Qt wants ARGB
|
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
uint *q = (uint*)img.scanLine(y);
|
|
|
|
for (int x = 0; x < w; ++x) {
|
|
|
|
const uint pixel = *q;
|
|
|
|
*q = ((pixel << 16) & 0xff0000) | ((pixel >> 16) & 0xff)
|
|
|
|
| (pixel & 0xff00ff00);
|
|
|
|
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
img = img.mirrored();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::endRenderingFrame(const QRegion &renderedRegion, const QRegion &damagedRegion)
|
|
|
|
{
|
|
|
|
Q_UNUSED(renderedRegion)
|
|
|
|
Q_UNUSED(damagedRegion)
|
|
|
|
glFlush();
|
|
|
|
if (m_backend->saveFrames()) {
|
|
|
|
QImage img = QImage(QSize(m_backBuffer->width(), m_backBuffer->height()), QImage::Format_ARGB32);
|
2015-11-17 10:00:53 +00:00
|
|
|
glReadnPixels(0, 0, m_backBuffer->width(), m_backBuffer->height(), GL_RGBA, GL_UNSIGNED_BYTE, img.byteCount(), (GLvoid*)img.bits());
|
2015-10-08 14:12:09 +00:00
|
|
|
convertFromGLImage(img, m_backBuffer->width(), m_backBuffer->height());
|
|
|
|
img.save(QStringLiteral("%1/%2.png").arg(m_backend->saveFrames()).arg(QString::number(m_frameCounter++)));
|
|
|
|
}
|
|
|
|
GLRenderTarget::popRenderTarget();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EglGbmBackend::usesOverlayWindow() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************
|
|
|
|
* EglTexture
|
|
|
|
************************************************/
|
|
|
|
|
2017-09-08 20:30:18 +00:00
|
|
|
EglGbmTexture::EglGbmTexture(KWin::SceneOpenGLTexture *texture, EglGbmBackend *backend)
|
2015-10-08 14:12:09 +00:00
|
|
|
: AbstractEglTexture(texture, backend)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EglGbmTexture::~EglGbmTexture() = default;
|
|
|
|
|
|
|
|
} // namespace
|