SkRasterClipStack.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2017 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 SkRasterClipStack_DEFINED
  8. #define SkRasterClipStack_DEFINED
  9. #include "include/core/SkClipOp.h"
  10. #include "include/core/SkDeque.h"
  11. #include "src/core/SkRasterClip.h"
  12. #include <new>
  13. template <typename T> class SkTStack {
  14. public:
  15. SkTStack(void* storage, size_t size) : fDeque(sizeof(T), storage, size), fTop(nullptr) {}
  16. ~SkTStack() {
  17. while (!fDeque.empty()) {
  18. ((T*)fDeque.back())->~T();
  19. fDeque.pop_back();
  20. }
  21. }
  22. bool empty() const { return fDeque.empty(); }
  23. int count() const { return fDeque.count(); }
  24. const T& top() const {
  25. SkASSERT(fTop);
  26. return *fTop;
  27. }
  28. T& top() {
  29. SkASSERT(fTop);
  30. return *fTop;
  31. }
  32. T* push_raw() { return (T*)fDeque.push_back(); }
  33. T& push() {
  34. fTop = this->push_raw();
  35. new (fTop) T();
  36. return *fTop;
  37. }
  38. T& push(const T& src) {
  39. fTop = this->push_raw();
  40. new (fTop) T(src);
  41. return *fTop;
  42. }
  43. void pop() {
  44. fTop->~T();
  45. fDeque.pop_back();
  46. fTop = fDeque.empty() ? nullptr : (T*)fDeque.back();
  47. }
  48. private:
  49. SkDeque fDeque;
  50. T* fTop;
  51. };
  52. class SkRasterClipStack : SkNoncopyable {
  53. int fCounter = 0;
  54. public:
  55. SkRasterClipStack(int width, int height)
  56. : fStack(fStorage, sizeof(fStorage))
  57. , fRootBounds(SkIRect::MakeWH(width, height))
  58. {
  59. Rec& rec = fStack.push();
  60. rec.fRC.setRect(fRootBounds);
  61. rec.fDeferredCount = 0;
  62. SkASSERT(fStack.count() == 1);
  63. }
  64. void setNewSize(int w, int h) {
  65. fRootBounds.setXYWH(0, 0, w, h);
  66. SkASSERT(fStack.count() == 1);
  67. Rec& rec = fStack.top();
  68. SkASSERT(rec.fDeferredCount == 0);
  69. rec.fRC.setRect(fRootBounds);
  70. }
  71. const SkRasterClip& rc() const { return fStack.top().fRC; }
  72. void save() {
  73. fCounter += 1;
  74. SkASSERT(fStack.top().fDeferredCount >= 0);
  75. fStack.top().fDeferredCount += 1;
  76. }
  77. void restore() {
  78. fCounter -= 1; SkASSERT(fCounter >= 0);
  79. if (--fStack.top().fDeferredCount < 0) {
  80. SkASSERT(fStack.top().fDeferredCount == -1);
  81. SkASSERT(fStack.count() > 1);
  82. fStack.pop();
  83. }
  84. }
  85. void clipRect(const SkMatrix& ctm, const SkRect& rect, SkClipOp op, bool aa) {
  86. this->writable_rc().op(rect, ctm, fRootBounds, (SkRegion::Op)op, aa);
  87. this->trimIfExpanding(op);
  88. this->validate();
  89. }
  90. void clipRRect(const SkMatrix& ctm, const SkRRect& rrect, SkClipOp op, bool aa) {
  91. this->writable_rc().op(rrect, ctm, fRootBounds, (SkRegion::Op)op, aa);
  92. this->trimIfExpanding(op);
  93. this->validate();
  94. }
  95. void clipPath(const SkMatrix& ctm, const SkPath& path, SkClipOp op, bool aa) {
  96. this->writable_rc().op(path, ctm, fRootBounds, (SkRegion::Op)op, aa);
  97. this->trimIfExpanding(op);
  98. this->validate();
  99. }
  100. void clipRegion(const SkRegion& rgn, SkClipOp op) {
  101. this->writable_rc().op(rgn, (SkRegion::Op)op);
  102. this->trimIfExpanding(op);
  103. this->validate();
  104. }
  105. void setDeviceClipRestriction(SkIRect* mutableClipRestriction) {
  106. this->writable_rc().setDeviceClipRestriction(mutableClipRestriction);
  107. }
  108. void validate() const {
  109. #ifdef SK_DEBUG
  110. const SkRasterClip& clip = this->rc();
  111. if (fRootBounds.isEmpty()) {
  112. SkASSERT(clip.isEmpty());
  113. } else if (!clip.isEmpty()) {
  114. SkASSERT(fRootBounds.contains(clip.getBounds()));
  115. }
  116. #endif
  117. }
  118. private:
  119. struct Rec {
  120. SkRasterClip fRC;
  121. int fDeferredCount; // 0 for a "normal" entry
  122. };
  123. enum {
  124. ELEM_COUNT = 16,
  125. PTR_COUNT = ELEM_COUNT * sizeof(Rec) / sizeof(void*)
  126. };
  127. void* fStorage[PTR_COUNT];
  128. SkTStack<Rec> fStack;
  129. SkIRect fRootBounds;
  130. SkRasterClip& writable_rc() {
  131. SkASSERT(fStack.top().fDeferredCount >= 0);
  132. if (fStack.top().fDeferredCount > 0) {
  133. fStack.top().fDeferredCount -= 1;
  134. fStack.push(fStack.top());
  135. fStack.top().fDeferredCount = 0;
  136. }
  137. return fStack.top().fRC;
  138. }
  139. void trimIfExpanding(SkClipOp op) {
  140. if ((int)op > (int)SkClipOp::kIntersect) {
  141. Rec& rec = fStack.top();
  142. SkASSERT(rec.fDeferredCount == 0);
  143. rec.fRC.op(fRootBounds, SkRegion::kIntersect_Op);
  144. }
  145. }
  146. };
  147. #endif