blob: 77d5ea1d03fcc30c47ba42f3d7085cb3d236ec9a [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,
brianosman54f30c12016-07-18 10:53:52 -070047 nullptr,
bsalomon0ba8c242015-10-07 09:20:28 -070048 mat,
49 GrTextureDomain::MakeTexelDomain(result, domainTexels),
50 GrTextureDomain::kDecal_Mode,
51 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -070052 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000053}
54
robertphillips3f7357f2015-10-27 07:17:33 -070055// Does the path in 'element' require SW rendering? If so, return true (and,
56// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
57// 'prOut' to the non-SW path renderer that will do the job).
robertphillips68737822015-10-29 12:12:21 -070058bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
cdalton93a379b2016-05-11 13:58:08 -070059 bool hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070060 const GrDrawContext* drawContext,
robertphillips68737822015-10-29 12:12:21 -070061 const SkMatrix& viewMatrix,
62 const Element* element,
63 GrPathRenderer** prOut,
64 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070065 if (Element::kRect_Type == element->getType()) {
66 // rects can always be drawn directly w/o using the software path
67 // TODO: skip rrects once we're drawing them directly.
68 if (prOut) {
69 *prOut = nullptr;
70 }
71 return false;
72 } else {
73 // We shouldn't get here with an empty clip element.
74 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070075
robertphillips3f7357f2015-10-27 07:17:33 -070076 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
77 SkPath path;
78 element->asPath(&path);
79 if (path.isInverseFillType()) {
80 path.toggleInverseFillType();
81 }
halcanary9d524f22016-03-29 09:03:52 -070082
robertphillips3f7357f2015-10-27 07:17:33 -070083 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -070084
robertphillips423e3372015-10-27 09:23:38 -070085 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070086 type = element->isAA()
87 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
88 : GrPathRendererChain::kStencilAndColor_DrawType;
89 } else {
90 type = element->isAA()
91 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -070092 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -070093 }
halcanary9d524f22016-03-29 09:03:52 -070094
bsalomon8acedde2016-06-24 10:42:16 -070095 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -070096 GrPathRenderer::CanDrawPathArgs canDrawArgs;
97 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
98 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -070099 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700100 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700101 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700102 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700103
robertphillips3f7357f2015-10-27 07:17:33 -0700104 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700105 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700106 if (prOut) {
107 *prOut = pr;
108 }
109 return SkToBool(!pr);
110 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000111}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000112
robertphillips@google.comfa662942012-05-17 12:20:22 +0000113/*
114 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
115 * will be used on any element. If so, it returns true to indicate that the
116 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
117 */
robertphillips391395d2016-03-02 09:26:36 -0800118bool GrClipMaskManager::UseSWOnlyPath(GrContext* context,
robertphillips59cf61a2016-07-13 09:18:21 -0700119 bool hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700120 const GrDrawContext* drawContext,
joshualitt8059eb92014-12-29 15:10:07 -0800121 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800122 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000123 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000124 // a clip gets complex enough it can just be done in SW regardless
125 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000126
joshualitt8059eb92014-12-29 15:10:07 -0800127 // Set the matrix so that rendered clip elements are transformed to mask space from clip
128 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700129 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800130
tfarinabf54e492014-10-23 17:47:18 -0700131 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000132 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700133
134 SkRegion::Op op = element->getOp();
135 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700136 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700137 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700138
robertphillips59cf61a2016-07-13 09:18:21 -0700139 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700140 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700141 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000142 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000143 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000144 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000145}
146
robertphillips976f5f02016-06-03 10:59:20 -0700147static bool get_analytic_clip_processor(const GrReducedClip::ElementList& elements,
148 bool abortIfAA,
149 SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700150 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700151 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000152 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700153 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700154 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700155 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
tfarinabf54e492014-10-23 17:47:18 -0700156 GrReducedClip::ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700157 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000158 SkRegion::Op op = iter.get()->getOp();
159 bool invert;
160 bool skip = false;
161 switch (op) {
162 case SkRegion::kReplace_Op:
163 SkASSERT(iter.get() == elements.head());
164 // Fallthrough, handled same as intersect.
165 case SkRegion::kIntersect_Op:
166 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700167 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000168 skip = true;
169 }
170 break;
171 case SkRegion::kDifference_Op:
172 invert = true;
173 // We don't currently have a cheap test for whether a rect is fully outside an
174 // element's primitive, so don't attempt to set skip.
175 break;
176 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700177 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000178 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000179 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700180 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800181 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700182 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700183 return false;
bsalomona912dde2015-10-14 15:01:50 -0700184 }
joshualittb0a8a372014-09-23 09:50:21 -0700185 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700186 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000187 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700188 edgeType =
189 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000190 }
bsalomona912dde2015-10-14 15:01:50 -0700191
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000192 switch (iter.get()->getType()) {
193 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700194 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
195 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000196 break;
197 case SkClipStack::Element::kRRect_Type: {
198 SkRRect rrect = iter.get()->getRRect();
199 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700200 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000201 break;
202 }
203 case SkClipStack::Element::kRect_Type: {
204 SkRect rect = iter.get()->getRect();
205 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700206 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000207 break;
208 }
209 default:
210 break;
211 }
bungeman06ca8ec2016-06-09 08:01:03 -0700212 if (!fps.back()) {
213 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000214 }
215 }
mtklein217daa72014-07-02 12:55:21 -0700216 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000217 }
218
bsalomon0b5b6b22015-10-14 08:31:34 -0700219 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700220 if (fps.count()) {
221 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000222 }
bungeman06ca8ec2016-06-09 08:01:03 -0700223 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000224}
225
robertphillips@google.comf294b772012-04-27 14:29:26 +0000226////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000227// sort out what kind of clip mask needs to be created: alpha, stencil,
228// scissor, or entirely software
robertphillips976f5f02016-06-03 10:59:20 -0700229bool GrClipMaskManager::SetupClipping(GrContext* context,
robertphillips976f5f02016-06-03 10:59:20 -0700230 GrDrawContext* drawContext,
cdalton846c0512016-05-13 10:25:00 -0700231 const GrClipStackClip& clip,
bsalomonbd2bbe42016-07-08 07:36:42 -0700232 const SkRect* origDevBounds,
robertphillips59cf61a2016-07-13 09:18:21 -0700233 bool useHWAA,
234 bool hasUserStencilSettings,
bsalomon0ba8c242015-10-07 09:20:28 -0700235 GrAppliedClip* out) {
cdalton846c0512016-05-13 10:25:00 -0700236 if (!clip.clipStack() || clip.clipStack()->isWideOpen()) {
237 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700238 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000239
bsalomonf045d602015-11-18 19:01:12 -0800240 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800241 int32_t genID = 0;
242 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000243 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800244 bool requiresAA = false;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000245
cdalton846c0512016-05-13 10:25:00 -0700246 SkIRect clipSpaceReduceQueryBounds;
bsalomonbd2bbe42016-07-08 07:36:42 -0700247 SkRect devBounds;
248 if (origDevBounds) {
249 if (!devBounds.intersect(SkRect::MakeIWH(drawContext->width(), drawContext->height()),
250 *origDevBounds)) {
bsalomon9ce30e12015-03-06 08:42:34 -0800251 return false;
252 }
bsalomonbd2bbe42016-07-08 07:36:42 -0700253 devBounds.roundOut(&clipSpaceReduceQueryBounds);
254 clipSpaceReduceQueryBounds.offset(clip.origin());
cdalton846c0512016-05-13 10:25:00 -0700255 } else {
bsalomonbd2bbe42016-07-08 07:36:42 -0700256 devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
257 clipSpaceReduceQueryBounds.setXYWH(0, 0, drawContext->width(), drawContext->height());
258 clipSpaceReduceQueryBounds.offset(clip.origin());
bsalomon96e02a82015-03-06 07:13:01 -0800259 }
cdalton846c0512016-05-13 10:25:00 -0700260 GrReducedClip::ReduceClipStack(*clip.clipStack(),
261 clipSpaceReduceQueryBounds,
262 &elements,
263 &genID,
264 &initialState,
265 &clipSpaceIBounds,
266 &requiresAA);
267 if (elements.isEmpty()) {
bsalomonbd2bbe42016-07-08 07:36:42 -0700268 if (GrReducedClip::kAllOut_InitialState == initialState) {
cdalton846c0512016-05-13 10:25:00 -0700269 return false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700270 } else {
271 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
272 scissorSpaceIBounds.offset(-clip.origin());
csmartdalton97f6cd52016-07-13 13:37:08 -0700273 if (!GrClip::CanIgnoreScissor(scissorSpaceIBounds, devBounds)) {
bsalomonbd2bbe42016-07-08 07:36:42 -0700274 out->makeScissored(scissorSpaceIBounds);
275 }
276 return true;
cdalton846c0512016-05-13 10:25:00 -0700277 }
278 }
cdalton93a379b2016-05-11 13:58:08 -0700279
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000280 // An element count of 4 was chosen because of the common pattern in Blink of:
281 // isect RR
282 // diff RR
283 // isect convex_poly
284 // isect convex_poly
285 // when drawing rounded div borders. This could probably be tuned based on a
286 // configuration's relative costs of switching RTs to generate a mask vs
287 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700288 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800289 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
290 SkIntToScalar(-clip.origin().fY) };
cdaltonede75742015-11-11 15:27:57 -0800291 // When there are multiple samples we want to do per-sample clipping, not compute a
292 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700293 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
294 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700295 // With a single color sample, any coverage info is lost from color once it hits the
296 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
297 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700298 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700299 }
bungeman06ca8ec2016-06-09 08:01:03 -0700300 sk_sp<GrFragmentProcessor> clipFP;
bsalomonbd2bbe42016-07-08 07:36:42 -0700301 if (requiresAA &&
302 get_analytic_clip_processor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
303 &clipFP)) {
mtklein217daa72014-07-02 12:55:21 -0700304 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800305 scissorSpaceIBounds.offset(-clip.origin());
csmartdalton97f6cd52016-07-13 13:37:08 -0700306 if (GrClip::CanIgnoreScissor(scissorSpaceIBounds, devBounds)) {
307 out->makeFPBased(std::move(clipFP), SkRect::Make(scissorSpaceIBounds));
308 } else {
bungeman06ca8ec2016-06-09 08:01:03 -0700309 out->makeScissoredFPBased(std::move(clipFP), scissorSpaceIBounds);
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000310 }
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000311 return true;
312 }
313 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000314
cdaltonede75742015-11-11 15:27:57 -0800315 // If the stencil buffer is multisampled we can use it to do everything.
robertphillips976f5f02016-06-03 10:59:20 -0700316 if (!drawContext->isStencilBufferMultisampled() && requiresAA) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700317 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000318
joshualitt8059eb92014-12-29 15:10:07 -0800319 // The top-left of the mask corresponds to the top-left corner of the bounds.
320 SkVector clipToMaskOffset = {
321 SkIntToScalar(-clipSpaceIBounds.fLeft),
322 SkIntToScalar(-clipSpaceIBounds.fTop)
323 };
324
robertphillips59cf61a2016-07-13 09:18:21 -0700325 if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext,
robertphillips976f5f02016-06-03 10:59:20 -0700326 clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000327 // The clip geometry is complex enough that it will be more efficient to create it
328 // entirely in software
robertphillips976f5f02016-06-03 10:59:20 -0700329 result = CreateSoftwareClipMask(context->textureProvider(),
robertphillipsc99b8f02016-05-15 07:53:35 -0700330 genID,
331 initialState,
332 elements,
333 clipToMaskOffset,
334 clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000335 } else {
robertphillips976f5f02016-06-03 10:59:20 -0700336 result = CreateAlphaClipMask(context,
robertphillipsc99b8f02016-05-15 07:53:35 -0700337 genID,
338 initialState,
339 elements,
340 clipToMaskOffset,
341 clipSpaceIBounds);
robertphillips391395d2016-03-02 09:26:36 -0800342 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700343 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000344 }
345
bsalomon49f085d2014-09-05 13:34:00 -0700346 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000347 // The mask's top left coord should be pinned to the rounded-out top left corner of
348 // clipSpace bounds. We determine the mask's position WRT to the render target here.
349 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800350 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon6cc90062016-07-08 11:31:22 -0700351 out->makeFPBased(create_fp_for_mask(result.get(), rtSpaceMaskBounds),
352 SkRect::Make(rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000353 return true;
354 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000355 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000356 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000357
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000358 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800359 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
robertphillips976f5f02016-06-03 10:59:20 -0700360 CreateStencilClipMask(context,
361 drawContext,
362 genID,
363 initialState,
364 elements,
365 clipSpaceIBounds,
366 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000367
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000368 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
369 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
370 // use both stencil and scissor test to the bounds for the final draw.
371 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
372 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
csmartdalton97f6cd52016-07-13 13:37:08 -0700373 if (GrClip::CanIgnoreScissor(scissorSpaceIBounds, devBounds)) {
374 out->makeStencil(true, devBounds);
375 } else {
376 out->makeScissoredStencil(scissorSpaceIBounds);
377 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000378 return true;
379}
380
robertphillips391395d2016-03-02 09:26:36 -0800381static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700382 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700383 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800384 const SkMatrix& viewMatrix,
385 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800386
387 // TODO: Draw rrects directly here.
388 switch (element->getType()) {
389 case Element::kEmpty_Type:
390 SkDEBUGFAIL("Should never get here with an empty element.");
391 break;
robertphillips391395d2016-03-02 09:26:36 -0800392 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700393 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800394 element->getOp(),
395 element->isInverseFilled(),
396 element->isAA(),
397 viewMatrix, element->getRect());
398 break;
robertphillips86c60752016-03-02 08:43:13 -0800399 default: {
400 SkPath path;
401 element->asPath(&path);
402 if (path.isInverseFillType()) {
403 path.toggleInverseFillType();
404 }
405
cdalton862cff32016-05-12 15:09:48 -0700406 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800407 element->getOp(),
408 element->isInverseFilled(),
409 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800410 break;
411 }
412 }
robertphillips391395d2016-03-02 09:26:36 -0800413
414 return false;
415}
416
417static void draw_element(GrDrawContext* dc,
418 const GrClip& clip, // TODO: can this just always be WideOpen?
419 const GrPaint &paint,
420 const SkMatrix& viewMatrix,
421 const SkClipStack::Element* element) {
422
423 // TODO: Draw rrects directly here.
424 switch (element->getType()) {
425 case Element::kEmpty_Type:
426 SkDEBUGFAIL("Should never get here with an empty element.");
427 break;
428 case Element::kRect_Type:
429 dc->drawRect(clip, paint, viewMatrix, element->getRect());
430 break;
431 default: {
432 SkPath path;
433 element->asPath(&path);
434 if (path.isInverseFillType()) {
435 path.toggleInverseFillType();
436 }
437
bsalomon6663acf2016-05-10 09:14:17 -0700438 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800439 break;
440 }
441 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000442}
443
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000444////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700445// Create a 8-bit clip mask in alpha
446
447static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
448 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
449 GrUniqueKey::Builder builder(key, kDomain, 3);
450 builder[0] = clipGenID;
451 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
452 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
453}
454
robertphillipsc99b8f02016-05-15 07:53:35 -0700455sk_sp<GrTexture> GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
456 int32_t elementsGenID,
457 GrReducedClip::InitialState initialState,
458 const GrReducedClip::ElementList& elements,
459 const SkVector& clipToMaskOffset,
460 const SkIRect& clipSpaceIBounds) {
robertphillips391395d2016-03-02 09:26:36 -0800461 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700462 GrUniqueKey key;
463 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
464 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700465 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000466 }
467
robertphillips544b9aa2015-10-28 11:01:41 -0700468 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700469 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800470 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700471 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800472 }
473
robertphillipsc99b8f02016-05-15 07:53:35 -0700474 sk_sp<GrDrawContext> dc(context->newDrawContext(SkBackingFit::kApprox,
475 clipSpaceIBounds.width(),
476 clipSpaceIBounds.height(),
477 config));
robertphillips391395d2016-03-02 09:26:36 -0800478 if (!dc) {
479 return nullptr;
480 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700481
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000482 // The texture may be larger than necessary, this rect represents the part of the texture
483 // we populate with a rasterization of the clip.
484 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
485
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000486 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
487 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800488 dc->clear(&maskSpaceIBounds,
489 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
490 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000491
robertphillips391395d2016-03-02 09:26:36 -0800492 // Set the matrix so that rendered clip elements are transformed to mask space from clip
493 // space.
494 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
495
496 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000497 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
498 // pass must not set values outside of this bounds or stencil values outside the rect won't be
499 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800500
robertphillips@google.comf294b772012-04-27 14:29:26 +0000501 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700502 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000503 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000504 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000505 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000506 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700507 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700508
robertphillips391395d2016-03-02 09:26:36 -0800509 // draw directly into the result with the stencil set to make the pixels affected
510 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700511 static constexpr GrUserStencilSettings kStencilInElement(
512 GrUserStencilSettings::StaticInit<
513 0xffff,
514 GrUserStencilTest::kAlways,
515 0xffff,
516 GrUserStencilOp::kReplace,
517 GrUserStencilOp::kReplace,
518 0xffff>()
519 );
cdalton862cff32016-05-12 15:09:48 -0700520 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800521 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800522 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000523 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000524
robertphillips391395d2016-03-02 09:26:36 -0800525 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700526 static constexpr GrUserStencilSettings kDrawOutsideElement(
527 GrUserStencilSettings::StaticInit<
528 0x0000,
529 GrUserStencilTest::kEqual,
530 0xffff,
531 GrUserStencilOp::kZero,
532 GrUserStencilOp::kZero,
533 0xffff>()
534 );
cdalton862cff32016-05-12 15:09:48 -0700535 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800536 op, !invert, false,
537 translate,
538 SkRect::Make(clipSpaceIBounds))) {
robertphillips391395d2016-03-02 09:26:36 -0800539 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000540 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000541 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800542 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800543 GrPaint paint;
544 paint.setAntiAlias(element->isAA());
545 paint.setCoverageSetOpXPFactory(op, false);
546
cdalton846c0512016-05-13 10:25:00 -0700547 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000548 }
549 }
550
robertphillipsc99b8f02016-05-15 07:53:35 -0700551 sk_sp<GrTexture> texture(dc->asTexture());
552 SkASSERT(texture);
553 texture->resourcePriv().setUniqueKey(key);
554 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000555}
556
557////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000558// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000559// (as opposed to canvas) coordinates
robertphillips976f5f02016-06-03 10:59:20 -0700560bool GrClipMaskManager::CreateStencilClipMask(GrContext* context,
561 GrDrawContext* drawContext,
joshualitt9853cce2014-11-17 14:22:48 -0800562 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700563 GrReducedClip::InitialState initialState,
564 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000565 const SkIRect& clipSpaceIBounds,
566 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700567 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000568
robertphillips976f5f02016-06-03 10:59:20 -0700569 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
570 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700571 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000572 return false;
573 }
574
robertphillips976f5f02016-06-03 10:59:20 -0700575 // TODO: these need to be swapped over to using a StencilAttachmentProxy
egdaniel8dc7c3a2015-04-16 11:22:42 -0700576 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
577 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000578 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
579 SkVector translate = {
580 SkIntToScalar(clipSpaceToStencilOffset.fX),
581 SkIntToScalar(clipSpaceToStencilOffset.fY)
582 };
joshualitt8059eb92014-12-29 15:10:07 -0800583 SkMatrix viewMatrix;
584 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000585
bsalomon@google.com9f131742012-12-13 20:43:56 +0000586 // We set the current clip to the bounds so that our recursive draws are scissored to them.
587 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
588 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700589 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000590
robertphillips976f5f02016-06-03 10:59:20 -0700591 drawContext->drawContextPriv().clearStencilClip(
592 stencilSpaceIBounds,
593 GrReducedClip::kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000594
595 // walk through each clip element and perform its set op
596 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700597 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000598 const Element* element = iter.get();
csmartdalton656dbe42016-06-10 12:32:57 -0700599 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
joshualitt9853cce2014-11-17 14:22:48 -0800600
tomhudson@google.com8afae612012-08-14 15:03:35 +0000601 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000602 // enabled at bottom of loop
bsalomon6cc90062016-07-08 11:31:22 -0700603 clip.disableStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000604
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000605 // This will be used to determine whether the clip shape can be rendered into the
606 // stencil with arbitrary stencil settings.
607 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000608
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000609 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000610
halcanary96fcdcc2015-08-27 07:41:13 -0700611 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000612 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000613 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000614 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000615 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000616 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000617 element->asPath(&clipPath);
618 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000619 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000620 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000621 }
robertphillips68737822015-10-29 12:12:21 -0700622
bsalomon8acedde2016-06-24 10:42:16 -0700623 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700624 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700625 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700626 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700627 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700628 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700629 canDrawArgs.fHasUserStencilSettings = false;
630 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700631
robertphillips976f5f02016-06-03 10:59:20 -0700632 pr = context->drawingManager()->getPathRenderer(canDrawArgs, false,
633 GrPathRendererChain::kStencilOnly_DrawType,
634 &stencilSupport);
635 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000636 return false;
637 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000638 }
639
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000640 bool canRenderDirectToStencil =
641 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700642 bool drawDirectToClip; // Given the renderer, the element,
643 // fill rule, and set operation should
644 // we render the element directly to
645 // stencil bit used for clipping.
646 GrUserStencilSettings const* const* stencilPasses =
647 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
648 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000649
650 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700651 if (!drawDirectToClip) {
652 static constexpr GrUserStencilSettings kDrawToStencil(
653 GrUserStencilSettings::StaticInit<
654 0x0000,
655 GrUserStencilTest::kAlways,
656 0xffff,
657 GrUserStencilOp::kIncMaybeClamp,
658 GrUserStencilOp::kIncMaybeClamp,
659 0xffff>()
660 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000661 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700662 drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA,
663 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000664 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000665 if (!clipPath.isEmpty()) {
bsalomon8acedde2016-06-24 10:42:16 -0700666 GrShape shape(clipPath, GrStyle::SimpleFill());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000667 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700668 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700669 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700670 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700671
672 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700673 args.fResourceProvider = context->resourceProvider();
674 args.fPaint = &paint;
675 args.fUserStencilSettings = &kDrawToStencil;
676 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700677 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700678 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700679 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700680 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700681 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700682 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000683 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700684 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700685 args.fResourceProvider = context->resourceProvider();
686 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700687 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700688 args.fViewMatrix = &viewMatrix;
robertphillips976f5f02016-06-03 10:59:20 -0700689 args.fIsAA = element->isAA();
bsalomon8acedde2016-06-24 10:42:16 -0700690 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700691 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000692 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000693 }
694 }
695 }
696
697 // now we modify the clip bit by rendering either the clip
698 // element directly or a bounding rect of the entire clip.
cdalton93a379b2016-05-11 13:58:08 -0700699 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
joshualitt9853cce2014-11-17 14:22:48 -0800700
cdalton93a379b2016-05-11 13:58:08 -0700701 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000702 if (Element::kRect_Type == element->getType()) {
bsalomon6cc90062016-07-08 11:31:22 -0700703 clip.enableStencilClip(element->getRect().makeOffset(translate.fX,
704 translate.fY));
csmartdalton656dbe42016-06-10 12:32:57 -0700705 drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix,
706 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000707 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700708 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips976f5f02016-06-03 10:59:20 -0700709 GrPaint paint;
bsalomon6cc90062016-07-08 11:31:22 -0700710 SkRect bounds = clipPath.getBounds();
711 bounds.offset(translate.fX, translate.fY);
712 clip.enableStencilClip(bounds);
bungeman06ca8ec2016-06-09 08:01:03 -0700713 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700714 paint.setAntiAlias(element->isAA());
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.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700722 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700723 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700724 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700725 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000726 }
727 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000728 // The view matrix is setup to do clip space -> stencil space translation, so
729 // draw rect in clip space.
bsalomon6cc90062016-07-08 11:31:22 -0700730 SkRect bounds = SkRect::Make(clipSpaceIBounds);
731 bounds.offset(translate.fX, translate.fY);
732 clip.enableStencilClip(bounds);
csmartdalton656dbe42016-06-10 12:32:57 -0700733 drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix,
734 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000735 }
736 }
737 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000738 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000739 return true;
740}
741
bsalomon@google.com411dad02012-06-05 20:24:20 +0000742////////////////////////////////////////////////////////////////////////////////
robertphillipsc99b8f02016-05-15 07:53:35 -0700743sk_sp<GrTexture> GrClipMaskManager::CreateSoftwareClipMask(
robertphillips0152d732016-05-20 06:38:43 -0700744 GrTextureProvider* texProvider,
robertphillipsc99b8f02016-05-15 07:53:35 -0700745 int32_t elementsGenID,
746 GrReducedClip::InitialState initialState,
747 const GrReducedClip::ElementList& elements,
748 const SkVector& clipToMaskOffset,
749 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700750 GrUniqueKey key;
751 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips0152d732016-05-20 06:38:43 -0700752 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700753 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000754 }
755
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000756 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
757 // the top left corner of the resulting rect to the top left of the texture.
758 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
759
robertphillips0152d732016-05-20 06:38:43 -0700760 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000761
joshualitt8059eb92014-12-29 15:10:07 -0800762 // Set the matrix so that rendered clip elements are transformed to mask space from clip
763 // space.
764 SkMatrix translate;
765 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800766
robertphillips98377402016-05-13 05:47:23 -0700767 helper.init(maskSpaceIBounds, &translate);
tfarinabf54e492014-10-23 17:47:18 -0700768 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000769
tfarinabf54e492014-10-23 17:47:18 -0700770 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000771 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000772 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000773
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000774 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
775 // Intersect and reverse difference require modifying pixels outside of the geometry
776 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
777 // but leave the pixels inside the geometry alone. For reverse difference we invert all
778 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000779 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000780 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000781 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700782 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000783 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000784 SkPath clipPath;
785 element->asPath(&clipPath);
786 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700787 GrShape shape(clipPath, GrStyle::SimpleFill());
788 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000789 continue;
790 }
791
792 // The other ops (union, xor, diff) only affect pixels inside
793 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000794 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700795 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000796 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000797 SkPath path;
798 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700799 GrShape shape(path, GrStyle::SimpleFill());
800 helper.drawShape(shape, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000801 }
802 }
803
krajcevskiad1dc582014-06-10 15:06:47 -0700804 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800805 GrSurfaceDesc desc;
806 desc.fWidth = clipSpaceIBounds.width();
807 desc.fHeight = clipSpaceIBounds.height();
808 desc.fConfig = kAlpha_8_GrPixelConfig;
809
robertphillips0152d732016-05-20 06:38:43 -0700810 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800811 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700812 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700813 }
robertphillips391395d2016-03-02 09:26:36 -0800814 result->resourcePriv().setUniqueKey(key);
815
robertphillipsc99b8f02016-05-15 07:53:35 -0700816 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000817
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000818 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000819}