robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrClipMaskManager.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 9 | #include "GrCaps.h" |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 10 | #include "GrDrawingManager.h" |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 11 | #include "GrDrawContextPriv.h" |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 12 | #include "GrGpuResourcePriv.h" |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 13 | #include "GrPaint.h" |
| 14 | #include "GrPathRenderer.h" |
| 15 | #include "GrRenderTarget.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 16 | #include "GrRenderTargetPriv.h" |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [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" |
joshualitt | 3a0cfeb | 2014-10-27 07:38:01 -0700 | [diff] [blame] | 20 | #include "SkRasterClip.h" |
joshualitt | 3a0cfeb | 2014-10-27 07:38:01 -0700 | [diff] [blame] | 21 | #include "SkTLazy.h" |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 22 | #include "batches/GrRectBatchFactory.h" |
egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 23 | #include "effects/GrConvexPolyEffect.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 24 | #include "effects/GrPorterDuffXferProcessor.h" |
egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 25 | #include "effects/GrRRectEffect.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 26 | #include "effects/GrTextureDomain.h" |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 27 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 28 | typedef SkClipStack::Element Element; |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 29 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 30 | static const int kMaxAnalyticElements = 4; |
| 31 | |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 32 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 33 | // set up the draw state to enable the aa clipping mask. Besides setting up the |
bsalomon@google.com | 08283af | 2012-10-26 13:01:20 +0000 | [diff] [blame] | 34 | // stage matrix this also alters the vertex layout |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 35 | static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result, |
| 36 | const SkIRect &devBound) { |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 37 | SkMatrix mat; |
bsalomon | 309d4d5 | 2014-12-18 10:17:44 -0800 | [diff] [blame] | 38 | // We use device coords to compute the texture coordinates. We set our matrix to be a |
| 39 | // translation to the devBound, and then a scaling matrix to normalized coords. |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 40 | mat.setIDiv(result->width(), result->height()); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 41 | mat.preTranslate(SkIntToScalar(-devBound.fLeft), |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 42 | SkIntToScalar(-devBound.fTop)); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 43 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 44 | SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height()); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 45 | return sk_sp<GrFragmentProcessor>(GrTextureDomainEffect::Make( |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 46 | result, |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 47 | mat, |
| 48 | GrTextureDomain::MakeTexelDomain(result, domainTexels), |
| 49 | GrTextureDomain::kDecal_Mode, |
| 50 | GrTextureParams::kNone_FilterMode, |
robertphillips | 5f2fa47 | 2016-05-19 11:36:25 -0700 | [diff] [blame] | 51 | kDevice_GrCoordSet)); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 52 | } |
| 53 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 54 | // Does the path in 'element' require SW rendering? If so, return true (and, |
| 55 | // optionally, set 'prOut' to NULL. If not, return false (and, optionally, set |
| 56 | // 'prOut' to the non-SW path renderer that will do the job). |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 57 | bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 58 | bool hasUserStencilSettings, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 59 | const GrDrawContext* drawContext, |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 60 | const SkMatrix& viewMatrix, |
| 61 | const Element* element, |
| 62 | GrPathRenderer** prOut, |
| 63 | bool needsStencil) { |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 64 | if (Element::kRect_Type == element->getType()) { |
| 65 | // rects can always be drawn directly w/o using the software path |
| 66 | // TODO: skip rrects once we're drawing them directly. |
| 67 | if (prOut) { |
| 68 | *prOut = nullptr; |
| 69 | } |
| 70 | return false; |
| 71 | } else { |
| 72 | // We shouldn't get here with an empty clip element. |
| 73 | SkASSERT(Element::kEmpty_Type != element->getType()); |
robertphillips | 5c3ea4c | 2015-10-26 08:33:10 -0700 | [diff] [blame] | 74 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 75 | // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer |
| 76 | SkPath path; |
| 77 | element->asPath(&path); |
| 78 | if (path.isInverseFillType()) { |
| 79 | path.toggleInverseFillType(); |
| 80 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 81 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 82 | GrPathRendererChain::DrawType type; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 83 | |
robertphillips | 423e337 | 2015-10-27 09:23:38 -0700 | [diff] [blame] | 84 | if (needsStencil) { |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 85 | type = element->isAA() |
| 86 | ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType |
| 87 | : GrPathRendererChain::kStencilAndColor_DrawType; |
| 88 | } else { |
| 89 | type = element->isAA() |
| 90 | ? GrPathRendererChain::kColorAntiAlias_DrawType |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 91 | : GrPathRendererChain::kColor_DrawType; |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 92 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 93 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 94 | GrShape shape(path, GrStyle::SimpleFill()); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 95 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 96 | canDrawArgs.fShaderCaps = context->caps()->shaderCaps(); |
| 97 | canDrawArgs.fViewMatrix = &viewMatrix; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 98 | canDrawArgs.fShape = &shape; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 99 | canDrawArgs.fAntiAlias = element->isAA(); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 100 | canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings; |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 101 | canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 102 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 103 | // the 'false' parameter disallows use of the SW path renderer |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 104 | GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type); |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 105 | if (prOut) { |
| 106 | *prOut = pr; |
| 107 | } |
| 108 | return SkToBool(!pr); |
| 109 | } |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 110 | } |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 111 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 112 | /* |
| 113 | * This method traverses the clip stack to see if the GrSoftwarePathRenderer |
| 114 | * will be used on any element. If so, it returns true to indicate that the |
| 115 | * entire clip should be rendered in SW and then uploaded en masse to the gpu. |
| 116 | */ |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 117 | bool GrClipMaskManager::UseSWOnlyPath(GrContext* context, |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame^] | 118 | bool hasUserStencilSettings, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 119 | const GrDrawContext* drawContext, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 120 | const SkVector& clipToMaskOffset, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 121 | const GrReducedClip::ElementList& elements) { |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 122 | // TODO: generalize this function so that when |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 123 | // a clip gets complex enough it can just be done in SW regardless |
| 124 | // of whether it would invoke the GrSoftwarePathRenderer. |
skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 125 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 126 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 127 | // space. |
robertphillips | cf10b5a | 2015-10-27 07:53:35 -0700 | [diff] [blame] | 128 | const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 129 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 130 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 131 | const Element* element = iter.get(); |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 132 | |
| 133 | SkRegion::Op op = element->getOp(); |
| 134 | bool invert = element->isInverseFilled(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 135 | bool needsStencil = invert || |
robertphillips | 423e337 | 2015-10-27 09:23:38 -0700 | [diff] [blame] | 136 | SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op; |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 137 | |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame^] | 138 | if (PathNeedsSWRenderer(context, hasUserStencilSettings, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 139 | drawContext, translate, element, nullptr, needsStencil)) { |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 140 | return true; |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 141 | } |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 142 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 143 | return false; |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 144 | } |
| 145 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 146 | static bool get_analytic_clip_processor(const GrReducedClip::ElementList& elements, |
| 147 | bool abortIfAA, |
| 148 | SkVector& clipToRTOffset, |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 149 | const SkRect& drawBounds, |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 150 | sk_sp<GrFragmentProcessor>* resultFP) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 151 | SkRect boundsInClipSpace; |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 152 | boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 153 | SkASSERT(elements.count() <= kMaxAnalyticElements); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 154 | SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps; |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 155 | GrReducedClip::ElementList::Iter iter(elements); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 156 | while (iter.get()) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 157 | SkRegion::Op op = iter.get()->getOp(); |
| 158 | bool invert; |
| 159 | bool skip = false; |
| 160 | switch (op) { |
| 161 | case SkRegion::kReplace_Op: |
| 162 | SkASSERT(iter.get() == elements.head()); |
| 163 | // Fallthrough, handled same as intersect. |
| 164 | case SkRegion::kIntersect_Op: |
| 165 | invert = false; |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 166 | if (iter.get()->contains(boundsInClipSpace)) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 167 | skip = true; |
| 168 | } |
| 169 | break; |
| 170 | case SkRegion::kDifference_Op: |
| 171 | invert = true; |
| 172 | // We don't currently have a cheap test for whether a rect is fully outside an |
| 173 | // element's primitive, so don't attempt to set skip. |
| 174 | break; |
| 175 | default: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 176 | return false; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 177 | } |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 178 | if (!skip) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 179 | GrPrimitiveEdgeType edgeType; |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 180 | if (iter.get()->isAA()) { |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 181 | if (abortIfAA) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 182 | return false; |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 183 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 184 | edgeType = |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 185 | invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 186 | } else { |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 187 | edgeType = |
| 188 | invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 189 | } |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 190 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 191 | switch (iter.get()->getType()) { |
| 192 | case SkClipStack::Element::kPath_Type: |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 193 | fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(), |
| 194 | &clipToRTOffset)); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 195 | break; |
| 196 | case SkClipStack::Element::kRRect_Type: { |
| 197 | SkRRect rrect = iter.get()->getRRect(); |
| 198 | rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 199 | fps.emplace_back(GrRRectEffect::Make(edgeType, rrect)); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 200 | break; |
| 201 | } |
| 202 | case SkClipStack::Element::kRect_Type: { |
| 203 | SkRect rect = iter.get()->getRect(); |
| 204 | rect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 205 | fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect)); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 206 | break; |
| 207 | } |
| 208 | default: |
| 209 | break; |
| 210 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 211 | if (!fps.back()) { |
| 212 | return false; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 215 | iter.next(); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 216 | } |
| 217 | |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 218 | *resultFP = nullptr; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 219 | if (fps.count()) { |
| 220 | *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count()); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 221 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 222 | return true; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 223 | } |
| 224 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 225 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 226 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 227 | // scissor, or entirely software |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 228 | bool GrClipMaskManager::SetupClipping(GrContext* context, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 229 | GrDrawContext* drawContext, |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 230 | const GrClipStackClip& clip, |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 231 | const SkRect* origDevBounds, |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame^] | 232 | bool useHWAA, |
| 233 | bool hasUserStencilSettings, |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 234 | GrAppliedClip* out) { |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 235 | if (!clip.clipStack() || clip.clipStack()->isWideOpen()) { |
| 236 | return true; |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 237 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 238 | |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 239 | GrReducedClip::ElementList elements; |
brucedawson | 71d7f7f | 2015-02-26 13:28:53 -0800 | [diff] [blame] | 240 | int32_t genID = 0; |
| 241 | GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 242 | SkIRect clipSpaceIBounds; |
brucedawson | 71d7f7f | 2015-02-26 13:28:53 -0800 | [diff] [blame] | 243 | bool requiresAA = false; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 244 | |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 245 | SkIRect clipSpaceReduceQueryBounds; |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 246 | SkRect devBounds; |
| 247 | if (origDevBounds) { |
| 248 | if (!devBounds.intersect(SkRect::MakeIWH(drawContext->width(), drawContext->height()), |
| 249 | *origDevBounds)) { |
bsalomon | 9ce30e1 | 2015-03-06 08:42:34 -0800 | [diff] [blame] | 250 | return false; |
| 251 | } |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 252 | devBounds.roundOut(&clipSpaceReduceQueryBounds); |
| 253 | clipSpaceReduceQueryBounds.offset(clip.origin()); |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 254 | } else { |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 255 | devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height()); |
| 256 | clipSpaceReduceQueryBounds.setXYWH(0, 0, drawContext->width(), drawContext->height()); |
| 257 | clipSpaceReduceQueryBounds.offset(clip.origin()); |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 258 | } |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 259 | GrReducedClip::ReduceClipStack(*clip.clipStack(), |
| 260 | clipSpaceReduceQueryBounds, |
| 261 | &elements, |
| 262 | &genID, |
| 263 | &initialState, |
| 264 | &clipSpaceIBounds, |
| 265 | &requiresAA); |
| 266 | if (elements.isEmpty()) { |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 267 | if (GrReducedClip::kAllOut_InitialState == initialState) { |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 268 | return false; |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 269 | } else { |
| 270 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
| 271 | scissorSpaceIBounds.offset(-clip.origin()); |
msarett | ba3880f | 2016-07-12 18:48:21 -0700 | [diff] [blame] | 272 | if (!SkRect::Make(scissorSpaceIBounds).contains(devBounds)) { |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 273 | out->makeScissored(scissorSpaceIBounds); |
| 274 | } |
| 275 | return true; |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 276 | } |
| 277 | } |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 278 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 279 | // An element count of 4 was chosen because of the common pattern in Blink of: |
| 280 | // isect RR |
| 281 | // diff RR |
| 282 | // isect convex_poly |
| 283 | // isect convex_poly |
| 284 | // when drawing rounded div borders. This could probably be tuned based on a |
| 285 | // configuration's relative costs of switching RTs to generate a mask vs |
| 286 | // longer shaders. |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 287 | if (elements.count() <= kMaxAnalyticElements) { |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 288 | SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX), |
| 289 | SkIntToScalar(-clip.origin().fY) }; |
cdalton | ede7574 | 2015-11-11 15:27:57 -0800 | [diff] [blame] | 290 | // When there are multiple samples we want to do per-sample clipping, not compute a |
| 291 | // fractional pixel coverage. |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 292 | bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled(); |
| 293 | if (disallowAnalyticAA && !drawContext->numColorSamples()) { |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 294 | // With a single color sample, any coverage info is lost from color once it hits the |
| 295 | // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe |
| 296 | // is multisampled. |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame^] | 297 | disallowAnalyticAA = useHWAA || hasUserStencilSettings; |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 298 | } |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 299 | sk_sp<GrFragmentProcessor> clipFP; |
bsalomon | bd2bbe4 | 2016-07-08 07:36:42 -0700 | [diff] [blame] | 300 | if (requiresAA && |
| 301 | get_analytic_clip_processor(elements, disallowAnalyticAA, clipToRTOffset, devBounds, |
| 302 | &clipFP)) { |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 303 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 304 | scissorSpaceIBounds.offset(-clip.origin()); |
msarett | ba3880f | 2016-07-12 18:48:21 -0700 | [diff] [blame] | 305 | if (!SkRect::Make(scissorSpaceIBounds).contains(devBounds)) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 306 | out->makeScissoredFPBased(std::move(clipFP), scissorSpaceIBounds); |
msarett | ba3880f | 2016-07-12 18:48:21 -0700 | [diff] [blame] | 307 | return true; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 308 | } |
msarett | ba3880f | 2016-07-12 18:48:21 -0700 | [diff] [blame] | 309 | out->makeFPBased(std::move(clipFP), SkRect::Make(scissorSpaceIBounds)); |
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. |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 315 | if (!drawContext->isStencilBufferMultisampled() && requiresAA) { |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 316 | sk_sp<GrTexture> result; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 317 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 318 | // The top-left of the mask corresponds to the top-left corner of the bounds. |
| 319 | SkVector clipToMaskOffset = { |
| 320 | SkIntToScalar(-clipSpaceIBounds.fLeft), |
| 321 | SkIntToScalar(-clipSpaceIBounds.fTop) |
| 322 | }; |
| 323 | |
robertphillips | 59cf61a | 2016-07-13 09:18:21 -0700 | [diff] [blame^] | 324 | if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext, |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 325 | clipToMaskOffset, elements)) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 326 | // The clip geometry is complex enough that it will be more efficient to create it |
| 327 | // entirely in software |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 328 | result = CreateSoftwareClipMask(context->textureProvider(), |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 329 | genID, |
| 330 | initialState, |
| 331 | elements, |
| 332 | clipToMaskOffset, |
| 333 | clipSpaceIBounds); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 334 | } else { |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 335 | result = CreateAlphaClipMask(context, |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 336 | genID, |
| 337 | initialState, |
| 338 | elements, |
| 339 | clipToMaskOffset, |
| 340 | clipSpaceIBounds); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 341 | // If createAlphaClipMask fails it means UseSWOnlyPath has a bug |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 342 | SkASSERT(result); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 343 | } |
| 344 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 345 | if (result) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 346 | // The mask's top left coord should be pinned to the rounded-out top left corner of |
| 347 | // clipSpace bounds. We determine the mask's position WRT to the render target here. |
| 348 | SkIRect rtSpaceMaskBounds = clipSpaceIBounds; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 349 | rtSpaceMaskBounds.offset(-clip.origin()); |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 350 | out->makeFPBased(create_fp_for_mask(result.get(), rtSpaceMaskBounds), |
| 351 | SkRect::Make(rtSpaceMaskBounds)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 352 | return true; |
| 353 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 354 | // 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] | 355 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 356 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 357 | // use the stencil clip if we can't represent the clip as a rectangle. |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 358 | SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin(); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 359 | CreateStencilClipMask(context, |
| 360 | drawContext, |
| 361 | genID, |
| 362 | initialState, |
| 363 | elements, |
| 364 | clipSpaceIBounds, |
| 365 | clipSpaceToStencilSpaceOffset); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 366 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 367 | // This must occur after createStencilClipMask. That function may change the scissor. Also, it |
| 368 | // only guarantees that the stencil mask is correct within the bounds it was passed, so we must |
| 369 | // use both stencil and scissor test to the bounds for the final draw. |
| 370 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
| 371 | scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset); |
msarett | ba3880f | 2016-07-12 18:48:21 -0700 | [diff] [blame] | 372 | out->makeScissoredStencil(scissorSpaceIBounds); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 373 | return true; |
| 374 | } |
| 375 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 376 | static bool stencil_element(GrDrawContext* dc, |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 377 | const GrFixedClip& clip, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 378 | const GrUserStencilSettings* ss, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 379 | const SkMatrix& viewMatrix, |
| 380 | const SkClipStack::Element* element) { |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame] | 381 | |
| 382 | // TODO: Draw rrects directly here. |
| 383 | switch (element->getType()) { |
| 384 | case Element::kEmpty_Type: |
| 385 | SkDEBUGFAIL("Should never get here with an empty element."); |
| 386 | break; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 387 | case Element::kRect_Type: |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 388 | return dc->drawContextPriv().drawAndStencilRect(clip, ss, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 389 | element->getOp(), |
| 390 | element->isInverseFilled(), |
| 391 | element->isAA(), |
| 392 | viewMatrix, element->getRect()); |
| 393 | break; |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame] | 394 | default: { |
| 395 | SkPath path; |
| 396 | element->asPath(&path); |
| 397 | if (path.isInverseFillType()) { |
| 398 | path.toggleInverseFillType(); |
| 399 | } |
| 400 | |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 401 | return dc->drawContextPriv().drawAndStencilPath(clip, ss, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 402 | element->getOp(), |
| 403 | element->isInverseFilled(), |
| 404 | element->isAA(), viewMatrix, path); |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame] | 405 | break; |
| 406 | } |
| 407 | } |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 408 | |
| 409 | return false; |
| 410 | } |
| 411 | |
| 412 | static void draw_element(GrDrawContext* dc, |
| 413 | const GrClip& clip, // TODO: can this just always be WideOpen? |
| 414 | const GrPaint &paint, |
| 415 | const SkMatrix& viewMatrix, |
| 416 | const SkClipStack::Element* element) { |
| 417 | |
| 418 | // TODO: Draw rrects directly here. |
| 419 | switch (element->getType()) { |
| 420 | case Element::kEmpty_Type: |
| 421 | SkDEBUGFAIL("Should never get here with an empty element."); |
| 422 | break; |
| 423 | case Element::kRect_Type: |
| 424 | dc->drawRect(clip, paint, viewMatrix, element->getRect()); |
| 425 | break; |
| 426 | default: { |
| 427 | SkPath path; |
| 428 | element->asPath(&path); |
| 429 | if (path.isInverseFillType()) { |
| 430 | path.toggleInverseFillType(); |
| 431 | } |
| 432 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 433 | dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill()); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 434 | break; |
| 435 | } |
| 436 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 437 | } |
| 438 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 439 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 440 | // Create a 8-bit clip mask in alpha |
| 441 | |
| 442 | static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) { |
| 443 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 444 | GrUniqueKey::Builder builder(key, kDomain, 3); |
| 445 | builder[0] = clipGenID; |
| 446 | builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16); |
| 447 | builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16); |
| 448 | } |
| 449 | |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 450 | sk_sp<GrTexture> GrClipMaskManager::CreateAlphaClipMask(GrContext* context, |
| 451 | int32_t elementsGenID, |
| 452 | GrReducedClip::InitialState initialState, |
| 453 | const GrReducedClip::ElementList& elements, |
| 454 | const SkVector& clipToMaskOffset, |
| 455 | const SkIRect& clipSpaceIBounds) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 456 | GrResourceProvider* resourceProvider = context->resourceProvider(); |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 457 | GrUniqueKey key; |
| 458 | GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key); |
| 459 | if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) { |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 460 | return sk_sp<GrTexture>(texture); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 461 | } |
| 462 | |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 463 | // There's no texture in the cache. Let's try to allocate it then. |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 464 | GrPixelConfig config = kRGBA_8888_GrPixelConfig; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 465 | if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 466 | config = kAlpha_8_GrPixelConfig; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 467 | } |
| 468 | |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 469 | sk_sp<GrDrawContext> dc(context->newDrawContext(SkBackingFit::kApprox, |
| 470 | clipSpaceIBounds.width(), |
| 471 | clipSpaceIBounds.height(), |
| 472 | config)); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 473 | if (!dc) { |
| 474 | return nullptr; |
| 475 | } |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 476 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 477 | // The texture may be larger than necessary, this rect represents the part of the texture |
| 478 | // we populate with a rasterization of the clip. |
| 479 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 480 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 481 | // The scratch texture that we are drawing into can be substantially larger than the mask. Only |
| 482 | // clear the part that we care about. |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 483 | dc->clear(&maskSpaceIBounds, |
| 484 | GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000, |
| 485 | true); |
skia.committer@gmail.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 486 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 487 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 488 | // space. |
| 489 | const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY); |
| 490 | |
| 491 | // It is important that we use maskSpaceIBounds as the stencil rect in the below loop. |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 492 | // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first |
| 493 | // pass must not set values outside of this bounds or stencil values outside the rect won't be |
| 494 | // cleared. |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 495 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 496 | // walk through each clip element and perform its set op |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 497 | for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 498 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 499 | SkRegion::Op op = element->getOp(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 500 | bool invert = element->isInverseFilled(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 501 | if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 502 | GrFixedClip clip(maskSpaceIBounds); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 503 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 504 | // draw directly into the result with the stencil set to make the pixels affected |
| 505 | // by the clip shape be non-zero. |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 506 | static constexpr GrUserStencilSettings kStencilInElement( |
| 507 | GrUserStencilSettings::StaticInit< |
| 508 | 0xffff, |
| 509 | GrUserStencilTest::kAlways, |
| 510 | 0xffff, |
| 511 | GrUserStencilOp::kReplace, |
| 512 | GrUserStencilOp::kReplace, |
| 513 | 0xffff>() |
| 514 | ); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 515 | if (!stencil_element(dc.get(), clip, &kStencilInElement, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 516 | translate, element)) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 517 | return nullptr; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 518 | } |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 519 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 520 | // Draw to the exterior pixels (those with a zero stencil value). |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 521 | static constexpr GrUserStencilSettings kDrawOutsideElement( |
| 522 | GrUserStencilSettings::StaticInit< |
| 523 | 0x0000, |
| 524 | GrUserStencilTest::kEqual, |
| 525 | 0xffff, |
| 526 | GrUserStencilOp::kZero, |
| 527 | GrUserStencilOp::kZero, |
| 528 | 0xffff>() |
| 529 | ); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 530 | if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 531 | op, !invert, false, |
| 532 | translate, |
| 533 | SkRect::Make(clipSpaceIBounds))) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 534 | return nullptr; |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 535 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 536 | } else { |
robertphillips | 8b8f36f | 2016-03-02 08:53:12 -0800 | [diff] [blame] | 537 | // all the remaining ops can just be directly draw into the accumulation buffer |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 538 | GrPaint paint; |
| 539 | paint.setAntiAlias(element->isAA()); |
| 540 | paint.setCoverageSetOpXPFactory(op, false); |
| 541 | |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 542 | draw_element(dc.get(), GrNoClip(), paint, translate, element); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 543 | } |
| 544 | } |
| 545 | |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 546 | sk_sp<GrTexture> texture(dc->asTexture()); |
| 547 | SkASSERT(texture); |
| 548 | texture->resourcePriv().setUniqueKey(key); |
| 549 | return texture; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 553 | // Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 554 | // (as opposed to canvas) coordinates |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 555 | bool GrClipMaskManager::CreateStencilClipMask(GrContext* context, |
| 556 | GrDrawContext* drawContext, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 557 | int32_t elementsGenID, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 558 | GrReducedClip::InitialState initialState, |
| 559 | const GrReducedClip::ElementList& elements, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 560 | const SkIRect& clipSpaceIBounds, |
| 561 | const SkIPoint& clipSpaceToStencilOffset) { |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 562 | SkASSERT(drawContext); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 563 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 564 | GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment( |
| 565 | drawContext->accessRenderTarget()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 566 | if (nullptr == stencilAttachment) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 567 | return false; |
| 568 | } |
| 569 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 570 | // TODO: these need to be swapped over to using a StencilAttachmentProxy |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 571 | if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) { |
| 572 | stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 573 | // Set the matrix so that rendered clip elements are transformed from clip to stencil space. |
| 574 | SkVector translate = { |
| 575 | SkIntToScalar(clipSpaceToStencilOffset.fX), |
| 576 | SkIntToScalar(clipSpaceToStencilOffset.fY) |
| 577 | }; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 578 | SkMatrix viewMatrix; |
| 579 | viewMatrix.setTranslate(translate); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 580 | |
bsalomon@google.com | 9f13174 | 2012-12-13 20:43:56 +0000 | [diff] [blame] | 581 | // We set the current clip to the bounds so that our recursive draws are scissored to them. |
| 582 | SkIRect stencilSpaceIBounds(clipSpaceIBounds); |
| 583 | stencilSpaceIBounds.offset(clipSpaceToStencilOffset); |
cdalton | 846c051 | 2016-05-13 10:25:00 -0700 | [diff] [blame] | 584 | GrFixedClip clip(stencilSpaceIBounds); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 585 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 586 | drawContext->drawContextPriv().clearStencilClip( |
| 587 | stencilSpaceIBounds, |
| 588 | GrReducedClip::kAllIn_InitialState == initialState); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 589 | |
| 590 | // walk through each clip element and perform its set op |
| 591 | // with the existing clip. |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 592 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 593 | const Element* element = iter.get(); |
csmartdalton | 656dbe4 | 2016-06-10 12:32:57 -0700 | [diff] [blame] | 594 | bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 595 | |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 596 | bool fillInverted = false; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 597 | // enabled at bottom of loop |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 598 | clip.disableStencilClip(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 599 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 600 | // This will be used to determine whether the clip shape can be rendered into the |
| 601 | // stencil with arbitrary stencil settings. |
| 602 | GrPathRenderer::StencilSupport stencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 603 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 604 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 605 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 606 | GrPathRenderer* pr = nullptr; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 607 | SkPath clipPath; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 608 | if (Element::kRect_Type == element->getType()) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 609 | stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 610 | fillInverted = false; |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 611 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 612 | element->asPath(&clipPath); |
| 613 | fillInverted = clipPath.isInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 614 | if (fillInverted) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 615 | clipPath.toggleInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 616 | } |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 617 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 618 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 619 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 620 | canDrawArgs.fShaderCaps = context->caps()->shaderCaps(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 621 | canDrawArgs.fViewMatrix = &viewMatrix; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 622 | canDrawArgs.fShape = &shape; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 623 | canDrawArgs.fAntiAlias = false; |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 624 | canDrawArgs.fHasUserStencilSettings = false; |
| 625 | canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 626 | |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 627 | pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, |
| 628 | GrPathRendererChain::kStencilOnly_DrawType, |
| 629 | &stencilSupport); |
| 630 | if (!pr) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 631 | return false; |
| 632 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 633 | } |
| 634 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 635 | bool canRenderDirectToStencil = |
| 636 | GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 637 | bool drawDirectToClip; // Given the renderer, the element, |
| 638 | // fill rule, and set operation should |
| 639 | // we render the element directly to |
| 640 | // stencil bit used for clipping. |
| 641 | GrUserStencilSettings const* const* stencilPasses = |
| 642 | GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted, |
| 643 | &drawDirectToClip); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 644 | |
| 645 | // draw the element to the client stencil bits if necessary |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 646 | if (!drawDirectToClip) { |
| 647 | static constexpr GrUserStencilSettings kDrawToStencil( |
| 648 | GrUserStencilSettings::StaticInit< |
| 649 | 0x0000, |
| 650 | GrUserStencilTest::kAlways, |
| 651 | 0xffff, |
| 652 | GrUserStencilOp::kIncMaybeClamp, |
| 653 | GrUserStencilOp::kIncMaybeClamp, |
| 654 | 0xffff>() |
| 655 | ); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 656 | if (Element::kRect_Type == element->getType()) { |
csmartdalton | 656dbe4 | 2016-06-10 12:32:57 -0700 | [diff] [blame] | 657 | drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA, |
| 658 | viewMatrix, element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 659 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 660 | if (!clipPath.isEmpty()) { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 661 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 662 | if (canRenderDirectToStencil) { |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 663 | GrPaint paint; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 664 | paint.setXPFactory(GrDisableColorXPFactory::Make()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 665 | paint.setAntiAlias(element->isAA()); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 666 | |
| 667 | GrPathRenderer::DrawPathArgs args; |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 668 | args.fResourceProvider = context->resourceProvider(); |
| 669 | args.fPaint = &paint; |
| 670 | args.fUserStencilSettings = &kDrawToStencil; |
| 671 | args.fDrawContext = drawContext; |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 672 | args.fClip = &clip; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 673 | args.fViewMatrix = &viewMatrix; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 674 | args.fShape = &shape; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 675 | args.fAntiAlias = false; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 676 | args.fGammaCorrect = false; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 677 | pr->drawPath(args); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 678 | } else { |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 679 | GrPathRenderer::StencilPathArgs args; |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 680 | args.fResourceProvider = context->resourceProvider(); |
| 681 | args.fDrawContext = drawContext; |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 682 | args.fClip = &clip; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 683 | args.fViewMatrix = &viewMatrix; |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 684 | args.fIsAA = element->isAA(); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 685 | args.fShape = &shape; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 686 | pr->stencilPath(args); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 687 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | // now we modify the clip bit by rendering either the clip |
| 693 | // element directly or a bounding rect of the entire clip. |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 694 | for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) { |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 695 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 696 | if (drawDirectToClip) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 697 | if (Element::kRect_Type == element->getType()) { |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 698 | clip.enableStencilClip(element->getRect().makeOffset(translate.fX, |
| 699 | translate.fY)); |
csmartdalton | 656dbe4 | 2016-06-10 12:32:57 -0700 | [diff] [blame] | 700 | drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix, |
| 701 | element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 702 | } else { |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 703 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 704 | GrPaint paint; |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 705 | SkRect bounds = clipPath.getBounds(); |
| 706 | bounds.offset(translate.fX, translate.fY); |
| 707 | clip.enableStencilClip(bounds); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 708 | paint.setXPFactory(GrDisableColorXPFactory::Make()); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 709 | paint.setAntiAlias(element->isAA()); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 710 | GrPathRenderer::DrawPathArgs args; |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 711 | args.fResourceProvider = context->resourceProvider(); |
| 712 | args.fPaint = &paint; |
| 713 | args.fUserStencilSettings = *pass; |
| 714 | args.fDrawContext = drawContext; |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 715 | args.fClip = &clip; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 716 | args.fViewMatrix = &viewMatrix; |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 717 | args.fShape = &shape; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 718 | args.fAntiAlias = false; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 719 | args.fGammaCorrect = false; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 720 | pr->drawPath(args); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 721 | } |
| 722 | } else { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 723 | // The view matrix is setup to do clip space -> stencil space translation, so |
| 724 | // draw rect in clip space. |
bsalomon | 6cc9006 | 2016-07-08 11:31:22 -0700 | [diff] [blame] | 725 | SkRect bounds = SkRect::Make(clipSpaceIBounds); |
| 726 | bounds.offset(translate.fX, translate.fY); |
| 727 | clip.enableStencilClip(bounds); |
csmartdalton | 656dbe4 | 2016-06-10 12:32:57 -0700 | [diff] [blame] | 728 | drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix, |
| 729 | SkRect::Make(clipSpaceIBounds)); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 733 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 734 | return true; |
| 735 | } |
| 736 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 737 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 738 | sk_sp<GrTexture> GrClipMaskManager::CreateSoftwareClipMask( |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 739 | GrTextureProvider* texProvider, |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 740 | int32_t elementsGenID, |
| 741 | GrReducedClip::InitialState initialState, |
| 742 | const GrReducedClip::ElementList& elements, |
| 743 | const SkVector& clipToMaskOffset, |
| 744 | const SkIRect& clipSpaceIBounds) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 745 | GrUniqueKey key; |
| 746 | GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key); |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 747 | if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) { |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 748 | return sk_sp<GrTexture>(texture); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 749 | } |
| 750 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 751 | // The mask texture may be larger than necessary. We round out the clip space bounds and pin |
| 752 | // the top left corner of the resulting rect to the top left of the texture. |
| 753 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 754 | |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 755 | GrSWMaskHelper helper(texProvider); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 756 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 757 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 758 | // space. |
| 759 | SkMatrix translate; |
| 760 | translate.setTranslate(clipToMaskOffset); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 761 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 762 | helper.init(maskSpaceIBounds, &translate); |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 763 | helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 764 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 765 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 766 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 767 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 768 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 769 | if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
| 770 | // Intersect and reverse difference require modifying pixels outside of the geometry |
| 771 | // that is being "drawn". In both cases we erase all the pixels outside of the geometry |
| 772 | // but leave the pixels inside the geometry alone. For reverse difference we invert all |
| 773 | // the pixels before clearing the ones outside the geometry. |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 774 | if (SkRegion::kReverseDifference_Op == op) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 775 | SkRect temp = SkRect::Make(clipSpaceIBounds); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 776 | // invert the entire scene |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 777 | helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 778 | } |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 779 | SkPath clipPath; |
| 780 | element->asPath(&clipPath); |
| 781 | clipPath.toggleInverseFillType(); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 782 | GrShape shape(clipPath, GrStyle::SimpleFill()); |
| 783 | helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 784 | continue; |
| 785 | } |
| 786 | |
| 787 | // The other ops (union, xor, diff) only affect pixels inside |
| 788 | // the geometry so they can just be drawn normally |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 789 | if (Element::kRect_Type == element->getType()) { |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame] | 790 | helper.drawRect(element->getRect(), op, element->isAA(), 0xFF); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 791 | } else { |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 792 | SkPath path; |
| 793 | element->asPath(&path); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 794 | GrShape shape(path, GrStyle::SimpleFill()); |
| 795 | helper.drawShape(shape, op, element->isAA(), 0xFF); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 799 | // Allocate clip mask texture |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 800 | GrSurfaceDesc desc; |
| 801 | desc.fWidth = clipSpaceIBounds.width(); |
| 802 | desc.fHeight = clipSpaceIBounds.height(); |
| 803 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 804 | |
robertphillips | 0152d73 | 2016-05-20 06:38:43 -0700 | [diff] [blame] | 805 | sk_sp<GrTexture> result(texProvider->createApproxTexture(desc)); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 806 | if (!result) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 807 | return nullptr; |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 808 | } |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 809 | result->resourcePriv().setUniqueKey(key); |
| 810 | |
robertphillips | c99b8f0 | 2016-05-15 07:53:35 -0700 | [diff] [blame] | 811 | helper.toTexture(result.get()); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 812 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 813 | return result; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 814 | } |