5#include "flutter/impeller/aiks/aiks_unittests.h"
7#include "flutter/testing/testing.h"
25 canvas.ClipRect(layer_rect);
40 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
48 canvas.DrawPaint(
paint);
51 canvas.SaveLayer(
paint);
57 canvas.SaveLayer(
paint);
65 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
70 canvas.Scale(
Vector2(0.2, 0.2));
72 canvas.DrawPaint({.color = Color::Color::OrangeRed().WithAlpha(0.5),
74 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
80 .mask_blur_descriptor =
89 canvas.DrawCircle({300, 300}, 200,
filtered);
91 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
95 std::vector<Color>
colors = {
Color{0.9568, 0.2627, 0.2118, 1.0},
96 Color{0.1294, 0.5882, 0.9529, 1.0}};
97 std::vector<Scalar> stops = {0.0, 1.0};
101 {0, 0}, {100, 100}, std::move(
colors), std::move(stops),
110 canvas.DrawCircle({100, 100}, 100,
paint);
111 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
119 canvas.DrawCircle(
Point(150, 200), 100,
paint);
121 canvas.DrawCircle(
Point(250, 200), 100,
paint);
125 canvas.DrawCircle(
Point(450, 250), 100,
paint);
127 canvas.DrawCircle(
Point(550, 250), 100,
paint);
129 canvas.DrawCircle(
Point(500, 150), 100,
paint);
130 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
135 EXPECT_EQ(
GetContext()->GetCapabilities()->GetDefaultColorFormat(),
137 auto texture = CreateTextureForFixture(
"airplane.jpg",
141 canvas.Scale(GetContentScale());
142 canvas.DrawPaint({.color =
Color(0.9, 1.0, 0.9, 1.0)});
143 canvas.SaveLayer({});
149 canvas.DrawImageRect(
153 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
158 EXPECT_EQ(
GetContext()->GetCapabilities()->GetDefaultColorFormat(),
160 auto texture = CreateTextureForFixture(
"airplane.jpg",
164 canvas.Scale(GetContentScale());
165 canvas.DrawPaint({.color =
Color(0.1, 0.2, 0.1, 1.0)});
174 canvas.DrawImageRect(
178 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
181#define BLEND_MODE_TUPLE(blend_mode) {#blend_mode, BlendMode::k##blend_mode},
189 std::vector<const char*> blend_mode_names;
190 std::vector<BlendMode> blend_mode_values;
192 const std::vector<std::tuple<const char*, BlendMode>> blends = {
194 assert(blends.size() ==
196 for (
const auto& [
name,
mode] : blends) {
197 blend_mode_names.push_back(
name);
198 blend_mode_values.push_back(
mode);
202 return {blend_mode_names, blend_mode_values};
210 auto draw_color_wheel = [](
Canvas& canvas) {
213 auto color_wheel_sampler = [](
Radians r) {
217 auto color_cycle = [](
Scalar x) {
218 Scalar cycle = std::fmod(
x, 6.0f);
221 return Color(color_cycle(6 *
x + 1),
222 color_cycle(6 *
x - 1),
223 color_cycle(6 *
x - 3),
232 const int max_dist = 900;
233 for (
int i = 0;
i <= 900;
i++) {
236 Scalar normalized_distance =
static_cast<Scalar>(
i) / max_dist;
239 color_wheel_sampler(r).WithAlpha(1.0f - normalized_distance);
243 canvas.DrawCircle(position, 9 + normalized_distance * 3,
paint);
247 std::shared_ptr<Image> color_wheel_image;
248 Matrix color_wheel_transform;
252 static bool cache_the_wheel =
true;
253 static int current_blend_index = 3;
254 static float dst_alpha = 1;
255 static float src_alpha = 1;
261 ImGuiWindowFlags_AlwaysAutoResize)) {
262 ImGui::Checkbox(
"Cache the wheel", &cache_the_wheel);
263 ImGui::ListBox(
"Blending mode", ¤t_blend_index,
266 ImGui::SliderFloat(
"Source alpha", &src_alpha, 0, 1);
267 ImGui::ColorEdit4(
"Color A",
reinterpret_cast<float*
>(&color0));
268 ImGui::ColorEdit4(
"Color B",
reinterpret_cast<float*
>(&color1));
269 ImGui::ColorEdit4(
"Color C",
reinterpret_cast<float*
>(&color2));
270 ImGui::SliderFloat(
"Destination alpha", &dst_alpha, 0, 1);
274 static Point content_scale;
275 Point new_content_scale = GetContentScale();
277 if (!cache_the_wheel || new_content_scale != content_scale) {
278 content_scale = new_content_scale;
283 canvas.Scale(content_scale);
285 canvas.Translate(
Vector2(500, 400));
288 draw_color_wheel(canvas);
289 auto color_wheel_picture = canvas.EndRecordingAsPicture();
290 auto snapshot = color_wheel_picture.Snapshot(
renderer);
291 if (!snapshot.has_value() || !snapshot->texture) {
294 color_wheel_image = std::make_shared<Image>(snapshot->texture);
295 color_wheel_transform = snapshot->transform;
307 canvas.Transform(color_wheel_transform);
308 canvas.DrawImage(color_wheel_image,
Point(),
Paint());
313 canvas.Scale(content_scale);
314 canvas.Translate(
Vector2(500, 400));
326 paint.color = color0;
328 paint.color = color1;
329 canvas.DrawCircle(
Point(0, -1) * 45, 65,
paint);
330 paint.color = color2;
335 return canvas.EndRecordingAsPicture();
338 ASSERT_TRUE(OpenPlaygroundHere(
callback));
349 canvas.Translate({500, 300, 0});
353 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
365 canvas.DrawCircle(
Point::MakeXY(300.0, 300.0), 200.0, clear);
370 const std::shared_ptr<Image>& src_image,
371 const std::shared_ptr<Image>& dst_image,
374 ImGuiWindowFlags_AlwaysAutoResize)) {
375 ImGui::SliderFloat(
"Source alpha", &src_alpha, 0, 1);
395 for (
const auto&
color : source_colors) {
402 canvas.SaveLayer({});
404 canvas.DrawPaint({.color = destination_color});
407 canvas.SaveLayer({.blend_mode = blend_mode});
416 canvas.Translate(
Vector2(100, 0));
418 canvas.RestoreToCount(0);
425 canvas.Translate({0, 100});
428 canvas.SaveLayer({});
429 for (
const auto&
color : source_colors) {
432 {.color = destination_color.
Blend(
color, blend_mode),
434 canvas.Translate(
Vector2(100, 0));
446 canvas.Translate({0, 250});
452 for (
int y = 0;
y < 400 / 8;
y++) {
453 for (
int x = 0;
x < 800 / 16;
x++) {
463 canvas.DrawImage(dst_image, {0, 0},
467 canvas.DrawImage(src_image, {0, 0},
470 .blend_mode = blend_mode,
480 canvas.DrawImage(dst_image, {400, 0},
483 .blend_mode = blend_mode});
485 canvas.DrawImage(src_image, {400, 0},
493 return canvas.EndRecordingAsPicture();
496#define BLEND_MODE_TEST(blend_mode) \
497 TEST_P(AiksTest, BlendMode##blend_mode) { \
498 auto src_image = std::make_shared<Image>( \
499 CreateTextureForFixture("blend_mode_src.png")); \
500 auto dst_image = std::make_shared<Image>( \
501 CreateTextureForFixture("blend_mode_dst.png")); \
502 auto callback = [&](AiksContext& renderer) -> std::optional<Picture> { \
503 return BlendModeTest(GetContentScale(), BlendMode::k##blend_mode, \
504 src_image, dst_image,
1.0); \
506 OpenPlaygroundHere(callback); \
510#define BLEND_MODE_SRC_ALPHA_TEST(blend_mode) \
511 TEST_P(AiksTest, BlendModeSrcAlpha##blend_mode) { \
512 auto src_image = std::make_shared<Image>( \
513 CreateTextureForFixture("blend_mode_src.png")); \
514 auto dst_image = std::make_shared<Image>( \
515 CreateTextureForFixture("blend_mode_dst.png")); \
516 auto callback = [&](AiksContext& renderer) -> std::optional<Picture> { \
517 return BlendModeTest(GetContentScale(), BlendMode::k##blend_mode, \
518 src_image, dst_image,
0.5); \
520 OpenPlaygroundHere(callback); \
529 static Color foreground = Color::Color::OrangeRed().
WithAlpha(0.5);
530 static int current_blend_index = 3;
533 ImGuiWindowFlags_AlwaysAutoResize)) {
534 ImGui::ColorEdit4(
"Background",
reinterpret_cast<float*
>(&background));
535 ImGui::ColorEdit4(
"Foreground",
reinterpret_cast<float*
>(&foreground));
536 ImGui::ListBox(
"Blend mode", ¤t_blend_index,
537 modes.blend_mode_names.data(),
538 modes.blend_mode_names.size());
543 canvas.Scale(
Vector2(0.2, 0.2));
544 canvas.DrawPaint({.color = background});
546 {.color = foreground,
547 .blend_mode =
static_cast<BlendMode>(current_blend_index)});
548 return canvas.EndRecordingAsPicture();
550 ASSERT_TRUE(OpenPlaygroundHere(
callback));
554 auto texture = CreateTextureForFixture(
"airplane.jpg",
559 canvas.DrawImage(std::make_shared<Image>(
texture), {200, 200},
563 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
567 auto texture = CreateTextureForFixture(
"airplane.jpg",
572 canvas.DrawImage(std::make_shared<Image>(
texture), {200, 200},
576 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
580 auto texture = CreateTextureForFixture(
"airplane.jpg",
587 canvas.Scale(
Vector2(0.4, 0.4));
588 canvas.DrawImage(std::make_shared<Image>(
texture), {20, 20},
591 ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture()));
#define BLEND_MODE_TUPLE(blend_mode)
#define BLEND_MODE_SRC_ALPHA_TEST(blend_mode)
#define BLEND_MODE_TEST(blend_mode)
static bool ImGuiBegin(const char *name, bool *p_open, ImGuiWindowFlags flags)
static std::shared_ptr< ColorFilter > MakeBlend(BlendMode blend_mode, Color color)
static ColorSource MakeLinearGradient(Point start_point, Point end_point, std::vector< Color > colors, std::vector< Scalar > stops, Entity::TileMode tile_mode, Matrix effect_transform)
static constexpr BlendMode kLastAdvancedBlendMode
@ kNormal
Blurred inside and outside.
#define IMPELLER_FOR_EACH_BLEND_MODE(V)
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
static float max(float r, float g, float b)
static float min(float r, float g, float b)
SK_API GrDirectContext * GetContext(const SkImage *src)
PODArray< SkColor > colors
SK_API sk_sp< SkShader > Color(SkColor)
DEF_SWITCHES_START aot vmservice shared library name
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 mode
static Picture BlendModeTest(Vector2 content_scale, BlendMode blend_mode, const std::shared_ptr< Image > &src_image, const std::shared_ptr< Image > &dst_image, Scalar src_alpha)
TEST_P(AiksTest, CanRenderAdvancedBlendColorFilterWithSaveLayer)
static BlendModeSelection GetBlendModeSelection()
SIN Vec< N, float > abs(const Vec< N, float > &x)
def filtered(names, to_skip)
static constexpr Color LimeGreen()
static constexpr Color Black()
static constexpr Color CornflowerBlue()
static constexpr Color MediumTurquoise()
static constexpr Color White()
constexpr Color WithAlpha(Scalar new_alpha) const
static constexpr Color Orange()
static constexpr Color Red()
static constexpr Color MakeRGBA8(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
static constexpr Color DarkGray()
Color Blend(Color source, BlendMode blend_mode) const
Blends an unpremultiplied destination color into a given unpremultiplied source color to form a new u...
static constexpr Color Blue()
static constexpr Color Green()
A 4x4 matrix using column-major storage.
static constexpr Matrix MakeScale(const Vector3 &s)
FilterContents::BlurStyle style
In filters that use Gaussian distributions, "sigma" is a size of one standard deviation in terms of t...
static constexpr TPoint< Type > MakeXY(Type x, Type y)
static constexpr TRect MakeXYWH(Type x, Type y, Type width, Type height)
static constexpr TRect MakeSize(const TSize< U > &size)
constexpr TRect< T > Expand(T left, T top, T right, T bottom) const
Returns a rectangle with expanded edges. Negative expansion results in shrinking.
static constexpr TRect MakeLTRB(Type left, Type top, Type right, Type bottom)
std::vector< BlendMode > blend_mode_values
std::vector< const char * > blend_mode_names