blob: cce0b20eeeb7b12ae2e93cc95e0343929a04668b [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,
bsalomonbd2bbe42016-07-08 07:36:42 -0700149 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;
bsalomonbd2bbe42016-07-08 07:36:42 -0700152 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700153 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700154 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
tfarinabf54e492014-10-23 17:47:18 -0700155 GrReducedClip::ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700156 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000157 SkRegion::Op op = iter.get()->getOp();
158 bool invert;
159 bool skip = false;
160 switch (op) {
161 case SkRegion::kReplace_Op:
162 SkASSERT(iter.get() == elements.head());
163 // Fallthrough, handled same as intersect.
164 case SkRegion::kIntersect_Op:
165 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700166 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000167 skip = true;
168 }
169 break;
170 case SkRegion::kDifference_Op:
171 invert = true;
172 // We don't currently have a cheap test for whether a rect is fully outside an
173 // element's primitive, so don't attempt to set skip.
174 break;
175 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700176 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000177 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000178 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700179 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800180 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700181 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700182 return false;
bsalomona912dde2015-10-14 15:01:50 -0700183 }
joshualittb0a8a372014-09-23 09:50:21 -0700184 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700185 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000186 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700187 edgeType =
188 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000189 }
bsalomona912dde2015-10-14 15:01:50 -0700190
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000191 switch (iter.get()->getType()) {
192 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700193 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
194 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000195 break;
196 case SkClipStack::Element::kRRect_Type: {
197 SkRRect rrect = iter.get()->getRRect();
198 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700199 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000200 break;
201 }
202 case SkClipStack::Element::kRect_Type: {
203 SkRect rect = iter.get()->getRect();
204 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700205 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000206 break;
207 }
208 default:
209 break;
210 }
bungeman06ca8ec2016-06-09 08:01:03 -0700211 if (!fps.back()) {
212 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000213 }
214 }
mtklein217daa72014-07-02 12:55:21 -0700215 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000216 }
217
bsalomon0b5b6b22015-10-14 08:31:34 -0700218 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700219 if (fps.count()) {
220 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000221 }
bungeman06ca8ec2016-06-09 08:01:03 -0700222 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000223}
224
robertphillips@google.comf294b772012-04-27 14:29:26 +0000225////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000226// sort out what kind of clip mask needs to be created: alpha, stencil,
227// scissor, or entirely software
robertphillips976f5f02016-06-03 10:59:20 -0700228bool GrClipMaskManager::SetupClipping(GrContext* context,
229 const GrPipelineBuilder& pipelineBuilder,
230 GrDrawContext* drawContext,
cdalton846c0512016-05-13 10:25:00 -0700231 const GrClipStackClip& clip,
bsalomonbd2bbe42016-07-08 07:36:42 -0700232 const SkRect* origDevBounds,
bsalomon0ba8c242015-10-07 09:20:28 -0700233 GrAppliedClip* out) {
cdalton846c0512016-05-13 10:25:00 -0700234 if (!clip.clipStack() || clip.clipStack()->isWideOpen()) {
235 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700236 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000237
bsalomonf045d602015-11-18 19:01:12 -0800238 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800239 int32_t genID = 0;
240 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000241 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800242 bool requiresAA = false;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000243
cdalton846c0512016-05-13 10:25:00 -0700244 SkIRect clipSpaceReduceQueryBounds;
bsalomonbd2bbe42016-07-08 07:36:42 -0700245 SkRect devBounds;
246 if (origDevBounds) {
247 if (!devBounds.intersect(SkRect::MakeIWH(drawContext->width(), drawContext->height()),
248 *origDevBounds)) {
bsalomon9ce30e12015-03-06 08:42:34 -0800249 return false;
250 }
bsalomonbd2bbe42016-07-08 07:36:42 -0700251 devBounds.roundOut(&clipSpaceReduceQueryBounds);
252 clipSpaceReduceQueryBounds.offset(clip.origin());
cdalton846c0512016-05-13 10:25:00 -0700253 } else {
bsalomonbd2bbe42016-07-08 07:36:42 -0700254 devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
255 clipSpaceReduceQueryBounds.setXYWH(0, 0, drawContext->width(), drawContext->height());
256 clipSpaceReduceQueryBounds.offset(clip.origin());
bsalomon96e02a82015-03-06 07:13:01 -0800257 }
cdalton846c0512016-05-13 10:25:00 -0700258 GrReducedClip::ReduceClipStack(*clip.clipStack(),
259 clipSpaceReduceQueryBounds,
260 &elements,
261 &genID,
262 &initialState,
263 &clipSpaceIBounds,
264 &requiresAA);
265 if (elements.isEmpty()) {
bsalomonbd2bbe42016-07-08 07:36:42 -0700266 if (GrReducedClip::kAllOut_InitialState == initialState) {
cdalton846c0512016-05-13 10:25:00 -0700267 return false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700268 } else {
269 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
270 scissorSpaceIBounds.offset(-clip.origin());
271 if (!SkRect::Make(scissorSpaceIBounds).contains(devBounds)) {
272 out->makeScissored(scissorSpaceIBounds);
273 }
274 return true;
cdalton846c0512016-05-13 10:25:00 -0700275 }
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;
bsalomonbd2bbe42016-07-08 07:36:42 -0700300 if (requiresAA &&
301 get_analytic_clip_processor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
302 &clipFP)) {
mtklein217daa72014-07-02 12:55:21 -0700303 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800304 scissorSpaceIBounds.offset(-clip.origin());
bsalomonbd2bbe42016-07-08 07:36:42 -0700305 if (!SkRect::Make(scissorSpaceIBounds).contains(devBounds)) {
bungeman06ca8ec2016-06-09 08:01:03 -0700306 out->makeScissoredFPBased(std::move(clipFP), scissorSpaceIBounds);
robertphillips5f2fa472016-05-19 11:36:25 -0700307 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000308 }
bungeman06ca8ec2016-06-09 08:01:03 -0700309 out->makeFPBased(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000310 return true;
311 }
312 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000313
cdaltonede75742015-11-11 15:27:57 -0800314 // If the stencil buffer is multisampled we can use it to do everything.
robertphillips976f5f02016-06-03 10:59:20 -0700315 if (!drawContext->isStencilBufferMultisampled() && requiresAA) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700316 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000317
joshualitt8059eb92014-12-29 15:10:07 -0800318 // The top-left of the mask corresponds to the top-left corner of the bounds.
319 SkVector clipToMaskOffset = {
320 SkIntToScalar(-clipSpaceIBounds.fLeft),
321 SkIntToScalar(-clipSpaceIBounds.fTop)
322 };
323
robertphillips976f5f02016-06-03 10:59:20 -0700324 if (UseSWOnlyPath(context, pipelineBuilder, drawContext,
325 clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000326 // The clip geometry is complex enough that it will be more efficient to create it
327 // entirely in software
robertphillips976f5f02016-06-03 10:59:20 -0700328 result = CreateSoftwareClipMask(context->textureProvider(),
robertphillipsc99b8f02016-05-15 07:53:35 -0700329 genID,
330 initialState,
331 elements,
332 clipToMaskOffset,
333 clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000334 } else {
robertphillips976f5f02016-06-03 10:59:20 -0700335 result = CreateAlphaClipMask(context,
robertphillipsc99b8f02016-05-15 07:53:35 -0700336 genID,
337 initialState,
338 elements,
339 clipToMaskOffset,
340 clipSpaceIBounds);
robertphillips391395d2016-03-02 09:26:36 -0800341 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700342 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000343 }
344
bsalomon49f085d2014-09-05 13:34:00 -0700345 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000346 // The mask's top left coord should be pinned to the rounded-out top left corner of
347 // clipSpace bounds. We determine the mask's position WRT to the render target here.
348 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800349 rtSpaceMaskBounds.offset(-clip.origin());
robertphillips5f2fa472016-05-19 11:36:25 -0700350 out->makeFPBased(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000351 return true;
352 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000353 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000354 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000356 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800357 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
robertphillips976f5f02016-06-03 10:59:20 -0700358 CreateStencilClipMask(context,
359 drawContext,
360 genID,
361 initialState,
362 elements,
363 clipSpaceIBounds,
364 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000365
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000366 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
367 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
368 // use both stencil and scissor test to the bounds for the final draw.
369 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
370 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
robertphillips5f2fa472016-05-19 11:36:25 -0700371 out->makeScissoredStencil(true, scissorSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000372 return true;
373}
374
robertphillips391395d2016-03-02 09:26:36 -0800375static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700376 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700377 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800378 const SkMatrix& viewMatrix,
379 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800380
381 // TODO: Draw rrects directly here.
382 switch (element->getType()) {
383 case Element::kEmpty_Type:
384 SkDEBUGFAIL("Should never get here with an empty element.");
385 break;
robertphillips391395d2016-03-02 09:26:36 -0800386 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700387 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800388 element->getOp(),
389 element->isInverseFilled(),
390 element->isAA(),
391 viewMatrix, element->getRect());
392 break;
robertphillips86c60752016-03-02 08:43:13 -0800393 default: {
394 SkPath path;
395 element->asPath(&path);
396 if (path.isInverseFillType()) {
397 path.toggleInverseFillType();
398 }
399
cdalton862cff32016-05-12 15:09:48 -0700400 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800401 element->getOp(),
402 element->isInverseFilled(),
403 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800404 break;
405 }
406 }
robertphillips391395d2016-03-02 09:26:36 -0800407
408 return false;
409}
410
411static void draw_element(GrDrawContext* dc,
412 const GrClip& clip, // TODO: can this just always be WideOpen?
413 const GrPaint &paint,
414 const SkMatrix& viewMatrix,
415 const SkClipStack::Element* element) {
416
417 // TODO: Draw rrects directly here.
418 switch (element->getType()) {
419 case Element::kEmpty_Type:
420 SkDEBUGFAIL("Should never get here with an empty element.");
421 break;
422 case Element::kRect_Type:
423 dc->drawRect(clip, paint, viewMatrix, element->getRect());
424 break;
425 default: {
426 SkPath path;
427 element->asPath(&path);
428 if (path.isInverseFillType()) {
429 path.toggleInverseFillType();
430 }
431
bsalomon6663acf2016-05-10 09:14:17 -0700432 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800433 break;
434 }
435 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000436}
437
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000438////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700439// Create a 8-bit clip mask in alpha
440
441static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
442 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
443 GrUniqueKey::Builder builder(key, kDomain, 3);
444 builder[0] = clipGenID;
445 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
446 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
447}
448
robertphillipsc99b8f02016-05-15 07:53:35 -0700449sk_sp<GrTexture> GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
450 int32_t elementsGenID,
451 GrReducedClip::InitialState initialState,
452 const GrReducedClip::ElementList& elements,
453 const SkVector& clipToMaskOffset,
454 const SkIRect& clipSpaceIBounds) {
robertphillips391395d2016-03-02 09:26:36 -0800455 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700456 GrUniqueKey key;
457 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
458 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700459 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000460 }
461
robertphillips544b9aa2015-10-28 11:01:41 -0700462 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700463 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800464 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700465 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800466 }
467
robertphillipsc99b8f02016-05-15 07:53:35 -0700468 sk_sp<GrDrawContext> dc(context->newDrawContext(SkBackingFit::kApprox,
469 clipSpaceIBounds.width(),
470 clipSpaceIBounds.height(),
471 config));
robertphillips391395d2016-03-02 09:26:36 -0800472 if (!dc) {
473 return nullptr;
474 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700475
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000476 // The texture may be larger than necessary, this rect represents the part of the texture
477 // we populate with a rasterization of the clip.
478 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
479
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000480 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
481 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800482 dc->clear(&maskSpaceIBounds,
483 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
484 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000485
robertphillips391395d2016-03-02 09:26:36 -0800486 // Set the matrix so that rendered clip elements are transformed to mask space from clip
487 // space.
488 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
489
490 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000491 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
492 // pass must not set values outside of this bounds or stencil values outside the rect won't be
493 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800494
robertphillips@google.comf294b772012-04-27 14:29:26 +0000495 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700496 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000497 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000498 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000499 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000500 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700501 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700502
robertphillips391395d2016-03-02 09:26:36 -0800503 // draw directly into the result with the stencil set to make the pixels affected
504 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700505 static constexpr GrUserStencilSettings kStencilInElement(
506 GrUserStencilSettings::StaticInit<
507 0xffff,
508 GrUserStencilTest::kAlways,
509 0xffff,
510 GrUserStencilOp::kReplace,
511 GrUserStencilOp::kReplace,
512 0xffff>()
513 );
cdalton862cff32016-05-12 15:09:48 -0700514 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800515 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800516 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000517 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000518
robertphillips391395d2016-03-02 09:26:36 -0800519 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700520 static constexpr GrUserStencilSettings kDrawOutsideElement(
521 GrUserStencilSettings::StaticInit<
522 0x0000,
523 GrUserStencilTest::kEqual,
524 0xffff,
525 GrUserStencilOp::kZero,
526 GrUserStencilOp::kZero,
527 0xffff>()
528 );
cdalton862cff32016-05-12 15:09:48 -0700529 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800530 op, !invert, false,
531 translate,
532 SkRect::Make(clipSpaceIBounds))) {
robertphillips391395d2016-03-02 09:26:36 -0800533 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000534 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000535 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800536 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800537 GrPaint paint;
538 paint.setAntiAlias(element->isAA());
539 paint.setCoverageSetOpXPFactory(op, false);
540
cdalton846c0512016-05-13 10:25:00 -0700541 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000542 }
543 }
544
robertphillipsc99b8f02016-05-15 07:53:35 -0700545 sk_sp<GrTexture> texture(dc->asTexture());
546 SkASSERT(texture);
547 texture->resourcePriv().setUniqueKey(key);
548 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000549}
550
551////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000552// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000553// (as opposed to canvas) coordinates
robertphillips976f5f02016-06-03 10:59:20 -0700554bool GrClipMaskManager::CreateStencilClipMask(GrContext* context,
555 GrDrawContext* drawContext,
joshualitt9853cce2014-11-17 14:22:48 -0800556 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700557 GrReducedClip::InitialState initialState,
558 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000559 const SkIRect& clipSpaceIBounds,
560 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700561 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000562
robertphillips976f5f02016-06-03 10:59:20 -0700563 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
564 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700565 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000566 return false;
567 }
568
robertphillips976f5f02016-06-03 10:59:20 -0700569 // TODO: these need to be swapped over to using a StencilAttachmentProxy
egdaniel8dc7c3a2015-04-16 11:22:42 -0700570 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
571 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000572 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
573 SkVector translate = {
574 SkIntToScalar(clipSpaceToStencilOffset.fX),
575 SkIntToScalar(clipSpaceToStencilOffset.fY)
576 };
joshualitt8059eb92014-12-29 15:10:07 -0800577 SkMatrix viewMatrix;
578 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000579
bsalomon@google.com9f131742012-12-13 20:43:56 +0000580 // We set the current clip to the bounds so that our recursive draws are scissored to them.
581 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
582 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700583 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000584
robertphillips976f5f02016-06-03 10:59:20 -0700585 drawContext->drawContextPriv().clearStencilClip(
586 stencilSpaceIBounds,
587 GrReducedClip::kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000588
589 // walk through each clip element and perform its set op
590 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700591 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000592 const Element* element = iter.get();
csmartdalton656dbe42016-06-10 12:32:57 -0700593 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
joshualitt9853cce2014-11-17 14:22:48 -0800594
tomhudson@google.com8afae612012-08-14 15:03:35 +0000595 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000596 // enabled at bottom of loop
cdalton846c0512016-05-13 10:25:00 -0700597 clip.enableStencilClip(false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000598
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000599 // This will be used to determine whether the clip shape can be rendered into the
600 // stencil with arbitrary stencil settings.
601 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000602
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000603 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000604
halcanary96fcdcc2015-08-27 07:41:13 -0700605 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000606 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000607 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000608 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000609 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000610 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000611 element->asPath(&clipPath);
612 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000613 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000614 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000615 }
robertphillips68737822015-10-29 12:12:21 -0700616
bsalomon8acedde2016-06-24 10:42:16 -0700617 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700618 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700619 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700620 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700621 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700622 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700623 canDrawArgs.fHasUserStencilSettings = false;
624 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700625
robertphillips976f5f02016-06-03 10:59:20 -0700626 pr = context->drawingManager()->getPathRenderer(canDrawArgs, false,
627 GrPathRendererChain::kStencilOnly_DrawType,
628 &stencilSupport);
629 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000630 return false;
631 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000632 }
633
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000634 bool canRenderDirectToStencil =
635 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700636 bool drawDirectToClip; // Given the renderer, the element,
637 // fill rule, and set operation should
638 // we render the element directly to
639 // stencil bit used for clipping.
640 GrUserStencilSettings const* const* stencilPasses =
641 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
642 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000643
644 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700645 if (!drawDirectToClip) {
646 static constexpr GrUserStencilSettings kDrawToStencil(
647 GrUserStencilSettings::StaticInit<
648 0x0000,
649 GrUserStencilTest::kAlways,
650 0xffff,
651 GrUserStencilOp::kIncMaybeClamp,
652 GrUserStencilOp::kIncMaybeClamp,
653 0xffff>()
654 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000655 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700656 drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA,
657 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000658 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000659 if (!clipPath.isEmpty()) {
bsalomon8acedde2016-06-24 10:42:16 -0700660 GrShape shape(clipPath, GrStyle::SimpleFill());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000661 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700662 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700663 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700664 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700665
666 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700667 args.fResourceProvider = context->resourceProvider();
668 args.fPaint = &paint;
669 args.fUserStencilSettings = &kDrawToStencil;
670 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700671 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700672 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700673 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700674 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700675 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700676 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000677 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700678 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700679 args.fResourceProvider = context->resourceProvider();
680 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700681 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700682 args.fViewMatrix = &viewMatrix;
robertphillips976f5f02016-06-03 10:59:20 -0700683 args.fIsAA = element->isAA();
bsalomon8acedde2016-06-24 10:42:16 -0700684 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700685 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000686 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000687 }
688 }
689 }
690
691 // now we modify the clip bit by rendering either the clip
692 // element directly or a bounding rect of the entire clip.
cdalton846c0512016-05-13 10:25:00 -0700693 clip.enableStencilClip(true);
cdalton93a379b2016-05-11 13:58:08 -0700694 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
joshualitt9853cce2014-11-17 14:22:48 -0800695
cdalton93a379b2016-05-11 13:58:08 -0700696 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000697 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700698 drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix,
699 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000700 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700701 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips976f5f02016-06-03 10:59:20 -0700702 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700703 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700704 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700705 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700706 args.fResourceProvider = context->resourceProvider();
707 args.fPaint = &paint;
708 args.fUserStencilSettings = *pass;
709 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700710 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700711 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700712 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700713 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700714 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700715 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000716 }
717 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000718 // The view matrix is setup to do clip space -> stencil space translation, so
719 // draw rect in clip space.
csmartdalton656dbe42016-06-10 12:32:57 -0700720 drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix,
721 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000722 }
723 }
724 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000725 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726 return true;
727}
728
bsalomon@google.com411dad02012-06-05 20:24:20 +0000729////////////////////////////////////////////////////////////////////////////////
robertphillipsc99b8f02016-05-15 07:53:35 -0700730sk_sp<GrTexture> GrClipMaskManager::CreateSoftwareClipMask(
robertphillips0152d732016-05-20 06:38:43 -0700731 GrTextureProvider* texProvider,
robertphillipsc99b8f02016-05-15 07:53:35 -0700732 int32_t elementsGenID,
733 GrReducedClip::InitialState initialState,
734 const GrReducedClip::ElementList& elements,
735 const SkVector& clipToMaskOffset,
736 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700737 GrUniqueKey key;
738 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips0152d732016-05-20 06:38:43 -0700739 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700740 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000741 }
742
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000743 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
744 // the top left corner of the resulting rect to the top left of the texture.
745 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
746
robertphillips0152d732016-05-20 06:38:43 -0700747 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000748
joshualitt8059eb92014-12-29 15:10:07 -0800749 // Set the matrix so that rendered clip elements are transformed to mask space from clip
750 // space.
751 SkMatrix translate;
752 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800753
robertphillips98377402016-05-13 05:47:23 -0700754 helper.init(maskSpaceIBounds, &translate);
tfarinabf54e492014-10-23 17:47:18 -0700755 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000756
tfarinabf54e492014-10-23 17:47:18 -0700757 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000758 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000759 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000760
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000761 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
762 // Intersect and reverse difference require modifying pixels outside of the geometry
763 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
764 // but leave the pixels inside the geometry alone. For reverse difference we invert all
765 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000766 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000767 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000768 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700769 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000770 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000771 SkPath clipPath;
772 element->asPath(&clipPath);
773 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700774 GrShape shape(clipPath, GrStyle::SimpleFill());
775 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000776 continue;
777 }
778
779 // The other ops (union, xor, diff) only affect pixels inside
780 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000781 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700782 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000783 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000784 SkPath path;
785 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700786 GrShape shape(path, GrStyle::SimpleFill());
787 helper.drawShape(shape, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000788 }
789 }
790
krajcevskiad1dc582014-06-10 15:06:47 -0700791 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800792 GrSurfaceDesc desc;
793 desc.fWidth = clipSpaceIBounds.width();
794 desc.fHeight = clipSpaceIBounds.height();
795 desc.fConfig = kAlpha_8_GrPixelConfig;
796
robertphillips0152d732016-05-20 06:38:43 -0700797 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800798 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700799 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700800 }
robertphillips391395d2016-03-02 09:26:36 -0800801 result->resourcePriv().setUniqueKey(key);
802
robertphillipsc99b8f02016-05-15 07:53:35 -0700803 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000804
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000805 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000806}