Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
dl_path_effect.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_EFFECTS_DL_PATH_EFFECT_H_
6#define FLUTTER_DISPLAY_LIST_EFFECTS_DL_PATH_EFFECT_H_
7
8#include <optional>
9
10#include "flutter/display_list/dl_attributes.h"
11#include "flutter/fml/logging.h"
13
14namespace flutter {
15
16class DlDashPathEffect;
17
18// The DisplayList PathEffect class. This class implements all of the
19// facilities and adheres to the design goals of the |DlAttribute| base
20// class.
21
22// An enumerated type for the supported PathEffect operations.
23enum class DlPathEffectType {
24 kDash,
25};
26
27class DlPathEffect : public DlAttribute<DlPathEffect, DlPathEffectType> {
28 public:
29 virtual const DlDashPathEffect* asDash() const { return nullptr; }
30
31 virtual std::optional<SkRect> effect_bounds(SkRect&) const = 0;
32
33 protected:
34 DlPathEffect() = default;
35
36 private:
38};
39
40/// The DashPathEffect which breaks a path up into dash segments, and it
41/// only affects stroked paths.
42/// intervals: array containing an even number of entries (>=2), with
43/// the even indices specifying the length of "on" intervals, and the odd
44/// indices specifying the length of "off" intervals. This array will be
45/// copied in Make, and can be disposed of freely after.
46/// count: number of elements in the intervals array.
47/// phase: initial distance into the intervals at which to start the dashing
48/// effect for the path.
49///
50/// For example: if intervals[] = {10, 20}, count = 2, and phase = 25,
51/// this will set up a dashed path like so:
52/// 5 pixels off
53/// 10 pixels on
54/// 20 pixels off
55/// 10 pixels on
56/// 20 pixels off
57/// ...
58/// A phase of -5, 25, 55, 85, etc. would all result in the same path,
59/// because the sum of all the intervals is 30.
60///
61class DlDashPathEffect final : public DlPathEffect {
62 public:
63 static std::shared_ptr<DlPathEffect> Make(const SkScalar intervals[],
64 int count,
66
67 DlPathEffectType type() const override { return DlPathEffectType::kDash; }
68 size_t size() const override {
69 return sizeof(*this) + sizeof(SkScalar) * count_;
70 }
71
72 std::shared_ptr<DlPathEffect> shared() const override {
73 return Make(intervals(), count_, phase_);
74 }
75
76 const DlDashPathEffect* asDash() const override { return this; }
77
78 const SkScalar* intervals() const {
79 return reinterpret_cast<const SkScalar*>(this + 1);
80 }
81 int count() const { return count_; }
82 SkScalar phase() const { return phase_; }
83
84 std::optional<SkRect> effect_bounds(SkRect& rect) const override;
85
86 protected:
87 bool equals_(DlPathEffect const& other) const override {
89 auto that = static_cast<DlDashPathEffect const*>(&other);
90 return count_ == that->count_ && phase_ == that->phase_ &&
91 memcmp(intervals(), that->intervals(), sizeof(SkScalar) * count_) ==
92 0;
93 }
94
95 private:
96 // DlDashPathEffect constructor assumes the caller has prealloced storage for
97 // the intervals. If the intervals is nullptr the intervals will
98 // uninitialized.
100 : count_(count), phase_(phase) {
101 if (intervals != nullptr) {
102 SkScalar* intervals_ = reinterpret_cast<SkScalar*>(this + 1);
103 memcpy(intervals_, intervals, sizeof(SkScalar) * count);
104 }
105 }
106
107 explicit DlDashPathEffect(const DlDashPathEffect* dash_effect)
108 : DlDashPathEffect(dash_effect->intervals(),
109 dash_effect->count_,
110 dash_effect->phase_) {}
111
112 SkScalar* intervals_unsafe() { return reinterpret_cast<SkScalar*>(this + 1); }
113
114 int count_;
115 SkScalar phase_;
116
117 friend class DisplayListBuilder;
118 friend class DlPathEffect;
119
121};
122
123} // namespace flutter
124
125#endif // FLUTTER_DISPLAY_LIST_EFFECTS_DL_PATH_EFFECT_H_
virtual T type() const =0
std::shared_ptr< DlPathEffect > shared() const override
static std::shared_ptr< DlPathEffect > Make(const SkScalar intervals[], int count, SkScalar phase)
const DlDashPathEffect * asDash() const override
size_t size() const override
DlPathEffectType type() const override
bool equals_(DlPathEffect const &other) const override
std::optional< SkRect > effect_bounds(SkRect &rect) const override
const SkScalar * intervals() const
virtual const DlDashPathEffect * asDash() const
virtual std::optional< SkRect > effect_bounds(SkRect &) const =0
float SkScalar
Definition extension.cpp:12
#define FML_DCHECK(condition)
Definition logging.h:103
#define FML_DISALLOW_COPY_ASSIGN_AND_MOVE(TypeName)
Definition macros.h:31