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