8#include <QtCore/qthreadpool.h>
11#include <QtGui/qvector2d.h>
12#include <QtGui/qvector4d.h>
13#include <QtGui/private/qtriangulator_p.h>
14#include <QtGui/private/qtriangulatingstroker_p.h>
15#include <QtGui/private/qrhi_p.h>
17#include <QtQuick/private/qsgcurvefillnode_p.h>
18#include <QtQuick/private/qsgcurvestrokenode_p.h>
19#include <QtQuick/private/qquadpath_p.h>
20#include <QtQuick/private/qsgcurveprocessor_p.h>
21#include <QtQuick/qsgmaterial.h>
25Q_LOGGING_CATEGORY(lcShapeCurveRenderer,
"qt.shape.curverenderer");
30
31
32
33
34enum WireFrameType { SimpleWFT, StrokeWFT };
36class QQuickShapeWireFrameMaterialShader :
public QSGMaterialShader
39 QQuickShapeWireFrameMaterialShader(WireFrameType wft,
int viewCount) : m_wftype(wft)
41 setShaderFileName(VertexStage, wft == StrokeWFT ?
42 QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/shapestroke_wireframe.vert.qsb") :
43 QStringLiteral(
":/qt-project.org/shapes/shaders_ng/wireframe.vert.qsb"), viewCount);
44 setShaderFileName(FragmentStage,
46 QStringLiteral(
":/qt-project.org/scenegraph/shaders_ng/shapestroke_wireframe.frag.qsb") :
47 QStringLiteral(
":/qt-project.org/shapes/shaders_ng/wireframe.frag.qsb"), viewCount);
50 bool updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *)
override;
52 WireFrameType m_wftype;
55class QQuickShapeWireFrameMaterial :
public QSGMaterial
58 QQuickShapeWireFrameMaterial(WireFrameType wft) : m_wftype(wft)
60 setFlag(Blending,
true);
63 int compare(
const QSGMaterial *other)
const override
65 return (type() - other->type());
68 void setCosmeticStroke(
bool c)
73 void setStrokeWidth(
float width)
75 m_strokeWidth = width;
80 return (m_cosmeticStroke ? -1.0 : 1.0) * qAbs(m_strokeWidth);
84 QSGMaterialType *type()
const override
86 static QSGMaterialType t;
89 QSGMaterialShader *createShader(QSGRendererInterface::RenderMode)
const override
91 return new QQuickShapeWireFrameMaterialShader(m_wftype, viewCount());
94 WireFrameType m_wftype;
95 bool m_cosmeticStroke =
false;
96 float m_strokeWidth = 1.0f;
99bool QQuickShapeWireFrameMaterialShader::updateUniformData(RenderState &state, QSGMaterial *newMaterial, QSGMaterial *)
101 QByteArray *buf = state.uniformData();
102 Q_ASSERT(buf->size() >= 64);
103 const int matrixCount = qMin(state.projectionMatrixCount(), newMaterial->viewCount());
104 bool changed =
false;
105 float localScale = 1.0f;
107 for (
int viewIndex = 0; viewIndex < matrixCount; ++viewIndex) {
108 if (state.isMatrixDirty()) {
109 QMatrix4x4 m = state.combinedMatrix(viewIndex);
110 if (m_wftype == StrokeWFT)
112 memcpy(buf->data() + 64 * viewIndex, m.constData(), 64);
117 const float matrixScale = qSqrt(qAbs(state.determinant())) * state.devicePixelRatio() * localScale;
118 memcpy(buf->data() + matrixCount * 64, &matrixScale, 4);
119 const float dpr = state.devicePixelRatio();
120 memcpy(buf->data() + matrixCount * 64 + 8, &dpr, 4);
121 const float opacity = 1.0;
122 memcpy(buf->data() + matrixCount * 64 + 4, &opacity, 4);
123 const float strokeWidth =
static_cast<QQuickShapeWireFrameMaterial *>(newMaterial)->strokeWidth();
124 memcpy(buf->data() + matrixCount * 64 + 12, &strokeWidth, 4);
131template <WireFrameType wftype>
132class QQuickShapeWireFrameNode :
public QSGCurveAbstractNode
135 struct WireFrameVertex
137 float x, y, u, v, w, nx, ny, sw;
140 QQuickShapeWireFrameNode()
143 setFlag(OwnsGeometry,
true);
144 setGeometry(
new QSGGeometry(attributes(), 0, 0));
148 void setColor(QColor col)
override
153 void setCosmeticStroke(
bool c)
155 m_material->setCosmeticStroke(c);
158 void setStrokeWidth(
float width)
160 m_material->setStrokeWidth(width);
163 void activateMaterial()
165 m_material.reset(
new QQuickShapeWireFrameMaterial(wftype));
166 setMaterial(m_material.data());
169 static const QSGGeometry::AttributeSet &attributes()
171 static QSGGeometry::Attribute data[] = {
172 QSGGeometry::Attribute::createWithAttributeType(0, 2, QSGGeometry::FloatType, QSGGeometry::PositionAttribute),
173 QSGGeometry::Attribute::createWithAttributeType(1, 3, QSGGeometry::FloatType, QSGGeometry::TexCoordAttribute),
174 QSGGeometry::Attribute::createWithAttributeType(2, 3, QSGGeometry::FloatType, QSGGeometry::TexCoordAttribute),
176 static QSGGeometry::AttributeSet attrs = { 3,
sizeof(WireFrameVertex), data };
180 void cookGeometry() override
186 QScopedPointer<QQuickShapeWireFrameMaterial> m_material;
190QQuickShapeCurveRenderer::~QQuickShapeCurveRenderer()
192 for (
const PathData &pd : std::as_const(m_paths)) {
193 if (pd.currentRunner) {
194 pd.currentRunner->orphaned =
true;
195 if (!pd.currentRunner->isAsync || pd.currentRunner->isDone)
196 delete pd.currentRunner;
201void QQuickShapeCurveRenderer::beginSync(
int totalCount,
bool *countChanged)
203 if (countChanged !=
nullptr && totalCount != m_paths.size())
204 *countChanged =
true;
205 for (
int i = totalCount; i < m_paths.size(); i++) {
206 setFillTextureProvider(i,
nullptr);
207 m_removedPaths.append(m_paths.at(i));
209 m_paths.resize(totalCount);
212void QQuickShapeCurveRenderer::setPath(
int index,
const QPainterPath &path, QQuickShapePath::PathHints pathHints)
214 auto &pathData = m_paths[index];
215 pathData.originalPath = path;
216 pathData.pathHints = pathHints;
217 pathData.m_dirty |= PathDirty;
220void QQuickShapeCurveRenderer::setStrokeColor(
int index,
const QColor &color)
222 auto &pathData = m_paths[index];
223 const bool wasVisible = pathData.isStrokeVisible();
224 pathData.pen.setColor(color);
225 if (pathData.isStrokeVisible() != wasVisible)
226 pathData.m_dirty |= StrokeDirty;
228 pathData.m_dirty |= UniformsDirty;
231void QQuickShapeCurveRenderer::setStrokeWidth(
int index, qreal w)
233 auto &pathData = m_paths[index];
235 pathData.validPenWidth =
true;
236 pathData.pen.setWidthF(w);
238 pathData.validPenWidth =
false;
240 pathData.m_dirty |= StrokeDirty;
243void QQuickShapeCurveRenderer::setCosmeticStroke(
int index,
bool c)
245 auto &pathData = m_paths[index];
246 pathData.pen.setCosmetic(c);
247 pathData.m_dirty |= StrokeDirty;
250void QQuickShapeCurveRenderer::setFillColor(
int index,
const QColor &color)
252 auto &pathData = m_paths[index];
253 const bool wasVisible = pathData.isFillVisible();
254 pathData.fillColor = color;
255 if (pathData.isFillVisible() != wasVisible)
256 pathData.m_dirty |= FillDirty;
258 pathData.m_dirty |= UniformsDirty;
261void QQuickShapeCurveRenderer::setFillRule(
int index, QQuickShapePath::FillRule fillRule)
263 auto &pathData = m_paths[index];
264 pathData.fillRule = Qt::FillRule(fillRule);
265 pathData.m_dirty |= PathDirty;
268void QQuickShapeCurveRenderer::setJoinStyle(
int index,
269 QQuickShapePath::JoinStyle joinStyle,
272 auto &pathData = m_paths[index];
273 pathData.pen.setJoinStyle(Qt::PenJoinStyle(joinStyle));
274 pathData.pen.setMiterLimit(miterLimit);
275 pathData.m_dirty |= StrokeDirty;
278void QQuickShapeCurveRenderer::setCapStyle(
int index, QQuickShapePath::CapStyle capStyle)
280 auto &pathData = m_paths[index];
281 pathData.pen.setCapStyle(Qt::PenCapStyle(capStyle));
282 pathData.m_dirty |= StrokeDirty;
285void QQuickShapeCurveRenderer::setStrokeStyle(
int index,
286 QQuickShapePath::StrokeStyle strokeStyle,
288 const QVector<qreal> &dashPattern)
290 auto &pathData = m_paths[index];
291 pathData.pen.setStyle(Qt::PenStyle(strokeStyle));
292 if (strokeStyle == QQuickShapePath::DashLine) {
293 pathData.pen.setDashPattern(dashPattern);
294 pathData.pen.setDashOffset(dashOffset);
296 pathData.m_dirty |= StrokeDirty;
299void QQuickShapeCurveRenderer::setFillGradient(
int index, QQuickShapeGradient *gradient)
301 PathData &pd(m_paths[index]);
302 const bool wasVisible = pd.isFillVisible();
303 pd.gradientType = QGradient::NoGradient;
304 if (QQuickShapeLinearGradient *g = qobject_cast<QQuickShapeLinearGradient *>(gradient)) {
305 pd.gradientType = QGradient::LinearGradient;
306 pd.gradient.stops = gradient->gradientStops();
307 pd.gradient.spread = QGradient::Spread(gradient->spread());
308 pd.gradient.a = QPointF(g->x1(), g->y1());
309 pd.gradient.b = QPointF(g->x2(), g->y2());
310 }
else if (QQuickShapeRadialGradient *g = qobject_cast<QQuickShapeRadialGradient *>(gradient)) {
311 pd.gradientType = QGradient::RadialGradient;
312 pd.gradient.a = QPointF(g->centerX(), g->centerY());
313 pd.gradient.b = QPointF(g->focalX(), g->focalY());
314 pd.gradient.v0 = g->centerRadius();
315 pd.gradient.v1 = g->focalRadius();
316 }
else if (QQuickShapeConicalGradient *g = qobject_cast<QQuickShapeConicalGradient *>(gradient)) {
317 pd.gradientType = QGradient::ConicalGradient;
318 pd.gradient.a = QPointF(g->centerX(), g->centerY());
319 pd.gradient.v0 = g->angle();
320 }
else if (gradient !=
nullptr) {
321 static bool warned =
false;
324 qCWarning(lcShapeCurveRenderer) <<
"Unsupported gradient fill";
328 if (pd.gradientType != QGradient::NoGradient) {
329 pd.gradient.stops = gradient->gradientStops();
330 pd.gradient.spread = QGradient::Spread(gradient->spread());
333 pd.m_dirty |= (pd.isFillVisible() != wasVisible) ? FillDirty : UniformsDirty;
336void QQuickShapeCurveRenderer::setFillTransform(
int index,
const QSGTransform &transform)
338 auto &pathData = m_paths[index];
339 pathData.fillTransform = transform;
340 pathData.m_dirty |= UniformsDirty;
343void QQuickShapeCurveRenderer::setFillTextureProvider(
int index, QQuickItem *textureProviderItem)
345 auto &pathData = m_paths[index];
346 const bool wasVisible = pathData.isFillVisible();
347 if (pathData.fillTextureProviderItem !=
nullptr)
348 QQuickItemPrivate::get(pathData.fillTextureProviderItem)->derefWindow();
349 pathData.fillTextureProviderItem = textureProviderItem;
350 if (pathData.fillTextureProviderItem !=
nullptr)
351 QQuickItemPrivate::get(pathData.fillTextureProviderItem)->refWindow(m_item->window());
352 pathData.m_dirty |= (pathData.isFillVisible() != wasVisible) ? FillDirty : UniformsDirty;
355void QQuickShapeCurveRenderer::handleSceneChange(QQuickWindow *window)
357 for (
auto &pathData : m_paths) {
358 if (pathData.fillTextureProviderItem !=
nullptr) {
359 if (window ==
nullptr)
360 QQuickItemPrivate::get(pathData.fillTextureProviderItem)->derefWindow();
362 QQuickItemPrivate::get(pathData.fillTextureProviderItem)->refWindow(window);
367void QQuickShapeCurveRenderer::setAsyncCallback(
void (*callback)(
void *),
void *data)
369 m_asyncCallback = callback;
370 m_asyncCallbackData = data;
373void QQuickShapeCurveRenderer::endSync(
bool async)
375 bool asyncThreadsRunning =
false;
377 for (PathData &pathData : m_paths) {
378 if (!pathData.m_dirty)
381 if (pathData.m_dirty == UniformsDirty) {
386 if (pathData.currentRunner) {
389 if (pathData.currentRunner->isAsync) {
392 asyncThreadsRunning =
true;
396 delete pathData.currentRunner;
397 pathData.currentRunner =
nullptr;
401 pathData.currentRunner =
new QQuickShapeCurveRunnable;
402 setUpRunner(&pathData);
406 pathData.currentRunner->isAsync =
true;
407 QThreadPool::globalInstance()->start(pathData.currentRunner);
408 asyncThreadsRunning =
true;
412 pathData.currentRunner->run();
416 if (async && !asyncThreadsRunning && m_asyncCallback)
417 m_asyncCallback(m_asyncCallbackData);
420void QQuickShapeCurveRenderer::setUpRunner(PathData *pathData)
422 Q_ASSERT(pathData->currentRunner);
423 QQuickShapeCurveRunnable *runner = pathData->currentRunner;
424 runner->isDone =
false;
425 runner->pathData = *pathData;
426 runner->pathData.fillNodes.clear();
427 runner->pathData.strokeNodes.clear();
428 runner->pathData.currentRunner =
nullptr;
429 pathData->m_dirty = 0;
430 if (!runner->isInitialized) {
431 runner->isInitialized =
true;
432 runner->setAutoDelete(
false);
433 QObject::connect(runner, &QQuickShapeCurveRunnable::done, qApp,
434 [
this](QQuickShapeCurveRunnable *r) {
438 }
else if (r->isAsync) {
439 maybeUpdateAsyncItem();
445void QQuickShapeCurveRenderer::maybeUpdateAsyncItem()
447 for (
const PathData &pd : std::as_const(m_paths)) {
448 if (pd.currentRunner && !pd.currentRunner->isDone)
454 m_asyncCallback(m_asyncCallbackData);
459 qDeleteAll(pathData.fillNodes);
460 qDeleteAll(pathData.strokeNodes);
465 QQuickShapeCurveRenderer::processPath(&pathData);
469void QQuickShapeCurveRenderer::updateNode()
474 auto updateUniforms = [](
const PathData &pathData) {
475 for (
auto &pathNode : std::as_const(pathData.fillNodes)) {
476 if (pathNode->isDebugNode)
478 QSGCurveFillNode *fillNode =
static_cast<QSGCurveFillNode *>(pathNode);
479 fillNode->setColor(pathData.fillColor);
480 fillNode->setGradientType(pathData.gradientType);
481 fillNode->setFillGradient(pathData.gradient);
482 fillNode->setFillTransform(pathData.fillTransform);
483 fillNode->setFillTextureProvider(pathData.fillTextureProviderItem !=
nullptr
484 ? pathData.fillTextureProviderItem->textureProvider()
487 for (QSGCurveAbstractNode *pathNode : std::as_const(pathData.strokeNodes)) {
488 pathNode->setColor(pathData.pen.color());
489 if (pathNode->isDebugNode) {
490 auto *wfNode =
static_cast<QQuickShapeWireFrameNode<StrokeWFT> *>(pathNode);
491 wfNode->setStrokeWidth(pathData.pen.widthF());
492 wfNode->setCosmeticStroke(pathData.pen.isCosmetic());
494 auto *strokeNode =
static_cast<QSGCurveStrokeNode *>(pathNode);
495 strokeNode->setStrokeWidth(pathData.pen.widthF());
496 strokeNode->setCosmeticStroke(pathData.pen.isCosmetic());
501 NodeList toBeDeleted;
503 for (
const PathData &pathData : std::as_const(m_removedPaths)) {
504 toBeDeleted += pathData.fillNodes;
505 toBeDeleted += pathData.strokeNodes;
507 m_removedPaths.clear();
509 for (
int i = 0; i < m_paths.size(); i++) {
510 PathData &pathData = m_paths[i];
511 if (pathData.currentRunner) {
512 if (!pathData.currentRunner->isDone)
515 QSGNode *nextNode = pathData.strokeNodes.value(0);
517 for (
int j = i + 1; !nextNode && j < m_paths.size(); j++) {
518 const PathData &pd = m_paths[j];
519 nextNode = pd.fillNodes.isEmpty() ? pd.strokeNodes.value(0) : pd.fillNodes.value(0);
522 PathData &newData = pathData.currentRunner->pathData;
523 if (newData.m_dirty & PathDirty)
524 pathData.path = newData.path;
525 if (newData.m_dirty & FillDirty) {
526 pathData.fillPath = newData.fillPath;
527 for (
auto *node : std::as_const(newData.fillNodes)) {
529 m_rootNode->insertChildNodeBefore(node, nextNode);
531 m_rootNode->appendChildNode(node);
533 toBeDeleted += pathData.fillNodes;
534 pathData.fillNodes = newData.fillNodes;
536 if (newData.m_dirty & StrokeDirty) {
537 for (
auto *node : std::as_const(newData.strokeNodes)) {
539 m_rootNode->insertChildNodeBefore(node, nextNode);
541 m_rootNode->appendChildNode(node);
543 toBeDeleted += pathData.strokeNodes;
544 pathData.strokeNodes = newData.strokeNodes;
546 if (newData.m_dirty & UniformsDirty)
547 updateUniforms(newData);
550 newData.fillNodes.clear();
551 newData.strokeNodes.clear();
554 if (pathData.currentRunner->isAsync && (pathData.m_dirty & ~UniformsDirty)) {
556 setUpRunner(&pathData);
557 QThreadPool::globalInstance()->start(pathData.currentRunner);
561 pathData.currentRunner->deleteLater();
562 pathData.currentRunner =
nullptr;
566 if (pathData.m_dirty == UniformsDirty && !pathData.currentRunner) {
568 updateUniforms(pathData);
569 pathData.m_dirty = 0;
572 qDeleteAll(toBeDeleted);
575void QQuickShapeCurveRenderer::processPath(PathData *pathData)
577 static const bool doOverlapSolving = !qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_DISABLE_OVERLAP_SOLVER");
578 static const bool doIntersetionSolving = !qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_DISABLE_INTERSECTION_SOLVER");
579 static const bool useTriangulatingStroker = qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_TRIANGULATING_STROKER");
580 static const bool simplifyPath = qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_SIMPLIFY_PATHS");
582 int &dirtyFlags = pathData->m_dirty;
584 if (dirtyFlags & PathDirty) {
586 pathData->path = QQuadPath::fromPainterPath(pathData->originalPath.simplified(), QQuadPath::PathLinear | QQuadPath::PathNonIntersecting | QQuadPath::PathNonOverlappingControlPointTriangles);
588 pathData->path = QQuadPath::fromPainterPath(pathData->originalPath, QQuadPath::PathHints(
int(pathData->pathHints)));
589 pathData->path.setFillRule(pathData->fillRule);
590 pathData->fillPath = {};
591 dirtyFlags |= (FillDirty | StrokeDirty);
594 if (dirtyFlags & FillDirty) {
595 if (pathData->isFillVisible()) {
596 if (pathData->fillPath.isEmpty()) {
597 pathData->fillPath = pathData->path.subPathsClosed();
598 if (doIntersetionSolving)
599 QSGCurveProcessor::solveIntersections(pathData->fillPath);
600 pathData->fillPath.addCurvatureData();
601 if (doOverlapSolving)
602 QSGCurveProcessor::solveOverlaps(pathData->fillPath);
604 pathData->fillNodes = addFillNodes(pathData->fillPath);
605 dirtyFlags |= (StrokeDirty | UniformsDirty);
609 if (dirtyFlags & StrokeDirty) {
610 if (pathData->isStrokeVisible()) {
611 const QPen &pen = pathData->pen;
612 const bool solid = (pen.style() == Qt::SolidLine);
613 const QQuadPath &strokePath = solid ? pathData->path
614 : pathData->path.dashed(pen.widthF(),
617 if (useTriangulatingStroker)
618 pathData->strokeNodes = addTriangulatingStrokerNodes(strokePath, pen);
620 pathData->strokeNodes = addCurveStrokeNodes(strokePath, pen);
625QQuickShapeCurveRenderer::NodeList QQuickShapeCurveRenderer::addFillNodes(
const QQuadPath &path)
628 std::unique_ptr<QSGCurveFillNode> node(
new QSGCurveFillNode);
629 std::unique_ptr<QQuickShapeWireFrameNode<SimpleWFT>> wfNode;
631 const qsizetype approxDataCount = 20 * path.elementCount();
632 node->reserve(approxDataCount);
634 const int debugFlags = debugVisualization();
635 const bool wireFrame = debugFlags & DebugWireframe;
637 if (Q_LIKELY(!wireFrame)) {
638 QSGCurveProcessor::processFill(path,
640 [&node](
const std::array<QVector2D, 3> &v,
641 const std::array<QVector2D, 3> &n,
642 QSGCurveProcessor::uvForPointCallback uvForPoint)
644 node->appendTriangle(v, n, uvForPoint);
647 QVector<QQuickShapeWireFrameNode<SimpleWFT>::WireFrameVertex> wfVertices;
648 wfVertices.reserve(approxDataCount);
649 QSGCurveProcessor::processFill(path,
651 [&wfVertices, &node](
const std::array<QVector2D, 3> &v,
652 const std::array<QVector2D, 3> &n,
653 QSGCurveProcessor::uvForPointCallback uvForPoint)
655 node->appendTriangle(v, n, uvForPoint);
657 wfVertices.append({v.at(0).x(), v.at(0).y(), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f });
658 wfVertices.append({v.at(1).x(), v.at(1).y(), 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f });
659 wfVertices.append({v.at(2).x(), v.at(2).y(), 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f });
662 wfNode.reset(
new QQuickShapeWireFrameNode<SimpleWFT>);
663 const QVector<quint32> indices = node->uncookedIndexes();
664 QSGGeometry *wfg =
new QSGGeometry(QQuickShapeWireFrameNode<SimpleWFT>::attributes(),
667 QSGGeometry::UnsignedIntType);
668 wfNode->setGeometry(wfg);
670 wfg->setDrawingMode(QSGGeometry::DrawTriangles);
671 memcpy(wfg->indexData(),
673 indices.size() * wfg->sizeOfIndex());
674 memcpy(wfg->vertexData(),
676 wfg->vertexCount() * wfg->sizeOfVertex());
679 if (Q_UNLIKELY(debugFlags & DebugCurves))
680 node->setDebug(0.5f);
682 if (node->uncookedIndexes().size() > 0) {
683 node->cookGeometry();
684 ret.append(node.release());
686 ret.append(wfNode.release());
692QQuickShapeCurveRenderer::NodeList QQuickShapeCurveRenderer::addTriangulatingStrokerNodes(
const QQuadPath &path,
const QPen &pen)
695 const QColor &color = pen.color();
697 QVector<QQuickShapeWireFrameNode<StrokeWFT>::WireFrameVertex> wfVertices;
699 QTriangulatingStroker stroker;
700 const auto painterPath = path.toPainterPath();
701 const QVectorPath &vp = qtVectorPathForPath(painterPath);
702 stroker.process(vp, pen, {}, {});
704 auto *node =
new QSGCurveFillNode;
706 auto uvForPoint = [](QVector2D v1, QVector2D v2, QVector2D p)
708 double divisor = v1.x() * v2.y() - v2.x() * v1.y();
710 float u = (p.x() * v2.y() - p.y() * v2.x()) / divisor;
711 float v = (p.y() * v1.x() - p.x() * v1.y()) / divisor;
713 return QVector2D(u, v);
718 auto curveUv = [uvForPoint](QVector2D p0, QVector2D p1, QVector2D p2, QVector2D p)
720 QVector2D v1 = 2 * (p1 - p0);
721 QVector2D v2 = p2 - v1 - p0;
722 return uvForPoint(v1, v2, p - p0);
725 auto findPointOtherSide = [](
const QVector2D &startPoint,
const QVector2D &endPoint,
const QVector2D &referencePoint){
727 QVector2D baseLine = endPoint - startPoint;
728 QVector2D insideVector = referencePoint - startPoint;
729 QVector2D normal = QVector2D(-baseLine.y(), baseLine.x());
731 bool swap = QVector2D::dotProduct(insideVector, normal) < 0;
733 return swap ? startPoint + normal : startPoint - normal;
736 static bool disableExtraTriangles = qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_WIP_DISABLE_EXTRA_STROKE_TRIANGLES");
738 auto addStrokeTriangle = [&](
const QVector2D &p1,
const QVector2D &p2,
const QVector2D &p3){
739 if (p1 == p2 || p2 == p3) {
743 auto uvForPoint = [&p1, &p2, &p3, curveUv](QVector2D p) {
744 auto uv = curveUv(p1, p2, p3, p);
745 return QVector3D(uv.x(), uv.y(), 0.0f);
748 node->appendTriangle(p1, p2, p3, uvForPoint);
751 wfVertices.append({p1.x(), p1.y(), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f});
752 wfVertices.append({p2.x(), p2.y(), 0.0f, 0.1f, 0.0f, 0.0f, 0.0f, 1.0f});
753 wfVertices.append({p3.x(), p3.y(), 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f});
755 if (!disableExtraTriangles) {
758 QVector2D op = findPointOtherSide(p1, p3, p2);
759 node->appendTriangle(p1, op, p3, uvForPoint);
761 wfVertices.append({p1.x(), p1.y(), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f});
762 wfVertices.append({op.x(), op.y(), 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f});
763 wfVertices.append({p3.x(), p3.y(), 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f});
767 const int vertCount = stroker.vertexCount() / 2;
768 const float *verts = stroker.vertices();
769 for (
int i = 0; i < vertCount - 2; ++i) {
771 for (
int j = 0; j < 3; ++j) {
772 p[j] = QVector2D(verts[(i+j)*2], verts[(i+j)*2 + 1]);
774 addStrokeTriangle(p[0], p[1], p[2]);
777 QVector<quint32> indices = node->uncookedIndexes();
778 if (indices.size() > 0) {
779 node->setColor(color);
781 node->cookGeometry();
784 const bool wireFrame = debugVisualization() & DebugWireframe;
786 QQuickShapeWireFrameNode<StrokeWFT> *wfNode =
new QQuickShapeWireFrameNode<StrokeWFT>;
787 QSGGeometry *wfg =
new QSGGeometry(QQuickShapeWireFrameNode<StrokeWFT>::attributes(),
790 QSGGeometry::UnsignedIntType);
791 wfNode->setGeometry(wfg);
793 wfg->setDrawingMode(QSGGeometry::DrawTriangles);
794 memcpy(wfg->indexData(),
796 indices.size() * wfg->sizeOfIndex());
797 memcpy(wfg->vertexData(),
799 wfg->vertexCount() * wfg->sizeOfVertex());
807void QQuickShapeCurveRenderer::setRootNode(QSGNode *node)
809 clearNodeReferences();
813void QQuickShapeCurveRenderer::clearNodeReferences()
815 for (PathData &pd : m_paths) {
816 pd.fillNodes.clear();
817 pd.strokeNodes.clear();
821int QQuickShapeCurveRenderer::debugVisualizationFlags = QQuickShapeCurveRenderer::NoDebug;
823int QQuickShapeCurveRenderer::debugVisualization()
825 static const int envFlags = qEnvironmentVariableIntValue(
"QT_QUICKSHAPES_DEBUG");
826 return debugVisualizationFlags | envFlags;
829void QQuickShapeCurveRenderer::setDebugVisualization(
int options)
831 if (debugVisualizationFlags == options)
833 debugVisualizationFlags = options;
837
838
839
840
841QQuickShapeCurveRenderer::NodeList QQuickShapeCurveRenderer::addCurveStrokeNodes(
const QQuadPath &path,
const QPen &pen)
845 const bool debug = debugVisualization() & DebugCurves;
846 auto *node =
new QSGCurveStrokeNode;
847 node->setDebug(0.2f * debug);
848 QVector<QQuickShapeWireFrameNode<StrokeWFT>::WireFrameVertex> wfVertices;
850 const float penWidth = pen.widthF();
852 static const int subdivisions = qEnvironmentVariable(
"QT_QUICKSHAPES_STROKE_SUBDIVISIONS", QStringLiteral(
"3")).toInt();
854 const bool wireFrame = debugVisualization() & DebugWireframe;
855 QSGCurveProcessor::processStroke(path,
857 penWidth, pen.isCosmetic(),
861 [&wfVertices, &node, &wireFrame](
const std::array<QVector2D, 3> &vtx,
862 const std::array<QVector2D, 3> &ctl,
863 const std::array<QVector2D, 3> &n,
864 const std::array<
float, 3> &ex,
865 QSGCurveStrokeNode::TriangleFlags flags)
867 const QVector2D &v0 = vtx.at(0);
868 const QVector2D &v1 = vtx.at(1);
869 const QVector2D &v2 = vtx.at(2);
870 if (flags.testFlag(QSGCurveStrokeNode::TriangleFlag::Line))
871 node->appendTriangle(vtx, std::array<QVector2D, 2>{ctl.at(0), ctl.at(2)}, n, ex);
873 node->appendTriangle(vtx, ctl, n, ex);
875 if (Q_UNLIKELY(wireFrame)) {
876 wfVertices.append({v0.x(), v0.y(), 1.0f, 0.0f, 0.0f, n.at(0).x(), n.at(0).y(), ex.at(0)});
877 wfVertices.append({v1.x(), v1.y(), 0.0f, 1.0f, 0.0f, n.at(1).x(), n.at(1).y(), ex.at(1)});
878 wfVertices.append({v2.x(), v2.y(), 0.0f, 0.0f, 1.0f, n.at(2).x(), n.at(2).y(), ex.at(2)});
883 auto indexCopy = node->uncookedIndexes();
885 node->setColor(pen.color());
886 node->setStrokeWidth(penWidth);
887 node->setCosmeticStroke(pen.isCosmetic());
888 node->cookGeometry();
891 if (Q_UNLIKELY(wireFrame)) {
892 QQuickShapeWireFrameNode<StrokeWFT> *wfNode =
new QQuickShapeWireFrameNode<StrokeWFT>;
894 QSGGeometry *wfg =
new QSGGeometry(QQuickShapeWireFrameNode<StrokeWFT>::attributes(),
897 QSGGeometry::UnsignedIntType);
898 wfNode->setGeometry(wfg);
899 wfNode->setCosmeticStroke(pen.isCosmetic());
900 wfNode->setStrokeWidth(penWidth);
902 wfg->setDrawingMode(QSGGeometry::DrawTriangles);
903 memcpy(wfg->indexData(),
905 indexCopy.size() * wfg->sizeOfIndex());
906 memcpy(wfg->vertexData(),
908 wfg->vertexCount() * wfg->sizeOfVertex());
void run() override
Implement this pure virtual function in your subclass.