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