From 9e340b73de0f10bec26a5421ba04c804f5688415 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Wed, 5 Sep 2018 14:10:19 -0700 Subject: [PATCH 1/3] add option to invert paint colors to be used for smart invert --- lib/ui/painting.dart | 20 +++++++++++++++++++- lib/ui/painting/paint.cc | 8 ++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/ui/painting.dart b/lib/ui/painting.dart index e2fe0521a6ac7..d562f46e35ecd 100644 --- a/lib/ui/painting.dart +++ b/lib/ui/painting.dart @@ -1065,6 +1065,7 @@ class Paint { static const int _kMaskFilterIndex = 12; static const int _kMaskFilterBlurStyleIndex = 13; static const int _kMaskFilterSigmaIndex = 14; + static const int _kInvertColorIndex = 15; static const int _kIsAntiAliasOffset = _kIsAntiAliasIndex << 2; static const int _kColorOffset = _kColorIndex << 2; @@ -1081,6 +1082,7 @@ class Paint { static const int _kMaskFilterOffset = _kMaskFilterIndex << 2; static const int _kMaskFilterBlurStyleOffset = _kMaskFilterBlurStyleIndex << 2; static const int _kMaskFilterSigmaOffset = _kMaskFilterSigmaIndex << 2; + static const int _kInvertColorOffset = _kInvertColorIndex << 2; // If you add more fields, remember to update _kDataByteCount. static const int _kDataByteCount = 75; @@ -1363,6 +1365,18 @@ class Paint { } } + /// Whether the colors of the image are inverted when drawn. + /// + /// inverting the colors of an image applies a new color filter that will + /// override any user provided color filters. This is primarily intended to + /// be used for implementing smart invert on iOS. + bool get invertColors { + return _data.getInt32(_kInvertColorOffset, _kFakeHostEndian) == 1; + } + set invertColors(bool value) { + _data.setInt32(_kInvertColorOffset, value ? 1 : 0, _kFakeHostEndian); + } + @override String toString() { final StringBuffer result = new StringBuffer(); @@ -1411,8 +1425,12 @@ class Paint { result.write('${semicolon}filterQuality: $filterQuality'); semicolon = '; '; } - if (shader != null) + if (shader != null) { result.write('${semicolon}shader: $shader'); + semicolon = '; '; + } + if (invertColors) + result.write('${semicolon}invert: $invertColors'); result.write(')'); return result.toString(); } diff --git a/lib/ui/painting/paint.cc b/lib/ui/painting/paint.cc index 00631451549ee..233623aa9647c 100644 --- a/lib/ui/painting/paint.cc +++ b/lib/ui/painting/paint.cc @@ -30,6 +30,7 @@ constexpr int kColorFilterBlendModeIndex = 11; constexpr int kMaskFilterIndex = 12; constexpr int kMaskFilterBlurStyleIndex = 13; constexpr int kMaskFilterSigmaIndex = 14; +constexpr int kInvertColorIndex = 15; constexpr size_t kDataByteCount = 75; // 4 * (last index + 1) // Indices for objects. @@ -133,6 +134,13 @@ Paint::Paint(Dart_Handle paint_objects, Dart_Handle paint_data) { paint_.setMaskFilter(SkMaskFilter::MakeBlur(blur_style, sigma)); break; } + + if (uint_data[kInvertColorIndex]) { + SkScalar colorMatrix[20] = {-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}; + paint_.setColorFilter( + SkColorFilter::MakeMatrixFilterRowMajor255(colorMatrix)); + } } } // namespace blink From 1b374e7ceb81cae96537f01146326ffc2a8f3311 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Thu, 6 Sep 2018 13:00:50 -0700 Subject: [PATCH 2/3] merge color filters if present --- lib/ui/painting.dart | 4 ++-- lib/ui/painting/paint.cc | 26 ++++++++++++++++++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/lib/ui/painting.dart b/lib/ui/painting.dart index d562f46e35ecd..af08b848cf910 100644 --- a/lib/ui/painting.dart +++ b/lib/ui/painting.dart @@ -1368,8 +1368,8 @@ class Paint { /// Whether the colors of the image are inverted when drawn. /// /// inverting the colors of an image applies a new color filter that will - /// override any user provided color filters. This is primarily intended to - /// be used for implementing smart invert on iOS. + /// be composed with any user provided color filters. This is primarily + /// used for implementing smart invert on iOS. bool get invertColors { return _data.getInt32(_kInvertColorOffset, _kFakeHostEndian) == 1; } diff --git a/lib/ui/painting/paint.cc b/lib/ui/painting/paint.cc index 233623aa9647c..759d26c5db1a8 100644 --- a/lib/ui/painting/paint.cc +++ b/lib/ui/painting/paint.cc @@ -48,6 +48,11 @@ constexpr uint32_t kBlendModeDefault = // default SkPaintDefaults_MiterLimit in Skia (which is not in a public header). constexpr double kStrokeMiterLimitDefault = 4.0; +// A color matrix which inverts colors. +constexpr SkScalar invert_colors[20] = {-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}; + // Must be kept in sync with the MaskFilter private constants in painting.dart. enum MaskFilterType { Null, Blur }; @@ -117,7 +122,19 @@ Paint::Paint(Dart_Handle paint_objects, Dart_Handle paint_data) { if (filter_quality) paint_.setFilterQuality(static_cast(filter_quality)); - if (uint_data[kColorFilterIndex]) { + if (uint_data[kColorFilterIndex] && uint_data[kInvertColorIndex]) { + SkColor color = uint_data[kColorFilterColorIndex]; + SkBlendMode blend_mode = + static_cast(uint_data[kColorFilterBlendModeIndex]); + sk_sp color_filter = + SkColorFilter::MakeModeFilter(color, blend_mode); + sk_sp invert_filter = + SkColorFilter::MakeMatrixFilterRowMajor255(invert_colors); + paint_.setColorFilter(invert_filter->makeComposed(color_filter)); + } else if (uint_data[kInvertColorIndex]) { + paint_.setColorFilter( + SkColorFilter::MakeMatrixFilterRowMajor255(invert_colors)); + } else if (uint_data[kColorFilterIndex]) { SkColor color = uint_data[kColorFilterColorIndex]; SkBlendMode blend_mode = static_cast(uint_data[kColorFilterBlendModeIndex]); @@ -134,13 +151,6 @@ Paint::Paint(Dart_Handle paint_objects, Dart_Handle paint_data) { paint_.setMaskFilter(SkMaskFilter::MakeBlur(blur_style, sigma)); break; } - - if (uint_data[kInvertColorIndex]) { - SkScalar colorMatrix[20] = {-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}; - paint_.setColorFilter( - SkColorFilter::MakeMatrixFilterRowMajor255(colorMatrix)); - } } } // namespace blink From 5263e0f4e3ddcd1d90842e5dbd98d90f58c395f6 Mon Sep 17 00:00:00 2001 From: jonahwilliams Date: Thu, 6 Sep 2018 13:35:02 -0700 Subject: [PATCH 3/3] nicely format matrix --- lib/ui/painting/paint.cc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/ui/painting/paint.cc b/lib/ui/painting/paint.cc index 759d26c5db1a8..9b876542a61a7 100644 --- a/lib/ui/painting/paint.cc +++ b/lib/ui/painting/paint.cc @@ -49,9 +49,14 @@ constexpr uint32_t kBlendModeDefault = constexpr double kStrokeMiterLimitDefault = 4.0; // A color matrix which inverts colors. -constexpr SkScalar invert_colors[20] = {-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 off +constexpr SkScalar invert_colors[20] = { + -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 // Must be kept in sync with the MaskFilter private constants in painting.dart. enum MaskFilterType { Null, Blur };