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