Skip to content

Commit

Permalink
Make shader mask layer code builder aware (flutter#35622)
Browse files Browse the repository at this point in the history
  • Loading branch information
JsouLiang authored Aug 31, 2022
1 parent 6c9f6f5 commit 169181f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
42 changes: 28 additions & 14 deletions flow/layers/shader_mask_layer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

namespace flutter {

ShaderMaskLayer::ShaderMaskLayer(std::shared_ptr<DlColorSource> shader,
ShaderMaskLayer::ShaderMaskLayer(std::shared_ptr<DlColorSource> color_source,
const SkRect& mask_rect,
DlBlendMode blend_mode)
: CacheableContainerLayer(
RasterCacheUtil::kMinimumRendersBeforeCachingFilterLayer),
shader_(std::move(shader)),
color_source_(std::move(color_source)),
mask_rect_(mask_rect),
blend_mode_(blend_mode) {}

Expand All @@ -21,8 +21,8 @@ void ShaderMaskLayer::Diff(DiffContext* context, const Layer* old_layer) {
auto* prev = static_cast<const ShaderMaskLayer*>(old_layer);
if (!context->IsSubtreeDirty()) {
FML_DCHECK(prev);
if (shader_ != prev->shader_ || mask_rect_ != prev->mask_rect_ ||
blend_mode_ != prev->blend_mode_) {
if (color_source_ != prev->color_source_ ||
mask_rect_ != prev->mask_rect_ || blend_mode_ != prev->blend_mode_) {
context->MarkSubtreeDirty(context->GetOldLayerPaintRegion(old_layer));
}
}
Expand Down Expand Up @@ -54,19 +54,33 @@ void ShaderMaskLayer::Paint(PaintContext& context) const {
return;
}
}
auto shader_rect = SkRect::MakeWH(mask_rect_.width(), mask_rect_.height());

Layer::AutoSaveLayer save = Layer::AutoSaveLayer::Create(
context, paint_bounds(), cache_paint.sk_paint());
PaintChildren(context);
if (context.leaf_nodes_builder) {
context.builder_multiplexer->saveLayer(&paint_bounds(),
cache_paint.dl_paint());
PaintChildren(context);

SkPaint paint;
paint.setBlendMode(ToSk(blend_mode_));
if (shader_) {
paint.setShader(shader_->skia_object());
DlPaint dl_paint;
dl_paint.setBlendMode(blend_mode_);
if (color_source_) {
dl_paint.setColorSource(color_source_.get());
}
context.leaf_nodes_builder->translate(mask_rect_.left(), mask_rect_.top());
context.leaf_nodes_builder->drawRect(shader_rect, dl_paint);
context.builder_multiplexer->restore();
} else {
Layer::AutoSaveLayer save = Layer::AutoSaveLayer::Create(
context, paint_bounds(), cache_paint.sk_paint());
PaintChildren(context);
SkPaint paint;
paint.setBlendMode(ToSk(blend_mode_));
if (color_source_) {
paint.setShader(color_source_->skia_object());
}
context.leaf_nodes_canvas->translate(mask_rect_.left(), mask_rect_.top());
context.leaf_nodes_canvas->drawRect(shader_rect, paint);
}
context.leaf_nodes_canvas->translate(mask_rect_.left(), mask_rect_.top());
context.leaf_nodes_canvas->drawRect(
SkRect::MakeWH(mask_rect_.width(), mask_rect_.height()), paint);
}

} // namespace flutter
4 changes: 2 additions & 2 deletions flow/layers/shader_mask_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace flutter {

class ShaderMaskLayer : public CacheableContainerLayer {
public:
ShaderMaskLayer(std::shared_ptr<DlColorSource> shader,
ShaderMaskLayer(std::shared_ptr<DlColorSource> color_source,
const SkRect& mask_rect,
DlBlendMode blend_mode);

Expand All @@ -23,7 +23,7 @@ class ShaderMaskLayer : public CacheableContainerLayer {
void Paint(PaintContext& context) const override;

private:
std::shared_ptr<DlColorSource> shader_;
std::shared_ptr<DlColorSource> color_source_;
SkRect mask_rect_;
DlBlendMode blend_mode_;

Expand Down
11 changes: 5 additions & 6 deletions flow/layers/shader_mask_layer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -373,17 +373,16 @@ TEST_F(ShaderMaskLayerTest, OpacityInheritance) {
{
expected_builder.translate(offset.fX, offset.fY);
/* ShaderMaskLayer::Paint() */ {
expected_builder.setColor(opacity_alpha << 24);
expected_builder.saveLayer(&child_path.getBounds(), true);
DlPaint sl_paint = DlPaint().setColor(opacity_alpha << 24);
expected_builder.saveLayer(&child_path.getBounds(), &sl_paint);
{
/* child layer paint */ {
expected_builder.setColor(0xFF000000);
expected_builder.drawPath(child_path);
expected_builder.drawPath(child_path, DlPaint());
}
expected_builder.translate(mask_rect.fLeft, mask_rect.fTop);
expected_builder.setBlendMode(DlBlendMode::kSrc);
expected_builder.drawRect(
SkRect::MakeWH(mask_rect.width(), mask_rect.height()));
SkRect::MakeWH(mask_rect.width(), mask_rect.height()),
DlPaint().setBlendMode(DlBlendMode::kSrc));
}
expected_builder.restore();
}
Expand Down

0 comments on commit 169181f

Please sign in to comment.