2 * Copyright 2010 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 SkRasterClip_DEFINED
9 #define SkRasterClip_DEFINED
11 #include "include/core/SkClipOp.h"
12 #include "include/core/SkRegion.h"
13 #include "include/core/SkShader.h"
14 #include "include/private/SkMacros.h"
15 #include "src/core/SkAAClip.h"
20 * Wraps a SkRegion and SkAAClip, so we have a single object that can represent either our
21 * BW or antialiased clips.
26 explicit SkRasterClip(const SkIRect&);
27 explicit SkRasterClip(const SkRegion&);
28 explicit SkRasterClip(const SkRasterClip&);
29 SkRasterClip(const SkPath& path, const SkIRect& bounds, bool doAA);
33 SkRasterClip& operator=(const SkRasterClip&);
35 bool isBW() const { return fIsBW; }
36 bool isAA() const { return !fIsBW; }
37 const SkRegion& bwRgn() const { SkASSERT(fIsBW); return fBW; }
38 const SkAAClip& aaRgn() const { SkASSERT(!fIsBW); return fAA; }
40 bool isEmpty() const {
41 SkASSERT(this->computeIsEmpty() == fIsEmpty);
46 SkASSERT(this->computeIsRect() == fIsRect);
50 bool isComplex() const {
51 return fIsBW ? fBW.isComplex() : !fAA.isEmpty();
53 const SkIRect& getBounds() const {
54 return fIsBW ? fBW.getBounds() : fAA.getBounds();
58 bool setRect(const SkIRect&);
60 bool op(const SkIRect&, SkClipOp);
61 bool op(const SkRegion&, SkClipOp);
62 bool op(const SkRect&, const SkMatrix& matrix, SkClipOp, bool doAA);
63 bool op(const SkRRect&, const SkMatrix& matrix, SkClipOp, bool doAA);
64 bool op(const SkPath&, const SkMatrix& matrix, SkClipOp, bool doAA);
65 bool op(sk_sp<SkShader>);
67 void translate(int dx, int dy, SkRasterClip* dst) const;
69 bool quickContains(const SkIRect& rect) const {
70 return fIsBW ? fBW.quickContains(rect) : fAA.quickContains(rect);
74 * Return true if this region is empty, or if the specified rectangle does
75 * not intersect the region. Returning false is not a guarantee that they
76 * intersect, but returning true is a guarantee that they do not.
78 bool quickReject(const SkIRect& rect) const {
79 return !SkIRect::Intersects(this->getBounds(), rect);
83 void validate() const;
85 void validate() const {}
88 sk_sp<SkShader> clipShader() const { return fShader; }
94 // these 2 are caches based on querying the right obj based on fIsBW
97 // if present, this augments the clip, not replaces it
98 sk_sp<SkShader> fShader;
100 bool computeIsEmpty() const {
101 return fIsBW ? fBW.isEmpty() : fAA.isEmpty();
104 bool computeIsRect() const {
105 return fIsBW ? fBW.isRect() : fAA.isRect();
108 bool updateCacheAndReturnNonEmpty(bool detectAARect = true) {
109 fIsEmpty = this->computeIsEmpty();
111 // detect that our computed AA is really just a (hard-edged) rect
112 if (detectAARect && !fIsEmpty && !fIsBW && fAA.isRect()) {
113 fBW.setRect(fAA.getBounds());
114 fAA.setEmpty(); // don't need this anymore
118 fIsRect = this->computeIsRect();
124 bool op(const SkRasterClip&, SkClipOp);
127 class SkAutoRasterClipValidate : SkNoncopyable {
129 SkAutoRasterClipValidate(const SkRasterClip& rc) : fRC(rc) {
132 ~SkAutoRasterClipValidate() {
136 const SkRasterClip& fRC;
140 #define AUTO_RASTERCLIP_VALIDATE(rc) SkAutoRasterClipValidate arcv(rc)
142 #define AUTO_RASTERCLIP_VALIDATE(rc)
145 ///////////////////////////////////////////////////////////////////////////////
148 * Encapsulates the logic of deciding if we need to change/wrap the blitter
149 * for aaclipping. If so, getRgn and getBlitter return modified values. If
150 * not, they return the raw blitter and (bw) clip region.
152 * We need to keep the constructor/destructor cost as small as possible, so we
153 * can freely put this on the stack, and not pay too much for the case when
154 * we're really BW anyways.
156 class SkAAClipBlitterWrapper {
158 SkAAClipBlitterWrapper();
159 SkAAClipBlitterWrapper(const SkRasterClip&, SkBlitter*);
160 SkAAClipBlitterWrapper(const SkAAClip*, SkBlitter*);
162 void init(const SkRasterClip&, SkBlitter*);
164 const SkIRect& getBounds() const {
166 return fClipRgn->getBounds();
168 const SkRegion& getRgn() const {
172 SkBlitter* getBlitter() {
179 SkAAClipBlitter fAABlitter;
181 const SkRegion* fClipRgn;