Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qbackingstoredefaultcompositor.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6#include <QtGui/qtransform.h>
7#include <QtGui/private/qmath_p.h>
8#include <QtGui/private/qwindow_p.h>
9#include <qpa/qplatformgraphicsbuffer.h>
10#include <QtCore/qfile.h>
11
13
14using namespace Qt::StringLiterals;
15
20
22{
23 m_rhi = nullptr;
24 m_psNoBlend.reset();
25 m_psBlend.reset();
26 m_psPremulBlend.reset();
27 m_samplerNearest.reset();
28 m_samplerLinear.reset();
29 m_vbuf.reset();
30 m_texture.reset();
31 m_widgetQuadData.reset();
32 for (PerQuadData &d : m_textureQuadData)
33 d.reset();
34}
35
36QRhiTexture *QBackingStoreDefaultCompositor::toTexture(const QPlatformBackingStore *backingStore,
37 QRhi *rhi,
38 QRhiResourceUpdateBatch *resourceUpdates,
39 const QRegion &dirtyRegion,
40 QPlatformBackingStore::TextureFlags *flags) const
41{
42 return toTexture(backingStore->toImage(), rhi, resourceUpdates, dirtyRegion, flags);
43}
44
45QRhiTexture *QBackingStoreDefaultCompositor::toTexture(const QImage &sourceImage,
46 QRhi *rhi,
47 QRhiResourceUpdateBatch *resourceUpdates,
48 const QRegion &dirtyRegion,
49 QPlatformBackingStore::TextureFlags *flags) const
50{
51 Q_ASSERT(rhi);
52 Q_ASSERT(resourceUpdates);
53 Q_ASSERT(flags);
54
55 if (!m_rhi) {
56 m_rhi = rhi;
57 } else if (m_rhi != rhi) {
58 qWarning("QBackingStoreDefaultCompositor: the QRhi has changed unexpectedly, this should not happen");
59 return nullptr;
60 }
61
62 QImage image = sourceImage;
63
64 bool needsConversion = false;
65 *flags = {};
66
67 switch (image.format()) {
68 case QImage::Format_ARGB32_Premultiplied:
69 *flags |= QPlatformBackingStore::TexturePremultiplied;
70 Q_FALLTHROUGH();
71 case QImage::Format_RGB32:
72 case QImage::Format_ARGB32:
73 *flags |= QPlatformBackingStore::TextureSwizzle;
74 break;
75 case QImage::Format_RGBA8888_Premultiplied:
76 *flags |= QPlatformBackingStore::TexturePremultiplied;
77 Q_FALLTHROUGH();
78 case QImage::Format_RGBX8888:
79 case QImage::Format_RGBA8888:
80 break;
81 case QImage::Format_BGR30:
82 case QImage::Format_A2BGR30_Premultiplied:
83 // no fast path atm
84 needsConversion = true;
85 break;
86 case QImage::Format_RGB30:
87 case QImage::Format_A2RGB30_Premultiplied:
88 // no fast path atm
89 needsConversion = true;
90 break;
91 default:
92 needsConversion = true;
93 break;
94 }
95
96 if (image.size().isEmpty())
97 return nullptr;
98
99 const bool resized = !m_texture || m_texture->pixelSize() != image.size();
100 if (dirtyRegion.isEmpty() && !resized)
101 return m_texture.get();
102
103 if (needsConversion)
104 image = image.convertToFormat(QImage::Format_RGBA8888);
105 else
106 image.detach(); // if it was just wrapping data, that's no good, we need ownership, so detach
107
108 if (resized) {
109 if (!m_texture)
110 m_texture.reset(rhi->newTexture(QRhiTexture::RGBA8, image.size()));
111 else
112 m_texture->setPixelSize(image.size());
113 m_texture->create();
114 resourceUpdates->uploadTexture(m_texture.get(), image);
115 } else {
116 QRect imageRect = image.rect();
117 QRect rect = dirtyRegion.boundingRect() & imageRect;
119 subresDesc.setSourceTopLeft(rect.topLeft());
120 subresDesc.setSourceSize(rect.size());
121 subresDesc.setDestinationTopLeft(rect.topLeft());
122 QRhiTextureUploadDescription uploadDesc(QRhiTextureUploadEntry(0, 0, subresDesc));
123 resourceUpdates->uploadTexture(m_texture.get(), uploadDesc);
124 }
125
126 return m_texture.get();
127}
128
129static inline QRect scaledRect(const QRect &rect, qreal factor)
130{
131 return qt_mapFillRect(rect, QTransform::fromScale(factor, factor));
132}
133
134static inline QPoint scaledOffset(const QPoint &pt, qreal factor)
135{
136 return pt * factor;
137}
138
139static QRegion scaledDirtyRegion(const QRegion &region, qreal factor, const QPoint &offset)
140{
141 if (offset.isNull() && factor <= 1)
142 return region;
143
144 QTransform xf = QTransform::fromScale(factor, factor);
145 xf.translate(offset.x(), offset.y());
146 return xf.map(region);
147}
148
149static QMatrix4x4 targetTransform(const QRectF &target, const QRect &viewport, bool invertY)
150{
151 qreal x_scale = target.width() / viewport.width();
152 qreal y_scale = target.height() / viewport.height();
153
154 const QPointF relative_to_viewport = target.topLeft() - viewport.topLeft();
155 qreal x_translate = x_scale - 1 + ((relative_to_viewport.x() / viewport.width()) * 2);
156 qreal y_translate;
157 if (invertY)
158 y_translate = y_scale - 1 + ((relative_to_viewport.y() / viewport.height()) * 2);
159 else
160 y_translate = -y_scale + 1 - ((relative_to_viewport.y() / viewport.height()) * 2);
161
162 QMatrix4x4 matrix;
163 matrix(0,3) = x_translate;
164 matrix(1,3) = y_translate;
165
166 matrix(0,0) = x_scale;
167 matrix(1,1) = (invertY ? -1.0 : 1.0) * y_scale;
168
169 return matrix;
170}
171
176
177static QMatrix3x3 sourceTransform(const QRectF &subTexture,
178 const QSize &textureSize,
180{
181 qreal x_scale = subTexture.width() / textureSize.width();
182 qreal y_scale = subTexture.height() / textureSize.height();
183
184 const QPointF topLeft = subTexture.topLeft();
185 qreal x_translate = topLeft.x() / textureSize.width();
186 qreal y_translate = topLeft.y() / textureSize.height();
187
188 if (origin == SourceTransformOrigin::TopLeft) {
189 y_scale = -y_scale;
190 y_translate = 1 - y_translate;
191 }
192
193 QMatrix3x3 matrix;
194 matrix(0,2) = x_translate;
195 matrix(1,2) = y_translate;
196
197 matrix(0,0) = x_scale;
198 matrix(1,1) = y_scale;
199
200 return matrix;
201}
202
203static inline QRect toBottomLeftRect(const QRect &topLeftRect, int windowHeight)
204{
205 return QRect(topLeftRect.x(), windowHeight - topLeftRect.bottomRight().y() - 1,
206 topLeftRect.width(), topLeftRect.height());
207}
208
209static bool prepareDrawForRenderToTextureWidget(const QPlatformTextureList *textures,
210 int idx,
211 QWindow *window,
212 const QRect &deviceWindowRect,
213 const QPoint &offset,
214 bool invertTargetY,
215 bool invertSource,
216 QMatrix4x4 *target,
217 QMatrix3x3 *source)
218{
219 const QRect clipRect = textures->clipRect(idx);
220 if (clipRect.isEmpty())
221 return false;
222
223 QRect rectInWindow = textures->geometry(idx);
224 // relative to the TLW, not necessarily our window (if the flush is for a native child widget), have to adjust
225 rectInWindow.translate(-offset);
226
227 const QRect clippedRectInWindow = rectInWindow & clipRect.translated(rectInWindow.topLeft());
228 const QRect srcRect = toBottomLeftRect(clipRect, rectInWindow.height());
229
230 *target = targetTransform(scaledRect(clippedRectInWindow, window->devicePixelRatio()),
231 deviceWindowRect,
232 invertTargetY);
233
234 *source = sourceTransform(scaledRect(srcRect, window->devicePixelRatio()),
235 scaledRect(rectInWindow, window->devicePixelRatio()).size(),
236 invertSource ? SourceTransformOrigin::TopLeft : SourceTransformOrigin::BottomLeft);
237
238 return true;
239}
240
241static QShader getShader(const QString &name)
242{
243 QFile f(name);
244 if (f.open(QIODevice::ReadOnly))
245 return QShader::fromSerialized(f.readAll());
246
247 qWarning("QBackingStoreDefaultCompositor: Could not find built-in shader %s "
248 "(is something wrong with QtGui library resources?)",
249 qPrintable(name));
250 return QShader();
251}
252
253static void updateMatrix3x3(QRhiResourceUpdateBatch *resourceUpdates, QRhiBuffer *ubuf, const QMatrix3x3 &m)
254{
255 // mat3 is still 4 floats per column in the uniform buffer (but there is no
256 // 4th column), so 48 bytes altogether, not 36 or 64.
257
258 float f[12];
259 const float *src = static_cast<const float *>(m.constData());
260 float *dst = f;
261 memcpy(dst, src, 3 * sizeof(float));
262 memcpy(dst + 4, src + 3, 3 * sizeof(float));
263 memcpy(dst + 8, src + 6, 3 * sizeof(float));
264
265 resourceUpdates->updateDynamicBuffer(ubuf, 64, 48, f);
266}
267
273
275 QRhiShaderResourceBindings *srb,
276 QRhiRenderPassDescriptor *rpDesc,
277 PipelineBlend blend)
278{
279 QRhiGraphicsPipeline *ps = rhi->newGraphicsPipeline();
280
281 switch (blend) {
283 {
284 QRhiGraphicsPipeline::TargetBlend blend;
285 blend.enable = true;
286 blend.srcColor = QRhiGraphicsPipeline::SrcAlpha;
287 blend.dstColor = QRhiGraphicsPipeline::OneMinusSrcAlpha;
288 blend.srcAlpha = QRhiGraphicsPipeline::One;
289 blend.dstAlpha = QRhiGraphicsPipeline::One;
290 ps->setTargetBlends({ blend });
291 }
292 break;
294 {
295 QRhiGraphicsPipeline::TargetBlend blend;
296 blend.enable = true;
297 blend.srcColor = QRhiGraphicsPipeline::One;
298 blend.dstColor = QRhiGraphicsPipeline::OneMinusSrcAlpha;
299 blend.srcAlpha = QRhiGraphicsPipeline::One;
300 blend.dstAlpha = QRhiGraphicsPipeline::One;
301 ps->setTargetBlends({ blend });
302 }
303 break;
304 default:
305 break;
306 }
307
308 ps->setShaderStages({
309 { QRhiShaderStage::Vertex, getShader(":/qt-project.org/gui/painting/shaders/backingstorecompose.vert.qsb"_L1) },
310 { QRhiShaderStage::Fragment, getShader(":/qt-project.org/gui/painting/shaders/backingstorecompose.frag.qsb"_L1) }
311 });
312 QRhiVertexInputLayout inputLayout;
313 inputLayout.setBindings({ { 5 * sizeof(float) } });
314 inputLayout.setAttributes({
315 { 0, 0, QRhiVertexInputAttribute::Float3, 0 },
316 { 0, 1, QRhiVertexInputAttribute::Float2, quint32(3 * sizeof(float)) }
317 });
318 ps->setVertexInputLayout(inputLayout);
319 ps->setShaderResourceBindings(srb);
320 ps->setRenderPassDescriptor(rpDesc);
321
322 if (!ps->create()) {
323 qWarning("QBackingStoreDefaultCompositor: Failed to build graphics pipeline");
324 delete ps;
325 return nullptr;
326 }
327 return ps;
328}
329
330static const int UBUF_SIZE = 120;
331
332QBackingStoreDefaultCompositor::PerQuadData QBackingStoreDefaultCompositor::createPerQuadData(QRhiTexture *texture, QRhiTexture *textureExtra)
333{
334 PerQuadData d;
335
336 d.ubuf = m_rhi->newBuffer(QRhiBuffer::Dynamic, QRhiBuffer::UniformBuffer, UBUF_SIZE);
337 if (!d.ubuf->create())
338 qWarning("QBackingStoreDefaultCompositor: Failed to create uniform buffer");
339
340 d.srb = m_rhi->newShaderResourceBindings();
341 d.srb->setBindings({
342 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf, 0, UBUF_SIZE),
343 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, texture, m_samplerNearest.get())
344 });
345 if (!d.srb->create())
346 qWarning("QBackingStoreDefaultCompositor: Failed to create srb");
347 d.lastUsedTexture = texture;
348
349 if (textureExtra) {
350 d.srbExtra = m_rhi->newShaderResourceBindings();
351 d.srbExtra->setBindings({
352 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d.ubuf, 0, UBUF_SIZE),
353 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, textureExtra, m_samplerNearest.get())
354 });
355 if (!d.srbExtra->create())
356 qWarning("QBackingStoreDefaultCompositor: Failed to create srb");
357 }
358
359 d.lastUsedTextureExtra = textureExtra;
360
361 return d;
362}
363
364void QBackingStoreDefaultCompositor::updatePerQuadData(PerQuadData *d, QRhiTexture *texture, QRhiTexture *textureExtra,
365 UpdateQuadDataOptions options)
366{
367 // This whole check-if-texture-ptr-is-different is needed because there is
368 // nothing saying a QPlatformTextureList cannot return a different
369 // QRhiTexture* from the same index in a subsequent flush.
370
371 const QRhiSampler::Filter filter = options.testFlag(NeedsLinearFiltering) ? QRhiSampler::Linear : QRhiSampler::Nearest;
372 if ((d->lastUsedTexture == texture && d->lastUsedFilter == filter) || !d->srb)
373 return;
374
375 QRhiSampler *sampler = filter == QRhiSampler::Linear ? m_samplerLinear.get() : m_samplerNearest.get();
376 d->srb->setBindings({
377 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d->ubuf, 0, UBUF_SIZE),
378 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, texture, sampler)
379 });
380
381 d->srb->updateResources(QRhiShaderResourceBindings::BindingsAreSorted);
382 d->lastUsedTexture = texture;
383 d->lastUsedFilter = filter;
384
385 if (textureExtra) {
386 // The PerQuadData slot may be reused for a stereo (two-texture) widget
387 // after having been created for a non-stereo (single-texture) one, in
388 // which case srbExtra was never allocated.
389 const bool needsCreate = !d->srbExtra;
390 if (needsCreate)
391 d->srbExtra = m_rhi->newShaderResourceBindings();
392
393 d->srbExtra->setBindings({
394 QRhiShaderResourceBinding::uniformBuffer(0, QRhiShaderResourceBinding::VertexStage | QRhiShaderResourceBinding::FragmentStage, d->ubuf, 0, UBUF_SIZE),
395 QRhiShaderResourceBinding::sampledTexture(1, QRhiShaderResourceBinding::FragmentStage, textureExtra, sampler)
396 });
397
398 if (needsCreate) {
399 if (!d->srbExtra->create())
400 qWarning("QBackingStoreDefaultCompositor: Failed to create srb");
401 } else {
402 d->srbExtra->updateResources(QRhiShaderResourceBindings::BindingsAreSorted);
403 }
404 d->lastUsedTextureExtra = textureExtra;
405 }
406}
407
408void QBackingStoreDefaultCompositor::updateUniforms(PerQuadData *d, QRhiResourceUpdateBatch *resourceUpdates,
409 const QMatrix4x4 &target, const QMatrix3x3 &source,
410 UpdateUniformOptions options)
411{
412 resourceUpdates->updateDynamicBuffer(d->ubuf, 0, 64, target.constData());
413 updateMatrix3x3(resourceUpdates, d->ubuf, source);
414 float opacity = 1.0f;
415 resourceUpdates->updateDynamicBuffer(d->ubuf, 112, 4, &opacity);
416 qint32 textureSwizzle = options;
417 resourceUpdates->updateDynamicBuffer(d->ubuf, 116, 4, &textureSwizzle);
418}
419
420void QBackingStoreDefaultCompositor::ensureResources(QRhiResourceUpdateBatch *resourceUpdates, QRhiRenderPassDescriptor *rpDesc)
421{
422 static const float vertexData[] = {
423 -1, -1, 0, 0, 0,
424 -1, 1, 0, 0, 1,
425 1, -1, 0, 1, 0,
426 -1, 1, 0, 0, 1,
427 1, -1, 0, 1, 0,
428 1, 1, 0, 1, 1
429 };
430
431 if (!m_vbuf) {
432 m_vbuf.reset(m_rhi->newBuffer(QRhiBuffer::Immutable, QRhiBuffer::VertexBuffer, sizeof(vertexData)));
433 if (m_vbuf->create())
434 resourceUpdates->uploadStaticBuffer(m_vbuf.get(), vertexData);
435 else
436 qWarning("QBackingStoreDefaultCompositor: Failed to create vertex buffer");
437 }
438
439 if (!m_samplerNearest) {
440 m_samplerNearest.reset(m_rhi->newSampler(QRhiSampler::Nearest, QRhiSampler::Nearest, QRhiSampler::None,
441 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge));
442 if (!m_samplerNearest->create())
443 qWarning("QBackingStoreDefaultCompositor: Failed to create sampler (Nearest filtering)");
444 }
445
446 if (!m_samplerLinear) {
447 m_samplerLinear.reset(m_rhi->newSampler(QRhiSampler::Linear, QRhiSampler::Linear, QRhiSampler::None,
448 QRhiSampler::ClampToEdge, QRhiSampler::ClampToEdge));
449 if (!m_samplerLinear->create())
450 qWarning("QBackingStoreDefaultCompositor: Failed to create sampler (Linear filtering)");
451 }
452
453 if (!m_widgetQuadData.isValid())
454 m_widgetQuadData = createPerQuadData(m_texture.get());
455
456 QRhiShaderResourceBindings *srb = m_widgetQuadData.srb; // just for the layout
457 if (!m_psNoBlend)
458 m_psNoBlend.reset(createGraphicsPipeline(m_rhi, srb, rpDesc, PipelineBlend::None));
459 if (!m_psBlend)
460 m_psBlend.reset(createGraphicsPipeline(m_rhi, srb, rpDesc, PipelineBlend::Alpha));
461 if (!m_psPremulBlend)
462 m_psPremulBlend.reset(createGraphicsPipeline(m_rhi, srb, rpDesc, PipelineBlend::PremulAlpha));
463}
464
466 QRhi *rhi,
467 QRhiSwapChain *swapchain,
468 QWindow *window,
469 qreal sourceDevicePixelRatio,
470 const QRegion &region,
471 const QPoint &offset,
472 QPlatformTextureList *textures,
473 bool translucentBackground,
474 qreal sourceTransformFactor)
475{
476 if (!rhi || !swapchain)
477 return QPlatformBackingStore::FlushFailed;
478
479 // Note, the sourceTransformFactor is different from the sourceDevicePixelRatio,
480 // as the former may reflect the fact that the region and offset is pre-transformed,
481 // in which case we don't need to do a full transform here based on the source DPR.
482 // In the default case where no explicit source transform has been passed, we fall
483 // back to the source device pixel ratio.
484 if (!sourceTransformFactor)
485 sourceTransformFactor = sourceDevicePixelRatio;
486
487 Q_ASSERT(textures); // may be empty if there are no render-to-texture widgets at all, but null it cannot be
488
489 if (!m_rhi) {
490 m_rhi = rhi;
491 } else if (m_rhi != rhi) {
492 qWarning("QBackingStoreDefaultCompositor: the QRhi has changed unexpectedly, this should not happen");
493 return QPlatformBackingStore::FlushFailed;
494 }
495
496 if (!qt_window_private(window)->receivedExpose)
497 return QPlatformBackingStore::FlushSuccess;
498
499 qCDebug(lcQpaBackingStore) << "Composing and flushing" << region << "of" << window
500 << "at offset" << offset << "with" << textures->count() << "texture(s) in" << textures
501 << "via swapchain" << swapchain;
502
503 QWindowPrivate::get(window)->lastComposeTime.start();
504
505 if (swapchain->currentPixelSize() != swapchain->surfacePixelSize())
506 swapchain->createOrResize();
507
508 // Start recording a new frame.
509 QRhi::FrameOpResult frameResult = rhi->beginFrame(swapchain);
510 if (frameResult == QRhi::FrameOpSwapChainOutOfDate) {
511 if (!swapchain->createOrResize())
512 return QPlatformBackingStore::FlushFailed;
513 frameResult = rhi->beginFrame(swapchain);
514 }
515 if (frameResult == QRhi::FrameOpDeviceLost)
516 return QPlatformBackingStore::FlushFailedDueToLostDevice;
517 if (frameResult != QRhi::FrameOpSuccess)
518 return QPlatformBackingStore::FlushFailed;
519
520 // Prepare resource updates.
521 QRhiResourceUpdateBatch *resourceUpdates = rhi->nextResourceUpdateBatch();
522 QPlatformBackingStore::TextureFlags flags;
523
524 const QRegion dirtyRegion = scaledDirtyRegion(region, sourceTransformFactor, offset);
525 bool gotTextureFromGraphicsBuffer = false;
526 if (QPlatformGraphicsBuffer *graphicsBuffer = backingStore->graphicsBuffer()) {
527 if (graphicsBuffer->lock(QPlatformGraphicsBuffer::SWReadAccess)) {
528 const QImage::Format format = QImage::toImageFormat(graphicsBuffer->format());
529 const QSize size = graphicsBuffer->size();
530 QImage wrapperImage(graphicsBuffer->data(), size.width(), size.height(), graphicsBuffer->bytesPerLine(), format);
531 toTexture(wrapperImage, rhi, resourceUpdates, dirtyRegion, &flags);
532 gotTextureFromGraphicsBuffer = true;
533 graphicsBuffer->unlock();
534 if (graphicsBuffer->origin() == QPlatformGraphicsBuffer::OriginBottomLeft)
535 flags |= QPlatformBackingStore::TextureFlip;
536 }
537 }
538 if (!gotTextureFromGraphicsBuffer)
539 toTexture(backingStore, rhi, resourceUpdates, dirtyRegion, &flags);
540
541 ensureResources(resourceUpdates, swapchain->renderPassDescriptor());
542
543 UpdateUniformOptions uniformOptions;
544#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
545 if (flags & QPlatformBackingStore::TextureSwizzle)
546 uniformOptions |= NeedsRedBlueSwap;
547#else
548 if (flags & QPlatformBackingStore::TextureSwizzle)
549 uniformOptions |= NeedsAlphaRotate;
550#endif
551 const bool premultiplied = (flags & QPlatformBackingStore::TexturePremultiplied) != 0;
553 if (flags & QPlatformBackingStore::TextureFlip)
555
556 const qreal dpr = window->devicePixelRatio();
557 const QRect deviceWindowRect = scaledRect(QRect(QPoint(), window->size()), dpr);
558 const QRect sourceWindowRect = scaledRect(QRect(QPoint(), window->size()), sourceDevicePixelRatio);
559 // If sourceWindowRect is larger than deviceWindowRect, we are doing high
560 // DPI downscaling. In that case Linear filtering is a must, whereas for the
561 // 1:1 case Nearest must be used for Qt 5 visual compatibility.
562 const bool needsLinearSampler = sourceWindowRect.width() > deviceWindowRect.width()
563 && sourceWindowRect.height() > deviceWindowRect.height();
564
565 const bool invertTargetY = !rhi->isYUpInNDC();
566 const bool invertSource = !rhi->isYUpInFramebuffer();
567
568 if (m_texture) {
569 // The backingstore is for the entire tlw. In case of native children, offset tells the position
570 // relative to the tlw. The window rect is scaled by the source device pixel ratio to get
571 // the source rect.
572 const QPoint sourceWindowOffset = scaledOffset(offset, sourceTransformFactor);
573 const QRect srcRect = toBottomLeftRect(sourceWindowRect.translated(sourceWindowOffset), m_texture->pixelSize().height());
574 const QMatrix3x3 source = sourceTransform(srcRect, m_texture->pixelSize(), origin);
575 QMatrix4x4 target; // identity
576 if (invertTargetY)
577 target.data()[5] = -1.0f;
578 updateUniforms(&m_widgetQuadData, resourceUpdates, target, source, uniformOptions);
579 if (needsLinearSampler)
580 updatePerQuadData(&m_widgetQuadData, m_texture.get(), nullptr, NeedsLinearFiltering);
581 }
582
583 const int textureWidgetCount = textures->count();
584 const int oldTextureQuadDataCount = m_textureQuadData.size();
585 if (oldTextureQuadDataCount != textureWidgetCount) {
586 for (int i = textureWidgetCount; i < oldTextureQuadDataCount; ++i)
587 m_textureQuadData[i].reset();
588 m_textureQuadData.resize(textureWidgetCount);
589 }
590
591 for (int i = 0; i < textureWidgetCount; ++i) {
592 const bool invertSourceForTextureWidget = textures->flags(i).testFlag(QPlatformTextureList::MirrorVertically)
593 ? !invertSource : invertSource;
594 QMatrix4x4 target;
595 QMatrix3x3 source;
596 if (!prepareDrawForRenderToTextureWidget(textures, i, window, deviceWindowRect,
597 offset, invertTargetY, invertSourceForTextureWidget,
598 &target, &source))
599 {
600 m_textureQuadData[i].reset();
601 continue;
602 }
603 QRhiTexture *t = textures->texture(i);
604 QRhiTexture *tExtra = textures->textureExtra(i);
605 if (t) {
606 if (!m_textureQuadData[i].isValid())
607 m_textureQuadData[i] = createPerQuadData(t, tExtra);
608 else
609 updatePerQuadData(&m_textureQuadData[i], t, tExtra);
610 updateUniforms(&m_textureQuadData[i], resourceUpdates, target, source);
611 if (needsLinearSampler)
612 updatePerQuadData(&m_textureQuadData[i], t, tExtra, NeedsLinearFiltering);
613 } else {
614 m_textureQuadData[i].reset();
615 }
616 }
617
618 // Record the render pass (with committing the resource updates).
619 QRhiCommandBuffer *cb = swapchain->currentFrameCommandBuffer();
620 const QSize outputSizeInPixels = swapchain->currentPixelSize();
621 QColor clearColor = translucentBackground ? Qt::transparent : Qt::black;
622
623 cb->resourceUpdate(resourceUpdates);
624
625 auto render = [&](std::optional<QRhiSwapChain::StereoTargetBuffer> buffer = std::nullopt) {
626 QRhiRenderTarget* target = nullptr;
627 if (buffer.has_value())
628 target = swapchain->currentFrameRenderTarget(buffer.value());
629 else
630 target = swapchain->currentFrameRenderTarget();
631
632 cb->beginPass(target, clearColor, { 1.0f, 0 });
633
634 cb->setGraphicsPipeline(m_psNoBlend.get());
635 cb->setViewport({ 0, 0, float(outputSizeInPixels.width()), float(outputSizeInPixels.height()) });
636 QRhiCommandBuffer::VertexInput vbufBinding(m_vbuf.get(), 0);
637 cb->setVertexInput(0, 1, &vbufBinding);
638
639 // Textures for renderToTexture widgets.
640 for (int i = 0; i < textureWidgetCount; ++i) {
641 if (!textures->flags(i).testFlag(QPlatformTextureList::StacksOnTop)) {
642 if (m_textureQuadData[i].isValid()) {
643
644 QRhiShaderResourceBindings* srb = m_textureQuadData[i].srb;
645 if (buffer == QRhiSwapChain::RightBuffer && m_textureQuadData[i].srbExtra)
646 srb = m_textureQuadData[i].srbExtra;
647
648 cb->setShaderResources(srb);
649 cb->draw(6);
650 }
651 }
652 }
653
654 cb->setGraphicsPipeline(premultiplied ? m_psPremulBlend.get() : m_psBlend.get());
655
656 // Backingstore texture with the normal widgets.
657 if (m_texture) {
658 cb->setShaderResources(m_widgetQuadData.srb);
659 cb->draw(6);
660 }
661
662 // Textures for renderToTexture widgets that have WA_AlwaysStackOnTop set.
663 for (int i = 0; i < textureWidgetCount; ++i) {
664 const QPlatformTextureList::Flags flags = textures->flags(i);
665 if (flags.testFlag(QPlatformTextureList::StacksOnTop)) {
666 if (m_textureQuadData[i].isValid()) {
667 if (flags.testFlag(QPlatformTextureList::NeedsPremultipliedAlphaBlending))
668 cb->setGraphicsPipeline(m_psPremulBlend.get());
669 else
670 cb->setGraphicsPipeline(m_psBlend.get());
671
672 QRhiShaderResourceBindings* srb = m_textureQuadData[i].srb;
673 if (buffer == QRhiSwapChain::RightBuffer && m_textureQuadData[i].srbExtra)
674 srb = m_textureQuadData[i].srbExtra;
675
676 cb->setShaderResources(srb);
677 cb->draw(6);
678 }
679 }
680 }
681
682 cb->endPass();
683 };
684
685 if (swapchain->window()->format().stereo()) {
686 render(QRhiSwapChain::LeftBuffer);
687 render(QRhiSwapChain::RightBuffer);
688 } else
689 render();
690
691 rhi->endFrame(swapchain);
692
693 return QPlatformBackingStore::FlushSuccess;
694}
695
696QT_END_NAMESPACE
QRhiTexture * toTexture(const QPlatformBackingStore *backingStore, QRhi *rhi, QRhiResourceUpdateBatch *resourceUpdates, const QRegion &dirtyRegion, QPlatformBackingStore::TextureFlags *flags) const
QPlatformBackingStore::FlushResult flush(QPlatformBackingStore *backingStore, QRhi *rhi, QRhiSwapChain *swapchain, QWindow *window, qreal sourceDevicePixelRatio, const QRegion &region, const QPoint &offset, QPlatformTextureList *textures, bool translucentBackground, qreal sourceTransformFactor)
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:666
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:726
\inmodule QtGuiPrivate \inheaderfile rhi/qrhi.h
Definition qrhi.h:323
Combined button and popup list for selecting options.
static QMatrix4x4 targetTransform(const QRectF &target, const QRect &viewport, bool invertY)
static QRect scaledRect(const QRect &rect, qreal factor)
static QMatrix3x3 sourceTransform(const QRectF &subTexture, const QSize &textureSize, SourceTransformOrigin origin)
static void updateMatrix3x3(QRhiResourceUpdateBatch *resourceUpdates, QRhiBuffer *ubuf, const QMatrix3x3 &m)
static QRect toBottomLeftRect(const QRect &topLeftRect, int windowHeight)
static QShader getShader(const QString &name)
static QPoint scaledOffset(const QPoint &pt, qreal factor)
static bool prepareDrawForRenderToTextureWidget(const QPlatformTextureList *textures, int idx, QWindow *window, const QRect &deviceWindowRect, const QPoint &offset, bool invertTargetY, bool invertSource, QMatrix4x4 *target, QMatrix3x3 *source)
static const int UBUF_SIZE
static QRhiGraphicsPipeline * createGraphicsPipeline(QRhi *rhi, QRhiShaderResourceBindings *srb, QRhiRenderPassDescriptor *rpDesc, PipelineBlend blend)
static QRegion scaledDirtyRegion(const QRegion &region, qreal factor, const QPoint &offset)