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