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
qquickshapegenericrenderer.cpp
Go to the documentation of this file.
1// Copyright (C) 2024 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/private/qtriangulator_p.h>
7#include <QtGui/private/qtriangulatingstroker_p.h>
8#include <rhi/qrhi.h>
9#include <QSGVertexColorMaterial>
10#include <QSGTextureProvider>
11#include <private/qsgplaintexture_p.h>
12
13#include <QtQuick/private/qsggradientcache_p.h>
14
15#if QT_CONFIG(thread)
16#include <QThreadPool>
17#endif
18
20
21struct ColoredVertex // must match QSGGeometry::ColoredPoint2D
22{
23 float x, y;
25 void set(float nx, float ny, QQuickShapeGenericRenderer::Color4ub ncolor)
26 {
27 x = nx; y = ny; color = ncolor;
28 }
29};
30
32{
33 float r, g, b, a;
34 c.getRgbF(&r, &g, &b, &a);
36 uchar(qRound(r * a * 255)),
37 uchar(qRound(g * a * 255)),
38 uchar(qRound(b * a * 255)),
39 uchar(qRound(a * 255))
40 };
41 return color;
42}
43
44QQuickShapeGenericStrokeFillNode::QQuickShapeGenericStrokeFillNode(QQuickWindow *window)
45 : m_material(nullptr)
46{
47 setFlag(QSGNode::OwnsGeometry, true);
48 setFlag(QSGNode::UsePreprocess, true);
49 setGeometry(new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(), 0, 0));
50 activateMaterial(window, MatSolidColor);
52 qsgnode_set_description(this, QLatin1String("stroke-fill"));
53#endif
54}
55
57{
58 switch (m) {
59 case MatSolidColor:
60 // Use vertexcolor material. Items with different colors remain batchable
61 // this way, at the expense of having to provide per-vertex color values.
62 m_material.reset(QQuickShapeGenericMaterialFactory::createVertexColor(window));
63 break;
64 case MatLinearGradient:
65 m_material.reset(QQuickShapeGenericMaterialFactory::createLinearGradient(window, this));
66 break;
67 case MatRadialGradient:
68 m_material.reset(QQuickShapeGenericMaterialFactory::createRadialGradient(window, this));
69 break;
70 case MatConicalGradient:
71 m_material.reset(QQuickShapeGenericMaterialFactory::createConicalGradient(window, this));
72 break;
73 case MatTextureFill:
74 m_material.reset(QQuickShapeGenericMaterialFactory::createTextureFill(window, this));
75 break;
76 default:
77 qWarning("Unknown material %d", m);
78 return;
79 }
80
81 if (material() != m_material.data())
82 setMaterial(m_material.data());
83}
84
86{
87 if (m_fillTextureProvider != nullptr) {
88 if (QSGDynamicTexture *texture = qobject_cast<QSGDynamicTexture *>(m_fillTextureProvider->texture()))
89 texture->updateTexture();
90 }
91}
92
93void QQuickShapeGenericStrokeFillNode::handleTextureChanged()
94{
95 markDirty(QSGNode::DirtyMaterial);
96}
97
98void QQuickShapeGenericStrokeFillNode::handleTextureProviderDestroyed()
99{
100 m_fillTextureProvider = nullptr;
101 markDirty(QSGNode::DirtyMaterial);
102}
103
105{
106 for (ShapePathData &d : m_sp) {
107 if (d.pendingFill)
108 d.pendingFill->orphaned = true;
109 if (d.pendingStroke)
110 d.pendingStroke->orphaned = true;
111 }
112}
113
114// sync, and so triangulation too, happens on the gui thread
115// - except when async is set, in which case triangulation is moved to worker threads
116
117void QQuickShapeGenericRenderer::beginSync(int totalCount, bool *countChanged)
118{
119 for (int i = totalCount; i < m_sp.size(); i++) // Handle removal of paths
120 setFillTextureProvider(i, nullptr); // deref window
121
122 if (m_sp.size() != totalCount) {
123 m_sp.resize(totalCount);
124 m_accDirty |= DirtyList;
125 *countChanged = true;
126 } else {
127 *countChanged = false;
128 }
129 for (ShapePathData &d : m_sp)
130 d.syncDirty = 0;
131}
132
133void QQuickShapeGenericRenderer::setPath(int index, const QPainterPath &path, QQuickShapePath::PathHints)
134{
135 ShapePathData &d(m_sp[index]);
136 d.path = path;
137 d.syncDirty |= DirtyFillGeom | DirtyStrokeGeom;
138}
139
140void QQuickShapeGenericRenderer::setStrokeColor(int index, const QColor &color)
141{
142 ShapePathData &d(m_sp[index]);
143 const bool wasTransparent = d.strokeColor.a == 0;
144 d.strokeColor = colorToColor4ub(color);
145 const bool isTransparent = d.strokeColor.a == 0;
146 d.syncDirty |= DirtyColor;
147 if (wasTransparent && !isTransparent)
148 d.syncDirty |= DirtyStrokeGeom;
149}
150
152{
153 ShapePathData &d(m_sp[index]);
154 d.strokeWidth = w;
155 if (w > 0.0f)
156 d.pen.setWidthF(w);
157 d.syncDirty |= DirtyStrokeGeom;
158}
159
161{
162 ShapePathData &d(m_sp[index]);
163 d.pen.setCosmetic(c);
164 d.syncDirty |= DirtyStrokeGeom;
165 // as long as the stroke is cosmetic,
166 // QQuickShape::itemChange triggers re-triangulation whenever scale changes
167}
168
169void QQuickShapeGenericRenderer::setFillColor(int index, const QColor &color)
170{
171 ShapePathData &d(m_sp[index]);
172 const bool wasTransparent = d.fillColor.a == 0;
173 d.fillColor = colorToColor4ub(color);
174 const bool isTransparent = d.fillColor.a == 0;
175 d.syncDirty |= DirtyColor;
176 if (wasTransparent && !isTransparent)
177 d.syncDirty |= DirtyFillGeom;
178}
179
180void QQuickShapeGenericRenderer::setFillRule(int index, QQuickShapePath::FillRule fillRule)
181{
182 ShapePathData &d(m_sp[index]);
183 d.fillRule = Qt::FillRule(fillRule);
184 d.syncDirty |= DirtyFillGeom;
185}
186
187void QQuickShapeGenericRenderer::setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit)
188{
189 ShapePathData &d(m_sp[index]);
190 d.pen.setJoinStyle(Qt::PenJoinStyle(joinStyle));
191 d.pen.setMiterLimit(miterLimit);
192 d.syncDirty |= DirtyStrokeGeom;
193}
194
195void QQuickShapeGenericRenderer::setCapStyle(int index, QQuickShapePath::CapStyle capStyle)
196{
197 ShapePathData &d(m_sp[index]);
198 d.pen.setCapStyle(Qt::PenCapStyle(capStyle));
199 d.syncDirty |= DirtyStrokeGeom;
200}
201
202void QQuickShapeGenericRenderer::setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle,
203 qreal dashOffset, const QList<qreal> &dashPattern)
204{
205 ShapePathData &d(m_sp[index]);
206 d.pen.setStyle(Qt::PenStyle(strokeStyle));
207 if (strokeStyle == QQuickShapePath::DashLine) {
208 d.pen.setDashPattern(dashPattern);
209 d.pen.setDashOffset(dashOffset);
210 }
211 d.syncDirty |= DirtyStrokeGeom;
212}
213
215 copyGenericGradient(const QQuickShapeGradient *gradient, QSGGradientCache::GradientDesc *dst)
216{
217 Q_ASSERT(dst != nullptr);
218 if (gradient == nullptr)
219 return QQuickAbstractPathRenderer::NoGradient;
220
221 dst->stops = gradient->gradientStops(); // sorted
222 dst->spread = QGradient::Spread(gradient->spread());
223 if (const QQuickShapeLinearGradient *g = qobject_cast<const QQuickShapeLinearGradient *>(gradient)) {
224 dst->a = QPointF(g->x1(), g->y1());
225 dst->b = QPointF(g->x2(), g->y2());
226
227 return QQuickAbstractPathRenderer::LinearGradient;
228 } else if (const QQuickShapeRadialGradient *g = qobject_cast<const QQuickShapeRadialGradient *>(gradient)) {
229 dst->a = QPointF(g->centerX(), g->centerY());
230 dst->b = QPointF(g->focalX(), g->focalY());
231 dst->v0 = g->centerRadius();
232 dst->v1 = g->focalRadius();
233
234 return QQuickAbstractPathRenderer::RadialGradient;
235 } else if (const QQuickShapeConicalGradient *g = qobject_cast<const QQuickShapeConicalGradient *>(gradient)) {
236 dst->a = QPointF(g->centerX(), g->centerY());
237 dst->v0 = g->angle();
238
239 return QQuickAbstractPathRenderer::ConicalGradient;
240 }
241
242 Q_UNREACHABLE_RETURN(QQuickAbstractPathRenderer::NoGradient);
243}
244
245void QQuickShapeGenericRenderer::setFillGradient(int index, QQuickShapeGradient *gradient)
246{
247 ShapePathData &d(m_sp[index]);
248 d.fillGradientActive = copyGenericGradient(gradient, &d.fillGradient);
249 d.syncDirty |= DirtyFillGradient;
250}
251
252void QQuickShapeGenericRenderer::setStrokeGradient(int index, QQuickShapeGradient *gradient)
253{
254 ShapePathData &d(m_sp[index]);
255 d.strokeGradientActive = copyGenericGradient(gradient, &d.strokeGradient);
256 d.syncDirty |= DirtyStrokeGradient;
257}
258
259void QQuickShapeGenericRenderer::setFillTextureProvider(int index, QQuickItem *textureProviderItem)
260{
261 ShapePathData &d(m_sp[index]);
262 if ((d.fillTextureProviderItem == nullptr) != (textureProviderItem == nullptr))
263 d.syncDirty |= DirtyFillGeom;
264 if (d.fillTextureProviderItem != nullptr)
265 QQuickItemPrivate::get(d.fillTextureProviderItem)->derefWindow();
266 d.fillTextureProviderItem = textureProviderItem;
267 if (d.fillTextureProviderItem != nullptr)
268 QQuickItemPrivate::get(d.fillTextureProviderItem)->refWindow(m_item->window());
269 d.syncDirty |= DirtyFillTexture;
270}
271
273{
274 for (auto &pathData : m_sp) {
275 if (pathData.fillTextureProviderItem != nullptr) {
276 if (window == nullptr)
277 QQuickItemPrivate::get(pathData.fillTextureProviderItem)->derefWindow();
278 else
279 QQuickItemPrivate::get(pathData.fillTextureProviderItem)->refWindow(window);
280 }
281 }
282}
283
284
285void QQuickShapeGenericRenderer::setFillTransform(int index, const QSGTransform &transform)
286{
287 ShapePathData &d(m_sp[index]);
288 d.fillTransform = transform;
289 d.syncDirty |= DirtyFillTransform;
290}
291
293{
294 ShapePathData &d(m_sp[index]);
295 d.triangulationScale = scale;
296 d.syncDirty |= DirtyStrokeGeom;
297}
298
300{
301 QQuickShapeGenericRenderer::triangulateFill(path, fillColor, &fillVertices, &fillIndices, &indexType,
302 supportsElementIndexUint, triangulationScale);
303 // Emit done signal on main thread, since connected to lambda there
304 QMetaObject::invokeMethod(qApp, [this]() { emit done(this); }, Qt::QueuedConnection);
305}
306
308{
309 QQuickShapeGenericRenderer::triangulateStroke(path, pen, strokeColor, &strokeVertices, clipSize, triangulationScale);
310 // Emit done signal on main thread, since connected to lambda there
311 QMetaObject::invokeMethod(qApp, [this]() { emit done(this); }, Qt::QueuedConnection);
312}
313
314void QQuickShapeGenericRenderer::setAsyncCallback(void (*callback)(void *), void *data)
315{
316 m_asyncCallback = callback;
317 m_asyncCallbackData = data;
318}
319
320#if QT_CONFIG(thread)
321static QThreadPool *pathWorkThreadPool = nullptr;
322
323static void deletePathWorkThreadPool()
324{
325 delete pathWorkThreadPool;
326 pathWorkThreadPool = nullptr;
327}
328#endif
329
331{
332#if !QT_CONFIG(thread)
333 // Force synchronous mode for the no-thread configuration due
334 // to lack of QThreadPool.
335 async = false;
336#endif
337
338 bool didKickOffAsync = false;
339
340 for (int i = 0; i < m_sp.size(); ++i) {
341 ShapePathData &d(m_sp[i]);
342 if (!d.syncDirty)
343 continue;
344
345 m_accDirty |= d.syncDirty;
346
347 // Use a shadow dirty flag in order to avoid losing state in case there are
348 // multiple syncs with different dirty flags before we get to updateNode()
349 // on the render thread (with the gui thread blocked). For our purposes
350 // here syncDirty is still required since geometry regeneration must only
351 // happen when there was an actual change in this particular sync round.
352 d.effectiveDirty |= d.syncDirty;
353
354 if (d.path.isEmpty()) {
355 d.fillVertices.clear();
356 d.fillIndices.clear();
357 d.strokeVertices.clear();
358 continue;
359 }
360
361#if QT_CONFIG(thread)
362 if (async && !pathWorkThreadPool) {
363 qAddPostRoutine(deletePathWorkThreadPool);
364 pathWorkThreadPool = new QThreadPool;
365 const int idealCount = QThread::idealThreadCount();
366 pathWorkThreadPool->setMaxThreadCount(idealCount > 0 ? idealCount * 2 : 4);
367 }
368#endif
369
370 // RHI backend might not have had a chance of being initialized yet
371 // (we are called in the mainthread while renderthread might be in the
372 // process of starting up and setting the RHI backend.
373 // m_rhiBackendInitialized is set in updateNode() on the render thread
374 // when both threads are locked.
375 // Until then, we cannot assume we have support for uint32_t indices
376 // but only uint16_t ones.
377 const bool supportsElementIndexUint = m_rhiBackendInitialized ? m_supportsElementIndexUint : true;
378 if ((d.syncDirty & DirtyFillGeom) && d.fillColor.a) {
379 d.path.setFillRule(d.fillRule);
380 if (m_api == QSGRendererInterface::Unknown)
381 m_api = m_item->window()->rendererInterface()->graphicsApi();
382 if (async) {
384 r->setAutoDelete(false);
385 if (d.pendingFill)
386 d.pendingFill->orphaned = true;
387 d.pendingFill = r;
388 r->path = d.path;
389 r->fillColor = d.fillColor;
390 r->supportsElementIndexUint = supportsElementIndexUint;
391 r->triangulationScale = d.triangulationScale;
392 // Unlikely in practice but in theory m_sp could be
393 // resized. Therefore, capture 'i' instead of 'd'.
394 QObject::connect(r, &QQuickShapeFillRunnable::done, qApp, [this, i](QQuickShapeFillRunnable *r) {
395 // Bail out when orphaned (meaning either another run was
396 // started after this one, or the renderer got destroyed).
397 if (!r->orphaned && i < m_sp.size()) {
398 ShapePathData &d(m_sp[i]);
399 d.fillVertices = r->fillVertices;
400 d.fillIndices = r->fillIndices;
401 d.indexType = r->indexType;
402 d.pendingFill = nullptr;
403 d.effectiveDirty |= DirtyFillGeom;
404 maybeUpdateAsyncItem();
405 }
406 r->deleteLater();
407 });
408 didKickOffAsync = true;
409#if QT_CONFIG(thread)
410 // qtVectorPathForPath() initializes a unique_ptr without locking.
411 // Do that before starting the threads as otherwise we get a race condition.
412 qtVectorPathForPath(r->path);
413 pathWorkThreadPool->start(r);
414#endif
415 } else {
416 triangulateFill(d.path, d.fillColor, &d.fillVertices, &d.fillIndices, &d.indexType,
417 supportsElementIndexUint,
418 d.triangulationScale);
419 }
420 }
421
422 if ((d.syncDirty & DirtyStrokeGeom) && d.strokeWidth > 0.0f && (d.strokeColor.a || d.strokeGradientActive)) {
423 if (async) {
425 r->setAutoDelete(false);
426 if (d.pendingStroke)
427 d.pendingStroke->orphaned = true;
428 d.pendingStroke = r;
429 r->path = d.path;
430 r->pen = d.pen;
431 r->strokeColor = d.strokeColor;
432 r->clipSize = QSize(m_item->width(), m_item->height());
433 r->triangulationScale = d.triangulationScale;
434 QObject::connect(r, &QQuickShapeStrokeRunnable::done, qApp, [this, i](QQuickShapeStrokeRunnable *r) {
435 if (!r->orphaned && i < m_sp.size()) {
436 ShapePathData &d(m_sp[i]);
437 d.strokeVertices = r->strokeVertices;
438 d.pendingStroke = nullptr;
439 d.effectiveDirty |= DirtyStrokeGeom;
440 maybeUpdateAsyncItem();
441 }
442 r->deleteLater();
443 });
444 didKickOffAsync = true;
445#if QT_CONFIG(thread)
446 // qtVectorPathForPath() initializes a unique_ptr without locking.
447 // Do that before starting the threads as otherwise we get a race condition.
448 qtVectorPathForPath(r->path);
449 pathWorkThreadPool->start(r);
450#endif
451 } else {
452 triangulateStroke(d.path, d.pen, d.strokeColor, &d.strokeVertices,
453 QSize(m_item->width(), m_item->height()), d.triangulationScale);
454 }
455 }
456 }
457
458 if (!didKickOffAsync && async && m_asyncCallback)
459 m_asyncCallback(m_asyncCallbackData);
460}
461
462void QQuickShapeGenericRenderer::maybeUpdateAsyncItem()
463{
464 for (const ShapePathData &d : std::as_const(m_sp)) {
465 if (d.pendingFill || d.pendingStroke)
466 return;
467 }
468 m_accDirty |= DirtyFillGeom | DirtyStrokeGeom;
469 m_item->update();
470 if (m_asyncCallback)
471 m_asyncCallback(m_asyncCallbackData);
472}
473
474// the stroke/fill triangulation functions may be invoked either on the gui
475// thread or some worker thread and must thus be self-contained.
476void QQuickShapeGenericRenderer::triangulateFill(const QPainterPath &path,
477 const Color4ub &fillColor,
478 VertexContainerType *fillVertices,
479 IndexContainerType *fillIndices,
480 QSGGeometry::Type *indexType,
481 bool supportsElementIndexUint,
482 qreal triangulationScale)
483{
484 const QVectorPath &vp = qtVectorPathForPath(path);
485
486 QTriangleSet ts = qTriangulate(vp, QTransform::fromScale(triangulationScale, triangulationScale), 1, supportsElementIndexUint);
487 const int vertexCount = ts.vertices.size() / 2; // just a qreal vector with x,y hence the / 2
488 fillVertices->resize(vertexCount);
489 ColoredVertex *vdst = reinterpret_cast<ColoredVertex *>(fillVertices->data());
490 const qreal *vsrc = ts.vertices.constData();
491 for (int i = 0; i < vertexCount; ++i)
492 vdst[i].set(vsrc[i * 2] / triangulationScale, vsrc[i * 2 + 1] / triangulationScale, fillColor);
493
494 size_t indexByteSize;
495 if (ts.indices.type() == QVertexIndexVector::UnsignedShort) {
496 *indexType = QSGGeometry::UnsignedShortType;
497 // fillIndices is still QList<quint32>. Just resize to N/2 and pack
498 // the N quint16s into it.
499 fillIndices->resize(ts.indices.size() / 2);
500 indexByteSize = ts.indices.size() * sizeof(quint16);
501 } else {
502 *indexType = QSGGeometry::UnsignedIntType;
503 fillIndices->resize(ts.indices.size());
504 indexByteSize = ts.indices.size() * sizeof(quint32);
505 }
506 memcpy(fillIndices->data(), ts.indices.data(), indexByteSize);
507}
508
509void QQuickShapeGenericRenderer::triangulateStroke(const QPainterPath &path,
510 const QPen &pen,
511 const Color4ub &strokeColor,
512 VertexContainerType *strokeVertices,
513 const QSize &clipSize,
514 qreal triangulationScale)
515{
516 const QVectorPath &vp = qtVectorPathForPath(path);
517 const QRectF clip(QPointF(0, 0), clipSize);
518 const qreal inverseScale = 1.0 / triangulationScale;
519
520 QTriangulatingStroker stroker;
521 stroker.setInvScale(inverseScale);
522
523 if (pen.style() == Qt::SolidLine) {
524 stroker.process(vp, pen, clip, {});
525 } else {
526 QDashedStrokeProcessor dashStroker;
527 dashStroker.setInvScale(inverseScale);
528 dashStroker.process(vp, pen, clip, {});
529 QVectorPath dashStroke(dashStroker.points(), dashStroker.elementCount(),
530 dashStroker.elementTypes(), 0);
531 stroker.process(dashStroke, pen, clip, {});
532 }
533
534 if (!stroker.vertexCount()) {
535 strokeVertices->clear();
536 return;
537 }
538
539 const int vertexCount = stroker.vertexCount() / 2; // just a float vector with x,y hence the / 2
540 strokeVertices->resize(vertexCount);
541 ColoredVertex *vdst = reinterpret_cast<ColoredVertex *>(strokeVertices->data());
542 const float *vsrc = stroker.vertices();
543 for (int i = 0; i < vertexCount; ++i)
544 vdst[i].set(vsrc[i * 2], vsrc[i * 2 + 1], strokeColor);
545}
546
548{
549 if (m_rootNode != node) {
550 m_rootNode = node;
551 m_accDirty |= DirtyList;
552 }
553}
554
555// on the render thread with gui blocked
557{
558 if (!m_rootNode || !m_accDirty)
559 return;
560
561 // Is it now safe to query the rhi backend
562 if (!m_rhiBackendInitialized) {
563 auto findRHIBackend = [](QQuickItem *item) -> QRhi * {
564 if (auto *w = item->window()) {
565 if (auto *rhi = QQuickWindowPrivate::get(w)->rhi)
566 return rhi;
567 }
568 return nullptr;
569 };
570 auto *rhi = findRHIBackend(m_item);
571 if (rhi != nullptr) {
572 m_rhiBackendInitialized = true;
573 m_supportsElementIndexUint = rhi->isFeatureSupported(QRhi::ElementIndexUint);
574 }
575 }
576
577// [ m_rootNode ]
578// / / /
579// #0 [ fill ] [ stroke ] [ next ]
580// / / |
581// #1 [ fill ] [ stroke ] [ next ]
582// / / |
583// #2 [ fill ] [ stroke ] [ next ]
584// ...
585// ...
586
587 QQuickShapeGenericNode **nodePtr = &m_rootNode;
588 QQuickShapeGenericNode *prevNode = nullptr;
589
590 for (ShapePathData &d : m_sp) {
591 if (!*nodePtr) {
592 Q_ASSERT(prevNode);
593 *nodePtr = new QQuickShapeGenericNode;
594 prevNode->m_next = *nodePtr;
595 prevNode->appendChildNode(*nodePtr);
596 }
597
598 QQuickShapeGenericNode *node = *nodePtr;
599
600 if (m_accDirty & DirtyList) {
601 d.effectiveDirty |= DirtyFillGeom | DirtyStrokeGeom | DirtyColor | DirtyFillGradient
602 | DirtyFillTransform | DirtyFillTexture | DirtyStrokeGradient;
603 }
604
605 if (!d.effectiveDirty) {
606 prevNode = node;
607 nodePtr = &node->m_next;
608 continue;
609 }
610
611 if (d.fillColor.a == 0) {
612 delete node->m_fillNode;
613 node->m_fillNode = nullptr;
614 } else if (!node->m_fillNode) {
615 node->m_fillNode = new QQuickShapeGenericStrokeFillNode(m_item->window());
616 if (node->m_strokeNode)
617 node->removeChildNode(node->m_strokeNode);
618 node->appendChildNode(node->m_fillNode);
619 if (node->m_strokeNode)
620 node->appendChildNode(node->m_strokeNode);
621 d.effectiveDirty |= DirtyFillGeom;
622 }
623
624 if (d.strokeWidth <= 0.0f || d.strokeColor.a == 0) {
625 delete node->m_strokeNode;
626 node->m_strokeNode = nullptr;
627 } else if (!node->m_strokeNode) {
628 node->m_strokeNode = new QQuickShapeGenericStrokeFillNode(m_item->window());
629 node->appendChildNode(node->m_strokeNode);
630 d.effectiveDirty |= DirtyStrokeGeom;
631 }
632
633 updateFillNode(&d, node);
634 updateStrokeNode(&d, node);
635
636 d.effectiveDirty = 0;
637
638 prevNode = node;
639 nodePtr = &node->m_next;
640 }
641
642 if (*nodePtr && prevNode) {
643 prevNode->removeChildNode(*nodePtr);
644 delete *nodePtr;
645 *nodePtr = nullptr;
646 }
647
648 if (m_sp.isEmpty()) {
649 delete m_rootNode->m_fillNode;
650 m_rootNode->m_fillNode = nullptr;
651 delete m_rootNode->m_strokeNode;
652 m_rootNode->m_strokeNode = nullptr;
653 delete m_rootNode->m_next;
654 m_rootNode->m_next = nullptr;
655 }
656
657 m_accDirty = 0;
658}
659
660void QQuickShapeGenericRenderer::updateShadowDataInNode(ShapePathData *d, QQuickShapeGenericStrokeFillNode *n)
661{
662 if (d->fillGradientActive) {
663 if (d->effectiveDirty & DirtyFillGradient)
664 n->m_gradient = d->fillGradient;
665 }
666
667 if (d->effectiveDirty & DirtyFillTexture) {
668 bool needsUpdate = d->fillTextureProviderItem == nullptr && n->m_fillTextureProvider != nullptr;
669 if (!needsUpdate
670 && d->fillTextureProviderItem != nullptr
671 && n->m_fillTextureProvider != d->fillTextureProviderItem->textureProvider()) {
672 needsUpdate = true;
673 }
674
675 if (needsUpdate) {
676 if (n->m_fillTextureProvider != nullptr) {
677 QObject::disconnect(n->m_fillTextureProvider, &QSGTextureProvider::textureChanged,
678 n, &QQuickShapeGenericStrokeFillNode::handleTextureChanged);
679 QObject::disconnect(n->m_fillTextureProvider, &QSGTextureProvider::destroyed,
680 n, &QQuickShapeGenericStrokeFillNode::handleTextureProviderDestroyed);
681 }
682
683 n->m_fillTextureProvider = d->fillTextureProviderItem == nullptr
684 ? nullptr
685 : d->fillTextureProviderItem->textureProvider();
686
687 if (n->m_fillTextureProvider != nullptr) {
688 QObject::connect(n->m_fillTextureProvider, &QSGTextureProvider::textureChanged,
689 n, &QQuickShapeGenericStrokeFillNode::handleTextureChanged);
690 QObject::connect(n->m_fillTextureProvider, &QSGTextureProvider::destroyed,
691 n, &QQuickShapeGenericStrokeFillNode::handleTextureProviderDestroyed);
692 }
693 }
694 }
695 if (d->effectiveDirty & DirtyFillTransform)
696 n->m_fillTransform = d->fillTransform;
697}
698
699void QQuickShapeGenericRenderer::updateFillNode(ShapePathData *d, QQuickShapeGenericNode *node)
700{
701 if (!node->m_fillNode)
702 return;
704 return;
705
706 // Make a copy of the data that will be accessed by the material on
707 // the render thread. This must be done even when we bail out below.
709 updateShadowDataInNode(d, n);
710
711 QSGGeometry *g = n->geometry();
712 if (d->fillVertices.isEmpty()) {
713 if (g->vertexCount() || g->indexCount()) {
714 g->allocate(0, 0);
715 n->markDirty(QSGNode::DirtyGeometry);
716 }
717 return;
718 }
719
720 if (d->fillGradientActive) {
722 switch (d->fillGradientActive) {
723 case LinearGradient:
725 break;
726 case RadialGradient:
728 break;
729 case ConicalGradient:
731 break;
732 default:
733 Q_UNREACHABLE_RETURN();
734 }
735 n->activateMaterial(m_item->window(), gradMat);
736 if (d->effectiveDirty & (DirtyFillGradient | DirtyFillTransform)) {
737 // Gradients are implemented via a texture-based material.
738 n->markDirty(QSGNode::DirtyMaterial);
739 // stop here if only the gradient or filltransform changed; no need to touch the geometry
740 if (!(d->effectiveDirty & DirtyFillGeom))
741 return;
742 }
743 } else if (d->fillTextureProviderItem != nullptr) {
744 n->activateMaterial(m_item->window(), QQuickShapeGenericStrokeFillNode::MatTextureFill);
745 if (d->effectiveDirty & DirtyFillTexture)
746 n->markDirty(QSGNode::DirtyMaterial);
747 } else {
748 n->activateMaterial(m_item->window(), QQuickShapeGenericStrokeFillNode::MatSolidColor);
749 // fast path for updating only color values when no change in vertex positions
750 if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyFillGeom) && d->fillTextureProviderItem == nullptr) {
751 ColoredVertex *vdst = reinterpret_cast<ColoredVertex *>(g->vertexData());
752 for (int i = 0; i < g->vertexCount(); ++i)
753 vdst[i].set(vdst[i].x, vdst[i].y, d->fillColor);
754 n->markDirty(QSGNode::DirtyGeometry);
755 return;
756 }
757 }
758
759 const int indexCount = d->indexType == QSGGeometry::UnsignedShortType
760 ? d->fillIndices.size() * 2 : d->fillIndices.size();
761 if (g->indexType() != d->indexType) {
762 g = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(),
763 d->fillVertices.size(), indexCount, d->indexType);
764 n->setGeometry(g);
765 } else {
766 g->allocate(d->fillVertices.size(), indexCount);
767 }
768 g->setDrawingMode(QSGGeometry::DrawTriangles);
769
770 memcpy(g->vertexData(), d->fillVertices.constData(), g->vertexCount() * g->sizeOfVertex());
771 memcpy(g->indexData(), d->fillIndices.constData(), g->indexCount() * g->sizeOfIndex());
772
773 n->markDirty(QSGNode::DirtyGeometry);
774}
775
776void QQuickShapeGenericRenderer::updateStrokeNode(ShapePathData *d, QQuickShapeGenericNode *node)
777{
778 if (!node->m_strokeNode)
779 return;
780 if (!(d->effectiveDirty & (DirtyStrokeGeom | DirtyColor | DirtyStrokeGradient)))
781 return;
782
784 if (d->strokeGradientActive) {
785 if (d->effectiveDirty & DirtyStrokeGradient)
786 n->m_gradient = d->strokeGradient;
787 }
788
789 QSGGeometry *g = n->geometry();
790 if (d->strokeVertices.isEmpty()) {
791 if (g->vertexCount() || g->indexCount()) {
792 g->allocate(0, 0);
793 n->markDirty(QSGNode::DirtyGeometry);
794 }
795 return;
796 }
797
798 n->markDirty(QSGNode::DirtyGeometry);
799
800 // Async loading runs update once, bails out above, then updates again once
801 // ready. Set the material dirty then. This is in-line with fill where the
802 // first activateMaterial() achieves the same.
803 if (!g->vertexCount())
804 n->markDirty(QSGNode::DirtyMaterial);
805
806 if (d->strokeGradientActive) {
808 switch (d->strokeGradientActive) {
809 case LinearGradient:
811 break;
812 case RadialGradient:
814 break;
815 case ConicalGradient:
817 break;
818 default:
819 Q_UNREACHABLE_RETURN();
820 }
821 n->activateMaterial(m_item->window(), gradMat);
822 if (d->effectiveDirty & DirtyStrokeGradient) {
823 // Gradients are implemented via a texture-based material.
824 n->markDirty(QSGNode::DirtyMaterial);
825 // stop here if only the gradient changed; no need to touch the geometry
826 if (!(d->effectiveDirty & DirtyStrokeGeom))
827 return;
828 }
829 } else {
830 n->activateMaterial(m_item->window(), QQuickShapeGenericStrokeFillNode::MatSolidColor);
831 if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyStrokeGeom)) {
832 ColoredVertex *vdst = reinterpret_cast<ColoredVertex *>(g->vertexData());
833 for (int i = 0; i < g->vertexCount(); ++i)
834 vdst[i].set(vdst[i].x, vdst[i].y, d->strokeColor);
835 return;
836 }
837 }
838
839 g->allocate(d->strokeVertices.size(), 0);
840 g->setDrawingMode(QSGGeometry::DrawTriangleStrip);
841 memcpy(g->vertexData(), d->strokeVertices.constData(), g->vertexCount() * g->sizeOfVertex());
842}
843
844QSGMaterial *QQuickShapeGenericMaterialFactory::createVertexColor(QQuickWindow *window)
845{
846 QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi();
847
848 if (api == QSGRendererInterface::OpenGL || QSGRendererInterface::isApiRhiBased(api))
849 return new QSGVertexColorMaterial;
850
851 qWarning("Vertex-color material: Unsupported graphics API %d", api);
852 return nullptr;
853}
854
855QSGMaterial *QQuickShapeGenericMaterialFactory::createLinearGradient(QQuickWindow *window,
857{
858 QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi();
859
860 if (api == QSGRendererInterface::OpenGL || QSGRendererInterface::isApiRhiBased(api))
862
863 qWarning("Linear gradient material: Unsupported graphics API %d", api);
864 return nullptr;
865}
866
867QSGMaterial *QQuickShapeGenericMaterialFactory::createRadialGradient(QQuickWindow *window,
869{
870 QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi();
871
872 if (api == QSGRendererInterface::OpenGL || QSGRendererInterface::isApiRhiBased(api))
874
875 qWarning("Radial gradient material: Unsupported graphics API %d", api);
876 return nullptr;
877}
878
879QSGMaterial *QQuickShapeGenericMaterialFactory::createConicalGradient(QQuickWindow *window,
881{
882 QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi();
883
884 if (api == QSGRendererInterface::OpenGL || QSGRendererInterface::isApiRhiBased(api))
886
887 qWarning("Conical gradient material: Unsupported graphics API %d", api);
888 return nullptr;
889}
890
891QSGMaterial *QQuickShapeGenericMaterialFactory::createTextureFill(QQuickWindow *window,
893{
894 QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi();
895
896 if (api == QSGRendererInterface::OpenGL || QSGRendererInterface::isApiRhiBased(api))
898
899 qWarning("Texture fill material: Unsupported graphics API %d", api);
900 return nullptr;
901}
902
904{
905 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/lineargradient.vert.qsb"), viewCount);
906 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/lineargradient.frag.qsb"), viewCount);
907}
908
910 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
911{
912 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
913 QQuickShapeLinearGradientMaterial *m = static_cast<QQuickShapeLinearGradientMaterial *>(newMaterial);
914 bool changed = false;
915 QByteArray *buf = state.uniformData();
916 Q_ASSERT(buf->size() >= 84 + 64);
917 const int shaderMatrixCount = newMaterial->viewCount();
918 const int matrixCount = qMin(state.projectionMatrixCount(), shaderMatrixCount);
919
920 if (state.isMatrixDirty()) {
921 for (int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
922 const QMatrix4x4 m = state.combinedMatrix();
923 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
924 changed = true;
925 }
926 }
927
929
930 if (!oldMaterial || m_fillTransform != node->m_fillTransform) {
931 memcpy(buf->data() + 64 * shaderMatrixCount, node->m_fillTransform.invertedData(), 64);
932 m_fillTransform = node->m_fillTransform;
933 changed = true;
934 }
935
936 if (!oldMaterial || m_gradA.x() != node->m_gradient.a.x() || m_gradA.y() != node->m_gradient.a.y()) {
937 m_gradA = QVector2D(node->m_gradient.a.x(), node->m_gradient.a.y());
938 Q_ASSERT(sizeof(m_gradA) == 8);
939 memcpy(buf->data() + 64 * shaderMatrixCount + 64, &m_gradA, 8);
940 changed = true;
941 }
942
943 if (!oldMaterial || m_gradB.x() != node->m_gradient.b.x() || m_gradB.y() != node->m_gradient.b.y()) {
944 m_gradB = QVector2D(node->m_gradient.b.x(), node->m_gradient.b.y());
945 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8, &m_gradB, 8);
946 changed = true;
947 }
948
949 if (state.isOpacityDirty()) {
950 const float opacity = state.opacity();
951 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8 + 8, &opacity, 4);
952 changed = true;
953 }
954
955 return changed;
956}
957
958void QQuickShapeLinearGradientRhiShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
959 QSGMaterial *newMaterial, QSGMaterial *)
960{
961 if (binding != 1)
962 return;
963
964 QQuickShapeLinearGradientMaterial *m = static_cast<QQuickShapeLinearGradientMaterial *>(newMaterial);
966 const QSGGradientCacheKey cacheKey(node->m_gradient.stops, QGradient::Spread(node->m_gradient.spread));
967 QSGTexture *t = QSGGradientCache::cacheForRhi(state.rhi())->get(cacheKey);
968 t->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
969 *texture = t;
970}
971
973{
974 static QSGMaterialType type;
975 return &type;
976}
977
978int QQuickShapeLinearGradientMaterial::compare(const QSGMaterial *other) const
979{
980 Q_ASSERT(other && type() == other->type());
981 const QQuickShapeLinearGradientMaterial *m = static_cast<const QQuickShapeLinearGradientMaterial *>(other);
982
985 Q_ASSERT(a && b);
986 if (a == b)
987 return 0;
988
989 const QSGGradientCache::GradientDesc *ga = &a->m_gradient;
990 const QSGGradientCache::GradientDesc *gb = &b->m_gradient;
991
992 if (int d = ga->spread - gb->spread)
993 return d;
994
995 if (int d = ga->a.x() - gb->a.x())
996 return d;
997 if (int d = ga->a.y() - gb->a.y())
998 return d;
999 if (int d = ga->b.x() - gb->b.x())
1000 return d;
1001 if (int d = ga->b.y() - gb->b.y())
1002 return d;
1003
1004 if (int d = ga->stops.size() - gb->stops.size())
1005 return d;
1006
1007 for (int i = 0; i < ga->stops.size(); ++i) {
1008 if (int d = ga->stops[i].first - gb->stops[i].first)
1009 return d;
1010 if (int d = ga->stops[i].second.rgba() - gb->stops[i].second.rgba())
1011 return d;
1012 }
1013
1014 if (int d = a->m_fillTransform.compareTo(b->m_fillTransform))
1015 return d;
1016
1017 return 0;
1018}
1019
1020QSGMaterialShader *QQuickShapeLinearGradientMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
1021{
1022 Q_UNUSED(renderMode);
1023 return new QQuickShapeLinearGradientRhiShader(viewCount());
1024}
1025
1027{
1028 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/radialgradient.vert.qsb"), viewCount);
1029 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/radialgradient.frag.qsb"), viewCount);
1030}
1031
1033 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
1034{
1035 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
1036 QQuickShapeRadialGradientMaterial *m = static_cast<QQuickShapeRadialGradientMaterial *>(newMaterial);
1037 bool changed = false;
1038 QByteArray *buf = state.uniformData();
1039 Q_ASSERT(buf->size() >= 92 + 64);
1040 const int shaderMatrixCount = newMaterial->viewCount();
1041 const int matrixCount = qMin(state.projectionMatrixCount(), shaderMatrixCount);
1042
1043 if (state.isMatrixDirty()) {
1044 for (int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
1045 const QMatrix4x4 m = state.combinedMatrix();
1046 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
1047 changed = true;
1048 }
1049 }
1050
1052
1053 if (!oldMaterial || m_fillTransform != node->m_fillTransform) {
1054 memcpy(buf->data() + 64 * shaderMatrixCount, node->m_fillTransform.invertedData(), 64);
1055 m_fillTransform = node->m_fillTransform;
1056 changed = true;
1057 }
1058
1059 const QPointF centerPoint = node->m_gradient.a;
1060 const QPointF focalPoint = node->m_gradient.b;
1061 const QPointF focalToCenter = centerPoint - focalPoint;
1062 const float centerRadius = node->m_gradient.v0;
1063 const float focalRadius = node->m_gradient.v1;
1064
1065 if (!oldMaterial || m_focalPoint.x() != focalPoint.x() || m_focalPoint.y() != focalPoint.y()) {
1066 m_focalPoint = QVector2D(focalPoint.x(), focalPoint.y());
1067 Q_ASSERT(sizeof(m_focalPoint) == 8);
1068 memcpy(buf->data() + 64 * shaderMatrixCount + 64, &m_focalPoint, 8);
1069 changed = true;
1070 }
1071
1072 if (!oldMaterial || m_focalToCenter.x() != focalToCenter.x() || m_focalToCenter.y() != focalToCenter.y()) {
1073 m_focalToCenter = QVector2D(focalToCenter.x(), focalToCenter.y());
1074 Q_ASSERT(sizeof(m_focalToCenter) == 8);
1075 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8, &m_focalToCenter, 8);
1076 changed = true;
1077 }
1078
1079 if (!oldMaterial || m_centerRadius != centerRadius) {
1080 m_centerRadius = centerRadius;
1081 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8 + 8, &m_centerRadius, 4);
1082 changed = true;
1083 }
1084
1085 if (!oldMaterial || m_focalRadius != focalRadius) {
1086 m_focalRadius = focalRadius;
1087 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8 + 8 + 4, &m_focalRadius, 4);
1088 changed = true;
1089 }
1090
1091 if (state.isOpacityDirty()) {
1092 const float opacity = state.opacity();
1093 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8 + 8 + 4 + 4, &opacity, 4);
1094 changed = true;
1095 }
1096
1097 return changed;
1098}
1099
1100void QQuickShapeRadialGradientRhiShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
1101 QSGMaterial *newMaterial, QSGMaterial *)
1102{
1103 if (binding != 1)
1104 return;
1105
1106 QQuickShapeRadialGradientMaterial *m = static_cast<QQuickShapeRadialGradientMaterial *>(newMaterial);
1108 const QSGGradientCacheKey cacheKey(node->m_gradient.stops, QGradient::Spread(node->m_gradient.spread));
1109 QSGTexture *t = QSGGradientCache::cacheForRhi(state.rhi())->get(cacheKey);
1110 t->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
1111 *texture = t;
1112}
1113
1115{
1116 static QSGMaterialType type;
1117 return &type;
1118}
1119
1120int QQuickShapeRadialGradientMaterial::compare(const QSGMaterial *other) const
1121{
1122 Q_ASSERT(other && type() == other->type());
1123 const QQuickShapeRadialGradientMaterial *m = static_cast<const QQuickShapeRadialGradientMaterial *>(other);
1124
1127 Q_ASSERT(a && b);
1128 if (a == b)
1129 return 0;
1130
1131 const QSGGradientCache::GradientDesc *ga = &a->m_gradient;
1132 const QSGGradientCache::GradientDesc *gb = &b->m_gradient;
1133
1134 if (int d = ga->spread - gb->spread)
1135 return d;
1136
1137 if (int d = ga->a.x() - gb->a.x())
1138 return d;
1139 if (int d = ga->a.y() - gb->a.y())
1140 return d;
1141 if (int d = ga->b.x() - gb->b.x())
1142 return d;
1143 if (int d = ga->b.y() - gb->b.y())
1144 return d;
1145
1146 if (int d = ga->v0 - gb->v0)
1147 return d;
1148 if (int d = ga->v1 - gb->v1)
1149 return d;
1150
1151 if (int d = ga->stops.size() - gb->stops.size())
1152 return d;
1153
1154 for (int i = 0; i < ga->stops.size(); ++i) {
1155 if (int d = ga->stops[i].first - gb->stops[i].first)
1156 return d;
1157 if (int d = ga->stops[i].second.rgba() - gb->stops[i].second.rgba())
1158 return d;
1159 }
1160
1161 if (int d = a->m_fillTransform.compareTo(b->m_fillTransform))
1162 return d;
1163
1164 return 0;
1165}
1166
1167QSGMaterialShader *QQuickShapeRadialGradientMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
1168{
1169 Q_UNUSED(renderMode);
1170 return new QQuickShapeRadialGradientRhiShader(viewCount());
1171}
1172
1174{
1175 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/conicalgradient.vert.qsb"), viewCount);
1176 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/conicalgradient.frag.qsb"), viewCount);
1177}
1178
1180 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
1181{
1182 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
1183 QQuickShapeConicalGradientMaterial *m = static_cast<QQuickShapeConicalGradientMaterial *>(newMaterial);
1184 bool changed = false;
1185 QByteArray *buf = state.uniformData();
1186 Q_ASSERT(buf->size() >= 80 + 64);
1187 const int shaderMatrixCount = newMaterial->viewCount();
1188 const int matrixCount = qMin(state.projectionMatrixCount(), shaderMatrixCount);
1189
1190 if (state.isMatrixDirty()) {
1191 for (int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
1192 const QMatrix4x4 m = state.combinedMatrix();
1193 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
1194 changed = true;
1195 }
1196 }
1197
1199
1200 if (!oldMaterial || m_fillTransform != node->m_fillTransform) {
1201 memcpy(buf->data() + 64 * shaderMatrixCount, node->m_fillTransform.invertedData(), 64);
1202 m_fillTransform = node->m_fillTransform;
1203 changed = true;
1204 }
1205
1206 const QPointF centerPoint = node->m_gradient.a;
1207 const float angle = -qDegreesToRadians(node->m_gradient.v0);
1208
1209 if (!oldMaterial || m_centerPoint.x() != centerPoint.x() || m_centerPoint.y() != centerPoint.y()) {
1210 m_centerPoint = QVector2D(centerPoint.x(), centerPoint.y());
1211 Q_ASSERT(sizeof(m_centerPoint) == 8);
1212 memcpy(buf->data() + 64 * shaderMatrixCount + 64, &m_centerPoint, 8);
1213 changed = true;
1214 }
1215
1216 if (!oldMaterial || m_angle != angle) {
1217 m_angle = angle;
1218 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8, &m_angle, 4);
1219 changed = true;
1220 }
1221
1222 if (state.isOpacityDirty()) {
1223 const float opacity = state.opacity();
1224 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8 + 4, &opacity, 4);
1225 changed = true;
1226 }
1227
1228 return changed;
1229}
1230
1231void QQuickShapeConicalGradientRhiShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
1232 QSGMaterial *newMaterial, QSGMaterial *)
1233{
1234 if (binding != 1)
1235 return;
1236
1237 QQuickShapeConicalGradientMaterial *m = static_cast<QQuickShapeConicalGradientMaterial *>(newMaterial);
1239 const QSGGradientCacheKey cacheKey(node->m_gradient.stops, QGradient::Spread(node->m_gradient.spread));
1240 QSGTexture *t = QSGGradientCache::cacheForRhi(state.rhi())->get(cacheKey);
1241 t->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
1242 *texture = t;
1243}
1244
1246{
1247 static QSGMaterialType type;
1248 return &type;
1249}
1250
1251int QQuickShapeConicalGradientMaterial::compare(const QSGMaterial *other) const
1252{
1253 Q_ASSERT(other && type() == other->type());
1254 const QQuickShapeConicalGradientMaterial *m = static_cast<const QQuickShapeConicalGradientMaterial *>(other);
1255
1258 Q_ASSERT(a && b);
1259 if (a == b)
1260 return 0;
1261
1262 const QSGGradientCache::GradientDesc *ga = &a->m_gradient;
1263 const QSGGradientCache::GradientDesc *gb = &b->m_gradient;
1264
1265 if (int d = ga->a.x() - gb->a.x())
1266 return d;
1267 if (int d = ga->a.y() - gb->a.y())
1268 return d;
1269
1270 if (int d = ga->v0 - gb->v0)
1271 return d;
1272
1273 if (int d = ga->stops.size() - gb->stops.size())
1274 return d;
1275
1276 for (int i = 0; i < ga->stops.size(); ++i) {
1277 if (int d = ga->stops[i].first - gb->stops[i].first)
1278 return d;
1279 if (int d = ga->stops[i].second.rgba() - gb->stops[i].second.rgba())
1280 return d;
1281 }
1282
1283 if (int d = a->m_fillTransform.compareTo(b->m_fillTransform))
1284 return d;
1285
1286 return 0;
1287}
1288
1289QSGMaterialShader *QQuickShapeConicalGradientMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
1290{
1291 Q_UNUSED(renderMode);
1292 return new QQuickShapeConicalGradientRhiShader(viewCount());
1293}
1294
1296{
1297 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/texturefill.vert.qsb"), viewCount);
1298 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/texturefill.frag.qsb"), viewCount);
1299}
1300
1302 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
1303{
1304 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
1305 QQuickShapeTextureFillMaterial *m = static_cast<QQuickShapeTextureFillMaterial *>(newMaterial);
1306 bool changed = false;
1307 QByteArray *buf = state.uniformData();
1308 const int shaderMatrixCount = newMaterial->viewCount();
1309 const int matrixCount = qMin(state.projectionMatrixCount(), shaderMatrixCount);
1310 Q_ASSERT(buf->size() >= 64 * shaderMatrixCount + 64 + 8 + 4);
1311
1312 if (state.isMatrixDirty()) {
1313 for (int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
1314 const QMatrix4x4 m = state.combinedMatrix();
1315 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
1316 changed = true;
1317 }
1318 }
1319
1321
1322 if (!oldMaterial || m_fillTransform != node->m_fillTransform) {
1323 memcpy(buf->data() + 64 * shaderMatrixCount, node->m_fillTransform.invertedData(), 64);
1324 m_fillTransform = node->m_fillTransform;
1325 changed = true;
1326 }
1327
1328 const QSizeF boundsSize = node->m_fillTextureProvider != nullptr && node->m_fillTextureProvider->texture() != nullptr
1329 ? node->m_fillTextureProvider->texture()->textureSize()
1330 : QSizeF(0, 0);
1331
1332
1333 const QVector2D boundsVector(boundsSize.width() / state.devicePixelRatio(),
1334 boundsSize.height() / state.devicePixelRatio());
1335 if (!oldMaterial || m_boundsSize != boundsVector) {
1336 m_boundsSize = boundsVector;
1337 Q_ASSERT(sizeof(m_boundsSize) == 8);
1338 memcpy(buf->data() + 64 * shaderMatrixCount + 64, &m_boundsSize, 8);
1339 changed = true;
1340 }
1341
1342 if (state.isOpacityDirty()) {
1343 const float opacity = state.opacity();
1344 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8, &opacity, 4);
1345 changed = true;
1346 }
1347
1348 return changed;
1349}
1350
1351void QQuickShapeTextureFillRhiShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
1352 QSGMaterial *newMaterial, QSGMaterial *)
1353{
1354 if (binding != 1)
1355 return;
1356
1357 QQuickShapeTextureFillMaterial *m = static_cast<QQuickShapeTextureFillMaterial *>(newMaterial);
1359 if (node->m_fillTextureProvider != nullptr) {
1360 QSGTexture *providedTexture = node->m_fillTextureProvider->texture();
1361 if (providedTexture != nullptr) {
1362 if (providedTexture->isAtlasTexture()) {
1363 // Create a non-atlas copy to make texture coordinate wrapping work. This
1364 // texture copy is owned by the QSGTexture so memory is managed with the original
1365 // texture provider.
1366 QSGTexture *newTexture = providedTexture->removedFromAtlas(state.resourceUpdateBatch());
1367 if (newTexture != nullptr)
1368 providedTexture = newTexture;
1369 }
1370
1371 providedTexture->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
1372 *texture = providedTexture;
1373 return;
1374 }
1375 }
1376
1377 if (m->dummyTexture() == nullptr) {
1378 QSGPlainTexture *dummyTexture = new QSGPlainTexture;
1379 dummyTexture->setFiltering(QSGTexture::Nearest);
1380 dummyTexture->setHorizontalWrapMode(QSGTexture::Repeat);
1381 dummyTexture->setVerticalWrapMode(QSGTexture::Repeat);
1382 QImage img(128, 128, QImage::Format_ARGB32_Premultiplied);
1383 img.fill(0);
1384 dummyTexture->setImage(img);
1385 dummyTexture->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
1386
1387 m->setDummyTexture(dummyTexture);
1388 }
1389
1390 *texture = m->dummyTexture();
1391}
1392
1394{
1395 delete m_dummyTexture;
1396}
1397
1399{
1400 static QSGMaterialType type;
1401 return &type;
1402}
1403
1404int QQuickShapeTextureFillMaterial::compare(const QSGMaterial *other) const
1405{
1406 Q_ASSERT(other && type() == other->type());
1407 const QQuickShapeTextureFillMaterial *m = static_cast<const QQuickShapeTextureFillMaterial *>(other);
1408
1411 Q_ASSERT(a && b);
1412 if (a == b)
1413 return 0;
1414
1415 if (int d = a->m_fillTransform.compareTo(b->m_fillTransform))
1416 return d;
1417
1418 const qintptr diff = qintptr(a->m_fillTextureProvider) - qintptr(b->m_fillTextureProvider);
1419 return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
1420}
1421
1422QSGMaterialShader *QQuickShapeTextureFillMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
1423{
1424 Q_UNUSED(renderMode);
1425 return new QQuickShapeTextureFillRhiShader(viewCount());
1426}
1427
1428QT_END_NAMESPACE
1429
1430#include "moc_qquickshapegenericrenderer_p.cpp"
QSGMaterialType * type() const override
This function is called by the scene graph to query an identifier that is unique to the QSGMaterialSh...
QQuickShapeConicalGradientMaterial(QQuickShapeGenericStrokeFillNode *node)
int compare(const QSGMaterial *other) const override
Compares this material to other and returns 0 if they are equal; -1 if this material should sort befo...
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode renderMode) const override
This function returns a new instance of a the QSGMaterialShader implementation used to render geometr...
QQuickShapeGenericStrokeFillNode * node() const
void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to prepare use of sampled images in the shader,...
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
QQuickShapeGenericRenderer::Color4ub fillColor
QQuickShapeGenericRenderer::VertexContainerType fillVertices
QQuickShapeGenericRenderer::IndexContainerType fillIndices
QQuickShapeGenericStrokeFillNode * m_fillNode
QQuickShapeGenericStrokeFillNode * m_strokeNode
void setAsyncCallback(void(*)(void *), void *) override
void setFillGradient(int index, QQuickShapeGradient *gradient) override
void setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle, qreal dashOffset, const QList< qreal > &dashPattern) override
void setFillTransform(int index, const QSGTransform &transform) override
void setCosmeticStroke(int index, bool c) override
void setStrokeGradient(int index, QQuickShapeGradient *gradient) override
QList< QSGGeometry::ColoredPoint2D > VertexContainerType
void setPath(int index, const QPainterPath &path, QQuickShapePath::PathHints pathHints={}) override
void setStrokeColor(int index, const QColor &color) override
void setFillRule(int index, QQuickShapePath::FillRule fillRule) override
void setFillTextureProvider(int index, QQuickItem *textureProviderItem) override
void setFillColor(int index, const QColor &color) override
void setTriangulationScale(int index, qreal scale) override
void setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit) override
void setStrokeWidth(int index, qreal w) override
void setRootNode(QQuickShapeGenericNode *node)
void handleSceneChange(QQuickWindow *window) override
void beginSync(int totalCount, bool *countChanged) override
void setCapStyle(int index, QQuickShapePath::CapStyle capStyle) override
void preprocess() override
Override this function to do processing on the node before it is rendered.
void activateMaterial(QQuickWindow *window, Material m)
QQuickShapeLinearGradientMaterial(QQuickShapeGenericStrokeFillNode *node)
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode renderMode) const override
This function returns a new instance of a the QSGMaterialShader implementation used to render geometr...
int compare(const QSGMaterial *other) const override
Compares this material to other and returns 0 if they are equal; -1 if this material should sort befo...
QSGMaterialType * type() const override
This function is called by the scene graph to query an identifier that is unique to the QSGMaterialSh...
QQuickShapeGenericStrokeFillNode * node() const
void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to prepare use of sampled images in the shader,...
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
QQuickShapeGenericStrokeFillNode * node() const
QQuickShapeRadialGradientMaterial(QQuickShapeGenericStrokeFillNode *node)
QSGMaterialType * type() const override
This function is called by the scene graph to query an identifier that is unique to the QSGMaterialSh...
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode renderMode) const override
This function returns a new instance of a the QSGMaterialShader implementation used to render geometr...
int compare(const QSGMaterial *other) const override
Compares this material to other and returns 0 if they are equal; -1 if this material should sort befo...
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to prepare use of sampled images in the shader,...
QQuickShapeGenericRenderer::Color4ub strokeColor
QQuickShapeGenericRenderer::VertexContainerType strokeVertices
QQuickShapeTextureFillMaterial(QQuickShapeGenericStrokeFillNode *node)
QSGMaterialShader * createShader(QSGRendererInterface::RenderMode renderMode) const override
This function returns a new instance of a the QSGMaterialShader implementation used to render geometr...
int compare(const QSGMaterial *other) const override
Compares this material to other and returns 0 if they are equal; -1 if this material should sort befo...
QQuickShapeGenericStrokeFillNode * node() const
QSGMaterialType * type() const override
This function is called by the scene graph to query an identifier that is unique to the QSGMaterialSh...
bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to get the contents of the shader program's uniform buffer...
void updateSampledImage(RenderState &state, int binding, QSGTexture **texture, QSGMaterial *newMaterial, QSGMaterial *oldMaterial) override
This function is called by the scene graph to prepare use of sampled images in the shader,...
Combined button and popup list for selecting options.
static QQuickShapeGenericRenderer::Color4ub colorToColor4ub(const QColor &c)
static QQuickAbstractPathRenderer::FillGradientType copyGenericGradient(const QQuickShapeGradient *gradient, QSGGradientCache::GradientDesc *dst)
#define QSG_RUNTIME_DESCRIPTION
Definition qsgnode.h:17
void set(float nx, float ny, QQuickShapeGenericRenderer::Color4ub ncolor)