robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #include "GrClipMaskManager.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 9 | #include "GrCaps.h" |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 10 | #include "GrDrawContext.h" |
| 11 | #include "GrDrawTarget.h" |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 12 | #include "GrGpuResourcePriv.h" |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 13 | #include "GrPaint.h" |
| 14 | #include "GrPathRenderer.h" |
| 15 | #include "GrRenderTarget.h" |
bsalomon | 6bc1b5f | 2015-02-23 09:06:38 -0800 | [diff] [blame] | 16 | #include "GrRenderTargetPriv.h" |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 17 | #include "GrResourceProvider.h" |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 18 | #include "GrStencilAttachment.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" |
joshualitt | 3a0cfeb | 2014-10-27 07:38:01 -0700 | [diff] [blame] | 21 | #include "SkTLazy.h" |
egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 22 | #include "effects/GrConvexPolyEffect.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 23 | #include "effects/GrPorterDuffXferProcessor.h" |
egdaniel | 8d95ffa | 2014-12-08 13:26:43 -0800 | [diff] [blame] | 24 | #include "effects/GrRRectEffect.h" |
egdaniel | 9513143 | 2014-12-09 11:15:43 -0800 | [diff] [blame] | 25 | #include "effects/GrTextureDomain.h" |
bsalomon@google.com | c6b3e48 | 2012-12-07 20:43:52 +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 | |
| 29 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 30 | // 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] | 31 | // stage matrix this also alters the vertex layout |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 32 | static const GrFragmentProcessor* create_fp_for_mask(GrTexture* result, const SkIRect &devBound) { |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 33 | SkMatrix mat; |
bsalomon | 309d4d5 | 2014-12-18 10:17:44 -0800 | [diff] [blame] | 34 | // We use device coords to compute the texture coordinates. We set our matrix to be a |
| 35 | // translation to the devBound, and then a scaling matrix to normalized coords. |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 36 | mat.setIDiv(result->width(), result->height()); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 37 | mat.preTranslate(SkIntToScalar(-devBound.fLeft), |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 38 | SkIntToScalar(-devBound.fTop)); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 39 | |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 40 | SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height()); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 41 | return GrTextureDomainEffect::Create(result, |
| 42 | mat, |
| 43 | GrTextureDomain::MakeTexelDomain(result, domainTexels), |
| 44 | GrTextureDomain::kDecal_Mode, |
| 45 | GrTextureParams::kNone_FilterMode, |
| 46 | kDevice_GrCoordSet); |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 47 | } |
| 48 | |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 49 | static bool path_needs_SW_renderer(GrContext* context, |
| 50 | const GrDrawTarget* gpu, |
| 51 | const GrPipelineBuilder& pipelineBuilder, |
| 52 | const SkMatrix& viewMatrix, |
| 53 | const SkPath& origPath, |
| 54 | const GrStrokeInfo& stroke, |
| 55 | bool doAA) { |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 56 | // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer |
| 57 | SkTCopyOnFirstWrite<SkPath> path(origPath); |
| 58 | if (path->isInverseFillType()) { |
| 59 | path.writable()->toggleInverseFillType(); |
| 60 | } |
| 61 | // last (false) parameter disallows use of the SW path renderer |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 62 | GrPathRendererChain::DrawType type = doAA ? |
| 63 | GrPathRendererChain::kColorAntiAlias_DrawType : |
| 64 | GrPathRendererChain::kColor_DrawType; |
| 65 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 66 | return nullptr == context->getPathRenderer(gpu, &pipelineBuilder, viewMatrix, *path, stroke, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 67 | false, type); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 68 | } |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 69 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 70 | GrClipMaskManager::GrClipMaskManager(GrDrawTarget* drawTarget) |
bsalomon | c988d2c | 2015-10-07 09:30:05 -0700 | [diff] [blame] | 71 | : fDrawTarget(drawTarget) |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 72 | , fClipMode(kIgnoreClip_StencilClipMode) { |
| 73 | } |
| 74 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 75 | GrContext* GrClipMaskManager::getContext() { return fDrawTarget->cmmAccess().context(); } |
bsalomon | edd77a1 | 2015-05-29 09:45:57 -0700 | [diff] [blame] | 76 | |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 77 | /* |
| 78 | * This method traverses the clip stack to see if the GrSoftwarePathRenderer |
| 79 | * will be used on any element. If so, it returns true to indicate that the |
| 80 | * entire clip should be rendered in SW and then uploaded en masse to the gpu. |
| 81 | */ |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 82 | bool GrClipMaskManager::useSWOnlyPath(const GrPipelineBuilder& pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 83 | const SkVector& clipToMaskOffset, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 84 | const GrReducedClip::ElementList& elements) { |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 85 | // TODO: generalize this function so that when |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 86 | // a clip gets complex enough it can just be done in SW regardless |
| 87 | // of whether it would invoke the GrSoftwarePathRenderer. |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 88 | GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle); |
skia.committer@gmail.com | d21444a | 2012-12-07 02:01:25 +0000 | [diff] [blame] | 89 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 90 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 91 | // space. |
| 92 | SkMatrix translate; |
| 93 | translate.setTranslate(clipToMaskOffset); |
| 94 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 95 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 96 | const Element* element = iter.get(); |
robertphillips@google.com | f69a11b | 2012-06-15 13:58:07 +0000 | [diff] [blame] | 97 | // rects can always be drawn directly w/o using the software path |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 98 | // Skip rrects once we're drawing them directly. |
| 99 | if (Element::kRect_Type != element->getType()) { |
| 100 | SkPath path; |
| 101 | element->asPath(&path); |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 102 | if (path_needs_SW_renderer(this->getContext(), fDrawTarget, pipelineBuilder, translate, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 103 | path, stroke, element->isAA())) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 104 | return true; |
| 105 | } |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 106 | } |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 107 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 108 | return false; |
robertphillips@google.com | a72eef3 | 2012-05-01 17:22:59 +0000 | [diff] [blame] | 109 | } |
| 110 | |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame^] | 111 | bool GrClipMaskManager::getAnalyticClipProcessor(const GrReducedClip::ElementList& elements, |
| 112 | SkVector& clipToRTOffset, |
| 113 | const SkRect* drawBounds, |
| 114 | const GrFragmentProcessor** resultFP) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 115 | SkRect boundsInClipSpace; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 116 | if (drawBounds) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 117 | boundsInClipSpace = *drawBounds; |
| 118 | boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY); |
| 119 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 120 | SkASSERT(elements.count() <= kMaxAnalyticElements); |
| 121 | const GrFragmentProcessor* fps[kMaxAnalyticElements]; |
| 122 | for (int i = 0; i < kMaxAnalyticElements; ++i) { |
| 123 | fps[i] = nullptr; |
| 124 | } |
| 125 | int fpCnt = 0; |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 126 | GrReducedClip::ElementList::Iter iter(elements); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 127 | bool failed = false; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 128 | while (iter.get()) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 129 | SkRegion::Op op = iter.get()->getOp(); |
| 130 | bool invert; |
| 131 | bool skip = false; |
| 132 | switch (op) { |
| 133 | case SkRegion::kReplace_Op: |
| 134 | SkASSERT(iter.get() == elements.head()); |
| 135 | // Fallthrough, handled same as intersect. |
| 136 | case SkRegion::kIntersect_Op: |
| 137 | invert = false; |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 138 | if (drawBounds && iter.get()->contains(boundsInClipSpace)) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 139 | skip = true; |
| 140 | } |
| 141 | break; |
| 142 | case SkRegion::kDifference_Op: |
| 143 | invert = true; |
| 144 | // We don't currently have a cheap test for whether a rect is fully outside an |
| 145 | // element's primitive, so don't attempt to set skip. |
| 146 | break; |
| 147 | default: |
| 148 | failed = true; |
| 149 | break; |
| 150 | } |
| 151 | if (failed) { |
| 152 | break; |
| 153 | } |
| 154 | |
| 155 | if (!skip) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 156 | GrPrimitiveEdgeType edgeType; |
robertphillips | e85a32d | 2015-02-10 08:16:55 -0800 | [diff] [blame] | 157 | if (iter.get()->isAA()) { |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 158 | edgeType = |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 159 | invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 160 | } else { |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 161 | edgeType = |
| 162 | invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 163 | } |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 164 | switch (iter.get()->getType()) { |
| 165 | case SkClipStack::Element::kPath_Type: |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 166 | fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, iter.get()->getPath(), |
| 167 | &clipToRTOffset); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 168 | break; |
| 169 | case SkClipStack::Element::kRRect_Type: { |
| 170 | SkRRect rrect = iter.get()->getRRect(); |
| 171 | rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 172 | fps[fpCnt] = GrRRectEffect::Create(edgeType, rrect); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 173 | break; |
| 174 | } |
| 175 | case SkClipStack::Element::kRect_Type: { |
| 176 | SkRect rect = iter.get()->getRect(); |
| 177 | rect.offset(clipToRTOffset.fX, clipToRTOffset.fY); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 178 | fps[fpCnt] = GrConvexPolyEffect::Create(edgeType, rect); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 179 | break; |
| 180 | } |
| 181 | default: |
| 182 | break; |
| 183 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 184 | if (!fps[fpCnt]) { |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 185 | failed = true; |
| 186 | break; |
| 187 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 188 | fpCnt++; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 189 | } |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 190 | iter.next(); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 191 | } |
| 192 | |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame^] | 193 | *resultFP = nullptr; |
| 194 | if (!failed && fpCnt) { |
| 195 | *resultFP = GrFragmentProcessor::RunInSeries(fps, fpCnt); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 196 | } |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 197 | for (int i = 0; i < fpCnt; ++i) { |
| 198 | fps[i]->unref(); |
| 199 | } |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame^] | 200 | return !failed; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 201 | } |
| 202 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 203 | //////////////////////////////////////////////////////////////////////////////// |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 204 | // sort out what kind of clip mask needs to be created: alpha, stencil, |
| 205 | // scissor, or entirely software |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 206 | bool GrClipMaskManager::setupClipping(const GrPipelineBuilder& pipelineBuilder, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 207 | GrPipelineBuilder::AutoRestoreStencil* ars, |
bsalomon | 3e79124 | 2014-12-17 13:43:13 -0800 | [diff] [blame] | 208 | GrScissorState* scissorState, |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 209 | const SkRect* devBounds, |
| 210 | GrAppliedClip* out) { |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 211 | if (kRespectClip_StencilClipMode == fClipMode) { |
| 212 | fClipMode = kIgnoreClip_StencilClipMode; |
| 213 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 214 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 215 | GrReducedClip::ElementList elements(16); |
brucedawson | 71d7f7f | 2015-02-26 13:28:53 -0800 | [diff] [blame] | 216 | int32_t genID = 0; |
| 217 | GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 218 | SkIRect clipSpaceIBounds; |
brucedawson | 71d7f7f | 2015-02-26 13:28:53 -0800 | [diff] [blame] | 219 | bool requiresAA = false; |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 220 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 221 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 222 | // GrDrawTarget should have filtered this for us |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 223 | SkASSERT(rt); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 224 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 225 | SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(rt->width(), rt->height()); |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 226 | const GrClip& clip = pipelineBuilder.clip(); |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 227 | if (clip.isWideOpen(clipSpaceRTIBounds)) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 228 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 229 | return true; |
| 230 | } |
| 231 | |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 232 | // The clip mask manager always draws with a single IRect so we special case that logic here |
| 233 | // Image filters just use a rect, so we also special case that logic |
| 234 | switch (clip.clipType()) { |
| 235 | case GrClip::kWideOpen_ClipType: |
| 236 | SkFAIL("Should have caught this with clip.isWideOpen()"); |
| 237 | return true; |
bsalomon | 9ce30e1 | 2015-03-06 08:42:34 -0800 | [diff] [blame] | 238 | case GrClip::kIRect_ClipType: { |
| 239 | SkIRect scissor = clip.irect(); |
| 240 | if (scissor.intersect(clipSpaceRTIBounds)) { |
| 241 | scissorState->set(scissor); |
| 242 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
| 243 | return true; |
| 244 | } |
| 245 | return false; |
| 246 | } |
bsalomon | 96e02a8 | 2015-03-06 07:13:01 -0800 | [diff] [blame] | 247 | case GrClip::kClipStack_ClipType: { |
| 248 | clipSpaceRTIBounds.offset(clip.origin()); |
| 249 | GrReducedClip::ReduceClipStack(*clip.clipStack(), |
| 250 | clipSpaceRTIBounds, |
| 251 | &elements, |
| 252 | &genID, |
| 253 | &initialState, |
| 254 | &clipSpaceIBounds, |
| 255 | &requiresAA); |
| 256 | if (elements.isEmpty()) { |
| 257 | if (GrReducedClip::kAllIn_InitialState == initialState) { |
| 258 | if (clipSpaceIBounds == clipSpaceRTIBounds) { |
| 259 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
| 260 | return true; |
| 261 | } |
| 262 | } else { |
| 263 | return false; |
| 264 | } |
| 265 | } |
| 266 | } break; |
| 267 | } |
| 268 | |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 269 | // An element count of 4 was chosen because of the common pattern in Blink of: |
| 270 | // isect RR |
| 271 | // diff RR |
| 272 | // isect convex_poly |
| 273 | // isect convex_poly |
| 274 | // when drawing rounded div borders. This could probably be tuned based on a |
| 275 | // configuration's relative costs of switching RTs to generate a mask vs |
| 276 | // longer shaders. |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 277 | if (elements.count() <= kMaxAnalyticElements) { |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 278 | SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX), |
| 279 | SkIntToScalar(-clip.origin().fY) }; |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 280 | // When there are multiple color samples we want to do per-sample clipping, not compute |
| 281 | // a fractional pixel coverage. |
| 282 | bool disallowAnalyticAA = pipelineBuilder.getRenderTarget()->isUnifiedMultisampled(); |
| 283 | const GrFragmentProcessor* clipFP = nullptr; |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 284 | if (elements.isEmpty() || |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 285 | (requiresAA && !disallowAnalyticAA && |
bsalomon | 0b5b6b2 | 2015-10-14 08:31:34 -0700 | [diff] [blame^] | 286 | this->getAnalyticClipProcessor(elements, clipToRTOffset, devBounds, &clipFP))) { |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 287 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 288 | scissorSpaceIBounds.offset(-clip.origin()); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 289 | if (nullptr == devBounds || |
mtklein | 217daa7 | 2014-07-02 12:55:21 -0700 | [diff] [blame] | 290 | !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) { |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 291 | scissorState->set(scissorSpaceIBounds); |
commit-bot@chromium.org | e5a041c | 2014-03-07 19:43:43 +0000 | [diff] [blame] | 292 | } |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 293 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 294 | out->fClipCoverageFP.reset(clipFP); |
commit-bot@chromium.org | 65ee5f4 | 2014-02-04 17:49:48 +0000 | [diff] [blame] | 295 | return true; |
| 296 | } |
| 297 | } |
bsalomon@google.com | d3066bd | 2014-02-03 20:09:56 +0000 | [diff] [blame] | 298 | |
robertphillips@google.com | a3e5c63 | 2012-05-22 18:09:26 +0000 | [diff] [blame] | 299 | // If MSAA is enabled we can do everything in the stencil buffer. |
vbuzinov | 3e77ba9 | 2015-09-30 23:02:06 -0700 | [diff] [blame] | 300 | if (0 == rt->numStencilSamples() && requiresAA) { |
robertphillips | 588b9ca | 2015-10-04 08:40:31 -0700 | [diff] [blame] | 301 | SkAutoTUnref<GrTexture> result; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 302 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 303 | // The top-left of the mask corresponds to the top-left corner of the bounds. |
| 304 | SkVector clipToMaskOffset = { |
| 305 | SkIntToScalar(-clipSpaceIBounds.fLeft), |
| 306 | SkIntToScalar(-clipSpaceIBounds.fTop) |
| 307 | }; |
| 308 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 309 | if (this->useSWOnlyPath(pipelineBuilder, clipToMaskOffset, elements)) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 310 | // The clip geometry is complex enough that it will be more efficient to create it |
| 311 | // entirely in software |
robertphillips | 588b9ca | 2015-10-04 08:40:31 -0700 | [diff] [blame] | 312 | result.reset(this->createSoftwareClipMask(genID, |
| 313 | initialState, |
| 314 | elements, |
| 315 | clipToMaskOffset, |
| 316 | clipSpaceIBounds)); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 317 | } else { |
robertphillips | 588b9ca | 2015-10-04 08:40:31 -0700 | [diff] [blame] | 318 | result.reset(this->createAlphaClipMask(genID, |
| 319 | initialState, |
| 320 | elements, |
| 321 | clipToMaskOffset, |
| 322 | clipSpaceIBounds)); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 323 | } |
| 324 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 325 | if (result) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 326 | // The mask's top left coord should be pinned to the rounded-out top left corner of |
| 327 | // clipSpace bounds. We determine the mask's position WRT to the render target here. |
| 328 | SkIRect rtSpaceMaskBounds = clipSpaceIBounds; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 329 | rtSpaceMaskBounds.offset(-clip.origin()); |
bsalomon | 0ba8c24 | 2015-10-07 09:20:28 -0700 | [diff] [blame] | 330 | out->fClipCoverageFP.reset(create_fp_for_mask(result, rtSpaceMaskBounds)); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 331 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 332 | return true; |
| 333 | } |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 334 | // 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] | 335 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 336 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 337 | // use the stencil clip if we can't represent the clip as a rectangle. |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 338 | SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 339 | this->createStencilClipMask(rt, |
| 340 | genID, |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 341 | initialState, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 342 | elements, |
| 343 | clipSpaceIBounds, |
| 344 | clipSpaceToStencilSpaceOffset); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 345 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 346 | // This must occur after createStencilClipMask. That function may change the scissor. Also, it |
| 347 | // only guarantees that the stencil mask is correct within the bounds it was passed, so we must |
| 348 | // use both stencil and scissor test to the bounds for the final draw. |
| 349 | SkIRect scissorSpaceIBounds(clipSpaceIBounds); |
| 350 | scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset); |
joshualitt | 77b1307 | 2014-10-27 14:51:01 -0700 | [diff] [blame] | 351 | scissorState->set(scissorSpaceIBounds); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 352 | this->setPipelineBuilderStencil(pipelineBuilder, ars); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 353 | return true; |
| 354 | } |
| 355 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 356 | namespace { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 357 | //////////////////////////////////////////////////////////////////////////////// |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 358 | // Set a coverage drawing XPF on the pipelineBuilder for the given op and invertCoverage mode |
| 359 | void set_coverage_drawing_xpf(SkRegion::Op op, bool invertCoverage, |
| 360 | GrPipelineBuilder* pipelineBuilder) { |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 361 | SkASSERT(op <= SkRegion::kLastOp); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 362 | pipelineBuilder->setCoverageSetOpXPFactory(op, invertCoverage); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 363 | } |
robertphillips@google.com | 72176b2 | 2012-05-23 13:19:12 +0000 | [diff] [blame] | 364 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 365 | |
| 366 | //////////////////////////////////////////////////////////////////////////////// |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 367 | bool GrClipMaskManager::drawElement(GrPipelineBuilder* pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 368 | const SkMatrix& viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 369 | GrTexture* target, |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 370 | const SkClipStack::Element* element, |
| 371 | GrPathRenderer* pr) { |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 372 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 373 | pipelineBuilder->setRenderTarget(target->asRenderTarget()); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 374 | |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 375 | // The color we use to draw does not matter since we will always be using a GrCoverageSetOpXP |
| 376 | // which ignores color. |
| 377 | GrColor color = GrColor_WHITE; |
| 378 | |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 379 | // TODO: Draw rrects directly here. |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 380 | switch (element->getType()) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 381 | case Element::kEmpty_Type: |
| 382 | SkDEBUGFAIL("Should never get here with an empty element."); |
| 383 | break; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 384 | case Element::kRect_Type: |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 385 | // TODO: Do rects directly to the accumulator using a aa-rect GrProcessor that covers |
| 386 | // the entire mask bounds and writes 0 outside the rect. |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 387 | if (element->isAA()) { |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 388 | SkRect devRect = element->getRect(); |
| 389 | viewMatrix.mapRect(&devRect); |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 390 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 391 | fDrawTarget->drawAARect(*pipelineBuilder, color, viewMatrix, |
robertphillips | ea46150 | 2015-05-26 11:38:03 -0700 | [diff] [blame] | 392 | element->getRect(), devRect); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 393 | } else { |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 394 | fDrawTarget->drawNonAARect(*pipelineBuilder, color, viewMatrix, |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 395 | element->getRect()); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 396 | } |
| 397 | return true; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 398 | default: { |
| 399 | SkPath path; |
| 400 | element->asPath(&path); |
jvanverth | b3eb687 | 2014-10-24 07:12:51 -0700 | [diff] [blame] | 401 | path.setIsVolatile(true); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 402 | if (path.isInverseFillType()) { |
| 403 | path.toggleInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 404 | } |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 405 | GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 406 | if (nullptr == pr) { |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 407 | GrPathRendererChain::DrawType type; |
| 408 | type = element->isAA() ? GrPathRendererChain::kColorAntiAlias_DrawType : |
| 409 | GrPathRendererChain::kColor_DrawType; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 410 | pr = this->getContext()->getPathRenderer(fDrawTarget, pipelineBuilder, viewMatrix, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 411 | path, stroke, false, type); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 412 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 413 | if (nullptr == pr) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 414 | return false; |
| 415 | } |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 416 | GrPathRenderer::DrawPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 417 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 418 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 419 | args.fPipelineBuilder = pipelineBuilder; |
| 420 | args.fColor = color; |
| 421 | args.fViewMatrix = &viewMatrix; |
| 422 | args.fPath = &path; |
| 423 | args.fStroke = &stroke; |
| 424 | args.fAntiAlias = element->isAA(); |
| 425 | pr->drawPath(args); |
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 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 432 | bool GrClipMaskManager::canStencilAndDrawElement(GrPipelineBuilder* pipelineBuilder, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 433 | GrTexture* target, |
| 434 | GrPathRenderer** pr, |
| 435 | const SkClipStack::Element* element) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 436 | pipelineBuilder->setRenderTarget(target->asRenderTarget()); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 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 | } |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 448 | GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle); |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 449 | GrPathRendererChain::DrawType type = element->isAA() ? |
| 450 | GrPathRendererChain::kStencilAndColorAntiAlias_DrawType : |
| 451 | GrPathRendererChain::kStencilAndColor_DrawType; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 452 | *pr = this->getContext()->getPathRenderer(fDrawTarget, pipelineBuilder, SkMatrix::I(), path, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 453 | stroke, false, type); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 454 | return SkToBool(*pr); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 458 | void GrClipMaskManager::mergeMask(GrPipelineBuilder* pipelineBuilder, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 459 | GrTexture* dstMask, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 460 | GrTexture* srcMask, |
| 461 | SkRegion::Op op, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 462 | const SkIRect& dstBound, |
| 463 | const SkIRect& srcBound) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 464 | pipelineBuilder->setRenderTarget(dstMask->asRenderTarget()); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 465 | |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 466 | // We want to invert the coverage here |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 467 | set_coverage_drawing_xpf(op, false, pipelineBuilder); |
skia.committer@gmail.com | 72b2e6f | 2012-11-08 02:03:56 +0000 | [diff] [blame] | 468 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 469 | SkMatrix sampleM; |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 470 | sampleM.setIDiv(srcMask->width(), srcMask->height()); |
skia.committer@gmail.com | 956b310 | 2013-07-26 07:00:58 +0000 | [diff] [blame] | 471 | |
bsalomon | ac856c9 | 2015-08-27 06:30:17 -0700 | [diff] [blame] | 472 | pipelineBuilder->addCoverageFragmentProcessor( |
bsalomon | 4a33952 | 2015-10-06 08:40:50 -0700 | [diff] [blame] | 473 | GrTextureDomainEffect::Create(srcMask, |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 474 | sampleM, |
commit-bot@chromium.org | 907fbd5 | 2013-12-09 17:03:02 +0000 | [diff] [blame] | 475 | GrTextureDomain::MakeTexelDomain(srcMask, srcBound), |
| 476 | GrTextureDomain::kDecal_Mode, |
humper@google.com | b86add1 | 2013-07-25 18:49:07 +0000 | [diff] [blame] | 477 | GrTextureParams::kNone_FilterMode))->unref(); |
joshualitt | 73bb456 | 2015-03-25 07:16:21 -0700 | [diff] [blame] | 478 | |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 479 | // The color passed in here does not matter since the coverageSetOpXP won't read it. |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 480 | fDrawTarget->drawNonAARect(*pipelineBuilder, |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 481 | GrColor_WHITE, |
| 482 | SkMatrix::I(), |
| 483 | SkRect::Make(dstBound)); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 484 | } |
| 485 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 486 | GrTexture* GrClipMaskManager::createTempMask(int width, int height) { |
bsalomon | f2703d8 | 2014-10-28 14:33:06 -0700 | [diff] [blame] | 487 | GrSurfaceDesc desc; |
bsalomon | 3f490a0 | 2014-12-18 06:20:52 -0800 | [diff] [blame] | 488 | desc.fFlags = kRenderTarget_GrSurfaceFlag; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 489 | desc.fWidth = width; |
| 490 | desc.fHeight = height; |
bsalomon | 7622863 | 2015-05-29 08:02:10 -0700 | [diff] [blame] | 491 | if (this->getContext()->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
bsalomon | 51d1f7e | 2014-12-22 08:40:49 -0800 | [diff] [blame] | 492 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 493 | } else { |
| 494 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 495 | } |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 496 | |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 497 | return this->getContext()->textureProvider()->createApproxTexture(desc); |
robertphillips@google.com | 6d62df4 | 2012-05-07 18:07:36 +0000 | [diff] [blame] | 498 | } |
| 499 | |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 500 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 501 | // Create a 8-bit clip mask in alpha |
| 502 | |
| 503 | static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) { |
| 504 | static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| 505 | GrUniqueKey::Builder builder(key, kDomain, 3); |
| 506 | builder[0] = clipGenID; |
| 507 | builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16); |
| 508 | builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16); |
| 509 | } |
| 510 | |
| 511 | GrTexture* GrClipMaskManager::createCachedMask(int width, int height, const GrUniqueKey& key, |
| 512 | bool renderTarget) { |
| 513 | GrSurfaceDesc desc; |
| 514 | desc.fWidth = width; |
| 515 | desc.fHeight = height; |
| 516 | desc.fFlags = renderTarget ? kRenderTarget_GrSurfaceFlag : kNone_GrSurfaceFlags; |
| 517 | if (!renderTarget || fDrawTarget->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) { |
| 518 | desc.fConfig = kAlpha_8_GrPixelConfig; |
| 519 | } else { |
| 520 | desc.fConfig = kRGBA_8888_GrPixelConfig; |
| 521 | } |
| 522 | |
| 523 | GrTexture* texture = fDrawTarget->cmmAccess().resourceProvider()->createApproxTexture(desc, 0); |
| 524 | if (!texture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 525 | return nullptr; |
robertphillips@google.com | 8fff356 | 2012-05-11 12:53:50 +0000 | [diff] [blame] | 526 | } |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 527 | texture->resourcePriv().setUniqueKey(key); |
| 528 | return texture; |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 529 | } |
| 530 | |
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, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 534 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 535 | const SkIRect& clipSpaceIBounds) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 536 | GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider(); |
| 537 | GrUniqueKey key; |
| 538 | GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key); |
| 539 | if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 540 | return texture; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 541 | } |
| 542 | |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 543 | SkAutoTUnref<GrTexture> texture(this->createCachedMask( |
| 544 | clipSpaceIBounds.width(), clipSpaceIBounds.height(), key, true)); |
| 545 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 546 | // There's no texture in the cache. Let's try to allocate it then. |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 547 | if (!texture) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 548 | return nullptr; |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 549 | } |
| 550 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 551 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 552 | // space. |
| 553 | SkMatrix translate; |
| 554 | translate.setTranslate(clipToMaskOffset); |
| 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 | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 560 | // The scratch texture that we are drawing into can be substantially larger than the mask. Only |
| 561 | // clear the part that we care about. |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 562 | fDrawTarget->clear(&maskSpaceIBounds, |
joshualitt | 329bf48 | 2014-10-29 12:31:28 -0700 | [diff] [blame] | 563 | GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000, |
| 564 | true, |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 565 | texture->asRenderTarget()); |
skia.committer@gmail.com | d9f7503 | 2012-11-09 02:01:24 +0000 | [diff] [blame] | 566 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 567 | // When we use the stencil in the below loop it is important to have this clip installed. |
| 568 | // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first |
| 569 | // pass must not set values outside of this bounds or stencil values outside the rect won't be |
| 570 | // cleared. |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 571 | GrClip clip(maskSpaceIBounds); |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 572 | SkAutoTUnref<GrTexture> temp; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 573 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 574 | // walk through each clip element and perform its set op |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 575 | for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 576 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 577 | SkRegion::Op op = element->getOp(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 578 | bool invert = element->isInverseFilled(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 579 | if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 580 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 581 | |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 582 | pipelineBuilder.setClip(clip); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 583 | GrPathRenderer* pr = nullptr; |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 584 | bool useTemp = !this->canStencilAndDrawElement(&pipelineBuilder, texture, &pr, element); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 585 | GrTexture* dst; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 586 | // 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] | 587 | // mask buffer can be substantially larger than the actually clip stack element. We |
| 588 | // 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] | 589 | // the accumulator. |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 590 | SkIRect maskSpaceElementIBounds; |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 591 | |
| 592 | if (useTemp) { |
| 593 | if (invert) { |
| 594 | maskSpaceElementIBounds = maskSpaceIBounds; |
| 595 | } else { |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 596 | SkRect elementBounds = element->getBounds(); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 597 | elementBounds.offset(clipToMaskOffset); |
| 598 | elementBounds.roundOut(&maskSpaceElementIBounds); |
| 599 | } |
| 600 | |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 601 | if (!temp) { |
| 602 | temp.reset(this->createTempMask(maskSpaceIBounds.fRight, |
| 603 | maskSpaceIBounds.fBottom)); |
| 604 | if (!temp) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 605 | texture->resourcePriv().removeUniqueKey(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 606 | return nullptr; |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 607 | } |
skia.committer@gmail.com | a7aedfe | 2012-12-15 02:03:10 +0000 | [diff] [blame] | 608 | } |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 609 | dst = temp; |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 610 | // clear the temp target and set blend to replace |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 611 | fDrawTarget->clear(&maskSpaceElementIBounds, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 612 | invert ? 0xffffffff : 0x00000000, |
| 613 | true, |
| 614 | dst->asRenderTarget()); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 615 | set_coverage_drawing_xpf(SkRegion::kReplace_Op, invert, &pipelineBuilder); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 616 | } else { |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 617 | // draw directly into the result with the stencil set to make the pixels affected |
| 618 | // by the clip shape be non-zero. |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 619 | dst = texture; |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 620 | GR_STATIC_CONST_SAME_STENCIL(kStencilInElement, |
| 621 | kReplace_StencilOp, |
| 622 | kReplace_StencilOp, |
| 623 | kAlways_StencilFunc, |
| 624 | 0xffff, |
| 625 | 0xffff, |
| 626 | 0xffff); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 627 | pipelineBuilder.setStencil(kStencilInElement); |
| 628 | set_coverage_drawing_xpf(op, invert, &pipelineBuilder); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 629 | } |
bsalomon@google.com | 7b7cdd1 | 2012-11-07 16:17:24 +0000 | [diff] [blame] | 630 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 631 | if (!this->drawElement(&pipelineBuilder, translate, dst, element, pr)) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 632 | texture->resourcePriv().removeUniqueKey(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 633 | return nullptr; |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 634 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 635 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 636 | if (useTemp) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 637 | GrPipelineBuilder backgroundPipelineBuilder; |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 638 | backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget()); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 639 | |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 640 | // Now draw into the accumulator using the real operation and the temp buffer as a |
| 641 | // texture |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 642 | this->mergeMask(&backgroundPipelineBuilder, |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 643 | texture, |
bsalomon | 427cf28 | 2014-10-16 13:41:43 -0700 | [diff] [blame] | 644 | temp, |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 645 | op, |
| 646 | maskSpaceIBounds, |
| 647 | maskSpaceElementIBounds); |
| 648 | } else { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 649 | GrPipelineBuilder backgroundPipelineBuilder; |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 650 | backgroundPipelineBuilder.setRenderTarget(texture->asRenderTarget()); |
joshualitt | 8fc6c2d | 2014-12-22 15:27:05 -0800 | [diff] [blame] | 651 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 652 | set_coverage_drawing_xpf(op, !invert, &backgroundPipelineBuilder); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 653 | // Draw to the exterior pixels (those with a zero stencil value). |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 654 | GR_STATIC_CONST_SAME_STENCIL(kDrawOutsideElement, |
| 655 | kZero_StencilOp, |
| 656 | kZero_StencilOp, |
| 657 | kEqual_StencilFunc, |
| 658 | 0xffff, |
| 659 | 0x0000, |
| 660 | 0xffff); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 661 | backgroundPipelineBuilder.setStencil(kDrawOutsideElement); |
joshualitt | 73bb456 | 2015-03-25 07:16:21 -0700 | [diff] [blame] | 662 | |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 663 | // The color passed in here does not matter since the coverageSetOpXP won't read it. |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 664 | fDrawTarget->drawNonAARect(backgroundPipelineBuilder, GrColor_WHITE, translate, |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 665 | clipSpaceIBounds); |
bsalomon@google.com | b68addd | 2012-12-14 13:36:53 +0000 | [diff] [blame] | 666 | } |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 667 | } else { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 668 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 669 | |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 670 | // all the remaining ops can just be directly draw into the accumulation buffer |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 671 | set_coverage_drawing_xpf(op, false, &pipelineBuilder); |
egdaniel | 8750924 | 2014-12-17 13:37:13 -0800 | [diff] [blame] | 672 | // The color passed in here does not matter since the coverageSetOpXP won't read it. |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 673 | this->drawElement(&pipelineBuilder, translate, texture, element); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 677 | return texture.detach(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | //////////////////////////////////////////////////////////////////////////////// |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 681 | // 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] | 682 | // (as opposed to canvas) coordinates |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 683 | bool GrClipMaskManager::createStencilClipMask(GrRenderTarget* rt, |
| 684 | int32_t elementsGenID, |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 685 | GrReducedClip::InitialState initialState, |
| 686 | const GrReducedClip::ElementList& elements, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 687 | const SkIRect& clipSpaceIBounds, |
| 688 | const SkIPoint& clipSpaceToStencilOffset) { |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 689 | SkASSERT(rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 690 | |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 691 | GrStencilAttachment* stencilAttachment = |
| 692 | fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 693 | if (nullptr == stencilAttachment) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 694 | return false; |
| 695 | } |
| 696 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 697 | if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) { |
| 698 | stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset); |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 699 | // Set the matrix so that rendered clip elements are transformed from clip to stencil space. |
| 700 | SkVector translate = { |
| 701 | SkIntToScalar(clipSpaceToStencilOffset.fX), |
| 702 | SkIntToScalar(clipSpaceToStencilOffset.fY) |
| 703 | }; |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 704 | SkMatrix viewMatrix; |
| 705 | viewMatrix.setTranslate(translate); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 706 | |
bsalomon@google.com | 9f13174 | 2012-12-13 20:43:56 +0000 | [diff] [blame] | 707 | // We set the current clip to the bounds so that our recursive draws are scissored to them. |
| 708 | SkIRect stencilSpaceIBounds(clipSpaceIBounds); |
| 709 | stencilSpaceIBounds.offset(clipSpaceToStencilOffset); |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 710 | GrClip clip(stencilSpaceIBounds); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 711 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 712 | int clipBit = stencilAttachment->bits(); |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 713 | SkASSERT((clipBit <= 16) && "Ganesh only handles 16b or smaller stencil buffers"); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 714 | clipBit = (1 << (clipBit-1)); |
| 715 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 716 | fDrawTarget->cmmAccess().clearStencilClip(stencilSpaceIBounds, |
| 717 | GrReducedClip::kAllIn_InitialState == initialState, rt); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 718 | |
| 719 | // walk through each clip element and perform its set op |
| 720 | // with the existing clip. |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 721 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 722 | const Element* element = iter.get(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 723 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 724 | GrPipelineBuilder pipelineBuilder; |
joshualitt | 44701df | 2015-02-23 14:44:57 -0800 | [diff] [blame] | 725 | pipelineBuilder.setClip(clip); |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 726 | pipelineBuilder.setRenderTarget(rt); |
egdaniel | 080e673 | 2014-12-22 07:35:52 -0800 | [diff] [blame] | 727 | |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 728 | pipelineBuilder.setDisableColorXPFactory(); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 729 | |
| 730 | // if the target is MSAA then we want MSAA enabled when the clip is soft |
vbuzinov | 3e77ba9 | 2015-09-30 23:02:06 -0700 | [diff] [blame] | 731 | if (rt->isStencilBufferMultisampled()) { |
bsalomon | d79c549 | 2015-04-27 10:07:04 -0700 | [diff] [blame] | 732 | pipelineBuilder.setState(GrPipelineBuilder::kHWAntialias_Flag, element->isAA()); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 733 | } |
| 734 | |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 735 | bool fillInverted = false; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 736 | // enabled at bottom of loop |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 737 | fClipMode = kIgnoreClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 738 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 739 | // This will be used to determine whether the clip shape can be rendered into the |
| 740 | // stencil with arbitrary stencil settings. |
| 741 | GrPathRenderer::StencilSupport stencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 742 | |
kkinnunen | 1899651 | 2015-04-26 23:18:49 -0700 | [diff] [blame] | 743 | GrStrokeInfo stroke(SkStrokeRec::kFill_InitStyle); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 744 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 745 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 746 | GrPathRenderer* pr = nullptr; |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 747 | SkPath clipPath; |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 748 | if (Element::kRect_Type == element->getType()) { |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 749 | stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 750 | fillInverted = false; |
tomhudson@google.com | 8afae61 | 2012-08-14 15:03:35 +0000 | [diff] [blame] | 751 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 752 | element->asPath(&clipPath); |
| 753 | fillInverted = clipPath.isInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 754 | if (fillInverted) { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 755 | clipPath.toggleInverseFillType(); |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 756 | } |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 757 | pr = this->getContext()->getPathRenderer(fDrawTarget, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 758 | &pipelineBuilder, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 759 | viewMatrix, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 760 | clipPath, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 761 | stroke, |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 762 | false, |
| 763 | GrPathRendererChain::kStencilOnly_DrawType, |
robertphillips@google.com | e79f320 | 2014-02-11 16:30:21 +0000 | [diff] [blame] | 764 | &stencilSupport); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 765 | if (nullptr == pr) { |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 766 | return false; |
| 767 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 768 | } |
| 769 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 770 | int passes; |
| 771 | GrStencilSettings stencilSettings[GrStencilSettings::kMaxStencilClipPasses]; |
| 772 | |
bsalomon@google.com | 45a15f5 | 2012-12-10 19:10:17 +0000 | [diff] [blame] | 773 | bool canRenderDirectToStencil = |
| 774 | GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 775 | bool canDrawDirectToClip; // Given the renderer, the element, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 776 | // fill rule, and set operation can |
| 777 | // we render the element directly to |
| 778 | // stencil bit used for clipping. |
| 779 | canDrawDirectToClip = GrStencilSettings::GetClipPasses(op, |
| 780 | canRenderDirectToStencil, |
| 781 | clipBit, |
| 782 | fillInverted, |
| 783 | &passes, |
| 784 | stencilSettings); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 785 | |
| 786 | // draw the element to the client stencil bits if necessary |
| 787 | if (!canDrawDirectToClip) { |
| 788 | GR_STATIC_CONST_SAME_STENCIL(gDrawToStencil, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 789 | kIncClamp_StencilOp, |
| 790 | kIncClamp_StencilOp, |
| 791 | kAlways_StencilFunc, |
| 792 | 0xffff, |
| 793 | 0x0000, |
| 794 | 0xffff); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 795 | if (Element::kRect_Type == element->getType()) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 796 | *pipelineBuilder.stencil() = gDrawToStencil; |
joshualitt | 73bb456 | 2015-03-25 07:16:21 -0700 | [diff] [blame] | 797 | |
| 798 | // We need this AGP until everything is in GrBatch |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 799 | fDrawTarget->drawNonAARect(pipelineBuilder, |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 800 | GrColor_WHITE, |
| 801 | viewMatrix, |
| 802 | element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 803 | } else { |
commit-bot@chromium.org | e5b2af9 | 2014-02-16 13:25:24 +0000 | [diff] [blame] | 804 | if (!clipPath.isEmpty()) { |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 805 | if (canRenderDirectToStencil) { |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 806 | *pipelineBuilder.stencil() = gDrawToStencil; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 807 | |
| 808 | GrPathRenderer::DrawPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 809 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 810 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 811 | args.fPipelineBuilder = &pipelineBuilder; |
| 812 | args.fColor = GrColor_WHITE; |
| 813 | args.fViewMatrix = &viewMatrix; |
| 814 | args.fPath = &clipPath; |
| 815 | args.fStroke = &stroke; |
| 816 | args.fAntiAlias = false; |
| 817 | pr->drawPath(args); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 818 | } else { |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 819 | GrPathRenderer::StencilPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 820 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 821 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 822 | args.fPipelineBuilder = &pipelineBuilder; |
| 823 | args.fViewMatrix = &viewMatrix; |
| 824 | args.fPath = &clipPath; |
| 825 | args.fStroke = &stroke; |
| 826 | pr->stencilPath(args); |
commit-bot@chromium.org | 19dd017 | 2013-08-05 13:28:55 +0000 | [diff] [blame] | 827 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | // now we modify the clip bit by rendering either the clip |
| 833 | // element directly or a bounding rect of the entire clip. |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 834 | fClipMode = kModifyClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 835 | for (int p = 0; p < passes; ++p) { |
joshualitt | 4f6dc52 | 2015-07-09 12:17:44 -0700 | [diff] [blame] | 836 | *pipelineBuilder.stencil() = stencilSettings[p]; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 837 | |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 838 | if (canDrawDirectToClip) { |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 839 | if (Element::kRect_Type == element->getType()) { |
joshualitt | 73bb456 | 2015-03-25 07:16:21 -0700 | [diff] [blame] | 840 | // We need this AGP until everything is in GrBatch |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 841 | fDrawTarget->drawNonAARect(pipelineBuilder, |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 842 | GrColor_WHITE, |
| 843 | viewMatrix, |
| 844 | element->getRect()); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 845 | } else { |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 846 | GrPathRenderer::DrawPathArgs args; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 847 | args.fTarget = fDrawTarget; |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 848 | args.fResourceProvider = this->getContext()->resourceProvider(); |
| 849 | args.fPipelineBuilder = &pipelineBuilder; |
| 850 | args.fColor = GrColor_WHITE; |
| 851 | args.fViewMatrix = &viewMatrix; |
| 852 | args.fPath = &clipPath; |
| 853 | args.fStroke = &stroke; |
| 854 | args.fAntiAlias = false; |
| 855 | pr->drawPath(args); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 856 | } |
| 857 | } else { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 858 | // The view matrix is setup to do clip space -> stencil space translation, so |
| 859 | // draw rect in clip space. |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 860 | fDrawTarget->drawNonAARect(pipelineBuilder, |
joshualitt | d2b23e0 | 2015-08-21 10:53:34 -0700 | [diff] [blame] | 861 | GrColor_WHITE, |
| 862 | viewMatrix, |
| 863 | SkRect::Make(clipSpaceIBounds)); |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 864 | } |
| 865 | } |
| 866 | } |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 867 | } |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 868 | fClipMode = kRespectClip_StencilClipMode; |
robertphillips@google.com | 1e945b7 | 2012-04-16 18:03:03 +0000 | [diff] [blame] | 869 | return true; |
| 870 | } |
| 871 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 872 | // mapping of clip-respecting stencil funcs to normal stencil funcs |
| 873 | // mapping depends on whether stencil-clipping is in effect. |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 874 | static const GrStencilFunc |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 875 | gSpecialToBasicStencilFunc[2][kClipStencilFuncCount] = { |
| 876 | {// Stencil-Clipping is DISABLED, we are effectively always inside the clip |
| 877 | // In the Clip Funcs |
| 878 | kAlways_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 879 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 880 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 881 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 882 | // Special in the clip func that forces user's ref to be 0. |
| 883 | kNotEqual_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 884 | // make ref 0 and do normal nequal. |
| 885 | }, |
| 886 | {// Stencil-Clipping is ENABLED |
| 887 | // In the Clip Funcs |
| 888 | kEqual_StencilFunc, // kAlwaysIfInClip_StencilFunc |
| 889 | // eq stencil clip bit, mask |
| 890 | // out user bits. |
| 891 | |
| 892 | kEqual_StencilFunc, // kEqualIfInClip_StencilFunc |
| 893 | // add stencil bit to mask and ref |
| 894 | |
| 895 | kLess_StencilFunc, // kLessIfInClip_StencilFunc |
| 896 | kLEqual_StencilFunc, // kLEqualIfInClip_StencilFunc |
| 897 | // for both of these we can add |
| 898 | // the clip bit to the mask and |
| 899 | // ref and compare as normal |
| 900 | // Special in the clip func that forces user's ref to be 0. |
| 901 | kLess_StencilFunc, // kNonZeroIfInClip_StencilFunc |
| 902 | // make ref have only the clip bit set |
| 903 | // and make comparison be less |
| 904 | // 10..0 < 1..user_bits.. |
| 905 | } |
| 906 | }; |
| 907 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 908 | namespace { |
| 909 | // Sets the settings to clip against the stencil buffer clip while ignoring the |
| 910 | // client bits. |
| 911 | const GrStencilSettings& basic_apply_stencil_clip_settings() { |
| 912 | // stencil settings to use when clip is in stencil |
| 913 | GR_STATIC_CONST_SAME_STENCIL_STRUCT(gSettings, |
| 914 | kKeep_StencilOp, |
| 915 | kKeep_StencilOp, |
| 916 | kAlwaysIfInClip_StencilFunc, |
| 917 | 0x0000, |
| 918 | 0x0000, |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 919 | 0x0000); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 920 | return *GR_CONST_STENCIL_SETTINGS_PTR_FROM_STRUCT_PTR(&gSettings); |
| 921 | } |
| 922 | } |
| 923 | |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 924 | void GrClipMaskManager::setPipelineBuilderStencil(const GrPipelineBuilder& pipelineBuilder, |
egdaniel | 8dd688b | 2015-01-22 10:16:09 -0800 | [diff] [blame] | 925 | GrPipelineBuilder::AutoRestoreStencil* ars) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 926 | // We make two copies of the StencilSettings here (except in the early |
| 927 | // exit scenario. One copy from draw state to the stack var. Then another |
| 928 | // from the stack var to the gpu. We could make this class hold a ptr to |
| 929 | // GrGpu's fStencilSettings and eliminate the stack copy here. |
| 930 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 931 | // use stencil for clipping if clipping is enabled and the clip |
| 932 | // has been written into the stencil. |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 933 | GrStencilSettings settings; |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 934 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 935 | // The GrGpu client may not be using the stencil buffer but we may need to |
| 936 | // enable it in order to respect a stencil clip. |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 937 | if (pipelineBuilder.getStencil().isDisabled()) { |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 938 | if (GrClipMaskManager::kRespectClip_StencilClipMode == fClipMode) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 939 | settings = basic_apply_stencil_clip_settings(); |
| 940 | } else { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 941 | return; |
| 942 | } |
| 943 | } else { |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 944 | settings = pipelineBuilder.getStencil(); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 945 | } |
| 946 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 947 | int stencilBits = 0; |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 948 | GrRenderTarget* rt = pipelineBuilder.getRenderTarget(); |
egdaniel | ec00d94 | 2015-09-14 12:56:10 -0700 | [diff] [blame] | 949 | GrStencilAttachment* stencilAttachment = |
| 950 | fDrawTarget->cmmAccess().resourceProvider()->attachStencilAttachment(rt); |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 951 | if (stencilAttachment) { |
| 952 | stencilBits = stencilAttachment->bits(); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 953 | } |
| 954 | |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 955 | SkASSERT(fDrawTarget->caps()->stencilWrapOpsSupport() || !settings.usesWrapOp()); |
| 956 | SkASSERT(fDrawTarget->caps()->twoSidedStencilSupport() || !settings.isTwoSided()); |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 957 | this->adjustStencilParams(&settings, fClipMode, stencilBits); |
joshualitt | 5e6ba21 | 2015-07-13 07:35:05 -0700 | [diff] [blame] | 958 | ars->set(&pipelineBuilder); |
| 959 | ars->setStencil(settings); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | void GrClipMaskManager::adjustStencilParams(GrStencilSettings* settings, |
| 963 | StencilClipMode mode, |
| 964 | int stencilBitCnt) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 965 | SkASSERT(stencilBitCnt > 0); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 966 | |
| 967 | if (kModifyClip_StencilClipMode == mode) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 968 | // We assume that this clip manager itself is drawing to the GrGpu and |
| 969 | // has already setup the correct values. |
| 970 | return; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 971 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 972 | |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 973 | unsigned int clipBit = (1 << (stencilBitCnt - 1)); |
| 974 | unsigned int userBits = clipBit - 1; |
| 975 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 976 | GrStencilSettings::Face face = GrStencilSettings::kFront_Face; |
bsalomon | b3b9aec | 2015-09-10 11:16:35 -0700 | [diff] [blame] | 977 | bool twoSided = fDrawTarget->caps()->twoSidedStencilSupport(); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 978 | |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 979 | bool finished = false; |
| 980 | while (!finished) { |
| 981 | GrStencilFunc func = settings->func(face); |
| 982 | uint16_t writeMask = settings->writeMask(face); |
| 983 | uint16_t funcMask = settings->funcMask(face); |
| 984 | uint16_t funcRef = settings->funcRef(face); |
| 985 | |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 986 | SkASSERT((unsigned) func < kStencilFuncCount); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 987 | |
| 988 | writeMask &= userBits; |
| 989 | |
| 990 | if (func >= kBasicStencilFuncCount) { |
| 991 | int respectClip = kRespectClip_StencilClipMode == mode; |
| 992 | if (respectClip) { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 993 | switch (func) { |
| 994 | case kAlwaysIfInClip_StencilFunc: |
| 995 | funcMask = clipBit; |
| 996 | funcRef = clipBit; |
| 997 | break; |
| 998 | case kEqualIfInClip_StencilFunc: |
| 999 | case kLessIfInClip_StencilFunc: |
| 1000 | case kLEqualIfInClip_StencilFunc: |
| 1001 | funcMask = (funcMask & userBits) | clipBit; |
| 1002 | funcRef = (funcRef & userBits) | clipBit; |
| 1003 | break; |
| 1004 | case kNonZeroIfInClip_StencilFunc: |
| 1005 | funcMask = (funcMask & userBits) | clipBit; |
| 1006 | funcRef = clipBit; |
| 1007 | break; |
| 1008 | default: |
commit-bot@chromium.org | 88cb22b | 2014-04-30 14:17:00 +0000 | [diff] [blame] | 1009 | SkFAIL("Unknown stencil func"); |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1010 | } |
| 1011 | } else { |
| 1012 | funcMask &= userBits; |
| 1013 | funcRef &= userBits; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1014 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1015 | const GrStencilFunc* table = |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1016 | gSpecialToBasicStencilFunc[respectClip]; |
| 1017 | func = table[func - kBasicStencilFuncCount]; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 1018 | SkASSERT(func >= 0 && func < kBasicStencilFuncCount); |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1019 | } else { |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1020 | funcMask &= userBits; |
| 1021 | funcRef &= userBits; |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1022 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1023 | |
| 1024 | settings->setFunc(face, func); |
| 1025 | settings->setWriteMask(face, writeMask); |
| 1026 | settings->setFuncMask(face, funcMask); |
| 1027 | settings->setFuncRef(face, funcRef); |
| 1028 | |
| 1029 | if (GrStencilSettings::kFront_Face == face) { |
| 1030 | face = GrStencilSettings::kBack_Face; |
| 1031 | finished = !twoSided; |
| 1032 | } else { |
| 1033 | finished = true; |
| 1034 | } |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1035 | } |
bsalomon@google.com | a320194 | 2012-06-21 19:58:20 +0000 | [diff] [blame] | 1036 | if (!twoSided) { |
| 1037 | settings->copyFrontSettingsToBack(); |
| 1038 | } |
bsalomon@google.com | 411dad0 | 2012-06-05 20:24:20 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | //////////////////////////////////////////////////////////////////////////////// |
commit-bot@chromium.org | d3e5842 | 2013-11-05 15:03:08 +0000 | [diff] [blame] | 1042 | GrTexture* GrClipMaskManager::createSoftwareClipMask(int32_t elementsGenID, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1043 | GrReducedClip::InitialState initialState, |
| 1044 | const GrReducedClip::ElementList& elements, |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1045 | const SkVector& clipToMaskOffset, |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1046 | const SkIRect& clipSpaceIBounds) { |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 1047 | GrUniqueKey key; |
| 1048 | GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key); |
| 1049 | GrResourceProvider* resourceProvider = fDrawTarget->cmmAccess().resourceProvider(); |
| 1050 | if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) { |
| 1051 | return texture; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1054 | // The mask texture may be larger than necessary. We round out the clip space bounds and pin |
| 1055 | // the top left corner of the resulting rect to the top left of the texture. |
| 1056 | SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height()); |
| 1057 | |
robertphillips@google.com | 2c75681 | 2012-05-22 20:28:23 +0000 | [diff] [blame] | 1058 | GrSWMaskHelper helper(this->getContext()); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1059 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1060 | // Set the matrix so that rendered clip elements are transformed to mask space from clip |
| 1061 | // space. |
| 1062 | SkMatrix translate; |
| 1063 | translate.setTranslate(clipToMaskOffset); |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1064 | |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 1065 | helper.init(maskSpaceIBounds, &translate, false); |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 1066 | helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00); |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 1067 | SkStrokeRec stroke(SkStrokeRec::kFill_InitStyle); |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 1068 | |
tfarina | bf54e49 | 2014-10-23 17:47:18 -0700 | [diff] [blame] | 1069 | for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) { |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1070 | const Element* element = iter.get(); |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1071 | SkRegion::Op op = element->getOp(); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1072 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1073 | if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) { |
| 1074 | // Intersect and reverse difference require modifying pixels outside of the geometry |
| 1075 | // that is being "drawn". In both cases we erase all the pixels outside of the geometry |
| 1076 | // but leave the pixels inside the geometry alone. For reverse difference we invert all |
| 1077 | // the pixels before clearing the ones outside the geometry. |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1078 | if (SkRegion::kReverseDifference_Op == op) { |
reed@google.com | 4469938 | 2013-10-31 17:28:30 +0000 | [diff] [blame] | 1079 | SkRect temp = SkRect::Make(clipSpaceIBounds); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1080 | // invert the entire scene |
robertphillips@google.com | 366f1c6 | 2012-06-29 21:38:47 +0000 | [diff] [blame] | 1081 | helper.draw(temp, SkRegion::kXOR_Op, false, 0xFF); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1082 | } |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 1083 | SkPath clipPath; |
| 1084 | element->asPath(&clipPath); |
| 1085 | clipPath.toggleInverseFillType(); |
| 1086 | helper.draw(clipPath, stroke, SkRegion::kReplace_Op, element->isAA(), 0x00); |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 1087 | continue; |
| 1088 | } |
| 1089 | |
| 1090 | // The other ops (union, xor, diff) only affect pixels inside |
| 1091 | // the geometry so they can just be drawn normally |
bsalomon@google.com | 8182fa0 | 2012-12-04 14:06:06 +0000 | [diff] [blame] | 1092 | if (Element::kRect_Type == element->getType()) { |
| 1093 | helper.draw(element->getRect(), op, element->isAA(), 0xFF); |
| 1094 | } else { |
commit-bot@chromium.org | 5c05639 | 2014-02-17 19:50:02 +0000 | [diff] [blame] | 1095 | SkPath path; |
| 1096 | element->asPath(&path); |
| 1097 | helper.draw(path, stroke, op, element->isAA(), 0xFF); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 1101 | // Allocate clip mask texture |
bsalomon | 473addf | 2015-10-02 07:49:05 -0700 | [diff] [blame] | 1102 | GrTexture* result = this->createCachedMask(clipSpaceIBounds.width(), clipSpaceIBounds.height(), |
| 1103 | key, false); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1104 | if (nullptr == result) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 1105 | return nullptr; |
krajcevski | ad1dc58 | 2014-06-10 15:06:47 -0700 | [diff] [blame] | 1106 | } |
robertphillips@google.com | d92cf2e | 2013-07-19 18:13:02 +0000 | [diff] [blame] | 1107 | helper.toTexture(result); |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1108 | |
bsalomon@google.com | 4c2443e | 2012-12-06 20:58:57 +0000 | [diff] [blame] | 1109 | return result; |
robertphillips@google.com | 6b70a7b | 2012-05-11 15:32:48 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
robertphillips@google.com | f294b77 | 2012-04-27 14:29:26 +0000 | [diff] [blame] | 1112 | //////////////////////////////////////////////////////////////////////////////// |
bsalomon@google.com | 6e4e650 | 2013-02-25 20:12:45 +0000 | [diff] [blame] | 1113 | |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1114 | void GrClipMaskManager::adjustPathStencilParams(const GrStencilAttachment* stencilAttachment, |
joshualitt | 9853cce | 2014-11-17 14:22:48 -0800 | [diff] [blame] | 1115 | GrStencilSettings* settings) { |
egdaniel | 8dc7c3a | 2015-04-16 11:22:42 -0700 | [diff] [blame] | 1116 | if (stencilAttachment) { |
| 1117 | int stencilBits = stencilAttachment->bits(); |
joshualitt | 7a6184f | 2014-10-29 18:29:27 -0700 | [diff] [blame] | 1118 | this->adjustStencilParams(settings, fClipMode, stencilBits); |
commit-bot@chromium.org | c4dc0ad | 2013-10-09 14:11:33 +0000 | [diff] [blame] | 1119 | } |
| 1120 | } |