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, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 53 | const GrClip& clip, |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 54 | GrColor color, |
| 55 | const SkMatrix& viewMatrix, |
| 56 | const SkRect& rect) { |
| 57 | SkAutoTUnref<GrDrawBatch> batch(GrRectBatchFactory::CreateNonAAFill(color, viewMatrix, rect, |
| 58 | nullptr, nullptr)); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 59 | drawTarget->drawBatch(pipelineBuilder, clip, batch); |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 60 | } |
| 61 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 62 | // Does the path in 'element' require SW rendering? If so, return true (and, |
| 63 | // optionally, set 'prOut' to NULL. If not, return false (and, optionally, set |
| 64 | // 'prOut' to the non-SW path renderer that will do the job). |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 65 | bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 66 | bool hasUserStencilSettings, |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 67 | const GrRenderTarget* rt, |
| 68 | const SkMatrix& viewMatrix, |
| 69 | const Element* element, |
| 70 | GrPathRenderer** prOut, |
| 71 | bool needsStencil) { |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 72 | if (Element::kRect_Type == element->getType()) { |
| 73 | // rects can always be drawn directly w/o using the software path |
| 74 | // TODO: skip rrects once we're drawing them directly. |
| 75 | if (prOut) { |
| 76 | *prOut = nullptr; |
| 77 | } |
| 78 | return false; |
| 79 | } else { |
| 80 | // We shouldn't get here with an empty clip element. |
| 81 | SkASSERT(Element::kEmpty_Type != element->getType()); |
robertphillips | 5c3ea4c | 2015-10-26 08:33:10 -0700 | [diff] [blame] | 82 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 83 | // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer |
| 84 | SkPath path; |
| 85 | element->asPath(&path); |
| 86 | if (path.isInverseFillType()) { |
| 87 | path.toggleInverseFillType(); |
| 88 | } |
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; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 106 | canDrawArgs.fStyle = &GrStyle::SimpleFill(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 107 | canDrawArgs.fAntiAlias = element->isAA(); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 108 | canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 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; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 128 | constexpr bool kNeedsStencil = true; |
| 129 | constexpr bool kHasUserStencilSettings = false; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 130 | PathNeedsSWRenderer(context, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 131 | kHasUserStencilSettings, |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 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 | |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 140 | GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget) |
bsalomon | c988d2c | 2015-10-07 09:30:05 -0700 | [diff] [blame] | 141 | : fDrawTarget(drawTarget) |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 142 | , fClipMode(kIgnoreClip_StencilClipMode) { |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 143 | } |
| 144 | |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 145 | GrContext* GrClipMaskManager::getContext() { |
| 146 | return fDrawTarget->cmmAccess().context(); |
| 147 | } |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 148 | |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 149 | const GrCaps* GrClipMaskManager::caps() const { |
| 150 | return fDrawTarget->caps(); |
| 151 | } |
| 152 | |
| 153 | GrResourceProvider* GrClipMaskManager::resourceProvider() { |
| 154 | return fDrawTarget->cmmAccess().resourceProvider(); |
| 155 | } |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 156 | /* |
| 157 | * This method traverses the clip stack to see if the GrSoftwarePathRenderer |
| 158 | * will be used on any element. If so, it returns true to indicate that the |
| 159 | * entire clip should be rendered in SW and then uploaded en masse to the gpu. |
| 160 | */ |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 161 | bool GrClipMaskManager::UseSWOnlyPath(GrContext* context, |
| 162 | const GrPipelineBuilder& pipelineBuilder, |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 163 | const GrRenderTarget* rt, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 164 | const SkVector& clipToMaskOffset, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 165 | const GrReducedClip::ElementList& elements) { |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 166 | // TODO: generalize this function so that when |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 167 | // a clip gets complex enough it can just be done in SW regardless |
| 168 | // of whether it would invoke the GrSoftwarePathRenderer. |
skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 169 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 170 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 171 | // space. |
robertphillips | cf10b5a | 2015-10-27 07:53:35 -0700 | [diff] [blame] | 172 | const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY); |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 173 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 174 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 175 | const Element* element = iter.get(); |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 176 | |
| 177 | SkRegion::Op op = element->getOp(); |
| 178 | bool invert = element->isInverseFilled(); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 179 | bool needsStencil = invert || |
robertphillips | 423e337 | 2015-10-27 09:23:38 -0700 | [diff] [blame] | 180 | SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op; |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 181 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 182 | if (PathNeedsSWRenderer(context, pipelineBuilder.hasUserStencilSettings(), |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 183 | rt, translate, element, nullptr, needsStencil)) { |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 184 | return true; |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 185 | } |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 186 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 187 | return false; |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 188 | } |
| 189 | |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 190 | bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements, |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 191 | bool abortIfAA, |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 192 | SkVector& clipToRTOffset, |
| 193 | const SkRect* drawBounds, |
| 194 | const GrFragmentProcessor** resultFP) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 195 | SkRect boundsInClipSpace; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 196 | if (drawBounds) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 197 | boundsInClipSpace = *drawBounds; |
| 198 | boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY); |
| 199 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 200 | SkASSERT(elements.count() <= kMaxAnalyticElements); |
| 201 | const GrFragmentProcessor* fps[kMaxAnalyticElements]; |
| 202 | for (int i = 0; i < kMaxAnalyticElements; ++i) { |
| 203 | fps[i] = nullptr; |
| 204 | } |
| 205 | int fpCnt = 0; |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 206 | GrReducedClip::ElementList::Iter iter(elements); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 207 | bool failed = false; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 208 | while (iter.get()) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 209 | SkRegion::Op op = iter.get()->getOp(); |
| 210 | bool invert; |
| 211 | bool skip = false; |
| 212 | switch (op) { |
| 213 | case SkRegion::kReplace_Op: |
| 214 | SkASSERT(iter.get() == elements.head()); |
| 215 | // Fallthrough, handled same as intersect. |
| 216 | case SkRegion::kIntersect_Op: |
| 217 | invert = false; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 218 | if (drawBounds && iter.get()->contains(boundsInClipSpace)) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 219 | skip = true; |
| 220 | } |
| 221 | break; |
| 222 | case SkRegion::kDifference_Op: |
| 223 | invert = true; |
| 224 | // We don't currently have a cheap test for whether a rect is fully outside an |
| 225 | // element's primitive, so don't attempt to set skip. |
| 226 | break; |
| 227 | default: |
| 228 | failed = true; |
| 229 | break; |
| 230 | } |
| 231 | if (failed) { |
| 232 | break; |
| 233 | } |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 234 | if (!skip) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 235 | GrPrimitiveEdgeType edgeType; |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 236 | if (iter.get()->isAA()) { |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 237 | if (abortIfAA) { |
| 238 | failed = true; |
| 239 | break; |
| 240 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 241 | edgeType = |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 242 | invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 243 | } else { |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 244 | edgeType = |
| 245 | invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 246 | } |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 247 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 248 | switch (iter.get()->getType()) { |
| 249 | case SkClipStack::Element::kPath_Type: |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 250 | fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(), |
| 251 | &clipToRTOffset); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 252 | break; |
| 253 | case SkClipStack::Element::kRRect_Type: { |
| 254 | SkRRect rrect = iter.get()->getRRect(); |
| 255 | rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 256 | fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | case SkClipStack::Element::kRect_Type: { |
| 260 | SkRect rect = iter.get()->getRect(); |
| 261 | rect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 262 | fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 263 | break; |
| 264 | } |
| 265 | default: |
| 266 | break; |
| 267 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 268 | if (!fps[fpCnt]) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 269 | failed = true; |
| 270 | break; |
| 271 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 272 | fpCnt++; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 273 | } |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 274 | iter.next(); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 275 | } |
| 276 | |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 277 | *resultFP = nullptr; |
| 278 | if (!failed && fpCnt) { |
| 279 | *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 280 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 281 | for (int i = 0; i < fpCnt; ++i) { |
| 282 | fps[i]->unref(); |
| 283 | } |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame] | 284 | return !failed; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 285 | } |
| 286 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 287 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 288 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 289 | // scissor, or entirely software |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 290 | bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 291 | const GrClip& clip, |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 292 | const SkRect* devBounds, |
| 293 | GrAppliedClip* out) { |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 294 | if (kRespectClip_StencilClipMode == fClipMode) { |
| 295 | fClipMode = kIgnoreClip_StencilClipMode; |
| 296 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 297 | |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 298 | GrReducedClip::ElementList elements; |
brucedawson | 71d7f7f | 2015-02-26 13:28:53 -0800 | [diff] [blame] | 299 | int32_t genID = 0; |
| 300 | GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 301 | SkIRect clipSpaceIBounds; |
brucedawson | 71d7f7f | 2015-02-26 13:28:53 -0800 | [diff] [blame] | 302 | bool requiresAA = false; |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 303 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 304 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 305 | // GrDrawTarget should have filtered this for us |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 306 | SkASSERT(rt); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 307 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 308 | SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height()); |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 309 | if (clip.isWideOpen(clipSpaceRTIBounds)) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 310 | return true; |
| 311 | } |
| 312 | |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 313 | // The clip mask manager always draws with a single IRect so we special case that logic here |
| 314 | // Image filters just use a rect, so we also special case that logic |
| 315 | switch (clip.clipType()) { |
| 316 | case GrClip::kWideOpen_ClipType: |
| 317 | SkFAIL("Should have caught this with clip.isWideOpen()"); |
| 318 | return true; |
bsalomon | 9ce30e1 | 2015-03-06 08:42:34 -0800 | [diff] [blame] | 319 | case GrClip::kIRect_ClipType: { |
| 320 | SkIRect scissor = clip.irect(); |
| 321 | if (scissor.intersect(clipSpaceRTIBounds)) { |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 322 | out->fScissorState.set(scissor); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 323 | out->fHasStencilClip = kIgnoreClip_StencilClipMode != fClipMode; |
bsalomon | 9ce30e1 | 2015-03-06 08:42:34 -0800 | [diff] [blame] | 324 | return true; |
| 325 | } |
| 326 | return false; |
| 327 | } |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 328 | case GrClip::kClipStack_ClipType: { |
| 329 | clipSpaceRTIBounds.offset(clip.origin()); |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 330 | SkIRect clipSpaceReduceQueryBounds; |
bsalomon | 5b592e8 | 2016-03-09 09:55:55 -0800 | [diff] [blame] | 331 | #define DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION 0 |
Brian Salomon | 362c900 | 2015-11-30 17:02:50 -0500 | [diff] [blame] | 332 | if (devBounds && !DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION) { |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 333 | SkIRect devIBounds = devBounds->roundOut(); |
| 334 | devIBounds.offset(clip.origin()); |
| 335 | if (!clipSpaceReduceQueryBounds.intersect(clipSpaceRTIBounds, devIBounds)) { |
| 336 | return false; |
| 337 | } |
| 338 | } else { |
| 339 | clipSpaceReduceQueryBounds = clipSpaceRTIBounds; |
| 340 | } |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 341 | GrReducedClip::ReduceClipStack(*clip.clipStack(), |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 342 | clipSpaceReduceQueryBounds, |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 343 | &elements, |
| 344 | &genID, |
| 345 | &initialState, |
| 346 | &clipSpaceIBounds, |
| 347 | &requiresAA); |
| 348 | if (elements.isEmpty()) { |
| 349 | if (GrReducedClip::kAllIn_InitialState == initialState) { |
| 350 | if (clipSpaceIBounds == clipSpaceRTIBounds) { |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 351 | return true; |
| 352 | } |
| 353 | } else { |
| 354 | return false; |
| 355 | } |
| 356 | } |
| 357 | } break; |
| 358 | } |
| 359 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 360 | SkASSERT(kIgnoreClip_StencilClipMode == fClipMode); // TODO: Remove fClipMode. |
| 361 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 362 | // An element count of 4 was chosen because of the common pattern in Blink of: |
| 363 | // isect RR |
| 364 | // diff RR |
| 365 | // isect convex_poly |
| 366 | // isect convex_poly |
| 367 | // when drawing rounded div borders. This could probably be tuned based on a |
| 368 | // configuration's relative costs of switching RTs to generate a mask vs |
| 369 | // longer shaders. |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 370 | if (elements.count() <= kMaxAnalyticElements) { |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 371 | SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX), |
| 372 | SkIntToScalar(-clip.origin().fY) }; |
cdalton | ede7574 | 2015-11-11 15:27:57 -0800 | [diff] [blame] | 373 | // When there are multiple samples we want to do per-sample clipping, not compute a |
| 374 | // fractional pixel coverage. |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 375 | bool disallowAnalyticAA = rt->isStencilBufferMultisampled(); |
| 376 | if (disallowAnalyticAA && !rt->numColorSamples()) { |
| 377 | // With a single color sample, any coverage info is lost from color once it hits the |
| 378 | // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe |
| 379 | // is multisampled. |
| 380 | disallowAnalyticAA = pipelineBuilder.isHWAntialias() || |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 381 | pipelineBuilder.hasUserStencilSettings(); |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 382 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 383 | const GrFragmentProcessor* clipFP = nullptr; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 384 | if (elements.isEmpty() || |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 385 | (requiresAA && |
| 386 | this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds, |
| 387 | &clipFP))) { |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 388 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 389 | scissorSpaceIBounds.offset(-clip.origin()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 390 | if (nullptr == devBounds || |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 391 | !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) { |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 392 | out->fScissorState.set(scissorSpaceIBounds); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 393 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 394 | out->fClipCoverageFP.reset(clipFP); |
commit-bot@chromium.org | 65ee5f4 | 2014-02-04 17:49:48 +0000 | [diff] [blame] | 395 | return true; |
| 396 | } |
| 397 | } |
bsalomon@google.com | d3066bd | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 398 | |
cdalton | ede7574 | 2015-11-11 15:27:57 -0800 | [diff] [blame] | 399 | // If the stencil buffer is multisampled we can use it to do everything. |
| 400 | if (!rt->isStencilBufferMultisampled() && requiresAA) { |
robertphillips | 588b9ca | 2015-10-04 08:40:31 -0700 | [diff] [blame] | 401 | SkAutoTUnref<GrTexture> result; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 402 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 403 | // The top-left of the mask corresponds to the top-left corner of the bounds. |
| 404 | SkVector clipToMaskOffset = { |
| 405 | SkIntToScalar(-clipSpaceIBounds.fLeft), |
| 406 | SkIntToScalar(-clipSpaceIBounds.fTop) |
| 407 | }; |
| 408 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 409 | if (UseSWOnlyPath(this->getContext(), pipelineBuilder, rt, clipToMaskOffset, elements)) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 410 | // The clip geometry is complex enough that it will be more efficient to create it |
| 411 | // entirely in software |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 412 | result.reset(CreateSoftwareClipMask(this->getContext(), |
| 413 | genID, |
| 414 | initialState, |
| 415 | elements, |
| 416 | clipToMaskOffset, |
| 417 | clipSpaceIBounds)); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 418 | } else { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 419 | result.reset(CreateAlphaClipMask(this->getContext(), |
| 420 | genID, |
| 421 | initialState, |
| 422 | elements, |
| 423 | clipToMaskOffset, |
| 424 | clipSpaceIBounds)); |
| 425 | // If createAlphaClipMask fails it means UseSWOnlyPath has a bug |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 426 | SkASSERT(result); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 427 | } |
| 428 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 429 | if (result) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 430 | // The mask's top left coord should be pinned to the rounded-out top left corner of |
| 431 | // clipSpace bounds. We determine the mask's position WRT to the render target here. |
| 432 | SkIRect rtSpaceMaskBounds = clipSpaceIBounds; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 433 | rtSpaceMaskBounds.offset(-clip.origin()); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 434 | out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 435 | return true; |
| 436 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 437 | // 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] | 438 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 439 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 440 | // 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] | 441 | SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 442 | this->createStencilClipMask(rt, |
| 443 | genID, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 444 | initialState, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 445 | elements, |
| 446 | clipSpaceIBounds, |
| 447 | clipSpaceToStencilSpaceOffset); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 448 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 449 | // This must occur after createStencilClipMask. That function may change the scissor. Also, it |
| 450 | // only guarantees that the stencil mask is correct within the bounds it was passed, so we must |
| 451 | // use both stencil and scissor test to the bounds for the final draw. |
| 452 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
| 453 | scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset); |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 454 | out->fScissorState.set(scissorSpaceIBounds); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 455 | SkASSERT(kRespectClip_StencilClipMode == fClipMode); // TODO: Remove fClipMode. |
| 456 | out->fHasStencilClip = true; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 457 | return true; |
| 458 | } |
| 459 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 460 | static bool stencil_element(GrDrawContext* dc, |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 461 | const GrClip& clip, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 462 | const GrUserStencilSettings* ss, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 463 | const SkMatrix& viewMatrix, |
| 464 | const SkClipStack::Element* element) { |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame] | 465 | |
| 466 | // TODO: Draw rrects directly here. |
| 467 | switch (element->getType()) { |
| 468 | case Element::kEmpty_Type: |
| 469 | SkDEBUGFAIL("Should never get here with an empty element."); |
| 470 | break; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 471 | case Element::kRect_Type: |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 472 | return dc->drawContextPriv().drawAndStencilRect(clip, ss, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 473 | element->getOp(), |
| 474 | element->isInverseFilled(), |
| 475 | element->isAA(), |
| 476 | viewMatrix, element->getRect()); |
| 477 | break; |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame] | 478 | default: { |
| 479 | SkPath path; |
| 480 | element->asPath(&path); |
| 481 | if (path.isInverseFillType()) { |
| 482 | path.toggleInverseFillType(); |
| 483 | } |
| 484 | |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 485 | return dc->drawContextPriv().drawAndStencilPath(clip, ss, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 486 | element->getOp(), |
| 487 | element->isInverseFilled(), |
| 488 | element->isAA(), viewMatrix, path); |
robertphillips | 86c6075 | 2016-03-02 08:43:13 -0800 | [diff] [blame] | 489 | break; |
| 490 | } |
| 491 | } |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 492 | |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | static void draw_element(GrDrawContext* dc, |
| 497 | const GrClip& clip, // TODO: can this just always be WideOpen? |
| 498 | const GrPaint &paint, |
| 499 | const SkMatrix& viewMatrix, |
| 500 | const SkClipStack::Element* element) { |
| 501 | |
| 502 | // TODO: Draw rrects directly here. |
| 503 | switch (element->getType()) { |
| 504 | case Element::kEmpty_Type: |
| 505 | SkDEBUGFAIL("Should never get here with an empty element."); |
| 506 | break; |
| 507 | case Element::kRect_Type: |
| 508 | dc->drawRect(clip, paint, viewMatrix, element->getRect()); |
| 509 | break; |
| 510 | default: { |
| 511 | SkPath path; |
| 512 | element->asPath(&path); |
| 513 | if (path.isInverseFillType()) { |
| 514 | path.toggleInverseFillType(); |
| 515 | } |
| 516 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 517 | dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill()); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 518 | break; |
| 519 | } |
| 520 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 521 | } |
| 522 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 523 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 524 | // Create a 8-bit clip mask in alpha |
| 525 | |
| 526 | static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) { |
| 527 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 528 | GrUniqueKey::Builder builder(key, kDomain, 3); |
| 529 | builder[0] = clipGenID; |
| 530 | builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16); |
| 531 | builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16); |
| 532 | } |
| 533 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 534 | GrTexture* GrClipMaskManager::CreateAlphaClipMask(GrContext* context, |
| 535 | int32_t elementsGenID, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 536 | GrReducedClip::InitialState initialState, |
| 537 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 538 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 539 | const SkIRect& clipSpaceIBounds) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 540 | GrResourceProvider* resourceProvider = context->resourceProvider(); |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 541 | GrUniqueKey key; |
| 542 | GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key); |
| 543 | if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 544 | return texture; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 545 | } |
| 546 | |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 547 | // 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] | 548 | GrSurfaceDesc desc; |
| 549 | desc.fWidth = clipSpaceIBounds.width(); |
| 550 | desc.fHeight = clipSpaceIBounds.height(); |
| 551 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
| 552 | if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 553 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 554 | } else { |
| 555 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 556 | } |
| 557 | |
| 558 | SkAutoTUnref<GrTexture> texture(resourceProvider->createApproxTexture(desc, 0)); |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 559 | if (!texture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 560 | return nullptr; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 561 | } |
| 562 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 563 | texture->resourcePriv().setUniqueKey(key); |
| 564 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 565 | sk_sp<GrDrawContext> dc(context->drawContext(sk_ref_sp(texture->asRenderTarget()))); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 566 | if (!dc) { |
| 567 | return nullptr; |
| 568 | } |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 569 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 570 | // The texture may be larger than necessary, this rect represents the part of the texture |
| 571 | // we populate with a rasterization of the clip. |
| 572 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 573 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 574 | // The scratch texture that we are drawing into can be substantially larger than the mask. Only |
| 575 | // clear the part that we care about. |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 576 | dc->clear(&maskSpaceIBounds, |
| 577 | GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000, |
| 578 | true); |
skia.committer@gmail.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 579 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 580 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 581 | // space. |
| 582 | const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY); |
| 583 | |
| 584 | // 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] | 585 | // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first |
| 586 | // pass must not set values outside of this bounds or stencil values outside the rect won't be |
| 587 | // cleared. |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 588 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 589 | // walk through each clip element and perform its set op |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 590 | for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 591 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 592 | SkRegion::Op op = element->getOp(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 593 | bool invert = element->isInverseFilled(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 594 | if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 595 | #ifdef SK_DEBUG |
| 596 | GrPathRenderer* pr = GetPathRenderer(context, |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 597 | texture, translate, element); |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 598 | if (Element::kRect_Type != element->getType() && !pr) { |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 599 | // UseSWOnlyPath should now filter out all cases where gpu-side mask merging would |
| 600 | // be performed (i.e., pr would be NULL for a non-rect path). |
| 601 | // See https://bug.skia.org/4519 for rationale and details. |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 602 | SkASSERT(0); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 603 | } |
| 604 | #endif |
| 605 | |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 606 | GrClip clip(maskSpaceIBounds); |
| 607 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 608 | // draw directly into the result with the stencil set to make the pixels affected |
| 609 | // by the clip shape be non-zero. |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 610 | static constexpr GrUserStencilSettings kStencilInElement( |
| 611 | GrUserStencilSettings::StaticInit< |
| 612 | 0xffff, |
| 613 | GrUserStencilTest::kAlways, |
| 614 | 0xffff, |
| 615 | GrUserStencilOp::kReplace, |
| 616 | GrUserStencilOp::kReplace, |
| 617 | 0xffff>() |
| 618 | ); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 619 | if (!stencil_element(dc.get(), clip, &kStencilInElement, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 620 | translate, element)) { |
| 621 | texture->resourcePriv().removeUniqueKey(); |
| 622 | return nullptr; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 623 | } |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 624 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 625 | // Draw to the exterior pixels (those with a zero stencil value). |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 626 | static constexpr GrUserStencilSettings kDrawOutsideElement( |
| 627 | GrUserStencilSettings::StaticInit< |
| 628 | 0x0000, |
| 629 | GrUserStencilTest::kEqual, |
| 630 | 0xffff, |
| 631 | GrUserStencilOp::kZero, |
| 632 | GrUserStencilOp::kZero, |
| 633 | 0xffff>() |
| 634 | ); |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 635 | if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 636 | op, !invert, false, |
| 637 | translate, |
| 638 | SkRect::Make(clipSpaceIBounds))) { |
| 639 | texture->resourcePriv().removeUniqueKey(); |
| 640 | return nullptr; |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 641 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 642 | } else { |
robertphillips | 8b8f36f | 2016-03-02 08:53:12 -0800 | [diff] [blame] | 643 | // all the remaining ops can just be directly draw into the accumulation buffer |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 644 | GrPaint paint; |
| 645 | paint.setAntiAlias(element->isAA()); |
| 646 | paint.setCoverageSetOpXPFactory(op, false); |
| 647 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 648 | draw_element(dc.get(), GrClip::WideOpen(), paint, translate, element); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 649 | } |
| 650 | } |
| 651 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 652 | return texture.release(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 656 | // 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] | 657 | // (as opposed to canvas) coordinates |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 658 | bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt, |
| 659 | int32_t elementsGenID, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 660 | GrReducedClip::InitialState initialState, |
| 661 | const GrReducedClip::ElementList& elements, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 662 | const SkIRect& clipSpaceIBounds, |
| 663 | const SkIPoint& clipSpaceToStencilOffset) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 664 | SkASSERT(rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 665 | |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 666 | GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 667 | if (nullptr == stencilAttachment) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 668 | return false; |
| 669 | } |
| 670 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 671 | if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) { |
| 672 | stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 673 | // Set the matrix so that rendered clip elements are transformed from clip to stencil space. |
| 674 | SkVector translate = { |
| 675 | SkIntToScalar(clipSpaceToStencilOffset.fX), |
| 676 | SkIntToScalar(clipSpaceToStencilOffset.fY) |
| 677 | }; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 678 | SkMatrix viewMatrix; |
| 679 | viewMatrix.setTranslate(translate); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 680 | |
bsalomon@google.com | 9f13174 | 2012-12-13 20:43:56 +0000 | [diff] [blame] | 681 | // We set the current clip to the bounds so that our recursive draws are scissored to them. |
| 682 | SkIRect stencilSpaceIBounds(clipSpaceIBounds); |
| 683 | stencilSpaceIBounds.offset(clipSpaceToStencilOffset); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 684 | GrClip clip(stencilSpaceIBounds); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 685 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 686 | fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds, |
| 687 | GrReducedClip::kAllIn_InitialState == initialState, rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 688 | |
| 689 | // walk through each clip element and perform its set op |
| 690 | // with the existing clip. |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 691 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 692 | const Element* element = iter.get(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 693 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 694 | GrPipelineBuilder pipelineBuilder; |
| 695 | pipelineBuilder.setRenderTarget(rt); |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 696 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 697 | pipelineBuilder.setDisableColorXPFactory(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 698 | |
| 699 | // 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] | 700 | if (rt->isStencilBufferMultisampled()) { |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 701 | pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA()); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 702 | } |
| 703 | |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 704 | bool fillInverted = false; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 705 | // enabled at bottom of loop |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 706 | fClipMode = kIgnoreClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 707 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 708 | // This will be used to determine whether the clip shape can be rendered into the |
| 709 | // stencil with arbitrary stencil settings. |
| 710 | GrPathRenderer::StencilSupport stencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 711 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 712 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 713 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 714 | GrPathRenderer* pr = nullptr; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 715 | SkPath clipPath; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 716 | if (Element::kRect_Type == element->getType()) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 717 | stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 718 | fillInverted = false; |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 719 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 720 | element->asPath(&clipPath); |
| 721 | fillInverted = clipPath.isInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 722 | if (fillInverted) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 723 | clipPath.toggleInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 724 | } |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 725 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 726 | SkASSERT(!pipelineBuilder.hasUserStencilSettings()); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 727 | |
| 728 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 729 | canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps(); |
| 730 | canDrawArgs.fViewMatrix = &viewMatrix; |
| 731 | canDrawArgs.fPath = &clipPath; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 732 | canDrawArgs.fStyle = &GrStyle::SimpleFill(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 733 | canDrawArgs.fAntiAlias = false; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 734 | canDrawArgs.fHasUserStencilSettings = pipelineBuilder.hasUserStencilSettings(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 735 | canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled(); |
| 736 | |
| 737 | pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false, |
| 738 | GrPathRendererChain::kStencilOnly_DrawType, |
| 739 | &stencilSupport); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 740 | if (nullptr == pr) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 741 | return false; |
| 742 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 743 | } |
| 744 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 745 | bool canRenderDirectToStencil = |
| 746 | GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 747 | bool drawDirectToClip; // Given the renderer, the element, |
| 748 | // fill rule, and set operation should |
| 749 | // we render the element directly to |
| 750 | // stencil bit used for clipping. |
| 751 | GrUserStencilSettings const* const* stencilPasses = |
| 752 | GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted, |
| 753 | &drawDirectToClip); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 754 | |
| 755 | // draw the element to the client stencil bits if necessary |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 756 | if (!drawDirectToClip) { |
| 757 | static constexpr GrUserStencilSettings kDrawToStencil( |
| 758 | GrUserStencilSettings::StaticInit< |
| 759 | 0x0000, |
| 760 | GrUserStencilTest::kAlways, |
| 761 | 0xffff, |
| 762 | GrUserStencilOp::kIncMaybeClamp, |
| 763 | GrUserStencilOp::kIncMaybeClamp, |
| 764 | 0xffff>() |
| 765 | ); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 766 | if (Element::kRect_Type == element->getType()) { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 767 | pipelineBuilder.setUserStencil(&kDrawToStencil); |
joshualitt | 73bb456 | 2015-03-25 07:16:21 -0700 | [diff] [blame] | 768 | |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 769 | draw_non_aa_rect(fDrawTarget, pipelineBuilder, clip, GrColor_WHITE, viewMatrix, |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 770 | element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 771 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 772 | if (!clipPath.isEmpty()) { |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 773 | if (canRenderDirectToStencil) { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 774 | pipelineBuilder.setUserStencil(&kDrawToStencil); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 775 | |
| 776 | GrPathRenderer::DrawPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 777 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 778 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 779 | args.fPipelineBuilder = &pipelineBuilder; |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 780 | args.fClip = &clip; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 781 | args.fColor = GrColor_WHITE; |
| 782 | args.fViewMatrix = &viewMatrix; |
| 783 | args.fPath = &clipPath; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 784 | args.fStyle = &GrStyle::SimpleFill(); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 785 | args.fAntiAlias = false; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 786 | args.fGammaCorrect = false; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 787 | pr->drawPath(args); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 788 | } else { |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 789 | GrPathRenderer::StencilPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 790 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 791 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 792 | args.fPipelineBuilder = &pipelineBuilder; |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 793 | args.fClip = &clip; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 794 | args.fViewMatrix = &viewMatrix; |
| 795 | args.fPath = &clipPath; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 796 | pr->stencilPath(args); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 797 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | // now we modify the clip bit by rendering either the clip |
| 803 | // element directly or a bounding rect of the entire clip. |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 804 | fClipMode = kModifyClip_StencilClipMode; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 805 | for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) { |
| 806 | pipelineBuilder.setUserStencil(*pass); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 807 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame] | 808 | if (drawDirectToClip) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 809 | if (Element::kRect_Type == element->getType()) { |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 810 | draw_non_aa_rect(fDrawTarget, pipelineBuilder, clip, GrColor_WHITE, |
| 811 | viewMatrix, element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 812 | } else { |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 813 | GrPathRenderer::DrawPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 814 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 815 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 816 | args.fPipelineBuilder = &pipelineBuilder; |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 817 | args.fClip = &clip; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 818 | args.fColor = GrColor_WHITE; |
| 819 | args.fViewMatrix = &viewMatrix; |
| 820 | args.fPath = &clipPath; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 821 | args.fStyle = &GrStyle::SimpleFill(); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 822 | args.fAntiAlias = false; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 823 | args.fGammaCorrect = false; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 824 | pr->drawPath(args); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 825 | } |
| 826 | } else { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 827 | // The view matrix is setup to do clip space -> stencil space translation, so |
| 828 | // draw rect in clip space. |
cdalton | 862cff3 | 2016-05-12 15:09:48 -0700 | [diff] [blame] | 829 | draw_non_aa_rect(fDrawTarget, pipelineBuilder, clip, GrColor_WHITE, viewMatrix, |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 830 | SkRect::Make(clipSpaceIBounds)); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 834 | } |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 835 | fClipMode = kRespectClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 836 | return true; |
| 837 | } |
| 838 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 839 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 840 | GrTexture* GrClipMaskManager::CreateSoftwareClipMask(GrContext* context, |
| 841 | int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 842 | GrReducedClip::InitialState initialState, |
| 843 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 844 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 845 | const SkIRect& clipSpaceIBounds) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 846 | GrUniqueKey key; |
| 847 | GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 848 | GrResourceProvider* resourceProvider = context->resourceProvider(); |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 849 | if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) { |
| 850 | return texture; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 851 | } |
| 852 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 853 | // The mask texture may be larger than necessary. We round out the clip space bounds and pin |
| 854 | // the top left corner of the resulting rect to the top left of the texture. |
| 855 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 856 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 857 | GrSWMaskHelper helper(context); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 858 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 859 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 860 | // space. |
| 861 | SkMatrix translate; |
| 862 | translate.setTranslate(clipToMaskOffset); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 863 | |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame^] | 864 | helper.init(maskSpaceIBounds, &translate); |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 865 | helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 866 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 867 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 868 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 869 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 870 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 871 | if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
| 872 | // Intersect and reverse difference require modifying pixels outside of the geometry |
| 873 | // that is being "drawn". In both cases we erase all the pixels outside of the geometry |
| 874 | // but leave the pixels inside the geometry alone. For reverse difference we invert all |
| 875 | // the pixels before clearing the ones outside the geometry. |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 876 | if (SkRegion::kReverseDifference_Op == op) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 877 | SkRect temp = SkRect::Make(clipSpaceIBounds); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 878 | // invert the entire scene |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame^] | 879 | helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 880 | } |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 881 | SkPath clipPath; |
| 882 | element->asPath(&clipPath); |
| 883 | clipPath.toggleInverseFillType(); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame^] | 884 | helper.drawPath(clipPath, GrStyle::SimpleFill(), SkRegion::kReplace_Op, |
| 885 | element->isAA(), 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 886 | continue; |
| 887 | } |
| 888 | |
| 889 | // The other ops (union, xor, diff) only affect pixels inside |
| 890 | // the geometry so they can just be drawn normally |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 891 | if (Element::kRect_Type == element->getType()) { |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame^] | 892 | helper.drawRect(element->getRect(), op, element->isAA(), 0xFF); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 893 | } else { |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 894 | SkPath path; |
| 895 | element->asPath(&path); |
robertphillips | 9837740 | 2016-05-13 05:47:23 -0700 | [diff] [blame^] | 896 | helper.drawPath(path, GrStyle::SimpleFill(), op, element->isAA(), 0xFF); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 900 | // Allocate clip mask texture |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 901 | GrSurfaceDesc desc; |
| 902 | desc.fWidth = clipSpaceIBounds.width(); |
| 903 | desc.fHeight = clipSpaceIBounds.height(); |
| 904 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 905 | |
| 906 | GrTexture* result = context->resourceProvider()->createApproxTexture(desc, 0); |
| 907 | if (!result) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 908 | return nullptr; |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 909 | } |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 910 | result->resourcePriv().setUniqueKey(key); |
| 911 | |
robertphillips@google.com | d92cf2e | 2013-07-19 18:13:02 +0000 | [diff] [blame] | 912 | helper.toTexture(result); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 913 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 914 | return result; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 915 | } |