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