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