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 "flutter/display_list/dl_blend_mode.h"
10#include "flutter/display_list/dl_color.h"
11#include "flutter/display_list/effects/dl_color_filter.h"
12#include "flutter/display_list/effects/dl_color_source.h"
13#include "flutter/display_list/effects/dl_image_filter.h"
14#include "flutter/display_list/effects/dl_mask_filter.h"
15#include "flutter/display_list/effects/dl_path_effect.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
24 kLastStyle = kStrokeAndFill,
25 kDefaultStyle = kFill,
26};
27
28enum class DlStrokeCap {
29 kButt, //!< no stroke extension
30 kRound, //!< adds circle
31 kSquare, //!< adds square
32
33 kLastCap = kSquare,
34 kDefaultCap = kButt,
35};
36
37enum class DlStrokeJoin {
38 kMiter, //!< extends to miter limit
39 kRound, //!< adds circle
40 kBevel, //!< connects outside edges
41
42 kLastJoin = kBevel,
43 kDefaultJoin = kMiter,
44};
45
46class DlPaint {
47 public:
48 static constexpr DlColor kDefaultColor = DlColor::kBlack();
49 static constexpr float kDefaultWidth = 0.0;
50 static constexpr float kDefaultMiter = 4.0;
51
52 static const DlPaint kDefault;
53
54 DlPaint() : DlPaint(DlColor::kBlack()) {}
55 DlPaint(DlColor color);
56
57 bool isAntiAlias() const { return isAntiAlias_; }
58 DlPaint& setAntiAlias(bool isAntiAlias) {
59 isAntiAlias_ = isAntiAlias;
60 return *this;
61 }
62
63 bool isDither() const { return isDither_; }
64 DlPaint& setDither(bool isDither) {
65 isDither_ = isDither;
66 return *this;
67 }
68
69 bool isInvertColors() const { return isInvertColors_; }
70 DlPaint& setInvertColors(bool isInvertColors) {
71 isInvertColors_ = isInvertColors;
72 return *this;
73 }
74
75 DlColor getColor() const { return color_; }
76 DlPaint& setColor(DlColor color) {
77 color_ = color;
78 return *this;
79 }
80
81 uint8_t getAlpha() const { return color_.argb >> 24; }
82 DlPaint& setAlpha(uint8_t alpha) {
83 color_.argb = alpha << 24 | (color_.argb & 0x00FFFFFF);
84 return *this;
85 }
86 SkScalar getOpacity() const { return color_.getAlphaF(); }
87 DlPaint& setOpacity(SkScalar opacity) {
88 setAlpha(SkScalarRoundToInt(opacity * 0xff));
89 return *this;
90 }
91
92 DlBlendMode getBlendMode() const {
93 return static_cast<DlBlendMode>(blendMode_);
94 }
95 DlPaint& setBlendMode(DlBlendMode mode) {
96 blendMode_ = static_cast<unsigned>(mode);
97 return *this;
98 }
99
100 DlDrawStyle getDrawStyle() const {
101 return static_cast<DlDrawStyle>(drawStyle_);
102 }
103 DlPaint& setDrawStyle(DlDrawStyle style) {
104 drawStyle_ = static_cast<unsigned>(style);
105 return *this;
106 }
107
108 DlStrokeCap getStrokeCap() const {
109 return static_cast<DlStrokeCap>(strokeCap_);
110 }
111 DlPaint& setStrokeCap(DlStrokeCap cap) {
112 strokeCap_ = static_cast<unsigned>(cap);
113 return *this;
114 }
115
116 DlStrokeJoin getStrokeJoin() const {
117 return static_cast<DlStrokeJoin>(strokeJoin_);
118 }
119 DlPaint& setStrokeJoin(DlStrokeJoin join) {
120 strokeJoin_ = static_cast<unsigned>(join);
121 return *this;
122 }
123
124 float getStrokeWidth() const { return strokeWidth_; }
125 DlPaint& setStrokeWidth(float width) {
126 strokeWidth_ = width;
127 return *this;
128 }
129
130 float getStrokeMiter() const { return strokeMiter_; }
131 DlPaint& setStrokeMiter(float miter) {
132 strokeMiter_ = miter;
133 return *this;
134 }
135
136 std::shared_ptr<const DlColorSource> getColorSource() const {
137 return colorSource_;
138 }
139 const DlColorSource* getColorSourcePtr() const { return colorSource_.get(); }
140 DlPaint& setColorSource(std::shared_ptr<const DlColorSource> source) {
141 colorSource_ = source;
142 return *this;
143 }
144 DlPaint& setColorSource(const DlColorSource* source) {
145 colorSource_ = source ? source->shared() : nullptr;
146 return *this;
147 }
148
149 std::shared_ptr<const DlColorFilter> getColorFilter() const {
150 return colorFilter_;
151 }
152 const DlColorFilter* getColorFilterPtr() const { return colorFilter_.get(); }
153 DlPaint& setColorFilter(const std::shared_ptr<const DlColorFilter> filter) {
154 colorFilter_ = filter;
155 return *this;
156 }
157 DlPaint& setColorFilter(const DlColorFilter* filter) {
158 colorFilter_ = filter ? filter->shared() : nullptr;
159 return *this;
160 }
161
162 std::shared_ptr<const DlImageFilter> getImageFilter() const {
163 return imageFilter_;
164 }
165 const DlImageFilter* getImageFilterPtr() const { return imageFilter_.get(); }
166 DlPaint& setImageFilter(const std::shared_ptr<const DlImageFilter> filter) {
167 imageFilter_ = filter;
168 return *this;
169 }
170 DlPaint& setImageFilter(const DlImageFilter* filter) {
171 imageFilter_ = filter ? filter->shared() : nullptr;
172 return *this;
173 }
174
175 std::shared_ptr<const DlMaskFilter> getMaskFilter() const {
176 return maskFilter_;
177 }
178 const DlMaskFilter* getMaskFilterPtr() const { return maskFilter_.get(); }
179 DlPaint& setMaskFilter(std::shared_ptr<DlMaskFilter> filter) {
180 maskFilter_ = filter;
181 return *this;
182 }
183 DlPaint& setMaskFilter(const DlMaskFilter* filter) {
184 maskFilter_ = filter ? filter->shared() : nullptr;
185 return *this;
186 }
187
188 std::shared_ptr<const DlPathEffect> getPathEffect() const {
189 return pathEffect_;
190 }
191 const DlPathEffect* getPathEffectPtr() const { return pathEffect_.get(); }
192 DlPaint& setPathEffect(std::shared_ptr<DlPathEffect> pathEffect) {
193 pathEffect_ = pathEffect;
194 return *this;
195 }
196 DlPaint& setPathEffect(const DlPathEffect* effect) {
197 pathEffect_ = effect ? effect->shared() : nullptr;
198 return *this;
199 }
200
201 bool isDefault() const { return *this == kDefault; }
202
203 bool operator==(DlPaint const& other) const;
204 bool operator!=(DlPaint const& other) const { return !(*this == other); }
205
206 private:
207#define ASSERT_ENUM_FITS(last_enum, num_bits) \
208 static_assert(static_cast<int>(last_enum) < (1 << num_bits) && \
209 static_cast<int>(last_enum) * 2 >= (1 << num_bits))
210
211 static constexpr int kBlendModeBits = 5;
212 static constexpr int kDrawStyleBits = 2;
213 static constexpr int kStrokeCapBits = 2;
214 static constexpr int kStrokeJoinBits = 2;
215 ASSERT_ENUM_FITS(DlBlendMode::kLastMode, kBlendModeBits);
216 ASSERT_ENUM_FITS(DlDrawStyle::kLastStyle, kDrawStyleBits);
217 ASSERT_ENUM_FITS(DlStrokeCap::kLastCap, kStrokeCapBits);
218 ASSERT_ENUM_FITS(DlStrokeJoin::kLastJoin, kStrokeJoinBits);
219
220 union {
221 struct {
222 unsigned blendMode_ : kBlendModeBits;
223 unsigned drawStyle_ : kDrawStyleBits;
224 unsigned strokeCap_ : kStrokeCapBits;
225 unsigned strokeJoin_ : kStrokeJoinBits;
226 unsigned isAntiAlias_ : 1;
227 unsigned isDither_ : 1;
228 unsigned isInvertColors_ : 1;
229 };
230 };
231
232 DlColor color_;
233 float strokeWidth_;
234 float strokeMiter_;
235
236 std::shared_ptr<const DlColorSource> colorSource_;
237 std::shared_ptr<const DlColorFilter> colorFilter_;
238 std::shared_ptr<const DlImageFilter> imageFilter_;
239 std::shared_ptr<const DlMaskFilter> maskFilter_;
240 std::shared_ptr<const DlPathEffect> pathEffect_;
241};
242
243} // namespace flutter
244
245#endif // FLUTTER_DISPLAY_LIST_DL_PAINT_H_
246

source code of flutter_engine/flutter/display_list/dl_paint.h