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