ColorFilterLayer change SkColorFilter to DLColorFilter (flutter/engine#34744)

* change SkColorFilter to DlColorFilter

* draft for SkColorFilter to DlColorFilter

* draft for SkColorFilter to DlColorFilter

* Formatting code

* Use DlColorFilter for ColorFilterLayer; Change the test cases

* Use DlObject for test cases

* fix some nits

* update code formats

* update the local variable name
This commit is contained in:
JsouLiang
2022-08-05 21:00:54 +08:00
committed by GitHub
parent fdd3e512fd
commit 6d68d589db
5 changed files with 128 additions and 90 deletions

View File

@@ -3,12 +3,15 @@
// found in the LICENSE file.
#include "flutter/flow/layers/color_filter_layer.h"
#include "flutter/display_list/display_list_comparable.h"
#include "flutter/display_list/display_list_paint.h"
#include "flutter/flow/raster_cache_item.h"
#include "flutter/flow/raster_cache_util.h"
namespace flutter {
ColorFilterLayer::ColorFilterLayer(sk_sp<SkColorFilter> filter)
ColorFilterLayer::ColorFilterLayer(std::shared_ptr<const DlColorFilter> filter)
: CacheableContainerLayer(
RasterCacheUtil::kMinimumRendersBeforeCachingFilterLayer,
true),
@@ -19,7 +22,7 @@ void ColorFilterLayer::Diff(DiffContext* context, const Layer* old_layer) {
auto* prev = static_cast<const ColorFilterLayer*>(old_layer);
if (!context->IsSubtreeDirty()) {
FML_DCHECK(prev);
if (filter_ != prev->filter_) {
if (NotEquals(filter_, prev->filter_)) {
context->MarkSubtreeDirty(context->GetOldLayerPaintRegion(old_layer));
}
}
@@ -45,23 +48,28 @@ void ColorFilterLayer::Paint(PaintContext& context) const {
TRACE_EVENT0("flutter", "ColorFilterLayer::Paint");
FML_DCHECK(needs_painting(context));
AutoCachePaint cache_paint(context);
if (context.raster_cache) {
AutoCachePaint cache_paint(context);
if (layer_raster_cache_item_->IsCacheChildren()) {
cache_paint.setColorFilter(filter_);
cache_paint.setColorFilter(filter_.get());
}
if (layer_raster_cache_item_->Draw(context, cache_paint.sk_paint())) {
return;
}
}
cache_paint.setColorFilter(filter_);
Layer::AutoSaveLayer save = Layer::AutoSaveLayer::Create(
context, paint_bounds(), cache_paint.sk_paint());
PaintChildren(context);
AutoCachePaint cache_paint(context);
cache_paint.setColorFilter(filter_.get());
if (context.leaf_nodes_builder) {
context.leaf_nodes_builder->saveLayer(&paint_bounds(),
cache_paint.dl_paint());
PaintChildren(context);
context.leaf_nodes_builder->restore();
} else {
Layer::AutoSaveLayer save = Layer::AutoSaveLayer::Create(
context, paint_bounds(), cache_paint.sk_paint());
PaintChildren(context);
}
}
} // namespace flutter

View File

@@ -5,6 +5,7 @@
#ifndef FLUTTER_FLOW_LAYERS_COLOR_FILTER_LAYER_H_
#define FLUTTER_FLOW_LAYERS_COLOR_FILTER_LAYER_H_
#include "flutter/display_list/display_list_color_filter.h"
#include "flutter/flow/layers/cacheable_layer.h"
#include "flutter/flow/layers/layer.h"
#include "third_party/skia/include/core/SkColorFilter.h"
@@ -12,7 +13,7 @@ namespace flutter {
class ColorFilterLayer : public CacheableContainerLayer {
public:
explicit ColorFilterLayer(sk_sp<SkColorFilter> filter);
explicit ColorFilterLayer(std::shared_ptr<const DlColorFilter> filter);
void Diff(DiffContext* context, const Layer* old_layer) override;
@@ -21,7 +22,8 @@ class ColorFilterLayer : public CacheableContainerLayer {
void Paint(PaintContext& context) const override;
private:
sk_sp<SkColorFilter> filter_;
std::shared_ptr<const DlColorFilter> filter_;
FML_DISALLOW_COPY_AND_ASSIGN(ColorFilterLayer);
};

View File

@@ -2,6 +2,9 @@
// 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.h"
#include "flutter/display_list/display_list_color.h"
#include "flutter/display_list/display_list_paint.h"
#include "flutter/flow/compositor_context.h"
#include "flutter/flow/layers/color_filter_layer.h"
@@ -14,6 +17,7 @@
#include "flutter/flow/testing/mock_layer.h"
#include "flutter/fml/macros.h"
#include "flutter/testing/mock_canvas.h"
#include "include/core/SkColor.h"
#include "third_party/skia/include/core/SkColorFilter.h"
#include "third_party/skia/include/effects/SkColorMatrixFilter.h"
@@ -24,7 +28,8 @@ using ColorFilterLayerTest = LayerTest;
#ifndef NDEBUG
TEST_F(ColorFilterLayerTest, PaintingEmptyLayerDies) {
auto layer = std::make_shared<ColorFilterLayer>(sk_sp<SkColorFilter>());
auto layer = std::make_shared<ColorFilterLayer>(
std::make_shared<DlUnknownColorFilter>(sk_sp<SkColorFilter>()));
layer->Preroll(preroll_context(), SkMatrix());
EXPECT_EQ(layer->paint_bounds(), kEmptyRect);
@@ -39,7 +44,9 @@ TEST_F(ColorFilterLayerTest, PaintBeforePrerollDies) {
const SkRect child_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f);
const SkPath child_path = SkPath().addRect(child_bounds);
auto mock_layer = std::make_shared<MockLayer>(child_path);
auto layer = std::make_shared<ColorFilterLayer>(sk_sp<SkColorFilter>());
auto layer = std::make_shared<ColorFilterLayer>(
std::make_shared<DlUnknownColorFilter>(sk_sp<SkColorFilter>()));
layer->Add(mock_layer);
EXPECT_EQ(layer->paint_bounds(), kEmptyRect);
@@ -82,10 +89,10 @@ TEST_F(ColorFilterLayerTest, SimpleFilter) {
const SkRect child_bounds = SkRect::MakeLTRB(5.0f, 6.0f, 20.5f, 21.5f);
const SkPath child_path = SkPath().addRect(child_bounds);
const SkPaint child_paint = SkPaint(SkColors::kYellow);
auto layer_filter =
SkColorMatrixFilter::MakeLightingFilter(SK_ColorGREEN, SK_ColorYELLOW);
auto dl_color_filter = DlLinearToSrgbGammaColorFilter::instance;
auto mock_layer = std::make_shared<MockLayer>(child_path, child_paint);
auto layer = std::make_shared<ColorFilterLayer>(layer_filter);
auto layer = std::make_shared<ColorFilterLayer>(dl_color_filter);
layer->Add(mock_layer);
layer->Preroll(preroll_context(), initial_transform);
@@ -94,17 +101,23 @@ TEST_F(ColorFilterLayerTest, SimpleFilter) {
EXPECT_TRUE(layer->needs_painting(paint_context()));
EXPECT_EQ(mock_layer->parent_matrix(), initial_transform);
SkPaint filter_paint;
filter_paint.setColorFilter(layer_filter);
layer->Paint(paint_context());
EXPECT_EQ(
mock_canvas().draw_calls(),
std::vector({MockCanvas::DrawCall{
0, MockCanvas::SaveLayerData{child_bounds, filter_paint,
nullptr, 1}},
MockCanvas::DrawCall{
1, MockCanvas::DrawPathData{child_path, child_paint}},
MockCanvas::DrawCall{1, MockCanvas::RestoreData{0}}}));
DisplayListBuilder expected_builder;
/* ColorFilterLayer::Paint() */ {
DlPaint dl_paint;
dl_paint.setColorFilter(dl_color_filter.get());
expected_builder.saveLayer(&child_bounds, &dl_paint);
{
/* MockLayer::Paint() */ {
expected_builder.drawPath(child_path,
DlPaint().setColor(DlColor::kYellow()));
}
}
}
expected_builder.restore();
auto expected_display_list = expected_builder.Build();
layer->Paint(display_list_paint_context());
EXPECT_TRUE(DisplayListsEQ_Verbose(display_list(), expected_display_list));
}
TEST_F(ColorFilterLayerTest, MultipleChildren) {
@@ -115,11 +128,10 @@ TEST_F(ColorFilterLayerTest, MultipleChildren) {
SkPath().addRect(child_bounds.makeOffset(3.0f, 0.0f));
const SkPaint child_paint1 = SkPaint(SkColors::kYellow);
const SkPaint child_paint2 = SkPaint(SkColors::kCyan);
auto layer_filter =
SkColorMatrixFilter::MakeLightingFilter(SK_ColorGREEN, SK_ColorYELLOW);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
auto layer = std::make_shared<ColorFilterLayer>(layer_filter);
auto dl_color_filter = DlSrgbToLinearGammaColorFilter::instance;
auto layer = std::make_shared<ColorFilterLayer>(dl_color_filter);
layer->Add(mock_layer1);
layer->Add(mock_layer2);
@@ -136,19 +148,27 @@ TEST_F(ColorFilterLayerTest, MultipleChildren) {
EXPECT_EQ(mock_layer1->parent_matrix(), initial_transform);
EXPECT_EQ(mock_layer2->parent_matrix(), initial_transform);
SkPaint filter_paint;
filter_paint.setColorFilter(layer_filter);
layer->Paint(paint_context());
EXPECT_EQ(
mock_canvas().draw_calls(),
std::vector({MockCanvas::DrawCall{
0, MockCanvas::SaveLayerData{children_bounds,
filter_paint, nullptr, 1}},
MockCanvas::DrawCall{
1, MockCanvas::DrawPathData{child_path1, child_paint1}},
MockCanvas::DrawCall{
1, MockCanvas::DrawPathData{child_path2, child_paint2}},
MockCanvas::DrawCall{1, MockCanvas::RestoreData{0}}}));
DisplayListBuilder expected_builder;
/* ColorFilterLayer::Paint() */ {
DlPaint dl_paint;
dl_paint.setColorFilter(dl_color_filter.get());
expected_builder.saveLayer(&children_bounds, &dl_paint);
{
/* MockLayer::Paint() */ {
expected_builder.drawPath(child_path1,
DlPaint().setColor(DlColor::kYellow()));
}
/* MockLayer::Paint() */ {
expected_builder.drawPath(child_path2,
DlPaint().setColor(DlColor::kCyan()));
}
}
}
expected_builder.restore();
auto expected_display_list = expected_builder.Build();
layer->Paint(display_list_paint_context());
EXPECT_TRUE(DisplayListsEQ_Verbose(display_list(), expected_display_list));
}
TEST_F(ColorFilterLayerTest, Nested) {
@@ -159,14 +179,12 @@ TEST_F(ColorFilterLayerTest, Nested) {
SkPath().addRect(child_bounds.makeOffset(3.0f, 0.0f));
const SkPaint child_paint1 = SkPaint(SkColors::kYellow);
const SkPaint child_paint2 = SkPaint(SkColors::kCyan);
auto layer_filter1 =
SkColorMatrixFilter::MakeLightingFilter(SK_ColorGREEN, SK_ColorYELLOW);
auto layer_filter2 =
SkColorMatrixFilter::MakeLightingFilter(SK_ColorMAGENTA, SK_ColorBLUE);
auto mock_layer1 = std::make_shared<MockLayer>(child_path1, child_paint1);
auto mock_layer2 = std::make_shared<MockLayer>(child_path2, child_paint2);
auto layer1 = std::make_shared<ColorFilterLayer>(layer_filter1);
auto layer2 = std::make_shared<ColorFilterLayer>(layer_filter2);
auto dl_color_filter = DlSrgbToLinearGammaColorFilter::instance;
auto layer1 = std::make_shared<ColorFilterLayer>(dl_color_filter);
auto layer2 = std::make_shared<ColorFilterLayer>(dl_color_filter);
layer2->Add(mock_layer2);
layer1->Add(mock_layer1);
layer1->Add(layer2);
@@ -187,32 +205,44 @@ TEST_F(ColorFilterLayerTest, Nested) {
EXPECT_EQ(mock_layer1->parent_matrix(), initial_transform);
EXPECT_EQ(mock_layer2->parent_matrix(), initial_transform);
SkPaint filter_paint1, filter_paint2;
filter_paint1.setColorFilter(layer_filter1);
filter_paint2.setColorFilter(layer_filter2);
layer1->Paint(paint_context());
EXPECT_EQ(
mock_canvas().draw_calls(),
std::vector({MockCanvas::DrawCall{
0, MockCanvas::SaveLayerData{children_bounds,
filter_paint1, nullptr, 1}},
MockCanvas::DrawCall{
1, MockCanvas::DrawPathData{child_path1, child_paint1}},
MockCanvas::DrawCall{
1, MockCanvas::SaveLayerData{child_path2.getBounds(),
filter_paint2, nullptr, 2}},
MockCanvas::DrawCall{
2, MockCanvas::DrawPathData{child_path2, child_paint2}},
MockCanvas::DrawCall{2, MockCanvas::RestoreData{1}},
MockCanvas::DrawCall{1, MockCanvas::RestoreData{0}}}));
DisplayListBuilder expected_builder;
/* ColorFilterLayer::Paint() */ {
DlPaint dl_paint;
dl_paint.setColorFilter(dl_color_filter.get());
expected_builder.saveLayer(&children_bounds, &dl_paint);
{
/* MockLayer::Paint() */ {
expected_builder.drawPath(child_path1,
DlPaint().setColor(DlColor::kYellow()));
}
/* ColorFilter::Paint() */ {
DlPaint child_dl_paint;
child_dl_paint.setColor(DlColor::kBlack());
child_dl_paint.setColorFilter(dl_color_filter.get());
expected_builder.saveLayer(&child_path2.getBounds(), &child_dl_paint);
/* MockLayer::Paint() */ {
expected_builder.drawPath(child_path2,
DlPaint().setColor(DlColor::kCyan()));
}
expected_builder.restore();
}
}
}
expected_builder.restore();
auto expected_display_list = expected_builder.Build();
layer1->Paint(display_list_paint_context());
EXPECT_TRUE(DisplayListsEQ_Verbose(display_list(), expected_display_list));
}
TEST_F(ColorFilterLayerTest, Readback) {
auto layer_filter = SkColorFilters::LinearToSRGBGamma();
auto initial_transform = SkMatrix();
// ColorFilterLayer does not read from surface
auto layer = std::make_shared<ColorFilterLayer>(layer_filter);
auto layer = std::make_shared<ColorFilterLayer>(
DlLinearToSrgbGammaColorFilter::instance);
preroll_context()->surface_needs_readback = false;
layer->Preroll(preroll_context(), initial_transform);
EXPECT_FALSE(preroll_context()->surface_needs_readback);
@@ -227,8 +257,7 @@ TEST_F(ColorFilterLayerTest, Readback) {
}
TEST_F(ColorFilterLayerTest, CacheChild) {
auto layer_filter =
SkColorMatrixFilter::MakeLightingFilter(SK_ColorGREEN, SK_ColorYELLOW);
auto layer_filter = DlSrgbToLinearGammaColorFilter::instance;
auto initial_transform = SkMatrix::Translate(50.0, 25.5);
auto other_transform = SkMatrix::Scale(1.0, 2.0);
const SkPath child_path = SkPath().addRect(SkRect::MakeWH(5.0f, 5.0f));
@@ -268,8 +297,7 @@ TEST_F(ColorFilterLayerTest, CacheChild) {
}
TEST_F(ColorFilterLayerTest, CacheChildren) {
auto layer_filter =
SkColorMatrixFilter::MakeLightingFilter(SK_ColorGREEN, SK_ColorYELLOW);
auto layer_filter = DlSrgbToLinearGammaColorFilter::instance;
auto initial_transform = SkMatrix::Translate(50.0, 25.5);
auto other_transform = SkMatrix::Scale(1.0, 2.0);
const SkPath child_path1 = SkPath().addRect(SkRect::MakeWH(5.0f, 5.0f));
@@ -314,8 +342,7 @@ TEST_F(ColorFilterLayerTest, CacheChildren) {
}
TEST_F(ColorFilterLayerTest, CacheColorFilterLayerSelf) {
auto layer_filter =
SkColorMatrixFilter::MakeLightingFilter(SK_ColorGREEN, SK_ColorYELLOW);
auto layer_filter = DlSrgbToLinearGammaColorFilter::instance;
auto initial_transform = SkMatrix::Translate(50.0, 25.5);
auto other_transform = SkMatrix::Scale(1.0, 2.0);
const SkPath child_path1 = SkPath().addRect(SkRect::MakeWH(5.0f, 5.0f));
@@ -384,8 +411,8 @@ TEST_F(ColorFilterLayerTest, OpacityInheritance) {
auto initial_transform = SkMatrix::Translate(50.0, 25.5);
const SkPath child_path = SkPath().addRect(SkRect::MakeWH(5.0f, 5.0f));
auto mock_layer = std::make_shared<MockLayer>(child_path);
auto color_filter_layer =
std::make_shared<ColorFilterLayer>(layer_filter.skia_object());
auto color_filter_layer = std::make_shared<ColorFilterLayer>(
std::make_shared<DlMatrixColorFilter>(matrix));
color_filter_layer->Add(mock_layer);
PrerollContext* context = preroll_context();
@@ -409,13 +436,12 @@ TEST_F(ColorFilterLayerTest, OpacityInheritance) {
{
expected_builder.translate(offset.fX, offset.fY);
/* ColorFilterLayer::Paint() */ {
expected_builder.setColor(opacity_alpha << 24);
expected_builder.setColorFilter(&layer_filter);
expected_builder.saveLayer(&child_path.getBounds(), true);
DlPaint dl_paint;
dl_paint.setColor(opacity_alpha << 24);
dl_paint.setColorFilter(&layer_filter);
expected_builder.saveLayer(&child_path.getBounds(), &dl_paint);
/* MockLayer::Paint() */ {
expected_builder.setColor(0xFF000000);
expected_builder.setColorFilter(nullptr);
expected_builder.drawPath(child_path);
expected_builder.drawPath(child_path, DlPaint().setColor(0xFF000000));
}
expected_builder.restore();
}

View File

@@ -229,9 +229,11 @@ class Layer {
update_needs_paint();
}
void setColorFilter(sk_sp<SkColorFilter> filter) {
sk_paint_.setColorFilter(filter);
dl_paint_.setColorFilter(DlColorFilter::From(filter));
void setColorFilter(const DlColorFilter* filter) {
if (!filter)
return;
sk_paint_.setColorFilter(filter->skia_object());
dl_paint_.setColorFilter(filter);
update_needs_paint();
}

View File

@@ -139,8 +139,8 @@ void SceneBuilder::pushOpacity(Dart_Handle layer_handle,
void SceneBuilder::pushColorFilter(Dart_Handle layer_handle,
const ColorFilter* color_filter,
fml::RefPtr<EngineLayer> oldLayer) {
auto layer = std::make_shared<flutter::ColorFilterLayer>(
color_filter->filter()->skia_object());
auto layer =
std::make_shared<flutter::ColorFilterLayer>(color_filter->filter());
PushLayer(layer);
EngineLayer::MakeRetained(layer_handle, layer);