| 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "modules/skottie/src/layers/shapelayer/ShapeLayer.h" |
| 9 | |
| 10 | #include "include/core/SkPathTypes.h" |
| 11 | #include "include/private/base/SkAssert.h" |
| 12 | #include "include/private/base/SkDebug.h" |
| 13 | #include "modules/jsonreader/SkJSONReader.h" |
| 14 | #include "modules/skottie/include/Skottie.h" |
| 15 | #include "modules/skottie/include/SkottieProperty.h" |
| 16 | #include "modules/skottie/src/SkottieJson.h" |
| 17 | #include "modules/skottie/src/SkottiePriv.h" |
| 18 | #include "modules/skottie/src/animator/Animator.h" |
| 19 | #include "modules/sksg/include/SkSGDraw.h" |
| 20 | #include "modules/sksg/include/SkSGGeometryEffect.h" |
| 21 | #include "modules/sksg/include/SkSGGeometryNode.h" |
| 22 | #include "modules/sksg/include/SkSGGroup.h" |
| 23 | #include "modules/sksg/include/SkSGMerge.h" |
| 24 | #include "modules/sksg/include/SkSGPaint.h" |
| 25 | #include "modules/sksg/include/SkSGPath.h" // IWYU pragma: keep |
| 26 | #include "modules/sksg/include/SkSGRenderNode.h" |
| 27 | #include "modules/sksg/include/SkSGTransform.h" |
| 28 | |
| 29 | #include <string.h> |
| 30 | #include <algorithm> |
| 31 | #include <cstdint> |
| 32 | #include <cstdlib> |
| 33 | #include <iterator> |
| 34 | #include <utility> |
| 35 | |
| 36 | namespace skottie { |
| 37 | namespace internal { |
| 38 | |
| 39 | namespace { |
| 40 | |
| 41 | using GeometryAttacherT = sk_sp<sksg::GeometryNode> (*)(const skjson::ObjectValue&, |
| 42 | const AnimationBuilder*); |
| 43 | static constexpr GeometryAttacherT gGeometryAttachers[] = { |
| 44 | ShapeBuilder::AttachPathGeometry, |
| 45 | ShapeBuilder::AttachRRectGeometry, |
| 46 | ShapeBuilder::AttachEllipseGeometry, |
| 47 | ShapeBuilder::AttachPolystarGeometry, |
| 48 | }; |
| 49 | |
| 50 | using GeometryEffectAttacherT = |
| 51 | std::vector<sk_sp<sksg::GeometryNode>> (*)(const skjson::ObjectValue&, |
| 52 | const AnimationBuilder*, |
| 53 | std::vector<sk_sp<sksg::GeometryNode>>&&); |
| 54 | static constexpr GeometryEffectAttacherT gGeometryEffectAttachers[] = { |
| 55 | ShapeBuilder::AttachMergeGeometryEffect, |
| 56 | ShapeBuilder::AttachTrimGeometryEffect, |
| 57 | ShapeBuilder::AttachRoundGeometryEffect, |
| 58 | ShapeBuilder::AttachOffsetGeometryEffect, |
| 59 | ShapeBuilder::AttachPuckerBloatGeometryEffect, |
| 60 | }; |
| 61 | |
| 62 | using PaintAttacherT = sk_sp<sksg::PaintNode> (*)(const skjson::ObjectValue&, |
| 63 | const AnimationBuilder*); |
| 64 | static constexpr PaintAttacherT gPaintAttachers[] = { |
| 65 | ShapeBuilder::AttachColorFill, |
| 66 | ShapeBuilder::AttachColorStroke, |
| 67 | ShapeBuilder::AttachGradientFill, |
| 68 | ShapeBuilder::AttachGradientStroke, |
| 69 | }; |
| 70 | |
| 71 | // Some paint types (looking at you dashed-stroke) mess with the local geometry. |
| 72 | static constexpr GeometryEffectAttacherT gPaintGeometryAdjusters[] = { |
| 73 | nullptr, // color fill |
| 74 | ShapeBuilder::AdjustStrokeGeometry, // color stroke |
| 75 | nullptr, // gradient fill |
| 76 | ShapeBuilder::AdjustStrokeGeometry, // gradient stroke |
| 77 | }; |
| 78 | static_assert(std::size(gPaintGeometryAdjusters) == std::size(gPaintAttachers), ""); |
| 79 | |
| 80 | using DrawEffectAttacherT = |
| 81 | std::vector<sk_sp<sksg::RenderNode>> (*)(const skjson::ObjectValue&, |
| 82 | const AnimationBuilder*, |
| 83 | std::vector<sk_sp<sksg::RenderNode>>&&); |
| 84 | |
| 85 | static constexpr DrawEffectAttacherT gDrawEffectAttachers[] = { |
| 86 | ShapeBuilder::AttachRepeaterDrawEffect, |
| 87 | }; |
| 88 | |
| 89 | enum class ShapeType { |
| 90 | kGeometry, |
| 91 | kGeometryEffect, |
| 92 | kPaint, |
| 93 | kGroup, |
| 94 | kTransform, |
| 95 | kDrawEffect, |
| 96 | }; |
| 97 | |
| 98 | enum ShapeFlags : uint16_t { |
| 99 | kNone = 0x00, |
| 100 | kSuppressDraws = 0x01, |
| 101 | }; |
| 102 | |
| 103 | struct ShapeInfo { |
| 104 | const char* fTypeString; |
| 105 | ShapeType fShapeType; |
| 106 | uint16_t fAttacherIndex; // index into respective attacher tables |
| 107 | uint16_t fFlags; |
| 108 | }; |
| 109 | |
| 110 | const ShapeInfo* FindShapeInfo(const skjson::ObjectValue& jshape) { |
| 111 | static constexpr ShapeInfo gShapeInfo[] = { |
| 112 | { "el", ShapeType::kGeometry , 2, kNone }, // ellipse |
| 113 | { "fl", ShapeType::kPaint , 0, kNone }, // fill |
| 114 | { "gf", ShapeType::kPaint , 2, kNone }, // gfill |
| 115 | { "gr", ShapeType::kGroup , 0, kNone }, // group |
| 116 | { "gs", ShapeType::kPaint , 3, kNone }, // gstroke |
| 117 | { "mm", ShapeType::kGeometryEffect, 0, kSuppressDraws }, // merge |
| 118 | { "op", ShapeType::kGeometryEffect, 3, kNone }, // offset |
| 119 | { "pb", ShapeType::kGeometryEffect, 4, kNone }, // pucker/bloat |
| 120 | { "rc", ShapeType::kGeometry , 1, kNone }, // rrect |
| 121 | { "rd", ShapeType::kGeometryEffect, 2, kNone }, // round |
| 122 | { "rp", ShapeType::kDrawEffect , 0, kNone }, // repeater |
| 123 | { "sh", ShapeType::kGeometry , 0, kNone }, // shape |
| 124 | { "sr", ShapeType::kGeometry , 3, kNone }, // polystar |
| 125 | { "st", ShapeType::kPaint , 1, kNone }, // stroke |
| 126 | { "tm", ShapeType::kGeometryEffect, 1, kNone }, // trim |
| 127 | { "tr", ShapeType::kTransform , 0, kNone }, // transform |
| 128 | }; |
| 129 | |
| 130 | const skjson::StringValue* type = jshape["ty"]; |
| 131 | if (!type) { |
| 132 | return nullptr; |
| 133 | } |
| 134 | |
| 135 | const auto* info = bsearch(type->begin(), |
| 136 | gShapeInfo, |
| 137 | std::size(gShapeInfo), |
| 138 | sizeof(ShapeInfo), |
| 139 | [](const void* key, const void* info) { |
| 140 | return strcmp(static_cast<const char*>(key), |
| 141 | static_cast<const ShapeInfo*>(info)->fTypeString); |
| 142 | }); |
| 143 | |
| 144 | return static_cast<const ShapeInfo*>(info); |
| 145 | } |
| 146 | |
| 147 | struct GeometryEffectRec { |
| 148 | const skjson::ObjectValue& fJson; |
| 149 | GeometryEffectAttacherT fAttach; |
| 150 | }; |
| 151 | |
| 152 | // Unlike Skia, Lottie specifies the fill rule on paint, not on geometry. This is the transfer |
| 153 | // point, where we relocate the property to the geometry node as a local wrapper. |
| 154 | sk_sp<sksg::GeometryNode> AdjustGeometryFillRule(sk_sp<sksg::GeometryNode> geo, |
| 155 | const skjson::ObjectValue& jpaint) { |
| 156 | static constexpr SkPathFillType gFillTypes[] = { |
| 157 | SkPathFillType::kWinding, // "r": 1 |
| 158 | SkPathFillType::kEvenOdd, // "r": 2 |
| 159 | }; |
| 160 | const SkPathFillType ft = gFillTypes[std::min(ParseDefault<size_t>(jpaint["r"], 1) - 1, |
| 161 | std::size(gFillTypes) - 1)]; |
| 162 | return ft == SkPathFillType::kDefault |
| 163 | ? geo |
| 164 | : sksg::FillTypeOverride::Make(std::move(geo), ft); |
| 165 | } |
| 166 | |
| 167 | } // namespace |
| 168 | |
| 169 | sk_sp<sksg::GeometryNode> ShapeBuilder::AttachPathGeometry(const skjson::ObjectValue& jpath, |
| 170 | const AnimationBuilder* abuilder) { |
| 171 | return abuilder->attachPath(jpath["ks"]); |
| 172 | } |
| 173 | |
| 174 | struct AnimationBuilder::AttachShapeContext { |
| 175 | AttachShapeContext(std::vector<sk_sp<sksg::GeometryNode>>* geos, |
| 176 | std::vector<GeometryEffectRec>* effects, |
| 177 | size_t committedAnimators) |
| 178 | : fGeometryStack(geos) |
| 179 | , fGeometryEffectStack(effects) |
| 180 | , fCommittedAnimators(committedAnimators) {} |
| 181 | |
| 182 | std::vector<sk_sp<sksg::GeometryNode>>* fGeometryStack; |
| 183 | std::vector<GeometryEffectRec>* fGeometryEffectStack; |
| 184 | size_t fCommittedAnimators; |
| 185 | }; |
| 186 | |
| 187 | sk_sp<sksg::RenderNode> AnimationBuilder::attachShape(const skjson::ArrayValue* jshape, |
| 188 | AttachShapeContext* ctx, |
| 189 | bool suppress_draws) const { |
| 190 | if (!jshape) |
| 191 | return nullptr; |
| 192 | |
| 193 | SkDEBUGCODE(const auto initialGeometryEffects = ctx->fGeometryEffectStack->size();) |
| 194 | |
| 195 | const skjson::ObjectValue* jtransform = nullptr; |
| 196 | |
| 197 | struct ShapeRec { |
| 198 | const skjson::ObjectValue& fJson; |
| 199 | const ShapeInfo& fInfo; |
| 200 | bool fSuppressed; |
| 201 | }; |
| 202 | |
| 203 | // First pass (bottom->top): |
| 204 | // |
| 205 | // * pick up the group transform and opacity |
| 206 | // * push local geometry effects onto the stack |
| 207 | // * store recs for next pass |
| 208 | // |
| 209 | std::vector<ShapeRec> recs; |
| 210 | for (size_t i = 0; i < jshape->size(); ++i) { |
| 211 | const skjson::ObjectValue* shape = (*jshape)[jshape->size() - 1 - i]; |
| 212 | if (!shape) continue; |
| 213 | |
| 214 | const auto* info = FindShapeInfo(*shape); |
| 215 | if (!info) { |
| 216 | this->log(Logger::Level::kError, &(*shape)["ty"], "Unknown shape."); |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | if (ParseDefault<bool>((*shape)["hd"], false)) { |
| 221 | // Ignore hidden shapes. |
| 222 | continue; |
| 223 | } |
| 224 | |
| 225 | recs.push_back({ *shape, *info, suppress_draws }); |
| 226 | |
| 227 | // Some effects (merge) suppress any paints above them. |
| 228 | suppress_draws |= (info->fFlags & kSuppressDraws) != 0; |
| 229 | |
| 230 | switch (info->fShapeType) { |
| 231 | case ShapeType::kTransform: |
| 232 | // Just track the transform property for now -- we'll deal with it later. |
| 233 | jtransform = shape; |
| 234 | break; |
| 235 | case ShapeType::kGeometryEffect: |
| 236 | SkASSERT(info->fAttacherIndex < std::size(gGeometryEffectAttachers)); |
| 237 | ctx->fGeometryEffectStack->push_back( |
| 238 | { *shape, gGeometryEffectAttachers[info->fAttacherIndex] }); |
| 239 | break; |
| 240 | default: |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // Second pass (top -> bottom, after 2x reverse): |
| 246 | // |
| 247 | // * track local geometry |
| 248 | // * emit local paints |
| 249 | // |
| 250 | std::vector<sk_sp<sksg::GeometryNode>> geos; |
| 251 | std::vector<sk_sp<sksg::RenderNode >> draws; |
| 252 | |
| 253 | const auto add_draw = [this, &draws](sk_sp<sksg::RenderNode> draw, const ShapeRec& rec) { |
| 254 | // All draws can have an optional blend mode. |
| 255 | draws.push_back(this->attachBlendMode(rec.fJson, std::move(draw))); |
| 256 | }; |
| 257 | |
| 258 | for (auto rec = recs.rbegin(); rec != recs.rend(); ++rec) { |
| 259 | const AutoPropertyTracker apt(this, rec->fJson, PropertyObserver::NodeType::OTHER); |
| 260 | |
| 261 | switch (rec->fInfo.fShapeType) { |
| 262 | case ShapeType::kGeometry: { |
| 263 | SkASSERT(rec->fInfo.fAttacherIndex < std::size(gGeometryAttachers)); |
| 264 | if (auto geo = gGeometryAttachers[rec->fInfo.fAttacherIndex](rec->fJson, this)) { |
| 265 | geos.push_back(std::move(geo)); |
| 266 | } |
| 267 | } break; |
| 268 | case ShapeType::kGeometryEffect: { |
| 269 | // Apply the current effect and pop from the stack. |
| 270 | SkASSERT(rec->fInfo.fAttacherIndex < std::size(gGeometryEffectAttachers)); |
| 271 | if (!geos.empty()) { |
| 272 | geos = gGeometryEffectAttachers[rec->fInfo.fAttacherIndex](rec->fJson, |
| 273 | this, |
| 274 | std::move(geos)); |
| 275 | } |
| 276 | |
| 277 | SkASSERT(&ctx->fGeometryEffectStack->back().fJson == &rec->fJson); |
| 278 | SkASSERT(ctx->fGeometryEffectStack->back().fAttach == |
| 279 | gGeometryEffectAttachers[rec->fInfo.fAttacherIndex]); |
| 280 | ctx->fGeometryEffectStack->pop_back(); |
| 281 | } break; |
| 282 | case ShapeType::kGroup: { |
| 283 | AttachShapeContext groupShapeCtx(&geos, |
| 284 | ctx->fGeometryEffectStack, |
| 285 | ctx->fCommittedAnimators); |
| 286 | if (auto subgroup = |
| 287 | this->attachShape(rec->fJson["it"], &groupShapeCtx, rec->fSuppressed)) { |
| 288 | add_draw(std::move(subgroup), *rec); |
| 289 | SkASSERT(groupShapeCtx.fCommittedAnimators >= ctx->fCommittedAnimators); |
| 290 | ctx->fCommittedAnimators = groupShapeCtx.fCommittedAnimators; |
| 291 | } |
| 292 | } break; |
| 293 | case ShapeType::kPaint: { |
| 294 | SkASSERT(rec->fInfo.fAttacherIndex < std::size(gPaintAttachers)); |
| 295 | auto paint = gPaintAttachers[rec->fInfo.fAttacherIndex](rec->fJson, this); |
| 296 | if (!paint || geos.empty() || rec->fSuppressed) |
| 297 | break; |
| 298 | |
| 299 | auto drawGeos = geos; |
| 300 | |
| 301 | // Apply all pending effects from the stack. |
| 302 | for (auto it = ctx->fGeometryEffectStack->rbegin(); |
| 303 | it != ctx->fGeometryEffectStack->rend(); ++it) { |
| 304 | drawGeos = it->fAttach(it->fJson, this, std::move(drawGeos)); |
| 305 | } |
| 306 | |
| 307 | // Apply local paint geometry adjustments (e.g. dashing). |
| 308 | SkASSERT(rec->fInfo.fAttacherIndex < std::size(gPaintGeometryAdjusters)); |
| 309 | if (const auto adjuster = gPaintGeometryAdjusters[rec->fInfo.fAttacherIndex]) { |
| 310 | drawGeos = adjuster(rec->fJson, this, std::move(drawGeos)); |
| 311 | } |
| 312 | |
| 313 | // If we still have multiple geos, reduce using 'merge'. |
| 314 | auto geo = drawGeos.size() > 1 |
| 315 | ? ShapeBuilder::MergeGeometry(std::move(drawGeos), sksg::Merge::Mode::kMerge) |
| 316 | : drawGeos[0]; |
| 317 | SkASSERT(geo); |
| 318 | |
| 319 | // Apply paint-specific fill rule if needed. |
| 320 | geo = AdjustGeometryFillRule(std::move(geo), rec->fJson); |
| 321 | |
| 322 | add_draw(sksg::Draw::Make(std::move(geo), std::move(paint)), *rec); |
| 323 | ctx->fCommittedAnimators = fCurrentAnimatorScope->size(); |
| 324 | } break; |
| 325 | case ShapeType::kDrawEffect: { |
| 326 | SkASSERT(rec->fInfo.fAttacherIndex < std::size(gDrawEffectAttachers)); |
| 327 | if (!draws.empty()) { |
| 328 | draws = gDrawEffectAttachers[rec->fInfo.fAttacherIndex](rec->fJson, |
| 329 | this, |
| 330 | std::move(draws)); |
| 331 | ctx->fCommittedAnimators = fCurrentAnimatorScope->size(); |
| 332 | } |
| 333 | } break; |
| 334 | default: |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // By now we should have popped all local geometry effects. |
| 340 | SkASSERT(ctx->fGeometryEffectStack->size() == initialGeometryEffects); |
| 341 | |
| 342 | sk_sp<sksg::RenderNode> shape_wrapper; |
| 343 | if (draws.size() == 1) { |
| 344 | // For a single draw, we don't need a group. |
| 345 | shape_wrapper = std::move(draws.front()); |
| 346 | } else if (!draws.empty()) { |
| 347 | // Emit local draws reversed (bottom->top, per spec). |
| 348 | std::reverse(draws.begin(), draws.end()); |
| 349 | draws.shrink_to_fit(); |
| 350 | |
| 351 | // We need a group to dispatch multiple draws. |
| 352 | shape_wrapper = sksg::Group::Make(std::move(draws)); |
| 353 | } |
| 354 | |
| 355 | sk_sp<sksg::Transform> shape_transform; |
| 356 | if (jtransform) { |
| 357 | const AutoPropertyTracker apt(this, *jtransform, PropertyObserver::NodeType::OTHER); |
| 358 | |
| 359 | // This is tricky due to the interaction with ctx->fCommittedAnimators: we want any |
| 360 | // animators related to tranform/opacity to be committed => they must be inserted in front |
| 361 | // of the dangling/uncommitted ones. |
| 362 | AutoScope ascope(this); |
| 363 | |
| 364 | if ((shape_transform = this->attachMatrix2D(*jtransform, nullptr))) { |
| 365 | shape_wrapper = sksg::TransformEffect::Make(std::move(shape_wrapper), shape_transform); |
| 366 | } |
| 367 | shape_wrapper = this->attachOpacity(*jtransform, std::move(shape_wrapper)); |
| 368 | |
| 369 | auto local_scope = ascope.release(); |
| 370 | fCurrentAnimatorScope->insert(fCurrentAnimatorScope->begin() + ctx->fCommittedAnimators, |
| 371 | std::make_move_iterator(local_scope.begin()), |
| 372 | std::make_move_iterator(local_scope.end())); |
| 373 | ctx->fCommittedAnimators += local_scope.size(); |
| 374 | } |
| 375 | |
| 376 | // Push transformed local geometries to parent list, for subsequent paints. |
| 377 | for (auto& geo : geos) { |
| 378 | ctx->fGeometryStack->push_back(shape_transform |
| 379 | ? sksg::GeometryTransform::Make(std::move(geo), shape_transform) |
| 380 | : std::move(geo)); |
| 381 | } |
| 382 | |
| 383 | return shape_wrapper; |
| 384 | } |
| 385 | |
| 386 | sk_sp<sksg::RenderNode> AnimationBuilder::attachShapeLayer(const skjson::ObjectValue& layer, |
| 387 | LayerInfo*) const { |
| 388 | std::vector<sk_sp<sksg::GeometryNode>> geometryStack; |
| 389 | std::vector<GeometryEffectRec> geometryEffectStack; |
| 390 | AttachShapeContext shapeCtx(&geometryStack, &geometryEffectStack, |
| 391 | fCurrentAnimatorScope->size()); |
| 392 | auto shapeNode = this->attachShape(layer["shapes"], &shapeCtx); |
| 393 | |
| 394 | // Trim uncommitted animators: AttachShape consumes effects on the fly, and greedily attaches |
| 395 | // geometries => at the end, we can end up with unused geometries, which are nevertheless alive |
| 396 | // due to attached animators. To avoid this, we track committed animators and discard the |
| 397 | // orphans here. |
| 398 | SkASSERT(shapeCtx.fCommittedAnimators <= fCurrentAnimatorScope->size()); |
| 399 | fCurrentAnimatorScope->resize(shapeCtx.fCommittedAnimators); |
| 400 | |
| 401 | return shapeNode; |
| 402 | } |
| 403 | |
| 404 | } // namespace internal |
| 405 | } // namespace skottie |