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