mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 8 | #ifndef SkRecordPattern_DEFINED |
| 9 | #define SkRecordPattern_DEFINED |
| 10 | |
| 11 | #include "SkTLogic.h" |
| 12 | |
| 13 | namespace 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. |
| 19 | template <typename T> |
| 20 | class Is { |
| 21 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 22 | Is() : fPtr(nullptr) {} |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 23 | |
| 24 | typedef T type; |
| 25 | type* get() { return fPtr; } |
| 26 | |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 27 | bool operator()(T* ptr) { |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 28 | fPtr = ptr; |
| 29 | return true; |
| 30 | } |
| 31 | |
| 32 | template <typename U> |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 33 | bool operator()(U*) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 34 | fPtr = nullptr; |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 35 | return false; |
| 36 | } |
| 37 | |
| 38 | private: |
| 39 | type* fPtr; |
| 40 | }; |
| 41 | |
| 42 | // Matches any command that draws, and stores its paint. |
| 43 | class IsDraw { |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 44 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 45 | IsDraw() : fPaint(nullptr) {} |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 46 | |
| 47 | typedef SkPaint type; |
| 48 | type* get() { return fPaint; } |
| 49 | |
| 50 | template <typename T> |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 51 | SK_WHEN(T::kTags & kDraw_Tag, bool) operator()(T* draw) { |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 52 | fPaint = AsPtr(draw->paint); |
| 53 | return true; |
| 54 | } |
| 55 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 56 | bool operator()(DrawDrawable*) { |
| 57 | static_assert(DrawDrawable::kTags & kDraw_Tag, ""); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | fPaint = nullptr; |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 59 | return true; |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 60 | } |
| 61 | |
mtklein | 449d9b7 | 2015-09-28 10:33:02 -0700 | [diff] [blame] | 62 | template <typename T> |
| 63 | SK_WHEN(!(T::kTags & kDraw_Tag), bool) operator()(T* draw) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 64 | fPaint = nullptr; |
commit-bot@chromium.org | 1b54646 | 2014-05-06 21:32:19 +0000 | [diff] [blame] | 65 | return false; |
| 66 | } |
| 67 | |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 68 | private: |
| 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. |
| 77 | template <typename Matcher> |
| 78 | struct Not { |
| 79 | template <typename T> |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 80 | bool operator()(T* ptr) { return !Matcher()(ptr); } |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 83 | // Matches if any of First or Rest... does. Stores nothing. |
| 84 | template <typename First, typename... Rest> |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 85 | struct Or { |
| 86 | template <typename T> |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 87 | bool operator()(T* ptr) { return First()(ptr) || Or<Rest...>()(ptr); } |
| 88 | }; |
| 89 | template <typename First> |
| 90 | struct Or<First> { |
| 91 | template <typename T> |
| 92 | bool operator()(T* ptr) { return First()(ptr); } |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 95 | |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 96 | // Greedy is a special matcher that greedily matches Matcher 0 or more times. Stores nothing. |
commit-bot@chromium.org | 888e468 | 2014-05-08 18:58:32 +0000 | [diff] [blame] | 97 | template <typename Matcher> |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 98 | struct Greedy { |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 99 | template <typename T> |
commit-bot@chromium.org | c71da1f | 2014-05-07 21:16:09 +0000 | [diff] [blame] | 100 | bool operator()(T* ptr) { return Matcher()(ptr); } |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 103 | // Pattern matches each of its matchers in order. |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 104 | // |
| 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; |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 107 | // - first, second, third, ... return the data stored by their respective matchers in the pattern. |
| 108 | |
| 109 | template <typename... Matchers> class Pattern; |
| 110 | |
| 111 | template <> class Pattern<> { |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 112 | public: |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 113 | // Bottoms out recursion. Just return whatever i the front decided on. |
| 114 | int match(SkRecord*, int i) { return i; } |
| 115 | }; |
| 116 | |
| 117 | template <typename First, typename... Rest> |
| 118 | class Pattern<First, Rest...> { |
| 119 | public: |
| 120 | // If this pattern matches the SkRecord starting from i, |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 121 | // return the index just past the end of the pattern, otherwise return 0. |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 122 | SK_ALWAYS_INLINE int match(SkRecord* record, int i) { |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 123 | i = this->matchFirst(&fFirst, record, i); |
| 124 | return i > 0 ? fRest.match(record, i) : 0; |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 125 | } |
| 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). |
mtklein | c6ad06a | 2015-08-19 09:51:00 -0700 | [diff] [blame] | 129 | SK_ALWAYS_INLINE bool search(SkRecord* record, int* begin, int* end) { |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 130 | 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 | |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 139 | // 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.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 144 | |
| 145 | private: |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 146 | // If first isn't a Greedy, try to match at i once. |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 147 | template <typename T> |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 148 | int matchFirst(T* first, SkRecord* record, int i) { |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 149 | if (i < record->count()) { |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 150 | if (record->mutate(i, *first)) { |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 151 | return i+1; |
| 152 | } |
| 153 | } |
| 154 | return 0; |
| 155 | } |
| 156 | |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 157 | // If first is a Greedy, walk i until it doesn't match. |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 158 | template <typename T> |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 159 | int matchFirst(Greedy<T>* first, SkRecord* record, int i) { |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 160 | while (i < record->count()) { |
mtklein | 343a63d | 2016-03-22 11:46:53 -0700 | [diff] [blame] | 161 | if (!record->mutate(i, *first)) { |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 162 | return i; |
| 163 | } |
| 164 | i++; |
| 165 | } |
| 166 | return 0; |
| 167 | } |
| 168 | |
mtklein | 24e7db8 | 2015-11-19 08:53:27 -0800 | [diff] [blame] | 169 | First fFirst; |
| 170 | Pattern<Rest...> fRest; |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
commit-bot@chromium.org | 73fffeb | 2014-05-05 21:59:52 +0000 | [diff] [blame] | 173 | } // namespace SkRecords |
| 174 | |
| 175 | #endif//SkRecordPattern_DEFINED |