Set bind options before uploading to give hints to the driver
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / qsgadaptationlayer.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qsgadaptationlayer_p.h"
43
44 #include <qmath.h>
45 #include <QtQuick/private/qsgdistancefieldutil_p.h>
46 #include <QtQuick/private/qsgdistancefieldglyphnode_p.h>
47 #include <private/qrawfont_p.h>
48 #include <private/qdistancefield_p.h>
49 #include <QtGui/qguiapplication.h>
50 #include <qdir.h>
51
52 QT_BEGIN_NAMESPACE
53
54
55 QHash<QString, QOpenGLMultiGroupSharedResource> QSGDistanceFieldGlyphCache::m_caches_data;
56
57 QSGDistanceFieldGlyphCache::QSGDistanceFieldGlyphCache(QSGDistanceFieldGlyphCacheManager *man, QOpenGLContext *c, const QRawFont &font)
58     : ctx(c)
59     , m_manager(man)
60 {
61     Q_ASSERT(font.isValid());
62     m_font = font;
63
64     m_cacheData = cacheData();
65
66     QRawFontPrivate *fontD = QRawFontPrivate::get(m_font);
67     m_glyphCount = fontD->fontEngine->glyphCount();
68
69     m_cacheData->doubleGlyphResolution = qt_fontHasNarrowOutlines(font) && m_glyphCount < QT_DISTANCEFIELD_HIGHGLYPHCOUNT;
70
71     m_referenceFont = m_font;
72     m_referenceFont.setPixelSize(QT_DISTANCEFIELD_BASEFONTSIZE(m_cacheData->doubleGlyphResolution));
73     Q_ASSERT(m_referenceFont.isValid());
74 }
75
76 QSGDistanceFieldGlyphCache::~QSGDistanceFieldGlyphCache()
77 {
78 }
79
80 QSGDistanceFieldGlyphCache::GlyphCacheData *QSGDistanceFieldGlyphCache::cacheData()
81 {
82     QString key = QString::fromLatin1("%1_%2_%3_%4")
83             .arg(m_font.familyName())
84             .arg(m_font.styleName())
85             .arg(m_font.weight())
86             .arg(m_font.style());
87     return m_caches_data[key].value<QSGDistanceFieldGlyphCache::GlyphCacheData>(ctx);
88 }
89
90 qreal QSGDistanceFieldGlyphCache::fontScale() const
91 {
92     return qreal(m_font.pixelSize()) / QT_DISTANCEFIELD_BASEFONTSIZE(m_cacheData->doubleGlyphResolution);
93 }
94
95 int QSGDistanceFieldGlyphCache::distanceFieldRadius() const
96 {
97     return QT_DISTANCEFIELD_DEFAULT_RADIUS / QT_DISTANCEFIELD_SCALE(m_cacheData->doubleGlyphResolution);
98 }
99
100 QSGDistanceFieldGlyphCache::Metrics QSGDistanceFieldGlyphCache::glyphMetrics(glyph_t glyph)
101 {
102     QHash<glyph_t, Metrics>::iterator metric = m_metrics.find(glyph);
103     if (metric == m_metrics.end()) {
104         QPainterPath path = m_font.pathForGlyph(glyph);
105         QRectF br = path.boundingRect();
106
107         Metrics m;
108         m.width = br.width();
109         m.height = br.height();
110         m.baselineX = br.x();
111         m.baselineY = -br.y();
112
113         metric = m_metrics.insert(glyph, m);
114     }
115
116     return metric.value();
117 }
118
119 QSGDistanceFieldGlyphCache::TexCoord QSGDistanceFieldGlyphCache::glyphTexCoord(glyph_t glyph) const
120 {
121     return m_cacheData->texCoords.value(glyph);
122 }
123
124 static QSGDistanceFieldGlyphCache::Texture g_emptyTexture;
125
126 const QSGDistanceFieldGlyphCache::Texture *QSGDistanceFieldGlyphCache::glyphTexture(glyph_t glyph) const
127 {
128     QHash<glyph_t, Texture*>::const_iterator it = m_cacheData->glyphTextures.find(glyph);
129     if (it == m_cacheData->glyphTextures.constEnd())
130         return &g_emptyTexture;
131     return it.value();
132 }
133
134 void QSGDistanceFieldGlyphCache::populate(const QVector<glyph_t> &glyphs)
135 {
136     QSet<glyph_t> referencedGlyphs;
137     QSet<glyph_t> newGlyphs;
138     int count = glyphs.count();
139     for (int i = 0; i < count; ++i) {
140         glyph_t glyphIndex = glyphs.at(i);
141         if ((int) glyphIndex >= glyphCount()) {
142             qWarning("Warning: distance-field glyph is not available with index %d", glyphIndex);
143             continue;
144         }
145
146         ++m_cacheData->glyphRefCount[glyphIndex];
147         referencedGlyphs.insert(glyphIndex);
148
149         if (m_cacheData->texCoords.contains(glyphIndex) || newGlyphs.contains(glyphIndex))
150             continue;
151
152         QPainterPath path = m_referenceFont.pathForGlyph(glyphIndex);
153         m_cacheData->glyphPaths.insert(glyphIndex, path);
154         if (path.isEmpty()) {
155             TexCoord c;
156             c.width = 0;
157             c.height = 0;
158             m_cacheData->texCoords.insert(glyphIndex, c);
159             continue;
160         }
161
162         newGlyphs.insert(glyphIndex);
163     }
164
165     if (newGlyphs.isEmpty())
166         return;
167
168     referenceGlyphs(referencedGlyphs);
169     requestGlyphs(newGlyphs);
170 }
171
172 void QSGDistanceFieldGlyphCache::release(const QVector<glyph_t> &glyphs)
173 {
174     QSet<glyph_t> unusedGlyphs;
175     int count = glyphs.count();
176     for (int i = 0; i < count; ++i) {
177         glyph_t glyphIndex = glyphs.at(i);
178         if (--m_cacheData->glyphRefCount[glyphIndex] == 0 && !glyphTexCoord(glyphIndex).isNull())
179             unusedGlyphs.insert(glyphIndex);
180     }
181     releaseGlyphs(unusedGlyphs);
182 }
183
184 void QSGDistanceFieldGlyphCache::update()
185 {
186     if (m_cacheData->pendingGlyphs.isEmpty())
187         return;
188
189     QHash<glyph_t, QImage> distanceFields;
190
191     // ### Remove before final release
192     static bool cacheDistanceFields = QGuiApplication::arguments().contains(QLatin1String("--cache-distance-fields"));
193
194     QString tmpPath = QString::fromLatin1("%1/.qt/").arg(QDir::tempPath());
195     QString keyBase = QString::fromLatin1("%1%2%3_%4_%5_%6.fontblob")
196             .arg(tmpPath)
197             .arg(m_font.familyName())
198             .arg(m_font.styleName())
199             .arg(m_font.weight())
200             .arg(m_font.style());
201
202     if (cacheDistanceFields && !QFile::exists(tmpPath))
203         QDir(tmpPath).mkpath(tmpPath);
204
205     for (int i = 0; i < m_cacheData->pendingGlyphs.size(); ++i) {
206         glyph_t glyphIndex = m_cacheData->pendingGlyphs.at(i);
207
208         if (cacheDistanceFields) {
209             QString key = keyBase.arg(glyphIndex);
210             QFile file(key);
211             if (file.open(QFile::ReadOnly)) {
212                 int fileSize = file.size();
213                 int dim = sqrt(float(fileSize));
214                 QByteArray blob = file.readAll();
215                 QImage df(dim, dim, QImage::Format_Indexed8);
216                 memcpy(df.bits(), blob.constData(), fileSize);
217                 distanceFields.insert(glyphIndex, df);
218                 continue;
219             }
220         }
221
222         QImage distanceField = qt_renderDistanceFieldGlyph(m_font, glyphIndex, m_cacheData->doubleGlyphResolution);
223         distanceFields.insert(glyphIndex, distanceField);
224
225         if (cacheDistanceFields) {
226             QString key = keyBase.arg(glyphIndex);
227             QFile file(key);
228             file.open(QFile::WriteOnly);
229             file.write((const char *) distanceField.constBits(), distanceField.width() * distanceField.height());
230         }
231     }
232
233     m_cacheData->pendingGlyphs.reset();
234
235     storeGlyphs(distanceFields);
236 }
237
238 void QSGDistanceFieldGlyphCache::setGlyphsPosition(const QList<GlyphPosition> &glyphs)
239 {
240     QVector<quint32> invalidatedGlyphs;
241
242     int count = glyphs.count();
243     for (int i = 0; i < count; ++i) {
244         GlyphPosition glyph = glyphs.at(i);
245
246         QPainterPath path = m_cacheData->glyphPaths.value(glyph.glyph);
247         QRectF br = path.boundingRect();
248         TexCoord c;
249         c.xMargin = QT_DISTANCEFIELD_RADIUS(m_cacheData->doubleGlyphResolution) / qreal(QT_DISTANCEFIELD_SCALE(m_cacheData->doubleGlyphResolution));
250         c.yMargin = QT_DISTANCEFIELD_RADIUS(m_cacheData->doubleGlyphResolution) / qreal(QT_DISTANCEFIELD_SCALE(m_cacheData->doubleGlyphResolution));
251         c.x = glyph.position.x();
252         c.y = glyph.position.y();
253         c.width = br.width();
254         c.height = br.height();
255
256         if (m_cacheData->texCoords.contains(glyph.glyph))
257             invalidatedGlyphs.append(glyph.glyph);
258
259         m_cacheData->texCoords.insert(glyph.glyph, c);
260     }
261
262     if (!invalidatedGlyphs.isEmpty()) {
263         QLinkedList<QSGDistanceFieldGlyphNode *>::iterator it = m_cacheData->m_registeredNodes.begin();
264         while (it != m_cacheData->m_registeredNodes.end()) {
265             (*it)->invalidateGlyphs(invalidatedGlyphs);
266             ++it;
267         }
268     }
269 }
270
271 void QSGDistanceFieldGlyphCache::registerOwnerElement(QQuickItem *ownerElement)
272 {
273     Q_UNUSED(ownerElement);
274 }
275
276 void QSGDistanceFieldGlyphCache::unregisterOwnerElement(QQuickItem *ownerElement)
277 {
278     Q_UNUSED(ownerElement);
279 }
280
281 void QSGDistanceFieldGlyphCache::processPendingGlyphs()
282 {
283     /* Intentionally empty */
284 }
285
286 void QSGDistanceFieldGlyphCache::setGlyphsTexture(const QVector<glyph_t> &glyphs, const Texture &tex)
287 {
288     int i = m_cacheData->textures.indexOf(tex);
289     if (i == -1) {
290         m_cacheData->textures.append(tex);
291         i = m_cacheData->textures.size() - 1;
292     } else {
293         m_cacheData->textures[i].size = tex.size;
294     }
295     Texture *texture = &(m_cacheData->textures[i]);
296
297     QVector<quint32> invalidatedGlyphs;
298
299     int count = glyphs.count();
300     for (int j = 0; j < count; ++j) {
301         glyph_t glyphIndex = glyphs.at(j);
302         if (m_cacheData->glyphTextures.contains(glyphIndex))
303             invalidatedGlyphs.append(glyphIndex);
304         m_cacheData->glyphTextures.insert(glyphIndex, texture);
305     }
306
307     if (!invalidatedGlyphs.isEmpty()) {
308         QLinkedList<QSGDistanceFieldGlyphNode *>::iterator it = m_cacheData->m_registeredNodes.begin();
309         while (it != m_cacheData->m_registeredNodes.end()) {
310             (*it)->invalidateGlyphs(invalidatedGlyphs);
311             ++it;
312         }
313     }
314 }
315
316 void QSGDistanceFieldGlyphCache::markGlyphsToRender(const QVector<glyph_t> &glyphs)
317 {
318     int count = glyphs.count();
319     for (int i = 0; i < count; ++i)
320         m_cacheData->pendingGlyphs.add(glyphs.at(i));
321 }
322
323 void QSGDistanceFieldGlyphCache::removeGlyph(glyph_t glyph)
324 {
325     m_cacheData->texCoords.remove(glyph);
326     m_cacheData->glyphTextures.remove(glyph);
327 }
328
329 void QSGDistanceFieldGlyphCache::updateTexture(GLuint oldTex, GLuint newTex, const QSize &newTexSize)
330 {
331     int count = m_cacheData->textures.count();
332     for (int i = 0; i < count; ++i) {
333         Texture &tex = m_cacheData->textures[i];
334         if (tex.textureId == oldTex) {
335             tex.textureId = newTex;
336             tex.size = newTexSize;
337             return;
338         }
339     }
340 }
341
342 bool QSGDistanceFieldGlyphCache::containsGlyph(glyph_t glyph) const
343 {
344     return m_cacheData->texCoords.contains(glyph);
345 }
346
347 void QSGDistanceFieldGlyphCache::registerGlyphNode(QSGDistanceFieldGlyphNode *node)
348 {
349     m_cacheData->m_registeredNodes.append(node);
350 }
351
352 void QSGDistanceFieldGlyphCache::unregisterGlyphNode(QSGDistanceFieldGlyphNode *node)
353 {
354     m_cacheData->m_registeredNodes.removeOne(node);
355 }
356
357
358 QT_END_NAMESPACE