epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
fmalita | c3b589a | 2014-06-05 12:40:07 -0700 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkPath.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/core/SkClipStack.h" |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 11 | #include "src/core/SkRectPriv.h" |
| 12 | #include "src/shaders/SkShaderBase.h" |
| 13 | |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 14 | #include <atomic> |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 15 | #include <new> |
| 16 | |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 17 | #if SK_SUPPORT_GPU |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 18 | #include "src/gpu/GrProxyProvider.h" |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 19 | #endif |
| 20 | |
commit-bot@chromium.org | 6f954b9 | 2014-02-27 17:39:46 +0000 | [diff] [blame] | 21 | SkClipStack::Element::Element(const Element& that) { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 22 | switch (that.getDeviceSpaceType()) { |
| 23 | case DeviceSpaceType::kEmpty: |
| 24 | fDeviceSpaceRRect.setEmpty(); |
| 25 | fDeviceSpacePath.reset(); |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 26 | fShader.reset(); |
commit-bot@chromium.org | 6f954b9 | 2014-02-27 17:39:46 +0000 | [diff] [blame] | 27 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 28 | case DeviceSpaceType::kRect: // Rect uses rrect |
| 29 | case DeviceSpaceType::kRRect: |
| 30 | fDeviceSpacePath.reset(); |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 31 | fShader.reset(); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 32 | fDeviceSpaceRRect = that.fDeviceSpaceRRect; |
commit-bot@chromium.org | 6f954b9 | 2014-02-27 17:39:46 +0000 | [diff] [blame] | 33 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 34 | case DeviceSpaceType::kPath: |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 35 | fShader.reset(); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 36 | fDeviceSpacePath.set(that.getDeviceSpacePath()); |
commit-bot@chromium.org | 6f954b9 | 2014-02-27 17:39:46 +0000 | [diff] [blame] | 37 | break; |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 38 | case DeviceSpaceType::kShader: |
| 39 | fDeviceSpacePath.reset(); |
| 40 | fShader = that.fShader; |
| 41 | break; |
commit-bot@chromium.org | 6f954b9 | 2014-02-27 17:39:46 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | fSaveCount = that.fSaveCount; |
| 45 | fOp = that.fOp; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 46 | fDeviceSpaceType = that.fDeviceSpaceType; |
commit-bot@chromium.org | 6f954b9 | 2014-02-27 17:39:46 +0000 | [diff] [blame] | 47 | fDoAA = that.fDoAA; |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 48 | fIsReplace = that.fIsReplace; |
commit-bot@chromium.org | 6f954b9 | 2014-02-27 17:39:46 +0000 | [diff] [blame] | 49 | fFiniteBoundType = that.fFiniteBoundType; |
| 50 | fFiniteBound = that.fFiniteBound; |
| 51 | fIsIntersectionOfRects = that.fIsIntersectionOfRects; |
| 52 | fGenID = that.fGenID; |
| 53 | } |
| 54 | |
Robert Phillips | 427966a | 2018-12-20 17:20:43 -0500 | [diff] [blame] | 55 | SkClipStack::Element::~Element() { |
| 56 | #if SK_SUPPORT_GPU |
| 57 | for (int i = 0; i < fKeysToInvalidate.count(); ++i) { |
| 58 | fProxyProvider->processInvalidUniqueKey(fKeysToInvalidate[i], nullptr, |
| 59 | GrProxyProvider::InvalidateGPUResource::kYes); |
| 60 | } |
| 61 | #endif |
| 62 | } |
| 63 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 64 | bool SkClipStack::Element::operator== (const Element& element) const { |
| 65 | if (this == &element) { |
| 66 | return true; |
| 67 | } |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 68 | if (fOp != element.fOp || fDeviceSpaceType != element.fDeviceSpaceType || |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 69 | fDoAA != element.fDoAA || fIsReplace != element.fIsReplace || |
| 70 | fSaveCount != element.fSaveCount) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 71 | return false; |
| 72 | } |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 73 | switch (fDeviceSpaceType) { |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 74 | case DeviceSpaceType::kShader: |
| 75 | return this->getShader() == element.getShader(); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 76 | case DeviceSpaceType::kPath: |
| 77 | return this->getDeviceSpacePath() == element.getDeviceSpacePath(); |
| 78 | case DeviceSpaceType::kRRect: |
| 79 | return fDeviceSpaceRRect == element.fDeviceSpaceRRect; |
| 80 | case DeviceSpaceType::kRect: |
| 81 | return this->getDeviceSpaceRect() == element.getDeviceSpaceRect(); |
| 82 | case DeviceSpaceType::kEmpty: |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 83 | return true; |
| 84 | default: |
| 85 | SkDEBUGFAIL("Unexpected type."); |
| 86 | return false; |
| 87 | } |
| 88 | } |
| 89 | |
Brian Salomon | 5c72796 | 2017-08-21 12:37:36 -0400 | [diff] [blame] | 90 | const SkRect& SkClipStack::Element::getBounds() const { |
| 91 | static const SkRect kEmpty = {0, 0, 0, 0}; |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 92 | static const SkRect kInfinite = SkRectPriv::MakeLargeS32(); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 93 | switch (fDeviceSpaceType) { |
| 94 | case DeviceSpaceType::kRect: // fallthrough |
| 95 | case DeviceSpaceType::kRRect: |
| 96 | return fDeviceSpaceRRect.getBounds(); |
| 97 | case DeviceSpaceType::kPath: |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 98 | return fDeviceSpacePath->getBounds(); |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 99 | case DeviceSpaceType::kShader: |
| 100 | // Shaders have infinite bounds since any pixel could have clipped or full coverage |
| 101 | // (which is different from wide-open, where every pixel has 1.0 coverage, or empty |
| 102 | // where every pixel has 0.0 coverage). |
| 103 | return kInfinite; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 104 | case DeviceSpaceType::kEmpty: |
Brian Salomon | 5c72796 | 2017-08-21 12:37:36 -0400 | [diff] [blame] | 105 | return kEmpty; |
| 106 | default: |
| 107 | SkDEBUGFAIL("Unexpected type."); |
| 108 | return kEmpty; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | bool SkClipStack::Element::contains(const SkRect& rect) const { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 113 | switch (fDeviceSpaceType) { |
| 114 | case DeviceSpaceType::kRect: |
| 115 | return this->getDeviceSpaceRect().contains(rect); |
| 116 | case DeviceSpaceType::kRRect: |
| 117 | return fDeviceSpaceRRect.contains(rect); |
| 118 | case DeviceSpaceType::kPath: |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 119 | return fDeviceSpacePath->conservativelyContainsRect(rect); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 120 | case DeviceSpaceType::kEmpty: |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 121 | case DeviceSpaceType::kShader: |
Brian Salomon | 5c72796 | 2017-08-21 12:37:36 -0400 | [diff] [blame] | 122 | return false; |
| 123 | default: |
| 124 | SkDEBUGFAIL("Unexpected type."); |
| 125 | return false; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | bool SkClipStack::Element::contains(const SkRRect& rrect) const { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 130 | switch (fDeviceSpaceType) { |
| 131 | case DeviceSpaceType::kRect: |
| 132 | return this->getDeviceSpaceRect().contains(rrect.getBounds()); |
| 133 | case DeviceSpaceType::kRRect: |
Brian Salomon | 5c72796 | 2017-08-21 12:37:36 -0400 | [diff] [blame] | 134 | // We don't currently have a generalized rrect-rrect containment. |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 135 | return fDeviceSpaceRRect.contains(rrect.getBounds()) || rrect == fDeviceSpaceRRect; |
| 136 | case DeviceSpaceType::kPath: |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 137 | return fDeviceSpacePath->conservativelyContainsRect(rrect.getBounds()); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 138 | case DeviceSpaceType::kEmpty: |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 139 | case DeviceSpaceType::kShader: |
Brian Salomon | 5c72796 | 2017-08-21 12:37:36 -0400 | [diff] [blame] | 140 | return false; |
| 141 | default: |
| 142 | SkDEBUGFAIL("Unexpected type."); |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 147 | void SkClipStack::Element::invertShapeFillType() { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 148 | switch (fDeviceSpaceType) { |
| 149 | case DeviceSpaceType::kRect: |
| 150 | fDeviceSpacePath.init(); |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 151 | fDeviceSpacePath->addRect(this->getDeviceSpaceRect()); |
| 152 | fDeviceSpacePath->setFillType(SkPathFillType::kInverseEvenOdd); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 153 | fDeviceSpaceType = DeviceSpaceType::kPath; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 154 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 155 | case DeviceSpaceType::kRRect: |
| 156 | fDeviceSpacePath.init(); |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 157 | fDeviceSpacePath->addRRect(fDeviceSpaceRRect); |
| 158 | fDeviceSpacePath->setFillType(SkPathFillType::kInverseEvenOdd); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 159 | fDeviceSpaceType = DeviceSpaceType::kPath; |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 160 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 161 | case DeviceSpaceType::kPath: |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 162 | fDeviceSpacePath->toggleInverseFillType(); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 163 | break; |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 164 | case DeviceSpaceType::kShader: |
| 165 | fShader = as_SB(fShader)->makeInvertAlpha(); |
| 166 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 167 | case DeviceSpaceType::kEmpty: |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 168 | // Should this set to an empty, inverse filled path? |
| 169 | break; |
| 170 | } |
| 171 | } |
| 172 | |
Brian Salomon | 5c72796 | 2017-08-21 12:37:36 -0400 | [diff] [blame] | 173 | void SkClipStack::Element::initCommon(int saveCount, SkClipOp op, bool doAA) { |
| 174 | fSaveCount = saveCount; |
| 175 | fOp = op; |
| 176 | fDoAA = doAA; |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 177 | fIsReplace = false; |
Brian Salomon | 5c72796 | 2017-08-21 12:37:36 -0400 | [diff] [blame] | 178 | // A default of inside-out and empty bounds means the bounds are effectively void as it |
| 179 | // indicates that nothing is known to be outside the clip. |
| 180 | fFiniteBoundType = kInsideOut_BoundsType; |
| 181 | fFiniteBound.setEmpty(); |
| 182 | fIsIntersectionOfRects = false; |
| 183 | fGenID = kInvalidGenID; |
| 184 | } |
| 185 | |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 186 | void SkClipStack::Element::initRect(int saveCount, const SkRect& rect, const SkMatrix& m, |
| 187 | SkClipOp op, bool doAA) { |
| 188 | if (m.rectStaysRect()) { |
| 189 | SkRect devRect; |
| 190 | m.mapRect(&devRect, rect); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 191 | fDeviceSpaceRRect.setRect(devRect); |
| 192 | fDeviceSpaceType = DeviceSpaceType::kRect; |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 193 | this->initCommon(saveCount, op, doAA); |
| 194 | return; |
Brian Salomon | 5c72796 | 2017-08-21 12:37:36 -0400 | [diff] [blame] | 195 | } |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 196 | SkPath path; |
| 197 | path.addRect(rect); |
| 198 | path.setIsVolatile(true); |
| 199 | this->initAsPath(saveCount, path, m, op, doAA); |
Brian Salomon | 5c72796 | 2017-08-21 12:37:36 -0400 | [diff] [blame] | 200 | } |
| 201 | |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 202 | void SkClipStack::Element::initRRect(int saveCount, const SkRRect& rrect, const SkMatrix& m, |
| 203 | SkClipOp op, bool doAA) { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 204 | if (rrect.transform(m, &fDeviceSpaceRRect)) { |
| 205 | SkRRect::Type type = fDeviceSpaceRRect.getType(); |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 206 | if (SkRRect::kRect_Type == type || SkRRect::kEmpty_Type == type) { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 207 | fDeviceSpaceType = DeviceSpaceType::kRect; |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 208 | } else { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 209 | fDeviceSpaceType = DeviceSpaceType::kRRect; |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 210 | } |
| 211 | this->initCommon(saveCount, op, doAA); |
| 212 | return; |
| 213 | } |
| 214 | SkPath path; |
| 215 | path.addRRect(rrect); |
| 216 | path.setIsVolatile(true); |
| 217 | this->initAsPath(saveCount, path, m, op, doAA); |
| 218 | } |
| 219 | |
| 220 | void SkClipStack::Element::initPath(int saveCount, const SkPath& path, const SkMatrix& m, |
| 221 | SkClipOp op, bool doAA) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 222 | if (!path.isInverseFillType()) { |
robertphillips | 2b6ab61 | 2015-01-05 12:22:14 -0800 | [diff] [blame] | 223 | SkRect r; |
| 224 | if (path.isRect(&r)) { |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 225 | this->initRect(saveCount, r, m, op, doAA); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | SkRect ovalRect; |
| 229 | if (path.isOval(&ovalRect)) { |
| 230 | SkRRect rrect; |
| 231 | rrect.setOval(ovalRect); |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 232 | this->initRRect(saveCount, rrect, m, op, doAA); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 233 | return; |
| 234 | } |
| 235 | } |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 236 | this->initAsPath(saveCount, path, m, op, doAA); |
| 237 | } |
| 238 | |
| 239 | void SkClipStack::Element::initAsPath(int saveCount, const SkPath& path, const SkMatrix& m, |
| 240 | SkClipOp op, bool doAA) { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 241 | path.transform(m, fDeviceSpacePath.init()); |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 242 | fDeviceSpacePath->setIsVolatile(true); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 243 | fDeviceSpaceType = DeviceSpaceType::kPath; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 244 | this->initCommon(saveCount, op, doAA); |
| 245 | } |
| 246 | |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 247 | void SkClipStack::Element::initShader(int saveCount, sk_sp<SkShader> shader) { |
| 248 | SkASSERT(shader); |
| 249 | fDeviceSpaceType = DeviceSpaceType::kShader; |
| 250 | fShader = std::move(shader); |
| 251 | this->initCommon(saveCount, SkClipOp::kIntersect, false); |
| 252 | } |
| 253 | |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 254 | void SkClipStack::Element::initReplaceRect(int saveCount, const SkRect& rect, bool doAA) { |
| 255 | fDeviceSpaceRRect.setRect(rect); |
| 256 | fDeviceSpaceType = DeviceSpaceType::kRect; |
| 257 | this->initCommon(saveCount, SkClipOp::kIntersect, doAA); |
| 258 | fIsReplace = true; |
| 259 | } |
| 260 | |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 261 | void SkClipStack::Element::asDeviceSpacePath(SkPath* path) const { |
| 262 | switch (fDeviceSpaceType) { |
| 263 | case DeviceSpaceType::kEmpty: |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 264 | path->reset(); |
| 265 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 266 | case DeviceSpaceType::kRect: |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 267 | path->reset(); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 268 | path->addRect(this->getDeviceSpaceRect()); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 269 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 270 | case DeviceSpaceType::kRRect: |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 271 | path->reset(); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 272 | path->addRRect(fDeviceSpaceRRect); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 273 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 274 | case DeviceSpaceType::kPath: |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 275 | *path = *fDeviceSpacePath; |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 276 | break; |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 277 | case DeviceSpaceType::kShader: |
| 278 | path->reset(); |
| 279 | path->addRect(SkRectPriv::MakeLargeS32()); |
| 280 | break; |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 281 | } |
jvanverth | 0deb2d9 | 2014-10-24 12:41:32 -0700 | [diff] [blame] | 282 | path->setIsVolatile(true); |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 283 | } |
| 284 | |
commit-bot@chromium.org | 9cb671a | 2014-02-16 14:45:45 +0000 | [diff] [blame] | 285 | void SkClipStack::Element::setEmpty() { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 286 | fDeviceSpaceType = DeviceSpaceType::kEmpty; |
commit-bot@chromium.org | 9cb671a | 2014-02-16 14:45:45 +0000 | [diff] [blame] | 287 | fFiniteBound.setEmpty(); |
| 288 | fFiniteBoundType = kNormal_BoundsType; |
| 289 | fIsIntersectionOfRects = false; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 290 | fDeviceSpaceRRect.setEmpty(); |
| 291 | fDeviceSpacePath.reset(); |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 292 | fShader.reset(); |
commit-bot@chromium.org | 9cb671a | 2014-02-16 14:45:45 +0000 | [diff] [blame] | 293 | fGenID = kEmptyGenID; |
| 294 | SkDEBUGCODE(this->checkEmpty();) |
| 295 | } |
| 296 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 297 | void SkClipStack::Element::checkEmpty() const { |
| 298 | SkASSERT(fFiniteBound.isEmpty()); |
| 299 | SkASSERT(kNormal_BoundsType == fFiniteBoundType); |
| 300 | SkASSERT(!fIsIntersectionOfRects); |
| 301 | SkASSERT(kEmptyGenID == fGenID); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 302 | SkASSERT(fDeviceSpaceRRect.isEmpty()); |
| 303 | SkASSERT(!fDeviceSpacePath.isValid()); |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 304 | SkASSERT(!fShader); |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 305 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 306 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 307 | bool SkClipStack::Element::canBeIntersectedInPlace(int saveCount, SkClipOp op) const { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 308 | if (DeviceSpaceType::kEmpty == fDeviceSpaceType && |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 309 | (SkClipOp::kDifference == op || SkClipOp::kIntersect == op)) { |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 310 | return true; |
| 311 | } |
| 312 | // Only clips within the same save/restore frame (as captured by |
| 313 | // the save count) can be merged |
| 314 | return fSaveCount == saveCount && |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 315 | SkClipOp::kIntersect == op && |
| 316 | (SkClipOp::kIntersect == fOp || this->isReplaceOp()); |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 317 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 318 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 319 | bool SkClipStack::Element::rectRectIntersectAllowed(const SkRect& newR, bool newAA) const { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 320 | SkASSERT(DeviceSpaceType::kRect == fDeviceSpaceType); |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 321 | |
| 322 | if (fDoAA == newAA) { |
| 323 | // if the AA setting is the same there is no issue |
| 324 | return true; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 327 | if (!SkRect::Intersects(this->getDeviceSpaceRect(), newR)) { |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 328 | // The calling code will correctly set the result to the empty clip |
| 329 | return true; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 330 | } |
| 331 | |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 332 | if (this->getDeviceSpaceRect().contains(newR)) { |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 333 | // if the new rect carves out a portion of the old one there is no |
| 334 | // issue |
| 335 | return true; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 336 | } |
| 337 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 338 | // So either the two overlap in some complex manner or newR contains oldR. |
| 339 | // In the first, case the edges will require different AA. In the second, |
| 340 | // the AA setting that would be carried forward is incorrect (e.g., oldR |
| 341 | // is AA while newR is BW but since newR contains oldR, oldR will be |
| 342 | // drawn BW) since the new AA setting will predominate. |
| 343 | return false; |
| 344 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 345 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 346 | // a mirror of combineBoundsRevDiff |
| 347 | void SkClipStack::Element::combineBoundsDiff(FillCombo combination, const SkRect& prevFinite) { |
| 348 | switch (combination) { |
| 349 | case kInvPrev_InvCur_FillCombo: |
| 350 | // In this case the only pixels that can remain set |
| 351 | // are inside the current clip rect since the extensions |
| 352 | // to infinity of both clips cancel out and whatever |
| 353 | // is outside of the current clip is removed |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 354 | fFiniteBoundType = kNormal_BoundsType; |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 355 | break; |
| 356 | case kInvPrev_Cur_FillCombo: |
| 357 | // In this case the current op is finite so the only pixels |
| 358 | // that aren't set are whatever isn't set in the previous |
| 359 | // clip and whatever this clip carves out |
| 360 | fFiniteBound.join(prevFinite); |
| 361 | fFiniteBoundType = kInsideOut_BoundsType; |
| 362 | break; |
| 363 | case kPrev_InvCur_FillCombo: |
| 364 | // In this case everything outside of this clip's bound |
| 365 | // is erased, so the only pixels that can remain set |
| 366 | // occur w/in the intersection of the two finite bounds |
| 367 | if (!fFiniteBound.intersect(prevFinite)) { |
csmartdalton | d50e240 | 2016-07-22 08:39:06 -0700 | [diff] [blame] | 368 | fFiniteBound.setEmpty(); |
| 369 | fGenID = kEmptyGenID; |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 370 | } |
csmartdalton | d50e240 | 2016-07-22 08:39:06 -0700 | [diff] [blame] | 371 | fFiniteBoundType = kNormal_BoundsType; |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 372 | break; |
| 373 | case kPrev_Cur_FillCombo: |
| 374 | // The most conservative result bound is that of the |
| 375 | // prior clip. This could be wildly incorrect if the |
| 376 | // second clip either exactly matches the first clip |
| 377 | // (which should yield the empty set) or reduces the |
| 378 | // size of the prior bound (e.g., if the second clip |
| 379 | // exactly matched the bottom half of the prior clip). |
| 380 | // We ignore these two possibilities. |
| 381 | fFiniteBound = prevFinite; |
| 382 | break; |
| 383 | default: |
| 384 | SkDEBUGFAIL("SkClipStack::Element::combineBoundsDiff Invalid fill combination"); |
| 385 | break; |
| 386 | } |
| 387 | } |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 388 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 389 | // a mirror of combineBoundsUnion |
| 390 | void SkClipStack::Element::combineBoundsIntersection(int combination, const SkRect& prevFinite) { |
| 391 | |
| 392 | switch (combination) { |
| 393 | case kInvPrev_InvCur_FillCombo: |
| 394 | // The only pixels that aren't writable in this case |
| 395 | // occur in the union of the two finite bounds |
| 396 | fFiniteBound.join(prevFinite); |
| 397 | fFiniteBoundType = kInsideOut_BoundsType; |
| 398 | break; |
| 399 | case kInvPrev_Cur_FillCombo: |
| 400 | // In this case the only pixels that will remain writeable |
| 401 | // are within the current clip |
| 402 | break; |
| 403 | case kPrev_InvCur_FillCombo: |
| 404 | // In this case the only pixels that will remain writeable |
| 405 | // are with the previous clip |
| 406 | fFiniteBound = prevFinite; |
| 407 | fFiniteBoundType = kNormal_BoundsType; |
| 408 | break; |
| 409 | case kPrev_Cur_FillCombo: |
| 410 | if (!fFiniteBound.intersect(prevFinite)) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 411 | this->setEmpty(); |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 412 | } |
| 413 | break; |
| 414 | default: |
| 415 | SkDEBUGFAIL("SkClipStack::Element::combineBoundsIntersection Invalid fill combination"); |
| 416 | break; |
| 417 | } |
| 418 | } |
| 419 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 420 | void SkClipStack::Element::updateBoundAndGenID(const Element* prior) { |
| 421 | // We set this first here but we may overwrite it later if we determine that the clip is |
| 422 | // either wide-open or empty. |
| 423 | fGenID = GetNextGenID(); |
| 424 | |
| 425 | // First, optimistically update the current Element's bound information |
| 426 | // with the current clip's bound |
| 427 | fIsIntersectionOfRects = false; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 428 | switch (fDeviceSpaceType) { |
| 429 | case DeviceSpaceType::kRect: |
| 430 | fFiniteBound = this->getDeviceSpaceRect(); |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 431 | fFiniteBoundType = kNormal_BoundsType; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 432 | |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 433 | if (this->isReplaceOp() || |
| 434 | (SkClipOp::kIntersect == fOp && nullptr == prior) || |
| 435 | (SkClipOp::kIntersect == fOp && prior->fIsIntersectionOfRects && |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 436 | prior->rectRectIntersectAllowed(this->getDeviceSpaceRect(), fDoAA))) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 437 | fIsIntersectionOfRects = true; |
| 438 | } |
| 439 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 440 | case DeviceSpaceType::kRRect: |
| 441 | fFiniteBound = fDeviceSpaceRRect.getBounds(); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 442 | fFiniteBoundType = kNormal_BoundsType; |
| 443 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 444 | case DeviceSpaceType::kPath: |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 445 | fFiniteBound = fDeviceSpacePath->getBounds(); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 446 | |
John Stiles | a008b0f | 2020-08-16 08:48:02 -0400 | [diff] [blame] | 447 | if (fDeviceSpacePath->isInverseFillType()) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 448 | fFiniteBoundType = kInsideOut_BoundsType; |
| 449 | } else { |
| 450 | fFiniteBoundType = kNormal_BoundsType; |
| 451 | } |
| 452 | break; |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 453 | case DeviceSpaceType::kShader: |
| 454 | // A shader is infinite. We don't act as wide-open here (which is an empty bounds with |
| 455 | // the inside out type). This is because when the bounds is empty and inside-out, we |
| 456 | // know there's full coverage everywhere. With a shader, there's *unknown* coverage |
| 457 | // everywhere. |
| 458 | fFiniteBound = SkRectPriv::MakeLargeS32(); |
| 459 | fFiniteBoundType = kNormal_BoundsType; |
| 460 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 461 | case DeviceSpaceType::kEmpty: |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 462 | SkDEBUGFAIL("We shouldn't get here with an empty element."); |
| 463 | break; |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 464 | } |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 465 | |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 466 | // Now determine the previous Element's bound information taking into |
| 467 | // account that there may be no previous clip |
| 468 | SkRect prevFinite; |
| 469 | SkClipStack::BoundsType prevType; |
| 470 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 471 | if (nullptr == prior) { |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 472 | // no prior clip means the entire plane is writable |
| 473 | prevFinite.setEmpty(); // there are no pixels that cannot be drawn to |
| 474 | prevType = kInsideOut_BoundsType; |
| 475 | } else { |
| 476 | prevFinite = prior->fFiniteBound; |
| 477 | prevType = prior->fFiniteBoundType; |
| 478 | } |
| 479 | |
| 480 | FillCombo combination = kPrev_Cur_FillCombo; |
| 481 | if (kInsideOut_BoundsType == fFiniteBoundType) { |
| 482 | combination = (FillCombo) (combination | 0x01); |
| 483 | } |
| 484 | if (kInsideOut_BoundsType == prevType) { |
| 485 | combination = (FillCombo) (combination | 0x02); |
| 486 | } |
| 487 | |
| 488 | SkASSERT(kInvPrev_InvCur_FillCombo == combination || |
| 489 | kInvPrev_Cur_FillCombo == combination || |
| 490 | kPrev_InvCur_FillCombo == combination || |
| 491 | kPrev_Cur_FillCombo == combination); |
| 492 | |
| 493 | // Now integrate with clip with the prior clips |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 494 | if (!this->isReplaceOp()) { |
| 495 | switch (fOp) { |
| 496 | case SkClipOp::kDifference: |
| 497 | this->combineBoundsDiff(combination, prevFinite); |
| 498 | break; |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 499 | case SkClipOp::kIntersect: |
| 500 | this->combineBoundsIntersection(combination, prevFinite); |
| 501 | break; |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 502 | default: |
| 503 | SkDebugf("SkClipOp error\n"); |
| 504 | SkASSERT(0); |
| 505 | break; |
| 506 | } |
| 507 | } // else Replace just ignores everything prior and should already have filled in bounds. |
bsalomon@google.com | 8a98e3b | 2012-11-29 21:05:13 +0000 | [diff] [blame] | 508 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 509 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 510 | // This constant determines how many Element's are allocated together as a block in |
robertphillips@google.com | f9a9084 | 2012-08-17 14:25:43 +0000 | [diff] [blame] | 511 | // the deque. As such it needs to balance allocating too much memory vs. |
| 512 | // incurring allocation/deallocation thrashing. It should roughly correspond to |
| 513 | // the deepest save/restore stack we expect to see. |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 514 | static const int kDefaultElementAllocCnt = 8; |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 515 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 516 | SkClipStack::SkClipStack() |
Mike Reed | 6e1e27b | 2017-03-10 02:51:59 +0000 | [diff] [blame] | 517 | : fDeque(sizeof(Element), kDefaultElementAllocCnt) |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 518 | , fSaveCount(0) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Mike Reed | d37f22b | 2017-03-09 23:56:25 -0500 | [diff] [blame] | 521 | SkClipStack::SkClipStack(void* storage, size_t size) |
| 522 | : fDeque(sizeof(Element), storage, size, kDefaultElementAllocCnt) |
| 523 | , fSaveCount(0) { |
| 524 | } |
| 525 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 526 | SkClipStack::SkClipStack(const SkClipStack& b) |
Mike Reed | 6e1e27b | 2017-03-10 02:51:59 +0000 | [diff] [blame] | 527 | : fDeque(sizeof(Element), kDefaultElementAllocCnt) { |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 528 | *this = b; |
| 529 | } |
| 530 | |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 531 | SkClipStack::~SkClipStack() { |
| 532 | reset(); |
| 533 | } |
| 534 | |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 535 | SkClipStack& SkClipStack::operator=(const SkClipStack& b) { |
| 536 | if (this == &b) { |
| 537 | return *this; |
| 538 | } |
| 539 | reset(); |
| 540 | |
| 541 | fSaveCount = b.fSaveCount; |
| 542 | SkDeque::F2BIter recIter(b.fDeque); |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 543 | for (const Element* element = (const Element*)recIter.next(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 544 | element != nullptr; |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 545 | element = (const Element*)recIter.next()) { |
| 546 | new (fDeque.push_back()) Element(*element); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | return *this; |
| 550 | } |
| 551 | |
| 552 | bool SkClipStack::operator==(const SkClipStack& b) const { |
commit-bot@chromium.org | 86b39f3 | 2014-01-06 16:54:20 +0000 | [diff] [blame] | 553 | if (this->getTopmostGenID() == b.getTopmostGenID()) { |
| 554 | return true; |
| 555 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 556 | if (fSaveCount != b.fSaveCount || |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 557 | fDeque.count() != b.fDeque.count()) { |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 558 | return false; |
| 559 | } |
| 560 | SkDeque::F2BIter myIter(fDeque); |
| 561 | SkDeque::F2BIter bIter(b.fDeque); |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 562 | const Element* myElement = (const Element*)myIter.next(); |
| 563 | const Element* bElement = (const Element*)bIter.next(); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 564 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 565 | while (myElement != nullptr && bElement != nullptr) { |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 566 | if (*myElement != *bElement) { |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 567 | return false; |
| 568 | } |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 569 | myElement = (const Element*)myIter.next(); |
| 570 | bElement = (const Element*)bIter.next(); |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 571 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 572 | return myElement == nullptr && bElement == nullptr; |
vandebo@chromium.org | 1e1c36f | 2011-05-03 16:26:09 +0000 | [diff] [blame] | 573 | } |
| 574 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 575 | void SkClipStack::reset() { |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 576 | // We used a placement new for each object in fDeque, so we're responsible |
| 577 | // for calling the destructor on each of them as well. |
| 578 | while (!fDeque.empty()) { |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 579 | Element* element = (Element*)fDeque.back(); |
| 580 | element->~Element(); |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 581 | fDeque.pop_back(); |
| 582 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 583 | |
| 584 | fSaveCount = 0; |
| 585 | } |
| 586 | |
| 587 | void SkClipStack::save() { |
| 588 | fSaveCount += 1; |
| 589 | } |
| 590 | |
| 591 | void SkClipStack::restore() { |
| 592 | fSaveCount -= 1; |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 593 | restoreTo(fSaveCount); |
| 594 | } |
| 595 | |
| 596 | void SkClipStack::restoreTo(int saveCount) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 597 | while (!fDeque.empty()) { |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 598 | Element* element = (Element*)fDeque.back(); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 599 | if (element->fSaveCount <= saveCount) { |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 600 | break; |
| 601 | } |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 602 | element->~Element(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 603 | fDeque.pop_back(); |
| 604 | } |
| 605 | } |
| 606 | |
Hal Canary | f3ee34f | 2017-02-07 16:58:28 -0500 | [diff] [blame] | 607 | SkRect SkClipStack::bounds(const SkIRect& deviceBounds) const { |
| 608 | // TODO: optimize this. |
| 609 | SkRect r; |
| 610 | SkClipStack::BoundsType bounds; |
| 611 | this->getBounds(&r, &bounds); |
| 612 | if (bounds == SkClipStack::kInsideOut_BoundsType) { |
| 613 | return SkRect::Make(deviceBounds); |
| 614 | } |
| 615 | return r.intersect(SkRect::Make(deviceBounds)) ? r : SkRect::MakeEmpty(); |
| 616 | } |
| 617 | |
| 618 | // TODO: optimize this. |
| 619 | bool SkClipStack::isEmpty(const SkIRect& r) const { return this->bounds(r).isEmpty(); } |
| 620 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 621 | void SkClipStack::getBounds(SkRect* canvFiniteBound, |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 622 | BoundsType* boundType, |
| 623 | bool* isIntersectionOfRects) const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 624 | SkASSERT(canvFiniteBound && boundType); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 625 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 626 | Element* element = (Element*)fDeque.back(); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 627 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 628 | if (nullptr == element) { |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 629 | // the clip is wide open - the infinite plane w/ no pixels un-writeable |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 630 | canvFiniteBound->setEmpty(); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 631 | *boundType = kInsideOut_BoundsType; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 632 | if (isIntersectionOfRects) { |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 633 | *isIntersectionOfRects = false; |
| 634 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 635 | return; |
| 636 | } |
| 637 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 638 | *canvFiniteBound = element->fFiniteBound; |
| 639 | *boundType = element->fFiniteBoundType; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 640 | if (isIntersectionOfRects) { |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 641 | *isIntersectionOfRects = element->fIsIntersectionOfRects; |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 642 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 643 | } |
| 644 | |
reed | 4d2cce4 | 2016-08-22 13:03:47 -0700 | [diff] [blame] | 645 | bool SkClipStack::internalQuickContains(const SkRect& rect) const { |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 646 | Iter iter(*this, Iter::kTop_IterStart); |
| 647 | const Element* element = iter.prev(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 648 | while (element != nullptr) { |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 649 | // TODO: Once expanding ops are removed, this condition is equiv. to op == kDifference. |
| 650 | if (SkClipOp::kIntersect != element->getOp() && !element->isReplaceOp()) { |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 651 | return false; |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 652 | } |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 653 | if (element->isInverseFilled()) { |
| 654 | // Part of 'rect' could be trimmed off by the inverse-filled clip element |
| 655 | if (SkRect::Intersects(element->getBounds(), rect)) { |
| 656 | return false; |
| 657 | } |
| 658 | } else { |
| 659 | if (!element->contains(rect)) { |
| 660 | return false; |
| 661 | } |
| 662 | } |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 663 | if (element->isReplaceOp()) { |
junov@chromium.org | 8cdf0f5 | 2012-12-12 17:58:15 +0000 | [diff] [blame] | 664 | break; |
| 665 | } |
| 666 | element = iter.prev(); |
| 667 | } |
| 668 | return true; |
| 669 | } |
| 670 | |
reed | 4d2cce4 | 2016-08-22 13:03:47 -0700 | [diff] [blame] | 671 | bool SkClipStack::internalQuickContains(const SkRRect& rrect) const { |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 672 | Iter iter(*this, Iter::kTop_IterStart); |
| 673 | const Element* element = iter.prev(); |
| 674 | while (element != nullptr) { |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 675 | // TODO: Once expanding ops are removed, this condition is equiv. to op == kDifference. |
| 676 | if (SkClipOp::kIntersect != element->getOp() && !element->isReplaceOp()) { |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 677 | return false; |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 678 | } |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 679 | if (element->isInverseFilled()) { |
| 680 | // Part of 'rrect' could be trimmed off by the inverse-filled clip element |
| 681 | if (SkRect::Intersects(element->getBounds(), rrect.getBounds())) { |
| 682 | return false; |
| 683 | } |
| 684 | } else { |
| 685 | if (!element->contains(rrect)) { |
| 686 | return false; |
| 687 | } |
| 688 | } |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 689 | if (element->isReplaceOp()) { |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 690 | break; |
| 691 | } |
| 692 | element = iter.prev(); |
| 693 | } |
| 694 | return true; |
| 695 | } |
| 696 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 697 | void SkClipStack::pushElement(const Element& element) { |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 698 | // Use reverse iterator instead of back because Rect path may need previous |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 699 | SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 700 | Element* prior = (Element*) iter.prev(); |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 701 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 702 | if (prior) { |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 703 | if (element.isReplaceOp()) { |
| 704 | this->restoreTo(fSaveCount - 1); |
| 705 | prior = (Element*) fDeque.back(); |
| 706 | } else if (prior->canBeIntersectedInPlace(fSaveCount, element.getOp())) { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 707 | switch (prior->fDeviceSpaceType) { |
| 708 | case Element::DeviceSpaceType::kEmpty: |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 709 | SkDEBUGCODE(prior->checkEmpty();) |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 710 | return; |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 711 | case Element::DeviceSpaceType::kShader: |
| 712 | if (Element::DeviceSpaceType::kShader == element.getDeviceSpaceType()) { |
| 713 | prior->fShader = SkShaders::Blend(SkBlendMode::kSrcIn, |
| 714 | element.fShader, prior->fShader); |
| 715 | Element* priorPrior = (Element*) iter.prev(); |
| 716 | prior->updateBoundAndGenID(priorPrior); |
| 717 | return; |
| 718 | } |
| 719 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 720 | case Element::DeviceSpaceType::kRect: |
| 721 | if (Element::DeviceSpaceType::kRect == element.getDeviceSpaceType()) { |
| 722 | if (prior->rectRectIntersectAllowed(element.getDeviceSpaceRect(), |
| 723 | element.isAA())) { |
commit-bot@chromium.org | 032a52f | 2014-02-21 20:09:13 +0000 | [diff] [blame] | 724 | SkRect isectRect; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 725 | if (!isectRect.intersect(prior->getDeviceSpaceRect(), |
| 726 | element.getDeviceSpaceRect())) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 727 | prior->setEmpty(); |
| 728 | return; |
| 729 | } |
| 730 | |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 731 | prior->fDeviceSpaceRRect.setRect(isectRect); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 732 | prior->fDoAA = element.isAA(); |
| 733 | Element* priorPrior = (Element*) iter.prev(); |
| 734 | prior->updateBoundAndGenID(priorPrior); |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 735 | return; |
| 736 | } |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 737 | break; |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 738 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 739 | [[fallthrough]]; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 740 | default: |
| 741 | if (!SkRect::Intersects(prior->getBounds(), element.getBounds())) { |
| 742 | prior->setEmpty(); |
robertphillips@google.com | 08eacc1 | 2012-08-02 12:49:00 +0000 | [diff] [blame] | 743 | return; |
| 744 | } |
commit-bot@chromium.org | 6fbe54c | 2013-06-11 11:01:48 +0000 | [diff] [blame] | 745 | break; |
| 746 | } |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 749 | Element* newElement = new (fDeque.push_back()) Element(element); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 750 | newElement->updateBoundAndGenID(prior); |
| 751 | } |
| 752 | |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 753 | void SkClipStack::clipRRect(const SkRRect& rrect, const SkMatrix& matrix, SkClipOp op, bool doAA) { |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 754 | Element element(fSaveCount, rrect, matrix, op, doAA); |
| 755 | this->pushElement(element); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 758 | void SkClipStack::clipRect(const SkRect& rect, const SkMatrix& matrix, SkClipOp op, bool doAA) { |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 759 | Element element(fSaveCount, rect, matrix, op, doAA); |
| 760 | this->pushElement(element); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 763 | void SkClipStack::clipPath(const SkPath& path, const SkMatrix& matrix, SkClipOp op, |
Brian Salomon | a3b45d4 | 2016-10-03 11:36:16 -0400 | [diff] [blame] | 764 | bool doAA) { |
Brian Salomon | 2e86634 | 2017-08-28 12:38:58 -0400 | [diff] [blame] | 765 | Element element(fSaveCount, path, matrix, op, doAA); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 766 | this->pushElement(element); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 769 | void SkClipStack::clipShader(sk_sp<SkShader> shader) { |
| 770 | Element element(fSaveCount, std::move(shader)); |
| 771 | this->pushElement(element); |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 772 | } |
| 773 | |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 774 | void SkClipStack::replaceClip(const SkRect& rect, bool doAA) { |
| 775 | Element element(fSaveCount, rect, doAA); |
| 776 | this->pushElement(element); |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 777 | } |
| 778 | |
reed@google.com | 0557d9e | 2012-08-16 15:59:59 +0000 | [diff] [blame] | 779 | void SkClipStack::clipEmpty() { |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 780 | Element* element = (Element*) fDeque.back(); |
robertphillips@google.com | 63ae1cf | 2012-08-17 13:53:05 +0000 | [diff] [blame] | 781 | |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 782 | if (element && element->canBeIntersectedInPlace(fSaveCount, SkClipOp::kIntersect)) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 783 | element->setEmpty(); |
reed@google.com | 0557d9e | 2012-08-16 15:59:59 +0000 | [diff] [blame] | 784 | } |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 785 | new (fDeque.push_back()) Element(fSaveCount); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 786 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 787 | ((Element*)fDeque.back())->fGenID = kEmptyGenID; |
reed@google.com | 0557d9e | 2012-08-16 15:59:59 +0000 | [diff] [blame] | 788 | } |
| 789 | |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 790 | /////////////////////////////////////////////////////////////////////////////// |
| 791 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 792 | SkClipStack::Iter::Iter() : fStack(nullptr) { |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 793 | } |
| 794 | |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 795 | SkClipStack::Iter::Iter(const SkClipStack& stack, IterStart startLoc) |
| 796 | : fStack(&stack) { |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 797 | this->reset(stack, startLoc); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 798 | } |
| 799 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 800 | const SkClipStack::Element* SkClipStack::Iter::next() { |
| 801 | return (const SkClipStack::Element*)fIter.next(); |
reed@google.com | 5c3d147 | 2011-02-22 19:12:23 +0000 | [diff] [blame] | 802 | } |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 803 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 804 | const SkClipStack::Element* SkClipStack::Iter::prev() { |
| 805 | return (const SkClipStack::Element*)fIter.prev(); |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 808 | const SkClipStack::Element* SkClipStack::Iter::skipToTopmost(SkClipOp op) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 809 | if (nullptr == fStack) { |
| 810 | return nullptr; |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | fIter.reset(fStack->fDeque, SkDeque::Iter::kBack_IterStart); |
| 814 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 815 | const SkClipStack::Element* element = nullptr; |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 816 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 817 | for (element = (const SkClipStack::Element*) fIter.prev(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 818 | element; |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 819 | element = (const SkClipStack::Element*) fIter.prev()) { |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 820 | |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 821 | if (op == element->fOp) { |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 822 | // The Deque's iterator is actually one pace ahead of the |
bsalomon@google.com | 9128edc | 2012-11-29 18:58:19 +0000 | [diff] [blame] | 823 | // returned value. So while "element" is the element we want to |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 824 | // return, the iterator is actually pointing at (and will |
| 825 | // return on the next "next" or "prev" call) the element |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 826 | // in front of it in the deque. Bump the iterator forward a |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 827 | // step so we get the expected result. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 828 | if (nullptr == fIter.next()) { |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 829 | // The reverse iterator has run off the front of the deque |
| 830 | // (i.e., the "op" clip is the first clip) and can't |
| 831 | // recover. Reset the iterator to start at the front. |
| 832 | fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart); |
| 833 | } |
| 834 | break; |
| 835 | } |
| 836 | } |
| 837 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 838 | if (nullptr == element) { |
robertphillips@google.com | 5836b6d | 2012-07-18 12:06:15 +0000 | [diff] [blame] | 839 | // There were no "op" clips |
| 840 | fIter.reset(fStack->fDeque, SkDeque::Iter::kFront_IterStart); |
| 841 | } |
| 842 | |
| 843 | return this->next(); |
| 844 | } |
| 845 | |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 846 | void SkClipStack::Iter::reset(const SkClipStack& stack, IterStart startLoc) { |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 847 | fStack = &stack; |
robertphillips@google.com | 52cb2c7 | 2012-07-16 18:52:29 +0000 | [diff] [blame] | 848 | fIter.reset(stack.fDeque, static_cast<SkDeque::Iter::IterStart>(startLoc)); |
bsalomon@google.com | d302f14 | 2011-03-03 13:54:13 +0000 | [diff] [blame] | 849 | } |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 850 | |
| 851 | // helper method |
| 852 | void SkClipStack::getConservativeBounds(int offsetX, |
| 853 | int offsetY, |
| 854 | int maxWidth, |
| 855 | int maxHeight, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 856 | SkRect* devBounds, |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 857 | bool* isIntersectionOfRects) const { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 858 | SkASSERT(devBounds); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 859 | |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 860 | devBounds->setLTRB(0, 0, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 861 | SkIntToScalar(maxWidth), SkIntToScalar(maxHeight)); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 862 | |
| 863 | SkRect temp; |
| 864 | SkClipStack::BoundsType boundType; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 865 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 866 | // temp starts off in canvas space here |
robertphillips@google.com | 4c2a2f7 | 2012-07-24 22:07:50 +0000 | [diff] [blame] | 867 | this->getBounds(&temp, &boundType, isIntersectionOfRects); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 868 | if (SkClipStack::kInsideOut_BoundsType == boundType) { |
| 869 | return; |
| 870 | } |
| 871 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 872 | // but is converted to device space here |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 873 | temp.offset(SkIntToScalar(offsetX), SkIntToScalar(offsetY)); |
| 874 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 875 | if (!devBounds->intersect(temp)) { |
| 876 | devBounds->setEmpty(); |
robertphillips@google.com | 607fe07 | 2012-07-24 13:54:00 +0000 | [diff] [blame] | 877 | } |
| 878 | } |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 879 | |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 880 | bool SkClipStack::isRRect(const SkRect& bounds, SkRRect* rrect, bool* aa) const { |
Brian Salomon | 1c0b05a | 2019-04-19 15:37:28 -0400 | [diff] [blame] | 881 | const Element* back = static_cast<const Element*>(fDeque.back()); |
| 882 | if (!back) { |
| 883 | // TODO: return bounds? |
Brian Salomon | 3bfba2a | 2018-01-23 15:12:07 -0500 | [diff] [blame] | 884 | return false; |
| 885 | } |
Brian Salomon | 1c0b05a | 2019-04-19 15:37:28 -0400 | [diff] [blame] | 886 | // First check if the entire stack is known to be a rect by the top element. |
| 887 | if (back->fIsIntersectionOfRects && back->fFiniteBoundType == BoundsType::kNormal_BoundsType) { |
| 888 | rrect->setRect(back->fFiniteBound); |
| 889 | *aa = back->isAA(); |
| 890 | return true; |
| 891 | } |
| 892 | |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 893 | if (back->getDeviceSpaceType() != SkClipStack::Element::DeviceSpaceType::kRect && |
| 894 | back->getDeviceSpaceType() != SkClipStack::Element::DeviceSpaceType::kRRect) { |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 895 | return false; |
| 896 | } |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 897 | if (back->isReplaceOp()) { |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 898 | *rrect = back->asDeviceSpaceRRect(); |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 899 | *aa = back->isAA(); |
| 900 | return true; |
| 901 | } |
| 902 | |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 903 | if (back->getOp() == SkClipOp::kIntersect) { |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 904 | SkRect backBounds; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 905 | if (!backBounds.intersect(bounds, back->asDeviceSpaceRRect().rect())) { |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 906 | return false; |
| 907 | } |
Brian Salomon | 3061d27 | 2019-04-22 11:59:48 -0400 | [diff] [blame] | 908 | // We limit to 17 elements. This means the back element will be bounds checked at most 16 |
Brian Salomon | 1c0b05a | 2019-04-19 15:37:28 -0400 | [diff] [blame] | 909 | // times if it is an rrect. |
| 910 | int cnt = fDeque.count(); |
Brian Salomon | 3061d27 | 2019-04-22 11:59:48 -0400 | [diff] [blame] | 911 | if (cnt > 17) { |
Brian Salomon | 1c0b05a | 2019-04-19 15:37:28 -0400 | [diff] [blame] | 912 | return false; |
| 913 | } |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 914 | if (cnt > 1) { |
| 915 | SkDeque::Iter iter(fDeque, SkDeque::Iter::kBack_IterStart); |
| 916 | SkAssertResult(static_cast<const Element*>(iter.prev()) == back); |
| 917 | while (const Element* prior = (const Element*)iter.prev()) { |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 918 | // TODO: Once expanding clip ops are removed, this is equiv. to op == kDifference |
| 919 | if ((prior->getOp() != SkClipOp::kIntersect && !prior->isReplaceOp()) || |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 920 | !prior->contains(backBounds)) { |
| 921 | return false; |
| 922 | } |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 923 | if (prior->isReplaceOp()) { |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 924 | break; |
| 925 | } |
| 926 | } |
| 927 | } |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 928 | *rrect = back->asDeviceSpaceRRect(); |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 929 | *aa = back->isAA(); |
| 930 | return true; |
| 931 | } |
| 932 | return false; |
| 933 | } |
| 934 | |
Robert Phillips | 806be2d | 2017-06-28 15:23:59 -0400 | [diff] [blame] | 935 | uint32_t SkClipStack::GetNextGenID() { |
Mike Klein | 0ec1c57 | 2018-12-04 11:52:51 -0500 | [diff] [blame] | 936 | // 0-2 are reserved for invalid, empty & wide-open |
| 937 | static const uint32_t kFirstUnreservedGenID = 3; |
| 938 | static std::atomic<uint32_t> nextID{kFirstUnreservedGenID}; |
| 939 | |
Robert Phillips | 806be2d | 2017-06-28 15:23:59 -0400 | [diff] [blame] | 940 | uint32_t id; |
| 941 | do { |
Adlai Holler | 4888cda | 2020-11-06 16:37:37 -0500 | [diff] [blame] | 942 | id = nextID.fetch_add(1, std::memory_order_relaxed); |
Robert Phillips | 806be2d | 2017-06-28 15:23:59 -0400 | [diff] [blame] | 943 | } while (id < kFirstUnreservedGenID); |
| 944 | return id; |
robertphillips@google.com | 46f9350 | 2012-08-07 15:38:08 +0000 | [diff] [blame] | 945 | } |
robertphillips@google.com | 73e7102 | 2012-08-09 18:10:49 +0000 | [diff] [blame] | 946 | |
Robert Phillips | 806be2d | 2017-06-28 15:23:59 -0400 | [diff] [blame] | 947 | uint32_t SkClipStack::getTopmostGenID() const { |
robertphillips@google.com | 73e7102 | 2012-08-09 18:10:49 +0000 | [diff] [blame] | 948 | if (fDeque.empty()) { |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 949 | return kWideOpenGenID; |
robertphillips@google.com | 73e7102 | 2012-08-09 18:10:49 +0000 | [diff] [blame] | 950 | } |
| 951 | |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 952 | const Element* back = static_cast<const Element*>(fDeque.back()); |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 953 | if (kInsideOut_BoundsType == back->fFiniteBoundType && back->fFiniteBound.isEmpty() && |
| 954 | Element::DeviceSpaceType::kShader != back->fDeviceSpaceType) { |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 955 | return kWideOpenGenID; |
| 956 | } |
| 957 | |
| 958 | return back->getGenID(); |
robertphillips@google.com | 73e7102 | 2012-08-09 18:10:49 +0000 | [diff] [blame] | 959 | } |
bsalomon | b6b0252 | 2014-06-09 07:59:06 -0700 | [diff] [blame] | 960 | |
djsollen | efe46d2 | 2016-04-29 06:41:35 -0700 | [diff] [blame] | 961 | #ifdef SK_DEBUG |
bsalomon | b6b0252 | 2014-06-09 07:59:06 -0700 | [diff] [blame] | 962 | void SkClipStack::Element::dump() const { |
| 963 | static const char* kTypeStrings[] = { |
| 964 | "empty", |
| 965 | "rect", |
| 966 | "rrect", |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 967 | "path", |
| 968 | "shader" |
bsalomon | b6b0252 | 2014-06-09 07:59:06 -0700 | [diff] [blame] | 969 | }; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 970 | static_assert(0 == static_cast<int>(DeviceSpaceType::kEmpty), "enum mismatch"); |
| 971 | static_assert(1 == static_cast<int>(DeviceSpaceType::kRect), "enum mismatch"); |
| 972 | static_assert(2 == static_cast<int>(DeviceSpaceType::kRRect), "enum mismatch"); |
| 973 | static_assert(3 == static_cast<int>(DeviceSpaceType::kPath), "enum mismatch"); |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 974 | static_assert(4 == static_cast<int>(DeviceSpaceType::kShader), "enum mismatch"); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 975 | static_assert(SK_ARRAY_COUNT(kTypeStrings) == kTypeCnt, "enum mismatch"); |
bsalomon | b6b0252 | 2014-06-09 07:59:06 -0700 | [diff] [blame] | 976 | |
Michael Ludwig | a0438e6 | 2021-08-06 11:19:26 -0400 | [diff] [blame] | 977 | const char* opName = this->isReplaceOp() ? "replace" : |
| 978 | (fOp == SkClipOp::kDifference ? "difference" : "intersect"); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 979 | SkDebugf("Type: %s, Op: %s, AA: %s, Save Count: %d\n", kTypeStrings[(int)fDeviceSpaceType], |
Michael Ludwig | d59d3e1 | 2021-08-05 16:39:18 -0400 | [diff] [blame] | 980 | opName, (fDoAA ? "yes" : "no"), fSaveCount); |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 981 | switch (fDeviceSpaceType) { |
| 982 | case DeviceSpaceType::kEmpty: |
bsalomon | b6b0252 | 2014-06-09 07:59:06 -0700 | [diff] [blame] | 983 | SkDebugf("\n"); |
| 984 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 985 | case DeviceSpaceType::kRect: |
| 986 | this->getDeviceSpaceRect().dump(); |
bsalomon | b6b0252 | 2014-06-09 07:59:06 -0700 | [diff] [blame] | 987 | SkDebugf("\n"); |
| 988 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 989 | case DeviceSpaceType::kRRect: |
| 990 | this->getDeviceSpaceRRect().dump(); |
bsalomon | b6b0252 | 2014-06-09 07:59:06 -0700 | [diff] [blame] | 991 | SkDebugf("\n"); |
| 992 | break; |
Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 993 | case DeviceSpaceType::kPath: |
Mike Reed | edb22ec | 2020-12-06 14:08:41 -0500 | [diff] [blame] | 994 | this->getDeviceSpacePath().dump(nullptr, false); |
bsalomon | b6b0252 | 2014-06-09 07:59:06 -0700 | [diff] [blame] | 995 | break; |
Michael Ludwig | 4e221bd | 2020-06-05 11:29:36 -0400 | [diff] [blame] | 996 | case DeviceSpaceType::kShader: |
| 997 | // SkShaders don't provide much introspection that's worth while. |
| 998 | break; |
bsalomon | b6b0252 | 2014-06-09 07:59:06 -0700 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | void SkClipStack::dump() const { |
| 1003 | B2TIter iter(*this); |
| 1004 | const Element* e; |
| 1005 | while ((e = iter.next())) { |
| 1006 | e->dump(); |
| 1007 | SkDebugf("\n"); |
| 1008 | } |
| 1009 | } |
| 1010 | #endif |