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