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