1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtOpenGL module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "private/qpixmapfilter_p.h"
#include "private/qpixmapdata_gl_p.h"
#include "private/qpaintengineex_opengl2_p.h"
#include "private/qglengineshadermanager_p.h"
#include "qglpixmapfilter_p.h"
#include "qgraphicssystem_gl_p.h"
#include "qpaintengine_opengl_p.h"
#include "qcache.h"
#include "qglframebufferobject.h"
#include "qglshaderprogram.h"
#include "qgl_p.h"
#include "private/qapplication_p.h"
#include "private/qmath_p.h"
QT_BEGIN_NAMESPACE
void QGLPixmapFilterBase::bindTexture(const QPixmap &src) const
{
const_cast<QGLContext *>(QGLContext::currentContext())->d_func()->bindTexture(src, GL_TEXTURE_2D, GL_RGBA, QGLContext::BindOptions(QGLContext::DefaultBindOption | QGLContext::MemoryManagedBindOption));
}
void QGLPixmapFilterBase::drawImpl(QPainter *painter, const QPointF &pos, const QPixmap &src, const QRectF& source) const
{
processGL(painter, pos, src, source);
}
class QGLPixmapColorizeFilter: public QGLCustomShaderStage, public QGLPixmapFilter<QPixmapColorizeFilter>
{
public:
QGLPixmapColorizeFilter();
void setUniforms(QGLShaderProgram *program);
protected:
bool processGL(QPainter *painter, const QPointF &pos, const QPixmap &pixmap, const QRectF &srcRect) const;
};
class QGLPixmapConvolutionFilter: public QGLCustomShaderStage, public QGLPixmapFilter<QPixmapConvolutionFilter>
{
public:
QGLPixmapConvolutionFilter();
~QGLPixmapConvolutionFilter();
void setUniforms(QGLShaderProgram *program);
protected:
bool processGL(QPainter *painter, const QPointF &pos, const QPixmap &src, const QRectF &srcRect) const;
private:
QByteArray generateConvolutionShader() const;
mutable QSize m_srcSize;
mutable int m_prevKernelSize;
};
class QGLPixmapBlurFilter : public QGLCustomShaderStage, public QGLPixmapFilter<QPixmapBlurFilter>
{
public:
QGLPixmapBlurFilter(Qt::RenderHint hint);
void setUniforms(QGLShaderProgram *program);
protected:
bool processGL(QPainter *painter, const QPointF &pos, const QPixmap &src, const QRectF &srcRect) const;
private:
static QByteArray generateGaussianShader(int radius);
mutable QSize m_textureSize;
mutable bool m_horizontalBlur;
mutable bool m_haveCached;
mutable int m_cachedRadius;
mutable Qt::RenderHint m_hint;
};
extern QGLWidget *qt_gl_share_widget();
QPixmapFilter *QGL2PaintEngineEx::pixmapFilter(int type, const QPixmapFilter *prototype)
{
Q_D(QGL2PaintEngineEx);
switch (type) {
case QPixmapFilter::ColorizeFilter:
if (!d->colorizeFilter)
d->colorizeFilter.reset(new QGLPixmapColorizeFilter);
return d->colorizeFilter.data();
case QPixmapFilter::BlurFilter: {
const QPixmapBlurFilter *proto = static_cast<const QPixmapBlurFilter *>(prototype);
if (proto->blurHint() == Qt::PerformanceHint || proto->radius() <= 5) {
if (!d->fastBlurFilter)
d->fastBlurFilter.reset(new QGLPixmapBlurFilter(Qt::PerformanceHint));
return d->fastBlurFilter.data();
}
if (!d->blurFilter)
d->blurFilter.reset(new QGLPixmapBlurFilter(Qt::QualityHint));
return d->blurFilter.data();
}
case QPixmapFilter::ConvolutionFilter:
if (!d->convolutionFilter)
d->convolutionFilter.reset(new QGLPixmapConvolutionFilter);
return d->convolutionFilter.data();
default: break;
}
return QPaintEngineEx::pixmapFilter(type, prototype);
}
static const char *qt_gl_colorize_filter =
"uniform lowp vec4 colorizeColor;"
"uniform lowp float colorizeStrength;"
"lowp vec4 customShader(lowp sampler2D src, highp vec2 srcCoords)"
"{"
" lowp vec4 srcPixel = texture2D(src, srcCoords);"
" lowp float gray = dot(srcPixel.rgb, vec3(0.212671, 0.715160, 0.072169));"
" lowp vec3 colorized = 1.0-((1.0-gray)*(1.0-colorizeColor.rgb));"
" return vec4(mix(srcPixel.rgb, colorized * srcPixel.a, colorizeStrength), srcPixel.a);"
"}";
QGLPixmapColorizeFilter::QGLPixmapColorizeFilter()
{
setSource(qt_gl_colorize_filter);
}
bool QGLPixmapColorizeFilter::processGL(QPainter *painter, const QPointF &pos, const QPixmap &src, const QRectF &) const
{
QGLPixmapColorizeFilter *filter = const_cast<QGLPixmapColorizeFilter *>(this);
filter->setOnPainter(painter);
painter->drawPixmap(pos, src);
filter->removeFromPainter(painter);
return true;
}
void QGLPixmapColorizeFilter::setUniforms(QGLShaderProgram *program)
{
program->setUniformValue("colorizeColor", color());
program->setUniformValue("colorizeStrength", float(strength()));
}
void QGLPixmapConvolutionFilter::setUniforms(QGLShaderProgram *program)
{
const qreal *kernel = convolutionKernel();
int kernelWidth = columns();
int kernelHeight = rows();
int kernelSize = kernelWidth * kernelHeight;
QVarLengthArray<GLfloat> matrix(kernelSize);
QVarLengthArray<GLfloat> offset(kernelSize * 2);
for(int i = 0; i < kernelSize; ++i)
matrix[i] = kernel[i];
for(int y = 0; y < kernelHeight; ++y) {
for(int x = 0; x < kernelWidth; ++x) {
offset[(y * kernelWidth + x) * 2] = x - (kernelWidth / 2);
offset[(y * kernelWidth + x) * 2 + 1] = (kernelHeight / 2) - y;
}
}
const qreal iw = 1.0 / m_srcSize.width();
const qreal ih = 1.0 / m_srcSize.height();
program->setUniformValue("inv_texture_size", iw, ih);
program->setUniformValueArray("matrix", matrix.constData(), kernelSize, 1);
program->setUniformValueArray("offset", offset.constData(), kernelSize, 2);
}
// generates convolution filter code for arbitrary sized kernel
QByteArray QGLPixmapConvolutionFilter::generateConvolutionShader() const {
QByteArray code;
int kernelWidth = columns();
int kernelHeight = rows();
int kernelSize = kernelWidth * kernelHeight;
code.append("uniform highp vec2 inv_texture_size;\n"
"uniform mediump float matrix[");
code.append(QByteArray::number(kernelSize));
code.append("];\n"
"uniform highp vec2 offset[");
code.append(QByteArray::number(kernelSize));
code.append("];\n");
code.append("lowp vec4 customShader(lowp sampler2D src, highp vec2 srcCoords) {\n");
code.append(" int i = 0;\n"
" lowp vec4 sum = vec4(0.0);\n"
" for (i = 0; i < ");
code.append(QByteArray::number(kernelSize));
code.append("; i++) {\n"
" sum += matrix[i] * texture2D(src,srcCoords+inv_texture_size*offset[i]);\n"
" }\n"
" return sum;\n"
"}");
return code;
}
QGLPixmapConvolutionFilter::QGLPixmapConvolutionFilter()
: m_prevKernelSize(-1)
{
}
QGLPixmapConvolutionFilter::~QGLPixmapConvolutionFilter()
{
}
bool QGLPixmapConvolutionFilter::processGL(QPainter *painter, const QPointF &pos, const QPixmap &src, const QRectF &srcRect) const
{
QGLPixmapConvolutionFilter *filter = const_cast<QGLPixmapConvolutionFilter *>(this);
m_srcSize = src.size();
int kernelSize = rows() * columns();
if (m_prevKernelSize == -1 || m_prevKernelSize != kernelSize) {
filter->setSource(generateConvolutionShader());
m_prevKernelSize = kernelSize;
}
filter->setOnPainter(painter);
painter->drawPixmap(pos, src, srcRect);
filter->removeFromPainter(painter);
return true;
}
static const char *qt_gl_blur_filter_fast =
"const int samples = 9;"
"uniform mediump vec2 delta;"
"lowp vec4 customShader(lowp sampler2D src, highp vec2 srcCoords) {"
" mediump vec4 color = vec4(0.0, 0.0, 0.0, 0.0);"
" mediump float offset = (float(samples) - 1.0) / 2.0;"
" for (int i = 0; i < samples; i++) {"
" mediump vec2 coord = srcCoords + delta * (offset - float(i)) / offset;"
" color += texture2D(src, coord);"
" }"
" return color * (1.0 / float(samples));"
"}";
QGLPixmapBlurFilter::QGLPixmapBlurFilter(Qt::RenderHint hint)
: m_haveCached(false)
, m_cachedRadius(5)
, m_hint(hint)
{
if (hint == Qt::PerformanceHint) {
QGLPixmapBlurFilter *filter = const_cast<QGLPixmapBlurFilter *>(this);
filter->setSource(qt_gl_blur_filter_fast);
m_haveCached = true;
}
}
bool QGLPixmapBlurFilter::processGL(QPainter *painter, const QPointF &pos, const QPixmap &src, const QRectF &) const
{
QGLPixmapBlurFilter *filter = const_cast<QGLPixmapBlurFilter *>(this);
int radius = this->radius();
if (!m_haveCached || (m_hint == Qt::QualityHint && radius != m_cachedRadius)) {
// Only regenerate the shader from source if parameters have changed.
m_haveCached = true;
m_cachedRadius = radius;
filter->setSource(generateGaussianShader(radius));
}
QGLFramebufferObjectFormat format;
format.setInternalTextureFormat(GLenum(src.hasAlphaChannel() ? GL_RGBA : GL_RGB));
QGLFramebufferObject *fbo = qgl_fbo_pool()->acquire(src.size(), format);
if (!fbo)
return false;
glBindTexture(GL_TEXTURE_2D, fbo->texture());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
// prepare for updateUniforms
m_textureSize = src.size();
// horizontal pass, to pixmap
m_horizontalBlur = true;
QPainter fboPainter(fbo);
if (src.hasAlphaChannel()) {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
}
// ensure GL_LINEAR filtering is used
fboPainter.setRenderHint(QPainter::SmoothPixmapTransform);
filter->setOnPainter(&fboPainter);
fboPainter.drawPixmap(0, 0, src);
filter->removeFromPainter(&fboPainter);
fboPainter.end();
QGL2PaintEngineEx *engine = static_cast<QGL2PaintEngineEx *>(painter->paintEngine());
// vertical pass, to painter
m_horizontalBlur = false;
painter->save();
// ensure GL_LINEAR filtering is used
painter->setRenderHint(QPainter::SmoothPixmapTransform);
filter->setOnPainter(painter);
engine->drawTexture(src.rect().translated(pos.x(), pos.y()), fbo->texture(), fbo->size(), src.rect().translated(0, fbo->height() - src.height()));
filter->removeFromPainter(painter);
painter->restore();
qgl_fbo_pool()->release(fbo);
return true;
}
void QGLPixmapBlurFilter::setUniforms(QGLShaderProgram *program)
{
if (m_hint == Qt::QualityHint) {
if (m_horizontalBlur)
program->setUniformValue("delta", 1.0 / m_textureSize.width(), 0.0);
else
program->setUniformValue("delta", 0.0, 1.0 / m_textureSize.height());
} else {
// 1.4 is chosen to most closely match the blurriness of the gaussian blur
// at low radii
qreal blur = radius() / 1.4f;
if (m_horizontalBlur)
program->setUniformValue("delta", blur / m_textureSize.width(), 0.0);
else
program->setUniformValue("delta", 0.0, blur / m_textureSize.height());
}
}
static inline qreal gaussian(qreal dx, qreal sigma)
{
return exp(-dx * dx / (2 * sigma * sigma)) / (Q_2PI * sigma * sigma);
}
QByteArray QGLPixmapBlurFilter::generateGaussianShader(int radius)
{
Q_ASSERT(radius >= 1);
QByteArray source;
source.reserve(1000);
source.append("uniform highp vec2 delta;\n");
source.append("lowp vec4 customShader(lowp sampler2D src, highp vec2 srcCoords) {\n");
QVector<qreal> sampleOffsets;
QVector<qreal> weights;
QVector<qreal> gaussianComponents;
qreal sigma = radius / 1.65;
qreal sum = 0;
for (int i = -radius; i <= radius; ++i) {
float value = gaussian(i, sigma);
gaussianComponents << value;
sum += value;
}
// normalize
for (int i = 0; i < gaussianComponents.size(); ++i)
gaussianComponents[i] /= sum;
for (int i = 0; i < gaussianComponents.size() - 1; i += 2) {
qreal weight = gaussianComponents.at(i) + gaussianComponents.at(i + 1);
qreal offset = i - radius + gaussianComponents.at(i + 1) / weight;
sampleOffsets << offset;
weights << weight;
}
// odd size ?
if (gaussianComponents.size() & 1) {
sampleOffsets << radius;
weights << gaussianComponents.last();
}
int currentVariable = 1;
source.append(" mediump vec4 sample = vec4(0.0);\n");
source.append(" mediump vec2 coord;\n");
qreal weightSum = 0;
source.append(" mediump float c;\n");
for (int i = 0; i < sampleOffsets.size(); ++i) {
qreal delta = sampleOffsets.at(i);
++currentVariable;
QByteArray coordinate = "srcCoords";
if (delta != qreal(0)) {
coordinate.append(" + delta * float(");
coordinate.append(QByteArray::number(delta));
coordinate.append(")");
}
source.append(" coord = ");
source.append(coordinate);
source.append(";\n");
source.append(" sample += texture2D(src, coord)");
weightSum += weights.at(i);
if (weights.at(i) != qreal(1)) {
source.append(" * float(");
source.append(QByteArray::number(weights.at(i)));
source.append(");\n");
} else {
source.append(";\n");
}
}
source.append(" return ");
source.append("sample;\n");
source.append("}\n");
return source;
}
QT_END_NAMESPACE
|