}
int distanceFieldRadius() const
{
- return QT_DISTANCEFIELD_DEFAULT_RADIUS / QT_DISTANCEFIELD_SCALE(m_doubleGlyphResolution);
+ return QT_DISTANCEFIELD_RADIUS(m_doubleGlyphResolution) / QT_DISTANCEFIELD_SCALE(m_doubleGlyphResolution);
}
int glyphCount() const { return m_glyphCount; }
bool doubleGlyphResolution() const { return m_doubleGlyphResolution; }
QPointF position;
};
+ struct GlyphData {
+ Texture *texture;
+ TexCoord texCoord;
+ QRectF boundingRect;
+ quint32 ref;
+
+ GlyphData() : texture(0), ref(0) { }
+ };
+
virtual void requestGlyphs(const QSet<glyph_t> &glyphs) = 0;
virtual void storeGlyphs(const QHash<glyph_t, QImage> &glyphs) = 0;
virtual void referenceGlyphs(const QSet<glyph_t> &glyphs) = 0;
inline bool containsGlyph(glyph_t glyph);
GLuint textureIdForGlyph(glyph_t glyph) const;
+ GlyphData &glyphData(glyph_t glyph);
+
QOpenGLContext *ctx;
private:
- struct GlyphData {
- Texture *texture;
- TexCoord texCoord;
- QRectF boundingRect;
- quint32 ref;
-
- GlyphData() : texture(0), ref(0) { }
- };
-
- GlyphData &glyphData(glyph_t glyph);
-
QSGDistanceFieldGlyphCacheManager *m_manager;
QRawFont m_referenceFont;
QList<Texture> m_textures;
QHash<glyph_t, GlyphData> m_glyphsData;
QDataBuffer<glyph_t> m_pendingGlyphs;
+ QSet<glyph_t> m_populatingGlyphs;
QLinkedList<QSGDistanceFieldGlyphConsumer*> m_registeredNodes;
static Texture s_emptyTexture;
#include <QtGui/private/qdistancefield_p.h>
#include <QtQuick/private/qsgdistancefieldutil_p.h>
#include <qopenglfunctions.h>
+#include <qmath.h>
QT_BEGIN_NAMESPACE
, m_fbo(0)
, m_blitProgram(0)
{
- m_currentTexture = createTextureInfo();
-
m_blitVertexCoordinateArray[0] = -1.0f;
m_blitVertexCoordinateArray[1] = -1.0f;
m_blitVertexCoordinateArray[2] = 1.0f;
m_blitTextureCoordinateArray[5] = 1.0f;
m_blitTextureCoordinateArray[6] = 0.0f;
m_blitTextureCoordinateArray[7] = 1.0f;
+
+ m_areaAllocator = new QSGAreaAllocator(QSize(maxTextureSize(), m_maxTextureCount * maxTextureSize()));
}
QSGDefaultDistanceFieldGlyphCache::~QSGDefaultDistanceFieldGlyphCache()
glDeleteTextures(1, &m_textures[i].texture);
ctx->functions()->glDeleteFramebuffers(1, &m_fbo);
delete m_blitProgram;
+ delete m_areaAllocator;
}
void QSGDefaultDistanceFieldGlyphCache::requestGlyphs(const QSet<glyph_t> &glyphs)
for (QSet<glyph_t>::const_iterator it = glyphs.constBegin(); it != glyphs.constEnd() ; ++it) {
glyph_t glyphIndex = *it;
- if (cacheIsFull() && m_unusedGlyphs.isEmpty())
- continue;
-
- if (textureIsFull(m_currentTexture) && m_textures.count() < m_maxTextureCount)
- m_currentTexture = createTextureInfo();
-
- m_unusedGlyphs.remove(glyphIndex);
-
- TextureInfo *tex = m_currentTexture;
+ int glyphWidth = qCeil(glyphData(glyphIndex).boundingRect.width()) + distanceFieldRadius() * 2;
+ QSize glyphSize(glyphWidth, QT_DISTANCEFIELD_TILESIZE(doubleGlyphResolution()));
+ QRect alloc = m_areaAllocator->allocate(glyphSize);
- GlyphPosition p;
- p.glyph = glyphIndex;
- p.position = QPointF(tex->currX, tex->currY);
-
- if (!cacheIsFull()) {
- tex->currX += QT_DISTANCEFIELD_TILESIZE(doubleGlyphResolution());
- if (tex->currX >= maxTextureSize()) {
- tex->currX = 0;
- tex->currY += QT_DISTANCEFIELD_TILESIZE(doubleGlyphResolution());
- }
- } else {
- // Recycle glyphs
- if (!m_unusedGlyphs.isEmpty()) {
+ if (alloc.isNull()) {
+ // Unallocate unused glyphs until we can allocated the new glyph
+ while (alloc.isNull() && !m_unusedGlyphs.isEmpty()) {
glyph_t unusedGlyph = *m_unusedGlyphs.constBegin();
+
TexCoord unusedCoord = glyphTexCoord(unusedGlyph);
- tex = m_glyphsTexture.value(unusedGlyph);
- p.position = QPointF(unusedCoord.x, unusedCoord.y);
+ int unusedGlyphWidth = qCeil(glyphData(unusedGlyph).boundingRect.width()) + distanceFieldRadius() * 2;
+ m_areaAllocator->deallocate(QRect(unusedCoord.x, unusedCoord.y, unusedGlyphWidth, QT_DISTANCEFIELD_TILESIZE(doubleGlyphResolution())));
+
m_unusedGlyphs.remove(unusedGlyph);
m_glyphsTexture.remove(unusedGlyph);
removeGlyph(unusedGlyph);
+
+ alloc = m_areaAllocator->allocate(glyphSize);
}
- }
- if (p.position.y() < maxTextureSize()) {
- glyphPositions.append(p);
- glyphsToRender.append(glyphIndex);
- m_glyphsTexture.insert(glyphIndex, tex);
+ // Not enough space left for this glyph... skip to the next one
+ if (alloc.isNull())
+ continue;
}
+
+ TextureInfo *tex = textureInfo(alloc.y() / maxTextureSize());
+ alloc = QRect(alloc.x(), alloc.y() % maxTextureSize(), alloc.width(), alloc.height());
+ tex->allocatedArea |= alloc;
+
+ GlyphPosition p;
+ p.glyph = glyphIndex;
+ p.position = alloc.topLeft();
+
+ glyphPositions.append(p);
+ glyphsToRender.append(glyphIndex);
+ m_glyphsTexture.insert(glyphIndex, tex);
}
setGlyphsPosition(glyphPositions);
void QSGDefaultDistanceFieldGlyphCache::storeGlyphs(const QHash<glyph_t, QImage> &glyphs)
{
- int requiredWidth = maxTextureSize();
- int rows = 128 / (requiredWidth / QT_DISTANCEFIELD_TILESIZE(doubleGlyphResolution())); // Enough rows to fill the latin1 set by default..
-
QHash<TextureInfo *, QVector<glyph_t> > glyphTextures;
QHash<glyph_t, QImage>::const_iterator it;
TexCoord c = glyphTexCoord(glyphIndex);
TextureInfo *texInfo = m_glyphsTexture.value(glyphIndex);
- int requiredHeight = qMin(maxTextureSize(),
- qMax(texInfo->currY + QT_DISTANCEFIELD_TILESIZE(doubleGlyphResolution()),
- QT_DISTANCEFIELD_TILESIZE(doubleGlyphResolution()) * rows));
-
- resizeTexture(texInfo, requiredWidth, requiredHeight);
+ resizeTexture(texInfo, texInfo->allocatedArea.width(), texInfo->allocatedArea.height());
glBindTexture(GL_TEXTURE_2D, texInfo->texture);
glyphTextures[texInfo].append(glyphIndex);
QImage glyph = it.value();
+ int expectedWidth = qCeil(c.width + c.xMargin * 2);
+ if (glyph.width() != expectedWidth)
+ glyph = glyph.copy(0, 0, expectedWidth, glyph.height());
if (useWorkaroundBrokenFBOReadback()) {
uchar *inBits = glyph.scanLine(0);
#include <QtGui/qopenglfunctions.h>
#include <qopenglshaderprogram.h>
#include <QtGui/private/qopenglengineshadersource_p.h>
+#include <private/qsgareaallocator_p.h>
QT_BEGIN_NAMESPACE
void referenceGlyphs(const QSet<glyph_t> &glyphs);
void releaseGlyphs(const QSet<glyph_t> &glyphs);
- bool cacheIsFull() const {
- return m_textures.count() == m_maxTextureCount
- && textureIsFull(m_currentTexture);
- }
bool useWorkaroundBrokenFBOReadback() const;
int maxTextureSize() const;
struct TextureInfo {
GLuint texture;
QSize size;
- int currX;
- int currY;
+ QRect allocatedArea;
QImage image;
- TextureInfo() : texture(0), currX(0), currY(0)
+ TextureInfo() : texture(0)
{ }
};
void createTexture(TextureInfo * texInfo, int width, int height);
void resizeTexture(TextureInfo * texInfo, int width, int height);
- bool textureIsFull (const TextureInfo *tex) const { return tex->currY >= maxTextureSize(); }
- TextureInfo *createTextureInfo()
+ TextureInfo *textureInfo(int index)
{
- m_textures.append(TextureInfo());
- return &m_textures.last();
+ for (int i = m_textures.count(); i <= index; ++i)
+ m_textures.append(TextureInfo());
+
+ return &m_textures[index];
}
void createBlitProgram()
mutable int m_maxTextureSize;
int m_maxTextureCount;
- TextureInfo *m_currentTexture;
QList<TextureInfo> m_textures;
QHash<glyph_t, TextureInfo *> m_glyphsTexture;
GLuint m_fbo;
QSet<glyph_t> m_unusedGlyphs;
+ QSGAreaAllocator *m_areaAllocator;
+
QOpenGLShaderProgram *m_blitProgram;
GLfloat m_blitVertexCoordinateArray[8];
GLfloat m_blitTextureCoordinateArray[8];