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