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" |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 17 | #include "GrStencilBuffer.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(); |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 482 | // 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] | 483 | fClipTarget->drawSimpleRect(pipelineBuilder, |
| 484 | GrColor_WHITE, |
| 485 | SkMatrix::I(), |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 486 | SkRect::Make(dstBound)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 487 | } |
| 488 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 489 | GrTexture* GrClipMaskManager::createTempMask(int width, int height) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 490 | GrSurfaceDesc desc; |
bsalomon | 3f490a0 | 2014-12-18 06:20:52 -0800 | [diff] [blame] | 491 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 492 | desc.fWidth = width; |
| 493 | desc.fHeight = height; |
bsalomon | 51d1f7e | 2014-12-22 08:40:49 -0800 | [diff] [blame] | 494 | if (this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 495 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 496 | } else { |
| 497 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 498 | } |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 499 | |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 500 | return this->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 501 | } |
| 502 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 503 | //////////////////////////////////////////////////////////////////////////////// |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 504 | // Return the texture currently in the cache if it exists. Otherwise, return NULL |
| 505 | GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID, |
| 506 | const SkIRect& clipSpaceIBounds) { |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 507 | bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 508 | if (!cached) { |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 509 | return NULL; |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 510 | } |
| 511 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 512 | return fAACache.getLastMask(); |
| 513 | } |
| 514 | |
| 515 | //////////////////////////////////////////////////////////////////////////////// |
| 516 | // Allocate a texture in the texture cache. This function returns the texture |
| 517 | // allocated (or NULL on error). |
| 518 | GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID, |
| 519 | const SkIRect& clipSpaceIBounds, |
| 520 | bool willUpload) { |
| 521 | // Since we are setting up the cache we should free up the |
| 522 | // currently cached mask so it can be reused. |
| 523 | fAACache.reset(); |
| 524 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 525 | GrSurfaceDesc desc; |
| 526 | desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag; |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 527 | desc.fWidth = clipSpaceIBounds.width(); |
| 528 | desc.fHeight = clipSpaceIBounds.height(); |
| 529 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 530 | if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 531 | // We would always like A8 but it isn't supported on all platforms |
| 532 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 533 | } |
| 534 | |
| 535 | fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds); |
| 536 | return fAACache.getLastMask(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 537 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 538 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 539 | //////////////////////////////////////////////////////////////////////////////// |
| 540 | // Create a 8-bit clip mask in alpha |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 541 | GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 542 | GrReducedClip::InitialState initialState, |
| 543 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 544 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 545 | const SkIRect& clipSpaceIBounds) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 546 | SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 547 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 548 | // First, check for cached texture |
| 549 | GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 550 | if (result) { |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 551 | fCurrClipMaskType = kAlpha_ClipMaskType; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 552 | return result; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 553 | } |
| 554 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 555 | // There's no texture in the cache. Let's try to allocate it then. |
| 556 | result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 557 | if (NULL == result) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 558 | fAACache.reset(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 559 | return NULL; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 560 | } |
| 561 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 562 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 563 | // space. |
| 564 | SkMatrix translate; |
| 565 | translate.setTranslate(clipToMaskOffset); |
| 566 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 567 | // The texture may be larger than necessary, this rect represents the part of the texture |
| 568 | // we populate with a rasterization of the clip. |
| 569 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 570 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 571 | // The scratch texture that we are drawing into can be substantially larger than the mask. Only |
| 572 | // clear the part that we care about. |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 573 | fClipTarget->clear(&maskSpaceIBounds, |
| 574 | GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000, |
| 575 | true, |
| 576 | result->asRenderTarget()); |
skia.committer@gmail.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 577 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 578 | // When we use the stencil in the below loop it is important to have this clip installed. |
| 579 | // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first |
| 580 | // pass must not set values outside of this bounds or stencil values outside the rect won't be |
| 581 | // cleared. |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 582 | GrClip clip(maskSpaceIBounds); |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 583 | SkAutoTUnref<GrTexture> temp; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 584 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 585 | // walk through each clip element and perform its set op |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 586 | for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 587 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 588 | SkRegion::Op op = element->getOp(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 589 | bool invert = element->isInverseFilled(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 590 | if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 591 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 592 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 593 | pipelineBuilder.setClip(clip); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 594 | GrPathRenderer* pr = NULL; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 595 | bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, result, &pr, element); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 596 | GrTexture* dst; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 597 | // 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] | 598 | // mask buffer can be substantially larger than the actually clip stack element. We |
| 599 | // 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] | 600 | // the accumulator. |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 601 | SkIRect maskSpaceElementIBounds; |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 602 | |
| 603 | if (useTemp) { |
| 604 | if (invert) { |
| 605 | maskSpaceElementIBounds = maskSpaceIBounds; |
| 606 | } else { |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 607 | SkRect elementBounds = element->getBounds(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 608 | elementBounds.offset(clipToMaskOffset); |
| 609 | elementBounds.roundOut(&maskSpaceElementIBounds); |
| 610 | } |
| 611 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 612 | if (!temp) { |
| 613 | temp.reset(this->createTempMask(maskSpaceIBounds.fRight, |
| 614 | maskSpaceIBounds.fBottom)); |
| 615 | if (!temp) { |
| 616 | fAACache.reset(); |
| 617 | return NULL; |
| 618 | } |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 619 | } |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 620 | dst = temp; |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 621 | // clear the temp target and set blend to replace |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 622 | fClipTarget->clear(&maskSpaceElementIBounds, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 623 | invert ? 0xffffffff : 0x00000000, |
| 624 | true, |
| 625 | dst->asRenderTarget()); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 626 | set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 627 | } else { |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 628 | // draw directly into the result with the stencil set to make the pixels affected |
| 629 | // by the clip shape be non-zero. |
| 630 | dst = result; |
| 631 | GR_STATIC_CONST_SAME_STENCIL(kStencilInElement, |
| 632 | kReplace_StencilOp, |
| 633 | kReplace_StencilOp, |
| 634 | kAlways_StencilFunc, |
| 635 | 0xffff, |
| 636 | 0xffff, |
| 637 | 0xffff); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 638 | pipelineBuilder.setStencil(kStencilInElement); |
| 639 | set_coverage_drawing_xpf(op, invert, &pipelineBuilder); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 640 | } |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 641 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 642 | if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) { |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 643 | fAACache.reset(); |
| 644 | return NULL; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 645 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 646 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 647 | if (useTemp) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 648 | GrPipelineBuilder backgroundPipelineBuilder; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 649 | backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget()); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 650 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 651 | // Now draw into the accumulator using the real operation and the temp buffer as a |
| 652 | // texture |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 653 | this->mergeMask(&backgroundPipelineBuilder, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 654 | result, |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 655 | temp, |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 656 | op, |
| 657 | maskSpaceIBounds, |
| 658 | maskSpaceElementIBounds); |
| 659 | } else { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 660 | GrPipelineBuilder backgroundPipelineBuilder; |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 661 | backgroundPipelineBuilder.setRenderTarget(result->asRenderTarget()); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 662 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 663 | set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 664 | // Draw to the exterior pixels (those with a zero stencil value). |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 665 | GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement, |
| 666 | kZero_StencilOp, |
| 667 | kZero_StencilOp, |
| 668 | kEqual_StencilFunc, |
| 669 | 0xffff, |
| 670 | 0x0000, |
| 671 | 0xffff); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 672 | backgroundPipelineBuilder.setStencil(kDrawOutsideElement); |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 673 | // 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] | 674 | fClipTarget->drawSimpleRect(&backgroundPipelineBuilder, GrColor_WHITE, translate, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 675 | clipSpaceIBounds); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 676 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 677 | } else { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 678 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 679 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 680 | // all the remaining ops can just be directly draw into the accumulation buffer |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 681 | set_coverage_drawing_xpf(op, false, &pipelineBuilder); |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 682 | // 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] | 683 | this->drawElement(&pipelineBuilder, translate, result, element); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 687 | fCurrClipMaskType = kAlpha_ClipMaskType; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 688 | return result; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 692 | // 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] | 693 | // (as opposed to canvas) coordinates |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 694 | bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt, |
| 695 | int32_t elementsGenID, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 696 | GrReducedClip::InitialState initialState, |
| 697 | const GrReducedClip::ElementList& elements, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 698 | const SkIRect& clipSpaceIBounds, |
| 699 | const SkIPoint& clipSpaceToStencilOffset) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 700 | SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 701 | SkASSERT(rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 702 | |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 703 | GrStencilBuffer* stencilBuffer = rt->renderTargetPriv().attachStencilBuffer(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 704 | if (NULL == stencilBuffer) { |
| 705 | return false; |
| 706 | } |
| 707 | |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 708 | if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) { |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 709 | stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 710 | // Set the matrix so that rendered clip elements are transformed from clip to stencil space. |
| 711 | SkVector translate = { |
| 712 | SkIntToScalar(clipSpaceToStencilOffset.fX), |
| 713 | SkIntToScalar(clipSpaceToStencilOffset.fY) |
| 714 | }; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 715 | SkMatrix viewMatrix; |
| 716 | viewMatrix.setTranslate(translate); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 717 | |
bsalomon@google.com | 9f13174 | 2012-12-13 20:43:56 +0000 | [diff] [blame] | 718 | // We set the current clip to the bounds so that our recursive draws are scissored to them. |
| 719 | SkIRect stencilSpaceIBounds(clipSpaceIBounds); |
| 720 | stencilSpaceIBounds.offset(clipSpaceToStencilOffset); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 721 | GrClip clip(stencilSpaceIBounds); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 722 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 723 | int clipBit = stencilBuffer->bits(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 724 | SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers"); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 725 | clipBit = (1 << (clipBit-1)); |
| 726 | |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 727 | fClipTarget->clearStencilClip(stencilSpaceIBounds, |
| 728 | GrReducedClip::kAllIn_InitialState == initialState, |
| 729 | rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 730 | |
| 731 | // walk through each clip element and perform its set op |
| 732 | // with the existing clip. |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 733 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 734 | const Element* element = iter.get(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 735 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 736 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 737 | pipelineBuilder.setClip(clip); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 738 | pipelineBuilder.setRenderTarget(rt); |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 739 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 740 | pipelineBuilder.setDisableColorXPFactory(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 741 | |
| 742 | // if the target is MSAA then we want MSAA enabled when the clip is soft |
| 743 | if (rt->isMultisampled()) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 744 | pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_StateBit, element->isAA()); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 745 | } |
| 746 | |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 747 | bool fillInverted = false; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 748 | // enabled at bottom of loop |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 749 | fClipMode = kIgnoreClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 750 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 751 | // This will be used to determine whether the clip shape can be rendered into the |
| 752 | // stencil with arbitrary stencil settings. |
| 753 | GrPathRenderer::StencilSupport stencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 754 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 755 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 756 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 757 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 758 | GrPathRenderer* pr = NULL; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 759 | SkPath clipPath; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 760 | if (Element::kRect_Type == element->getType()) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 761 | stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 762 | fillInverted = false; |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 763 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 764 | element->asPath(&clipPath); |
| 765 | fillInverted = clipPath.isInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 766 | if (fillInverted) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 767 | clipPath.toggleInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 768 | } |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 769 | pr = this->getContext()->getPathRenderer(fClipTarget, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 770 | &pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 771 | viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 772 | clipPath, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 773 | stroke, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 774 | false, |
| 775 | GrPathRendererChain::kStencilOnly_DrawType, |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 776 | &stencilSupport); |
| 777 | if (NULL == pr) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 778 | return false; |
| 779 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 780 | } |
| 781 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 782 | int passes; |
| 783 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 784 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 785 | bool canRenderDirectToStencil = |
| 786 | GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 787 | bool canDrawDirectToClip; // Given the renderer, the element, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 788 | // fill rule, and set operation can |
| 789 | // we render the element directly to |
| 790 | // stencil bit used for clipping. |
| 791 | canDrawDirectToClip = GrStencilSettings::GetClipPasses(op, |
| 792 | canRenderDirectToStencil, |
| 793 | clipBit, |
| 794 | fillInverted, |
| 795 | &passes, |
| 796 | stencilSettings); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 797 | |
| 798 | // draw the element to the client stencil bits if necessary |
| 799 | if (!canDrawDirectToClip) { |
| 800 | GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 801 | kIncClamp_StencilOp, |
| 802 | kIncClamp_StencilOp, |
| 803 | kAlways_StencilFunc, |
| 804 | 0xffff, |
| 805 | 0x0000, |
| 806 | 0xffff); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 807 | if (Element::kRect_Type == element->getType()) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 808 | *pipelineBuilder.stencil() = gDrawToStencil; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 809 | fClipTarget->drawSimpleRect(&pipelineBuilder, |
| 810 | GrColor_WHITE, |
| 811 | viewMatrix, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 812 | element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 813 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 814 | if (!clipPath.isEmpty()) { |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 815 | GrDrawTarget::AutoGeometryPush agp(fClipTarget); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 816 | if (canRenderDirectToStencil) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 817 | *pipelineBuilder.stencil() = gDrawToStencil; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 818 | pr->drawPath(fClipTarget, &pipelineBuilder, GrColor_WHITE, |
| 819 | viewMatrix, clipPath, stroke, false); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 820 | } else { |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 821 | pr->stencilPath(fClipTarget, &pipelineBuilder, viewMatrix, |
| 822 | clipPath, stroke); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 823 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | // now we modify the clip bit by rendering either the clip |
| 829 | // element directly or a bounding rect of the entire clip. |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 830 | fClipMode = kModifyClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 831 | for (int p = 0; p < passes; ++p) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 832 | GrPipelineBuilder pipelineBuilderCopy(pipelineBuilder); |
| 833 | *pipelineBuilderCopy.stencil() = stencilSettings[p]; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 834 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 835 | if (canDrawDirectToClip) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 836 | if (Element::kRect_Type == element->getType()) { |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 837 | fClipTarget->drawSimpleRect(&pipelineBuilderCopy, |
| 838 | GrColor_WHITE, |
| 839 | viewMatrix, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 840 | element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 841 | } else { |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 842 | GrDrawTarget::AutoGeometryPush agp(fClipTarget); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 843 | pr->drawPath(fClipTarget, &pipelineBuilderCopy, GrColor_WHITE, |
| 844 | viewMatrix, clipPath, stroke, false); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 845 | } |
| 846 | } else { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 847 | // The view matrix is setup to do clip space -> stencil space translation, so |
| 848 | // draw rect in clip space. |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 849 | fClipTarget->drawSimpleRect(&pipelineBuilderCopy, |
| 850 | GrColor_WHITE, |
| 851 | viewMatrix, |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 852 | SkRect::Make(clipSpaceIBounds)); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 856 | } |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 857 | // 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] | 858 | SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 859 | fCurrClipMaskType = kStencil_ClipMaskType; |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 860 | fClipMode = kRespectClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 861 | return true; |
| 862 | } |
| 863 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 864 | // mapping of clip-respecting stencil funcs to normal stencil funcs |
| 865 | // mapping depends on whether stencil-clipping is in effect. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 866 | static const GrStencilFunc |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 867 | gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = { |
| 868 | {// Stencil-Clipping is DISABLED, we are effectively always inside the clip |
| 869 | // In the Clip Funcs |
| 870 | kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 871 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 872 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 873 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 874 | // Special in the clip func that forces user's ref to be 0. |
| 875 | kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 876 | // make ref 0 and do normal nequal. |
| 877 | }, |
| 878 | {// Stencil-Clipping is ENABLED |
| 879 | // In the Clip Funcs |
| 880 | kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 881 | // eq stencil clip bit, mask |
| 882 | // out user bits. |
| 883 | |
| 884 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 885 | // add stencil bit to mask and ref |
| 886 | |
| 887 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 888 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 889 | // for both of these we can add |
| 890 | // the clip bit to the mask and |
| 891 | // ref and compare as normal |
| 892 | // Special in the clip func that forces user's ref to be 0. |
| 893 | kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 894 | // make ref have only the clip bit set |
| 895 | // and make comparison be less |
| 896 | // 10..0 < 1..user_bits.. |
| 897 | } |
| 898 | }; |
| 899 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 900 | namespace { |
| 901 | // Sets the settings to clip against the stencil buffer clip while ignoring the |
| 902 | // client bits. |
| 903 | const GrStencilSettings& basic_apply_stencil_clip_settings() { |
| 904 | // stencil settings to use when clip is in stencil |
| 905 | GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings, |
| 906 | kKeep_StencilOp, |
| 907 | kKeep_StencilOp, |
| 908 | kAlwaysIfInClip_StencilFunc, |
| 909 | 0x0000, |
| 910 | 0x0000, |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 911 | 0x0000); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 912 | return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings); |
| 913 | } |
| 914 | } |
| 915 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 916 | void GrClipMaskManager::setPipelineBuilderStencil(GrPipelineBuilder* pipelineBuilder, |
| 917 | GrPipelineBuilder::AutoRestoreStencil* ars) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 918 | // We make two copies of the StencilSettings here (except in the early |
| 919 | // exit scenario. One copy from draw state to the stack var. Then another |
| 920 | // from the stack var to the gpu. We could make this class hold a ptr to |
| 921 | // GrGpu's fStencilSettings and eliminate the stack copy here. |
| 922 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 923 | // use stencil for clipping if clipping is enabled and the clip |
| 924 | // has been written into the stencil. |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 925 | GrStencilSettings settings; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 926 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 927 | // The GrGpu client may not be using the stencil buffer but we may need to |
| 928 | // enable it in order to respect a stencil clip. |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 929 | if (pipelineBuilder->getStencil().isDisabled()) { |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 930 | if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 931 | settings = basic_apply_stencil_clip_settings(); |
| 932 | } else { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 933 | return; |
| 934 | } |
| 935 | } else { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 936 | settings = pipelineBuilder->getStencil(); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 937 | } |
| 938 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 939 | int stencilBits = 0; |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 940 | GrRenderTarget* rt = pipelineBuilder->getRenderTarget(); |
| 941 | GrStencilBuffer* stencilBuffer = rt->renderTargetPriv().attachStencilBuffer(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 942 | if (stencilBuffer) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 943 | stencilBits = stencilBuffer->bits(); |
| 944 | } |
| 945 | |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 946 | SkASSERT(fClipTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp()); |
| 947 | SkASSERT(fClipTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided()); |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 948 | this->adjustStencilParams(&settings, fClipMode, stencilBits); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 949 | ars->set(pipelineBuilder); |
| 950 | pipelineBuilder->setStencil(settings); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings, |
| 954 | StencilClipMode mode, |
| 955 | int stencilBitCnt) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 956 | SkASSERT(stencilBitCnt > 0); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 957 | |
| 958 | if (kModifyClip_StencilClipMode == mode) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 959 | // We assume that this clip manager itself is drawing to the GrGpu and |
| 960 | // has already setup the correct values. |
| 961 | return; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 962 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 963 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 964 | unsigned int clipBit = (1 << (stencilBitCnt - 1)); |
| 965 | unsigned int userBits = clipBit - 1; |
| 966 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 967 | GrStencilSettings::Face face = GrStencilSettings::kFront_Face; |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 968 | bool twoSided = fClipTarget->caps()->twoSidedStencilSupport(); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 969 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 970 | bool finished = false; |
| 971 | while (!finished) { |
| 972 | GrStencilFunc func = settings->func(face); |
| 973 | uint16_t writeMask = settings->writeMask(face); |
| 974 | uint16_t funcMask = settings->funcMask(face); |
| 975 | uint16_t funcRef = settings->funcRef(face); |
| 976 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 977 | SkASSERT((unsigned) func < kStencilFuncCount); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 978 | |
| 979 | writeMask &= userBits; |
| 980 | |
| 981 | if (func >= kBasicStencilFuncCount) { |
| 982 | int respectClip = kRespectClip_StencilClipMode == mode; |
| 983 | if (respectClip) { |
| 984 | // The GrGpu class should have checked this |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 985 | SkASSERT(this->isClipInStencil()); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 986 | switch (func) { |
| 987 | case kAlwaysIfInClip_StencilFunc: |
| 988 | funcMask = clipBit; |
| 989 | funcRef = clipBit; |
| 990 | break; |
| 991 | case kEqualIfInClip_StencilFunc: |
| 992 | case kLessIfInClip_StencilFunc: |
| 993 | case kLEqualIfInClip_StencilFunc: |
| 994 | funcMask = (funcMask & userBits) | clipBit; |
| 995 | funcRef = (funcRef & userBits) | clipBit; |
| 996 | break; |
| 997 | case kNonZeroIfInClip_StencilFunc: |
| 998 | funcMask = (funcMask & userBits) | clipBit; |
| 999 | funcRef = clipBit; |
| 1000 | break; |
| 1001 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 1002 | SkFAIL("Unknown stencil func"); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1003 | } |
| 1004 | } else { |
| 1005 | funcMask &= userBits; |
| 1006 | funcRef &= userBits; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1007 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1008 | const GrStencilFunc* table = |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1009 | gSpecialToBasicStencilFunc[respectClip]; |
| 1010 | func = table[func - kBasicStencilFuncCount]; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1011 | SkASSERT(func >= 0 && func < kBasicStencilFuncCount); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1012 | } else { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1013 | funcMask &= userBits; |
| 1014 | funcRef &= userBits; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1015 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1016 | |
| 1017 | settings->setFunc(face, func); |
| 1018 | settings->setWriteMask(face, writeMask); |
| 1019 | settings->setFuncMask(face, funcMask); |
| 1020 | settings->setFuncRef(face, funcRef); |
| 1021 | |
| 1022 | if (GrStencilSettings::kFront_Face == face) { |
| 1023 | face = GrStencilSettings::kBack_Face; |
| 1024 | finished = !twoSided; |
| 1025 | } else { |
| 1026 | finished = true; |
| 1027 | } |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1028 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1029 | if (!twoSided) { |
| 1030 | settings->copyFrontSettingsToBack(); |
| 1031 | } |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1035 | GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1036 | GrReducedClip::InitialState initialState, |
| 1037 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1038 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1039 | const SkIRect& clipSpaceIBounds) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1040 | SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1041 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 1042 | GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1043 | if (result) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1044 | return result; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1045 | } |
| 1046 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1047 | // The mask texture may be larger than necessary. We round out the clip space bounds and pin |
| 1048 | // the top left corner of the resulting rect to the top left of the texture. |
| 1049 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 1050 | |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 1051 | GrSWMaskHelper helper(this->getContext()); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1052 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1053 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 1054 | // space. |
| 1055 | SkMatrix translate; |
| 1056 | translate.setTranslate(clipToMaskOffset); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1057 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1058 | helper.init(maskSpaceIBounds, &translate, false); |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 1059 | helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00); |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 1060 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 1061 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 1062 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1063 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1064 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1065 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1066 | if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
| 1067 | // Intersect and reverse difference require modifying pixels outside of the geometry |
| 1068 | // that is being "drawn". In both cases we erase all the pixels outside of the geometry |
| 1069 | // but leave the pixels inside the geometry alone. For reverse difference we invert all |
| 1070 | // the pixels before clearing the ones outside the geometry. |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1071 | if (SkRegion::kReverseDifference_Op == op) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 1072 | SkRect temp = SkRect::Make(clipSpaceIBounds); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1073 | // invert the entire scene |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 1074 | helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1075 | } |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 1076 | SkPath clipPath; |
| 1077 | element->asPath(&clipPath); |
| 1078 | clipPath.toggleInverseFillType(); |
| 1079 | helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1080 | continue; |
| 1081 | } |
| 1082 | |
| 1083 | // The other ops (union, xor, diff) only affect pixels inside |
| 1084 | // the geometry so they can just be drawn normally |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1085 | if (Element::kRect_Type == element->getType()) { |
| 1086 | helper.draw(element->getRect(), op, element->isAA(), 0xFF); |
| 1087 | } else { |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 1088 | SkPath path; |
| 1089 | element->asPath(&path); |
| 1090 | helper.draw(path, stroke, op, element->isAA(), 0xFF); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 1094 | // Allocate clip mask texture |
| 1095 | result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true); |
| 1096 | if (NULL == result) { |
| 1097 | fAACache.reset(); |
| 1098 | return NULL; |
| 1099 | } |
robertphillips@google.com | d92cf2e | 2013-07-19 18:13:02 +0000 | [diff] [blame] | 1100 | helper.toTexture(result); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1101 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 1102 | fCurrClipMaskType = kAlpha_ClipMaskType; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1103 | return result; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1106 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 1107 | void GrClipMaskManager::purgeResources() { |
| 1108 | fAACache.purgeResources(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1109 | } |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 1110 | |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 1111 | void GrClipMaskManager::setClipTarget(GrClipTarget* clipTarget) { |
| 1112 | fClipTarget = clipTarget; |
| 1113 | fAACache.setContext(clipTarget->getContext()); |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 1114 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 1115 | |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1116 | void GrClipMaskManager::adjustPathStencilParams(const GrStencilBuffer* stencilBuffer, |
| 1117 | GrStencilSettings* settings) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1118 | if (stencilBuffer) { |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1119 | int stencilBits = stencilBuffer->bits(); |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 1120 | this->adjustStencilParams(settings, fClipMode, stencilBits); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 1121 | } |
| 1122 | } |