blob: 13dafea0e4dfb43e766fd6bdf0b95697a46c4a32 [file] [log] [blame]
robertphillips@google.com1e945b72012-04-16 18:03:03 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrClipMaskManager.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -07009#include "GrCaps.h"
robertphillips68737822015-10-29 12:12:21 -070010#include "GrDrawingManager.h"
robertphillips391395d2016-03-02 09:26:36 -080011#include "GrDrawContextPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070012#include "GrGpuResourcePriv.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrPaint.h"
14#include "GrPathRenderer.h"
15#include "GrRenderTarget.h"
bsalomon6bc1b5f2015-02-23 09:06:38 -080016#include "GrRenderTargetPriv.h"
bsalomon473addf2015-10-02 07:49:05 -070017#include "GrResourceProvider.h"
egdaniel8dc7c3a2015-04-16 11:22:42 -070018#include "GrStencilAttachment.h"
robertphillips@google.com58b20212012-06-27 20:44:52 +000019#include "GrSWMaskHelper.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070020#include "SkRasterClip.h"
joshualitt3a0cfeb2014-10-27 07:38:01 -070021#include "SkTLazy.h"
joshualitta8b84992016-01-13 13:35:35 -080022#include "batches/GrRectBatchFactory.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080023#include "effects/GrConvexPolyEffect.h"
egdaniel95131432014-12-09 11:15:43 -080024#include "effects/GrPorterDuffXferProcessor.h"
egdaniel8d95ffa2014-12-08 13:26:43 -080025#include "effects/GrRRectEffect.h"
egdaniel95131432014-12-09 11:15:43 -080026#include "effects/GrTextureDomain.h"
bsalomon@google.comc6b3e482012-12-07 20:43:52 +000027
bsalomon@google.com8182fa02012-12-04 14:06:06 +000028typedef SkClipStack::Element Element;
bsalomon@google.com51a62862012-11-26 21:19:43 +000029
robertphillips976f5f02016-06-03 10:59:20 -070030static const int kMaxAnalyticElements = 4;
31
bsalomon@google.com51a62862012-11-26 21:19:43 +000032////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +000033// set up the draw state to enable the aa clipping mask. Besides setting up the
bsalomon@google.com08283af2012-10-26 13:01:20 +000034// stage matrix this also alters the vertex layout
bungeman06ca8ec2016-06-09 08:01:03 -070035static sk_sp<GrFragmentProcessor> create_fp_for_mask(GrTexture* result,
36 const SkIRect &devBound) {
bsalomon@google.comb9086a02012-11-01 18:02:54 +000037 SkMatrix mat;
bsalomon309d4d52014-12-18 10:17:44 -080038 // We use device coords to compute the texture coordinates. We set our matrix to be a
39 // translation to the devBound, and then a scaling matrix to normalized coords.
robertphillips@google.coma72eef32012-05-01 17:22:59 +000040 mat.setIDiv(result->width(), result->height());
rmistry@google.comfbfcd562012-08-23 18:09:54 +000041 mat.preTranslate(SkIntToScalar(-devBound.fLeft),
robertphillips@google.com7b112892012-07-31 15:18:21 +000042 SkIntToScalar(-devBound.fTop));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000043
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +000044 SkIRect domainTexels = SkIRect::MakeWH(devBound.width(), devBound.height());
bungeman06ca8ec2016-06-09 08:01:03 -070045 return sk_sp<GrFragmentProcessor>(GrTextureDomainEffect::Make(
robertphillips5f2fa472016-05-19 11:36:25 -070046 result,
bsalomon0ba8c242015-10-07 09:20:28 -070047 mat,
48 GrTextureDomain::MakeTexelDomain(result, domainTexels),
49 GrTextureDomain::kDecal_Mode,
50 GrTextureParams::kNone_FilterMode,
robertphillips5f2fa472016-05-19 11:36:25 -070051 kDevice_GrCoordSet));
robertphillips@google.coma72eef32012-05-01 17:22:59 +000052}
53
robertphillips3f7357f2015-10-27 07:17:33 -070054// Does the path in 'element' require SW rendering? If so, return true (and,
55// optionally, set 'prOut' to NULL. If not, return false (and, optionally, set
56// 'prOut' to the non-SW path renderer that will do the job).
robertphillips68737822015-10-29 12:12:21 -070057bool GrClipMaskManager::PathNeedsSWRenderer(GrContext* context,
cdalton93a379b2016-05-11 13:58:08 -070058 bool hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -070059 const GrDrawContext* drawContext,
robertphillips68737822015-10-29 12:12:21 -070060 const SkMatrix& viewMatrix,
61 const Element* element,
62 GrPathRenderer** prOut,
63 bool needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070064 if (Element::kRect_Type == element->getType()) {
65 // rects can always be drawn directly w/o using the software path
66 // TODO: skip rrects once we're drawing them directly.
67 if (prOut) {
68 *prOut = nullptr;
69 }
70 return false;
71 } else {
72 // We shouldn't get here with an empty clip element.
73 SkASSERT(Element::kEmpty_Type != element->getType());
robertphillips5c3ea4c2015-10-26 08:33:10 -070074
robertphillips3f7357f2015-10-27 07:17:33 -070075 // the gpu alpha mask will draw the inverse paths as non-inverse to a temp buffer
76 SkPath path;
77 element->asPath(&path);
78 if (path.isInverseFillType()) {
79 path.toggleInverseFillType();
80 }
halcanary9d524f22016-03-29 09:03:52 -070081
robertphillips3f7357f2015-10-27 07:17:33 -070082 GrPathRendererChain::DrawType type;
halcanary9d524f22016-03-29 09:03:52 -070083
robertphillips423e3372015-10-27 09:23:38 -070084 if (needsStencil) {
robertphillips3f7357f2015-10-27 07:17:33 -070085 type = element->isAA()
86 ? GrPathRendererChain::kStencilAndColorAntiAlias_DrawType
87 : GrPathRendererChain::kStencilAndColor_DrawType;
88 } else {
89 type = element->isAA()
90 ? GrPathRendererChain::kColorAntiAlias_DrawType
halcanary9d524f22016-03-29 09:03:52 -070091 : GrPathRendererChain::kColor_DrawType;
robertphillips3f7357f2015-10-27 07:17:33 -070092 }
halcanary9d524f22016-03-29 09:03:52 -070093
bsalomon8acedde2016-06-24 10:42:16 -070094 GrShape shape(path, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -070095 GrPathRenderer::CanDrawPathArgs canDrawArgs;
96 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
97 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -070098 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -070099 canDrawArgs.fAntiAlias = element->isAA();
cdalton93a379b2016-05-11 13:58:08 -0700100 canDrawArgs.fHasUserStencilSettings = hasUserStencilSettings;
robertphillips976f5f02016-06-03 10:59:20 -0700101 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700102
robertphillips3f7357f2015-10-27 07:17:33 -0700103 // the 'false' parameter disallows use of the SW path renderer
robertphillips68737822015-10-29 12:12:21 -0700104 GrPathRenderer* pr = context->drawingManager()->getPathRenderer(canDrawArgs, false, type);
robertphillips3f7357f2015-10-27 07:17:33 -0700105 if (prOut) {
106 *prOut = pr;
107 }
108 return SkToBool(!pr);
109 }
robertphillips@google.come79f3202014-02-11 16:30:21 +0000110}
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000111
robertphillips@google.comfa662942012-05-17 12:20:22 +0000112/*
113 * This method traverses the clip stack to see if the GrSoftwarePathRenderer
114 * will be used on any element. If so, it returns true to indicate that the
115 * entire clip should be rendered in SW and then uploaded en masse to the gpu.
116 */
robertphillips391395d2016-03-02 09:26:36 -0800117bool GrClipMaskManager::UseSWOnlyPath(GrContext* context,
robertphillips59cf61a2016-07-13 09:18:21 -0700118 bool hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700119 const GrDrawContext* drawContext,
joshualitt8059eb92014-12-29 15:10:07 -0800120 const SkVector& clipToMaskOffset,
joshualitt9853cce2014-11-17 14:22:48 -0800121 const GrReducedClip::ElementList& elements) {
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000122 // TODO: generalize this function so that when
robertphillips@google.comfa662942012-05-17 12:20:22 +0000123 // a clip gets complex enough it can just be done in SW regardless
124 // of whether it would invoke the GrSoftwarePathRenderer.
skia.committer@gmail.comd21444a2012-12-07 02:01:25 +0000125
joshualitt8059eb92014-12-29 15:10:07 -0800126 // Set the matrix so that rendered clip elements are transformed to mask space from clip
127 // space.
robertphillipscf10b5a2015-10-27 07:53:35 -0700128 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
joshualitt8059eb92014-12-29 15:10:07 -0800129
tfarinabf54e492014-10-23 17:47:18 -0700130 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000131 const Element* element = iter.get();
robertphillips3f7357f2015-10-27 07:17:33 -0700132
133 SkRegion::Op op = element->getOp();
134 bool invert = element->isInverseFilled();
halcanary9d524f22016-03-29 09:03:52 -0700135 bool needsStencil = invert ||
robertphillips423e3372015-10-27 09:23:38 -0700136 SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op;
robertphillips3f7357f2015-10-27 07:17:33 -0700137
robertphillips59cf61a2016-07-13 09:18:21 -0700138 if (PathNeedsSWRenderer(context, hasUserStencilSettings,
robertphillips976f5f02016-06-03 10:59:20 -0700139 drawContext, translate, element, nullptr, needsStencil)) {
robertphillips3f7357f2015-10-27 07:17:33 -0700140 return true;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000141 }
robertphillips@google.comfa662942012-05-17 12:20:22 +0000142 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000143 return false;
robertphillips@google.coma72eef32012-05-01 17:22:59 +0000144}
145
robertphillips976f5f02016-06-03 10:59:20 -0700146static bool get_analytic_clip_processor(const GrReducedClip::ElementList& elements,
147 bool abortIfAA,
148 SkVector& clipToRTOffset,
bsalomonbd2bbe42016-07-08 07:36:42 -0700149 const SkRect& drawBounds,
bungeman06ca8ec2016-06-09 08:01:03 -0700150 sk_sp<GrFragmentProcessor>* resultFP) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000151 SkRect boundsInClipSpace;
bsalomonbd2bbe42016-07-08 07:36:42 -0700152 boundsInClipSpace = drawBounds.makeOffset(-clipToRTOffset.fX, -clipToRTOffset.fY);
bsalomon0ba8c242015-10-07 09:20:28 -0700153 SkASSERT(elements.count() <= kMaxAnalyticElements);
bungeman06ca8ec2016-06-09 08:01:03 -0700154 SkSTArray<kMaxAnalyticElements, sk_sp<GrFragmentProcessor>> fps;
tfarinabf54e492014-10-23 17:47:18 -0700155 GrReducedClip::ElementList::Iter iter(elements);
bsalomon49f085d2014-09-05 13:34:00 -0700156 while (iter.get()) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000157 SkRegion::Op op = iter.get()->getOp();
158 bool invert;
159 bool skip = false;
160 switch (op) {
161 case SkRegion::kReplace_Op:
162 SkASSERT(iter.get() == elements.head());
163 // Fallthrough, handled same as intersect.
164 case SkRegion::kIntersect_Op:
165 invert = false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700166 if (iter.get()->contains(boundsInClipSpace)) {
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000167 skip = true;
168 }
169 break;
170 case SkRegion::kDifference_Op:
171 invert = true;
172 // We don't currently have a cheap test for whether a rect is fully outside an
173 // element's primitive, so don't attempt to set skip.
174 break;
175 default:
bungeman06ca8ec2016-06-09 08:01:03 -0700176 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000177 }
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000178 if (!skip) {
joshualittb0a8a372014-09-23 09:50:21 -0700179 GrPrimitiveEdgeType edgeType;
robertphillipse85a32d2015-02-10 08:16:55 -0800180 if (iter.get()->isAA()) {
bsalomona912dde2015-10-14 15:01:50 -0700181 if (abortIfAA) {
bungeman06ca8ec2016-06-09 08:01:03 -0700182 return false;
bsalomona912dde2015-10-14 15:01:50 -0700183 }
joshualittb0a8a372014-09-23 09:50:21 -0700184 edgeType =
bsalomon0ba8c242015-10-07 09:20:28 -0700185 invert ? kInverseFillAA_GrProcessorEdgeType : kFillAA_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000186 } else {
bsalomon0ba8c242015-10-07 09:20:28 -0700187 edgeType =
188 invert ? kInverseFillBW_GrProcessorEdgeType : kFillBW_GrProcessorEdgeType;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000189 }
bsalomona912dde2015-10-14 15:01:50 -0700190
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000191 switch (iter.get()->getType()) {
192 case SkClipStack::Element::kPath_Type:
bungeman06ca8ec2016-06-09 08:01:03 -0700193 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, iter.get()->getPath(),
194 &clipToRTOffset));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000195 break;
196 case SkClipStack::Element::kRRect_Type: {
197 SkRRect rrect = iter.get()->getRRect();
198 rrect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700199 fps.emplace_back(GrRRectEffect::Make(edgeType, rrect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000200 break;
201 }
202 case SkClipStack::Element::kRect_Type: {
203 SkRect rect = iter.get()->getRect();
204 rect.offset(clipToRTOffset.fX, clipToRTOffset.fY);
bungeman06ca8ec2016-06-09 08:01:03 -0700205 fps.emplace_back(GrConvexPolyEffect::Make(edgeType, rect));
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000206 break;
207 }
208 default:
209 break;
210 }
bungeman06ca8ec2016-06-09 08:01:03 -0700211 if (!fps.back()) {
212 return false;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000213 }
214 }
mtklein217daa72014-07-02 12:55:21 -0700215 iter.next();
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000216 }
217
bsalomon0b5b6b22015-10-14 08:31:34 -0700218 *resultFP = nullptr;
bungeman06ca8ec2016-06-09 08:01:03 -0700219 if (fps.count()) {
220 *resultFP = GrFragmentProcessor::RunInSeries(fps.begin(), fps.count());
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000221 }
bungeman06ca8ec2016-06-09 08:01:03 -0700222 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000223}
224
robertphillips@google.comf294b772012-04-27 14:29:26 +0000225////////////////////////////////////////////////////////////////////////////////
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000226// sort out what kind of clip mask needs to be created: alpha, stencil,
227// scissor, or entirely software
robertphillips976f5f02016-06-03 10:59:20 -0700228bool GrClipMaskManager::SetupClipping(GrContext* context,
robertphillips976f5f02016-06-03 10:59:20 -0700229 GrDrawContext* drawContext,
cdalton846c0512016-05-13 10:25:00 -0700230 const GrClipStackClip& clip,
bsalomonbd2bbe42016-07-08 07:36:42 -0700231 const SkRect* origDevBounds,
robertphillips59cf61a2016-07-13 09:18:21 -0700232 bool useHWAA,
233 bool hasUserStencilSettings,
bsalomon0ba8c242015-10-07 09:20:28 -0700234 GrAppliedClip* out) {
cdalton846c0512016-05-13 10:25:00 -0700235 if (!clip.clipStack() || clip.clipStack()->isWideOpen()) {
236 return true;
joshualitt7a6184f2014-10-29 18:29:27 -0700237 }
bsalomon@google.coma3201942012-06-21 19:58:20 +0000238
bsalomonf045d602015-11-18 19:01:12 -0800239 GrReducedClip::ElementList elements;
brucedawson71d7f7f2015-02-26 13:28:53 -0800240 int32_t genID = 0;
241 GrReducedClip::InitialState initialState = GrReducedClip::kAllIn_InitialState;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000242 SkIRect clipSpaceIBounds;
brucedawson71d7f7f2015-02-26 13:28:53 -0800243 bool requiresAA = false;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000244
cdalton846c0512016-05-13 10:25:00 -0700245 SkIRect clipSpaceReduceQueryBounds;
bsalomonbd2bbe42016-07-08 07:36:42 -0700246 SkRect devBounds;
247 if (origDevBounds) {
248 if (!devBounds.intersect(SkRect::MakeIWH(drawContext->width(), drawContext->height()),
249 *origDevBounds)) {
bsalomon9ce30e12015-03-06 08:42:34 -0800250 return false;
251 }
bsalomonbd2bbe42016-07-08 07:36:42 -0700252 devBounds.roundOut(&clipSpaceReduceQueryBounds);
253 clipSpaceReduceQueryBounds.offset(clip.origin());
cdalton846c0512016-05-13 10:25:00 -0700254 } else {
bsalomonbd2bbe42016-07-08 07:36:42 -0700255 devBounds = SkRect::MakeIWH(drawContext->width(), drawContext->height());
256 clipSpaceReduceQueryBounds.setXYWH(0, 0, drawContext->width(), drawContext->height());
257 clipSpaceReduceQueryBounds.offset(clip.origin());
bsalomon96e02a82015-03-06 07:13:01 -0800258 }
cdalton846c0512016-05-13 10:25:00 -0700259 GrReducedClip::ReduceClipStack(*clip.clipStack(),
260 clipSpaceReduceQueryBounds,
261 &elements,
262 &genID,
263 &initialState,
264 &clipSpaceIBounds,
265 &requiresAA);
266 if (elements.isEmpty()) {
bsalomonbd2bbe42016-07-08 07:36:42 -0700267 if (GrReducedClip::kAllOut_InitialState == initialState) {
cdalton846c0512016-05-13 10:25:00 -0700268 return false;
bsalomonbd2bbe42016-07-08 07:36:42 -0700269 } else {
270 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
271 scissorSpaceIBounds.offset(-clip.origin());
msarettba3880f2016-07-12 18:48:21 -0700272 if (!SkRect::Make(scissorSpaceIBounds).contains(devBounds)) {
bsalomonbd2bbe42016-07-08 07:36:42 -0700273 out->makeScissored(scissorSpaceIBounds);
274 }
275 return true;
cdalton846c0512016-05-13 10:25:00 -0700276 }
277 }
cdalton93a379b2016-05-11 13:58:08 -0700278
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000279 // An element count of 4 was chosen because of the common pattern in Blink of:
280 // isect RR
281 // diff RR
282 // isect convex_poly
283 // isect convex_poly
284 // when drawing rounded div borders. This could probably be tuned based on a
285 // configuration's relative costs of switching RTs to generate a mask vs
286 // longer shaders.
bsalomon0ba8c242015-10-07 09:20:28 -0700287 if (elements.count() <= kMaxAnalyticElements) {
joshualitt44701df2015-02-23 14:44:57 -0800288 SkVector clipToRTOffset = { SkIntToScalar(-clip.origin().fX),
289 SkIntToScalar(-clip.origin().fY) };
cdaltonede75742015-11-11 15:27:57 -0800290 // When there are multiple samples we want to do per-sample clipping, not compute a
291 // fractional pixel coverage.
robertphillips976f5f02016-06-03 10:59:20 -0700292 bool disallowAnalyticAA = drawContext->isStencilBufferMultisampled();
293 if (disallowAnalyticAA && !drawContext->numColorSamples()) {
cdalton3ccf2e72016-05-06 09:41:16 -0700294 // With a single color sample, any coverage info is lost from color once it hits the
295 // color buffer anyway, so we may as well use coverage AA if nothing else in the pipe
296 // is multisampled.
robertphillips59cf61a2016-07-13 09:18:21 -0700297 disallowAnalyticAA = useHWAA || hasUserStencilSettings;
cdalton3ccf2e72016-05-06 09:41:16 -0700298 }
bungeman06ca8ec2016-06-09 08:01:03 -0700299 sk_sp<GrFragmentProcessor> clipFP;
bsalomonbd2bbe42016-07-08 07:36:42 -0700300 if (requiresAA &&
301 get_analytic_clip_processor(elements, disallowAnalyticAA, clipToRTOffset, devBounds,
302 &clipFP)) {
mtklein217daa72014-07-02 12:55:21 -0700303 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
joshualitt44701df2015-02-23 14:44:57 -0800304 scissorSpaceIBounds.offset(-clip.origin());
msarettba3880f2016-07-12 18:48:21 -0700305 if (!SkRect::Make(scissorSpaceIBounds).contains(devBounds)) {
bungeman06ca8ec2016-06-09 08:01:03 -0700306 out->makeScissoredFPBased(std::move(clipFP), scissorSpaceIBounds);
msarettba3880f2016-07-12 18:48:21 -0700307 return true;
commit-bot@chromium.orge5a041c2014-03-07 19:43:43 +0000308 }
msarettba3880f2016-07-12 18:48:21 -0700309 out->makeFPBased(std::move(clipFP), SkRect::Make(scissorSpaceIBounds));
commit-bot@chromium.org65ee5f42014-02-04 17:49:48 +0000310 return true;
311 }
312 }
bsalomon@google.comd3066bd2014-02-03 20:09:56 +0000313
cdaltonede75742015-11-11 15:27:57 -0800314 // If the stencil buffer is multisampled we can use it to do everything.
robertphillips976f5f02016-06-03 10:59:20 -0700315 if (!drawContext->isStencilBufferMultisampled() && requiresAA) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700316 sk_sp<GrTexture> result;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000317
joshualitt8059eb92014-12-29 15:10:07 -0800318 // The top-left of the mask corresponds to the top-left corner of the bounds.
319 SkVector clipToMaskOffset = {
320 SkIntToScalar(-clipSpaceIBounds.fLeft),
321 SkIntToScalar(-clipSpaceIBounds.fTop)
322 };
323
robertphillips59cf61a2016-07-13 09:18:21 -0700324 if (UseSWOnlyPath(context, hasUserStencilSettings, drawContext,
robertphillips976f5f02016-06-03 10:59:20 -0700325 clipToMaskOffset, elements)) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000326 // The clip geometry is complex enough that it will be more efficient to create it
327 // entirely in software
robertphillips976f5f02016-06-03 10:59:20 -0700328 result = CreateSoftwareClipMask(context->textureProvider(),
robertphillipsc99b8f02016-05-15 07:53:35 -0700329 genID,
330 initialState,
331 elements,
332 clipToMaskOffset,
333 clipSpaceIBounds);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000334 } else {
robertphillips976f5f02016-06-03 10:59:20 -0700335 result = CreateAlphaClipMask(context,
robertphillipsc99b8f02016-05-15 07:53:35 -0700336 genID,
337 initialState,
338 elements,
339 clipToMaskOffset,
340 clipSpaceIBounds);
robertphillips391395d2016-03-02 09:26:36 -0800341 // If createAlphaClipMask fails it means UseSWOnlyPath has a bug
robertphillips3f7357f2015-10-27 07:17:33 -0700342 SkASSERT(result);
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000343 }
344
bsalomon49f085d2014-09-05 13:34:00 -0700345 if (result) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000346 // The mask's top left coord should be pinned to the rounded-out top left corner of
347 // clipSpace bounds. We determine the mask's position WRT to the render target here.
348 SkIRect rtSpaceMaskBounds = clipSpaceIBounds;
joshualitt44701df2015-02-23 14:44:57 -0800349 rtSpaceMaskBounds.offset(-clip.origin());
bsalomon6cc90062016-07-08 11:31:22 -0700350 out->makeFPBased(create_fp_for_mask(result.get(), rtSpaceMaskBounds),
351 SkRect::Make(rtSpaceMaskBounds));
robertphillips@google.comf294b772012-04-27 14:29:26 +0000352 return true;
353 }
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000354 // if alpha clip mask creation fails fall through to the non-AA code paths
robertphillips@google.comf294b772012-04-27 14:29:26 +0000355 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000356
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000357 // use the stencil clip if we can't represent the clip as a rectangle.
joshualitt44701df2015-02-23 14:44:57 -0800358 SkIPoint clipSpaceToStencilSpaceOffset = -clip.origin();
robertphillips976f5f02016-06-03 10:59:20 -0700359 CreateStencilClipMask(context,
360 drawContext,
361 genID,
362 initialState,
363 elements,
364 clipSpaceIBounds,
365 clipSpaceToStencilSpaceOffset);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000366
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000367 // This must occur after createStencilClipMask. That function may change the scissor. Also, it
368 // only guarantees that the stencil mask is correct within the bounds it was passed, so we must
369 // use both stencil and scissor test to the bounds for the final draw.
370 SkIRect scissorSpaceIBounds(clipSpaceIBounds);
371 scissorSpaceIBounds.offset(clipSpaceToStencilSpaceOffset);
msarettba3880f2016-07-12 18:48:21 -0700372 out->makeScissoredStencil(scissorSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000373 return true;
374}
375
robertphillips391395d2016-03-02 09:26:36 -0800376static bool stencil_element(GrDrawContext* dc,
cdalton846c0512016-05-13 10:25:00 -0700377 const GrFixedClip& clip,
cdalton93a379b2016-05-11 13:58:08 -0700378 const GrUserStencilSettings* ss,
robertphillips391395d2016-03-02 09:26:36 -0800379 const SkMatrix& viewMatrix,
380 const SkClipStack::Element* element) {
robertphillips86c60752016-03-02 08:43:13 -0800381
382 // TODO: Draw rrects directly here.
383 switch (element->getType()) {
384 case Element::kEmpty_Type:
385 SkDEBUGFAIL("Should never get here with an empty element.");
386 break;
robertphillips391395d2016-03-02 09:26:36 -0800387 case Element::kRect_Type:
cdalton862cff32016-05-12 15:09:48 -0700388 return dc->drawContextPriv().drawAndStencilRect(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800389 element->getOp(),
390 element->isInverseFilled(),
391 element->isAA(),
392 viewMatrix, element->getRect());
393 break;
robertphillips86c60752016-03-02 08:43:13 -0800394 default: {
395 SkPath path;
396 element->asPath(&path);
397 if (path.isInverseFillType()) {
398 path.toggleInverseFillType();
399 }
400
cdalton862cff32016-05-12 15:09:48 -0700401 return dc->drawContextPriv().drawAndStencilPath(clip, ss,
robertphillips391395d2016-03-02 09:26:36 -0800402 element->getOp(),
403 element->isInverseFilled(),
404 element->isAA(), viewMatrix, path);
robertphillips86c60752016-03-02 08:43:13 -0800405 break;
406 }
407 }
robertphillips391395d2016-03-02 09:26:36 -0800408
409 return false;
410}
411
412static void draw_element(GrDrawContext* dc,
413 const GrClip& clip, // TODO: can this just always be WideOpen?
414 const GrPaint &paint,
415 const SkMatrix& viewMatrix,
416 const SkClipStack::Element* element) {
417
418 // TODO: Draw rrects directly here.
419 switch (element->getType()) {
420 case Element::kEmpty_Type:
421 SkDEBUGFAIL("Should never get here with an empty element.");
422 break;
423 case Element::kRect_Type:
424 dc->drawRect(clip, paint, viewMatrix, element->getRect());
425 break;
426 default: {
427 SkPath path;
428 element->asPath(&path);
429 if (path.isInverseFillType()) {
430 path.toggleInverseFillType();
431 }
432
bsalomon6663acf2016-05-10 09:14:17 -0700433 dc->drawPath(clip, paint, viewMatrix, path, GrStyle::SimpleFill());
robertphillips391395d2016-03-02 09:26:36 -0800434 break;
435 }
436 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000437}
438
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000439////////////////////////////////////////////////////////////////////////////////
bsalomon473addf2015-10-02 07:49:05 -0700440// Create a 8-bit clip mask in alpha
441
442static void GetClipMaskKey(int32_t clipGenID, const SkIRect& bounds, GrUniqueKey* key) {
443 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain();
444 GrUniqueKey::Builder builder(key, kDomain, 3);
445 builder[0] = clipGenID;
446 builder[1] = SkToU16(bounds.fLeft) | (SkToU16(bounds.fRight) << 16);
447 builder[2] = SkToU16(bounds.fTop) | (SkToU16(bounds.fBottom) << 16);
448}
449
robertphillipsc99b8f02016-05-15 07:53:35 -0700450sk_sp<GrTexture> GrClipMaskManager::CreateAlphaClipMask(GrContext* context,
451 int32_t elementsGenID,
452 GrReducedClip::InitialState initialState,
453 const GrReducedClip::ElementList& elements,
454 const SkVector& clipToMaskOffset,
455 const SkIRect& clipSpaceIBounds) {
robertphillips391395d2016-03-02 09:26:36 -0800456 GrResourceProvider* resourceProvider = context->resourceProvider();
bsalomon473addf2015-10-02 07:49:05 -0700457 GrUniqueKey key;
458 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
459 if (GrTexture* texture = resourceProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700460 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000461 }
462
robertphillips544b9aa2015-10-28 11:01:41 -0700463 // There's no texture in the cache. Let's try to allocate it then.
robertphillipsc99b8f02016-05-15 07:53:35 -0700464 GrPixelConfig config = kRGBA_8888_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800465 if (context->caps()->isConfigRenderable(kAlpha_8_GrPixelConfig, false)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700466 config = kAlpha_8_GrPixelConfig;
robertphillips391395d2016-03-02 09:26:36 -0800467 }
468
robertphillipsc99b8f02016-05-15 07:53:35 -0700469 sk_sp<GrDrawContext> dc(context->newDrawContext(SkBackingFit::kApprox,
470 clipSpaceIBounds.width(),
471 clipSpaceIBounds.height(),
472 config));
robertphillips391395d2016-03-02 09:26:36 -0800473 if (!dc) {
474 return nullptr;
475 }
robertphillipsc99b8f02016-05-15 07:53:35 -0700476
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000477 // The texture may be larger than necessary, this rect represents the part of the texture
478 // we populate with a rasterization of the clip.
479 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
480
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000481 // The scratch texture that we are drawing into can be substantially larger than the mask. Only
482 // clear the part that we care about.
robertphillips391395d2016-03-02 09:26:36 -0800483 dc->clear(&maskSpaceIBounds,
484 GrReducedClip::kAllIn_InitialState == initialState ? 0xffffffff : 0x00000000,
485 true);
skia.committer@gmail.comd9f75032012-11-09 02:01:24 +0000486
robertphillips391395d2016-03-02 09:26:36 -0800487 // Set the matrix so that rendered clip elements are transformed to mask space from clip
488 // space.
489 const SkMatrix translate = SkMatrix::MakeTrans(clipToMaskOffset.fX, clipToMaskOffset.fY);
490
491 // It is important that we use maskSpaceIBounds as the stencil rect in the below loop.
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000492 // The second pass that zeros the stencil buffer renders the rect maskSpaceIBounds so the first
493 // pass must not set values outside of this bounds or stencil values outside the rect won't be
494 // cleared.
joshualitt9853cce2014-11-17 14:22:48 -0800495
robertphillips@google.comf294b772012-04-27 14:29:26 +0000496 // walk through each clip element and perform its set op
tfarinabf54e492014-10-23 17:47:18 -0700497 for (GrReducedClip::ElementList::Iter iter = elements.headIter(); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000498 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000499 SkRegion::Op op = element->getOp();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000500 bool invert = element->isInverseFilled();
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000501 if (invert || SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
cdalton846c0512016-05-13 10:25:00 -0700502 GrFixedClip clip(maskSpaceIBounds);
cdalton862cff32016-05-12 15:09:48 -0700503
robertphillips391395d2016-03-02 09:26:36 -0800504 // draw directly into the result with the stencil set to make the pixels affected
505 // by the clip shape be non-zero.
cdalton93a379b2016-05-11 13:58:08 -0700506 static constexpr GrUserStencilSettings kStencilInElement(
507 GrUserStencilSettings::StaticInit<
508 0xffff,
509 GrUserStencilTest::kAlways,
510 0xffff,
511 GrUserStencilOp::kReplace,
512 GrUserStencilOp::kReplace,
513 0xffff>()
514 );
cdalton862cff32016-05-12 15:09:48 -0700515 if (!stencil_element(dc.get(), clip, &kStencilInElement,
robertphillips391395d2016-03-02 09:26:36 -0800516 translate, element)) {
robertphillips391395d2016-03-02 09:26:36 -0800517 return nullptr;
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000518 }
bsalomon@google.com7b7cdd12012-11-07 16:17:24 +0000519
robertphillips391395d2016-03-02 09:26:36 -0800520 // Draw to the exterior pixels (those with a zero stencil value).
cdalton93a379b2016-05-11 13:58:08 -0700521 static constexpr GrUserStencilSettings kDrawOutsideElement(
522 GrUserStencilSettings::StaticInit<
523 0x0000,
524 GrUserStencilTest::kEqual,
525 0xffff,
526 GrUserStencilOp::kZero,
527 GrUserStencilOp::kZero,
528 0xffff>()
529 );
cdalton862cff32016-05-12 15:09:48 -0700530 if (!dc->drawContextPriv().drawAndStencilRect(clip, &kDrawOutsideElement,
robertphillips391395d2016-03-02 09:26:36 -0800531 op, !invert, false,
532 translate,
533 SkRect::Make(clipSpaceIBounds))) {
robertphillips391395d2016-03-02 09:26:36 -0800534 return nullptr;
bsalomon@google.comb68addd2012-12-14 13:36:53 +0000535 }
robertphillips@google.comf294b772012-04-27 14:29:26 +0000536 } else {
robertphillips8b8f36f2016-03-02 08:53:12 -0800537 // all the remaining ops can just be directly draw into the accumulation buffer
robertphillips391395d2016-03-02 09:26:36 -0800538 GrPaint paint;
539 paint.setAntiAlias(element->isAA());
540 paint.setCoverageSetOpXPFactory(op, false);
541
cdalton846c0512016-05-13 10:25:00 -0700542 draw_element(dc.get(), GrNoClip(), paint, translate, element);
robertphillips@google.comf294b772012-04-27 14:29:26 +0000543 }
544 }
545
robertphillipsc99b8f02016-05-15 07:53:35 -0700546 sk_sp<GrTexture> texture(dc->asTexture());
547 SkASSERT(texture);
548 texture->resourcePriv().setUniqueKey(key);
549 return texture;
robertphillips@google.comf294b772012-04-27 14:29:26 +0000550}
551
552////////////////////////////////////////////////////////////////////////////////
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000553// Create a 1-bit clip mask in the stencil buffer. 'devClipBounds' are in device
robertphillips@google.comf8d904a2012-07-31 12:18:16 +0000554// (as opposed to canvas) coordinates
robertphillips976f5f02016-06-03 10:59:20 -0700555bool GrClipMaskManager::CreateStencilClipMask(GrContext* context,
556 GrDrawContext* drawContext,
joshualitt9853cce2014-11-17 14:22:48 -0800557 int32_t elementsGenID,
tfarinabf54e492014-10-23 17:47:18 -0700558 GrReducedClip::InitialState initialState,
559 const GrReducedClip::ElementList& elements,
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000560 const SkIRect& clipSpaceIBounds,
561 const SkIPoint& clipSpaceToStencilOffset) {
robertphillips976f5f02016-06-03 10:59:20 -0700562 SkASSERT(drawContext);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000563
robertphillips976f5f02016-06-03 10:59:20 -0700564 GrStencilAttachment* stencilAttachment = context->resourceProvider()->attachStencilAttachment(
565 drawContext->accessRenderTarget());
halcanary96fcdcc2015-08-27 07:41:13 -0700566 if (nullptr == stencilAttachment) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000567 return false;
568 }
569
robertphillips976f5f02016-06-03 10:59:20 -0700570 // TODO: these need to be swapped over to using a StencilAttachmentProxy
egdaniel8dc7c3a2015-04-16 11:22:42 -0700571 if (stencilAttachment->mustRenderClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset)) {
572 stencilAttachment->setLastClip(elementsGenID, clipSpaceIBounds, clipSpaceToStencilOffset);
bsalomon@google.com137f1342013-05-29 21:27:53 +0000573 // Set the matrix so that rendered clip elements are transformed from clip to stencil space.
574 SkVector translate = {
575 SkIntToScalar(clipSpaceToStencilOffset.fX),
576 SkIntToScalar(clipSpaceToStencilOffset.fY)
577 };
joshualitt8059eb92014-12-29 15:10:07 -0800578 SkMatrix viewMatrix;
579 viewMatrix.setTranslate(translate);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000580
bsalomon@google.com9f131742012-12-13 20:43:56 +0000581 // We set the current clip to the bounds so that our recursive draws are scissored to them.
582 SkIRect stencilSpaceIBounds(clipSpaceIBounds);
583 stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
cdalton846c0512016-05-13 10:25:00 -0700584 GrFixedClip clip(stencilSpaceIBounds);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000585
robertphillips976f5f02016-06-03 10:59:20 -0700586 drawContext->drawContextPriv().clearStencilClip(
587 stencilSpaceIBounds,
588 GrReducedClip::kAllIn_InitialState == initialState);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000589
590 // walk through each clip element and perform its set op
591 // with the existing clip.
tfarinabf54e492014-10-23 17:47:18 -0700592 for (GrReducedClip::ElementList::Iter iter(elements.headIter()); iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000593 const Element* element = iter.get();
csmartdalton656dbe42016-06-10 12:32:57 -0700594 bool useHWAA = element->isAA() && drawContext->isStencilBufferMultisampled();
joshualitt9853cce2014-11-17 14:22:48 -0800595
tomhudson@google.com8afae612012-08-14 15:03:35 +0000596 bool fillInverted = false;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000597 // enabled at bottom of loop
bsalomon6cc90062016-07-08 11:31:22 -0700598 clip.disableStencilClip();
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000599
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000600 // This will be used to determine whether the clip shape can be rendered into the
601 // stencil with arbitrary stencil settings.
602 GrPathRenderer::StencilSupport stencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000603
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000604 SkRegion::Op op = element->getOp();
robertphillips@google.comf294b772012-04-27 14:29:26 +0000605
halcanary96fcdcc2015-08-27 07:41:13 -0700606 GrPathRenderer* pr = nullptr;
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000607 SkPath clipPath;
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000608 if (Element::kRect_Type == element->getType()) {
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000609 stencilSupport = GrPathRenderer::kNoRestriction_StencilSupport;
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000610 fillInverted = false;
tomhudson@google.com8afae612012-08-14 15:03:35 +0000611 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000612 element->asPath(&clipPath);
613 fillInverted = clipPath.isInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000614 if (fillInverted) {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000615 clipPath.toggleInverseFillType();
robertphillips@google.come79f3202014-02-11 16:30:21 +0000616 }
robertphillips68737822015-10-29 12:12:21 -0700617
bsalomon8acedde2016-06-24 10:42:16 -0700618 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips68737822015-10-29 12:12:21 -0700619 GrPathRenderer::CanDrawPathArgs canDrawArgs;
robertphillips976f5f02016-06-03 10:59:20 -0700620 canDrawArgs.fShaderCaps = context->caps()->shaderCaps();
robertphillips68737822015-10-29 12:12:21 -0700621 canDrawArgs.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700622 canDrawArgs.fShape = &shape;
robertphillips68737822015-10-29 12:12:21 -0700623 canDrawArgs.fAntiAlias = false;
robertphillips976f5f02016-06-03 10:59:20 -0700624 canDrawArgs.fHasUserStencilSettings = false;
625 canDrawArgs.fIsStencilBufferMSAA = drawContext->isStencilBufferMultisampled();
robertphillips68737822015-10-29 12:12:21 -0700626
robertphillips976f5f02016-06-03 10:59:20 -0700627 pr = context->drawingManager()->getPathRenderer(canDrawArgs, false,
628 GrPathRendererChain::kStencilOnly_DrawType,
629 &stencilSupport);
630 if (!pr) {
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000631 return false;
632 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000633 }
634
bsalomon@google.com45a15f52012-12-10 19:10:17 +0000635 bool canRenderDirectToStencil =
636 GrPathRenderer::kNoRestriction_StencilSupport == stencilSupport;
cdalton93a379b2016-05-11 13:58:08 -0700637 bool drawDirectToClip; // Given the renderer, the element,
638 // fill rule, and set operation should
639 // we render the element directly to
640 // stencil bit used for clipping.
641 GrUserStencilSettings const* const* stencilPasses =
642 GrStencilSettings::GetClipPasses(op, canRenderDirectToStencil, fillInverted,
643 &drawDirectToClip);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000644
645 // draw the element to the client stencil bits if necessary
cdalton93a379b2016-05-11 13:58:08 -0700646 if (!drawDirectToClip) {
647 static constexpr GrUserStencilSettings kDrawToStencil(
648 GrUserStencilSettings::StaticInit<
649 0x0000,
650 GrUserStencilTest::kAlways,
651 0xffff,
652 GrUserStencilOp::kIncMaybeClamp,
653 GrUserStencilOp::kIncMaybeClamp,
654 0xffff>()
655 );
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000656 if (Element::kRect_Type == element->getType()) {
csmartdalton656dbe42016-06-10 12:32:57 -0700657 drawContext->drawContextPriv().stencilRect(clip, &kDrawToStencil, useHWAA,
658 viewMatrix, element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000659 } else {
commit-bot@chromium.orge5b2af92014-02-16 13:25:24 +0000660 if (!clipPath.isEmpty()) {
bsalomon8acedde2016-06-24 10:42:16 -0700661 GrShape shape(clipPath, GrStyle::SimpleFill());
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000662 if (canRenderDirectToStencil) {
robertphillips976f5f02016-06-03 10:59:20 -0700663 GrPaint paint;
bungeman06ca8ec2016-06-09 08:01:03 -0700664 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700665 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700666
667 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700668 args.fResourceProvider = context->resourceProvider();
669 args.fPaint = &paint;
670 args.fUserStencilSettings = &kDrawToStencil;
671 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700672 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700673 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700674 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700675 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700676 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700677 pr->drawPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000678 } else {
bsalomon0aff2fa2015-07-31 06:48:27 -0700679 GrPathRenderer::StencilPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700680 args.fResourceProvider = context->resourceProvider();
681 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700682 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700683 args.fViewMatrix = &viewMatrix;
robertphillips976f5f02016-06-03 10:59:20 -0700684 args.fIsAA = element->isAA();
bsalomon8acedde2016-06-24 10:42:16 -0700685 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700686 pr->stencilPath(args);
commit-bot@chromium.org19dd0172013-08-05 13:28:55 +0000687 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000688 }
689 }
690 }
691
692 // now we modify the clip bit by rendering either the clip
693 // element directly or a bounding rect of the entire clip.
cdalton93a379b2016-05-11 13:58:08 -0700694 for (GrUserStencilSettings const* const* pass = stencilPasses; *pass; ++pass) {
joshualitt9853cce2014-11-17 14:22:48 -0800695
cdalton93a379b2016-05-11 13:58:08 -0700696 if (drawDirectToClip) {
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000697 if (Element::kRect_Type == element->getType()) {
bsalomon6cc90062016-07-08 11:31:22 -0700698 clip.enableStencilClip(element->getRect().makeOffset(translate.fX,
699 translate.fY));
csmartdalton656dbe42016-06-10 12:32:57 -0700700 drawContext->drawContextPriv().stencilRect(clip, *pass, useHWAA, viewMatrix,
701 element->getRect());
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000702 } else {
bsalomon8acedde2016-06-24 10:42:16 -0700703 GrShape shape(clipPath, GrStyle::SimpleFill());
robertphillips976f5f02016-06-03 10:59:20 -0700704 GrPaint paint;
bsalomon6cc90062016-07-08 11:31:22 -0700705 SkRect bounds = clipPath.getBounds();
706 bounds.offset(translate.fX, translate.fY);
707 clip.enableStencilClip(bounds);
bungeman06ca8ec2016-06-09 08:01:03 -0700708 paint.setXPFactory(GrDisableColorXPFactory::Make());
robertphillips976f5f02016-06-03 10:59:20 -0700709 paint.setAntiAlias(element->isAA());
bsalomon0aff2fa2015-07-31 06:48:27 -0700710 GrPathRenderer::DrawPathArgs args;
robertphillips976f5f02016-06-03 10:59:20 -0700711 args.fResourceProvider = context->resourceProvider();
712 args.fPaint = &paint;
713 args.fUserStencilSettings = *pass;
714 args.fDrawContext = drawContext;
cdalton862cff32016-05-12 15:09:48 -0700715 args.fClip = &clip;
bsalomon0aff2fa2015-07-31 06:48:27 -0700716 args.fViewMatrix = &viewMatrix;
bsalomon8acedde2016-06-24 10:42:16 -0700717 args.fShape = &shape;
bsalomon0aff2fa2015-07-31 06:48:27 -0700718 args.fAntiAlias = false;
brianosman0e3c5542016-04-13 13:56:21 -0700719 args.fGammaCorrect = false;
bsalomon0aff2fa2015-07-31 06:48:27 -0700720 pr->drawPath(args);
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000721 }
722 } else {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000723 // The view matrix is setup to do clip space -> stencil space translation, so
724 // draw rect in clip space.
bsalomon6cc90062016-07-08 11:31:22 -0700725 SkRect bounds = SkRect::Make(clipSpaceIBounds);
726 bounds.offset(translate.fX, translate.fY);
727 clip.enableStencilClip(bounds);
csmartdalton656dbe42016-06-10 12:32:57 -0700728 drawContext->drawContextPriv().stencilRect(clip, *pass, false, viewMatrix,
729 SkRect::Make(clipSpaceIBounds));
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000730 }
731 }
732 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000733 }
robertphillips@google.com1e945b72012-04-16 18:03:03 +0000734 return true;
735}
736
bsalomon@google.com411dad02012-06-05 20:24:20 +0000737////////////////////////////////////////////////////////////////////////////////
robertphillipsc99b8f02016-05-15 07:53:35 -0700738sk_sp<GrTexture> GrClipMaskManager::CreateSoftwareClipMask(
robertphillips0152d732016-05-20 06:38:43 -0700739 GrTextureProvider* texProvider,
robertphillipsc99b8f02016-05-15 07:53:35 -0700740 int32_t elementsGenID,
741 GrReducedClip::InitialState initialState,
742 const GrReducedClip::ElementList& elements,
743 const SkVector& clipToMaskOffset,
744 const SkIRect& clipSpaceIBounds) {
bsalomon473addf2015-10-02 07:49:05 -0700745 GrUniqueKey key;
746 GetClipMaskKey(elementsGenID, clipSpaceIBounds, &key);
robertphillips0152d732016-05-20 06:38:43 -0700747 if (GrTexture* texture = texProvider->findAndRefTextureByUniqueKey(key)) {
robertphillipsc99b8f02016-05-15 07:53:35 -0700748 return sk_sp<GrTexture>(texture);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000749 }
750
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000751 // The mask texture may be larger than necessary. We round out the clip space bounds and pin
752 // the top left corner of the resulting rect to the top left of the texture.
753 SkIRect maskSpaceIBounds = SkIRect::MakeWH(clipSpaceIBounds.width(), clipSpaceIBounds.height());
754
robertphillips0152d732016-05-20 06:38:43 -0700755 GrSWMaskHelper helper(texProvider);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000756
joshualitt8059eb92014-12-29 15:10:07 -0800757 // Set the matrix so that rendered clip elements are transformed to mask space from clip
758 // space.
759 SkMatrix translate;
760 translate.setTranslate(clipToMaskOffset);
joshualitt9853cce2014-11-17 14:22:48 -0800761
robertphillips98377402016-05-13 05:47:23 -0700762 helper.init(maskSpaceIBounds, &translate);
tfarinabf54e492014-10-23 17:47:18 -0700763 helper.clear(GrReducedClip::kAllIn_InitialState == initialState ? 0xFF : 0x00);
sugoi@google.com12b4e272012-12-06 20:13:11 +0000764
tfarinabf54e492014-10-23 17:47:18 -0700765 for (GrReducedClip::ElementList::Iter iter(elements.headIter()) ; iter.get(); iter.next()) {
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000766 const Element* element = iter.get();
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000767 SkRegion::Op op = element->getOp();
robertphillips@google.comfa662942012-05-17 12:20:22 +0000768
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000769 if (SkRegion::kIntersect_Op == op || SkRegion::kReverseDifference_Op == op) {
770 // Intersect and reverse difference require modifying pixels outside of the geometry
771 // that is being "drawn". In both cases we erase all the pixels outside of the geometry
772 // but leave the pixels inside the geometry alone. For reverse difference we invert all
773 // the pixels before clearing the ones outside the geometry.
robertphillips@google.comfa662942012-05-17 12:20:22 +0000774 if (SkRegion::kReverseDifference_Op == op) {
reed@google.com44699382013-10-31 17:28:30 +0000775 SkRect temp = SkRect::Make(clipSpaceIBounds);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000776 // invert the entire scene
robertphillips98377402016-05-13 05:47:23 -0700777 helper.drawRect(temp, SkRegion::kXOR_Op, false, 0xFF);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000778 }
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000779 SkPath clipPath;
780 element->asPath(&clipPath);
781 clipPath.toggleInverseFillType();
bsalomon8acedde2016-06-24 10:42:16 -0700782 GrShape shape(clipPath, GrStyle::SimpleFill());
783 helper.drawShape(shape, SkRegion::kReplace_Op, element->isAA(), 0x00);
robertphillips@google.comfa662942012-05-17 12:20:22 +0000784 continue;
785 }
786
787 // The other ops (union, xor, diff) only affect pixels inside
788 // the geometry so they can just be drawn normally
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000789 if (Element::kRect_Type == element->getType()) {
robertphillips98377402016-05-13 05:47:23 -0700790 helper.drawRect(element->getRect(), op, element->isAA(), 0xFF);
bsalomon@google.com8182fa02012-12-04 14:06:06 +0000791 } else {
commit-bot@chromium.org5c056392014-02-17 19:50:02 +0000792 SkPath path;
793 element->asPath(&path);
bsalomon8acedde2016-06-24 10:42:16 -0700794 GrShape shape(path, GrStyle::SimpleFill());
795 helper.drawShape(shape, op, element->isAA(), 0xFF);
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000796 }
797 }
798
krajcevskiad1dc582014-06-10 15:06:47 -0700799 // Allocate clip mask texture
robertphillips391395d2016-03-02 09:26:36 -0800800 GrSurfaceDesc desc;
801 desc.fWidth = clipSpaceIBounds.width();
802 desc.fHeight = clipSpaceIBounds.height();
803 desc.fConfig = kAlpha_8_GrPixelConfig;
804
robertphillips0152d732016-05-20 06:38:43 -0700805 sk_sp<GrTexture> result(texProvider->createApproxTexture(desc));
robertphillips391395d2016-03-02 09:26:36 -0800806 if (!result) {
halcanary96fcdcc2015-08-27 07:41:13 -0700807 return nullptr;
krajcevskiad1dc582014-06-10 15:06:47 -0700808 }
robertphillips391395d2016-03-02 09:26:36 -0800809 result->resourcePriv().setUniqueKey(key);
810
robertphillipsc99b8f02016-05-15 07:53:35 -0700811 helper.toTexture(result.get());
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000812
bsalomon@google.com4c2443e2012-12-06 20:58:57 +0000813 return result;
robertphillips@google.com6b70a7b2012-05-11 15:32:48 +0000814}