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