Changeset View
Changeset View
Standalone View
Standalone View
effects/showpaint/showpaint.cpp
Show All 15 Lines | |||||
16 | GNU General Public License for more details. | 16 | GNU General Public License for more details. | ||
17 | 17 | | |||
18 | You should have received a copy of the GNU General Public License | 18 | You should have received a copy of the GNU General Public License | ||
19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 19 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
20 | *********************************************************************/ | 20 | *********************************************************************/ | ||
21 | 21 | | |||
22 | #include "showpaint.h" | 22 | #include "showpaint.h" | ||
23 | 23 | | |||
24 | #include <kwinconfig.h> | | |||
25 | | ||||
26 | #include <kwinglutils.h> | 24 | #include <kwinglutils.h> | ||
27 | #ifdef KWIN_HAVE_XRENDER_COMPOSITING | 25 | #ifdef KWIN_HAVE_XRENDER_COMPOSITING | ||
28 | #include <xcb/render.h> | 26 | #include <xcb/render.h> | ||
29 | #endif | 27 | #endif | ||
30 | 28 | | |||
31 | #include <math.h> | | |||
32 | | ||||
33 | #include <qcolor.h> | | |||
34 | #include <QPainter> | 29 | #include <QPainter> | ||
35 | 30 | | |||
36 | namespace KWin | 31 | namespace KWin | ||
37 | { | 32 | { | ||
38 | 33 | | |||
39 | static QColor colors[] = { Qt::red, Qt::green, Qt::blue, Qt::cyan, Qt::magenta, | 34 | static const qreal s_alpha = 0.2; | ||
40 | Qt::yellow, Qt::gray | 35 | static const QVector<QColor> s_colors { | ||
36 | Qt::red, | ||||
37 | Qt::green, | ||||
38 | Qt::blue, | ||||
39 | Qt::cyan, | ||||
40 | Qt::magenta, | ||||
41 | Qt::yellow, | ||||
42 | Qt::gray | ||||
41 | }; | 43 | }; | ||
42 | 44 | | |||
43 | ShowPaintEffect::ShowPaintEffect() | | |||
44 | : color_index(0) | | |||
45 | { | | |||
46 | } | | |||
47 | | ||||
48 | ShowPaintEffect::~ShowPaintEffect() | | |||
49 | { | | |||
50 | } | | |||
51 | | ||||
52 | void ShowPaintEffect::paintScreen(int mask, QRegion region, ScreenPaintData& data) | 45 | void ShowPaintEffect::paintScreen(int mask, QRegion region, ScreenPaintData &data) | ||
53 | { | 46 | { | ||
54 | painted = QRegion(); | 47 | m_painted = QRegion(); | ||
55 | effects->paintScreen(mask, region, data); | 48 | effects->paintScreen(mask, region, data); | ||
56 | if (effects->isOpenGLCompositing()) | 49 | if (effects->isOpenGLCompositing()) { | ||
57 | paintGL(data.projectionMatrix()); | 50 | paintGL(data.projectionMatrix()); | ||
51 | } | ||||
58 | #ifdef KWIN_HAVE_XRENDER_COMPOSITING | 52 | #ifdef KWIN_HAVE_XRENDER_COMPOSITING | ||
59 | if (effects->compositingType() == XRenderCompositing) | 53 | if (effects->compositingType() == XRenderCompositing) { | ||
60 | paintXrender(); | 54 | paintXrender(); | ||
55 | } | ||||
61 | #endif | 56 | #endif | ||
62 | if (effects->compositingType() == QPainterCompositing) { | 57 | if (effects->compositingType() == QPainterCompositing) { | ||
63 | paintQPainter(); | 58 | paintQPainter(); | ||
64 | } | 59 | } | ||
65 | if (++color_index == sizeof(colors) / sizeof(colors[ 0 ])) | 60 | if (++m_colorIndex == s_colors.count()) { | ||
66 | color_index = 0; | 61 | m_colorIndex = 0; | ||
62 | } | ||||
67 | } | 63 | } | ||
68 | 64 | | |||
69 | void ShowPaintEffect::paintWindow(EffectWindow* w, int mask, QRegion region, WindowPaintData& data) | 65 | void ShowPaintEffect::paintWindow(EffectWindow *w, int mask, QRegion region, WindowPaintData &data) | ||
70 | { | 66 | { | ||
71 | painted |= region; | 67 | m_painted |= region; | ||
72 | effects->paintWindow(w, mask, region, data); | 68 | effects->paintWindow(w, mask, region, data); | ||
73 | } | 69 | } | ||
74 | 70 | | |||
75 | void ShowPaintEffect::paintGL(const QMatrix4x4 &projection) | 71 | void ShowPaintEffect::paintGL(const QMatrix4x4 &projection) | ||
76 | { | 72 | { | ||
77 | GLVertexBuffer *vbo = GLVertexBuffer::streamingBuffer(); | 73 | GLVertexBuffer *vbo = GLVertexBuffer::streamingBuffer(); | ||
78 | vbo->reset(); | 74 | vbo->reset(); | ||
79 | vbo->setUseColor(true); | 75 | vbo->setUseColor(true); | ||
80 | ShaderBinder binder(ShaderTrait::UniformColor); | 76 | ShaderBinder binder(ShaderTrait::UniformColor); | ||
81 | binder.shader()->setUniform(GLShader::ModelViewProjectionMatrix, projection); | 77 | binder.shader()->setUniform(GLShader::ModelViewProjectionMatrix, projection); | ||
82 | glEnable(GL_BLEND); | 78 | glEnable(GL_BLEND); | ||
83 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | 79 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | ||
84 | QColor color = colors[ color_index ]; | 80 | QColor color = s_colors[m_colorIndex]; | ||
85 | color.setAlphaF(0.2); | 81 | color.setAlphaF(s_alpha); | ||
86 | vbo->setColor(color); | 82 | vbo->setColor(color); | ||
87 | QVector<float> verts; | 83 | QVector<float> verts; | ||
88 | verts.reserve(painted.rects().count() * 12); | 84 | verts.reserve(m_painted.rectCount() * 12); | ||
89 | foreach (const QRect & r, painted.rects()) { | 85 | for (const QRect &r : m_painted) { | ||
90 | verts << r.x() + r.width() << r.y(); | 86 | verts << r.x() + r.width() << r.y(); | ||
91 | verts << r.x() << r.y(); | 87 | verts << r.x() << r.y(); | ||
92 | verts << r.x() << r.y() + r.height(); | 88 | verts << r.x() << r.y() + r.height(); | ||
93 | verts << r.x() << r.y() + r.height(); | 89 | verts << r.x() << r.y() + r.height(); | ||
94 | verts << r.x() + r.width() << r.y() + r.height(); | 90 | verts << r.x() + r.width() << r.y() + r.height(); | ||
95 | verts << r.x() + r.width() << r.y(); | 91 | verts << r.x() + r.width() << r.y(); | ||
96 | } | 92 | } | ||
97 | vbo->setData(verts.count() / 2, 2, verts.data(), NULL); | 93 | vbo->setData(verts.count() / 2, 2, verts.data(), nullptr); | ||
98 | vbo->render(GL_TRIANGLES); | 94 | vbo->render(GL_TRIANGLES); | ||
99 | glDisable(GL_BLEND); | 95 | glDisable(GL_BLEND); | ||
100 | } | 96 | } | ||
101 | 97 | | |||
102 | void ShowPaintEffect::paintXrender() | 98 | void ShowPaintEffect::paintXrender() | ||
103 | { | 99 | { | ||
104 | #ifdef KWIN_HAVE_XRENDER_COMPOSITING | 100 | #ifdef KWIN_HAVE_XRENDER_COMPOSITING | ||
105 | xcb_render_color_t col; | 101 | xcb_render_color_t col; | ||
106 | float alpha = 0.2; | 102 | const QColor &color = s_colors[m_colorIndex]; | ||
107 | const QColor& color = colors[ color_index ]; | 103 | col.alpha = int(s_alpha * 0xffff); | ||
108 | col.alpha = int(alpha * 0xffff); | 104 | col.red = int(s_alpha * 0xffff * color.red() / 255); | ||
109 | col.red = int(alpha * 0xffff * color.red() / 255); | 105 | col.green = int(s_alpha * 0xffff * color.green() / 255); | ||
110 | col.green = int(alpha * 0xffff * color.green() / 255); | 106 | col.blue = int(s_alpha * 0xffff * color.blue() / 255); | ||
111 | col.blue = int(alpha * 0xffff * color.blue() / 255); | | |||
112 | QVector<xcb_rectangle_t> rects; | 107 | QVector<xcb_rectangle_t> rects; | ||
113 | foreach (const QRect & r, painted.rects()) { | 108 | rects.reserve(m_painted.rectCount()); | ||
109 | for (const QRect &r : m_painted) { | ||||
114 | xcb_rectangle_t rect = {int16_t(r.x()), int16_t(r.y()), uint16_t(r.width()), uint16_t(r.height())}; | 110 | xcb_rectangle_t rect = {int16_t(r.x()), int16_t(r.y()), uint16_t(r.width()), uint16_t(r.height())}; | ||
115 | rects << rect; | 111 | rects << rect; | ||
116 | } | 112 | } | ||
117 | xcb_render_fill_rectangles(xcbConnection(), XCB_RENDER_PICT_OP_OVER, effects->xrenderBufferPicture(), col, rects.count(), rects.constData()); | 113 | xcb_render_fill_rectangles(xcbConnection(), XCB_RENDER_PICT_OP_OVER, effects->xrenderBufferPicture(), col, rects.count(), rects.constData()); | ||
118 | #endif | 114 | #endif | ||
119 | } | 115 | } | ||
120 | 116 | | |||
121 | void ShowPaintEffect::paintQPainter() | 117 | void ShowPaintEffect::paintQPainter() | ||
122 | { | 118 | { | ||
123 | QColor color = colors[ color_index ]; | 119 | QColor color = s_colors[m_colorIndex]; | ||
124 | color.setAlphaF(0.2); | 120 | color.setAlphaF(s_alpha); | ||
125 | foreach (const QRect & r, painted.rects()) { | 121 | for (const QRect &r : m_painted) { | ||
126 | effects->scenePainter()->fillRect(r, color); | 122 | effects->scenePainter()->fillRect(r, color); | ||
127 | } | 123 | } | ||
128 | } | 124 | } | ||
129 | 125 | | |||
130 | } // namespace | 126 | } // namespace KWin |