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