blob: 93481811451da2d2849a8013b830cc3893a3fa6d [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
robertphillips976f5f02016-06-03 10:59:20 -070054void GrClipMaskManager::DrawNonAARect(GrDrawContext* drawContext,
55 const GrFixedClip& clip,
56 const SkMatrix& viewMatrix,
57 const SkRect& rect,
58 bool doAA,
59 const GrUserStencilSettings* stencilSettings) {
60 drawContext->drawContextPriv().stencilRect(clip, stencilSettings, doAA, viewMatrix, rect);
joshualitta8b84992016-01-13 13:35:35 -080061}
62
robertphillips3f7357f2015-10-27 07:17:33 -070063// Does the path in 'element' require SW rendering? If so, return true (and,
64// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
65// 'prOut' to the non-SW path renderer that will do the job).
robertphillips68737822015-10-29 12:12:21 -070066bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
cdalton93a379b2016-05-11 13:58:08 -070067 bool hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070068 const GrDrawContext* drawContext,
robertphillips68737822015-10-29 12:12:21 -070069 const SkMatrix& viewMatrix,
70 const Element* element,
71 GrPathRenderer** prOut,
72 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070073 if (Element::kRect_Type == element->getType()) {
74 // rects can always be drawn directly w/o using the software path
75 // TODO: skip rrects once we're drawing them directly.
76 if (prOut) {
77 *prOut = nullptr;
78 }
79 return false;
80 } else {
81 // We shouldn't get here with an empty clip element.
82 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070083
robertphillips3f7357f2015-10-27 07:17:33 -070084 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
85 SkPath path;
86 element->asPath(&path);
87 if (path.isInverseFillType()) {
88 path.toggleInverseFillType();
89 }
halcanary9d524f22016-03-29 09:03:52 -070090
robertphillips3f7357f2015-10-27 07:17:33 -070091 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -070092
robertphillips423e3372015-10-27 09:23:38 -070093 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070094 type = element->isAA()
95 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
96 : GrPathRendererChain::kStencilAndColor_DrawType;
97 } else {
98 type = element->isAA()
99 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -0700100 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -0700101 }
halcanary9d524f22016-03-29 09:03:52 -0700102
robertphillips68737822015-10-29 12:12:21 -0700103 GrPathRenderer::CanDrawPathArgs canDrawArgs;
104 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
105 canDrawArgs.fViewMatrix = &viewMatrix;
106 canDrawArgs.fPath = &path;
bsalomon6663acf2016-05-10 09:14:17 -0700107 canDrawArgs.fStyle = &GrStyle::SimpleFill();
robertphillips68737822015-10-29 12:12:21 -0700108 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700109 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700110 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700111
robertphillips3f7357f2015-10-27 07:17:33 -0700112 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700113 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700114 if (prOut) {
115 *prOut = pr;
116 }
117 return SkToBool(!pr);
118 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000119}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000120
robertphillips@google.comfa662942012-05-17 12:20:22 +0000121/*
122 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
123 * will be used on any element. If so, it returns true to indicate that the
124 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
125 */
robertphillips391395d2016-03-02 09:26:36 -0800126bool GrClipMaskManager::UseSWOnlyPath(GrContext* context,
127 const GrPipelineBuilder& pipelineBuilder,
robertphillips976f5f02016-06-03 10:59:20 -0700128 const GrDrawContext* drawContext,
joshualitt8059eb92014-12-29 15:10:07 -0800129 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800130 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000131 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000132 // a clip gets complex enough it can just be done in SW regardless
133 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000134
joshualitt8059eb92014-12-29 15:10:07 -0800135 // Set the matrix so that rendered clip elements are transformed to mask space from clip
136 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700137 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800138
tfarinabf54e492014-10-23 17:47:18 -0700139 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000140 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700141
142 SkRegion::Op op = element->getOp();
143 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700144 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700145 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700146
cdalton93a379b2016-05-11 13:58:08 -0700147 if (PathNeedsSWRenderer(context, pipelineBuilder.hasUserStencilSettings(),
robertphillips976f5f02016-06-03 10:59:20 -0700148 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700149 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000150 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000151 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000152 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000153}
154
robertphillips976f5f02016-06-03 10:59:20 -0700155static bool get_analytic_clip_processor(const GrReducedClip::ElementList& elements,
156 bool abortIfAA,
157 SkVector& clipToRTOffset,
158 const SkRect* drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700159 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000160 SkRect boundsInClipSpace;
bsalomon49f085d2014-09-05 13:34:00 -0700161 if (drawBounds) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000162 boundsInClipSpace = *drawBounds;
163 boundsInClipSpace.offset(-clipToRTOffset.fX, -clipToRTOffset.fY);
164 }
bsalomon0ba8c242015-10-07 09:20:28 -0700165 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700166 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
tfarinabf54e492014-10-23 17:47:18 -0700167 GrReducedClip::ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700168 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000169 SkRegion::Op op = iter.get()->getOp();
170 bool invert;
171 bool skip = false;
172 switch (op) {
173 case SkRegion::kReplace_Op:
174 SkASSERT(iter.get() == elements.head());
175 // Fallthrough, handled same as intersect.
176 case SkRegion::kIntersect_Op:
177 invert = false;
bsalomon49f085d2014-09-05 13:34:00 -0700178 if (drawBounds && iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000179 skip = true;
180 }
181 break;
182 case SkRegion::kDifference_Op:
183 invert = true;
184 // We don't currently have a cheap test for whether a rect is fully outside an
185 // element's primitive, so don't attempt to set skip.
186 break;
187 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700188 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000189 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000190 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700191 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800192 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700193 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700194 return false;
bsalomona912dde2015-10-14 15:01:50 -0700195 }
joshualittb0a8a372014-09-23 09:50:21 -0700196 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700197 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000198 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700199 edgeType =
200 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000201 }
bsalomona912dde2015-10-14 15:01:50 -0700202
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000203 switch (iter.get()->getType()) {
204 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700205 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
206 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000207 break;
208 case SkClipStack::Element::kRRect_Type: {
209 SkRRect rrect = iter.get()->getRRect();
210 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700211 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000212 break;
213 }
214 case SkClipStack::Element::kRect_Type: {
215 SkRect rect = iter.get()->getRect();
216 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700217 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000218 break;
219 }
220 default:
221 break;
222 }
bungeman06ca8ec2016-06-09 08:01:03 -0700223 if (!fps.back()) {
224 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000225 }
226 }
mtklein217daa72014-07-02 12:55:21 -0700227 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000228 }
229
bsalomon0b5b6b22015-10-14 08:31:34 -0700230 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700231 if (fps.count()) {
232 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000233 }
bungeman06ca8ec2016-06-09 08:01:03 -0700234 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000235}
236
robertphillips@google.comf294b772012-04-27 14:29:26 +0000237////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000238// sort out what kind of clip mask needs to be created: alpha, stencil,
239// scissor, or entirely software
robertphillips976f5f02016-06-03 10:59:20 -0700240bool GrClipMaskManager::SetupClipping(GrContext* context,
241 const GrPipelineBuilder& pipelineBuilder,
242 GrDrawContext* drawContext,
cdalton846c0512016-05-13 10:25:00 -0700243 const GrClipStackClip& clip,
bsalomon0ba8c242015-10-07 09:20:28 -0700244 const SkRect* devBounds,
245 GrAppliedClip* out) {
cdalton846c0512016-05-13 10:25:00 -0700246 if (!clip.clipStack() || clip.clipStack()->isWideOpen()) {
247 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700248 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000249
bsalomonf045d602015-11-18 19:01:12 -0800250 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800251 int32_t genID = 0;
252 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000253 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800254 bool requiresAA = false;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000255
robertphillips976f5f02016-06-03 10:59:20 -0700256 SkIRect clipSpaceRTIBounds = SkIRect::MakeWH(drawContext->width(), drawContext->height());
cdalton846c0512016-05-13 10:25:00 -0700257 clipSpaceRTIBounds.offset(clip.origin());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000258
cdalton846c0512016-05-13 10:25:00 -0700259 SkIRect clipSpaceReduceQueryBounds;
260#define DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION 0
261 if (devBounds && !DISABLE_DEV_BOUNDS_FOR_CLIP_REDUCTION) {
262 SkIRect devIBounds = devBounds->roundOut();
263 devIBounds.offset(clip.origin());
264 if (!clipSpaceReduceQueryBounds.intersect(clipSpaceRTIBounds, devIBounds)) {
bsalomon9ce30e12015-03-06 08:42:34 -0800265 return false;
266 }
cdalton846c0512016-05-13 10:25:00 -0700267 } else {
268 clipSpaceReduceQueryBounds = clipSpaceRTIBounds;
bsalomon96e02a82015-03-06 07:13:01 -0800269 }
cdalton846c0512016-05-13 10:25:00 -0700270 GrReducedClip::ReduceClipStack(*clip.clipStack(),
271 clipSpaceReduceQueryBounds,
272 &elements,
273 &genID,
274 &initialState,
275 &clipSpaceIBounds,
276 &requiresAA);
277 if (elements.isEmpty()) {
278 if (GrReducedClip::kAllIn_InitialState == initialState) {
279 if (clipSpaceIBounds == clipSpaceRTIBounds) {
280 return true;
281 }
282 } else {
283 return false;
284 }
285 }
cdalton93a379b2016-05-11 13:58:08 -0700286
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000287 // An element count of 4 was chosen because of the common pattern in Blink of:
288 // isect RR
289 // diff RR
290 // isect convex_poly
291 // isect convex_poly
292 // when drawing rounded div borders. This could probably be tuned based on a
293 // configuration's relative costs of switching RTs to generate a mask vs
294 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700295 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800296 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
297 SkIntToScalar(-clip.origin().fY) };
cdaltonede75742015-11-11 15:27:57 -0800298 // When there are multiple samples we want to do per-sample clipping, not compute a
299 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700300 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
301 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700302 // With a single color sample, any coverage info is lost from color once it hits the
303 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
304 // is multisampled.
305 disallowAnalyticAA = pipelineBuilder.isHWAntialias() ||
cdalton93a379b2016-05-11 13:58:08 -0700306 pipelineBuilder.hasUserStencilSettings();
cdalton3ccf2e72016-05-06 09:41:16 -0700307 }
bungeman06ca8ec2016-06-09 08:01:03 -0700308 sk_sp<GrFragmentProcessor> clipFP;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000309 if (elements.isEmpty() ||
bsalomona912dde2015-10-14 15:01:50 -0700310 (requiresAA &&
robertphillips976f5f02016-06-03 10:59:20 -0700311 get_analytic_clip_processor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
312 &clipFP))) {
mtklein217daa72014-07-02 12:55:21 -0700313 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800314 scissorSpaceIBounds.offset(-clip.origin());
robertphillips976f5f02016-06-03 10:59:20 -0700315 if (!devBounds || !SkRect::Make(scissorSpaceIBounds).contains(*devBounds)) {
bungeman06ca8ec2016-06-09 08:01:03 -0700316 out->makeScissoredFPBased(std::move(clipFP), scissorSpaceIBounds);
robertphillips5f2fa472016-05-19 11:36:25 -0700317 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000318 }
bungeman06ca8ec2016-06-09 08:01:03 -0700319 out->makeFPBased(std::move(clipFP));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000320 return true;
321 }
322 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000323
cdaltonede75742015-11-11 15:27:57 -0800324 // If the stencil buffer is multisampled we can use it to do everything.
robertphillips976f5f02016-06-03 10:59:20 -0700325 if (!drawContext->isStencilBufferMultisampled() && requiresAA) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700326 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000327
joshualitt8059eb92014-12-29 15:10:07 -0800328 // The top-left of the mask corresponds to the top-left corner of the bounds.
329 SkVector clipToMaskOffset = {
330 SkIntToScalar(-clipSpaceIBounds.fLeft),
331 SkIntToScalar(-clipSpaceIBounds.fTop)
332 };
333
robertphillips976f5f02016-06-03 10:59:20 -0700334 if (UseSWOnlyPath(context, pipelineBuilder, drawContext,
335 clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000336 // The clip geometry is complex enough that it will be more efficient to create it
337 // entirely in software
robertphillips976f5f02016-06-03 10:59:20 -0700338 result = CreateSoftwareClipMask(context->textureProvider(),
robertphillipsc99b8f02016-05-15 07:53:35 -0700339 genID,
340 initialState,
341 elements,
342 clipToMaskOffset,
343 clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000344 } else {
robertphillips976f5f02016-06-03 10:59:20 -0700345 result = CreateAlphaClipMask(context,
robertphillipsc99b8f02016-05-15 07:53:35 -0700346 genID,
347 initialState,
348 elements,
349 clipToMaskOffset,
350 clipSpaceIBounds);
robertphillips391395d2016-03-02 09:26:36 -0800351 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700352 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000353 }
354
bsalomon49f085d2014-09-05 13:34:00 -0700355 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000356 // The mask's top left coord should be pinned to the rounded-out top left corner of
357 // clipSpace bounds. We determine the mask's position WRT to the render target here.
358 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800359 rtSpaceMaskBounds.offset(-clip.origin());
robertphillips5f2fa472016-05-19 11:36:25 -0700360 out->makeFPBased(create_fp_for_mask(result.get(), rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000361 return true;
362 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000363 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000364 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000365
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000366 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800367 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
robertphillips976f5f02016-06-03 10:59:20 -0700368 CreateStencilClipMask(context,
369 drawContext,
370 genID,
371 initialState,
372 elements,
373 clipSpaceIBounds,
374 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000375
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000376 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
377 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
378 // use both stencil and scissor test to the bounds for the final draw.
379 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
380 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
robertphillips5f2fa472016-05-19 11:36:25 -0700381 out->makeScissoredStencil(true, scissorSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000382 return true;
383}
384
robertphillips391395d2016-03-02 09:26:36 -0800385static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700386 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700387 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800388 const SkMatrix& viewMatrix,
389 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800390
391 // TODO: Draw rrects directly here.
392 switch (element->getType()) {
393 case Element::kEmpty_Type:
394 SkDEBUGFAIL("Should never get here with an empty element.");
395 break;
robertphillips391395d2016-03-02 09:26:36 -0800396 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700397 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800398 element->getOp(),
399 element->isInverseFilled(),
400 element->isAA(),
401 viewMatrix, element->getRect());
402 break;
robertphillips86c60752016-03-02 08:43:13 -0800403 default: {
404 SkPath path;
405 element->asPath(&path);
406 if (path.isInverseFillType()) {
407 path.toggleInverseFillType();
408 }
409
cdalton862cff32016-05-12 15:09:48 -0700410 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800411 element->getOp(),
412 element->isInverseFilled(),
413 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800414 break;
415 }
416 }
robertphillips391395d2016-03-02 09:26:36 -0800417
418 return false;
419}
420
421static void draw_element(GrDrawContext* dc,
422 const GrClip& clip, // TODO: can this just always be WideOpen?
423 const GrPaint &paint,
424 const SkMatrix& viewMatrix,
425 const SkClipStack::Element* element) {
426
427 // TODO: Draw rrects directly here.
428 switch (element->getType()) {
429 case Element::kEmpty_Type:
430 SkDEBUGFAIL("Should never get here with an empty element.");
431 break;
432 case Element::kRect_Type:
433 dc->drawRect(clip, paint, viewMatrix, element->getRect());
434 break;
435 default: {
436 SkPath path;
437 element->asPath(&path);
438 if (path.isInverseFillType()) {
439 path.toggleInverseFillType();
440 }
441
bsalomon6663acf2016-05-10 09:14:17 -0700442 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800443 break;
444 }
445 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000446}
447
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000448////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700449// Create a 8-bit clip mask in alpha
450
451static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
452 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
453 GrUniqueKey::Builder builder(key, kDomain, 3);
454 builder[0] = clipGenID;
455 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
456 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
457}
458
robertphillipsc99b8f02016-05-15 07:53:35 -0700459sk_sp<GrTexture> GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
460 int32_t elementsGenID,
461 GrReducedClip::InitialState initialState,
462 const GrReducedClip::ElementList& elements,
463 const SkVector& clipToMaskOffset,
464 const SkIRect& clipSpaceIBounds) {
robertphillips391395d2016-03-02 09:26:36 -0800465 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700466 GrUniqueKey key;
467 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
468 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700469 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000470 }
471
robertphillips544b9aa2015-10-28 11:01:41 -0700472 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700473 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800474 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700475 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800476 }
477
robertphillipsc99b8f02016-05-15 07:53:35 -0700478 sk_sp<GrDrawContext> dc(context->newDrawContext(SkBackingFit::kApprox,
479 clipSpaceIBounds.width(),
480 clipSpaceIBounds.height(),
481 config));
robertphillips391395d2016-03-02 09:26:36 -0800482 if (!dc) {
483 return nullptr;
484 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700485
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000486 // The texture may be larger than necessary, this rect represents the part of the texture
487 // we populate with a rasterization of the clip.
488 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
489
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000490 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
491 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800492 dc->clear(&maskSpaceIBounds,
493 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
494 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000495
robertphillips391395d2016-03-02 09:26:36 -0800496 // Set the matrix so that rendered clip elements are transformed to mask space from clip
497 // space.
498 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
499
500 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000501 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
502 // pass must not set values outside of this bounds or stencil values outside the rect won't be
503 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800504
robertphillips@google.comf294b772012-04-27 14:29:26 +0000505 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700506 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000507 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000508 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000509 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000510 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700511 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700512
robertphillips391395d2016-03-02 09:26:36 -0800513 // draw directly into the result with the stencil set to make the pixels affected
514 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700515 static constexpr GrUserStencilSettings kStencilInElement(
516 GrUserStencilSettings::StaticInit<
517 0xffff,
518 GrUserStencilTest::kAlways,
519 0xffff,
520 GrUserStencilOp::kReplace,
521 GrUserStencilOp::kReplace,
522 0xffff>()
523 );
cdalton862cff32016-05-12 15:09:48 -0700524 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800525 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800526 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000527 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000528
robertphillips391395d2016-03-02 09:26:36 -0800529 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700530 static constexpr GrUserStencilSettings kDrawOutsideElement(
531 GrUserStencilSettings::StaticInit<
532 0x0000,
533 GrUserStencilTest::kEqual,
534 0xffff,
535 GrUserStencilOp::kZero,
536 GrUserStencilOp::kZero,
537 0xffff>()
538 );
cdalton862cff32016-05-12 15:09:48 -0700539 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800540 op, !invert, false,
541 translate,
542 SkRect::Make(clipSpaceIBounds))) {
robertphillips391395d2016-03-02 09:26:36 -0800543 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000544 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000545 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800546 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800547 GrPaint paint;
548 paint.setAntiAlias(element->isAA());
549 paint.setCoverageSetOpXPFactory(op, false);
550
cdalton846c0512016-05-13 10:25:00 -0700551 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000552 }
553 }
554
robertphillipsc99b8f02016-05-15 07:53:35 -0700555 sk_sp<GrTexture> texture(dc->asTexture());
556 SkASSERT(texture);
557 texture->resourcePriv().setUniqueKey(key);
558 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000559}
560
561////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000562// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000563// (as opposed to canvas) coordinates
robertphillips976f5f02016-06-03 10:59:20 -0700564bool GrClipMaskManager::CreateStencilClipMask(GrContext* context,
565 GrDrawContext* drawContext,
joshualitt9853cce2014-11-17 14:22:48 -0800566 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700567 GrReducedClip::InitialState initialState,
568 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000569 const SkIRect& clipSpaceIBounds,
570 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700571 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000572
robertphillips976f5f02016-06-03 10:59:20 -0700573 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
574 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700575 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000576 return false;
577 }
578
robertphillips976f5f02016-06-03 10:59:20 -0700579 // TODO: these need to be swapped over to using a StencilAttachmentProxy
egdaniel8dc7c3a2015-04-16 11:22:42 -0700580 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
581 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000582 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
583 SkVector translate = {
584 SkIntToScalar(clipSpaceToStencilOffset.fX),
585 SkIntToScalar(clipSpaceToStencilOffset.fY)
586 };
joshualitt8059eb92014-12-29 15:10:07 -0800587 SkMatrix viewMatrix;
588 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000589
bsalomon@google.com9f131742012-12-13 20:43:56 +0000590 // We set the current clip to the bounds so that our recursive draws are scissored to them.
591 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
592 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700593 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000594
robertphillips976f5f02016-06-03 10:59:20 -0700595 drawContext->drawContextPriv().clearStencilClip(
596 stencilSpaceIBounds,
597 GrReducedClip::kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000598
599 // walk through each clip element and perform its set op
600 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700601 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000602 const Element* element = iter.get();
joshualitt9853cce2014-11-17 14:22:48 -0800603
tomhudson@google.com8afae612012-08-14 15:03:35 +0000604 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000605 // enabled at bottom of loop
cdalton846c0512016-05-13 10:25:00 -0700606 clip.enableStencilClip(false);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000607
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000608 // This will be used to determine whether the clip shape can be rendered into the
609 // stencil with arbitrary stencil settings.
610 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000611
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000612 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000613
halcanary96fcdcc2015-08-27 07:41:13 -0700614 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000615 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000616 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000617 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000618 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000619 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000620 element->asPath(&clipPath);
621 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000622 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000623 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000624 }
robertphillips68737822015-10-29 12:12:21 -0700625
robertphillips68737822015-10-29 12:12:21 -0700626 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700627 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700628 canDrawArgs.fViewMatrix = &viewMatrix;
629 canDrawArgs.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700630 canDrawArgs.fStyle = &GrStyle::SimpleFill();
robertphillips68737822015-10-29 12:12:21 -0700631 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700632 canDrawArgs.fHasUserStencilSettings = false;
633 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700634
robertphillips976f5f02016-06-03 10:59:20 -0700635 pr = context->drawingManager()->getPathRenderer(canDrawArgs, false,
636 GrPathRendererChain::kStencilOnly_DrawType,
637 &stencilSupport);
638 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000639 return false;
640 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000641 }
642
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000643 bool canRenderDirectToStencil =
644 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700645 bool drawDirectToClip; // Given the renderer, the element,
646 // fill rule, and set operation should
647 // we render the element directly to
648 // stencil bit used for clipping.
649 GrUserStencilSettings const* const* stencilPasses =
650 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
651 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000652
653 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700654 if (!drawDirectToClip) {
655 static constexpr GrUserStencilSettings kDrawToStencil(
656 GrUserStencilSettings::StaticInit<
657 0x0000,
658 GrUserStencilTest::kAlways,
659 0xffff,
660 GrUserStencilOp::kIncMaybeClamp,
661 GrUserStencilOp::kIncMaybeClamp,
662 0xffff>()
663 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000664 if (Element::kRect_Type == element->getType()) {
robertphillips976f5f02016-06-03 10:59:20 -0700665 DrawNonAARect(drawContext, clip, viewMatrix,
666 element->getRect(), element->isAA(), &kDrawToStencil);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000667 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000668 if (!clipPath.isEmpty()) {
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000669 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700670 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700671 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700672 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700673
674 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700675 args.fResourceProvider = context->resourceProvider();
676 args.fPaint = &paint;
677 args.fUserStencilSettings = &kDrawToStencil;
678 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700679 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700680 args.fColor = GrColor_WHITE;
681 args.fViewMatrix = &viewMatrix;
682 args.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700683 args.fStyle = &GrStyle::SimpleFill();
bsalomon0aff2fa2015-07-31 06:48:27 -0700684 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700685 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700686 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000687 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700688 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700689 args.fResourceProvider = context->resourceProvider();
690 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700691 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700692 args.fViewMatrix = &viewMatrix;
693 args.fPath = &clipPath;
robertphillips976f5f02016-06-03 10:59:20 -0700694 args.fIsAA = element->isAA();
bsalomon0aff2fa2015-07-31 06:48:27 -0700695 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000696 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000697 }
698 }
699 }
700
701 // now we modify the clip bit by rendering either the clip
702 // element directly or a bounding rect of the entire clip.
cdalton846c0512016-05-13 10:25:00 -0700703 clip.enableStencilClip(true);
cdalton93a379b2016-05-11 13:58:08 -0700704 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
joshualitt9853cce2014-11-17 14:22:48 -0800705
cdalton93a379b2016-05-11 13:58:08 -0700706 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000707 if (Element::kRect_Type == element->getType()) {
robertphillips976f5f02016-06-03 10:59:20 -0700708 DrawNonAARect(drawContext, clip,
709 viewMatrix, element->getRect(), element->isAA(), *pass);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000710 } else {
robertphillips976f5f02016-06-03 10:59:20 -0700711 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700712 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700713 paint.setAntiAlias(element->isAA());
714
bsalomon0aff2fa2015-07-31 06:48:27 -0700715 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700716 args.fResourceProvider = context->resourceProvider();
717 args.fPaint = &paint;
718 args.fUserStencilSettings = *pass;
719 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700720 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700721 args.fColor = GrColor_WHITE;
722 args.fViewMatrix = &viewMatrix;
723 args.fPath = &clipPath;
bsalomon6663acf2016-05-10 09:14:17 -0700724 args.fStyle = &GrStyle::SimpleFill();
bsalomon0aff2fa2015-07-31 06:48:27 -0700725 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700726 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700727 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000728 }
729 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000730 // The view matrix is setup to do clip space -> stencil space translation, so
731 // draw rect in clip space.
robertphillips976f5f02016-06-03 10:59:20 -0700732 DrawNonAARect(drawContext, clip, viewMatrix,
733 SkRect::Make(clipSpaceIBounds), false, *pass);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000734 }
735 }
736 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000737 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738 return true;
739}
740
bsalomon@google.com411dad02012-06-05 20:24:20 +0000741////////////////////////////////////////////////////////////////////////////////
robertphillipsc99b8f02016-05-15 07:53:35 -0700742sk_sp<GrTexture> GrClipMaskManager::CreateSoftwareClipMask(
robertphillips0152d732016-05-20 06:38:43 -0700743 GrTextureProvider* texProvider,
robertphillipsc99b8f02016-05-15 07:53:35 -0700744 int32_t elementsGenID,
745 GrReducedClip::InitialState initialState,
746 const GrReducedClip::ElementList& elements,
747 const SkVector& clipToMaskOffset,
748 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700749 GrUniqueKey key;
750 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips0152d732016-05-20 06:38:43 -0700751 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700752 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000753 }
754
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000755 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
756 // the top left corner of the resulting rect to the top left of the texture.
757 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
758
robertphillips0152d732016-05-20 06:38:43 -0700759 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000760
joshualitt8059eb92014-12-29 15:10:07 -0800761 // Set the matrix so that rendered clip elements are transformed to mask space from clip
762 // space.
763 SkMatrix translate;
764 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800765
robertphillips98377402016-05-13 05:47:23 -0700766 helper.init(maskSpaceIBounds, &translate);
tfarinabf54e492014-10-23 17:47:18 -0700767 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000768
tfarinabf54e492014-10-23 17:47:18 -0700769 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000770 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000771 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000772
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000773 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
774 // Intersect and reverse difference require modifying pixels outside of the geometry
775 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
776 // but leave the pixels inside the geometry alone. For reverse difference we invert all
777 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000778 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000779 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000780 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700781 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000782 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000783 SkPath clipPath;
784 element->asPath(&clipPath);
785 clipPath.toggleInverseFillType();
robertphillips98377402016-05-13 05:47:23 -0700786 helper.drawPath(clipPath, GrStyle::SimpleFill(), SkRegion::kReplace_Op,
787 element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000788 continue;
789 }
790
791 // The other ops (union, xor, diff) only affect pixels inside
792 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000793 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700794 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000795 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000796 SkPath path;
797 element->asPath(&path);
robertphillips98377402016-05-13 05:47:23 -0700798 helper.drawPath(path, GrStyle::SimpleFill(), op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000799 }
800 }
801
krajcevskiad1dc582014-06-10 15:06:47 -0700802 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800803 GrSurfaceDesc desc;
804 desc.fWidth = clipSpaceIBounds.width();
805 desc.fHeight = clipSpaceIBounds.height();
806 desc.fConfig = kAlpha_8_GrPixelConfig;
807
robertphillips0152d732016-05-20 06:38:43 -0700808 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800809 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700810 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700811 }
robertphillips391395d2016-03-02 09:26:36 -0800812 result->resourcePriv().setUniqueKey(key);
813
robertphillipsc99b8f02016-05-15 07:53:35 -0700814 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000815
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000816 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000817}