Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkRecordPattern.h
Go to the documentation of this file.
1/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifndef SkRecordPattern_DEFINED
9#define SkRecordPattern_DEFINED
10
12#include "src/core/SkRecord.h"
13
14namespace SkRecords {
15
16// First, some matchers. These match a single command in the SkRecord,
17// and may hang onto some data from it. If so, you can get the data by calling .get().
18
19// Matches a command of type T, and stores that command.
20template <typename T>
21class Is {
22public:
23 Is() : fPtr(nullptr) {}
24
25 typedef T type;
26 type* get() { return fPtr; }
27
28 bool operator()(T* ptr) {
29 fPtr = ptr;
30 return true;
31 }
32
33 template <typename U>
34 bool operator()(U*) {
35 fPtr = nullptr;
36 return false;
37 }
38
39private:
40 type* fPtr;
41};
42
43// Matches any command that draws, and stores its paint.
44class IsDraw {
45public:
46 IsDraw() : fPaint(nullptr) {}
47
48 SkPaint* get() { return fPaint; }
49
50 template <typename T>
51 std::enable_if_t<(T::kTags & kDrawWithPaint_Tag) == kDrawWithPaint_Tag, bool>
52 operator()(T* draw) {
53 fPaint = AsPtr(draw->paint);
54 return true;
55 }
56
57 template <typename T>
58 std::enable_if_t<(T::kTags & kDrawWithPaint_Tag) == kDraw_Tag, bool> operator()(T* draw) {
59 fPaint = nullptr;
60 return true;
61 }
62
63 template <typename T>
64 std::enable_if_t<!(T::kTags & kDraw_Tag), bool> operator()(T* draw) {
65 fPaint = nullptr;
66 return false;
67 }
68
69private:
70 // Abstracts away whether the paint is always part of the command or optional.
71 template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; }
72 template <typename T> static T* AsPtr(T& x) { return &x; }
73
74 SkPaint* fPaint;
75};
76
77// Matches any command that draws *once* (logically), and stores its paint.
79public:
80 IsSingleDraw() : fPaint(nullptr) {}
81
82 SkPaint* get() { return fPaint; }
83
84 template <typename T>
85 std::enable_if_t<(T::kTags & kDrawWithPaint_Tag) == kDrawWithPaint_Tag &&
86 !(T::kTags & kMultiDraw_Tag),
87 bool>
89 fPaint = AsPtr(draw->paint);
90 return true;
91 }
92
93 template <typename T>
94 std::enable_if_t<(T::kTags & kDrawWithPaint_Tag) == kDraw_Tag &&
95 !(T::kTags & kMultiDraw_Tag),
96 bool>
98 fPaint = nullptr;
99 return true;
100 }
101
102 template <typename T>
103 std::enable_if_t<!(T::kTags & kDraw_Tag) || (T::kTags & kMultiDraw_Tag), bool>
105 fPaint = nullptr;
106 return false;
107 }
108
109private:
110 // Abstracts away whether the paint is always part of the command or optional.
111 template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; }
112 template <typename T> static T* AsPtr(T& x) { return &x; }
113
114 SkPaint* fPaint;
115};
116
117// Matches if Matcher doesn't. Stores nothing.
118template <typename Matcher>
119struct Not {
120 template <typename T>
121 bool operator()(T* ptr) { return !Matcher()(ptr); }
122};
123
124// Matches if any of First or Rest... does. Stores nothing.
125template <typename First, typename... Rest>
126struct Or {
127 template <typename T>
128 bool operator()(T* ptr) { return First()(ptr) || Or<Rest...>()(ptr); }
129};
130template <typename First>
131struct Or<First> {
132 template <typename T>
133 bool operator()(T* ptr) { return First()(ptr); }
134};
135
136
137// Greedy is a special matcher that greedily matches Matcher 0 or more times. Stores nothing.
138template <typename Matcher>
139struct Greedy {
140 template <typename T>
141 bool operator()(T* ptr) { return Matcher()(ptr); }
142};
143
144// Pattern matches each of its matchers in order.
145//
146// This is the main entry point to pattern matching, and so provides a couple of extra API bits:
147// - search scans through the record to look for matches;
148// - first, second, third, ... return the data stored by their respective matchers in the pattern.
149
150template <typename... Matchers> class Pattern;
151
152template <> class Pattern<> {
153public:
154 // Bottoms out recursion. Just return whatever i the front decided on.
155 int match(SkRecord*, int i) { return i; }
156};
157
158template <typename First, typename... Rest>
159class Pattern<First, Rest...> {
160public:
161 // If this pattern matches the SkRecord starting from i,
162 // return the index just past the end of the pattern, otherwise return 0.
163 SK_ALWAYS_INLINE int match(SkRecord* record, int i) {
164 i = this->matchFirst(&fFirst, record, i);
165 return i > 0 ? fRest.match(record, i) : 0;
166 }
167
168 // Starting from *end, walk through the SkRecord to find the first span matching this pattern.
169 // If there is no such span, return false. If there is, return true and set [*begin, *end).
170 SK_ALWAYS_INLINE bool search(SkRecord* record, int* begin, int* end) {
171 for (*begin = *end; *begin < record->count(); ++(*begin)) {
172 *end = this->match(record, *begin);
173 if (*end != 0) {
174 return true;
175 }
176 }
177 return false;
178 }
179
180 // TODO: some sort of smart get<i>()
181 template <typename T> T* first() { return fFirst.get(); }
182 template <typename T> T* second() { return fRest.template first<T>(); }
183 template <typename T> T* third() { return fRest.template second<T>(); }
184 template <typename T> T* fourth() { return fRest.template third<T>(); }
185
186private:
187 // If first isn't a Greedy, try to match at i once.
188 template <typename T>
189 int matchFirst(T* first, SkRecord* record, int i) {
190 if (i < record->count()) {
191 if (record->mutate(i, *first)) {
192 return i+1;
193 }
194 }
195 return 0;
196 }
197
198 // If first is a Greedy, walk i until it doesn't match.
199 template <typename T>
200 int matchFirst(Greedy<T>* first, SkRecord* record, int i) {
201 while (i < record->count()) {
202 if (!record->mutate(i, *first)) {
203 return i;
204 }
205 i++;
206 }
207 return 0;
208 }
209
210 First fFirst;
211 Pattern<Rest...> fRest;
212};
213
214} // namespace SkRecords
215
216#endif//SkRecordPattern_DEFINED
static bool match(const char *needle, const char *haystack)
Definition DM.cpp:1132
#define SK_ALWAYS_INLINE
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition aaclip.cpp:27
auto mutate(int i, F &&f) -> decltype(f((SkRecords::NoOp *) nullptr))
Definition SkRecord.h:54
int count() const
Definition SkRecord.h:38
std::enable_if_t<(T::kTags &kDrawWithPaint_Tag)==kDrawWithPaint_Tag &&!(T::kTags &kMultiDraw_Tag), bool > operator()(T *draw)
std::enable_if_t<(T::kTags &kDrawWithPaint_Tag)==kDraw_Tag &&!(T::kTags &kMultiDraw_Tag), bool > operator()(T *draw)
std::enable_if_t<!(T::kTags &kDraw_Tag)||(T::kTags &kMultiDraw_Tag), bool > operator()(T *draw)
bool operator()(T *ptr)
bool operator()(U *)
SK_ALWAYS_INLINE int match(SkRecord *record, int i)
SK_ALWAYS_INLINE bool search(SkRecord *record, int *begin, int *end)
int match(SkRecord *, int i)
static const char * begin(const StringSlice &s)
Definition editor.cpp:252
glong glong end
double x
sk_sp< const sktext::gpu::Slug > slug kDraw_Tag kHasImage_Tag kHasPaint_Tag kMultiDraw_Tag
Definition SkRecords.h:329
@ kDrawWithPaint_Tag
Definition SkRecords.h:166
unsigned useCenter kDraw_Tag
Definition SkRecords.h:257
#define T
bool operator()(T *ptr)
bool operator()(T *ptr)
bool operator()(T *ptr)