robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrClipMaskManager.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 9 | #include "GrCaps.h" |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 10 | #include "GrDrawingManager.h" |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 11 | #include "GrDrawContextPriv.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 12 | #include "GrDrawTarget.h" |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 13 | #include "GrGpuResourcePriv.h" |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 14 | #include "GrPaint.h" |
| 15 | #include "GrPathRenderer.h" |
| 16 | #include "GrRenderTarget.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 17 | #include "GrRenderTargetPriv.h" |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 18 | #include "GrResourceProvider.h" |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 19 | #include "GrStencilAttachment.h" |
robertphillips@google.com | 58b2021 | 2012-06-27 20:44:52 +0000 | [diff] [blame] | 20 | #include "GrSWMaskHelper.h" |
joshualitt | 3a0cfeb | 2014-10-27 07:38:01 -0700 | [diff] [blame] | 21 | #include "SkRasterClip.h" |
joshualitt | 3a0cfeb | 2014-10-27 07:38:01 -0700 | [diff] [blame] | 22 | #include "SkTLazy.h" |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 23 | #include "batches/GrRectBatchFactory.h" |
egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 24 | #include "effects/GrConvexPolyEffect.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 25 | #include "effects/GrPorterDuffXferProcessor.h" |
egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 26 | #include "effects/GrRRectEffect.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 27 | #include "effects/GrTextureDomain.h" |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 28 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 29 | typedef SkClipStack::Element Element; |
bsalomon@google.com | 51a6286 | 2012-11-26 21:19:43 +0000 | [diff] [blame] | 30 | |
| 31 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 32 | // 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] | 33 | // stage matrix this also alters the vertex layout |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 34 | static const GrFragmentProcessor* create_fp_for_mask(GrTexture* result, const SkIRect &devBound) { |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 35 | SkMatrix mat; |
bsalomon | 309d4d5 | 2014-12-18 10:17:44 -0800 | [diff] [blame] | 36 | // We use device coords to compute the texture coordinates. We set our matrix to be a |
| 37 | // 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] | 38 | mat.setIDiv(result->width(), result->height()); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 39 | mat.preTranslate(SkIntToScalar(-devBound.fLeft), |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 40 | SkIntToScalar(-devBound.fTop)); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 41 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 42 | SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height()); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 43 | return GrTextureDomainEffect::Create(result, |
| 44 | mat, |
| 45 | GrTextureDomain::MakeTexelDomain(result, domainTexels), |
| 46 | GrTextureDomain::kDecal_Mode, |
| 47 | GrTextureParams::kNone_FilterMode, |
| 48 | kDevice_GrCoordSet); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 49 | } |
| 50 | |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 51 | static void draw_non_aa_rect(GrDrawTarget* drawTarget, |
| 52 | const GrPipelineBuilder& pipelineBuilder, |
| 53 | GrColor color, |
| 54 | const SkMatrix& viewMatrix, |
| 55 | const SkRect& rect) { |
| 56 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect, |
| 57 | nullptr, nullptr)); |
| 58 | drawTarget->drawBatch(pipelineBuilder, batch); |
| 59 | } |
| 60 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 61 | // Does the path in 'element' require SW rendering? If so, return true (and, |
| 62 | // optionally, set 'prOut' to NULL. If not, return false (and, optionally, set |
| 63 | // 'prOut' to the non-SW path renderer that will do the job). |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 64 | bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context, |
| 65 | bool isStencilDisabled, |
| 66 | const GrRenderTarget* rt, |
| 67 | const SkMatrix& viewMatrix, |
| 68 | const Element* element, |
| 69 | GrPathRenderer** prOut, |
| 70 | bool needsStencil) { |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 71 | if (Element::kRect_Type == element->getType()) { |
| 72 | // rects can always be drawn directly w/o using the software path |
| 73 | // TODO: skip rrects once we're drawing them directly. |
| 74 | if (prOut) { |
| 75 | *prOut = nullptr; |
| 76 | } |
| 77 | return false; |
| 78 | } else { |
| 79 | // We shouldn't get here with an empty clip element. |
| 80 | SkASSERT(Element::kEmpty_Type != element->getType()); |
robertphillips | 5c3ea4c | 2015-10-26 08:33:10 -0700 | [diff] [blame] | 81 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 82 | // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer |
| 83 | SkPath path; |
| 84 | element->asPath(&path); |
| 85 | if (path.isInverseFillType()) { |
| 86 | path.toggleInverseFillType(); |
| 87 | } |
| 88 | GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 89 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 90 | GrPathRendererChain::DrawType type; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 91 | |
robertphillips | 423e337 | 2015-10-27 09:23:38 -0700 | [diff] [blame] | 92 | if (needsStencil) { |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 93 | type = element->isAA() |
| 94 | ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType |
| 95 | : GrPathRendererChain::kStencilAndColor_DrawType; |
| 96 | } else { |
| 97 | type = element->isAA() |
| 98 | ? GrPathRendererChain::kColorAntiAlias_DrawType |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 99 | : GrPathRendererChain::kColor_DrawType; |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 100 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 101 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 102 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 103 | canDrawArgs.fShaderCaps = context->caps()->shaderCaps(); |
| 104 | canDrawArgs.fViewMatrix = &viewMatrix; |
| 105 | canDrawArgs.fPath = &path; |
| 106 | canDrawArgs.fStroke = &stroke; |
| 107 | canDrawArgs.fAntiAlias = element->isAA(); |
| 108 | canDrawArgs.fIsStencilDisabled = isStencilDisabled; |
| 109 | canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled(); |
| 110 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 111 | // the 'false' parameter disallows use of the SW path renderer |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 112 | GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type); |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 113 | if (prOut) { |
| 114 | *prOut = pr; |
| 115 | } |
| 116 | return SkToBool(!pr); |
| 117 | } |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 118 | } |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 119 | |
robertphillips | 423e337 | 2015-10-27 09:23:38 -0700 | [diff] [blame] | 120 | // Determines whether it is possible to draw the element to both the stencil buffer and the |
| 121 | // alpha mask simultaneously. If so and the element is a path a compatible path renderer is |
| 122 | // also returned. |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 123 | GrPathRenderer* GrClipMaskManager::GetPathRenderer(GrContext* context, |
| 124 | GrTexture* texture, |
| 125 | const SkMatrix& viewMatrix, |
| 126 | const SkClipStack::Element* element) { |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 127 | GrPathRenderer* pr; |
robertphillips | 423e337 | 2015-10-27 09:23:38 -0700 | [diff] [blame] | 128 | static const bool kNeedsStencil = true; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 129 | static const bool kStencilIsDisabled = true; |
| 130 | PathNeedsSWRenderer(context, |
| 131 | kStencilIsDisabled, |
| 132 | texture->asRenderTarget(), |
| 133 | viewMatrix, |
| 134 | element, |
| 135 | &pr, |
| 136 | kNeedsStencil); |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 137 | return pr; |
robertphillips | 423e337 | 2015-10-27 09:23:38 -0700 | [diff] [blame] | 138 | } |
| 139 | |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 140 | GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget, bool debugClipBatchToBounds) |
bsalomon | c988d2c | 2015-10-07 09:30:05 -0700 | [diff] [blame] | 141 | : fDrawTarget(drawTarget) |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 142 | , fClipMode(kIgnoreClip_StencilClipMode) |
| 143 | , fDebugClipBatchToBounds(debugClipBatchToBounds) { |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 144 | } |
| 145 | |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 146 | GrContext* GrClipMaskManager::getContext() { |
| 147 | return fDrawTarget->cmmAccess().context(); |
| 148 | } |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 149 | |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 150 | const GrCaps* GrClipMaskManager::caps() const { |
| 151 | return fDrawTarget->caps(); |
| 152 | } |
| 153 | |
| 154 | GrResourceProvider* GrClipMaskManager::resourceProvider() { |
| 155 | return fDrawTarget->cmmAccess().resourceProvider(); |
| 156 | } |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 157 | /* |
| 158 | * This method traverses the clip stack to see if the GrSoftwarePathRenderer |
| 159 | * will be used on any element. If so, it returns true to indicate that the |
| 160 | * entire clip should be rendered in SW and then uploaded en masse to the gpu. |
| 161 | */ |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 162 | bool GrClipMaskManager::UseSWOnlyPath(GrContext* context, |
| 163 | const GrPipelineBuilder& pipelineBuilder, |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 164 | const GrRenderTarget* rt, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 165 | const SkVector& clipToMaskOffset, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 166 | const GrReducedClip::ElementList& elements) { |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 167 | // TODO: generalize this function so that when |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 168 | // a clip gets complex enough it can just be done in SW regardless |
| 169 | // of whether it would invoke the GrSoftwarePathRenderer. |
skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 170 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 171 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 172 | // space. |
robertphillips | cf10b5a | 2015-10-27 07:53:35 -0700 | [diff] [blame] | 173 | const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 174 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 175 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 176 | const Element* element = iter.get(); |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 177 | |
| 178 | SkRegion::Op op = element->getOp(); |
| 179 | bool invert = element->isInverseFilled(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 180 | bool needsStencil = invert || |
robertphillips | 423e337 | 2015-10-27 09:23:38 -0700 | [diff] [blame] | 181 | SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op; |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 182 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 183 | if (PathNeedsSWRenderer(context, pipelineBuilder.getStencil().isDisabled(), |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 184 | rt, translate, element, nullptr, needsStencil)) { |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 185 | return true; |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 186 | } |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 187 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 188 | return false; |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 189 | } |
| 190 | |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 191 | bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements, |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 192 | bool abortIfAA, |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 193 | SkVector& clipToRTOffset, |
| 194 | const SkRect* drawBounds, |
| 195 | const GrFragmentProcessor** resultFP) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 196 | SkRect boundsInClipSpace; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 197 | if (drawBounds) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 198 | boundsInClipSpace = *drawBounds; |
| 199 | boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY); |
| 200 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 201 | SkASSERT(elements.count() <= kMaxAnalyticElements); |
| 202 | const GrFragmentProcessor* fps[kMaxAnalyticElements]; |
| 203 | for (int i = 0; i < kMaxAnalyticElements; ++i) { |
| 204 | fps[i] = nullptr; |
| 205 | } |
| 206 | int fpCnt = 0; |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 207 | GrReducedClip::ElementList::Iter iter(elements); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 208 | bool failed = false; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 209 | while (iter.get()) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 210 | SkRegion::Op op = iter.get()->getOp(); |
| 211 | bool invert; |
| 212 | bool skip = false; |
| 213 | switch (op) { |
| 214 | case SkRegion::kReplace_Op: |
| 215 | SkASSERT(iter.get() == elements.head()); |
| 216 | // Fallthrough, handled same as intersect. |
| 217 | case SkRegion::kIntersect_Op: |
| 218 | invert = false; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 219 | if (drawBounds && iter.get()->contains(boundsInClipSpace)) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 220 | skip = true; |
| 221 | } |
| 222 | break; |
| 223 | case SkRegion::kDifference_Op: |
| 224 | invert = true; |
| 225 | // We don't currently have a cheap test for whether a rect is fully outside an |
| 226 | // element's primitive, so don't attempt to set skip. |
| 227 | break; |
| 228 | default: |
| 229 | failed = true; |
| 230 | break; |
| 231 | } |
| 232 | if (failed) { |
| 233 | break; |
| 234 | } |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 235 | if (!skip) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 236 | GrPrimitiveEdgeType edgeType; |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 237 | if (iter.get()->isAA()) { |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 238 | if (abortIfAA) { |
| 239 | failed = true; |
| 240 | break; |
| 241 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 242 | edgeType = |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 243 | invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 244 | } else { |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 245 | edgeType = |
| 246 | invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 247 | } |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 248 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 249 | switch (iter.get()->getType()) { |
| 250 | case SkClipStack::Element::kPath_Type: |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 251 | fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(), |
| 252 | &clipToRTOffset); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 253 | break; |
| 254 | case SkClipStack::Element::kRRect_Type: { |
| 255 | SkRRect rrect = iter.get()->getRRect(); |
| 256 | rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 257 | fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 258 | break; |
| 259 | } |
| 260 | case SkClipStack::Element::kRect_Type: { |
| 261 | SkRect rect = iter.get()->getRect(); |
| 262 | rect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 263 | fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 264 | break; |
| 265 | } |
| 266 | default: |
| 267 | break; |
| 268 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 269 | if (!fps[fpCnt]) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 270 | failed = true; |
| 271 | break; |
| 272 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 273 | fpCnt++; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 274 | } |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 275 | iter.next(); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 276 | } |
| 277 | |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 278 | *resultFP = nullptr; |
| 279 | if (!failed && fpCnt) { |
| 280 | *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 281 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 282 | for (int i = 0; i < fpCnt; ++i) { |
| 283 | fps[i]->unref(); |
| 284 | } |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 285 | return !failed; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 286 | } |
| 287 | |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 288 | static void add_rect_to_clip(const GrClip& clip, const SkRect& devRect, GrClip* out) { |
| 289 | switch (clip.clipType()) { |
| 290 | case GrClip::kClipStack_ClipType: { |
| 291 | SkClipStack* stack = new SkClipStack; |
| 292 | *stack = *clip.clipStack(); |
| 293 | // The stack is actually in clip space not device space. |
| 294 | SkRect clipRect = devRect; |
| 295 | SkPoint origin = { SkIntToScalar(clip.origin().fX), SkIntToScalar(clip.origin().fY) }; |
| 296 | clipRect.offset(origin); |
| 297 | SkIRect iclipRect; |
| 298 | clipRect.roundOut(&iclipRect); |
| 299 | clipRect = SkRect::Make(iclipRect); |
| 300 | stack->clipDevRect(clipRect, SkRegion::kIntersect_Op, false); |
| 301 | out->setClipStack(stack, &clip.origin()); |
| 302 | break; |
| 303 | } |
| 304 | case GrClip::kWideOpen_ClipType: |
| 305 | *out = GrClip(devRect); |
| 306 | break; |
| 307 | case GrClip::kIRect_ClipType: { |
| 308 | SkIRect intersect; |
| 309 | devRect.roundOut(&intersect); |
| 310 | if (intersect.intersect(clip.irect())) { |
| 311 | *out = GrClip(intersect); |
| 312 | } else { |
| 313 | *out = clip; |
| 314 | } |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 320 | bool GrClipMaskManager::setupScissorClip(const GrPipelineBuilder& pipelineBuilder, |
| 321 | GrPipelineBuilder::AutoRestoreStencil* ars, |
| 322 | const SkIRect& clipScissor, |
| 323 | const SkRect* devBounds, |
| 324 | GrAppliedClip* out) { |
| 325 | if (kRespectClip_StencilClipMode == fClipMode) { |
| 326 | fClipMode = kIgnoreClip_StencilClipMode; |
| 327 | } |
| 328 | |
| 329 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
| 330 | |
| 331 | SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height()); |
| 332 | SkIRect devBoundsScissor; |
| 333 | const SkIRect* scissor = &clipScissor; |
| 334 | bool doDevBoundsClip = fDebugClipBatchToBounds && devBounds; |
| 335 | if (doDevBoundsClip) { |
| 336 | devBounds->roundOut(&devBoundsScissor); |
| 337 | if (devBoundsScissor.intersect(clipScissor)) { |
| 338 | scissor = &devBoundsScissor; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (scissor->contains(clipSpaceRTIBounds)) { |
| 343 | // This counts as wide open |
| 344 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | if (clipSpaceRTIBounds.intersect(*scissor)) { |
| 349 | out->fScissorState.set(clipSpaceRTIBounds); |
| 350 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
| 351 | return true; |
| 352 | } |
| 353 | return false; |
| 354 | } |
| 355 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 356 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 357 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 358 | // scissor, or entirely software |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 359 | bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 360 | GrPipelineBuilder::AutoRestoreStencil* ars, |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 361 | const SkRect* devBounds, |
| 362 | GrAppliedClip* out) { |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 363 | if (kRespectClip_StencilClipMode == fClipMode) { |
| 364 | fClipMode = kIgnoreClip_StencilClipMode; |
| 365 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 366 | |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 367 | GrReducedClip::ElementList elements; |
brucedawson | 71d7f7f | 2015-02-26 13:28:53 -0800 | [diff] [blame] | 368 | int32_t genID = 0; |
| 369 | GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 370 | SkIRect clipSpaceIBounds; |
brucedawson | 71d7f7f | 2015-02-26 13:28:53 -0800 | [diff] [blame] | 371 | bool requiresAA = false; |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 372 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 373 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 374 | // GrDrawTarget should have filtered this for us |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 375 | SkASSERT(rt); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 376 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 377 | SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height()); |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 378 | GrClip devBoundsClip; |
| 379 | bool doDevBoundsClip = fDebugClipBatchToBounds && devBounds; |
| 380 | if (doDevBoundsClip) { |
| 381 | add_rect_to_clip(pipelineBuilder.clip(), *devBounds, &devBoundsClip); |
| 382 | } |
| 383 | const GrClip& clip = doDevBoundsClip ? devBoundsClip : pipelineBuilder.clip(); |
| 384 | |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 385 | if (clip.isWideOpen(clipSpaceRTIBounds)) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 386 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 387 | return true; |
| 388 | } |
| 389 | |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 390 | // The clip mask manager always draws with a single IRect so we special case that logic here |
| 391 | // Image filters just use a rect, so we also special case that logic |
| 392 | switch (clip.clipType()) { |
| 393 | case GrClip::kWideOpen_ClipType: |
| 394 | SkFAIL("Should have caught this with clip.isWideOpen()"); |
| 395 | return true; |
bsalomon | 9ce30e1 | 2015-03-06 08:42:34 -0800 | [diff] [blame] | 396 | case GrClip::kIRect_ClipType: { |
| 397 | SkIRect scissor = clip.irect(); |
| 398 | if (scissor.intersect(clipSpaceRTIBounds)) { |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 399 | out->fScissorState.set(scissor); |
bsalomon | 9ce30e1 | 2015-03-06 08:42:34 -0800 | [diff] [blame] | 400 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
| 401 | return true; |
| 402 | } |
| 403 | return false; |
| 404 | } |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 405 | case GrClip::kClipStack_ClipType: { |
| 406 | clipSpaceRTIBounds.offset(clip.origin()); |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 407 | SkIRect clipSpaceReduceQueryBounds; |
bsalomon | 5b592e8 | 2016-03-09 09:55:55 -0800 | [diff] [blame] | 408 | #define DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION 0 |
Brian Salomon | 362c900 | 2015-11-30 17:02:50 -0500 | [diff] [blame] | 409 | if (devBounds && !DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION) { |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 410 | SkIRect devIBounds = devBounds->roundOut(); |
| 411 | devIBounds.offset(clip.origin()); |
| 412 | if (!clipSpaceReduceQueryBounds.intersect(clipSpaceRTIBounds, devIBounds)) { |
| 413 | return false; |
| 414 | } |
| 415 | } else { |
| 416 | clipSpaceReduceQueryBounds = clipSpaceRTIBounds; |
| 417 | } |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 418 | GrReducedClip::ReduceClipStack(*clip.clipStack(), |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 419 | clipSpaceReduceQueryBounds, |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 420 | &elements, |
| 421 | &genID, |
| 422 | &initialState, |
| 423 | &clipSpaceIBounds, |
| 424 | &requiresAA); |
| 425 | if (elements.isEmpty()) { |
| 426 | if (GrReducedClip::kAllIn_InitialState == initialState) { |
| 427 | if (clipSpaceIBounds == clipSpaceRTIBounds) { |
| 428 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
| 429 | return true; |
| 430 | } |
| 431 | } else { |
| 432 | return false; |
| 433 | } |
| 434 | } |
| 435 | } break; |
| 436 | } |
| 437 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 438 | // An element count of 4 was chosen because of the common pattern in Blink of: |
| 439 | // isect RR |
| 440 | // diff RR |
| 441 | // isect convex_poly |
| 442 | // isect convex_poly |
| 443 | // when drawing rounded div borders. This could probably be tuned based on a |
| 444 | // configuration's relative costs of switching RTs to generate a mask vs |
| 445 | // longer shaders. |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 446 | if (elements.count() <= kMaxAnalyticElements) { |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 447 | SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX), |
| 448 | SkIntToScalar(-clip.origin().fY) }; |
cdalton | ede7574 | 2015-11-11 15:27:57 -0800 | [diff] [blame] | 449 | // When there are multiple samples we want to do per-sample clipping, not compute a |
| 450 | // fractional pixel coverage. |
| 451 | bool disallowAnalyticAA = rt->isUnifiedMultisampled() || pipelineBuilder.hasMixedSamples(); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 452 | const GrFragmentProcessor* clipFP = nullptr; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 453 | if (elements.isEmpty() || |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 454 | (requiresAA && |
| 455 | this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds, |
| 456 | &clipFP))) { |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 457 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 458 | scissorSpaceIBounds.offset(-clip.origin()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 459 | if (nullptr == devBounds || |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 460 | !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) { |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 461 | out->fScissorState.set(scissorSpaceIBounds); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 462 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 463 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 464 | out->fClipCoverageFP.reset(clipFP); |
commit-bot@chromium.org | 65ee5f4 | 2014-02-04 17:49:48 +0000 | [diff] [blame] | 465 | return true; |
| 466 | } |
| 467 | } |
bsalomon@google.com | d3066bd | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 468 | |
cdalton | ede7574 | 2015-11-11 15:27:57 -0800 | [diff] [blame] | 469 | // If the stencil buffer is multisampled we can use it to do everything. |
| 470 | if (!rt->isStencilBufferMultisampled() && requiresAA) { |
robertphillips | 588b9ca | 2015-10-04 08:40:31 -0700 | [diff] [blame] | 471 | SkAutoTUnref<GrTexture> result; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 472 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 473 | // The top-left of the mask corresponds to the top-left corner of the bounds. |
| 474 | SkVector clipToMaskOffset = { |
| 475 | SkIntToScalar(-clipSpaceIBounds.fLeft), |
| 476 | SkIntToScalar(-clipSpaceIBounds.fTop) |
| 477 | }; |
| 478 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 479 | if (UseSWOnlyPath(this->getContext(), pipelineBuilder, rt, clipToMaskOffset, elements)) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 480 | // The clip geometry is complex enough that it will be more efficient to create it |
| 481 | // entirely in software |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 482 | result.reset(CreateSoftwareClipMask(this->getContext(), |
| 483 | genID, |
| 484 | initialState, |
| 485 | elements, |
| 486 | clipToMaskOffset, |
| 487 | clipSpaceIBounds)); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 488 | } else { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 489 | result.reset(CreateAlphaClipMask(this->getContext(), |
| 490 | genID, |
| 491 | initialState, |
| 492 | elements, |
| 493 | clipToMaskOffset, |
| 494 | clipSpaceIBounds)); |
| 495 | // If createAlphaClipMask fails it means UseSWOnlyPath has a bug |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 496 | SkASSERT(result); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 497 | } |
| 498 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 499 | if (result) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 500 | // The mask's top left coord should be pinned to the rounded-out top left corner of |
| 501 | // clipSpace bounds. We determine the mask's position WRT to the render target here. |
| 502 | SkIRect rtSpaceMaskBounds = clipSpaceIBounds; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 503 | rtSpaceMaskBounds.offset(-clip.origin()); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 504 | out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds)); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 505 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 506 | return true; |
| 507 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 508 | // 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] | 509 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 510 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 511 | // use the stencil clip if we can't represent the clip as a rectangle. |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 512 | SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 513 | this->createStencilClipMask(rt, |
| 514 | genID, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 515 | initialState, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 516 | elements, |
| 517 | clipSpaceIBounds, |
| 518 | clipSpaceToStencilSpaceOffset); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 519 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 520 | // This must occur after createStencilClipMask. That function may change the scissor. Also, it |
| 521 | // only guarantees that the stencil mask is correct within the bounds it was passed, so we must |
| 522 | // use both stencil and scissor test to the bounds for the final draw. |
| 523 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
| 524 | scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset); |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 525 | out->fScissorState.set(scissorSpaceIBounds); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 526 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 527 | return true; |
| 528 | } |
| 529 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 530 | static bool stencil_element(GrDrawContext* dc, |
| 531 | const SkIRect* scissorRect, |
| 532 | const GrStencilSettings& ss, |
| 533 | const SkMatrix& viewMatrix, |
| 534 | const SkClipStack::Element* element) { |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame] | 535 | |
| 536 | // TODO: Draw rrects directly here. |
| 537 | switch (element->getType()) { |
| 538 | case Element::kEmpty_Type: |
| 539 | SkDEBUGFAIL("Should never get here with an empty element."); |
| 540 | break; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 541 | case Element::kRect_Type: |
| 542 | return dc->drawContextPriv().drawAndStencilRect(scissorRect, ss, |
| 543 | element->getOp(), |
| 544 | element->isInverseFilled(), |
| 545 | element->isAA(), |
| 546 | viewMatrix, element->getRect()); |
| 547 | break; |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame] | 548 | default: { |
| 549 | SkPath path; |
| 550 | element->asPath(&path); |
| 551 | if (path.isInverseFillType()) { |
| 552 | path.toggleInverseFillType(); |
| 553 | } |
| 554 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 555 | return dc->drawContextPriv().drawAndStencilPath(scissorRect, ss, |
| 556 | element->getOp(), |
| 557 | element->isInverseFilled(), |
| 558 | element->isAA(), viewMatrix, path); |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame] | 559 | break; |
| 560 | } |
| 561 | } |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 562 | |
| 563 | return false; |
| 564 | } |
| 565 | |
| 566 | static void draw_element(GrDrawContext* dc, |
| 567 | const GrClip& clip, // TODO: can this just always be WideOpen? |
| 568 | const GrPaint &paint, |
| 569 | const SkMatrix& viewMatrix, |
| 570 | const SkClipStack::Element* element) { |
| 571 | |
| 572 | // TODO: Draw rrects directly here. |
| 573 | switch (element->getType()) { |
| 574 | case Element::kEmpty_Type: |
| 575 | SkDEBUGFAIL("Should never get here with an empty element."); |
| 576 | break; |
| 577 | case Element::kRect_Type: |
| 578 | dc->drawRect(clip, paint, viewMatrix, element->getRect()); |
| 579 | break; |
| 580 | default: { |
| 581 | SkPath path; |
| 582 | element->asPath(&path); |
| 583 | if (path.isInverseFillType()) { |
| 584 | path.toggleInverseFillType(); |
| 585 | } |
| 586 | |
| 587 | dc->drawPath(clip, paint, viewMatrix, path, GrStrokeInfo::FillInfo()); |
| 588 | break; |
| 589 | } |
| 590 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 591 | } |
| 592 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 593 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 594 | // Create a 8-bit clip mask in alpha |
| 595 | |
| 596 | static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) { |
| 597 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 598 | GrUniqueKey::Builder builder(key, kDomain, 3); |
| 599 | builder[0] = clipGenID; |
| 600 | builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16); |
| 601 | builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16); |
| 602 | } |
| 603 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 604 | GrTexture* GrClipMaskManager::CreateAlphaClipMask(GrContext* context, |
| 605 | int32_t elementsGenID, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 606 | GrReducedClip::InitialState initialState, |
| 607 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 608 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 609 | const SkIRect& clipSpaceIBounds) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 610 | GrResourceProvider* resourceProvider = context->resourceProvider(); |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 611 | GrUniqueKey key; |
| 612 | GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key); |
| 613 | if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 614 | return texture; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 615 | } |
| 616 | |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 617 | // There's no texture in the cache. Let's try to allocate it then. |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 618 | GrSurfaceDesc desc; |
| 619 | desc.fWidth = clipSpaceIBounds.width(); |
| 620 | desc.fHeight = clipSpaceIBounds.height(); |
| 621 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 622 | if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 623 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 624 | } else { |
| 625 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 626 | } |
| 627 | |
| 628 | SkAutoTUnref<GrTexture> texture(resourceProvider->createApproxTexture(desc, 0)); |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 629 | if (!texture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 630 | return nullptr; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 631 | } |
| 632 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 633 | texture->resourcePriv().setUniqueKey(key); |
| 634 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 635 | sk_sp<GrDrawContext> dc(context->drawContext(sk_ref_sp(texture->asRenderTarget()))); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 636 | if (!dc) { |
| 637 | return nullptr; |
| 638 | } |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 639 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 640 | // The texture may be larger than necessary, this rect represents the part of the texture |
| 641 | // we populate with a rasterization of the clip. |
| 642 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 643 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 644 | // The scratch texture that we are drawing into can be substantially larger than the mask. Only |
| 645 | // clear the part that we care about. |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 646 | dc->clear(&maskSpaceIBounds, |
| 647 | GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000, |
| 648 | true); |
skia.committer@gmail.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 649 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 650 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 651 | // space. |
| 652 | const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY); |
| 653 | |
| 654 | // 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] | 655 | // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first |
| 656 | // pass must not set values outside of this bounds or stencil values outside the rect won't be |
| 657 | // cleared. |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 658 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 659 | // walk through each clip element and perform its set op |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 660 | for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 661 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 662 | SkRegion::Op op = element->getOp(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 663 | bool invert = element->isInverseFilled(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 664 | if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 665 | #ifdef SK_DEBUG |
| 666 | GrPathRenderer* pr = GetPathRenderer(context, |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 667 | texture, translate, element); |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 668 | if (Element::kRect_Type != element->getType() && !pr) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 669 | // UseSWOnlyPath should now filter out all cases where gpu-side mask merging would |
| 670 | // be performed (i.e., pr would be NULL for a non-rect path). |
| 671 | // See https://bug.skia.org/4519 for rationale and details. |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 672 | SkASSERT(0); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 673 | } |
| 674 | #endif |
| 675 | |
| 676 | // draw directly into the result with the stencil set to make the pixels affected |
| 677 | // by the clip shape be non-zero. |
bsalomon | 3de75da | 2016-04-29 08:44:16 -0700 | [diff] [blame] | 678 | static constexpr GrStencilSettings kStencilInElement( |
| 679 | kReplace_StencilOp, |
| 680 | kReplace_StencilOp, |
| 681 | kAlways_StencilFunc, |
| 682 | 0xffff, |
| 683 | 0xffff, |
| 684 | 0xffff); |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 685 | if (!stencil_element(dc.get(), &maskSpaceIBounds, kStencilInElement, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 686 | translate, element)) { |
| 687 | texture->resourcePriv().removeUniqueKey(); |
| 688 | return nullptr; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 689 | } |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 690 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 691 | // Draw to the exterior pixels (those with a zero stencil value). |
bsalomon | 3de75da | 2016-04-29 08:44:16 -0700 | [diff] [blame] | 692 | static constexpr GrStencilSettings kDrawOutsideElement( |
| 693 | kZero_StencilOp, |
| 694 | kZero_StencilOp, |
| 695 | kEqual_StencilFunc, |
| 696 | 0xffff, |
| 697 | 0x0000, |
| 698 | 0xffff); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 699 | if (!dc->drawContextPriv().drawAndStencilRect(&maskSpaceIBounds, kDrawOutsideElement, |
| 700 | op, !invert, false, |
| 701 | translate, |
| 702 | SkRect::Make(clipSpaceIBounds))) { |
| 703 | texture->resourcePriv().removeUniqueKey(); |
| 704 | return nullptr; |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 705 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 706 | } else { |
robertphillips | 8b8f36f | 2016-03-02 08:53:12 -0800 | [diff] [blame] | 707 | // all the remaining ops can just be directly draw into the accumulation buffer |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 708 | GrPaint paint; |
| 709 | paint.setAntiAlias(element->isAA()); |
| 710 | paint.setCoverageSetOpXPFactory(op, false); |
| 711 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 712 | draw_element(dc.get(), GrClip::WideOpen(), paint, translate, element); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 713 | } |
| 714 | } |
| 715 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 716 | return texture.release(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 720 | // 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] | 721 | // (as opposed to canvas) coordinates |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 722 | bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt, |
| 723 | int32_t elementsGenID, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 724 | GrReducedClip::InitialState initialState, |
| 725 | const GrReducedClip::ElementList& elements, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 726 | const SkIRect& clipSpaceIBounds, |
| 727 | const SkIPoint& clipSpaceToStencilOffset) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 728 | SkASSERT(rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 729 | |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 730 | GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 731 | if (nullptr == stencilAttachment) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 732 | return false; |
| 733 | } |
| 734 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 735 | if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) { |
| 736 | stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 737 | // Set the matrix so that rendered clip elements are transformed from clip to stencil space. |
| 738 | SkVector translate = { |
| 739 | SkIntToScalar(clipSpaceToStencilOffset.fX), |
| 740 | SkIntToScalar(clipSpaceToStencilOffset.fY) |
| 741 | }; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 742 | SkMatrix viewMatrix; |
| 743 | viewMatrix.setTranslate(translate); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 744 | |
bsalomon@google.com | 9f13174 | 2012-12-13 20:43:56 +0000 | [diff] [blame] | 745 | // We set the current clip to the bounds so that our recursive draws are scissored to them. |
| 746 | SkIRect stencilSpaceIBounds(clipSpaceIBounds); |
| 747 | stencilSpaceIBounds.offset(clipSpaceToStencilOffset); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 748 | GrClip clip(stencilSpaceIBounds); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 749 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 750 | int clipBit = stencilAttachment->bits(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 751 | SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers"); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 752 | clipBit = (1 << (clipBit-1)); |
| 753 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 754 | fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds, |
| 755 | GrReducedClip::kAllIn_InitialState == initialState, rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 756 | |
| 757 | // walk through each clip element and perform its set op |
| 758 | // with the existing clip. |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 759 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 760 | const Element* element = iter.get(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 761 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 762 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 763 | pipelineBuilder.setClip(clip); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 764 | pipelineBuilder.setRenderTarget(rt); |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 765 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 766 | pipelineBuilder.setDisableColorXPFactory(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 767 | |
| 768 | // if the target is MSAA then we want MSAA enabled when the clip is soft |
cdalton | ede7574 | 2015-11-11 15:27:57 -0800 | [diff] [blame] | 769 | if (rt->isStencilBufferMultisampled()) { |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 770 | pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA()); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 771 | } |
| 772 | |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 773 | bool fillInverted = false; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 774 | // enabled at bottom of loop |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 775 | fClipMode = kIgnoreClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 776 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 777 | // This will be used to determine whether the clip shape can be rendered into the |
| 778 | // stencil with arbitrary stencil settings. |
| 779 | GrPathRenderer::StencilSupport stencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 780 | |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 781 | GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 782 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 783 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 784 | GrPathRenderer* pr = nullptr; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 785 | SkPath clipPath; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 786 | if (Element::kRect_Type == element->getType()) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 787 | stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 788 | fillInverted = false; |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 789 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 790 | element->asPath(&clipPath); |
| 791 | fillInverted = clipPath.isInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 792 | if (fillInverted) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 793 | clipPath.toggleInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 794 | } |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 795 | |
| 796 | SkASSERT(pipelineBuilder.getStencil().isDisabled()); |
| 797 | |
| 798 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 799 | canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps(); |
| 800 | canDrawArgs.fViewMatrix = &viewMatrix; |
| 801 | canDrawArgs.fPath = &clipPath; |
| 802 | canDrawArgs.fStroke = &stroke; |
| 803 | canDrawArgs.fAntiAlias = false; |
| 804 | canDrawArgs.fIsStencilDisabled = pipelineBuilder.getStencil().isDisabled(); |
| 805 | canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled(); |
| 806 | |
| 807 | pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false, |
| 808 | GrPathRendererChain::kStencilOnly_DrawType, |
| 809 | &stencilSupport); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 810 | if (nullptr == pr) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 811 | return false; |
| 812 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 813 | } |
| 814 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 815 | int passes; |
| 816 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 817 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 818 | bool canRenderDirectToStencil = |
| 819 | GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 820 | bool canDrawDirectToClip; // Given the renderer, the element, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 821 | // fill rule, and set operation can |
| 822 | // we render the element directly to |
| 823 | // stencil bit used for clipping. |
| 824 | canDrawDirectToClip = GrStencilSettings::GetClipPasses(op, |
| 825 | canRenderDirectToStencil, |
| 826 | clipBit, |
| 827 | fillInverted, |
| 828 | &passes, |
| 829 | stencilSettings); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 830 | |
| 831 | // draw the element to the client stencil bits if necessary |
| 832 | if (!canDrawDirectToClip) { |
bsalomon | 3de75da | 2016-04-29 08:44:16 -0700 | [diff] [blame] | 833 | static constexpr GrStencilSettings kDrawToStencil( |
| 834 | kIncClamp_StencilOp, |
| 835 | kIncClamp_StencilOp, |
| 836 | kAlways_StencilFunc, |
| 837 | 0xffff, |
| 838 | 0x0000, |
| 839 | 0xffff); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 840 | if (Element::kRect_Type == element->getType()) { |
bsalomon | 3de75da | 2016-04-29 08:44:16 -0700 | [diff] [blame] | 841 | *pipelineBuilder.stencil() = kDrawToStencil; |
joshualitt | 73bb456 | 2015-03-25 07:16:21 -0700 | [diff] [blame] | 842 | |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 843 | draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix, |
| 844 | element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 845 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 846 | if (!clipPath.isEmpty()) { |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 847 | if (canRenderDirectToStencil) { |
bsalomon | 3de75da | 2016-04-29 08:44:16 -0700 | [diff] [blame] | 848 | *pipelineBuilder.stencil() = kDrawToStencil; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 849 | |
| 850 | GrPathRenderer::DrawPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 851 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 852 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 853 | args.fPipelineBuilder = &pipelineBuilder; |
| 854 | args.fColor = GrColor_WHITE; |
| 855 | args.fViewMatrix = &viewMatrix; |
| 856 | args.fPath = &clipPath; |
| 857 | args.fStroke = &stroke; |
| 858 | args.fAntiAlias = false; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 859 | args.fGammaCorrect = false; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 860 | pr->drawPath(args); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 861 | } else { |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 862 | GrPathRenderer::StencilPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 863 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 864 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 865 | args.fPipelineBuilder = &pipelineBuilder; |
| 866 | args.fViewMatrix = &viewMatrix; |
| 867 | args.fPath = &clipPath; |
| 868 | args.fStroke = &stroke; |
| 869 | pr->stencilPath(args); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 870 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 871 | } |
| 872 | } |
| 873 | } |
| 874 | |
| 875 | // now we modify the clip bit by rendering either the clip |
| 876 | // element directly or a bounding rect of the entire clip. |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 877 | fClipMode = kModifyClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 878 | for (int p = 0; p < passes; ++p) { |
joshualitt | 4f6dc52 | 2015-07-09 12:17:44 -0700 | [diff] [blame] | 879 | *pipelineBuilder.stencil() = stencilSettings[p]; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 880 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 881 | if (canDrawDirectToClip) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 882 | if (Element::kRect_Type == element->getType()) { |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 883 | draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix, |
| 884 | element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 885 | } else { |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 886 | GrPathRenderer::DrawPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 887 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 888 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 889 | args.fPipelineBuilder = &pipelineBuilder; |
| 890 | args.fColor = GrColor_WHITE; |
| 891 | args.fViewMatrix = &viewMatrix; |
| 892 | args.fPath = &clipPath; |
| 893 | args.fStroke = &stroke; |
| 894 | args.fAntiAlias = false; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 895 | args.fGammaCorrect = false; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 896 | pr->drawPath(args); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 897 | } |
| 898 | } else { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 899 | // The view matrix is setup to do clip space -> stencil space translation, so |
| 900 | // draw rect in clip space. |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 901 | draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix, |
| 902 | SkRect::Make(clipSpaceIBounds)); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 903 | } |
| 904 | } |
| 905 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 906 | } |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 907 | fClipMode = kRespectClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 908 | return true; |
| 909 | } |
| 910 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 911 | // mapping of clip-respecting stencil funcs to normal stencil funcs |
| 912 | // mapping depends on whether stencil-clipping is in effect. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 913 | static const GrStencilFunc |
bsalomon | 3de75da | 2016-04-29 08:44:16 -0700 | [diff] [blame] | 914 | gSpecialToBasicStencilFunc[2][kClipStencilFuncCnt] = { |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 915 | {// Stencil-Clipping is DISABLED, we are effectively always inside the clip |
| 916 | // In the Clip Funcs |
| 917 | kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 918 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 919 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 920 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 921 | // Special in the clip func that forces user's ref to be 0. |
| 922 | kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 923 | // make ref 0 and do normal nequal. |
| 924 | }, |
| 925 | {// Stencil-Clipping is ENABLED |
| 926 | // In the Clip Funcs |
| 927 | kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 928 | // eq stencil clip bit, mask |
| 929 | // out user bits. |
| 930 | |
| 931 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 932 | // add stencil bit to mask and ref |
| 933 | |
| 934 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 935 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 936 | // for both of these we can add |
| 937 | // the clip bit to the mask and |
| 938 | // ref and compare as normal |
| 939 | // Special in the clip func that forces user's ref to be 0. |
| 940 | kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 941 | // make ref have only the clip bit set |
| 942 | // and make comparison be less |
| 943 | // 10..0 < 1..user_bits.. |
| 944 | } |
| 945 | }; |
| 946 | |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 947 | void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 948 | GrPipelineBuilder::AutoRestoreStencil* ars) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 949 | // We make two copies of the StencilSettings here (except in the early |
| 950 | // exit scenario. One copy from draw state to the stack var. Then another |
| 951 | // from the stack var to the gpu. We could make this class hold a ptr to |
| 952 | // GrGpu's fStencilSettings and eliminate the stack copy here. |
| 953 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 954 | // use stencil for clipping if clipping is enabled and the clip |
| 955 | // has been written into the stencil. |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 956 | GrStencilSettings settings; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 957 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 958 | // The GrGpu client may not be using the stencil buffer but we may need to |
| 959 | // enable it in order to respect a stencil clip. |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 960 | if (pipelineBuilder.getStencil().isDisabled()) { |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 961 | if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) { |
bsalomon | 3de75da | 2016-04-29 08:44:16 -0700 | [diff] [blame] | 962 | static constexpr GrStencilSettings kBasicApplyClipSettings( |
| 963 | kKeep_StencilOp, |
| 964 | kKeep_StencilOp, |
| 965 | kAlwaysIfInClip_StencilFunc, |
| 966 | 0x0000, |
| 967 | 0x0000, |
| 968 | 0x0000); |
| 969 | settings = kBasicApplyClipSettings; |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 970 | } else { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 971 | return; |
| 972 | } |
| 973 | } else { |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 974 | settings = pipelineBuilder.getStencil(); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 975 | } |
| 976 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 977 | int stencilBits = 0; |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 978 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 979 | GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt); |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 980 | if (stencilAttachment) { |
| 981 | stencilBits = stencilAttachment->bits(); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 982 | } |
| 983 | |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 984 | SkASSERT(this->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp()); |
| 985 | SkASSERT(this->caps()->twoSidedStencilSupport() || !settings.isTwoSided()); |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 986 | this->adjustStencilParams(&settings, fClipMode, stencilBits); |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 987 | ars->set(&pipelineBuilder); |
| 988 | ars->setStencil(settings); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings, |
| 992 | StencilClipMode mode, |
| 993 | int stencilBitCnt) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 994 | SkASSERT(stencilBitCnt > 0); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 995 | |
| 996 | if (kModifyClip_StencilClipMode == mode) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 997 | // We assume that this clip manager itself is drawing to the GrGpu and |
| 998 | // has already setup the correct values. |
| 999 | return; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1000 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1001 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1002 | unsigned int clipBit = (1 << (stencilBitCnt - 1)); |
| 1003 | unsigned int userBits = clipBit - 1; |
| 1004 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1005 | GrStencilSettings::Face face = GrStencilSettings::kFront_Face; |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 1006 | bool twoSided = this->caps()->twoSidedStencilSupport(); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1007 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1008 | bool finished = false; |
| 1009 | while (!finished) { |
| 1010 | GrStencilFunc func = settings->func(face); |
| 1011 | uint16_t writeMask = settings->writeMask(face); |
| 1012 | uint16_t funcMask = settings->funcMask(face); |
| 1013 | uint16_t funcRef = settings->funcRef(face); |
| 1014 | |
bsalomon | 3de75da | 2016-04-29 08:44:16 -0700 | [diff] [blame] | 1015 | SkASSERT((unsigned) func < kStencilFuncCnt); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1016 | |
| 1017 | writeMask &= userBits; |
| 1018 | |
bsalomon | 3de75da | 2016-04-29 08:44:16 -0700 | [diff] [blame] | 1019 | if (func >= kBasicStencilFuncCnt) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1020 | int respectClip = kRespectClip_StencilClipMode == mode; |
| 1021 | if (respectClip) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1022 | switch (func) { |
| 1023 | case kAlwaysIfInClip_StencilFunc: |
| 1024 | funcMask = clipBit; |
| 1025 | funcRef = clipBit; |
| 1026 | break; |
| 1027 | case kEqualIfInClip_StencilFunc: |
| 1028 | case kLessIfInClip_StencilFunc: |
| 1029 | case kLEqualIfInClip_StencilFunc: |
| 1030 | funcMask = (funcMask & userBits) | clipBit; |
| 1031 | funcRef = (funcRef & userBits) | clipBit; |
| 1032 | break; |
| 1033 | case kNonZeroIfInClip_StencilFunc: |
| 1034 | funcMask = (funcMask & userBits) | clipBit; |
| 1035 | funcRef = clipBit; |
| 1036 | break; |
| 1037 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 1038 | SkFAIL("Unknown stencil func"); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1039 | } |
| 1040 | } else { |
| 1041 | funcMask &= userBits; |
| 1042 | funcRef &= userBits; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1043 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1044 | const GrStencilFunc* table = |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1045 | gSpecialToBasicStencilFunc[respectClip]; |
bsalomon | 3de75da | 2016-04-29 08:44:16 -0700 | [diff] [blame] | 1046 | func = table[func - kBasicStencilFuncCnt]; |
| 1047 | SkASSERT(func >= 0 && func < kBasicStencilFuncCnt); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1048 | } else { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1049 | funcMask &= userBits; |
| 1050 | funcRef &= userBits; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1051 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1052 | |
| 1053 | settings->setFunc(face, func); |
| 1054 | settings->setWriteMask(face, writeMask); |
| 1055 | settings->setFuncMask(face, funcMask); |
| 1056 | settings->setFuncRef(face, funcRef); |
| 1057 | |
| 1058 | if (GrStencilSettings::kFront_Face == face) { |
| 1059 | face = GrStencilSettings::kBack_Face; |
| 1060 | finished = !twoSided; |
| 1061 | } else { |
| 1062 | finished = true; |
| 1063 | } |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1064 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1065 | if (!twoSided) { |
| 1066 | settings->copyFrontSettingsToBack(); |
| 1067 | } |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1068 | } |
| 1069 | |
| 1070 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1071 | GrTexture* GrClipMaskManager::CreateSoftwareClipMask(GrContext* context, |
| 1072 | int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1073 | GrReducedClip::InitialState initialState, |
| 1074 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1075 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1076 | const SkIRect& clipSpaceIBounds) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 1077 | GrUniqueKey key; |
| 1078 | GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1079 | GrResourceProvider* resourceProvider = context->resourceProvider(); |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 1080 | if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) { |
| 1081 | return texture; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1084 | // The mask texture may be larger than necessary. We round out the clip space bounds and pin |
| 1085 | // the top left corner of the resulting rect to the top left of the texture. |
| 1086 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 1087 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1088 | GrSWMaskHelper helper(context); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1089 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1090 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 1091 | // space. |
| 1092 | SkMatrix translate; |
| 1093 | translate.setTranslate(clipToMaskOffset); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1094 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1095 | helper.init(maskSpaceIBounds, &translate, false); |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 1096 | helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00); |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 1097 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 1098 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 1099 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1100 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1101 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1102 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1103 | if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
| 1104 | // Intersect and reverse difference require modifying pixels outside of the geometry |
| 1105 | // that is being "drawn". In both cases we erase all the pixels outside of the geometry |
| 1106 | // but leave the pixels inside the geometry alone. For reverse difference we invert all |
| 1107 | // the pixels before clearing the ones outside the geometry. |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1108 | if (SkRegion::kReverseDifference_Op == op) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 1109 | SkRect temp = SkRect::Make(clipSpaceIBounds); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1110 | // invert the entire scene |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 1111 | helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1112 | } |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 1113 | SkPath clipPath; |
| 1114 | element->asPath(&clipPath); |
| 1115 | clipPath.toggleInverseFillType(); |
| 1116 | helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1117 | continue; |
| 1118 | } |
| 1119 | |
| 1120 | // The other ops (union, xor, diff) only affect pixels inside |
| 1121 | // the geometry so they can just be drawn normally |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1122 | if (Element::kRect_Type == element->getType()) { |
| 1123 | helper.draw(element->getRect(), op, element->isAA(), 0xFF); |
| 1124 | } else { |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 1125 | SkPath path; |
| 1126 | element->asPath(&path); |
| 1127 | helper.draw(path, stroke, op, element->isAA(), 0xFF); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1128 | } |
| 1129 | } |
| 1130 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 1131 | // Allocate clip mask texture |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1132 | GrSurfaceDesc desc; |
| 1133 | desc.fWidth = clipSpaceIBounds.width(); |
| 1134 | desc.fHeight = clipSpaceIBounds.height(); |
| 1135 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 1136 | |
| 1137 | GrTexture* result = context->resourceProvider()->createApproxTexture(desc, 0); |
| 1138 | if (!result) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1139 | return nullptr; |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 1140 | } |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 1141 | result->resourcePriv().setUniqueKey(key); |
| 1142 | |
robertphillips@google.com | d92cf2e | 2013-07-19 18:13:02 +0000 | [diff] [blame] | 1143 | helper.toTexture(result); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1144 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1145 | return result; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1146 | } |
| 1147 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1148 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 1149 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1150 | void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1151 | GrStencilSettings* settings) { |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1152 | if (stencilAttachment) { |
| 1153 | int stencilBits = stencilAttachment->bits(); |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 1154 | this->adjustStencilParams(settings, fClipMode, stencilBits); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 1155 | } |
| 1156 | } |