forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
paint.cc
128 lines (115 loc) · 4.15 KB
/
paint.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
123
124
125
126
127
128
// 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 "impeller/aiks/paint.h"
#include "impeller/entity/contents/solid_color_contents.h"
#include "impeller/entity/geometry.h"
namespace impeller {
std::shared_ptr<Contents> Paint::CreateContentsForEntity(const Path& path,
bool cover) const {
std::unique_ptr<Geometry> geometry;
switch (style) {
case Style::kFill:
geometry = cover ? Geometry::MakeCover() : Geometry::MakeFillPath(path);
break;
case Style::kStroke:
geometry =
cover ? Geometry::MakeCover()
: Geometry::MakeStrokePath(path, stroke_width, stroke_miter,
stroke_cap, stroke_join);
break;
}
return CreateContentsForGeometry(std::move(geometry));
}
std::shared_ptr<Contents> Paint::CreateContentsForGeometry(
std::unique_ptr<Geometry> geometry) const {
if (color_source.has_value()) {
auto& source = color_source.value();
auto contents = source();
contents->SetGeometry(std::move(geometry));
contents->SetAlpha(color.alpha);
return contents;
}
auto solid_color = std::make_shared<SolidColorContents>();
solid_color->SetGeometry(std::move(geometry));
solid_color->SetColor(color);
return solid_color;
}
std::shared_ptr<Contents> Paint::CreateContentsForGeometry(
const std::shared_ptr<Geometry>& geometry) const {
if (color_source.has_value()) {
auto& source = color_source.value();
auto contents = source();
contents->SetGeometry(geometry);
contents->SetAlpha(color.alpha);
return contents;
}
auto solid_color = std::make_shared<SolidColorContents>();
solid_color->SetGeometry(geometry);
solid_color->SetColor(color);
return solid_color;
}
std::shared_ptr<Contents> Paint::WithFilters(
std::shared_ptr<Contents> input,
std::optional<bool> is_solid_color,
const Matrix& effect_transform) const {
bool is_solid_color_val = is_solid_color.value_or(!color_source);
input = WithColorFilter(input);
input = WithMaskBlur(input, is_solid_color_val, effect_transform);
input = WithImageFilter(input, effect_transform);
return input;
}
std::shared_ptr<Contents> Paint::WithFiltersForSubpassTarget(
std::shared_ptr<Contents> input,
const Matrix& effect_transform) const {
input = WithImageFilter(input, effect_transform);
input = WithColorFilter(input, /**absorb_opacity=*/true);
return input;
}
std::shared_ptr<Contents> Paint::WithMaskBlur(
std::shared_ptr<Contents> input,
bool is_solid_color,
const Matrix& effect_transform) const {
if (mask_blur_descriptor.has_value()) {
input = mask_blur_descriptor->CreateMaskBlur(
FilterInput::Make(input), is_solid_color, effect_transform);
}
return input;
}
std::shared_ptr<Contents> Paint::WithImageFilter(
std::shared_ptr<Contents> input,
const Matrix& effect_transform) const {
if (image_filter.has_value()) {
const ImageFilterProc& filter = image_filter.value();
input = filter(FilterInput::Make(input), effect_transform);
}
return input;
}
std::shared_ptr<Contents> Paint::WithColorFilter(
std::shared_ptr<Contents> input,
bool absorb_opacity) const {
if (color_filter.has_value()) {
const ColorFilterProc& filter = color_filter.value();
auto color_filter_contents = filter(FilterInput::Make(input));
if (color_filter_contents) {
color_filter_contents->SetAbsorbOpacity(absorb_opacity);
}
input = color_filter_contents;
}
return input;
}
std::shared_ptr<FilterContents> Paint::MaskBlurDescriptor::CreateMaskBlur(
const FilterInput::Ref& input,
bool is_solid_color,
const Matrix& effect_transform) const {
if (is_solid_color) {
return FilterContents::MakeGaussianBlur(
input, sigma, sigma, style, Entity::TileMode::kDecal, effect_transform);
}
return FilterContents::MakeBorderMaskBlur(input, sigma, sigma, style,
effect_transform);
}
bool Paint::HasColorFilter() const {
return color_filter.has_value();
}
} // namespace impeller