| 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" |
| Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 12 | #include "GrDeferredProxyUploader.h" |
| robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 13 | #include "GrDrawingManager.h" |
| Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 14 | #include "GrRenderTargetContextPriv.h" |
| csmartdalton | 02fa32c | 2016-08-19 13:29:27 -0700 | [diff] [blame] | 15 | #include "GrFixedClip.h" |
| bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 16 | #include "GrGpuResourcePriv.h" |
| Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 17 | #include "GrResourceProvider.h" |
| egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 18 | #include "GrStencilAttachment.h" |
| robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 19 | #include "GrSWMaskHelper.h" |
| Robert Phillips | e305cc1f | 2016-12-14 12:19:05 -0500 | [diff] [blame] | 20 | #include "GrTextureProxy.h" |
| egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 21 | #include "effects/GrConvexPolyEffect.h" |
| 22 | #include "effects/GrRRectEffect.h" |
| egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 23 | #include "effects/GrTextureDomain.h" |
| Mike Reed | ebfce6d | 2016-12-12 10:02:12 -0500 | [diff] [blame] | 24 | #include "SkClipOpPriv.h" |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 25 | #include "SkMakeUnique.h" |
| 26 | #include "SkTaskGroup.h" |
| 27 | #include "SkTraceEvent.h" |
| bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 28 | |
| bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 29 | typedef SkClipStack::Element Element; |
| csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 30 | typedef GrReducedClip::InitialState InitialState; |
| csmartdalton | 77f2fae | 2016-08-08 09:55:06 -0700 | [diff] [blame] | 31 | typedef GrReducedClip::ElementList ElementList; |
| bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 32 | |
| robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 33 | static const int kMaxAnalyticElements = 4; |
| Brian Salomon | 19f0ed5 | 2017-01-06 13:54:58 -0500 | [diff] [blame] | 34 | const char GrClipStackClip::kMaskTestTag[] = "clip_mask"; |
| robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 35 | |
| csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 36 | bool GrClipStackClip::quickContains(const SkRect& rect) const { |
| reed | 4d2cce4 | 2016-08-22 13:03:47 -0700 | [diff] [blame] | 37 | if (!fStack || fStack->isWideOpen()) { |
| csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 38 | return true; |
| 39 | } |
| Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 40 | return fStack->quickContains(rect); |
| csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 43 | bool GrClipStackClip::quickContains(const SkRRect& rrect) const { |
| reed | 4d2cce4 | 2016-08-22 13:03:47 -0700 | [diff] [blame] | 44 | if (!fStack || fStack->isWideOpen()) { |
| bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 45 | return true; |
| 46 | } |
| Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 47 | return fStack->quickContains(rrect); |
| bsalomon | 7f0d9f3 | 2016-08-15 14:49:10 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 50 | bool GrClipStackClip::isRRect(const SkRect& origRTBounds, SkRRect* rr, GrAA* aa) const { |
| bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 51 | if (!fStack) { |
| 52 | return false; |
| 53 | } |
| 54 | const SkRect* rtBounds = &origRTBounds; |
| Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 55 | bool isAA; |
| 56 | if (fStack->isRRect(*rtBounds, rr, &isAA)) { |
| 57 | *aa = GrBoolToAA(isAA); |
| bsalomon | cb31e51 | 2016-08-26 10:48:19 -0700 | [diff] [blame] | 58 | return true; |
| 59 | } |
| 60 | return false; |
| 61 | } |
| 62 | |
| csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 63 | void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult, |
| 64 | bool* isIntersectionOfRects) const { |
| 65 | if (!fStack) { |
| 66 | devResult->setXYWH(0, 0, width, height); |
| 67 | if (isIntersectionOfRects) { |
| 68 | *isIntersectionOfRects = true; |
| 69 | } |
| 70 | return; |
| 71 | } |
| 72 | SkRect devBounds; |
| Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 73 | fStack->getConservativeBounds(0, 0, width, height, &devBounds, isIntersectionOfRects); |
| csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 74 | devBounds.roundOut(devResult); |
| 75 | } |
| 76 | |
| bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 77 | //////////////////////////////////////////////////////////////////////////////// |
| Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 78 | // set up the draw state to enable the aa clipping mask. |
| Brian Salomon | aff329b | 2017-08-11 09:40:37 -0400 | [diff] [blame] | 79 | static std::unique_ptr<GrFragmentProcessor> create_fp_for_mask(sk_sp<GrTextureProxy> mask, |
| 80 | const SkIRect& devBound) { |
| bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 81 | SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height()); |
| Robert Phillips | fbcef6e | 2017-06-15 12:07:18 -0400 | [diff] [blame] | 82 | return GrDeviceSpaceTextureDecalFragmentProcessor::Make(std::move(mask), domainTexels, |
| Brian Salomon | 2ebd0c8 | 2016-10-03 17:15:28 -0400 | [diff] [blame] | 83 | {devBound.fLeft, devBound.fTop}); |
| robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 86 | // Does the path in 'element' require SW rendering? If so, return true (and, |
| 87 | // optionally, set 'prOut' to NULL. If not, return false (and, optionally, set |
| 88 | // 'prOut' to the non-SW path renderer that will do the job). |
| csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 89 | bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context, |
| Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 90 | const SkIRect& scissorRect, |
| csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 91 | bool hasUserStencilSettings, |
| Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 92 | const GrRenderTargetContext* renderTargetContext, |
| csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 93 | const SkMatrix& viewMatrix, |
| 94 | const Element* element, |
| 95 | GrPathRenderer** prOut, |
| 96 | bool needsStencil) { |
| Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 97 | if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) { |
| robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 98 | // rects can always be drawn directly w/o using the software path |
| 99 | // TODO: skip rrects once we're drawing them directly. |
| 100 | if (prOut) { |
| 101 | *prOut = nullptr; |
| 102 | } |
| 103 | return false; |
| 104 | } else { |
| 105 | // We shouldn't get here with an empty clip element. |
| Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 106 | SkASSERT(Element::DeviceSpaceType::kEmpty != element->getDeviceSpaceType()); |
| robertphillips | 5c3ea4c | 2015-10-26 08:33:10 -0700 | [diff] [blame] | 107 | |
| robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 108 | // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer |
| 109 | SkPath path; |
| Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 110 | element->asDeviceSpacePath(&path); |
| robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 111 | if (path.isInverseFillType()) { |
| 112 | path.toggleInverseFillType(); |
| 113 | } |
| halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 114 | |
| Brian Salomon | 82125e9 | 2016-12-10 09:35:48 -0500 | [diff] [blame] | 115 | GrPathRendererChain::DrawType type = |
| 116 | needsStencil ? GrPathRendererChain::DrawType::kStencilAndColor |
| 117 | : GrPathRendererChain::DrawType::kColor; |
| halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 118 | |
| bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 119 | GrShape shape(path, GrStyle::SimpleFill()); |
| robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 120 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 121 | canDrawArgs.fCaps = context->caps(); |
| Chris Dalton | db91c6e | 2017-09-08 16:25:08 -0600 | [diff] [blame] | 122 | canDrawArgs.fClipConservativeBounds = &scissorRect; |
| robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 123 | canDrawArgs.fViewMatrix = &viewMatrix; |
| bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 124 | canDrawArgs.fShape = &shape; |
| Brian Salomon | 7c8460e | 2017-05-12 11:36:10 -0400 | [diff] [blame] | 125 | canDrawArgs.fAAType = GrChooseAAType(GrBoolToAA(element->isAA()), |
| 126 | renderTargetContext->fsaaType(), |
| Brian Salomon | e225b56 | 2017-06-14 13:00:03 -0400 | [diff] [blame] | 127 | GrAllowMixedSamples::kYes, |
| 128 | *context->caps()); |
| cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 129 | canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings; |
| robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 130 | |
| robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 131 | // the 'false' parameter disallows use of the SW path renderer |
| csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 132 | GrPathRenderer* pr = |
| 133 | context->contextPriv().drawingManager()->getPathRenderer(canDrawArgs, false, type); |
| robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 134 | if (prOut) { |
| 135 | *prOut = pr; |
| 136 | } |
| 137 | return SkToBool(!pr); |
| 138 | } |
| robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 139 | } |
| robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 140 | |
| robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 141 | /* |
| 142 | * This method traverses the clip stack to see if the GrSoftwarePathRenderer |
| 143 | * will be used on any element. If so, it returns true to indicate that the |
| 144 | * entire clip should be rendered in SW and then uploaded en masse to the gpu. |
| 145 | */ |
| csmartdalton | c6f411e | 2016-08-05 22:32:12 -0700 | [diff] [blame] | 146 | bool GrClipStackClip::UseSWOnlyPath(GrContext* context, |
| 147 | bool hasUserStencilSettings, |
| Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 148 | const GrRenderTargetContext* renderTargetContext, |
| csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 149 | const GrReducedClip& reducedClip) { |
| robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 150 | // TODO: generalize this function so that when |
| robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 151 | // a clip gets complex enough it can just be done in SW regardless |
| 152 | // of whether it would invoke the GrSoftwarePathRenderer. |
| skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 153 | |
| Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 154 | // If we're avoiding stencils, always use SW: |
| 155 | if (context->caps()->avoidStencilBuffers()) |
| 156 | return true; |
| 157 | |
| joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 158 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 159 | // space. |
| csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 160 | SkMatrix translate; |
| 161 | translate.setTranslate(SkIntToScalar(-reducedClip.left()), SkIntToScalar(-reducedClip.top())); |
| joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 162 | |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 163 | for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) { |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 164 | const Element* element = iter.get(); |
| robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 165 | |
| Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 166 | SkClipOp op = element->getOp(); |
| robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 167 | bool invert = element->isInverseFilled(); |
| halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 168 | bool needsStencil = invert || |
| Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 169 | kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op; |
| robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 170 | |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 171 | if (PathNeedsSWRenderer(context, reducedClip.scissor(), hasUserStencilSettings, |
| Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 172 | renderTargetContext, translate, element, nullptr, needsStencil)) { |
| robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 173 | return true; |
| robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 174 | } |
| robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 175 | } |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 176 | return false; |
| robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| Chris Dalton | d8d1593 | 2017-11-01 19:21:24 +0000 | [diff] [blame] | 179 | static bool get_analytic_clip_processor(const ElementList& elements, |
| 180 | bool abortIfAA, |
| 181 | const SkRect& drawDevBounds, |
| 182 | std::unique_ptr<GrFragmentProcessor>* resultFP) { |
| 183 | SkASSERT(elements.count() <= kMaxAnalyticElements); |
| 184 | SkSTArray<kMaxAnalyticElements, std::unique_ptr<GrFragmentProcessor>> fps; |
| 185 | ElementList::Iter iter(elements); |
| 186 | while (iter.get()) { |
| 187 | SkClipOp op = iter.get()->getOp(); |
| 188 | bool invert; |
| 189 | bool skip = false; |
| 190 | switch (op) { |
| 191 | case kReplace_SkClipOp: |
| 192 | SkASSERT(iter.get() == elements.head()); |
| 193 | // Fallthrough, handled same as intersect. |
| 194 | case kIntersect_SkClipOp: |
| 195 | invert = false; |
| 196 | if (iter.get()->contains(drawDevBounds)) { |
| 197 | skip = true; |
| 198 | } |
| 199 | break; |
| 200 | case kDifference_SkClipOp: |
| 201 | invert = true; |
| 202 | // We don't currently have a cheap test for whether a rect is fully outside an |
| 203 | // element's primitive, so don't attempt to set skip. |
| 204 | break; |
| 205 | default: |
| 206 | return false; |
| 207 | } |
| 208 | if (!skip) { |
| 209 | GrPrimitiveEdgeType edgeType; |
| 210 | if (iter.get()->isAA()) { |
| 211 | if (abortIfAA) { |
| 212 | return false; |
| 213 | } |
| 214 | edgeType = |
| 215 | invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType; |
| 216 | } else { |
| 217 | edgeType = |
| 218 | invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType; |
| 219 | } |
| 220 | |
| 221 | switch (iter.get()->getDeviceSpaceType()) { |
| 222 | case SkClipStack::Element::DeviceSpaceType::kPath: |
| 223 | fps.emplace_back( |
| 224 | GrConvexPolyEffect::Make(edgeType, iter.get()->getDeviceSpacePath())); |
| 225 | break; |
| 226 | case SkClipStack::Element::DeviceSpaceType::kRRect: { |
| 227 | fps.emplace_back( |
| 228 | GrRRectEffect::Make(edgeType, iter.get()->getDeviceSpaceRRect())); |
| 229 | break; |
| 230 | } |
| 231 | case SkClipStack::Element::DeviceSpaceType::kRect: { |
| 232 | fps.emplace_back( |
| 233 | GrConvexPolyEffect::Make(edgeType, iter.get()->getDeviceSpaceRect())); |
| 234 | break; |
| 235 | } |
| 236 | default: |
| 237 | break; |
| 238 | } |
| 239 | if (!fps.back()) { |
| 240 | return false; |
| 241 | } |
| 242 | } |
| 243 | iter.next(); |
| 244 | } |
| 245 | |
| 246 | *resultFP = nullptr; |
| 247 | if (fps.count()) { |
| 248 | *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count()); |
| 249 | } |
| 250 | return true; |
| 251 | } |
| 252 | |
| robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 253 | //////////////////////////////////////////////////////////////////////////////// |
| robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 254 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 255 | // scissor, or entirely software |
| Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 256 | bool GrClipStackClip::apply(GrContext* context, GrRenderTargetContext* renderTargetContext, |
| Brian Salomon | 97180af | 2017-03-14 13:42:58 -0400 | [diff] [blame] | 257 | bool useHWAA, bool hasUserStencilSettings, GrAppliedClip* out, |
| 258 | SkRect* bounds) const { |
| Brian Salomon | 97180af | 2017-03-14 13:42:58 -0400 | [diff] [blame] | 259 | SkRect devBounds = SkRect::MakeIWH(renderTargetContext->width(), renderTargetContext->height()); |
| 260 | if (!devBounds.intersect(*bounds)) { |
| csmartdalton | cbecb08 | 2016-07-22 08:59:08 -0700 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
| Brian Salomon | 510dd42 | 2017-03-16 12:15:22 -0400 | [diff] [blame] | 264 | if (!fStack || fStack->isWideOpen()) { |
| 265 | return true; |
| 266 | } |
| 267 | |
| Chris Dalton | d8d1593 | 2017-11-01 19:21:24 +0000 | [diff] [blame] | 268 | const GrReducedClip reducedClip(*fStack, devBounds, |
| 269 | renderTargetContext->priv().maxWindowRectangles()); |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 270 | |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 271 | if (reducedClip.hasScissor() && !GrClip::IsInsideClip(reducedClip.scissor(), devBounds)) { |
| Chris Dalton | 4c92d4a | 2017-11-06 13:48:04 -0700 | [diff] [blame] | 272 | out->hardClip().addScissor(reducedClip.scissor(), bounds); |
| cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 273 | } |
| cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 274 | |
| csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 275 | if (!reducedClip.windowRectangles().empty()) { |
| Chris Dalton | 4c92d4a | 2017-11-06 13:48:04 -0700 | [diff] [blame] | 276 | out->hardClip().addWindowRectangles(reducedClip.windowRectangles(), |
| 277 | GrWindowRectsState::Mode::kExclusive); |
| csmartdalton | bf4a8f9 | 2016-09-06 10:01:06 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 280 | if (reducedClip.maskElements().isEmpty()) { |
| csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 281 | return InitialState::kAllIn == reducedClip.initialState(); |
| 282 | } |
| 283 | |
| csmartdalton | 3affdc1 | 2016-10-28 12:01:10 -0700 | [diff] [blame] | 284 | #ifdef SK_DEBUG |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 285 | SkASSERT(reducedClip.hasScissor()); |
| Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 286 | SkIRect rtIBounds = SkIRect::MakeWH(renderTargetContext->width(), |
| 287 | renderTargetContext->height()); |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 288 | const SkIRect& scissor = reducedClip.scissor(); |
| 289 | SkASSERT(rtIBounds.contains(scissor)); // Mask shouldn't be larger than the RT. |
| csmartdalton | 3affdc1 | 2016-10-28 12:01:10 -0700 | [diff] [blame] | 290 | #endif |
| csmartdalton | d211e78 | 2016-08-15 11:17:19 -0700 | [diff] [blame] | 291 | |
| Chris Dalton | d8d1593 | 2017-11-01 19:21:24 +0000 | [diff] [blame] | 292 | bool avoidStencilBuffers = context->caps()->avoidStencilBuffers(); |
| 293 | |
| 294 | // An element count of 4 was chosen because of the common pattern in Blink of: |
| 295 | // isect RR |
| 296 | // diff RR |
| 297 | // isect convex_poly |
| 298 | // isect convex_poly |
| 299 | // when drawing rounded div borders. This could probably be tuned based on a |
| 300 | // configuration's relative costs of switching RTs to generate a mask vs |
| 301 | // longer shaders. |
| 302 | if (reducedClip.maskElements().count() <= kMaxAnalyticElements) { |
| 303 | // When there are multiple samples we want to do per-sample clipping, not compute a |
| 304 | // fractional pixel coverage. |
| 305 | bool disallowAnalyticAA = |
| 306 | GrFSAAType::kNone != renderTargetContext->fsaaType() && !avoidStencilBuffers; |
| 307 | if (disallowAnalyticAA && !renderTargetContext->numColorSamples()) { |
| 308 | // With a single color sample, any coverage info is lost from color once it hits the |
| 309 | // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe |
| 310 | // is multisampled. |
| 311 | disallowAnalyticAA = useHWAA || hasUserStencilSettings; |
| 312 | } |
| 313 | std::unique_ptr<GrFragmentProcessor> clipFP; |
| 314 | if ((reducedClip.maskRequiresAA() || avoidStencilBuffers) && |
| 315 | get_analytic_clip_processor(reducedClip.maskElements(), disallowAnalyticAA, devBounds, |
| 316 | &clipFP)) { |
| 317 | if (clipFP) { |
| 318 | out->addCoverageFP(std::move(clipFP)); |
| 319 | } |
| 320 | return true; |
| 321 | } |
| 322 | } |
| 323 | |
| cdalton | ede7574 | 2015-11-11 15:27:57 -0800 | [diff] [blame] | 324 | // If the stencil buffer is multisampled we can use it to do everything. |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 325 | if ((GrFSAAType::kNone == renderTargetContext->fsaaType() && reducedClip.maskRequiresAA()) || |
| Chris Dalton | d8d1593 | 2017-11-01 19:21:24 +0000 | [diff] [blame] | 326 | avoidStencilBuffers) { |
| Robert Phillips | 875218e | 2017-02-24 08:37:13 -0500 | [diff] [blame] | 327 | sk_sp<GrTextureProxy> result; |
| Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 328 | if (UseSWOnlyPath(context, hasUserStencilSettings, renderTargetContext, reducedClip)) { |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 329 | // The clip geometry is complex enough that it will be more efficient to create it |
| 330 | // entirely in software |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 331 | result = this->createSoftwareClipMask(context, reducedClip, renderTargetContext); |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 332 | } else { |
| Brian Salomon | 19f0ed5 | 2017-01-06 13:54:58 -0500 | [diff] [blame] | 333 | result = this->createAlphaClipMask(context, reducedClip); |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 336 | if (result) { |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 337 | // 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] | 338 | // the clip's device space bounds. |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 339 | out->addCoverageFP(create_fp_for_mask(std::move(result), reducedClip.scissor())); |
| robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 340 | return true; |
| 341 | } |
| Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 342 | |
| 343 | // If alpha or software clip mask creation fails, fall through to the stencil code paths, |
| 344 | // unless stencils are disallowed. |
| 345 | if (context->caps()->avoidStencilBuffers()) { |
| Chris Dalton | d8d1593 | 2017-11-01 19:21:24 +0000 | [diff] [blame] | 346 | SkDebugf("WARNING: Clip mask requires stencil, but stencil unavailable. Clip will be ignored.\n"); |
| Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 347 | return false; |
| 348 | } |
| robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 349 | } |
| robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 350 | |
| Robert Phillips | 6504813 | 2017-08-10 08:44:49 -0400 | [diff] [blame] | 351 | renderTargetContext->setNeedsStencil(); |
| csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 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. |
| Chris Dalton | d8d1593 | 2017-11-01 19:21:24 +0000 | [diff] [blame] | 356 | if (renderTargetContext->priv().mustRenderClip(reducedClip.maskGenID(), |
| 357 | reducedClip.scissor())) { |
| Brian Salomon | 9a76772 | 2017-03-13 17:57:28 -0400 | [diff] [blame] | 358 | reducedClip.drawStencilClipMask(context, renderTargetContext); |
| Chris Dalton | d8d1593 | 2017-11-01 19:21:24 +0000 | [diff] [blame] | 359 | renderTargetContext->priv().setLastClip(reducedClip.maskGenID(), reducedClip.scissor()); |
| csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 360 | } |
| Chris Dalton | 4c92d4a | 2017-11-06 13:48:04 -0700 | [diff] [blame] | 361 | out->hardClip().addStencilClip(reducedClip.maskGenID()); |
| 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 | |
| Chris Dalton | d8d1593 | 2017-11-01 19:21:24 +0000 | [diff] [blame] | 368 | static void create_clip_mask_key(uint32_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(); |
| Chris Dalton | d8d1593 | 2017-11-01 19:21:24 +0000 | [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 | |
| Robert Phillips | 806be2d | 2017-06-28 15:23:59 -0400 | [diff] [blame] | 378 | static void add_invalidate_on_pop_message(const SkClipStack& stack, uint32_t clipGenID, |
| Brian Salomon | 19f0ed5 | 2017-01-06 13:54:58 -0500 | [diff] [blame] | 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; |
| Chris Dalton | d8d1593 | 2017-11-01 19:21:24 +0000 | [diff] [blame] | 396 | create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(), &key); |
| Robert Phillips | 875218e | 2017-02-24 08:37:13 -0500 | [diff] [blame] | 397 | |
| Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 398 | sk_sp<GrTextureProxy> proxy(resourceProvider->findOrCreateProxyByUniqueKey( |
| Robert Phillips | 066f020 | 2017-07-25 10:16:35 -0400 | [diff] [blame] | 399 | key, kBottomLeft_GrSurfaceOrigin)); |
| Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 400 | if (proxy) { |
| 401 | return proxy; |
| robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 402 | } |
| 403 | |
| Robert Phillips | dd3b3f4 | 2017-04-24 10:57:28 -0400 | [diff] [blame] | 404 | sk_sp<GrRenderTargetContext> rtc(context->makeDeferredRenderTargetContextWithFallback( |
| Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 405 | SkBackingFit::kApprox, |
| 406 | reducedClip.width(), |
| 407 | reducedClip.height(), |
| 408 | kAlpha_8_GrPixelConfig, |
| 409 | nullptr)); |
| 410 | if (!rtc) { |
| robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 411 | return nullptr; |
| 412 | } |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 413 | |
| Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 414 | if (!reducedClip.drawAlphaClipMask(rtc.get())) { |
| csmartdalton | bde96c6 | 2016-08-31 12:54:46 -0700 | [diff] [blame] | 415 | return nullptr; |
| robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 416 | } |
| 417 | |
| Robert Phillips | 875218e | 2017-02-24 08:37:13 -0500 | [diff] [blame] | 418 | sk_sp<GrTextureProxy> result(rtc->asTextureProxyRef()); |
| 419 | if (!result) { |
| Robert Phillips | e60ad62 | 2016-11-17 10:22:48 -0500 | [diff] [blame] | 420 | return nullptr; |
| 421 | } |
| 422 | |
| Robert Phillips | 019ff27 | 2017-07-24 14:47:57 -0400 | [diff] [blame] | 423 | SkASSERT(result->origin() == kBottomLeft_GrSurfaceOrigin); |
| Robert Phillips | d374948 | 2017-03-14 09:17:43 -0400 | [diff] [blame] | 424 | resourceProvider->assignUniqueKeyToProxy(key, result.get()); |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 425 | add_invalidate_on_pop_message(*fStack, reducedClip.maskGenID(), key); |
| Robert Phillips | 875218e | 2017-02-24 08:37:13 -0500 | [diff] [blame] | 426 | |
| 427 | return result; |
| robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 430 | namespace { |
| Robert Phillips | 875218e | 2017-02-24 08:37:13 -0500 | [diff] [blame] | 431 | |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 432 | /** |
| Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 433 | * Payload class for use with GrTDeferredProxyUploader. The clip mask code renders multiple |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 434 | * elements, each storing their own AA setting (and already transformed into device space). This |
| 435 | * stores all of the information needed by the worker thread to draw all clip elements (see below, |
| 436 | * in createSoftwareClipMask). |
| 437 | */ |
| 438 | class ClipMaskData { |
| 439 | public: |
| 440 | ClipMaskData(const GrReducedClip& reducedClip) |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 441 | : fScissor(reducedClip.scissor()) |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 442 | , fInitialState(reducedClip.initialState()) { |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 443 | for (ElementList::Iter iter(reducedClip.maskElements()); iter.get(); iter.next()) { |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 444 | fElements.addToTail(*iter.get()); |
| 445 | } |
| robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 448 | const SkIRect& scissor() const { return fScissor; } |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 449 | InitialState initialState() const { return fInitialState; } |
| 450 | const ElementList& elements() const { return fElements; } |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 451 | |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 452 | private: |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 453 | SkIRect fScissor; |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 454 | InitialState fInitialState; |
| 455 | ElementList fElements; |
| 456 | }; |
| robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 457 | |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | static void draw_clip_elements_to_mask_helper(GrSWMaskHelper& helper, const ElementList& elements, |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 461 | const SkIRect& scissor, InitialState initialState) { |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 462 | // Set the matrix so that rendered clip elements are transformed to mask space from clip space. |
| joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 463 | SkMatrix translate; |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 464 | translate.setTranslate(SkIntToScalar(-scissor.left()), SkIntToScalar(-scissor.top())); |
| joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 465 | |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 466 | helper.clear(InitialState::kAllIn == initialState ? 0xFF : 0x00); |
| sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 467 | |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 468 | for (ElementList::Iter iter(elements); iter.get(); iter.next()) { |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 469 | const Element* element = iter.get(); |
| Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 470 | SkClipOp op = element->getOp(); |
| Brian Salomon | 0e8fc8b | 2016-12-09 15:10:07 -0500 | [diff] [blame] | 471 | GrAA aa = GrBoolToAA(element->isAA()); |
| robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 472 | |
| Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 473 | if (kIntersect_SkClipOp == op || kReverseDifference_SkClipOp == op) { |
| bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 474 | // Intersect and reverse difference require modifying pixels outside of the geometry |
| 475 | // that is being "drawn". In both cases we erase all the pixels outside of the geometry |
| 476 | // but leave the pixels inside the geometry alone. For reverse difference we invert all |
| 477 | // the pixels before clearing the ones outside the geometry. |
| Mike Reed | c1f7774 | 2016-12-09 09:00:50 -0500 | [diff] [blame] | 478 | if (kReverseDifference_SkClipOp == op) { |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 479 | SkRect temp = SkRect::Make(scissor); |
| robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 480 | // invert the entire scene |
| Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 481 | helper.drawRect(temp, translate, SkRegion::kXOR_Op, GrAA::kNo, 0xFF); |
| robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 482 | } |
| commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 483 | SkPath clipPath; |
| Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 484 | element->asDeviceSpacePath(&clipPath); |
| commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 485 | clipPath.toggleInverseFillType(); |
| bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 486 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
| Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 487 | helper.drawShape(shape, translate, SkRegion::kReplace_Op, aa, 0x00); |
| robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 488 | continue; |
| 489 | } |
| 490 | |
| 491 | // The other ops (union, xor, diff) only affect pixels inside |
| 492 | // the geometry so they can just be drawn normally |
| Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 493 | if (Element::DeviceSpaceType::kRect == element->getDeviceSpaceType()) { |
| Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 494 | helper.drawRect(element->getDeviceSpaceRect(), translate, (SkRegion::Op)op, aa, 0xFF); |
| bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 495 | } else { |
| commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 496 | SkPath path; |
| Brian Salomon | f3b46e5 | 2017-08-30 11:37:57 -0400 | [diff] [blame] | 497 | element->asDeviceSpacePath(&path); |
| bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 498 | GrShape shape(path, GrStyle::SimpleFill()); |
| Brian Salomon | 7407756 | 2017-08-30 13:55:35 -0400 | [diff] [blame] | 499 | helper.drawShape(shape, translate, (SkRegion::Op)op, aa, 0xFF); |
| robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 502 | } |
| robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 503 | |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 504 | sk_sp<GrTextureProxy> GrClipStackClip::createSoftwareClipMask( |
| 505 | GrContext* context, const GrReducedClip& reducedClip, |
| 506 | GrRenderTargetContext* renderTargetContext) const { |
| 507 | GrUniqueKey key; |
| Chris Dalton | d8d1593 | 2017-11-01 19:21:24 +0000 | [diff] [blame] | 508 | create_clip_mask_key(reducedClip.maskGenID(), reducedClip.scissor(), &key); |
| robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 509 | |
| Greg Daniel | cd87140 | 2017-09-26 12:49:26 -0400 | [diff] [blame] | 510 | sk_sp<GrTextureProxy> proxy(context->resourceProvider()->findOrCreateProxyByUniqueKey( |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 511 | key, kTopLeft_GrSurfaceOrigin)); |
| 512 | if (proxy) { |
| 513 | return proxy; |
| 514 | } |
| 515 | |
| 516 | // The mask texture may be larger than necessary. We round out the clip bounds and pin the top |
| 517 | // left corner of the resulting rect to the top left of the texture. |
| 518 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height()); |
| 519 | |
| 520 | SkTaskGroup* taskGroup = context->contextPriv().getTaskGroup(); |
| 521 | if (taskGroup && renderTargetContext) { |
| 522 | // Create our texture proxy |
| 523 | GrSurfaceDesc desc; |
| 524 | desc.fOrigin = kTopLeft_GrSurfaceOrigin; |
| 525 | desc.fWidth = maskSpaceIBounds.width(); |
| 526 | desc.fHeight = maskSpaceIBounds.height(); |
| 527 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| Brian Osman | d140fe9 | 2017-10-03 12:17:26 -0400 | [diff] [blame] | 528 | // MDB TODO: We're going to fill this proxy with an ASAP upload (which is out of order wrt |
| 529 | // to ops), so it can't have any pending IO. |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 530 | proxy = GrSurfaceProxy::MakeDeferred(context->resourceProvider(), desc, |
| Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 531 | SkBackingFit::kApprox, SkBudgeted::kYes, |
| 532 | GrResourceProvider::kNoPendingIO_Flag); |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 533 | |
| Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 534 | auto uploader = skstd::make_unique<GrTDeferredProxyUploader<ClipMaskData>>(reducedClip); |
| 535 | GrTDeferredProxyUploader<ClipMaskData>* uploaderRaw = uploader.get(); |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 536 | auto drawAndUploadMask = [uploaderRaw, maskSpaceIBounds] { |
| 537 | TRACE_EVENT0("skia", "Threaded SW Clip Mask Render"); |
| 538 | GrSWMaskHelper helper(uploaderRaw->getPixels()); |
| 539 | if (helper.init(maskSpaceIBounds)) { |
| 540 | draw_clip_elements_to_mask_helper(helper, uploaderRaw->data().elements(), |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 541 | uploaderRaw->data().scissor(), |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 542 | uploaderRaw->data().initialState()); |
| 543 | } else { |
| 544 | SkDEBUGFAIL("Unable to allocate SW clip mask."); |
| 545 | } |
| Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 546 | uploaderRaw->signalAndFreeData(); |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 547 | }; |
| 548 | |
| 549 | taskGroup->add(std::move(drawAndUploadMask)); |
| Brian Osman | 099fa0f | 2017-10-02 16:38:32 -0400 | [diff] [blame] | 550 | proxy->texPriv().setDeferredUploader(std::move(uploader)); |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 551 | } else { |
| 552 | GrSWMaskHelper helper; |
| 553 | if (!helper.init(maskSpaceIBounds)) { |
| 554 | return nullptr; |
| 555 | } |
| 556 | |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 557 | draw_clip_elements_to_mask_helper(helper, reducedClip.maskElements(), reducedClip.scissor(), |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 558 | reducedClip.initialState()); |
| 559 | |
| 560 | proxy = helper.toTextureProxy(context, SkBackingFit::kApprox); |
| 561 | } |
| 562 | |
| 563 | SkASSERT(proxy->origin() == kTopLeft_GrSurfaceOrigin); |
| 564 | context->resourceProvider()->assignUniqueKeyToProxy(key, proxy.get()); |
| Chris Dalton | 7947193 | 2017-10-27 01:50:57 -0600 | [diff] [blame] | 565 | add_invalidate_on_pop_message(*fStack, reducedClip.maskGenID(), key); |
| Brian Osman | 5d03474 | 2017-09-11 13:38:55 -0400 | [diff] [blame] | 566 | return proxy; |
| robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 567 | } |