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