[skottie] Clean up AttachLayerContext

Avoid double "ind" lookups, avoid repeated AttachParentLayerMatrix
calls, hide private functions.

TBR=
Change-Id: I78f11593ffe241de3cdfabf7b3a88e8f4e62aa0c
Reviewed-on: https://skia-review.googlesource.com/127327
Commit-Queue: Florin Malita <fmalita@chromium.org>
Reviewed-by: Florin Malita <fmalita@chromium.org>
diff --git a/experimental/skottie/Skottie.cpp b/experimental/skottie/Skottie.cpp
index 03429e7..18c8c19 100644
--- a/experimental/skottie/Skottie.cpp
+++ b/experimental/skottie/Skottie.cpp
@@ -886,35 +886,15 @@
 
 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) {
-        SkASSERT(jlayer.isObject());
-        SkASSERT(fLayerList.isArray());
-
-        const auto parent_index = jlayer["parent"].toDefault<int>(-1);
-        if (parent_index < 0)
-            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 nullptr;
-    }
-
     sk_sp<sksg::Matrix> AttachLayerMatrix(const json::ValueRef& jlayer) {
         SkASSERT(jlayer.isObject());
 
@@ -925,15 +905,38 @@
         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());
+
+        const auto parent_index = jlayer["parent"].toDefault<int>(-1);
+        if (parent_index < 0 || parent_index == layer_index)
+            return nullptr;
+
+        if (auto* m = fLayerMatrixMap.find(parent_index))
+            return *m;
+
+        for (const json::ValueRef l : fLayerList) {
+            if (l["ind"].toDefault<int>(-1) == parent_index) {
+                return this->AttachLayerMatrixImpl(l, parent_index);
+            }
+        }
+
+        return nullptr;
+    }
+
+    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));
     }
 };