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