blob: e5f13309864ebf05862c6c46099e7bee6f054bde [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrClipMaskManager.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -07009#include "GrCaps.h"
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"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrPaint.h"
14#include "GrPathRenderer.h"
15#include "GrRenderTarget.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080016#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070017#include "GrResourceProvider.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070020#include "SkRasterClip.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070021#include "SkTLazy.h"
joshualitta8b84992016-01-13 13:35:35 -080022#include "batches/GrRectBatchFactory.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080023#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080024#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080025#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080026#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000027
bsalomon@google.com8182fa02012-12-04 14:06:06 +000028typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000029
robertphillips976f5f02016-06-03 10:59:20 -070030static const int kMaxAnalyticElements = 4;
31
bsalomon@google.com51a62862012-11-26 21:19:43 +000032////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000033// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000034// stage matrix this also alters the vertex layout
bungeman06ca8ec2016-06-09 08:01:03 -070035static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
36 const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000037 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080038 // We use device coords to compute the texture coordinates. We set our matrix to be a
39 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000041 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000042 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000043
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000044 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bungeman06ca8ec2016-06-09 08:01:03 -070045 return sk_sp<GrFragmentProcessor>(GrTextureDomainEffect::Make(
robertphillips5f2fa472016-05-19 11:36:25 -070046 result,
bsalomon0ba8c242015-10-07 09:20:28 -070047 mat,
48 GrTextureDomain::MakeTexelDomain(result, domainTexels),
49 GrTextureDomain::kDecal_Mode,
50 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -070051 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000052}
53
robertphillips3f7357f2015-10-27 07:17:33 -070054// Does the path in 'element' require SW rendering? If so, return true (and,
55// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
56// 'prOut' to the non-SW path renderer that will do the job).
robertphillips68737822015-10-29 12:12:21 -070057bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
cdalton93a379b2016-05-11 13:58:08 -070058 bool hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070059 const GrDrawContext* drawContext,
robertphillips68737822015-10-29 12:12:21 -070060 const SkMatrix& viewMatrix,
61 const Element* element,
62 GrPathRenderer** prOut,
63 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070064 if (Element::kRect_Type == element->getType()) {
65 // rects can always be drawn directly w/o using the software path
66 // TODO: skip rrects once we're drawing them directly.
67 if (prOut) {
68 *prOut = nullptr;
69 }
70 return false;
71 } else {
72 // We shouldn't get here with an empty clip element.
73 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070074
robertphillips3f7357f2015-10-27 07:17:33 -070075 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
76 SkPath path;
77 element->asPath(&path);
78 if (path.isInverseFillType()) {
79 path.toggleInverseFillType();
80 }
halcanary9d524f22016-03-29 09:03:52 -070081
robertphillips3f7357f2015-10-27 07:17:33 -070082 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -070083
robertphillips423e3372015-10-27 09:23:38 -070084 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070085 type = element->isAA()
86 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
87 : GrPathRendererChain::kStencilAndColor_DrawType;
88 } else {
89 type = element->isAA()
90 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -070091 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -070092 }
halcanary9d524f22016-03-29 09:03:52 -070093
bsalomon8acedde2016-06-24 10:42:16 -070094 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -070095 GrPathRenderer::CanDrawPathArgs canDrawArgs;
96 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
97 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -070098 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -070099 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700100 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700101 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700102
robertphillips3f7357f2015-10-27 07:17:33 -0700103 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700104 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700105 if (prOut) {
106 *prOut = pr;
107 }
108 return SkToBool(!pr);
109 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000110}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000111
robertphillips@google.comfa662942012-05-17 12:20:22 +0000112/*
113 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
114 * will be used on any element. If so, it returns true to indicate that the
115 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
116 */
robertphillips391395d2016-03-02 09:26:36 -0800117bool GrClipMaskManager::UseSWOnlyPath(GrContext* context,
118 const GrPipelineBuilder& pipelineBuilder,
robertphillips976f5f02016-06-03 10:59:20 -0700119 const GrDrawContext* drawContext,
joshualitt8059eb92014-12-29 15:10:07 -0800120 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800121 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000122 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000123 // a clip gets complex enough it can just be done in SW regardless
124 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000125
joshualitt8059eb92014-12-29 15:10:07 -0800126 // Set the matrix so that rendered clip elements are transformed to mask space from clip
127 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700128 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800129
tfarinabf54e492014-10-23 17:47:18 -0700130 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000131 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700132
133 SkRegion::Op op = element->getOp();
134 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700135 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700136 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700137
cdalton93a379b2016-05-11 13:58:08 -0700138 if (PathNeedsSWRenderer(context, pipelineBuilder.hasUserStencilSettings(),
robertphillips976f5f02016-06-03 10:59:20 -0700139 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700140 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000141 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000142 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000143 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000144}
145
robertphillips976f5f02016-06-03 10:59:20 -0700146static bool get_analytic_clip_processor(const GrReducedClip::ElementList& elements,
147 bool abortIfAA,
148 SkVector& clipToRTOffset,
149 const SkRect* drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700150 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000151 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700152 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000153 boundsInClipSpace = *drawBounds;
154 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
155 }
bsalomon0ba8c242015-10-07 09:20:28 -0700156 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700157 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
tfarinabf54e492014-10-23 17:47:18 -0700158 GrReducedClip::ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700159 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000160 SkRegion::Op op = iter.get()->getOp();
161 bool invert;
162 bool skip = false;
163 switch (op) {
164 case SkRegion::kReplace_Op:
165 SkASSERT(iter.get() == elements.head());
166 // Fallthrough, handled same as intersect.
167 case SkRegion::kIntersect_Op:
168 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700169 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000170 skip = true;
171 }
172 break;
173 case SkRegion::kDifference_Op:
174 invert = true;
175 // We don't currently have a cheap test for whether a rect is fully outside an
176 // element's primitive, so don't attempt to set skip.
177 break;
178 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700179 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000180 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000181 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700182 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800183 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700184 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700185 return false;
bsalomona912dde2015-10-14 15:01:50 -0700186 }
joshualittb0a8a372014-09-23 09:50:21 -0700187 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700188 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000189 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700190 edgeType =
191 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000192 }
bsalomona912dde2015-10-14 15:01:50 -0700193
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000194 switch (iter.get()->getType()) {
195 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700196 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
197 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000198 break;
199 case SkClipStack::Element::kRRect_Type: {
200 SkRRect rrect = iter.get()->getRRect();
201 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700202 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000203 break;
204 }
205 case SkClipStack::Element::kRect_Type: {
206 SkRect rect = iter.get()->getRect();
207 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700208 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000209 break;
210 }
211 default:
212 break;
213 }
bungeman06ca8ec2016-06-09 08:01:03 -0700214 if (!fps.back()) {
215 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000216 }
217 }
mtklein217daa72014-07-02 12:55:21 -0700218 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000219 }
220
bsalomon0b5b6b22015-10-14 08:31:34 -0700221 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700222 if (fps.count()) {
223 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000224 }
bungeman06ca8ec2016-06-09 08:01:03 -0700225 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000226}
227
robertphillips@google.comf294b772012-04-27 14:29:26 +0000228////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000229// sort out what kind of clip mask needs to be created: alpha, stencil,
230// scissor, or entirely software
robertphillips976f5f02016-06-03 10:59:20 -0700231bool GrClipMaskManager::SetupClipping(GrContext* context,
232 const GrPipelineBuilder& pipelineBuilder,
233 GrDrawContext* drawContext,
cdalton846c0512016-05-13 10:25:00 -0700234 const GrClipStackClip& clip,
bsalomon0ba8c242015-10-07 09:20:28 -0700235 const SkRect* devBounds,
236 GrAppliedClip* out) {
cdalton846c0512016-05-13 10:25:00 -0700237 if (!clip.clipStack() || clip.clipStack()->isWideOpen()) {
238 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700239 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000240
bsalomonf045d602015-11-18 19:01:12 -0800241 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800242 int32_t genID = 0;
243 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000244 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800245 bool requiresAA = false;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000246
robertphillips976f5f02016-06-03 10:59:20 -0700247 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(drawContext->width(), drawContext->height());
cdalton846c0512016-05-13 10:25:00 -0700248 clipSpaceRTIBounds.offset(clip.origin());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000249
cdalton846c0512016-05-13 10:25:00 -0700250 SkIRect clipSpaceReduceQueryBounds;
251#define DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION 0
252 if (devBounds && !DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION) {
253 SkIRect devIBounds = devBounds->roundOut();
254 devIBounds.offset(clip.origin());
255 if (!clipSpaceReduceQueryBounds.intersect(clipSpaceRTIBounds, devIBounds)) {
bsalomon9ce30e12015-03-06 08:42:34 -0800256 return false;
257 }
cdalton846c0512016-05-13 10:25:00 -0700258 } else {
259 clipSpaceReduceQueryBounds = clipSpaceRTIBounds;
bsalomon96e02a82015-03-06 07:13:01 -0800260 }
cdalton846c0512016-05-13 10:25:00 -0700261 GrReducedClip::ReduceClipStack(*clip.clipStack(),
262 clipSpaceReduceQueryBounds,
263 &elements,
264 &genID,
265 &initialState,
266 &clipSpaceIBounds,
267 &requiresAA);
268 if (elements.isEmpty()) {
269 if (GrReducedClip::kAllIn_InitialState == initialState) {
270 if (clipSpaceIBounds == clipSpaceRTIBounds) {
271 return true;
272 }
273 } else {
274 return false;
275 }
276 }
cdalton93a379b2016-05-11 13:58:08 -0700277
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000278 // An element count of 4 was chosen because of the common pattern in Blink of:
279 // isect RR
280 // diff RR
281 // isect convex_poly
282 // isect convex_poly
283 // when drawing rounded div borders. This could probably be tuned based on a
284 // configuration's relative costs of switching RTs to generate a mask vs
285 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700286 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800287 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
288 SkIntToScalar(-clip.origin().fY) };
cdaltonede75742015-11-11 15:27:57 -0800289 // When there are multiple samples we want to do per-sample clipping, not compute a
290 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700291 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
292 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700293 // With a single color sample, any coverage info is lost from color once it hits the
294 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
295 // is multisampled.
296 disallowAnalyticAA = pipelineBuilder.isHWAntialias() ||
cdalton93a379b2016-05-11 13:58:08 -0700297 pipelineBuilder.hasUserStencilSettings();
cdalton3ccf2e72016-05-06 09:41:16 -0700298 }
bungeman06ca8ec2016-06-09 08:01:03 -0700299 sk_sp<GrFragmentProcessor> clipFP;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000300 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700301 (requiresAA &&
robertphillips976f5f02016-06-03 10:59:20 -0700302 get_analytic_clip_processor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
303 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700304 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800305 scissorSpaceIBounds.offset(-clip.origin());
robertphillips976f5f02016-06-03 10:59:20 -0700306 if (!devBounds || !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
bungeman06ca8ec2016-06-09 08:01:03 -0700307 out->makeScissoredFPBased(std::move(clipFP), scissorSpaceIBounds);
robertphillips5f2fa472016-05-19 11:36:25 -0700308 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000309 }
bungeman06ca8ec2016-06-09 08:01:03 -0700310 out->makeFPBased(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000311 return true;
312 }
313 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000314
cdaltonede75742015-11-11 15:27:57 -0800315 // If the stencil buffer is multisampled we can use it to do everything.
robertphillips976f5f02016-06-03 10:59:20 -0700316 if (!drawContext->isStencilBufferMultisampled() && requiresAA) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700317 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000318
joshualitt8059eb92014-12-29 15:10:07 -0800319 // The top-left of the mask corresponds to the top-left corner of the bounds.
320 SkVector clipToMaskOffset = {
321 SkIntToScalar(-clipSpaceIBounds.fLeft),
322 SkIntToScalar(-clipSpaceIBounds.fTop)
323 };
324
robertphillips976f5f02016-06-03 10:59:20 -0700325 if (UseSWOnlyPath(context, pipelineBuilder, drawContext,
326 clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000327 // The clip geometry is complex enough that it will be more efficient to create it
328 // entirely in software
robertphillips976f5f02016-06-03 10:59:20 -0700329 result = CreateSoftwareClipMask(context->textureProvider(),
robertphillipsc99b8f02016-05-15 07:53:35 -0700330 genID,
331 initialState,
332 elements,
333 clipToMaskOffset,
334 clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000335 } else {
robertphillips976f5f02016-06-03 10:59:20 -0700336 result = CreateAlphaClipMask(context,
robertphillipsc99b8f02016-05-15 07:53:35 -0700337 genID,
338 initialState,
339 elements,
340 clipToMaskOffset,
341 clipSpaceIBounds);
robertphillips391395d2016-03-02 09:26:36 -0800342 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700343 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000344 }
345
bsalomon49f085d2014-09-05 13:34:00 -0700346 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000347 // The mask's top left coord should be pinned to the rounded-out top left corner of
348 // clipSpace bounds. We determine the mask's position WRT to the render target here.
349 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800350 rtSpaceMaskBounds.offset(-clip.origin());
robertphillips5f2fa472016-05-19 11:36:25 -0700351 out->makeFPBased(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000352 return true;
353 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000354 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000356
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000357 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800358 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
robertphillips976f5f02016-06-03 10:59:20 -0700359 CreateStencilClipMask(context,
360 drawContext,
361 genID,
362 initialState,
363 elements,
364 clipSpaceIBounds,
365 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000366
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000367 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
368 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
369 // use both stencil and scissor test to the bounds for the final draw.
370 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
371 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
robertphillips5f2fa472016-05-19 11:36:25 -0700372 out->makeScissoredStencil(true, scissorSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000373 return true;
374}
375
robertphillips391395d2016-03-02 09:26:36 -0800376static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700377 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700378 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800379 const SkMatrix& viewMatrix,
380 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800381
382 // TODO: Draw rrects directly here.
383 switch (element->getType()) {
384 case Element::kEmpty_Type:
385 SkDEBUGFAIL("Should never get here with an empty element.");
386 break;
robertphillips391395d2016-03-02 09:26:36 -0800387 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700388 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800389 element->getOp(),
390 element->isInverseFilled(),
391 element->isAA(),
392 viewMatrix, element->getRect());
393 break;
robertphillips86c60752016-03-02 08:43:13 -0800394 default: {
395 SkPath path;
396 element->asPath(&path);
397 if (path.isInverseFillType()) {
398 path.toggleInverseFillType();
399 }
400
cdalton862cff32016-05-12 15:09:48 -0700401 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800402 element->getOp(),
403 element->isInverseFilled(),
404 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800405 break;
406 }
407 }
robertphillips391395d2016-03-02 09:26:36 -0800408
409 return false;
410}
411
412static void draw_element(GrDrawContext* dc,
413 const GrClip& clip, // TODO: can this just always be WideOpen?
414 const GrPaint &paint,
415 const SkMatrix& viewMatrix,
416 const SkClipStack::Element* element) {
417
418 // TODO: Draw rrects directly here.
419 switch (element->getType()) {
420 case Element::kEmpty_Type:
421 SkDEBUGFAIL("Should never get here with an empty element.");
422 break;
423 case Element::kRect_Type:
424 dc->drawRect(clip, paint, viewMatrix, element->getRect());
425 break;
426 default: {
427 SkPath path;
428 element->asPath(&path);
429 if (path.isInverseFillType()) {
430 path.toggleInverseFillType();
431 }
432
bsalomon6663acf2016-05-10 09:14:17 -0700433 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800434 break;
435 }
436 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437}
438
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000439////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700440// Create a 8-bit clip mask in alpha
441
442static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
443 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
444 GrUniqueKey::Builder builder(key, kDomain, 3);
445 builder[0] = clipGenID;
446 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
447 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
448}
449
robertphillipsc99b8f02016-05-15 07:53:35 -0700450sk_sp<GrTexture> GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
451 int32_t elementsGenID,
452 GrReducedClip::InitialState initialState,
453 const GrReducedClip::ElementList& elements,
454 const SkVector& clipToMaskOffset,
455 const SkIRect& clipSpaceIBounds) {
robertphillips391395d2016-03-02 09:26:36 -0800456 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700457 GrUniqueKey key;
458 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
459 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700460 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000461 }
462
robertphillips544b9aa2015-10-28 11:01:41 -0700463 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700464 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800465 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700466 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800467 }
468
robertphillipsc99b8f02016-05-15 07:53:35 -0700469 sk_sp<GrDrawContext> dc(context->newDrawContext(SkBackingFit::kApprox,
470 clipSpaceIBounds.width(),
471 clipSpaceIBounds.height(),
472 config));
robertphillips391395d2016-03-02 09:26:36 -0800473 if (!dc) {
474 return nullptr;
475 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700476
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000477 // The texture may be larger than necessary, this rect represents the part of the texture
478 // we populate with a rasterization of the clip.
479 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
480
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000481 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
482 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800483 dc->clear(&maskSpaceIBounds,
484 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
485 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000486
robertphillips391395d2016-03-02 09:26:36 -0800487 // Set the matrix so that rendered clip elements are transformed to mask space from clip
488 // space.
489 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
490
491 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000492 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
493 // pass must not set values outside of this bounds or stencil values outside the rect won't be
494 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800495
robertphillips@google.comf294b772012-04-27 14:29:26 +0000496 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700497 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000498 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000499 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000500 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000501 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700502 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700503
robertphillips391395d2016-03-02 09:26:36 -0800504 // draw directly into the result with the stencil set to make the pixels affected
505 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700506 static constexpr GrUserStencilSettings kStencilInElement(
507 GrUserStencilSettings::StaticInit<
508 0xffff,
509 GrUserStencilTest::kAlways,
510 0xffff,
511 GrUserStencilOp::kReplace,
512 GrUserStencilOp::kReplace,
513 0xffff>()
514 );
cdalton862cff32016-05-12 15:09:48 -0700515 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800516 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800517 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000518 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000519
robertphillips391395d2016-03-02 09:26:36 -0800520 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700521 static constexpr GrUserStencilSettings kDrawOutsideElement(
522 GrUserStencilSettings::StaticInit<
523 0x0000,
524 GrUserStencilTest::kEqual,
525 0xffff,
526 GrUserStencilOp::kZero,
527 GrUserStencilOp::kZero,
528 0xffff>()
529 );
cdalton862cff32016-05-12 15:09:48 -0700530 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800531 op, !invert, false,
532 translate,
533 SkRect::Make(clipSpaceIBounds))) {
robertphillips391395d2016-03-02 09:26:36 -0800534 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000535 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000536 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800537 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800538 GrPaint paint;
539 paint.setAntiAlias(element->isAA());
540 paint.setCoverageSetOpXPFactory(op, false);
541
cdalton846c0512016-05-13 10:25:00 -0700542 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000543 }
544 }
545
robertphillipsc99b8f02016-05-15 07:53:35 -0700546 sk_sp<GrTexture> texture(dc->asTexture());
547 SkASSERT(texture);
548 texture->resourcePriv().setUniqueKey(key);
549 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000550}
551
552////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000553// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000554// (as opposed to canvas) coordinates
robertphillips976f5f02016-06-03 10:59:20 -0700555bool GrClipMaskManager::CreateStencilClipMask(GrContext* context,
556 GrDrawContext* drawContext,
joshualitt9853cce2014-11-17 14:22:48 -0800557 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700558 GrReducedClip::InitialState initialState,
559 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 const SkIRect& clipSpaceIBounds,
561 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700562 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000563
robertphillips976f5f02016-06-03 10:59:20 -0700564 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
565 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700566 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000567 return false;
568 }
569
robertphillips976f5f02016-06-03 10:59:20 -0700570 // TODO: these need to be swapped over to using a StencilAttachmentProxy
egdaniel8dc7c3a2015-04-16 11:22:42 -0700571 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
572 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000573 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
574 SkVector translate = {
575 SkIntToScalar(clipSpaceToStencilOffset.fX),
576 SkIntToScalar(clipSpaceToStencilOffset.fY)
577 };
joshualitt8059eb92014-12-29 15:10:07 -0800578 SkMatrix viewMatrix;
579 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000580
bsalomon@google.com9f131742012-12-13 20:43:56 +0000581 // We set the current clip to the bounds so that our recursive draws are scissored to them.
582 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
583 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700584 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000585
robertphillips976f5f02016-06-03 10:59:20 -0700586 drawContext->drawContextPriv().clearStencilClip(
587 stencilSpaceIBounds,
588 GrReducedClip::kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000589
590 // walk through each clip element and perform its set op
591 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700592 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000593 const Element* element = iter.get();
csmartdalton656dbe42016-06-10 12:32:57 -0700594 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
joshualitt9853cce2014-11-17 14:22:48 -0800595
tomhudson@google.com8afae612012-08-14 15:03:35 +0000596 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000597 // enabled at bottom of loop
cdalton846c0512016-05-13 10:25:00 -0700598 clip.enableStencilClip(false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000599
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000600 // This will be used to determine whether the clip shape can be rendered into the
601 // stencil with arbitrary stencil settings.
602 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000603
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000604 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000605
halcanary96fcdcc2015-08-27 07:41:13 -0700606 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000607 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000608 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000609 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000610 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000611 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000612 element->asPath(&clipPath);
613 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000614 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000615 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000616 }
robertphillips68737822015-10-29 12:12:21 -0700617
bsalomon8acedde2016-06-24 10:42:16 -0700618 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700619 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700620 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700621 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700622 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700623 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700624 canDrawArgs.fHasUserStencilSettings = false;
625 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700626
robertphillips976f5f02016-06-03 10:59:20 -0700627 pr = context->drawingManager()->getPathRenderer(canDrawArgs, false,
628 GrPathRendererChain::kStencilOnly_DrawType,
629 &stencilSupport);
630 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000631 return false;
632 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000633 }
634
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000635 bool canRenderDirectToStencil =
636 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700637 bool drawDirectToClip; // Given the renderer, the element,
638 // fill rule, and set operation should
639 // we render the element directly to
640 // stencil bit used for clipping.
641 GrUserStencilSettings const* const* stencilPasses =
642 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
643 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000644
645 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700646 if (!drawDirectToClip) {
647 static constexpr GrUserStencilSettings kDrawToStencil(
648 GrUserStencilSettings::StaticInit<
649 0x0000,
650 GrUserStencilTest::kAlways,
651 0xffff,
652 GrUserStencilOp::kIncMaybeClamp,
653 GrUserStencilOp::kIncMaybeClamp,
654 0xffff>()
655 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000656 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700657 drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA,
658 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000660 if (!clipPath.isEmpty()) {
bsalomon8acedde2016-06-24 10:42:16 -0700661 GrShape shape(clipPath, GrStyle::SimpleFill());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000662 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700663 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700664 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700665 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700666
667 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700668 args.fResourceProvider = context->resourceProvider();
669 args.fPaint = &paint;
670 args.fUserStencilSettings = &kDrawToStencil;
671 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700672 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700673 args.fColor = GrColor_WHITE;
674 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700675 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700676 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700677 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700678 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000679 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700680 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700681 args.fResourceProvider = context->resourceProvider();
682 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700683 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700684 args.fViewMatrix = &viewMatrix;
robertphillips976f5f02016-06-03 10:59:20 -0700685 args.fIsAA = element->isAA();
bsalomon8acedde2016-06-24 10:42:16 -0700686 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700687 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000688 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000689 }
690 }
691 }
692
693 // now we modify the clip bit by rendering either the clip
694 // element directly or a bounding rect of the entire clip.
cdalton846c0512016-05-13 10:25:00 -0700695 clip.enableStencilClip(true);
cdalton93a379b2016-05-11 13:58:08 -0700696 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
joshualitt9853cce2014-11-17 14:22:48 -0800697
cdalton93a379b2016-05-11 13:58:08 -0700698 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000699 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700700 drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix,
701 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000702 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700703 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips976f5f02016-06-03 10:59:20 -0700704 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700705 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700706 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700707 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700708 args.fResourceProvider = context->resourceProvider();
709 args.fPaint = &paint;
710 args.fUserStencilSettings = *pass;
711 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700712 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700713 args.fColor = GrColor_WHITE;
714 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700715 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700716 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700717 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700718 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000719 }
720 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000721 // The view matrix is setup to do clip space -> stencil space translation, so
722 // draw rect in clip space.
csmartdalton656dbe42016-06-10 12:32:57 -0700723 drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix,
724 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 }
726 }
727 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000729 return true;
730}
731
bsalomon@google.com411dad02012-06-05 20:24:20 +0000732////////////////////////////////////////////////////////////////////////////////
robertphillipsc99b8f02016-05-15 07:53:35 -0700733sk_sp<GrTexture> GrClipMaskManager::CreateSoftwareClipMask(
robertphillips0152d732016-05-20 06:38:43 -0700734 GrTextureProvider* texProvider,
robertphillipsc99b8f02016-05-15 07:53:35 -0700735 int32_t elementsGenID,
736 GrReducedClip::InitialState initialState,
737 const GrReducedClip::ElementList& elements,
738 const SkVector& clipToMaskOffset,
739 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700740 GrUniqueKey key;
741 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips0152d732016-05-20 06:38:43 -0700742 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700743 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000744 }
745
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000746 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
747 // the top left corner of the resulting rect to the top left of the texture.
748 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
749
robertphillips0152d732016-05-20 06:38:43 -0700750 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000751
joshualitt8059eb92014-12-29 15:10:07 -0800752 // Set the matrix so that rendered clip elements are transformed to mask space from clip
753 // space.
754 SkMatrix translate;
755 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800756
robertphillips98377402016-05-13 05:47:23 -0700757 helper.init(maskSpaceIBounds, &translate);
tfarinabf54e492014-10-23 17:47:18 -0700758 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000759
tfarinabf54e492014-10-23 17:47:18 -0700760 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000761 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000762 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000763
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000764 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
765 // Intersect and reverse difference require modifying pixels outside of the geometry
766 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
767 // but leave the pixels inside the geometry alone. For reverse difference we invert all
768 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000769 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000770 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000771 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700772 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000773 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000774 SkPath clipPath;
775 element->asPath(&clipPath);
776 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700777 GrShape shape(clipPath, GrStyle::SimpleFill());
778 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000779 continue;
780 }
781
782 // The other ops (union, xor, diff) only affect pixels inside
783 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000784 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700785 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000786 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000787 SkPath path;
788 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700789 GrShape shape(path, GrStyle::SimpleFill());
790 helper.drawShape(shape, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000791 }
792 }
793
krajcevskiad1dc582014-06-10 15:06:47 -0700794 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800795 GrSurfaceDesc desc;
796 desc.fWidth = clipSpaceIBounds.width();
797 desc.fHeight = clipSpaceIBounds.height();
798 desc.fConfig = kAlpha_8_GrPixelConfig;
799
robertphillips0152d732016-05-20 06:38:43 -0700800 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800801 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700802 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700803 }
robertphillips391395d2016-03-02 09:26:36 -0800804 result->resourcePriv().setUniqueKey(key);
805
robertphillipsc99b8f02016-05-15 07:53:35 -0700806 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000807
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000808 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000809}