Flutter Engine
The Flutter Engine
ExternalProperties.cpp
Go to the documentation of this file.
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"
18#include "tools/Resources.h"
20
21#include <cmath>
22#include <vector>
23
24namespace {
25
26static constexpr char kWebFontResource[] = "fonts/Roboto-Regular.ttf";
27static constexpr char kSkottieResource[] = "skottie/skottie_sample_webfont.json";
28
29// Mock web font loader which serves a single local font (checked in under resources/).
30class FakeWebFontProvider final : public skresources::ResourceProvider {
31public:
32 FakeWebFontProvider() : fTypeface(ToolUtils::CreateTypefaceFromResource(kWebFontResource)) {}
33
34 sk_sp<SkTypeface> loadTypeface(const char[], const char[]) const override {
35 return fTypeface;
36 }
37
38private:
39 sk_sp<SkTypeface> fTypeface;
40
42};
43
44} // namespace
45
47public:
48protected:
49 SkString getName() const override { return SkString("skottie_external_props"); }
50
51 SkISize getISize() override { return SkISize::Make(kSize, kSize); }
52
53 void onOnceBeforeDraw() override {
54 if (auto stream = GetResourceAsStream(kSkottieResource)) {
55 fPropManager = std::make_unique<skottie_utils::CustomPropertyManager>();
56 fAnimation = skottie::Animation::Builder()
57 .setFontManager(ToolUtils::TestFontMgr())
58 .setResourceProvider(sk_make_sp<FakeWebFontProvider>())
59 .setPropertyObserver(fPropManager->getPropertyObserver())
60 .setTextShapingFactory(SkShapers::BestAvailable())
61 .make(stream.get());
62 }
63 }
64
65 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
66 if (!fAnimation) {
67 *errorMsg = "No animation";
68 return DrawResult::kFail;
69 }
70
71 for (size_t i = 0; i < 4; ++i) {
72 this->update_props(i);
73 auto dest = SkRect::MakeWH(kSize/2, kSize/2).makeOffset(kSize * (i & 1) / 2,
74 kSize * (i & 2) / 4);
75 fAnimation->render(canvas, &dest);
76 }
77 return DrawResult::kOk;
78 }
79
80 bool onAnimate(double nanos) override {
81 if (!fAnimation) {
82 return false;
83 }
84
85 const auto duration = fAnimation->duration();
86 fAnimation->seek(std::fmod(1e-9 * nanos, duration) / duration);
87 return true;
88 }
89
90private:
91 void update_props(size_t i) {
92
93 SkASSERT(i < 4);
94 if (!i) {
95 return;
96 }
97
98 static constexpr struct {
99 const char* txt_string;
100 SkColor txt_color,
101 solid_color;
102 float transform_scale;
103 } gTests[] = {
104 { "update #1", SK_ColorRED , SK_ColorYELLOW, 100.f },
105 { "update #2", SK_ColorGREEN , SK_ColorBLUE , 50.f },
106 { "update #3", SK_ColorMAGENTA, SK_ColorCYAN , 150.f },
107 };
108
109 SkASSERT(i - 1 < std::size(gTests));
110 const auto& tst = gTests[i - 1];
111
112 for (const auto& prop : fPropManager->getColorProps()) {
113 SkAssertResult(fPropManager->setColor(prop, tst.solid_color));
114 }
115
116 for (const auto& prop : fPropManager->getTransformProps()) {
117 auto t = fPropManager->getTransform(prop);
118 t.fScale = {tst.transform_scale, tst.transform_scale};
119 SkAssertResult(fPropManager->setTransform(prop, t));
120 }
121
122 for (const auto& prop : fPropManager->getTextProps()) {
123 auto txt = fPropManager->getText(prop);
124 txt.fText.set(tst.txt_string);
125 txt.fFillColor = tst.txt_color;
126 SkAssertResult(fPropManager->setText(prop, txt));
127 }
128 }
129
130 inline static constexpr SkScalar kSize = 800;
131
132 sk_sp<skottie::Animation> fAnimation;
133 std::unique_ptr<skottie_utils::CustomPropertyManager> fPropManager;
134
135 using INHERITED = skiagm::GM;
136};
137
static const TestCase gTests[]
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
std::unique_ptr< SkStreamAsset > GetResourceAsStream(const char *resource, bool useFileStream)
Definition: Resources.cpp:31
#define SkASSERT(cond)
Definition: SkAssert.h:116
constexpr SkColor SK_ColorYELLOW
Definition: SkColor.h:139
constexpr SkColor SK_ColorMAGENTA
Definition: SkColor.h:147
uint32_t SkColor
Definition: SkColor.h:37
constexpr SkColor SK_ColorCYAN
Definition: SkColor.h:143
constexpr SkColor SK_ColorBLUE
Definition: SkColor.h:135
constexpr SkColor SK_ColorRED
Definition: SkColor.h:126
constexpr SkColor SK_ColorGREEN
Definition: SkColor.h:131
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
skresources::ResourceProvider ResourceProvider
DrawResult onDraw(SkCanvas *canvas, SkString *errorMsg) override
bool onAnimate(double nanos) override
SkString getName() const override
void onOnceBeforeDraw() override
SkISize getISize() override
Definition: gm.h:110
double duration() const
Definition: Skottie.h:268
void render(SkCanvas *canvas, const SkRect *dst=nullptr) const
Definition: Skottie.cpp:482
void seek(SkScalar t, sksg::InvalidationController *ic=nullptr)
Definition: Skottie.h:244
virtual sk_sp< SkTypeface > loadTypeface(const char[], const char[]) const
Definition: SkResources.h:206
double duration
Definition: examples.cpp:30
float SkScalar
Definition: extension.cpp:12
#define DEF_GM(CODE)
Definition: gm.h:40
sk_sp< Factory > BestAvailable()
sk_sp< SkTypeface > CreateTypefaceFromResource(const char *resource, int ttcIndex)
sk_sp< SkFontMgr > TestFontMgr()
DlVertices::Builder Builder
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
DrawResult
Definition: gm.h:104
dest
Definition: zip.py:79
static SkScalar prop(SkScalar radius, SkScalar newSize, SkScalar oldSize)
Definition: rrect.cpp:83
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20
constexpr SkRect makeOffset(float dx, float dy) const
Definition: SkRect.h:965
static constexpr SkRect MakeWH(float w, float h)
Definition: SkRect.h:609