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