blob: 975f56a7579741fcbdb8001ff17841ade82f7ce1 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
csmartdaltonc6f411e2016-08-05 22:32:12 -07002 * Copyright 2016 Google Inc.
robertphillips@google.com1e945b72012-04-16 18:03:03 +00003 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
csmartdaltonc6f411e2016-08-05 22:32:12 -07008#include "GrClipStackClip.h"
9
csmartdalton28341fa2016-08-17 10:00:21 -070010#include "GrAppliedClip.h"
robertphillips68737822015-10-29 12:12:21 -070011#include "GrDrawingManager.h"
robertphillips391395d2016-03-02 09:26:36 -080012#include "GrDrawContextPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070013#include "GrGpuResourcePriv.h"
csmartdalton28341fa2016-08-17 10:00:21 -070014#include "GrRenderTargetPriv.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070015#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000016#include "GrSWMaskHelper.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080017#include "effects/GrConvexPolyEffect.h"
18#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080019#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000020
bsalomon@google.com8182fa02012-12-04 14:06:06 +000021typedef SkClipStack::Element Element;
csmartdaltoncbecb082016-07-22 08:59:08 -070022typedef GrReducedClip::InitialState InitialState;
csmartdalton77f2fae2016-08-08 09:55:06 -070023typedef GrReducedClip::ElementList ElementList;
bsalomon@google.com51a62862012-11-26 21:19:43 +000024
robertphillips976f5f02016-06-03 10:59:20 -070025static const int kMaxAnalyticElements = 4;
26
csmartdaltonc6f411e2016-08-05 22:32:12 -070027bool GrClipStackClip::quickContains(const SkRect& rect) const {
28 if (!fStack) {
29 return true;
30 }
31 return fStack->quickContains(rect.makeOffset(SkIntToScalar(fOrigin.x()),
32 SkIntToScalar(fOrigin.y())));
33}
34
bsalomon7f0d9f32016-08-15 14:49:10 -070035bool GrClipStackClip::quickContains(const SkRRect& rrect) const {
36 if (!fStack) {
37 return true;
38 }
39 return fStack->quickContains(rrect.makeOffset(SkIntToScalar(fOrigin.fX),
40 SkIntToScalar(fOrigin.fY)));
41}
42
csmartdaltonc6f411e2016-08-05 22:32:12 -070043void GrClipStackClip::getConservativeBounds(int width, int height, SkIRect* devResult,
44 bool* isIntersectionOfRects) const {
45 if (!fStack) {
46 devResult->setXYWH(0, 0, width, height);
47 if (isIntersectionOfRects) {
48 *isIntersectionOfRects = true;
49 }
50 return;
51 }
52 SkRect devBounds;
53 fStack->getConservativeBounds(-fOrigin.x(), -fOrigin.y(), width, height, &devBounds,
54 isIntersectionOfRects);
55 devBounds.roundOut(devResult);
56}
57
bsalomon@google.com51a62862012-11-26 21:19:43 +000058////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000059// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000060// stage matrix this also alters the vertex layout
bungeman06ca8ec2016-06-09 08:01:03 -070061static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
62 const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000063 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080064 // We use device coords to compute the texture coordinates. We set our matrix to be a
65 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000066 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000067 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000068 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000069
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000070 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bungeman06ca8ec2016-06-09 08:01:03 -070071 return sk_sp<GrFragmentProcessor>(GrTextureDomainEffect::Make(
robertphillips5f2fa472016-05-19 11:36:25 -070072 result,
brianosman54f30c12016-07-18 10:53:52 -070073 nullptr,
bsalomon0ba8c242015-10-07 09:20:28 -070074 mat,
75 GrTextureDomain::MakeTexelDomain(result, domainTexels),
76 GrTextureDomain::kDecal_Mode,
77 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -070078 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000079}
80
robertphillips3f7357f2015-10-27 07:17:33 -070081// Does the path in 'element' require SW rendering? If so, return true (and,
82// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
83// 'prOut' to the non-SW path renderer that will do the job).
csmartdaltonc6f411e2016-08-05 22:32:12 -070084bool GrClipStackClip::PathNeedsSWRenderer(GrContext* context,
85 bool hasUserStencilSettings,
86 const GrDrawContext* drawContext,
87 const SkMatrix& viewMatrix,
88 const Element* element,
89 GrPathRenderer** prOut,
90 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070091 if (Element::kRect_Type == element->getType()) {
92 // rects can always be drawn directly w/o using the software path
93 // TODO: skip rrects once we're drawing them directly.
94 if (prOut) {
95 *prOut = nullptr;
96 }
97 return false;
98 } else {
99 // We shouldn't get here with an empty clip element.
100 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -0700101
robertphillips3f7357f2015-10-27 07:17:33 -0700102 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
103 SkPath path;
104 element->asPath(&path);
105 if (path.isInverseFillType()) {
106 path.toggleInverseFillType();
107 }
halcanary9d524f22016-03-29 09:03:52 -0700108
robertphillips3f7357f2015-10-27 07:17:33 -0700109 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -0700110
robertphillips423e3372015-10-27 09:23:38 -0700111 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -0700112 type = element->isAA()
113 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
114 : GrPathRendererChain::kStencilAndColor_DrawType;
115 } else {
116 type = element->isAA()
117 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700118 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700119 }
halcanary9d524f22016-03-29 09:03:52 -0700120
bsalomon8acedde2016-06-24 10:42:16 -0700121 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700122 GrPathRenderer::CanDrawPathArgs canDrawArgs;
123 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
124 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700125 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700126 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700127 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700128 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700129
robertphillips3f7357f2015-10-27 07:17:33 -0700130 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700131 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700132 if (prOut) {
133 *prOut = pr;
134 }
135 return SkToBool(!pr);
136 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000137}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000138
robertphillips@google.comfa662942012-05-17 12:20:22 +0000139/*
140 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
141 * will be used on any element. If so, it returns true to indicate that the
142 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
143 */
csmartdaltonc6f411e2016-08-05 22:32:12 -0700144bool GrClipStackClip::UseSWOnlyPath(GrContext* context,
145 bool hasUserStencilSettings,
146 const GrDrawContext* drawContext,
147 const SkVector& clipToMaskOffset,
csmartdalton77f2fae2016-08-08 09:55:06 -0700148 const ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000149 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000150 // a clip gets complex enough it can just be done in SW regardless
151 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000152
joshualitt8059eb92014-12-29 15:10:07 -0800153 // Set the matrix so that rendered clip elements are transformed to mask space from clip
154 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700155 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800156
csmartdalton77f2fae2016-08-08 09:55:06 -0700157 for (ElementList::Iter iter(elements); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000158 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700159
160 SkRegion::Op op = element->getOp();
161 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700162 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700163 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700164
robertphillips59cf61a2016-07-13 09:18:21 -0700165 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700166 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700167 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000168 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000169 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000170 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000171}
172
csmartdalton77f2fae2016-08-08 09:55:06 -0700173static bool get_analytic_clip_processor(const ElementList& elements,
robertphillips976f5f02016-06-03 10:59:20 -0700174 bool abortIfAA,
csmartdaltoncbecb082016-07-22 08:59:08 -0700175 const SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700176 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700177 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000178 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700179 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700180 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700181 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
csmartdalton77f2fae2016-08-08 09:55:06 -0700182 ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700183 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000184 SkRegion::Op op = iter.get()->getOp();
185 bool invert;
186 bool skip = false;
187 switch (op) {
188 case SkRegion::kReplace_Op:
189 SkASSERT(iter.get() == elements.head());
190 // Fallthrough, handled same as intersect.
191 case SkRegion::kIntersect_Op:
192 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700193 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000194 skip = true;
195 }
196 break;
197 case SkRegion::kDifference_Op:
198 invert = true;
199 // We don't currently have a cheap test for whether a rect is fully outside an
200 // element's primitive, so don't attempt to set skip.
201 break;
202 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700203 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000204 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000205 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700206 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800207 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700208 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700209 return false;
bsalomona912dde2015-10-14 15:01:50 -0700210 }
joshualittb0a8a372014-09-23 09:50:21 -0700211 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700212 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000213 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700214 edgeType =
215 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000216 }
bsalomona912dde2015-10-14 15:01:50 -0700217
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000218 switch (iter.get()->getType()) {
219 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700220 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
221 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000222 break;
223 case SkClipStack::Element::kRRect_Type: {
224 SkRRect rrect = iter.get()->getRRect();
225 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700226 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000227 break;
228 }
229 case SkClipStack::Element::kRect_Type: {
230 SkRect rect = iter.get()->getRect();
231 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700232 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000233 break;
234 }
235 default:
236 break;
237 }
bungeman06ca8ec2016-06-09 08:01:03 -0700238 if (!fps.back()) {
239 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000240 }
241 }
mtklein217daa72014-07-02 12:55:21 -0700242 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000243 }
244
bsalomon0b5b6b22015-10-14 08:31:34 -0700245 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700246 if (fps.count()) {
247 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000248 }
bungeman06ca8ec2016-06-09 08:01:03 -0700249 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000250}
251
robertphillips@google.comf294b772012-04-27 14:29:26 +0000252////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000253// sort out what kind of clip mask needs to be created: alpha, stencil,
254// scissor, or entirely software
csmartdaltond211e782016-08-15 11:17:19 -0700255bool GrClipStackClip::apply(GrContext* context, GrDrawContext* drawContext, bool useHWAA,
256 bool hasUserStencilSettings, GrAppliedClip* out) const {
csmartdaltonc6f411e2016-08-05 22:32:12 -0700257 if (!fStack || fStack->isWideOpen()) {
cdalton846c0512016-05-13 10:25:00 -0700258 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700259 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000260
csmartdaltoncbecb082016-07-22 08:59:08 -0700261 SkRect devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
csmartdaltond211e782016-08-15 11:17:19 -0700262 if (!devBounds.intersect(out->clippedDrawBounds())) {
csmartdaltoncbecb082016-07-22 08:59:08 -0700263 return false;
264 }
265
csmartdaltonc6f411e2016-08-05 22:32:12 -0700266 const SkScalar clipX = SkIntToScalar(fOrigin.x()),
267 clipY = SkIntToScalar(fOrigin.y());
csmartdaltoncbecb082016-07-22 08:59:08 -0700268
csmartdalton77f2fae2016-08-08 09:55:06 -0700269 SkRect clipSpaceDevBounds = devBounds.makeOffset(clipX, clipY);
270 const GrReducedClip reducedClip(*fStack, clipSpaceDevBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000271
csmartdaltond211e782016-08-15 11:17:19 -0700272 if (reducedClip.hasIBounds() &&
273 !GrClip::IsInsideClip(reducedClip.ibounds(), clipSpaceDevBounds)) {
274 SkIRect scissorSpaceIBounds(reducedClip.ibounds());
275 scissorSpaceIBounds.offset(-fOrigin);
276 out->addScissor(scissorSpaceIBounds);
cdalton846c0512016-05-13 10:25:00 -0700277 }
cdalton93a379b2016-05-11 13:58:08 -0700278
csmartdaltond211e782016-08-15 11:17:19 -0700279 if (reducedClip.elements().isEmpty()) {
280 return InitialState::kAllIn == reducedClip.initialState();
281 }
282
283 SkASSERT(reducedClip.hasIBounds());
284
csmartdalton28341fa2016-08-17 10:00:21 -0700285 // Attempt to implement difference clip rects with window rectangles. This will eventually
286 // become more comprehensive.
287 if (drawContext->accessRenderTarget()->renderTargetPriv().supportsWindowRectangles() &&
288 1 == reducedClip.elements().count() && !reducedClip.requiresAA() &&
289 InitialState::kAllIn == reducedClip.initialState()) {
290 const Element* element = reducedClip.elements().head();
291 SkRegion::Op op = element->getOp();
292 if (Element::kRect_Type == element->getType() &&
293 (SkRegion::kDifference_Op == op || SkRegion::kXOR_Op == op)) {
294 SkIRect window;
295 element->getRect().round(&window);
296 window.offset(-fOrigin);
297 out->addWindowRectangle(window);
298 return true;
299 }
300 }
301
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000302 // An element count of 4 was chosen because of the common pattern in Blink of:
303 // isect RR
304 // diff RR
305 // isect convex_poly
306 // isect convex_poly
307 // when drawing rounded div borders. This could probably be tuned based on a
308 // configuration's relative costs of switching RTs to generate a mask vs
309 // longer shaders.
csmartdalton77f2fae2016-08-08 09:55:06 -0700310 if (reducedClip.elements().count() <= kMaxAnalyticElements) {
cdaltonede75742015-11-11 15:27:57 -0800311 // When there are multiple samples we want to do per-sample clipping, not compute a
312 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700313 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
314 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700315 // With a single color sample, any coverage info is lost from color once it hits the
316 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
317 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700318 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700319 }
bungeman06ca8ec2016-06-09 08:01:03 -0700320 sk_sp<GrFragmentProcessor> clipFP;
csmartdalton77f2fae2016-08-08 09:55:06 -0700321 if (reducedClip.requiresAA() &&
322 get_analytic_clip_processor(reducedClip.elements(), disallowAnalyticAA,
323 {-clipX, -clipY}, devBounds, &clipFP)) {
csmartdaltond211e782016-08-15 11:17:19 -0700324 out->addCoverageFP(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000325 return true;
326 }
327 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000328
cdaltonede75742015-11-11 15:27:57 -0800329 // If the stencil buffer is multisampled we can use it to do everything.
csmartdalton77f2fae2016-08-08 09:55:06 -0700330 if (!drawContext->isStencilBufferMultisampled() && reducedClip.requiresAA()) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700331 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000332
joshualitt8059eb92014-12-29 15:10:07 -0800333 // The top-left of the mask corresponds to the top-left corner of the bounds.
334 SkVector clipToMaskOffset = {
csmartdalton77f2fae2016-08-08 09:55:06 -0700335 SkIntToScalar(-reducedClip.left()),
336 SkIntToScalar(-reducedClip.top())
joshualitt8059eb92014-12-29 15:10:07 -0800337 };
338
robertphillips59cf61a2016-07-13 09:18:21 -0700339 if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700340 clipToMaskOffset, reducedClip.elements())) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000341 // The clip geometry is complex enough that it will be more efficient to create it
342 // entirely in software
csmartdalton77f2fae2016-08-08 09:55:06 -0700343 result = CreateSoftwareClipMask(context->textureProvider(), reducedClip,
344 clipToMaskOffset);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000345 } else {
csmartdalton77f2fae2016-08-08 09:55:06 -0700346 result = CreateAlphaClipMask(context, reducedClip, clipToMaskOffset);
robertphillips391395d2016-03-02 09:26:36 -0800347 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700348 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000349 }
350
bsalomon49f085d2014-09-05 13:34:00 -0700351 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000352 // The mask's top left coord should be pinned to the rounded-out top left corner of
353 // clipSpace bounds. We determine the mask's position WRT to the render target here.
csmartdaltond211e782016-08-15 11:17:19 -0700354 SkIRect rtSpaceMaskBounds = reducedClip.ibounds();
csmartdaltonc6f411e2016-08-05 22:32:12 -0700355 rtSpaceMaskBounds.offset(-fOrigin);
csmartdaltond211e782016-08-15 11:17:19 -0700356 out->addCoverageFP(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000357 return true;
358 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000359 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000360 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000362 // use the stencil clip if we can't represent the clip as a rectangle.
csmartdaltonc6f411e2016-08-05 22:32:12 -0700363 SkIPoint clipSpaceToStencilSpaceOffset = -fOrigin;
csmartdalton77f2fae2016-08-08 09:55:06 -0700364 CreateStencilClipMask(context, drawContext, reducedClip, clipSpaceToStencilSpaceOffset);
csmartdaltond211e782016-08-15 11:17:19 -0700365 out->addStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000366 return true;
367}
368
robertphillips391395d2016-03-02 09:26:36 -0800369static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700370 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700371 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800372 const SkMatrix& viewMatrix,
373 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800374
375 // TODO: Draw rrects directly here.
376 switch (element->getType()) {
377 case Element::kEmpty_Type:
378 SkDEBUGFAIL("Should never get here with an empty element.");
379 break;
robertphillips391395d2016-03-02 09:26:36 -0800380 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700381 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800382 element->getOp(),
383 element->isInverseFilled(),
384 element->isAA(),
385 viewMatrix, element->getRect());
386 break;
robertphillips86c60752016-03-02 08:43:13 -0800387 default: {
388 SkPath path;
389 element->asPath(&path);
390 if (path.isInverseFillType()) {
391 path.toggleInverseFillType();
392 }
393
cdalton862cff32016-05-12 15:09:48 -0700394 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800395 element->getOp(),
396 element->isInverseFilled(),
397 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800398 break;
399 }
400 }
robertphillips391395d2016-03-02 09:26:36 -0800401
402 return false;
403}
404
405static void draw_element(GrDrawContext* dc,
406 const GrClip& clip, // TODO: can this just always be WideOpen?
407 const GrPaint &paint,
408 const SkMatrix& viewMatrix,
409 const SkClipStack::Element* element) {
410
411 // TODO: Draw rrects directly here.
412 switch (element->getType()) {
413 case Element::kEmpty_Type:
414 SkDEBUGFAIL("Should never get here with an empty element.");
415 break;
416 case Element::kRect_Type:
417 dc->drawRect(clip, paint, viewMatrix, element->getRect());
418 break;
419 default: {
420 SkPath path;
421 element->asPath(&path);
422 if (path.isInverseFillType()) {
423 path.toggleInverseFillType();
424 }
425
bsalomon6663acf2016-05-10 09:14:17 -0700426 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800427 break;
428 }
429 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000430}
431
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000432////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700433// Create a 8-bit clip mask in alpha
434
435static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
436 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
437 GrUniqueKey::Builder builder(key, kDomain, 3);
438 builder[0] = clipGenID;
439 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
440 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
441}
442
csmartdaltonc6f411e2016-08-05 22:32:12 -0700443sk_sp<GrTexture> GrClipStackClip::CreateAlphaClipMask(GrContext* context,
csmartdalton77f2fae2016-08-08 09:55:06 -0700444 const GrReducedClip& reducedClip,
445 const SkVector& clipToMaskOffset) {
robertphillips391395d2016-03-02 09:26:36 -0800446 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700447 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700448 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
bsalomon473addf2015-10-02 07:49:05 -0700449 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700450 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000451 }
452
robertphillips544b9aa2015-10-28 11:01:41 -0700453 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700454 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800455 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700456 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800457 }
458
robertphillips6738c702016-07-27 12:13:51 -0700459 sk_sp<GrDrawContext> dc(context->makeDrawContext(SkBackingFit::kApprox,
csmartdalton77f2fae2016-08-08 09:55:06 -0700460 reducedClip.width(),
461 reducedClip.height(),
robertphillips6738c702016-07-27 12:13:51 -0700462 config, nullptr));
robertphillips391395d2016-03-02 09:26:36 -0800463 if (!dc) {
464 return nullptr;
465 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700466
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000467 // The texture may be larger than necessary, this rect represents the part of the texture
468 // we populate with a rasterization of the clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700469 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000470
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000471 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
472 // clear the part that we care about.
csmartdaltond211e782016-08-15 11:17:19 -0700473 dc->clear(&maskSpaceIBounds, InitialState::kAllIn == reducedClip.initialState() ? -1 : 0, true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000474
robertphillips391395d2016-03-02 09:26:36 -0800475 // Set the matrix so that rendered clip elements are transformed to mask space from clip
476 // space.
477 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
478
479 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000480 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
481 // pass must not set values outside of this bounds or stencil values outside the rect won't be
482 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800483
robertphillips@google.comf294b772012-04-27 14:29:26 +0000484 // walk through each clip element and perform its set op
csmartdalton77f2fae2016-08-08 09:55:06 -0700485 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000486 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000487 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000488 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000489 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700490 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700491
robertphillips391395d2016-03-02 09:26:36 -0800492 // draw directly into the result with the stencil set to make the pixels affected
493 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700494 static constexpr GrUserStencilSettings kStencilInElement(
495 GrUserStencilSettings::StaticInit<
496 0xffff,
497 GrUserStencilTest::kAlways,
498 0xffff,
499 GrUserStencilOp::kReplace,
500 GrUserStencilOp::kReplace,
501 0xffff>()
502 );
cdalton862cff32016-05-12 15:09:48 -0700503 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800504 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800505 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000506 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000507
robertphillips391395d2016-03-02 09:26:36 -0800508 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700509 static constexpr GrUserStencilSettings kDrawOutsideElement(
510 GrUserStencilSettings::StaticInit<
511 0x0000,
512 GrUserStencilTest::kEqual,
513 0xffff,
514 GrUserStencilOp::kZero,
515 GrUserStencilOp::kZero,
516 0xffff>()
517 );
cdalton862cff32016-05-12 15:09:48 -0700518 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800519 op, !invert, false,
520 translate,
csmartdaltond211e782016-08-15 11:17:19 -0700521 SkRect::Make(reducedClip.ibounds()))) {
robertphillips391395d2016-03-02 09:26:36 -0800522 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000523 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000524 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800525 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800526 GrPaint paint;
527 paint.setAntiAlias(element->isAA());
528 paint.setCoverageSetOpXPFactory(op, false);
529
cdalton846c0512016-05-13 10:25:00 -0700530 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000531 }
532 }
533
robertphillipsc99b8f02016-05-15 07:53:35 -0700534 sk_sp<GrTexture> texture(dc->asTexture());
535 SkASSERT(texture);
536 texture->resourcePriv().setUniqueKey(key);
537 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000538}
539
540////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000541// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000542// (as opposed to canvas) coordinates
csmartdaltonc6f411e2016-08-05 22:32:12 -0700543bool GrClipStackClip::CreateStencilClipMask(GrContext* context,
544 GrDrawContext* drawContext,
csmartdalton77f2fae2016-08-08 09:55:06 -0700545 const GrReducedClip& reducedClip,
csmartdaltonc6f411e2016-08-05 22:32:12 -0700546 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700547 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000548
robertphillips976f5f02016-06-03 10:59:20 -0700549 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
550 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700551 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000552 return false;
553 }
554
robertphillips976f5f02016-06-03 10:59:20 -0700555 // TODO: these need to be swapped over to using a StencilAttachmentProxy
csmartdalton8d3f92a2016-08-17 09:39:38 -0700556 if (stencilAttachment->mustRenderClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
csmartdalton77f2fae2016-08-08 09:55:06 -0700557 clipSpaceToStencilOffset)) {
csmartdalton8d3f92a2016-08-17 09:39:38 -0700558 stencilAttachment->setLastClip(reducedClip.elementsGenID(), reducedClip.ibounds(),
csmartdalton77f2fae2016-08-08 09:55:06 -0700559 clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000560 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
561 SkVector translate = {
562 SkIntToScalar(clipSpaceToStencilOffset.fX),
563 SkIntToScalar(clipSpaceToStencilOffset.fY)
564 };
joshualitt8059eb92014-12-29 15:10:07 -0800565 SkMatrix viewMatrix;
566 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000567
bsalomon@google.com9f131742012-12-13 20:43:56 +0000568 // We set the current clip to the bounds so that our recursive draws are scissored to them.
csmartdaltond211e782016-08-15 11:17:19 -0700569 SkIRect stencilSpaceIBounds(reducedClip.ibounds());
bsalomon@google.com9f131742012-12-13 20:43:56 +0000570 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700571 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000572
csmartdaltond211e782016-08-15 11:17:19 -0700573 bool insideClip = InitialState::kAllIn == reducedClip.initialState();
csmartdalton77f2fae2016-08-08 09:55:06 -0700574 drawContext->drawContextPriv().clearStencilClip(stencilSpaceIBounds, insideClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000575
576 // walk through each clip element and perform its set op
577 // with the existing clip.
csmartdalton77f2fae2016-08-08 09:55:06 -0700578 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000579 const Element* element = iter.get();
csmartdalton656dbe42016-06-10 12:32:57 -0700580 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
joshualitt9853cce2014-11-17 14:22:48 -0800581
tomhudson@google.com8afae612012-08-14 15:03:35 +0000582 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000583 // enabled at bottom of loop
bsalomon6cc90062016-07-08 11:31:22 -0700584 clip.disableStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000585
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000586 // This will be used to determine whether the clip shape can be rendered into the
587 // stencil with arbitrary stencil settings.
588 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000589
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000590 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000591
halcanary96fcdcc2015-08-27 07:41:13 -0700592 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000593 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000594 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000595 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000596 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000597 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000598 element->asPath(&clipPath);
599 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000600 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000601 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000602 }
robertphillips68737822015-10-29 12:12:21 -0700603
bsalomon8acedde2016-06-24 10:42:16 -0700604 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700605 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700606 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700607 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700608 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700609 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700610 canDrawArgs.fHasUserStencilSettings = false;
611 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700612
csmartdaltonc6f411e2016-08-05 22:32:12 -0700613 GrDrawingManager* dm = context->drawingManager();
614 pr = dm->getPathRenderer(canDrawArgs, false,
615 GrPathRendererChain::kStencilOnly_DrawType,
616 &stencilSupport);
robertphillips976f5f02016-06-03 10:59:20 -0700617 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000618 return false;
619 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000620 }
621
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000622 bool canRenderDirectToStencil =
623 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700624 bool drawDirectToClip; // Given the renderer, the element,
625 // fill rule, and set operation should
626 // we render the element directly to
627 // stencil bit used for clipping.
628 GrUserStencilSettings const* const* stencilPasses =
629 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
630 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000631
632 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700633 if (!drawDirectToClip) {
634 static constexpr GrUserStencilSettings kDrawToStencil(
635 GrUserStencilSettings::StaticInit<
636 0x0000,
637 GrUserStencilTest::kAlways,
638 0xffff,
639 GrUserStencilOp::kIncMaybeClamp,
640 GrUserStencilOp::kIncMaybeClamp,
641 0xffff>()
642 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000643 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700644 drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA,
645 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000646 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000647 if (!clipPath.isEmpty()) {
bsalomon8acedde2016-06-24 10:42:16 -0700648 GrShape shape(clipPath, GrStyle::SimpleFill());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000649 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700650 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700651 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700652 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700653
654 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700655 args.fResourceProvider = context->resourceProvider();
656 args.fPaint = &paint;
657 args.fUserStencilSettings = &kDrawToStencil;
658 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700659 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700660 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700661 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700662 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700663 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700664 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000665 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700666 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700667 args.fResourceProvider = context->resourceProvider();
668 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700669 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700670 args.fViewMatrix = &viewMatrix;
robertphillips976f5f02016-06-03 10:59:20 -0700671 args.fIsAA = element->isAA();
bsalomon8acedde2016-06-24 10:42:16 -0700672 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700673 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000674 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000675 }
676 }
677 }
678
csmartdaltond211e782016-08-15 11:17:19 -0700679 // Just enable stencil clip. The passes choose whether or not they will actually use it.
680 clip.enableStencilClip();
681
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000682 // now we modify the clip bit by rendering either the clip
683 // element directly or a bounding rect of the entire clip.
cdalton93a379b2016-05-11 13:58:08 -0700684 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
cdalton93a379b2016-05-11 13:58:08 -0700685 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000686 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700687 drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix,
688 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000689 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700690 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips976f5f02016-06-03 10:59:20 -0700691 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700692 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700693 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700694 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700695 args.fResourceProvider = context->resourceProvider();
696 args.fPaint = &paint;
697 args.fUserStencilSettings = *pass;
698 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700699 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700700 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700701 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700702 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700703 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700704 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000705 }
706 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000707 // The view matrix is setup to do clip space -> stencil space translation, so
708 // draw rect in clip space.
csmartdalton656dbe42016-06-10 12:32:57 -0700709 drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix,
csmartdaltond211e782016-08-15 11:17:19 -0700710 SkRect::Make(reducedClip.ibounds()));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000711 }
712 }
713 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000714 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000715 return true;
716}
717
bsalomon@google.com411dad02012-06-05 20:24:20 +0000718////////////////////////////////////////////////////////////////////////////////
csmartdaltonc6f411e2016-08-05 22:32:12 -0700719sk_sp<GrTexture> GrClipStackClip::CreateSoftwareClipMask(GrTextureProvider* texProvider,
csmartdalton77f2fae2016-08-08 09:55:06 -0700720 const GrReducedClip& reducedClip,
721 const SkVector& clipToMaskOffset) {
bsalomon473addf2015-10-02 07:49:05 -0700722 GrUniqueKey key;
csmartdalton8d3f92a2016-08-17 09:39:38 -0700723 GetClipMaskKey(reducedClip.elementsGenID(), reducedClip.ibounds(), &key);
robertphillips0152d732016-05-20 06:38:43 -0700724 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700725 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000726 }
727
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000728 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
729 // the top left corner of the resulting rect to the top left of the texture.
csmartdalton77f2fae2016-08-08 09:55:06 -0700730 SkIRect maskSpaceIBounds = SkIRect::MakeWH(reducedClip.width(), reducedClip.height());
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000731
robertphillips0152d732016-05-20 06:38:43 -0700732 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000733
joshualitt8059eb92014-12-29 15:10:07 -0800734 // Set the matrix so that rendered clip elements are transformed to mask space from clip
735 // space.
736 SkMatrix translate;
737 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800738
robertphillips98377402016-05-13 05:47:23 -0700739 helper.init(maskSpaceIBounds, &translate);
csmartdaltond211e782016-08-15 11:17:19 -0700740 helper.clear(InitialState::kAllIn == reducedClip.initialState() ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000741
csmartdalton77f2fae2016-08-08 09:55:06 -0700742 for (ElementList::Iter iter(reducedClip.elements()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000743 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000744 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000745
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000746 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
747 // Intersect and reverse difference require modifying pixels outside of the geometry
748 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
749 // but leave the pixels inside the geometry alone. For reverse difference we invert all
750 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000751 if (SkRegion::kReverseDifference_Op == op) {
csmartdaltond211e782016-08-15 11:17:19 -0700752 SkRect temp = SkRect::Make(reducedClip.ibounds());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000753 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700754 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000755 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000756 SkPath clipPath;
757 element->asPath(&clipPath);
758 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700759 GrShape shape(clipPath, GrStyle::SimpleFill());
760 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000761 continue;
762 }
763
764 // The other ops (union, xor, diff) only affect pixels inside
765 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000766 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700767 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000768 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000769 SkPath path;
770 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700771 GrShape shape(path, GrStyle::SimpleFill());
772 helper.drawShape(shape, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000773 }
774 }
775
krajcevskiad1dc582014-06-10 15:06:47 -0700776 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800777 GrSurfaceDesc desc;
csmartdalton77f2fae2016-08-08 09:55:06 -0700778 desc.fWidth = reducedClip.width();
779 desc.fHeight = reducedClip.height();
robertphillips391395d2016-03-02 09:26:36 -0800780 desc.fConfig = kAlpha_8_GrPixelConfig;
781
robertphillips0152d732016-05-20 06:38:43 -0700782 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800783 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700784 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700785 }
robertphillips391395d2016-03-02 09:26:36 -0800786 result->resourcePriv().setUniqueKey(key);
787
robertphillipsc99b8f02016-05-15 07:53:35 -0700788 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000789
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000790 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000791}