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