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