Flutter Engine
The Flutter Engine
dl_paint.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_DISPLAY_LIST_DL_PAINT_H_
6#define FLUTTER_DISPLAY_LIST_DL_PAINT_H_
7
8#include <memory>
9#include <utility>
10#include "flutter/display_list/dl_blend_mode.h"
11#include "flutter/display_list/dl_color.h"
12#include "flutter/display_list/effects/dl_color_filter.h"
13#include "flutter/display_list/effects/dl_color_source.h"
14#include "flutter/display_list/effects/dl_image_filter.h"
15#include "flutter/display_list/effects/dl_mask_filter.h"
16
17namespace flutter {
18
19enum class DlDrawStyle {
20 kFill, //!< fills interior of shapes
21 kStroke, //!< strokes boundary of shapes
22 kStrokeAndFill, //!< both strokes and fills shapes
23
26};
27
28enum class DlStrokeCap {
29 kButt, //!< no stroke extension
30 kRound, //!< adds circle
31 kSquare, //!< adds square
32
35};
36
37enum class DlStrokeJoin {
38 kMiter, //!< extends to miter limit
39 kRound, //!< adds circle
40 kBevel, //!< connects outside edges
41
44};
45
46class DlPaint {
47 public:
49 static constexpr float kDefaultWidth = 0.0;
50 static constexpr float kDefaultMiter = 4.0;
51
52 static const DlPaint kDefault;
53
55 explicit DlPaint(DlColor color);
56
57 bool isAntiAlias() const { return is_anti_alias_; }
60 return *this;
61 }
62
63 bool isInvertColors() const { return is_invert_colors_; }
66 return *this;
67 }
68
69 DlColor getColor() const { return color_; }
71 color_ = color;
72 return *this;
73 }
74
75 uint8_t getAlpha() const { return color_.argb() >> 24; }
76 DlPaint& setAlpha(uint8_t alpha) { return setColor(color_.withAlpha(alpha)); }
77 SkScalar getOpacity() const { return color_.getAlphaF(); }
79 setAlpha(SkScalarRoundToInt(opacity * 0xff));
80 return *this;
81 }
82
84 return static_cast<DlBlendMode>(blend_mode_);
85 }
87 blend_mode_ = static_cast<unsigned>(mode);
88 return *this;
89 }
90
92 return static_cast<DlDrawStyle>(draw_style_);
93 }
95 draw_style_ = static_cast<unsigned>(style);
96 return *this;
97 }
98
100 return static_cast<DlStrokeCap>(stroke_cap_);
101 }
103 stroke_cap_ = static_cast<unsigned>(cap);
104 return *this;
105 }
106
108 return static_cast<DlStrokeJoin>(stroke_join_);
109 }
111 stroke_join_ = static_cast<unsigned>(join);
112 return *this;
113 }
114
115 float getStrokeWidth() const { return stroke_width_; }
117 stroke_width_ = width;
118 return *this;
119 }
120
121 float getStrokeMiter() const { return stroke_miter_; }
122 DlPaint& setStrokeMiter(float miter) {
123 stroke_miter_ = miter;
124 return *this;
125 }
126
127 std::shared_ptr<const DlColorSource> getColorSource() const {
128 return color_source_;
129 }
130 const DlColorSource* getColorSourcePtr() const { return color_source_.get(); }
131 DlPaint& setColorSource(std::shared_ptr<const DlColorSource> source) {
132 color_source_ = std::move(source);
133 return *this;
134 }
136 color_source_ = source ? source->shared() : nullptr;
137 return *this;
138 }
139
140 std::shared_ptr<const DlColorFilter> getColorFilter() const {
141 return color_filter_;
142 }
143 const DlColorFilter* getColorFilterPtr() const { return color_filter_.get(); }
144 DlPaint& setColorFilter(const std::shared_ptr<const DlColorFilter>& filter) {
145 color_filter_ = filter;
146 return *this;
147 }
149 color_filter_ = filter ? filter->shared() : nullptr;
150 return *this;
151 }
152
153 std::shared_ptr<const DlImageFilter> getImageFilter() const {
154 return image_filter_;
155 }
156 const DlImageFilter* getImageFilterPtr() const { return image_filter_.get(); }
157 DlPaint& setImageFilter(const std::shared_ptr<const DlImageFilter>& filter) {
158 image_filter_ = filter;
159 return *this;
160 }
162 image_filter_ = filter ? filter->shared() : nullptr;
163 return *this;
164 }
165
166 std::shared_ptr<const DlMaskFilter> getMaskFilter() const {
167 return mask_filter_;
168 }
169 const DlMaskFilter* getMaskFilterPtr() const { return mask_filter_.get(); }
170 DlPaint& setMaskFilter(const std::shared_ptr<DlMaskFilter>& filter) {
171 mask_filter_ = filter;
172 return *this;
173 }
175 mask_filter_ = filter ? filter->shared() : nullptr;
176 return *this;
177 }
178
179 bool isDefault() const { return *this == kDefault; }
180
181 bool operator==(DlPaint const& other) const;
182 bool operator!=(DlPaint const& other) const { return !(*this == other); }
183
184 private:
185#define ASSERT_ENUM_FITS(last_enum, num_bits) \
186 static_assert(static_cast<int>(last_enum) < (1 << num_bits) && \
187 static_cast<int>(last_enum) * 2 >= (1 << num_bits))
188
189 static constexpr int kBlendModeBits = 5;
190 static constexpr int kDrawStyleBits = 2;
191 static constexpr int kStrokeCapBits = 2;
192 static constexpr int kStrokeJoinBits = 2;
193 ASSERT_ENUM_FITS(DlBlendMode::kLastMode, kBlendModeBits);
194 ASSERT_ENUM_FITS(DlDrawStyle::kLastStyle, kDrawStyleBits);
195 ASSERT_ENUM_FITS(DlStrokeCap::kLastCap, kStrokeCapBits);
196 ASSERT_ENUM_FITS(DlStrokeJoin::kLastJoin, kStrokeJoinBits);
197
198 union {
199 struct {
200 unsigned blend_mode_ : kBlendModeBits;
201 unsigned draw_style_ : kDrawStyleBits;
202 unsigned stroke_cap_ : kStrokeCapBits;
203 unsigned stroke_join_ : kStrokeJoinBits;
204 unsigned is_anti_alias_ : 1;
205 unsigned is_invert_colors_ : 1;
206 };
207 };
208
209 DlColor color_;
210 float stroke_width_;
211 float stroke_miter_;
212
213 std::shared_ptr<const DlColorSource> color_source_;
214 std::shared_ptr<const DlColorFilter> color_filter_;
215 std::shared_ptr<const DlImageFilter> image_filter_;
216 std::shared_ptr<const DlMaskFilter> mask_filter_;
217};
218
219} // namespace flutter
220
221#endif // FLUTTER_DISPLAY_LIST_DL_PAINT_H_
#define SkScalarRoundToInt(x)
Definition: SkScalar.h:37
virtual std::shared_ptr< D > shared() const =0
bool isAntiAlias() const
Definition: dl_paint.h:57
bool operator==(DlPaint const &other) const
Definition: dl_paint.cc:20
DlStrokeCap getStrokeCap() const
Definition: dl_paint.h:99
bool isDefault() const
Definition: dl_paint.h:179
DlColor getColor() const
Definition: dl_paint.h:69
DlPaint & setColor(DlColor color)
Definition: dl_paint.h:70
DlPaint & setAntiAlias(bool isAntiAlias)
Definition: dl_paint.h:58
static const DlPaint kDefault
Definition: dl_paint.h:52
DlPaint & setInvertColors(bool isInvertColors)
Definition: dl_paint.h:64
DlBlendMode getBlendMode() const
Definition: dl_paint.h:83
DlPaint & setColorFilter(const std::shared_ptr< const DlColorFilter > &filter)
Definition: dl_paint.h:144
static constexpr float kDefaultWidth
Definition: dl_paint.h:49
unsigned is_anti_alias_
Definition: dl_paint.h:204
unsigned stroke_cap_
Definition: dl_paint.h:202
DlPaint & setMaskFilter(const std::shared_ptr< DlMaskFilter > &filter)
Definition: dl_paint.h:170
float getStrokeMiter() const
Definition: dl_paint.h:121
DlPaint & setImageFilter(const DlImageFilter *filter)
Definition: dl_paint.h:161
std::shared_ptr< const DlColorSource > getColorSource() const
Definition: dl_paint.h:127
DlStrokeJoin getStrokeJoin() const
Definition: dl_paint.h:107
SkScalar getOpacity() const
Definition: dl_paint.h:77
DlPaint & setMaskFilter(const DlMaskFilter *filter)
Definition: dl_paint.h:174
unsigned is_invert_colors_
Definition: dl_paint.h:205
uint8_t getAlpha() const
Definition: dl_paint.h:75
unsigned draw_style_
Definition: dl_paint.h:201
const DlColorSource * getColorSourcePtr() const
Definition: dl_paint.h:130
DlPaint & setStrokeCap(DlStrokeCap cap)
Definition: dl_paint.h:102
const DlMaskFilter * getMaskFilterPtr() const
Definition: dl_paint.h:169
DlPaint & setStrokeWidth(float width)
Definition: dl_paint.h:116
DlPaint & setAlpha(uint8_t alpha)
Definition: dl_paint.h:76
const DlColorFilter * getColorFilterPtr() const
Definition: dl_paint.h:143
DlPaint & setStrokeMiter(float miter)
Definition: dl_paint.h:122
bool operator!=(DlPaint const &other) const
Definition: dl_paint.h:182
unsigned blend_mode_
Definition: dl_paint.h:200
DlPaint & setBlendMode(DlBlendMode mode)
Definition: dl_paint.h:86
static constexpr float kDefaultMiter
Definition: dl_paint.h:50
const DlImageFilter * getImageFilterPtr() const
Definition: dl_paint.h:156
DlDrawStyle getDrawStyle() const
Definition: dl_paint.h:91
std::shared_ptr< const DlMaskFilter > getMaskFilter() const
Definition: dl_paint.h:166
std::shared_ptr< const DlColorFilter > getColorFilter() const
Definition: dl_paint.h:140
DlPaint & setImageFilter(const std::shared_ptr< const DlImageFilter > &filter)
Definition: dl_paint.h:157
DlPaint & setColorFilter(const DlColorFilter *filter)
Definition: dl_paint.h:148
float getStrokeWidth() const
Definition: dl_paint.h:115
DlPaint & setOpacity(SkScalar opacity)
Definition: dl_paint.h:78
std::shared_ptr< const DlImageFilter > getImageFilter() const
Definition: dl_paint.h:153
DlPaint & setDrawStyle(DlDrawStyle style)
Definition: dl_paint.h:94
static constexpr DlColor kDefaultColor
Definition: dl_paint.h:48
DlPaint & setStrokeJoin(DlStrokeJoin join)
Definition: dl_paint.h:110
DlPaint & setColorSource(std::shared_ptr< const DlColorSource > source)
Definition: dl_paint.h:131
DlPaint & setColorSource(const DlColorSource *source)
Definition: dl_paint.h:135
unsigned stroke_join_
Definition: dl_paint.h:203
bool isInvertColors() const
Definition: dl_paint.h:63
DlColor color
SkBitmap source
Definition: examples.cpp:28
float SkScalar
Definition: extension.cpp:12
constexpr SkColor4f kBlack
Definition: SkColor.h:435
DlStrokeJoin
Definition: dl_paint.h:37
@ kMiter
extends to miter limit
@ kBevel
connects outside edges
DlStrokeCap
Definition: dl_paint.h:28
@ kRound
adds circle
@ kButt
no stroke extension
@ kSquare
adds square
DlDrawStyle
Definition: dl_paint.h:19
@ kStrokeAndFill
both strokes and fills shapes
@ kStroke
strokes boundary of shapes
@ kFill
fills interior of shapes
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive mode
Definition: switches.h:228
@ kLastMode
last valid value
flutter::DlColor DlColor
int32_t width
static SkString join(const CommandLineFlags::StringArray &)
Definition: skpbench.cpp:741
constexpr float getAlphaF() const
Definition: dl_color.h:47
static constexpr DlColor kBlack()
Definition: dl_color.h:22
constexpr DlColor withAlpha(uint8_t alpha) const
Definition: dl_color.h:63
constexpr uint32_t argb() const
Definition: dl_color.h:82