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