forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaint_flags.h
228 lines (196 loc) · 7.66 KB
/
paint_flags.h
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CC_PAINT_PAINT_FLAGS_H_
#define CC_PAINT_PAINT_FLAGS_H_
#include <utility>
#include "base/compiler_specific.h"
#include "cc/paint/paint_export.h"
#include "third_party/skia/include/core/SkColorFilter.h"
#include "third_party/skia/include/core/SkDrawLooper.h"
#include "third_party/skia/include/core/SkMaskFilter.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkPathEffect.h"
#include "third_party/skia/include/core/SkSamplingOptions.h"
class SkCanvas;
namespace cc {
class PaintFilter;
class PaintShader;
class CC_PAINT_EXPORT PaintFlags {
public:
PaintFlags();
PaintFlags(const PaintFlags& flags);
PaintFlags(PaintFlags&& other);
~PaintFlags();
PaintFlags& operator=(const PaintFlags& other);
PaintFlags& operator=(PaintFlags&& other);
enum Style {
kFill_Style = SkPaint::kFill_Style,
kStroke_Style = SkPaint::kStroke_Style,
};
bool nothingToDraw() const;
ALWAYS_INLINE Style getStyle() const {
return static_cast<Style>(bitfields_.style_);
}
ALWAYS_INLINE void setStyle(Style style) { bitfields_.style_ = style; }
ALWAYS_INLINE SkColor getColor() const { return color_; }
ALWAYS_INLINE void setColor(SkColor color) { color_ = color; }
ALWAYS_INLINE uint8_t getAlpha() const { return SkColorGetA(color_); }
ALWAYS_INLINE void setAlpha(uint8_t a) {
color_ = SkColorSetARGB(a, SkColorGetR(color_), SkColorGetG(color_),
SkColorGetB(color_));
}
ALWAYS_INLINE void setBlendMode(SkBlendMode mode) {
blend_mode_ = static_cast<uint32_t>(mode);
}
ALWAYS_INLINE SkBlendMode getBlendMode() const {
return static_cast<SkBlendMode>(blend_mode_);
}
ALWAYS_INLINE bool isAntiAlias() const { return bitfields_.antialias_; }
ALWAYS_INLINE void setAntiAlias(bool aa) { bitfields_.antialias_ = aa; }
ALWAYS_INLINE bool isDither() const { return bitfields_.dither_; }
ALWAYS_INLINE void setDither(bool dither) { bitfields_.dither_ = dither; }
enum class FilterQuality {
kNone,
kLow,
kMedium,
kHigh,
kLast = kHigh,
};
ALWAYS_INLINE void setFilterQuality(FilterQuality quality) {
bitfields_.filter_quality_ = static_cast<int>(quality);
}
ALWAYS_INLINE FilterQuality getFilterQuality() const {
return static_cast<FilterQuality>(bitfields_.filter_quality_);
}
ALWAYS_INLINE bool useDarkModeForImage() const {
return bitfields_.use_dark_mode_for_image_;
}
ALWAYS_INLINE void setUseDarkModeForImage(bool use_dark_mode_for_image) {
bitfields_.use_dark_mode_for_image_ = use_dark_mode_for_image;
}
ALWAYS_INLINE SkScalar getStrokeWidth() const { return width_; }
ALWAYS_INLINE void setStrokeWidth(SkScalar width) { width_ = width; }
ALWAYS_INLINE SkScalar getStrokeMiter() const { return miter_limit_; }
ALWAYS_INLINE void setStrokeMiter(SkScalar miter) { miter_limit_ = miter; }
enum Cap {
kButt_Cap = SkPaint::kButt_Cap, //!< begin/end contours with no extension
kRound_Cap = SkPaint::kRound_Cap, //!< begin/end contours with a
//! semi-circle extension
kSquare_Cap = SkPaint::kSquare_Cap, //!< begin/end contours with a half
//! square extension
kLast_Cap = kSquare_Cap,
kDefault_Cap = kButt_Cap
};
ALWAYS_INLINE Cap getStrokeCap() const {
return static_cast<Cap>(bitfields_.cap_type_);
}
ALWAYS_INLINE void setStrokeCap(Cap cap) { bitfields_.cap_type_ = cap; }
enum Join {
kMiter_Join = SkPaint::kMiter_Join,
kRound_Join = SkPaint::kRound_Join,
kBevel_Join = SkPaint::kBevel_Join,
kLast_Join = kBevel_Join,
kDefault_Join = kMiter_Join
};
ALWAYS_INLINE Join getStrokeJoin() const {
return static_cast<Join>(bitfields_.join_type_);
}
ALWAYS_INLINE void setStrokeJoin(Join join) { bitfields_.join_type_ = join; }
ALWAYS_INLINE const sk_sp<SkColorFilter>& getColorFilter() const {
return color_filter_;
}
ALWAYS_INLINE void setColorFilter(sk_sp<SkColorFilter> filter) {
color_filter_ = std::move(filter);
}
ALWAYS_INLINE const sk_sp<SkMaskFilter>& getMaskFilter() const {
return mask_filter_;
}
ALWAYS_INLINE void setMaskFilter(sk_sp<SkMaskFilter> mask) {
mask_filter_ = std::move(mask);
}
ALWAYS_INLINE const PaintShader* getShader() const { return shader_.get(); }
// Returns true if the shader has been set on the flags.
ALWAYS_INLINE bool HasShader() const { return !!shader_; }
// Returns whether the shader is opaque. Note that it is only valid to call
// this function if HasShader() returns true.
bool ShaderIsOpaque() const;
void setShader(sk_sp<PaintShader> shader);
ALWAYS_INLINE const sk_sp<SkPathEffect>& getPathEffect() const {
return path_effect_;
}
ALWAYS_INLINE void setPathEffect(sk_sp<SkPathEffect> effect) {
path_effect_ = std::move(effect);
}
bool getFillPath(const SkPath& src,
SkPath* dst,
const SkRect* cull_rect = nullptr,
SkScalar res_scale = 1) const;
ALWAYS_INLINE const sk_sp<PaintFilter>& getImageFilter() const {
return image_filter_;
}
void setImageFilter(sk_sp<PaintFilter> filter);
ALWAYS_INLINE const sk_sp<SkDrawLooper>& getLooper() const {
return draw_looper_;
}
ALWAYS_INLINE void setLooper(sk_sp<SkDrawLooper> looper) {
draw_looper_ = std::move(looper);
}
// Returns true if this just represents an opacity blend when used as
// saveLayer flags, thus the saveLayer can be converted to a saveLayerAlpha.
bool IsSimpleOpacity() const;
// Returns true if this (of a drawOp) allows the sequence
// saveLayerAlpha/drawOp/restore to be folded into a single drawOp by baking
// the alpha in the saveLayerAlpha into the flags of the drawOp.
bool SupportsFoldingAlpha() const;
// SkPaint does not support loopers, so callers of SkToPaint need
// to check for loopers manually (see getLooper()).
SkPaint ToSkPaint() const;
template <typename Proc>
void DrawToSk(SkCanvas* canvas, Proc proc) const {
SkPaint paint = ToSkPaint();
if (const sk_sp<SkDrawLooper>& looper = getLooper())
looper->apply(canvas, paint, proc);
else
proc(canvas, paint);
}
static SkSamplingOptions FilterQualityToSkSamplingOptions(
FilterQuality filter_quality);
bool IsValid() const;
bool operator==(const PaintFlags& other) const;
bool operator!=(const PaintFlags& other) const { return !(*this == other); }
bool HasDiscardableImages() const;
size_t GetSerializedSize() const;
private:
friend class PaintOpReader;
friend class PaintOpWriter;
sk_sp<SkPathEffect> path_effect_;
sk_sp<PaintShader> shader_;
sk_sp<SkMaskFilter> mask_filter_;
sk_sp<SkColorFilter> color_filter_;
sk_sp<SkDrawLooper> draw_looper_;
sk_sp<PaintFilter> image_filter_;
// Match(ish) SkPaint defaults. SkPaintDefaults is not public, so this
// just uses these values and ignores any SkUserConfig overrides.
SkColor color_ = SK_ColorBLACK;
float width_ = 0.f;
float miter_limit_ = 4.f;
uint32_t blend_mode_ = static_cast<uint32_t>(SkBlendMode::kSrcOver);
struct PaintFlagsBitfields {
uint32_t antialias_ : 1;
uint32_t dither_ : 1;
uint32_t cap_type_ : 2;
uint32_t join_type_ : 2;
uint32_t style_ : 2;
uint32_t filter_quality_ : 2;
// Specifies whether the compositor should use a dark mode filter when
// rasterizing image on the draw op with this PaintFlags.
uint32_t use_dark_mode_for_image_ : 1;
};
union {
PaintFlagsBitfields bitfields_;
uint32_t bitfields_uint_;
};
};
} // namespace cc
#endif // CC_PAINT_PAINT_FLAGS_H_