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