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