| 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, |
| bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +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; |
| 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; |
| robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame^] | 78 | fList.back().fOp = SkRegion::kReplace_Op; |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 79 | fList.back().fDoAA = false; |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 80 | fConservativeBounds.set(r); |
| 81 | fConservativeBoundsValid = true; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 85 | static void intersectWith(SkRect* dst, const SkRect& src) { |
| 86 | if (!dst->intersect(src)) { |
| 87 | dst->setEmpty(); |
| 88 | } |
| 89 | } |
| 90 | |
| reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 91 | void GrClip::setFromIterator(GrClipIterator* iter, GrScalar tx, GrScalar ty, |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 92 | const GrRect* conservativeBounds) { |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 93 | fList.reset(); |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 94 | fRequiresAA = false; |
| 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 | int rectCount = 0; |
| 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 | // compute bounds for common case of series of intersecting rects. |
| 99 | bool isectRectValid = true; |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 100 | |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 101 | if (iter) { |
| 102 | for (iter->rewind(); !iter->isDone(); iter->next()) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 103 | Element& e = fList.push_back(); |
| 104 | e.fType = iter->getType(); |
| 105 | e.fOp = iter->getOp(); |
| robertphillips@google.com | fa1d291 | 2012-04-16 14:49:14 +0000 | [diff] [blame] | 106 | e.fDoAA = iter->getDoAA(); |
| 107 | if (e.fDoAA) { |
| 108 | fRequiresAA = true; |
| 109 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 110 | // iterators should not emit replace |
| robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame^] | 111 | GrAssert(SkRegion::kReplace_Op != e.fOp); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 112 | switch (e.fType) { |
| 113 | case kRect_ClipType: |
| 114 | iter->getRect(&e.fRect); |
| reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 115 | if (tx || ty) { |
| 116 | e.fRect.offset(tx, ty); |
| 117 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 118 | ++rectCount; |
| 119 | if (isectRectValid) { |
| robertphillips@google.com | 0f191f3 | 2012-04-25 15:23:36 +0000 | [diff] [blame^] | 120 | if (SkRegion::kIntersect_Op == e.fOp) { |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 121 | GrAssert(fList.count() <= 2); |
| 122 | if (fList.count() > 1) { |
| 123 | GrAssert(2 == rectCount); |
| 124 | rectCount = 1; |
| 125 | fList.pop_back(); |
| 126 | GrAssert(kRect_ClipType == fList.back().fType); |
| reed@google.com | 20efde7 | 2011-05-09 17:00:02 +0000 | [diff] [blame] | 127 | intersectWith(&fList.back().fRect, e.fRect); |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 128 | } |
| 129 | } else { |
| 130 | isectRectValid = false; |
| 131 | } |
| 132 | } |
| 133 | break; |
| 134 | case kPath_ClipType: |
| reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 135 | e.fPath = *iter->getPath(); |
| reed@google.com | 6f8f292 | 2011-03-04 22:27:10 +0000 | [diff] [blame] | 136 | if (tx || ty) { |
| 137 | e.fPath.offset(tx, ty); |
| 138 | } |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 139 | e.fPathFill = iter->getPathFill(); |
| 140 | isectRectValid = false; |
| 141 | break; |
| 142 | default: |
| 143 | GrCrash("Unknown clip element type."); |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 144 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 147 | fConservativeBoundsValid = false; |
| epoger@google.com | 17b7894 | 2011-08-26 14:40:38 +0000 | [diff] [blame] | 148 | if (isectRectValid && rectCount) { |
| 149 | fConservativeBounds = fList[0].fRect; |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 150 | fConservativeBoundsValid = true; |
| bsalomon@google.com | 0b50b2e | 2011-03-08 21:07:21 +0000 | [diff] [blame] | 151 | } else if (NULL != conservativeBounds) { |
| 152 | fConservativeBounds = *conservativeBounds; |
| 153 | fConservativeBoundsValid = true; |
| bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 154 | } |
| reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 155 | } |