| 1 | /**************************************************************************** | 
| 2 | ** | 
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | 
| 4 | ** Contact: https://www.qt.io/licensing/ | 
| 5 | ** | 
| 6 | ** This file is part of the QtQuick module of the Qt Toolkit. | 
| 7 | ** | 
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | 
| 9 | ** Commercial License Usage | 
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | 
| 11 | ** accordance with the commercial license agreement provided with the | 
| 12 | ** Software or, alternatively, in accordance with the terms contained in | 
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | 
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | 
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | 
| 16 | ** | 
| 17 | ** GNU Lesser General Public License Usage | 
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
| 19 | ** General Public License version 3 as published by the Free Software | 
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | 
| 21 | ** packaging of this file. Please review the following information to | 
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | 
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | 
| 24 | ** | 
| 25 | ** GNU General Public License Usage | 
| 26 | ** Alternatively, this file may be used under the terms of the GNU | 
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | 
| 28 | ** Public license version 3 or any later version approved by the KDE Free | 
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | 
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | 
| 31 | ** included in the packaging of this file. Please review the following | 
| 32 | ** information to ensure the GNU General Public License requirements will | 
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | 
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | 
| 35 | ** | 
| 36 | ** $QT_END_LICENSE$ | 
| 37 | ** | 
| 38 | ****************************************************************************/ | 
| 39 |  | 
| 40 | #include "qsgsoftwarerenderablenodeupdater_p.h" | 
| 41 |  | 
| 42 | #include "qsgabstractsoftwarerenderer_p.h" | 
| 43 | #include "qsgsoftwareinternalimagenode_p.h" | 
| 44 | #include "qsgsoftwareinternalrectanglenode_p.h" | 
| 45 | #include "qsgsoftwareglyphnode_p.h" | 
| 46 | #include "qsgsoftwarepublicnodes_p.h" | 
| 47 | #include "qsgsoftwarepainternode_p.h" | 
| 48 | #include "qsgsoftwarepixmaptexture_p.h" | 
| 49 |  | 
| 50 | #include <QtQuick/qsgsimplerectnode.h> | 
| 51 | #include <QtQuick/qsgsimpletexturenode.h> | 
| 52 | #include <QtQuick/qsgrendernode.h> | 
| 53 |  | 
| 54 | QT_BEGIN_NAMESPACE | 
| 55 |  | 
| 56 | QSGSoftwareRenderableNodeUpdater::QSGSoftwareRenderableNodeUpdater(QSGAbstractSoftwareRenderer *renderer) | 
| 57 |     : m_renderer(renderer) | 
| 58 | { | 
| 59 |     m_opacityState.push(t: 1.0f); | 
| 60 |     // Invalid RectF by default for no clip | 
| 61 |     m_clipState.push(t: QRegion()); | 
| 62 |     m_hasClip = false; | 
| 63 |     m_transformState.push(t: QTransform()); | 
| 64 | } | 
| 65 |  | 
| 66 | QSGSoftwareRenderableNodeUpdater::~QSGSoftwareRenderableNodeUpdater() | 
| 67 | { | 
| 68 |  | 
| 69 | } | 
| 70 |  | 
| 71 | bool QSGSoftwareRenderableNodeUpdater::visit(QSGTransformNode *node) | 
| 72 | { | 
| 73 |     m_transformState.push(t: node->matrix().toTransform() * m_transformState.top()); | 
| 74 |     m_stateMap[node] = currentState(node); | 
| 75 |     return true; | 
| 76 | } | 
| 77 |  | 
| 78 | void QSGSoftwareRenderableNodeUpdater::endVisit(QSGTransformNode *) | 
| 79 | { | 
| 80 |     m_transformState.pop(); | 
| 81 | } | 
| 82 |  | 
| 83 | bool QSGSoftwareRenderableNodeUpdater::visit(QSGClipNode *node) | 
| 84 | { | 
| 85 |     // Make sure to translate the clip rect into world coordinates | 
| 86 |     if (m_clipState.count() == 0 || (m_clipState.count() == 1 && m_clipState.top().isNull())) { | 
| 87 |         m_clipState.push(t: m_transformState.top().map(r: QRegion(node->clipRect().toRect()))); | 
| 88 |         m_hasClip = true; | 
| 89 |     } else { | 
| 90 |         const QRegion transformedClipRect = m_transformState.top().map(r: QRegion(node->clipRect().toRect())); | 
| 91 |         m_clipState.push(t: transformedClipRect.intersected(r: m_clipState.top())); | 
| 92 |     } | 
| 93 |     m_stateMap[node] = currentState(node); | 
| 94 |     return true; | 
| 95 | } | 
| 96 |  | 
| 97 | void QSGSoftwareRenderableNodeUpdater::endVisit(QSGClipNode *) | 
| 98 | { | 
| 99 |     m_clipState.pop(); | 
| 100 |     if (m_clipState.count() == 0 || (m_clipState.count() == 1 && m_clipState.top().isNull())) | 
| 101 |         m_hasClip = false; | 
| 102 | } | 
| 103 |  | 
| 104 | bool QSGSoftwareRenderableNodeUpdater::visit(QSGGeometryNode *node) | 
| 105 | { | 
| 106 |     if (QSGSimpleRectNode *rectNode = dynamic_cast<QSGSimpleRectNode *>(node)) { | 
| 107 |         return updateRenderableNode(type: QSGSoftwareRenderableNode::SimpleRect, node: rectNode); | 
| 108 |     } else if (QSGSimpleTextureNode *tn = dynamic_cast<QSGSimpleTextureNode *>(node)) { | 
| 109 |         return updateRenderableNode(type: QSGSoftwareRenderableNode::SimpleTexture, node: tn); | 
| 110 |     } else if (QSGNinePatchNode *nn = dynamic_cast<QSGNinePatchNode *>(node)) { | 
| 111 |         return updateRenderableNode(type: QSGSoftwareRenderableNode::NinePatch, node: nn); | 
| 112 |     } else if (QSGRectangleNode *rn = dynamic_cast<QSGRectangleNode *>(node)) { | 
| 113 |         return updateRenderableNode(type: QSGSoftwareRenderableNode::SimpleRectangle, node: rn); | 
| 114 |     } else if (QSGImageNode *n = dynamic_cast<QSGImageNode *>(node)) { | 
| 115 |         return updateRenderableNode(type: QSGSoftwareRenderableNode::SimpleImage, node: n); | 
| 116 |     } else { | 
| 117 |         // We dont know, so skip | 
| 118 |         return false; | 
| 119 |     } | 
| 120 | } | 
| 121 |  | 
| 122 | void QSGSoftwareRenderableNodeUpdater::endVisit(QSGGeometryNode *) | 
| 123 | { | 
| 124 | } | 
| 125 |  | 
| 126 | bool QSGSoftwareRenderableNodeUpdater::visit(QSGOpacityNode *node) | 
| 127 | { | 
| 128 |     m_opacityState.push(t: m_opacityState.top() * node->opacity()); | 
| 129 |     m_stateMap[node] = currentState(node); | 
| 130 |     return true; | 
| 131 | } | 
| 132 |  | 
| 133 | void QSGSoftwareRenderableNodeUpdater::endVisit(QSGOpacityNode *) | 
| 134 | { | 
| 135 |     m_opacityState.pop(); | 
| 136 | } | 
| 137 |  | 
| 138 | bool QSGSoftwareRenderableNodeUpdater::visit(QSGInternalImageNode *node) | 
| 139 | { | 
| 140 |     return updateRenderableNode(type: QSGSoftwareRenderableNode::Image, node); | 
| 141 | } | 
| 142 |  | 
| 143 | void QSGSoftwareRenderableNodeUpdater::endVisit(QSGInternalImageNode *) | 
| 144 | { | 
| 145 | } | 
| 146 |  | 
| 147 | bool QSGSoftwareRenderableNodeUpdater::visit(QSGPainterNode *node) | 
| 148 | { | 
| 149 |     return updateRenderableNode(type: QSGSoftwareRenderableNode::Painter, node); | 
| 150 | } | 
| 151 |  | 
| 152 | void QSGSoftwareRenderableNodeUpdater::endVisit(QSGPainterNode *) | 
| 153 | { | 
| 154 | } | 
| 155 |  | 
| 156 | bool QSGSoftwareRenderableNodeUpdater::visit(QSGInternalRectangleNode *node) | 
| 157 | { | 
| 158 |     return updateRenderableNode(type: QSGSoftwareRenderableNode::Rectangle, node); | 
| 159 | } | 
| 160 |  | 
| 161 | void QSGSoftwareRenderableNodeUpdater::endVisit(QSGInternalRectangleNode *) | 
| 162 | { | 
| 163 | } | 
| 164 |  | 
| 165 | bool QSGSoftwareRenderableNodeUpdater::visit(QSGGlyphNode *node) | 
| 166 | { | 
| 167 |     return updateRenderableNode(type: QSGSoftwareRenderableNode::Glyph, node); | 
| 168 | } | 
| 169 |  | 
| 170 | void QSGSoftwareRenderableNodeUpdater::endVisit(QSGGlyphNode *) | 
| 171 | { | 
| 172 | } | 
| 173 |  | 
| 174 | bool QSGSoftwareRenderableNodeUpdater::visit(QSGRootNode *node) | 
| 175 | { | 
| 176 |     m_stateMap[node] = currentState(node); | 
| 177 |     return true; | 
| 178 | } | 
| 179 |  | 
| 180 | void QSGSoftwareRenderableNodeUpdater::endVisit(QSGRootNode *) | 
| 181 | { | 
| 182 | } | 
| 183 |  | 
| 184 | #if QT_CONFIG(quick_sprite) | 
| 185 | bool QSGSoftwareRenderableNodeUpdater::visit(QSGSpriteNode *node) | 
| 186 | { | 
| 187 |     return updateRenderableNode(type: QSGSoftwareRenderableNode::SpriteNode, node); | 
| 188 | } | 
| 189 |  | 
| 190 | void QSGSoftwareRenderableNodeUpdater::endVisit(QSGSpriteNode *) | 
| 191 | { | 
| 192 |  | 
| 193 | } | 
| 194 | #endif | 
| 195 |  | 
| 196 | bool QSGSoftwareRenderableNodeUpdater::visit(QSGRenderNode *node) | 
| 197 | { | 
| 198 |     return updateRenderableNode(type: QSGSoftwareRenderableNode::RenderNode, node); | 
| 199 | } | 
| 200 |  | 
| 201 | void QSGSoftwareRenderableNodeUpdater::endVisit(QSGRenderNode *) | 
| 202 | { | 
| 203 | } | 
| 204 |  | 
| 205 | void QSGSoftwareRenderableNodeUpdater::updateNodes(QSGNode *node, bool isNodeRemoved) | 
| 206 | { | 
| 207 |     m_opacityState.clear(); | 
| 208 |     m_clipState.clear(); | 
| 209 |     m_transformState.clear(); | 
| 210 |  | 
| 211 |     auto parentNode = node->parent(); | 
| 212 |     // If the node was deleted, it will have no parent | 
| 213 |     // check if the state map has the previous parent | 
| 214 |     if ((!parentNode || isNodeRemoved ) && m_stateMap.contains(akey: node)) | 
| 215 |         parentNode = m_stateMap[node].parent; | 
| 216 |  | 
| 217 |     // If we find a parent, use its state for updating the new children | 
| 218 |     if (parentNode && m_stateMap.contains(akey: parentNode)) { | 
| 219 |         auto state = m_stateMap[parentNode]; | 
| 220 |         m_opacityState.push(t: state.opacity); | 
| 221 |         m_transformState.push(t: state.transform); | 
| 222 |         m_clipState.push(t: state.clip); | 
| 223 |         m_hasClip = state.hasClip; | 
| 224 |     } else { | 
| 225 |         // There is no parent, and no previous parent, so likely a root node | 
| 226 |         m_opacityState.push(t: 1.0f); | 
| 227 |         m_transformState.push(t: QTransform()); | 
| 228 |         m_clipState.push(t: QRegion()); | 
| 229 |         m_hasClip = false; | 
| 230 |     } | 
| 231 |  | 
| 232 |     // If the node is being removed, then cleanup the state data | 
| 233 |     // Then just visit the children without visiting the now removed node | 
| 234 |     if (isNodeRemoved) { | 
| 235 |         m_stateMap.remove(akey: node); | 
| 236 |         return; | 
| 237 |     } | 
| 238 |  | 
| 239 |     // Visit the current node itself first | 
| 240 |     switch (node->type()) { | 
| 241 |     case QSGNode::ClipNodeType: { | 
| 242 |         QSGClipNode *c = static_cast<QSGClipNode*>(node); | 
| 243 |         if (visit(node: c)) | 
| 244 |             visitChildren(node: c); | 
| 245 |         endVisit(c); | 
| 246 |         break; | 
| 247 |     } | 
| 248 |     case QSGNode::TransformNodeType: { | 
| 249 |         QSGTransformNode *c = static_cast<QSGTransformNode*>(node); | 
| 250 |         if (visit(node: c)) | 
| 251 |             visitChildren(node: c); | 
| 252 |         endVisit(c); | 
| 253 |         break; | 
| 254 |     } | 
| 255 |     case QSGNode::OpacityNodeType: { | 
| 256 |         QSGOpacityNode *c = static_cast<QSGOpacityNode*>(node); | 
| 257 |         if (visit(node: c)) | 
| 258 |             visitChildren(node: c); | 
| 259 |         endVisit(c); | 
| 260 |         break; | 
| 261 |     } | 
| 262 |     case QSGNode::GeometryNodeType: { | 
| 263 |         if (node->flags() & QSGNode::IsVisitableNode) { | 
| 264 |             QSGVisitableNode *v = static_cast<QSGVisitableNode*>(node); | 
| 265 |             v->accept(this); | 
| 266 |         } else { | 
| 267 |             QSGGeometryNode *c = static_cast<QSGGeometryNode*>(node); | 
| 268 |             if (visit(node: c)) | 
| 269 |                 visitChildren(node: c); | 
| 270 |             endVisit(c); | 
| 271 |         } | 
| 272 |         break; | 
| 273 |     } | 
| 274 |     case QSGNode::RootNodeType: { | 
| 275 |         QSGRootNode *root = static_cast<QSGRootNode*>(node); | 
| 276 |         if (visit(node: root)) | 
| 277 |             visitChildren(node: root); | 
| 278 |         endVisit(root); | 
| 279 |         break; | 
| 280 |     } | 
| 281 |     case QSGNode::BasicNodeType: { | 
| 282 |             visitChildren(node); | 
| 283 |         break; | 
| 284 |     } | 
| 285 |     case QSGNode::RenderNodeType: { | 
| 286 |         QSGRenderNode *r = static_cast<QSGRenderNode*>(node); | 
| 287 |         if (visit(node: r)) | 
| 288 |             visitChildren(node: r); | 
| 289 |         endVisit(r); | 
| 290 |         break; | 
| 291 |     } | 
| 292 |     default: | 
| 293 |         Q_UNREACHABLE(); | 
| 294 |         break; | 
| 295 |     } | 
| 296 | } | 
| 297 |  | 
| 298 | QSGSoftwareRenderableNodeUpdater::NodeState QSGSoftwareRenderableNodeUpdater::currentState(QSGNode *node) const | 
| 299 | { | 
| 300 |     NodeState state; | 
| 301 |     state.opacity = m_opacityState.top(); | 
| 302 |     state.clip = m_clipState.top(); | 
| 303 |     state.hasClip = m_hasClip; | 
| 304 |     state.transform = m_transformState.top(); | 
| 305 |     state.parent = node->parent(); | 
| 306 |     return state; | 
| 307 | } | 
| 308 |  | 
| 309 | QT_END_NAMESPACE | 
| 310 |  |