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" |
joshualitt | 3a0cfeb | 2014-10-27 07:38:01 -0700 | [diff] [blame] | 20 | #include "SkRasterClip.h" |
| 21 | #include "SkStrokeRec.h" |
| 22 | #include "SkTLazy.h" |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 23 | #include "effects/GrTextureDomain.h" |
| 24 | #include "effects/GrConvexPolyEffect.h" |
| 25 | #include "effects/GrRRectEffect.h" |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +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 | |
| 31 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +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 |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 35 | void setup_drawstate_aaclip(GrGpu* gpu, |
| 36 | GrTexture* result, |
| 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. |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 52 | drawState->addCoverageProcessor( |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 53 | GrTextureDomainEffect::Create(result, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 54 | mat, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 55 | GrTextureDomain::MakeTexelDomain(result, domainTexels), |
| 56 | GrTextureDomain::kDecal_Mode, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 57 | GrTextureParams::kNone_FilterMode, |
bsalomon@google.com | 77af680 | 2013-10-02 13:04:56 +0000 | [diff] [blame] | 58 | kPosition_GrCoordSet))->unref(); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 59 | } |
| 60 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 61 | bool path_needs_SW_renderer(GrContext* context, |
| 62 | GrGpu* gpu, |
| 63 | const SkPath& origPath, |
| 64 | const SkStrokeRec& stroke, |
| 65 | bool doAA) { |
| 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 | } |
| 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 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 76 | return NULL == context->getPathRenderer(*path, stroke, gpu, false, type); |
| 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 | */ |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 86 | bool GrClipMaskManager::useSWOnlyPath(const GrReducedClip::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 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 93 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 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 |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 96 | // Skip rrects once we're drawing them directly. |
| 97 | if (Element::kRect_Type != element->getType()) { |
| 98 | SkPath path; |
| 99 | element->asPath(&path); |
| 100 | if (path_needs_SW_renderer(this->getContext(), fGpu, path, stroke, element->isAA())) { |
| 101 | return true; |
| 102 | } |
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 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 108 | bool GrClipMaskManager::installClipEffects(const GrReducedClip::ElementList& elements, |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 109 | GrDrawState::AutoRestoreEffects* are, |
| 110 | const SkVector& clipToRTOffset, |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 111 | const SkRect* drawBounds) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 112 | |
| 113 | GrDrawState* drawState = fGpu->drawState(); |
| 114 | SkRect boundsInClipSpace; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 115 | if (drawBounds) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 116 | boundsInClipSpace = *drawBounds; |
| 117 | boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY); |
| 118 | } |
| 119 | |
| 120 | are->set(drawState); |
| 121 | GrRenderTarget* rt = drawState->getRenderTarget(); |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 122 | GrReducedClip::ElementList::Iter iter(elements); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 123 | |
| 124 | bool setARE = false; |
| 125 | bool failed = false; |
| 126 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 127 | while (iter.get()) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 128 | SkRegion::Op op = iter.get()->getOp(); |
| 129 | bool invert; |
| 130 | bool skip = false; |
| 131 | switch (op) { |
| 132 | case SkRegion::kReplace_Op: |
| 133 | SkASSERT(iter.get() == elements.head()); |
| 134 | // Fallthrough, handled same as intersect. |
| 135 | case SkRegion::kIntersect_Op: |
| 136 | invert = false; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 137 | if (drawBounds && iter.get()->contains(boundsInClipSpace)) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 138 | skip = true; |
| 139 | } |
| 140 | break; |
| 141 | case SkRegion::kDifference_Op: |
| 142 | invert = true; |
| 143 | // We don't currently have a cheap test for whether a rect is fully outside an |
| 144 | // element's primitive, so don't attempt to set skip. |
| 145 | break; |
| 146 | default: |
| 147 | failed = true; |
| 148 | break; |
| 149 | } |
| 150 | if (failed) { |
| 151 | break; |
| 152 | } |
| 153 | |
| 154 | if (!skip) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 155 | GrPrimitiveEdgeType edgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 156 | if (GR_AA_CLIP && iter.get()->isAA()) { |
| 157 | if (rt->isMultisampled()) { |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 158 | // Coverage based AA clips don't place nicely with MSAA. |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 159 | failed = true; |
| 160 | break; |
| 161 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 162 | edgeType = |
| 163 | invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 164 | } else { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 165 | edgeType = |
| 166 | invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 167 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 168 | SkAutoTUnref<GrFragmentProcessor> fp; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 169 | switch (iter.get()->getType()) { |
| 170 | case SkClipStack::Element::kPath_Type: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 171 | fp.reset(GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(), |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 172 | &clipToRTOffset)); |
| 173 | break; |
| 174 | case SkClipStack::Element::kRRect_Type: { |
| 175 | SkRRect rrect = iter.get()->getRRect(); |
| 176 | rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 177 | fp.reset(GrRRectEffect::Create(edgeType, rrect)); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | case SkClipStack::Element::kRect_Type: { |
| 181 | SkRect rect = iter.get()->getRect(); |
| 182 | rect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 183 | fp.reset(GrConvexPolyEffect::Create(edgeType, rect)); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | default: |
| 187 | break; |
| 188 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 189 | if (fp) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 190 | if (!setARE) { |
| 191 | are->set(fGpu->drawState()); |
| 192 | setARE = true; |
| 193 | } |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 194 | fGpu->drawState()->addCoverageProcessor(fp); |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 195 | } else { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 196 | failed = true; |
| 197 | break; |
| 198 | } |
| 199 | } |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 200 | iter.next(); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | if (failed) { |
| 204 | are->set(NULL); |
| 205 | } |
| 206 | |
| 207 | return !failed; |
| 208 | } |
| 209 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 210 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 211 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 212 | // scissor, or entirely software |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 213 | bool GrClipMaskManager::setupClipping(const GrClipData* clipDataIn, |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 214 | const SkRect* devBounds, |
commit-bot@chromium.org | 3ae0e6c | 2014-02-11 18:24:25 +0000 | [diff] [blame] | 215 | GrDrawState::AutoRestoreEffects* are, |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 216 | GrDrawState::AutoRestoreStencil* ars, |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame^] | 217 | ScissorState* scissorState) { |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 218 | fCurrClipMaskType = kNone_ClipMaskType; |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 219 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 220 | GrReducedClip::ElementList elements(16); |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 221 | int32_t genID; |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 222 | GrReducedClip::InitialState initialState; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 223 | SkIRect clipSpaceIBounds; |
| 224 | bool requiresAA; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 225 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 226 | GrDrawState* drawState = fGpu->drawState(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 227 | |
| 228 | const GrRenderTarget* rt = drawState->getRenderTarget(); |
| 229 | // GrDrawTarget should have filtered this for us |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 230 | SkASSERT(rt); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 231 | |
| 232 | bool ignoreClip = !drawState->isClipState() || clipDataIn->fClipStack->isWideOpen(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 233 | if (!ignoreClip) { |
| 234 | SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height()); |
| 235 | clipSpaceRTIBounds.offset(clipDataIn->fOrigin); |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 236 | GrReducedClip::ReduceClipStack(*clipDataIn->fClipStack, |
| 237 | clipSpaceRTIBounds, |
| 238 | &elements, |
| 239 | &genID, |
| 240 | &initialState, |
| 241 | &clipSpaceIBounds, |
| 242 | &requiresAA); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 243 | if (elements.isEmpty()) { |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 244 | if (GrReducedClip::kAllIn_InitialState == initialState) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 245 | ignoreClip = clipSpaceIBounds == clipSpaceRTIBounds; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 246 | } else { |
| 247 | return false; |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | if (ignoreClip) { |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 253 | this->setDrawStateStencil(ars); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 254 | return true; |
| 255 | } |
| 256 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 257 | // An element count of 4 was chosen because of the common pattern in Blink of: |
| 258 | // isect RR |
| 259 | // diff RR |
| 260 | // isect convex_poly |
| 261 | // isect convex_poly |
| 262 | // when drawing rounded div borders. This could probably be tuned based on a |
| 263 | // configuration's relative costs of switching RTs to generate a mask vs |
| 264 | // longer shaders. |
| 265 | if (elements.count() <= 4) { |
| 266 | SkVector clipToRTOffset = { SkIntToScalar(-clipDataIn->fOrigin.fX), |
commit-bot@chromium.org | b21fac1 | 2014-02-07 21:13:11 +0000 | [diff] [blame] | 267 | SkIntToScalar(-clipDataIn->fOrigin.fY) }; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 268 | if (elements.isEmpty() || |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 269 | (requiresAA && this->installClipEffects(elements, are, clipToRTOffset, devBounds))) { |
| 270 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
| 271 | scissorSpaceIBounds.offset(-clipDataIn->fOrigin); |
| 272 | if (NULL == devBounds || |
| 273 | !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) { |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 274 | scissorState->set(scissorSpaceIBounds); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 275 | } |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 276 | this->setDrawStateStencil(ars); |
commit-bot@chromium.org | 65ee5f4 | 2014-02-04 17:49:48 +0000 | [diff] [blame] | 277 | return true; |
| 278 | } |
| 279 | } |
bsalomon@google.com | d3066bd | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 280 | |
commit-bot@chromium.org | 65ee5f4 | 2014-02-04 17:49:48 +0000 | [diff] [blame] | 281 | #if GR_AA_CLIP |
robertphillips@google.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 282 | // 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] | 283 | if (0 == rt->numSamples() && requiresAA) { |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 284 | GrTexture* result = NULL; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 285 | |
| 286 | if (this->useSWOnlyPath(elements)) { |
| 287 | // The clip geometry is complex enough that it will be more efficient to create it |
| 288 | // entirely in software |
| 289 | result = this->createSoftwareClipMask(genID, |
| 290 | initialState, |
| 291 | elements, |
| 292 | clipSpaceIBounds); |
| 293 | } else { |
| 294 | result = this->createAlphaClipMask(genID, |
| 295 | initialState, |
| 296 | elements, |
| 297 | clipSpaceIBounds); |
| 298 | } |
| 299 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 300 | if (result) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 301 | // The mask's top left coord should be pinned to the rounded-out top left corner of |
| 302 | // clipSpace bounds. We determine the mask's position WRT to the render target here. |
| 303 | SkIRect rtSpaceMaskBounds = clipSpaceIBounds; |
| 304 | rtSpaceMaskBounds.offset(-clipDataIn->fOrigin); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 305 | are->set(fGpu->drawState()); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 306 | setup_drawstate_aaclip(fGpu, result, rtSpaceMaskBounds); |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 307 | this->setDrawStateStencil(ars); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 308 | return true; |
| 309 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 310 | // 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] | 311 | } |
| 312 | #endif // GR_AA_CLIP |
| 313 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 314 | // Either a hard (stencil buffer) clip was explicitly requested or an anti-aliased clip couldn't |
| 315 | // be created. In either case, free up the texture in the anti-aliased mask cache. |
| 316 | // TODO: this may require more investigation. Ganesh performs a lot of utility draws (e.g., |
| 317 | // clears, InOrderDrawBuffer playbacks) that hit the stencil buffer path. These may be |
| 318 | // "incorrectly" clearing the AA cache. |
robertphillips@google.com | 5acc0e3 | 2012-05-17 12:01:02 +0000 | [diff] [blame] | 319 | fAACache.reset(); |
| 320 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 321 | // 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] | 322 | SkIPoint clipSpaceToStencilSpaceOffset = -clipDataIn->fOrigin; |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 323 | this->createStencilClipMask(genID, |
| 324 | initialState, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 325 | elements, |
| 326 | clipSpaceIBounds, |
| 327 | clipSpaceToStencilSpaceOffset); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 328 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 329 | // This must occur after createStencilClipMask. That function may change the scissor. Also, it |
| 330 | // only guarantees that the stencil mask is correct within the bounds it was passed, so we must |
| 331 | // use both stencil and scissor test to the bounds for the final draw. |
| 332 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
| 333 | scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset); |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 334 | scissorState->set(scissorSpaceIBounds); |
| 335 | this->setDrawStateStencil(ars); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | |
| 339 | #define VISUALIZE_COMPLEX_CLIP 0 |
| 340 | |
| 341 | #if VISUALIZE_COMPLEX_CLIP |
tfarina@chromium.org | 223137f | 2012-11-21 22:38:36 +0000 | [diff] [blame] | 342 | #include "SkRandom.h" |
| 343 | SkRandom gRandom; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 344 | #define SET_RANDOM_COLOR drawState->setColor(0xff000000 | gRandom.nextU()); |
| 345 | #else |
| 346 | #define SET_RANDOM_COLOR |
| 347 | #endif |
| 348 | |
| 349 | namespace { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 350 | |
| 351 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 352 | // set up the OpenGL blend function to perform the specified |
| 353 | // boolean operation for alpha clip mask creation |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 354 | void setup_boolean_blendcoeffs(GrDrawState* drawState, SkRegion::Op op) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 355 | |
| 356 | switch (op) { |
| 357 | case SkRegion::kReplace_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 358 | drawState->setBlendFunc(kOne_GrBlendCoeff, kZero_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 359 | break; |
| 360 | case SkRegion::kIntersect_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 361 | drawState->setBlendFunc(kDC_GrBlendCoeff, kZero_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 362 | break; |
| 363 | case SkRegion::kUnion_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 364 | drawState->setBlendFunc(kOne_GrBlendCoeff, kISC_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 365 | break; |
| 366 | case SkRegion::kXOR_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 367 | drawState->setBlendFunc(kIDC_GrBlendCoeff, kISC_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 368 | break; |
| 369 | case SkRegion::kDifference_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 370 | drawState->setBlendFunc(kZero_GrBlendCoeff, kISC_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 371 | break; |
| 372 | case SkRegion::kReverseDifference_Op: |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 373 | drawState->setBlendFunc(kIDC_GrBlendCoeff, kZero_GrBlendCoeff); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 374 | break; |
| 375 | default: |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 376 | SkASSERT(false); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 377 | break; |
| 378 | } |
| 379 | } |
| 380 | |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 381 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 382 | |
| 383 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 384 | bool GrClipMaskManager::drawElement(GrTexture* target, |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 385 | const SkClipStack::Element* element, |
| 386 | GrPathRenderer* pr) { |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 387 | GrDrawState* drawState = fGpu->drawState(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 388 | |
| 389 | drawState->setRenderTarget(target->asRenderTarget()); |
| 390 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 391 | // TODO: Draw rrects directly here. |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 392 | switch (element->getType()) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 393 | case Element::kEmpty_Type: |
| 394 | SkDEBUGFAIL("Should never get here with an empty element."); |
| 395 | break; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 396 | case Element::kRect_Type: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 397 | // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers |
| 398 | // the entire mask bounds and writes 0 outside the rect. |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 399 | if (element->isAA()) { |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 400 | this->getContext()->getAARectRenderer()->fillAARect(fGpu, |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 401 | element->getRect(), |
| 402 | SkMatrix::I(), |
| 403 | element->getRect()); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 404 | } else { |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 405 | fGpu->drawSimpleRect(element->getRect()); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 406 | } |
| 407 | return true; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 408 | default: { |
| 409 | SkPath path; |
| 410 | element->asPath(&path); |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 411 | path.setIsVolatile(true); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 412 | if (path.isInverseFillType()) { |
| 413 | path.toggleInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 414 | } |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 415 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 416 | if (NULL == pr) { |
| 417 | GrPathRendererChain::DrawType type; |
| 418 | type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType : |
| 419 | GrPathRendererChain::kColor_DrawType; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 420 | pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 421 | } |
| 422 | if (NULL == pr) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 423 | return false; |
| 424 | } |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 425 | pr->drawPath(path, stroke, fGpu, element->isAA()); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 426 | break; |
| 427 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 428 | } |
| 429 | return true; |
| 430 | } |
| 431 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 432 | bool GrClipMaskManager::canStencilAndDrawElement(GrTexture* target, |
| 433 | const SkClipStack::Element* element, |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 434 | GrPathRenderer** pr) { |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 435 | GrDrawState* drawState = fGpu->drawState(); |
| 436 | drawState->setRenderTarget(target->asRenderTarget()); |
| 437 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 438 | if (Element::kRect_Type == element->getType()) { |
| 439 | return true; |
| 440 | } else { |
| 441 | // We shouldn't get here with an empty clip element. |
| 442 | SkASSERT(Element::kEmpty_Type != element->getType()); |
| 443 | SkPath path; |
| 444 | element->asPath(&path); |
| 445 | if (path.isInverseFillType()) { |
| 446 | path.toggleInverseFillType(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 447 | } |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 448 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
| 449 | GrPathRendererChain::DrawType type = element->isAA() ? |
| 450 | GrPathRendererChain::kStencilAndColorAntiAlias_DrawType : |
| 451 | GrPathRendererChain::kStencilAndColor_DrawType; |
| 452 | *pr = this->getContext()->getPathRenderer(path, stroke, fGpu, false, type); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 453 | return SkToBool(*pr); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 457 | void GrClipMaskManager::mergeMask(GrTexture* dstMask, |
| 458 | GrTexture* srcMask, |
| 459 | SkRegion::Op op, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 460 | const SkIRect& dstBound, |
| 461 | const SkIRect& srcBound) { |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 462 | GrDrawState::AutoViewMatrixRestore avmr; |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 463 | GrDrawState* drawState = fGpu->drawState(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 464 | SkAssertResult(avmr.setIdentity(drawState)); |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 465 | GrDrawState::AutoRestoreEffects are(drawState); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 466 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 467 | drawState->setRenderTarget(dstMask->asRenderTarget()); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 468 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 469 | setup_boolean_blendcoeffs(drawState, op); |
skia.committer@gmail.com | 72b2e6f | 2012-11-08 02:03:56 +0000 | [diff] [blame] | 470 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 471 | SkMatrix sampleM; |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 472 | sampleM.setIDiv(srcMask->width(), srcMask->height()); |
skia.committer@gmail.com | 956b310 | 2013-07-26 07:00:58 +0000 | [diff] [blame] | 473 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 474 | drawState->addColorProcessor( |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 475 | GrTextureDomainEffect::Create(srcMask, |
| 476 | sampleM, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 477 | GrTextureDomain::MakeTexelDomain(srcMask, srcBound), |
| 478 | GrTextureDomain::kDecal_Mode, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 479 | GrTextureParams::kNone_FilterMode))->unref(); |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 480 | fGpu->drawSimpleRect(SkRect::Make(dstBound)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 481 | } |
| 482 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 483 | GrTexture* GrClipMaskManager::createTempMask(int width, int height) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 484 | GrSurfaceDesc desc; |
| 485 | desc.fFlags = kRenderTarget_GrSurfaceFlag|kNoStencil_GrSurfaceFlag; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 486 | desc.fWidth = width; |
| 487 | desc.fHeight = height; |
robertphillips@google.com | 75b3c96 | 2012-06-07 12:08:45 +0000 | [diff] [blame] | 488 | desc.fConfig = kAlpha_8_GrPixelConfig; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 489 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 490 | return fGpu->getContext()->refScratchTexture(desc, GrContext::kApprox_ScratchTexMatch); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 491 | } |
| 492 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 493 | //////////////////////////////////////////////////////////////////////////////// |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 494 | // Return the texture currently in the cache if it exists. Otherwise, return NULL |
| 495 | GrTexture* GrClipMaskManager::getCachedMaskTexture(int32_t elementsGenID, |
| 496 | const SkIRect& clipSpaceIBounds) { |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 497 | bool cached = fAACache.canReuse(elementsGenID, clipSpaceIBounds); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 498 | if (!cached) { |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 499 | return NULL; |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 500 | } |
| 501 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 502 | return fAACache.getLastMask(); |
| 503 | } |
| 504 | |
| 505 | //////////////////////////////////////////////////////////////////////////////// |
| 506 | // Allocate a texture in the texture cache. This function returns the texture |
| 507 | // allocated (or NULL on error). |
| 508 | GrTexture* GrClipMaskManager::allocMaskTexture(int32_t elementsGenID, |
| 509 | const SkIRect& clipSpaceIBounds, |
| 510 | bool willUpload) { |
| 511 | // Since we are setting up the cache we should free up the |
| 512 | // currently cached mask so it can be reused. |
| 513 | fAACache.reset(); |
| 514 | |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 515 | GrSurfaceDesc desc; |
| 516 | desc.fFlags = willUpload ? kNone_GrSurfaceFlags : kRenderTarget_GrSurfaceFlag; |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 517 | desc.fWidth = clipSpaceIBounds.width(); |
| 518 | desc.fHeight = clipSpaceIBounds.height(); |
| 519 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 520 | if (willUpload || this->getContext()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 521 | // We would always like A8 but it isn't supported on all platforms |
| 522 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 523 | } |
| 524 | |
| 525 | fAACache.acquireMask(elementsGenID, desc, clipSpaceIBounds); |
| 526 | return fAACache.getLastMask(); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 527 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 528 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 529 | //////////////////////////////////////////////////////////////////////////////// |
| 530 | // Create a 8-bit clip mask in alpha |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 531 | GrTexture* GrClipMaskManager::createAlphaClipMask(int32_t elementsGenID, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 532 | GrReducedClip::InitialState initialState, |
| 533 | const GrReducedClip::ElementList& elements, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 534 | const SkIRect& clipSpaceIBounds) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 535 | SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 536 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 537 | // First, check for cached texture |
| 538 | GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 539 | if (result) { |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 540 | fCurrClipMaskType = kAlpha_ClipMaskType; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 541 | return result; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 542 | } |
| 543 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 544 | // There's no texture in the cache. Let's try to allocate it then. |
| 545 | result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, false); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 546 | if (NULL == result) { |
robertphillips@google.com | f105b10 | 2012-05-14 12:18:26 +0000 | [diff] [blame] | 547 | fAACache.reset(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 548 | return NULL; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 549 | } |
| 550 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 551 | // 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] | 552 | SkVector clipToMaskOffset = { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 553 | SkIntToScalar(-clipSpaceIBounds.fLeft), |
| 554 | SkIntToScalar(-clipSpaceIBounds.fTop) |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 555 | }; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 556 | // The texture may be larger than necessary, this rect represents the part of the texture |
| 557 | // we populate with a rasterization of the clip. |
| 558 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 559 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 560 | // Set the matrix so that rendered clip elements are transformed to mask space from clip space. |
| 561 | SkMatrix translate; |
| 562 | translate.setTranslate(clipToMaskOffset); |
| 563 | GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &translate); |
| 564 | |
| 565 | GrDrawState* drawState = fGpu->drawState(); |
| 566 | |
bsalomon@google.com | cf939ae | 2012-12-13 19:59:23 +0000 | [diff] [blame] | 567 | // We're drawing a coverage mask and want coverage to be run through the blend function. |
| 568 | drawState->enableState(GrDrawState::kCoverageDrawing_StateBit); |
| 569 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 570 | // The scratch texture that we are drawing into can be substantially larger than the mask. Only |
| 571 | // clear the part that we care about. |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 572 | fGpu->clear(&maskSpaceIBounds, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 573 | GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000, |
robertphillips@google.com | 56ce48a | 2013-10-31 21:44:25 +0000 | [diff] [blame] | 574 | true, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 575 | result->asRenderTarget()); |
skia.committer@gmail.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 576 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 577 | // When we use the stencil in the below loop it is important to have this clip installed. |
| 578 | // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first |
| 579 | // pass must not set values outside of this bounds or stencil values outside the rect won't be |
| 580 | // cleared. |
| 581 | GrDrawTarget::AutoClipRestore acr(fGpu, maskSpaceIBounds); |
| 582 | drawState->enableState(GrDrawState::kClip_StateBit); |
| 583 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 584 | SkAutoTUnref<GrTexture> temp; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 585 | // walk through each clip element and perform its set op |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 586 | for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 587 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 588 | SkRegion::Op op = element->getOp(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 589 | bool invert = element->isInverseFilled(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 590 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 591 | if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 592 | GrPathRenderer* pr = NULL; |
| 593 | bool useTemp = !this->canStencilAndDrawElement(result, element, &pr); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 594 | GrTexture* dst; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 595 | // 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] | 596 | // mask buffer can be substantially larger than the actually clip stack element. We |
| 597 | // 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] | 598 | // the accumulator. |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 599 | SkIRect maskSpaceElementIBounds; |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 600 | |
| 601 | if (useTemp) { |
| 602 | if (invert) { |
| 603 | maskSpaceElementIBounds = maskSpaceIBounds; |
| 604 | } else { |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 605 | SkRect elementBounds = element->getBounds(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 606 | elementBounds.offset(clipToMaskOffset); |
| 607 | elementBounds.roundOut(&maskSpaceElementIBounds); |
| 608 | } |
| 609 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 610 | if (!temp) { |
| 611 | temp.reset(this->createTempMask(maskSpaceIBounds.fRight, |
| 612 | maskSpaceIBounds.fBottom)); |
| 613 | if (!temp) { |
| 614 | fAACache.reset(); |
| 615 | return NULL; |
| 616 | } |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 617 | } |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 618 | dst = temp; |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 619 | // clear the temp target and set blend to replace |
| 620 | fGpu->clear(&maskSpaceElementIBounds, |
| 621 | invert ? 0xffffffff : 0x00000000, |
robertphillips@google.com | 56ce48a | 2013-10-31 21:44:25 +0000 | [diff] [blame] | 622 | true, |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 623 | dst->asRenderTarget()); |
| 624 | setup_boolean_blendcoeffs(drawState, SkRegion::kReplace_Op); |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 625 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 626 | } else { |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 627 | // draw directly into the result with the stencil set to make the pixels affected |
| 628 | // by the clip shape be non-zero. |
| 629 | dst = result; |
| 630 | GR_STATIC_CONST_SAME_STENCIL(kStencilInElement, |
| 631 | kReplace_StencilOp, |
| 632 | kReplace_StencilOp, |
| 633 | kAlways_StencilFunc, |
| 634 | 0xffff, |
| 635 | 0xffff, |
| 636 | 0xffff); |
| 637 | drawState->setStencil(kStencilInElement); |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 638 | setup_boolean_blendcoeffs(drawState, op); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 639 | } |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 640 | |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 641 | drawState->setAlpha(invert ? 0x00 : 0xff); |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 642 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 643 | if (!this->drawElement(dst, element, pr)) { |
| 644 | fAACache.reset(); |
| 645 | return NULL; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 646 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 647 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 648 | if (useTemp) { |
| 649 | // Now draw into the accumulator using the real operation and the temp buffer as a |
| 650 | // texture |
| 651 | this->mergeMask(result, |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 652 | temp, |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 653 | op, |
| 654 | maskSpaceIBounds, |
| 655 | maskSpaceElementIBounds); |
| 656 | } else { |
| 657 | // Draw to the exterior pixels (those with a zero stencil value). |
| 658 | drawState->setAlpha(invert ? 0xff : 0x00); |
| 659 | GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement, |
| 660 | kZero_StencilOp, |
| 661 | kZero_StencilOp, |
| 662 | kEqual_StencilFunc, |
| 663 | 0xffff, |
| 664 | 0x0000, |
| 665 | 0xffff); |
| 666 | drawState->setStencil(kDrawOutsideElement); |
| 667 | fGpu->drawSimpleRect(clipSpaceIBounds); |
| 668 | drawState->disableStencil(); |
| 669 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 670 | } else { |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 671 | // all the remaining ops can just be directly draw into the accumulation buffer |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +0000 | [diff] [blame] | 672 | drawState->setAlpha(0xff); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 673 | setup_boolean_blendcoeffs(drawState, op); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 674 | this->drawElement(result, element); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
| 677 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 678 | fCurrClipMaskType = kAlpha_ClipMaskType; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 679 | return result; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 683 | // 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] | 684 | // (as opposed to canvas) coordinates |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 685 | bool GrClipMaskManager::createStencilClipMask(int32_t elementsGenID, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 686 | GrReducedClip::InitialState initialState, |
| 687 | const GrReducedClip::ElementList& elements, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 688 | const SkIRect& clipSpaceIBounds, |
| 689 | const SkIPoint& clipSpaceToStencilOffset) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 690 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 691 | SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 692 | |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 693 | GrDrawState* drawState = fGpu->drawState(); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 694 | SkASSERT(drawState->isClipState()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 695 | |
| 696 | GrRenderTarget* rt = drawState->getRenderTarget(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 697 | SkASSERT(rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 698 | |
| 699 | // TODO: dynamically attach a SB when needed. |
| 700 | GrStencilBuffer* stencilBuffer = rt->getStencilBuffer(); |
| 701 | if (NULL == stencilBuffer) { |
| 702 | return false; |
| 703 | } |
| 704 | |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 705 | if (stencilBuffer->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) { |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 706 | stencilBuffer->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 707 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 708 | // Set the matrix so that rendered clip elements are transformed from clip to stencil space. |
| 709 | SkVector translate = { |
| 710 | SkIntToScalar(clipSpaceToStencilOffset.fX), |
| 711 | SkIntToScalar(clipSpaceToStencilOffset.fY) |
| 712 | }; |
| 713 | SkMatrix matrix; |
| 714 | matrix.setTranslate(translate); |
| 715 | GrDrawTarget::AutoGeometryAndStatePush agasp(fGpu, GrDrawTarget::kReset_ASRInit, &matrix); |
bsalomon@google.com | 13b85aa | 2012-06-15 21:09:40 +0000 | [diff] [blame] | 716 | drawState = fGpu->drawState(); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 717 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 718 | drawState->setRenderTarget(rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 719 | |
bsalomon@google.com | 9f13174 | 2012-12-13 20:43:56 +0000 | [diff] [blame] | 720 | // We set the current clip to the bounds so that our recursive draws are scissored to them. |
| 721 | SkIRect stencilSpaceIBounds(clipSpaceIBounds); |
| 722 | stencilSpaceIBounds.offset(clipSpaceToStencilOffset); |
| 723 | GrDrawTarget::AutoClipRestore acr(fGpu, stencilSpaceIBounds); |
| 724 | drawState->enableState(GrDrawState::kClip_StateBit); |
| 725 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 726 | #if !VISUALIZE_COMPLEX_CLIP |
| 727 | drawState->enableState(GrDrawState::kNoColorWrites_StateBit); |
| 728 | #endif |
| 729 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 730 | int clipBit = stencilBuffer->bits(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 731 | SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers"); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 732 | clipBit = (1 << (clipBit-1)); |
| 733 | |
joshualitt | 6db519c | 2014-10-29 08:48:18 -0700 | [diff] [blame^] | 734 | fGpu->clearStencilClip(stencilSpaceIBounds, |
| 735 | GrReducedClip::kAllIn_InitialState == initialState, |
| 736 | rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 737 | |
| 738 | // walk through each clip element and perform its set op |
| 739 | // with the existing clip. |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 740 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 741 | const Element* element = iter.get(); |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 742 | bool fillInverted = false; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 743 | // enabled at bottom of loop |
| 744 | drawState->disableState(GrGpu::kModifyStencilClip_StateBit); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 745 | // if the target is MSAA then we want MSAA enabled when the clip is soft |
| 746 | if (rt->isMultisampled()) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 747 | drawState->setState(GrDrawState::kHWAntialias_StateBit, element->isAA()); |
bsalomon@google.com | ded4f4b | 2012-06-28 18:48:06 +0000 | [diff] [blame] | 748 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 749 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 750 | // This will be used to determine whether the clip shape can be rendered into the |
| 751 | // stencil with arbitrary stencil settings. |
| 752 | GrPathRenderer::StencilSupport stencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 753 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 754 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 755 | |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 756 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 757 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 758 | GrPathRenderer* pr = NULL; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 759 | SkPath clipPath; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 760 | if (Element::kRect_Type == element->getType()) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 761 | stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 762 | fillInverted = false; |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 763 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 764 | element->asPath(&clipPath); |
| 765 | fillInverted = clipPath.isInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 766 | if (fillInverted) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 767 | clipPath.toggleInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 768 | } |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 769 | pr = this->getContext()->getPathRenderer(clipPath, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 770 | stroke, |
| 771 | fGpu, |
| 772 | false, |
| 773 | GrPathRendererChain::kStencilOnly_DrawType, |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 774 | &stencilSupport); |
| 775 | if (NULL == pr) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 776 | return false; |
| 777 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 778 | } |
| 779 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 780 | int passes; |
| 781 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 782 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 783 | bool canRenderDirectToStencil = |
| 784 | GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 785 | bool canDrawDirectToClip; // Given the renderer, the element, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 786 | // fill rule, and set operation can |
| 787 | // we render the element directly to |
| 788 | // stencil bit used for clipping. |
| 789 | canDrawDirectToClip = GrStencilSettings::GetClipPasses(op, |
| 790 | canRenderDirectToStencil, |
| 791 | clipBit, |
| 792 | fillInverted, |
| 793 | &passes, |
| 794 | stencilSettings); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 795 | |
| 796 | // draw the element to the client stencil bits if necessary |
| 797 | if (!canDrawDirectToClip) { |
| 798 | GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 799 | kIncClamp_StencilOp, |
| 800 | kIncClamp_StencilOp, |
| 801 | kAlways_StencilFunc, |
| 802 | 0xffff, |
| 803 | 0x0000, |
| 804 | 0xffff); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 805 | SET_RANDOM_COLOR |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 806 | if (Element::kRect_Type == element->getType()) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 807 | *drawState->stencil() = gDrawToStencil; |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 808 | fGpu->drawSimpleRect(element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 809 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 810 | if (!clipPath.isEmpty()) { |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 811 | if (canRenderDirectToStencil) { |
| 812 | *drawState->stencil() = gDrawToStencil; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 813 | pr->drawPath(clipPath, stroke, fGpu, false); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 814 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 815 | pr->stencilPath(clipPath, stroke, fGpu); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 816 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | // now we modify the clip bit by rendering either the clip |
| 822 | // element directly or a bounding rect of the entire clip. |
| 823 | drawState->enableState(GrGpu::kModifyStencilClip_StateBit); |
| 824 | for (int p = 0; p < passes; ++p) { |
| 825 | *drawState->stencil() = stencilSettings[p]; |
| 826 | if (canDrawDirectToClip) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 827 | if (Element::kRect_Type == element->getType()) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 828 | SET_RANDOM_COLOR |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 829 | fGpu->drawSimpleRect(element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 830 | } else { |
| 831 | SET_RANDOM_COLOR |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 832 | pr->drawPath(clipPath, stroke, fGpu, false); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 833 | } |
| 834 | } else { |
| 835 | SET_RANDOM_COLOR |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 836 | // The view matrix is setup to do clip space -> stencil space translation, so |
| 837 | // draw rect in clip space. |
bsalomon | 01c8da1 | 2014-08-04 09:21:30 -0700 | [diff] [blame] | 838 | fGpu->drawSimpleRect(SkRect::Make(clipSpaceIBounds)); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 842 | } |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 843 | // 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] | 844 | SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 845 | fCurrClipMaskType = kStencil_ClipMaskType; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 846 | return true; |
| 847 | } |
| 848 | |
robertphillips@google.com | f8d904a | 2012-07-31 12:18:16 +0000 | [diff] [blame] | 849 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 850 | // mapping of clip-respecting stencil funcs to normal stencil funcs |
| 851 | // mapping depends on whether stencil-clipping is in effect. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 852 | static const GrStencilFunc |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 853 | gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = { |
| 854 | {// Stencil-Clipping is DISABLED, we are effectively always inside the clip |
| 855 | // In the Clip Funcs |
| 856 | kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 857 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 858 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 859 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 860 | // Special in the clip func that forces user's ref to be 0. |
| 861 | kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 862 | // make ref 0 and do normal nequal. |
| 863 | }, |
| 864 | {// Stencil-Clipping is ENABLED |
| 865 | // In the Clip Funcs |
| 866 | kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 867 | // eq stencil clip bit, mask |
| 868 | // out user bits. |
| 869 | |
| 870 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 871 | // add stencil bit to mask and ref |
| 872 | |
| 873 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 874 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 875 | // for both of these we can add |
| 876 | // the clip bit to the mask and |
| 877 | // ref and compare as normal |
| 878 | // Special in the clip func that forces user's ref to be 0. |
| 879 | kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 880 | // make ref have only the clip bit set |
| 881 | // and make comparison be less |
| 882 | // 10..0 < 1..user_bits.. |
| 883 | } |
| 884 | }; |
| 885 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 886 | namespace { |
| 887 | // Sets the settings to clip against the stencil buffer clip while ignoring the |
| 888 | // client bits. |
| 889 | const GrStencilSettings& basic_apply_stencil_clip_settings() { |
| 890 | // stencil settings to use when clip is in stencil |
| 891 | GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings, |
| 892 | kKeep_StencilOp, |
| 893 | kKeep_StencilOp, |
| 894 | kAlwaysIfInClip_StencilFunc, |
| 895 | 0x0000, |
| 896 | 0x0000, |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 897 | 0x0000); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 898 | return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings); |
| 899 | } |
| 900 | } |
| 901 | |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 902 | void GrClipMaskManager::setDrawStateStencil(GrDrawState::AutoRestoreStencil* ars) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 903 | // We make two copies of the StencilSettings here (except in the early |
| 904 | // exit scenario. One copy from draw state to the stack var. Then another |
| 905 | // from the stack var to the gpu. We could make this class hold a ptr to |
| 906 | // GrGpu's fStencilSettings and eliminate the stack copy here. |
| 907 | |
| 908 | const GrDrawState& drawState = fGpu->getDrawState(); |
| 909 | |
| 910 | // use stencil for clipping if clipping is enabled and the clip |
| 911 | // has been written into the stencil. |
| 912 | GrClipMaskManager::StencilClipMode clipMode; |
| 913 | if (this->isClipInStencil() && drawState.isClipState()) { |
| 914 | clipMode = GrClipMaskManager::kRespectClip_StencilClipMode; |
| 915 | // 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] | 916 | SkASSERT(!drawState.isStateFlagEnabled( |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 917 | GrGpu::kModifyStencilClip_StateBit)); |
| 918 | } else if (drawState.isStateFlagEnabled( |
| 919 | GrGpu::kModifyStencilClip_StateBit)) { |
| 920 | clipMode = GrClipMaskManager::kModifyClip_StencilClipMode; |
| 921 | } else { |
| 922 | clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode; |
| 923 | } |
| 924 | |
| 925 | GrStencilSettings settings; |
| 926 | // The GrGpu client may not be using the stencil buffer but we may need to |
| 927 | // enable it in order to respect a stencil clip. |
| 928 | if (drawState.getStencil().isDisabled()) { |
| 929 | if (GrClipMaskManager::kRespectClip_StencilClipMode == clipMode) { |
| 930 | settings = basic_apply_stencil_clip_settings(); |
| 931 | } else { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 932 | return; |
| 933 | } |
| 934 | } else { |
| 935 | settings = drawState.getStencil(); |
| 936 | } |
| 937 | |
| 938 | // TODO: dynamically attach a stencil buffer |
| 939 | int stencilBits = 0; |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 940 | GrStencilBuffer* stencilBuffer = drawState.getRenderTarget()->getStencilBuffer(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 941 | if (stencilBuffer) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 942 | stencilBits = stencilBuffer->bits(); |
| 943 | } |
| 944 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 945 | SkASSERT(fGpu->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp()); |
| 946 | SkASSERT(fGpu->caps()->twoSidedStencilSupport() || !settings.isTwoSided()); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 947 | this->adjustStencilParams(&settings, clipMode, stencilBits); |
joshualitt | a58fe35 | 2014-10-27 08:39:00 -0700 | [diff] [blame] | 948 | ars->set(fGpu->drawState()); |
| 949 | fGpu->drawState()->setStencil(settings); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings, |
| 953 | StencilClipMode mode, |
| 954 | int stencilBitCnt) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 955 | SkASSERT(stencilBitCnt > 0); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 956 | |
| 957 | if (kModifyClip_StencilClipMode == mode) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 958 | // We assume that this clip manager itself is drawing to the GrGpu and |
| 959 | // has already setup the correct values. |
| 960 | return; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 961 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 962 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 963 | unsigned int clipBit = (1 << (stencilBitCnt - 1)); |
| 964 | unsigned int userBits = clipBit - 1; |
| 965 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 966 | GrStencilSettings::Face face = GrStencilSettings::kFront_Face; |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 967 | bool twoSided = fGpu->caps()->twoSidedStencilSupport(); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 968 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 969 | bool finished = false; |
| 970 | while (!finished) { |
| 971 | GrStencilFunc func = settings->func(face); |
| 972 | uint16_t writeMask = settings->writeMask(face); |
| 973 | uint16_t funcMask = settings->funcMask(face); |
| 974 | uint16_t funcRef = settings->funcRef(face); |
| 975 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 976 | SkASSERT((unsigned) func < kStencilFuncCount); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 977 | |
| 978 | writeMask &= userBits; |
| 979 | |
| 980 | if (func >= kBasicStencilFuncCount) { |
| 981 | int respectClip = kRespectClip_StencilClipMode == mode; |
| 982 | if (respectClip) { |
| 983 | // The GrGpu class should have checked this |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 984 | SkASSERT(this->isClipInStencil()); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 985 | switch (func) { |
| 986 | case kAlwaysIfInClip_StencilFunc: |
| 987 | funcMask = clipBit; |
| 988 | funcRef = clipBit; |
| 989 | break; |
| 990 | case kEqualIfInClip_StencilFunc: |
| 991 | case kLessIfInClip_StencilFunc: |
| 992 | case kLEqualIfInClip_StencilFunc: |
| 993 | funcMask = (funcMask & userBits) | clipBit; |
| 994 | funcRef = (funcRef & userBits) | clipBit; |
| 995 | break; |
| 996 | case kNonZeroIfInClip_StencilFunc: |
| 997 | funcMask = (funcMask & userBits) | clipBit; |
| 998 | funcRef = clipBit; |
| 999 | break; |
| 1000 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 1001 | SkFAIL("Unknown stencil func"); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } else { |
| 1004 | funcMask &= userBits; |
| 1005 | funcRef &= userBits; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1006 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1007 | const GrStencilFunc* table = |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1008 | gSpecialToBasicStencilFunc[respectClip]; |
| 1009 | func = table[func - kBasicStencilFuncCount]; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1010 | SkASSERT(func >= 0 && func < kBasicStencilFuncCount); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1011 | } else { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1012 | funcMask &= userBits; |
| 1013 | funcRef &= userBits; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1014 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1015 | |
| 1016 | settings->setFunc(face, func); |
| 1017 | settings->setWriteMask(face, writeMask); |
| 1018 | settings->setFuncMask(face, funcMask); |
| 1019 | settings->setFuncRef(face, funcRef); |
| 1020 | |
| 1021 | if (GrStencilSettings::kFront_Face == face) { |
| 1022 | face = GrStencilSettings::kBack_Face; |
| 1023 | finished = !twoSided; |
| 1024 | } else { |
| 1025 | finished = true; |
| 1026 | } |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1027 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1028 | if (!twoSided) { |
| 1029 | settings->copyFrontSettingsToBack(); |
| 1030 | } |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1034 | GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1035 | GrReducedClip::InitialState initialState, |
| 1036 | const GrReducedClip::ElementList& elements, |
| 1037 | const SkIRect& clipSpaceIBounds) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1038 | SkASSERT(kNone_ClipMaskType == fCurrClipMaskType); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1039 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 1040 | GrTexture* result = this->getCachedMaskTexture(elementsGenID, clipSpaceIBounds); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1041 | if (result) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1042 | return result; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1045 | // The mask texture may be larger than necessary. We round out the clip space bounds and pin |
| 1046 | // the top left corner of the resulting rect to the top left of the texture. |
| 1047 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 1048 | |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 1049 | GrSWMaskHelper helper(this->getContext()); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1050 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 1051 | SkMatrix matrix; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1052 | matrix.setTranslate(SkIntToScalar(-clipSpaceIBounds.fLeft), |
| 1053 | SkIntToScalar(-clipSpaceIBounds.fTop)); |
krajcevski | 71614ac | 2014-08-13 10:36:18 -0700 | [diff] [blame] | 1054 | helper.init(maskSpaceIBounds, &matrix, false); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1055 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 1056 | helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1057 | |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 1058 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 1059 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 1060 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) { |
robertphillips@google.com | a6f11c4 | 2012-07-23 17:39:44 +0000 | [diff] [blame] | 1061 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1062 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1063 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1064 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1065 | if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
| 1066 | // Intersect and reverse difference require modifying pixels outside of the geometry |
| 1067 | // that is being "drawn". In both cases we erase all the pixels outside of the geometry |
| 1068 | // but leave the pixels inside the geometry alone. For reverse difference we invert all |
| 1069 | // the pixels before clearing the ones outside the geometry. |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1070 | if (SkRegion::kReverseDifference_Op == op) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 1071 | SkRect temp = SkRect::Make(clipSpaceIBounds); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1072 | // invert the entire scene |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 1073 | helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 1076 | SkPath clipPath; |
| 1077 | element->asPath(&clipPath); |
| 1078 | clipPath.toggleInverseFillType(); |
| 1079 | helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1080 | |
| 1081 | continue; |
| 1082 | } |
| 1083 | |
| 1084 | // The other ops (union, xor, diff) only affect pixels inside |
| 1085 | // the geometry so they can just be drawn normally |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1086 | if (Element::kRect_Type == element->getType()) { |
| 1087 | helper.draw(element->getRect(), op, element->isAA(), 0xFF); |
| 1088 | } else { |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 1089 | SkPath path; |
| 1090 | element->asPath(&path); |
| 1091 | helper.draw(path, stroke, op, element->isAA(), 0xFF); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 1095 | // Allocate clip mask texture |
| 1096 | result = this->allocMaskTexture(elementsGenID, clipSpaceIBounds, true); |
| 1097 | if (NULL == result) { |
| 1098 | fAACache.reset(); |
| 1099 | return NULL; |
| 1100 | } |
robertphillips@google.com | d92cf2e | 2013-07-19 18:13:02 +0000 | [diff] [blame] | 1101 | helper.toTexture(result); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1102 | |
bsalomon@google.com | c8f7f47 | 2012-06-18 13:44:51 +0000 | [diff] [blame] | 1103 | fCurrClipMaskType = kAlpha_ClipMaskType; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1104 | return result; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1107 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | c8dc1f7 | 2014-08-21 13:02:13 -0700 | [diff] [blame] | 1108 | void GrClipMaskManager::purgeResources() { |
| 1109 | fAACache.purgeResources(); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1110 | } |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 1111 | |
| 1112 | void GrClipMaskManager::setGpu(GrGpu* gpu) { |
| 1113 | fGpu = gpu; |
| 1114 | fAACache.setContext(gpu->getContext()); |
| 1115 | } |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 1116 | |
| 1117 | void GrClipMaskManager::adjustPathStencilParams(GrStencilSettings* settings) { |
| 1118 | const GrDrawState& drawState = fGpu->getDrawState(); |
| 1119 | GrClipMaskManager::StencilClipMode clipMode; |
| 1120 | if (this->isClipInStencil() && drawState.isClipState()) { |
| 1121 | clipMode = GrClipMaskManager::kRespectClip_StencilClipMode; |
| 1122 | // We can't be modifying the clip and respecting it at the same time. |
| 1123 | SkASSERT(!drawState.isStateFlagEnabled( |
| 1124 | GrGpu::kModifyStencilClip_StateBit)); |
| 1125 | } else if (drawState.isStateFlagEnabled( |
| 1126 | GrGpu::kModifyStencilClip_StateBit)) { |
| 1127 | clipMode = GrClipMaskManager::kModifyClip_StencilClipMode; |
| 1128 | } else { |
| 1129 | clipMode = GrClipMaskManager::kIgnoreClip_StencilClipMode; |
| 1130 | } |
| 1131 | |
| 1132 | // TODO: dynamically attach a stencil buffer |
| 1133 | int stencilBits = 0; |
| 1134 | GrStencilBuffer* stencilBuffer = |
| 1135 | drawState.getRenderTarget()->getStencilBuffer(); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 1136 | if (stencilBuffer) { |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 1137 | stencilBits = stencilBuffer->bits(); |
| 1138 | this->adjustStencilParams(settings, clipMode, stencilBits); |
| 1139 | } |
| 1140 | } |