Commit 7637cfc2 authored by Vlad Zahorodnii's avatar Vlad Zahorodnii
Browse files

[scenes/opengl] Fix overlapping shadow tiles

Summary:
This problem appears if shadow corner tiles are too big and
some window has size smaller than 2 * shadowTileSize.

This change tries to address the problem above by exclusing
overlapping tile parts. If there are any two overlapping corners
then tile between them(top/right/bottom/left) is not rendered.

Also, because some corner tile parts can be excluded, corner tiles
are expected to be symmetrical(i.e. if we remove right half from
the top-left tile and left half from the top-right tile and
stick them together, they still look fine, there are no misalignments, etc).
Most shadows(e.g. shadows from Breeze) have such behaviour.

No tiles are overlapping

{F5728514, layout=center, size=full}

Overlapping tiles

{F5728516, layout=center, size=full}

And this is how it supposed to be

{F5728517, layout=center, size=full}

Test Plan:
* apply D11069 to Breeze
* in System Settings/Application Style/Window Decorations, choose "Very Large" shadow size
* open Konsole
* resize it to a minimum possible size

Reviewers: #kwin, graesslin, davidedmundson

Reviewed By: #kwin, davidedmundson

Subscribers: davidedmundson, ngraham, anemeth, abetts, kwin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D10811
parent a3cff85e
......@@ -39,6 +39,7 @@ integrationTest(WAYLAND_ONLY NAME testShellClient SRCS shell_client_test.cpp)
integrationTest(WAYLAND_ONLY NAME testDontCrashNoBorder SRCS dont_crash_no_border.cpp)
integrationTest(NAME testXClipboardSync SRCS xclipboardsync_test.cpp)
integrationTest(WAYLAND_ONLY NAME testSceneOpenGL SRCS scene_opengl_test.cpp generic_scene_opengl_test.cpp)
integrationTest(WAYLAND_ONLY NAME testSceneOpenGLShadow SRCS scene_opengl_shadow_test.cpp)
integrationTest(WAYLAND_ONLY NAME testSceneOpenGLES SRCS scene_opengl_es_test.cpp generic_scene_opengl_test.cpp)
integrationTest(WAYLAND_ONLY NAME testNoXdgRuntimeDir SRCS no_xdg_runtime_dir_test.cpp)
integrationTest(WAYLAND_ONLY NAME testScreenChanges SRCS screen_changes_test.cpp)
......@@ -79,3 +80,4 @@ endif()
add_subdirectory(scripting)
add_subdirectory(effects)
add_subdirectory(fakes)
add_subdirectory(org.kde.kdecoration2)
########################################################
# FakeDecoWithShadows
########################################################
add_library(fakedecoshadows MODULE fakedecoration_with_shadows.cpp)
set_target_properties(fakedecoshadows PROPERTIES
PREFIX ""
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/fakes/org.kde.kdecoration2")
target_link_libraries(fakedecoshadows
PUBLIC
Qt5::Core
Qt5::Gui
PRIVATE
KDecoration2::KDecoration
KF5::CoreAddons)
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2018 Vlad Zagorodniy <vladzzag@gmail.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/>.
*********************************************************************/
#include <QPainter>
#include <KDecoration2/Decoration>
#include <KPluginFactory>
class FakeDecoWithShadows : public KDecoration2::Decoration
{
Q_OBJECT
public:
explicit FakeDecoWithShadows(QObject *parent = nullptr, const QVariantList &args = QVariantList())
: Decoration(parent, args) {}
~FakeDecoWithShadows() override {}
void paint(QPainter *painter, const QRect &repaintRegion) override {
Q_UNUSED(painter)
Q_UNUSED(repaintRegion)
}
public Q_SLOTS:
void init() override {
const int shadowSize = 128;
const int offsetTop = 64;
const int offsetLeft = 48;
const QRect shadowRect(0, 0, 4 * shadowSize + 1, 4 * shadowSize + 1);
QImage shadowTexture(shadowRect.size(), QImage::Format_ARGB32_Premultiplied);
shadowTexture.fill(Qt::transparent);
const QMargins padding(
shadowSize - offsetLeft,
shadowSize - offsetTop,
shadowSize + offsetLeft,
shadowSize + offsetTop);
auto decoShadow = QSharedPointer<KDecoration2::DecorationShadow>::create();
decoShadow->setPadding(padding);
decoShadow->setInnerShadowRect(QRect(shadowRect.center(), QSize(1, 1)));
decoShadow->setShadow(shadowTexture);
setShadow(decoShadow);
}
};
K_PLUGIN_FACTORY_WITH_JSON(
FakeDecoWithShadowsFactory,
"fakedecoration_with_shadows.json",
registerPlugin<FakeDecoWithShadows>();
)
#include "fakedecoration_with_shadows.moc"
{
"KPlugin": {
"Description": "Window decoration to test shadow tile overlaps",
"EnabledByDefault": false,
"Id": "org.kde.test.fakedecowithshadows",
"Name": "Fake Decoration With Shadows",
"ServiceTypes": [
"org.kde.kdecoration2"
]
},
"org.kde.kdecoration2": {
"blur": false,
"kcmodule": false
}
}
This diff is collapsed.
......@@ -2137,8 +2137,14 @@ SceneOpenGLShadow::~SceneOpenGLShadow()
void SceneOpenGLShadow::buildQuads()
{
// prepare window quads
m_shadowQuads.clear();
// Do not draw shadows if window width or window height is less than
// 5 px. 5 is an arbitrary choice.
if (topLevel()->width() < 5 || topLevel()->height() < 5) {
m_shadowQuads.clear();
setShadowRegion(QRegion());
return;
}
const QSizeF top(elementSize(ShadowElementTop));
const QSizeF topRight(elementSize(ShadowElementTopRight));
const QSizeF right(elementSize(ShadowElementRight));
......@@ -2147,105 +2153,178 @@ void SceneOpenGLShadow::buildQuads()
const QSizeF bottomLeft(elementSize(ShadowElementBottomLeft));
const QSizeF left(elementSize(ShadowElementLeft));
const QSizeF topLeft(elementSize(ShadowElementTopLeft));
if ((left.width() - leftOffset() > topLevel()->width()) ||
(right.width() - rightOffset() > topLevel()->width()) ||
(top.height() - topOffset() > topLevel()->height()) ||
(bottom.height() - bottomOffset() > topLevel()->height())) {
// if our shadow is bigger than the window, we don't render the shadow
setShadowRegion(QRegion());
return;
}
const QRectF outerRect(QPointF(-leftOffset(), -topOffset()),
QPointF(topLevel()->width() + rightOffset(), topLevel()->height() + bottomOffset()));
QPointF(topLevel()->width() + rightOffset(),
topLevel()->height() + bottomOffset()));
const int width = std::max({topLeft.width(), left.width(), bottomLeft.width()})
+ std::max(top.width(), bottom.width())
+ std::max({topRight.width(), right.width(), bottomRight.width()});
const int height = std::max({topLeft.height(), top.height(), topRight.height()})
+ std::max(left.height(), right.height())
+ std::max({bottomLeft.height(), bottom.height(), bottomRight.height()});
QRectF topLeftRect(outerRect.topLeft(), topLeft);
QRectF topRightRect(outerRect.topRight() - QPointF(topRight.width(), 0), topRight);
QRectF bottomRightRect(
outerRect.bottomRight() - QPointF(bottomRight.width(), bottomRight.height()),
bottomRight);
QRectF bottomLeftRect(outerRect.bottomLeft() - QPointF(0, bottomLeft.height()), bottomLeft);
// Re-distribute the corner tiles so no one of them is overlapping with others.
// By doing this, we assume that shadow's corner tiles are symmetric
// and it is OK to not draw top/right/bottom/left tile between corners.
// For example, let's say top-left and top-right tiles are overlapping.
// In that case, the right side of the top-left tile will be shifted to left,
// the left side of the top-right tile will shifted to right, and the top
// tile won't be rendered.
bool drawTop = true;
if (topLeftRect.right() >= topRightRect.left()) {
const float halfOverlap = qAbs(topLeftRect.right() - topRightRect.left()) / 2;
topLeftRect.setRight(topLeftRect.right() - halfOverlap);
topRightRect.setLeft(topRightRect.left() + halfOverlap);
drawTop = false;
}
bool drawRight = true;
if (topRightRect.bottom() >= bottomRightRect.top()) {
const float halfOverlap = qAbs(topRightRect.bottom() - bottomRightRect.top()) / 2;
topRightRect.setBottom(topRightRect.bottom() - halfOverlap);
bottomRightRect.setTop(bottomRightRect.top() + halfOverlap);
drawRight = false;
}
bool drawBottom = true;
if (bottomLeftRect.right() >= bottomRightRect.left()) {
const float halfOverlap = qAbs(bottomLeftRect.right() - bottomRightRect.left()) / 2;
bottomLeftRect.setRight(bottomLeftRect.right() - halfOverlap);
bottomRightRect.setLeft(bottomRightRect.left() + halfOverlap);
drawBottom = false;
}
bool drawLeft = true;
if (topLeftRect.bottom() >= bottomLeftRect.top()) {
const float halfOverlap = qAbs(topLeftRect.bottom() - bottomLeftRect.top()) / 2;
topLeftRect.setBottom(topLeftRect.bottom() - halfOverlap);
bottomLeftRect.setTop(bottomLeftRect.top() + halfOverlap);
drawLeft = false;
}
qreal tx1 = 0.0,
tx2 = 0.0,
ty1 = 0.0,
ty2 = 0.0;
const int width = std::max({topLeft.width(), left.width(), bottomLeft.width()}) +
std::max(top.width(), bottom.width()) +
std::max({topRight.width(), right.width(), bottomRight.width()});
const int height = std::max({topLeft.height(), top.height(), topRight.height()}) +
std::max(left.height(), right.height()) +
std::max({bottomLeft.height(), bottom.height(), bottomRight.height()});
qreal tx1(0.0), tx2(0.0), ty1(0.0), ty2(0.0);
m_shadowQuads.clear();
tx2 = topLeft.width()/width;
ty2 = topLeft.height()/height;
tx1 = 0.0;
ty1 = 0.0;
tx2 = topLeftRect.width() / width;
ty2 = topLeftRect.height() / height;
WindowQuad topLeftQuad(WindowQuadShadow);
topLeftQuad[ 0 ] = WindowVertex(outerRect.x(), outerRect.y(), tx1, ty1);
topLeftQuad[ 1 ] = WindowVertex(outerRect.x() + topLeft.width(), outerRect.y(), tx2, ty1);
topLeftQuad[ 2 ] = WindowVertex(outerRect.x() + topLeft.width(), outerRect.y() + topLeft.height(), tx2, ty2);
topLeftQuad[ 3 ] = WindowVertex(outerRect.x(), outerRect.y() + topLeft.height(), tx1, ty2);
topLeftQuad[0] = WindowVertex(topLeftRect.left(), topLeftRect.top(), tx1, ty1);
topLeftQuad[1] = WindowVertex(topLeftRect.right(), topLeftRect.top(), tx2, ty1);
topLeftQuad[2] = WindowVertex(topLeftRect.right(), topLeftRect.bottom(), tx2, ty2);
topLeftQuad[3] = WindowVertex(topLeftRect.left(), topLeftRect.bottom(), tx1, ty2);
m_shadowQuads.append(topLeftQuad);
tx1 = tx2;
tx2 = (topLeft.width() + top.width())/width;
ty2 = top.height()/height;
WindowQuad topQuad(WindowQuadShadow);
topQuad[ 0 ] = WindowVertex(outerRect.x() + topLeft.width(), outerRect.y(), tx1, ty1);
topQuad[ 1 ] = WindowVertex(outerRect.right() - topRight.width(), outerRect.y(), tx2, ty1);
topQuad[ 2 ] = WindowVertex(outerRect.right() - topRight.width(), outerRect.y() + top.height(),tx2, ty2);
topQuad[ 3 ] = WindowVertex(outerRect.x() + topLeft.width(), outerRect.y() + top.height(), tx1, ty2);
m_shadowQuads.append(topQuad);
tx1 = tx2;
tx1 = 1.0 - topRightRect.width() / width;
ty1 = 0.0;
tx2 = 1.0;
ty2 = topRight.height()/height;
ty2 = topRightRect.height() / height;
WindowQuad topRightQuad(WindowQuadShadow);
topRightQuad[ 0 ] = WindowVertex(outerRect.right() - topRight.width(), outerRect.y(), tx1, ty1);
topRightQuad[ 1 ] = WindowVertex(outerRect.right(), outerRect.y(), tx2, ty1);
topRightQuad[ 2 ] = WindowVertex(outerRect.right(), outerRect.y() + topRight.height(), tx2, ty2);
topRightQuad[ 3 ] = WindowVertex(outerRect.right() - topRight.width(), outerRect.y() + topRight.height(), tx1, ty2);
topRightQuad[0] = WindowVertex(topRightRect.left(), topRightRect.top(), tx1, ty1);
topRightQuad[1] = WindowVertex(topRightRect.right(), topRightRect.top(), tx2, ty1);
topRightQuad[2] = WindowVertex(topRightRect.right(), topRightRect.bottom(), tx2, ty2);
topRightQuad[3] = WindowVertex(topRightRect.left(), topRightRect.bottom(), tx1, ty2);
m_shadowQuads.append(topRightQuad);
tx1 = (width - right.width())/width;
ty1 = topRight.height()/height;
ty2 = (topRight.height() + right.height())/height;
WindowQuad rightQuad(WindowQuadShadow);
rightQuad[ 0 ] = WindowVertex(outerRect.right() - right.width(), outerRect.y() + topRight.height(), tx1, ty1);
rightQuad[ 1 ] = WindowVertex(outerRect.right(), outerRect.y() + topRight.height(), tx2, ty1);
rightQuad[ 2 ] = WindowVertex(outerRect.right(), outerRect.bottom() - bottomRight.height(), tx2, ty2);
rightQuad[ 3 ] = WindowVertex(outerRect.right() - right.width(), outerRect.bottom() - bottomRight.height(), tx1, ty2);
m_shadowQuads.append(rightQuad);
tx1 = (width - bottomRight.width())/width;
ty1 = ty2;
tx1 = 1.0 - bottomRightRect.width() / width;
tx2 = 1.0;
ty1 = 1.0 - bottomRightRect.height() / height;
ty2 = 1.0;
WindowQuad bottomRightQuad(WindowQuadShadow);
bottomRightQuad[ 0 ] = WindowVertex(outerRect.right() - bottomRight.width(), outerRect.bottom() - bottomRight.height(), tx1, ty1);
bottomRightQuad[ 1 ] = WindowVertex(outerRect.right(), outerRect.bottom() - bottomRight.height(), tx2, ty1);
bottomRightQuad[ 2 ] = WindowVertex(outerRect.right(), outerRect.bottom(), tx2, ty2);
bottomRightQuad[ 3 ] = WindowVertex(outerRect.right() - bottomRight.width(), outerRect.bottom(), tx1, ty2);
bottomRightQuad[0] = WindowVertex(bottomRightRect.left(), bottomRightRect.top(), tx1, ty1);
bottomRightQuad[1] = WindowVertex(bottomRightRect.right(), bottomRightRect.top(), tx2, ty1);
bottomRightQuad[2] = WindowVertex(bottomRightRect.right(), bottomRightRect.bottom(), tx2, ty2);
bottomRightQuad[3] = WindowVertex(bottomRightRect.left(), bottomRightRect.bottom(), tx1, ty2);
m_shadowQuads.append(bottomRightQuad);
tx2 = tx1;
tx1 = bottomLeft.width()/width;
ty1 = (height - bottom.height())/height;
WindowQuad bottomQuad(WindowQuadShadow);
bottomQuad[ 0 ] = WindowVertex(outerRect.x() + bottomLeft.width(), outerRect.bottom() - bottom.height(), tx1, ty1);
bottomQuad[ 1 ] = WindowVertex(outerRect.right() - bottomRight.width(), outerRect.bottom() - bottom.height(), tx2, ty1);
bottomQuad[ 2 ] = WindowVertex(outerRect.right() - bottomRight.width(), outerRect.bottom(), tx2, ty2);
bottomQuad[ 3 ] = WindowVertex(outerRect.x() + bottomLeft.width(), outerRect.bottom(), tx1, ty2);
m_shadowQuads.append(bottomQuad);
tx1 = 0.0;
tx2 = bottomLeft.width()/width;
ty1 = (height - bottomLeft.height())/height;
tx2 = bottomLeftRect.width() / width;
ty1 = 1.0 - bottomLeftRect.height() / height;
ty2 = 1.0;
WindowQuad bottomLeftQuad(WindowQuadShadow);
bottomLeftQuad[ 0 ] = WindowVertex(outerRect.x(), outerRect.bottom() - bottomLeft.height(), tx1, ty1);
bottomLeftQuad[ 1 ] = WindowVertex(outerRect.x() + bottomLeft.width(), outerRect.bottom() - bottomLeft.height(), tx2, ty1);
bottomLeftQuad[ 2 ] = WindowVertex(outerRect.x() + bottomLeft.width(), outerRect.bottom(), tx2, ty2);
bottomLeftQuad[ 3 ] = WindowVertex(outerRect.x(), outerRect.bottom(), tx1, ty2);
bottomLeftQuad[0] = WindowVertex(bottomLeftRect.left(), bottomLeftRect.top(), tx1, ty1);
bottomLeftQuad[1] = WindowVertex(bottomLeftRect.right(), bottomLeftRect.top(), tx2, ty1);
bottomLeftQuad[2] = WindowVertex(bottomLeftRect.right(), bottomLeftRect.bottom(), tx2, ty2);
bottomLeftQuad[3] = WindowVertex(bottomLeftRect.left(), bottomLeftRect.bottom(), tx1, ty2);
m_shadowQuads.append(bottomLeftQuad);
tx2 = left.width()/width;
ty2 = ty1;
ty1 = topLeft.height()/height;
WindowQuad leftQuad(WindowQuadShadow);
leftQuad[ 0 ] = WindowVertex(outerRect.x(), outerRect.y() + topLeft.height(), tx1, ty1);
leftQuad[ 1 ] = WindowVertex(outerRect.x() + left.width(), outerRect.y() + topLeft.height(), tx2, ty1);
leftQuad[ 2 ] = WindowVertex(outerRect.x() + left.width(), outerRect.bottom() - bottomLeft.height(), tx2, ty2);
leftQuad[ 3 ] = WindowVertex(outerRect.x(), outerRect.bottom() - bottomLeft.height(), tx1, ty2);
m_shadowQuads.append(leftQuad);
if (drawTop) {
const QRectF topRect(
topLeftRect.topRight(),
topRightRect.bottomLeft());
tx1 = topLeft.width() / width;
ty1 = 0.0;
tx2 = 1.0 - topRight.width() / width;
ty2 = topRect.height() / height;
WindowQuad topQuad(WindowQuadShadow);
topQuad[0] = WindowVertex(topRect.left(), topRect.top(), tx1, ty1);
topQuad[1] = WindowVertex(topRect.right(), topRect.top(), tx2, ty1);
topQuad[2] = WindowVertex(topRect.right(), topRect.bottom(), tx2, ty2);
topQuad[3] = WindowVertex(topRect.left(), topRect.bottom(), tx1, ty2);
m_shadowQuads.append(topQuad);
}
if (drawRight) {
const QRectF rightRect(
topRightRect.bottomLeft(),
bottomRightRect.topRight());
tx1 = 1.0 - rightRect.width() / width;
ty1 = topRight.height() / height;
tx2 = 1.0;
ty2 = 1.0 - bottomRight.height() / height;
WindowQuad rightQuad(WindowQuadShadow);
rightQuad[0] = WindowVertex(rightRect.left(), rightRect.top(), tx1, ty1);
rightQuad[1] = WindowVertex(rightRect.right(), rightRect.top(), tx2, ty1);
rightQuad[2] = WindowVertex(rightRect.right(), rightRect.bottom(), tx2, ty2);
rightQuad[3] = WindowVertex(rightRect.left(), rightRect.bottom(), tx1, ty2);
m_shadowQuads.append(rightQuad);
}
if (drawBottom) {
const QRectF bottomRect(
bottomLeftRect.topRight(),
bottomRightRect.bottomLeft());
tx1 = bottomLeft.width() / width;
ty1 = 1.0 - bottomRect.height() / height;
tx2 = 1.0 - bottomRight.width() / width;
ty2 = 1.0;
WindowQuad bottomQuad(WindowQuadShadow);
bottomQuad[0] = WindowVertex(bottomRect.left(), bottomRect.top(), tx1, ty1);
bottomQuad[1] = WindowVertex(bottomRect.right(), bottomRect.top(), tx2, ty1);
bottomQuad[2] = WindowVertex(bottomRect.right(), bottomRect.bottom(), tx2, ty2);
bottomQuad[3] = WindowVertex(bottomRect.left(), bottomRect.bottom(), tx1, ty2);
m_shadowQuads.append(bottomQuad);
}
if (drawLeft) {
const QRectF leftRect(
topLeftRect.bottomLeft(),
bottomLeftRect.topRight());
tx1 = 0.0;
ty1 = topLeft.height() / height;
tx2 = leftRect.width() / width;
ty2 = 1.0 - bottomRight.height() / height;
WindowQuad leftQuad(WindowQuadShadow);
leftQuad[0] = WindowVertex(leftRect.left(), leftRect.top(), tx1, ty1);
leftQuad[1] = WindowVertex(leftRect.right(), leftRect.top(), tx2, ty1);
leftQuad[2] = WindowVertex(leftRect.right(), leftRect.bottom(), tx2, ty2);
leftQuad[3] = WindowVertex(leftRect.left(), leftRect.bottom(), tx1, ty2);
m_shadowQuads.append(leftQuad);
}
}
bool SceneOpenGLShadow::prepareBackend()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment