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