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() |
| 21 | : fList(fListMemory, kPreAllocElements) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 22 | fBounds.setEmpty(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 23 | fBoundsValid = 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) |
| 27 | : fList(fListMemory, kPreAllocElements) { |
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) |
| 32 | : fList(fListMemory, kPreAllocElements) { |
| 33 | this->setFromIRect(rect); |
| 34 | } |
| 35 | |
| 36 | GrClip::GrClip(const GrRect& rect) |
| 37 | : fList(fListMemory, kPreAllocElements) { |
| 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 | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 43 | : fList(fListMemory, kPreAllocElements) { |
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; |
| 51 | fBounds = src.fBounds; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 52 | fBoundsValid = src.fBoundsValid; |
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(); |
| 58 | fBounds.setEmpty(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 59 | fBoundsValid = 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; |
| 71 | fBounds = r; |
| 72 | fBoundsValid = true; |
| 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; |
| 85 | fBounds.set(r); |
| 86 | fBoundsValid = true; |
| 87 | } |
| 88 | } |
| 89 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame^] | 90 | void GrClip::setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
| 91 | const GrRect* bounds) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 92 | fList.reset(); |
| 93 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 94 | int rectCount = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 96 | // compute bounds for common case of series of intersecting rects. |
| 97 | bool isectRectValid = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 98 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 99 | if (iter) { |
| 100 | for (iter->rewind(); !iter->isDone(); iter->next()) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 101 | Element& e = fList.push_back(); |
| 102 | e.fType = iter->getType(); |
| 103 | e.fOp = iter->getOp(); |
| 104 | // iterators should not emit replace |
| 105 | GrAssert(kReplace_SetOp != e.fOp); |
| 106 | switch (e.fType) { |
| 107 | case kRect_ClipType: |
| 108 | iter->getRect(&e.fRect); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame^] | 109 | if (tx || ty) { |
| 110 | e.fRect.offset(tx, ty); |
| 111 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 112 | ++rectCount; |
| 113 | if (isectRectValid) { |
| 114 | if (1 == rectCount || kIntersect_SetOp == e.fOp) { |
| 115 | GrAssert(fList.count() <= 2); |
| 116 | if (fList.count() > 1) { |
| 117 | GrAssert(2 == rectCount); |
| 118 | rectCount = 1; |
| 119 | fList.pop_back(); |
| 120 | GrAssert(kRect_ClipType == fList.back().fType); |
| 121 | fList.back().fRect.intersectWith(e.fRect); |
| 122 | } |
| 123 | } else { |
| 124 | isectRectValid = false; |
| 125 | } |
| 126 | } |
| 127 | break; |
| 128 | case kPath_ClipType: |
| 129 | e.fPath.resetFromIter(iter->getPathIter()); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame^] | 130 | if (tx || ty) { |
| 131 | e.fPath.offset(tx, ty); |
| 132 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 133 | e.fPathFill = iter->getPathFill(); |
| 134 | isectRectValid = false; |
| 135 | break; |
| 136 | default: |
| 137 | GrCrash("Unknown clip element type."); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 138 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 139 | } |
| 140 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 141 | fBoundsValid = false; |
| 142 | if (NULL == bounds) { |
| 143 | if (isectRectValid) { |
| 144 | fBoundsValid = true; |
| 145 | if (rectCount > 0) { |
| 146 | fBounds = fList[0].fRect; |
| 147 | } else { |
| 148 | fBounds.setEmpty(); |
| 149 | } |
| 150 | } |
| 151 | } else { |
| 152 | fBounds = *bounds; |
| 153 | fBoundsValid = true; |
| 154 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 155 | } |