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