robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1 | /* |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 2 | * Copyright 2016 Google Inc. |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 8 | #include "GrClipStackClip.h" |
| 9 | |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 10 | #include "GrAppliedClip.h" |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 11 | #include "GrContextPriv.h" |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 12 | #include "GrDrawingManager.h" |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 13 | #include "GrRenderTargetContextPriv.h" |
csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 14 | #include "GrFixedClip.h" |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 15 | #include "GrGpuResourcePriv.h" |
csmartdalton | 28341fa | 2016-08-17 10:00:21 -0700 | [diff] [blame] | 16 | #include "GrRenderTargetPriv.h" |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 17 | #include "GrStencilAttachment.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 18 | #include "GrSWMaskHelper.h" |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame^] | 19 | #include "GrTextureProxy.h" |
egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 20 | #include "effects/GrConvexPolyEffect.h" |
| 21 | #include "effects/GrRRectEffect.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 22 | #include "effects/GrTextureDomain.h" |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 23 | #include "SkClipOpPriv.h" |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 24 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 25 | typedef SkClipStack::Element Element; |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 26 | typedef GrReducedClip::InitialState InitialState; |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 27 | typedef GrReducedClip::ElementList ElementList; |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 28 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 29 | static const int kMaxAnalyticElements = 4; |
| 30 | |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 31 | bool GrClipStackClip::quickContains(const SkRect& rect) const { |
reed | 4d2cce4 | 2016-08-22 13:03:47 -0700 | [diff] [blame] | 32 | if (!fStack || fStack->isWideOpen()) { |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 33 | return true; |
| 34 | } |
| 35 | return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()), |
| 36 | SkIntToScalar(fOrigin.y()))); |
| 37 | } |
| 38 | |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 39 | bool GrClipStackClip::quickContains(const SkRRect& rrect) const { |
reed | 4d2cce4 | 2016-08-22 13:03:47 -0700 | [diff] [blame] | 40 | if (!fStack || fStack->isWideOpen()) { |
bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 41 | return true; |
| 42 | } |
| 43 | return fStack->quickContains(rrect.makeOffset(SkIntToScalar(fOrigin.fX), |
| 44 | SkIntToScalar(fOrigin.fY))); |
| 45 | } |
| 46 | |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 47 | bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const { |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 48 | if (!fStack) { |
| 49 | return false; |
| 50 | } |
| 51 | const SkRect* rtBounds = &origRTBounds; |
| 52 | SkRect tempRTBounds; |
| 53 | bool origin = fOrigin.fX || fOrigin.fY; |
| 54 | if (origin) { |
| 55 | tempRTBounds = origRTBounds; |
| 56 | tempRTBounds.offset(SkIntToScalar(fOrigin.fX), SkIntToScalar(fOrigin.fY)); |
| 57 | rtBounds = &tempRTBounds; |
| 58 | } |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 59 | bool isAA; |
| 60 | if (fStack->isRRect(*rtBounds, rr, &isAA)) { |
| 61 | *aa = GrBoolToAA(isAA); |
bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 62 | if (origin) { |
| 63 | rr->offset(-SkIntToScalar(fOrigin.fX), -SkIntToScalar(fOrigin.fY)); |
| 64 | } |
| 65 | return true; |
| 66 | } |
| 67 | return false; |
| 68 | } |
| 69 | |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 70 | void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult, |
| 71 | bool* isIntersectionOfRects) const { |
| 72 | if (!fStack) { |
| 73 | devResult->setXYWH(0, 0, width, height); |
| 74 | if (isIntersectionOfRects) { |
| 75 | *isIntersectionOfRects = true; |
| 76 | } |
| 77 | return; |
| 78 | } |
| 79 | SkRect devBounds; |
| 80 | fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds, |
| 81 | isIntersectionOfRects); |
| 82 | devBounds.roundOut(devResult); |
| 83 | } |
| 84 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 85 | //////////////////////////////////////////////////////////////////////////////// |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 86 | // set up the draw state to enable the aa clipping mask. |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 87 | static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result, |
| 88 | const SkIRect &devBound) { |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 89 | SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height()); |
Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 90 | return GrDeviceSpaceTextureDecalFragmentProcessor::Make(result, domainTexels, |
| 91 | {devBound.fLeft, devBound.fTop}); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 92 | } |
| 93 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 94 | // Does the path in 'element' require SW rendering? If so, return true (and, |
| 95 | // optionally, set 'prOut' to NULL. If not, return false (and, optionally, set |
| 96 | // 'prOut' to the non-SW path renderer that will do the job). |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 97 | bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context, |
| 98 | bool hasUserStencilSettings, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 99 | const GrRenderTargetContext* renderTargetContext, |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 100 | const SkMatrix& viewMatrix, |
| 101 | const Element* element, |
| 102 | GrPathRenderer** prOut, |
| 103 | bool needsStencil) { |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 104 | if (Element::kRect_Type == element->getType()) { |
| 105 | // rects can always be drawn directly w/o using the software path |
| 106 | // TODO: skip rrects once we're drawing them directly. |
| 107 | if (prOut) { |
| 108 | *prOut = nullptr; |
| 109 | } |
| 110 | return false; |
| 111 | } else { |
| 112 | // We shouldn't get here with an empty clip element. |
| 113 | SkASSERT(Element::kEmpty_Type != element->getType()); |
robertphillips | 5c3ea4c | 2015-10-26 08:33:10 -0700 | [diff] [blame] | 114 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 115 | // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer |
| 116 | SkPath path; |
| 117 | element->asPath(&path); |
| 118 | if (path.isInverseFillType()) { |
| 119 | path.toggleInverseFillType(); |
| 120 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 121 | |
Brian Salomon | 82125e9 | 2016-12-10 09:35:48 -0500 | [diff] [blame] | 122 | GrPathRendererChain::DrawType type = |
| 123 | needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor |
| 124 | : GrPathRendererChain::DrawType::kColor; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 125 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 126 | GrShape shape(path, GrStyle::SimpleFill()); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 127 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 128 | canDrawArgs.fShaderCaps = context->caps()->shaderCaps(); |
| 129 | canDrawArgs.fViewMatrix = &viewMatrix; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 130 | canDrawArgs.fShape = &shape; |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 131 | if (!element->isAA()) { |
| 132 | canDrawArgs.fAAType = GrAAType::kNone; |
| 133 | } else if (renderTargetContext->isUnifiedMultisampled()) { |
| 134 | canDrawArgs.fAAType = GrAAType::kMSAA; |
| 135 | } else if (renderTargetContext->isStencilBufferMultisampled()){ |
| 136 | canDrawArgs.fAAType = GrAAType::kMixedSamples; |
| 137 | } else { |
| 138 | canDrawArgs.fAAType = GrAAType::kCoverage; |
| 139 | } |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 140 | canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 141 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 142 | // the 'false' parameter disallows use of the SW path renderer |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 143 | GrPathRenderer* pr = |
| 144 | context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type); |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 145 | if (prOut) { |
| 146 | *prOut = pr; |
| 147 | } |
| 148 | return SkToBool(!pr); |
| 149 | } |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 150 | } |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 151 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 152 | /* |
| 153 | * This method traverses the clip stack to see if the GrSoftwarePathRenderer |
| 154 | * will be used on any element. If so, it returns true to indicate that the |
| 155 | * entire clip should be rendered in SW and then uploaded en masse to the gpu. |
| 156 | */ |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 157 | bool GrClipStackClip::UseSWOnlyPath(GrContext* context, |
| 158 | bool hasUserStencilSettings, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 159 | const GrRenderTargetContext* renderTargetContext, |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 160 | const GrReducedClip& reducedClip) { |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 161 | // TODO: generalize this function so that when |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 162 | // a clip gets complex enough it can just be done in SW regardless |
| 163 | // of whether it would invoke the GrSoftwarePathRenderer. |
skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 164 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 165 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 166 | // space. |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 167 | SkMatrix translate; |
| 168 | translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top())); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 169 | |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 170 | for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 171 | const Element* element = iter.get(); |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 172 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 173 | SkClipOp op = element->getOp(); |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 174 | bool invert = element->isInverseFilled(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 175 | bool needsStencil = invert || |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 176 | kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op; |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 177 | |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 178 | if (PathNeedsSWRenderer(context, hasUserStencilSettings, |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 179 | renderTargetContext, translate, element, nullptr, needsStencil)) { |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 180 | return true; |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 181 | } |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 182 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 183 | return false; |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 184 | } |
| 185 | |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 186 | static bool get_analytic_clip_processor(const ElementList& elements, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 187 | bool abortIfAA, |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 188 | const SkVector& clipToRTOffset, |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 189 | const SkRect& drawBounds, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 190 | sk_sp<GrFragmentProcessor>* resultFP) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 191 | SkRect boundsInClipSpace; |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 192 | boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 193 | SkASSERT(elements.count() <= kMaxAnalyticElements); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 194 | SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps; |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 195 | ElementList::Iter iter(elements); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 196 | while (iter.get()) { |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 197 | SkClipOp op = iter.get()->getOp(); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 198 | bool invert; |
| 199 | bool skip = false; |
| 200 | switch (op) { |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 201 | case kReplace_SkClipOp: |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 202 | SkASSERT(iter.get() == elements.head()); |
| 203 | // Fallthrough, handled same as intersect. |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 204 | case kIntersect_SkClipOp: |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 205 | invert = false; |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 206 | if (iter.get()->contains(boundsInClipSpace)) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 207 | skip = true; |
| 208 | } |
| 209 | break; |
Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 210 | case kDifference_SkClipOp: |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 211 | invert = true; |
| 212 | // We don't currently have a cheap test for whether a rect is fully outside an |
| 213 | // element's primitive, so don't attempt to set skip. |
| 214 | break; |
| 215 | default: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 216 | return false; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 217 | } |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 218 | if (!skip) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 219 | GrPrimitiveEdgeType edgeType; |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 220 | if (iter.get()->isAA()) { |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 221 | if (abortIfAA) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 222 | return false; |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 223 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 224 | edgeType = |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 225 | invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 226 | } else { |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 227 | edgeType = |
| 228 | invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 229 | } |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 230 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 231 | switch (iter.get()->getType()) { |
| 232 | case SkClipStack::Element::kPath_Type: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 233 | fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(), |
| 234 | &clipToRTOffset)); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 235 | break; |
| 236 | case SkClipStack::Element::kRRect_Type: { |
| 237 | SkRRect rrect = iter.get()->getRRect(); |
| 238 | rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 239 | fps.emplace_back(GrRRectEffect::Make(edgeType, rrect)); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | case SkClipStack::Element::kRect_Type: { |
| 243 | SkRect rect = iter.get()->getRect(); |
| 244 | rect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 245 | fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect)); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 246 | break; |
| 247 | } |
| 248 | default: |
| 249 | break; |
| 250 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 251 | if (!fps.back()) { |
| 252 | return false; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 255 | iter.next(); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 256 | } |
| 257 | |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 258 | *resultFP = nullptr; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 259 | if (fps.count()) { |
| 260 | *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count()); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 261 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 262 | return true; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 263 | } |
| 264 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 265 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 266 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 267 | // scissor, or entirely software |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 268 | bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| 269 | bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out) const { |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 270 | if (!fStack || fStack->isWideOpen()) { |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 271 | return true; |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 272 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 273 | |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 274 | SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), |
| 275 | renderTargetContext->height()); |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 276 | if (!devBounds.intersect(out->clippedDrawBounds())) { |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 280 | const SkScalar clipX = SkIntToScalar(fOrigin.x()), |
| 281 | clipY = SkIntToScalar(fOrigin.y()); |
csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 282 | |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 283 | SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY); |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 284 | const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds, |
Robert Phillips | ec2249f | 2016-11-09 08:54:35 -0500 | [diff] [blame] | 285 | renderTargetContext->priv().maxWindowRectangles()); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 286 | |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 287 | if (reducedClip.hasIBounds() && |
| 288 | !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) { |
| 289 | SkIRect scissorSpaceIBounds(reducedClip.ibounds()); |
| 290 | scissorSpaceIBounds.offset(-fOrigin); |
| 291 | out->addScissor(scissorSpaceIBounds); |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 292 | } |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 293 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 294 | if (!reducedClip.windowRectangles().empty()) { |
| 295 | out->addWindowRectangles(reducedClip.windowRectangles(), fOrigin, |
| 296 | GrWindowRectsState::Mode::kExclusive); |
| 297 | } |
| 298 | |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 299 | if (reducedClip.elements().isEmpty()) { |
| 300 | return InitialState::kAllIn == reducedClip.initialState(); |
| 301 | } |
| 302 | |
csmartdalton | 3affdc1 | 2016-10-28 12:01:10 -0700 | [diff] [blame] | 303 | #ifdef SK_DEBUG |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 304 | SkASSERT(reducedClip.hasIBounds()); |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 305 | SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(), |
| 306 | renderTargetContext->height()); |
csmartdalton | 3affdc1 | 2016-10-28 12:01:10 -0700 | [diff] [blame] | 307 | SkIRect clipIBounds = reducedClip.ibounds().makeOffset(-fOrigin.x(), -fOrigin.y()); |
| 308 | SkASSERT(rtIBounds.contains(clipIBounds)); // Mask shouldn't be larger than the RT. |
| 309 | #endif |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 310 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 311 | // An element count of 4 was chosen because of the common pattern in Blink of: |
| 312 | // isect RR |
| 313 | // diff RR |
| 314 | // isect convex_poly |
| 315 | // isect convex_poly |
| 316 | // when drawing rounded div borders. This could probably be tuned based on a |
| 317 | // configuration's relative costs of switching RTs to generate a mask vs |
| 318 | // longer shaders. |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 319 | if (reducedClip.elements().count() <= kMaxAnalyticElements) { |
cdalton | ede7574 | 2015-11-11 15:27:57 -0800 | [diff] [blame] | 320 | // When there are multiple samples we want to do per-sample clipping, not compute a |
| 321 | // fractional pixel coverage. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 322 | bool disallowAnalyticAA = renderTargetContext->isStencilBufferMultisampled(); |
| 323 | if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) { |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 324 | // With a single color sample, any coverage info is lost from color once it hits the |
| 325 | // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe |
| 326 | // is multisampled. |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame] | 327 | disallowAnalyticAA = useHWAA || hasUserStencilSettings; |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 328 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 329 | sk_sp<GrFragmentProcessor> clipFP; |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 330 | if (reducedClip.requiresAA() && |
| 331 | get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA, |
| 332 | {-clipX, -clipY}, devBounds, &clipFP)) { |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 333 | out->addCoverageFP(std::move(clipFP)); |
commit-bot@chromium.org | 65ee5f4 | 2014-02-04 17:49:48 +0000 | [diff] [blame] | 334 | return true; |
| 335 | } |
| 336 | } |
bsalomon@google.com | d3066bd | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 337 | |
cdalton | ede7574 | 2015-11-11 15:27:57 -0800 | [diff] [blame] | 338 | // If the stencil buffer is multisampled we can use it to do everything. |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 339 | if (!renderTargetContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) { |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 340 | sk_sp<GrTexture> result; |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 341 | if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 342 | // The clip geometry is complex enough that it will be more efficient to create it |
| 343 | // entirely in software |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame^] | 344 | result = CreateSoftwareClipMask(context, reducedClip); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 345 | } else { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 346 | result = CreateAlphaClipMask(context, reducedClip); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 347 | } |
| 348 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 349 | if (result) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 350 | // The mask's top left coord should be pinned to the rounded-out top left corner of |
| 351 | // clipSpace bounds. We determine the mask's position WRT to the render target here. |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 352 | SkIRect rtSpaceMaskBounds = reducedClip.ibounds(); |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 353 | rtSpaceMaskBounds.offset(-fOrigin); |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 354 | out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 355 | return true; |
| 356 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 357 | // if alpha clip mask creation fails fall through to the non-AA code paths |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 358 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 359 | |
Robert Phillips | ec2249f | 2016-11-09 08:54:35 -0500 | [diff] [blame] | 360 | GrRenderTarget* rt = renderTargetContext->accessRenderTarget(); |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 361 | if (!rt) { |
| 362 | return true; |
| 363 | } |
Robert Phillips | ec2249f | 2016-11-09 08:54:35 -0500 | [diff] [blame] | 364 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 365 | // use the stencil clip if we can't represent the clip as a rectangle. |
csmartdalton | 7cdda99 | 2016-11-01 07:03:03 -0700 | [diff] [blame] | 366 | if (!context->resourceProvider()->attachStencilAttachment(rt)) { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 367 | SkDebugf("WARNING: failed to attach stencil buffer for clip mask. Clip will be ignored.\n"); |
| 368 | return true; |
| 369 | } |
| 370 | |
csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 371 | // This relies on the property that a reduced sub-rect of the last clip will contain all the |
| 372 | // relevant window rectangles that were in the last clip. This subtle requirement will go away |
| 373 | // after clipping is overhauled. |
csmartdalton | 7cdda99 | 2016-11-01 07:03:03 -0700 | [diff] [blame] | 374 | if (renderTargetContext->priv().mustRenderClip(reducedClip.elementsGenID(), |
| 375 | reducedClip.ibounds(), fOrigin)) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 376 | reducedClip.drawStencilClipMask(context, renderTargetContext, fOrigin); |
csmartdalton | 7cdda99 | 2016-11-01 07:03:03 -0700 | [diff] [blame] | 377 | renderTargetContext->priv().setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(), |
| 378 | fOrigin); |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 379 | } |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 380 | out->addStencilClip(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 381 | return true; |
| 382 | } |
| 383 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 384 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 385 | // Create a 8-bit clip mask in alpha |
| 386 | |
| 387 | static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) { |
| 388 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 389 | GrUniqueKey::Builder builder(key, kDomain, 3); |
| 390 | builder[0] = clipGenID; |
csmartdalton | 3affdc1 | 2016-10-28 12:01:10 -0700 | [diff] [blame] | 391 | // SkToS16 because image filters outset layers to a size indicated by the filter, which can |
| 392 | // sometimes result in negative coordinates from clip space. |
| 393 | builder[1] = SkToS16(bounds.fLeft) | (SkToS16(bounds.fRight) << 16); |
| 394 | builder[2] = SkToS16(bounds.fTop) | (SkToS16(bounds.fBottom) << 16); |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 395 | } |
| 396 | |
csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 397 | sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context, |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 398 | const GrReducedClip& reducedClip) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 399 | GrResourceProvider* resourceProvider = context->resourceProvider(); |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 400 | GrUniqueKey key; |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 401 | GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key); |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 402 | if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) { |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 403 | return sk_sp<GrTexture>(texture); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 406 | sk_sp<GrRenderTargetContext> rtc(context->makeRenderTargetContextWithFallback( |
| 407 | SkBackingFit::kApprox, |
| 408 | reducedClip.width(), |
| 409 | reducedClip.height(), |
| 410 | kAlpha_8_GrPixelConfig, |
| 411 | nullptr)); |
| 412 | if (!rtc) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 413 | return nullptr; |
| 414 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 415 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 416 | if (!reducedClip.drawAlphaClipMask(rtc.get())) { |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 417 | return nullptr; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 420 | sk_sp<GrTexture> texture(rtc->asTexture()); |
Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 421 | if (!texture) { |
| 422 | return nullptr; |
| 423 | } |
| 424 | |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 425 | texture->resourcePriv().setUniqueKey(key); |
| 426 | return texture; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame^] | 429 | sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrContext* context, |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 430 | const GrReducedClip& reducedClip) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 431 | GrUniqueKey key; |
csmartdalton | 8d3f92a | 2016-08-17 09:39:38 -0700 | [diff] [blame] | 432 | GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key); |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame^] | 433 | if (GrTexture* texture = context->textureProvider()->findAndRefTextureByUniqueKey(key)) { |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 434 | return sk_sp<GrTexture>(texture); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 435 | } |
| 436 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 437 | // The mask texture may be larger than necessary. We round out the clip space bounds and pin |
| 438 | // the top left corner of the resulting rect to the top left of the texture. |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 439 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height()); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 440 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame^] | 441 | GrSWMaskHelper helper; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 442 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 443 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 444 | // space. |
| 445 | SkMatrix translate; |
csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 446 | translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top())); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 447 | |
csmartdalton | 3affdc1 | 2016-10-28 12:01:10 -0700 | [diff] [blame] | 448 | if (!helper.init(maskSpaceIBounds, &translate)) { |
| 449 | return nullptr; |
| 450 | } |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 451 | helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 452 | |
csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 453 | for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 454 | const Element* element = iter.get(); |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 455 | SkClipOp op = element->getOp(); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 456 | GrAA aa = GrBoolToAA(element->isAA()); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 457 | |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 458 | if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 459 | // Intersect and reverse difference require modifying pixels outside of the geometry |
| 460 | // that is being "drawn". In both cases we erase all the pixels outside of the geometry |
| 461 | // but leave the pixels inside the geometry alone. For reverse difference we invert all |
| 462 | // the pixels before clearing the ones outside the geometry. |
Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 463 | if (kReverseDifference_SkClipOp == op) { |
csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 464 | SkRect temp = SkRect::Make(reducedClip.ibounds()); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 465 | // invert the entire scene |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 466 | helper.drawRect(temp, SkRegion::kXOR_Op, GrAA::kNo, 0xFF); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 467 | } |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 468 | SkPath clipPath; |
| 469 | element->asPath(&clipPath); |
| 470 | clipPath.toggleInverseFillType(); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 471 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 472 | helper.drawShape(shape, SkRegion::kReplace_Op, aa, 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 473 | continue; |
| 474 | } |
| 475 | |
| 476 | // The other ops (union, xor, diff) only affect pixels inside |
| 477 | // the geometry so they can just be drawn normally |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 478 | if (Element::kRect_Type == element->getType()) { |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 479 | helper.drawRect(element->getRect(), (SkRegion::Op)op, aa, 0xFF); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 480 | } else { |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 481 | SkPath path; |
| 482 | element->asPath(&path); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 483 | GrShape shape(path, GrStyle::SimpleFill()); |
Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 484 | helper.drawShape(shape, (SkRegion::Op)op, aa, 0xFF); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame^] | 488 | sk_sp<GrTextureProxy> result(helper.toTexture(context, SkBackingFit::kApprox)); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 489 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame^] | 490 | GrTexture* tex = result->instantiate(context->textureProvider()); |
| 491 | if (!tex) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 492 | return nullptr; |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 493 | } |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 494 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame^] | 495 | tex->resourcePriv().setUniqueKey(key); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 496 | |
Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame^] | 497 | return sk_ref_sp(tex); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 498 | } |