2015-04-10 08:44:07 +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 "drm_backend.h"
|
2015-05-05 15:27:03 +00:00
|
|
|
#include "logging.h"
|
2015-04-10 08:44:07 +00:00
|
|
|
#include "options.h"
|
|
|
|
#include "screens.h"
|
|
|
|
// kwin libs
|
|
|
|
#include <kwinglplatform.h>
|
|
|
|
// Qt
|
|
|
|
#include <QOpenGLContext>
|
|
|
|
// system
|
|
|
|
#include <gbm.h>
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
|
|
|
|
|
|
|
EglGbmBackend::EglGbmBackend(DrmBackend *b)
|
|
|
|
: QObject(NULL)
|
|
|
|
, AbstractEglBackend()
|
|
|
|
, m_backend(b)
|
|
|
|
{
|
|
|
|
initializeEgl();
|
|
|
|
init();
|
|
|
|
// Egl is always direct rendering
|
|
|
|
setIsDirectRendering(true);
|
2015-07-17 18:39:37 +00:00
|
|
|
setSyncsToVBlank(true);
|
2015-04-24 08:05:38 +00:00
|
|
|
connect(m_backend, &DrmBackend::outputAdded, this, &EglGbmBackend::createOutput);
|
|
|
|
connect(m_backend, &DrmBackend::outputRemoved, this,
|
|
|
|
[this] (DrmOutput *output) {
|
|
|
|
auto it = std::find_if(m_outputs.begin(), m_outputs.end(),
|
|
|
|
[output] (const Output &o) {
|
|
|
|
return o.output == output;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (it == m_outputs.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cleanupOutput(*it);
|
|
|
|
m_outputs.erase(it);
|
|
|
|
}
|
|
|
|
);
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
EglGbmBackend::~EglGbmBackend()
|
|
|
|
{
|
|
|
|
// TODO: cleanup front buffer?
|
|
|
|
cleanup();
|
|
|
|
if (m_device) {
|
|
|
|
gbm_device_destroy(m_device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:48:55 +00:00
|
|
|
void EglGbmBackend::cleanupSurfaces()
|
|
|
|
{
|
|
|
|
for (auto it = m_outputs.constBegin(); it != m_outputs.constEnd(); ++it) {
|
2015-04-24 08:05:38 +00:00
|
|
|
cleanupOutput(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::cleanupOutput(const Output &o)
|
|
|
|
{
|
|
|
|
// TODO: cleanup front buffer?
|
|
|
|
if (o.eglSurface != EGL_NO_SURFACE) {
|
|
|
|
eglDestroySurface(eglDisplay(), o.eglSurface);
|
|
|
|
}
|
|
|
|
if (o.gbmSurface) {
|
|
|
|
gbm_surface_destroy(o.gbmSurface);
|
2015-04-17 13:48:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 08:44:07 +00:00
|
|
|
bool EglGbmBackend::initializeEgl()
|
|
|
|
{
|
|
|
|
initClientExtensions();
|
|
|
|
EGLDisplay display = EGL_NO_DISPLAY;
|
|
|
|
|
|
|
|
// Use eglGetPlatformDisplayEXT() to get the display pointer
|
|
|
|
// if the implementation supports it.
|
|
|
|
if (!hasClientExtension(QByteArrayLiteral("EGL_EXT_platform_base")) ||
|
|
|
|
!hasClientExtension(QByteArrayLiteral("EGL_MESA_platform_gbm"))) {
|
|
|
|
setFailed("EGL_EXT_platform_base and/or EGL_MESA_platform_gbm missing");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_device = gbm_create_device(m_backend->fd());
|
|
|
|
if (!m_device) {
|
|
|
|
setFailed("Could not create gbm device");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
display = eglGetPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA, m_device, nullptr);
|
|
|
|
|
|
|
|
if (display == EGL_NO_DISPLAY)
|
|
|
|
return false;
|
|
|
|
setEglDisplay(display);
|
|
|
|
return initEglAPI();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::init()
|
|
|
|
{
|
|
|
|
if (!initRenderingContext()) {
|
|
|
|
setFailed("Could not initialize rendering context");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
initKWinGL();
|
|
|
|
initBufferAge();
|
|
|
|
initWayland();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EglGbmBackend::initRenderingContext()
|
|
|
|
{
|
|
|
|
initBufferConfigs();
|
|
|
|
|
2015-11-13 07:29:49 +00:00
|
|
|
if (!createContext()) {
|
|
|
|
return false;
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 13:48:55 +00:00
|
|
|
const auto outputs = m_backend->outputs();
|
|
|
|
for (DrmOutput *drmOutput: outputs) {
|
2015-04-24 08:05:38 +00:00
|
|
|
createOutput(drmOutput);
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
2015-04-17 13:48:55 +00:00
|
|
|
if (m_outputs.isEmpty()) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_DRM) << "Create Window Surfaces failed";
|
2015-04-10 08:44:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-17 13:48:55 +00:00
|
|
|
// set our first surface as the one for the abstract backend, just to make it happy
|
|
|
|
setSurface(m_outputs.first().eglSurface);
|
2015-04-10 08:44:07 +00:00
|
|
|
|
2015-04-17 13:48:55 +00:00
|
|
|
return makeContextCurrent(m_outputs.first());
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-24 08:05:38 +00:00
|
|
|
void EglGbmBackend::createOutput(DrmOutput *drmOutput)
|
|
|
|
{
|
|
|
|
Output o;
|
|
|
|
o.output = drmOutput;
|
|
|
|
o.gbmSurface = gbm_surface_create(m_device, drmOutput->size().width(), drmOutput->size().height(),
|
|
|
|
GBM_FORMAT_XRGB8888, GBM_BO_USE_SCANOUT | GBM_BO_USE_RENDERING);
|
|
|
|
if (!o.gbmSurface) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_DRM) << "Create gbm surface failed";
|
2015-04-24 08:05:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
o.eglSurface = eglCreatePlatformWindowSurfaceEXT(eglDisplay(), config(), (void *)o.gbmSurface, nullptr);
|
|
|
|
if (o.eglSurface == EGL_NO_SURFACE) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_DRM) << "Create Window Surface failed";
|
2015-04-24 08:05:38 +00:00
|
|
|
gbm_surface_destroy(o.gbmSurface);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_outputs << o;
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:48:55 +00:00
|
|
|
bool EglGbmBackend::makeContextCurrent(const Output &output)
|
2015-04-10 08:44:07 +00:00
|
|
|
{
|
2015-04-17 13:48:55 +00:00
|
|
|
const EGLSurface surface = output.eglSurface;
|
|
|
|
if (surface == EGL_NO_SURFACE) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (eglMakeCurrent(eglDisplay(), surface, surface, context()) == EGL_FALSE) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_DRM) << "Make Context Current failed";
|
2015-04-10 08:44:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EGLint error = eglGetError();
|
|
|
|
if (error != EGL_SUCCESS) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCWarning(KWIN_DRM) << "Error occurred while creating context " << error;
|
2015-04-10 08:44:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-04-17 13:48:55 +00:00
|
|
|
// TODO: ensure the viewport is set correctly each time
|
|
|
|
const QSize &overall = screens()->size();
|
|
|
|
const QRect &v = output.output->geometry();
|
|
|
|
// TODO: are the values correct?
|
|
|
|
glViewport(-v.x(), v.height() - overall.height() - v.y(), overall.width(), overall.height());
|
2015-04-10 08:44:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-04-10 08:44:07 +00:00
|
|
|
EGL_CONFIG_CAVEAT, EGL_NONE,
|
|
|
|
EGL_NONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint count;
|
|
|
|
EGLConfig configs[1024];
|
|
|
|
if (eglChooseConfig(eglDisplay(), config_attribs, configs, 1, &count) == EGL_FALSE) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_DRM) << "choose config failed";
|
2015-04-10 08:44:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (count != 1) {
|
2015-05-05 15:27:03 +00:00
|
|
|
qCCritical(KWIN_DRM) << "choose config did not return a config" << count;
|
2015-04-10 08:44:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
setConfig(configs[0]);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EglGbmBackend::present()
|
|
|
|
{
|
2015-04-17 13:48:55 +00:00
|
|
|
for (auto &o: m_outputs) {
|
|
|
|
makeContextCurrent(o);
|
2015-04-23 07:55:49 +00:00
|
|
|
presentOnOutput(o);
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 07:55:49 +00:00
|
|
|
void EglGbmBackend::presentOnOutput(EglGbmBackend::Output &o)
|
|
|
|
{
|
|
|
|
eglSwapBuffers(eglDisplay(), o.eglSurface);
|
|
|
|
auto oldBuffer = o.buffer;
|
|
|
|
o.buffer = m_backend->createBuffer(o.gbmSurface);
|
|
|
|
m_backend->present(o.buffer, o.output);
|
|
|
|
delete oldBuffer;
|
|
|
|
if (supportsBufferAge()) {
|
|
|
|
eglQuerySurface(eglDisplay(), o.eglSurface, EGL_BUFFER_AGE_EXT, &o.bufferAge);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-04-10 08:44:07 +00:00
|
|
|
void EglGbmBackend::screenGeometryChanged(const QSize &size)
|
|
|
|
{
|
|
|
|
Q_UNUSED(size)
|
|
|
|
// TODO, create new buffer?
|
|
|
|
}
|
|
|
|
|
|
|
|
SceneOpenGL::TexturePrivate *EglGbmBackend::createBackendTexture(SceneOpenGL::Texture *texture)
|
|
|
|
{
|
|
|
|
return new EglGbmTexture(texture, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion EglGbmBackend::prepareRenderingFrame()
|
|
|
|
{
|
2015-04-23 07:55:49 +00:00
|
|
|
startRenderTimer();
|
|
|
|
return QRegion();
|
|
|
|
}
|
|
|
|
|
|
|
|
QRegion EglGbmBackend::prepareRenderingForScreen(int screenId)
|
|
|
|
{
|
|
|
|
const Output &o = m_outputs.at(screenId);
|
|
|
|
makeContextCurrent(o);
|
2015-04-17 13:48:55 +00:00
|
|
|
if (supportsBufferAge()) {
|
2015-04-23 07:55:49 +00:00
|
|
|
QRegion region;
|
|
|
|
|
|
|
|
// Note: An age of zero means the buffer contents are undefined
|
|
|
|
if (o.bufferAge > 0 && o.bufferAge <= o.damageHistory.count()) {
|
|
|
|
for (int i = 0; i < o.bufferAge - 1; i++)
|
|
|
|
region |= o.damageHistory[i];
|
|
|
|
} else {
|
|
|
|
region = o.output->geometry();
|
2015-04-17 13:48:55 +00:00
|
|
|
}
|
2015-04-23 07:55:49 +00:00
|
|
|
|
|
|
|
return region;
|
2015-04-17 13:48:55 +00:00
|
|
|
}
|
2015-04-23 07:55:49 +00:00
|
|
|
return QRegion();
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 07:55:49 +00:00
|
|
|
void EglGbmBackend::endRenderingFrame(const QRegion &renderedRegion, const QRegion &damagedRegion)
|
2015-04-17 13:48:55 +00:00
|
|
|
{
|
2015-04-23 07:55:49 +00:00
|
|
|
Q_UNUSED(renderedRegion)
|
|
|
|
Q_UNUSED(damagedRegion)
|
2015-04-17 13:48:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-23 07:55:49 +00:00
|
|
|
void EglGbmBackend::endRenderingFrameForScreen(int screenId, const QRegion &renderedRegion, const QRegion &damagedRegion)
|
2015-04-10 08:44:07 +00:00
|
|
|
{
|
2015-04-23 07:55:49 +00:00
|
|
|
Output &o = m_outputs[screenId];
|
|
|
|
if (damagedRegion.intersected(o.output->geometry()).isEmpty() && screenId == 0) {
|
2015-04-10 08:44:07 +00:00
|
|
|
|
|
|
|
// If the damaged region of a window is fully occluded, the only
|
|
|
|
// rendering done, if any, will have been to repair a reused back
|
|
|
|
// buffer, making it identical to the front buffer.
|
|
|
|
//
|
|
|
|
// In this case we won't post the back buffer. Instead we'll just
|
|
|
|
// set the buffer age to 1, so the repaired regions won't be
|
|
|
|
// rendered again in the next frame.
|
2015-04-23 07:55:49 +00:00
|
|
|
if (!renderedRegion.intersected(o.output->geometry()).isEmpty())
|
2015-04-10 08:44:07 +00:00
|
|
|
glFlush();
|
|
|
|
|
2015-04-17 13:48:55 +00:00
|
|
|
for (auto &o: m_outputs) {
|
|
|
|
o.bufferAge = 1;
|
|
|
|
}
|
2015-04-10 08:44:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-04-23 07:55:49 +00:00
|
|
|
presentOnOutput(o);
|
2015-04-10 08:44:07 +00:00
|
|
|
|
|
|
|
// Save the damaged region to history
|
2015-04-23 07:55:49 +00:00
|
|
|
// Note: damage history is only collected for the first screen. For any other screen full repaints
|
|
|
|
// are triggered. This is due to a limitation in Scene::paintGenericScreen which resets the Toplevel's
|
|
|
|
// repaint. So multiple calls to Scene::paintScreen as it's done in multi-output rendering only
|
|
|
|
// have correct damage information for the first screen. If we try to track damage nevertheless,
|
|
|
|
// it creates artifacts. So for the time being we work around the problem by only supporting buffer
|
|
|
|
// age on the first output. To properly support buffer age on all outputs the rendering needs to
|
|
|
|
// be refactored in general.
|
|
|
|
if (supportsBufferAge() && screenId == 0) {
|
|
|
|
if (o.damageHistory.count() > 10) {
|
|
|
|
o.damageHistory.removeLast();
|
|
|
|
}
|
|
|
|
|
|
|
|
o.damageHistory.prepend(damagedRegion.intersected(o.output->geometry()));
|
|
|
|
}
|
2015-04-10 08:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool EglGbmBackend::usesOverlayWindow() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-17 13:48:55 +00:00
|
|
|
bool EglGbmBackend::perScreenRendering() const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-10 08:44:07 +00:00
|
|
|
/************************************************
|
|
|
|
* EglTexture
|
|
|
|
************************************************/
|
|
|
|
|
|
|
|
EglGbmTexture::EglGbmTexture(KWin::SceneOpenGL::Texture *texture, EglGbmBackend *backend)
|
|
|
|
: AbstractEglTexture(texture, backend)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EglGbmTexture::~EglGbmTexture() = default;
|
|
|
|
|
|
|
|
} // namespace
|