2010-11-21 13:01:39 +00:00
|
|
|
/********************************************************************
|
|
|
|
KWin - the KDE window manager
|
|
|
|
This file is part of the KDE project.
|
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
Copyright (C) 2010, 2012 Martin Gräßlin <mgraesslin@kde.org>
|
2010-11-21 13:01:39 +00:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*********************************************************************/
|
2012-08-26 15:14:23 +00:00
|
|
|
#include "eglonxbackend.h"
|
|
|
|
// kwin
|
|
|
|
#include "options.h"
|
|
|
|
#include "overlaywindow.h"
|
2013-03-11 10:01:38 +00:00
|
|
|
#include "xcbutils.h"
|
2012-08-26 15:14:23 +00:00
|
|
|
// kwin libs
|
|
|
|
#include <kwinglplatform.h>
|
|
|
|
|
|
|
|
namespace KWin
|
|
|
|
{
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
EglOnXBackend::EglOnXBackend()
|
|
|
|
: OpenGLBackend()
|
|
|
|
, surfaceHasSubPost(0)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
// Egl is always direct rendering
|
|
|
|
setIsDirectRendering(true);
|
|
|
|
}
|
2010-11-28 15:21:12 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
EglOnXBackend::~EglOnXBackend()
|
|
|
|
{
|
|
|
|
cleanupGL();
|
|
|
|
eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
|
|
|
|
eglDestroyContext(dpy, ctx);
|
|
|
|
eglDestroySurface(dpy, surface);
|
|
|
|
eglTerminate(dpy);
|
|
|
|
eglReleaseThread();
|
|
|
|
if (overlayWindow()->window()) {
|
|
|
|
overlayWindow()->destroy();
|
|
|
|
}
|
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
void EglOnXBackend::init()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-08-26 15:14:23 +00:00
|
|
|
if (!initRenderingContext()) {
|
|
|
|
setFailed("Could not initialize rendering context");
|
2010-11-28 15:21:12 +00:00
|
|
|
return;
|
2012-08-26 15:14:23 +00:00
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2010-12-05 10:55:19 +00:00
|
|
|
initEGL();
|
2011-10-16 06:53:11 +00:00
|
|
|
if (!hasGLExtension("EGL_KHR_image") &&
|
|
|
|
(!hasGLExtension("EGL_KHR_image_base") ||
|
|
|
|
!hasGLExtension("EGL_KHR_image_pixmap"))) {
|
2012-08-26 15:14:23 +00:00
|
|
|
setFailed("Required support for binding pixmaps to EGLImages not found, disabling compositing");
|
2010-12-05 10:55:19 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-04-29 06:18:18 +00:00
|
|
|
GLPlatform *glPlatform = GLPlatform::instance();
|
2012-09-29 11:19:35 +00:00
|
|
|
glPlatform->detect(EglPlatformInterface);
|
2012-04-29 06:18:18 +00:00
|
|
|
glPlatform->printResults();
|
2012-09-29 11:19:35 +00:00
|
|
|
initGL(EglPlatformInterface);
|
2010-12-05 10:55:19 +00:00
|
|
|
if (!hasGLExtension("GL_OES_EGL_image")) {
|
2012-08-26 15:14:23 +00:00
|
|
|
setFailed("Required extension GL_OES_EGL_image not found, disabling compositing");
|
2010-11-21 13:01:39 +00:00
|
|
|
return;
|
2010-12-04 11:31:18 +00:00
|
|
|
}
|
2012-03-29 20:11:28 +00:00
|
|
|
|
2013-03-22 11:18:51 +00:00
|
|
|
// check for EGL_NV_post_sub_buffer and whether it can be used on the surface
|
|
|
|
if (eglPostSubBufferNV) {
|
|
|
|
if (eglQuerySurface(dpy, surface, EGL_POST_SUB_BUFFER_SUPPORTED_NV, &surfaceHasSubPost) == EGL_FALSE) {
|
|
|
|
EGLint error = eglGetError();
|
|
|
|
if (error != EGL_SUCCESS && error != EGL_BAD_ATTRIBUTE) {
|
|
|
|
setFailed("query surface failed");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
surfaceHasSubPost = EGL_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (surfaceHasSubPost) {
|
|
|
|
kDebug(1212) << "EGL implementation and surface support eglPostSubBufferNV, let's use it";
|
|
|
|
|
2013-04-12 12:05:19 +00:00
|
|
|
if (options->glPreferBufferSwap() != Options::NoSwapEncourage) {
|
|
|
|
// check if swap interval 1 is supported
|
|
|
|
EGLint val;
|
|
|
|
eglGetConfigAttrib(dpy, config, EGL_MAX_SWAP_INTERVAL, &val);
|
|
|
|
if (val >= 1) {
|
|
|
|
if (eglSwapInterval(dpy, 1)) {
|
|
|
|
kDebug(1212) << "Enabled v-sync";
|
|
|
|
setHasWaitSync(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kWarning(1212) << "Cannot enable v-sync as max. swap interval is" << val;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// disable v-sync
|
|
|
|
eglSwapInterval(dpy, 0);
|
2013-03-22 11:18:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* In the GLX backend, we fall back to using glCopyPixels if we have no extension providing support for partial screen updates.
|
|
|
|
* However, that does not work in EGL - glCopyPixels with glDrawBuffer(GL_FRONT); does nothing.
|
|
|
|
* Hence we need EGL to preserve the backbuffer for us, so that we can draw the partial updates on it and call
|
|
|
|
* eglSwapBuffers() for each frame. eglSwapBuffers() then does the copy (no page flip possible in this mode),
|
|
|
|
* which means it is slow and not synced to the v-blank. */
|
|
|
|
kWarning(1212) << "eglPostSubBufferNV not supported, have to enable buffer preservation - which breaks v-sync and performance";
|
|
|
|
eglSurfaceAttrib(dpy, surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_PRESERVED);
|
2012-03-29 20:11:28 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
bool EglOnXBackend::initRenderingContext()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
|
|
|
dpy = eglGetDisplay(display());
|
|
|
|
if (dpy == EGL_NO_DISPLAY)
|
2010-11-28 15:21:12 +00:00
|
|
|
return false;
|
|
|
|
EGLint major, minor;
|
2011-01-30 14:34:42 +00:00
|
|
|
if (eglInitialize(dpy, &major, &minor) == EGL_FALSE)
|
2010-11-28 15:21:12 +00:00
|
|
|
return false;
|
2012-09-29 11:19:35 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
2011-01-30 14:34:42 +00:00
|
|
|
eglBindAPI(EGL_OPENGL_ES_API);
|
2012-09-29 11:19:35 +00:00
|
|
|
#else
|
|
|
|
if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE) {
|
|
|
|
kError(1212) << "bind OpenGL API failed";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
2010-11-28 15:21:12 +00:00
|
|
|
initBufferConfigs();
|
2012-08-26 15:14:23 +00:00
|
|
|
if (!overlayWindow()->create()) {
|
2011-01-30 14:34:42 +00:00
|
|
|
kError(1212) << "Could not get overlay window";
|
2010-11-28 15:21:12 +00:00
|
|
|
return false;
|
2011-04-23 14:27:23 +00:00
|
|
|
} else {
|
2012-08-26 15:14:23 +00:00
|
|
|
overlayWindow()->setup(None);
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2012-08-26 15:14:23 +00:00
|
|
|
surface = eglCreateWindowSurface(dpy, config, overlayWindow()->window(), 0);
|
2010-11-28 15:21:12 +00:00
|
|
|
|
|
|
|
const EGLint context_attribs[] = {
|
2012-09-29 11:19:35 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
2010-11-28 15:21:12 +00:00
|
|
|
EGL_CONTEXT_CLIENT_VERSION, 2,
|
2012-09-29 11:19:35 +00:00
|
|
|
#endif
|
2010-11-28 15:21:12 +00:00
|
|
|
EGL_NONE
|
|
|
|
};
|
|
|
|
|
2011-01-30 14:34:42 +00:00
|
|
|
ctx = eglCreateContext(dpy, config, EGL_NO_CONTEXT, context_attribs);
|
2012-09-29 11:19:35 +00:00
|
|
|
if (ctx == EGL_NO_CONTEXT) {
|
|
|
|
kError(1212) << "Create Context failed";
|
2010-11-28 15:21:12 +00:00
|
|
|
return false;
|
2012-09-29 11:19:35 +00:00
|
|
|
}
|
|
|
|
if (eglMakeCurrent(dpy, surface, surface, ctx) == EGL_FALSE) {
|
|
|
|
kError(1212) << "Make Context Current failed";
|
2010-11-28 15:21:12 +00:00
|
|
|
return false;
|
2012-09-29 11:19:35 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
kDebug(1212) << "EGL version: " << major << "." << minor;
|
2010-11-28 15:21:12 +00:00
|
|
|
EGLint error = eglGetError();
|
2011-01-30 14:34:42 +00:00
|
|
|
if (error != EGL_SUCCESS) {
|
|
|
|
kWarning(1212) << "Error occurred while creating context " << error;
|
2010-11-28 15:21:12 +00:00
|
|
|
return false;
|
2010-11-21 13:01:39 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
bool EglOnXBackend::initBufferConfigs()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2010-11-28 15:21:12 +00:00
|
|
|
const EGLint config_attribs[] = {
|
2011-12-01 09:00:38 +00:00
|
|
|
EGL_SURFACE_TYPE, EGL_WINDOW_BIT|EGL_SWAP_BEHAVIOR_PRESERVED_BIT,
|
2010-11-28 15:21:12 +00:00
|
|
|
EGL_RED_SIZE, 1,
|
|
|
|
EGL_GREEN_SIZE, 1,
|
|
|
|
EGL_BLUE_SIZE, 1,
|
|
|
|
EGL_ALPHA_SIZE, 0,
|
2012-09-29 11:19:35 +00:00
|
|
|
#ifdef KWIN_HAVE_OPENGLES
|
2010-11-28 15:21:12 +00:00
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
2012-09-29 11:19:35 +00:00
|
|
|
#else
|
|
|
|
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
|
|
|
|
#endif
|
2010-11-28 15:21:12 +00:00
|
|
|
EGL_CONFIG_CAVEAT, EGL_NONE,
|
|
|
|
EGL_NONE,
|
|
|
|
};
|
|
|
|
|
|
|
|
EGLint count;
|
|
|
|
EGLConfig configs[1024];
|
2012-09-29 11:19:35 +00:00
|
|
|
if (eglChooseConfig(dpy, config_attribs, configs, 1024, &count) == EGL_FALSE) {
|
|
|
|
kError(1212) << "choose config failed";
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-28 15:21:12 +00:00
|
|
|
|
2013-03-11 10:01:38 +00:00
|
|
|
Xcb::WindowAttributes attribs(rootWindow());
|
|
|
|
if (!attribs) {
|
|
|
|
kError(1212) << "Failed to get window attributes of root window";
|
|
|
|
return false;
|
|
|
|
}
|
2010-11-28 15:21:12 +00:00
|
|
|
|
|
|
|
config = configs[0];
|
2011-01-30 14:34:42 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
2010-11-28 15:21:12 +00:00
|
|
|
EGLint val;
|
2012-09-29 11:19:35 +00:00
|
|
|
if (eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &val) == EGL_FALSE) {
|
|
|
|
kError(1212) << "egl get config attrib failed";
|
|
|
|
}
|
2013-03-11 10:01:38 +00:00
|
|
|
if (uint32_t(val) == attribs->visual) {
|
2010-11-28 15:21:12 +00:00
|
|
|
config = configs[i];
|
|
|
|
break;
|
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-10-07 11:43:31 +00:00
|
|
|
void EglOnXBackend::present()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2013-03-10 21:18:30 +00:00
|
|
|
const QRegion displayRegion(0, 0, displayWidth(), displayHeight());
|
|
|
|
const bool fullRepaint = (lastDamage() == displayRegion);
|
2013-03-22 11:18:51 +00:00
|
|
|
|
|
|
|
if (fullRepaint || !surfaceHasSubPost) {
|
|
|
|
// the entire screen changed, or we cannot do partial updates (which implies we enabled surface preservation)
|
2013-02-18 22:17:46 +00:00
|
|
|
eglSwapBuffers(dpy, surface);
|
|
|
|
} else {
|
2013-03-22 11:18:51 +00:00
|
|
|
// a part of the screen changed, and we can use eglPostSubBufferNV to copy the updated area
|
|
|
|
foreach (const QRect & r, lastDamage().rects()) {
|
|
|
|
eglPostSubBufferNV(dpy, surface, r.left(), displayHeight() - r.bottom() - 1, r.width(), r.height());
|
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2011-10-29 15:09:26 +00:00
|
|
|
|
2013-03-22 11:18:51 +00:00
|
|
|
setLastDamage(QRegion());
|
2012-08-26 15:14:23 +00:00
|
|
|
eglWaitGL();
|
2013-03-11 10:01:38 +00:00
|
|
|
xcb_flush(connection());
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
void EglOnXBackend::screenGeometryChanged(const QSize &size)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-08-26 15:14:23 +00:00
|
|
|
Q_UNUSED(size)
|
|
|
|
// no backend specific code needed
|
|
|
|
// TODO: base implementation in OpenGLBackend
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
SceneOpenGL::TexturePrivate *EglOnXBackend::createBackendTexture(SceneOpenGL::Texture *texture)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-08-26 15:14:23 +00:00
|
|
|
return new EglTexture(texture, this);
|
|
|
|
}
|
2011-10-16 08:09:16 +00:00
|
|
|
|
2012-09-29 11:19:35 +00:00
|
|
|
void EglOnXBackend::prepareRenderingFrame()
|
2012-08-26 15:14:23 +00:00
|
|
|
{
|
|
|
|
if (!lastDamage().isEmpty())
|
2012-10-07 11:43:31 +00:00
|
|
|
present();
|
2013-03-22 11:18:51 +00:00
|
|
|
eglWaitNative(EGL_CORE_NATIVE_ENGINE);
|
2012-08-26 15:14:23 +00:00
|
|
|
startRenderTimer();
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2013-03-10 21:18:30 +00:00
|
|
|
void EglOnXBackend::endRenderingFrame(const QRegion &damage)
|
2011-11-26 15:15:46 +00:00
|
|
|
{
|
2012-08-26 15:14:23 +00:00
|
|
|
setLastDamage(damage);
|
|
|
|
glFlush();
|
|
|
|
|
|
|
|
if (overlayWindow()->window()) // show the window only after the first pass,
|
|
|
|
overlayWindow()->show(); // since that pass may take long
|
2011-11-26 15:15:46 +00:00
|
|
|
}
|
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
/************************************************
|
|
|
|
* EglTexture
|
|
|
|
************************************************/
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
EglTexture::EglTexture(KWin::SceneOpenGL::Texture *texture, KWin::EglOnXBackend *backend)
|
|
|
|
: SceneOpenGL::TexturePrivate()
|
|
|
|
, q(texture)
|
|
|
|
, m_backend(backend)
|
|
|
|
, m_image(EGL_NO_IMAGE_KHR)
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-07-18 15:55:39 +00:00
|
|
|
m_target = GL_TEXTURE_2D;
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
EglTexture::~EglTexture()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2011-12-01 09:00:38 +00:00
|
|
|
if (m_image != EGL_NO_IMAGE_KHR) {
|
2012-08-26 15:14:23 +00:00
|
|
|
eglDestroyImageKHR(m_backend->dpy, m_image);
|
2011-12-01 09:00:38 +00:00
|
|
|
}
|
2011-01-30 14:34:42 +00:00
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
OpenGLBackend *EglTexture::backend()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-08-26 15:14:23 +00:00
|
|
|
return m_backend;
|
2011-07-18 15:55:39 +00:00
|
|
|
}
|
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
void EglTexture::findTarget()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-08-26 15:14:23 +00:00
|
|
|
if (m_target != GL_TEXTURE_2D) {
|
|
|
|
m_target = GL_TEXTURE_2D;
|
|
|
|
}
|
|
|
|
}
|
2011-07-18 15:55:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
bool EglTexture::loadTexture(const Pixmap &pix, const QSize &size, int depth)
|
|
|
|
{
|
2010-12-05 08:17:38 +00:00
|
|
|
Q_UNUSED(depth)
|
2011-03-10 09:50:42 +00:00
|
|
|
if (pix == None)
|
|
|
|
return false;
|
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
glGenTextures(1, &m_texture);
|
|
|
|
q->setWrapMode(GL_CLAMP_TO_EDGE);
|
|
|
|
q->setFilter(GL_LINEAR);
|
|
|
|
q->bind();
|
2011-07-18 15:55:39 +00:00
|
|
|
const EGLint attribs[] = {
|
|
|
|
EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
|
|
|
|
EGL_NONE
|
|
|
|
};
|
2012-08-26 15:14:23 +00:00
|
|
|
m_image = eglCreateImageKHR(m_backend->dpy, EGL_NO_CONTEXT, EGL_NATIVE_PIXMAP_KHR,
|
2011-07-18 15:55:39 +00:00
|
|
|
(EGLClientBuffer)pix, attribs);
|
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
if (EGL_NO_IMAGE_KHR == m_image) {
|
2011-07-18 15:55:39 +00:00
|
|
|
kDebug(1212) << "failed to create egl image";
|
2012-08-26 15:14:23 +00:00
|
|
|
q->unbind();
|
|
|
|
q->discard();
|
2011-07-18 15:55:39 +00:00
|
|
|
return false;
|
2010-11-21 13:01:39 +00:00
|
|
|
}
|
2012-08-26 15:14:23 +00:00
|
|
|
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)m_image);
|
|
|
|
q->unbind();
|
2011-07-18 15:55:39 +00:00
|
|
|
checkGLError("load texture");
|
2012-08-26 15:14:23 +00:00
|
|
|
q->setYInverted(true);
|
|
|
|
m_size = size;
|
2011-01-30 14:34:42 +00:00
|
|
|
return true;
|
|
|
|
}
|
2010-11-21 13:01:39 +00:00
|
|
|
|
2012-08-26 15:14:23 +00:00
|
|
|
void KWin::EglTexture::onDamage()
|
2011-01-30 14:34:42 +00:00
|
|
|
{
|
2012-02-20 09:25:13 +00:00
|
|
|
if (options->isGlStrictBinding()) {
|
2011-12-01 09:00:38 +00:00
|
|
|
// This is just implemented to be consistent with
|
|
|
|
// the example in mesa/demos/src/egl/opengles1/texture_from_pixmap.c
|
|
|
|
eglWaitNative(EGL_CORE_NATIVE_ENGINE);
|
|
|
|
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES) m_image);
|
|
|
|
}
|
2012-01-07 11:12:29 +00:00
|
|
|
GLTexturePrivate::onDamage();
|
|
|
|
}
|
2012-08-26 15:14:23 +00:00
|
|
|
|
|
|
|
} // namespace
|