5#include "flutter/impeller/aiks/aiks_unittests.h"
7#include "flutter/testing/testing.h"
72 canvas.
DrawPaint({.color = Color::Color::OrangeRed().WithAlpha(0.5),
80 .mask_blur_descriptor =
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),
135 EXPECT_EQ(GetContext()->GetCapabilities()->GetDefaultColorFormat(),
137 auto texture = CreateTextureForFixture(
"airplane.jpg",
141 canvas.
Scale(GetContentScale());
158 EXPECT_EQ(GetContext()->GetCapabilities()->GetDefaultColorFormat(),
160 auto texture = CreateTextureForFixture(
"airplane.jpg",
164 canvas.
Scale(GetContentScale());
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);
219 return std::max(0.0f, std::min(1.0f, 2 - std::abs(2 - cycle)));
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);
241 -distance * std::cos(r.
radians));
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);
288 draw_color_wheel(canvas);
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;
313 canvas.
Scale(content_scale);
326 paint.color = color0;
328 paint.color = color1;
330 paint.color = color2;
338 ASSERT_TRUE(OpenPlaygroundHere(
callback));
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());
546 {.color = foreground,
547 .blend_mode =
static_cast<BlendMode>(current_blend_index)});
550 ASSERT_TRUE(OpenPlaygroundHere(
callback));
554 auto texture = CreateTextureForFixture(
"airplane.jpg",
567 auto texture = CreateTextureForFixture(
"airplane.jpg",
#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)
void DrawImageRect(const std::shared_ptr< Image > &image, Rect source, Rect dest, const Paint &paint, SamplerDescriptor sampler={}, SourceRectConstraint src_rect_constraint=SourceRectConstraint::kFast)
virtual void SaveLayer(const Paint &paint, std::optional< Rect > bounds=std::nullopt, const std::shared_ptr< ImageFilter > &backdrop_filter=nullptr, ContentBoundsPromise bounds_promise=ContentBoundsPromise::kUnknown, uint32_t total_content_depth=kMaxDepth)
void Transform(const Matrix &transform)
void DrawImage(const std::shared_ptr< Image > &image, Point offset, const Paint &paint, SamplerDescriptor sampler={})
void Rotate(Radians radians)
void DrawPaint(const Paint &paint)
void ClipRect(const Rect &rect, Entity::ClipOperation clip_op=Entity::ClipOperation::kIntersect)
void Scale(const Vector2 &scale)
Picture EndRecordingAsPicture()
virtual void Save(uint32_t total_content_depth=kMaxDepth)
void DrawRect(const Rect &rect, const Paint &paint)
void Translate(const Vector3 &offset)
void DrawCircle(const Point ¢er, Scalar radius, const Paint &paint)
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 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()
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)
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
std::optional< Snapshot > Snapshot(AiksContext &context)
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