reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2010 Google Inc. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | |
| 18 | #include "GrClip.h" |
| 19 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 20 | GrClip::GrClip() |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 21 | : fList(&fListStorage) { |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 22 | fConservativeBounds.setEmpty(); |
| 23 | fConservativeBoundsValid = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 24 | } |
| 25 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 26 | GrClip::GrClip(const GrClip& src) |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 27 | : fList(&fListStorage) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 28 | *this = src; |
| 29 | } |
| 30 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 31 | GrClip::GrClip(const GrIRect& rect) |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 32 | : fList(&fListStorage) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 33 | this->setFromIRect(rect); |
| 34 | } |
| 35 | |
| 36 | GrClip::GrClip(const GrRect& rect) |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 37 | : fList(&fListStorage) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 38 | this->setFromRect(rect); |
| 39 | } |
| 40 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 41 | GrClip::GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
| 42 | const GrRect* bounds) |
bsalomon@google.com | a55847b | 2011-04-20 15:47:04 +0000 | [diff] [blame] | 43 | : fList(&fListStorage) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 44 | this->setFromIterator(iter, tx, ty, bounds); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | GrClip::~GrClip() {} |
| 48 | |
| 49 | GrClip& GrClip::operator=(const GrClip& src) { |
| 50 | fList = src.fList; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 51 | fConservativeBounds = src.fConservativeBounds; |
| 52 | fConservativeBoundsValid = src.fConservativeBoundsValid; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 53 | return *this; |
| 54 | } |
| 55 | |
| 56 | void GrClip::setEmpty() { |
| 57 | fList.reset(); |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 58 | fConservativeBounds.setEmpty(); |
| 59 | fConservativeBoundsValid = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 60 | } |
| 61 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 62 | void GrClip::setFromRect(const GrRect& r) { |
| 63 | fList.reset(); |
| 64 | if (r.isEmpty()) { |
| 65 | // use a canonical empty rect for == testing. |
| 66 | setEmpty(); |
| 67 | } else { |
| 68 | fList.push_back(); |
| 69 | fList.back().fRect = r; |
| 70 | fList.back().fType = kRect_ClipType; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 71 | fConservativeBounds = r; |
| 72 | fConservativeBoundsValid = true; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | void GrClip::setFromIRect(const GrIRect& r) { |
| 77 | fList.reset(); |
| 78 | if (r.isEmpty()) { |
| 79 | // use a canonical empty rect for == testing. |
| 80 | setEmpty(); |
| 81 | } else { |
| 82 | fList.push_back(); |
| 83 | fList.back().fRect.set(r); |
| 84 | fList.back().fType = kRect_ClipType; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 85 | fConservativeBounds.set(r); |
| 86 | fConservativeBoundsValid = true; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 90 | static void intersectWith(SkRect* dst, const SkRect& src) { |
| 91 | if (!dst->intersect(src)) { |
| 92 | dst->setEmpty(); |
| 93 | } |
| 94 | } |
| 95 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 96 | void GrClip::setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 97 | const GrRect* conservativeBounds) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 98 | fList.reset(); |
| 99 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 100 | int rectCount = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 101 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 102 | // compute bounds for common case of series of intersecting rects. |
| 103 | bool isectRectValid = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 104 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 105 | if (iter) { |
| 106 | for (iter->rewind(); !iter->isDone(); iter->next()) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 107 | Element& e = fList.push_back(); |
| 108 | e.fType = iter->getType(); |
| 109 | e.fOp = iter->getOp(); |
| 110 | // iterators should not emit replace |
| 111 | GrAssert(kReplace_SetOp != e.fOp); |
| 112 | switch (e.fType) { |
| 113 | case kRect_ClipType: |
| 114 | iter->getRect(&e.fRect); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 115 | if (tx || ty) { |
| 116 | e.fRect.offset(tx, ty); |
| 117 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 118 | ++rectCount; |
| 119 | if (isectRectValid) { |
| 120 | if (1 == rectCount || kIntersect_SetOp == e.fOp) { |
| 121 | GrAssert(fList.count() <= 2); |
| 122 | if (fList.count() > 1) { |
| 123 | GrAssert(2 == rectCount); |
| 124 | rectCount = 1; |
| 125 | fList.pop_back(); |
| 126 | GrAssert(kRect_ClipType == fList.back().fType); |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 127 | intersectWith(&fList.back().fRect, e.fRect); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 128 | } |
| 129 | } else { |
| 130 | isectRectValid = false; |
| 131 | } |
| 132 | } |
| 133 | break; |
| 134 | case kPath_ClipType: |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 135 | e.fPath = *iter->getPath(); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 136 | if (tx || ty) { |
| 137 | e.fPath.offset(tx, ty); |
| 138 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 139 | e.fPathFill = iter->getPathFill(); |
| 140 | isectRectValid = false; |
| 141 | break; |
| 142 | default: |
| 143 | GrCrash("Unknown clip element type."); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 147 | fConservativeBoundsValid = false; |
| 148 | if (isectRectValid) { |
| 149 | fConservativeBoundsValid = true; |
| 150 | if (rectCount > 0) { |
| 151 | fConservativeBounds = fList[0].fRect; |
| 152 | } else { |
| 153 | fConservativeBounds.setEmpty(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 154 | } |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 155 | } else if (NULL != conservativeBounds) { |
| 156 | fConservativeBounds = *conservativeBounds; |
| 157 | fConservativeBoundsValid = true; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 158 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 159 | } |