24QSGDefaultRenderContext::QSGDefaultRenderContext(QSGContext *context)
25 : QSGRenderContext(context)
28 , m_rhiAtlasManager(
nullptr)
29 , m_currentFrameCommandBuffer(
nullptr)
30 , m_currentFrameRenderPass(
nullptr)
31 , m_useDepthBufferFor2D(
true)
32 , m_glyphCacheResourceUpdates(
nullptr)
40void QSGDefaultRenderContext::initialize(
const QSGRenderContext::InitParams *params)
45 const InitParams *initParams =
static_cast<
const InitParams *>(params);
46 if (initParams->sType != INIT_PARAMS_MAGIC)
47 qFatal(
"QSGDefaultRenderContext: Invalid parameters passed to initialize()");
49 m_initParams = *initParams;
51 m_rhi = m_initParams.rhi;
52 m_maxTextureSize = m_rhi->resourceLimit(QRhi::TextureSizeMax);
53 if (!m_rhiAtlasManager)
54 m_rhiAtlasManager =
new QSGRhiAtlasTexture::Manager(
this, m_initParams.initialSurfacePixelSize, m_initParams.maybeSurface);
56 m_glyphCacheResourceUpdates =
nullptr;
58 m_sg->renderContextInitialized(
this);
72void QSGDefaultRenderContext::flushGlyphCaches()
74 auto it = m_staleGlyphCaches.begin();
75 while (it != m_staleGlyphCaches.end()) {
76 if (!(*it)->isActive()) {
77 qCDebug(lcGlyphCaches()) <<
"Deleting stale glyph cache:"
79 <<
"fontEngine:" << fontEngineOfRawFont((*it)->referenceFont())
80 << (*it)->referenceFont().familyName();
82 it = m_staleGlyphCaches.erase(it);
89 auto it = m_fontEnginesToClean.begin();
90 while (it != m_fontEnginesToClean.end()) {
91 if (it.value() == 0) {
92 it.key()->clearGlyphCache(
this);
93 if (!it.key()->ref.deref())
95 it = m_fontEnginesToClean.erase(it);
103void QSGDefaultRenderContext::invalidateGlyphCaches()
106 auto it = m_glyphCaches.begin();
107 while (it != m_glyphCaches.end()) {
108 if (!(*it)->isActive()) {
109 qCDebug(lcGlyphCaches()) <<
"Direct delete glyph cache:"
111 <<
"fontEngine:" << fontEngineOfRawFont(it.value()->referenceFont())
112 << it.value()->referenceFont().familyName()
113 << it.value()->referenceFont().styleName()
115 << it.key().familyName
116 << it.key().styleName
117 << it.key().faceId.filename;
120 qCDebug(lcGlyphCaches()) <<
"Stale glyph cache:"
122 <<
"fontEngine:" << fontEngineOfRawFont(it.value()->referenceFont())
123 << it.value()->referenceFont().familyName()
124 << it.value()->referenceFont().styleName()
126 << it.key().familyName
127 << it.key().styleName
128 << it.key().faceId.filename;
130 m_staleGlyphCaches.append(*it);
133 it = m_glyphCaches.erase(it);
137 qDeleteAll(m_curveGlyphAtlases);
138 m_curveGlyphAtlases.clear();
141void QSGDefaultRenderContext::invalidate()
146 qDeleteAll(m_texturesToDelete);
147 m_texturesToDelete.clear();
149 qDeleteAll(m_textures);
152 for (
auto it = m_depthStencilBuffers.cbegin(), end = m_depthStencilBuffers.cend(); it != end; ++it) {
153 QSharedPointer<QSGDepthStencilBuffer> buf = it.value().toStrongRef();
158 m_depthStencilBuffers.clear();
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 if (m_rhiAtlasManager) {
177 m_rhiAtlasManager->invalidate();
178 m_rhiAtlasManager->deleteLater();
179 m_rhiAtlasManager =
nullptr;
187 for (
auto it = m_fontEnginesToClean.constBegin(); it != m_fontEnginesToClean.constEnd(); ++it) {
188 it.key()->clearGlyphCache(
this);
189 if (!it.key()->ref.deref())
192 m_fontEnginesToClean.clear();
194 qDeleteAll(m_curveGlyphAtlases);
195 m_curveGlyphAtlases.clear();
197 qCDebug(lcGlyphCaches()) <<
"Deleting" << m_glyphCaches.size() <<
"live glyph caches";
199 qDeleteAll(m_glyphCaches);
200 m_glyphCaches.clear();
202 qCDebug(lcGlyphCaches()) <<
"Deleting" << m_staleGlyphCaches.size() <<
"stale glyph caches";
204 qDeleteAll(m_staleGlyphCaches);
205 m_staleGlyphCaches.clear();
207 resetGlyphCacheResources();
212 m_sg->renderContextInvalidated(
this);
217void QSGDefaultRenderContext::prepareSync(qreal devicePixelRatio,
218 QRhiCommandBuffer *cb,
219 const QQuickGraphicsConfiguration &config)
221 m_currentDevicePixelRatio = devicePixelRatio;
222 m_useDepthBufferFor2D = config.isDepthBufferEnabledFor2D();
227 m_currentFrameCommandBuffer = cb;
230void QSGDefaultRenderContext::beginNextFrame(QSGRenderer *renderer,
const QSGRenderTarget &renderTarget,
231 RenderPassCallback mainPassRecordingStart,
232 RenderPassCallback mainPassRecordingEnd,
233 void *callbackUserData)
235 renderer->setRenderTarget(renderTarget);
236 renderer->setRenderPassRecordingCallbacks(mainPassRecordingStart, mainPassRecordingEnd, callbackUserData);
238 m_currentFrameCommandBuffer = renderTarget.cb;
239 m_currentFrameRenderPass = renderTarget.rpDesc;
254QSGTexture *QSGDefaultRenderContext::createTexture(
const QImage &image, uint flags)
const
256 bool atlas = flags & CreateTexture_Atlas;
257 bool mipmap = flags & CreateTexture_Mipmap;
258 bool alpha = flags & CreateTexture_Alpha;
263 if (!mipmap && atlas && QThread::currentThread() == m_rhi->thread()) {
264 QSGTexture *t = m_rhiAtlasManager->create(image, alpha);
270 QSGPlainTexture *texture =
new QSGPlainTexture;
271 texture->setImage(image);
272 if (texture->hasAlphaChannel() && !alpha)
273 texture->setHasAlphaChannel(
false);
298void QSGDefaultRenderContext::preprocess()
300 for (
auto it = m_glyphCaches.begin(); it != m_glyphCaches.end(); ++it) {
301 it.value()->processPendingGlyphs();
302 it.value()->update();
305 for (
auto it = m_staleGlyphCaches.begin(); it != m_staleGlyphCaches.end(); ++it) {
306 (*it)->processPendingGlyphs();
311QSGCurveGlyphAtlas *QSGDefaultRenderContext::curveGlyphAtlas(
const QRawFont &font)
313 FontKey key = FontKey(font, 0);
314 QSGCurveGlyphAtlas *atlas = m_curveGlyphAtlases.value(key,
nullptr);
315 if (atlas ==
nullptr) {
316 atlas =
new QSGCurveGlyphAtlas(font);
317 m_curveGlyphAtlases.insert(key, atlas);
323QSGDistanceFieldGlyphCache *QSGDefaultRenderContext::distanceFieldGlyphCache(
const QRawFont &font,
int renderTypeQuality)
325 FontKey key(font, renderTypeQuality);
326 QSGDistanceFieldGlyphCache *cache = m_glyphCaches.value(key, 0);
327 if (!cache && font.isValid()) {
328 cache =
new QSGRhiDistanceFieldGlyphCache(
this, font, renderTypeQuality);
329 m_glyphCaches.insert(key, cache);
331 qCDebug(lcGlyphCaches()) <<
"Creating new glyph cache:"
333 <<
"fontEngine:" << fontEngineOfRawFont(cache->referenceFont())
334 << cache->referenceFont().familyName()
335 << cache->referenceFont().styleName()
339 << key.faceId.filename;
341 qCDebug(lcGlyphCaches()) <<
"Found existing glyph cache:"
343 <<
"fontEngine:" << fontEngineOfRawFont(cache->referenceFont())
344 << cache->referenceFont().familyName()
345 << cache->referenceFont().styleName()
349 << key.faceId.filename;
374void QSGDefaultRenderContext::resetGlyphCacheResources()
376 if (m_glyphCacheResourceUpdates) {
377 m_glyphCacheResourceUpdates->release();
378 m_glyphCacheResourceUpdates =
nullptr;
381 for (QRhiTexture *t : std::as_const(m_pendingGlyphCacheTextures))
384 m_pendingGlyphCacheTextures.clear();
396QSharedPointer<QSGDepthStencilBuffer> QSGDefaultRenderContext::getDepthStencilBuffer(
const QSize &size,
int sampleCount)
398 const auto key = std::pair(size, sampleCount);
399 auto it = m_depthStencilBuffers.constFind(key);
400 if (it != m_depthStencilBuffers.cend()) {
402 return it.value().toStrongRef();
405 QRhiRenderBuffer *ds = m_rhi->newRenderBuffer(QRhiRenderBuffer::DepthStencil, size, sampleCount);
407 qWarning(
"Failed to build depth-stencil buffer for layer");
411 QSharedPointer<QSGDepthStencilBuffer> buf(
new QSGDepthStencilBuffer(ds));
412 addDepthStencilBuffer(buf);
416void QSGDefaultRenderContext::addDepthStencilBuffer(
const QSharedPointer<QSGDepthStencilBuffer> &ds)
419 const auto key = std::pair(ds->ds->pixelSize(), ds->ds->sampleCount());
421 m_depthStencilBuffers.insert(key, ds.toWeakRef());
static QFontEngine * fontEngineOfRawFont(const QRawFont &font)