diff options
Diffstat (limited to 'src/quick/scenegraph/qsgrhitextureglyphcache.cpp')
-rw-r--r-- | src/quick/scenegraph/qsgrhitextureglyphcache.cpp | 146 |
1 files changed, 52 insertions, 94 deletions
diff --git a/src/quick/scenegraph/qsgrhitextureglyphcache.cpp b/src/quick/scenegraph/qsgrhitextureglyphcache.cpp index 1ec7e0c92d..7e626be8e2 100644 --- a/src/quick/scenegraph/qsgrhitextureglyphcache.cpp +++ b/src/quick/scenegraph/qsgrhitextureglyphcache.cpp @@ -1,52 +1,19 @@ -/**************************************************************************** -** -** Copyright (C) 2019 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$ -** -****************************************************************************/ +// Copyright (C) 2019 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qsgrhitextureglyphcache_p.h" +#include "qsgdefaultrendercontext_p.h" #include <qrgb.h> #include <private/qdrawhelper_p.h> QT_BEGIN_NAMESPACE -QSGRhiTextureGlyphCache::QSGRhiTextureGlyphCache(QRhi *rhi, QFontEngine::GlyphFormat format, const QTransform &matrix, +QSGRhiTextureGlyphCache::QSGRhiTextureGlyphCache(QSGDefaultRenderContext *rc, + QFontEngine::GlyphFormat format, const QTransform &matrix, const QColor &color) : QImageTextureGlyphCache(format, matrix, color), - m_rhi(rhi) + m_rc(rc), + m_rhi(rc->rhi()) { // Some OpenGL implementations, for instance macOS, have issues with // GL_ALPHA render targets. Similarly, BGRA may be problematic on GLES 2.0. @@ -56,13 +23,7 @@ QSGRhiTextureGlyphCache::QSGRhiTextureGlyphCache(QRhi *rhi, QFontEngine::GlyphFo QSGRhiTextureGlyphCache::~QSGRhiTextureGlyphCache() { - if (m_resourceUpdates) - m_resourceUpdates->release(); - - delete m_texture; - - // should be empty, but just in case - qDeleteAll(m_pendingDispose); + m_rc->deferredReleaseGlyphCacheTexture(m_texture); } QRhiTexture *QSGRhiTextureGlyphCache::createEmptyTexture(QRhiTexture::Format format) @@ -73,8 +34,7 @@ QRhiTexture *QSGRhiTextureGlyphCache::createEmptyTexture(QRhiTexture::Format for return nullptr; } - if (!m_resourceUpdates) - m_resourceUpdates = m_rhi->nextResourceUpdateBatch(); + QRhiResourceUpdateBatch *resourceUpdates = m_rc->glyphCacheResourceUpdates(); // The new texture must be cleared to 0 always, this cannot be avoided // otherwise artifacts will occur around the glyphs. @@ -85,7 +45,7 @@ QRhiTexture *QSGRhiTextureGlyphCache::createEmptyTexture(QRhiTexture::Format for data.fill(0, m_size.width() * m_size.height() * 4); QRhiTextureSubresourceUploadDescription subresDesc(data.constData(), data.size()); subresDesc.setSourceSize(m_size); - m_resourceUpdates->uploadTexture(t, QRhiTextureUploadEntry(0, 0, subresDesc)); + resourceUpdates->uploadTexture(t, QRhiTextureUploadEntry(0, 0, subresDesc)); return t; } @@ -116,11 +76,9 @@ void QSGRhiTextureGlyphCache::resizeTextureData(int width, int height) if (!t) return; - if (!m_resourceUpdates) - m_resourceUpdates = m_rhi->nextResourceUpdateBatch(); - + QRhiResourceUpdateBatch *resourceUpdates = m_rc->glyphCacheResourceUpdates(); if (m_resizeWithTextureCopy) { - m_resourceUpdates->copyTexture(t, m_texture); + resourceUpdates->copyTexture(t, m_texture); } else { QImageTextureGlyphCache::resizeTextureData(width, height); QImage img = image(); @@ -128,10 +86,10 @@ void QSGRhiTextureGlyphCache::resizeTextureData(int width, int height) QRhiTextureSubresourceUploadDescription subresDesc(img); const QSize oldSize = m_texture->pixelSize(); subresDesc.setSourceSize(QSize(qMin(oldSize.width(), width), qMin(oldSize.height(), height))); - m_resourceUpdates->uploadTexture(t, QRhiTextureUploadEntry(0, 0, subresDesc)); + resourceUpdates->uploadTexture(t, QRhiTextureUploadEntry(0, 0, subresDesc)); } - m_pendingDispose.insert(m_texture); + m_rc->deferredReleaseGlyphCacheTexture(m_texture); m_texture = t; } } @@ -151,36 +109,42 @@ void QSGRhiTextureGlyphCache::prepareGlyphImage(QImage *img) m_bgra = false; if (img->format() == QImage::Format_Mono) { - *img = img->convertToFormat(QImage::Format_Grayscale8); - } else if (img->depth() == 32) { - if (img->format() == QImage::Format_RGB32 || img->format() == QImage::Format_ARGB32_Premultiplied) { - // We need to make the alpha component equal to the average of the RGB values. - // This is needed when drawing sub-pixel antialiased text on translucent targets. + *img = std::move(*img).convertToFormat(QImage::Format_Grayscale8); + } else if (img->format() == QImage::Format_RGB32 || img->format() == QImage::Format_ARGB32_Premultiplied) { + // We need to make the alpha component equal to the average of the RGB values. + // This is needed when drawing sub-pixel antialiased text on translucent targets. + if (img->format() == QImage::Format_RGB32 +#if Q_BYTE_ORDER != Q_BIG_ENDIAN + || !supportsBgra +#endif + ) { for (int y = 0; y < maskHeight; ++y) { - QRgb *src = (QRgb *) img->scanLine(y); + QRgb *src = reinterpret_cast<QRgb *>(img->scanLine(y)); for (int x = 0; x < maskWidth; ++x) { - int r = qRed(src[x]); - int g = qGreen(src[x]); - int b = qBlue(src[x]); - int avg; - if (img->format() == QImage::Format_RGB32) - avg = (r + g + b + 1) / 3; // "+1" for rounding. - else // Format_ARGB_Premultiplied - avg = qAlpha(src[x]); - - src[x] = qRgba(r, g, b, avg); + QRgb &rgb = src[x]; + + if (img->format() == QImage::Format_RGB32) { + int r = qRed(rgb); + int g = qGreen(rgb); + int b = qBlue(rgb); + int avg = (r + g + b + 1) / 3; // "+1" for rounding. + rgb = qRgba(r, g, b, avg); + } + #if Q_BYTE_ORDER != Q_BIG_ENDIAN - if (supportsBgra) { - m_bgra = true; - } else { + if (!supportsBgra) { // swizzle the bits to accommodate for the RGBA upload. - src[x] = ARGB2RGBA(src[x]); + rgb = ARGB2RGBA(rgb); m_bgra = false; } #endif } } } +#if Q_BYTE_ORDER != Q_BIG_ENDIAN + if (supportsBgra) + m_bgra = true; +#endif } } @@ -191,9 +155,9 @@ void QSGRhiTextureGlyphCache::fillTexture(const Coord &c, glyph_t glyph, const Q if (!m_resizeWithTextureCopy) { QImageTextureGlyphCache::fillTexture(c, glyph, subPixelPosition); - mask = image(); - subresDesc.setSourceTopLeft(QPoint(c.x, c.y)); - subresDesc.setSourceSize(QSize(c.w, c.h)); + // Explicitly copy() here to avoid fillTexture detaching the *entire* image() when + // it is still referenced by QRhiTextureSubresourceUploadDescription. + mask = image().copy(QRect(c.x, c.y, c.w, c.h)); } else { mask = textureMapForGlyph(glyph, subPixelPosition); } @@ -222,18 +186,19 @@ void QSGRhiTextureGlyphCache::endFillTexture() return; } - if (!m_resourceUpdates) - m_resourceUpdates = m_rhi->nextResourceUpdateBatch(); - + QRhiResourceUpdateBatch *resourceUpdates = m_rc->glyphCacheResourceUpdates(); QRhiTextureUploadDescription desc; desc.setEntries(m_uploads.cbegin(), m_uploads.cend()); - m_resourceUpdates->uploadTexture(m_texture, desc); + resourceUpdates->uploadTexture(m_texture, desc); m_uploads.clear(); } int QSGRhiTextureGlyphCache::glyphPadding() const { - return 1; + if (m_format == QFontEngine::Format_Mono) + return 8; + else + return 1; } int QSGRhiTextureGlyphCache::maxTextureWidth() const @@ -251,17 +216,10 @@ int QSGRhiTextureGlyphCache::maxTextureHeight() const void QSGRhiTextureGlyphCache::commitResourceUpdates(QRhiResourceUpdateBatch *mergeInto) { - if (m_resourceUpdates) { - mergeInto->merge(m_resourceUpdates); - m_resourceUpdates->release(); - m_resourceUpdates = nullptr; + if (QRhiResourceUpdateBatch *resourceUpdates = m_rc->maybeGlyphCacheResourceUpdates()) { + mergeInto->merge(resourceUpdates); + m_rc->resetGlyphCacheResources(); } - - // now let's assume the resource updates will be committed in this frame - for (QRhiTexture *t : m_pendingDispose) - t->deleteLater(); // will be deleted after the frame is submitted -> safe - - m_pendingDispose.clear(); } bool QSGRhiTextureGlyphCache::eightBitFormatIsAlphaSwizzled() const |