This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Refactor ColorFilter to have a native wrapper #9668
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ff4ed6b
Refactor ColorFilter to have a native wrapper
dnfield 4b820ed
comment
dnfield 8935830
tests
dnfield 1bfc97c
typo
dnfield c8a8c35
Merge remote-tracking branch 'upstream/master' into native_color_filter
dnfield 884f0e3
Use right accessor
dnfield File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "flutter/lib/ui/painting/color_filter.h" | ||
|
|
||
| #include "third_party/tonic/converter/dart_converter.h" | ||
| #include "third_party/tonic/dart_args.h" | ||
| #include "third_party/tonic/dart_binding_macros.h" | ||
| #include "third_party/tonic/dart_library_natives.h" | ||
|
|
||
| namespace flutter { | ||
|
|
||
| static void ColorFilter_constructor(Dart_NativeArguments args) { | ||
| DartCallConstructor(&ColorFilter::Create, args); | ||
| } | ||
|
|
||
| IMPLEMENT_WRAPPERTYPEINFO(ui, ColorFilter); | ||
|
|
||
| #define FOR_EACH_BINDING(V) \ | ||
| V(ColorFilter, initMode) \ | ||
| V(ColorFilter, initMatrix) \ | ||
| V(ColorFilter, initSrgbToLinearGamma) \ | ||
| V(ColorFilter, initLinearToSrgbGamma) | ||
|
|
||
| FOR_EACH_BINDING(DART_NATIVE_CALLBACK) | ||
|
|
||
| void ColorFilter::RegisterNatives(tonic::DartLibraryNatives* natives) { | ||
| natives->Register( | ||
| {{"ColorFilter_constructor", ColorFilter_constructor, 1, true}, | ||
| FOR_EACH_BINDING(DART_REGISTER_NATIVE)}); | ||
| } | ||
|
|
||
| fml::RefPtr<ColorFilter> ColorFilter::Create() { | ||
| return fml::MakeRefCounted<ColorFilter>(); | ||
| } | ||
|
|
||
| void ColorFilter::initMode(int color, int blend_mode) { | ||
| filter_ = SkColorFilters::Blend(static_cast<SkColor>(color), | ||
| static_cast<SkBlendMode>(blend_mode)); | ||
| } | ||
|
|
||
| sk_sp<SkColorFilter> ColorFilter::MakeColorMatrixFilter255( | ||
| const float array[20]) { | ||
| float tmp[20]; | ||
| memcpy(tmp, array, sizeof(tmp)); | ||
| tmp[4] *= 1.0f / 255; | ||
| tmp[9] *= 1.0f / 255; | ||
| tmp[14] *= 1.0f / 255; | ||
| tmp[19] *= 1.0f / 255; | ||
| return SkColorFilters::Matrix(tmp); | ||
| } | ||
|
|
||
| void ColorFilter::initMatrix(const tonic::Float32List& color_matrix) { | ||
| FML_CHECK(color_matrix.num_elements() == 20); | ||
|
|
||
| filter_ = MakeColorMatrixFilter255(color_matrix.data()); | ||
| } | ||
|
|
||
| void ColorFilter::initLinearToSrgbGamma() { | ||
| filter_ = SkColorFilters::LinearToSRGBGamma(); | ||
| } | ||
|
|
||
| void ColorFilter::initSrgbToLinearGamma() { | ||
| filter_ = SkColorFilters::SRGBToLinearGamma(); | ||
| } | ||
|
|
||
| ColorFilter::~ColorFilter() = default; | ||
|
|
||
| } // namespace flutter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #ifndef FLUTTER_LIB_UI_COLOR_FILTER_H_ | ||
| #define FLUTTER_LIB_UI_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" | ||
|
|
||
| using tonic::DartPersistentValue; | ||
|
|
||
| namespace tonic { | ||
| class DartLibraryNatives; | ||
| } // namespace tonic | ||
|
|
||
| namespace flutter { | ||
|
|
||
| // A handle to an SkCodec object. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is right? I don't see any SkCodec usage in this class |
||
| // | ||
| // Doesn't mirror SkCodec's API but provides a simple sequential access API. | ||
| class ColorFilter : public RefCountedDartWrappable<ColorFilter> { | ||
| DEFINE_WRAPPERTYPEINFO(); | ||
| FML_FRIEND_MAKE_REF_COUNTED(ColorFilter); | ||
|
|
||
| public: | ||
| static fml::RefPtr<ColorFilter> Create(); | ||
|
|
||
| // Flutter still defines the matrix to be biased by 255 in the last column | ||
| // (translate). skia is normalized, treating the last column as 0...1, so we | ||
| // post-scale here before calling the skia factory. | ||
| static sk_sp<SkColorFilter> MakeColorMatrixFilter255(const float array[20]); | ||
|
|
||
| void initMode(int color, int blend_mode); | ||
| void initMatrix(const tonic::Float32List& color_matrix); | ||
| void initSrgbToLinearGamma(); | ||
| void initLinearToSrgbGamma(); | ||
|
|
||
| ~ColorFilter() override; | ||
|
|
||
| sk_sp<SkColorFilter> filter() const { return filter_; } | ||
|
|
||
| static void RegisterNatives(tonic::DartLibraryNatives* natives); | ||
|
|
||
| private: | ||
| sk_sp<SkColorFilter> filter_; | ||
| }; | ||
|
|
||
| } // namespace flutter | ||
|
|
||
| #endif // FLUTTER_LIB_UI_COLOR_FILTER_H_ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the overall approach! My only concern is the test coverage. We could have a C++ unit test to cover paint.cc.
It might be a little more challenging to add a unit test for the Dart code. I wonder if we can use the fixture similar to the
shell_test.dartthat used in https://github.com/flutter/engine/blob/master/shell/common/shell_unittests.cc#L156. It allows the C++ unit test to specify a Dart function as the entry point, and a C++ native callback. I've used it to set aonReportTimingscallback on the Dart side, and check its results on the C++ side (https://github.com/flutter/engine/blob/master/shell/common/shell_unittests.cc#L315).I think it would be nice if we can create the
ColorFilteron the Dart side, and check theSkColorFilterobject is properly set on the C++ side. CC @chinmaygarde who might have more suggestions how to do this kind of Dart fixture test.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added dart tests that will serve to test the C++ side, and that cover the same code. LMK if you think we need more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8935830