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