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