| Flutter Impeller |

aiks_dl_runtime_effect_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <memory>
6
7 #include "flutter/display_list/dl_builder.h"
8 #include "flutter/display_list/dl_paint.h"
9 #include "flutter/display_list/effects/dl_color_source.h"
10 #include "flutter/display_list/effects/dl_image_filter.h"
11 #include "flutter/display_list/effects/dl_runtime_effect.h"
12 #include "flutter/impeller/display_list/aiks_unittests.h"
13
14 namespace impeller {
15 namespace testing {
16
17 using namespace flutter;
18
19 namespace {
20 std::shared_ptr<DlColorSource> MakeRuntimeEffect(
21 AiksTest* test,
22 std::string_view name,
23 const std::shared_ptr<std::vector<uint8_t>>& uniform_data = {},
24 const std::vector<std::shared_ptr<DlColorSource>>& samplers = {}) {
25 auto runtime_stages = test->OpenAssetAsRuntimeStage(name.data());
26 auto runtime_stage = runtime_stages[PlaygroundBackendToRuntimeStageBackend(
27 test->GetBackend())];
28 FML_CHECK(runtime_stage);
29 FML_CHECK(runtime_stage->IsDirty());
30
31 auto dl_runtime_effect = DlRuntimeEffect::MakeImpeller(runtime_stage);
32
33 return DlColorSource::MakeRuntimeEffect(dl_runtime_effect, samplers,
34 uniform_data);
35 }
36 } // namespace
37
38 // Regression test for https://github.com/flutter/flutter/issues/126701 .
39 TEST_P(AiksTest, CanRenderClippedRuntimeEffects) {
40 struct FragUniforms {
41 Vector2 iResolution;
42 Scalar iTime;
43 } frag_uniforms = {.iResolution = Vector2(400, 400), .iTime = 100.0};
44 auto uniform_data = std::make_shared<std::vector<uint8_t>>();
45 uniform_data->resize(sizeof(FragUniforms));
46 memcpy(uniform_data->data(), &frag_uniforms, sizeof(FragUniforms));
47
48 DlPaint paint;
49 paint.setColorSource(
50 MakeRuntimeEffect(this, "runtime_stage_example.frag.iplr", uniform_data));
51
52 DisplayListBuilder builder;
53 builder.Save();
54 builder.ClipRoundRect(
55 DlRoundRect::MakeRectXY(DlRect::MakeXYWH(0, 0, 400, 400), 10.0, 10.0),
56 DlClipOp::kIntersect);
57 builder.DrawRect(DlRect::MakeXYWH(0, 0, 400, 400), paint);
58 builder.Restore();
59
60 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
61 }
62
63 TEST_P(AiksTest, DrawPaintTransformsBounds) {
64 struct FragUniforms {
65 Size size;
66 } frag_uniforms = {.size = Size::MakeWH(400, 400)};
67 auto uniform_data = std::make_shared<std::vector<uint8_t>>();
68 uniform_data->resize(sizeof(FragUniforms));
69 memcpy(uniform_data->data(), &frag_uniforms, sizeof(FragUniforms));
70
71 DlPaint paint;
72 paint.setColorSource(
73 MakeRuntimeEffect(this, "gradient.frag.iplr", uniform_data));
74
75 DisplayListBuilder builder;
76 builder.Save();
77 builder.Scale(GetContentScale().x, GetContentScale().y);
78 builder.DrawPaint(paint);
79 builder.Restore();
80
81 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
82 }
83
84 TEST_P(AiksTest, CanRenderRuntimeEffectFilter) {
85 auto runtime_stages =
86 OpenAssetAsRuntimeStage("runtime_stage_filter_example.frag.iplr");
87
88 std::shared_ptr<RuntimeStage> runtime_stage =
89 runtime_stages[PlaygroundBackendToRuntimeStageBackend(GetBackend())];
90 ASSERT_TRUE(runtime_stage);
91 ASSERT_TRUE(runtime_stage->IsDirty());
92
93 std::vector<std::shared_ptr<DlColorSource>> sampler_inputs = {
94 nullptr,
95 };
96 auto uniform_data = std::make_shared<std::vector<uint8_t>>();
97 uniform_data->resize(sizeof(Vector2));
98
99 DlPaint paint;
100 paint.setColor(DlColor::kAqua());
101 paint.setImageFilter(DlImageFilter::MakeRuntimeEffect(
102 DlRuntimeEffect::MakeImpeller(runtime_stage), sampler_inputs,
103 uniform_data));
104
105 DisplayListBuilder builder;
106 builder.DrawRect(DlRect::MakeXYWH(0, 0, 400, 400), paint);
107
108 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
109 }
110
111 TEST_P(AiksTest, RuntimeEffectWithInvalidSamplerDoesNotCrash) {
112 ScopedValidationDisable disable_validation;
113
114 // Create a sampler that is not usable as an input to the runtime effect.
115 std::vector<flutter::DlColor> colors = {flutter::DlColor::kBlue(),
116 flutter::DlColor::kRed()};
117 const float stops[2] = {0.0, 1.0};
118 auto linear = flutter::DlColorSource::MakeLinear({0.0, 0.0}, {300.0, 300.0},
119 2, colors.data(), stops,
120 flutter::DlTileMode::kClamp);
121 std::vector<std::shared_ptr<DlColorSource>> sampler_inputs = {
122 linear,
123 };
124
125 auto uniform_data = std::make_shared<std::vector<uint8_t>>();
126 uniform_data->resize(sizeof(Vector2));
127
128 DlPaint paint;
129 paint.setColorSource(
130 MakeRuntimeEffect(this, "runtime_stage_filter_example.frag.iplr",
131 uniform_data, sampler_inputs));
132
133 DisplayListBuilder builder;
134 builder.DrawPaint(paint);
135
136 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
137 }
138
139 } // namespace testing
140 } // namespace impeller
aiks_unittests.h
impeller::AiksPlayground
Definition: aiks_playground.h:16
x
int32_t x
Definition: dl_golden_unittests.cc:458
flutter
Definition: dl_golden_blur_unittests.cc:16
impeller::testing::AiksTest
AiksPlayground AiksTest
Definition: aiks_unittests.h:17
impeller::testing::TEST_P
TEST_P(AiksTest, DrawAtlasNoColor)
Definition: aiks_dl_atlas_unittests.cc:60
impeller
Definition: allocation.cc:12
impeller::Vector2
Point Vector2
Definition: point.h:331
impeller::Scalar
float Scalar
Definition: scalar.h:19
impeller::PlaygroundBackendToRuntimeStageBackend
constexpr RuntimeStageBackend PlaygroundBackendToRuntimeStageBackend(PlaygroundBackend backend)
Definition: playground.h:33
impeller::ScopedValidationDisable
Definition: validation.h:56
impeller::TPoint< Scalar >
impeller::TSize< Scalar >
impeller::TSize< Scalar >::MakeWH
static constexpr TSize MakeWH(Type width, Type height)
Definition: size.h:43