forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
image_filter_layer.cc
122 lines (98 loc) · 4.12 KB
/
image_filter_layer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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/flow/layers/image_filter_layer.h"
#include "flutter/flow/layers/layer.h"
#include "flutter/flow/raster_cache_util.h"
namespace flutter {
ImageFilterLayer::ImageFilterLayer(sk_sp<SkImageFilter> filter)
: CacheableContainerLayer(
RasterCacheUtil::kMinimumRendersBeforeCachingFilterLayer),
filter_(std::move(filter)),
transformed_filter_(nullptr) {}
void ImageFilterLayer::Diff(DiffContext* context, const Layer* old_layer) {
DiffContext::AutoSubtreeRestore subtree(context);
auto* prev = static_cast<const ImageFilterLayer*>(old_layer);
if (!context->IsSubtreeDirty()) {
FML_DCHECK(prev);
if (filter_ != prev->filter_) {
context->MarkSubtreeDirty(context->GetOldLayerPaintRegion(old_layer));
}
}
if (context->has_raster_cache()) {
context->SetTransform(
RasterCacheUtil::GetIntegralTransCTM(context->GetTransform()));
}
if (filter_) {
auto filter = filter_->makeWithLocalMatrix(context->GetTransform());
if (filter) {
// This transform will be applied to every child rect in the subtree
context->PushFilterBoundsAdjustment([filter](SkRect rect) {
return SkRect::Make(
filter->filterBounds(rect.roundOut(), SkMatrix::I(),
SkImageFilter::kForward_MapDirection));
});
}
}
DiffChildren(context, prev);
context->SetLayerPaintRegion(this, context->CurrentSubtreeRegion());
}
void ImageFilterLayer::Preroll(PrerollContext* context,
const SkMatrix& matrix) {
TRACE_EVENT0("flutter", "ImageFilterLayer::Preroll");
Layer::AutoPrerollSaveLayerState save =
Layer::AutoPrerollSaveLayerState::Create(context);
SkMatrix child_matrix = matrix;
if (context->raster_cache) {
child_matrix = RasterCacheUtil::GetIntegralTransCTM(child_matrix);
}
AutoCache cache =
AutoCache(layer_raster_cache_item_.get(), context, child_matrix);
SkRect child_bounds = SkRect::MakeEmpty();
PrerollChildren(context, child_matrix, &child_bounds);
context->subtree_can_inherit_opacity = true;
// We always paint with a saveLayer (or a cached rendering),
// so we can always apply opacity in any of those cases.
context->subtree_can_inherit_opacity = true;
if (!filter_) {
set_paint_bounds(child_bounds);
return;
}
const SkIRect filter_input_bounds = child_bounds.roundOut();
SkIRect filter_output_bounds = filter_->filterBounds(
filter_input_bounds, SkMatrix::I(), SkImageFilter::kForward_MapDirection);
child_bounds = SkRect::Make(filter_output_bounds);
set_paint_bounds(child_bounds);
// CacheChildren only when the transformed_filter_ doesn't equal null.
// So in here we reset the LayerRasterCacheItem cache state.
layer_raster_cache_item_->MarkNotCacheChildren();
transformed_filter_ = filter_->makeWithLocalMatrix(child_matrix);
if (transformed_filter_) {
layer_raster_cache_item_->MarkCacheChildren();
}
}
void ImageFilterLayer::Paint(PaintContext& context) const {
TRACE_EVENT0("flutter", "ImageFilterLayer::Paint");
FML_DCHECK(needs_painting(context));
AutoCachePaint cache_paint(context);
if (context.raster_cache) {
context.internal_nodes_canvas->setMatrix(
RasterCacheUtil::GetIntegralTransCTM(
context.leaf_nodes_canvas->getTotalMatrix()));
}
if (layer_raster_cache_item_->IsCacheChildren()) {
cache_paint.setImageFilter(transformed_filter_);
}
if (layer_raster_cache_item_->Draw(context, cache_paint.sk_paint())) {
return;
}
cache_paint.setImageFilter(filter_);
// Normally a save_layer is sized to the current layer bounds, but in this
// case the bounds of the child may not be the same as the filtered version
// so we use the bounds of the child container which do not include any
// modifications that the filter might apply.
Layer::AutoSaveLayer save_layer = Layer::AutoSaveLayer::Create(
context, child_paint_bounds(), cache_paint.sk_paint());
PaintChildren(context);
}
} // namespace flutter