Flutter Engine
 
Loading...
Searching...
No Matches
dl_sk_paint_dispatcher.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_SKIA_DL_SK_PAINT_DISPATCHER_H_
6#define FLUTTER_DISPLAY_LIST_SKIA_DL_SK_PAINT_DISPATCHER_H_
7
10
11namespace flutter {
12
13// A utility class that will monitor the DlOpReceiver methods relating
14// to the rendering attributes and accumulate them into an SkPaint
15// which can be accessed at any time via paint().
16class DlSkPaintDispatchHelper : public virtual DlOpReceiver {
17 public:
18 explicit DlSkPaintDispatchHelper(SkScalar opacity = SK_Scalar1)
19 : current_color_(DlColor::kBlack()), opacity_(opacity) {
20 if (opacity < SK_Scalar1) {
21 paint_.setAlphaf(opacity);
22 }
23 }
24
25 void setAntiAlias(bool aa) override;
26 void setDrawStyle(DlDrawStyle style) override;
27 void setColor(DlColor color) override;
28 void setStrokeWidth(SkScalar width) override;
29 void setStrokeMiter(SkScalar limit) override;
30 void setStrokeCap(DlStrokeCap cap) override;
31 void setStrokeJoin(DlStrokeJoin join) override;
32 void setColorSource(const DlColorSource* source) override;
33 void setColorFilter(const DlColorFilter* filter) override;
34 void setInvertColors(bool invert) override;
35 void setBlendMode(DlBlendMode mode) override;
36 void setMaskFilter(const DlMaskFilter* filter) override;
37 void setImageFilter(const DlImageFilter* filter) override;
38
39 const SkPaint& paint(bool uses_shader = true) {
40 // On the Impeller backend, we will only support dithering of *gradients*,
41 // and it will be enabled by default (without the option to disable it).
42 // Until Skia support is completely removed, we only want to respect the
43 // dither flag for gradients (otherwise it will also apply to, for
44 // example, image ops and image sources, which are not dithered in
45 // Impeller) and only on those operations that use the color source.
46 //
47 // The color_source_gradient_ flag lets us know if the color source is
48 // a gradient and then the uses_shader flag passed in to this method by
49 // the rendering op lets us know if the color source is used (and is
50 // therefore the primary source of colors for the op).
51 //
52 // See https://github.com/flutter/flutter/issues/112498.
53 paint_.setDither(uses_shader && color_source_gradient_);
54 return paint_;
55 }
56 /// Returns the current opacity attribute which is used to reduce
57 /// the alpha of all setColor calls encountered in the streeam
58 SkScalar opacity() { return opacity_; }
59 /// Returns the combined opacity that includes both the current
60 /// opacity attribute and the alpha of the most recent color.
61 /// The most recently set color will have combined the two and
62 /// stored the combined value in the alpha of the paint.
63 SkScalar combined_opacity() { return paint_.getAlphaf(); }
64 /// Returns true iff the current opacity attribute is not opaque,
65 /// irrespective of the alpha of the current color
66 bool has_opacity() { return opacity_ < SK_Scalar1; }
67
68 protected:
69 void save_opacity(SkScalar opacity_for_children);
70 void restore_opacity();
71
72 private:
73 SkPaint paint_;
74 bool color_source_gradient_ = false;
75 bool invert_colors_ = false;
76 sk_sp<SkColorFilter> sk_color_filter_;
77
78 sk_sp<SkColorFilter> makeColorFilter() const;
79
80 struct SaveInfo {
81 explicit SaveInfo(SkScalar opacity) : opacity(opacity) {}
82
83 SkScalar opacity;
84 };
85 std::vector<SaveInfo> save_stack_;
86
87 void set_opacity(SkScalar opacity) {
88 if (opacity_ != opacity) {
89 opacity_ = opacity;
90 setColor(current_color_);
91 }
92 }
93
94 DlColor current_color_;
95 SkScalar opacity_;
96};
97
98} // namespace flutter
99
100#endif // FLUTTER_DISPLAY_LIST_SKIA_DL_SK_PAINT_DISPATCHER_H_
Internal API for rendering recorded display lists to backends.
void setStrokeMiter(SkScalar limit) override
void setDrawStyle(DlDrawStyle style) override
void setMaskFilter(const DlMaskFilter *filter) override
void setColorFilter(const DlColorFilter *filter) override
DlSkPaintDispatchHelper(SkScalar opacity=SK_Scalar1)
const SkPaint & paint(bool uses_shader=true)
void setStrokeCap(DlStrokeCap cap) override
void setBlendMode(DlBlendMode mode) override
void setColorSource(const DlColorSource *source) override
void setStrokeWidth(SkScalar width) override
void save_opacity(SkScalar opacity_for_children)
void setImageFilter(const DlImageFilter *filter) override
void setColor(DlColor color) override
void setInvertColors(bool invert) override
void setStrokeJoin(DlStrokeJoin join) override
DlStrokeJoin
Definition dl_paint.h:37
DlStrokeCap
Definition dl_paint.h:28
DlDrawStyle
Definition dl_paint.h:19
BlendMode
Definition color.h:58
flutter::DlColor DlColor
int32_t width