Commit cc1f30b4 authored by Vlad Zahorodnii's avatar Vlad Zahorodnii

[effects/showpaint] Modernize code

Summary:
Summary of changes:
* Use QVector to store colors
* Add Q_OBJECT macro
* Port away from QRegion::rects
* Fix coding style
* Use default member initialization
* Use nullptr
* Reserve rects in paintXrender

The behavior of this effect haven't been changed, it still acts as before.

Test Plan: Compiles, and the Show Paint effect is still a psychedelic thing.

Reviewers: #kwin, davidedmundson

Reviewed By: #kwin, davidedmundson

Subscribers: kwin

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D15424
parent 113ebe9b
...@@ -21,54 +21,50 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. ...@@ -21,54 +21,50 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "showpaint.h" #include "showpaint.h"
#include <kwinconfig.h>
#include <kwinglutils.h> #include <kwinglutils.h>
#ifdef KWIN_HAVE_XRENDER_COMPOSITING #ifdef KWIN_HAVE_XRENDER_COMPOSITING
#include <xcb/render.h> #include <xcb/render.h>
#endif #endif
#include <math.h>
#include <qcolor.h>
#include <QPainter> #include <QPainter>
namespace KWin namespace KWin
{ {
static QColor colors[] = { Qt::red, Qt::green, Qt::blue, Qt::cyan, Qt::magenta, static const qreal s_alpha = 0.2;
Qt::yellow, Qt::gray static const QVector<QColor> s_colors {
}; Qt::red,
Qt::green,
ShowPaintEffect::ShowPaintEffect() Qt::blue,
: color_index(0) Qt::cyan,
{ Qt::magenta,
} Qt::yellow,
Qt::gray
ShowPaintEffect::~ShowPaintEffect() };
{
} void ShowPaintEffect::paintScreen(int mask, QRegion region, ScreenPaintData &data)
void ShowPaintEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data)
{ {
painted = QRegion(); m_painted = QRegion();
effects->paintScreen(mask, region, data); effects->paintScreen(mask, region, data);
if (effects->isOpenGLCompositing()) if (effects->isOpenGLCompositing()) {
paintGL(data.projectionMatrix()); paintGL(data.projectionMatrix());
}
#ifdef KWIN_HAVE_XRENDER_COMPOSITING #ifdef KWIN_HAVE_XRENDER_COMPOSITING
if (effects->compositingType() == XRenderCompositing) if (effects->compositingType() == XRenderCompositing) {
paintXrender(); paintXrender();
}
#endif #endif
if (effects->compositingType() == QPainterCompositing) { if (effects->compositingType() == QPainterCompositing) {
paintQPainter(); paintQPainter();
} }
if (++color_index == sizeof(colors) / sizeof(colors[ 0 ])) if (++m_colorIndex == s_colors.count()) {
color_index = 0; m_colorIndex = 0;
}
} }
void ShowPaintEffect::paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data) void ShowPaintEffect::paintWindow(EffectWindow *w, int mask, QRegion region, WindowPaintData &data)
{ {
painted |= region; m_painted |= region;
effects->paintWindow(w, mask, region, data); effects->paintWindow(w, mask, region, data);
} }
...@@ -81,12 +77,12 @@ void ShowPaintEffect::paintGL(const QMatrix4x4 &projection) ...@@ -81,12 +77,12 @@ void ShowPaintEffect::paintGL(const QMatrix4x4 &projection)
binder.shader()->setUniform(GLShader::ModelViewProjectionMatrix, projection); binder.shader()->setUniform(GLShader::ModelViewProjectionMatrix, projection);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
QColor color = colors[ color_index ]; QColor color = s_colors[m_colorIndex];
color.setAlphaF(0.2); color.setAlphaF(s_alpha);
vbo->setColor(color); vbo->setColor(color);
QVector<float> verts; QVector<float> verts;
verts.reserve(painted.rects().count() * 12); verts.reserve(m_painted.rectCount() * 12);
foreach (const QRect & r, painted.rects()) { for (const QRect &r : m_painted) {
verts << r.x() + r.width() << r.y(); verts << r.x() + r.width() << r.y();
verts << r.x() << r.y(); verts << r.x() << r.y();
verts << r.x() << r.y() + r.height(); verts << r.x() << r.y() + r.height();
...@@ -94,7 +90,7 @@ void ShowPaintEffect::paintGL(const QMatrix4x4 &projection) ...@@ -94,7 +90,7 @@ void ShowPaintEffect::paintGL(const QMatrix4x4 &projection)
verts << r.x() + r.width() << r.y() + r.height(); verts << r.x() + r.width() << r.y() + r.height();
verts << r.x() + r.width() << r.y(); verts << r.x() + r.width() << r.y();
} }
vbo->setData(verts.count() / 2, 2, verts.data(), NULL); vbo->setData(verts.count() / 2, 2, verts.data(), nullptr);
vbo->render(GL_TRIANGLES); vbo->render(GL_TRIANGLES);
glDisable(GL_BLEND); glDisable(GL_BLEND);
} }
...@@ -103,14 +99,14 @@ void ShowPaintEffect::paintXrender() ...@@ -103,14 +99,14 @@ void ShowPaintEffect::paintXrender()
{ {
#ifdef KWIN_HAVE_XRENDER_COMPOSITING #ifdef KWIN_HAVE_XRENDER_COMPOSITING
xcb_render_color_t col; xcb_render_color_t col;
float alpha = 0.2; const QColor &color = s_colors[m_colorIndex];
const QColor& color = colors[ color_index ]; col.alpha = int(s_alpha * 0xffff);
col.alpha = int(alpha * 0xffff); col.red = int(s_alpha * 0xffff * color.red() / 255);
col.red = int(alpha * 0xffff * color.red() / 255); col.green = int(s_alpha * 0xffff * color.green() / 255);
col.green = int(alpha * 0xffff * color.green() / 255); col.blue = int(s_alpha * 0xffff * color.blue() / 255);
col.blue = int(alpha * 0xffff * color.blue() / 255);
QVector<xcb_rectangle_t> rects; QVector<xcb_rectangle_t> rects;
foreach (const QRect & r, painted.rects()) { rects.reserve(m_painted.rectCount());
for (const QRect &r : m_painted) {
xcb_rectangle_t rect = {int16_t(r.x()), int16_t(r.y()), uint16_t(r.width()), uint16_t(r.height())}; xcb_rectangle_t rect = {int16_t(r.x()), int16_t(r.y()), uint16_t(r.width()), uint16_t(r.height())};
rects << rect; rects << rect;
} }
...@@ -120,11 +116,11 @@ void ShowPaintEffect::paintXrender() ...@@ -120,11 +116,11 @@ void ShowPaintEffect::paintXrender()
void ShowPaintEffect::paintQPainter() void ShowPaintEffect::paintQPainter()
{ {
QColor color = colors[ color_index ]; QColor color = s_colors[m_colorIndex];
color.setAlphaF(0.2); color.setAlphaF(s_alpha);
foreach (const QRect & r, painted.rects()) { for (const QRect &r : m_painted) {
effects->scenePainter()->fillRect(r, color); effects->scenePainter()->fillRect(r, color);
} }
} }
} // namespace } // namespace KWin
...@@ -26,22 +26,23 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. ...@@ -26,22 +26,23 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
namespace KWin namespace KWin
{ {
class ShowPaintEffect class ShowPaintEffect : public Effect
: public Effect
{ {
Q_OBJECT
public: public:
ShowPaintEffect(); void paintScreen(int mask, QRegion region, ScreenPaintData &data) override;
~ShowPaintEffect(); void paintWindow(EffectWindow *w, int mask, QRegion region, WindowPaintData &data) override;
virtual void paintScreen(int mask, QRegion region, ScreenPaintData& data);
virtual void paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data);
private: private:
void paintGL(const QMatrix4x4 &projection); void paintGL(const QMatrix4x4 &projection);
void paintXrender(); void paintXrender();
void paintQPainter(); void paintQPainter();
QRegion painted; // what's painted in one pass
int color_index; QRegion m_painted; // what's painted in one pass
int m_colorIndex = 0;
}; };
} // namespace } // namespace KWin
#endif #endif
Markdown is supported
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