| 1 |
/* |
| 2 |
* Copyright 2019 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 "include/core/SkColor.h" |
| 9 |
#include "include/core/SkPoint.h" |
| 10 |
#include "include/core/SkRefCnt.h" |
| 11 |
#include "include/core/SkScalar.h" |
| 12 |
#include "include/core/SkShader.h" |
| 13 |
#include "include/core/SkSize.h" |
| 14 |
#include "include/core/SkTileMode.h" |
| 15 |
#include "include/effects/SkGradient.h" |
| 16 |
#include "include/private/base/SkTPin.h" |
| 17 |
#include "modules/skottie/src/SkottiePriv.h" |
| 18 |
#include "modules/skottie/src/SkottieValue.h" |
| 19 |
#include "modules/skottie/src/effects/Effects.h" |
| 20 |
#include "modules/sksg/include/SkSGRenderNode.h" |
| 21 |
|
| 22 |
#include <algorithm> |
| 23 |
#include <cmath> |
| 24 |
#include <cstddef> |
| 25 |
#include <utility> |
| 26 |
|
| 27 |
namespace skjson { |
| 28 |
class ArrayValue; |
| 29 |
} |
| 30 |
|
| 31 |
namespace skottie { |
| 32 |
namespace internal { |
| 33 |
|
| 34 |
namespace { |
| 35 |
|
| 36 |
class LinearWipeAdapter final : public MaskShaderEffectBase { |
| 37 |
public: |
| 38 |
static sk_sp<LinearWipeAdapter> Make(const skjson::ArrayValue& jprops, |
| 39 |
sk_sp<sksg::RenderNode> layer, |
| 40 |
const SkSize& layer_size, |
| 41 |
const AnimationBuilder* abuilder) { |
| 42 |
return sk_sp<LinearWipeAdapter>(new LinearWipeAdapter(jprops, |
| 43 |
std::move(layer), |
| 44 |
layer_size, |
| 45 |
abuilder)); |
| 46 |
} |
| 47 |
|
| 48 |
private: |
| 49 |
LinearWipeAdapter(const skjson::ArrayValue& jprops, |
| 50 |
sk_sp<sksg::RenderNode> layer, |
| 51 |
const SkSize& layer_size, |
| 52 |
const AnimationBuilder* abuilder) |
| 53 |
: INHERITED(std::move(layer), layer_size) { |
| 54 |
enum : size_t { |
| 55 |
kCompletion_Index = 0, |
| 56 |
kAngle_Index = 1, |
| 57 |
kFeather_Index = 2, |
| 58 |
}; |
| 59 |
|
| 60 |
EffectBinder(jprops, *abuilder, this) |
| 61 |
.bind(kCompletion_Index, fCompletion) |
| 62 |
.bind( kAngle_Index, fAngle ) |
| 63 |
.bind( kFeather_Index, fFeather ); |
| 64 |
} |
| 65 |
|
| 66 |
MaskInfo onMakeMask() const override { |
| 67 |
if (fCompletion >= 100) { |
| 68 |
// The layer is fully disabled. |
| 69 |
// TODO: fix layer controller visibility clash and pass a null shader instead. |
| 70 |
return { SkShaders::Color(SK_ColorTRANSPARENT), false }; |
| 71 |
} |
| 72 |
|
| 73 |
if (fCompletion <= 0) { |
| 74 |
// The layer is fully visible (no mask). |
| 75 |
return { nullptr, true }; |
| 76 |
} |
| 77 |
|
| 78 |
const auto t = SkTPin(fCompletion * 0.01f, 0.0f, 1.0f), |
| 79 |
feather = std::max(fFeather, 0.0f), |
| 80 |
angle = SkDegreesToRadians(90 - fAngle), |
| 81 |
cos_ = std::cos(angle), |
| 82 |
sin_ = std::sin(angle); |
| 83 |
|
| 84 |
// Select the correct diagonal vector depending on quadrant. |
| 85 |
const SkVector angle_v = {cos_, sin_}, |
| 86 |
diag_v = {std::copysign(this->layerSize().width() , cos_), |
| 87 |
std::copysign(this->layerSize().height(), sin_)}; |
| 88 |
|
| 89 |
// The transition length is the projection of the diagonal onto the angle vector. |
| 90 |
const auto len = SkVector::DotProduct(diag_v, angle_v); |
| 91 |
|
| 92 |
// Pad the gradient segment to accommodate optional feather ramps at both extremities. |
| 93 |
const auto grad_len = len + feather * 2; |
| 94 |
const SkVector grad_v = angle_v * grad_len, |
| 95 |
adjusted_grad_v = { grad_v.fX, -grad_v.fY }, // Y flipped for drawing space. |
| 96 |
center_v = {0.5f * this->layerSize().width(), |
| 97 |
0.5f * this->layerSize().height()}; |
| 98 |
|
| 99 |
// Gradient start/end points: |
| 100 |
const SkPoint pts[] = { |
| 101 |
center_v - adjusted_grad_v * 0.5f, |
| 102 |
center_v + adjusted_grad_v * 0.5f, |
| 103 |
}; |
| 104 |
|
| 105 |
static constexpr SkColor4f colors[] = { SkColors::kTransparent, SkColors::kWhite }; |
| 106 |
|
| 107 |
// To emulate the feather effect, we distance the color stops to generate |
| 108 |
// a linear transition/ramp. For t == 0 the ramp should be completely outside/before |
| 109 |
// the transition domain, and for t == 1 it should be completely outside/after. |
| 110 |
// |
| 111 |
// [0 ................... |len|] |
| 112 |
// |
| 113 |
// [0 <feather_ramp> [ ] <feather_ramp> |grad_len|] |
| 114 |
const auto adjusted_t = t * (len + feather) / grad_len; |
| 115 |
const SkScalar pos[] = { adjusted_t, |
| 116 |
adjusted_t + feather / grad_len }; |
| 117 |
return { SkShaders::LinearGradient(pts, {{colors, pos, SkTileMode::kClamp}, {}}), true }; |
| 118 |
} |
| 119 |
|
| 120 |
ScalarValue fCompletion = 0, |
| 121 |
fAngle = 0, |
| 122 |
fFeather = 0; |
| 123 |
|
| 124 |
using INHERITED = MaskShaderEffectBase; |
| 125 |
}; |
| 126 |
|
| 127 |
} // namespace |
| 128 |
|
| 129 |
sk_sp<sksg::RenderNode> EffectBuilder::attachLinearWipeEffect(const skjson::ArrayValue& jprops, |
| 130 |
sk_sp<sksg::RenderNode> layer) const { |
| 131 |
return fBuilder->attachDiscardableAdapter<LinearWipeAdapter>(jprops, |
| 132 |
std::move(layer), |
| 133 |
fLayerSize, |
| 134 |
fBuilder); |
| 135 |
} |
| 136 |
|
| 137 |
} // namespace internal |
| 138 |
} // namespace skottie |
| 139 |
|