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 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame^] | 13 | GrClip::GrClip() { |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 14 | fConservativeBounds.setEmpty(); |
| 15 | fConservativeBoundsValid = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | } |
| 17 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame^] | 18 | GrClip::GrClip(const GrClip& src) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 19 | *this = src; |
| 20 | } |
| 21 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame^] | 22 | GrClip::GrClip(const GrIRect& rect) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 23 | this->setFromIRect(rect); |
| 24 | } |
| 25 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame^] | 26 | GrClip::GrClip(const GrRect& rect) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 27 | this->setFromRect(rect); |
| 28 | } |
| 29 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 30 | GrClip::GrClip(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame^] | 31 | const GrRect* bounds) { |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 32 | this->setFromIterator(iter, tx, ty, bounds); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | GrClip::~GrClip() {} |
| 36 | |
| 37 | GrClip& GrClip::operator=(const GrClip& src) { |
| 38 | fList = src.fList; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 39 | fConservativeBounds = src.fConservativeBounds; |
| 40 | fConservativeBoundsValid = src.fConservativeBoundsValid; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 41 | return *this; |
| 42 | } |
| 43 | |
| 44 | void GrClip::setEmpty() { |
| 45 | fList.reset(); |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 46 | fConservativeBounds.setEmpty(); |
| 47 | fConservativeBoundsValid = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 48 | } |
| 49 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 50 | void GrClip::setFromRect(const GrRect& r) { |
| 51 | fList.reset(); |
| 52 | if (r.isEmpty()) { |
| 53 | // use a canonical empty rect for == testing. |
| 54 | setEmpty(); |
| 55 | } else { |
| 56 | fList.push_back(); |
| 57 | fList.back().fRect = r; |
| 58 | fList.back().fType = kRect_ClipType; |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 59 | fList.back().fOp = kReplace_SetOp; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 60 | fConservativeBounds = r; |
| 61 | fConservativeBoundsValid = true; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | void GrClip::setFromIRect(const GrIRect& r) { |
| 66 | fList.reset(); |
| 67 | if (r.isEmpty()) { |
| 68 | // use a canonical empty rect for == testing. |
| 69 | setEmpty(); |
| 70 | } else { |
| 71 | fList.push_back(); |
| 72 | fList.back().fRect.set(r); |
| 73 | fList.back().fType = kRect_ClipType; |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 74 | fList.back().fOp = kReplace_SetOp; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 75 | fConservativeBounds.set(r); |
| 76 | fConservativeBoundsValid = true; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 80 | static void intersectWith(SkRect* dst, const SkRect& src) { |
| 81 | if (!dst->intersect(src)) { |
| 82 | dst->setEmpty(); |
| 83 | } |
| 84 | } |
| 85 | |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 86 | void GrClip::setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 87 | const GrRect* conservativeBounds) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 88 | fList.reset(); |
| 89 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 90 | int rectCount = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 91 | |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 92 | // compute bounds for common case of series of intersecting rects. |
| 93 | bool isectRectValid = true; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 94 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 95 | if (iter) { |
| 96 | for (iter->rewind(); !iter->isDone(); iter->next()) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 97 | Element& e = fList.push_back(); |
| 98 | e.fType = iter->getType(); |
| 99 | e.fOp = iter->getOp(); |
| 100 | // iterators should not emit replace |
| 101 | GrAssert(kReplace_SetOp != e.fOp); |
| 102 | switch (e.fType) { |
| 103 | case kRect_ClipType: |
| 104 | iter->getRect(&e.fRect); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 105 | if (tx || ty) { |
| 106 | e.fRect.offset(tx, ty); |
| 107 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 108 | ++rectCount; |
| 109 | if (isectRectValid) { |
bsalomon@google.com | ab3dee5 | 2011-08-29 15:18:41 +0000 | [diff] [blame] | 110 | if (kIntersect_SetOp == e.fOp) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 111 | GrAssert(fList.count() <= 2); |
| 112 | if (fList.count() > 1) { |
| 113 | GrAssert(2 == rectCount); |
| 114 | rectCount = 1; |
| 115 | fList.pop_back(); |
| 116 | GrAssert(kRect_ClipType == fList.back().fType); |
reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 117 | intersectWith(&fList.back().fRect, e.fRect); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 118 | } |
| 119 | } else { |
| 120 | isectRectValid = false; |
| 121 | } |
| 122 | } |
| 123 | break; |
| 124 | case kPath_ClipType: |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 125 | e.fPath = *iter->getPath(); |
reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 126 | if (tx || ty) { |
| 127 | e.fPath.offset(tx, ty); |
| 128 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 129 | e.fPathFill = iter->getPathFill(); |
| 130 | isectRectValid = false; |
| 131 | break; |
| 132 | default: |
| 133 | GrCrash("Unknown clip element type."); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 134 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 135 | } |
| 136 | } |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 137 | fConservativeBoundsValid = false; |
epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 138 | if (isectRectValid && rectCount) { |
| 139 | fConservativeBounds = fList[0].fRect; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 140 | fConservativeBoundsValid = true; |
bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 141 | } else if (NULL != conservativeBounds) { |
| 142 | fConservativeBounds = *conservativeBounds; |
| 143 | fConservativeBoundsValid = true; |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 144 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 145 | } |