| 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 #include "include/core/SkPath.h"
15 #include "include/core/SkRRect.h"
16
17 namespace impeller {
18 namespace testing {
19
20 using namespace flutter;
21
22 namespace {
23 std::shared_ptr<DlColorSource> MakeRuntimeEffect(
24 AiksTest* test,
25 std::string_view name,
26 const std::shared_ptr<std::vector<uint8_t>>& uniform_data = {},
27 const std::vector<std::shared_ptr<DlColorSource>>& samplers = {}) {
28 auto runtime_stages = test->OpenAssetAsRuntimeStage(name.data());
29 auto runtime_stage = runtime_stages[PlaygroundBackendToRuntimeStageBackend(
30 test->GetBackend())];
31 FML_CHECK(runtime_stage);
32 FML_CHECK(runtime_stage->IsDirty());
33
34 auto dl_runtime_effect = DlRuntimeEffect::MakeImpeller(runtime_stage);
35
36 return DlColorSource::MakeRuntimeEffect(dl_runtime_effect, samplers,
37 uniform_data);
38 }
39 } // namespace
40
41 // Regression test for https://github.com/flutter/flutter/issues/126701 .
42 TEST_P(AiksTest, CanRenderClippedRuntimeEffects) {
43 struct FragUniforms {
44 Vector2 iResolution;
45 Scalar iTime;
46 } frag_uniforms = {.iResolution = Vector2(400, 400), .iTime = 100.0};
47 auto uniform_data = std::make_shared<std::vector<uint8_t>>();
48 uniform_data->resize(sizeof(FragUniforms));
49 memcpy(uniform_data->data(), &frag_uniforms, sizeof(FragUniforms));
50
51 DlPaint paint;
52 paint.setColorSource(
53 MakeRuntimeEffect(this, "runtime_stage_example.frag.iplr", uniform_data));
54
55 DisplayListBuilder builder;
56 builder.Save();
57 builder.ClipRRect(
58 SkRRect::MakeRectXY(SkRect::MakeXYWH(0, 0, 400, 400), 10.0, 10.0),
59 DlCanvas::ClipOp::kIntersect);
60 builder.DrawRect(SkRect::MakeXYWH(0, 0, 400, 400), paint);
61 builder.Restore();
62
63 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
64 }
65
66 TEST_P(AiksTest, DrawPaintTransformsBounds) {
67 struct FragUniforms {
68 Size size;
69 } frag_uniforms = {.size = Size::MakeWH(400, 400)};
70 auto uniform_data = std::make_shared<std::vector<uint8_t>>();
71 uniform_data->resize(sizeof(FragUniforms));
72 memcpy(uniform_data->data(), &frag_uniforms, sizeof(FragUniforms));
73
74 DlPaint paint;
75 paint.setColorSource(
76 MakeRuntimeEffect(this, "gradient.frag.iplr", uniform_data));
77
78 DisplayListBuilder builder;
79 builder.Save();
80 builder.Scale(GetContentScale().x, GetContentScale().y);
81 builder.DrawPaint(paint);
82 builder.Restore();
83
84 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
85 }
86
87 TEST_P(AiksTest, CanRenderRuntimeEffectFilter) {
88 auto runtime_stages =
89 OpenAssetAsRuntimeStage("runtime_stage_filter_example.frag.iplr");
90
91 std::shared_ptr<RuntimeStage> runtime_stage =
92 runtime_stages[PlaygroundBackendToRuntimeStageBackend(GetBackend())];
93 ASSERT_TRUE(runtime_stage);
94 ASSERT_TRUE(runtime_stage->IsDirty());
95
96 std::vector<std::shared_ptr<DlColorSource>> sampler_inputs = {
97 nullptr,
98 };
99 auto uniform_data = std::make_shared<std::vector<uint8_t>>();
100 uniform_data->resize(sizeof(Vector2));
101
102 DlPaint paint;
103 paint.setColor(DlColor::kAqua());
104 paint.setImageFilter(DlImageFilter::MakeRuntimeEffect(
105 DlRuntimeEffect::MakeImpeller(runtime_stage), sampler_inputs,
106 uniform_data));
107
108 DisplayListBuilder builder;
109 builder.DrawRect(SkRect::MakeXYWH(0, 0, 400, 400), paint);
110
111 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
112 }
113
114 } // namespace testing
115 } // namespace impeller
impeller::AiksPlayground
Definition: aiks_playground.h:16
impeller::Scalar
float Scalar
Definition: scalar.h:18
aiks_unittests.h
impeller::testing::AiksTest
AiksPlayground AiksTest
Definition: aiks_unittests.h:17
impeller::Vector2
Point Vector2
Definition: point.h:331
x
int32_t x
Definition: dl_golden_unittests.cc:443
impeller::PlaygroundBackendToRuntimeStageBackend
constexpr RuntimeStageBackend PlaygroundBackendToRuntimeStageBackend(PlaygroundBackend backend)
Definition: playground.h:33
impeller::TSize< Scalar >
flutter
Definition: dl_golden_blur_unittests.cc:16
impeller::testing::TEST_P
TEST_P(AiksTest, DrawAtlasNoColor)
Definition: aiks_dl_atlas_unittests.cc:57
impeller::TPoint< Scalar >
impeller::TSize< Scalar >::MakeWH
static constexpr TSize MakeWH(Type width, Type height)
Definition: size.h:43
impeller
Definition: allocation.cc:12