blob: 0d6270f8d05e54036b374cc32b19978beac6f827 [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
bsalomon7f0d9f32016-08-15 14:49:10 -070033bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
34 if (!fStack) {
35 return true;
36 }
37 return fStack->quickContains(rrect.makeOffset(SkIntToScalar(fOrigin.fX),
38 SkIntToScalar(fOrigin.fY)));
39}
40
csmartdaltonc6f411e2016-08-05 22:32:12 -070041void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
42 bool* isIntersectionOfRects) const {
43 if (!fStack) {
44 devResult->setXYWH(0, 0, width, height);
45 if (isIntersectionOfRects) {
46 *isIntersectionOfRects = true;
47 }
48 return;
49 }
50 SkRect devBounds;
51 fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
52 isIntersectionOfRects);
53 devBounds.roundOut(devResult);
54}
55
bsalomon@google.com51a62862012-11-26 21:19:43 +000056////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000057// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000058// stage matrix this also alters the vertex layout
bungeman06ca8ec2016-06-09 08:01:03 -070059static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
60 const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000061 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080062 // We use device coords to compute the texture coordinates. We set our matrix to be a
63 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000064 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000065 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000066 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000067
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000068 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bungeman06ca8ec2016-06-09 08:01:03 -070069 return sk_sp<GrFragmentProcessor>(GrTextureDomainEffect::Make(
robertphillips5f2fa472016-05-19 11:36:25 -070070 result,
brianosman54f30c12016-07-18 10:53:52 -070071 nullptr,
bsalomon0ba8c242015-10-07 09:20:28 -070072 mat,
73 GrTextureDomain::MakeTexelDomain(result, domainTexels),
74 GrTextureDomain::kDecal_Mode,
75 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -070076 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000077}
78
robertphillips3f7357f2015-10-27 07:17:33 -070079// Does the path in 'element' require SW rendering? If so, return true (and,
80// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
81// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070082bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
83 bool hasUserStencilSettings,
84 const GrDrawContext* drawContext,
85 const SkMatrix& viewMatrix,
86 const Element* element,
87 GrPathRenderer** prOut,
88 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070089 if (Element::kRect_Type == element->getType()) {
90 // rects can always be drawn directly w/o using the software path
91 // TODO: skip rrects once we're drawing them directly.
92 if (prOut) {
93 *prOut = nullptr;
94 }
95 return false;
96 } else {
97 // We shouldn't get here with an empty clip element.
98 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070099
robertphillips3f7357f2015-10-27 07:17:33 -0700100 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
101 SkPath path;
102 element->asPath(&path);
103 if (path.isInverseFillType()) {
104 path.toggleInverseFillType();
105 }
halcanary9d524f22016-03-29 09:03:52 -0700106
robertphillips3f7357f2015-10-27 07:17:33 -0700107 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -0700108
robertphillips423e3372015-10-27 09:23:38 -0700109 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700110 type = element->isAA()
111 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
112 : GrPathRendererChain::kStencilAndColor_DrawType;
113 } else {
114 type = element->isAA()
115 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700116 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700117 }
halcanary9d524f22016-03-29 09:03:52 -0700118
bsalomon8acedde2016-06-24 10:42:16 -0700119 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700120 GrPathRenderer::CanDrawPathArgs canDrawArgs;
121 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
122 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700123 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700124 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700125 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700126 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700127
robertphillips3f7357f2015-10-27 07:17:33 -0700128 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700129 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700130 if (prOut) {
131 *prOut = pr;
132 }
133 return SkToBool(!pr);
134 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000135}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000136
robertphillips@google.comfa662942012-05-17 12:20:22 +0000137/*
138 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
139 * will be used on any element. If so, it returns true to indicate that the
140 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
141 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700142bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
143 bool hasUserStencilSettings,
144 const GrDrawContext* drawContext,
145 const SkVector& clipToMaskOffset,
csmartdalton77f2fae2016-08-08 09:55:06 -0700146 const ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000147 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000148 // a clip gets complex enough it can just be done in SW regardless
149 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000150
joshualitt8059eb92014-12-29 15:10:07 -0800151 // Set the matrix so that rendered clip elements are transformed to mask space from clip
152 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700153 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800154
csmartdalton77f2fae2016-08-08 09:55:06 -0700155 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000156 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700157
158 SkRegion::Op op = element->getOp();
159 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700160 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700161 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700162
robertphillips59cf61a2016-07-13 09:18:21 -0700163 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700164 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700165 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000166 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000167 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000168 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000169}
170
csmartdalton77f2fae2016-08-08 09:55:06 -0700171static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700172 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700173 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700174 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700175 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000176 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700177 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700178 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700179 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700180 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700181 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000182 SkRegion::Op op = iter.get()->getOp();
183 bool invert;
184 bool skip = false;
185 switch (op) {
186 case SkRegion::kReplace_Op:
187 SkASSERT(iter.get() == elements.head());
188 // Fallthrough, handled same as intersect.
189 case SkRegion::kIntersect_Op:
190 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700191 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000192 skip = true;
193 }
194 break;
195 case SkRegion::kDifference_Op:
196 invert = true;
197 // We don't currently have a cheap test for whether a rect is fully outside an
198 // element's primitive, so don't attempt to set skip.
199 break;
200 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700201 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000202 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000203 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700204 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800205 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700206 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700207 return false;
bsalomona912dde2015-10-14 15:01:50 -0700208 }
joshualittb0a8a372014-09-23 09:50:21 -0700209 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700210 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000211 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700212 edgeType =
213 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000214 }
bsalomona912dde2015-10-14 15:01:50 -0700215
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000216 switch (iter.get()->getType()) {
217 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700218 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
219 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000220 break;
221 case SkClipStack::Element::kRRect_Type: {
222 SkRRect rrect = iter.get()->getRRect();
223 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700224 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000225 break;
226 }
227 case SkClipStack::Element::kRect_Type: {
228 SkRect rect = iter.get()->getRect();
229 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700230 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000231 break;
232 }
233 default:
234 break;
235 }
bungeman06ca8ec2016-06-09 08:01:03 -0700236 if (!fps.back()) {
237 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000238 }
239 }
mtklein217daa72014-07-02 12:55:21 -0700240 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000241 }
242
bsalomon0b5b6b22015-10-14 08:31:34 -0700243 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700244 if (fps.count()) {
245 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000246 }
bungeman06ca8ec2016-06-09 08:01:03 -0700247 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000248}
249
robertphillips@google.comf294b772012-04-27 14:29:26 +0000250////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000251// sort out what kind of clip mask needs to be created: alpha, stencil,
252// scissor, or entirely software
csmartdaltond211e782016-08-15 11:17:19 -0700253bool GrClipStackClip::apply(GrContext* context, GrDrawContext* drawContext, bool useHWAA,
254 bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700255 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700256 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700257 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000258
csmartdaltoncbecb082016-07-22 08:59:08 -0700259 SkRect devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700260 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700261 return false;
262 }
263
csmartdaltonc6f411e2016-08-05 22:32:12 -0700264 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
265 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700266
csmartdalton77f2fae2016-08-08 09:55:06 -0700267 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
268 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000269
csmartdaltond211e782016-08-15 11:17:19 -0700270 if (reducedClip.hasIBounds() &&
271 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
272 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
273 scissorSpaceIBounds.offset(-fOrigin);
274 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700275 }
cdalton93a379b2016-05-11 13:58:08 -0700276
csmartdaltond211e782016-08-15 11:17:19 -0700277 if (reducedClip.elements().isEmpty()) {
278 return InitialState::kAllIn == reducedClip.initialState();
279 }
280
281 SkASSERT(reducedClip.hasIBounds());
282
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000283 // An element count of 4 was chosen because of the common pattern in Blink of:
284 // isect RR
285 // diff RR
286 // isect convex_poly
287 // isect convex_poly
288 // when drawing rounded div borders. This could probably be tuned based on a
289 // configuration's relative costs of switching RTs to generate a mask vs
290 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700291 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800292 // When there are multiple samples we want to do per-sample clipping, not compute a
293 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700294 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
295 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700296 // With a single color sample, any coverage info is lost from color once it hits the
297 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
298 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700299 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700300 }
bungeman06ca8ec2016-06-09 08:01:03 -0700301 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700302 if (reducedClip.requiresAA() &&
303 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
304 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700305 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000306 return true;
307 }
308 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000309
cdaltonede75742015-11-11 15:27:57 -0800310 // If the stencil buffer is multisampled we can use it to do everything.
csmartdalton77f2fae2016-08-08 09:55:06 -0700311 if (!drawContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700312 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000313
joshualitt8059eb92014-12-29 15:10:07 -0800314 // The top-left of the mask corresponds to the top-left corner of the bounds.
315 SkVector clipToMaskOffset = {
csmartdalton77f2fae2016-08-08 09:55:06 -0700316 SkIntToScalar(-reducedClip.left()),
317 SkIntToScalar(-reducedClip.top())
joshualitt8059eb92014-12-29 15:10:07 -0800318 };
319
robertphillips59cf61a2016-07-13 09:18:21 -0700320 if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700321 clipToMaskOffset, reducedClip.elements())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000322 // The clip geometry is complex enough that it will be more efficient to create it
323 // entirely in software
csmartdalton77f2fae2016-08-08 09:55:06 -0700324 result = CreateSoftwareClipMask(context->textureProvider(), reducedClip,
325 clipToMaskOffset);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000326 } else {
csmartdalton77f2fae2016-08-08 09:55:06 -0700327 result = CreateAlphaClipMask(context, reducedClip, clipToMaskOffset);
robertphillips391395d2016-03-02 09:26:36 -0800328 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700329 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000330 }
331
bsalomon49f085d2014-09-05 13:34:00 -0700332 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000333 // The mask's top left coord should be pinned to the rounded-out top left corner of
334 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700335 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700336 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700337 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000338 return true;
339 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000340 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000341 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000342
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000343 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdaltonc6f411e2016-08-05 22:32:12 -0700344 SkIPoint clipSpaceToStencilSpaceOffset = -fOrigin;
csmartdalton77f2fae2016-08-08 09:55:06 -0700345 CreateStencilClipMask(context, drawContext, reducedClip, clipSpaceToStencilSpaceOffset);
csmartdaltond211e782016-08-15 11:17:19 -0700346 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000347 return true;
348}
349
robertphillips391395d2016-03-02 09:26:36 -0800350static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700351 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700352 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800353 const SkMatrix& viewMatrix,
354 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800355
356 // TODO: Draw rrects directly here.
357 switch (element->getType()) {
358 case Element::kEmpty_Type:
359 SkDEBUGFAIL("Should never get here with an empty element.");
360 break;
robertphillips391395d2016-03-02 09:26:36 -0800361 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700362 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800363 element->getOp(),
364 element->isInverseFilled(),
365 element->isAA(),
366 viewMatrix, element->getRect());
367 break;
robertphillips86c60752016-03-02 08:43:13 -0800368 default: {
369 SkPath path;
370 element->asPath(&path);
371 if (path.isInverseFillType()) {
372 path.toggleInverseFillType();
373 }
374
cdalton862cff32016-05-12 15:09:48 -0700375 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800376 element->getOp(),
377 element->isInverseFilled(),
378 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800379 break;
380 }
381 }
robertphillips391395d2016-03-02 09:26:36 -0800382
383 return false;
384}
385
386static void draw_element(GrDrawContext* dc,
387 const GrClip& clip, // TODO: can this just always be WideOpen?
388 const GrPaint &paint,
389 const SkMatrix& viewMatrix,
390 const SkClipStack::Element* element) {
391
392 // TODO: Draw rrects directly here.
393 switch (element->getType()) {
394 case Element::kEmpty_Type:
395 SkDEBUGFAIL("Should never get here with an empty element.");
396 break;
397 case Element::kRect_Type:
398 dc->drawRect(clip, paint, viewMatrix, element->getRect());
399 break;
400 default: {
401 SkPath path;
402 element->asPath(&path);
403 if (path.isInverseFillType()) {
404 path.toggleInverseFillType();
405 }
406
bsalomon6663acf2016-05-10 09:14:17 -0700407 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800408 break;
409 }
410 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000411}
412
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000413////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700414// Create a 8-bit clip mask in alpha
415
416static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
417 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
418 GrUniqueKey::Builder builder(key, kDomain, 3);
419 builder[0] = clipGenID;
420 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
421 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
422}
423
csmartdaltonc6f411e2016-08-05 22:32:12 -0700424sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdalton77f2fae2016-08-08 09:55:06 -0700425 const GrReducedClip& reducedClip,
426 const SkVector& clipToMaskOffset) {
robertphillips391395d2016-03-02 09:26:36 -0800427 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700428 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700429 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700430 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700431 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000432 }
433
robertphillips544b9aa2015-10-28 11:01:41 -0700434 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700435 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800436 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700437 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800438 }
439
robertphillips6738c702016-07-27 12:13:51 -0700440 sk_sp<GrDrawContext> dc(context->makeDrawContext(SkBackingFit::kApprox,
csmartdalton77f2fae2016-08-08 09:55:06 -0700441 reducedClip.width(),
442 reducedClip.height(),
robertphillips6738c702016-07-27 12:13:51 -0700443 config, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800444 if (!dc) {
445 return nullptr;
446 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700447
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000448 // The texture may be larger than necessary, this rect represents the part of the texture
449 // we populate with a rasterization of the clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700450 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000451
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000452 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
453 // clear the part that we care about.
csmartdaltond211e782016-08-15 11:17:19 -0700454 dc->clear(&maskSpaceIBounds, InitialState::kAllIn == reducedClip.initialState() ? -1 : 0, true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000455
robertphillips391395d2016-03-02 09:26:36 -0800456 // Set the matrix so that rendered clip elements are transformed to mask space from clip
457 // space.
458 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
459
460 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000461 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
462 // pass must not set values outside of this bounds or stencil values outside the rect won't be
463 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800464
robertphillips@google.comf294b772012-04-27 14:29:26 +0000465 // walk through each clip element and perform its set op
csmartdalton77f2fae2016-08-08 09:55:06 -0700466 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000467 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000468 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000469 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000470 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700471 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700472
robertphillips391395d2016-03-02 09:26:36 -0800473 // draw directly into the result with the stencil set to make the pixels affected
474 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700475 static constexpr GrUserStencilSettings kStencilInElement(
476 GrUserStencilSettings::StaticInit<
477 0xffff,
478 GrUserStencilTest::kAlways,
479 0xffff,
480 GrUserStencilOp::kReplace,
481 GrUserStencilOp::kReplace,
482 0xffff>()
483 );
cdalton862cff32016-05-12 15:09:48 -0700484 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800485 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800486 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000487 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000488
robertphillips391395d2016-03-02 09:26:36 -0800489 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700490 static constexpr GrUserStencilSettings kDrawOutsideElement(
491 GrUserStencilSettings::StaticInit<
492 0x0000,
493 GrUserStencilTest::kEqual,
494 0xffff,
495 GrUserStencilOp::kZero,
496 GrUserStencilOp::kZero,
497 0xffff>()
498 );
cdalton862cff32016-05-12 15:09:48 -0700499 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800500 op, !invert, false,
501 translate,
csmartdaltond211e782016-08-15 11:17:19 -0700502 SkRect::Make(reducedClip.ibounds()))) {
robertphillips391395d2016-03-02 09:26:36 -0800503 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000504 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000505 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800506 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800507 GrPaint paint;
508 paint.setAntiAlias(element->isAA());
509 paint.setCoverageSetOpXPFactory(op, false);
510
cdalton846c0512016-05-13 10:25:00 -0700511 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000512 }
513 }
514
robertphillipsc99b8f02016-05-15 07:53:35 -0700515 sk_sp<GrTexture> texture(dc->asTexture());
516 SkASSERT(texture);
517 texture->resourcePriv().setUniqueKey(key);
518 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000519}
520
521////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000522// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000523// (as opposed to canvas) coordinates
csmartdaltonc6f411e2016-08-05 22:32:12 -0700524bool GrClipStackClip::CreateStencilClipMask(GrContext* context,
525 GrDrawContext* drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700526 const GrReducedClip& reducedClip,
csmartdaltonc6f411e2016-08-05 22:32:12 -0700527 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700528 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000529
robertphillips976f5f02016-06-03 10:59:20 -0700530 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
531 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700532 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000533 return false;
534 }
535
robertphillips976f5f02016-06-03 10:59:20 -0700536 // TODO: these need to be swapped over to using a StencilAttachmentProxy
csmartdalton8d3f92a2016-08-17 09:39:38 -0700537 if (stencilAttachment->mustRenderClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
csmartdalton77f2fae2016-08-08 09:55:06 -0700538 clipSpaceToStencilOffset)) {
csmartdalton8d3f92a2016-08-17 09:39:38 -0700539 stencilAttachment->setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
csmartdalton77f2fae2016-08-08 09:55:06 -0700540 clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000541 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
542 SkVector translate = {
543 SkIntToScalar(clipSpaceToStencilOffset.fX),
544 SkIntToScalar(clipSpaceToStencilOffset.fY)
545 };
joshualitt8059eb92014-12-29 15:10:07 -0800546 SkMatrix viewMatrix;
547 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000548
bsalomon@google.com9f131742012-12-13 20:43:56 +0000549 // We set the current clip to the bounds so that our recursive draws are scissored to them.
csmartdaltond211e782016-08-15 11:17:19 -0700550 SkIRect stencilSpaceIBounds(reducedClip.ibounds());
bsalomon@google.com9f131742012-12-13 20:43:56 +0000551 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700552 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000553
csmartdaltond211e782016-08-15 11:17:19 -0700554 bool insideClip = InitialState::kAllIn == reducedClip.initialState();
csmartdalton77f2fae2016-08-08 09:55:06 -0700555 drawContext->drawContextPriv().clearStencilClip(stencilSpaceIBounds, insideClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000556
557 // walk through each clip element and perform its set op
558 // with the existing clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700559 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 const Element* element = iter.get();
csmartdalton656dbe42016-06-10 12:32:57 -0700561 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
joshualitt9853cce2014-11-17 14:22:48 -0800562
tomhudson@google.com8afae612012-08-14 15:03:35 +0000563 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000564 // enabled at bottom of loop
bsalomon6cc90062016-07-08 11:31:22 -0700565 clip.disableStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000566
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000567 // This will be used to determine whether the clip shape can be rendered into the
568 // stencil with arbitrary stencil settings.
569 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000570
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000571 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000572
halcanary96fcdcc2015-08-27 07:41:13 -0700573 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000574 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000575 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000576 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000577 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000578 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000579 element->asPath(&clipPath);
580 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000581 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000582 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000583 }
robertphillips68737822015-10-29 12:12:21 -0700584
bsalomon8acedde2016-06-24 10:42:16 -0700585 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700586 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700587 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700588 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700589 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700590 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700591 canDrawArgs.fHasUserStencilSettings = false;
592 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700593
csmartdaltonc6f411e2016-08-05 22:32:12 -0700594 GrDrawingManager* dm = context->drawingManager();
595 pr = dm->getPathRenderer(canDrawArgs, false,
596 GrPathRendererChain::kStencilOnly_DrawType,
597 &stencilSupport);
robertphillips976f5f02016-06-03 10:59:20 -0700598 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000599 return false;
600 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000601 }
602
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000603 bool canRenderDirectToStencil =
604 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700605 bool drawDirectToClip; // Given the renderer, the element,
606 // fill rule, and set operation should
607 // we render the element directly to
608 // stencil bit used for clipping.
609 GrUserStencilSettings const* const* stencilPasses =
610 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
611 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000612
613 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700614 if (!drawDirectToClip) {
615 static constexpr GrUserStencilSettings kDrawToStencil(
616 GrUserStencilSettings::StaticInit<
617 0x0000,
618 GrUserStencilTest::kAlways,
619 0xffff,
620 GrUserStencilOp::kIncMaybeClamp,
621 GrUserStencilOp::kIncMaybeClamp,
622 0xffff>()
623 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000624 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700625 drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA,
626 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000627 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000628 if (!clipPath.isEmpty()) {
bsalomon8acedde2016-06-24 10:42:16 -0700629 GrShape shape(clipPath, GrStyle::SimpleFill());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000630 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700631 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700632 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700633 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700634
635 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700636 args.fResourceProvider = context->resourceProvider();
637 args.fPaint = &paint;
638 args.fUserStencilSettings = &kDrawToStencil;
639 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700640 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700641 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700642 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700643 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700644 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700645 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000646 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700647 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700648 args.fResourceProvider = context->resourceProvider();
649 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700650 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700651 args.fViewMatrix = &viewMatrix;
robertphillips976f5f02016-06-03 10:59:20 -0700652 args.fIsAA = element->isAA();
bsalomon8acedde2016-06-24 10:42:16 -0700653 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700654 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000655 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000656 }
657 }
658 }
659
csmartdaltond211e782016-08-15 11:17:19 -0700660 // Just enable stencil clip. The passes choose whether or not they will actually use it.
661 clip.enableStencilClip();
662
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000663 // now we modify the clip bit by rendering either the clip
664 // element directly or a bounding rect of the entire clip.
cdalton93a379b2016-05-11 13:58:08 -0700665 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
cdalton93a379b2016-05-11 13:58:08 -0700666 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000667 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700668 drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix,
669 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000670 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700671 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips976f5f02016-06-03 10:59:20 -0700672 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700673 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700674 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700675 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700676 args.fResourceProvider = context->resourceProvider();
677 args.fPaint = &paint;
678 args.fUserStencilSettings = *pass;
679 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700680 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700681 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700682 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700683 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700684 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700685 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000686 }
687 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000688 // The view matrix is setup to do clip space -> stencil space translation, so
689 // draw rect in clip space.
csmartdalton656dbe42016-06-10 12:32:57 -0700690 drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix,
csmartdaltond211e782016-08-15 11:17:19 -0700691 SkRect::Make(reducedClip.ibounds()));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000692 }
693 }
694 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000695 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000696 return true;
697}
698
bsalomon@google.com411dad02012-06-05 20:24:20 +0000699////////////////////////////////////////////////////////////////////////////////
csmartdaltonc6f411e2016-08-05 22:32:12 -0700700sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
csmartdalton77f2fae2016-08-08 09:55:06 -0700701 const GrReducedClip& reducedClip,
702 const SkVector& clipToMaskOffset) {
bsalomon473addf2015-10-02 07:49:05 -0700703 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700704 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
robertphillips0152d732016-05-20 06:38:43 -0700705 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700706 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000707 }
708
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000709 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
710 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700711 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000712
robertphillips0152d732016-05-20 06:38:43 -0700713 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000714
joshualitt8059eb92014-12-29 15:10:07 -0800715 // Set the matrix so that rendered clip elements are transformed to mask space from clip
716 // space.
717 SkMatrix translate;
718 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800719
robertphillips98377402016-05-13 05:47:23 -0700720 helper.init(maskSpaceIBounds, &translate);
csmartdaltond211e782016-08-15 11:17:19 -0700721 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000722
csmartdalton77f2fae2016-08-08 09:55:06 -0700723 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000724 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000725 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000726
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000727 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
728 // Intersect and reverse difference require modifying pixels outside of the geometry
729 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
730 // but leave the pixels inside the geometry alone. For reverse difference we invert all
731 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000732 if (SkRegion::kReverseDifference_Op == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700733 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000734 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700735 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000736 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000737 SkPath clipPath;
738 element->asPath(&clipPath);
739 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700740 GrShape shape(clipPath, GrStyle::SimpleFill());
741 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000742 continue;
743 }
744
745 // The other ops (union, xor, diff) only affect pixels inside
746 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000747 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700748 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000749 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000750 SkPath path;
751 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700752 GrShape shape(path, GrStyle::SimpleFill());
753 helper.drawShape(shape, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000754 }
755 }
756
krajcevskiad1dc582014-06-10 15:06:47 -0700757 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800758 GrSurfaceDesc desc;
csmartdalton77f2fae2016-08-08 09:55:06 -0700759 desc.fWidth = reducedClip.width();
760 desc.fHeight = reducedClip.height();
robertphillips391395d2016-03-02 09:26:36 -0800761 desc.fConfig = kAlpha_8_GrPixelConfig;
762
robertphillips0152d732016-05-20 06:38:43 -0700763 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800764 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700765 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700766 }
robertphillips391395d2016-03-02 09:26:36 -0800767 result->resourcePriv().setUniqueKey(key);
768
robertphillipsc99b8f02016-05-15 07:53:35 -0700769 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000770
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000771 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000772}