blob: e117144eb93bdb4bd83f5c2f5d2abccf1ca5d5f7 [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
csmartdaltonc6f411e2016-08-05 22:32:12 -0700245bool GrClipStackClip::apply(GrContext* context,
246 GrDrawContext* drawContext,
247 const SkRect* origDevBounds,
248 bool useHWAA,
249 bool hasUserStencilSettings,
250 GrAppliedClip* out) const {
251 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700252 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700253 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000254
csmartdaltoncbecb082016-07-22 08:59:08 -0700255 SkRect devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
256 if (origDevBounds && !devBounds.intersect(*origDevBounds)) {
257 return false;
258 }
259
csmartdaltonc6f411e2016-08-05 22:32:12 -0700260 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
261 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700262
csmartdalton77f2fae2016-08-08 09:55:06 -0700263 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
264 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000265
csmartdalton77f2fae2016-08-08 09:55:06 -0700266 if (reducedClip.elements().isEmpty()) {
267 if (GrReducedClip::InitialState::kAllOut == reducedClip.initialState()) {
cdalton846c0512016-05-13 10:25:00 -0700268 return false;
269 }
csmartdalton77f2fae2016-08-08 09:55:06 -0700270 if (!GrClip::IsInsideClip(reducedClip.iBounds(), clipSpaceDevBounds)) {
271 SkIRect scissorSpaceIBounds(reducedClip.iBounds());
272 scissorSpaceIBounds.offset(-fOrigin);
273 out->makeScissored(scissorSpaceIBounds);
274 }
275 return true;
cdalton846c0512016-05-13 10:25:00 -0700276 }
cdalton93a379b2016-05-11 13:58:08 -0700277
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000278 // An element count of 4 was chosen because of the common pattern in Blink of:
279 // isect RR
280 // diff RR
281 // isect convex_poly
282 // isect convex_poly
283 // when drawing rounded div borders. This could probably be tuned based on a
284 // configuration's relative costs of switching RTs to generate a mask vs
285 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700286 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800287 // When there are multiple samples we want to do per-sample clipping, not compute a
288 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700289 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
290 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700291 // With a single color sample, any coverage info is lost from color once it hits the
292 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
293 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700294 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700295 }
bungeman06ca8ec2016-06-09 08:01:03 -0700296 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700297 if (reducedClip.requiresAA() &&
298 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
299 {-clipX, -clipY}, devBounds, &clipFP)) {
300 SkIRect scissorSpaceIBounds(reducedClip.iBounds());
csmartdaltonc6f411e2016-08-05 22:32:12 -0700301 scissorSpaceIBounds.offset(-fOrigin);
csmartdaltoncbecb082016-07-22 08:59:08 -0700302 if (GrClip::IsInsideClip(scissorSpaceIBounds, devBounds)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700303 out->makeFPBased(std::move(clipFP), SkRect::Make(scissorSpaceIBounds));
304 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700305 out->makeScissoredFPBased(std::move(clipFP), scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000306 }
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000307 return true;
308 }
309 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000310
cdaltonede75742015-11-11 15:27:57 -0800311 // If the stencil buffer is multisampled we can use it to do everything.
csmartdalton77f2fae2016-08-08 09:55:06 -0700312 if (!drawContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700313 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000314
joshualitt8059eb92014-12-29 15:10:07 -0800315 // The top-left of the mask corresponds to the top-left corner of the bounds.
316 SkVector clipToMaskOffset = {
csmartdalton77f2fae2016-08-08 09:55:06 -0700317 SkIntToScalar(-reducedClip.left()),
318 SkIntToScalar(-reducedClip.top())
joshualitt8059eb92014-12-29 15:10:07 -0800319 };
320
robertphillips59cf61a2016-07-13 09:18:21 -0700321 if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700322 clipToMaskOffset, reducedClip.elements())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000323 // The clip geometry is complex enough that it will be more efficient to create it
324 // entirely in software
csmartdalton77f2fae2016-08-08 09:55:06 -0700325 result = CreateSoftwareClipMask(context->textureProvider(), reducedClip,
326 clipToMaskOffset);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000327 } else {
csmartdalton77f2fae2016-08-08 09:55:06 -0700328 result = CreateAlphaClipMask(context, reducedClip, clipToMaskOffset);
robertphillips391395d2016-03-02 09:26:36 -0800329 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700330 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000331 }
332
bsalomon49f085d2014-09-05 13:34:00 -0700333 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000334 // The mask's top left coord should be pinned to the rounded-out top left corner of
335 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdalton77f2fae2016-08-08 09:55:06 -0700336 SkIRect rtSpaceMaskBounds = reducedClip.iBounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700337 rtSpaceMaskBounds.offset(-fOrigin);
bsalomon6cc90062016-07-08 11:31:22 -0700338 out->makeFPBased(create_fp_for_mask(result.get(), rtSpaceMaskBounds),
339 SkRect::Make(rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000340 return true;
341 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000342 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000343 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000344
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000345 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdaltonc6f411e2016-08-05 22:32:12 -0700346 SkIPoint clipSpaceToStencilSpaceOffset = -fOrigin;
csmartdalton77f2fae2016-08-08 09:55:06 -0700347 CreateStencilClipMask(context, drawContext, reducedClip, clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000348
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000349 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
350 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
351 // use both stencil and scissor test to the bounds for the final draw.
csmartdalton77f2fae2016-08-08 09:55:06 -0700352 if (GrClip::IsInsideClip(reducedClip.iBounds(), clipSpaceDevBounds)) {
csmartdalton97f6cd52016-07-13 13:37:08 -0700353 out->makeStencil(true, devBounds);
354 } else {
csmartdalton77f2fae2016-08-08 09:55:06 -0700355 SkIRect scissorSpaceIBounds(reducedClip.iBounds());
356 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
csmartdalton97f6cd52016-07-13 13:37:08 -0700357 out->makeScissoredStencil(scissorSpaceIBounds);
358 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000359 return true;
360}
361
robertphillips391395d2016-03-02 09:26:36 -0800362static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700363 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700364 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800365 const SkMatrix& viewMatrix,
366 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800367
368 // TODO: Draw rrects directly here.
369 switch (element->getType()) {
370 case Element::kEmpty_Type:
371 SkDEBUGFAIL("Should never get here with an empty element.");
372 break;
robertphillips391395d2016-03-02 09:26:36 -0800373 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700374 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800375 element->getOp(),
376 element->isInverseFilled(),
377 element->isAA(),
378 viewMatrix, element->getRect());
379 break;
robertphillips86c60752016-03-02 08:43:13 -0800380 default: {
381 SkPath path;
382 element->asPath(&path);
383 if (path.isInverseFillType()) {
384 path.toggleInverseFillType();
385 }
386
cdalton862cff32016-05-12 15:09:48 -0700387 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800388 element->getOp(),
389 element->isInverseFilled(),
390 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800391 break;
392 }
393 }
robertphillips391395d2016-03-02 09:26:36 -0800394
395 return false;
396}
397
398static void draw_element(GrDrawContext* dc,
399 const GrClip& clip, // TODO: can this just always be WideOpen?
400 const GrPaint &paint,
401 const SkMatrix& viewMatrix,
402 const SkClipStack::Element* element) {
403
404 // TODO: Draw rrects directly here.
405 switch (element->getType()) {
406 case Element::kEmpty_Type:
407 SkDEBUGFAIL("Should never get here with an empty element.");
408 break;
409 case Element::kRect_Type:
410 dc->drawRect(clip, paint, viewMatrix, element->getRect());
411 break;
412 default: {
413 SkPath path;
414 element->asPath(&path);
415 if (path.isInverseFillType()) {
416 path.toggleInverseFillType();
417 }
418
bsalomon6663acf2016-05-10 09:14:17 -0700419 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800420 break;
421 }
422 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000423}
424
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000425////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700426// Create a 8-bit clip mask in alpha
427
428static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
429 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
430 GrUniqueKey::Builder builder(key, kDomain, 3);
431 builder[0] = clipGenID;
432 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
433 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
434}
435
csmartdaltonc6f411e2016-08-05 22:32:12 -0700436sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdalton77f2fae2016-08-08 09:55:06 -0700437 const GrReducedClip& reducedClip,
438 const SkVector& clipToMaskOffset) {
robertphillips391395d2016-03-02 09:26:36 -0800439 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700440 GrUniqueKey key;
csmartdalton77f2fae2016-08-08 09:55:06 -0700441 GetClipMaskKey(reducedClip.genID(), reducedClip.iBounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700442 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700443 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000444 }
445
robertphillips544b9aa2015-10-28 11:01:41 -0700446 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700447 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800448 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700449 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800450 }
451
robertphillips6738c702016-07-27 12:13:51 -0700452 sk_sp<GrDrawContext> dc(context->makeDrawContext(SkBackingFit::kApprox,
csmartdalton77f2fae2016-08-08 09:55:06 -0700453 reducedClip.width(),
454 reducedClip.height(),
robertphillips6738c702016-07-27 12:13:51 -0700455 config, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800456 if (!dc) {
457 return nullptr;
458 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700459
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000460 // The texture may be larger than necessary, this rect represents the part of the texture
461 // we populate with a rasterization of the clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700462 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000463
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000464 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
465 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800466 dc->clear(&maskSpaceIBounds,
csmartdalton77f2fae2016-08-08 09:55:06 -0700467 GrReducedClip::InitialState::kAllIn == reducedClip.initialState() ? -1 : 0,
robertphillips391395d2016-03-02 09:26:36 -0800468 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000469
robertphillips391395d2016-03-02 09:26:36 -0800470 // Set the matrix so that rendered clip elements are transformed to mask space from clip
471 // space.
472 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
473
474 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000475 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
476 // pass must not set values outside of this bounds or stencil values outside the rect won't be
477 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800478
robertphillips@google.comf294b772012-04-27 14:29:26 +0000479 // walk through each clip element and perform its set op
csmartdalton77f2fae2016-08-08 09:55:06 -0700480 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000481 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000482 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000483 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000484 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700485 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700486
robertphillips391395d2016-03-02 09:26:36 -0800487 // draw directly into the result with the stencil set to make the pixels affected
488 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700489 static constexpr GrUserStencilSettings kStencilInElement(
490 GrUserStencilSettings::StaticInit<
491 0xffff,
492 GrUserStencilTest::kAlways,
493 0xffff,
494 GrUserStencilOp::kReplace,
495 GrUserStencilOp::kReplace,
496 0xffff>()
497 );
cdalton862cff32016-05-12 15:09:48 -0700498 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800499 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800500 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000501 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000502
robertphillips391395d2016-03-02 09:26:36 -0800503 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700504 static constexpr GrUserStencilSettings kDrawOutsideElement(
505 GrUserStencilSettings::StaticInit<
506 0x0000,
507 GrUserStencilTest::kEqual,
508 0xffff,
509 GrUserStencilOp::kZero,
510 GrUserStencilOp::kZero,
511 0xffff>()
512 );
cdalton862cff32016-05-12 15:09:48 -0700513 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800514 op, !invert, false,
515 translate,
csmartdalton77f2fae2016-08-08 09:55:06 -0700516 SkRect::Make(reducedClip.iBounds()))) {
robertphillips391395d2016-03-02 09:26:36 -0800517 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000518 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000519 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800520 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800521 GrPaint paint;
522 paint.setAntiAlias(element->isAA());
523 paint.setCoverageSetOpXPFactory(op, false);
524
cdalton846c0512016-05-13 10:25:00 -0700525 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000526 }
527 }
528
robertphillipsc99b8f02016-05-15 07:53:35 -0700529 sk_sp<GrTexture> texture(dc->asTexture());
530 SkASSERT(texture);
531 texture->resourcePriv().setUniqueKey(key);
532 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000533}
534
535////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000536// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000537// (as opposed to canvas) coordinates
csmartdaltonc6f411e2016-08-05 22:32:12 -0700538bool GrClipStackClip::CreateStencilClipMask(GrContext* context,
539 GrDrawContext* drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700540 const GrReducedClip& reducedClip,
csmartdaltonc6f411e2016-08-05 22:32:12 -0700541 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700542 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000543
robertphillips976f5f02016-06-03 10:59:20 -0700544 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
545 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700546 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000547 return false;
548 }
549
robertphillips976f5f02016-06-03 10:59:20 -0700550 // TODO: these need to be swapped over to using a StencilAttachmentProxy
csmartdalton77f2fae2016-08-08 09:55:06 -0700551 if (stencilAttachment->mustRenderClip(reducedClip.genID(), reducedClip.iBounds(),
552 clipSpaceToStencilOffset)) {
553 stencilAttachment->setLastClip(reducedClip.genID(), reducedClip.iBounds(),
554 clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000555 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
556 SkVector translate = {
557 SkIntToScalar(clipSpaceToStencilOffset.fX),
558 SkIntToScalar(clipSpaceToStencilOffset.fY)
559 };
joshualitt8059eb92014-12-29 15:10:07 -0800560 SkMatrix viewMatrix;
561 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000562
bsalomon@google.com9f131742012-12-13 20:43:56 +0000563 // We set the current clip to the bounds so that our recursive draws are scissored to them.
csmartdalton77f2fae2016-08-08 09:55:06 -0700564 SkIRect stencilSpaceIBounds(reducedClip.iBounds());
bsalomon@google.com9f131742012-12-13 20:43:56 +0000565 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700566 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000567
csmartdalton77f2fae2016-08-08 09:55:06 -0700568 bool insideClip = GrReducedClip::InitialState::kAllIn == reducedClip.initialState();
569 drawContext->drawContextPriv().clearStencilClip(stencilSpaceIBounds, insideClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000570
571 // walk through each clip element and perform its set op
572 // with the existing clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700573 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000574 const Element* element = iter.get();
csmartdalton656dbe42016-06-10 12:32:57 -0700575 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
joshualitt9853cce2014-11-17 14:22:48 -0800576
tomhudson@google.com8afae612012-08-14 15:03:35 +0000577 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000578 // enabled at bottom of loop
bsalomon6cc90062016-07-08 11:31:22 -0700579 clip.disableStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000580
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000581 // This will be used to determine whether the clip shape can be rendered into the
582 // stencil with arbitrary stencil settings.
583 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000584
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000585 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000586
halcanary96fcdcc2015-08-27 07:41:13 -0700587 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000588 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000589 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000590 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000591 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000592 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000593 element->asPath(&clipPath);
594 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000595 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000596 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000597 }
robertphillips68737822015-10-29 12:12:21 -0700598
bsalomon8acedde2016-06-24 10:42:16 -0700599 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700600 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700601 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700602 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700603 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700604 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700605 canDrawArgs.fHasUserStencilSettings = false;
606 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700607
csmartdaltonc6f411e2016-08-05 22:32:12 -0700608 GrDrawingManager* dm = context->drawingManager();
609 pr = dm->getPathRenderer(canDrawArgs, false,
610 GrPathRendererChain::kStencilOnly_DrawType,
611 &stencilSupport);
robertphillips976f5f02016-06-03 10:59:20 -0700612 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000613 return false;
614 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000615 }
616
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000617 bool canRenderDirectToStencil =
618 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700619 bool drawDirectToClip; // Given the renderer, the element,
620 // fill rule, and set operation should
621 // we render the element directly to
622 // stencil bit used for clipping.
623 GrUserStencilSettings const* const* stencilPasses =
624 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
625 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000626
627 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700628 if (!drawDirectToClip) {
629 static constexpr GrUserStencilSettings kDrawToStencil(
630 GrUserStencilSettings::StaticInit<
631 0x0000,
632 GrUserStencilTest::kAlways,
633 0xffff,
634 GrUserStencilOp::kIncMaybeClamp,
635 GrUserStencilOp::kIncMaybeClamp,
636 0xffff>()
637 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000638 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700639 drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA,
640 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000642 if (!clipPath.isEmpty()) {
bsalomon8acedde2016-06-24 10:42:16 -0700643 GrShape shape(clipPath, GrStyle::SimpleFill());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000644 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700645 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700646 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700647 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700648
649 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700650 args.fResourceProvider = context->resourceProvider();
651 args.fPaint = &paint;
652 args.fUserStencilSettings = &kDrawToStencil;
653 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700654 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700655 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700656 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700657 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700658 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700659 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000660 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700661 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700662 args.fResourceProvider = context->resourceProvider();
663 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700664 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700665 args.fViewMatrix = &viewMatrix;
robertphillips976f5f02016-06-03 10:59:20 -0700666 args.fIsAA = element->isAA();
bsalomon8acedde2016-06-24 10:42:16 -0700667 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700668 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000669 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000670 }
671 }
672 }
673
674 // now we modify the clip bit by rendering either the clip
675 // element directly or a bounding rect of the entire clip.
cdalton93a379b2016-05-11 13:58:08 -0700676 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
joshualitt9853cce2014-11-17 14:22:48 -0800677
cdalton93a379b2016-05-11 13:58:08 -0700678 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000679 if (Element::kRect_Type == element->getType()) {
bsalomon6cc90062016-07-08 11:31:22 -0700680 clip.enableStencilClip(element->getRect().makeOffset(translate.fX,
681 translate.fY));
csmartdalton656dbe42016-06-10 12:32:57 -0700682 drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix,
683 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000684 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700685 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips976f5f02016-06-03 10:59:20 -0700686 GrPaint paint;
bsalomon6cc90062016-07-08 11:31:22 -0700687 SkRect bounds = clipPath.getBounds();
688 bounds.offset(translate.fX, translate.fY);
689 clip.enableStencilClip(bounds);
bungeman06ca8ec2016-06-09 08:01:03 -0700690 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700691 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700692 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700693 args.fResourceProvider = context->resourceProvider();
694 args.fPaint = &paint;
695 args.fUserStencilSettings = *pass;
696 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700697 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700698 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700699 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700700 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700701 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700702 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000703 }
704 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000705 // The view matrix is setup to do clip space -> stencil space translation, so
706 // draw rect in clip space.
csmartdalton77f2fae2016-08-08 09:55:06 -0700707 SkRect bounds = SkRect::Make(reducedClip.iBounds());
bsalomon6cc90062016-07-08 11:31:22 -0700708 bounds.offset(translate.fX, translate.fY);
709 clip.enableStencilClip(bounds);
csmartdalton656dbe42016-06-10 12:32:57 -0700710 drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix,
csmartdalton77f2fae2016-08-08 09:55:06 -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;
csmartdalton77f2fae2016-08-08 09:55:06 -0700724 GetClipMaskKey(reducedClip.genID(), 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);
csmartdalton77f2fae2016-08-08 09:55:06 -0700741 helper.clear(GrReducedClip::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) {
csmartdalton77f2fae2016-08-08 09:55:06 -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}