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