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