blob: 6cb216ee0c37dbf00f462af008df8ed6d25b1d60 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
csmartdaltonc6f411e2016-08-05 22:32:12 -07002 * Copyright 2016 Google Inc.
robertphillips@google.com1e945b72012-04-16 18:03:03 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
csmartdaltonc6f411e2016-08-05 22:32:12 -07008#include "GrClipStackClip.h"
9
robertphillips68737822015-10-29 12:12:21 -070010#include "GrDrawingManager.h"
robertphillips391395d2016-03-02 09:26:36 -080011#include "GrDrawContextPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070012#include "GrGpuResourcePriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070013#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000014#include "GrSWMaskHelper.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080015#include "effects/GrConvexPolyEffect.h"
16#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080017#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000018
bsalomon@google.com8182fa02012-12-04 14:06:06 +000019typedef SkClipStack::Element Element;
csmartdaltoncbecb082016-07-22 08:59:08 -070020typedef GrReducedClip::InitialState InitialState;
csmartdalton77f2fae2016-08-08 09:55:06 -070021typedef GrReducedClip::ElementList ElementList;
bsalomon@google.com51a62862012-11-26 21:19:43 +000022
robertphillips976f5f02016-06-03 10:59:20 -070023static const int kMaxAnalyticElements = 4;
24
csmartdaltonc6f411e2016-08-05 22:32:12 -070025bool GrClipStackClip::quickContains(const SkRect& rect) const {
26 if (!fStack) {
27 return true;
28 }
29 return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()),
30 SkIntToScalar(fOrigin.y())));
31}
32
33void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
34 bool* isIntersectionOfRects) const {
35 if (!fStack) {
36 devResult->setXYWH(0, 0, width, height);
37 if (isIntersectionOfRects) {
38 *isIntersectionOfRects = true;
39 }
40 return;
41 }
42 SkRect devBounds;
43 fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
44 isIntersectionOfRects);
45 devBounds.roundOut(devResult);
46}
47
bsalomon@google.com51a62862012-11-26 21:19:43 +000048////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000049// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000050// stage matrix this also alters the vertex layout
bungeman06ca8ec2016-06-09 08:01:03 -070051static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
52 const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000053 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080054 // We use device coords to compute the texture coordinates. We set our matrix to be a
55 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000056 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000057 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000058 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000059
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000060 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bungeman06ca8ec2016-06-09 08:01:03 -070061 return sk_sp<GrFragmentProcessor>(GrTextureDomainEffect::Make(
robertphillips5f2fa472016-05-19 11:36:25 -070062 result,
brianosman54f30c12016-07-18 10:53:52 -070063 nullptr,
bsalomon0ba8c242015-10-07 09:20:28 -070064 mat,
65 GrTextureDomain::MakeTexelDomain(result, domainTexels),
66 GrTextureDomain::kDecal_Mode,
67 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -070068 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000069}
70
robertphillips3f7357f2015-10-27 07:17:33 -070071// Does the path in 'element' require SW rendering? If so, return true (and,
72// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
73// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070074bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
75 bool hasUserStencilSettings,
76 const GrDrawContext* drawContext,
77 const SkMatrix& viewMatrix,
78 const Element* element,
79 GrPathRenderer** prOut,
80 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070081 if (Element::kRect_Type == element->getType()) {
82 // rects can always be drawn directly w/o using the software path
83 // TODO: skip rrects once we're drawing them directly.
84 if (prOut) {
85 *prOut = nullptr;
86 }
87 return false;
88 } else {
89 // We shouldn't get here with an empty clip element.
90 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070091
robertphillips3f7357f2015-10-27 07:17:33 -070092 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
93 SkPath path;
94 element->asPath(&path);
95 if (path.isInverseFillType()) {
96 path.toggleInverseFillType();
97 }
halcanary9d524f22016-03-29 09:03:52 -070098
robertphillips3f7357f2015-10-27 07:17:33 -070099 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -0700100
robertphillips423e3372015-10-27 09:23:38 -0700101 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700102 type = element->isAA()
103 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
104 : GrPathRendererChain::kStencilAndColor_DrawType;
105 } else {
106 type = element->isAA()
107 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700108 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700109 }
halcanary9d524f22016-03-29 09:03:52 -0700110
bsalomon8acedde2016-06-24 10:42:16 -0700111 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700112 GrPathRenderer::CanDrawPathArgs canDrawArgs;
113 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
114 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700115 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700116 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700117 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700118 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700119
robertphillips3f7357f2015-10-27 07:17:33 -0700120 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700121 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700122 if (prOut) {
123 *prOut = pr;
124 }
125 return SkToBool(!pr);
126 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000127}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000128
robertphillips@google.comfa662942012-05-17 12:20:22 +0000129/*
130 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
131 * will be used on any element. If so, it returns true to indicate that the
132 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
133 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700134bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
135 bool hasUserStencilSettings,
136 const GrDrawContext* drawContext,
137 const SkVector& clipToMaskOffset,
csmartdalton77f2fae2016-08-08 09:55:06 -0700138 const ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000139 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000140 // a clip gets complex enough it can just be done in SW regardless
141 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000142
joshualitt8059eb92014-12-29 15:10:07 -0800143 // Set the matrix so that rendered clip elements are transformed to mask space from clip
144 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700145 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800146
csmartdalton77f2fae2016-08-08 09:55:06 -0700147 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000148 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700149
150 SkRegion::Op op = element->getOp();
151 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700152 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700153 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700154
robertphillips59cf61a2016-07-13 09:18:21 -0700155 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700156 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700157 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000158 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000159 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000160 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000161}
162
csmartdalton77f2fae2016-08-08 09:55:06 -0700163static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700164 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700165 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700166 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700167 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000168 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700169 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700170 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700171 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700172 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700173 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000174 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;
bsalomonbd2bbe42016-07-08 07:36:42 -0700183 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000184 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:
bungeman06ca8ec2016-06-09 08:01:03 -0700193 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000194 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000195 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700196 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800197 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700198 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700199 return false;
bsalomona912dde2015-10-14 15:01:50 -0700200 }
joshualittb0a8a372014-09-23 09:50:21 -0700201 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700202 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000203 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700204 edgeType =
205 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000206 }
bsalomona912dde2015-10-14 15:01:50 -0700207
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000208 switch (iter.get()->getType()) {
209 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700210 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
211 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000212 break;
213 case SkClipStack::Element::kRRect_Type: {
214 SkRRect rrect = iter.get()->getRRect();
215 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700216 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000217 break;
218 }
219 case SkClipStack::Element::kRect_Type: {
220 SkRect rect = iter.get()->getRect();
221 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700222 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000223 break;
224 }
225 default:
226 break;
227 }
bungeman06ca8ec2016-06-09 08:01:03 -0700228 if (!fps.back()) {
229 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000230 }
231 }
mtklein217daa72014-07-02 12:55:21 -0700232 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000233 }
234
bsalomon0b5b6b22015-10-14 08:31:34 -0700235 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700236 if (fps.count()) {
237 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000238 }
bungeman06ca8ec2016-06-09 08:01:03 -0700239 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000240}
241
robertphillips@google.comf294b772012-04-27 14:29:26 +0000242////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000243// sort out what kind of clip mask needs to be created: alpha, stencil,
244// scissor, or entirely software
csmartdaltond211e782016-08-15 11:17:19 -0700245bool GrClipStackClip::apply(GrContext* context, GrDrawContext* drawContext, bool useHWAA,
246 bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700247 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700248 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700249 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000250
csmartdaltoncbecb082016-07-22 08:59:08 -0700251 SkRect devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700252 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700253 return false;
254 }
255
csmartdaltonc6f411e2016-08-05 22:32:12 -0700256 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
257 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700258
csmartdalton77f2fae2016-08-08 09:55:06 -0700259 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
260 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000261
csmartdaltond211e782016-08-15 11:17:19 -0700262 if (reducedClip.hasIBounds() &&
263 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
264 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
265 scissorSpaceIBounds.offset(-fOrigin);
266 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700267 }
cdalton93a379b2016-05-11 13:58:08 -0700268
csmartdaltond211e782016-08-15 11:17:19 -0700269 if (reducedClip.elements().isEmpty()) {
270 return InitialState::kAllIn == reducedClip.initialState();
271 }
272
273 SkASSERT(reducedClip.hasIBounds());
274
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000275 // An element count of 4 was chosen because of the common pattern in Blink of:
276 // isect RR
277 // diff RR
278 // isect convex_poly
279 // isect convex_poly
280 // when drawing rounded div borders. This could probably be tuned based on a
281 // configuration's relative costs of switching RTs to generate a mask vs
282 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700283 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800284 // When there are multiple samples we want to do per-sample clipping, not compute a
285 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700286 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
287 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700288 // With a single color sample, any coverage info is lost from color once it hits the
289 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
290 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700291 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700292 }
bungeman06ca8ec2016-06-09 08:01:03 -0700293 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700294 if (reducedClip.requiresAA() &&
295 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
296 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700297 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000298 return true;
299 }
300 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000301
cdaltonede75742015-11-11 15:27:57 -0800302 // If the stencil buffer is multisampled we can use it to do everything.
csmartdalton77f2fae2016-08-08 09:55:06 -0700303 if (!drawContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700304 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000305
joshualitt8059eb92014-12-29 15:10:07 -0800306 // The top-left of the mask corresponds to the top-left corner of the bounds.
307 SkVector clipToMaskOffset = {
csmartdalton77f2fae2016-08-08 09:55:06 -0700308 SkIntToScalar(-reducedClip.left()),
309 SkIntToScalar(-reducedClip.top())
joshualitt8059eb92014-12-29 15:10:07 -0800310 };
311
robertphillips59cf61a2016-07-13 09:18:21 -0700312 if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700313 clipToMaskOffset, reducedClip.elements())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000314 // The clip geometry is complex enough that it will be more efficient to create it
315 // entirely in software
csmartdalton77f2fae2016-08-08 09:55:06 -0700316 result = CreateSoftwareClipMask(context->textureProvider(), reducedClip,
317 clipToMaskOffset);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000318 } else {
csmartdalton77f2fae2016-08-08 09:55:06 -0700319 result = CreateAlphaClipMask(context, reducedClip, clipToMaskOffset);
robertphillips391395d2016-03-02 09:26:36 -0800320 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700321 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000322 }
323
bsalomon49f085d2014-09-05 13:34:00 -0700324 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000325 // The mask's top left coord should be pinned to the rounded-out top left corner of
326 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700327 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700328 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700329 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000330 return true;
331 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000332 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000333 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000334
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000335 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdaltonc6f411e2016-08-05 22:32:12 -0700336 SkIPoint clipSpaceToStencilSpaceOffset = -fOrigin;
csmartdalton77f2fae2016-08-08 09:55:06 -0700337 CreateStencilClipMask(context, drawContext, reducedClip, clipSpaceToStencilSpaceOffset);
csmartdaltond211e782016-08-15 11:17:19 -0700338 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000339 return true;
340}
341
robertphillips391395d2016-03-02 09:26:36 -0800342static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700343 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700344 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800345 const SkMatrix& viewMatrix,
346 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800347
348 // TODO: Draw rrects directly here.
349 switch (element->getType()) {
350 case Element::kEmpty_Type:
351 SkDEBUGFAIL("Should never get here with an empty element.");
352 break;
robertphillips391395d2016-03-02 09:26:36 -0800353 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700354 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800355 element->getOp(),
356 element->isInverseFilled(),
357 element->isAA(),
358 viewMatrix, element->getRect());
359 break;
robertphillips86c60752016-03-02 08:43:13 -0800360 default: {
361 SkPath path;
362 element->asPath(&path);
363 if (path.isInverseFillType()) {
364 path.toggleInverseFillType();
365 }
366
cdalton862cff32016-05-12 15:09:48 -0700367 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800368 element->getOp(),
369 element->isInverseFilled(),
370 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800371 break;
372 }
373 }
robertphillips391395d2016-03-02 09:26:36 -0800374
375 return false;
376}
377
378static void draw_element(GrDrawContext* dc,
379 const GrClip& clip, // TODO: can this just always be WideOpen?
380 const GrPaint &paint,
381 const SkMatrix& viewMatrix,
382 const SkClipStack::Element* element) {
383
384 // TODO: Draw rrects directly here.
385 switch (element->getType()) {
386 case Element::kEmpty_Type:
387 SkDEBUGFAIL("Should never get here with an empty element.");
388 break;
389 case Element::kRect_Type:
390 dc->drawRect(clip, paint, viewMatrix, element->getRect());
391 break;
392 default: {
393 SkPath path;
394 element->asPath(&path);
395 if (path.isInverseFillType()) {
396 path.toggleInverseFillType();
397 }
398
bsalomon6663acf2016-05-10 09:14:17 -0700399 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800400 break;
401 }
402 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000403}
404
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000405////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700406// Create a 8-bit clip mask in alpha
407
408static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
409 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
410 GrUniqueKey::Builder builder(key, kDomain, 3);
411 builder[0] = clipGenID;
412 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
413 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
414}
415
csmartdaltonc6f411e2016-08-05 22:32:12 -0700416sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdalton77f2fae2016-08-08 09:55:06 -0700417 const GrReducedClip& reducedClip,
418 const SkVector& clipToMaskOffset) {
robertphillips391395d2016-03-02 09:26:36 -0800419 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700420 GrUniqueKey key;
csmartdaltond211e782016-08-15 11:17:19 -0700421 GetClipMaskKey(reducedClip.genID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700422 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700423 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000424 }
425
robertphillips544b9aa2015-10-28 11:01:41 -0700426 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700427 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800428 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700429 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800430 }
431
robertphillips6738c702016-07-27 12:13:51 -0700432 sk_sp<GrDrawContext> dc(context->makeDrawContext(SkBackingFit::kApprox,
csmartdalton77f2fae2016-08-08 09:55:06 -0700433 reducedClip.width(),
434 reducedClip.height(),
robertphillips6738c702016-07-27 12:13:51 -0700435 config, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800436 if (!dc) {
437 return nullptr;
438 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700439
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000440 // The texture may be larger than necessary, this rect represents the part of the texture
441 // we populate with a rasterization of the clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700442 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000443
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000444 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
445 // clear the part that we care about.
csmartdaltond211e782016-08-15 11:17:19 -0700446 dc->clear(&maskSpaceIBounds, InitialState::kAllIn == reducedClip.initialState() ? -1 : 0, true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000447
robertphillips391395d2016-03-02 09:26:36 -0800448 // Set the matrix so that rendered clip elements are transformed to mask space from clip
449 // space.
450 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
451
452 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000453 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
454 // pass must not set values outside of this bounds or stencil values outside the rect won't be
455 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800456
robertphillips@google.comf294b772012-04-27 14:29:26 +0000457 // walk through each clip element and perform its set op
csmartdalton77f2fae2016-08-08 09:55:06 -0700458 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000459 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000460 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000461 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000462 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700463 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700464
robertphillips391395d2016-03-02 09:26:36 -0800465 // draw directly into the result with the stencil set to make the pixels affected
466 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700467 static constexpr GrUserStencilSettings kStencilInElement(
468 GrUserStencilSettings::StaticInit<
469 0xffff,
470 GrUserStencilTest::kAlways,
471 0xffff,
472 GrUserStencilOp::kReplace,
473 GrUserStencilOp::kReplace,
474 0xffff>()
475 );
cdalton862cff32016-05-12 15:09:48 -0700476 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800477 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800478 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000479 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000480
robertphillips391395d2016-03-02 09:26:36 -0800481 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700482 static constexpr GrUserStencilSettings kDrawOutsideElement(
483 GrUserStencilSettings::StaticInit<
484 0x0000,
485 GrUserStencilTest::kEqual,
486 0xffff,
487 GrUserStencilOp::kZero,
488 GrUserStencilOp::kZero,
489 0xffff>()
490 );
cdalton862cff32016-05-12 15:09:48 -0700491 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800492 op, !invert, false,
493 translate,
csmartdaltond211e782016-08-15 11:17:19 -0700494 SkRect::Make(reducedClip.ibounds()))) {
robertphillips391395d2016-03-02 09:26:36 -0800495 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000496 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000497 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800498 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800499 GrPaint paint;
500 paint.setAntiAlias(element->isAA());
501 paint.setCoverageSetOpXPFactory(op, false);
502
cdalton846c0512016-05-13 10:25:00 -0700503 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000504 }
505 }
506
robertphillipsc99b8f02016-05-15 07:53:35 -0700507 sk_sp<GrTexture> texture(dc->asTexture());
508 SkASSERT(texture);
509 texture->resourcePriv().setUniqueKey(key);
510 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000511}
512
513////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000514// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000515// (as opposed to canvas) coordinates
csmartdaltonc6f411e2016-08-05 22:32:12 -0700516bool GrClipStackClip::CreateStencilClipMask(GrContext* context,
517 GrDrawContext* drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700518 const GrReducedClip& reducedClip,
csmartdaltonc6f411e2016-08-05 22:32:12 -0700519 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700520 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000521
robertphillips976f5f02016-06-03 10:59:20 -0700522 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
523 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700524 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000525 return false;
526 }
527
robertphillips976f5f02016-06-03 10:59:20 -0700528 // TODO: these need to be swapped over to using a StencilAttachmentProxy
csmartdaltond211e782016-08-15 11:17:19 -0700529 if (stencilAttachment->mustRenderClip(reducedClip.genID(), reducedClip.ibounds(),
csmartdalton77f2fae2016-08-08 09:55:06 -0700530 clipSpaceToStencilOffset)) {
csmartdaltond211e782016-08-15 11:17:19 -0700531 stencilAttachment->setLastClip(reducedClip.genID(), reducedClip.ibounds(),
csmartdalton77f2fae2016-08-08 09:55:06 -0700532 clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000533 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
534 SkVector translate = {
535 SkIntToScalar(clipSpaceToStencilOffset.fX),
536 SkIntToScalar(clipSpaceToStencilOffset.fY)
537 };
joshualitt8059eb92014-12-29 15:10:07 -0800538 SkMatrix viewMatrix;
539 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000540
bsalomon@google.com9f131742012-12-13 20:43:56 +0000541 // We set the current clip to the bounds so that our recursive draws are scissored to them.
csmartdaltond211e782016-08-15 11:17:19 -0700542 SkIRect stencilSpaceIBounds(reducedClip.ibounds());
bsalomon@google.com9f131742012-12-13 20:43:56 +0000543 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700544 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000545
csmartdaltond211e782016-08-15 11:17:19 -0700546 bool insideClip = InitialState::kAllIn == reducedClip.initialState();
csmartdalton77f2fae2016-08-08 09:55:06 -0700547 drawContext->drawContextPriv().clearStencilClip(stencilSpaceIBounds, insideClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000548
549 // walk through each clip element and perform its set op
550 // with the existing clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700551 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000552 const Element* element = iter.get();
csmartdalton656dbe42016-06-10 12:32:57 -0700553 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
joshualitt9853cce2014-11-17 14:22:48 -0800554
tomhudson@google.com8afae612012-08-14 15:03:35 +0000555 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000556 // enabled at bottom of loop
bsalomon6cc90062016-07-08 11:31:22 -0700557 clip.disableStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000558
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000559 // This will be used to determine whether the clip shape can be rendered into the
560 // stencil with arbitrary stencil settings.
561 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000562
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000563 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000564
halcanary96fcdcc2015-08-27 07:41:13 -0700565 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000566 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000567 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000568 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000569 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000570 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000571 element->asPath(&clipPath);
572 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000573 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000574 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000575 }
robertphillips68737822015-10-29 12:12:21 -0700576
bsalomon8acedde2016-06-24 10:42:16 -0700577 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700578 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700579 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700580 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700581 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700582 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700583 canDrawArgs.fHasUserStencilSettings = false;
584 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700585
csmartdaltonc6f411e2016-08-05 22:32:12 -0700586 GrDrawingManager* dm = context->drawingManager();
587 pr = dm->getPathRenderer(canDrawArgs, false,
588 GrPathRendererChain::kStencilOnly_DrawType,
589 &stencilSupport);
robertphillips976f5f02016-06-03 10:59:20 -0700590 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000591 return false;
592 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000593 }
594
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000595 bool canRenderDirectToStencil =
596 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700597 bool drawDirectToClip; // Given the renderer, the element,
598 // fill rule, and set operation should
599 // we render the element directly to
600 // stencil bit used for clipping.
601 GrUserStencilSettings const* const* stencilPasses =
602 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
603 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000604
605 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700606 if (!drawDirectToClip) {
607 static constexpr GrUserStencilSettings kDrawToStencil(
608 GrUserStencilSettings::StaticInit<
609 0x0000,
610 GrUserStencilTest::kAlways,
611 0xffff,
612 GrUserStencilOp::kIncMaybeClamp,
613 GrUserStencilOp::kIncMaybeClamp,
614 0xffff>()
615 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000616 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700617 drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA,
618 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000619 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000620 if (!clipPath.isEmpty()) {
bsalomon8acedde2016-06-24 10:42:16 -0700621 GrShape shape(clipPath, GrStyle::SimpleFill());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000622 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700623 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700624 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700625 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700626
627 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700628 args.fResourceProvider = context->resourceProvider();
629 args.fPaint = &paint;
630 args.fUserStencilSettings = &kDrawToStencil;
631 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700632 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700633 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700634 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700635 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700636 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700637 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000638 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700639 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700640 args.fResourceProvider = context->resourceProvider();
641 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700642 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700643 args.fViewMatrix = &viewMatrix;
robertphillips976f5f02016-06-03 10:59:20 -0700644 args.fIsAA = element->isAA();
bsalomon8acedde2016-06-24 10:42:16 -0700645 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700646 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000647 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000648 }
649 }
650 }
651
csmartdaltond211e782016-08-15 11:17:19 -0700652 // Just enable stencil clip. The passes choose whether or not they will actually use it.
653 clip.enableStencilClip();
654
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000655 // now we modify the clip bit by rendering either the clip
656 // element directly or a bounding rect of the entire clip.
cdalton93a379b2016-05-11 13:58:08 -0700657 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
cdalton93a379b2016-05-11 13:58:08 -0700658 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000659 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700660 drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix,
661 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000662 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700663 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips976f5f02016-06-03 10:59:20 -0700664 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700665 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700666 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700667 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700668 args.fResourceProvider = context->resourceProvider();
669 args.fPaint = &paint;
670 args.fUserStencilSettings = *pass;
671 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700672 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700673 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700674 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700675 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700676 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700677 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000678 }
679 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000680 // The view matrix is setup to do clip space -> stencil space translation, so
681 // draw rect in clip space.
csmartdalton656dbe42016-06-10 12:32:57 -0700682 drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix,
csmartdaltond211e782016-08-15 11:17:19 -0700683 SkRect::Make(reducedClip.ibounds()));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000684 }
685 }
686 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000687 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000688 return true;
689}
690
bsalomon@google.com411dad02012-06-05 20:24:20 +0000691////////////////////////////////////////////////////////////////////////////////
csmartdaltonc6f411e2016-08-05 22:32:12 -0700692sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
csmartdalton77f2fae2016-08-08 09:55:06 -0700693 const GrReducedClip& reducedClip,
694 const SkVector& clipToMaskOffset) {
bsalomon473addf2015-10-02 07:49:05 -0700695 GrUniqueKey key;
csmartdaltond211e782016-08-15 11:17:19 -0700696 GetClipMaskKey(reducedClip.genID(), reducedClip.ibounds(), &key);
robertphillips0152d732016-05-20 06:38:43 -0700697 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700698 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000699 }
700
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000701 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
702 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700703 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000704
robertphillips0152d732016-05-20 06:38:43 -0700705 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000706
joshualitt8059eb92014-12-29 15:10:07 -0800707 // Set the matrix so that rendered clip elements are transformed to mask space from clip
708 // space.
709 SkMatrix translate;
710 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800711
robertphillips98377402016-05-13 05:47:23 -0700712 helper.init(maskSpaceIBounds, &translate);
csmartdaltond211e782016-08-15 11:17:19 -0700713 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000714
csmartdalton77f2fae2016-08-08 09:55:06 -0700715 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000716 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000717 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000718
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000719 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
720 // Intersect and reverse difference require modifying pixels outside of the geometry
721 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
722 // but leave the pixels inside the geometry alone. For reverse difference we invert all
723 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000724 if (SkRegion::kReverseDifference_Op == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700725 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000726 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700727 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000728 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000729 SkPath clipPath;
730 element->asPath(&clipPath);
731 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700732 GrShape shape(clipPath, GrStyle::SimpleFill());
733 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000734 continue;
735 }
736
737 // The other ops (union, xor, diff) only affect pixels inside
738 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000739 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700740 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000741 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000742 SkPath path;
743 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700744 GrShape shape(path, GrStyle::SimpleFill());
745 helper.drawShape(shape, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000746 }
747 }
748
krajcevskiad1dc582014-06-10 15:06:47 -0700749 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800750 GrSurfaceDesc desc;
csmartdalton77f2fae2016-08-08 09:55:06 -0700751 desc.fWidth = reducedClip.width();
752 desc.fHeight = reducedClip.height();
robertphillips391395d2016-03-02 09:26:36 -0800753 desc.fConfig = kAlpha_8_GrPixelConfig;
754
robertphillips0152d732016-05-20 06:38:43 -0700755 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800756 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700757 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700758 }
robertphillips391395d2016-03-02 09:26:36 -0800759 result->resourcePriv().setUniqueKey(key);
760
robertphillipsc99b8f02016-05-15 07:53:35 -0700761 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000762
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000763 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000764}