From 5fd71e9bd83935c907eb5ad8ffc270ae42ff05ae Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Fri, 8 Aug 2014 12:10:11 +0200 Subject: Adapt to new visitor pattern for rendering Change-Id: I32b5d748a5cadcb88511f401924d3b3635275881 Reviewed-by: Lars Knoll --- softwarecontext/context.cpp | 34 +-------------- softwarecontext/context.h | 3 +- softwarecontext/glyphnode.h | 2 +- softwarecontext/imagenode.h | 2 +- softwarecontext/rectanglenode.h | 2 +- softwarecontext/renderingvisitor.cpp | 81 ++++++++++++++++++++++++++++++++++++ softwarecontext/renderingvisitor.h | 30 +++++++++++++ softwarecontext/softwarecontext.pro | 6 ++- 8 files changed, 121 insertions(+), 39 deletions(-) create mode 100644 softwarecontext/renderingvisitor.cpp create mode 100644 softwarecontext/renderingvisitor.h (limited to 'softwarecontext') diff --git a/softwarecontext/context.cpp b/softwarecontext/context.cpp index 9d0b7a3097..d656125f0c 100644 --- a/softwarecontext/context.cpp +++ b/softwarecontext/context.cpp @@ -45,6 +45,7 @@ #include "imagenode.h" #include "pixmaptexture.h" #include "glyphnode.h" +#include "renderingvisitor.h" #include #include @@ -99,42 +100,12 @@ void Renderer::render() painter.setRenderHint(QPainter::Antialiasing); painter.fillRect(rect, Qt::white); - renderNode(&painter, rootNode()); + RenderingVisitor(&painter).visitChildren(rootNode()); backingStore->endPaint(); backingStore->flush(rect); } -void Renderer::renderNode(QPainter *painter, QSGNode *node) -{ - bool restore = false; - - if (node->type() == QSGNode::TransformNodeType) { - QSGTransformNode *tn = static_cast(node); - painter->save(); - restore = true; - painter->setTransform(tn->matrix().toTransform(), /*combine*/true); - } else if (node->type() == QSGNode::ClipNodeType) { - QSGClipNode *cn = static_cast(node); - painter->save(); - restore = true; - painter->setClipRect(cn->clipRect(), Qt::IntersectClip); - } else if (node->type() == QSGNode::OpacityNodeType) { - QSGOpacityNode *on = static_cast(node); - painter->save(); - restore = true; - painter->setOpacity(on->opacity()); - } - - node->paint(painter); - - for (QSGNode *child = node->firstChild(); child; child = child->nextSibling()) - renderNode(painter, child); - - if (restore) - painter->restore(); -} - RenderContext::RenderContext(QSGContext *ctx) : QSGRenderContext(ctx) , currentWindow(0) @@ -192,5 +163,4 @@ void RenderContext::renderNextFrame(QSGRenderer *renderer, GLuint fbo) QSGRenderContext::renderNextFrame(renderer, fbo); } - } // namespace diff --git a/softwarecontext/context.h b/softwarecontext/context.h index 0eb51ba9cd..b2d478d70a 100644 --- a/softwarecontext/context.h +++ b/softwarecontext/context.h @@ -45,6 +45,7 @@ #include #include +#include #include #include #include @@ -62,8 +63,6 @@ public: virtual void render(); private: - void renderNode(QPainter *painter, QSGNode *node); - QScopedPointer backingStore; }; diff --git a/softwarecontext/glyphnode.h b/softwarecontext/glyphnode.h index 1724ef8394..08d4d18895 100644 --- a/softwarecontext/glyphnode.h +++ b/softwarecontext/glyphnode.h @@ -16,7 +16,7 @@ public: virtual void setPreferredAntialiasingMode(AntialiasingMode); virtual void update(); - virtual void paint(QPainter *painter); + void paint(QPainter *painter); private: QPointF m_position; diff --git a/softwarecontext/imagenode.h b/softwarecontext/imagenode.h index bfa7447a83..5b59da2f3b 100644 --- a/softwarecontext/imagenode.h +++ b/softwarecontext/imagenode.h @@ -21,7 +21,7 @@ public: virtual void setVerticalWrapMode(QSGTexture::WrapMode wrapMode); virtual void update(); - virtual void paint(QPainter *painter); + void paint(QPainter *painter); private: QRectF m_targetRect; diff --git a/softwarecontext/rectanglenode.h b/softwarecontext/rectanglenode.h index 2f1051df52..3405c8db84 100644 --- a/softwarecontext/rectanglenode.h +++ b/softwarecontext/rectanglenode.h @@ -22,7 +22,7 @@ public: virtual void update(); - virtual void paint(QPainter *); + void paint(QPainter *); private: QRectF m_rect; diff --git a/softwarecontext/renderingvisitor.cpp b/softwarecontext/renderingvisitor.cpp new file mode 100644 index 0000000000..3a58600972 --- /dev/null +++ b/softwarecontext/renderingvisitor.cpp @@ -0,0 +1,81 @@ +#include "renderingvisitor.h" + +#include "imagenode.h" +#include "rectanglenode.h" +#include "glyphnode.h" + +RenderingVisitor::RenderingVisitor(QPainter *painter) + : painter(painter) +{ + +} + +void RenderingVisitor::visit(QSGTransformNode *node) +{ + painter->save(); + painter->setTransform(node->matrix().toTransform(), /*combine*/true); +} + +void RenderingVisitor::endVisit(QSGTransformNode *) +{ + painter->restore(); +} + +void RenderingVisitor::visit(QSGClipNode *node) +{ + painter->save(); + painter->setClipRect(node->clipRect(), Qt::IntersectClip); +} + +void RenderingVisitor::endVisit(QSGClipNode *) +{ + painter->restore(); +} + +void RenderingVisitor::visit(QSGGeometryNode *node) +{ + Q_UNREACHABLE(); +} + +void RenderingVisitor::endVisit(QSGGeometryNode *node) +{ + Q_UNREACHABLE(); +} + +void RenderingVisitor::visit(QSGOpacityNode *node) +{ + painter->save(); + painter->setOpacity(node->opacity()); +} + +void RenderingVisitor::endVisit(QSGOpacityNode *node) +{ + painter->restore(); +} + +void RenderingVisitor::visit(QSGImageNode *node) +{ + static_cast(node)->paint(painter); +} + +void RenderingVisitor::endVisit(QSGImageNode *) +{ +} + +void RenderingVisitor::visit(QSGRectangleNode *node) +{ + static_cast(node)->paint(painter); +} + +void RenderingVisitor::endVisit(QSGRectangleNode *) +{ +} + +void RenderingVisitor::visit(QSGGlyphNode *node) +{ + static_cast(node)->paint(painter); +} + +void RenderingVisitor::endVisit(QSGGlyphNode *) +{ +} diff --git a/softwarecontext/renderingvisitor.h b/softwarecontext/renderingvisitor.h new file mode 100644 index 0000000000..213903aa5f --- /dev/null +++ b/softwarecontext/renderingvisitor.h @@ -0,0 +1,30 @@ +#ifndef RENDERINGVISITOR_H +#define RENDERINGVISITOR_H + +#include + +class RenderingVisitor : public QSGNodeVisitorEx +{ +public: + RenderingVisitor(QPainter *painter); + + virtual void visit(QSGTransformNode *node); + virtual void endVisit(QSGTransformNode *); + virtual void visit(QSGClipNode *node); + virtual void endVisit(QSGClipNode *node); + virtual void visit(QSGGeometryNode *node); + virtual void endVisit(QSGGeometryNode *node); + virtual void visit(QSGOpacityNode *node); + virtual void endVisit(QSGOpacityNode *node); + virtual void visit(QSGImageNode *node); + virtual void endVisit(QSGImageNode *node); + virtual void visit(QSGRectangleNode *node); + virtual void endVisit(QSGRectangleNode *node); + virtual void visit(QSGGlyphNode *node); + virtual void endVisit(QSGGlyphNode *node); + +private: + QPainter *painter; +}; + +#endif // RENDERINGVISITOR_H diff --git a/softwarecontext/softwarecontext.pro b/softwarecontext/softwarecontext.pro index 31bb122eaf..bde31d2773 100644 --- a/softwarecontext/softwarecontext.pro +++ b/softwarecontext/softwarecontext.pro @@ -12,7 +12,8 @@ SOURCES += \ rectanglenode.cpp \ imagenode.cpp \ pixmaptexture.cpp \ - glyphnode.cpp + glyphnode.cpp \ + renderingvisitor.cpp HEADERS += \ context.h \ @@ -21,7 +22,8 @@ HEADERS += \ rectanglenode.h \ imagenode.h \ pixmaptexture.h \ - glyphnode.h + glyphnode.h \ + renderingvisitor.h OTHER_FILES += softwarecontext.json -- cgit v1.2.3