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(this);
304}
305
307{
308 QQuickShapeGenericRenderer::triangulateStroke(path, pen, strokeColor, &strokeVertices, clipSize, triangulationScale);
309 emit done(this);
310}
311
312void QQuickShapeGenericRenderer::setAsyncCallback(void (*callback)(void *), void *data)
313{
314 m_asyncCallback = callback;
315 m_asyncCallbackData = data;
316}
317
318#if QT_CONFIG(thread)
319static QThreadPool *pathWorkThreadPool = nullptr;
320
321static void deletePathWorkThreadPool()
322{
323 delete pathWorkThreadPool;
324 pathWorkThreadPool = nullptr;
325}
326#endif
327
329{
330#if !QT_CONFIG(thread)
331 // Force synchronous mode for the no-thread configuration due
332 // to lack of QThreadPool.
333 async = false;
334#endif
335
336 bool didKickOffAsync = false;
337
338 for (int i = 0; i < m_sp.size(); ++i) {
339 ShapePathData &d(m_sp[i]);
340 if (!d.syncDirty)
341 continue;
342
343 m_accDirty |= d.syncDirty;
344
345 // Use a shadow dirty flag in order to avoid losing state in case there are
346 // multiple syncs with different dirty flags before we get to updateNode()
347 // on the render thread (with the gui thread blocked). For our purposes
348 // here syncDirty is still required since geometry regeneration must only
349 // happen when there was an actual change in this particular sync round.
350 d.effectiveDirty |= d.syncDirty;
351
352 if (d.path.isEmpty()) {
353 d.fillVertices.clear();
354 d.fillIndices.clear();
355 d.strokeVertices.clear();
356 continue;
357 }
358
359#if QT_CONFIG(thread)
360 if (async && !pathWorkThreadPool) {
361 qAddPostRoutine(deletePathWorkThreadPool);
362 pathWorkThreadPool = new QThreadPool;
363 const int idealCount = QThread::idealThreadCount();
364 pathWorkThreadPool->setMaxThreadCount(idealCount > 0 ? idealCount * 2 : 4);
365 }
366#endif
367
368 // RHI backend might not have had a chance of being initialized yet
369 // (we are called in the mainthread while renderthread might be in the
370 // process of starting up and setting the RHI backend.
371 // m_rhiBackendInitialized is set in updateNode() on the render thread
372 // when both threads are locked.
373 // Until then, we cannot assume we have support for uint32_t indices
374 // but only uint16_t ones.
375 const bool supportsElementIndexUint = m_rhiBackendInitialized ? m_supportsElementIndexUint : true;
376 if ((d.syncDirty & DirtyFillGeom) && d.fillColor.a) {
377 d.path.setFillRule(d.fillRule);
378 if (m_api == QSGRendererInterface::Unknown)
379 m_api = m_item->window()->rendererInterface()->graphicsApi();
380 if (async) {
382 r->setAutoDelete(false);
383 if (d.pendingFill)
384 d.pendingFill->orphaned = true;
385 d.pendingFill = r;
386 r->path = d.path;
387 r->fillColor = d.fillColor;
388 r->supportsElementIndexUint = supportsElementIndexUint;
389 r->triangulationScale = d.triangulationScale;
390 // Unlikely in practice but in theory m_sp could be
391 // resized. Therefore, capture 'i' instead of 'd'.
392 QObject::connect(r, &QQuickShapeFillRunnable::done, qApp, [this, i](QQuickShapeFillRunnable *r) {
393 // Bail out when orphaned (meaning either another run was
394 // started after this one, or the renderer got destroyed).
395 if (!r->orphaned && i < m_sp.size()) {
396 ShapePathData &d(m_sp[i]);
397 d.fillVertices = r->fillVertices;
398 d.fillIndices = r->fillIndices;
399 d.indexType = r->indexType;
400 d.pendingFill = nullptr;
401 d.effectiveDirty |= DirtyFillGeom;
402 maybeUpdateAsyncItem();
403 }
404 r->deleteLater();
405 });
406 didKickOffAsync = true;
407#if QT_CONFIG(thread)
408 // qtVectorPathForPath() initializes a unique_ptr without locking.
409 // Do that before starting the threads as otherwise we get a race condition.
410 qtVectorPathForPath(r->path);
411 pathWorkThreadPool->start(r);
412#endif
413 } else {
414 triangulateFill(d.path, d.fillColor, &d.fillVertices, &d.fillIndices, &d.indexType,
415 supportsElementIndexUint,
416 d.triangulationScale);
417 }
418 }
419
420 if ((d.syncDirty & DirtyStrokeGeom) && d.strokeWidth > 0.0f && (d.strokeColor.a || d.strokeGradientActive)) {
421 if (async) {
423 r->setAutoDelete(false);
424 if (d.pendingStroke)
425 d.pendingStroke->orphaned = true;
426 d.pendingStroke = r;
427 r->path = d.path;
428 r->pen = d.pen;
429 r->strokeColor = d.strokeColor;
430 r->clipSize = QSize(m_item->width(), m_item->height());
431 r->triangulationScale = d.triangulationScale;
432 QObject::connect(r, &QQuickShapeStrokeRunnable::done, qApp, [this, i](QQuickShapeStrokeRunnable *r) {
433 if (!r->orphaned && i < m_sp.size()) {
434 ShapePathData &d(m_sp[i]);
435 d.strokeVertices = r->strokeVertices;
436 d.pendingStroke = nullptr;
437 d.effectiveDirty |= DirtyStrokeGeom;
438 maybeUpdateAsyncItem();
439 }
440 r->deleteLater();
441 });
442 didKickOffAsync = true;
443#if QT_CONFIG(thread)
444 // qtVectorPathForPath() initializes a unique_ptr without locking.
445 // Do that before starting the threads as otherwise we get a race condition.
446 qtVectorPathForPath(r->path);
447 pathWorkThreadPool->start(r);
448#endif
449 } else {
450 triangulateStroke(d.path, d.pen, d.strokeColor, &d.strokeVertices,
451 QSize(m_item->width(), m_item->height()), d.triangulationScale);
452 }
453 }
454 }
455
456 if (!didKickOffAsync && async && m_asyncCallback)
457 m_asyncCallback(m_asyncCallbackData);
458}
459
460void QQuickShapeGenericRenderer::maybeUpdateAsyncItem()
461{
462 for (const ShapePathData &d : std::as_const(m_sp)) {
463 if (d.pendingFill || d.pendingStroke)
464 return;
465 }
466 m_accDirty |= DirtyFillGeom | DirtyStrokeGeom;
467 m_item->update();
468 if (m_asyncCallback)
469 m_asyncCallback(m_asyncCallbackData);
470}
471
472// the stroke/fill triangulation functions may be invoked either on the gui
473// thread or some worker thread and must thus be self-contained.
474void QQuickShapeGenericRenderer::triangulateFill(const QPainterPath &path,
475 const Color4ub &fillColor,
476 VertexContainerType *fillVertices,
477 IndexContainerType *fillIndices,
478 QSGGeometry::Type *indexType,
479 bool supportsElementIndexUint,
480 qreal triangulationScale)
481{
482 const QVectorPath &vp = qtVectorPathForPath(path);
483
484 QTriangleSet ts = qTriangulate(vp, QTransform::fromScale(triangulationScale, triangulationScale), 1, supportsElementIndexUint);
485 const int vertexCount = ts.vertices.size() / 2; // just a qreal vector with x,y hence the / 2
486 fillVertices->resize(vertexCount);
487 ColoredVertex *vdst = reinterpret_cast<ColoredVertex *>(fillVertices->data());
488 const qreal *vsrc = ts.vertices.constData();
489 for (int i = 0; i < vertexCount; ++i)
490 vdst[i].set(vsrc[i * 2] / triangulationScale, vsrc[i * 2 + 1] / triangulationScale, fillColor);
491
492 size_t indexByteSize;
493 if (ts.indices.type() == QVertexIndexVector::UnsignedShort) {
494 *indexType = QSGGeometry::UnsignedShortType;
495 // fillIndices is still QList<quint32>. Just resize to N/2 and pack
496 // the N quint16s into it.
497 fillIndices->resize(ts.indices.size() / 2);
498 indexByteSize = ts.indices.size() * sizeof(quint16);
499 } else {
500 *indexType = QSGGeometry::UnsignedIntType;
501 fillIndices->resize(ts.indices.size());
502 indexByteSize = ts.indices.size() * sizeof(quint32);
503 }
504 memcpy(fillIndices->data(), ts.indices.data(), indexByteSize);
505}
506
507void QQuickShapeGenericRenderer::triangulateStroke(const QPainterPath &path,
508 const QPen &pen,
509 const Color4ub &strokeColor,
510 VertexContainerType *strokeVertices,
511 const QSize &clipSize,
512 qreal triangulationScale)
513{
514 const QVectorPath &vp = qtVectorPathForPath(path);
515 const QRectF clip(QPointF(0, 0), clipSize);
516 const qreal inverseScale = 1.0 / triangulationScale;
517
518 QTriangulatingStroker stroker;
519 stroker.setInvScale(inverseScale);
520
521 if (pen.style() == Qt::SolidLine) {
522 stroker.process(vp, pen, clip, {});
523 } else {
524 QDashedStrokeProcessor dashStroker;
525 dashStroker.setInvScale(inverseScale);
526 dashStroker.process(vp, pen, clip, {});
527 QVectorPath dashStroke(dashStroker.points(), dashStroker.elementCount(),
528 dashStroker.elementTypes(), 0);
529 stroker.process(dashStroke, pen, clip, {});
530 }
531
532 if (!stroker.vertexCount()) {
533 strokeVertices->clear();
534 return;
535 }
536
537 const int vertexCount = stroker.vertexCount() / 2; // just a float vector with x,y hence the / 2
538 strokeVertices->resize(vertexCount);
539 ColoredVertex *vdst = reinterpret_cast<ColoredVertex *>(strokeVertices->data());
540 const float *vsrc = stroker.vertices();
541 for (int i = 0; i < vertexCount; ++i)
542 vdst[i].set(vsrc[i * 2], vsrc[i * 2 + 1], strokeColor);
543}
544
546{
547 if (m_rootNode != node) {
548 m_rootNode = node;
549 m_accDirty |= DirtyList;
550 }
551}
552
553// on the render thread with gui blocked
555{
556 if (!m_rootNode || !m_accDirty)
557 return;
558
559 // Is it now safe to query the rhi backend
560 if (!m_rhiBackendInitialized) {
561 auto findRHIBackend = [](QQuickItem *item) -> QRhi * {
562 if (auto *w = item->window()) {
563 if (auto *rhi = QQuickWindowPrivate::get(w)->rhi)
564 return rhi;
565 }
566 return nullptr;
567 };
568 auto *rhi = findRHIBackend(m_item);
569 if (rhi != nullptr) {
570 m_rhiBackendInitialized = true;
571 m_supportsElementIndexUint = rhi->isFeatureSupported(QRhi::ElementIndexUint);
572 }
573 }
574
575// [ m_rootNode ]
576// / / /
577// #0 [ fill ] [ stroke ] [ next ]
578// / / |
579// #1 [ fill ] [ stroke ] [ next ]
580// / / |
581// #2 [ fill ] [ stroke ] [ next ]
582// ...
583// ...
584
585 QQuickShapeGenericNode **nodePtr = &m_rootNode;
586 QQuickShapeGenericNode *prevNode = nullptr;
587
588 for (ShapePathData &d : m_sp) {
589 if (!*nodePtr) {
590 Q_ASSERT(prevNode);
591 *nodePtr = new QQuickShapeGenericNode;
592 prevNode->m_next = *nodePtr;
593 prevNode->appendChildNode(*nodePtr);
594 }
595
596 QQuickShapeGenericNode *node = *nodePtr;
597
598 if (m_accDirty & DirtyList) {
599 d.effectiveDirty |= DirtyFillGeom | DirtyStrokeGeom | DirtyColor | DirtyFillGradient
600 | DirtyFillTransform | DirtyFillTexture | DirtyStrokeGradient;
601 }
602
603 if (!d.effectiveDirty) {
604 prevNode = node;
605 nodePtr = &node->m_next;
606 continue;
607 }
608
609 if (d.fillColor.a == 0) {
610 delete node->m_fillNode;
611 node->m_fillNode = nullptr;
612 } else if (!node->m_fillNode) {
613 node->m_fillNode = new QQuickShapeGenericStrokeFillNode(m_item->window());
614 if (node->m_strokeNode)
615 node->removeChildNode(node->m_strokeNode);
616 node->appendChildNode(node->m_fillNode);
617 if (node->m_strokeNode)
618 node->appendChildNode(node->m_strokeNode);
619 d.effectiveDirty |= DirtyFillGeom;
620 }
621
622 if (d.strokeWidth <= 0.0f || d.strokeColor.a == 0) {
623 delete node->m_strokeNode;
624 node->m_strokeNode = nullptr;
625 } else if (!node->m_strokeNode) {
626 node->m_strokeNode = new QQuickShapeGenericStrokeFillNode(m_item->window());
627 node->appendChildNode(node->m_strokeNode);
628 d.effectiveDirty |= DirtyStrokeGeom;
629 }
630
631 updateFillNode(&d, node);
632 updateStrokeNode(&d, node);
633
634 d.effectiveDirty = 0;
635
636 prevNode = node;
637 nodePtr = &node->m_next;
638 }
639
640 if (*nodePtr && prevNode) {
641 prevNode->removeChildNode(*nodePtr);
642 delete *nodePtr;
643 *nodePtr = nullptr;
644 }
645
646 if (m_sp.isEmpty()) {
647 delete m_rootNode->m_fillNode;
648 m_rootNode->m_fillNode = nullptr;
649 delete m_rootNode->m_strokeNode;
650 m_rootNode->m_strokeNode = nullptr;
651 delete m_rootNode->m_next;
652 m_rootNode->m_next = nullptr;
653 }
654
655 m_accDirty = 0;
656}
657
658void QQuickShapeGenericRenderer::updateShadowDataInNode(ShapePathData *d, QQuickShapeGenericStrokeFillNode *n)
659{
660 if (d->fillGradientActive) {
661 if (d->effectiveDirty & DirtyFillGradient)
662 n->m_gradient = d->fillGradient;
663 }
664
665 if (d->effectiveDirty & DirtyFillTexture) {
666 bool needsUpdate = d->fillTextureProviderItem == nullptr && n->m_fillTextureProvider != nullptr;
667 if (!needsUpdate
668 && d->fillTextureProviderItem != nullptr
669 && n->m_fillTextureProvider != d->fillTextureProviderItem->textureProvider()) {
670 needsUpdate = true;
671 }
672
673 if (needsUpdate) {
674 if (n->m_fillTextureProvider != nullptr) {
675 QObject::disconnect(n->m_fillTextureProvider, &QSGTextureProvider::textureChanged,
676 n, &QQuickShapeGenericStrokeFillNode::handleTextureChanged);
677 QObject::disconnect(n->m_fillTextureProvider, &QSGTextureProvider::destroyed,
678 n, &QQuickShapeGenericStrokeFillNode::handleTextureProviderDestroyed);
679 }
680
681 n->m_fillTextureProvider = d->fillTextureProviderItem == nullptr
682 ? nullptr
683 : d->fillTextureProviderItem->textureProvider();
684
685 if (n->m_fillTextureProvider != nullptr) {
686 QObject::connect(n->m_fillTextureProvider, &QSGTextureProvider::textureChanged,
687 n, &QQuickShapeGenericStrokeFillNode::handleTextureChanged);
688 QObject::connect(n->m_fillTextureProvider, &QSGTextureProvider::destroyed,
689 n, &QQuickShapeGenericStrokeFillNode::handleTextureProviderDestroyed);
690 }
691 }
692 }
693 if (d->effectiveDirty & DirtyFillTransform)
694 n->m_fillTransform = d->fillTransform;
695}
696
697void QQuickShapeGenericRenderer::updateFillNode(ShapePathData *d, QQuickShapeGenericNode *node)
698{
699 if (!node->m_fillNode)
700 return;
702 return;
703
704 // Make a copy of the data that will be accessed by the material on
705 // the render thread. This must be done even when we bail out below.
707 updateShadowDataInNode(d, n);
708
709 QSGGeometry *g = n->geometry();
710 if (d->fillVertices.isEmpty()) {
711 if (g->vertexCount() || g->indexCount()) {
712 g->allocate(0, 0);
713 n->markDirty(QSGNode::DirtyGeometry);
714 }
715 return;
716 }
717
718 if (d->fillGradientActive) {
720 switch (d->fillGradientActive) {
721 case LinearGradient:
723 break;
724 case RadialGradient:
726 break;
727 case ConicalGradient:
729 break;
730 default:
731 Q_UNREACHABLE_RETURN();
732 }
733 n->activateMaterial(m_item->window(), gradMat);
734 if (d->effectiveDirty & (DirtyFillGradient | DirtyFillTransform)) {
735 // Gradients are implemented via a texture-based material.
736 n->markDirty(QSGNode::DirtyMaterial);
737 // stop here if only the gradient or filltransform changed; no need to touch the geometry
738 if (!(d->effectiveDirty & DirtyFillGeom))
739 return;
740 }
741 } else if (d->fillTextureProviderItem != nullptr) {
742 n->activateMaterial(m_item->window(), QQuickShapeGenericStrokeFillNode::MatTextureFill);
743 if (d->effectiveDirty & DirtyFillTexture)
744 n->markDirty(QSGNode::DirtyMaterial);
745 } else {
746 n->activateMaterial(m_item->window(), QQuickShapeGenericStrokeFillNode::MatSolidColor);
747 // fast path for updating only color values when no change in vertex positions
748 if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyFillGeom) && d->fillTextureProviderItem == nullptr) {
749 ColoredVertex *vdst = reinterpret_cast<ColoredVertex *>(g->vertexData());
750 for (int i = 0; i < g->vertexCount(); ++i)
751 vdst[i].set(vdst[i].x, vdst[i].y, d->fillColor);
752 n->markDirty(QSGNode::DirtyGeometry);
753 return;
754 }
755 }
756
757 const int indexCount = d->indexType == QSGGeometry::UnsignedShortType
758 ? d->fillIndices.size() * 2 : d->fillIndices.size();
759 if (g->indexType() != d->indexType) {
760 g = new QSGGeometry(QSGGeometry::defaultAttributes_ColoredPoint2D(),
761 d->fillVertices.size(), indexCount, d->indexType);
762 n->setGeometry(g);
763 } else {
764 g->allocate(d->fillVertices.size(), indexCount);
765 }
766 g->setDrawingMode(QSGGeometry::DrawTriangles);
767
768 memcpy(g->vertexData(), d->fillVertices.constData(), g->vertexCount() * g->sizeOfVertex());
769 memcpy(g->indexData(), d->fillIndices.constData(), g->indexCount() * g->sizeOfIndex());
770
771 n->markDirty(QSGNode::DirtyGeometry);
772}
773
774void QQuickShapeGenericRenderer::updateStrokeNode(ShapePathData *d, QQuickShapeGenericNode *node)
775{
776 if (!node->m_strokeNode)
777 return;
778 if (!(d->effectiveDirty & (DirtyStrokeGeom | DirtyColor | DirtyStrokeGradient)))
779 return;
780
782 if (d->strokeGradientActive) {
783 if (d->effectiveDirty & DirtyStrokeGradient)
784 n->m_gradient = d->strokeGradient;
785 }
786
787 QSGGeometry *g = n->geometry();
788 if (d->strokeVertices.isEmpty()) {
789 if (g->vertexCount() || g->indexCount()) {
790 g->allocate(0, 0);
791 n->markDirty(QSGNode::DirtyGeometry);
792 }
793 return;
794 }
795
796 n->markDirty(QSGNode::DirtyGeometry);
797
798 // Async loading runs update once, bails out above, then updates again once
799 // ready. Set the material dirty then. This is in-line with fill where the
800 // first activateMaterial() achieves the same.
801 if (!g->vertexCount())
802 n->markDirty(QSGNode::DirtyMaterial);
803
804 if (d->strokeGradientActive) {
806 switch (d->strokeGradientActive) {
807 case LinearGradient:
809 break;
810 case RadialGradient:
812 break;
813 case ConicalGradient:
815 break;
816 default:
817 Q_UNREACHABLE_RETURN();
818 }
819 n->activateMaterial(m_item->window(), gradMat);
820 if (d->effectiveDirty & DirtyStrokeGradient) {
821 // Gradients are implemented via a texture-based material.
822 n->markDirty(QSGNode::DirtyMaterial);
823 // stop here if only the gradient changed; no need to touch the geometry
824 if (!(d->effectiveDirty & DirtyStrokeGeom))
825 return;
826 }
827 } else {
828 n->activateMaterial(m_item->window(), QQuickShapeGenericStrokeFillNode::MatSolidColor);
829 if ((d->effectiveDirty & DirtyColor) && !(d->effectiveDirty & DirtyStrokeGeom)) {
830 ColoredVertex *vdst = reinterpret_cast<ColoredVertex *>(g->vertexData());
831 for (int i = 0; i < g->vertexCount(); ++i)
832 vdst[i].set(vdst[i].x, vdst[i].y, d->strokeColor);
833 return;
834 }
835 }
836
837 g->allocate(d->strokeVertices.size(), 0);
838 g->setDrawingMode(QSGGeometry::DrawTriangleStrip);
839 memcpy(g->vertexData(), d->strokeVertices.constData(), g->vertexCount() * g->sizeOfVertex());
840}
841
842QSGMaterial *QQuickShapeGenericMaterialFactory::createVertexColor(QQuickWindow *window)
843{
844 QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi();
845
846 if (api == QSGRendererInterface::OpenGL || QSGRendererInterface::isApiRhiBased(api))
847 return new QSGVertexColorMaterial;
848
849 qWarning("Vertex-color material: Unsupported graphics API %d", api);
850 return nullptr;
851}
852
853QSGMaterial *QQuickShapeGenericMaterialFactory::createLinearGradient(QQuickWindow *window,
855{
856 QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi();
857
858 if (api == QSGRendererInterface::OpenGL || QSGRendererInterface::isApiRhiBased(api))
860
861 qWarning("Linear gradient material: Unsupported graphics API %d", api);
862 return nullptr;
863}
864
865QSGMaterial *QQuickShapeGenericMaterialFactory::createRadialGradient(QQuickWindow *window,
867{
868 QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi();
869
870 if (api == QSGRendererInterface::OpenGL || QSGRendererInterface::isApiRhiBased(api))
872
873 qWarning("Radial gradient material: Unsupported graphics API %d", api);
874 return nullptr;
875}
876
877QSGMaterial *QQuickShapeGenericMaterialFactory::createConicalGradient(QQuickWindow *window,
879{
880 QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi();
881
882 if (api == QSGRendererInterface::OpenGL || QSGRendererInterface::isApiRhiBased(api))
884
885 qWarning("Conical gradient material: Unsupported graphics API %d", api);
886 return nullptr;
887}
888
889QSGMaterial *QQuickShapeGenericMaterialFactory::createTextureFill(QQuickWindow *window,
891{
892 QSGRendererInterface::GraphicsApi api = window->rendererInterface()->graphicsApi();
893
894 if (api == QSGRendererInterface::OpenGL || QSGRendererInterface::isApiRhiBased(api))
896
897 qWarning("Texture fill material: Unsupported graphics API %d", api);
898 return nullptr;
899}
900
902{
903 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/lineargradient.vert.qsb"), viewCount);
904 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/lineargradient.frag.qsb"), viewCount);
905}
906
908 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
909{
910 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
911 QQuickShapeLinearGradientMaterial *m = static_cast<QQuickShapeLinearGradientMaterial *>(newMaterial);
912 bool changed = false;
913 QByteArray *buf = state.uniformData();
914 Q_ASSERT(buf->size() >= 84 + 64);
915 const int shaderMatrixCount = newMaterial->viewCount();
916 const int matrixCount = qMin(state.projectionMatrixCount(), shaderMatrixCount);
917
918 if (state.isMatrixDirty()) {
919 for (int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
920 const QMatrix4x4 m = state.combinedMatrix();
921 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
922 changed = true;
923 }
924 }
925
927
928 if (!oldMaterial || m_fillTransform != node->m_fillTransform) {
929 memcpy(buf->data() + 64 * shaderMatrixCount, node->m_fillTransform.invertedData(), 64);
930 m_fillTransform = node->m_fillTransform;
931 changed = true;
932 }
933
934 if (!oldMaterial || m_gradA.x() != node->m_gradient.a.x() || m_gradA.y() != node->m_gradient.a.y()) {
935 m_gradA = QVector2D(node->m_gradient.a.x(), node->m_gradient.a.y());
936 Q_ASSERT(sizeof(m_gradA) == 8);
937 memcpy(buf->data() + 64 * shaderMatrixCount + 64, &m_gradA, 8);
938 changed = true;
939 }
940
941 if (!oldMaterial || m_gradB.x() != node->m_gradient.b.x() || m_gradB.y() != node->m_gradient.b.y()) {
942 m_gradB = QVector2D(node->m_gradient.b.x(), node->m_gradient.b.y());
943 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8, &m_gradB, 8);
944 changed = true;
945 }
946
947 if (state.isOpacityDirty()) {
948 const float opacity = state.opacity();
949 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8 + 8, &opacity, 4);
950 changed = true;
951 }
952
953 return changed;
954}
955
956void QQuickShapeLinearGradientRhiShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
957 QSGMaterial *newMaterial, QSGMaterial *)
958{
959 if (binding != 1)
960 return;
961
962 QQuickShapeLinearGradientMaterial *m = static_cast<QQuickShapeLinearGradientMaterial *>(newMaterial);
964 const QSGGradientCacheKey cacheKey(node->m_gradient.stops, QGradient::Spread(node->m_gradient.spread));
965 QSGTexture *t = QSGGradientCache::cacheForRhi(state.rhi())->get(cacheKey);
966 t->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
967 *texture = t;
968}
969
971{
972 static QSGMaterialType type;
973 return &type;
974}
975
976int QQuickShapeLinearGradientMaterial::compare(const QSGMaterial *other) const
977{
978 Q_ASSERT(other && type() == other->type());
979 const QQuickShapeLinearGradientMaterial *m = static_cast<const QQuickShapeLinearGradientMaterial *>(other);
980
983 Q_ASSERT(a && b);
984 if (a == b)
985 return 0;
986
987 const QSGGradientCache::GradientDesc *ga = &a->m_gradient;
988 const QSGGradientCache::GradientDesc *gb = &b->m_gradient;
989
990 if (int d = ga->spread - gb->spread)
991 return d;
992
993 if (int d = ga->a.x() - gb->a.x())
994 return d;
995 if (int d = ga->a.y() - gb->a.y())
996 return d;
997 if (int d = ga->b.x() - gb->b.x())
998 return d;
999 if (int d = ga->b.y() - gb->b.y())
1000 return d;
1001
1002 if (int d = ga->stops.size() - gb->stops.size())
1003 return d;
1004
1005 for (int i = 0; i < ga->stops.size(); ++i) {
1006 if (int d = ga->stops[i].first - gb->stops[i].first)
1007 return d;
1008 if (int d = ga->stops[i].second.rgba() - gb->stops[i].second.rgba())
1009 return d;
1010 }
1011
1012 if (int d = a->m_fillTransform.compareTo(b->m_fillTransform))
1013 return d;
1014
1015 return 0;
1016}
1017
1018QSGMaterialShader *QQuickShapeLinearGradientMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
1019{
1020 Q_UNUSED(renderMode);
1021 return new QQuickShapeLinearGradientRhiShader(viewCount());
1022}
1023
1025{
1026 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/radialgradient.vert.qsb"), viewCount);
1027 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/radialgradient.frag.qsb"), viewCount);
1028}
1029
1031 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
1032{
1033 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
1034 QQuickShapeRadialGradientMaterial *m = static_cast<QQuickShapeRadialGradientMaterial *>(newMaterial);
1035 bool changed = false;
1036 QByteArray *buf = state.uniformData();
1037 Q_ASSERT(buf->size() >= 92 + 64);
1038 const int shaderMatrixCount = newMaterial->viewCount();
1039 const int matrixCount = qMin(state.projectionMatrixCount(), shaderMatrixCount);
1040
1041 if (state.isMatrixDirty()) {
1042 for (int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
1043 const QMatrix4x4 m = state.combinedMatrix();
1044 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
1045 changed = true;
1046 }
1047 }
1048
1050
1051 if (!oldMaterial || m_fillTransform != node->m_fillTransform) {
1052 memcpy(buf->data() + 64 * shaderMatrixCount, node->m_fillTransform.invertedData(), 64);
1053 m_fillTransform = node->m_fillTransform;
1054 changed = true;
1055 }
1056
1057 const QPointF centerPoint = node->m_gradient.a;
1058 const QPointF focalPoint = node->m_gradient.b;
1059 const QPointF focalToCenter = centerPoint - focalPoint;
1060 const float centerRadius = node->m_gradient.v0;
1061 const float focalRadius = node->m_gradient.v1;
1062
1063 if (!oldMaterial || m_focalPoint.x() != focalPoint.x() || m_focalPoint.y() != focalPoint.y()) {
1064 m_focalPoint = QVector2D(focalPoint.x(), focalPoint.y());
1065 Q_ASSERT(sizeof(m_focalPoint) == 8);
1066 memcpy(buf->data() + 64 * shaderMatrixCount + 64, &m_focalPoint, 8);
1067 changed = true;
1068 }
1069
1070 if (!oldMaterial || m_focalToCenter.x() != focalToCenter.x() || m_focalToCenter.y() != focalToCenter.y()) {
1071 m_focalToCenter = QVector2D(focalToCenter.x(), focalToCenter.y());
1072 Q_ASSERT(sizeof(m_focalToCenter) == 8);
1073 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8, &m_focalToCenter, 8);
1074 changed = true;
1075 }
1076
1077 if (!oldMaterial || m_centerRadius != centerRadius) {
1078 m_centerRadius = centerRadius;
1079 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8 + 8, &m_centerRadius, 4);
1080 changed = true;
1081 }
1082
1083 if (!oldMaterial || m_focalRadius != focalRadius) {
1084 m_focalRadius = focalRadius;
1085 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8 + 8 + 4, &m_focalRadius, 4);
1086 changed = true;
1087 }
1088
1089 if (state.isOpacityDirty()) {
1090 const float opacity = state.opacity();
1091 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8 + 8 + 4 + 4, &opacity, 4);
1092 changed = true;
1093 }
1094
1095 return changed;
1096}
1097
1098void QQuickShapeRadialGradientRhiShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
1099 QSGMaterial *newMaterial, QSGMaterial *)
1100{
1101 if (binding != 1)
1102 return;
1103
1104 QQuickShapeRadialGradientMaterial *m = static_cast<QQuickShapeRadialGradientMaterial *>(newMaterial);
1106 const QSGGradientCacheKey cacheKey(node->m_gradient.stops, QGradient::Spread(node->m_gradient.spread));
1107 QSGTexture *t = QSGGradientCache::cacheForRhi(state.rhi())->get(cacheKey);
1108 t->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
1109 *texture = t;
1110}
1111
1113{
1114 static QSGMaterialType type;
1115 return &type;
1116}
1117
1118int QQuickShapeRadialGradientMaterial::compare(const QSGMaterial *other) const
1119{
1120 Q_ASSERT(other && type() == other->type());
1121 const QQuickShapeRadialGradientMaterial *m = static_cast<const QQuickShapeRadialGradientMaterial *>(other);
1122
1125 Q_ASSERT(a && b);
1126 if (a == b)
1127 return 0;
1128
1129 const QSGGradientCache::GradientDesc *ga = &a->m_gradient;
1130 const QSGGradientCache::GradientDesc *gb = &b->m_gradient;
1131
1132 if (int d = ga->spread - gb->spread)
1133 return d;
1134
1135 if (int d = ga->a.x() - gb->a.x())
1136 return d;
1137 if (int d = ga->a.y() - gb->a.y())
1138 return d;
1139 if (int d = ga->b.x() - gb->b.x())
1140 return d;
1141 if (int d = ga->b.y() - gb->b.y())
1142 return d;
1143
1144 if (int d = ga->v0 - gb->v0)
1145 return d;
1146 if (int d = ga->v1 - gb->v1)
1147 return d;
1148
1149 if (int d = ga->stops.size() - gb->stops.size())
1150 return d;
1151
1152 for (int i = 0; i < ga->stops.size(); ++i) {
1153 if (int d = ga->stops[i].first - gb->stops[i].first)
1154 return d;
1155 if (int d = ga->stops[i].second.rgba() - gb->stops[i].second.rgba())
1156 return d;
1157 }
1158
1159 if (int d = a->m_fillTransform.compareTo(b->m_fillTransform))
1160 return d;
1161
1162 return 0;
1163}
1164
1165QSGMaterialShader *QQuickShapeRadialGradientMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
1166{
1167 Q_UNUSED(renderMode);
1168 return new QQuickShapeRadialGradientRhiShader(viewCount());
1169}
1170
1172{
1173 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/conicalgradient.vert.qsb"), viewCount);
1174 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/conicalgradient.frag.qsb"), viewCount);
1175}
1176
1178 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
1179{
1180 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
1181 QQuickShapeConicalGradientMaterial *m = static_cast<QQuickShapeConicalGradientMaterial *>(newMaterial);
1182 bool changed = false;
1183 QByteArray *buf = state.uniformData();
1184 Q_ASSERT(buf->size() >= 80 + 64);
1185 const int shaderMatrixCount = newMaterial->viewCount();
1186 const int matrixCount = qMin(state.projectionMatrixCount(), shaderMatrixCount);
1187
1188 if (state.isMatrixDirty()) {
1189 for (int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
1190 const QMatrix4x4 m = state.combinedMatrix();
1191 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
1192 changed = true;
1193 }
1194 }
1195
1197
1198 if (!oldMaterial || m_fillTransform != node->m_fillTransform) {
1199 memcpy(buf->data() + 64 * shaderMatrixCount, node->m_fillTransform.invertedData(), 64);
1200 m_fillTransform = node->m_fillTransform;
1201 changed = true;
1202 }
1203
1204 const QPointF centerPoint = node->m_gradient.a;
1205 const float angle = -qDegreesToRadians(node->m_gradient.v0);
1206
1207 if (!oldMaterial || m_centerPoint.x() != centerPoint.x() || m_centerPoint.y() != centerPoint.y()) {
1208 m_centerPoint = QVector2D(centerPoint.x(), centerPoint.y());
1209 Q_ASSERT(sizeof(m_centerPoint) == 8);
1210 memcpy(buf->data() + 64 * shaderMatrixCount + 64, &m_centerPoint, 8);
1211 changed = true;
1212 }
1213
1214 if (!oldMaterial || m_angle != angle) {
1215 m_angle = angle;
1216 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8, &m_angle, 4);
1217 changed = true;
1218 }
1219
1220 if (state.isOpacityDirty()) {
1221 const float opacity = state.opacity();
1222 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8 + 4, &opacity, 4);
1223 changed = true;
1224 }
1225
1226 return changed;
1227}
1228
1229void QQuickShapeConicalGradientRhiShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
1230 QSGMaterial *newMaterial, QSGMaterial *)
1231{
1232 if (binding != 1)
1233 return;
1234
1235 QQuickShapeConicalGradientMaterial *m = static_cast<QQuickShapeConicalGradientMaterial *>(newMaterial);
1237 const QSGGradientCacheKey cacheKey(node->m_gradient.stops, QGradient::Spread(node->m_gradient.spread));
1238 QSGTexture *t = QSGGradientCache::cacheForRhi(state.rhi())->get(cacheKey);
1239 t->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
1240 *texture = t;
1241}
1242
1244{
1245 static QSGMaterialType type;
1246 return &type;
1247}
1248
1249int QQuickShapeConicalGradientMaterial::compare(const QSGMaterial *other) const
1250{
1251 Q_ASSERT(other && type() == other->type());
1252 const QQuickShapeConicalGradientMaterial *m = static_cast<const QQuickShapeConicalGradientMaterial *>(other);
1253
1256 Q_ASSERT(a && b);
1257 if (a == b)
1258 return 0;
1259
1260 const QSGGradientCache::GradientDesc *ga = &a->m_gradient;
1261 const QSGGradientCache::GradientDesc *gb = &b->m_gradient;
1262
1263 if (int d = ga->a.x() - gb->a.x())
1264 return d;
1265 if (int d = ga->a.y() - gb->a.y())
1266 return d;
1267
1268 if (int d = ga->v0 - gb->v0)
1269 return d;
1270
1271 if (int d = ga->stops.size() - gb->stops.size())
1272 return d;
1273
1274 for (int i = 0; i < ga->stops.size(); ++i) {
1275 if (int d = ga->stops[i].first - gb->stops[i].first)
1276 return d;
1277 if (int d = ga->stops[i].second.rgba() - gb->stops[i].second.rgba())
1278 return d;
1279 }
1280
1281 if (int d = a->m_fillTransform.compareTo(b->m_fillTransform))
1282 return d;
1283
1284 return 0;
1285}
1286
1287QSGMaterialShader *QQuickShapeConicalGradientMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
1288{
1289 Q_UNUSED(renderMode);
1290 return new QQuickShapeConicalGradientRhiShader(viewCount());
1291}
1292
1294{
1295 setShaderFileName(VertexStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/texturefill.vert.qsb"), viewCount);
1296 setShaderFileName(FragmentStage, QStringLiteral(":/qt-project.org/shapes/shaders_ng/texturefill.frag.qsb"), viewCount);
1297}
1298
1300 QSGMaterial *newMaterial, QSGMaterial *oldMaterial)
1301{
1302 Q_ASSERT(oldMaterial == nullptr || newMaterial->type() == oldMaterial->type());
1303 QQuickShapeTextureFillMaterial *m = static_cast<QQuickShapeTextureFillMaterial *>(newMaterial);
1304 bool changed = false;
1305 QByteArray *buf = state.uniformData();
1306 const int shaderMatrixCount = newMaterial->viewCount();
1307 const int matrixCount = qMin(state.projectionMatrixCount(), shaderMatrixCount);
1308 Q_ASSERT(buf->size() >= 64 * shaderMatrixCount + 64 + 8 + 4);
1309
1310 if (state.isMatrixDirty()) {
1311 for (int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
1312 const QMatrix4x4 m = state.combinedMatrix();
1313 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
1314 changed = true;
1315 }
1316 }
1317
1319
1320 if (!oldMaterial || m_fillTransform != node->m_fillTransform) {
1321 memcpy(buf->data() + 64 * shaderMatrixCount, node->m_fillTransform.invertedData(), 64);
1322 m_fillTransform = node->m_fillTransform;
1323 changed = true;
1324 }
1325
1326 const QSizeF boundsSize = node->m_fillTextureProvider != nullptr && node->m_fillTextureProvider->texture() != nullptr
1327 ? node->m_fillTextureProvider->texture()->textureSize()
1328 : QSizeF(0, 0);
1329
1330
1331 const QVector2D boundsVector(boundsSize.width() / state.devicePixelRatio(),
1332 boundsSize.height() / state.devicePixelRatio());
1333 if (!oldMaterial || m_boundsSize != boundsVector) {
1334 m_boundsSize = boundsVector;
1335 Q_ASSERT(sizeof(m_boundsSize) == 8);
1336 memcpy(buf->data() + 64 * shaderMatrixCount + 64, &m_boundsSize, 8);
1337 changed = true;
1338 }
1339
1340 if (state.isOpacityDirty()) {
1341 const float opacity = state.opacity();
1342 memcpy(buf->data() + 64 * shaderMatrixCount + 64 + 8, &opacity, 4);
1343 changed = true;
1344 }
1345
1346 return changed;
1347}
1348
1349void QQuickShapeTextureFillRhiShader::updateSampledImage(RenderState &state, int binding, QSGTexture **texture,
1350 QSGMaterial *newMaterial, QSGMaterial *)
1351{
1352 if (binding != 1)
1353 return;
1354
1355 QQuickShapeTextureFillMaterial *m = static_cast<QQuickShapeTextureFillMaterial *>(newMaterial);
1357 if (node->m_fillTextureProvider != nullptr) {
1358 QSGTexture *providedTexture = node->m_fillTextureProvider->texture();
1359 if (providedTexture != nullptr) {
1360 if (providedTexture->isAtlasTexture()) {
1361 // Create a non-atlas copy to make texture coordinate wrapping work. This
1362 // texture copy is owned by the QSGTexture so memory is managed with the original
1363 // texture provider.
1364 QSGTexture *newTexture = providedTexture->removedFromAtlas(state.resourceUpdateBatch());
1365 if (newTexture != nullptr)
1366 providedTexture = newTexture;
1367 }
1368
1369 providedTexture->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
1370 *texture = providedTexture;
1371 return;
1372 }
1373 }
1374
1375 if (m->dummyTexture() == nullptr) {
1376 QSGPlainTexture *dummyTexture = new QSGPlainTexture;
1377 dummyTexture->setFiltering(QSGTexture::Nearest);
1378 dummyTexture->setHorizontalWrapMode(QSGTexture::Repeat);
1379 dummyTexture->setVerticalWrapMode(QSGTexture::Repeat);
1380 QImage img(128, 128, QImage::Format_ARGB32_Premultiplied);
1381 img.fill(0);
1382 dummyTexture->setImage(img);
1383 dummyTexture->commitTextureOperations(state.rhi(), state.resourceUpdateBatch());
1384
1385 m->setDummyTexture(dummyTexture);
1386 }
1387
1388 *texture = m->dummyTexture();
1389}
1390
1392{
1393 delete m_dummyTexture;
1394}
1395
1397{
1398 static QSGMaterialType type;
1399 return &type;
1400}
1401
1402int QQuickShapeTextureFillMaterial::compare(const QSGMaterial *other) const
1403{
1404 Q_ASSERT(other && type() == other->type());
1405 const QQuickShapeTextureFillMaterial *m = static_cast<const QQuickShapeTextureFillMaterial *>(other);
1406
1409 Q_ASSERT(a && b);
1410 if (a == b)
1411 return 0;
1412
1413 if (int d = a->m_fillTransform.compareTo(b->m_fillTransform))
1414 return d;
1415
1416 const qintptr diff = qintptr(a->m_fillTextureProvider) - qintptr(b->m_fillTextureProvider);
1417 return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
1418}
1419
1420QSGMaterialShader *QQuickShapeTextureFillMaterial::createShader(QSGRendererInterface::RenderMode renderMode) const
1421{
1422 Q_UNUSED(renderMode);
1423 return new QQuickShapeTextureFillRhiShader(viewCount());
1424}
1425
1426QT_END_NAMESPACE
1427
1428#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)