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, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 65 | bool hasUserStencilSettings, |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 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 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 88 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 89 | GrPathRendererChain::DrawType type; |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 90 | |
robertphillips | 423e337 | 2015-10-27 09:23:38 -0700 | [diff] [blame] | 91 | if (needsStencil) { |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 92 | type = element->isAA() |
| 93 | ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType |
| 94 | : GrPathRendererChain::kStencilAndColor_DrawType; |
| 95 | } else { |
| 96 | type = element->isAA() |
| 97 | ? GrPathRendererChain::kColorAntiAlias_DrawType |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 98 | : GrPathRendererChain::kColor_DrawType; |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 99 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 100 | |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 101 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 102 | canDrawArgs.fShaderCaps = context->caps()->shaderCaps(); |
| 103 | canDrawArgs.fViewMatrix = &viewMatrix; |
| 104 | canDrawArgs.fPath = &path; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 105 | canDrawArgs.fStyle = &GrStyle::SimpleFill(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 106 | canDrawArgs.fAntiAlias = element->isAA(); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 107 | canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 108 | canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled(); |
| 109 | |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 110 | // the 'false' parameter disallows use of the SW path renderer |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 111 | GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type); |
robertphillips | 3f7357f | 2015-10-27 07:17:33 -0700 | [diff] [blame] | 112 | if (prOut) { |
| 113 | *prOut = pr; |
| 114 | } |
| 115 | return SkToBool(!pr); |
| 116 | } |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 117 | } |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 118 | |
robertphillips | 423e337 | 2015-10-27 09:23:38 -0700 | [diff] [blame] | 119 | // Determines whether it is possible to draw the element to both the stencil buffer and the |
| 120 | // alpha mask simultaneously. If so and the element is a path a compatible path renderer is |
| 121 | // also returned. |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 122 | GrPathRenderer* GrClipMaskManager::GetPathRenderer(GrContext* context, |
| 123 | GrTexture* texture, |
| 124 | const SkMatrix& viewMatrix, |
| 125 | const SkClipStack::Element* element) { |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 126 | GrPathRenderer* pr; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 127 | constexpr bool kNeedsStencil = true; |
| 128 | constexpr bool kHasUserStencilSettings = false; |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 129 | PathNeedsSWRenderer(context, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 130 | kHasUserStencilSettings, |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 131 | texture->asRenderTarget(), |
| 132 | viewMatrix, |
| 133 | element, |
| 134 | &pr, |
| 135 | kNeedsStencil); |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 136 | return pr; |
robertphillips | 423e337 | 2015-10-27 09:23:38 -0700 | [diff] [blame] | 137 | } |
| 138 | |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 139 | GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget, bool debugClipBatchToBounds) |
bsalomon | c988d2c | 2015-10-07 09:30:05 -0700 | [diff] [blame] | 140 | : fDrawTarget(drawTarget) |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 141 | , fClipMode(kIgnoreClip_StencilClipMode) |
| 142 | , fDebugClipBatchToBounds(debugClipBatchToBounds) { |
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 | |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 287 | static void add_rect_to_clip(const GrClip& clip, const SkRect& devRect, GrClip* out) { |
| 288 | switch (clip.clipType()) { |
| 289 | case GrClip::kClipStack_ClipType: { |
| 290 | SkClipStack* stack = new SkClipStack; |
| 291 | *stack = *clip.clipStack(); |
| 292 | // The stack is actually in clip space not device space. |
| 293 | SkRect clipRect = devRect; |
| 294 | SkPoint origin = { SkIntToScalar(clip.origin().fX), SkIntToScalar(clip.origin().fY) }; |
| 295 | clipRect.offset(origin); |
| 296 | SkIRect iclipRect; |
| 297 | clipRect.roundOut(&iclipRect); |
| 298 | clipRect = SkRect::Make(iclipRect); |
| 299 | stack->clipDevRect(clipRect, SkRegion::kIntersect_Op, false); |
| 300 | out->setClipStack(stack, &clip.origin()); |
| 301 | break; |
| 302 | } |
| 303 | case GrClip::kWideOpen_ClipType: |
| 304 | *out = GrClip(devRect); |
| 305 | break; |
| 306 | case GrClip::kIRect_ClipType: { |
| 307 | SkIRect intersect; |
| 308 | devRect.roundOut(&intersect); |
| 309 | if (intersect.intersect(clip.irect())) { |
| 310 | *out = GrClip(intersect); |
| 311 | } else { |
| 312 | *out = clip; |
| 313 | } |
| 314 | break; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 319 | bool GrClipMaskManager::setupScissorClip(const GrPipelineBuilder& pipelineBuilder, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 320 | const SkIRect& clipScissor, |
| 321 | const SkRect* devBounds, |
| 322 | GrAppliedClip* out) { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 323 | SkASSERT(kModifyClip_StencilClipMode != fClipMode); // TODO: Remove fClipMode. |
| 324 | fClipMode = kIgnoreClip_StencilClipMode; |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 325 | |
| 326 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
| 327 | |
| 328 | SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height()); |
| 329 | SkIRect devBoundsScissor; |
| 330 | const SkIRect* scissor = &clipScissor; |
| 331 | bool doDevBoundsClip = fDebugClipBatchToBounds && devBounds; |
| 332 | if (doDevBoundsClip) { |
| 333 | devBounds->roundOut(&devBoundsScissor); |
| 334 | if (devBoundsScissor.intersect(clipScissor)) { |
| 335 | scissor = &devBoundsScissor; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | if (scissor->contains(clipSpaceRTIBounds)) { |
| 340 | // This counts as wide open |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 341 | return true; |
| 342 | } |
| 343 | |
| 344 | if (clipSpaceRTIBounds.intersect(*scissor)) { |
| 345 | out->fScissorState.set(clipSpaceRTIBounds); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 346 | return true; |
| 347 | } |
| 348 | return false; |
| 349 | } |
| 350 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 351 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 352 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 353 | // scissor, or entirely software |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 354 | bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder, |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 355 | const SkRect* devBounds, |
| 356 | GrAppliedClip* out) { |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 357 | if (kRespectClip_StencilClipMode == fClipMode) { |
| 358 | fClipMode = kIgnoreClip_StencilClipMode; |
| 359 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 360 | |
bsalomon | f045d60 | 2015-11-18 19:01:12 -0800 | [diff] [blame] | 361 | GrReducedClip::ElementList elements; |
brucedawson | 71d7f7f | 2015-02-26 13:28:53 -0800 | [diff] [blame] | 362 | int32_t genID = 0; |
| 363 | GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 364 | SkIRect clipSpaceIBounds; |
brucedawson | 71d7f7f | 2015-02-26 13:28:53 -0800 | [diff] [blame] | 365 | bool requiresAA = false; |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 366 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 367 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 368 | // GrDrawTarget should have filtered this for us |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 369 | SkASSERT(rt); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 370 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 371 | SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height()); |
bsalomon | 69cfe95 | 2015-11-30 13:27:47 -0800 | [diff] [blame] | 372 | GrClip devBoundsClip; |
| 373 | bool doDevBoundsClip = fDebugClipBatchToBounds && devBounds; |
| 374 | if (doDevBoundsClip) { |
| 375 | add_rect_to_clip(pipelineBuilder.clip(), *devBounds, &devBoundsClip); |
| 376 | } |
| 377 | const GrClip& clip = doDevBoundsClip ? devBoundsClip : pipelineBuilder.clip(); |
| 378 | |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 379 | if (clip.isWideOpen(clipSpaceRTIBounds)) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 380 | return true; |
| 381 | } |
| 382 | |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 383 | // The clip mask manager always draws with a single IRect so we special case that logic here |
| 384 | // Image filters just use a rect, so we also special case that logic |
| 385 | switch (clip.clipType()) { |
| 386 | case GrClip::kWideOpen_ClipType: |
| 387 | SkFAIL("Should have caught this with clip.isWideOpen()"); |
| 388 | return true; |
bsalomon | 9ce30e1 | 2015-03-06 08:42:34 -0800 | [diff] [blame] | 389 | case GrClip::kIRect_ClipType: { |
| 390 | SkIRect scissor = clip.irect(); |
| 391 | if (scissor.intersect(clipSpaceRTIBounds)) { |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 392 | out->fScissorState.set(scissor); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 393 | out->fHasStencilClip = kIgnoreClip_StencilClipMode != fClipMode; |
bsalomon | 9ce30e1 | 2015-03-06 08:42:34 -0800 | [diff] [blame] | 394 | return true; |
| 395 | } |
| 396 | return false; |
| 397 | } |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 398 | case GrClip::kClipStack_ClipType: { |
| 399 | clipSpaceRTIBounds.offset(clip.origin()); |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 400 | SkIRect clipSpaceReduceQueryBounds; |
bsalomon | 5b592e8 | 2016-03-09 09:55:55 -0800 | [diff] [blame] | 401 | #define DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION 0 |
Brian Salomon | 362c900 | 2015-11-30 17:02:50 -0500 | [diff] [blame] | 402 | if (devBounds && !DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION) { |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 403 | SkIRect devIBounds = devBounds->roundOut(); |
| 404 | devIBounds.offset(clip.origin()); |
| 405 | if (!clipSpaceReduceQueryBounds.intersect(clipSpaceRTIBounds, devIBounds)) { |
| 406 | return false; |
| 407 | } |
| 408 | } else { |
| 409 | clipSpaceReduceQueryBounds = clipSpaceRTIBounds; |
| 410 | } |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 411 | GrReducedClip::ReduceClipStack(*clip.clipStack(), |
bsalomon | db4758c | 2015-11-23 11:14:20 -0800 | [diff] [blame] | 412 | clipSpaceReduceQueryBounds, |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 413 | &elements, |
| 414 | &genID, |
| 415 | &initialState, |
| 416 | &clipSpaceIBounds, |
| 417 | &requiresAA); |
| 418 | if (elements.isEmpty()) { |
| 419 | if (GrReducedClip::kAllIn_InitialState == initialState) { |
| 420 | if (clipSpaceIBounds == clipSpaceRTIBounds) { |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 421 | return true; |
| 422 | } |
| 423 | } else { |
| 424 | return false; |
| 425 | } |
| 426 | } |
| 427 | } break; |
| 428 | } |
| 429 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 430 | SkASSERT(kIgnoreClip_StencilClipMode == fClipMode); // TODO: Remove fClipMode. |
| 431 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 432 | // An element count of 4 was chosen because of the common pattern in Blink of: |
| 433 | // isect RR |
| 434 | // diff RR |
| 435 | // isect convex_poly |
| 436 | // isect convex_poly |
| 437 | // when drawing rounded div borders. This could probably be tuned based on a |
| 438 | // configuration's relative costs of switching RTs to generate a mask vs |
| 439 | // longer shaders. |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 440 | if (elements.count() <= kMaxAnalyticElements) { |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 441 | SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX), |
| 442 | SkIntToScalar(-clip.origin().fY) }; |
cdalton | ede7574 | 2015-11-11 15:27:57 -0800 | [diff] [blame] | 443 | // When there are multiple samples we want to do per-sample clipping, not compute a |
| 444 | // fractional pixel coverage. |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 445 | bool disallowAnalyticAA = rt->isStencilBufferMultisampled(); |
| 446 | if (disallowAnalyticAA && !rt->numColorSamples()) { |
| 447 | // With a single color sample, any coverage info is lost from color once it hits the |
| 448 | // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe |
| 449 | // is multisampled. |
| 450 | disallowAnalyticAA = pipelineBuilder.isHWAntialias() || |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 451 | pipelineBuilder.hasUserStencilSettings(); |
cdalton | 3ccf2e7 | 2016-05-06 09:41:16 -0700 | [diff] [blame] | 452 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 453 | const GrFragmentProcessor* clipFP = nullptr; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 454 | if (elements.isEmpty() || |
bsalomon | a912dde | 2015-10-14 15:01:50 -0700 | [diff] [blame] | 455 | (requiresAA && |
| 456 | this->getAnalyticClipProcessor(elements, disallowAnalyticAA, clipToRTOffset, devBounds, |
| 457 | &clipFP))) { |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 458 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 459 | scissorSpaceIBounds.offset(-clip.origin()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 460 | if (nullptr == devBounds || |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 461 | !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) { |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 462 | out->fScissorState.set(scissorSpaceIBounds); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 463 | } |
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)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 505 | return true; |
| 506 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 507 | // 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] | 508 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 509 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 510 | // 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] | 511 | SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 512 | this->createStencilClipMask(rt, |
| 513 | genID, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 514 | initialState, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 515 | elements, |
| 516 | clipSpaceIBounds, |
| 517 | clipSpaceToStencilSpaceOffset); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 518 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 519 | // This must occur after createStencilClipMask. That function may change the scissor. Also, it |
| 520 | // only guarantees that the stencil mask is correct within the bounds it was passed, so we must |
| 521 | // use both stencil and scissor test to the bounds for the final draw. |
| 522 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
| 523 | scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset); |
bsalomon | e91f7b5 | 2015-10-27 06:42:50 -0700 | [diff] [blame] | 524 | out->fScissorState.set(scissorSpaceIBounds); |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 525 | SkASSERT(kRespectClip_StencilClipMode == fClipMode); // TODO: Remove fClipMode. |
| 526 | out->fHasStencilClip = true; |
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, |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 532 | const GrUserStencilSettings* ss, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 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 | |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 587 | dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill()); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 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. |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 678 | static constexpr GrUserStencilSettings kStencilInElement( |
| 679 | GrUserStencilSettings::StaticInit< |
| 680 | 0xffff, |
| 681 | GrUserStencilTest::kAlways, |
| 682 | 0xffff, |
| 683 | GrUserStencilOp::kReplace, |
| 684 | GrUserStencilOp::kReplace, |
| 685 | 0xffff>() |
| 686 | ); |
| 687 | if (!stencil_element(dc.get(), &maskSpaceIBounds, &kStencilInElement, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 688 | translate, element)) { |
| 689 | texture->resourcePriv().removeUniqueKey(); |
| 690 | return nullptr; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 691 | } |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 692 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 693 | // Draw to the exterior pixels (those with a zero stencil value). |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 694 | static constexpr GrUserStencilSettings kDrawOutsideElement( |
| 695 | GrUserStencilSettings::StaticInit< |
| 696 | 0x0000, |
| 697 | GrUserStencilTest::kEqual, |
| 698 | 0xffff, |
| 699 | GrUserStencilOp::kZero, |
| 700 | GrUserStencilOp::kZero, |
| 701 | 0xffff>() |
| 702 | ); |
| 703 | if (!dc->drawContextPriv().drawAndStencilRect(&maskSpaceIBounds, &kDrawOutsideElement, |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 704 | op, !invert, false, |
| 705 | translate, |
| 706 | SkRect::Make(clipSpaceIBounds))) { |
| 707 | texture->resourcePriv().removeUniqueKey(); |
| 708 | return nullptr; |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 709 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 710 | } else { |
robertphillips | 8b8f36f | 2016-03-02 08:53:12 -0800 | [diff] [blame] | 711 | // all the remaining ops can just be directly draw into the accumulation buffer |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 712 | GrPaint paint; |
| 713 | paint.setAntiAlias(element->isAA()); |
| 714 | paint.setCoverageSetOpXPFactory(op, false); |
| 715 | |
robertphillips | 6c7e325 | 2016-04-27 10:47:51 -0700 | [diff] [blame] | 716 | draw_element(dc.get(), GrClip::WideOpen(), paint, translate, element); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 717 | } |
| 718 | } |
| 719 | |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 720 | return texture.release(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 724 | // 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] | 725 | // (as opposed to canvas) coordinates |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 726 | bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt, |
| 727 | int32_t elementsGenID, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 728 | GrReducedClip::InitialState initialState, |
| 729 | const GrReducedClip::ElementList& elements, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 730 | const SkIRect& clipSpaceIBounds, |
| 731 | const SkIPoint& clipSpaceToStencilOffset) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 732 | SkASSERT(rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 733 | |
robertphillips | 544b9aa | 2015-10-28 11:01:41 -0700 | [diff] [blame] | 734 | GrStencilAttachment* stencilAttachment = this->resourceProvider()->attachStencilAttachment(rt); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 735 | if (nullptr == stencilAttachment) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 736 | return false; |
| 737 | } |
| 738 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 739 | if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) { |
| 740 | stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 741 | // Set the matrix so that rendered clip elements are transformed from clip to stencil space. |
| 742 | SkVector translate = { |
| 743 | SkIntToScalar(clipSpaceToStencilOffset.fX), |
| 744 | SkIntToScalar(clipSpaceToStencilOffset.fY) |
| 745 | }; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 746 | SkMatrix viewMatrix; |
| 747 | viewMatrix.setTranslate(translate); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 748 | |
bsalomon@google.com | 9f13174 | 2012-12-13 20:43:56 +0000 | [diff] [blame] | 749 | // We set the current clip to the bounds so that our recursive draws are scissored to them. |
| 750 | SkIRect stencilSpaceIBounds(clipSpaceIBounds); |
| 751 | stencilSpaceIBounds.offset(clipSpaceToStencilOffset); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 752 | GrClip clip(stencilSpaceIBounds); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 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 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 781 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 782 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 783 | GrPathRenderer* pr = nullptr; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 784 | SkPath clipPath; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 785 | if (Element::kRect_Type == element->getType()) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 786 | stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 787 | fillInverted = false; |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 788 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 789 | element->asPath(&clipPath); |
| 790 | fillInverted = clipPath.isInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 791 | if (fillInverted) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 792 | clipPath.toggleInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 793 | } |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 794 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 795 | SkASSERT(!pipelineBuilder.hasUserStencilSettings()); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 796 | |
| 797 | GrPathRenderer::CanDrawPathArgs canDrawArgs; |
| 798 | canDrawArgs.fShaderCaps = this->getContext()->caps()->shaderCaps(); |
| 799 | canDrawArgs.fViewMatrix = &viewMatrix; |
| 800 | canDrawArgs.fPath = &clipPath; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 801 | canDrawArgs.fStyle = &GrStyle::SimpleFill(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 802 | canDrawArgs.fAntiAlias = false; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 803 | canDrawArgs.fHasUserStencilSettings = pipelineBuilder.hasUserStencilSettings(); |
robertphillips | 6873782 | 2015-10-29 12:12:21 -0700 | [diff] [blame] | 804 | canDrawArgs.fIsStencilBufferMSAA = rt->isStencilBufferMultisampled(); |
| 805 | |
| 806 | pr = this->getContext()->drawingManager()->getPathRenderer(canDrawArgs, false, |
| 807 | GrPathRendererChain::kStencilOnly_DrawType, |
| 808 | &stencilSupport); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 809 | if (nullptr == pr) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 810 | return false; |
| 811 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 812 | } |
| 813 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 814 | bool canRenderDirectToStencil = |
| 815 | GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 816 | bool drawDirectToClip; // Given the renderer, the element, |
| 817 | // fill rule, and set operation should |
| 818 | // we render the element directly to |
| 819 | // stencil bit used for clipping. |
| 820 | GrUserStencilSettings const* const* stencilPasses = |
| 821 | GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted, |
| 822 | &drawDirectToClip); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 823 | |
| 824 | // draw the element to the client stencil bits if necessary |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 825 | if (!drawDirectToClip) { |
| 826 | static constexpr GrUserStencilSettings kDrawToStencil( |
| 827 | GrUserStencilSettings::StaticInit< |
| 828 | 0x0000, |
| 829 | GrUserStencilTest::kAlways, |
| 830 | 0xffff, |
| 831 | GrUserStencilOp::kIncMaybeClamp, |
| 832 | GrUserStencilOp::kIncMaybeClamp, |
| 833 | 0xffff>() |
| 834 | ); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 835 | if (Element::kRect_Type == element->getType()) { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 836 | pipelineBuilder.setUserStencil(&kDrawToStencil); |
joshualitt | 73bb456 | 2015-03-25 07:16:21 -0700 | [diff] [blame] | 837 | |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 838 | draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix, |
| 839 | element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 840 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 841 | if (!clipPath.isEmpty()) { |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 842 | if (canRenderDirectToStencil) { |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 843 | pipelineBuilder.setUserStencil(&kDrawToStencil); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 844 | |
| 845 | GrPathRenderer::DrawPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 846 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 847 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 848 | args.fPipelineBuilder = &pipelineBuilder; |
| 849 | args.fColor = GrColor_WHITE; |
| 850 | args.fViewMatrix = &viewMatrix; |
| 851 | args.fPath = &clipPath; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 852 | args.fStyle = &GrStyle::SimpleFill(); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 853 | args.fAntiAlias = false; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 854 | args.fGammaCorrect = false; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 855 | pr->drawPath(args); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 856 | } else { |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 857 | GrPathRenderer::StencilPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 858 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 859 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 860 | args.fPipelineBuilder = &pipelineBuilder; |
| 861 | args.fViewMatrix = &viewMatrix; |
| 862 | args.fPath = &clipPath; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 863 | pr->stencilPath(args); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 864 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | // now we modify the clip bit by rendering either the clip |
| 870 | // element directly or a bounding rect of the entire clip. |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 871 | fClipMode = kModifyClip_StencilClipMode; |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 872 | for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) { |
| 873 | pipelineBuilder.setUserStencil(*pass); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 874 | |
cdalton | 93a379b | 2016-05-11 13:58:08 -0700 | [diff] [blame^] | 875 | if (drawDirectToClip) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 876 | if (Element::kRect_Type == element->getType()) { |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 877 | draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix, |
| 878 | element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 879 | } else { |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 880 | GrPathRenderer::DrawPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 881 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 882 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 883 | args.fPipelineBuilder = &pipelineBuilder; |
| 884 | args.fColor = GrColor_WHITE; |
| 885 | args.fViewMatrix = &viewMatrix; |
| 886 | args.fPath = &clipPath; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 887 | args.fStyle = &GrStyle::SimpleFill(); |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 888 | args.fAntiAlias = false; |
brianosman | 0e3c554 | 2016-04-13 13:56:21 -0700 | [diff] [blame] | 889 | args.fGammaCorrect = false; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 890 | pr->drawPath(args); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 891 | } |
| 892 | } else { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 893 | // The view matrix is setup to do clip space -> stencil space translation, so |
| 894 | // draw rect in clip space. |
joshualitt | a8b8499 | 2016-01-13 13:35:35 -0800 | [diff] [blame] | 895 | draw_non_aa_rect(fDrawTarget, pipelineBuilder, GrColor_WHITE, viewMatrix, |
| 896 | SkRect::Make(clipSpaceIBounds)); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 900 | } |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 901 | fClipMode = kRespectClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 902 | return true; |
| 903 | } |
| 904 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 905 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 906 | GrTexture* GrClipMaskManager::CreateSoftwareClipMask(GrContext* context, |
| 907 | int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 908 | GrReducedClip::InitialState initialState, |
| 909 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 910 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 911 | const SkIRect& clipSpaceIBounds) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 912 | GrUniqueKey key; |
| 913 | GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key); |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 914 | GrResourceProvider* resourceProvider = context->resourceProvider(); |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 915 | if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) { |
| 916 | return texture; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 917 | } |
| 918 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 919 | // The mask texture may be larger than necessary. We round out the clip space bounds and pin |
| 920 | // the top left corner of the resulting rect to the top left of the texture. |
| 921 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 922 | |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 923 | GrSWMaskHelper helper(context); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 924 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 925 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 926 | // space. |
| 927 | SkMatrix translate; |
| 928 | translate.setTranslate(clipToMaskOffset); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 929 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 930 | helper.init(maskSpaceIBounds, &translate, false); |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 931 | helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 932 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 933 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 934 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 935 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 936 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 937 | if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
| 938 | // Intersect and reverse difference require modifying pixels outside of the geometry |
| 939 | // that is being "drawn". In both cases we erase all the pixels outside of the geometry |
| 940 | // but leave the pixels inside the geometry alone. For reverse difference we invert all |
| 941 | // the pixels before clearing the ones outside the geometry. |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 942 | if (SkRegion::kReverseDifference_Op == op) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 943 | SkRect temp = SkRect::Make(clipSpaceIBounds); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 944 | // invert the entire scene |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 945 | helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 946 | } |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 947 | SkPath clipPath; |
| 948 | element->asPath(&clipPath); |
| 949 | clipPath.toggleInverseFillType(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 950 | helper.draw(clipPath, GrStyle::SimpleFill(), SkRegion::kReplace_Op, element->isAA(), |
| 951 | 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 952 | continue; |
| 953 | } |
| 954 | |
| 955 | // The other ops (union, xor, diff) only affect pixels inside |
| 956 | // the geometry so they can just be drawn normally |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 957 | if (Element::kRect_Type == element->getType()) { |
| 958 | helper.draw(element->getRect(), op, element->isAA(), 0xFF); |
| 959 | } else { |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 960 | SkPath path; |
| 961 | element->asPath(&path); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 962 | helper.draw(path, GrStyle::SimpleFill(), op, element->isAA(), 0xFF); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 963 | } |
| 964 | } |
| 965 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 966 | // Allocate clip mask texture |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 967 | GrSurfaceDesc desc; |
| 968 | desc.fWidth = clipSpaceIBounds.width(); |
| 969 | desc.fHeight = clipSpaceIBounds.height(); |
| 970 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 971 | |
| 972 | GrTexture* result = context->resourceProvider()->createApproxTexture(desc, 0); |
| 973 | if (!result) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 974 | return nullptr; |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 975 | } |
robertphillips | 391395d | 2016-03-02 09:26:36 -0800 | [diff] [blame] | 976 | result->resourcePriv().setUniqueKey(key); |
| 977 | |
robertphillips@google.com | d92cf2e | 2013-07-19 18:13:02 +0000 | [diff] [blame] | 978 | helper.toTexture(result); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 979 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 980 | return result; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 981 | } |