SkRecordPattern.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #ifndef SkRecordPattern_DEFINED
  8. #define SkRecordPattern_DEFINED
  9. #include "include/private/SkTLogic.h"
  10. #include "src/core/SkRecord.h"
  11. namespace SkRecords {
  12. // First, some matchers. These match a single command in the SkRecord,
  13. // and may hang onto some data from it. If so, you can get the data by calling .get().
  14. // Matches a command of type T, and stores that command.
  15. template <typename T>
  16. class Is {
  17. public:
  18. Is() : fPtr(nullptr) {}
  19. typedef T type;
  20. type* get() { return fPtr; }
  21. bool operator()(T* ptr) {
  22. fPtr = ptr;
  23. return true;
  24. }
  25. template <typename U>
  26. bool operator()(U*) {
  27. fPtr = nullptr;
  28. return false;
  29. }
  30. private:
  31. type* fPtr;
  32. };
  33. // Matches any command that draws, and stores its paint.
  34. class IsDraw {
  35. public:
  36. IsDraw() : fPaint(nullptr) {}
  37. typedef SkPaint type;
  38. type* get() { return fPaint; }
  39. template <typename T>
  40. SK_WHEN((T::kTags & kDrawWithPaint_Tag) == kDrawWithPaint_Tag, bool) operator()(T* draw) {
  41. fPaint = AsPtr(draw->paint);
  42. return true;
  43. }
  44. template <typename T>
  45. SK_WHEN((T::kTags & kDrawWithPaint_Tag) == kDraw_Tag, bool) operator()(T* draw) {
  46. fPaint = nullptr;
  47. return true;
  48. }
  49. template <typename T>
  50. SK_WHEN(!(T::kTags & kDraw_Tag), bool) operator()(T* draw) {
  51. fPaint = nullptr;
  52. return false;
  53. }
  54. private:
  55. // Abstracts away whether the paint is always part of the command or optional.
  56. template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; }
  57. template <typename T> static T* AsPtr(T& x) { return &x; }
  58. type* fPaint;
  59. };
  60. // Matches if Matcher doesn't. Stores nothing.
  61. template <typename Matcher>
  62. struct Not {
  63. template <typename T>
  64. bool operator()(T* ptr) { return !Matcher()(ptr); }
  65. };
  66. // Matches if any of First or Rest... does. Stores nothing.
  67. template <typename First, typename... Rest>
  68. struct Or {
  69. template <typename T>
  70. bool operator()(T* ptr) { return First()(ptr) || Or<Rest...>()(ptr); }
  71. };
  72. template <typename First>
  73. struct Or<First> {
  74. template <typename T>
  75. bool operator()(T* ptr) { return First()(ptr); }
  76. };
  77. // Greedy is a special matcher that greedily matches Matcher 0 or more times. Stores nothing.
  78. template <typename Matcher>
  79. struct Greedy {
  80. template <typename T>
  81. bool operator()(T* ptr) { return Matcher()(ptr); }
  82. };
  83. // Pattern matches each of its matchers in order.
  84. //
  85. // This is the main entry point to pattern matching, and so provides a couple of extra API bits:
  86. // - search scans through the record to look for matches;
  87. // - first, second, third, ... return the data stored by their respective matchers in the pattern.
  88. template <typename... Matchers> class Pattern;
  89. template <> class Pattern<> {
  90. public:
  91. // Bottoms out recursion. Just return whatever i the front decided on.
  92. int match(SkRecord*, int i) { return i; }
  93. };
  94. template <typename First, typename... Rest>
  95. class Pattern<First, Rest...> {
  96. public:
  97. // If this pattern matches the SkRecord starting from i,
  98. // return the index just past the end of the pattern, otherwise return 0.
  99. SK_ALWAYS_INLINE int match(SkRecord* record, int i) {
  100. i = this->matchFirst(&fFirst, record, i);
  101. return i > 0 ? fRest.match(record, i) : 0;
  102. }
  103. // Starting from *end, walk through the SkRecord to find the first span matching this pattern.
  104. // If there is no such span, return false. If there is, return true and set [*begin, *end).
  105. SK_ALWAYS_INLINE bool search(SkRecord* record, int* begin, int* end) {
  106. for (*begin = *end; *begin < record->count(); ++(*begin)) {
  107. *end = this->match(record, *begin);
  108. if (*end != 0) {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. // TODO: some sort of smart get<i>()
  115. template <typename T> T* first() { return fFirst.get(); }
  116. template <typename T> T* second() { return fRest.template first<T>(); }
  117. template <typename T> T* third() { return fRest.template second<T>(); }
  118. template <typename T> T* fourth() { return fRest.template third<T>(); }
  119. private:
  120. // If first isn't a Greedy, try to match at i once.
  121. template <typename T>
  122. int matchFirst(T* first, SkRecord* record, int i) {
  123. if (i < record->count()) {
  124. if (record->mutate(i, *first)) {
  125. return i+1;
  126. }
  127. }
  128. return 0;
  129. }
  130. // If first is a Greedy, walk i until it doesn't match.
  131. template <typename T>
  132. int matchFirst(Greedy<T>* first, SkRecord* record, int i) {
  133. while (i < record->count()) {
  134. if (!record->mutate(i, *first)) {
  135. return i;
  136. }
  137. i++;
  138. }
  139. return 0;
  140. }
  141. First fFirst;
  142. Pattern<Rest...> fRest;
  143. };
  144. } // namespace SkRecords
  145. #endif//SkRecordPattern_DEFINED