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