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