diff options
author | Laszlo Agocs <laszlo.agocs@qt.io> | 2020-05-06 15:43:36 +0200 |
---|---|---|
committer | Laszlo Agocs <laszlo.agocs@qt.io> | 2020-06-02 15:44:25 +0200 |
commit | 2efc801a13c775cd2377001fe3362e94edc0e97b (patch) | |
tree | e01b814e07d095907bb019b041ec36dc68c057dc /src | |
parent | f15f4704c4a196ad44331b66a3d1d703f168428e (diff) |
Remove layer and dist.field glyph cache OpenGL versions
Task-number: QTBUG-79268
Change-Id: I16123a8a49d17e3ffdd5cbcfbebcd8bfa46e648c
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/quick/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/quick/scenegraph/qsgdefaultcontext.cpp | 7 | ||||
-rw-r--r-- | src/quick/scenegraph/qsgdefaultrendercontext.cpp | 6 | ||||
-rw-r--r-- | src/quick/scenegraph/qsgopengldistancefieldglyphcache.cpp | 814 | ||||
-rw-r--r-- | src/quick/scenegraph/qsgopengldistancefieldglyphcache_p.h | 164 | ||||
-rw-r--r-- | src/quick/scenegraph/qsgopengllayer.cpp | 481 | ||||
-rw-r--r-- | src/quick/scenegraph/qsgopengllayer_p.h | 161 | ||||
-rw-r--r-- | src/quick/scenegraph/scenegraph.pri | 8 |
8 files changed, 4 insertions, 1639 deletions
diff --git a/src/quick/CMakeLists.txt b/src/quick/CMakeLists.txt index 19493a6510..ad559cbf0b 100644 --- a/src/quick/CMakeLists.txt +++ b/src/quick/CMakeLists.txt @@ -397,8 +397,6 @@ qt_extend_target(Quick CONDITION QT_FEATURE_opengl OR QT_FEATURE_opengles2 OR QT scenegraph/qsgdefaultrendercontext.cpp scenegraph/qsgdefaultrendercontext_p.h scenegraph/qsgdistancefieldglyphnode.cpp scenegraph/qsgdistancefieldglyphnode_p.cpp scenegraph/qsgdistancefieldglyphnode_p.h scenegraph/qsgdistancefieldglyphnode_p_p.h - scenegraph/qsgopengldistancefieldglyphcache.cpp scenegraph/qsgopengldistancefieldglyphcache_p.h - scenegraph/qsgopengllayer.cpp scenegraph/qsgopengllayer_p.h scenegraph/qsgrhidistancefieldglyphcache.cpp scenegraph/qsgrhidistancefieldglyphcache_p.h scenegraph/qsgrhilayer.cpp scenegraph/qsgrhilayer_p.h scenegraph/qsgrhishadereffectnode.cpp scenegraph/qsgrhishadereffectnode_p.h diff --git a/src/quick/scenegraph/qsgdefaultcontext.cpp b/src/quick/scenegraph/qsgdefaultcontext.cpp index f7701d8f7f..5204c3ecfc 100644 --- a/src/quick/scenegraph/qsgdefaultcontext.cpp +++ b/src/quick/scenegraph/qsgdefaultcontext.cpp @@ -45,7 +45,6 @@ #include <QtQuick/private/qsgdefaultglyphnode_p.h> #include <QtQuick/private/qsgdistancefieldglyphnode_p.h> #include <QtQuick/private/qsgdistancefieldglyphnode_p_p.h> -#include <QtQuick/private/qsgopengllayer_p.h> #include <QtQuick/private/qsgrhisupport_p.h> #include <QtQuick/private/qsgrhilayer_p.h> #include <QtQuick/private/qsgdefaultrendercontext_p.h> @@ -222,11 +221,7 @@ QSGGlyphNode *QSGDefaultContext::createGlyphNode(QSGRenderContext *rc, bool pref QSGLayer *QSGDefaultContext::createLayer(QSGRenderContext *renderContext) { - auto rc = static_cast<const QSGDefaultRenderContext *>(renderContext); - if (rc->rhi()) - return new QSGRhiLayer(renderContext); - else - return new QSGOpenGLLayer(renderContext); + return new QSGRhiLayer(renderContext); } QSurfaceFormat QSGDefaultContext::defaultSurfaceFormat() const diff --git a/src/quick/scenegraph/qsgdefaultrendercontext.cpp b/src/quick/scenegraph/qsgdefaultrendercontext.cpp index a4ce8be967..be110b5de5 100644 --- a/src/quick/scenegraph/qsgdefaultrendercontext.cpp +++ b/src/quick/scenegraph/qsgdefaultrendercontext.cpp @@ -50,7 +50,6 @@ #include <QtQuick/private/qsgopenglatlastexture_p.h> #include <QtQuick/private/qsgcompressedtexture_p.h> -#include <QtQuick/private/qsgopengldistancefieldglyphcache_p.h> QT_BEGIN_NAMESPACE @@ -437,10 +436,7 @@ QSGDistanceFieldGlyphCache *QSGDefaultRenderContext::distanceFieldGlyphCache(con QString key = fontKey(font); QSGDistanceFieldGlyphCache *cache = m_glyphCaches.value(key, 0); if (!cache) { - if (m_rhi) - cache = new QSGRhiDistanceFieldGlyphCache(m_rhi, font); - else - cache = new QSGOpenGLDistanceFieldGlyphCache(openglContext(), font); + cache = new QSGRhiDistanceFieldGlyphCache(m_rhi, font); m_glyphCaches.insert(key, cache); } diff --git a/src/quick/scenegraph/qsgopengldistancefieldglyphcache.cpp b/src/quick/scenegraph/qsgopengldistancefieldglyphcache.cpp deleted file mode 100644 index 04dd6d64ad..0000000000 --- a/src/quick/scenegraph/qsgopengldistancefieldglyphcache.cpp +++ /dev/null @@ -1,814 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2018 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "qsgopengldistancefieldglyphcache_p.h" - -#include <QtCore/qelapsedtimer.h> -#include <QtCore/qbuffer.h> -#include <QtCore/qendian.h> -#include <QtQml/qqmlfile.h> - -#include <QtGui/private/qdistancefield_p.h> -#include <private/qopenglcontext_p.h> -#include <QtQml/private/qqmlglobal_p.h> -#include <qopenglfunctions.h> -#include <qopenglversionfunctionsfactory.h> -#include <qopenglframebufferobject.h> -#include <qmath.h> -#include "qsgcontext_p.h" - - -#if !QT_CONFIG(opengles2) -#include <qopenglfunctions_3_2_core.h> -#endif - -QT_BEGIN_NAMESPACE - -DEFINE_BOOL_CONFIG_OPTION(qmlUseGlyphCacheWorkaround, QML_USE_GLYPHCACHE_WORKAROUND) -DEFINE_BOOL_CONFIG_OPTION(qsgPreferFullSizeGlyphCacheTextures, QSG_PREFER_FULLSIZE_GLYPHCACHE_TEXTURES) - -#if !defined(QSG_OPENGL_DISTANCEFIELD_GLYPH_CACHE_PADDING) -# define QSG_OPENGL_DISTANCEFIELD_GLYPH_CACHE_PADDING 2 -#endif - -QSGOpenGLDistanceFieldGlyphCache::QSGOpenGLDistanceFieldGlyphCache(QOpenGLContext *c, - const QRawFont &font) - : QSGDistanceFieldGlyphCache(font) - , m_maxTextureWidth(0) - , m_maxTextureHeight(0) - , m_maxTextureCount(3) - , m_areaAllocator(nullptr) - , m_blitProgram(nullptr) - , m_blitBuffer(QOpenGLBuffer::VertexBuffer) - , m_fboGuard(nullptr) - , m_funcs(c->functions()) -#if !QT_CONFIG(opengles2) - , m_coreFuncs(nullptr) -#endif -{ - if (Q_LIKELY(m_blitBuffer.create())) { - m_blitBuffer.bind(); - static const GLfloat buffer[16] = {-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, - 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f}; - m_blitBuffer.allocate(buffer, sizeof(buffer)); - m_blitBuffer.release(); - } else { - qWarning("Buffer creation failed"); - } - - m_coreProfile = (c->format().profile() == QSurfaceFormat::CoreProfile); - - // Load a pregenerated cache if the font contains one - loadPregeneratedCache(font); -} - -QSGOpenGLDistanceFieldGlyphCache::~QSGOpenGLDistanceFieldGlyphCache() -{ - for (int i = 0; i < m_textures.count(); ++i) - m_funcs->glDeleteTextures(1, &m_textures[i].texture); - - if (m_fboGuard != nullptr) - m_fboGuard->free(); - - delete m_blitProgram; - delete m_areaAllocator; -} - -void QSGOpenGLDistanceFieldGlyphCache::requestGlyphs(const QSet<glyph_t> &glyphs) -{ - QList<GlyphPosition> glyphPositions; - QVector<glyph_t> glyphsToRender; - - const int padding = QSG_OPENGL_DISTANCEFIELD_GLYPH_CACHE_PADDING; - const qreal scaleFactor = qreal(1) / QT_DISTANCEFIELD_SCALE(m_doubleGlyphResolution); - - if (m_maxTextureHeight == 0) { - m_funcs->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &m_maxTextureWidth); - - // We need to add a buffer to avoid glyphs that overlap the border between two - // textures causing the height of the textures to extend beyond the limit. - m_maxTextureHeight = m_maxTextureWidth - (qCeil(m_referenceFont.pixelSize() * scaleFactor) + distanceFieldRadius() * 2 + padding * 2); - } - - if (m_areaAllocator == nullptr) - m_areaAllocator = new QSGAreaAllocator(QSize(m_maxTextureWidth, m_maxTextureCount * m_maxTextureHeight)); - - for (QSet<glyph_t>::const_iterator it = glyphs.constBegin(); it != glyphs.constEnd() ; ++it) { - glyph_t glyphIndex = *it; - - QRectF boundingRect = glyphData(glyphIndex).boundingRect; - int glyphWidth = qCeil(boundingRect.width()) + distanceFieldRadius() * 2; - int glyphHeight = qCeil(boundingRect.height()) + distanceFieldRadius() * 2; - QSize glyphSize(glyphWidth + padding * 2, glyphHeight + padding * 2); - QRect alloc = m_areaAllocator->allocate(glyphSize); - - 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); - QRectF unusedGlyphBoundingRect = glyphData(unusedGlyph).boundingRect; - int unusedGlyphWidth = qCeil(unusedGlyphBoundingRect.width()) + distanceFieldRadius() * 2; - int unusedGlyphHeight = qCeil(unusedGlyphBoundingRect.height()) + distanceFieldRadius() * 2; - m_areaAllocator->deallocate(QRect(unusedCoord.x - padding, - unusedCoord.y - padding, - padding * 2 + unusedGlyphWidth, - padding * 2 + unusedGlyphHeight)); - - m_unusedGlyphs.remove(unusedGlyph); - m_glyphsTexture.remove(unusedGlyph); - removeGlyph(unusedGlyph); - - alloc = m_areaAllocator->allocate(glyphSize); - } - - // Not enough space left for this glyph... skip to the next one - if (alloc.isNull()) - continue; - } - - TextureInfo *tex = textureInfo(alloc.y() / m_maxTextureHeight); - alloc = QRect(alloc.x(), alloc.y() % m_maxTextureHeight, alloc.width(), alloc.height()); - - tex->allocatedArea |= alloc; - Q_ASSERT(tex->padding == padding || tex->padding < 0); - tex->padding = padding; - - GlyphPosition p; - p.glyph = glyphIndex; - p.position = alloc.topLeft() + QPoint(padding, padding); - - glyphPositions.append(p); - glyphsToRender.append(glyphIndex); - m_glyphsTexture.insert(glyphIndex, tex); - } - - setGlyphsPosition(glyphPositions); - markGlyphsToRender(glyphsToRender); -} - -void QSGOpenGLDistanceFieldGlyphCache::storeGlyphs(const QList<QDistanceField> &glyphs) -{ - typedef QHash<TextureInfo *, QVector<glyph_t> > GlyphTextureHash; - typedef GlyphTextureHash::const_iterator GlyphTextureHashConstIt; - - GlyphTextureHash glyphTextures; - - GLint alignment = 4; // default value - m_funcs->glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment); - - // Distance field data is always tightly packed - m_funcs->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - - for (int i = 0; i < glyphs.size(); ++i) { - QDistanceField glyph = glyphs.at(i); - glyph_t glyphIndex = glyph.glyph(); - TexCoord c = glyphTexCoord(glyphIndex); - TextureInfo *texInfo = m_glyphsTexture.value(glyphIndex); - - resizeTexture(texInfo, texInfo->allocatedArea.width(), texInfo->allocatedArea.height()); - m_funcs->glBindTexture(GL_TEXTURE_2D, texInfo->texture); - - glyphTextures[texInfo].append(glyphIndex); - - int padding = texInfo->padding; - int expectedWidth = qCeil(c.width + c.xMargin * 2); - glyph = glyph.copy(-padding, -padding, - expectedWidth + padding * 2, glyph.height() + padding * 2); - - if (useTextureResizeWorkaround()) { - uchar *inBits = glyph.scanLine(0); - uchar *outBits = texInfo->image.scanLine(int(c.y) - padding) + int(c.x) - padding; - for (int y = 0; y < glyph.height(); ++y) { - memcpy(outBits, inBits, glyph.width()); - inBits += glyph.width(); - outBits += texInfo->image.width(); - } - } - -#if !QT_CONFIG(opengles2) - const GLenum format = isCoreProfile() ? GL_RED : GL_ALPHA; -#else - const GLenum format = GL_ALPHA; -#endif - if (useTextureUploadWorkaround()) { - for (int i = 0; i < glyph.height(); ++i) { - m_funcs->glTexSubImage2D(GL_TEXTURE_2D, 0, - c.x - padding, c.y + i - padding, glyph.width(),1, - format, GL_UNSIGNED_BYTE, - glyph.scanLine(i)); - } - } else { - m_funcs->glTexSubImage2D(GL_TEXTURE_2D, 0, - c.x - padding, c.y - padding, glyph.width(), glyph.height(), - format, GL_UNSIGNED_BYTE, - glyph.constBits()); - } - } - - // restore to previous alignment - m_funcs->glPixelStorei(GL_UNPACK_ALIGNMENT, alignment); - - for (GlyphTextureHashConstIt i = glyphTextures.constBegin(), cend = glyphTextures.constEnd(); i != cend; ++i) { - Texture t; - t.textureId = i.key()->texture; - t.size = i.key()->size; - t.rhiBased = false; - setGlyphsTexture(i.value(), t); - } -} - -void QSGOpenGLDistanceFieldGlyphCache::referenceGlyphs(const QSet<glyph_t> &glyphs) -{ - m_unusedGlyphs -= glyphs; -} - -void QSGOpenGLDistanceFieldGlyphCache::releaseGlyphs(const QSet<glyph_t> &glyphs) -{ - m_unusedGlyphs += glyphs; -} - -void QSGOpenGLDistanceFieldGlyphCache::createTexture(TextureInfo *texInfo, - int width, - int height) -{ - QByteArray zeroBuf(width * height, 0); - createTexture(texInfo, width, height, zeroBuf.constData()); -} - -void QSGOpenGLDistanceFieldGlyphCache::createTexture(TextureInfo *texInfo, - int width, - int height, - const void *pixels) -{ - if (useTextureResizeWorkaround() && texInfo->image.isNull()) { - texInfo->image = QDistanceField(width, height); - memcpy(texInfo->image.bits(), pixels, width * height); - } - - while (m_funcs->glGetError() != GL_NO_ERROR) { } - - m_funcs->glGenTextures(1, &texInfo->texture); - m_funcs->glBindTexture(GL_TEXTURE_2D, texInfo->texture); - - m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); -#if !QT_CONFIG(opengles2) - if (!QOpenGLContext::currentContext()->isOpenGLES()) - m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); - const GLint internalFormat = isCoreProfile() ? GL_R8 : GL_ALPHA; - const GLenum format = isCoreProfile() ? GL_RED : GL_ALPHA; -#else - const GLint internalFormat = GL_ALPHA; - const GLenum format = GL_ALPHA; -#endif - - m_funcs->glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, width, height, 0, format, GL_UNSIGNED_BYTE, pixels); - - texInfo->size = QSize(width, height); - - GLuint error = m_funcs->glGetError(); - if (error != GL_NO_ERROR) { - m_funcs->glBindTexture(GL_TEXTURE_2D, 0); - m_funcs->glDeleteTextures(1, &texInfo->texture); - texInfo->texture = 0; - } - -} - -static void freeFramebufferFunc(QOpenGLFunctions *funcs, GLuint id) -{ - funcs->glDeleteFramebuffers(1, &id); -} - -void QSGOpenGLDistanceFieldGlyphCache::resizeTexture(TextureInfo *texInfo, int width, int height) -{ - QOpenGLContext *ctx = QOpenGLContext::currentContext(); - Q_ASSERT(ctx); - - int oldWidth = texInfo->size.width(); - int oldHeight = texInfo->size.height(); - if (width == oldWidth && height == oldHeight) - return; - - GLuint oldTexture = texInfo->texture; - createTexture(texInfo, width, height); - - if (!oldTexture) - return; - - updateTexture(oldTexture, texInfo->texture, texInfo->size); - -#if !QT_CONFIG(opengles2) - if (isCoreProfile() && !useTextureResizeWorkaround()) { - // For an OpenGL Core Profile we can use http://www.opengl.org/wiki/Framebuffer#Blitting - // to efficiently copy the contents of the old texture to the new texture - // TODO: Use ARB_copy_image if available of if we have >=4.3 context - if (!m_coreFuncs) { - m_coreFuncs = QOpenGLVersionFunctionsFactory::get<QOpenGLFunctions_3_2_Core>(ctx); - Q_ASSERT(m_coreFuncs); - m_coreFuncs->initializeOpenGLFunctions(); - } - - // Create a framebuffer object to which we can attach our old and new textures (to - // the first two color buffer attachment points) - if (!m_fboGuard) { - GLuint fbo; - m_coreFuncs->glGenFramebuffers(1, &fbo); - m_fboGuard = new QOpenGLSharedResourceGuard(ctx, fbo, freeFramebufferFunc); - } - - // Bind the FBO to both the GL_READ_FRAMEBUFFER? and GL_DRAW_FRAMEBUFFER targets - m_coreFuncs->glBindFramebuffer(GL_FRAMEBUFFER, m_fboGuard->id()); - - // Bind the old texture to GL_COLOR_ATTACHMENT0 - m_coreFuncs->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - GL_TEXTURE_2D, oldTexture, 0); - - // Bind the new texture to GL_COLOR_ATTACHMENT1 - m_coreFuncs->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, - GL_TEXTURE_2D, texInfo->texture, 0); - - // Set the source and destination buffers - m_coreFuncs->glReadBuffer(GL_COLOR_ATTACHMENT0); - m_coreFuncs->glDrawBuffer(GL_COLOR_ATTACHMENT1); - - // Do the blit - m_coreFuncs->glBlitFramebuffer(0, 0, oldWidth, oldHeight, - 0, 0, oldWidth, oldHeight, - GL_COLOR_BUFFER_BIT, GL_NEAREST); - - // Reset the default framebuffer - QOpenGLFramebufferObject::bindDefault(); - - return; - } else if (useTextureResizeWorkaround()) { -#else - if (useTextureResizeWorkaround()) { -#endif - GLint alignment = 4; // default value - m_funcs->glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment); - m_funcs->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - -#if !QT_CONFIG(opengles2) - const GLenum format = isCoreProfile() ? GL_RED : GL_ALPHA; -#else - const GLenum format = GL_ALPHA; -#endif - - if (useTextureUploadWorkaround()) { - for (int i = 0; i < texInfo->image.height(); ++i) { - m_funcs->glTexSubImage2D(GL_TEXTURE_2D, 0, - 0, i, oldWidth, 1, - format, GL_UNSIGNED_BYTE, - texInfo->image.scanLine(i)); - } - } else { - m_funcs->glTexSubImage2D(GL_TEXTURE_2D, 0, - 0, 0, oldWidth, oldHeight, - format, GL_UNSIGNED_BYTE, - texInfo->image.constBits()); - } - - m_funcs->glPixelStorei(GL_UNPACK_ALIGNMENT, alignment); // restore to previous value - - texInfo->image = texInfo->image.copy(0, 0, width, height); - m_funcs->glDeleteTextures(1, &oldTexture); - return; - } - - if (!m_blitProgram) - createBlitProgram(); - - Q_ASSERT(m_blitProgram); - - if (!m_fboGuard) { - GLuint fbo; - m_funcs->glGenFramebuffers(1, &fbo); - m_fboGuard = new QOpenGLSharedResourceGuard(ctx, fbo, freeFramebufferFunc); - } - m_funcs->glBindFramebuffer(GL_FRAMEBUFFER, m_fboGuard->id()); - - GLuint tmp_texture; - m_funcs->glGenTextures(1, &tmp_texture); - m_funcs->glBindTexture(GL_TEXTURE_2D, tmp_texture); - m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); -#if !QT_CONFIG(opengles2) - if (!ctx->isOpenGLES()) - m_funcs->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0); -#endif - m_funcs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, oldWidth, oldHeight, 0, - GL_RGBA, GL_UNSIGNED_BYTE, nullptr); - m_funcs->glBindTexture(GL_TEXTURE_2D, 0); - m_funcs->glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - GL_TEXTURE_2D, tmp_texture, 0); - - m_funcs->glActiveTexture(GL_TEXTURE0); - m_funcs->glBindTexture(GL_TEXTURE_2D, oldTexture); - - // save current render states - GLboolean stencilTestEnabled; - GLboolean depthTestEnabled; - GLboolean scissorTestEnabled; - GLboolean blendEnabled; - GLint viewport[4]; - GLint oldProgram; - m_funcs->glGetBooleanv(GL_STENCIL_TEST, &stencilTestEnabled); - m_funcs->glGetBooleanv(GL_DEPTH_TEST, &depthTestEnabled); - m_funcs->glGetBooleanv(GL_SCISSOR_TEST, &scissorTestEnabled); - m_funcs->glGetBooleanv(GL_BLEND, &blendEnabled); - m_funcs->glGetIntegerv(GL_VIEWPORT, &viewport[0]); - m_funcs->glGetIntegerv(GL_CURRENT_PROGRAM, &oldProgram); - - m_funcs->glDisable(GL_STENCIL_TEST); - m_funcs->glDisable(GL_DEPTH_TEST); - m_funcs->glDisable(GL_SCISSOR_TEST); - m_funcs->glDisable(GL_BLEND); - - m_funcs->glViewport(0, 0, oldWidth, oldHeight); - - const bool vaoInit = m_vao.isCreated(); - if (isCoreProfile()) { - if ( !vaoInit ) - m_vao.create(); - m_vao.bind(); - } - m_blitProgram->bind(); - if (!vaoInit || !isCoreProfile()) { - m_blitBuffer.bind(); - - m_blitProgram->enableAttributeArray(int(QT_VERTEX_COORDS_ATTR)); - m_blitProgram->enableAttributeArray(int(QT_TEXTURE_COORDS_ATTR)); - m_blitProgram->setAttributeBuffer(int(QT_VERTEX_COORDS_ATTR), GL_FLOAT, 0, 2); - m_blitProgram->setAttributeBuffer(int(QT_TEXTURE_COORDS_ATTR), GL_FLOAT, 32, 2); - } - m_blitProgram->disableAttributeArray(int(QT_OPACITY_ATTR)); - m_blitProgram->setUniformValue("imageTexture", GLuint(0)); - - m_funcs->glDrawArrays(GL_TRIANGLE_FAN, 0, 4); - - m_funcs->glBindTexture(GL_TEXTURE_2D, texInfo->texture); - - if (useTextureUploadWorkaround()) { - for (int i = 0; i < oldHeight; ++i) - m_funcs->glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, i, 0, i, oldWidth, 1); - } else { - m_funcs->glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, oldWidth, oldHeight); - } - - m_funcs->glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, - GL_RENDERBUFFER, 0); - m_funcs->glDeleteTextures(1, &tmp_texture); - m_funcs->glDeleteTextures(1, &oldTexture); - - QOpenGLFramebufferObject::bindDefault(); - - // restore render states - if (stencilTestEnabled) - m_funcs->glEnable(GL_STENCIL_TEST); - if (depthTestEnabled) - m_funcs->glEnable(GL_DEPTH_TEST); - if (scissorTestEnabled) - m_funcs->glEnable(GL_SCISSOR_TEST); - if (blendEnabled) - m_funcs->glEnable(GL_BLEND); - m_funcs->glViewport(viewport[0], viewport[1], viewport[2], viewport[3]); - m_funcs->glUseProgram(oldProgram); - - m_blitProgram->disableAttributeArray(int(QT_VERTEX_COORDS_ATTR)); - m_blitProgram->disableAttributeArray(int(QT_TEXTURE_COORDS_ATTR)); - if (isCoreProfile()) - m_vao.release(); -} - -bool QSGOpenGLDistanceFieldGlyphCache::useTextureResizeWorkaround() const -{ - static bool set = false; - static bool useWorkaround = false; - if (!set) { - QOpenGLContextPrivate *ctx_p = static_cast<QOpenGLContextPrivate *>(QOpenGLContextPrivate::get(QOpenGLContext::currentContext())); - useWorkaround = ctx_p->workaround_brokenFBOReadBack - || qmlUseGlyphCacheWorkaround(); // on some hardware the workaround is faster (see QTBUG-29264) - set = true; - } - return useWorkaround; -} - -bool QSGOpenGLDistanceFieldGlyphCache::useTextureUploadWorkaround() const -{ - static bool set = false; - static bool useWorkaround = false; - if (!set) { - useWorkaround = qstrcmp(reinterpret_cast<const char*>(m_funcs->glGetString(GL_RENDERER)), - "Mali-400 MP") == 0; - set = true; - } - return useWorkaround; -} - -bool QSGOpenGLDistanceFieldGlyphCache::createFullSizeTextures() const -{ - return qsgPreferFullSizeGlyphCacheTextures() && glyphCount() > QT_DISTANCEFIELD_HIGHGLYPHCOUNT(); -} - -namespace { - struct Qtdf { - // We need these structs to be tightly packed, but some compilers we use do not - // support #pragma pack(1), so we need to hardcode the offsets/sizes in the - // file format - enum TableSize { - HeaderSize = 14, - GlyphRecordSize = 46, - TextureRecordSize = 17 - }; - - enum Offset { - // Header - majorVersion = 0, - minorVersion = 1, - pixelSize = 2, - textureSize = 4, - flags = 8, - headerPadding = 9, - numGlyphs = 10, - - // Glyph record - glyphIndex = 0, - textureOffsetX = 4, - textureOffsetY = 8, - textureWidth = 12, - textureHeight = 16, - xMargin = 20, - yMargin = 24, - boundingRectX = 28, - boundingRectY = 32, - boundingRectWidth = 36, - boundingRectHeight = 40, - textureIndex = 44, - - // Texture record - allocatedX = 0, - allocatedY = 4, - allocatedWidth = 8, - allocatedHeight = 12, - texturePadding = 16 - - }; - - template <typename T> - static inline T fetch(const char *data, Offset offset) - { - return qFromBigEndian<T>(data + int(offset)); - } - }; -} - -bool QSGOpenGLDistanceFieldGlyphCache::loadPregeneratedCache(const QRawFont &font) -{ - // The pregenerated data must be loaded first, otherwise the area allocator - // will be wrong - if (m_areaAllocator != nullptr) { - qWarning("Font cache must be loaded before cache is used"); - return false; - } - - static QElapsedTimer timer; - - bool profile = QSG_LOG_TIME_GLYPH().isDebugEnabled(); - if (profile) - timer.start(); - - QByteArray qtdfTable = font.fontTable("qtdf"); - if (qtdfTable.isEmpty()) - return false; - - typedef QHash<TextureInfo *, QVector<glyph_t> > GlyphTextureHash; - - GlyphTextureHash glyphTextures; - - if (uint(qtdfTable.size()) < Qtdf::HeaderSize) { - qWarning("Invalid qtdf table in font '%s'", - qPrintable(font.familyName())); - return false; - } - - const char *qtdfTableStart = qtdfTable.constData(); - const char *qtdfTableEnd = qtdfTableStart + qtdfTable.size(); - - int padding = 0; - int textureCount = 0; - { - quint8 majorVersion = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::majorVersion); - quint8 minorVersion = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::minorVersion); - if (majorVersion != 5 || minorVersion != 12) { - qWarning("Invalid version of qtdf table %d.%d in font '%s'", - majorVersion, - minorVersion, - qPrintable(font.familyName())); - return false; - } - - qreal pixelSize = qreal(Qtdf::fetch<quint16>(qtdfTableStart, Qtdf::pixelSize)); - m_maxTextureWidth = m_maxTextureHeight = Qtdf::fetch<quint32>(qtdfTableStart, Qtdf::textureSize); - m_doubleGlyphResolution = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::flags) == 1; - padding = Qtdf::fetch<quint8>(qtdfTableStart, Qtdf::headerPadding); - - if (pixelSize <= 0.0) { - qWarning("Invalid pixel size in '%s'", qPrintable(font.familyName())); - return false; - } - - if (m_maxTextureWidth <= 0) { - qWarning("Invalid texture size in '%s'", qPrintable(font.familyName())); - return false; - } - - int systemMaxTextureSize; - m_funcs->glGetIntegerv(GL_MAX_TEXTURE_SIZE, &systemMaxTextureSize); - - if (m_maxTextureWidth > systemMaxTextureSize) { - qWarning("System maximum texture size is %d. This is lower than the value in '%s', which is %d", - systemMaxTextureSize, - qPrintable(font.familyName()), - m_maxTextureWidth); - } - - if (padding != QSG_OPENGL_DISTANCEFIELD_GLYPH_CACHE_PADDING) { - qWarning("Padding mismatch in '%s'. Font requires %d, but Qt is compiled with %d.", - qPrintable(font.familyName()), - padding, - QSG_OPENGL_DISTANCEFIELD_GLYPH_CACHE_PADDING); - } - - m_referenceFont.setPixelSize(pixelSize); - - quint32 glyphCount = Qtdf::fetch<quint32>(qtdfTableStart, Qtdf::numGlyphs); - m_unusedGlyphs.reserve(glyphCount); - - const char *allocatorData = qtdfTableStart + Qtdf::HeaderSize; - { - m_areaAllocator = new QSGAreaAllocator(QSize(0, 0)); - allocatorData = m_areaAllocator->deserialize(allocatorData, qtdfTableEnd - allocatorData); - if (allocatorData == nullptr) - return false; - } - - if (m_areaAllocator->size().height() % m_maxTextureHeight != 0) { - qWarning("Area allocator size mismatch in '%s'", qPrintable(font.familyName())); - return false; - } - - textureCount = m_areaAllocator->size().height() / m_maxTextureHeight; - m_maxTextureCount = qMax(m_maxTextureCount, textureCount); - - const char *textureRecord = allocatorData; - for (int i = 0; i < textureCount; ++i, textureRecord += Qtdf::TextureRecordSize) { - if (textureRecord + Qtdf::TextureRecordSize > qtdfTableEnd) { - qWarning("qtdf table too small in font '%s'.", - qPrintable(font.familyName())); - return false; - } - - TextureInfo *tex = textureInfo(i); - tex->allocatedArea.setX(Qtdf::fetch<quint32>(textureRecord, Qtdf::allocatedX)); - tex->allocatedArea.setY(Qtdf::fetch<quint32>(textureRecord, Qtdf::allocatedY)); - tex->allocatedArea.setWidth(Qtdf::fetch<quint32>(textureRecord, Qtdf::allocatedWidth)); - tex->allocatedArea.setHeight(Qtdf::fetch<quint32>(textureRecord, Qtdf::allocatedHeight)); - tex->padding = Qtdf::fetch<quint8>(textureRecord, Qtdf::texturePadding); - } - - const char *glyphRecord = textureRecord; - for (quint32 i = 0; i < glyphCount; ++i, glyphRecord += Qtdf::GlyphRecordSize) { - if (glyphRecord + Qtdf::GlyphRecordSize > qtdfTableEnd) { - qWarning("qtdf table too small in font '%s'.", - qPrintable(font.familyName())); - return false; - } - - glyph_t glyph = Qtdf::fetch<quint32>(glyphRecord, Qtdf::glyphIndex); - m_unusedGlyphs.insert(glyph); - - GlyphData &glyphData = emptyData(glyph); - -#define FROM_FIXED_POINT(value) \ -(((qreal)value)/(qreal)65536) - - glyphData.texCoord.x = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureOffsetX)); - glyphData.texCoord.y = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureOffsetY)); - glyphData.texCoord.width = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureWidth)); - glyphData.texCoord.height = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::textureHeight)); - glyphData.texCoord.xMargin = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::xMargin)); - glyphData.texCoord.yMargin = FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::yMargin)); - glyphData.boundingRect.setX(FROM_FIXED_POINT(Qtdf::fetch<qint32>(glyphRecord, Qtdf::boundingRectX))); - glyphData.boundingRect.setY(FROM_FIXED_POINT(Qtdf::fetch<qint32>(glyphRecord, Qtdf::boundingRectY))); - glyphData.boundingRect.setWidth(FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::boundingRectWidth))); - glyphData.boundingRect.setHeight(FROM_FIXED_POINT(Qtdf::fetch<quint32>(glyphRecord, Qtdf::boundingRectHeight))); - -#undef FROM_FIXED_POINT - - int textureIndex = Qtdf::fetch<quint16>(glyphRecord, Qtdf::textureIndex); - if (textureIndex < 0 || textureIndex >= textureCount) { - qWarning("Invalid texture index %d (texture count == %d) in '%s'", - textureIndex, - textureCount, - qPrintable(font.familyName())); - return false; - } - - - TextureInfo *texInfo = textureInfo(textureIndex); - m_glyphsTexture.insert(glyph, texInfo); - - glyphTextures[texInfo].append(glyph); - } - - GLint alignment = 4; // default value - m_funcs->glGetIntegerv(GL_UNPACK_ALIGNMENT, &alignment); - - m_funcs->glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - - const uchar *textureData = reinterpret_cast<const uchar *>(glyphRecord); - for (int i = 0; i < textureCount; ++i) { - - TextureInfo *texInfo = textureInfo(i); - - int width = texInfo->allocatedArea.width(); - int height = texInfo->allocatedArea.height(); - qint64 size = width * height; - if (reinterpret_cast<const char *>(textureData + size) > qtdfTableEnd) { - qWarning("qtdf table too small in font '%s'.", - qPrintable(font.familyName())); - return false; - } - - createTexture(texInfo, width, height, textureData); - - QVector<glyph_t> glyphs = glyphTextures.value(texInfo); - - Texture t; - t.textureId = texInfo->texture; - t.size = texInfo->size; - t.rhiBased = false; - - setGlyphsTexture(glyphs, t); - - textureData += size; - } - - m_funcs->glPixelStorei(GL_UNPACK_ALIGNMENT, alignment); - } - - if (profile) { - quint64 now = timer.elapsed(); - qCDebug(QSG_LOG_TIME_GLYPH, - "distancefield: %d pre-generated glyphs loaded in %dms", - m_unusedGlyphs.size(), - (int) now); - } - - return true; -} - -QT_END_NAMESPACE diff --git a/src/quick/scenegraph/qsgopengldistancefieldglyphcache_p.h b/src/quick/scenegraph/qsgopengldistancefieldglyphcache_p.h deleted file mode 100644 index b095fc94a0..0000000000 --- a/src/quick/scenegraph/qsgopengldistancefieldglyphcache_p.h +++ /dev/null @@ -1,164 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef QSGOPENGLDISTANCEFIELDGLYPHCACHE_H -#define QSGOPENGLDISTANCEFIELDGLYPHCACHE_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include "qsgadaptationlayer_p.h" -#include <qopenglfunctions.h> -#include <qopenglshaderprogram.h> -#include <qopenglbuffer.h> -#include <qopenglvertexarrayobject.h> -#include <private/qopenglengineshadersource_p.h> -#include <private/qsgareaallocator_p.h> - -QT_BEGIN_NAMESPACE - -class QOpenGLSharedResourceGuard; -#if !QT_CONFIG(opengles2) -class QOpenGLFunctions_3_2_Core; -#endif - -class Q_QUICK_PRIVATE_EXPORT QSGOpenGLDistanceFieldGlyphCache : public QSGDistanceFieldGlyphCache -{ -public: - QSGOpenGLDistanceFieldGlyphCache(QOpenGLContext *c, const QRawFont &font); - virtual ~QSGOpenGLDistanceFieldGlyphCache(); - - void requestGlyphs(const QSet<glyph_t> &glyphs) override; - void storeGlyphs(const QList<QDistanceField> &glyphs) override; - void referenceGlyphs(const QSet<glyph_t> &glyphs) override; - void releaseGlyphs(const QSet<glyph_t> &glyphs) override; - - bool useTextureResizeWorkaround() const; - bool useTextureUploadWorkaround() const; - bool createFullSizeTextures() const; - - void setMaxTextureCount(int max) { m_maxTextureCount = max; } - int maxTextureCount() const { return m_maxTextureCount; } - - bool eightBitFormatIsAlphaSwizzled() const override { return !m_coreProfile; } - -private: - bool loadPregeneratedCache(const QRawFont &font); - inline bool isCoreProfile() const { return m_coreProfile; } - - struct TextureInfo { - GLuint texture; - QSize size; - QRect allocatedArea; - QDistanceField image; - int padding = -1; - - TextureInfo(const QRect &preallocRect = QRect(0, 0, 1, 1)) : texture(0), allocatedArea(preallocRect) { } - }; - - void createTexture(TextureInfo * texInfo, int width, int height, const void *pixels); - void createTexture(TextureInfo * texInfo, int width, int height); - void resizeTexture(TextureInfo * texInfo, int width, int height); - - TextureInfo *textureInfo(int index) - { - Q_ASSERT(m_maxTextureWidth > 0 && m_maxTextureHeight > 0); - for (int i = m_textures.count(); i <= index; ++i) { - if (createFullSizeTextures()) - m_textures.append(QRect(0, 0, m_maxTextureWidth, m_maxTextureWidth)); - else - m_textures.append(TextureInfo()); - } - - return &m_textures[index]; - } - - void createBlitProgram() - { - m_blitProgram = new QOpenGLShaderProgram; - { - const QString source = QLatin1String(qopenglslMainWithTexCoordsVertexShader) - + QLatin1String(qopenglslUntransformedPositionVertexShader); - - m_blitProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Vertex, source); - } - { - const QString source = QLatin1String(qopenglslMainFragmentShader) - + QLatin1String(qopenglslImageSrcFragmentShader); - - m_blitProgram->addCacheableShaderFromSourceCode(QOpenGLShader::Fragment, source); - } - m_blitProgram->bindAttributeLocation("vertexCoordsArray", QT_VERTEX_COORDS_ATTR); - m_blitProgram->bindAttributeLocation("textureCoordArray", QT_TEXTURE_COORDS_ATTR); - m_blitProgram->link(); - } - - int m_maxTextureWidth; - int m_maxTextureHeight; - int m_maxTextureCount; - bool m_coreProfile; - - QList<TextureInfo> m_textures; - QHash<glyph_t, TextureInfo *> m_glyphsTexture; - QSet<glyph_t> m_unusedGlyphs; - - QSGAreaAllocator *m_areaAllocator; - - QOpenGLShaderProgram *m_blitProgram; - QOpenGLBuffer m_blitBuffer; - QOpenGLVertexArrayObject m_vao; - - QOpenGLSharedResourceGuard *m_fboGuard; - QOpenGLFunctions *m_funcs; -#if !QT_CONFIG(opengles2) - QOpenGLFunctions_3_2_Core *m_coreFuncs; -#endif -}; - -QT_END_NAMESPACE - -#endif // QSGOPENGLDISTANCEFIELDGLYPHCACHE_H diff --git a/src/quick/scenegraph/qsgopengllayer.cpp b/src/quick/scenegraph/qsgopengllayer.cpp deleted file mode 100644 index 6f06c27459..0000000000 --- a/src/quick/scenegraph/qsgopengllayer.cpp +++ /dev/null @@ -1,481 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#include "qsgopengllayer_p.h" - -#include <private/qqmlglobal_p.h> -#include <private/qsgrenderer_p.h> -#include <private/qsgdefaultrendercontext_p.h> - -#include <QOpenGLFramebufferObject> -#include <QOpenGLFunctions> -#include <private/qopenglextensions_p.h> - -#include <QtQuick/private/qsgdepthstencilbuffer_p.h> - -#ifdef QSG_DEBUG_FBO_OVERLAY -DEFINE_BOOL_CONFIG_OPTION(qmlFboOverlay, QML_FBO_OVERLAY) -#endif -DEFINE_BOOL_CONFIG_OPTION(qmlFboFlushBeforeDetach, QML_FBO_FLUSH_BEFORE_DETACH) - -namespace -{ - class BindableFbo : public QSGBindable - { - public: - BindableFbo(QOpenGLFramebufferObject *fbo, QSGDepthStencilBuffer *depthStencil); - virtual ~BindableFbo(); - void bind() const override; - private: - QOpenGLFramebufferObject *m_fbo; - QSGDepthStencilBuffer *m_depthStencil; - }; - - BindableFbo::BindableFbo(QOpenGLFramebufferObject *fbo, QSGDepthStencilBuffer *depthStencil) - : m_fbo(fbo) - , m_depthStencil(depthStencil) - { - } - - BindableFbo::~BindableFbo() - { - if (qmlFboFlushBeforeDetach()) - QOpenGLContext::currentContext()->functions()->glFlush(); - if (m_depthStencil) - m_depthStencil->detach(); - } - - void BindableFbo::bind() const - { - m_fbo->bind(); - if (m_depthStencil) - m_depthStencil->attach(); - } -} - -QSGOpenGLLayer::QSGOpenGLLayer(QSGRenderContext *context) - : QSGLayer(*(new QSGTexturePrivate)) - , m_item(nullptr) - , m_device_pixel_ratio(1) - , m_format(GL_RGBA) - , m_renderer(nullptr) - , m_fbo(nullptr) - , m_secondaryFbo(nullptr) - , m_transparentTexture(0) -#ifdef QSG_DEBUG_FBO_OVERLAY - , m_debugOverlay(nullptr) -#endif - , m_samples(0) - , m_mipmap(false) - , m_live(true) - , m_recursive(false) - , m_dirtyTexture(true) - , m_multisamplingChecked(false) - , m_multisampling(false) - , m_grab(false) - , m_mirrorHorizontal(false) - , m_mirrorVertical(true) -{ - m_context = static_cast<QSGDefaultRenderContext *>(context); -} - -QSGOpenGLLayer::~QSGOpenGLLayer() -{ - invalidated(); -} - -void QSGOpenGLLayer::invalidated() -{ - delete m_renderer; - m_renderer = nullptr; - delete m_fbo; - delete m_secondaryFbo; - m_fbo = m_secondaryFbo = nullptr; -#ifdef QSG_DEBUG_FBO_OVERLAY - delete m_debugOverlay; - m_debugOverlay = nullptr; -#endif - if (m_transparentTexture) { - QOpenGLContext::currentContext()->functions()->glDeleteTextures(1, &m_transparentTexture); - m_transparentTexture = 0; - } -} - -int QSGOpenGLLayer::textureId() const -{ - return m_fbo ? m_fbo->texture() : 0; -} - -qint64 QSGOpenGLLayer::comparisonKey() const -{ - return m_fbo ? m_fbo->texture() : 0; -} - -bool QSGOpenGLLayer::hasAlphaChannel() const -{ - return m_format != GL_RGB; -} - -bool QSGOpenGLLayer::hasMipmaps() const -{ - return m_mipmap; -} - - -void QSGOpenGLLayer::bind() -{ -#ifndef QT_NO_DEBUG - if (!m_recursive && m_fbo && ((m_multisampling && m_secondaryFbo->isBound()) || m_fbo->isBound())) - qWarning("ShaderEffectSource: \'recursive\' must be set to true when rendering recursively."); -#endif - QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); - if (!m_fbo && m_format == GL_RGBA) { - if (m_transparentTexture == 0) { - funcs->glGenTextures(1, &m_transparentTexture); - funcs->glBindTexture(GL_TEXTURE_2D, m_transparentTexture); - const uint zero = 0; - funcs->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &zero); - } else { - funcs->glBindTexture(GL_TEXTURE_2D, m_transparentTexture); - } - } else { - funcs->glBindTexture(GL_TEXTURE_2D, m_fbo ? m_fbo->texture() : 0); - updateBindOptions(); - } -} - -bool QSGOpenGLLayer::updateTexture() -{ - bool doGrab = (m_live || m_grab) && m_dirtyTexture; - if (doGrab) - grab(); - if (m_grab) - emit scheduledUpdateCompleted(); - m_grab = false; - return doGrab; -} - -void QSGOpenGLLayer::setHasMipmaps(bool mipmap) -{ - if (mipmap == m_mipmap) - return; - m_mipmap = mipmap; - if (m_mipmap && m_fbo && !m_fbo->format().mipmap()) - markDirtyTexture(); -} - - -void QSGOpenGLLayer::setItem(QSGNode *item) -{ - if (item == m_item) - return; - m_item = item; - - if (m_live && !m_item) { - delete m_fbo; - delete m_secondaryFbo; - m_fbo = m_secondaryFbo = nullptr; - m_depthStencilBuffer.clear(); - } - - markDirtyTexture(); -} - -void QSGOpenGLLayer::setRect(const QRectF &rect) -{ - if (rect == m_rect) - return; - m_rect = rect; - markDirtyTexture(); -} - -void QSGOpenGLLayer::setSize(const QSize &size) -{ - if (size == m_size) - return; - m_size = size; - - if (m_live && m_size.isNull()) { - delete m_fbo; - delete m_secondaryFbo; - m_fbo = m_secondaryFbo = nullptr; - m_depthStencilBuffer.clear(); - } - - markDirtyTexture(); -} - -void QSGOpenGLLayer::setFormat(GLenum format) -{ - if (format == m_format) - return; - m_format = format; - markDirtyTexture(); -} - -void QSGOpenGLLayer::setLive(bool live) -{ - if (live == m_live) - return; - m_live = live; - - if (m_live && (!m_item || m_size.isNull())) { - delete m_fbo; - delete m_secondaryFbo; - m_fbo = m_secondaryFbo = nullptr; - m_depthStencilBuffer.clear(); - } - - markDirtyTexture(); -} - -void QSGOpenGLLayer::scheduleUpdate() -{ - if (m_grab) - return; - m_grab = true; - if (m_dirtyTexture) - emit updateRequested(); -} - -void QSGOpenGLLayer::setRecursive(bool recursive) -{ - m_recursive = recursive; -} - -void QSGOpenGLLayer::setMirrorHorizontal(bool mirror) -{ - m_mirrorHorizontal = mirror; -} - -void QSGOpenGLLayer::setMirrorVertical(bool mirror) -{ - m_mirrorVertical = mirror; -} - -void QSGOpenGLLayer::markDirtyTexture() -{ - m_dirtyTexture = true; - if (m_live || m_grab) - emit updateRequested(); -} - -void QSGOpenGLLayer::grab() -{ - if (!m_item || m_size.isNull()) { - delete m_fbo; - delete m_secondaryFbo; - m_fbo = m_secondaryFbo = nullptr; - m_depthStencilBuffer.clear(); - m_dirtyTexture = false; - return; - } - QSGNode *root = m_item; - while (root->firstChild() && root->type() != QSGNode::RootNodeType) - root = root->firstChild(); - if (root->type() != QSGNode::RootNodeType) - return; - - if (!m_renderer) { - m_renderer = m_context->createRenderer(); - connect(m_renderer, SIGNAL(sceneGraphChanged()), this, SLOT(markDirtyTexture())); - } - m_renderer->setDevicePixelRatio(m_device_pixel_ratio); - m_renderer->setRootNode(static_cast<QSGRootNode *>(root)); - - QOpenGLFunctions *funcs = QOpenGLContext::currentContext()->functions(); - bool deleteFboLater = false; - - int effectiveSamples = m_samples; - // By default m_samples is 0. Fall back to the context's setting in this case. - if (effectiveSamples == 0) - effectiveSamples = m_context->openglContext()->format().samples(); - - const bool needsNewFbo = !m_fbo || m_fbo->size() != m_size || m_fbo->format().internalTextureFormat() != m_format; - const bool mipmapGotEnabled = m_fbo && !m_fbo->format().mipmap() && m_mipmap; - const bool msaaGotEnabled = effectiveSamples > 1 && (!m_secondaryFbo || m_secondaryFbo->format().samples() != effectiveSamples); - const bool msaaGotDisabled = effectiveSamples <= 1 && m_secondaryFbo; - - if (needsNewFbo || mipmapGotEnabled || msaaGotEnabled || msaaGotDisabled) { - if (!m_multisamplingChecked) { - if (effectiveSamples <= 1) { - m_multisampling = false; - } else { - QOpenGLExtensions *e = static_cast<QOpenGLExtensions *>(funcs); - m_multisampling = e->hasOpenGLExtension(QOpenGLExtensions::FramebufferMultisample) - && e->hasOpenGLExtension(QOpenGLExtensions::FramebufferBlit); - } - m_multisamplingChecked = true; - } - if (m_multisampling) { - // Don't delete the FBO right away in case it is used recursively. - deleteFboLater = true; - delete m_secondaryFbo; - QOpenGLFramebufferObjectFormat format; - - format.setInternalTextureFormat(m_format); - format.setSamples(effectiveSamples); - m_secondaryFbo = new QOpenGLFramebufferObject(m_size, format); - m_depthStencilBuffer = m_context->depthStencilBufferForFbo(m_secondaryFbo); - } else { - QOpenGLFramebufferObjectFormat format; - format.setInternalTextureFormat(m_format); - format.setMipmap(m_mipmap); - if (m_recursive) { - deleteFboLater = true; - delete m_secondaryFbo; - m_secondaryFbo = new QOpenGLFramebufferObject(m_size, format); - funcs->glBindTexture(GL_TEXTURE_2D, m_secondaryFbo->texture()); - updateBindOptions(true); - m_depthStencilBuffer = m_context->depthStencilBufferForFbo(m_secondaryFbo); - } else { - delete m_fbo; - delete m_secondaryFbo; - m_fbo = new QOpenGLFramebufferObject(m_size, format); - m_secondaryFbo = nullptr; - funcs->glBindTexture(GL_TEXTURE_2D, m_fbo->texture()); - updateBindOptions(true); - m_depthStencilBuffer = m_context->depthStencilBufferForFbo(m_fbo); - } - } - } - - if (m_recursive && !m_secondaryFbo) { - // m_fbo already created, m_recursive was just set. - Q_ASSERT(m_fbo); - Q_ASSERT(!m_multisampling); - - m_secondaryFbo = new QOpenGLFramebufferObject(m_size, m_fbo->format()); - funcs->glBindTexture(GL_TEXTURE_2D, m_secondaryFbo->texture()); - updateBindOptions(true); - } - - // Render texture. - root->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip and opacity update. - m_renderer->nodeChanged(root, QSGNode::DirtyForceUpdate); // Force render list update. - -#ifdef QSG_DEBUG_FBO_OVERLAY - if (qmlFboOverlay()) { - if (!m_debugOverlay) - m_debugOverlay = new QSGSimpleRectNode(); - m_debugOverlay->setRect(QRectF(0, 0, m_size.width(), m_size.height())); - m_debugOverlay->setColor(QColor(0xff, 0x00, 0x80, 0x40)); - root->appendChildNode(m_debugOverlay); - } -#endif - - m_dirtyTexture = false; - - m_renderer->setDeviceRect(m_size); - m_renderer->setViewportRect(m_size); - QRectF mirrored(m_mirrorHorizontal ? m_rect.right() : m_rect.left(), - m_mirrorVertical ? m_rect.bottom() : m_rect.top(), - m_mirrorHorizontal ? -m_rect.width() : m_rect.width(), - m_mirrorVertical ? -m_rect.height() : m_rect.height()); - m_renderer->setProjectionMatrixToRect(mirrored); - m_renderer->setClearColor(Qt::transparent); - - if (m_multisampling) { - m_renderer->renderScene(BindableFbo(m_secondaryFbo, m_depthStencilBuffer.data())); - - if (deleteFboLater) { - delete m_fbo; - QOpenGLFramebufferObjectFormat format; - format.setInternalTextureFormat(m_format); - format.setAttachment(QOpenGLFramebufferObject::NoAttachment); - format.setMipmap(m_mipmap); - format.setSamples(0); - m_fbo = new QOpenGLFramebufferObject(m_size, format); - funcs->glBindTexture(GL_TEXTURE_2D, m_fbo->texture()); - updateBindOptions(true); - } - - QRect r(QPoint(), m_size); - QOpenGLFramebufferObject::blitFramebuffer(m_fbo, r, m_secondaryFbo, r); - } else { - if (m_recursive) { - m_renderer->renderScene(BindableFbo(m_secondaryFbo, m_depthStencilBuffer.data())); - - if (deleteFboLater) { - delete m_fbo; - QOpenGLFramebufferObjectFormat format; - format.setAttachment(QOpenGLFramebufferObject::CombinedDepthStencil); - format.setInternalTextureFormat(m_format); - format.setMipmap(m_mipmap); - m_fbo = new QOpenGLFramebufferObject(m_size, format); - funcs->glBindTexture(GL_TEXTURE_2D, m_fbo->texture()); - updateBindOptions(true); - } - qSwap(m_fbo, m_secondaryFbo); - } else { - m_renderer->renderScene(BindableFbo(m_fbo, m_depthStencilBuffer.data())); - } - } - - if (m_mipmap) { - funcs->glBindTexture(GL_TEXTURE_2D, textureId()); - funcs->glGenerateMipmap(GL_TEXTURE_2D); - } - - root->markDirty(QSGNode::DirtyForceUpdate); // Force matrix, clip, opacity and render list update. - -#ifdef QSG_DEBUG_FBO_OVERLAY - if (qmlFboOverlay()) - root->removeChildNode(m_debugOverlay); -#endif - if (m_recursive) - markDirtyTexture(); // Continuously update if 'live' and 'recursive'. -} - -QImage QSGOpenGLLayer::toImage() const -{ - if (m_fbo) - return m_fbo->toImage(); - - return QImage(); -} - -QRectF QSGOpenGLLayer::normalizedTextureSubRect() const -{ - return QRectF(m_mirrorHorizontal ? 1 : 0, - m_mirrorVertical ? 0 : 1, - m_mirrorHorizontal ? -1 : 1, - m_mirrorVertical ? 1 : -1); -} - -#include "moc_qsgopengllayer_p.cpp" diff --git a/src/quick/scenegraph/qsgopengllayer_p.h b/src/quick/scenegraph/qsgopengllayer_p.h deleted file mode 100644 index 2d33a6e5be..0000000000 --- a/src/quick/scenegraph/qsgopengllayer_p.h +++ /dev/null @@ -1,161 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtQuick module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -#ifndef QSGOPENGLLAYER_P_H -#define QSGOPENGLLAYER_P_H - -// -// W A R N I N G -// ------------- -// -// This file is not part of the Qt API. It exists purely as an -// implementation detail. This header file may change from version to -// version without notice, or even be removed. -// -// We mean it. -// - -#include <private/qsgadaptationlayer_p.h> -#include <private/qsgcontext_p.h> -#include <private/qsgtexture_p.h> -#include <qsgsimplerectnode.h> - -QT_BEGIN_NAMESPACE - -#define QSG_DEBUG_FBO_OVERLAY - -class QOpenGLFramebufferObject; -class QSGDepthStencilBuffer; -class QSGDefaultRenderContext; - -class Q_QUICK_PRIVATE_EXPORT QSGOpenGLLayer : public QSGLayer -{ - Q_OBJECT -public: - QSGOpenGLLayer(QSGRenderContext *context); - ~QSGOpenGLLayer(); - - bool updateTexture() override; - - // The item's "paint node", not effect node. - QSGNode *item() const { return m_item; } - void setItem(QSGNode *item) override; - - QRectF rect() const { return m_rect; } - void setRect(const QRectF &rect) override; - - QSize size() const { return m_size; } - void setSize(const QSize &size) override; - - void setHasMipmaps(bool mipmap) override; - - void bind() override; - - bool hasAlphaChannel() const override; - bool hasMipmaps() const override; - int textureId() const override; - QSize textureSize() const override { return m_size; } - qint64 comparisonKey() const override; - - GLenum format() const { return m_format; } - void setFormat(GLenum format) override; - - bool live() const { return bool(m_live); } - void setLive(bool live) override; - - bool recursive() const { return bool(m_recursive); } - void setRecursive(bool recursive) override; - - void setDevicePixelRatio(qreal ratio) override { m_device_pixel_ratio = ratio; } - - bool mirrorHorizontal() const { return bool(m_mirrorHorizontal); } - void setMirrorHorizontal(bool mirror) override; - - bool mirrorVertical() const { return bool(m_mirrorVertical); } - void setMirrorVertical(bool mirror) override; - - void scheduleUpdate() override; - - QImage toImage() const override; - - QRectF normalizedTextureSubRect() const override; - - int samples() const { return m_samples; } - void setSamples(int samples) override { m_samples = samples; } - -public Q_SLOTS: - void markDirtyTexture() override; - void invalidated() override; - -private: - void grab(); - - QSGNode *m_item; - QRectF m_rect; - QSize m_size; - qreal m_device_pixel_ratio; - GLenum m_format; - - QSGRenderer *m_renderer; - QOpenGLFramebufferObject *m_fbo; - QOpenGLFramebufferObject *m_secondaryFbo; - QSharedPointer<QSGDepthStencilBuffer> m_depthStencilBuffer; - - GLuint m_transparentTexture; - -#ifdef QSG_DEBUG_FBO_OVERLAY - QSGSimpleRectNode *m_debugOverlay; -#endif - - QSGDefaultRenderContext *m_context; - int m_samples; - - uint m_mipmap : 1; - uint m_live : 1; - uint m_recursive : 1; - uint m_dirtyTexture : 1; - uint m_multisamplingChecked : 1; - uint m_multisampling : 1; - uint m_grab : 1; - uint m_mirrorHorizontal : 1; - uint m_mirrorVertical : 1; -}; - -QT_END_NAMESPACE - -#endif // QSGOPENGLLAYER_P_H diff --git a/src/quick/scenegraph/scenegraph.pri b/src/quick/scenegraph/scenegraph.pri index 2e777cb6a8..7b4198ff6f 100644 --- a/src/quick/scenegraph/scenegraph.pri +++ b/src/quick/scenegraph/scenegraph.pri @@ -121,7 +121,6 @@ qtConfig(opengl(es1|es2)?) { SOURCES += \ $$PWD/qsgdefaultglyphnode.cpp \ $$PWD/qsgdefaultglyphnode_p.cpp \ - $$PWD/qsgopengldistancefieldglyphcache.cpp \ $$PWD/qsgdistancefieldglyphnode.cpp \ $$PWD/qsgdistancefieldglyphnode_p.cpp \ $$PWD/qsgdefaultinternalimagenode.cpp \ @@ -131,11 +130,9 @@ qtConfig(opengl(es1|es2)?) { $$PWD/util/qsgdefaultpainternode.cpp \ $$PWD/util/qsgdefaultrectanglenode.cpp \ $$PWD/util/qsgdefaultimagenode.cpp \ - $$PWD/util/qsgdefaultninepatchnode.cpp \ - $$PWD/qsgopengllayer.cpp + $$PWD/util/qsgdefaultninepatchnode.cpp HEADERS += \ $$PWD/qsgdefaultglyphnode_p.h \ - $$PWD/qsgopengldistancefieldglyphcache_p.h \ $$PWD/qsgdistancefieldglyphnode_p.h \ $$PWD/qsgdistancefieldglyphnode_p_p.h \ $$PWD/qsgdefaultglyphnode_p_p.h \ @@ -146,8 +143,7 @@ qtConfig(opengl(es1|es2)?) { $$PWD/util/qsgdefaultpainternode_p.h \ $$PWD/util/qsgdefaultrectanglenode_p.h \ $$PWD/util/qsgdefaultimagenode_p.h \ - $$PWD/util/qsgdefaultninepatchnode_p.h \ - $$PWD/qsgopengllayer_p.h + $$PWD/util/qsgdefaultninepatchnode_p.h qtConfig(thread) { SOURCES += \ |