diff options
-rw-r--r-- | experimental/skottie/Skottie.cpp | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/experimental/skottie/Skottie.cpp b/experimental/skottie/Skottie.cpp index 03429e7228..18c8c193c0 100644 --- a/experimental/skottie/Skottie.cpp +++ b/experimental/skottie/Skottie.cpp @@ -886,54 +886,57 @@ sk_sp<sksg::RenderNode> AttachTextLayer(const json::ValueRef& layer, AttachConte struct AttachLayerContext { AttachLayerContext(const json::ValueRef& jlayers, AttachContext* ctx) - : fLayerList(jlayers), fCtx(ctx) {} + : fLayerList(jlayers), fCtx(ctx) { + SkASSERT(fLayerList.isArray()); + } - const json::ValueRef fLayerList; + const json::ValueRef fLayerList; AttachContext* fCtx; SkTHashMap<int, sk_sp<sksg::Matrix>> fLayerMatrixMap; sk_sp<sksg::RenderNode> fCurrentMatte; - sk_sp<sksg::Matrix> AttachParentLayerMatrix(const json::ValueRef& jlayer) { + sk_sp<sksg::Matrix> AttachLayerMatrix(const json::ValueRef& jlayer) { + SkASSERT(jlayer.isObject()); + + const auto layer_index = jlayer["ind"].toDefault<int>(-1); + if (layer_index < 0) + return nullptr; + + if (auto* m = fLayerMatrixMap.find(layer_index)) + return *m; + + return this->AttachLayerMatrixImpl(jlayer, layer_index); + } + +private: + sk_sp<sksg::Matrix> AttachParentLayerMatrix(const json::ValueRef& jlayer, int layer_index) { SkASSERT(jlayer.isObject()); - SkASSERT(fLayerList.isArray()); const auto parent_index = jlayer["parent"].toDefault<int>(-1); - if (parent_index < 0) + if (parent_index < 0 || parent_index == layer_index) return nullptr; if (auto* m = fLayerMatrixMap.find(parent_index)) return *m; - sk_sp<sksg::Matrix> matrix; for (const json::ValueRef l : fLayerList) { if (l["ind"].toDefault<int>(-1) == parent_index) { - matrix = this->AttachLayerMatrix(l); - break; + return this->AttachLayerMatrixImpl(l, parent_index); } } return nullptr; } - sk_sp<sksg::Matrix> AttachLayerMatrix(const json::ValueRef& jlayer) { - SkASSERT(jlayer.isObject()); - - const auto layer_index = jlayer["ind"].toDefault<int>(-1); - if (layer_index < 0) - return nullptr; - - if (auto* m = fLayerMatrixMap.find(layer_index)) - return *m; + sk_sp<sksg::Matrix> AttachLayerMatrixImpl(const json::ValueRef& jlayer, int layer_index) { + SkASSERT(!fLayerMatrixMap.find(layer_index)); // Add a stub entry to break recursion cycles. fLayerMatrixMap.set(layer_index, nullptr); - auto parent_matrix = this->AttachParentLayerMatrix(jlayer); + auto parent_matrix = this->AttachParentLayerMatrix(jlayer, layer_index); - return *fLayerMatrixMap.set(layer_index, - AttachMatrix(jlayer["ks"], - fCtx, - this->AttachParentLayerMatrix(jlayer))); + return *fLayerMatrixMap.set(layer_index, AttachMatrix(jlayer["ks"], fCtx, parent_matrix)); } }; |