Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fractional scaling on roundness, outline, and shadow #151

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/ShapeCornersEffect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <opengl/glutils.h>
#include <effect/effecthandler.h>
#include <core/output.h>
#include <core/renderviewport.h>
#else
#include <kwinglutils.h>
#endif
Expand Down Expand Up @@ -90,11 +91,6 @@ bool ShapeCornersEffect::isMaximized(const KWin::EffectWindow *w) {
(w->y() == screenGeometry.y() && w->height() == screenGeometry.height());
}

inline QRectF operator *(const QRect& r, const qreal scale) { return {r.x() * scale, r.y() * scale, r.width() * scale, r.height() * scale}; }
inline QRectF operator *(const QRectF& r, const qreal scale) { return {r.x() * scale, r.y() * scale, r.width() * scale, r.height() * scale}; }
inline QRect toRect(const QRectF& r) { return {static_cast<int>(r.x()), static_cast<int>(r.y()), static_cast<int>(r.width()), static_cast<int>(r.height())}; }
inline const QRect& toRect(const QRect& r) { return r; }

void ShapeCornersEffect::prePaintWindow(KWin::EffectWindow *w, KWin::WindowPrePaintData &data, std::chrono::milliseconds time)
{
if (!hasEffect(w))
Expand Down Expand Up @@ -147,9 +143,15 @@ void ShapeCornersEffect::drawWindow(KWin::EffectWindow *w, int mask, const QRegi
return;
}

#if QT_VERSION_MAJOR >= 6
const auto scale = viewport.scale();
#else
const auto scale = KWin::effects->renderTargetScale();
#endif

redirect(w);
setShader(w, m_shaderManager.GetShader().get());
m_shaderManager.Bind(w);
m_shaderManager.Bind(w, scale);
glActiveTexture(GL_TEXTURE0);

#if QT_VERSION_MAJOR >= 6
Expand Down
32 changes: 17 additions & 15 deletions src/ShapeCornersShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,23 @@ bool ShapeCornersShader::IsValid() const {
}

const std::unique_ptr<KWin::GLShader>&
ShapeCornersShader::Bind(KWin::EffectWindow *w) const {
ShapeCornersShader::Bind(KWin::EffectWindow *w, qreal scale) const {
QColor outlineColor, shadowColor;
float shadowSize;
qreal shadowSize;
auto& m_palette = m_widget->palette();
auto xy = QVector2D(w->frameGeometry().topLeft() - w->expandedGeometry().topLeft());
auto max_shadow_size = xy.length();
auto frameGeometry = w->frameGeometry() * scale;
auto expandedGeometry = w->expandedGeometry() * scale;
auto xy = QVector2D(frameGeometry.topLeft() - expandedGeometry.topLeft());
qreal max_shadow_size = xy.length();
m_manager->pushShader(m_shader.get());
m_shader->setUniform(m_shader_windowSize, QVector2DSize(w->frameGeometry().size()));
m_shader->setUniform(m_shader_windowExpandedSize, QVector2DSize(w->expandedGeometry().size()));
m_shader->setUniform(m_shader_windowSize, toVector2D(frameGeometry.size()));
m_shader->setUniform(m_shader_windowExpandedSize, toVector2D(expandedGeometry.size()));
m_shader->setUniform(m_shader_windowTopLeft, xy);
m_shader->setUniform(m_shader_front, 0);
if (ShapeCornersEffect::isWindowActive(w)) {
shadowSize = std::min(static_cast<float>(ShapeCornersConfig::shadowSize()), max_shadow_size);
m_shader->setUniform(m_shader_radius, static_cast<float>(ShapeCornersConfig::size()));
m_shader->setUniform(m_shader_outlineThickness, static_cast<float>(ShapeCornersConfig::outlineThickness()));
shadowSize = std::min(ShapeCornersConfig::shadowSize() * scale, max_shadow_size);
m_shader->setUniform(m_shader_radius, static_cast<float>(ShapeCornersConfig::size() * scale));
m_shader->setUniform(m_shader_outlineThickness, static_cast<float>(ShapeCornersConfig::outlineThickness() * scale));

outlineColor = ShapeCornersConfig::activeOutlineUsePalette() ?
m_palette.color(QPalette::Active, static_cast<QPalette::ColorRole>(ShapeCornersConfig::activeOutlinePalette())):
Expand All @@ -73,9 +75,9 @@ ShapeCornersShader::Bind(KWin::EffectWindow *w) const {
outlineColor.setAlpha(ShapeCornersConfig::activeOutlineAlpha());
shadowColor.setAlpha(ShapeCornersConfig::activeShadowAlpha());
} else {
shadowSize = std::min(static_cast<float>(ShapeCornersConfig::inactiveShadowSize()), max_shadow_size);
m_shader->setUniform(m_shader_radius, static_cast<float>(ShapeCornersConfig::inactiveCornerRadius()));
m_shader->setUniform(m_shader_outlineThickness, static_cast<float>(ShapeCornersConfig::inactiveOutlineThickness()));
shadowSize = std::min(ShapeCornersConfig::inactiveShadowSize() * scale, max_shadow_size);
m_shader->setUniform(m_shader_radius, static_cast<float>(ShapeCornersConfig::inactiveCornerRadius() * scale));
m_shader->setUniform(m_shader_outlineThickness, static_cast<float>(ShapeCornersConfig::inactiveOutlineThickness() * scale));

outlineColor = ShapeCornersConfig::inactiveOutlineUsePalette() ?
m_palette.color(QPalette::Inactive, static_cast<QPalette::ColorRole>(ShapeCornersConfig::inactiveOutlinePalette())):
Expand All @@ -86,15 +88,15 @@ ShapeCornersShader::Bind(KWin::EffectWindow *w) const {
outlineColor.setAlpha(ShapeCornersConfig::inactiveOutlineAlpha());
shadowColor.setAlpha(ShapeCornersConfig::inactiveShadowAlpha());
}
m_shader->setUniform(m_shader_shadowSize, shadowSize);
m_shader->setUniform(m_shader_shadowSize, static_cast<float>(shadowSize));
m_shader->setUniform(m_shader_outlineColor, outlineColor);
m_shader->setUniform(m_shader_shadowColor, shadowColor);
return m_shader;
}

const std::unique_ptr<KWin::GLShader>&
ShapeCornersShader::Bind(const QMatrix4x4& mvp, KWin::EffectWindow *w) const {
Bind(w);
ShapeCornersShader::Bind(const QMatrix4x4& mvp, KWin::EffectWindow *w, qreal scale) const {
Bind(w, scale);
m_shader->setUniform(KWin::GLShader::ModelViewProjectionMatrix, mvp);
return m_shader;
}
Expand Down
11 changes: 5 additions & 6 deletions src/ShapeCornersShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ namespace KWin {
class ShaderManager;
}

struct QVector2DSize final: public QVector2D {
explicit QVector2DSize(const QSizeF s):
QVector2D(static_cast<float>(s.width()), static_cast<float>(s.height())) {}
};
inline QRectF operator *(const QRect& r, const qreal scale) { return {r.x() * scale, r.y() * scale, r.width() * scale, r.height() * scale}; }
inline QRectF operator *(const QRectF& r, const qreal scale) { return {r.x() * scale, r.y() * scale, r.width() * scale, r.height() * scale}; }
inline QVector2D toVector2D(const QSizeF& s) { return {static_cast<float>(s.width()), static_cast<float>(s.height())}; }

class ShapeCornersShader {
public:
Expand All @@ -42,8 +41,8 @@ class ShapeCornersShader {
* \param w The window that the effect will be rendering on
* \return A reference to the unique pointer of the loaded shader.
*/
const std::unique_ptr<KWin::GLShader>& Bind(KWin::EffectWindow *w) const;
const std::unique_ptr<KWin::GLShader>& Bind(const QMatrix4x4& mvp, KWin::EffectWindow *w) const;
const std::unique_ptr<KWin::GLShader>& Bind(KWin::EffectWindow *w, qreal scale) const;
const std::unique_ptr<KWin::GLShader>& Bind(const QMatrix4x4& mvp, KWin::EffectWindow *w, qreal scale) const;

/**
* \brief Pop the shader from the stack of rendering.
Expand Down