kwin/scene_opengl_egl.cpp

285 lines
7.6 KiB
C++
Raw Normal View History

2010-11-21 16:01:39 +03:00
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2010 Martin Gräßlin <kde@martin-graesslin.com>
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/>.
*********************************************************************/
// This file is included in scene_opengl.cpp
//#include "scene_opengl.h"
#include <QX11Info>
EGLDisplay dpy;
EGLConfig config;
EGLSurface surface;
EGLContext ctx;
2010-11-21 16:01:39 +03:00
2011-01-30 17:34:42 +03:00
SceneOpenGL::SceneOpenGL(Workspace* ws)
: Scene(ws)
, init_ok(false)
, selfCheckDone(true)
{
if (!initRenderingContext())
return;
2010-11-21 16:01:39 +03:00
initEGL();
if (!hasGLExtension("EGL_KHR_image_pixmap")) {
kError(1212) << "Required extension EGL_KHR_image_pixmap not found, disabling compositing";
return;
}
initGL();
if (!hasGLExtension("GL_OES_EGL_image")) {
kError(1212) << "Required extension GL_OES_EGL_image not found, disabling compositing";
return;
}
2011-01-30 17:34:42 +03:00
debug = qstrcmp(qgetenv("KWIN_GL_DEBUG"), "1") == 0;
2010-12-11 12:57:29 +03:00
if (!ShaderManager::instance()->isValid()) {
2011-01-30 17:34:42 +03:00
kError(1212) << "Shaders not valid, ES compositing not possible";
2010-11-21 16:01:39 +03:00
return;
}
2010-12-11 12:57:29 +03:00
ShaderManager::instance()->pushShader(ShaderManager::SimpleShader);
2010-11-21 16:01:39 +03:00
2011-01-30 17:34:42 +03:00
if (checkGLError("Init")) {
kError(1212) << "OpenGL compositing setup failed";
2010-11-21 16:01:39 +03:00
return; // error
}
2011-01-30 17:34:42 +03:00
init_ok = true;
}
2010-11-21 16:01:39 +03:00
SceneOpenGL::~SceneOpenGL()
2011-01-30 17:34:42 +03:00
{
if (!init_ok) {
// TODO this probably needs to clean up whatever has been created until the failure
wspace->destroyOverlay();
return;
}
2011-01-30 17:34:42 +03:00
foreach (Window * w, windows)
delete w;
// do cleanup after initBuffer()
2011-01-30 17:34:42 +03:00
eglMakeCurrent(dpy, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
eglDestroyContext(dpy, ctx);
eglDestroySurface(dpy, surface);
eglTerminate(dpy);
eglReleaseThread();
2010-11-21 16:01:39 +03:00
SceneOpenGL::EffectFrame::cleanup();
2011-01-30 17:34:42 +03:00
checkGLError("Cleanup");
if (wspace->overlayWindow()) {
wspace->destroyOverlay();
}
2011-01-30 17:34:42 +03:00
}
2010-11-21 16:01:39 +03:00
bool SceneOpenGL::initTfp()
2011-01-30 17:34:42 +03:00
{
2010-11-21 16:01:39 +03:00
return false;
2011-01-30 17:34:42 +03:00
}
2010-11-21 16:01:39 +03:00
bool SceneOpenGL::initRenderingContext()
2011-01-30 17:34:42 +03:00
{
dpy = eglGetDisplay(display());
if (dpy == EGL_NO_DISPLAY)
return false;
EGLint major, minor;
2011-01-30 17:34:42 +03:00
if (eglInitialize(dpy, &major, &minor) == EGL_FALSE)
return false;
2011-01-30 17:34:42 +03:00
eglBindAPI(EGL_OPENGL_ES_API);
initBufferConfigs();
2011-01-30 17:34:42 +03:00
if (!wspace->createOverlay()) {
kError(1212) << "Could not get overlay window";
return false;
} else {
wspace->setupOverlay(None);
2011-01-30 17:34:42 +03:00
}
surface = eglCreateWindowSurface(dpy, config, wspace->overlayWindow(), 0);
const EGLint context_attribs[] = {
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_NONE
};
2011-01-30 17:34:42 +03:00
ctx = eglCreateContext(dpy, config, EGL_NO_CONTEXT, context_attribs);
if (ctx == EGL_NO_CONTEXT)
return false;
2011-01-30 17:34:42 +03:00
if (eglMakeCurrent(dpy, surface, surface, ctx) == EGL_FALSE)
return false;
2011-01-30 17:34:42 +03:00
kDebug(1212) << "EGL version: " << major << "." << minor;
EGLint error = eglGetError();
2011-01-30 17:34:42 +03:00
if (error != EGL_SUCCESS) {
kWarning(1212) << "Error occurred while creating context " << error;
return false;
2010-11-21 16:01:39 +03:00
}
2011-01-30 17:34:42 +03:00
return true;
}
2010-11-21 16:01:39 +03:00
bool SceneOpenGL::initBuffer()
2011-01-30 17:34:42 +03:00
{
2010-11-21 16:01:39 +03:00
return false;
2011-01-30 17:34:42 +03:00
}
2010-11-21 16:01:39 +03:00
bool SceneOpenGL::initBufferConfigs()
2011-01-30 17:34:42 +03:00
{
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,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_CONFIG_CAVEAT, EGL_NONE,
EGL_NONE,
};
EGLint count;
EGLConfig configs[1024];
eglChooseConfig(dpy, config_attribs, configs, 1024, &count);
EGLint visualId = XVisualIDFromVisual((Visual*)QX11Info::appVisual());
config = configs[0];
2011-01-30 17:34:42 +03:00
for (int i = 0; i < count; i++) {
EGLint val;
eglGetConfigAttrib(dpy, configs[i], EGL_NATIVE_VISUAL_ID, &val);
2011-01-30 17:34:42 +03:00
if (visualId == val) {
config = configs[i];
break;
}
2010-11-21 16:01:39 +03:00
}
2011-01-30 17:34:42 +03:00
return true;
}
2010-11-21 16:01:39 +03:00
bool SceneOpenGL::initDrawableConfigs()
2011-01-30 17:34:42 +03:00
{
2010-11-21 16:01:39 +03:00
return false;
2011-01-30 17:34:42 +03:00
}
2010-11-21 16:01:39 +03:00
void SceneOpenGL::selfCheckSetup()
2011-01-30 17:34:42 +03:00
{
2010-11-21 16:01:39 +03:00
// not used in EGL
2011-01-30 17:34:42 +03:00
}
2010-11-21 16:01:39 +03:00
bool SceneOpenGL::selfCheckFinish()
2011-01-30 17:34:42 +03:00
{
2010-11-21 16:01:39 +03:00
// not used in EGL
return true;
2011-01-30 17:34:42 +03:00
}
2010-11-21 16:01:39 +03:00
// the entry function for painting
2011-01-30 17:34:42 +03:00
void SceneOpenGL::paint(QRegion damage, ToplevelList toplevels)
{
2010-11-21 16:01:39 +03:00
QTime t = QTime::currentTime();
2011-01-30 17:34:42 +03:00
foreach (Toplevel * c, toplevels) {
assert(windows.contains(c));
stacking_order.append(windows[ c ]);
}
grabXServer();
XSync(display(), false);
2010-11-21 16:01:39 +03:00
int mask = 0;
2011-01-30 17:34:42 +03:00
paintScreen(&mask, &damage); // call generic implementation
ungrabXServer(); // ungrab before flushBuffer(), it may wait for vsync
2011-01-30 17:34:42 +03:00
if (wspace->overlayWindow()) // show the window only after the first pass, since
wspace->showOverlay(); // that pass may take long
2010-11-21 16:01:39 +03:00
lastRenderTime = t.elapsed();
2011-01-30 17:34:42 +03:00
flushBuffer(mask, damage);
2010-11-21 16:01:39 +03:00
// do cleanup
stacking_order.clear();
2011-01-30 17:34:42 +03:00
checkGLError("PostPaint");
}
2010-11-21 16:01:39 +03:00
void SceneOpenGL::waitSync()
2011-01-30 17:34:42 +03:00
{
2010-11-21 16:01:39 +03:00
// not used in EGL
2011-01-30 17:34:42 +03:00
}
2010-11-21 16:01:39 +03:00
2011-01-30 17:34:42 +03:00
void SceneOpenGL::flushBuffer(int mask, QRegion damage)
{
Q_UNUSED(damage)
glFlush();
2011-01-30 17:34:42 +03:00
if (mask & PAINT_SCREEN_REGION) {
// TODO: implement me properly
2011-01-30 17:34:42 +03:00
eglSwapBuffers(dpy, surface);
} else {
eglSwapBuffers(dpy, surface);
}
eglWaitGL();
// TODO: remove for wayland
2011-01-30 17:34:42 +03:00
XFlush(display());
}
2010-11-21 16:01:39 +03:00
//****************************************
// SceneOpenGL::Texture
//****************************************
void SceneOpenGL::Texture::init()
2011-01-30 17:34:42 +03:00
{
findTarget();
2011-01-30 17:34:42 +03:00
}
2010-11-21 16:01:39 +03:00
void SceneOpenGL::Texture::release()
2011-01-30 17:34:42 +03:00
{
mTexture = None;
2011-01-30 17:34:42 +03:00
}
2010-11-21 16:01:39 +03:00
void SceneOpenGL::Texture::findTarget()
2011-01-30 17:34:42 +03:00
{
2010-11-21 16:01:39 +03:00
mTarget = GL_TEXTURE_2D;
2011-01-30 17:34:42 +03:00
}
2010-11-21 16:01:39 +03:00
2011-01-30 17:34:42 +03:00
bool SceneOpenGL::Texture::load(const Pixmap& pix, const QSize& size,
int depth, QRegion region)
{
2010-12-05 11:17:38 +03:00
Q_UNUSED(size)
Q_UNUSED(depth)
Q_UNUSED(region)
if (pix == None)
return false;
2011-01-30 17:34:42 +03:00
if (mTexture == None) {
createTexture();
bind();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
const EGLint attribs[] = {
EGL_IMAGE_PRESERVED_KHR, EGL_TRUE,
EGL_NONE
};
EGLImageKHR image = eglCreateImageKHR(dpy, EGL_NO_CONTEXT, EGL_NATIVE_PIXMAP_KHR,
2011-01-30 17:34:42 +03:00
(EGLClientBuffer)pix, attribs);
if (EGL_NO_IMAGE_KHR == image) {
kDebug(1212) << "failed to create egl image";
unbind();
return false;
}
glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
2011-01-30 17:34:42 +03:00
eglDestroyImageKHR(dpy, image);
unbind();
checkGLError("load texture");
2010-11-21 16:01:39 +03:00
}
2011-01-30 17:34:42 +03:00
return true;
}
2010-11-21 16:01:39 +03:00
void SceneOpenGL::Texture::bind()
2011-01-30 17:34:42 +03:00
{
2010-11-21 16:01:39 +03:00
GLTexture::bind();
2011-01-30 17:34:42 +03:00
}
2010-11-21 16:01:39 +03:00
void SceneOpenGL::Texture::unbind()
2011-01-30 17:34:42 +03:00
{
2010-11-21 16:01:39 +03:00
GLTexture::unbind();
2011-01-30 17:34:42 +03:00
}