kwin/lanczosfilter.cpp

431 lines
15 KiB
C++
Raw Normal View History

/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2010 by Fredrik Höglund <fredrik@kde.org>
Copyright (C) 2010 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 "lanczosfilter.h"
#include "client.h"
#include "deleted.h"
#include "effects.h"
#include "unmanaged.h"
#include "options.h"
#include "workspace.h"
2011-01-30 14:34:42 +00:00
#include <kwinglutils.h>
#include <kwinglplatform.h>
#include <kwineffects.h>
#include <KDE/KGlobalSettings>
#include <qmath.h>
#include <cmath>
namespace KWin
{
2011-01-30 14:34:42 +00:00
LanczosFilter::LanczosFilter(QObject* parent)
: QObject(parent)
, m_offscreenTex(0)
, m_offscreenTarget(0)
, m_inited(false)
, m_shader(0)
, m_uTexUnit(0)
, m_uOffsets(0)
, m_uKernel(0)
2011-01-30 14:34:42 +00:00
{
}
LanczosFilter::~LanczosFilter()
2011-01-30 14:34:42 +00:00
{
delete m_offscreenTarget;
delete m_offscreenTex;
2011-01-30 14:34:42 +00:00
}
void LanczosFilter::init()
2011-01-30 14:34:42 +00:00
{
if (m_inited)
return;
m_inited = true;
const bool force = (qstrcmp(qgetenv("KWIN_FORCE_LANCZOS"), "1") == 0);
if (force) {
kWarning(1212) << "Lanczos Filter forced on by environment variable";
}
if (!force && options->glSmoothScale() != 2)
return; // disabled by config
if (!GLRenderTarget::supported())
return;
GLPlatform *gl = GLPlatform::instance();
if (!force) {
// The lanczos filter is reported to be broken with the Intel driver prior SandyBridge
if (gl->driver() == Driver_Intel && gl->chipClass() < SandyBridge)
return;
// Broken on IvyBridge with Mesa 9.1 - BUG 313613
if (gl->driver() == Driver_Intel && gl->chipClass() == IvyBridge && gl->mesaVersion() >= kVersionNumber(9, 1) && gl->mesaVersion() < kVersionNumber(9, 2))
return;
// also radeon before R600 has trouble
if (gl->isRadeon() && gl->chipClass() < R600)
return;
}
m_shader.reset(ShaderManager::instance()->loadFragmentShader(ShaderManager::SimpleShader,
gl->glslVersion() >= kVersionNumber(1, 40) ?
":/resources/shaders/1.40/lanczos-fragment.glsl" :
":/resources/shaders/1.10/lanczos-fragment.glsl"));
if (m_shader->isValid()) {
ShaderBinder binder(m_shader.data());
m_uTexUnit = m_shader->uniformLocation("texUnit");
m_uKernel = m_shader->uniformLocation("kernel");
m_uOffsets = m_shader->uniformLocation("offsets");
} else {
kDebug(1212) << "Shader is not valid";
m_shader.reset();
}
2011-01-30 14:34:42 +00:00
}
void LanczosFilter::updateOffscreenSurfaces()
2011-01-30 14:34:42 +00:00
{
int w = displayWidth();
int h = displayHeight();
2011-01-30 14:34:42 +00:00
if (!GLTexture::NPOTTextureSupported()) {
w = nearestPowerOfTwo(w);
h = nearestPowerOfTwo(h);
}
if (!m_offscreenTex || m_offscreenTex->width() != w || m_offscreenTex->height() != h) {
if (m_offscreenTex) {
delete m_offscreenTex;
delete m_offscreenTarget;
}
2011-01-30 14:34:42 +00:00
m_offscreenTex = new GLTexture(w, h);
m_offscreenTex->setFilter(GL_LINEAR);
m_offscreenTex->setWrapMode(GL_CLAMP_TO_EDGE);
m_offscreenTarget = new GLRenderTarget(*m_offscreenTex);
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
static float sinc(float x)
{
return std::sin(x * M_PI) / (x * M_PI);
}
2011-01-30 14:34:42 +00:00
static float lanczos(float x, float a)
{
if (qFuzzyCompare(x + 1.0, 1.0))
return 1.0;
2011-01-30 14:34:42 +00:00
if (qAbs(x) >= a)
return 0.0;
2011-01-30 14:34:42 +00:00
return sinc(x) * sinc(x / a);
}
void LanczosFilter::createKernel(float delta, int *size)
2011-01-30 14:34:42 +00:00
{
const float a = 2.0;
// The two outermost samples always fall at points where the lanczos
// function returns 0, so we'll skip them.
2011-01-30 14:34:42 +00:00
const int sampleCount = qBound(3, qCeil(delta * a) * 2 + 1 - 2, 29);
const int center = sampleCount / 2;
const int kernelSize = center + 1;
const float factor = 1.0 / delta;
2011-01-30 14:34:42 +00:00
QVector<float> values(kernelSize);
float sum = 0;
2011-01-30 14:34:42 +00:00
for (int i = 0; i < kernelSize; i++) {
const float val = lanczos(i * factor, a);
sum += i > 0 ? val * 2 : val;
values[i] = val;
}
memset(m_kernel, 0, 16 * sizeof(QVector4D));
// Normalize the kernel
2011-01-30 14:34:42 +00:00
for (int i = 0; i < kernelSize; i++) {
const float val = values[i] / sum;
2011-01-30 14:34:42 +00:00
m_kernel[i] = QVector4D(val, val, val, val);
}
*size = kernelSize;
2011-01-30 14:34:42 +00:00
}
void LanczosFilter::createOffsets(int count, float width, Qt::Orientation direction)
2011-01-30 14:34:42 +00:00
{
memset(m_offsets, 0, 16 * sizeof(QVector2D));
2011-01-30 14:34:42 +00:00
for (int i = 0; i < count; i++) {
m_offsets[i] = (direction == Qt::Horizontal) ?
QVector2D(i / width, 0) : QVector2D(0, i / width);
}
2011-01-30 14:34:42 +00:00
}
2011-01-30 14:34:42 +00:00
void LanczosFilter::performPaint(EffectWindowImpl* w, int mask, QRegion region, WindowPaintData& data)
{
if ((data.xScale() < 0.9 || data.yScale() < 0.9) &&
2011-01-30 14:34:42 +00:00
KGlobalSettings::graphicEffectsLevel() & KGlobalSettings::SimpleAnimationEffects) {
if (!m_inited)
init();
2011-01-30 14:34:42 +00:00
const QRect screenRect = Workspace::self()->clientArea(ScreenArea, w->screen(), w->desktop());
// window geometry may not be bigger than screen geometry to fit into the FBO
2011-01-30 14:34:42 +00:00
if (m_shader && w->width() <= screenRect.width() && w->height() <= screenRect.height()) {
double left = 0;
double top = 0;
double right = w->width();
double bottom = w->height();
2011-01-30 14:34:42 +00:00
foreach (const WindowQuad & quad, data.quads) {
// we need this loop to include the decoration padding
left = qMin(left, quad.left());
top = qMin(top, quad.top());
right = qMax(right, quad.right());
bottom = qMax(bottom, quad.bottom());
2011-01-30 14:34:42 +00:00
}
double width = right - left;
double height = bottom - top;
2011-01-30 14:34:42 +00:00
if (width > screenRect.width() || height > screenRect.height()) {
// window with padding does not fit into the framebuffer
// so cut of the shadow
left = 0;
top = 0;
width = w->width();
height = w->height();
2011-01-30 14:34:42 +00:00
}
int tx = data.xTranslation() + w->x() + left * data.xScale();
int ty = data.yTranslation() + w->y() + top * data.yScale();
int tw = width * data.xScale();
int th = height * data.yScale();
const QRect textureRect(tx, ty, tw, th);
const bool hardwareClipping = !(QRegion(textureRect)-region).isEmpty();
int sw = width;
int sh = height;
2011-01-30 14:34:42 +00:00
GLTexture *cachedTexture = static_cast< GLTexture*>(w->data(LanczosCacheRole).value<void*>());
if (cachedTexture) {
if (cachedTexture->width() == tw && cachedTexture->height() == th) {
cachedTexture->bind();
if (hardwareClipping) {
glEnable(GL_SCISSOR_TEST);
}
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
const qreal rgb = data.brightness() * data.opacity();
const qreal a = data.opacity();
ShaderBinder binder(ShaderManager::SimpleShader);
GLShader *shader = binder.shader();
shader->setUniform(GLShader::Offset, QVector2D(0, 0));
shader->setUniform(GLShader::ModulationConstant, QVector4D(rgb, rgb, rgb, a));
shader->setUniform(GLShader::Saturation, data.saturation());
cachedTexture->render(region, textureRect, hardwareClipping);
glDisable(GL_BLEND);
if (hardwareClipping) {
glDisable(GL_SCISSOR_TEST);
}
cachedTexture->unbind();
2011-01-30 14:34:42 +00:00
m_timer.start(5000, this);
return;
2011-01-30 14:34:42 +00:00
} else {
// offscreen texture not matching - delete
delete cachedTexture;
cachedTexture = 0;
2011-01-30 14:34:42 +00:00
w->setData(LanczosCacheRole, QVariant());
}
2011-01-30 14:34:42 +00:00
}
WindowPaintData thumbData = data;
thumbData.setXScale(1.0);
thumbData.setYScale(1.0);
thumbData.setXTranslation(-w->x() - left);
thumbData.setYTranslation(-w->y() - top);
thumbData.setBrightness(1.0);
thumbData.setOpacity(1.0);
thumbData.setSaturation(1.0);
// Bind the offscreen FBO and draw the window on it unscaled
updateOffscreenSurfaces();
GLRenderTarget::pushRenderTarget(m_offscreenTarget);
glClearColor(0.0, 0.0, 0.0, 0.0);
2011-01-30 14:34:42 +00:00
glClear(GL_COLOR_BUFFER_BIT);
w->sceneWindow()->performPaint(mask, infiniteRegion(), thumbData);
// Create a scratch texture and copy the rendered window into it
2011-01-30 14:34:42 +00:00
GLTexture tex(sw, sh);
tex.setFilter(GL_LINEAR);
tex.setWrapMode(GL_CLAMP_TO_EDGE);
tex.bind();
2011-01-30 14:34:42 +00:00
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, m_offscreenTex->height() - sh, sw, sh);
// Set up the shader for horizontal scaling
float dx = sw / float(tw);
int kernelSize;
createKernel(dx, &kernelSize);
createOffsets(kernelSize, sw, Qt::Horizontal);
ShaderManager::instance()->pushShader(m_shader.data());
setUniforms();
// Draw the window back into the FBO, this time scaled horizontally
2011-01-30 14:34:42 +00:00
glClear(GL_COLOR_BUFFER_BIT);
QVector<float> verts;
QVector<float> texCoords;
verts.reserve(12);
texCoords.reserve(12);
texCoords << 1.0 << 0.0; verts << tw << 0.0; // Top right
texCoords << 0.0 << 0.0; verts << 0.0 << 0.0; // Top left
texCoords << 0.0 << 1.0; verts << 0.0 << sh; // Bottom left
texCoords << 0.0 << 1.0; verts << 0.0 << sh; // Bottom left
texCoords << 1.0 << 1.0; verts << tw << sh; // Bottom right
texCoords << 1.0 << 0.0; verts << tw << 0.0; // Top right
GLVertexBuffer *vbo = GLVertexBuffer::streamingBuffer();
vbo->reset();
vbo->setData(6, 2, verts.constData(), texCoords.constData());
vbo->render(GL_TRIANGLES);
// At this point we don't need the scratch texture anymore
tex.unbind();
tex.discard();
// create scratch texture for second rendering pass
2011-01-30 14:34:42 +00:00
GLTexture tex2(tw, sh);
tex2.setFilter(GL_LINEAR);
tex2.setWrapMode(GL_CLAMP_TO_EDGE);
tex2.bind();
2011-01-30 14:34:42 +00:00
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, m_offscreenTex->height() - sh, tw, sh);
// Set up the shader for vertical scaling
float dy = sh / float(th);
createKernel(dy, &kernelSize);
createOffsets(kernelSize, m_offscreenTex->height(), Qt::Vertical);
setUniforms();
// Now draw the horizontally scaled window in the FBO at the right
// coordinates on the screen, while scaling it vertically and blending it.
2011-01-30 14:34:42 +00:00
glClear(GL_COLOR_BUFFER_BIT);
verts.clear();
verts << tw << 0.0; // Top right
verts << 0.0 << 0.0; // Top left
verts << 0.0 << th; // Bottom left
verts << 0.0 << th; // Bottom left
verts << tw << th; // Bottom right
verts << tw << 0.0; // Top right
vbo->setData(6, 2, verts.constData(), texCoords.constData());
vbo->render(GL_TRIANGLES);
tex2.unbind();
tex2.discard();
ShaderManager::instance()->popShader();
// create cache texture
2011-01-30 14:34:42 +00:00
GLTexture *cache = new GLTexture(tw, th);
2011-01-30 14:34:42 +00:00
cache->setFilter(GL_LINEAR);
cache->setWrapMode(GL_CLAMP_TO_EDGE);
cache->bind();
2011-01-30 14:34:42 +00:00
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, m_offscreenTex->height() - th, tw, th);
GLRenderTarget::popRenderTarget();
if (hardwareClipping) {
glEnable(GL_SCISSOR_TEST);
}
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
const qreal rgb = data.brightness() * data.opacity();
const qreal a = data.opacity();
ShaderBinder binder(ShaderManager::SimpleShader);
GLShader *shader = binder.shader();
shader->setUniform(GLShader::Offset, QVector2D(0, 0));
shader->setUniform(GLShader::ModulationConstant, QVector4D(rgb, rgb, rgb, a));
shader->setUniform(GLShader::Saturation, data.saturation());
cache->render(region, textureRect, hardwareClipping);
glDisable(GL_BLEND);
if (hardwareClipping) {
glDisable(GL_SCISSOR_TEST);
}
cache->unbind();
2011-01-30 14:34:42 +00:00
w->setData(LanczosCacheRole, QVariant::fromValue(static_cast<void*>(cache)));
// Delete the offscreen surface after 5 seconds
2011-01-30 14:34:42 +00:00
m_timer.start(5000, this);
return;
2011-01-30 14:34:42 +00:00
}
} // if ( effects->compositingType() == KWin::OpenGLCompositing )
w->sceneWindow()->performPaint(mask, region, data);
} // End of function
2011-01-30 14:34:42 +00:00
void LanczosFilter::timerEvent(QTimerEvent *event)
{
if (event->timerId() == m_timer.timerId()) {
m_timer.stop();
delete m_offscreenTarget;
delete m_offscreenTex;
m_offscreenTarget = 0;
m_offscreenTex = 0;
foreach (Client *c, Workspace::self()->clientList()) {
discardCacheTexture(c->effectWindow());
}
foreach (Client *c, Workspace::self()->desktopList()) {
discardCacheTexture(c->effectWindow());
}
foreach (Unmanaged *u, Workspace::self()->unmanagedList()) {
discardCacheTexture(u->effectWindow());
}
foreach (Deleted *d, Workspace::self()->deletedList()) {
discardCacheTexture(d->effectWindow());
}
}
2011-01-30 14:34:42 +00:00
}
void LanczosFilter::discardCacheTexture(EffectWindow *w)
{
QVariant cachedTextureVariant = w->data(LanczosCacheRole);
if (cachedTextureVariant.isValid()) {
delete static_cast< GLTexture*>(cachedTextureVariant.value<void*>());
w->setData(LanczosCacheRole, QVariant());
}
}
void LanczosFilter::setUniforms()
2011-01-30 14:34:42 +00:00
{
glUniform1i(m_uTexUnit, 0);
glUniform2fv(m_uOffsets, 16, (const GLfloat*)m_offsets);
glUniform4fv(m_uKernel, 16, (const GLfloat*)m_kernel);
2011-01-30 14:34:42 +00:00
}
} // namespace