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