2 * Copyright 2017 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #ifndef SkRasterClipStack_DEFINED
9 #define SkRasterClipStack_DEFINED
11 #include "include/core/SkClipOp.h"
12 #include "src/core/SkRasterClip.h"
13 #include "src/core/SkScan.h"
14 #include "src/core/SkTBlockList.h"
16 class SkRasterClipStack : SkNoncopyable {
18 SkRasterClipStack(int width, int height)
19 : fRootBounds(SkIRect::MakeWH(width, height))
20 , fDisableAA(SkScan::DowngradeClipAA(fRootBounds)) {
21 fStack.emplace_back(SkRasterClip(fRootBounds));
22 SkASSERT(fStack.count() == 1);
25 void setNewSize(int w, int h) {
26 fRootBounds.setXYWH(0, 0, w, h);
28 SkASSERT(fStack.count() == 1);
29 Rec& rec = fStack.back();
30 SkASSERT(rec.fDeferredCount == 0);
31 rec.fRC.setRect(fRootBounds);
34 const SkRasterClip& rc() const { return fStack.back().fRC; }
37 SkDEBUGCODE(fCounter += 1);
38 SkASSERT(fStack.back().fDeferredCount >= 0);
39 fStack.back().fDeferredCount += 1;
43 SkDEBUGCODE(fCounter -= 1);
44 SkASSERT(fCounter >= 0);
46 if (--fStack.back().fDeferredCount < 0) {
47 SkASSERT(fStack.back().fDeferredCount == -1);
48 SkASSERT(fStack.count() > 1);
53 void clipRect(const SkMatrix& ctm, const SkRect& rect, SkClipOp op, bool aa) {
54 this->writable_rc().op(rect, ctm, op, this->finalAA(aa));
58 void clipRRect(const SkMatrix& ctm, const SkRRect& rrect, SkClipOp op, bool aa) {
59 this->writable_rc().op(rrect, ctm, op, this->finalAA(aa));
63 void clipPath(const SkMatrix& ctm, const SkPath& path, SkClipOp op, bool aa) {
64 this->writable_rc().op(path, ctm, op, this->finalAA(aa));
68 void clipShader(sk_sp<SkShader> sh) {
69 this->writable_rc().op(std::move(sh));
73 void clipRegion(const SkRegion& rgn, SkClipOp op) {
74 this->writable_rc().op(rgn, op);
78 void replaceClip(const SkIRect& rect) {
79 SkIRect devRect = rect;
80 if (!devRect.intersect(fRootBounds)) {
81 this->writable_rc().setEmpty();
83 this->writable_rc().setRect(devRect);
87 void validate() const {
89 const SkRasterClip& clip = this->rc();
90 if (fRootBounds.isEmpty()) {
91 SkASSERT(clip.isEmpty());
92 } else if (!clip.isEmpty()) {
93 SkASSERT(fRootBounds.contains(clip.getBounds()));
101 int fDeferredCount; // 0 for a "normal" entry
103 Rec(const SkRasterClip& rc) : fRC(rc), fDeferredCount(0) {}
106 SkTBlockList<Rec, 16> fStack;
109 SkDEBUGCODE(int fCounter = 0);
111 SkRasterClip& writable_rc() {
112 SkASSERT(fStack.back().fDeferredCount >= 0);
113 if (fStack.back().fDeferredCount > 0) {
114 fStack.back().fDeferredCount -= 1;
115 fStack.emplace_back(fStack.back().fRC);
117 return fStack.back().fRC;
120 bool finalAA(bool aa) const { return aa && !fDisableAA; }