blob: 45f45724c199596972772cf1b4f3df71ef212693 [file] [log] [blame]
mtkleinc6ad06a2015-08-19 09:51:00 -07001/*
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
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +00008#ifndef SkRecordPattern_DEFINED
9#define SkRecordPattern_DEFINED
10
11#include "SkTLogic.h"
12
13namespace SkRecords {
14
15// First, some matchers. These match a single command in the SkRecord,
16// and may hang onto some data from it. If so, you can get the data by calling .get().
17
18// Matches a command of type T, and stores that command.
19template <typename T>
20class Is {
21public:
halcanary96fcdcc2015-08-27 07:41:13 -070022 Is() : fPtr(nullptr) {}
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000023
24 typedef T type;
25 type* get() { return fPtr; }
26
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000027 bool operator()(T* ptr) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000028 fPtr = ptr;
29 return true;
30 }
31
32 template <typename U>
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000033 bool operator()(U*) {
halcanary96fcdcc2015-08-27 07:41:13 -070034 fPtr = nullptr;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000035 return false;
36 }
37
38private:
39 type* fPtr;
40};
41
42// Matches any command that draws, and stores its paint.
43class IsDraw {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000044public:
halcanary96fcdcc2015-08-27 07:41:13 -070045 IsDraw() : fPaint(nullptr) {}
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000046
47 typedef SkPaint type;
48 type* get() { return fPaint; }
49
50 template <typename T>
mtklein449d9b72015-09-28 10:33:02 -070051 SK_WHEN(T::kTags & kDraw_Tag, bool) operator()(T* draw) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000052 fPaint = AsPtr(draw->paint);
53 return true;
54 }
55
mtklein449d9b72015-09-28 10:33:02 -070056 bool operator()(DrawDrawable*) {
57 static_assert(DrawDrawable::kTags & kDraw_Tag, "");
halcanary96fcdcc2015-08-27 07:41:13 -070058 fPaint = nullptr;
mtklein449d9b72015-09-28 10:33:02 -070059 return true;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000060 }
61
mtklein449d9b72015-09-28 10:33:02 -070062 template <typename T>
63 SK_WHEN(!(T::kTags & kDraw_Tag), bool) operator()(T* draw) {
halcanary96fcdcc2015-08-27 07:41:13 -070064 fPaint = nullptr;
commit-bot@chromium.org1b546462014-05-06 21:32:19 +000065 return false;
66 }
67
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000068private:
69 // Abstracts away whether the paint is always part of the command or optional.
70 template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; }
71 template <typename T> static T* AsPtr(T& x) { return &x; }
72
73 type* fPaint;
74};
75
76// Matches if Matcher doesn't. Stores nothing.
77template <typename Matcher>
78struct Not {
79 template <typename T>
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +000080 bool operator()(T* ptr) { return !Matcher()(ptr); }
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000081};
82
mtklein24e7db82015-11-19 08:53:27 -080083// Matches if any of First or Rest... does. Stores nothing.
84template <typename First, typename... Rest>
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000085struct Or {
86 template <typename T>
mtklein24e7db82015-11-19 08:53:27 -080087 bool operator()(T* ptr) { return First()(ptr) || Or<Rest...>()(ptr); }
88};
89template <typename First>
90struct Or<First> {
91 template <typename T>
92 bool operator()(T* ptr) { return First()(ptr); }
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000093};
94
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000095
mtklein24e7db82015-11-19 08:53:27 -080096// Greedy is a special matcher that greedily matches Matcher 0 or more times. Stores nothing.
commit-bot@chromium.org888e4682014-05-08 18:58:32 +000097template <typename Matcher>
mtklein24e7db82015-11-19 08:53:27 -080098struct Greedy {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +000099 template <typename T>
commit-bot@chromium.orgc71da1f2014-05-07 21:16:09 +0000100 bool operator()(T* ptr) { return Matcher()(ptr); }
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000101};
102
mtklein24e7db82015-11-19 08:53:27 -0800103// Pattern matches each of its matchers in order.
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000104//
105// This is the main entry point to pattern matching, and so provides a couple of extra API bits:
106// - search scans through the record to look for matches;
mtklein24e7db82015-11-19 08:53:27 -0800107// - first, second, third, ... return the data stored by their respective matchers in the pattern.
108
109template <typename... Matchers> class Pattern;
110
111template <> class Pattern<> {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000112public:
mtklein24e7db82015-11-19 08:53:27 -0800113 // Bottoms out recursion. Just return whatever i the front decided on.
114 int match(SkRecord*, int i) { return i; }
115};
116
117template <typename First, typename... Rest>
118class Pattern<First, Rest...> {
119public:
120 // If this pattern matches the SkRecord starting from i,
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000121 // return the index just past the end of the pattern, otherwise return 0.
mtkleinc6ad06a2015-08-19 09:51:00 -0700122 SK_ALWAYS_INLINE int match(SkRecord* record, int i) {
mtklein24e7db82015-11-19 08:53:27 -0800123 i = this->matchFirst(&fFirst, record, i);
124 return i > 0 ? fRest.match(record, i) : 0;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000125 }
126
127 // Starting from *end, walk through the SkRecord to find the first span matching this pattern.
128 // If there is no such span, return false. If there is, return true and set [*begin, *end).
mtkleinc6ad06a2015-08-19 09:51:00 -0700129 SK_ALWAYS_INLINE bool search(SkRecord* record, int* begin, int* end) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000130 for (*begin = *end; *begin < record->count(); ++(*begin)) {
131 *end = this->match(record, *begin);
132 if (*end != 0) {
133 return true;
134 }
135 }
136 return false;
137 }
138
mtklein24e7db82015-11-19 08:53:27 -0800139 // TODO: some sort of smart get<i>()
140 template <typename T> T* first() { return fFirst.get(); }
141 template <typename T> T* second() { return fRest.template first<T>(); }
142 template <typename T> T* third() { return fRest.template second<T>(); }
143 template <typename T> T* fourth() { return fRest.template third<T>(); }
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000144
145private:
mtklein24e7db82015-11-19 08:53:27 -0800146 // If first isn't a Greedy, try to match at i once.
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000147 template <typename T>
mtklein24e7db82015-11-19 08:53:27 -0800148 int matchFirst(T* first, SkRecord* record, int i) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000149 if (i < record->count()) {
mtklein343a63d2016-03-22 11:46:53 -0700150 if (record->mutate(i, *first)) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000151 return i+1;
152 }
153 }
154 return 0;
155 }
156
mtklein24e7db82015-11-19 08:53:27 -0800157 // If first is a Greedy, walk i until it doesn't match.
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000158 template <typename T>
mtklein24e7db82015-11-19 08:53:27 -0800159 int matchFirst(Greedy<T>* first, SkRecord* record, int i) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000160 while (i < record->count()) {
mtklein343a63d2016-03-22 11:46:53 -0700161 if (!record->mutate(i, *first)) {
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000162 return i;
163 }
164 i++;
165 }
166 return 0;
167 }
168
mtklein24e7db82015-11-19 08:53:27 -0800169 First fFirst;
170 Pattern<Rest...> fRest;
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000171};
172
commit-bot@chromium.org73fffeb2014-05-05 21:59:52 +0000173} // namespace SkRecords
174
175#endif//SkRecordPattern_DEFINED