1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qsgdefaultglyphnode_p_p.h"
44 #include <qopenglshaderprogram.h>
46 #include <QtGui/private/qopengltextureglyphcache_p.h>
47 #include <QtGui/private/qguiapplication_p.h>
48 #include <qpa/qplatformintegration.h>
49 #include <private/qfontengine_p.h>
50 #include <private/qopenglextensions_p.h>
52 #include <QtQuick/private/qsgtexture_p.h>
54 #include <private/qrawfont_p.h>
55 #include <QtCore/qmath.h>
59 class QSGTextMaskMaterialData : public QSGMaterialShader
62 QSGTextMaskMaterialData();
64 virtual void updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect);
65 virtual char const *const *attributeNames() const;
67 virtual void activate();
68 virtual void deactivate();
71 virtual void initialize();
72 virtual const char *vertexShader() const;
73 virtual const char *fragmentShader() const;
77 int m_textureScale_id;
80 const char *QSGTextMaskMaterialData::vertexShader() const {
82 "uniform highp mat4 matrix; \n"
83 "uniform highp vec2 textureScale; \n"
84 "attribute highp vec4 vCoord; \n"
85 "attribute highp vec2 tCoord; \n"
86 "varying highp vec2 sampleCoord; \n"
88 " sampleCoord = tCoord * textureScale; \n"
89 " gl_Position = matrix * vCoord; \n"
93 const char *QSGTextMaskMaterialData::fragmentShader() const {
95 "varying highp vec2 sampleCoord; \n"
96 "uniform sampler2D texture; \n"
97 "uniform lowp vec4 color; \n"
99 " gl_FragColor = vec4(texture2D(texture, sampleCoord).rgb, 1.0); \n"
103 char const *const *QSGTextMaskMaterialData::attributeNames() const
105 static char const *const attr[] = { "vCoord", "tCoord", 0 };
109 QSGTextMaskMaterialData::QSGTextMaskMaterialData()
113 void QSGTextMaskMaterialData::initialize()
115 m_matrix_id = program()->uniformLocation("matrix");
116 m_color_id = program()->uniformLocation("color");
117 m_textureScale_id = program()->uniformLocation("textureScale");
120 static inline qreal fontSmoothingGamma()
122 static qreal fontSmoothingGamma = QGuiApplicationPrivate::platformIntegration()->styleHint(QPlatformIntegration::FontSmoothingGamma).toReal();
123 return fontSmoothingGamma;
126 void QSGTextMaskMaterialData::activate()
128 QSGMaterialShader::activate();
129 glBlendFunc(GL_CONSTANT_COLOR, GL_ONE_MINUS_SRC_COLOR);
131 #if !defined(QT_OPENGL_ES_2) && defined(GL_ARB_framebuffer_sRGB)
132 // 0.25 was found to be acceptable error margin by experimentation. On Mac, the gamma is 2.0,
133 // but using sRGB looks okay.
134 if (qAbs(fontSmoothingGamma() - 2.2) < 0.25)
135 glEnable(GL_FRAMEBUFFER_SRGB);
139 void QSGTextMaskMaterialData::deactivate()
141 QSGMaterialShader::deactivate();
142 glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
144 #if !defined(QT_OPENGL_ES_2) && defined(GL_ARB_framebuffer_sRGB)
145 if (qAbs(fontSmoothingGamma() - 2.2) < 0.25)
146 glDisable(GL_FRAMEBUFFER_SRGB);
150 void QSGTextMaskMaterialData::updateState(const RenderState &state, QSGMaterial *newEffect, QSGMaterial *oldEffect)
152 Q_ASSERT(oldEffect == 0 || newEffect->type() == oldEffect->type());
153 QSGTextMaskMaterial *material = static_cast<QSGTextMaskMaterial *>(newEffect);
154 QSGTextMaskMaterial *oldMaterial = static_cast<QSGTextMaskMaterial *>(oldEffect);
156 if (oldMaterial == 0 || material->color() != oldMaterial->color() || state.isOpacityDirty()) {
157 QColor c = material->color();
158 QVector4D color(c.redF(), c.greenF(), c.blueF(), c.alphaF());
159 color *= state.opacity();
160 program()->setUniformValue(m_color_id, color);
162 if (oldMaterial == 0 || material->color() != oldMaterial->color()) {
163 state.context()->functions()->glBlendColor(c.redF(),
170 bool updated = material->ensureUpToDate();
171 Q_ASSERT(material->texture());
173 Q_ASSERT(oldMaterial == 0 || oldMaterial->texture());
176 || oldMaterial->texture()->textureId() != material->texture()->textureId()) {
177 program()->setUniformValue(m_textureScale_id, QVector2D(1.0 / material->cacheTextureWidth(),
178 1.0 / material->cacheTextureHeight()));
179 glBindTexture(GL_TEXTURE_2D, material->texture()->textureId());
181 // Set the mag/min filters to be linear. We only need to do this when the texture
182 // has been recreated.
184 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
185 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
189 if (state.isMatrixDirty())
190 program()->setUniformValue(m_matrix_id, state.combinedMatrix());
193 QSGTextMaskMaterial::QSGTextMaskMaterial(const QRawFont &font)
194 : m_texture(0), m_glyphCache(), m_font(font)
199 QSGTextMaskMaterial::~QSGTextMaskMaterial()
203 void QSGTextMaskMaterial::init()
205 Q_ASSERT(m_font.isValid());
207 QFontEngineGlyphCache::Type type = QFontEngineGlyphCache::Raster_RGBMask;
208 setFlag(Blending, true);
210 QOpenGLContext *ctx = const_cast<QOpenGLContext *>(QOpenGLContext::currentContext());
213 QRawFontPrivate *fontD = QRawFontPrivate::get(m_font);
214 if (fontD->fontEngine != 0) {
215 m_glyphCache = fontD->fontEngine->glyphCache(ctx, type, QTransform());
216 if (!m_glyphCache || m_glyphCache->cacheType() != type) {
217 m_glyphCache = new QOpenGLTextureGlyphCache(type, QTransform());
218 fontD->fontEngine->setGlyphCache(ctx, m_glyphCache.data());
223 void QSGTextMaskMaterial::populate(const QPointF &p,
224 const QVector<quint32> &glyphIndexes,
225 const QVector<QPointF> &glyphPositions,
226 QSGGeometry *geometry,
227 QRectF *boundingRect,
230 Q_ASSERT(m_font.isValid());
231 QVector<QFixedPoint> fixedPointPositions;
232 for (int i=0; i<glyphPositions.size(); ++i)
233 fixedPointPositions.append(QFixedPoint::fromPointF(glyphPositions.at(i)));
235 QTextureGlyphCache *cache = glyphCache();
237 QRawFontPrivate *fontD = QRawFontPrivate::get(m_font);
238 cache->populate(fontD->fontEngine, glyphIndexes.size(), glyphIndexes.constData(),
239 fixedPointPositions.data());
240 cache->fillInPendingGlyphs();
242 int margin = fontD->fontEngine->glyphMargin(cache->cacheType());
244 Q_ASSERT(geometry->indexType() == GL_UNSIGNED_SHORT);
245 geometry->allocate(glyphIndexes.size() * 4, glyphIndexes.size() * 6);
246 QVector4D *vp = (QVector4D *)geometry->vertexDataAsTexturedPoint2D();
247 Q_ASSERT(geometry->sizeOfVertex() == sizeof(QVector4D));
248 ushort *ip = geometry->indexDataAsUShort();
250 QPointF position(p.x(), p.y() - m_font.ascent());
251 bool supportsSubPixelPositions = fontD->fontEngine->supportsSubPixelPositions();
252 for (int i=0; i<glyphIndexes.size(); ++i) {
253 QFixed subPixelPosition;
254 if (supportsSubPixelPositions)
255 subPixelPosition = fontD->fontEngine->subPixelPositionForX(QFixed::fromReal(glyphPositions.at(i).x()));
257 QTextureGlyphCache::GlyphAndSubPixelPosition glyph(glyphIndexes.at(i), subPixelPosition);
258 const QTextureGlyphCache::Coord &c = cache->coords.value(glyph);
260 QPointF glyphPosition = glyphPositions.at(i) + position;
261 int x = qFloor(glyphPosition.x()) + c.baseLineX - margin;
262 int y = qFloor(glyphPosition.y()) - c.baseLineY - margin;
264 *boundingRect |= QRectF(x + margin, y + margin, c.w, c.h);
272 float tx2 = (c.x + c.w);
274 float ty2 = (c.y + c.h);
276 if (baseLine->isNull())
277 *baseLine = glyphPosition;
279 vp[4 * i + 0] = QVector4D(cx1, cy1, tx1, ty1);
280 vp[4 * i + 1] = QVector4D(cx2, cy1, tx2, ty1);
281 vp[4 * i + 2] = QVector4D(cx1, cy2, tx1, ty2);
282 vp[4 * i + 3] = QVector4D(cx2, cy2, tx2, ty2);
285 ip[6 * i + 0] = o + 0;
286 ip[6 * i + 1] = o + 2;
287 ip[6 * i + 2] = o + 3;
288 ip[6 * i + 3] = o + 3;
289 ip[6 * i + 4] = o + 1;
290 ip[6 * i + 5] = o + 0;
294 QSGMaterialType *QSGTextMaskMaterial::type() const
296 static QSGMaterialType type;
300 QOpenGLTextureGlyphCache *QSGTextMaskMaterial::glyphCache() const
302 return static_cast<QOpenGLTextureGlyphCache*>(m_glyphCache.data());
305 QSGMaterialShader *QSGTextMaskMaterial::createShader() const
307 return new QSGTextMaskMaterialData;
310 int QSGTextMaskMaterial::compare(const QSGMaterial *o) const
312 Q_ASSERT(o && type() == o->type());
313 const QSGTextMaskMaterial *other = static_cast<const QSGTextMaskMaterial *>(o);
314 if (m_glyphCache != other->m_glyphCache)
315 return m_glyphCache - other->m_glyphCache;
316 QRgb c1 = m_color.rgba();
317 QRgb c2 = other->m_color.rgba();
318 return int(c2 < c1) - int(c1 < c2);
321 bool QSGTextMaskMaterial::ensureUpToDate()
323 QSize glyphCacheSize(glyphCache()->width(), glyphCache()->height());
324 if (glyphCacheSize != m_size) {
327 m_texture = new QSGPlainTexture();
328 m_texture->setTextureId(glyphCache()->texture());
329 m_texture->setTextureSize(QSize(glyphCache()->width(), glyphCache()->height()));
330 m_texture->setOwnsTexture(false);
332 m_size = glyphCacheSize;
340 int QSGTextMaskMaterial::cacheTextureWidth() const
342 return glyphCache()->width();
345 int QSGTextMaskMaterial::cacheTextureHeight() const
347 return glyphCache()->height();