Skip to content

Commit

Permalink
Add DlImageFilter objects (flutter#32140)
Browse files Browse the repository at this point in the history
  • Loading branch information
flar authored Mar 21, 2022
1 parent ab6b671 commit 3f2750c
Show file tree
Hide file tree
Showing 22 changed files with 1,123 additions and 169 deletions.
2 changes: 1 addition & 1 deletion DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ deps = {
'src': 'https://github.com/flutter/buildroot.git' + '@' + '8cbf38af7d48cc298ae86e614533b4b2d0dc6758',

'src/flutter/impeller':
Var('github_git') + '/flutter/impeller' + '@' + '114e92d2d046b3496f2b9422946295efaae676dd',
Var('github_git') + '/flutter/impeller' + '@' + '70fd402c739f04d450b74c53c493e0d327d4cc18',

# Fuchsia compatibility
#
Expand Down
3 changes: 3 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ FILE: ../../../flutter/display_list/display_list_dispatcher.h
FILE: ../../../flutter/display_list/display_list_enum_unittests.cc
FILE: ../../../flutter/display_list/display_list_flags.cc
FILE: ../../../flutter/display_list/display_list_flags.h
FILE: ../../../flutter/display_list/display_list_image_filter.cc
FILE: ../../../flutter/display_list/display_list_image_filter.h
FILE: ../../../flutter/display_list/display_list_image_filter_unittests.cc
FILE: ../../../flutter/display_list/display_list_mask_filter.cc
FILE: ../../../flutter/display_list/display_list_mask_filter.h
FILE: ../../../flutter/display_list/display_list_mask_filter_unittests.cc
Expand Down
3 changes: 3 additions & 0 deletions display_list/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ source_set("display_list") {
"display_list_dispatcher.h",
"display_list_flags.cc",
"display_list_flags.h",
"display_list_image_filter.cc",
"display_list_image_filter.h",
"display_list_mask_filter.cc",
"display_list_mask_filter.h",
"display_list_ops.cc",
Expand Down Expand Up @@ -59,6 +61,7 @@ source_set("unittests") {
"display_list_color_source_unittests.cc",
"display_list_complexity_unittests.cc",
"display_list_enum_unittests.cc",
"display_list_image_filter_unittests.cc",
"display_list_mask_filter_unittests.cc",
"display_list_test_utils.cc",
"display_list_test_utils.h",
Expand Down
7 changes: 5 additions & 2 deletions display_list/display_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ namespace flutter {
\
V(SetBlender) \
V(ClearBlender) \
V(SetImageFilter) \
V(ClearImageFilter) \
V(SetPathEffect) \
V(ClearPathEffect) \
\
Expand All @@ -88,6 +86,11 @@ namespace flutter {
V(SetSkColorSource) \
V(SetImageColorSource) \
\
V(ClearImageFilter) \
V(SetPodImageFilter) \
V(SetSkImageFilter) \
V(SetSharedImageFilter) \
\
V(ClearMaskFilter) \
V(SetPodMaskFilter) \
V(SetSkMaskFilter) \
Expand Down
38 changes: 33 additions & 5 deletions display_list/display_list_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,38 @@ void DisplayListBuilder::onSetColorSource(const DlColorSource* source) {
}
}
}
void DisplayListBuilder::onSetImageFilter(sk_sp<SkImageFilter> filter) {
(current_image_filter_ = filter) //
? Push<SetImageFilterOp>(0, 0, std::move(filter))
: Push<ClearImageFilterOp>(0, 0);
void DisplayListBuilder::onSetImageFilter(const DlImageFilter* filter) {
if (filter == nullptr) {
current_image_filter_ = nullptr;
Push<ClearImageFilterOp>(0, 0);
} else {
current_image_filter_ = filter->shared();
switch (filter->type()) {
case DlImageFilterType::kBlur: {
const DlBlurImageFilter* blur_filter = filter->asBlur();
FML_DCHECK(blur_filter);
void* pod = Push<SetPodImageFilterOp>(blur_filter->size(), 0);
new (pod) DlBlurImageFilter(blur_filter);
break;
}
case DlImageFilterType::kMatrix: {
const DlMatrixImageFilter* matrix_filter = filter->asMatrix();
FML_DCHECK(matrix_filter);
void* pod = Push<SetPodImageFilterOp>(matrix_filter->size(), 0);
new (pod) DlMatrixImageFilter(matrix_filter);
break;
}
case DlImageFilterType::kComposeFilter:
case DlImageFilterType::kColorFilter: {
Push<SetSharedImageFilterOp>(0, 0, filter);
break;
}
case DlImageFilterType::kUnknown: {
Push<SetSkImageFilterOp>(0, 0, filter->skia_object());
break;
}
}
}
}
void DisplayListBuilder::onSetColorFilter(const DlColorFilter* filter) {
if (filter == nullptr) {
Expand Down Expand Up @@ -291,7 +319,7 @@ void DisplayListBuilder::setAttributesFromPaint(
setColorFilter(DlColorFilter::From(color_filter).get());
}
if (flags.applies_image_filter()) {
setImageFilter(sk_ref_sp(paint.getImageFilter()));
setImageFilter(DlImageFilter::From(paint.getImageFilter()).get());
}
if (flags.applies_path_effect()) {
setPathEffect(sk_ref_sp(paint.getPathEffect()));
Expand Down
14 changes: 8 additions & 6 deletions display_list/display_list_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ class DisplayListBuilder final : public virtual Dispatcher,
onSetColorSource(source);
}
}
void setImageFilter(sk_sp<SkImageFilter> filter) override {
if (current_image_filter_ != filter) {
onSetImageFilter(std::move(filter));
void setImageFilter(const DlImageFilter* filter) override {
if (NotEquals(current_image_filter_, filter)) {
onSetImageFilter(filter);
}
}
void setColorFilter(const DlColorFilter* filter) override {
Expand Down Expand Up @@ -141,7 +141,9 @@ class DisplayListBuilder final : public virtual Dispatcher,
std::shared_ptr<const DlMaskFilter> getMaskFilter() const {
return current_mask_filter_;
}
sk_sp<SkImageFilter> getImageFilter() const { return current_image_filter_; }
std::shared_ptr<DlImageFilter> getImageFilter() const {
return current_image_filter_;
}

void save() override;
// Only the |renders_with_attributes()| option will be accepted here. Any
Expand Down Expand Up @@ -380,7 +382,7 @@ class DisplayListBuilder final : public virtual Dispatcher,
void onSetBlendMode(DlBlendMode mode);
void onSetBlender(sk_sp<SkBlender> blender);
void onSetColorSource(const DlColorSource* source);
void onSetImageFilter(sk_sp<SkImageFilter> filter);
void onSetImageFilter(const DlImageFilter* filter);
void onSetColorFilter(const DlColorFilter* filter);
void onSetPathEffect(sk_sp<SkPathEffect> effect);
void onSetMaskFilter(const DlMaskFilter* filter);
Expand All @@ -401,7 +403,7 @@ class DisplayListBuilder final : public virtual Dispatcher,
sk_sp<SkBlender> current_blender_;
std::shared_ptr<const DlColorSource> current_color_source_;
std::shared_ptr<const DlColorFilter> current_color_filter_;
sk_sp<SkImageFilter> current_image_filter_;
std::shared_ptr<DlImageFilter> current_image_filter_;
sk_sp<SkPathEffect> current_path_effect_;
std::shared_ptr<const DlMaskFilter> current_mask_filter_;
};
Expand Down
31 changes: 12 additions & 19 deletions display_list/display_list_canvas_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -877,48 +877,45 @@ class CanvasCompareTester {
}
}
{
sk_sp<SkImageFilter> filter = SkImageFilters::Arithmetic(
sk_sp<SkImageFilter> sk_filter = SkImageFilters::Arithmetic(
0.1, 0.1, 0.1, 0.25, true, nullptr, nullptr);
DlUnknownImageFilter filter(sk_filter);
{
RenderWith(testP, env, tolerance,
CaseParameters(
"saveLayer ImageFilter, no bounds",
[=](SkCanvas* cv, SkPaint& p) {
SkPaint save_p;
save_p.setImageFilter(filter);
save_p.setImageFilter(filter.skia_object());
cv->saveLayer(nullptr, &save_p);
p.setStrokeWidth(5.0);
},
[=](DisplayListBuilder& b) {
b.setImageFilter(filter);
b.setImageFilter(&filter);
b.saveLayer(nullptr, true);
b.setImageFilter(nullptr);
b.setStrokeWidth(5.0);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
}
EXPECT_TRUE(filter->unique())
<< "saveLayer ImageFilter, no bounds Cleanup";
{
RenderWith(testP, env, tolerance,
CaseParameters(
"saveLayer ImageFilter and bounds",
[=](SkCanvas* cv, SkPaint& p) {
SkPaint save_p;
save_p.setImageFilter(filter);
save_p.setImageFilter(filter.skia_object());
cv->saveLayer(RenderBounds, &save_p);
p.setStrokeWidth(5.0);
},
[=](DisplayListBuilder& b) {
b.setImageFilter(filter);
b.setImageFilter(&filter);
b.saveLayer(&RenderBounds, true);
b.setImageFilter(nullptr);
b.setStrokeWidth(5.0);
})
.with_restore(cv_safe_restore, dl_safe_restore, true));
}
EXPECT_TRUE(filter->unique())
<< "saveLayer ImageFilter and bounds Cleanup";
}
}

Expand Down Expand Up @@ -1093,39 +1090,35 @@ class CanvasCompareTester {
b.setStrokeWidth(5.0);
};
blur_env.init_ref(cv_blur_setup, testP.cv_renderer(), dl_blur_setup);
sk_sp<SkImageFilter> filter =
SkImageFilters::Blur(5.0, 5.0, SkTileMode::kDecal, nullptr, nullptr);
DlBlurImageFilter filter_decal_5(5.0, 5.0, DlTileMode::kDecal);
BoundsTolerance blur5Tolerance = tolerance.addBoundsPadding(4, 4);
{
RenderWith(testP, blur_env, blur5Tolerance,
CaseParameters(
"ImageFilter == Decal Blur 5",
[=](SkCanvas* cv, SkPaint& p) {
cv_blur_setup(cv, p);
p.setImageFilter(filter);
p.setImageFilter(filter_decal_5.skia_object());
},
[=](DisplayListBuilder& b) {
dl_blur_setup(b);
b.setImageFilter(filter);
b.setImageFilter(&filter_decal_5);
}));
}
EXPECT_TRUE(filter->unique()) << "ImageFilter Cleanup";
filter =
SkImageFilters::Blur(5.0, 5.0, SkTileMode::kClamp, nullptr, nullptr);
DlBlurImageFilter filter_clamp_5(5.0, 5.0, DlTileMode::kClamp);
{
RenderWith(testP, blur_env, blur5Tolerance,
CaseParameters(
"ImageFilter == Clamp Blur 5",
[=](SkCanvas* cv, SkPaint& p) {
cv_blur_setup(cv, p);
p.setImageFilter(filter);
p.setImageFilter(filter_clamp_5.skia_object());
},
[=](DisplayListBuilder& b) {
dl_blur_setup(b);
b.setImageFilter(filter);
b.setImageFilter(&filter_clamp_5);
}));
}
EXPECT_TRUE(filter->unique()) << "ImageFilter Cleanup";
}

{
Expand Down
2 changes: 1 addition & 1 deletion display_list/display_list_complexity_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class ComplexityCalculatorHelper
void setBlendMode(DlBlendMode mode) override {}
void setBlender(sk_sp<SkBlender> blender) override {}
void setColorSource(const DlColorSource* source) override {}
void setImageFilter(sk_sp<SkImageFilter> filter) override {}
void setImageFilter(const DlImageFilter* filter) override {}
void setColorFilter(const DlColorFilter* filter) override {}
void setPathEffect(sk_sp<SkPathEffect> effect) override {}
void setMaskFilter(const DlMaskFilter* filter) override {}
Expand Down
3 changes: 2 additions & 1 deletion display_list/display_list_dispatcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "flutter/display_list/display_list_blend_mode.h"
#include "flutter/display_list/display_list_color_filter.h"
#include "flutter/display_list/display_list_color_source.h"
#include "flutter/display_list/display_list_image_filter.h"
#include "flutter/display_list/display_list_mask_filter.h"

namespace flutter {
Expand Down Expand Up @@ -50,7 +51,7 @@ class Dispatcher {
virtual void setBlender(sk_sp<SkBlender> blender) = 0;
virtual void setPathEffect(sk_sp<SkPathEffect> effect) = 0;
virtual void setMaskFilter(const DlMaskFilter* filter) = 0;
virtual void setImageFilter(sk_sp<SkImageFilter> filter) = 0;
virtual void setImageFilter(const DlImageFilter* filter) = 0;

// All of the following methods are nearly 1:1 with their counterparts
// in |SkCanvas| and have the same behavior and output.
Expand Down
29 changes: 29 additions & 0 deletions display_list/display_list_image_filter.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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/display_list/display_list_image_filter.h"

namespace flutter {

std::shared_ptr<DlImageFilter> DlImageFilter::From(SkImageFilter* sk_filter) {
if (sk_filter == nullptr) {
return nullptr;
}
{
SkColorFilter* color_filter;
if (sk_filter->isColorFilterNode(&color_filter)) {
FML_DCHECK(color_filter != nullptr);
// If |isColorFilterNode| succeeds, the pointer it sets into color_filter
// will be ref'd already so we do not use sk_ref_sp() here as that would
// double-ref the color filter object. Instead we use a bare sk_sp
// constructor to adopt this reference into an sk_sp<SkCF> without
// reffing it and let the compiler manage the refs.
return std::make_shared<DlColorFilterImageFilter>(
DlColorFilter::From(sk_sp<SkColorFilter>(color_filter)));
}
}
return std::make_shared<DlUnknownImageFilter>(sk_ref_sp(sk_filter));
}

} // namespace flutter
Loading

0 comments on commit 3f2750c

Please sign in to comment.