• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 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 "gm/gm.h"
9 #include "include/core/SkColor.h"
10 #include "include/utils/SkAnimCodecPlayer.h"
11 #include "modules/skottie/include/Skottie.h"
12 #include "modules/skottie/include/SkottieProperty.h"
13 #include "modules/skottie/utils/SkottieUtils.h"
14 #include "modules/skresources/include/SkResources.h"
15 #include "tools/Resources.h"
16 
17 #include <cmath>
18 #include <vector>
19 
20 namespace {
21 
22 static constexpr char kWebFontResource[] = "fonts/Roboto-Regular.ttf";
23 static constexpr char kSkottieResource[] = "skottie/skottie_sample_webfont.json";
24 
25 // Mock web font loader which serves a single local font (checked in under resources/).
26 class FakeWebFontProvider final : public skresources::ResourceProvider {
27 public:
FakeWebFontProvider()28     FakeWebFontProvider()
29         : fTypeface(SkTypeface::MakeFromData(GetResourceAsData(kWebFontResource))) {}
30 
loadTypeface(const char[],const char[]) const31     sk_sp<SkTypeface> loadTypeface(const char[], const char[]) const override {
32         return fTypeface;
33     }
34 
35 private:
36     sk_sp<SkTypeface> fTypeface;
37 
38     using INHERITED = skresources::ResourceProvider;
39 };
40 
41 } // namespace
42 
43 class SkottieExternalPropsGM : public skiagm::GM {
44 public:
45 protected:
onShortName()46     SkString onShortName() override {
47         return SkString("skottie_external_props");
48     }
49 
onISize()50     SkISize onISize() override {
51         return SkISize::Make(kSize, kSize);
52     }
53 
onOnceBeforeDraw()54     void onOnceBeforeDraw() override {
55         if (auto stream = GetResourceAsStream(kSkottieResource)) {
56             fPropManager = std::make_unique<skottie_utils::CustomPropertyManager>();
57             fAnimation = skottie::Animation::Builder()
58                             .setResourceProvider(sk_make_sp<FakeWebFontProvider>())
59                             .setPropertyObserver(fPropManager->getPropertyObserver())
60                             .make(stream.get());
61         }
62     }
63 
onDraw(SkCanvas * canvas,SkString * errorMsg)64     DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
65         if (!fAnimation) {
66             *errorMsg = "No animation";
67             return DrawResult::kFail;
68         }
69 
70         for (size_t i = 0; i < 4; ++i) {
71             this->update_props(i);
72             auto dest = SkRect::MakeWH(kSize/2, kSize/2).makeOffset(kSize * (i & 1) / 2,
73                                                                     kSize * (i & 2) / 4);
74             fAnimation->render(canvas, &dest);
75         }
76         return DrawResult::kOk;
77     }
78 
onAnimate(double nanos)79     bool onAnimate(double nanos) override {
80         if (!fAnimation) {
81             return false;
82         }
83 
84         const auto duration = fAnimation->duration();
85         fAnimation->seek(std::fmod(1e-9 * nanos, duration) / duration);
86         return true;
87     }
88 
89 private:
update_props(size_t i)90     void update_props(size_t i) {
91 
92         SkASSERT(i < 4);
93         if (!i) {
94             return;
95         }
96 
97         static constexpr struct {
98             const char* txt_string;
99             SkColor     txt_color,
100                         solid_color;
101             float       transform_scale;
102         } gTests[] = {
103             { "update #1", SK_ColorRED    , SK_ColorYELLOW, 100.f },
104             { "update #2", SK_ColorGREEN  , SK_ColorBLUE  ,  50.f },
105             { "update #3", SK_ColorMAGENTA, SK_ColorCYAN  , 150.f },
106         };
107 
108         SkASSERT(i - 1 < SK_ARRAY_COUNT(gTests));
109         const auto& tst = gTests[i - 1];
110 
111         for (const auto& prop : fPropManager->getColorProps()) {
112             SkAssertResult(fPropManager->setColor(prop, tst.solid_color));
113         }
114 
115         for (const auto& prop : fPropManager->getTransformProps()) {
116             auto t = fPropManager->getTransform(prop);
117             t.fScale = {tst.transform_scale, tst.transform_scale};
118             SkAssertResult(fPropManager->setTransform(prop, t));
119         }
120 
121         for (const auto& prop : fPropManager->getTextProps()) {
122             auto txt = fPropManager->getText(prop);
123             txt.fText.set(tst.txt_string);
124             txt.fFillColor = tst.txt_color;
125             SkAssertResult(fPropManager->setText(prop, txt));
126         }
127     }
128 
129     inline static constexpr SkScalar kSize = 800;
130 
131     sk_sp<skottie::Animation>                             fAnimation;
132     std::unique_ptr<skottie_utils::CustomPropertyManager> fPropManager;
133 
134     using INHERITED = skiagm::GM;
135 };
136 
137 DEF_GM(return new SkottieExternalPropsGM;)
138