From 9dd4c6e54b658f91860deace5a0bc46a35bc7a88 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Thu, 16 Feb 2023 14:53:48 -0800 Subject: [PATCH 1/2] [Impeller] implement invert colors flag --- impeller/aiks/paint.cc | 21 +++++++++++++++++++ impeller/aiks/paint.h | 5 ++++- .../display_list/display_list_dispatcher.cc | 2 +- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/impeller/aiks/paint.cc b/impeller/aiks/paint.cc index 5f35cc52301eb..60dec041d1e74 100644 --- a/impeller/aiks/paint.cc +++ b/impeller/aiks/paint.cc @@ -61,6 +61,7 @@ std::shared_ptr Paint::WithFilters( const Matrix& effect_transform) const { bool is_solid_color_val = is_solid_color.value_or(!color_source); input = WithColorFilter(input); + input = WithInvertFilter(input); input = WithMaskBlur(input, is_solid_color_val, effect_transform); input = WithImageFilter(input, effect_transform); return input; @@ -114,6 +115,26 @@ std::shared_ptr Paint::WithColorFilter( return input; } +/// A color matrix which inverts colors. +// clang-format off +constexpr Matrix kColorInversion = Matrix( + -1.0, 0, 0, 1.0, 0, // + 0, -1.0, 0, 1.0, 0, // + 0, 0, -1.0, 1.0, 0, // + 1.0, 1.0, 1.0, 1.0, 0, // +); +// clang-format on + +std::shared_ptr Paint::WithInvertFilter( + std::shared_ptr input) const { + if (!invert_colors) { + return input; + } + + return ColorFilterContents::MakeColorMatrix({std::move(input)}, + kColorInversion); +} + std::shared_ptr Paint::MaskBlurDescriptor::CreateMaskBlur( const FilterInput::Ref& input, bool is_solid_color, diff --git a/impeller/aiks/paint.h b/impeller/aiks/paint.h index f18aca1953c3e..07feec152a324 100644 --- a/impeller/aiks/paint.h +++ b/impeller/aiks/paint.h @@ -67,6 +67,7 @@ struct Paint { Scalar stroke_miter = 4.0; Style style = Style::kFill; BlendMode blend_mode = BlendMode::kSourceOver; + bool invert_colors = false; std::optional image_filter; std::optional color_filter; @@ -123,6 +124,8 @@ struct Paint { std::shared_ptr WithColorFilter(std::shared_ptr input, bool absorb_opacity = false) const; -}; + + std::shared_ptr Paint::WithInvertFilter( + std::shared_ptr input) const; } // namespace impeller diff --git a/impeller/display_list/display_list_dispatcher.cc b/impeller/display_list/display_list_dispatcher.cc index 4e7d7ffe69882..4dd2111f442d9 100644 --- a/impeller/display_list/display_list_dispatcher.cc +++ b/impeller/display_list/display_list_dispatcher.cc @@ -580,7 +580,7 @@ void DisplayListDispatcher::setColorFilter( // |flutter::Dispatcher| void DisplayListDispatcher::setInvertColors(bool invert) { - UNIMPLEMENTED; + paint_.invert_colors = invert; } // |flutter::Dispatcher| From 152287c0149be61f18e2f0e07561ce5d587abc2b Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Fri, 17 Feb 2023 15:20:10 -0800 Subject: [PATCH 2/2] [Impeller] add invert colors support --- impeller/aiks/aiks_unittests.cc | 10 ++++++++++ impeller/aiks/paint.cc | 18 ++++++++++-------- impeller/aiks/paint.h | 3 ++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/impeller/aiks/aiks_unittests.cc b/impeller/aiks/aiks_unittests.cc index d7bcab300b4df..84853ce685298 100644 --- a/impeller/aiks/aiks_unittests.cc +++ b/impeller/aiks/aiks_unittests.cc @@ -82,6 +82,16 @@ TEST_P(AiksTest, CanRenderImage) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } +TEST_P(AiksTest, CanRenderInvertedImage) { + Canvas canvas; + Paint paint; + auto image = std::make_shared(CreateTextureForFixture("kalimba.jpg")); + paint.color = Color::Red(); + paint.invert_colors = true; + canvas.DrawImage(image, Point::MakeXY(100.0, 100.0), paint); + ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); +} + bool GenerateMipmap(const std::shared_ptr& context, std::shared_ptr texture, std::string label) { diff --git a/impeller/aiks/paint.cc b/impeller/aiks/paint.cc index 60dec041d1e74..bf738951cfb0e 100644 --- a/impeller/aiks/paint.cc +++ b/impeller/aiks/paint.cc @@ -117,12 +117,14 @@ std::shared_ptr Paint::WithColorFilter( /// A color matrix which inverts colors. // clang-format off -constexpr Matrix kColorInversion = Matrix( - -1.0, 0, 0, 1.0, 0, // - 0, -1.0, 0, 1.0, 0, // - 0, 0, -1.0, 1.0, 0, // - 1.0, 1.0, 1.0, 1.0, 0, // -); +constexpr ColorFilterContents::ColorMatrix kColorInversion = { + .array = { + -1.0, 0, 0, 1.0, 0, // + 0, -1.0, 0, 1.0, 0, // + 0, 0, -1.0, 1.0, 0, // + 1.0, 1.0, 1.0, 1.0, 0 // + } +}; // clang-format on std::shared_ptr Paint::WithInvertFilter( @@ -131,8 +133,8 @@ std::shared_ptr Paint::WithInvertFilter( return input; } - return ColorFilterContents::MakeColorMatrix({std::move(input)}, - kColorInversion); + return ColorFilterContents::MakeColorMatrix( + {FilterInput::Make(std::move(input))}, kColorInversion); } std::shared_ptr Paint::MaskBlurDescriptor::CreateMaskBlur( diff --git a/impeller/aiks/paint.h b/impeller/aiks/paint.h index 07feec152a324..990f829fc4067 100644 --- a/impeller/aiks/paint.h +++ b/impeller/aiks/paint.h @@ -125,7 +125,8 @@ struct Paint { std::shared_ptr WithColorFilter(std::shared_ptr input, bool absorb_opacity = false) const; - std::shared_ptr Paint::WithInvertFilter( + std::shared_ptr WithInvertFilter( std::shared_ptr input) const; +}; } // namespace impeller