From 99d159a6da04f39ae924fe3a9464c8577198d978 Mon Sep 17 00:00:00 2001 From: Jim Graham Date: Mon, 14 Feb 2022 14:06:00 -0800 Subject: [PATCH] move de-Skia-ifying code to a DlColorFilter static factory, + formatting --- ci/licenses_golden/licenses_flutter | 2 + display_list/BUILD.gn | 2 + display_list/display_list_builder.cc | 30 +------------ display_list/display_list_builder.h | 1 - display_list/display_list_color_filter.cc | 53 +++++++++++++++++------ display_list/display_list_color_filter.h | 22 ++++------ display_list/display_list_utils.cc | 3 +- lib/ui/painting/color_filter.h | 2 +- lib/ui/painting/paint.cc | 3 +- 9 files changed, 57 insertions(+), 61 deletions(-) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 43bcb9aa830c0..637a09ab70ab3 100755 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -52,6 +52,8 @@ FILE: ../../../flutter/display_list/display_list_canvas_dispatcher.h FILE: ../../../flutter/display_list/display_list_canvas_recorder.cc FILE: ../../../flutter/display_list/display_list_canvas_recorder.h FILE: ../../../flutter/display_list/display_list_canvas_unittests.cc +FILE: ../../../flutter/display_list/display_list_color_filter.cc +FILE: ../../../flutter/display_list/display_list_color_filter.h FILE: ../../../flutter/display_list/display_list_complexity.cc FILE: ../../../flutter/display_list/display_list_complexity.h FILE: ../../../flutter/display_list/display_list_dispatcher.cc diff --git a/display_list/BUILD.gn b/display_list/BUILD.gn index 44482cb983018..b88e229e3ccac 100644 --- a/display_list/BUILD.gn +++ b/display_list/BUILD.gn @@ -14,6 +14,8 @@ source_set("display_list") { "display_list_canvas_dispatcher.h", "display_list_canvas_recorder.cc", "display_list_canvas_recorder.h", + "display_list_color_filter.cc", + "display_list_color_filter.h", "display_list_complexity.cc", "display_list_complexity.h", "display_list_dispatcher.cc", diff --git a/display_list/display_list_builder.cc b/display_list/display_list_builder.cc index a7b515198e254..3a06c6efe02f1 100644 --- a/display_list/display_list_builder.cc +++ b/display_list/display_list_builder.cc @@ -135,33 +135,6 @@ void DisplayListBuilder::onSetImageFilter(sk_sp filter) { ? Push(0, 0, std::move(filter)) : Push(0, 0); } -void DisplayListBuilder::setColorFilter(sk_sp filter) { - if (filter == nullptr) { - if (current_color_filter_->type() != DlColorFilter::kNone) { - onSetColorFilter(&DlNoColorFilter::instance); - } - return; - } - { - SkColor color; - SkBlendMode mode; - if (filter->asAColorMode(&color, &mode)) { - DlBlendColorFilter blend_filter(color, mode); - onSetColorFilter(&blend_filter); - return; - } - } - { - float matrix[20]; - if (filter->asAColorMatrix(matrix)) { - DlMatrixColorFilter matrix_filter(matrix); - onSetColorFilter(&matrix_filter); - return; - } - } - DlUnknownColorFilter unknown_filter(std::move(filter)); - onSetColorFilter(&unknown_filter); -} void DisplayListBuilder::onSetColorFilter(const DlColorFilter* filter) { if (filter == nullptr) { filter = &DlNoColorFilter::instance; @@ -278,7 +251,8 @@ void DisplayListBuilder::setAttributesFromPaint( // we must clear it because it is a second potential color filter // that is composed with the paint's color filter. setInvertColors(false); - setColorFilter(sk_ref_sp(paint.getColorFilter())); + DlColorFilter filter = DlColorFilter::From(paint.getColorFilter()); + setColorFilter(&filter); } if (flags.applies_image_filter()) { setImageFilter(sk_ref_sp(paint.getImageFilter())); diff --git a/display_list/display_list_builder.h b/display_list/display_list_builder.h index 4fb9a8beca652..8cb9bfbaf461b 100644 --- a/display_list/display_list_builder.h +++ b/display_list/display_list_builder.h @@ -392,7 +392,6 @@ class DisplayListBuilder final : public virtual Dispatcher, void onSetBlender(sk_sp blender); void onSetShader(sk_sp shader); void onSetImageFilter(sk_sp filter); - void setColorFilter(sk_sp filter); void onSetColorFilter(const DlColorFilter* filter); void onSetPathEffect(sk_sp effect); void onSetMaskFilter(sk_sp filter); diff --git a/display_list/display_list_color_filter.cc b/display_list/display_list_color_filter.cc index 9bbd1380ce18c..a4932ea3f0d77 100644 --- a/display_list/display_list_color_filter.cc +++ b/display_list/display_list_color_filter.cc @@ -6,6 +6,26 @@ namespace flutter { +DlColorFilter DlColorFilter::From(SkColorFilter* sk_filter) { + if (sk_filter == nullptr) { + return DlNoColorFilter::instance; + } + { + SkColor color; + SkBlendMode mode; + if (sk_filter->asAColorMode(&color, &mode)) { + return DlBlendColorFilter(color, mode); + } + } + { + float matrix[20]; + if (sk_filter->asAColorMatrix(matrix)) { + return DlMatrixColorFilter(matrix); + } + } + return DlUnknownColorFilter(sk_ref_sp(sk_filter)); +} + DlColorFilter::~DlColorFilter() { if (type_ == kUnknown) { delete static_cast(this); @@ -53,19 +73,16 @@ bool DlColorFilter::Equals(const DlColorFilter* a, const DlColorFilter* b) { return true; case kBlend: return static_cast(a)->equals( - static_cast(b) - ); + static_cast(b)); case kMatrix: return static_cast(a)->equals( - static_cast(b) - ); + static_cast(b)); case kSrgbToLinearGamma: case kLinearToSrgbGamma: return true; case kUnknown: return static_cast(a)->equals( - static_cast(b) - ); + static_cast(b)); } } @@ -78,9 +95,11 @@ sk_sp DlColorFilter::sk_filter() const { case kMatrix: return static_cast(this)->sk_filter(); case kSrgbToLinearGamma: - return static_cast(this)->sk_filter(); + return static_cast(this) + ->sk_filter(); case kLinearToSrgbGamma: - return static_cast(this)->sk_filter(); + return static_cast(this) + ->sk_filter(); case kUnknown: return static_cast(this)->sk_filter(); } @@ -91,15 +110,20 @@ std::shared_ptr DlColorFilter::shared() const { case kNone: return std::make_shared(); case kBlend: - return std::make_shared(static_cast(this)); + return std::make_shared( + static_cast(this)); case kMatrix: - return std::make_shared(static_cast(this)); + return std::make_shared( + static_cast(this)); case kSrgbToLinearGamma: - return std::make_shared(static_cast(this)); + return std::make_shared( + static_cast(this)); case kLinearToSrgbGamma: - return std::make_shared(static_cast(this)); + return std::make_shared( + static_cast(this)); case kUnknown: - return std::make_shared(static_cast(this)); + return std::make_shared( + static_cast(this)); } } @@ -115,7 +139,8 @@ bool DlColorFilter::modifies_transparent_black() const { // Look at the matrix to make a faster determination? // Basically, are the translation components all 0? case kUnknown: - return sk_filter()->filterColor(SK_ColorTRANSPARENT) != SK_ColorTRANSPARENT; + return sk_filter()->filterColor(SK_ColorTRANSPARENT) != + SK_ColorTRANSPARENT; } } diff --git a/display_list/display_list_color_filter.h b/display_list/display_list_color_filter.h index c76a5271ed54d..e835195ad1a66 100644 --- a/display_list/display_list_color_filter.h +++ b/display_list/display_list_color_filter.h @@ -29,6 +29,8 @@ class DlColorFilter { Type type() const { return type_; } + static DlColorFilter From(SkColorFilter* sk_filter); + size_t size() const; bool equals(const DlColorFilter* other) const; static bool Equals(const DlColorFilter* a, const DlColorFilter* b); @@ -37,15 +39,13 @@ class DlColorFilter { bool modifies_transparent_black() const; const DlBlendColorFilter* asABlendFilter() const { - return type_ == kBlend - ? reinterpret_cast(this) - : nullptr; + return type_ == kBlend ? reinterpret_cast(this) + : nullptr; } const DlMatrixColorFilter* asAMatrixFilter() const { - return type_ == kMatrix - ? reinterpret_cast(this) - : nullptr; + return type_ == kMatrix ? reinterpret_cast(this) + : nullptr; } const DlSrgbToLinearGammaColorFilter* asASrgbToLinearFilter() const { @@ -88,8 +88,7 @@ class DlNoColorFilter final : public DlColorFilter { class DlBlendColorFilter final : public DlColorFilter { public: DlBlendColorFilter(SkColor color, SkBlendMode mode) - : DlColorFilter(kBlend), color_(color), mode_(mode) { - } + : DlColorFilter(kBlend), color_(color), mode_(mode) {} DlBlendColorFilter(const DlBlendColorFilter& filter) : DlBlendColorFilter(filter.color_, filter.mode_) {} DlBlendColorFilter(const DlBlendColorFilter* filter) @@ -114,8 +113,7 @@ class DlBlendColorFilter final : public DlColorFilter { class DlMatrixColorFilter final : public DlColorFilter { public: - DlMatrixColorFilter(const float matrix[20]) - : DlColorFilter(kMatrix) { + DlMatrixColorFilter(const float matrix[20]) : DlColorFilter(kMatrix) { memcpy(matrix_, &matrix, sizeof(matrix_)); } DlMatrixColorFilter(const DlMatrixColorFilter& filter) @@ -156,9 +154,7 @@ class DlSrgbToLinearGammaColorFilter final : public DlColorFilter { return true; } - sk_sp sk_filter() const { - return sk_filter_; - } + sk_sp sk_filter() const { return sk_filter_; } private: static const sk_sp sk_filter_; diff --git a/display_list/display_list_utils.cc b/display_list/display_list_utils.cc index 7b9a6305a7e38..676ff2f1ece4f 100644 --- a/display_list/display_list_utils.cc +++ b/display_list/display_list_utils.cc @@ -94,8 +94,7 @@ void SkPaintDispatchHelper::setMaskBlurFilter(SkBlurStyle style, paint_.setMaskFilter(SkMaskFilter::MakeBlur(style, sigma)); } -sk_sp SkPaintDispatchHelper::makeColorFilter() - const { +sk_sp SkPaintDispatchHelper::makeColorFilter() const { if (!invert_colors_) { return color_filter_ ? color_filter_->sk_filter() : nullptr; } diff --git a/lib/ui/painting/color_filter.h b/lib/ui/painting/color_filter.h index 2c5140d78ee85..706d3e5e074c2 100644 --- a/lib/ui/painting/color_filter.h +++ b/lib/ui/painting/color_filter.h @@ -5,10 +5,10 @@ #ifndef FLUTTER_LIB_UI_COLOR_FILTER_H_ #define FLUTTER_LIB_UI_COLOR_FILTER_H_ +#include "flutter/display_list/display_list_color_filter.h" #include "flutter/lib/ui/dart_wrapper.h" #include "third_party/skia/include/core/SkColorFilter.h" #include "third_party/tonic/typed_data/typed_list.h" -#include "flutter/display_list/display_list_color_filter.h" using tonic::DartPersistentValue; diff --git a/lib/ui/painting/paint.cc b/lib/ui/painting/paint.cc index 8fb945715a90a..32e5418f44b06 100644 --- a/lib/ui/painting/paint.cc +++ b/lib/ui/painting/paint.cc @@ -156,8 +156,7 @@ const SkPaint* Paint::paint(SkPaint& paint) const { } if (uint_data[kInvertColorIndex]) { - sk_sp invert_filter = - SkColorFilters::Matrix(invert_colors); + sk_sp invert_filter = SkColorFilters::Matrix(invert_colors); sk_sp current_filter = paint.refColorFilter(); if (current_filter) { invert_filter = invert_filter->makeComposed(current_filter);