blob: 695eda981fa379456995dd0ae1f2e22c678b38e0 [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
csmartdalton28341fa2016-08-17 10:00:21 -070010#include "GrAppliedClip.h"
robertphillips68737822015-10-29 12:12:21 -070011#include "GrDrawingManager.h"
robertphillips391395d2016-03-02 09:26:36 -080012#include "GrDrawContextPriv.h"
csmartdalton02fa32c2016-08-19 13:29:27 -070013#include "GrFixedClip.h"
bsalomon473addf2015-10-02 07:49:05 -070014#include "GrGpuResourcePriv.h"
csmartdalton28341fa2016-08-17 10:00:21 -070015#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070016#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000017#include "GrSWMaskHelper.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080018#include "effects/GrConvexPolyEffect.h"
19#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080020#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000021
bsalomon@google.com8182fa02012-12-04 14:06:06 +000022typedef SkClipStack::Element Element;
csmartdaltoncbecb082016-07-22 08:59:08 -070023typedef GrReducedClip::InitialState InitialState;
csmartdalton77f2fae2016-08-08 09:55:06 -070024typedef GrReducedClip::ElementList ElementList;
bsalomon@google.com51a62862012-11-26 21:19:43 +000025
robertphillips976f5f02016-06-03 10:59:20 -070026static const int kMaxAnalyticElements = 4;
27
csmartdaltonc6f411e2016-08-05 22:32:12 -070028bool GrClipStackClip::quickContains(const SkRect& rect) const {
29 if (!fStack) {
30 return true;
31 }
32 return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()),
33 SkIntToScalar(fOrigin.y())));
34}
35
bsalomon7f0d9f32016-08-15 14:49:10 -070036bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
37 if (!fStack) {
38 return true;
39 }
40 return fStack->quickContains(rrect.makeOffset(SkIntToScalar(fOrigin.fX),
41 SkIntToScalar(fOrigin.fY)));
42}
43
csmartdaltonc6f411e2016-08-05 22:32:12 -070044void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
45 bool* isIntersectionOfRects) const {
46 if (!fStack) {
47 devResult->setXYWH(0, 0, width, height);
48 if (isIntersectionOfRects) {
49 *isIntersectionOfRects = true;
50 }
51 return;
52 }
53 SkRect devBounds;
54 fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
55 isIntersectionOfRects);
56 devBounds.roundOut(devResult);
57}
58
bsalomon@google.com51a62862012-11-26 21:19:43 +000059////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000060// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000061// stage matrix this also alters the vertex layout
bungeman06ca8ec2016-06-09 08:01:03 -070062static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
63 const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000064 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080065 // We use device coords to compute the texture coordinates. We set our matrix to be a
66 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000067 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000068 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000069 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000070
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000071 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bungeman06ca8ec2016-06-09 08:01:03 -070072 return sk_sp<GrFragmentProcessor>(GrTextureDomainEffect::Make(
robertphillips5f2fa472016-05-19 11:36:25 -070073 result,
brianosman54f30c12016-07-18 10:53:52 -070074 nullptr,
bsalomon0ba8c242015-10-07 09:20:28 -070075 mat,
76 GrTextureDomain::MakeTexelDomain(result, domainTexels),
77 GrTextureDomain::kDecal_Mode,
78 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -070079 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000080}
81
robertphillips3f7357f2015-10-27 07:17:33 -070082// Does the path in 'element' require SW rendering? If so, return true (and,
83// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
84// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070085bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
86 bool hasUserStencilSettings,
87 const GrDrawContext* drawContext,
88 const SkMatrix& viewMatrix,
89 const Element* element,
90 GrPathRenderer** prOut,
91 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070092 if (Element::kRect_Type == element->getType()) {
93 // rects can always be drawn directly w/o using the software path
94 // TODO: skip rrects once we're drawing them directly.
95 if (prOut) {
96 *prOut = nullptr;
97 }
98 return false;
99 } else {
100 // We shouldn't get here with an empty clip element.
101 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700102
robertphillips3f7357f2015-10-27 07:17:33 -0700103 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
104 SkPath path;
105 element->asPath(&path);
106 if (path.isInverseFillType()) {
107 path.toggleInverseFillType();
108 }
halcanary9d524f22016-03-29 09:03:52 -0700109
robertphillips3f7357f2015-10-27 07:17:33 -0700110 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -0700111
robertphillips423e3372015-10-27 09:23:38 -0700112 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700113 type = element->isAA()
114 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
115 : GrPathRendererChain::kStencilAndColor_DrawType;
116 } else {
117 type = element->isAA()
118 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700119 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700120 }
halcanary9d524f22016-03-29 09:03:52 -0700121
bsalomon8acedde2016-06-24 10:42:16 -0700122 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700123 GrPathRenderer::CanDrawPathArgs canDrawArgs;
124 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
125 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700126 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700127 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700128 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700129 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700130
robertphillips3f7357f2015-10-27 07:17:33 -0700131 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700132 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700133 if (prOut) {
134 *prOut = pr;
135 }
136 return SkToBool(!pr);
137 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000138}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000139
robertphillips@google.comfa662942012-05-17 12:20:22 +0000140/*
141 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
142 * will be used on any element. If so, it returns true to indicate that the
143 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
144 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700145bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
146 bool hasUserStencilSettings,
147 const GrDrawContext* drawContext,
148 const SkVector& clipToMaskOffset,
csmartdalton77f2fae2016-08-08 09:55:06 -0700149 const ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000150 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000151 // a clip gets complex enough it can just be done in SW regardless
152 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000153
joshualitt8059eb92014-12-29 15:10:07 -0800154 // Set the matrix so that rendered clip elements are transformed to mask space from clip
155 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700156 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800157
csmartdalton77f2fae2016-08-08 09:55:06 -0700158 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000159 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700160
161 SkRegion::Op op = element->getOp();
162 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700163 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700164 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700165
robertphillips59cf61a2016-07-13 09:18:21 -0700166 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700167 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700168 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000169 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000170 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000171 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000172}
173
csmartdalton77f2fae2016-08-08 09:55:06 -0700174static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700175 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700176 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700177 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700178 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000179 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700180 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700181 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700182 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700183 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700184 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000185 SkRegion::Op op = iter.get()->getOp();
186 bool invert;
187 bool skip = false;
188 switch (op) {
189 case SkRegion::kReplace_Op:
190 SkASSERT(iter.get() == elements.head());
191 // Fallthrough, handled same as intersect.
192 case SkRegion::kIntersect_Op:
193 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700194 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000195 skip = true;
196 }
197 break;
198 case SkRegion::kDifference_Op:
199 invert = true;
200 // We don't currently have a cheap test for whether a rect is fully outside an
201 // element's primitive, so don't attempt to set skip.
202 break;
203 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700204 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000205 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000206 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700207 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800208 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700209 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700210 return false;
bsalomona912dde2015-10-14 15:01:50 -0700211 }
joshualittb0a8a372014-09-23 09:50:21 -0700212 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700213 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000214 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700215 edgeType =
216 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000217 }
bsalomona912dde2015-10-14 15:01:50 -0700218
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000219 switch (iter.get()->getType()) {
220 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700221 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
222 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000223 break;
224 case SkClipStack::Element::kRRect_Type: {
225 SkRRect rrect = iter.get()->getRRect();
226 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700227 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000228 break;
229 }
230 case SkClipStack::Element::kRect_Type: {
231 SkRect rect = iter.get()->getRect();
232 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700233 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000234 break;
235 }
236 default:
237 break;
238 }
bungeman06ca8ec2016-06-09 08:01:03 -0700239 if (!fps.back()) {
240 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000241 }
242 }
mtklein217daa72014-07-02 12:55:21 -0700243 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000244 }
245
bsalomon0b5b6b22015-10-14 08:31:34 -0700246 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700247 if (fps.count()) {
248 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000249 }
bungeman06ca8ec2016-06-09 08:01:03 -0700250 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000251}
252
robertphillips@google.comf294b772012-04-27 14:29:26 +0000253////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000254// sort out what kind of clip mask needs to be created: alpha, stencil,
255// scissor, or entirely software
csmartdaltond211e782016-08-15 11:17:19 -0700256bool GrClipStackClip::apply(GrContext* context, GrDrawContext* drawContext, bool useHWAA,
257 bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700258 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700259 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700260 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000261
csmartdaltoncbecb082016-07-22 08:59:08 -0700262 SkRect devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700263 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700264 return false;
265 }
266
csmartdaltonc6f411e2016-08-05 22:32:12 -0700267 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
268 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700269
csmartdalton77f2fae2016-08-08 09:55:06 -0700270 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
271 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000272
csmartdaltond211e782016-08-15 11:17:19 -0700273 if (reducedClip.hasIBounds() &&
274 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
275 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
276 scissorSpaceIBounds.offset(-fOrigin);
277 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700278 }
cdalton93a379b2016-05-11 13:58:08 -0700279
csmartdaltond211e782016-08-15 11:17:19 -0700280 if (reducedClip.elements().isEmpty()) {
281 return InitialState::kAllIn == reducedClip.initialState();
282 }
283
284 SkASSERT(reducedClip.hasIBounds());
285
csmartdalton28341fa2016-08-17 10:00:21 -0700286 // Attempt to implement difference clip rects with window rectangles. This will eventually
287 // become more comprehensive.
288 if (drawContext->accessRenderTarget()->renderTargetPriv().supportsWindowRectangles() &&
289 1 == reducedClip.elements().count() && !reducedClip.requiresAA() &&
290 InitialState::kAllIn == reducedClip.initialState()) {
291 const Element* element = reducedClip.elements().head();
292 SkRegion::Op op = element->getOp();
293 if (Element::kRect_Type == element->getType() &&
294 (SkRegion::kDifference_Op == op || SkRegion::kXOR_Op == op)) {
295 SkIRect window;
296 element->getRect().round(&window);
297 window.offset(-fOrigin);
298 out->addWindowRectangle(window);
299 return true;
300 }
301 }
302
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000303 // An element count of 4 was chosen because of the common pattern in Blink of:
304 // isect RR
305 // diff RR
306 // isect convex_poly
307 // isect convex_poly
308 // when drawing rounded div borders. This could probably be tuned based on a
309 // configuration's relative costs of switching RTs to generate a mask vs
310 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700311 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800312 // When there are multiple samples we want to do per-sample clipping, not compute a
313 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700314 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
315 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700316 // With a single color sample, any coverage info is lost from color once it hits the
317 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
318 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700319 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700320 }
bungeman06ca8ec2016-06-09 08:01:03 -0700321 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700322 if (reducedClip.requiresAA() &&
323 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
324 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700325 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000326 return true;
327 }
328 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000329
cdaltonede75742015-11-11 15:27:57 -0800330 // If the stencil buffer is multisampled we can use it to do everything.
csmartdalton77f2fae2016-08-08 09:55:06 -0700331 if (!drawContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700332 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000333
joshualitt8059eb92014-12-29 15:10:07 -0800334 // The top-left of the mask corresponds to the top-left corner of the bounds.
335 SkVector clipToMaskOffset = {
csmartdalton77f2fae2016-08-08 09:55:06 -0700336 SkIntToScalar(-reducedClip.left()),
337 SkIntToScalar(-reducedClip.top())
joshualitt8059eb92014-12-29 15:10:07 -0800338 };
339
robertphillips59cf61a2016-07-13 09:18:21 -0700340 if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700341 clipToMaskOffset, reducedClip.elements())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000342 // The clip geometry is complex enough that it will be more efficient to create it
343 // entirely in software
csmartdalton77f2fae2016-08-08 09:55:06 -0700344 result = CreateSoftwareClipMask(context->textureProvider(), reducedClip,
345 clipToMaskOffset);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000346 } else {
csmartdalton77f2fae2016-08-08 09:55:06 -0700347 result = CreateAlphaClipMask(context, reducedClip, clipToMaskOffset);
robertphillips391395d2016-03-02 09:26:36 -0800348 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700349 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000350 }
351
bsalomon49f085d2014-09-05 13:34:00 -0700352 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000353 // The mask's top left coord should be pinned to the rounded-out top left corner of
354 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700355 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700356 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700357 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000358 return true;
359 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000360 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000362
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000363 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdaltonc6f411e2016-08-05 22:32:12 -0700364 SkIPoint clipSpaceToStencilSpaceOffset = -fOrigin;
csmartdalton77f2fae2016-08-08 09:55:06 -0700365 CreateStencilClipMask(context, drawContext, reducedClip, clipSpaceToStencilSpaceOffset);
csmartdaltond211e782016-08-15 11:17:19 -0700366 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000367 return true;
368}
369
robertphillips391395d2016-03-02 09:26:36 -0800370static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700371 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700372 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800373 const SkMatrix& viewMatrix,
374 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800375
376 // TODO: Draw rrects directly here.
377 switch (element->getType()) {
378 case Element::kEmpty_Type:
379 SkDEBUGFAIL("Should never get here with an empty element.");
380 break;
robertphillips391395d2016-03-02 09:26:36 -0800381 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700382 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800383 element->getOp(),
384 element->isInverseFilled(),
385 element->isAA(),
386 viewMatrix, element->getRect());
387 break;
robertphillips86c60752016-03-02 08:43:13 -0800388 default: {
389 SkPath path;
390 element->asPath(&path);
391 if (path.isInverseFillType()) {
392 path.toggleInverseFillType();
393 }
394
cdalton862cff32016-05-12 15:09:48 -0700395 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800396 element->getOp(),
397 element->isInverseFilled(),
398 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800399 break;
400 }
401 }
robertphillips391395d2016-03-02 09:26:36 -0800402
403 return false;
404}
405
406static void draw_element(GrDrawContext* dc,
407 const GrClip& clip, // TODO: can this just always be WideOpen?
408 const GrPaint &paint,
409 const SkMatrix& viewMatrix,
410 const SkClipStack::Element* element) {
411
412 // TODO: Draw rrects directly here.
413 switch (element->getType()) {
414 case Element::kEmpty_Type:
415 SkDEBUGFAIL("Should never get here with an empty element.");
416 break;
417 case Element::kRect_Type:
418 dc->drawRect(clip, paint, viewMatrix, element->getRect());
419 break;
420 default: {
421 SkPath path;
422 element->asPath(&path);
423 if (path.isInverseFillType()) {
424 path.toggleInverseFillType();
425 }
426
bsalomon6663acf2016-05-10 09:14:17 -0700427 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800428 break;
429 }
430 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000431}
432
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000433////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700434// Create a 8-bit clip mask in alpha
435
436static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
437 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
438 GrUniqueKey::Builder builder(key, kDomain, 3);
439 builder[0] = clipGenID;
440 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
441 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
442}
443
csmartdaltonc6f411e2016-08-05 22:32:12 -0700444sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdalton77f2fae2016-08-08 09:55:06 -0700445 const GrReducedClip& reducedClip,
446 const SkVector& clipToMaskOffset) {
robertphillips391395d2016-03-02 09:26:36 -0800447 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700448 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700449 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700450 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700451 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000452 }
453
robertphillips544b9aa2015-10-28 11:01:41 -0700454 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700455 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800456 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700457 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800458 }
459
robertphillips6738c702016-07-27 12:13:51 -0700460 sk_sp<GrDrawContext> dc(context->makeDrawContext(SkBackingFit::kApprox,
csmartdalton77f2fae2016-08-08 09:55:06 -0700461 reducedClip.width(),
462 reducedClip.height(),
robertphillips6738c702016-07-27 12:13:51 -0700463 config, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800464 if (!dc) {
465 return nullptr;
466 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700467
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000468 // The texture may be larger than necessary, this rect represents the part of the texture
469 // we populate with a rasterization of the clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700470 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000471
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000472 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
473 // clear the part that we care about.
csmartdaltond211e782016-08-15 11:17:19 -0700474 dc->clear(&maskSpaceIBounds, InitialState::kAllIn == reducedClip.initialState() ? -1 : 0, true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000475
robertphillips391395d2016-03-02 09:26:36 -0800476 // Set the matrix so that rendered clip elements are transformed to mask space from clip
477 // space.
478 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
479
480 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000481 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
482 // pass must not set values outside of this bounds or stencil values outside the rect won't be
483 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800484
robertphillips@google.comf294b772012-04-27 14:29:26 +0000485 // walk through each clip element and perform its set op
csmartdalton77f2fae2016-08-08 09:55:06 -0700486 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000487 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000488 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000489 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000490 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700491 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700492
robertphillips391395d2016-03-02 09:26:36 -0800493 // draw directly into the result with the stencil set to make the pixels affected
494 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700495 static constexpr GrUserStencilSettings kStencilInElement(
496 GrUserStencilSettings::StaticInit<
497 0xffff,
498 GrUserStencilTest::kAlways,
499 0xffff,
500 GrUserStencilOp::kReplace,
501 GrUserStencilOp::kReplace,
502 0xffff>()
503 );
cdalton862cff32016-05-12 15:09:48 -0700504 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800505 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800506 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000507 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000508
robertphillips391395d2016-03-02 09:26:36 -0800509 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700510 static constexpr GrUserStencilSettings kDrawOutsideElement(
511 GrUserStencilSettings::StaticInit<
512 0x0000,
513 GrUserStencilTest::kEqual,
514 0xffff,
515 GrUserStencilOp::kZero,
516 GrUserStencilOp::kZero,
517 0xffff>()
518 );
cdalton862cff32016-05-12 15:09:48 -0700519 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800520 op, !invert, false,
521 translate,
csmartdaltond211e782016-08-15 11:17:19 -0700522 SkRect::Make(reducedClip.ibounds()))) {
robertphillips391395d2016-03-02 09:26:36 -0800523 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000524 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000525 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800526 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800527 GrPaint paint;
528 paint.setAntiAlias(element->isAA());
529 paint.setCoverageSetOpXPFactory(op, false);
530
cdalton846c0512016-05-13 10:25:00 -0700531 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000532 }
533 }
534
robertphillipsc99b8f02016-05-15 07:53:35 -0700535 sk_sp<GrTexture> texture(dc->asTexture());
536 SkASSERT(texture);
537 texture->resourcePriv().setUniqueKey(key);
538 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000539}
540
541////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000542// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000543// (as opposed to canvas) coordinates
csmartdaltonc6f411e2016-08-05 22:32:12 -0700544bool GrClipStackClip::CreateStencilClipMask(GrContext* context,
545 GrDrawContext* drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700546 const GrReducedClip& reducedClip,
csmartdaltonc6f411e2016-08-05 22:32:12 -0700547 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700548 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000549
robertphillips976f5f02016-06-03 10:59:20 -0700550 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
551 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700552 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000553 return false;
554 }
555
robertphillips976f5f02016-06-03 10:59:20 -0700556 // TODO: these need to be swapped over to using a StencilAttachmentProxy
csmartdalton8d3f92a2016-08-17 09:39:38 -0700557 if (stencilAttachment->mustRenderClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
csmartdalton77f2fae2016-08-08 09:55:06 -0700558 clipSpaceToStencilOffset)) {
csmartdalton8d3f92a2016-08-17 09:39:38 -0700559 stencilAttachment->setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
csmartdalton77f2fae2016-08-08 09:55:06 -0700560 clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000561 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
562 SkVector translate = {
563 SkIntToScalar(clipSpaceToStencilOffset.fX),
564 SkIntToScalar(clipSpaceToStencilOffset.fY)
565 };
joshualitt8059eb92014-12-29 15:10:07 -0800566 SkMatrix viewMatrix;
567 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000568
bsalomon@google.com9f131742012-12-13 20:43:56 +0000569 // We set the current clip to the bounds so that our recursive draws are scissored to them.
csmartdaltond211e782016-08-15 11:17:19 -0700570 SkIRect stencilSpaceIBounds(reducedClip.ibounds());
bsalomon@google.com9f131742012-12-13 20:43:56 +0000571 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700572 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000573
csmartdaltond211e782016-08-15 11:17:19 -0700574 bool insideClip = InitialState::kAllIn == reducedClip.initialState();
csmartdalton77f2fae2016-08-08 09:55:06 -0700575 drawContext->drawContextPriv().clearStencilClip(stencilSpaceIBounds, insideClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000576
577 // walk through each clip element and perform its set op
578 // with the existing clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700579 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000580 const Element* element = iter.get();
csmartdalton656dbe42016-06-10 12:32:57 -0700581 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
joshualitt9853cce2014-11-17 14:22:48 -0800582
tomhudson@google.com8afae612012-08-14 15:03:35 +0000583 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000584 // enabled at bottom of loop
bsalomon6cc90062016-07-08 11:31:22 -0700585 clip.disableStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000586
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000587 // This will be used to determine whether the clip shape can be rendered into the
588 // stencil with arbitrary stencil settings.
589 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000590
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000591 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000592
halcanary96fcdcc2015-08-27 07:41:13 -0700593 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000594 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000595 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000596 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000597 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000598 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000599 element->asPath(&clipPath);
600 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000601 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000602 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000603 }
robertphillips68737822015-10-29 12:12:21 -0700604
bsalomon8acedde2016-06-24 10:42:16 -0700605 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700606 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700607 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700608 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700609 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700610 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700611 canDrawArgs.fHasUserStencilSettings = false;
612 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700613
csmartdaltonc6f411e2016-08-05 22:32:12 -0700614 GrDrawingManager* dm = context->drawingManager();
615 pr = dm->getPathRenderer(canDrawArgs, false,
616 GrPathRendererChain::kStencilOnly_DrawType,
617 &stencilSupport);
robertphillips976f5f02016-06-03 10:59:20 -0700618 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000619 return false;
620 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000621 }
622
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000623 bool canRenderDirectToStencil =
624 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700625 bool drawDirectToClip; // Given the renderer, the element,
626 // fill rule, and set operation should
627 // we render the element directly to
628 // stencil bit used for clipping.
629 GrUserStencilSettings const* const* stencilPasses =
630 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
631 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000632
633 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700634 if (!drawDirectToClip) {
635 static constexpr GrUserStencilSettings kDrawToStencil(
636 GrUserStencilSettings::StaticInit<
637 0x0000,
638 GrUserStencilTest::kAlways,
639 0xffff,
640 GrUserStencilOp::kIncMaybeClamp,
641 GrUserStencilOp::kIncMaybeClamp,
642 0xffff>()
643 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000644 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700645 drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA,
646 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000647 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000648 if (!clipPath.isEmpty()) {
bsalomon8acedde2016-06-24 10:42:16 -0700649 GrShape shape(clipPath, GrStyle::SimpleFill());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000650 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700651 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700652 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700653 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700654
655 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700656 args.fResourceProvider = context->resourceProvider();
657 args.fPaint = &paint;
658 args.fUserStencilSettings = &kDrawToStencil;
659 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700660 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700661 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700662 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700663 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700664 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700665 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000666 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700667 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700668 args.fResourceProvider = context->resourceProvider();
669 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700670 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700671 args.fViewMatrix = &viewMatrix;
robertphillips976f5f02016-06-03 10:59:20 -0700672 args.fIsAA = element->isAA();
bsalomon8acedde2016-06-24 10:42:16 -0700673 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700674 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000675 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000676 }
677 }
678 }
679
csmartdaltond211e782016-08-15 11:17:19 -0700680 // Just enable stencil clip. The passes choose whether or not they will actually use it.
681 clip.enableStencilClip();
682
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000683 // now we modify the clip bit by rendering either the clip
684 // element directly or a bounding rect of the entire clip.
cdalton93a379b2016-05-11 13:58:08 -0700685 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
cdalton93a379b2016-05-11 13:58:08 -0700686 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000687 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700688 drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix,
689 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000690 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700691 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips976f5f02016-06-03 10:59:20 -0700692 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700693 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700694 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700695 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700696 args.fResourceProvider = context->resourceProvider();
697 args.fPaint = &paint;
698 args.fUserStencilSettings = *pass;
699 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700700 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700701 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700702 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700703 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700704 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700705 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000706 }
707 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000708 // The view matrix is setup to do clip space -> stencil space translation, so
709 // draw rect in clip space.
csmartdalton656dbe42016-06-10 12:32:57 -0700710 drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix,
csmartdaltond211e782016-08-15 11:17:19 -0700711 SkRect::Make(reducedClip.ibounds()));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000712 }
713 }
714 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716 return true;
717}
718
bsalomon@google.com411dad02012-06-05 20:24:20 +0000719////////////////////////////////////////////////////////////////////////////////
csmartdaltonc6f411e2016-08-05 22:32:12 -0700720sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
csmartdalton77f2fae2016-08-08 09:55:06 -0700721 const GrReducedClip& reducedClip,
722 const SkVector& clipToMaskOffset) {
bsalomon473addf2015-10-02 07:49:05 -0700723 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700724 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
robertphillips0152d732016-05-20 06:38:43 -0700725 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700726 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000727 }
728
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000729 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
730 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700731 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000732
robertphillips0152d732016-05-20 06:38:43 -0700733 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000734
joshualitt8059eb92014-12-29 15:10:07 -0800735 // Set the matrix so that rendered clip elements are transformed to mask space from clip
736 // space.
737 SkMatrix translate;
738 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800739
robertphillips98377402016-05-13 05:47:23 -0700740 helper.init(maskSpaceIBounds, &translate);
csmartdaltond211e782016-08-15 11:17:19 -0700741 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000742
csmartdalton77f2fae2016-08-08 09:55:06 -0700743 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000744 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000745 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000746
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000747 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
748 // Intersect and reverse difference require modifying pixels outside of the geometry
749 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
750 // but leave the pixels inside the geometry alone. For reverse difference we invert all
751 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000752 if (SkRegion::kReverseDifference_Op == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700753 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000754 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700755 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000756 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000757 SkPath clipPath;
758 element->asPath(&clipPath);
759 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700760 GrShape shape(clipPath, GrStyle::SimpleFill());
761 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000762 continue;
763 }
764
765 // The other ops (union, xor, diff) only affect pixels inside
766 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000767 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700768 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000769 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000770 SkPath path;
771 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700772 GrShape shape(path, GrStyle::SimpleFill());
773 helper.drawShape(shape, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000774 }
775 }
776
krajcevskiad1dc582014-06-10 15:06:47 -0700777 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800778 GrSurfaceDesc desc;
csmartdalton77f2fae2016-08-08 09:55:06 -0700779 desc.fWidth = reducedClip.width();
780 desc.fHeight = reducedClip.height();
robertphillips391395d2016-03-02 09:26:36 -0800781 desc.fConfig = kAlpha_8_GrPixelConfig;
782
robertphillips0152d732016-05-20 06:38:43 -0700783 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800784 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700785 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700786 }
robertphillips391395d2016-03-02 09:26:36 -0800787 result->resourcePriv().setUniqueKey(key);
788
robertphillipsc99b8f02016-05-15 07:53:35 -0700789 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000790
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000791 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000792}