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