| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
| epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 11 | #include "GrClip.h" |
| 12 | |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 13 | GrClip::GrClip() |
| 14 | : fRequiresAA(false) { |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 15 | fConservativeBounds.setEmpty(); |
| 16 | fConservativeBoundsValid = true; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 17 | } |
| 18 | |
| bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 19 | GrClip::GrClip(const GrClip& src) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 20 | *this = src; |
| 21 | } |
| 22 | |
| bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 23 | GrClip::GrClip(const GrIRect& rect) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 24 | this->setFromIRect(rect); |
| 25 | } |
| 26 | |
| bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 27 | GrClip::GrClip(const GrRect& rect) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 28 | this->setFromRect(rect); |
| 29 | } |
| 30 | |
| reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 31 | GrClip::GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
| robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 32 | const GrRect& bounds) { |
| reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 33 | this->setFromIterator(iter, tx, ty, bounds); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | GrClip::~GrClip() {} |
| 37 | |
| 38 | GrClip& GrClip::operator=(const GrClip& src) { |
| 39 | fList = src.fList; |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 40 | fConservativeBounds = src.fConservativeBounds; |
| 41 | fConservativeBoundsValid = src.fConservativeBoundsValid; |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 42 | fRequiresAA = src.fRequiresAA; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 43 | return *this; |
| 44 | } |
| 45 | |
| 46 | void GrClip::setEmpty() { |
| 47 | fList.reset(); |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 48 | fConservativeBounds.setEmpty(); |
| 49 | fConservativeBoundsValid = true; |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 50 | fRequiresAA = false; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 53 | void GrClip::setFromRect(const GrRect& r) { |
| 54 | fList.reset(); |
| 55 | if (r.isEmpty()) { |
| 56 | // use a canonical empty rect for == testing. |
| 57 | setEmpty(); |
| 58 | } else { |
| 59 | fList.push_back(); |
| 60 | fList.back().fRect = r; |
| 61 | fList.back().fType = kRect_ClipType; |
| robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 62 | fList.back().fOp = SkRegion::kReplace_Op; |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 63 | fList.back().fDoAA = false; |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 64 | fConservativeBounds = r; |
| 65 | fConservativeBoundsValid = true; |
| robertphillips@google.com | b99225c | 2012-07-24 18:20:10 +0000 | [diff] [blame^] | 66 | fRequiresAA = false; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | void GrClip::setFromIRect(const GrIRect& r) { |
| 71 | fList.reset(); |
| 72 | if (r.isEmpty()) { |
| 73 | // use a canonical empty rect for == testing. |
| 74 | setEmpty(); |
| 75 | } else { |
| 76 | fList.push_back(); |
| 77 | fList.back().fRect.set(r); |
| 78 | fList.back().fType = kRect_ClipType; |
| robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 79 | fList.back().fOp = SkRegion::kReplace_Op; |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 80 | fList.back().fDoAA = false; |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 81 | fConservativeBounds.set(r); |
| 82 | fConservativeBoundsValid = true; |
| robertphillips@google.com | b99225c | 2012-07-24 18:20:10 +0000 | [diff] [blame^] | 83 | fRequiresAA = false; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
| reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 87 | static void intersectWith(SkRect* dst, const SkRect& src) { |
| 88 | if (!dst->intersect(src)) { |
| 89 | dst->setEmpty(); |
| 90 | } |
| 91 | } |
| 92 | |
| reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 93 | void GrClip::setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
| robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 94 | const GrRect& conservativeBounds) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | fList.reset(); |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 96 | fRequiresAA = false; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 98 | int rectCount = 0; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 100 | // compute bounds for common case of series of intersecting rects. |
| 101 | bool isectRectValid = true; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 102 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 103 | if (iter) { |
| 104 | for (iter->rewind(); !iter->isDone(); iter->next()) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 105 | Element& e = fList.push_back(); |
| 106 | e.fType = iter->getType(); |
| 107 | e.fOp = iter->getOp(); |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 108 | e.fDoAA = iter->getDoAA(); |
| 109 | if (e.fDoAA) { |
| 110 | fRequiresAA = true; |
| 111 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 112 | // iterators should not emit replace |
| robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 113 | GrAssert(SkRegion::kReplace_Op != e.fOp); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 114 | switch (e.fType) { |
| 115 | case kRect_ClipType: |
| 116 | iter->getRect(&e.fRect); |
| reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 117 | if (tx || ty) { |
| 118 | e.fRect.offset(tx, ty); |
| 119 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 120 | ++rectCount; |
| 121 | if (isectRectValid) { |
| robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame] | 122 | if (SkRegion::kIntersect_Op == e.fOp) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 123 | GrAssert(fList.count() <= 2); |
| 124 | if (fList.count() > 1) { |
| 125 | GrAssert(2 == rectCount); |
| 126 | rectCount = 1; |
| 127 | fList.pop_back(); |
| 128 | GrAssert(kRect_ClipType == fList.back().fType); |
| reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 129 | intersectWith(&fList.back().fRect, e.fRect); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 130 | } |
| 131 | } else { |
| 132 | isectRectValid = false; |
| 133 | } |
| 134 | } |
| 135 | break; |
| 136 | case kPath_ClipType: |
| reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 137 | e.fPath = *iter->getPath(); |
| reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 138 | if (tx || ty) { |
| 139 | e.fPath.offset(tx, ty); |
| 140 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 141 | e.fPathFill = iter->getPathFill(); |
| 142 | isectRectValid = false; |
| 143 | break; |
| 144 | default: |
| 145 | GrCrash("Unknown clip element type."); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 146 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | } |
| 148 | } |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 149 | fConservativeBoundsValid = false; |
| epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 150 | if (isectRectValid && rectCount) { |
| 151 | fConservativeBounds = fList[0].fRect; |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 152 | fConservativeBoundsValid = true; |
| robertphillips@google.com | 3e11c0b | 2012-07-11 18:20:35 +0000 | [diff] [blame] | 153 | } else { |
| 154 | fConservativeBounds = conservativeBounds; |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 155 | fConservativeBoundsValid = true; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 156 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 157 | } |
| robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 158 | |
| 159 | /////////////////////////////////////////////////////////////////////////////// |
| 160 | |
| 161 | GrClip::Iter::Iter() |
| 162 | : fStack(NULL) |
| 163 | , fCurIndex(0) { |
| 164 | } |
| 165 | |
| 166 | GrClip::Iter::Iter(const GrClip& stack, IterStart startLoc) |
| 167 | : fStack(&stack) { |
| 168 | this->reset(stack, startLoc); |
| 169 | } |
| 170 | |
| 171 | const GrClip::Iter::Clip* GrClip::Iter::updateClip(int index) { |
| 172 | |
| 173 | if (NULL == fStack) { |
| 174 | return NULL; |
| 175 | } |
| 176 | |
| 177 | GrAssert(0 <= index && index < fStack->getElementCount()); |
| 178 | |
| 179 | |
| 180 | |
| 181 | switch (fStack->getElementType(index)) { |
| 182 | case kRect_ClipType: |
| 183 | fClip.fRect = &fStack->getRect(index); |
| 184 | fClip.fPath = NULL; |
| 185 | break; |
| 186 | case kPath_ClipType: |
| 187 | fClip.fRect = NULL; |
| 188 | fClip.fPath = &fStack->getPath(index); |
| 189 | break; |
| 190 | } |
| 191 | fClip.fOp = fStack->getOp(index); |
| 192 | fClip.fDoAA = fStack->getDoAA(index); |
| 193 | return &fClip; |
| 194 | } |
| 195 | |
| 196 | const GrClip::Iter::Clip* GrClip::Iter::next() { |
| 197 | |
| 198 | if (NULL == fStack) { |
| 199 | return NULL; |
| 200 | } |
| 201 | |
| 202 | if (0 > fCurIndex || fCurIndex >= fStack->getElementCount()) { |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | int oldIndex = fCurIndex; |
| 207 | ++fCurIndex; |
| 208 | |
| 209 | return this->updateClip(oldIndex); |
| 210 | } |
| 211 | |
| 212 | const GrClip::Iter::Clip* GrClip::Iter::prev() { |
| 213 | |
| 214 | if (NULL == fStack) { |
| 215 | return NULL; |
| 216 | } |
| 217 | |
| 218 | if (0 > fCurIndex || fCurIndex >= fStack->getElementCount()) { |
| 219 | return NULL; |
| 220 | } |
| 221 | |
| 222 | int oldIndex = fCurIndex; |
| 223 | --fCurIndex; |
| 224 | |
| 225 | return this->updateClip(oldIndex); |
| 226 | } |
| 227 | |
| 228 | const GrClip::Iter::Clip* GrClip::Iter::skipToTopmost(SkRegion::Op op) { |
| 229 | |
| 230 | GrAssert(SkRegion::kReplace_Op == op); |
| 231 | |
| 232 | if (NULL == fStack) { |
| 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | // GrClip removes all clips below the topmost replace |
| 237 | this->reset(*fStack, kBottom_IterStart); |
| 238 | |
| 239 | return this->next(); |
| 240 | } |
| 241 | |
| 242 | void GrClip::Iter::reset(const GrClip& stack, IterStart startLoc) { |
| 243 | fStack = &stack; |
| 244 | if (kBottom_IterStart == startLoc) { |
| 245 | fCurIndex = 0; |
| 246 | } else { |
| 247 | fCurIndex = fStack->getElementCount()-1; |
| 248 | } |
| 249 | } |