30QSGDefaultRenderContext::QSGDefaultRenderContext(QSGContext *context)
31 : QSGRenderContext(context)
34 , m_rhiAtlasManager(
nullptr)
35 , m_currentFrameCommandBuffer(
nullptr)
36 , m_currentFrameRenderPass(
nullptr)
37 , m_useDepthBufferFor2D(
true)
38 , m_glyphCacheResourceUpdates(
nullptr)
46void QSGDefaultRenderContext::initialize(
const QSGRenderContext::InitParams *params)
51 const InitParams *initParams =
static_cast<
const InitParams *>(params);
52 if (initParams->sType != INIT_PARAMS_MAGIC)
53 qFatal(
"QSGDefaultRenderContext: Invalid parameters passed to initialize()");
55 m_initParams = *initParams;
57 m_rhi = m_initParams.rhi;
58 m_maxTextureSize = m_rhi->resourceLimit(QRhi::TextureSizeMax);
59 if (!m_rhiAtlasManager)
60 m_rhiAtlasManager =
new QSGRhiAtlasTexture::Manager(
this, m_initParams.initialSurfacePixelSize, m_initParams.maybeSurface);
62 m_glyphCacheResourceUpdates =
nullptr;
64 m_sg->renderContextInitialized(
this);
78void QSGDefaultRenderContext::flushGlyphCaches()
80 auto it = m_staleGlyphCaches.begin();
81 while (it != m_staleGlyphCaches.end()) {
82 if (!(*it)->isActive()) {
83 qCDebug(lcGlyphCaches()) <<
"Deleting stale glyph cache:"
85 <<
"fontEngine:" << fontEngineOfRawFont((*it)->referenceFont())
86 << (*it)->referenceFont().familyName();
88 it = m_staleGlyphCaches.erase(it);
95 auto it = m_fontEnginesToClean.begin();
96 while (it != m_fontEnginesToClean.end()) {
97 if (it.value() == 0) {
98 it.key()->clearGlyphCache(
this);
99 if (!it.key()->ref.deref())
101 it = m_fontEnginesToClean.erase(it);
109void QSGDefaultRenderContext::invalidateGlyphCaches()
112 auto it = m_glyphCaches.begin();
113 while (it != m_glyphCaches.end()) {
114 if (!(*it)->isActive()) {
115 qCDebug(lcGlyphCaches()) <<
"Direct delete glyph cache:"
117 <<
"fontEngine:" << fontEngineOfRawFont(it.value()->referenceFont())
118 << it.value()->referenceFont().familyName()
119 << it.value()->referenceFont().styleName()
121 << it.key().familyName
122 << it.key().styleName
123 << it.key().faceId.filename;
126 qCDebug(lcGlyphCaches()) <<
"Stale glyph cache:"
128 <<
"fontEngine:" << fontEngineOfRawFont(it.value()->referenceFont())
129 << it.value()->referenceFont().familyName()
130 << it.value()->referenceFont().styleName()
132 << it.key().familyName
133 << it.key().styleName
134 << it.key().faceId.filename;
136 m_staleGlyphCaches.append(*it);
139 it = m_glyphCaches.erase(it);
143 qDeleteAll(m_curveGlyphAtlases);
144 m_curveGlyphAtlases.clear();
147void QSGDefaultRenderContext::invalidate()
152 qDeleteAll(m_texturesToDelete);
153 m_texturesToDelete.clear();
155 qDeleteAll(m_textures);
158 for (
auto it = m_depthStencilBuffers.cbegin(), end = m_depthStencilBuffers.cend(); it != end; ++it) {
159 QSharedPointer<QSGDepthStencilBuffer> buf = it.value().toStrongRef();
164 m_depthStencilBuffers.clear();
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 if (m_rhiAtlasManager) {
183 m_rhiAtlasManager->invalidate();
184 m_rhiAtlasManager->deleteLater();
185 m_rhiAtlasManager =
nullptr;
193 for (
auto it = m_fontEnginesToClean.constBegin(); it != m_fontEnginesToClean.constEnd(); ++it) {
194 it.key()->clearGlyphCache(
this);
195 if (!it.key()->ref.deref())
198 m_fontEnginesToClean.clear();
200 qDeleteAll(m_curveGlyphAtlases);
201 m_curveGlyphAtlases.clear();
203 qCDebug(lcGlyphCaches()) <<
"Deleting" << m_glyphCaches.size() <<
"live glyph caches";
205 qDeleteAll(m_glyphCaches);
206 m_glyphCaches.clear();
208 qCDebug(lcGlyphCaches()) <<
"Deleting" << m_staleGlyphCaches.size() <<
"stale glyph caches";
210 qDeleteAll(m_staleGlyphCaches);
211 m_staleGlyphCaches.clear();
213 resetGlyphCacheResources();
218 m_sg->renderContextInvalidated(
this);
223void QSGDefaultRenderContext::prepareSync(qreal devicePixelRatio,
224 QRhiCommandBuffer *cb,
225 const QQuickGraphicsConfiguration &config)
227 m_currentDevicePixelRatio = devicePixelRatio;
228 m_useDepthBufferFor2D = config.isDepthBufferEnabledFor2D();
233 m_currentFrameCommandBuffer = cb;
236void QSGDefaultRenderContext::beginNextFrame(QSGRenderer *renderer,
const QSGRenderTarget &renderTarget,
237 RenderPassCallback mainPassRecordingStart,
238 RenderPassCallback mainPassRecordingEnd,
239 void *callbackUserData)
241 renderer->setRenderTarget(renderTarget);
242 renderer->setRenderPassRecordingCallbacks(mainPassRecordingStart, mainPassRecordingEnd, callbackUserData);
244 m_currentFrameCommandBuffer = renderTarget.cb;
245 m_currentFrameRenderPass = renderTarget.rpDesc;
260QSGTexture *QSGDefaultRenderContext::createTexture(
const QImage &image, uint flags)
const
262 bool atlas = flags & CreateTexture_Atlas;
263 bool mipmap = flags & CreateTexture_Mipmap;
264 bool alpha = flags & CreateTexture_Alpha;
269 if (!mipmap && atlas && QThread::currentThread() == m_rhi->thread()) {
270 QSGTexture *t = m_rhiAtlasManager->create(image, alpha);
276 QSGPlainTexture *texture =
new QSGPlainTexture;
277 texture->setImage(image);
278 if (texture->hasAlphaChannel() && !alpha)
279 texture->setHasAlphaChannel(
false);
304void QSGDefaultRenderContext::preprocess()
306 for (
auto it = m_glyphCaches.begin(); it != m_glyphCaches.end(); ++it) {
307 it.value()->processPendingGlyphs();
308 it.value()->update();
311 for (
auto it = m_staleGlyphCaches.begin(); it != m_staleGlyphCaches.end(); ++it) {
312 (*it)->processPendingGlyphs();
317QSGCurveGlyphAtlas *QSGDefaultRenderContext::curveGlyphAtlas(
const QRawFont &font)
319 FontKey key = FontKey(font, 0);
320 QSGCurveGlyphAtlas *atlas = m_curveGlyphAtlases.value(key,
nullptr);
321 if (atlas ==
nullptr) {
322 atlas =
new QSGCurveGlyphAtlas(font);
323 m_curveGlyphAtlases.insert(key, atlas);
329QSGDistanceFieldGlyphCache *QSGDefaultRenderContext::distanceFieldGlyphCache(
const QRawFont &font,
int renderTypeQuality)
331 FontKey key(font, renderTypeQuality);
332 QSGDistanceFieldGlyphCache *cache = m_glyphCaches.value(key, 0);
333 if (!cache && font.isValid()) {
334 cache =
new QSGRhiDistanceFieldGlyphCache(
this, font, renderTypeQuality);
335 m_glyphCaches.insert(key, cache);
337 qCDebug(lcGlyphCaches()) <<
"Creating new glyph cache:"
339 <<
"fontEngine:" << fontEngineOfRawFont(cache->referenceFont())
340 << cache->referenceFont().familyName()
341 << cache->referenceFont().styleName()
345 << key.faceId.filename;
347 qCDebug(lcGlyphCaches()) <<
"Found existing glyph cache:"
349 <<
"fontEngine:" << fontEngineOfRawFont(cache->referenceFont())
350 << cache->referenceFont().familyName()
351 << cache->referenceFont().styleName()
355 << key.faceId.filename;
380void QSGDefaultRenderContext::resetGlyphCacheResources()
382 if (m_glyphCacheResourceUpdates) {
383 m_glyphCacheResourceUpdates->release();
384 m_glyphCacheResourceUpdates =
nullptr;
387 for (QRhiTexture *t : std::as_const(m_pendingGlyphCacheTextures))
390 m_pendingGlyphCacheTextures.clear();
402QSharedPointer<QSGDepthStencilBuffer> QSGDefaultRenderContext::getDepthStencilBuffer(
const QSize &size,
int sampleCount)
404 const auto key = std::pair(size, sampleCount);
405 auto it = m_depthStencilBuffers.constFind(key);
406 if (it != m_depthStencilBuffers.cend()) {
408 return it.value().toStrongRef();
411 QRhiRenderBuffer *ds = m_rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, size, sampleCount);
413 qWarning(
"Failed to build depth-stencil buffer for layer");
417 QSharedPointer<QSGDepthStencilBuffer> buf(
new QSGDepthStencilBuffer(ds));
418 addDepthStencilBuffer(buf);
422void QSGDefaultRenderContext::addDepthStencilBuffer(
const QSharedPointer<QSGDepthStencilBuffer> &ds)
425 const auto key = std::pair(ds->ds->pixelSize(), ds->ds->sampleCount());
427 m_depthStencilBuffers.insert(key, ds.toWeakRef());
static QFontEngine * fontEngineOfRawFont(const QRawFont &font)