reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 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 | #include "SkRasterClip.h" |
| 9 | |
| 10 | |
| 11 | SkRasterClip::SkRasterClip() { |
| 12 | fIsBW = true; |
| 13 | } |
| 14 | |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame^] | 15 | SkRasterClip::SkRasterClip(const SkIRect& bounds) : fBW(bounds) { |
| 16 | fIsBW = true; |
| 17 | } |
| 18 | |
| 19 | SkRasterClip::~SkRasterClip() {} |
| 20 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 21 | bool SkRasterClip::isEmpty() const { |
| 22 | return fIsBW ? fBW.isEmpty() : fAA.isEmpty(); |
| 23 | } |
| 24 | |
| 25 | bool SkRasterClip::isRect() const { |
| 26 | return fIsBW ? fBW.isRect() : false; |
| 27 | } |
| 28 | |
| 29 | bool SkRasterClip::isComplex() const { |
| 30 | return fIsBW ? fBW.isComplex() : !fAA.isEmpty(); |
| 31 | } |
| 32 | |
| 33 | const SkIRect& SkRasterClip::getBounds() const { |
| 34 | return fIsBW ? fBW.getBounds() : fAA.getBounds(); |
| 35 | } |
| 36 | |
| 37 | bool SkRasterClip::setEmpty() { |
| 38 | fIsBW = true; |
| 39 | fBW.setEmpty(); |
| 40 | fAA.setEmpty(); |
| 41 | return false; |
| 42 | } |
| 43 | |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame^] | 44 | bool SkRasterClip::setRect(const SkIRect& rect) { |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 45 | fIsBW = true; |
| 46 | fAA.setEmpty(); |
| 47 | return fBW.setRect(rect); |
| 48 | } |
| 49 | |
| 50 | bool SkRasterClip::setPath(const SkPath& path, const SkRegion& clip, bool doAA) { |
| 51 | if (this->isBW() && !doAA) { |
| 52 | return fBW.setPath(path, clip); |
| 53 | } else { |
| 54 | if (this->isBW()) { |
| 55 | this->convertToAA(); |
| 56 | } |
| 57 | return fAA.setPath(path, &clip, doAA); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | bool SkRasterClip::setPath(const SkPath& path, const SkIRect& clip, bool doAA) { |
| 62 | SkRegion tmp; |
| 63 | tmp.setRect(clip); |
| 64 | return this->setPath(path, tmp, doAA); |
| 65 | } |
| 66 | |
| 67 | bool SkRasterClip::setPath(const SkPath& path, const SkRasterClip& clip, |
| 68 | bool doAA) { |
| 69 | if (clip.isBW()) { |
| 70 | return this->setPath(path, clip.bwRgn(), doAA); |
| 71 | } else { |
| 72 | SkRegion tmp; |
| 73 | tmp.setRect(clip.getBounds()); |
| 74 | if (!this->setPath(path, clip, doAA)) { |
| 75 | return false; |
| 76 | } |
| 77 | return this->op(clip, SkRegion::kIntersect_Op); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool SkRasterClip::op(const SkIRect& rect, SkRegion::Op op) { |
| 82 | return fIsBW ? fBW.op(rect, op) : fAA.op(rect, op); |
| 83 | } |
| 84 | |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame^] | 85 | bool SkRasterClip::op(const SkRegion& rgn, SkRegion::Op op) { |
| 86 | if (fIsBW) { |
| 87 | return fBW.op(rgn, op); |
| 88 | } else { |
| 89 | SkAAClip tmp; |
| 90 | tmp.setRegion(rgn); |
| 91 | return fAA.op(tmp, op); |
| 92 | } |
| 93 | } |
| 94 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 95 | bool SkRasterClip::op(const SkRasterClip& clip, SkRegion::Op op) { |
| 96 | if (this->isBW() && clip.isBW()) { |
| 97 | return fBW.op(clip.fBW, op); |
| 98 | } else { |
| 99 | SkAAClip tmp; |
| 100 | const SkAAClip* other; |
| 101 | |
| 102 | if (this->isBW()) { |
| 103 | this->convertToAA(); |
| 104 | } |
| 105 | if (clip.isBW()) { |
| 106 | tmp.setRegion(clip.bwRgn()); |
| 107 | other = &tmp; |
| 108 | } else { |
| 109 | other = &clip.aaRgn(); |
| 110 | } |
| 111 | return fAA.op(*other, op); |
| 112 | } |
| 113 | } |
| 114 | |
reed@google.com | ba16da9 | 2011-10-11 13:15:03 +0000 | [diff] [blame^] | 115 | const SkRegion& SkRasterClip::forceGetBW() { |
| 116 | if (!fIsBW) { |
| 117 | fBW.setRect(fAA.getBounds()); |
| 118 | } |
| 119 | return fBW; |
| 120 | } |
| 121 | |
reed@google.com | d1e3c5f | 2011-10-10 19:36:25 +0000 | [diff] [blame] | 122 | void SkRasterClip::convertToAA() { |
| 123 | SkASSERT(fIsBW); |
| 124 | fAA.setRegion(fBW); |
| 125 | fIsBW = false; |
| 126 | } |
| 127 | |