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 | |
| 41 | GrClip::GrClip(GrClipIterator* iter, const GrRect* bounds) |
| 42 | : fList(fListMemory, kPreAllocElements) { |
| 43 | this->setFromIterator(iter, bounds); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | GrClip::~GrClip() {} |
| 47 | |
| 48 | GrClip& GrClip::operator=(const GrClip& src) { |
| 49 | fList = src.fList; |
| 50 | fBounds = src.fBounds; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 51 | fBoundsValid = src.fBoundsValid; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 52 | return *this; |
| 53 | } |
| 54 | |
| 55 | void GrClip::setEmpty() { |
| 56 | fList.reset(); |
| 57 | fBounds.setEmpty(); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 58 | fBoundsValid = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 59 | } |
| 60 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 61 | void GrClip::setFromRect(const GrRect& r) { |
| 62 | fList.reset(); |
| 63 | if (r.isEmpty()) { |
| 64 | // use a canonical empty rect for == testing. |
| 65 | setEmpty(); |
| 66 | } else { |
| 67 | fList.push_back(); |
| 68 | fList.back().fRect = r; |
| 69 | fList.back().fType = kRect_ClipType; |
| 70 | fBounds = r; |
| 71 | fBoundsValid = true; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void GrClip::setFromIRect(const GrIRect& r) { |
| 76 | fList.reset(); |
| 77 | if (r.isEmpty()) { |
| 78 | // use a canonical empty rect for == testing. |
| 79 | setEmpty(); |
| 80 | } else { |
| 81 | fList.push_back(); |
| 82 | fList.back().fRect.set(r); |
| 83 | fList.back().fType = kRect_ClipType; |
| 84 | fBounds.set(r); |
| 85 | fBoundsValid = true; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void GrClip::setFromIterator(GrClipIterator* iter, const GrRect* bounds) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 90 | fList.reset(); |
| 91 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 92 | int rectCount = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 94 | // compute bounds for common case of series of intersecting rects. |
| 95 | bool isectRectValid = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 96 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 97 | if (iter) { |
| 98 | for (iter->rewind(); !iter->isDone(); iter->next()) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 99 | Element& e = fList.push_back(); |
| 100 | e.fType = iter->getType(); |
| 101 | e.fOp = iter->getOp(); |
| 102 | // iterators should not emit replace |
| 103 | GrAssert(kReplace_SetOp != e.fOp); |
| 104 | switch (e.fType) { |
| 105 | case kRect_ClipType: |
| 106 | iter->getRect(&e.fRect); |
| 107 | ++rectCount; |
| 108 | if (isectRectValid) { |
| 109 | if (1 == rectCount || kIntersect_SetOp == e.fOp) { |
| 110 | GrAssert(fList.count() <= 2); |
| 111 | if (fList.count() > 1) { |
| 112 | GrAssert(2 == rectCount); |
| 113 | rectCount = 1; |
| 114 | fList.pop_back(); |
| 115 | GrAssert(kRect_ClipType == fList.back().fType); |
| 116 | fList.back().fRect.intersectWith(e.fRect); |
| 117 | } |
| 118 | } else { |
| 119 | isectRectValid = false; |
| 120 | } |
| 121 | } |
| 122 | break; |
| 123 | case kPath_ClipType: |
| 124 | e.fPath.resetFromIter(iter->getPathIter()); |
| 125 | e.fPathFill = iter->getPathFill(); |
| 126 | isectRectValid = false; |
| 127 | break; |
| 128 | default: |
| 129 | GrCrash("Unknown clip element type."); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 130 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame^] | 133 | fBoundsValid = false; |
| 134 | if (NULL == bounds) { |
| 135 | if (isectRectValid) { |
| 136 | fBoundsValid = true; |
| 137 | if (rectCount > 0) { |
| 138 | fBounds = fList[0].fRect; |
| 139 | } else { |
| 140 | fBounds.setEmpty(); |
| 141 | } |
| 142 | } |
| 143 | } else { |
| 144 | fBounds = *bounds; |
| 145 | fBoundsValid = true; |
| 146 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 147 | } |