xref: /aosp_15_r20/external/skia/src/core/SkRecordPattern.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 
11 #include "include/private/base/SkTLogic.h"
12 #include "src/core/SkRecord.h"
13 
14 namespace 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.
20 template <typename T>
21 class Is {
22 public:
Is()23     Is() : fPtr(nullptr) {}
24 
25     typedef T type;
get()26     type* get() { return fPtr; }
27 
operator()28     bool operator()(T* ptr) {
29         fPtr = ptr;
30         return true;
31     }
32 
33     template <typename U>
operator()34     bool operator()(U*) {
35         fPtr = nullptr;
36         return false;
37     }
38 
39 private:
40     type* fPtr;
41 };
42 
43 // Matches any command that draws, and stores its paint.
44 class IsDraw {
45 public:
IsDraw()46     IsDraw() : fPaint(nullptr) {}
47 
get()48     SkPaint* get() { return fPaint; }
49 
50     template <typename T>
51     std::enable_if_t<(T::kTags & kDrawWithPaint_Tag) == kDrawWithPaint_Tag, bool>
operator()52     operator()(T* draw) {
53         fPaint = AsPtr(draw->paint);
54         return true;
55     }
56 
57     template <typename T>
operator()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>
operator()64     std::enable_if_t<!(T::kTags & kDraw_Tag), bool> operator()(T* draw) {
65         fPaint = nullptr;
66         return false;
67     }
68 
69 private:
70     // Abstracts away whether the paint is always part of the command or optional.
AsPtr(SkRecords::Optional<T> & x)71     template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; }
AsPtr(T & 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.
78 class IsSingleDraw {
79 public:
IsSingleDraw()80     IsSingleDraw() : fPaint(nullptr) {}
81 
get()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>
operator()88     operator()(T* draw) {
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>
operator()97     operator()(T* draw) {
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>
operator()104     operator()(T* draw) {
105         fPaint = nullptr;
106         return false;
107     }
108 
109 private:
110     // Abstracts away whether the paint is always part of the command or optional.
AsPtr(SkRecords::Optional<T> & x)111     template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; }
AsPtr(T & 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.
118 template <typename Matcher>
119 struct Not {
120     template <typename T>
operatorNot121     bool operator()(T* ptr) { return !Matcher()(ptr); }
122 };
123 
124 // Matches if any of First or Rest... does.  Stores nothing.
125 template <typename First, typename... Rest>
126 struct Or {
127     template <typename T>
operatorOr128     bool operator()(T* ptr) { return First()(ptr) || Or<Rest...>()(ptr); }
129 };
130 template <typename First>
131 struct 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.
138 template <typename Matcher>
139 struct 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 
150 template <typename... Matchers> class Pattern;
151 
152 template <> class Pattern<> {
153 public:
154     // Bottoms out recursion.  Just return whatever i the front decided on.
155     int match(SkRecord*, int i) { return i; }
156 };
157 
158 template <typename First, typename... Rest>
159 class Pattern<First, Rest...> {
160 public:
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 
186 private:
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
217